1 //===-- RISCVISelDAGToDAG.cpp - A dag to dag inst selector for RISCV ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines an instruction selector for the RISCV target.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVISelDAGToDAG.h"
14 #include "MCTargetDesc/RISCVMCTargetDesc.h"
15 #include "MCTargetDesc/RISCVMatInt.h"
16 #include "RISCVISelLowering.h"
17 #include "RISCVMachineFunctionInfo.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/IR/IntrinsicsRISCV.h"
20 #include "llvm/Support/Alignment.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/KnownBits.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/raw_ostream.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "riscv-isel"
29 
30 namespace llvm {
31 namespace RISCV {
32 #define GET_RISCVVSSEGTable_IMPL
33 #define GET_RISCVVLSEGTable_IMPL
34 #define GET_RISCVVLXSEGTable_IMPL
35 #define GET_RISCVVSXSEGTable_IMPL
36 #define GET_RISCVVLETable_IMPL
37 #define GET_RISCVVSETable_IMPL
38 #define GET_RISCVVLXTable_IMPL
39 #define GET_RISCVVSXTable_IMPL
40 #define GET_RISCVMaskedPseudosTable_IMPL
41 #include "RISCVGenSearchableTables.inc"
42 } // namespace RISCV
43 } // namespace llvm
44 
45 void RISCVDAGToDAGISel::PreprocessISelDAG() {
46   for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
47                                        E = CurDAG->allnodes_end();
48        I != E;) {
49     SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues.
50 
51     // Convert integer SPLAT_VECTOR to VMV_V_X_VL and floating-point
52     // SPLAT_VECTOR to VFMV_V_F_VL to reduce isel burden.
53     if (N->getOpcode() == ISD::SPLAT_VECTOR) {
54       MVT VT = N->getSimpleValueType(0);
55       unsigned Opc =
56           VT.isInteger() ? RISCVISD::VMV_V_X_VL : RISCVISD::VFMV_V_F_VL;
57       SDLoc DL(N);
58       SDValue VL = CurDAG->getRegister(RISCV::X0, Subtarget->getXLenVT());
59       SDValue Result = CurDAG->getNode(Opc, DL, VT, CurDAG->getUNDEF(VT),
60                                        N->getOperand(0), VL);
61 
62       --I;
63       CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
64       ++I;
65       CurDAG->DeleteNode(N);
66       continue;
67     }
68 
69     // Lower SPLAT_VECTOR_SPLIT_I64 to two scalar stores and a stride 0 vector
70     // load. Done after lowering and combining so that we have a chance to
71     // optimize this to VMV_V_X_VL when the upper bits aren't needed.
72     if (N->getOpcode() != RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL)
73       continue;
74 
75     assert(N->getNumOperands() == 4 && "Unexpected number of operands");
76     MVT VT = N->getSimpleValueType(0);
77     SDValue Passthru = N->getOperand(0);
78     SDValue Lo = N->getOperand(1);
79     SDValue Hi = N->getOperand(2);
80     SDValue VL = N->getOperand(3);
81     assert(VT.getVectorElementType() == MVT::i64 && VT.isScalableVector() &&
82            Lo.getValueType() == MVT::i32 && Hi.getValueType() == MVT::i32 &&
83            "Unexpected VTs!");
84     MachineFunction &MF = CurDAG->getMachineFunction();
85     RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
86     SDLoc DL(N);
87 
88     // We use the same frame index we use for moving two i32s into 64-bit FPR.
89     // This is an analogous operation.
90     int FI = FuncInfo->getMoveF64FrameIndex(MF);
91     MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, FI);
92     const TargetLowering &TLI = CurDAG->getTargetLoweringInfo();
93     SDValue StackSlot =
94         CurDAG->getFrameIndex(FI, TLI.getPointerTy(CurDAG->getDataLayout()));
95 
96     SDValue Chain = CurDAG->getEntryNode();
97     Lo = CurDAG->getStore(Chain, DL, Lo, StackSlot, MPI, Align(8));
98 
99     SDValue OffsetSlot =
100         CurDAG->getMemBasePlusOffset(StackSlot, TypeSize::Fixed(4), DL);
101     Hi = CurDAG->getStore(Chain, DL, Hi, OffsetSlot, MPI.getWithOffset(4),
102                           Align(8));
103 
104     Chain = CurDAG->getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
105 
106     SDVTList VTs = CurDAG->getVTList({VT, MVT::Other});
107     SDValue IntID =
108         CurDAG->getTargetConstant(Intrinsic::riscv_vlse, DL, MVT::i64);
109     SDValue Ops[] = {Chain,
110                      IntID,
111                      Passthru,
112                      StackSlot,
113                      CurDAG->getRegister(RISCV::X0, MVT::i64),
114                      VL};
115 
116     SDValue Result = CurDAG->getMemIntrinsicNode(
117         ISD::INTRINSIC_W_CHAIN, DL, VTs, Ops, MVT::i64, MPI, Align(8),
118         MachineMemOperand::MOLoad);
119 
120     // We're about to replace all uses of the SPLAT_VECTOR_SPLIT_I64 with the
121     // vlse we created.  This will cause general havok on the dag because
122     // anything below the conversion could be folded into other existing nodes.
123     // To avoid invalidating 'I', back it up to the convert node.
124     --I;
125     CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
126 
127     // Now that we did that, the node is dead.  Increment the iterator to the
128     // next node to process, then delete N.
129     ++I;
130     CurDAG->DeleteNode(N);
131   }
132 }
133 
134 void RISCVDAGToDAGISel::PostprocessISelDAG() {
135   HandleSDNode Dummy(CurDAG->getRoot());
136   SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
137 
138   bool MadeChange = false;
139   while (Position != CurDAG->allnodes_begin()) {
140     SDNode *N = &*--Position;
141     // Skip dead nodes and any non-machine opcodes.
142     if (N->use_empty() || !N->isMachineOpcode())
143       continue;
144 
145     MadeChange |= doPeepholeSExtW(N);
146     MadeChange |= doPeepholeLoadStoreADDI(N);
147     MadeChange |= doPeepholeMaskedRVV(N);
148   }
149 
150   CurDAG->setRoot(Dummy.getValue());
151 
152   if (MadeChange)
153     CurDAG->RemoveDeadNodes();
154 }
155 
156 static SDNode *selectImmWithConstantPool(SelectionDAG *CurDAG, const SDLoc &DL,
157                                          const MVT VT, int64_t Imm,
158                                          const RISCVSubtarget &Subtarget) {
159   assert(VT == MVT::i64 && "Expecting MVT::i64");
160   const RISCVTargetLowering *TLI = Subtarget.getTargetLowering();
161   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(CurDAG->getConstantPool(
162       ConstantInt::get(EVT(VT).getTypeForEVT(*CurDAG->getContext()), Imm), VT));
163   SDValue Addr = TLI->getAddr(CP, *CurDAG);
164   SDValue Offset = CurDAG->getTargetConstant(0, DL, VT);
165   // Since there is no data race, the chain can be the entry node.
166   SDNode *Load = CurDAG->getMachineNode(RISCV::LD, DL, VT, Addr, Offset,
167                                         CurDAG->getEntryNode());
168   MachineFunction &MF = CurDAG->getMachineFunction();
169   MachineMemOperand *MemOp = MF.getMachineMemOperand(
170       MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad,
171       LLT(VT), CP->getAlign());
172   CurDAG->setNodeMemRefs(cast<MachineSDNode>(Load), {MemOp});
173   return Load;
174 }
175 
176 static SDNode *selectImm(SelectionDAG *CurDAG, const SDLoc &DL, const MVT VT,
177                          int64_t Imm, const RISCVSubtarget &Subtarget) {
178   MVT XLenVT = Subtarget.getXLenVT();
179   RISCVMatInt::InstSeq Seq =
180       RISCVMatInt::generateInstSeq(Imm, Subtarget.getFeatureBits());
181 
182   // If Imm is expensive to build, then we put it into constant pool.
183   if (Subtarget.useConstantPoolForLargeInts() &&
184       Seq.size() > Subtarget.getMaxBuildIntsCost())
185     return selectImmWithConstantPool(CurDAG, DL, VT, Imm, Subtarget);
186 
187   SDNode *Result = nullptr;
188   SDValue SrcReg = CurDAG->getRegister(RISCV::X0, XLenVT);
189   for (RISCVMatInt::Inst &Inst : Seq) {
190     SDValue SDImm = CurDAG->getTargetConstant(Inst.Imm, DL, XLenVT);
191     if (Inst.Opc == RISCV::LUI)
192       Result = CurDAG->getMachineNode(RISCV::LUI, DL, XLenVT, SDImm);
193     else if (Inst.Opc == RISCV::ADD_UW)
194       Result = CurDAG->getMachineNode(RISCV::ADD_UW, DL, XLenVT, SrcReg,
195                                       CurDAG->getRegister(RISCV::X0, XLenVT));
196     else if (Inst.Opc == RISCV::SH1ADD || Inst.Opc == RISCV::SH2ADD ||
197              Inst.Opc == RISCV::SH3ADD)
198       Result = CurDAG->getMachineNode(Inst.Opc, DL, XLenVT, SrcReg, SrcReg);
199     else
200       Result = CurDAG->getMachineNode(Inst.Opc, DL, XLenVT, SrcReg, SDImm);
201 
202     // Only the first instruction has X0 as its source.
203     SrcReg = SDValue(Result, 0);
204   }
205 
206   return Result;
207 }
208 
209 static SDValue createTupleImpl(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs,
210                                unsigned RegClassID, unsigned SubReg0) {
211   assert(Regs.size() >= 2 && Regs.size() <= 8);
212 
213   SDLoc DL(Regs[0]);
214   SmallVector<SDValue, 8> Ops;
215 
216   Ops.push_back(CurDAG.getTargetConstant(RegClassID, DL, MVT::i32));
217 
218   for (unsigned I = 0; I < Regs.size(); ++I) {
219     Ops.push_back(Regs[I]);
220     Ops.push_back(CurDAG.getTargetConstant(SubReg0 + I, DL, MVT::i32));
221   }
222   SDNode *N =
223       CurDAG.getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
224   return SDValue(N, 0);
225 }
226 
227 static SDValue createM1Tuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs,
228                              unsigned NF) {
229   static const unsigned RegClassIDs[] = {
230       RISCV::VRN2M1RegClassID, RISCV::VRN3M1RegClassID, RISCV::VRN4M1RegClassID,
231       RISCV::VRN5M1RegClassID, RISCV::VRN6M1RegClassID, RISCV::VRN7M1RegClassID,
232       RISCV::VRN8M1RegClassID};
233 
234   return createTupleImpl(CurDAG, Regs, RegClassIDs[NF - 2], RISCV::sub_vrm1_0);
235 }
236 
237 static SDValue createM2Tuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs,
238                              unsigned NF) {
239   static const unsigned RegClassIDs[] = {RISCV::VRN2M2RegClassID,
240                                          RISCV::VRN3M2RegClassID,
241                                          RISCV::VRN4M2RegClassID};
242 
243   return createTupleImpl(CurDAG, Regs, RegClassIDs[NF - 2], RISCV::sub_vrm2_0);
244 }
245 
246 static SDValue createM4Tuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs,
247                              unsigned NF) {
248   return createTupleImpl(CurDAG, Regs, RISCV::VRN2M4RegClassID,
249                          RISCV::sub_vrm4_0);
250 }
251 
252 static SDValue createTuple(SelectionDAG &CurDAG, ArrayRef<SDValue> Regs,
253                            unsigned NF, RISCVII::VLMUL LMUL) {
254   switch (LMUL) {
255   default:
256     llvm_unreachable("Invalid LMUL.");
257   case RISCVII::VLMUL::LMUL_F8:
258   case RISCVII::VLMUL::LMUL_F4:
259   case RISCVII::VLMUL::LMUL_F2:
260   case RISCVII::VLMUL::LMUL_1:
261     return createM1Tuple(CurDAG, Regs, NF);
262   case RISCVII::VLMUL::LMUL_2:
263     return createM2Tuple(CurDAG, Regs, NF);
264   case RISCVII::VLMUL::LMUL_4:
265     return createM4Tuple(CurDAG, Regs, NF);
266   }
267 }
268 
269 void RISCVDAGToDAGISel::addVectorLoadStoreOperands(
270     SDNode *Node, unsigned Log2SEW, const SDLoc &DL, unsigned CurOp,
271     bool IsMasked, bool IsStridedOrIndexed, SmallVectorImpl<SDValue> &Operands,
272     bool IsLoad, MVT *IndexVT) {
273   SDValue Chain = Node->getOperand(0);
274   SDValue Glue;
275 
276   SDValue Base;
277   SelectBaseAddr(Node->getOperand(CurOp++), Base);
278   Operands.push_back(Base); // Base pointer.
279 
280   if (IsStridedOrIndexed) {
281     Operands.push_back(Node->getOperand(CurOp++)); // Index.
282     if (IndexVT)
283       *IndexVT = Operands.back()->getSimpleValueType(0);
284   }
285 
286   if (IsMasked) {
287     // Mask needs to be copied to V0.
288     SDValue Mask = Node->getOperand(CurOp++);
289     Chain = CurDAG->getCopyToReg(Chain, DL, RISCV::V0, Mask, SDValue());
290     Glue = Chain.getValue(1);
291     Operands.push_back(CurDAG->getRegister(RISCV::V0, Mask.getValueType()));
292   }
293   SDValue VL;
294   selectVLOp(Node->getOperand(CurOp++), VL);
295   Operands.push_back(VL);
296 
297   MVT XLenVT = Subtarget->getXLenVT();
298   SDValue SEWOp = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT);
299   Operands.push_back(SEWOp);
300 
301   // Masked load has the tail policy argument.
302   if (IsMasked && IsLoad) {
303     // Policy must be a constant.
304     uint64_t Policy = Node->getConstantOperandVal(CurOp++);
305     SDValue PolicyOp = CurDAG->getTargetConstant(Policy, DL, XLenVT);
306     Operands.push_back(PolicyOp);
307   }
308 
309   Operands.push_back(Chain); // Chain.
310   if (Glue)
311     Operands.push_back(Glue);
312 }
313 
314 void RISCVDAGToDAGISel::selectVLSEG(SDNode *Node, bool IsMasked,
315                                     bool IsStrided) {
316   SDLoc DL(Node);
317   unsigned NF = Node->getNumValues() - 1;
318   MVT VT = Node->getSimpleValueType(0);
319   unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
320   RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
321 
322   unsigned CurOp = 2;
323   SmallVector<SDValue, 8> Operands;
324   if (IsMasked) {
325     SmallVector<SDValue, 8> Regs(Node->op_begin() + CurOp,
326                                  Node->op_begin() + CurOp + NF);
327     SDValue MaskedOff = createTuple(*CurDAG, Regs, NF, LMUL);
328     Operands.push_back(MaskedOff);
329     CurOp += NF;
330   }
331 
332   addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
333                              Operands, /*IsLoad=*/true);
334 
335   const RISCV::VLSEGPseudo *P =
336       RISCV::getVLSEGPseudo(NF, IsMasked, IsStrided, /*FF*/ false, Log2SEW,
337                             static_cast<unsigned>(LMUL));
338   MachineSDNode *Load =
339       CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped, MVT::Other, Operands);
340 
341   if (auto *MemOp = dyn_cast<MemSDNode>(Node))
342     CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
343 
344   SDValue SuperReg = SDValue(Load, 0);
345   for (unsigned I = 0; I < NF; ++I) {
346     unsigned SubRegIdx = RISCVTargetLowering::getSubregIndexByMVT(VT, I);
347     ReplaceUses(SDValue(Node, I),
348                 CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, SuperReg));
349   }
350 
351   ReplaceUses(SDValue(Node, NF), SDValue(Load, 1));
352   CurDAG->RemoveDeadNode(Node);
353 }
354 
355 void RISCVDAGToDAGISel::selectVLSEGFF(SDNode *Node, bool IsMasked) {
356   SDLoc DL(Node);
357   unsigned NF = Node->getNumValues() - 2; // Do not count VL and Chain.
358   MVT VT = Node->getSimpleValueType(0);
359   MVT XLenVT = Subtarget->getXLenVT();
360   unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
361   RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
362 
363   unsigned CurOp = 2;
364   SmallVector<SDValue, 7> Operands;
365   if (IsMasked) {
366     SmallVector<SDValue, 8> Regs(Node->op_begin() + CurOp,
367                                  Node->op_begin() + CurOp + NF);
368     SDValue MaskedOff = createTuple(*CurDAG, Regs, NF, LMUL);
369     Operands.push_back(MaskedOff);
370     CurOp += NF;
371   }
372 
373   addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
374                              /*IsStridedOrIndexed*/ false, Operands,
375                              /*IsLoad=*/true);
376 
377   const RISCV::VLSEGPseudo *P =
378       RISCV::getVLSEGPseudo(NF, IsMasked, /*Strided*/ false, /*FF*/ true,
379                             Log2SEW, static_cast<unsigned>(LMUL));
380   MachineSDNode *Load = CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped,
381                                                MVT::Other, MVT::Glue, Operands);
382   SDNode *ReadVL = CurDAG->getMachineNode(RISCV::PseudoReadVL, DL, XLenVT,
383                                           /*Glue*/ SDValue(Load, 2));
384 
385   if (auto *MemOp = dyn_cast<MemSDNode>(Node))
386     CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
387 
388   SDValue SuperReg = SDValue(Load, 0);
389   for (unsigned I = 0; I < NF; ++I) {
390     unsigned SubRegIdx = RISCVTargetLowering::getSubregIndexByMVT(VT, I);
391     ReplaceUses(SDValue(Node, I),
392                 CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, SuperReg));
393   }
394 
395   ReplaceUses(SDValue(Node, NF), SDValue(ReadVL, 0));   // VL
396   ReplaceUses(SDValue(Node, NF + 1), SDValue(Load, 1)); // Chain
397   CurDAG->RemoveDeadNode(Node);
398 }
399 
400 void RISCVDAGToDAGISel::selectVLXSEG(SDNode *Node, bool IsMasked,
401                                      bool IsOrdered) {
402   SDLoc DL(Node);
403   unsigned NF = Node->getNumValues() - 1;
404   MVT VT = Node->getSimpleValueType(0);
405   unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
406   RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
407 
408   unsigned CurOp = 2;
409   SmallVector<SDValue, 8> Operands;
410   if (IsMasked) {
411     SmallVector<SDValue, 8> Regs(Node->op_begin() + CurOp,
412                                  Node->op_begin() + CurOp + NF);
413     SDValue MaskedOff = createTuple(*CurDAG, Regs, NF, LMUL);
414     Operands.push_back(MaskedOff);
415     CurOp += NF;
416   }
417 
418   MVT IndexVT;
419   addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
420                              /*IsStridedOrIndexed*/ true, Operands,
421                              /*IsLoad=*/true, &IndexVT);
422 
423   assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
424          "Element count mismatch");
425 
426   RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT);
427   unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
428   if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
429     report_fatal_error("The V extension does not support EEW=64 for index "
430                        "values when XLEN=32");
431   }
432   const RISCV::VLXSEGPseudo *P = RISCV::getVLXSEGPseudo(
433       NF, IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL),
434       static_cast<unsigned>(IndexLMUL));
435   MachineSDNode *Load =
436       CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped, MVT::Other, Operands);
437 
438   if (auto *MemOp = dyn_cast<MemSDNode>(Node))
439     CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
440 
441   SDValue SuperReg = SDValue(Load, 0);
442   for (unsigned I = 0; I < NF; ++I) {
443     unsigned SubRegIdx = RISCVTargetLowering::getSubregIndexByMVT(VT, I);
444     ReplaceUses(SDValue(Node, I),
445                 CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, SuperReg));
446   }
447 
448   ReplaceUses(SDValue(Node, NF), SDValue(Load, 1));
449   CurDAG->RemoveDeadNode(Node);
450 }
451 
452 void RISCVDAGToDAGISel::selectVSSEG(SDNode *Node, bool IsMasked,
453                                     bool IsStrided) {
454   SDLoc DL(Node);
455   unsigned NF = Node->getNumOperands() - 4;
456   if (IsStrided)
457     NF--;
458   if (IsMasked)
459     NF--;
460   MVT VT = Node->getOperand(2)->getSimpleValueType(0);
461   unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
462   RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
463   SmallVector<SDValue, 8> Regs(Node->op_begin() + 2, Node->op_begin() + 2 + NF);
464   SDValue StoreVal = createTuple(*CurDAG, Regs, NF, LMUL);
465 
466   SmallVector<SDValue, 8> Operands;
467   Operands.push_back(StoreVal);
468   unsigned CurOp = 2 + NF;
469 
470   addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
471                              Operands);
472 
473   const RISCV::VSSEGPseudo *P = RISCV::getVSSEGPseudo(
474       NF, IsMasked, IsStrided, Log2SEW, static_cast<unsigned>(LMUL));
475   MachineSDNode *Store =
476       CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0), Operands);
477 
478   if (auto *MemOp = dyn_cast<MemSDNode>(Node))
479     CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()});
480 
481   ReplaceNode(Node, Store);
482 }
483 
484 void RISCVDAGToDAGISel::selectVSXSEG(SDNode *Node, bool IsMasked,
485                                      bool IsOrdered) {
486   SDLoc DL(Node);
487   unsigned NF = Node->getNumOperands() - 5;
488   if (IsMasked)
489     --NF;
490   MVT VT = Node->getOperand(2)->getSimpleValueType(0);
491   unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
492   RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
493   SmallVector<SDValue, 8> Regs(Node->op_begin() + 2, Node->op_begin() + 2 + NF);
494   SDValue StoreVal = createTuple(*CurDAG, Regs, NF, LMUL);
495 
496   SmallVector<SDValue, 8> Operands;
497   Operands.push_back(StoreVal);
498   unsigned CurOp = 2 + NF;
499 
500   MVT IndexVT;
501   addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
502                              /*IsStridedOrIndexed*/ true, Operands,
503                              /*IsLoad=*/false, &IndexVT);
504 
505   assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
506          "Element count mismatch");
507 
508   RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT);
509   unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
510   if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
511     report_fatal_error("The V extension does not support EEW=64 for index "
512                        "values when XLEN=32");
513   }
514   const RISCV::VSXSEGPseudo *P = RISCV::getVSXSEGPseudo(
515       NF, IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL),
516       static_cast<unsigned>(IndexLMUL));
517   MachineSDNode *Store =
518       CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0), Operands);
519 
520   if (auto *MemOp = dyn_cast<MemSDNode>(Node))
521     CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()});
522 
523   ReplaceNode(Node, Store);
524 }
525 
526 void RISCVDAGToDAGISel::selectVSETVLI(SDNode *Node) {
527   if (!Subtarget->hasVInstructions())
528     return;
529 
530   assert((Node->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
531           Node->getOpcode() == ISD::INTRINSIC_WO_CHAIN) &&
532          "Unexpected opcode");
533 
534   SDLoc DL(Node);
535   MVT XLenVT = Subtarget->getXLenVT();
536 
537   bool HasChain = Node->getOpcode() == ISD::INTRINSIC_W_CHAIN;
538   unsigned IntNoOffset = HasChain ? 1 : 0;
539   unsigned IntNo = Node->getConstantOperandVal(IntNoOffset);
540 
541   assert((IntNo == Intrinsic::riscv_vsetvli ||
542           IntNo == Intrinsic::riscv_vsetvlimax ||
543           IntNo == Intrinsic::riscv_vsetvli_opt ||
544           IntNo == Intrinsic::riscv_vsetvlimax_opt) &&
545          "Unexpected vsetvli intrinsic");
546 
547   bool VLMax = IntNo == Intrinsic::riscv_vsetvlimax ||
548                IntNo == Intrinsic::riscv_vsetvlimax_opt;
549   unsigned Offset = IntNoOffset + (VLMax ? 1 : 2);
550 
551   assert(Node->getNumOperands() == Offset + 2 &&
552          "Unexpected number of operands");
553 
554   unsigned SEW =
555       RISCVVType::decodeVSEW(Node->getConstantOperandVal(Offset) & 0x7);
556   RISCVII::VLMUL VLMul = static_cast<RISCVII::VLMUL>(
557       Node->getConstantOperandVal(Offset + 1) & 0x7);
558 
559   unsigned VTypeI = RISCVVType::encodeVTYPE(VLMul, SEW, /*TailAgnostic*/ true,
560                                             /*MaskAgnostic*/ false);
561   SDValue VTypeIOp = CurDAG->getTargetConstant(VTypeI, DL, XLenVT);
562 
563   SmallVector<EVT, 2> VTs = {XLenVT};
564   if (HasChain)
565     VTs.push_back(MVT::Other);
566 
567   SDValue VLOperand;
568   unsigned Opcode = RISCV::PseudoVSETVLI;
569   if (VLMax) {
570     VLOperand = CurDAG->getRegister(RISCV::X0, XLenVT);
571     Opcode = RISCV::PseudoVSETVLIX0;
572   } else {
573     VLOperand = Node->getOperand(IntNoOffset + 1);
574 
575     if (auto *C = dyn_cast<ConstantSDNode>(VLOperand)) {
576       uint64_t AVL = C->getZExtValue();
577       if (isUInt<5>(AVL)) {
578         SDValue VLImm = CurDAG->getTargetConstant(AVL, DL, XLenVT);
579         SmallVector<SDValue, 3> Ops = {VLImm, VTypeIOp};
580         if (HasChain)
581           Ops.push_back(Node->getOperand(0));
582         ReplaceNode(
583             Node, CurDAG->getMachineNode(RISCV::PseudoVSETIVLI, DL, VTs, Ops));
584         return;
585       }
586     }
587   }
588 
589   SmallVector<SDValue, 3> Ops = {VLOperand, VTypeIOp};
590   if (HasChain)
591     Ops.push_back(Node->getOperand(0));
592 
593   ReplaceNode(Node, CurDAG->getMachineNode(Opcode, DL, VTs, Ops));
594 }
595 
596 void RISCVDAGToDAGISel::Select(SDNode *Node) {
597   // If we have a custom node, we have already selected.
598   if (Node->isMachineOpcode()) {
599     LLVM_DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n");
600     Node->setNodeId(-1);
601     return;
602   }
603 
604   // Instruction Selection not handled by the auto-generated tablegen selection
605   // should be handled here.
606   unsigned Opcode = Node->getOpcode();
607   MVT XLenVT = Subtarget->getXLenVT();
608   SDLoc DL(Node);
609   MVT VT = Node->getSimpleValueType(0);
610 
611   switch (Opcode) {
612   case ISD::Constant: {
613     auto *ConstNode = cast<ConstantSDNode>(Node);
614     if (VT == XLenVT && ConstNode->isZero()) {
615       SDValue New =
616           CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, RISCV::X0, XLenVT);
617       ReplaceNode(Node, New.getNode());
618       return;
619     }
620     int64_t Imm = ConstNode->getSExtValue();
621     // If the upper XLen-16 bits are not used, try to convert this to a simm12
622     // by sign extending bit 15.
623     if (isUInt<16>(Imm) && isInt<12>(SignExtend64(Imm, 16)) &&
624         hasAllHUsers(Node))
625       Imm = SignExtend64(Imm, 16);
626     // If the upper 32-bits are not used try to convert this into a simm32 by
627     // sign extending bit 32.
628     if (!isInt<32>(Imm) && isUInt<32>(Imm) && hasAllWUsers(Node))
629       Imm = SignExtend64(Imm, 32);
630 
631     ReplaceNode(Node, selectImm(CurDAG, DL, VT, Imm, *Subtarget));
632     return;
633   }
634   case ISD::FrameIndex: {
635     SDValue Imm = CurDAG->getTargetConstant(0, DL, XLenVT);
636     int FI = cast<FrameIndexSDNode>(Node)->getIndex();
637     SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
638     ReplaceNode(Node, CurDAG->getMachineNode(RISCV::ADDI, DL, VT, TFI, Imm));
639     return;
640   }
641   case ISD::SRL: {
642     // Optimize (srl (and X, C2), C) ->
643     //          (srli (slli X, (XLen-C3), (XLen-C3) + C)
644     // Where C2 is a mask with C3 trailing ones.
645     // Taking into account that the C2 may have had lower bits unset by
646     // SimplifyDemandedBits. This avoids materializing the C2 immediate.
647     // This pattern occurs when type legalizing right shifts for types with
648     // less than XLen bits.
649     auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
650     if (!N1C)
651       break;
652     SDValue N0 = Node->getOperand(0);
653     if (N0.getOpcode() != ISD::AND || !N0.hasOneUse() ||
654         !isa<ConstantSDNode>(N0.getOperand(1)))
655       break;
656     unsigned ShAmt = N1C->getZExtValue();
657     uint64_t Mask = N0.getConstantOperandVal(1);
658     Mask |= maskTrailingOnes<uint64_t>(ShAmt);
659     if (!isMask_64(Mask))
660       break;
661     unsigned TrailingOnes = countTrailingOnes(Mask);
662     // 32 trailing ones should use srliw via tablegen pattern.
663     if (TrailingOnes == 32 || ShAmt >= TrailingOnes)
664       break;
665     unsigned LShAmt = Subtarget->getXLen() - TrailingOnes;
666     SDNode *SLLI =
667         CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0->getOperand(0),
668                                CurDAG->getTargetConstant(LShAmt, DL, VT));
669     SDNode *SRLI = CurDAG->getMachineNode(
670         RISCV::SRLI, DL, VT, SDValue(SLLI, 0),
671         CurDAG->getTargetConstant(LShAmt + ShAmt, DL, VT));
672     ReplaceNode(Node, SRLI);
673     return;
674   }
675   case ISD::SRA: {
676     // Optimize (sra (sext_inreg X, i16), C) ->
677     //          (srai (slli X, (XLen-16), (XLen-16) + C)
678     // And      (sra (sext_inreg X, i8), C) ->
679     //          (srai (slli X, (XLen-8), (XLen-8) + C)
680     // This can occur when Zbb is enabled, which makes sext_inreg i16/i8 legal.
681     // This transform matches the code we get without Zbb. The shifts are more
682     // compressible, and this can help expose CSE opportunities in the sdiv by
683     // constant optimization.
684     auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
685     if (!N1C)
686       break;
687     SDValue N0 = Node->getOperand(0);
688     if (N0.getOpcode() != ISD::SIGN_EXTEND_INREG || !N0.hasOneUse())
689       break;
690     unsigned ShAmt = N1C->getZExtValue();
691     unsigned ExtSize =
692         cast<VTSDNode>(N0.getOperand(1))->getVT().getSizeInBits();
693     // ExtSize of 32 should use sraiw via tablegen pattern.
694     if (ExtSize >= 32 || ShAmt >= ExtSize)
695       break;
696     unsigned LShAmt = Subtarget->getXLen() - ExtSize;
697     SDNode *SLLI =
698         CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0->getOperand(0),
699                                CurDAG->getTargetConstant(LShAmt, DL, VT));
700     SDNode *SRAI = CurDAG->getMachineNode(
701         RISCV::SRAI, DL, VT, SDValue(SLLI, 0),
702         CurDAG->getTargetConstant(LShAmt + ShAmt, DL, VT));
703     ReplaceNode(Node, SRAI);
704     return;
705   }
706   case ISD::AND: {
707     auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
708     if (!N1C)
709       break;
710 
711     SDValue N0 = Node->getOperand(0);
712 
713     bool LeftShift = N0.getOpcode() == ISD::SHL;
714     if (!LeftShift && N0.getOpcode() != ISD::SRL)
715       break;
716 
717     auto *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
718     if (!C)
719       break;
720     uint64_t C2 = C->getZExtValue();
721     unsigned XLen = Subtarget->getXLen();
722     if (!C2 || C2 >= XLen)
723       break;
724 
725     uint64_t C1 = N1C->getZExtValue();
726 
727     // Keep track of whether this is an andi.
728     bool IsANDI = isInt<12>(N1C->getSExtValue());
729 
730     // Clear irrelevant bits in the mask.
731     if (LeftShift)
732       C1 &= maskTrailingZeros<uint64_t>(C2);
733     else
734       C1 &= maskTrailingOnes<uint64_t>(XLen - C2);
735 
736     // Some transforms should only be done if the shift has a single use or
737     // the AND would become (srli (slli X, 32), 32)
738     bool OneUseOrZExtW = N0.hasOneUse() || C1 == UINT64_C(0xFFFFFFFF);
739 
740     SDValue X = N0.getOperand(0);
741 
742     // Turn (and (srl x, c2) c1) -> (srli (slli x, c3-c2), c3) if c1 is a mask
743     // with c3 leading zeros.
744     if (!LeftShift && isMask_64(C1)) {
745       uint64_t C3 = XLen - (64 - countLeadingZeros(C1));
746       if (C2 < C3) {
747         // If the number of leading zeros is C2+32 this can be SRLIW.
748         if (C2 + 32 == C3) {
749           SDNode *SRLIW =
750               CurDAG->getMachineNode(RISCV::SRLIW, DL, XLenVT, X,
751                                      CurDAG->getTargetConstant(C2, DL, XLenVT));
752           ReplaceNode(Node, SRLIW);
753           return;
754         }
755 
756         // (and (srl (sexti32 Y), c2), c1) -> (srliw (sraiw Y, 31), c3 - 32) if
757         // c1 is a mask with c3 leading zeros and c2 >= 32 and c3-c2==1.
758         //
759         // This pattern occurs when (i32 (srl (sra 31), c3 - 32)) is type
760         // legalized and goes through DAG combine.
761         if (C2 >= 32 && (C3 - C2) == 1 && N0.hasOneUse() &&
762             X.getOpcode() == ISD::SIGN_EXTEND_INREG &&
763             cast<VTSDNode>(X.getOperand(1))->getVT() == MVT::i32) {
764           SDNode *SRAIW =
765               CurDAG->getMachineNode(RISCV::SRAIW, DL, XLenVT, X.getOperand(0),
766                                      CurDAG->getTargetConstant(31, DL, XLenVT));
767           SDNode *SRLIW = CurDAG->getMachineNode(
768               RISCV::SRLIW, DL, XLenVT, SDValue(SRAIW, 0),
769               CurDAG->getTargetConstant(C3 - 32, DL, XLenVT));
770           ReplaceNode(Node, SRLIW);
771           return;
772         }
773 
774         // (srli (slli x, c3-c2), c3).
775         // Skip it in order to select sraiw.
776         bool Skip = Subtarget->hasStdExtZba() && C3 == 32 &&
777                     X.getOpcode() == ISD::SIGN_EXTEND_INREG &&
778                     cast<VTSDNode>(X.getOperand(1))->getVT() == MVT::i32;
779         if (OneUseOrZExtW && !IsANDI && !Skip) {
780           SDNode *SLLI = CurDAG->getMachineNode(
781               RISCV::SLLI, DL, XLenVT, X,
782               CurDAG->getTargetConstant(C3 - C2, DL, XLenVT));
783           SDNode *SRLI =
784               CurDAG->getMachineNode(RISCV::SRLI, DL, XLenVT, SDValue(SLLI, 0),
785                                      CurDAG->getTargetConstant(C3, DL, XLenVT));
786           ReplaceNode(Node, SRLI);
787           return;
788         }
789       }
790     }
791 
792     // Turn (and (shl x, c2), c1) -> (srli (slli c2+c3), c3) if c1 is a mask
793     // shifted by c2 bits with c3 leading zeros.
794     if (LeftShift && isShiftedMask_64(C1)) {
795       uint64_t C3 = XLen - (64 - countLeadingZeros(C1));
796 
797       if (C2 + C3 < XLen &&
798           C1 == (maskTrailingOnes<uint64_t>(XLen - (C2 + C3)) << C2)) {
799         // Use slli.uw when possible.
800         if ((XLen - (C2 + C3)) == 32 && Subtarget->hasStdExtZba()) {
801           SDNode *SLLI_UW =
802               CurDAG->getMachineNode(RISCV::SLLI_UW, DL, XLenVT, X,
803                                      CurDAG->getTargetConstant(C2, DL, XLenVT));
804           ReplaceNode(Node, SLLI_UW);
805           return;
806         }
807 
808         // (srli (slli c2+c3), c3)
809         if (OneUseOrZExtW && !IsANDI) {
810           SDNode *SLLI = CurDAG->getMachineNode(
811               RISCV::SLLI, DL, XLenVT, X,
812               CurDAG->getTargetConstant(C2 + C3, DL, XLenVT));
813           SDNode *SRLI =
814               CurDAG->getMachineNode(RISCV::SRLI, DL, XLenVT, SDValue(SLLI, 0),
815                                      CurDAG->getTargetConstant(C3, DL, XLenVT));
816           ReplaceNode(Node, SRLI);
817           return;
818         }
819       }
820     }
821 
822     // Turn (and (shr x, c2), c1) -> (slli (srli x, c2+c3), c3) if c1 is a
823     // shifted mask with c2 leading zeros and c3 trailing zeros.
824     if (!LeftShift && isShiftedMask_64(C1)) {
825       uint64_t Leading = XLen - (64 - countLeadingZeros(C1));
826       uint64_t C3 = countTrailingZeros(C1);
827       if (Leading == C2 && C2 + C3 < XLen && OneUseOrZExtW && !IsANDI) {
828         SDNode *SRLI = CurDAG->getMachineNode(
829             RISCV::SRLI, DL, XLenVT, X,
830             CurDAG->getTargetConstant(C2 + C3, DL, XLenVT));
831         SDNode *SLLI =
832             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLI, 0),
833                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
834         ReplaceNode(Node, SLLI);
835         return;
836       }
837       // If the leading zero count is C2+32, we can use SRLIW instead of SRLI.
838       if (Leading > 32 && (Leading - 32) == C2 && C2 + C3 < 32 &&
839           OneUseOrZExtW && !IsANDI) {
840         SDNode *SRLIW = CurDAG->getMachineNode(
841             RISCV::SRLIW, DL, XLenVT, X,
842             CurDAG->getTargetConstant(C2 + C3, DL, XLenVT));
843         SDNode *SLLI =
844             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLIW, 0),
845                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
846         ReplaceNode(Node, SLLI);
847         return;
848       }
849     }
850 
851     // Turn (and (shl x, c2), c1) -> (slli (srli x, c3-c2), c3) if c1 is a
852     // shifted mask with no leading zeros and c3 trailing zeros.
853     if (LeftShift && isShiftedMask_64(C1)) {
854       uint64_t Leading = XLen - (64 - countLeadingZeros(C1));
855       uint64_t C3 = countTrailingZeros(C1);
856       if (Leading == 0 && C2 < C3 && OneUseOrZExtW && !IsANDI) {
857         SDNode *SRLI = CurDAG->getMachineNode(
858             RISCV::SRLI, DL, XLenVT, X,
859             CurDAG->getTargetConstant(C3 - C2, DL, XLenVT));
860         SDNode *SLLI =
861             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLI, 0),
862                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
863         ReplaceNode(Node, SLLI);
864         return;
865       }
866       // If we have (32-C2) leading zeros, we can use SRLIW instead of SRLI.
867       if (C2 < C3 && Leading + C2 == 32 && OneUseOrZExtW && !IsANDI) {
868         SDNode *SRLIW = CurDAG->getMachineNode(
869             RISCV::SRLIW, DL, XLenVT, X,
870             CurDAG->getTargetConstant(C3 - C2, DL, XLenVT));
871         SDNode *SLLI =
872             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLIW, 0),
873                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
874         ReplaceNode(Node, SLLI);
875         return;
876       }
877     }
878 
879     break;
880   }
881   case ISD::MUL: {
882     // Special case for calculating (mul (and X, C2), C1) where the full product
883     // fits in XLen bits. We can shift X left by the number of leading zeros in
884     // C2 and shift C1 left by XLen-lzcnt(C2). This will ensure the final
885     // product has XLen trailing zeros, putting it in the output of MULHU. This
886     // can avoid materializing a constant in a register for C2.
887 
888     // RHS should be a constant.
889     auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
890     if (!N1C || !N1C->hasOneUse())
891       break;
892 
893     // LHS should be an AND with constant.
894     SDValue N0 = Node->getOperand(0);
895     if (N0.getOpcode() != ISD::AND || !isa<ConstantSDNode>(N0.getOperand(1)))
896       break;
897 
898     uint64_t C2 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
899 
900     // Constant should be a mask.
901     if (!isMask_64(C2))
902       break;
903 
904     // This should be the only use of the AND unless we will use
905     // (SRLI (SLLI X, 32), 32). We don't use a shift pair for other AND
906     // constants.
907     if (!N0.hasOneUse() && C2 != UINT64_C(0xFFFFFFFF))
908       break;
909 
910     // If this can be an ANDI, ZEXT.H or ZEXT.W we don't need to do this
911     // optimization.
912     if (isInt<12>(C2) ||
913         (C2 == UINT64_C(0xFFFF) &&
914          (Subtarget->hasStdExtZbb() || Subtarget->hasStdExtZbp())) ||
915         (C2 == UINT64_C(0xFFFFFFFF) && Subtarget->hasStdExtZba()))
916       break;
917 
918     // We need to shift left the AND input and C1 by a total of XLen bits.
919 
920     // How far left do we need to shift the AND input?
921     unsigned XLen = Subtarget->getXLen();
922     unsigned LeadingZeros = XLen - (64 - countLeadingZeros(C2));
923 
924     // The constant gets shifted by the remaining amount unless that would
925     // shift bits out.
926     uint64_t C1 = N1C->getZExtValue();
927     unsigned ConstantShift = XLen - LeadingZeros;
928     if (ConstantShift > (XLen - (64 - countLeadingZeros(C1))))
929       break;
930 
931     uint64_t ShiftedC1 = C1 << ConstantShift;
932     // If this RV32, we need to sign extend the constant.
933     if (XLen == 32)
934       ShiftedC1 = SignExtend64(ShiftedC1, 32);
935 
936     // Create (mulhu (slli X, lzcnt(C2)), C1 << (XLen - lzcnt(C2))).
937     SDNode *Imm = selectImm(CurDAG, DL, VT, ShiftedC1, *Subtarget);
938     SDNode *SLLI =
939         CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0.getOperand(0),
940                                CurDAG->getTargetConstant(LeadingZeros, DL, VT));
941     SDNode *MULHU = CurDAG->getMachineNode(RISCV::MULHU, DL, VT,
942                                            SDValue(SLLI, 0), SDValue(Imm, 0));
943     ReplaceNode(Node, MULHU);
944     return;
945   }
946   case ISD::INTRINSIC_WO_CHAIN: {
947     unsigned IntNo = Node->getConstantOperandVal(0);
948     switch (IntNo) {
949       // By default we do not custom select any intrinsic.
950     default:
951       break;
952     case Intrinsic::riscv_vmsgeu:
953     case Intrinsic::riscv_vmsge: {
954       SDValue Src1 = Node->getOperand(1);
955       SDValue Src2 = Node->getOperand(2);
956       bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu;
957       bool IsCmpUnsignedZero = false;
958       // Only custom select scalar second operand.
959       if (Src2.getValueType() != XLenVT)
960         break;
961       // Small constants are handled with patterns.
962       if (auto *C = dyn_cast<ConstantSDNode>(Src2)) {
963         int64_t CVal = C->getSExtValue();
964         if (CVal >= -15 && CVal <= 16) {
965           if (!IsUnsigned || CVal != 0)
966             break;
967           IsCmpUnsignedZero = true;
968         }
969       }
970       MVT Src1VT = Src1.getSimpleValueType();
971       unsigned VMSLTOpcode, VMNANDOpcode, VMSetOpcode;
972       switch (RISCVTargetLowering::getLMUL(Src1VT)) {
973       default:
974         llvm_unreachable("Unexpected LMUL!");
975 #define CASE_VMSLT_VMNAND_VMSET_OPCODES(lmulenum, suffix, suffix_b)            \
976   case RISCVII::VLMUL::lmulenum:                                               \
977     VMSLTOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix                 \
978                              : RISCV::PseudoVMSLT_VX_##suffix;                 \
979     VMNANDOpcode = RISCV::PseudoVMNAND_MM_##suffix;                            \
980     VMSetOpcode = RISCV::PseudoVMSET_M_##suffix_b;                             \
981     break;
982         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_F8, MF8, B1)
983         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_F4, MF4, B2)
984         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_F2, MF2, B4)
985         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_1, M1, B8)
986         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_2, M2, B16)
987         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_4, M4, B32)
988         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_8, M8, B64)
989 #undef CASE_VMSLT_VMNAND_VMSET_OPCODES
990       }
991       SDValue SEW = CurDAG->getTargetConstant(
992           Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT);
993       SDValue VL;
994       selectVLOp(Node->getOperand(3), VL);
995 
996       // If vmsgeu with 0 immediate, expand it to vmset.
997       if (IsCmpUnsignedZero) {
998         ReplaceNode(Node, CurDAG->getMachineNode(VMSetOpcode, DL, VT, VL, SEW));
999         return;
1000       }
1001 
1002       // Expand to
1003       // vmslt{u}.vx vd, va, x; vmnand.mm vd, vd, vd
1004       SDValue Cmp = SDValue(
1005           CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}),
1006           0);
1007       ReplaceNode(Node, CurDAG->getMachineNode(VMNANDOpcode, DL, VT,
1008                                                {Cmp, Cmp, VL, SEW}));
1009       return;
1010     }
1011     case Intrinsic::riscv_vmsgeu_mask:
1012     case Intrinsic::riscv_vmsge_mask: {
1013       SDValue Src1 = Node->getOperand(2);
1014       SDValue Src2 = Node->getOperand(3);
1015       bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu_mask;
1016       bool IsCmpUnsignedZero = false;
1017       // Only custom select scalar second operand.
1018       if (Src2.getValueType() != XLenVT)
1019         break;
1020       // Small constants are handled with patterns.
1021       if (auto *C = dyn_cast<ConstantSDNode>(Src2)) {
1022         int64_t CVal = C->getSExtValue();
1023         if (CVal >= -15 && CVal <= 16) {
1024           if (!IsUnsigned || CVal != 0)
1025             break;
1026           IsCmpUnsignedZero = true;
1027         }
1028       }
1029       MVT Src1VT = Src1.getSimpleValueType();
1030       unsigned VMSLTOpcode, VMSLTMaskOpcode, VMXOROpcode, VMANDNOpcode,
1031           VMOROpcode;
1032       switch (RISCVTargetLowering::getLMUL(Src1VT)) {
1033       default:
1034         llvm_unreachable("Unexpected LMUL!");
1035 #define CASE_VMSLT_OPCODES(lmulenum, suffix, suffix_b)                         \
1036   case RISCVII::VLMUL::lmulenum:                                               \
1037     VMSLTOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix                 \
1038                              : RISCV::PseudoVMSLT_VX_##suffix;                 \
1039     VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix##_MASK      \
1040                                  : RISCV::PseudoVMSLT_VX_##suffix##_MASK;      \
1041     break;
1042         CASE_VMSLT_OPCODES(LMUL_F8, MF8, B1)
1043         CASE_VMSLT_OPCODES(LMUL_F4, MF4, B2)
1044         CASE_VMSLT_OPCODES(LMUL_F2, MF2, B4)
1045         CASE_VMSLT_OPCODES(LMUL_1, M1, B8)
1046         CASE_VMSLT_OPCODES(LMUL_2, M2, B16)
1047         CASE_VMSLT_OPCODES(LMUL_4, M4, B32)
1048         CASE_VMSLT_OPCODES(LMUL_8, M8, B64)
1049 #undef CASE_VMSLT_OPCODES
1050       }
1051       // Mask operations use the LMUL from the mask type.
1052       switch (RISCVTargetLowering::getLMUL(VT)) {
1053       default:
1054         llvm_unreachable("Unexpected LMUL!");
1055 #define CASE_VMXOR_VMANDN_VMOR_OPCODES(lmulenum, suffix)                       \
1056   case RISCVII::VLMUL::lmulenum:                                               \
1057     VMXOROpcode = RISCV::PseudoVMXOR_MM_##suffix;                              \
1058     VMANDNOpcode = RISCV::PseudoVMANDN_MM_##suffix;                            \
1059     VMOROpcode = RISCV::PseudoVMOR_MM_##suffix;                                \
1060     break;
1061         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F8, MF8)
1062         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F4, MF4)
1063         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F2, MF2)
1064         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_1, M1)
1065         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_2, M2)
1066         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_4, M4)
1067         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_8, M8)
1068 #undef CASE_VMXOR_VMANDN_VMOR_OPCODES
1069       }
1070       SDValue SEW = CurDAG->getTargetConstant(
1071           Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT);
1072       SDValue MaskSEW = CurDAG->getTargetConstant(0, DL, XLenVT);
1073       SDValue VL;
1074       selectVLOp(Node->getOperand(5), VL);
1075       SDValue MaskedOff = Node->getOperand(1);
1076       SDValue Mask = Node->getOperand(4);
1077 
1078       // If vmsgeu_mask with 0 immediate, expand it to vmor mask, maskedoff.
1079       if (IsCmpUnsignedZero) {
1080         // We don't need vmor if the MaskedOff and the Mask are the same
1081         // value.
1082         if (Mask == MaskedOff) {
1083           ReplaceUses(Node, Mask.getNode());
1084           return;
1085         }
1086         ReplaceNode(Node,
1087                     CurDAG->getMachineNode(VMOROpcode, DL, VT,
1088                                            {Mask, MaskedOff, VL, MaskSEW}));
1089         return;
1090       }
1091 
1092       // If the MaskedOff value and the Mask are the same value use
1093       // vmslt{u}.vx vt, va, x;  vmandn.mm vd, vd, vt
1094       // This avoids needing to copy v0 to vd before starting the next sequence.
1095       if (Mask == MaskedOff) {
1096         SDValue Cmp = SDValue(
1097             CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}),
1098             0);
1099         ReplaceNode(Node, CurDAG->getMachineNode(VMANDNOpcode, DL, VT,
1100                                                  {Mask, Cmp, VL, MaskSEW}));
1101         return;
1102       }
1103 
1104       // Mask needs to be copied to V0.
1105       SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL,
1106                                            RISCV::V0, Mask, SDValue());
1107       SDValue Glue = Chain.getValue(1);
1108       SDValue V0 = CurDAG->getRegister(RISCV::V0, VT);
1109 
1110       // Otherwise use
1111       // vmslt{u}.vx vd, va, x, v0.t; vmxor.mm vd, vd, v0
1112       SDValue Cmp = SDValue(
1113           CurDAG->getMachineNode(VMSLTMaskOpcode, DL, VT,
1114                                  {MaskedOff, Src1, Src2, V0, VL, SEW, Glue}),
1115           0);
1116       ReplaceNode(Node, CurDAG->getMachineNode(VMXOROpcode, DL, VT,
1117                                                {Cmp, Mask, VL, MaskSEW}));
1118       return;
1119     }
1120     case Intrinsic::riscv_vsetvli_opt:
1121     case Intrinsic::riscv_vsetvlimax_opt:
1122       return selectVSETVLI(Node);
1123     }
1124     break;
1125   }
1126   case ISD::INTRINSIC_W_CHAIN: {
1127     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
1128     switch (IntNo) {
1129       // By default we do not custom select any intrinsic.
1130     default:
1131       break;
1132     case Intrinsic::riscv_vsetvli:
1133     case Intrinsic::riscv_vsetvlimax:
1134       return selectVSETVLI(Node);
1135     case Intrinsic::riscv_vlseg2:
1136     case Intrinsic::riscv_vlseg3:
1137     case Intrinsic::riscv_vlseg4:
1138     case Intrinsic::riscv_vlseg5:
1139     case Intrinsic::riscv_vlseg6:
1140     case Intrinsic::riscv_vlseg7:
1141     case Intrinsic::riscv_vlseg8: {
1142       selectVLSEG(Node, /*IsMasked*/ false, /*IsStrided*/ false);
1143       return;
1144     }
1145     case Intrinsic::riscv_vlseg2_mask:
1146     case Intrinsic::riscv_vlseg3_mask:
1147     case Intrinsic::riscv_vlseg4_mask:
1148     case Intrinsic::riscv_vlseg5_mask:
1149     case Intrinsic::riscv_vlseg6_mask:
1150     case Intrinsic::riscv_vlseg7_mask:
1151     case Intrinsic::riscv_vlseg8_mask: {
1152       selectVLSEG(Node, /*IsMasked*/ true, /*IsStrided*/ false);
1153       return;
1154     }
1155     case Intrinsic::riscv_vlsseg2:
1156     case Intrinsic::riscv_vlsseg3:
1157     case Intrinsic::riscv_vlsseg4:
1158     case Intrinsic::riscv_vlsseg5:
1159     case Intrinsic::riscv_vlsseg6:
1160     case Intrinsic::riscv_vlsseg7:
1161     case Intrinsic::riscv_vlsseg8: {
1162       selectVLSEG(Node, /*IsMasked*/ false, /*IsStrided*/ true);
1163       return;
1164     }
1165     case Intrinsic::riscv_vlsseg2_mask:
1166     case Intrinsic::riscv_vlsseg3_mask:
1167     case Intrinsic::riscv_vlsseg4_mask:
1168     case Intrinsic::riscv_vlsseg5_mask:
1169     case Intrinsic::riscv_vlsseg6_mask:
1170     case Intrinsic::riscv_vlsseg7_mask:
1171     case Intrinsic::riscv_vlsseg8_mask: {
1172       selectVLSEG(Node, /*IsMasked*/ true, /*IsStrided*/ true);
1173       return;
1174     }
1175     case Intrinsic::riscv_vloxseg2:
1176     case Intrinsic::riscv_vloxseg3:
1177     case Intrinsic::riscv_vloxseg4:
1178     case Intrinsic::riscv_vloxseg5:
1179     case Intrinsic::riscv_vloxseg6:
1180     case Intrinsic::riscv_vloxseg7:
1181     case Intrinsic::riscv_vloxseg8:
1182       selectVLXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ true);
1183       return;
1184     case Intrinsic::riscv_vluxseg2:
1185     case Intrinsic::riscv_vluxseg3:
1186     case Intrinsic::riscv_vluxseg4:
1187     case Intrinsic::riscv_vluxseg5:
1188     case Intrinsic::riscv_vluxseg6:
1189     case Intrinsic::riscv_vluxseg7:
1190     case Intrinsic::riscv_vluxseg8:
1191       selectVLXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ false);
1192       return;
1193     case Intrinsic::riscv_vloxseg2_mask:
1194     case Intrinsic::riscv_vloxseg3_mask:
1195     case Intrinsic::riscv_vloxseg4_mask:
1196     case Intrinsic::riscv_vloxseg5_mask:
1197     case Intrinsic::riscv_vloxseg6_mask:
1198     case Intrinsic::riscv_vloxseg7_mask:
1199     case Intrinsic::riscv_vloxseg8_mask:
1200       selectVLXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ true);
1201       return;
1202     case Intrinsic::riscv_vluxseg2_mask:
1203     case Intrinsic::riscv_vluxseg3_mask:
1204     case Intrinsic::riscv_vluxseg4_mask:
1205     case Intrinsic::riscv_vluxseg5_mask:
1206     case Intrinsic::riscv_vluxseg6_mask:
1207     case Intrinsic::riscv_vluxseg7_mask:
1208     case Intrinsic::riscv_vluxseg8_mask:
1209       selectVLXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ false);
1210       return;
1211     case Intrinsic::riscv_vlseg8ff:
1212     case Intrinsic::riscv_vlseg7ff:
1213     case Intrinsic::riscv_vlseg6ff:
1214     case Intrinsic::riscv_vlseg5ff:
1215     case Intrinsic::riscv_vlseg4ff:
1216     case Intrinsic::riscv_vlseg3ff:
1217     case Intrinsic::riscv_vlseg2ff: {
1218       selectVLSEGFF(Node, /*IsMasked*/ false);
1219       return;
1220     }
1221     case Intrinsic::riscv_vlseg8ff_mask:
1222     case Intrinsic::riscv_vlseg7ff_mask:
1223     case Intrinsic::riscv_vlseg6ff_mask:
1224     case Intrinsic::riscv_vlseg5ff_mask:
1225     case Intrinsic::riscv_vlseg4ff_mask:
1226     case Intrinsic::riscv_vlseg3ff_mask:
1227     case Intrinsic::riscv_vlseg2ff_mask: {
1228       selectVLSEGFF(Node, /*IsMasked*/ true);
1229       return;
1230     }
1231     case Intrinsic::riscv_vloxei:
1232     case Intrinsic::riscv_vloxei_mask:
1233     case Intrinsic::riscv_vluxei:
1234     case Intrinsic::riscv_vluxei_mask: {
1235       bool IsMasked = IntNo == Intrinsic::riscv_vloxei_mask ||
1236                       IntNo == Intrinsic::riscv_vluxei_mask;
1237       bool IsOrdered = IntNo == Intrinsic::riscv_vloxei ||
1238                        IntNo == Intrinsic::riscv_vloxei_mask;
1239 
1240       MVT VT = Node->getSimpleValueType(0);
1241       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1242 
1243       unsigned CurOp = 2;
1244       // Masked intrinsic only have TU version pseduo instructions.
1245       bool IsTU = IsMasked || (!IsMasked && !Node->getOperand(CurOp).isUndef());
1246       SmallVector<SDValue, 8> Operands;
1247       if (IsTU)
1248         Operands.push_back(Node->getOperand(CurOp++));
1249       else
1250         // Skip the undef passthru operand for nomask TA version pseudo
1251         CurOp++;
1252 
1253       MVT IndexVT;
1254       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
1255                                  /*IsStridedOrIndexed*/ true, Operands,
1256                                  /*IsLoad=*/true, &IndexVT);
1257 
1258       assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
1259              "Element count mismatch");
1260 
1261       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1262       RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT);
1263       unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
1264       if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
1265         report_fatal_error("The V extension does not support EEW=64 for index "
1266                            "values when XLEN=32");
1267       }
1268       const RISCV::VLX_VSXPseudo *P = RISCV::getVLXPseudo(
1269           IsMasked, IsTU, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL),
1270           static_cast<unsigned>(IndexLMUL));
1271       MachineSDNode *Load =
1272           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1273 
1274       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1275         CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
1276 
1277       ReplaceNode(Node, Load);
1278       return;
1279     }
1280     case Intrinsic::riscv_vlm:
1281     case Intrinsic::riscv_vle:
1282     case Intrinsic::riscv_vle_mask:
1283     case Intrinsic::riscv_vlse:
1284     case Intrinsic::riscv_vlse_mask: {
1285       bool IsMasked = IntNo == Intrinsic::riscv_vle_mask ||
1286                       IntNo == Intrinsic::riscv_vlse_mask;
1287       bool IsStrided =
1288           IntNo == Intrinsic::riscv_vlse || IntNo == Intrinsic::riscv_vlse_mask;
1289 
1290       MVT VT = Node->getSimpleValueType(0);
1291       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1292 
1293       unsigned CurOp = 2;
1294       // The riscv_vlm intrinsic are always tail agnostic and no passthru operand.
1295       bool HasPassthruOperand = IntNo != Intrinsic::riscv_vlm;
1296       // Masked intrinsic only have TU version pseduo instructions.
1297       bool IsTU =
1298           HasPassthruOperand &&
1299           ((!IsMasked && !Node->getOperand(CurOp).isUndef()) || IsMasked);
1300       SmallVector<SDValue, 8> Operands;
1301       if (IsTU)
1302         Operands.push_back(Node->getOperand(CurOp++));
1303       else if (HasPassthruOperand)
1304         // Skip the undef passthru operand for nomask TA version pseudo
1305         CurOp++;
1306 
1307       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
1308                                  Operands, /*IsLoad=*/true);
1309 
1310       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1311       const RISCV::VLEPseudo *P =
1312           RISCV::getVLEPseudo(IsMasked, IsTU, IsStrided, /*FF*/ false, Log2SEW,
1313                               static_cast<unsigned>(LMUL));
1314       MachineSDNode *Load =
1315           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1316 
1317       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1318         CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
1319 
1320       ReplaceNode(Node, Load);
1321       return;
1322     }
1323     case Intrinsic::riscv_vleff:
1324     case Intrinsic::riscv_vleff_mask: {
1325       bool IsMasked = IntNo == Intrinsic::riscv_vleff_mask;
1326 
1327       MVT VT = Node->getSimpleValueType(0);
1328       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1329 
1330       unsigned CurOp = 2;
1331       // Masked intrinsic only have TU version pseduo instructions.
1332       bool IsTU = IsMasked || (!IsMasked && !Node->getOperand(CurOp).isUndef());
1333       SmallVector<SDValue, 7> Operands;
1334       if (IsTU)
1335         Operands.push_back(Node->getOperand(CurOp++));
1336       else
1337         // Skip the undef passthru operand for nomask TA version pseudo
1338         CurOp++;
1339 
1340       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
1341                                  /*IsStridedOrIndexed*/ false, Operands,
1342                                  /*IsLoad=*/true);
1343 
1344       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1345       const RISCV::VLEPseudo *P =
1346           RISCV::getVLEPseudo(IsMasked, IsTU, /*Strided*/ false, /*FF*/ true,
1347                               Log2SEW, static_cast<unsigned>(LMUL));
1348       MachineSDNode *Load =
1349           CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0),
1350                                  MVT::Other, MVT::Glue, Operands);
1351       SDNode *ReadVL = CurDAG->getMachineNode(RISCV::PseudoReadVL, DL, XLenVT,
1352                                               /*Glue*/ SDValue(Load, 2));
1353 
1354       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1355         CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
1356 
1357       ReplaceUses(SDValue(Node, 0), SDValue(Load, 0));
1358       ReplaceUses(SDValue(Node, 1), SDValue(ReadVL, 0)); // VL
1359       ReplaceUses(SDValue(Node, 2), SDValue(Load, 1));   // Chain
1360       CurDAG->RemoveDeadNode(Node);
1361       return;
1362     }
1363     }
1364     break;
1365   }
1366   case ISD::INTRINSIC_VOID: {
1367     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
1368     switch (IntNo) {
1369     case Intrinsic::riscv_vsseg2:
1370     case Intrinsic::riscv_vsseg3:
1371     case Intrinsic::riscv_vsseg4:
1372     case Intrinsic::riscv_vsseg5:
1373     case Intrinsic::riscv_vsseg6:
1374     case Intrinsic::riscv_vsseg7:
1375     case Intrinsic::riscv_vsseg8: {
1376       selectVSSEG(Node, /*IsMasked*/ false, /*IsStrided*/ false);
1377       return;
1378     }
1379     case Intrinsic::riscv_vsseg2_mask:
1380     case Intrinsic::riscv_vsseg3_mask:
1381     case Intrinsic::riscv_vsseg4_mask:
1382     case Intrinsic::riscv_vsseg5_mask:
1383     case Intrinsic::riscv_vsseg6_mask:
1384     case Intrinsic::riscv_vsseg7_mask:
1385     case Intrinsic::riscv_vsseg8_mask: {
1386       selectVSSEG(Node, /*IsMasked*/ true, /*IsStrided*/ false);
1387       return;
1388     }
1389     case Intrinsic::riscv_vssseg2:
1390     case Intrinsic::riscv_vssseg3:
1391     case Intrinsic::riscv_vssseg4:
1392     case Intrinsic::riscv_vssseg5:
1393     case Intrinsic::riscv_vssseg6:
1394     case Intrinsic::riscv_vssseg7:
1395     case Intrinsic::riscv_vssseg8: {
1396       selectVSSEG(Node, /*IsMasked*/ false, /*IsStrided*/ true);
1397       return;
1398     }
1399     case Intrinsic::riscv_vssseg2_mask:
1400     case Intrinsic::riscv_vssseg3_mask:
1401     case Intrinsic::riscv_vssseg4_mask:
1402     case Intrinsic::riscv_vssseg5_mask:
1403     case Intrinsic::riscv_vssseg6_mask:
1404     case Intrinsic::riscv_vssseg7_mask:
1405     case Intrinsic::riscv_vssseg8_mask: {
1406       selectVSSEG(Node, /*IsMasked*/ true, /*IsStrided*/ true);
1407       return;
1408     }
1409     case Intrinsic::riscv_vsoxseg2:
1410     case Intrinsic::riscv_vsoxseg3:
1411     case Intrinsic::riscv_vsoxseg4:
1412     case Intrinsic::riscv_vsoxseg5:
1413     case Intrinsic::riscv_vsoxseg6:
1414     case Intrinsic::riscv_vsoxseg7:
1415     case Intrinsic::riscv_vsoxseg8:
1416       selectVSXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ true);
1417       return;
1418     case Intrinsic::riscv_vsuxseg2:
1419     case Intrinsic::riscv_vsuxseg3:
1420     case Intrinsic::riscv_vsuxseg4:
1421     case Intrinsic::riscv_vsuxseg5:
1422     case Intrinsic::riscv_vsuxseg6:
1423     case Intrinsic::riscv_vsuxseg7:
1424     case Intrinsic::riscv_vsuxseg8:
1425       selectVSXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ false);
1426       return;
1427     case Intrinsic::riscv_vsoxseg2_mask:
1428     case Intrinsic::riscv_vsoxseg3_mask:
1429     case Intrinsic::riscv_vsoxseg4_mask:
1430     case Intrinsic::riscv_vsoxseg5_mask:
1431     case Intrinsic::riscv_vsoxseg6_mask:
1432     case Intrinsic::riscv_vsoxseg7_mask:
1433     case Intrinsic::riscv_vsoxseg8_mask:
1434       selectVSXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ true);
1435       return;
1436     case Intrinsic::riscv_vsuxseg2_mask:
1437     case Intrinsic::riscv_vsuxseg3_mask:
1438     case Intrinsic::riscv_vsuxseg4_mask:
1439     case Intrinsic::riscv_vsuxseg5_mask:
1440     case Intrinsic::riscv_vsuxseg6_mask:
1441     case Intrinsic::riscv_vsuxseg7_mask:
1442     case Intrinsic::riscv_vsuxseg8_mask:
1443       selectVSXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ false);
1444       return;
1445     case Intrinsic::riscv_vsoxei:
1446     case Intrinsic::riscv_vsoxei_mask:
1447     case Intrinsic::riscv_vsuxei:
1448     case Intrinsic::riscv_vsuxei_mask: {
1449       bool IsMasked = IntNo == Intrinsic::riscv_vsoxei_mask ||
1450                       IntNo == Intrinsic::riscv_vsuxei_mask;
1451       bool IsOrdered = IntNo == Intrinsic::riscv_vsoxei ||
1452                        IntNo == Intrinsic::riscv_vsoxei_mask;
1453 
1454       MVT VT = Node->getOperand(2)->getSimpleValueType(0);
1455       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1456 
1457       unsigned CurOp = 2;
1458       SmallVector<SDValue, 8> Operands;
1459       Operands.push_back(Node->getOperand(CurOp++)); // Store value.
1460 
1461       MVT IndexVT;
1462       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
1463                                  /*IsStridedOrIndexed*/ true, Operands,
1464                                  /*IsLoad=*/false, &IndexVT);
1465 
1466       assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
1467              "Element count mismatch");
1468 
1469       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1470       RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT);
1471       unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
1472       if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
1473         report_fatal_error("The V extension does not support EEW=64 for index "
1474                            "values when XLEN=32");
1475       }
1476       const RISCV::VLX_VSXPseudo *P = RISCV::getVSXPseudo(
1477           IsMasked, /*TU*/ false, IsOrdered, IndexLog2EEW,
1478           static_cast<unsigned>(LMUL), static_cast<unsigned>(IndexLMUL));
1479       MachineSDNode *Store =
1480           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1481 
1482       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1483         CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()});
1484 
1485       ReplaceNode(Node, Store);
1486       return;
1487     }
1488     case Intrinsic::riscv_vsm:
1489     case Intrinsic::riscv_vse:
1490     case Intrinsic::riscv_vse_mask:
1491     case Intrinsic::riscv_vsse:
1492     case Intrinsic::riscv_vsse_mask: {
1493       bool IsMasked = IntNo == Intrinsic::riscv_vse_mask ||
1494                       IntNo == Intrinsic::riscv_vsse_mask;
1495       bool IsStrided =
1496           IntNo == Intrinsic::riscv_vsse || IntNo == Intrinsic::riscv_vsse_mask;
1497 
1498       MVT VT = Node->getOperand(2)->getSimpleValueType(0);
1499       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1500 
1501       unsigned CurOp = 2;
1502       SmallVector<SDValue, 8> Operands;
1503       Operands.push_back(Node->getOperand(CurOp++)); // Store value.
1504 
1505       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
1506                                  Operands);
1507 
1508       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1509       const RISCV::VSEPseudo *P = RISCV::getVSEPseudo(
1510           IsMasked, IsStrided, Log2SEW, static_cast<unsigned>(LMUL));
1511       MachineSDNode *Store =
1512           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1513       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1514         CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()});
1515 
1516       ReplaceNode(Node, Store);
1517       return;
1518     }
1519     }
1520     break;
1521   }
1522   case ISD::BITCAST: {
1523     MVT SrcVT = Node->getOperand(0).getSimpleValueType();
1524     // Just drop bitcasts between vectors if both are fixed or both are
1525     // scalable.
1526     if ((VT.isScalableVector() && SrcVT.isScalableVector()) ||
1527         (VT.isFixedLengthVector() && SrcVT.isFixedLengthVector())) {
1528       ReplaceUses(SDValue(Node, 0), Node->getOperand(0));
1529       CurDAG->RemoveDeadNode(Node);
1530       return;
1531     }
1532     break;
1533   }
1534   case ISD::INSERT_SUBVECTOR: {
1535     SDValue V = Node->getOperand(0);
1536     SDValue SubV = Node->getOperand(1);
1537     SDLoc DL(SubV);
1538     auto Idx = Node->getConstantOperandVal(2);
1539     MVT SubVecVT = SubV.getSimpleValueType();
1540 
1541     const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering();
1542     MVT SubVecContainerVT = SubVecVT;
1543     // Establish the correct scalable-vector types for any fixed-length type.
1544     if (SubVecVT.isFixedLengthVector())
1545       SubVecContainerVT = TLI.getContainerForFixedLengthVector(SubVecVT);
1546     if (VT.isFixedLengthVector())
1547       VT = TLI.getContainerForFixedLengthVector(VT);
1548 
1549     const auto *TRI = Subtarget->getRegisterInfo();
1550     unsigned SubRegIdx;
1551     std::tie(SubRegIdx, Idx) =
1552         RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
1553             VT, SubVecContainerVT, Idx, TRI);
1554 
1555     // If the Idx hasn't been completely eliminated then this is a subvector
1556     // insert which doesn't naturally align to a vector register. These must
1557     // be handled using instructions to manipulate the vector registers.
1558     if (Idx != 0)
1559       break;
1560 
1561     RISCVII::VLMUL SubVecLMUL = RISCVTargetLowering::getLMUL(SubVecContainerVT);
1562     bool IsSubVecPartReg = SubVecLMUL == RISCVII::VLMUL::LMUL_F2 ||
1563                            SubVecLMUL == RISCVII::VLMUL::LMUL_F4 ||
1564                            SubVecLMUL == RISCVII::VLMUL::LMUL_F8;
1565     (void)IsSubVecPartReg; // Silence unused variable warning without asserts.
1566     assert((!IsSubVecPartReg || V.isUndef()) &&
1567            "Expecting lowering to have created legal INSERT_SUBVECTORs when "
1568            "the subvector is smaller than a full-sized register");
1569 
1570     // If we haven't set a SubRegIdx, then we must be going between
1571     // equally-sized LMUL groups (e.g. VR -> VR). This can be done as a copy.
1572     if (SubRegIdx == RISCV::NoSubRegister) {
1573       unsigned InRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(VT);
1574       assert(RISCVTargetLowering::getRegClassIDForVecVT(SubVecContainerVT) ==
1575                  InRegClassID &&
1576              "Unexpected subvector extraction");
1577       SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT);
1578       SDNode *NewNode = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1579                                                DL, VT, SubV, RC);
1580       ReplaceNode(Node, NewNode);
1581       return;
1582     }
1583 
1584     SDValue Insert = CurDAG->getTargetInsertSubreg(SubRegIdx, DL, VT, V, SubV);
1585     ReplaceNode(Node, Insert.getNode());
1586     return;
1587   }
1588   case ISD::EXTRACT_SUBVECTOR: {
1589     SDValue V = Node->getOperand(0);
1590     auto Idx = Node->getConstantOperandVal(1);
1591     MVT InVT = V.getSimpleValueType();
1592     SDLoc DL(V);
1593 
1594     const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering();
1595     MVT SubVecContainerVT = VT;
1596     // Establish the correct scalable-vector types for any fixed-length type.
1597     if (VT.isFixedLengthVector())
1598       SubVecContainerVT = TLI.getContainerForFixedLengthVector(VT);
1599     if (InVT.isFixedLengthVector())
1600       InVT = TLI.getContainerForFixedLengthVector(InVT);
1601 
1602     const auto *TRI = Subtarget->getRegisterInfo();
1603     unsigned SubRegIdx;
1604     std::tie(SubRegIdx, Idx) =
1605         RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
1606             InVT, SubVecContainerVT, Idx, TRI);
1607 
1608     // If the Idx hasn't been completely eliminated then this is a subvector
1609     // extract which doesn't naturally align to a vector register. These must
1610     // be handled using instructions to manipulate the vector registers.
1611     if (Idx != 0)
1612       break;
1613 
1614     // If we haven't set a SubRegIdx, then we must be going between
1615     // equally-sized LMUL types (e.g. VR -> VR). This can be done as a copy.
1616     if (SubRegIdx == RISCV::NoSubRegister) {
1617       unsigned InRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(InVT);
1618       assert(RISCVTargetLowering::getRegClassIDForVecVT(SubVecContainerVT) ==
1619                  InRegClassID &&
1620              "Unexpected subvector extraction");
1621       SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT);
1622       SDNode *NewNode =
1623           CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DL, VT, V, RC);
1624       ReplaceNode(Node, NewNode);
1625       return;
1626     }
1627 
1628     SDValue Extract = CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, V);
1629     ReplaceNode(Node, Extract.getNode());
1630     return;
1631   }
1632   case ISD::SPLAT_VECTOR:
1633   case RISCVISD::VMV_S_X_VL:
1634   case RISCVISD::VFMV_S_F_VL:
1635   case RISCVISD::VMV_V_X_VL:
1636   case RISCVISD::VFMV_V_F_VL: {
1637     // Try to match splat of a scalar load to a strided load with stride of x0.
1638     bool IsScalarMove = Node->getOpcode() == RISCVISD::VMV_S_X_VL ||
1639                         Node->getOpcode() == RISCVISD::VFMV_S_F_VL;
1640     bool HasPassthruOperand = Node->getOpcode() != ISD::SPLAT_VECTOR;
1641     if (HasPassthruOperand && !Node->getOperand(0).isUndef())
1642       break;
1643     SDValue Src = HasPassthruOperand ? Node->getOperand(1) : Node->getOperand(0);
1644     auto *Ld = dyn_cast<LoadSDNode>(Src);
1645     if (!Ld)
1646       break;
1647     EVT MemVT = Ld->getMemoryVT();
1648     // The memory VT should be the same size as the element type.
1649     if (MemVT.getStoreSize() != VT.getVectorElementType().getStoreSize())
1650       break;
1651     if (!IsProfitableToFold(Src, Node, Node) ||
1652         !IsLegalToFold(Src, Node, Node, TM.getOptLevel()))
1653       break;
1654 
1655     SDValue VL;
1656     if (Node->getOpcode() == ISD::SPLAT_VECTOR)
1657       VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, DL, XLenVT);
1658     else if (IsScalarMove) {
1659       // We could deal with more VL if we update the VSETVLI insert pass to
1660       // avoid introducing more VSETVLI.
1661       if (!isOneConstant(Node->getOperand(2)))
1662         break;
1663       selectVLOp(Node->getOperand(2), VL);
1664     } else
1665       selectVLOp(Node->getOperand(2), VL);
1666 
1667     unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1668     SDValue SEW = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT);
1669 
1670     SDValue Operands[] = {Ld->getBasePtr(),
1671                           CurDAG->getRegister(RISCV::X0, XLenVT), VL, SEW,
1672                           Ld->getChain()};
1673 
1674     RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1675     const RISCV::VLEPseudo *P = RISCV::getVLEPseudo(
1676         /*IsMasked*/ false, /*IsTU*/ false, /*IsStrided*/ true, /*FF*/ false,
1677         Log2SEW, static_cast<unsigned>(LMUL));
1678     MachineSDNode *Load =
1679         CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1680 
1681     CurDAG->setNodeMemRefs(Load, {Ld->getMemOperand()});
1682 
1683     ReplaceNode(Node, Load);
1684     return;
1685   }
1686   }
1687 
1688   // Select the default instruction.
1689   SelectCode(Node);
1690 }
1691 
1692 bool RISCVDAGToDAGISel::SelectInlineAsmMemoryOperand(
1693     const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
1694   switch (ConstraintID) {
1695   case InlineAsm::Constraint_m:
1696     // We just support simple memory operands that have a single address
1697     // operand and need no special handling.
1698     OutOps.push_back(Op);
1699     return false;
1700   case InlineAsm::Constraint_A:
1701     OutOps.push_back(Op);
1702     return false;
1703   default:
1704     break;
1705   }
1706 
1707   return true;
1708 }
1709 
1710 bool RISCVDAGToDAGISel::SelectAddrFI(SDValue Addr, SDValue &Base) {
1711   if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1712     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT());
1713     return true;
1714   }
1715   return false;
1716 }
1717 
1718 bool RISCVDAGToDAGISel::SelectBaseAddr(SDValue Addr, SDValue &Base) {
1719   // If this is FrameIndex, select it directly. Otherwise just let it get
1720   // selected to a register independently.
1721   if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr))
1722     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT());
1723   else
1724     Base = Addr;
1725   return true;
1726 }
1727 
1728 bool RISCVDAGToDAGISel::selectShiftMask(SDValue N, unsigned ShiftWidth,
1729                                         SDValue &ShAmt) {
1730   // Shift instructions on RISCV only read the lower 5 or 6 bits of the shift
1731   // amount. If there is an AND on the shift amount, we can bypass it if it
1732   // doesn't affect any of those bits.
1733   if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(N.getOperand(1))) {
1734     const APInt &AndMask = N->getConstantOperandAPInt(1);
1735 
1736     // Since the max shift amount is a power of 2 we can subtract 1 to make a
1737     // mask that covers the bits needed to represent all shift amounts.
1738     assert(isPowerOf2_32(ShiftWidth) && "Unexpected max shift amount!");
1739     APInt ShMask(AndMask.getBitWidth(), ShiftWidth - 1);
1740 
1741     if (ShMask.isSubsetOf(AndMask)) {
1742       ShAmt = N.getOperand(0);
1743       return true;
1744     }
1745 
1746     // SimplifyDemandedBits may have optimized the mask so try restoring any
1747     // bits that are known zero.
1748     KnownBits Known = CurDAG->computeKnownBits(N->getOperand(0));
1749     if (ShMask.isSubsetOf(AndMask | Known.Zero)) {
1750       ShAmt = N.getOperand(0);
1751       return true;
1752     }
1753   } else if (N.getOpcode() == ISD::SUB &&
1754              isa<ConstantSDNode>(N.getOperand(0))) {
1755     uint64_t Imm = N.getConstantOperandVal(0);
1756     // If we are shifting by N-X where N == 0 mod Size, then just shift by -X to
1757     // generate a NEG instead of a SUB of a constant.
1758     if (Imm != 0 && Imm % ShiftWidth == 0) {
1759       SDLoc DL(N);
1760       EVT VT = N.getValueType();
1761       SDValue Zero =
1762           CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, RISCV::X0, VT);
1763       unsigned NegOpc = VT == MVT::i64 ? RISCV::SUBW : RISCV::SUB;
1764       MachineSDNode *Neg = CurDAG->getMachineNode(NegOpc, DL, VT, Zero,
1765                                                   N.getOperand(1));
1766       ShAmt = SDValue(Neg, 0);
1767       return true;
1768     }
1769   }
1770 
1771   ShAmt = N;
1772   return true;
1773 }
1774 
1775 bool RISCVDAGToDAGISel::selectSExti32(SDValue N, SDValue &Val) {
1776   if (N.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1777       cast<VTSDNode>(N.getOperand(1))->getVT() == MVT::i32) {
1778     Val = N.getOperand(0);
1779     return true;
1780   }
1781   MVT VT = N.getSimpleValueType();
1782   if (CurDAG->ComputeNumSignBits(N) > (VT.getSizeInBits() - 32)) {
1783     Val = N;
1784     return true;
1785   }
1786 
1787   return false;
1788 }
1789 
1790 bool RISCVDAGToDAGISel::selectZExti32(SDValue N, SDValue &Val) {
1791   if (N.getOpcode() == ISD::AND) {
1792     auto *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
1793     if (C && C->getZExtValue() == UINT64_C(0xFFFFFFFF)) {
1794       Val = N.getOperand(0);
1795       return true;
1796     }
1797   }
1798   MVT VT = N.getSimpleValueType();
1799   APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), 32);
1800   if (CurDAG->MaskedValueIsZero(N, Mask)) {
1801     Val = N;
1802     return true;
1803   }
1804 
1805   return false;
1806 }
1807 
1808 // Return true if all users of this SDNode* only consume the lower \p Bits.
1809 // This can be used to form W instructions for add/sub/mul/shl even when the
1810 // root isn't a sext_inreg. This can allow the ADDW/SUBW/MULW/SLLIW to CSE if
1811 // SimplifyDemandedBits has made it so some users see a sext_inreg and some
1812 // don't. The sext_inreg+add/sub/mul/shl will get selected, but still leave
1813 // the add/sub/mul/shl to become non-W instructions. By checking the users we
1814 // may be able to use a W instruction and CSE with the other instruction if
1815 // this has happened. We could try to detect that the CSE opportunity exists
1816 // before doing this, but that would be more complicated.
1817 // TODO: Does this need to look through AND/OR/XOR to their users to find more
1818 // opportunities.
1819 bool RISCVDAGToDAGISel::hasAllNBitUsers(SDNode *Node, unsigned Bits) const {
1820   assert((Node->getOpcode() == ISD::ADD || Node->getOpcode() == ISD::SUB ||
1821           Node->getOpcode() == ISD::MUL || Node->getOpcode() == ISD::SHL ||
1822           Node->getOpcode() == ISD::SRL ||
1823           Node->getOpcode() == ISD::SIGN_EXTEND_INREG ||
1824           isa<ConstantSDNode>(Node)) &&
1825          "Unexpected opcode");
1826 
1827   for (auto UI = Node->use_begin(), UE = Node->use_end(); UI != UE; ++UI) {
1828     SDNode *User = *UI;
1829     // Users of this node should have already been instruction selected
1830     if (!User->isMachineOpcode())
1831       return false;
1832 
1833     // TODO: Add more opcodes?
1834     switch (User->getMachineOpcode()) {
1835     default:
1836       return false;
1837     case RISCV::ADDW:
1838     case RISCV::ADDIW:
1839     case RISCV::SUBW:
1840     case RISCV::MULW:
1841     case RISCV::SLLW:
1842     case RISCV::SLLIW:
1843     case RISCV::SRAW:
1844     case RISCV::SRAIW:
1845     case RISCV::SRLW:
1846     case RISCV::SRLIW:
1847     case RISCV::DIVW:
1848     case RISCV::DIVUW:
1849     case RISCV::REMW:
1850     case RISCV::REMUW:
1851     case RISCV::ROLW:
1852     case RISCV::RORW:
1853     case RISCV::RORIW:
1854     case RISCV::CLZW:
1855     case RISCV::CTZW:
1856     case RISCV::CPOPW:
1857     case RISCV::SLLI_UW:
1858     case RISCV::FMV_W_X:
1859     case RISCV::FCVT_H_W:
1860     case RISCV::FCVT_H_WU:
1861     case RISCV::FCVT_S_W:
1862     case RISCV::FCVT_S_WU:
1863     case RISCV::FCVT_D_W:
1864     case RISCV::FCVT_D_WU:
1865       if (Bits < 32)
1866         return false;
1867       break;
1868     case RISCV::SLLI:
1869       // SLLI only uses the lower (XLen - ShAmt) bits.
1870       if (Bits < Subtarget->getXLen() - User->getConstantOperandVal(1))
1871         return false;
1872       break;
1873     case RISCV::ANDI:
1874       if (Bits < (64 - countLeadingZeros(User->getConstantOperandVal(1))))
1875         return false;
1876       break;
1877     case RISCV::SEXT_B:
1878       if (Bits < 8)
1879         return false;
1880       break;
1881     case RISCV::SEXT_H:
1882     case RISCV::FMV_H_X:
1883     case RISCV::ZEXT_H_RV32:
1884     case RISCV::ZEXT_H_RV64:
1885       if (Bits < 16)
1886         return false;
1887       break;
1888     case RISCV::ADD_UW:
1889     case RISCV::SH1ADD_UW:
1890     case RISCV::SH2ADD_UW:
1891     case RISCV::SH3ADD_UW:
1892       // The first operand to add.uw/shXadd.uw is implicitly zero extended from
1893       // 32 bits.
1894       if (UI.getOperandNo() != 0 || Bits < 32)
1895         return false;
1896       break;
1897     case RISCV::SB:
1898       if (UI.getOperandNo() != 0 || Bits < 8)
1899         return false;
1900       break;
1901     case RISCV::SH:
1902       if (UI.getOperandNo() != 0 || Bits < 16)
1903         return false;
1904       break;
1905     case RISCV::SW:
1906       if (UI.getOperandNo() != 0 || Bits < 32)
1907         return false;
1908       break;
1909     }
1910   }
1911 
1912   return true;
1913 }
1914 
1915 // Select VL as a 5 bit immediate or a value that will become a register. This
1916 // allows us to choose betwen VSETIVLI or VSETVLI later.
1917 bool RISCVDAGToDAGISel::selectVLOp(SDValue N, SDValue &VL) {
1918   auto *C = dyn_cast<ConstantSDNode>(N);
1919   if (C && isUInt<5>(C->getZExtValue())) {
1920     VL = CurDAG->getTargetConstant(C->getZExtValue(), SDLoc(N),
1921                                    N->getValueType(0));
1922   } else if (C && C->isAllOnesValue()) {
1923     // Treat all ones as VLMax.
1924     VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
1925                                    N->getValueType(0));
1926   } else if (isa<RegisterSDNode>(N) &&
1927              cast<RegisterSDNode>(N)->getReg() == RISCV::X0) {
1928     // All our VL operands use an operand that allows GPRNoX0 or an immediate
1929     // as the register class. Convert X0 to a special immediate to pass the
1930     // MachineVerifier. This is recognized specially by the vsetvli insertion
1931     // pass.
1932     VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
1933                                    N->getValueType(0));
1934   } else {
1935     VL = N;
1936   }
1937 
1938   return true;
1939 }
1940 
1941 bool RISCVDAGToDAGISel::selectVSplat(SDValue N, SDValue &SplatVal) {
1942   if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef())
1943     return false;
1944   SplatVal = N.getOperand(1);
1945   return true;
1946 }
1947 
1948 using ValidateFn = bool (*)(int64_t);
1949 
1950 static bool selectVSplatSimmHelper(SDValue N, SDValue &SplatVal,
1951                                    SelectionDAG &DAG,
1952                                    const RISCVSubtarget &Subtarget,
1953                                    ValidateFn ValidateImm) {
1954   if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef() ||
1955       !isa<ConstantSDNode>(N.getOperand(1)))
1956     return false;
1957 
1958   int64_t SplatImm =
1959       cast<ConstantSDNode>(N.getOperand(1))->getSExtValue();
1960 
1961   // The semantics of RISCVISD::VMV_V_X_VL is that when the operand
1962   // type is wider than the resulting vector element type: an implicit
1963   // truncation first takes place. Therefore, perform a manual
1964   // truncation/sign-extension in order to ignore any truncated bits and catch
1965   // any zero-extended immediate.
1966   // For example, we wish to match (i8 -1) -> (XLenVT 255) as a simm5 by first
1967   // sign-extending to (XLenVT -1).
1968   MVT XLenVT = Subtarget.getXLenVT();
1969   assert(XLenVT == N.getOperand(1).getSimpleValueType() &&
1970          "Unexpected splat operand type");
1971   MVT EltVT = N.getSimpleValueType().getVectorElementType();
1972   if (EltVT.bitsLT(XLenVT))
1973     SplatImm = SignExtend64(SplatImm, EltVT.getSizeInBits());
1974 
1975   if (!ValidateImm(SplatImm))
1976     return false;
1977 
1978   SplatVal = DAG.getTargetConstant(SplatImm, SDLoc(N), XLenVT);
1979   return true;
1980 }
1981 
1982 bool RISCVDAGToDAGISel::selectVSplatSimm5(SDValue N, SDValue &SplatVal) {
1983   return selectVSplatSimmHelper(N, SplatVal, *CurDAG, *Subtarget,
1984                                 [](int64_t Imm) { return isInt<5>(Imm); });
1985 }
1986 
1987 bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1(SDValue N, SDValue &SplatVal) {
1988   return selectVSplatSimmHelper(
1989       N, SplatVal, *CurDAG, *Subtarget,
1990       [](int64_t Imm) { return (isInt<5>(Imm) && Imm != -16) || Imm == 16; });
1991 }
1992 
1993 bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1NonZero(SDValue N,
1994                                                       SDValue &SplatVal) {
1995   return selectVSplatSimmHelper(
1996       N, SplatVal, *CurDAG, *Subtarget, [](int64_t Imm) {
1997         return Imm != 0 && ((isInt<5>(Imm) && Imm != -16) || Imm == 16);
1998       });
1999 }
2000 
2001 bool RISCVDAGToDAGISel::selectVSplatUimm5(SDValue N, SDValue &SplatVal) {
2002   if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef() ||
2003       !isa<ConstantSDNode>(N.getOperand(1)))
2004     return false;
2005 
2006   int64_t SplatImm =
2007       cast<ConstantSDNode>(N.getOperand(1))->getSExtValue();
2008 
2009   if (!isUInt<5>(SplatImm))
2010     return false;
2011 
2012   SplatVal =
2013       CurDAG->getTargetConstant(SplatImm, SDLoc(N), Subtarget->getXLenVT());
2014 
2015   return true;
2016 }
2017 
2018 bool RISCVDAGToDAGISel::selectRVVSimm5(SDValue N, unsigned Width,
2019                                        SDValue &Imm) {
2020   if (auto *C = dyn_cast<ConstantSDNode>(N)) {
2021     int64_t ImmVal = SignExtend64(C->getSExtValue(), Width);
2022 
2023     if (!isInt<5>(ImmVal))
2024       return false;
2025 
2026     Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), Subtarget->getXLenVT());
2027     return true;
2028   }
2029 
2030   return false;
2031 }
2032 
2033 // Merge an ADDI into the offset of a load/store instruction where possible.
2034 // (load (addi base, off1), off2) -> (load base, off1+off2)
2035 // (store val, (addi base, off1), off2) -> (store val, base, off1+off2)
2036 // This is possible when off1+off2 fits a 12-bit immediate.
2037 bool RISCVDAGToDAGISel::doPeepholeLoadStoreADDI(SDNode *N) {
2038   int OffsetOpIdx;
2039   int BaseOpIdx;
2040 
2041   // Only attempt this optimisation for I-type loads and S-type stores.
2042   switch (N->getMachineOpcode()) {
2043   default:
2044     return false;
2045   case RISCV::LB:
2046   case RISCV::LH:
2047   case RISCV::LW:
2048   case RISCV::LBU:
2049   case RISCV::LHU:
2050   case RISCV::LWU:
2051   case RISCV::LD:
2052   case RISCV::FLH:
2053   case RISCV::FLW:
2054   case RISCV::FLD:
2055     BaseOpIdx = 0;
2056     OffsetOpIdx = 1;
2057     break;
2058   case RISCV::SB:
2059   case RISCV::SH:
2060   case RISCV::SW:
2061   case RISCV::SD:
2062   case RISCV::FSH:
2063   case RISCV::FSW:
2064   case RISCV::FSD:
2065     BaseOpIdx = 1;
2066     OffsetOpIdx = 2;
2067     break;
2068   }
2069 
2070   if (!isa<ConstantSDNode>(N->getOperand(OffsetOpIdx)))
2071     return false;
2072 
2073   SDValue Base = N->getOperand(BaseOpIdx);
2074 
2075   // If the base is an ADDI, we can merge it in to the load/store.
2076   if (!Base.isMachineOpcode() || Base.getMachineOpcode() != RISCV::ADDI)
2077     return false;
2078 
2079   SDValue ImmOperand = Base.getOperand(1);
2080   uint64_t Offset2 = N->getConstantOperandVal(OffsetOpIdx);
2081 
2082   if (auto *Const = dyn_cast<ConstantSDNode>(ImmOperand)) {
2083     int64_t Offset1 = Const->getSExtValue();
2084     int64_t CombinedOffset = Offset1 + Offset2;
2085     if (!isInt<12>(CombinedOffset))
2086       return false;
2087     ImmOperand = CurDAG->getTargetConstant(CombinedOffset, SDLoc(ImmOperand),
2088                                            ImmOperand.getValueType());
2089   } else if (auto *GA = dyn_cast<GlobalAddressSDNode>(ImmOperand)) {
2090     // If the off1 in (addi base, off1) is a global variable's address (its
2091     // low part, really), then we can rely on the alignment of that variable
2092     // to provide a margin of safety before off1 can overflow the 12 bits.
2093     // Check if off2 falls within that margin; if so off1+off2 can't overflow.
2094     const DataLayout &DL = CurDAG->getDataLayout();
2095     Align Alignment = GA->getGlobal()->getPointerAlignment(DL);
2096     if (Offset2 != 0 && Alignment <= Offset2)
2097       return false;
2098     int64_t Offset1 = GA->getOffset();
2099     int64_t CombinedOffset = Offset1 + Offset2;
2100     ImmOperand = CurDAG->getTargetGlobalAddress(
2101         GA->getGlobal(), SDLoc(ImmOperand), ImmOperand.getValueType(),
2102         CombinedOffset, GA->getTargetFlags());
2103   } else if (auto *CP = dyn_cast<ConstantPoolSDNode>(ImmOperand)) {
2104     // Ditto.
2105     Align Alignment = CP->getAlign();
2106     if (Offset2 != 0 && Alignment <= Offset2)
2107       return false;
2108     int64_t Offset1 = CP->getOffset();
2109     int64_t CombinedOffset = Offset1 + Offset2;
2110     ImmOperand = CurDAG->getTargetConstantPool(
2111         CP->getConstVal(), ImmOperand.getValueType(), CP->getAlign(),
2112         CombinedOffset, CP->getTargetFlags());
2113   } else {
2114     return false;
2115   }
2116 
2117   LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase:    ");
2118   LLVM_DEBUG(Base->dump(CurDAG));
2119   LLVM_DEBUG(dbgs() << "\nN: ");
2120   LLVM_DEBUG(N->dump(CurDAG));
2121   LLVM_DEBUG(dbgs() << "\n");
2122 
2123   // Modify the offset operand of the load/store.
2124   if (BaseOpIdx == 0) // Load
2125     CurDAG->UpdateNodeOperands(N, Base.getOperand(0), ImmOperand,
2126                                N->getOperand(2));
2127   else // Store
2128     CurDAG->UpdateNodeOperands(N, N->getOperand(0), Base.getOperand(0),
2129                                ImmOperand, N->getOperand(3));
2130 
2131   return true;
2132 }
2133 
2134 // Try to remove sext.w if the input is a W instruction or can be made into
2135 // a W instruction cheaply.
2136 bool RISCVDAGToDAGISel::doPeepholeSExtW(SDNode *N) {
2137   // Look for the sext.w pattern, addiw rd, rs1, 0.
2138   if (N->getMachineOpcode() != RISCV::ADDIW ||
2139       !isNullConstant(N->getOperand(1)))
2140     return false;
2141 
2142   SDValue N0 = N->getOperand(0);
2143   if (!N0.isMachineOpcode())
2144     return false;
2145 
2146   switch (N0.getMachineOpcode()) {
2147   default:
2148     break;
2149   case RISCV::ADD:
2150   case RISCV::ADDI:
2151   case RISCV::SUB:
2152   case RISCV::MUL:
2153   case RISCV::SLLI: {
2154     // Convert sext.w+add/sub/mul to their W instructions. This will create
2155     // a new independent instruction. This improves latency.
2156     unsigned Opc;
2157     switch (N0.getMachineOpcode()) {
2158     default:
2159       llvm_unreachable("Unexpected opcode!");
2160     case RISCV::ADD:  Opc = RISCV::ADDW;  break;
2161     case RISCV::ADDI: Opc = RISCV::ADDIW; break;
2162     case RISCV::SUB:  Opc = RISCV::SUBW;  break;
2163     case RISCV::MUL:  Opc = RISCV::MULW;  break;
2164     case RISCV::SLLI: Opc = RISCV::SLLIW; break;
2165     }
2166 
2167     SDValue N00 = N0.getOperand(0);
2168     SDValue N01 = N0.getOperand(1);
2169 
2170     // Shift amount needs to be uimm5.
2171     if (N0.getMachineOpcode() == RISCV::SLLI &&
2172         !isUInt<5>(cast<ConstantSDNode>(N01)->getSExtValue()))
2173       break;
2174 
2175     SDNode *Result =
2176         CurDAG->getMachineNode(Opc, SDLoc(N), N->getValueType(0),
2177                                N00, N01);
2178     ReplaceUses(N, Result);
2179     return true;
2180   }
2181   case RISCV::ADDW:
2182   case RISCV::ADDIW:
2183   case RISCV::SUBW:
2184   case RISCV::MULW:
2185   case RISCV::SLLIW:
2186     // Result is already sign extended just remove the sext.w.
2187     // NOTE: We only handle the nodes that are selected with hasAllWUsers.
2188     ReplaceUses(N, N0.getNode());
2189     return true;
2190   }
2191 
2192   return false;
2193 }
2194 
2195 // Optimize masked RVV pseudo instructions with a known all-ones mask to their
2196 // corresponding "unmasked" pseudo versions. The mask we're interested in will
2197 // take the form of a V0 physical register operand, with a glued
2198 // register-setting instruction.
2199 bool RISCVDAGToDAGISel::doPeepholeMaskedRVV(SDNode *N) {
2200   const RISCV::RISCVMaskedPseudoInfo *I =
2201       RISCV::getMaskedPseudoInfo(N->getMachineOpcode());
2202   if (!I)
2203     return false;
2204 
2205   unsigned MaskOpIdx = I->MaskOpIdx;
2206 
2207   // Check that we're using V0 as a mask register.
2208   if (!isa<RegisterSDNode>(N->getOperand(MaskOpIdx)) ||
2209       cast<RegisterSDNode>(N->getOperand(MaskOpIdx))->getReg() != RISCV::V0)
2210     return false;
2211 
2212   // The glued user defines V0.
2213   const auto *Glued = N->getGluedNode();
2214 
2215   if (!Glued || Glued->getOpcode() != ISD::CopyToReg)
2216     return false;
2217 
2218   // Check that we're defining V0 as a mask register.
2219   if (!isa<RegisterSDNode>(Glued->getOperand(1)) ||
2220       cast<RegisterSDNode>(Glued->getOperand(1))->getReg() != RISCV::V0)
2221     return false;
2222 
2223   // Check the instruction defining V0; it needs to be a VMSET pseudo.
2224   SDValue MaskSetter = Glued->getOperand(2);
2225 
2226   const auto IsVMSet = [](unsigned Opc) {
2227     return Opc == RISCV::PseudoVMSET_M_B1 || Opc == RISCV::PseudoVMSET_M_B16 ||
2228            Opc == RISCV::PseudoVMSET_M_B2 || Opc == RISCV::PseudoVMSET_M_B32 ||
2229            Opc == RISCV::PseudoVMSET_M_B4 || Opc == RISCV::PseudoVMSET_M_B64 ||
2230            Opc == RISCV::PseudoVMSET_M_B8;
2231   };
2232 
2233   // TODO: Check that the VMSET is the expected bitwidth? The pseudo has
2234   // undefined behaviour if it's the wrong bitwidth, so we could choose to
2235   // assume that it's all-ones? Same applies to its VL.
2236   if (!MaskSetter->isMachineOpcode() || !IsVMSet(MaskSetter.getMachineOpcode()))
2237     return false;
2238 
2239   // Retrieve the tail policy operand index, if any.
2240   Optional<unsigned> TailPolicyOpIdx;
2241   const RISCVInstrInfo *TII = static_cast<const RISCVInstrInfo *>(
2242       CurDAG->getSubtarget().getInstrInfo());
2243 
2244   const MCInstrDesc &MaskedMCID = TII->get(N->getMachineOpcode());
2245 
2246   if (RISCVII::hasVecPolicyOp(MaskedMCID.TSFlags)) {
2247     // The last operand of the pseudo is the policy op, but we're expecting a
2248     // Glue operand last. We may also have a chain.
2249     TailPolicyOpIdx = N->getNumOperands() - 1;
2250     if (N->getOperand(*TailPolicyOpIdx).getValueType() == MVT::Glue)
2251       (*TailPolicyOpIdx)--;
2252     if (N->getOperand(*TailPolicyOpIdx).getValueType() == MVT::Other)
2253       (*TailPolicyOpIdx)--;
2254 
2255     // If the policy isn't TAIL_AGNOSTIC we can't perform this optimization.
2256     if (N->getConstantOperandVal(*TailPolicyOpIdx) != RISCVII::TAIL_AGNOSTIC)
2257       return false;
2258   }
2259 
2260   const MCInstrDesc &UnmaskedMCID = TII->get(I->UnmaskedPseudo);
2261 
2262   // Check that we're dropping the merge operand, the mask operand, and any
2263   // policy operand when we transform to this unmasked pseudo.
2264   assert(!RISCVII::hasMergeOp(UnmaskedMCID.TSFlags) &&
2265          RISCVII::hasDummyMaskOp(UnmaskedMCID.TSFlags) &&
2266          !RISCVII::hasVecPolicyOp(UnmaskedMCID.TSFlags) &&
2267          "Unexpected pseudo to transform to");
2268   (void)UnmaskedMCID;
2269 
2270   SmallVector<SDValue, 8> Ops;
2271   // Skip the merge operand at index 0.
2272   for (unsigned I = 1, E = N->getNumOperands(); I != E; I++) {
2273     // Skip the mask, the policy, and the Glue.
2274     SDValue Op = N->getOperand(I);
2275     if (I == MaskOpIdx || I == TailPolicyOpIdx ||
2276         Op.getValueType() == MVT::Glue)
2277       continue;
2278     Ops.push_back(Op);
2279   }
2280 
2281   // Transitively apply any node glued to our new node.
2282   if (auto *TGlued = Glued->getGluedNode())
2283     Ops.push_back(SDValue(TGlued, TGlued->getNumValues() - 1));
2284 
2285   SDNode *Result =
2286       CurDAG->getMachineNode(I->UnmaskedPseudo, SDLoc(N), N->getVTList(), Ops);
2287   ReplaceUses(N, Result);
2288 
2289   return true;
2290 }
2291 
2292 // This pass converts a legalized DAG into a RISCV-specific DAG, ready
2293 // for instruction scheduling.
2294 FunctionPass *llvm::createRISCVISelDag(RISCVTargetMachine &TM) {
2295   return new RISCVDAGToDAGISel(TM);
2296 }
2297