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 a c.andi. If we can't use c.andi, the
728     // shift pair might offer more compression opportunities.
729     // TODO: We could check for C extension here, but we don't have many lit
730     // tests with the C extension enabled so not checking gets better coverage.
731     // TODO: What if ANDI faster than shift?
732     bool IsCANDI = isInt<6>(N1C->getSExtValue());
733 
734     // Clear irrelevant bits in the mask.
735     if (LeftShift)
736       C1 &= maskTrailingZeros<uint64_t>(C2);
737     else
738       C1 &= maskTrailingOnes<uint64_t>(XLen - C2);
739 
740     // Some transforms should only be done if the shift has a single use or
741     // the AND would become (srli (slli X, 32), 32)
742     bool OneUseOrZExtW = N0.hasOneUse() || C1 == UINT64_C(0xFFFFFFFF);
743 
744     SDValue X = N0.getOperand(0);
745 
746     // Turn (and (srl x, c2) c1) -> (srli (slli x, c3-c2), c3) if c1 is a mask
747     // with c3 leading zeros.
748     if (!LeftShift && isMask_64(C1)) {
749       uint64_t C3 = XLen - (64 - countLeadingZeros(C1));
750       if (C2 < C3) {
751         // If the number of leading zeros is C2+32 this can be SRLIW.
752         if (C2 + 32 == C3) {
753           SDNode *SRLIW =
754               CurDAG->getMachineNode(RISCV::SRLIW, DL, XLenVT, X,
755                                      CurDAG->getTargetConstant(C2, DL, XLenVT));
756           ReplaceNode(Node, SRLIW);
757           return;
758         }
759 
760         // (and (srl (sexti32 Y), c2), c1) -> (srliw (sraiw Y, 31), c3 - 32) if
761         // c1 is a mask with c3 leading zeros and c2 >= 32 and c3-c2==1.
762         //
763         // This pattern occurs when (i32 (srl (sra 31), c3 - 32)) is type
764         // legalized and goes through DAG combine.
765         if (C2 >= 32 && (C3 - C2) == 1 && N0.hasOneUse() &&
766             X.getOpcode() == ISD::SIGN_EXTEND_INREG &&
767             cast<VTSDNode>(X.getOperand(1))->getVT() == MVT::i32) {
768           SDNode *SRAIW =
769               CurDAG->getMachineNode(RISCV::SRAIW, DL, XLenVT, X.getOperand(0),
770                                      CurDAG->getTargetConstant(31, DL, XLenVT));
771           SDNode *SRLIW = CurDAG->getMachineNode(
772               RISCV::SRLIW, DL, XLenVT, SDValue(SRAIW, 0),
773               CurDAG->getTargetConstant(C3 - 32, DL, XLenVT));
774           ReplaceNode(Node, SRLIW);
775           return;
776         }
777 
778         // (srli (slli x, c3-c2), c3).
779         // Skip if we could use (zext.w (sraiw X, C2)).
780         bool Skip = Subtarget->hasStdExtZba() && C3 == 32 &&
781                     X.getOpcode() == ISD::SIGN_EXTEND_INREG &&
782                     cast<VTSDNode>(X.getOperand(1))->getVT() == MVT::i32;
783         // Also Skip if we can use bexti.
784         Skip |= Subtarget->hasStdExtZbs() && C3 == XLen - 1;
785         if (OneUseOrZExtW && !Skip) {
786           SDNode *SLLI = CurDAG->getMachineNode(
787               RISCV::SLLI, DL, XLenVT, X,
788               CurDAG->getTargetConstant(C3 - C2, DL, XLenVT));
789           SDNode *SRLI =
790               CurDAG->getMachineNode(RISCV::SRLI, DL, XLenVT, SDValue(SLLI, 0),
791                                      CurDAG->getTargetConstant(C3, DL, XLenVT));
792           ReplaceNode(Node, SRLI);
793           return;
794         }
795       }
796     }
797 
798     // Turn (and (shl x, c2), c1) -> (srli (slli c2+c3), c3) if c1 is a mask
799     // shifted by c2 bits with c3 leading zeros.
800     if (LeftShift && isShiftedMask_64(C1)) {
801       uint64_t C3 = XLen - (64 - countLeadingZeros(C1));
802 
803       if (C2 + C3 < XLen &&
804           C1 == (maskTrailingOnes<uint64_t>(XLen - (C2 + C3)) << C2)) {
805         // Use slli.uw when possible.
806         if ((XLen - (C2 + C3)) == 32 && Subtarget->hasStdExtZba()) {
807           SDNode *SLLI_UW =
808               CurDAG->getMachineNode(RISCV::SLLI_UW, DL, XLenVT, X,
809                                      CurDAG->getTargetConstant(C2, DL, XLenVT));
810           ReplaceNode(Node, SLLI_UW);
811           return;
812         }
813 
814         // (srli (slli c2+c3), c3)
815         if (OneUseOrZExtW && !IsCANDI) {
816           SDNode *SLLI = CurDAG->getMachineNode(
817               RISCV::SLLI, DL, XLenVT, X,
818               CurDAG->getTargetConstant(C2 + C3, DL, XLenVT));
819           SDNode *SRLI =
820               CurDAG->getMachineNode(RISCV::SRLI, DL, XLenVT, SDValue(SLLI, 0),
821                                      CurDAG->getTargetConstant(C3, DL, XLenVT));
822           ReplaceNode(Node, SRLI);
823           return;
824         }
825       }
826     }
827 
828     // Turn (and (shr x, c2), c1) -> (slli (srli x, c2+c3), c3) if c1 is a
829     // shifted mask with c2 leading zeros and c3 trailing zeros.
830     if (!LeftShift && isShiftedMask_64(C1)) {
831       uint64_t Leading = XLen - (64 - countLeadingZeros(C1));
832       uint64_t C3 = countTrailingZeros(C1);
833       if (Leading == C2 && C2 + C3 < XLen && OneUseOrZExtW && !IsCANDI) {
834         unsigned SrliOpc = RISCV::SRLI;
835         // If the input is zexti32 we should use SRLIW.
836         if (X.getOpcode() == ISD::AND && isa<ConstantSDNode>(X.getOperand(1)) &&
837             X.getConstantOperandVal(1) == UINT64_C(0xFFFFFFFF)) {
838           SrliOpc = RISCV::SRLIW;
839           X = X.getOperand(0);
840         }
841         SDNode *SRLI = CurDAG->getMachineNode(
842             SrliOpc, DL, XLenVT, X,
843             CurDAG->getTargetConstant(C2 + C3, DL, XLenVT));
844         SDNode *SLLI =
845             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLI, 0),
846                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
847         ReplaceNode(Node, SLLI);
848         return;
849       }
850       // If the leading zero count is C2+32, we can use SRLIW instead of SRLI.
851       if (Leading > 32 && (Leading - 32) == C2 && C2 + C3 < 32 &&
852           OneUseOrZExtW && !IsCANDI) {
853         SDNode *SRLIW = CurDAG->getMachineNode(
854             RISCV::SRLIW, DL, XLenVT, X,
855             CurDAG->getTargetConstant(C2 + C3, DL, XLenVT));
856         SDNode *SLLI =
857             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLIW, 0),
858                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
859         ReplaceNode(Node, SLLI);
860         return;
861       }
862     }
863 
864     // Turn (and (shl x, c2), c1) -> (slli (srli x, c3-c2), c3) if c1 is a
865     // shifted mask with no leading zeros and c3 trailing zeros.
866     if (LeftShift && isShiftedMask_64(C1)) {
867       uint64_t Leading = XLen - (64 - countLeadingZeros(C1));
868       uint64_t C3 = countTrailingZeros(C1);
869       if (Leading == 0 && C2 < C3 && OneUseOrZExtW && !IsCANDI) {
870         SDNode *SRLI = CurDAG->getMachineNode(
871             RISCV::SRLI, DL, XLenVT, X,
872             CurDAG->getTargetConstant(C3 - C2, DL, XLenVT));
873         SDNode *SLLI =
874             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLI, 0),
875                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
876         ReplaceNode(Node, SLLI);
877         return;
878       }
879       // If we have (32-C2) leading zeros, we can use SRLIW instead of SRLI.
880       if (C2 < C3 && Leading + C2 == 32 && OneUseOrZExtW && !IsCANDI) {
881         SDNode *SRLIW = CurDAG->getMachineNode(
882             RISCV::SRLIW, DL, XLenVT, X,
883             CurDAG->getTargetConstant(C3 - C2, DL, XLenVT));
884         SDNode *SLLI =
885             CurDAG->getMachineNode(RISCV::SLLI, DL, XLenVT, SDValue(SRLIW, 0),
886                                    CurDAG->getTargetConstant(C3, DL, XLenVT));
887         ReplaceNode(Node, SLLI);
888         return;
889       }
890     }
891 
892     break;
893   }
894   case ISD::MUL: {
895     // Special case for calculating (mul (and X, C2), C1) where the full product
896     // fits in XLen bits. We can shift X left by the number of leading zeros in
897     // C2 and shift C1 left by XLen-lzcnt(C2). This will ensure the final
898     // product has XLen trailing zeros, putting it in the output of MULHU. This
899     // can avoid materializing a constant in a register for C2.
900 
901     // RHS should be a constant.
902     auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
903     if (!N1C || !N1C->hasOneUse())
904       break;
905 
906     // LHS should be an AND with constant.
907     SDValue N0 = Node->getOperand(0);
908     if (N0.getOpcode() != ISD::AND || !isa<ConstantSDNode>(N0.getOperand(1)))
909       break;
910 
911     uint64_t C2 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
912 
913     // Constant should be a mask.
914     if (!isMask_64(C2))
915       break;
916 
917     // This should be the only use of the AND unless we will use
918     // (SRLI (SLLI X, 32), 32). We don't use a shift pair for other AND
919     // constants.
920     if (!N0.hasOneUse() && C2 != UINT64_C(0xFFFFFFFF))
921       break;
922 
923     // If this can be an ANDI, ZEXT.H or ZEXT.W we don't need to do this
924     // optimization.
925     if (isInt<12>(C2) ||
926         (C2 == UINT64_C(0xFFFF) &&
927          (Subtarget->hasStdExtZbb() || Subtarget->hasStdExtZbp())) ||
928         (C2 == UINT64_C(0xFFFFFFFF) && Subtarget->hasStdExtZba()))
929       break;
930 
931     // We need to shift left the AND input and C1 by a total of XLen bits.
932 
933     // How far left do we need to shift the AND input?
934     unsigned XLen = Subtarget->getXLen();
935     unsigned LeadingZeros = XLen - (64 - countLeadingZeros(C2));
936 
937     // The constant gets shifted by the remaining amount unless that would
938     // shift bits out.
939     uint64_t C1 = N1C->getZExtValue();
940     unsigned ConstantShift = XLen - LeadingZeros;
941     if (ConstantShift > (XLen - (64 - countLeadingZeros(C1))))
942       break;
943 
944     uint64_t ShiftedC1 = C1 << ConstantShift;
945     // If this RV32, we need to sign extend the constant.
946     if (XLen == 32)
947       ShiftedC1 = SignExtend64(ShiftedC1, 32);
948 
949     // Create (mulhu (slli X, lzcnt(C2)), C1 << (XLen - lzcnt(C2))).
950     SDNode *Imm = selectImm(CurDAG, DL, VT, ShiftedC1, *Subtarget);
951     SDNode *SLLI =
952         CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0.getOperand(0),
953                                CurDAG->getTargetConstant(LeadingZeros, DL, VT));
954     SDNode *MULHU = CurDAG->getMachineNode(RISCV::MULHU, DL, VT,
955                                            SDValue(SLLI, 0), SDValue(Imm, 0));
956     ReplaceNode(Node, MULHU);
957     return;
958   }
959   case ISD::INTRINSIC_WO_CHAIN: {
960     unsigned IntNo = Node->getConstantOperandVal(0);
961     switch (IntNo) {
962       // By default we do not custom select any intrinsic.
963     default:
964       break;
965     case Intrinsic::riscv_vmsgeu:
966     case Intrinsic::riscv_vmsge: {
967       SDValue Src1 = Node->getOperand(1);
968       SDValue Src2 = Node->getOperand(2);
969       bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu;
970       bool IsCmpUnsignedZero = false;
971       // Only custom select scalar second operand.
972       if (Src2.getValueType() != XLenVT)
973         break;
974       // Small constants are handled with patterns.
975       if (auto *C = dyn_cast<ConstantSDNode>(Src2)) {
976         int64_t CVal = C->getSExtValue();
977         if (CVal >= -15 && CVal <= 16) {
978           if (!IsUnsigned || CVal != 0)
979             break;
980           IsCmpUnsignedZero = true;
981         }
982       }
983       MVT Src1VT = Src1.getSimpleValueType();
984       unsigned VMSLTOpcode, VMNANDOpcode, VMSetOpcode;
985       switch (RISCVTargetLowering::getLMUL(Src1VT)) {
986       default:
987         llvm_unreachable("Unexpected LMUL!");
988 #define CASE_VMSLT_VMNAND_VMSET_OPCODES(lmulenum, suffix, suffix_b)            \
989   case RISCVII::VLMUL::lmulenum:                                               \
990     VMSLTOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix                 \
991                              : RISCV::PseudoVMSLT_VX_##suffix;                 \
992     VMNANDOpcode = RISCV::PseudoVMNAND_MM_##suffix;                            \
993     VMSetOpcode = RISCV::PseudoVMSET_M_##suffix_b;                             \
994     break;
995         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_F8, MF8, B1)
996         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_F4, MF4, B2)
997         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_F2, MF2, B4)
998         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_1, M1, B8)
999         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_2, M2, B16)
1000         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_4, M4, B32)
1001         CASE_VMSLT_VMNAND_VMSET_OPCODES(LMUL_8, M8, B64)
1002 #undef CASE_VMSLT_VMNAND_VMSET_OPCODES
1003       }
1004       SDValue SEW = CurDAG->getTargetConstant(
1005           Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT);
1006       SDValue VL;
1007       selectVLOp(Node->getOperand(3), VL);
1008 
1009       // If vmsgeu with 0 immediate, expand it to vmset.
1010       if (IsCmpUnsignedZero) {
1011         ReplaceNode(Node, CurDAG->getMachineNode(VMSetOpcode, DL, VT, VL, SEW));
1012         return;
1013       }
1014 
1015       // Expand to
1016       // vmslt{u}.vx vd, va, x; vmnand.mm vd, vd, vd
1017       SDValue Cmp = SDValue(
1018           CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}),
1019           0);
1020       ReplaceNode(Node, CurDAG->getMachineNode(VMNANDOpcode, DL, VT,
1021                                                {Cmp, Cmp, VL, SEW}));
1022       return;
1023     }
1024     case Intrinsic::riscv_vmsgeu_mask:
1025     case Intrinsic::riscv_vmsge_mask: {
1026       SDValue Src1 = Node->getOperand(2);
1027       SDValue Src2 = Node->getOperand(3);
1028       bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu_mask;
1029       bool IsCmpUnsignedZero = false;
1030       // Only custom select scalar second operand.
1031       if (Src2.getValueType() != XLenVT)
1032         break;
1033       // Small constants are handled with patterns.
1034       if (auto *C = dyn_cast<ConstantSDNode>(Src2)) {
1035         int64_t CVal = C->getSExtValue();
1036         if (CVal >= -15 && CVal <= 16) {
1037           if (!IsUnsigned || CVal != 0)
1038             break;
1039           IsCmpUnsignedZero = true;
1040         }
1041       }
1042       MVT Src1VT = Src1.getSimpleValueType();
1043       unsigned VMSLTOpcode, VMSLTMaskOpcode, VMXOROpcode, VMANDNOpcode,
1044           VMOROpcode;
1045       switch (RISCVTargetLowering::getLMUL(Src1VT)) {
1046       default:
1047         llvm_unreachable("Unexpected LMUL!");
1048 #define CASE_VMSLT_OPCODES(lmulenum, suffix, suffix_b)                         \
1049   case RISCVII::VLMUL::lmulenum:                                               \
1050     VMSLTOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix                 \
1051                              : RISCV::PseudoVMSLT_VX_##suffix;                 \
1052     VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix##_MASK      \
1053                                  : RISCV::PseudoVMSLT_VX_##suffix##_MASK;      \
1054     break;
1055         CASE_VMSLT_OPCODES(LMUL_F8, MF8, B1)
1056         CASE_VMSLT_OPCODES(LMUL_F4, MF4, B2)
1057         CASE_VMSLT_OPCODES(LMUL_F2, MF2, B4)
1058         CASE_VMSLT_OPCODES(LMUL_1, M1, B8)
1059         CASE_VMSLT_OPCODES(LMUL_2, M2, B16)
1060         CASE_VMSLT_OPCODES(LMUL_4, M4, B32)
1061         CASE_VMSLT_OPCODES(LMUL_8, M8, B64)
1062 #undef CASE_VMSLT_OPCODES
1063       }
1064       // Mask operations use the LMUL from the mask type.
1065       switch (RISCVTargetLowering::getLMUL(VT)) {
1066       default:
1067         llvm_unreachable("Unexpected LMUL!");
1068 #define CASE_VMXOR_VMANDN_VMOR_OPCODES(lmulenum, suffix)                       \
1069   case RISCVII::VLMUL::lmulenum:                                               \
1070     VMXOROpcode = RISCV::PseudoVMXOR_MM_##suffix;                              \
1071     VMANDNOpcode = RISCV::PseudoVMANDN_MM_##suffix;                            \
1072     VMOROpcode = RISCV::PseudoVMOR_MM_##suffix;                                \
1073     break;
1074         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F8, MF8)
1075         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F4, MF4)
1076         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F2, MF2)
1077         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_1, M1)
1078         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_2, M2)
1079         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_4, M4)
1080         CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_8, M8)
1081 #undef CASE_VMXOR_VMANDN_VMOR_OPCODES
1082       }
1083       SDValue SEW = CurDAG->getTargetConstant(
1084           Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT);
1085       SDValue MaskSEW = CurDAG->getTargetConstant(0, DL, XLenVT);
1086       SDValue VL;
1087       selectVLOp(Node->getOperand(5), VL);
1088       SDValue MaskedOff = Node->getOperand(1);
1089       SDValue Mask = Node->getOperand(4);
1090 
1091       // If vmsgeu_mask with 0 immediate, expand it to vmor mask, maskedoff.
1092       if (IsCmpUnsignedZero) {
1093         // We don't need vmor if the MaskedOff and the Mask are the same
1094         // value.
1095         if (Mask == MaskedOff) {
1096           ReplaceUses(Node, Mask.getNode());
1097           return;
1098         }
1099         ReplaceNode(Node,
1100                     CurDAG->getMachineNode(VMOROpcode, DL, VT,
1101                                            {Mask, MaskedOff, VL, MaskSEW}));
1102         return;
1103       }
1104 
1105       // If the MaskedOff value and the Mask are the same value use
1106       // vmslt{u}.vx vt, va, x;  vmandn.mm vd, vd, vt
1107       // This avoids needing to copy v0 to vd before starting the next sequence.
1108       if (Mask == MaskedOff) {
1109         SDValue Cmp = SDValue(
1110             CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}),
1111             0);
1112         ReplaceNode(Node, CurDAG->getMachineNode(VMANDNOpcode, DL, VT,
1113                                                  {Mask, Cmp, VL, MaskSEW}));
1114         return;
1115       }
1116 
1117       // Mask needs to be copied to V0.
1118       SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL,
1119                                            RISCV::V0, Mask, SDValue());
1120       SDValue Glue = Chain.getValue(1);
1121       SDValue V0 = CurDAG->getRegister(RISCV::V0, VT);
1122 
1123       // Otherwise use
1124       // vmslt{u}.vx vd, va, x, v0.t; vmxor.mm vd, vd, v0
1125       // The result is mask undisturbed.
1126       // We use the same instructions to emulate mask agnostic behavior, because
1127       // the agnostic result can be either undisturbed or all 1.
1128       SDValue Cmp = SDValue(
1129           CurDAG->getMachineNode(VMSLTMaskOpcode, DL, VT,
1130                                  {MaskedOff, Src1, Src2, V0, VL, SEW, Glue}),
1131           0);
1132       // vmxor.mm vd, vd, v0 is used to update active value.
1133       ReplaceNode(Node, CurDAG->getMachineNode(VMXOROpcode, DL, VT,
1134                                                {Cmp, Mask, VL, MaskSEW}));
1135       return;
1136     }
1137     case Intrinsic::riscv_vsetvli_opt:
1138     case Intrinsic::riscv_vsetvlimax_opt:
1139       return selectVSETVLI(Node);
1140     }
1141     break;
1142   }
1143   case ISD::INTRINSIC_W_CHAIN: {
1144     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
1145     switch (IntNo) {
1146       // By default we do not custom select any intrinsic.
1147     default:
1148       break;
1149     case Intrinsic::riscv_vsetvli:
1150     case Intrinsic::riscv_vsetvlimax:
1151       return selectVSETVLI(Node);
1152     case Intrinsic::riscv_vlseg2:
1153     case Intrinsic::riscv_vlseg3:
1154     case Intrinsic::riscv_vlseg4:
1155     case Intrinsic::riscv_vlseg5:
1156     case Intrinsic::riscv_vlseg6:
1157     case Intrinsic::riscv_vlseg7:
1158     case Intrinsic::riscv_vlseg8: {
1159       selectVLSEG(Node, /*IsMasked*/ false, /*IsStrided*/ false);
1160       return;
1161     }
1162     case Intrinsic::riscv_vlseg2_mask:
1163     case Intrinsic::riscv_vlseg3_mask:
1164     case Intrinsic::riscv_vlseg4_mask:
1165     case Intrinsic::riscv_vlseg5_mask:
1166     case Intrinsic::riscv_vlseg6_mask:
1167     case Intrinsic::riscv_vlseg7_mask:
1168     case Intrinsic::riscv_vlseg8_mask: {
1169       selectVLSEG(Node, /*IsMasked*/ true, /*IsStrided*/ false);
1170       return;
1171     }
1172     case Intrinsic::riscv_vlsseg2:
1173     case Intrinsic::riscv_vlsseg3:
1174     case Intrinsic::riscv_vlsseg4:
1175     case Intrinsic::riscv_vlsseg5:
1176     case Intrinsic::riscv_vlsseg6:
1177     case Intrinsic::riscv_vlsseg7:
1178     case Intrinsic::riscv_vlsseg8: {
1179       selectVLSEG(Node, /*IsMasked*/ false, /*IsStrided*/ true);
1180       return;
1181     }
1182     case Intrinsic::riscv_vlsseg2_mask:
1183     case Intrinsic::riscv_vlsseg3_mask:
1184     case Intrinsic::riscv_vlsseg4_mask:
1185     case Intrinsic::riscv_vlsseg5_mask:
1186     case Intrinsic::riscv_vlsseg6_mask:
1187     case Intrinsic::riscv_vlsseg7_mask:
1188     case Intrinsic::riscv_vlsseg8_mask: {
1189       selectVLSEG(Node, /*IsMasked*/ true, /*IsStrided*/ true);
1190       return;
1191     }
1192     case Intrinsic::riscv_vloxseg2:
1193     case Intrinsic::riscv_vloxseg3:
1194     case Intrinsic::riscv_vloxseg4:
1195     case Intrinsic::riscv_vloxseg5:
1196     case Intrinsic::riscv_vloxseg6:
1197     case Intrinsic::riscv_vloxseg7:
1198     case Intrinsic::riscv_vloxseg8:
1199       selectVLXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ true);
1200       return;
1201     case Intrinsic::riscv_vluxseg2:
1202     case Intrinsic::riscv_vluxseg3:
1203     case Intrinsic::riscv_vluxseg4:
1204     case Intrinsic::riscv_vluxseg5:
1205     case Intrinsic::riscv_vluxseg6:
1206     case Intrinsic::riscv_vluxseg7:
1207     case Intrinsic::riscv_vluxseg8:
1208       selectVLXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ false);
1209       return;
1210     case Intrinsic::riscv_vloxseg2_mask:
1211     case Intrinsic::riscv_vloxseg3_mask:
1212     case Intrinsic::riscv_vloxseg4_mask:
1213     case Intrinsic::riscv_vloxseg5_mask:
1214     case Intrinsic::riscv_vloxseg6_mask:
1215     case Intrinsic::riscv_vloxseg7_mask:
1216     case Intrinsic::riscv_vloxseg8_mask:
1217       selectVLXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ true);
1218       return;
1219     case Intrinsic::riscv_vluxseg2_mask:
1220     case Intrinsic::riscv_vluxseg3_mask:
1221     case Intrinsic::riscv_vluxseg4_mask:
1222     case Intrinsic::riscv_vluxseg5_mask:
1223     case Intrinsic::riscv_vluxseg6_mask:
1224     case Intrinsic::riscv_vluxseg7_mask:
1225     case Intrinsic::riscv_vluxseg8_mask:
1226       selectVLXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ false);
1227       return;
1228     case Intrinsic::riscv_vlseg8ff:
1229     case Intrinsic::riscv_vlseg7ff:
1230     case Intrinsic::riscv_vlseg6ff:
1231     case Intrinsic::riscv_vlseg5ff:
1232     case Intrinsic::riscv_vlseg4ff:
1233     case Intrinsic::riscv_vlseg3ff:
1234     case Intrinsic::riscv_vlseg2ff: {
1235       selectVLSEGFF(Node, /*IsMasked*/ false);
1236       return;
1237     }
1238     case Intrinsic::riscv_vlseg8ff_mask:
1239     case Intrinsic::riscv_vlseg7ff_mask:
1240     case Intrinsic::riscv_vlseg6ff_mask:
1241     case Intrinsic::riscv_vlseg5ff_mask:
1242     case Intrinsic::riscv_vlseg4ff_mask:
1243     case Intrinsic::riscv_vlseg3ff_mask:
1244     case Intrinsic::riscv_vlseg2ff_mask: {
1245       selectVLSEGFF(Node, /*IsMasked*/ true);
1246       return;
1247     }
1248     case Intrinsic::riscv_vloxei:
1249     case Intrinsic::riscv_vloxei_mask:
1250     case Intrinsic::riscv_vluxei:
1251     case Intrinsic::riscv_vluxei_mask: {
1252       bool IsMasked = IntNo == Intrinsic::riscv_vloxei_mask ||
1253                       IntNo == Intrinsic::riscv_vluxei_mask;
1254       bool IsOrdered = IntNo == Intrinsic::riscv_vloxei ||
1255                        IntNo == Intrinsic::riscv_vloxei_mask;
1256 
1257       MVT VT = Node->getSimpleValueType(0);
1258       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1259 
1260       unsigned CurOp = 2;
1261       // Masked intrinsic only have TU version pseduo instructions.
1262       bool IsTU = IsMasked || (!IsMasked && !Node->getOperand(CurOp).isUndef());
1263       SmallVector<SDValue, 8> Operands;
1264       if (IsTU)
1265         Operands.push_back(Node->getOperand(CurOp++));
1266       else
1267         // Skip the undef passthru operand for nomask TA version pseudo
1268         CurOp++;
1269 
1270       MVT IndexVT;
1271       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
1272                                  /*IsStridedOrIndexed*/ true, Operands,
1273                                  /*IsLoad=*/true, &IndexVT);
1274 
1275       assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
1276              "Element count mismatch");
1277 
1278       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1279       RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT);
1280       unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
1281       if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
1282         report_fatal_error("The V extension does not support EEW=64 for index "
1283                            "values when XLEN=32");
1284       }
1285       const RISCV::VLX_VSXPseudo *P = RISCV::getVLXPseudo(
1286           IsMasked, IsTU, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL),
1287           static_cast<unsigned>(IndexLMUL));
1288       MachineSDNode *Load =
1289           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1290 
1291       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1292         CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
1293 
1294       ReplaceNode(Node, Load);
1295       return;
1296     }
1297     case Intrinsic::riscv_vlm:
1298     case Intrinsic::riscv_vle:
1299     case Intrinsic::riscv_vle_mask:
1300     case Intrinsic::riscv_vlse:
1301     case Intrinsic::riscv_vlse_mask: {
1302       bool IsMasked = IntNo == Intrinsic::riscv_vle_mask ||
1303                       IntNo == Intrinsic::riscv_vlse_mask;
1304       bool IsStrided =
1305           IntNo == Intrinsic::riscv_vlse || IntNo == Intrinsic::riscv_vlse_mask;
1306 
1307       MVT VT = Node->getSimpleValueType(0);
1308       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1309 
1310       unsigned CurOp = 2;
1311       // The riscv_vlm intrinsic are always tail agnostic and no passthru operand.
1312       bool HasPassthruOperand = IntNo != Intrinsic::riscv_vlm;
1313       // Masked intrinsic only have TU version pseduo instructions.
1314       bool IsTU =
1315           HasPassthruOperand &&
1316           ((!IsMasked && !Node->getOperand(CurOp).isUndef()) || IsMasked);
1317       SmallVector<SDValue, 8> Operands;
1318       if (IsTU)
1319         Operands.push_back(Node->getOperand(CurOp++));
1320       else if (HasPassthruOperand)
1321         // Skip the undef passthru operand for nomask TA version pseudo
1322         CurOp++;
1323 
1324       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
1325                                  Operands, /*IsLoad=*/true);
1326 
1327       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1328       const RISCV::VLEPseudo *P =
1329           RISCV::getVLEPseudo(IsMasked, IsTU, IsStrided, /*FF*/ false, Log2SEW,
1330                               static_cast<unsigned>(LMUL));
1331       MachineSDNode *Load =
1332           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1333 
1334       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1335         CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
1336 
1337       ReplaceNode(Node, Load);
1338       return;
1339     }
1340     case Intrinsic::riscv_vleff:
1341     case Intrinsic::riscv_vleff_mask: {
1342       bool IsMasked = IntNo == Intrinsic::riscv_vleff_mask;
1343 
1344       MVT VT = Node->getSimpleValueType(0);
1345       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1346 
1347       unsigned CurOp = 2;
1348       // Masked intrinsic only have TU version pseduo instructions.
1349       bool IsTU = IsMasked || (!IsMasked && !Node->getOperand(CurOp).isUndef());
1350       SmallVector<SDValue, 7> Operands;
1351       if (IsTU)
1352         Operands.push_back(Node->getOperand(CurOp++));
1353       else
1354         // Skip the undef passthru operand for nomask TA version pseudo
1355         CurOp++;
1356 
1357       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
1358                                  /*IsStridedOrIndexed*/ false, Operands,
1359                                  /*IsLoad=*/true);
1360 
1361       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1362       const RISCV::VLEPseudo *P =
1363           RISCV::getVLEPseudo(IsMasked, IsTU, /*Strided*/ false, /*FF*/ true,
1364                               Log2SEW, static_cast<unsigned>(LMUL));
1365       MachineSDNode *Load =
1366           CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0),
1367                                  MVT::Other, MVT::Glue, Operands);
1368       SDNode *ReadVL = CurDAG->getMachineNode(RISCV::PseudoReadVL, DL, XLenVT,
1369                                               /*Glue*/ SDValue(Load, 2));
1370 
1371       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1372         CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
1373 
1374       ReplaceUses(SDValue(Node, 0), SDValue(Load, 0));
1375       ReplaceUses(SDValue(Node, 1), SDValue(ReadVL, 0)); // VL
1376       ReplaceUses(SDValue(Node, 2), SDValue(Load, 1));   // Chain
1377       CurDAG->RemoveDeadNode(Node);
1378       return;
1379     }
1380     }
1381     break;
1382   }
1383   case ISD::INTRINSIC_VOID: {
1384     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
1385     switch (IntNo) {
1386     case Intrinsic::riscv_vsseg2:
1387     case Intrinsic::riscv_vsseg3:
1388     case Intrinsic::riscv_vsseg4:
1389     case Intrinsic::riscv_vsseg5:
1390     case Intrinsic::riscv_vsseg6:
1391     case Intrinsic::riscv_vsseg7:
1392     case Intrinsic::riscv_vsseg8: {
1393       selectVSSEG(Node, /*IsMasked*/ false, /*IsStrided*/ false);
1394       return;
1395     }
1396     case Intrinsic::riscv_vsseg2_mask:
1397     case Intrinsic::riscv_vsseg3_mask:
1398     case Intrinsic::riscv_vsseg4_mask:
1399     case Intrinsic::riscv_vsseg5_mask:
1400     case Intrinsic::riscv_vsseg6_mask:
1401     case Intrinsic::riscv_vsseg7_mask:
1402     case Intrinsic::riscv_vsseg8_mask: {
1403       selectVSSEG(Node, /*IsMasked*/ true, /*IsStrided*/ false);
1404       return;
1405     }
1406     case Intrinsic::riscv_vssseg2:
1407     case Intrinsic::riscv_vssseg3:
1408     case Intrinsic::riscv_vssseg4:
1409     case Intrinsic::riscv_vssseg5:
1410     case Intrinsic::riscv_vssseg6:
1411     case Intrinsic::riscv_vssseg7:
1412     case Intrinsic::riscv_vssseg8: {
1413       selectVSSEG(Node, /*IsMasked*/ false, /*IsStrided*/ true);
1414       return;
1415     }
1416     case Intrinsic::riscv_vssseg2_mask:
1417     case Intrinsic::riscv_vssseg3_mask:
1418     case Intrinsic::riscv_vssseg4_mask:
1419     case Intrinsic::riscv_vssseg5_mask:
1420     case Intrinsic::riscv_vssseg6_mask:
1421     case Intrinsic::riscv_vssseg7_mask:
1422     case Intrinsic::riscv_vssseg8_mask: {
1423       selectVSSEG(Node, /*IsMasked*/ true, /*IsStrided*/ true);
1424       return;
1425     }
1426     case Intrinsic::riscv_vsoxseg2:
1427     case Intrinsic::riscv_vsoxseg3:
1428     case Intrinsic::riscv_vsoxseg4:
1429     case Intrinsic::riscv_vsoxseg5:
1430     case Intrinsic::riscv_vsoxseg6:
1431     case Intrinsic::riscv_vsoxseg7:
1432     case Intrinsic::riscv_vsoxseg8:
1433       selectVSXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ true);
1434       return;
1435     case Intrinsic::riscv_vsuxseg2:
1436     case Intrinsic::riscv_vsuxseg3:
1437     case Intrinsic::riscv_vsuxseg4:
1438     case Intrinsic::riscv_vsuxseg5:
1439     case Intrinsic::riscv_vsuxseg6:
1440     case Intrinsic::riscv_vsuxseg7:
1441     case Intrinsic::riscv_vsuxseg8:
1442       selectVSXSEG(Node, /*IsMasked*/ false, /*IsOrdered*/ false);
1443       return;
1444     case Intrinsic::riscv_vsoxseg2_mask:
1445     case Intrinsic::riscv_vsoxseg3_mask:
1446     case Intrinsic::riscv_vsoxseg4_mask:
1447     case Intrinsic::riscv_vsoxseg5_mask:
1448     case Intrinsic::riscv_vsoxseg6_mask:
1449     case Intrinsic::riscv_vsoxseg7_mask:
1450     case Intrinsic::riscv_vsoxseg8_mask:
1451       selectVSXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ true);
1452       return;
1453     case Intrinsic::riscv_vsuxseg2_mask:
1454     case Intrinsic::riscv_vsuxseg3_mask:
1455     case Intrinsic::riscv_vsuxseg4_mask:
1456     case Intrinsic::riscv_vsuxseg5_mask:
1457     case Intrinsic::riscv_vsuxseg6_mask:
1458     case Intrinsic::riscv_vsuxseg7_mask:
1459     case Intrinsic::riscv_vsuxseg8_mask:
1460       selectVSXSEG(Node, /*IsMasked*/ true, /*IsOrdered*/ false);
1461       return;
1462     case Intrinsic::riscv_vsoxei:
1463     case Intrinsic::riscv_vsoxei_mask:
1464     case Intrinsic::riscv_vsuxei:
1465     case Intrinsic::riscv_vsuxei_mask: {
1466       bool IsMasked = IntNo == Intrinsic::riscv_vsoxei_mask ||
1467                       IntNo == Intrinsic::riscv_vsuxei_mask;
1468       bool IsOrdered = IntNo == Intrinsic::riscv_vsoxei ||
1469                        IntNo == Intrinsic::riscv_vsoxei_mask;
1470 
1471       MVT VT = Node->getOperand(2)->getSimpleValueType(0);
1472       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1473 
1474       unsigned CurOp = 2;
1475       SmallVector<SDValue, 8> Operands;
1476       Operands.push_back(Node->getOperand(CurOp++)); // Store value.
1477 
1478       MVT IndexVT;
1479       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
1480                                  /*IsStridedOrIndexed*/ true, Operands,
1481                                  /*IsLoad=*/false, &IndexVT);
1482 
1483       assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
1484              "Element count mismatch");
1485 
1486       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1487       RISCVII::VLMUL IndexLMUL = RISCVTargetLowering::getLMUL(IndexVT);
1488       unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
1489       if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
1490         report_fatal_error("The V extension does not support EEW=64 for index "
1491                            "values when XLEN=32");
1492       }
1493       const RISCV::VLX_VSXPseudo *P = RISCV::getVSXPseudo(
1494           IsMasked, /*TU*/ false, IsOrdered, IndexLog2EEW,
1495           static_cast<unsigned>(LMUL), static_cast<unsigned>(IndexLMUL));
1496       MachineSDNode *Store =
1497           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1498 
1499       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1500         CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()});
1501 
1502       ReplaceNode(Node, Store);
1503       return;
1504     }
1505     case Intrinsic::riscv_vsm:
1506     case Intrinsic::riscv_vse:
1507     case Intrinsic::riscv_vse_mask:
1508     case Intrinsic::riscv_vsse:
1509     case Intrinsic::riscv_vsse_mask: {
1510       bool IsMasked = IntNo == Intrinsic::riscv_vse_mask ||
1511                       IntNo == Intrinsic::riscv_vsse_mask;
1512       bool IsStrided =
1513           IntNo == Intrinsic::riscv_vsse || IntNo == Intrinsic::riscv_vsse_mask;
1514 
1515       MVT VT = Node->getOperand(2)->getSimpleValueType(0);
1516       unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1517 
1518       unsigned CurOp = 2;
1519       SmallVector<SDValue, 8> Operands;
1520       Operands.push_back(Node->getOperand(CurOp++)); // Store value.
1521 
1522       addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
1523                                  Operands);
1524 
1525       RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1526       const RISCV::VSEPseudo *P = RISCV::getVSEPseudo(
1527           IsMasked, IsStrided, Log2SEW, static_cast<unsigned>(LMUL));
1528       MachineSDNode *Store =
1529           CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1530       if (auto *MemOp = dyn_cast<MemSDNode>(Node))
1531         CurDAG->setNodeMemRefs(Store, {MemOp->getMemOperand()});
1532 
1533       ReplaceNode(Node, Store);
1534       return;
1535     }
1536     }
1537     break;
1538   }
1539   case ISD::BITCAST: {
1540     MVT SrcVT = Node->getOperand(0).getSimpleValueType();
1541     // Just drop bitcasts between vectors if both are fixed or both are
1542     // scalable.
1543     if ((VT.isScalableVector() && SrcVT.isScalableVector()) ||
1544         (VT.isFixedLengthVector() && SrcVT.isFixedLengthVector())) {
1545       ReplaceUses(SDValue(Node, 0), Node->getOperand(0));
1546       CurDAG->RemoveDeadNode(Node);
1547       return;
1548     }
1549     break;
1550   }
1551   case ISD::INSERT_SUBVECTOR: {
1552     SDValue V = Node->getOperand(0);
1553     SDValue SubV = Node->getOperand(1);
1554     SDLoc DL(SubV);
1555     auto Idx = Node->getConstantOperandVal(2);
1556     MVT SubVecVT = SubV.getSimpleValueType();
1557 
1558     const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering();
1559     MVT SubVecContainerVT = SubVecVT;
1560     // Establish the correct scalable-vector types for any fixed-length type.
1561     if (SubVecVT.isFixedLengthVector())
1562       SubVecContainerVT = TLI.getContainerForFixedLengthVector(SubVecVT);
1563     if (VT.isFixedLengthVector())
1564       VT = TLI.getContainerForFixedLengthVector(VT);
1565 
1566     const auto *TRI = Subtarget->getRegisterInfo();
1567     unsigned SubRegIdx;
1568     std::tie(SubRegIdx, Idx) =
1569         RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
1570             VT, SubVecContainerVT, Idx, TRI);
1571 
1572     // If the Idx hasn't been completely eliminated then this is a subvector
1573     // insert which doesn't naturally align to a vector register. These must
1574     // be handled using instructions to manipulate the vector registers.
1575     if (Idx != 0)
1576       break;
1577 
1578     RISCVII::VLMUL SubVecLMUL = RISCVTargetLowering::getLMUL(SubVecContainerVT);
1579     bool IsSubVecPartReg = SubVecLMUL == RISCVII::VLMUL::LMUL_F2 ||
1580                            SubVecLMUL == RISCVII::VLMUL::LMUL_F4 ||
1581                            SubVecLMUL == RISCVII::VLMUL::LMUL_F8;
1582     (void)IsSubVecPartReg; // Silence unused variable warning without asserts.
1583     assert((!IsSubVecPartReg || V.isUndef()) &&
1584            "Expecting lowering to have created legal INSERT_SUBVECTORs when "
1585            "the subvector is smaller than a full-sized register");
1586 
1587     // If we haven't set a SubRegIdx, then we must be going between
1588     // equally-sized LMUL groups (e.g. VR -> VR). This can be done as a copy.
1589     if (SubRegIdx == RISCV::NoSubRegister) {
1590       unsigned InRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(VT);
1591       assert(RISCVTargetLowering::getRegClassIDForVecVT(SubVecContainerVT) ==
1592                  InRegClassID &&
1593              "Unexpected subvector extraction");
1594       SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT);
1595       SDNode *NewNode = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1596                                                DL, VT, SubV, RC);
1597       ReplaceNode(Node, NewNode);
1598       return;
1599     }
1600 
1601     SDValue Insert = CurDAG->getTargetInsertSubreg(SubRegIdx, DL, VT, V, SubV);
1602     ReplaceNode(Node, Insert.getNode());
1603     return;
1604   }
1605   case ISD::EXTRACT_SUBVECTOR: {
1606     SDValue V = Node->getOperand(0);
1607     auto Idx = Node->getConstantOperandVal(1);
1608     MVT InVT = V.getSimpleValueType();
1609     SDLoc DL(V);
1610 
1611     const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering();
1612     MVT SubVecContainerVT = VT;
1613     // Establish the correct scalable-vector types for any fixed-length type.
1614     if (VT.isFixedLengthVector())
1615       SubVecContainerVT = TLI.getContainerForFixedLengthVector(VT);
1616     if (InVT.isFixedLengthVector())
1617       InVT = TLI.getContainerForFixedLengthVector(InVT);
1618 
1619     const auto *TRI = Subtarget->getRegisterInfo();
1620     unsigned SubRegIdx;
1621     std::tie(SubRegIdx, Idx) =
1622         RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
1623             InVT, SubVecContainerVT, Idx, TRI);
1624 
1625     // If the Idx hasn't been completely eliminated then this is a subvector
1626     // extract which doesn't naturally align to a vector register. These must
1627     // be handled using instructions to manipulate the vector registers.
1628     if (Idx != 0)
1629       break;
1630 
1631     // If we haven't set a SubRegIdx, then we must be going between
1632     // equally-sized LMUL types (e.g. VR -> VR). This can be done as a copy.
1633     if (SubRegIdx == RISCV::NoSubRegister) {
1634       unsigned InRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(InVT);
1635       assert(RISCVTargetLowering::getRegClassIDForVecVT(SubVecContainerVT) ==
1636                  InRegClassID &&
1637              "Unexpected subvector extraction");
1638       SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT);
1639       SDNode *NewNode =
1640           CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DL, VT, V, RC);
1641       ReplaceNode(Node, NewNode);
1642       return;
1643     }
1644 
1645     SDValue Extract = CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, V);
1646     ReplaceNode(Node, Extract.getNode());
1647     return;
1648   }
1649   case ISD::SPLAT_VECTOR:
1650   case RISCVISD::VMV_S_X_VL:
1651   case RISCVISD::VFMV_S_F_VL:
1652   case RISCVISD::VMV_V_X_VL:
1653   case RISCVISD::VFMV_V_F_VL: {
1654     // Try to match splat of a scalar load to a strided load with stride of x0.
1655     bool IsScalarMove = Node->getOpcode() == RISCVISD::VMV_S_X_VL ||
1656                         Node->getOpcode() == RISCVISD::VFMV_S_F_VL;
1657     bool HasPassthruOperand = Node->getOpcode() != ISD::SPLAT_VECTOR;
1658     if (HasPassthruOperand && !Node->getOperand(0).isUndef())
1659       break;
1660     SDValue Src = HasPassthruOperand ? Node->getOperand(1) : Node->getOperand(0);
1661     auto *Ld = dyn_cast<LoadSDNode>(Src);
1662     if (!Ld)
1663       break;
1664     EVT MemVT = Ld->getMemoryVT();
1665     // The memory VT should be the same size as the element type.
1666     if (MemVT.getStoreSize() != VT.getVectorElementType().getStoreSize())
1667       break;
1668     if (!IsProfitableToFold(Src, Node, Node) ||
1669         !IsLegalToFold(Src, Node, Node, TM.getOptLevel()))
1670       break;
1671 
1672     SDValue VL;
1673     if (Node->getOpcode() == ISD::SPLAT_VECTOR)
1674       VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, DL, XLenVT);
1675     else if (IsScalarMove) {
1676       // We could deal with more VL if we update the VSETVLI insert pass to
1677       // avoid introducing more VSETVLI.
1678       if (!isOneConstant(Node->getOperand(2)))
1679         break;
1680       selectVLOp(Node->getOperand(2), VL);
1681     } else
1682       selectVLOp(Node->getOperand(2), VL);
1683 
1684     unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
1685     SDValue SEW = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT);
1686 
1687     SDValue Operands[] = {Ld->getBasePtr(),
1688                           CurDAG->getRegister(RISCV::X0, XLenVT), VL, SEW,
1689                           Ld->getChain()};
1690 
1691     RISCVII::VLMUL LMUL = RISCVTargetLowering::getLMUL(VT);
1692     const RISCV::VLEPseudo *P = RISCV::getVLEPseudo(
1693         /*IsMasked*/ false, /*IsTU*/ false, /*IsStrided*/ true, /*FF*/ false,
1694         Log2SEW, static_cast<unsigned>(LMUL));
1695     MachineSDNode *Load =
1696         CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
1697 
1698     CurDAG->setNodeMemRefs(Load, {Ld->getMemOperand()});
1699 
1700     ReplaceNode(Node, Load);
1701     return;
1702   }
1703   }
1704 
1705   // Select the default instruction.
1706   SelectCode(Node);
1707 }
1708 
1709 bool RISCVDAGToDAGISel::SelectInlineAsmMemoryOperand(
1710     const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
1711   switch (ConstraintID) {
1712   case InlineAsm::Constraint_m:
1713     // We just support simple memory operands that have a single address
1714     // operand and need no special handling.
1715     OutOps.push_back(Op);
1716     return false;
1717   case InlineAsm::Constraint_A:
1718     OutOps.push_back(Op);
1719     return false;
1720   default:
1721     break;
1722   }
1723 
1724   return true;
1725 }
1726 
1727 bool RISCVDAGToDAGISel::SelectAddrFI(SDValue Addr, SDValue &Base) {
1728   if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
1729     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT());
1730     return true;
1731   }
1732   return false;
1733 }
1734 
1735 bool RISCVDAGToDAGISel::SelectBaseAddr(SDValue Addr, SDValue &Base) {
1736   // If this is FrameIndex, select it directly. Otherwise just let it get
1737   // selected to a register independently.
1738   if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr))
1739     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT());
1740   else
1741     Base = Addr;
1742   return true;
1743 }
1744 
1745 bool RISCVDAGToDAGISel::selectShiftMask(SDValue N, unsigned ShiftWidth,
1746                                         SDValue &ShAmt) {
1747   // Shift instructions on RISCV only read the lower 5 or 6 bits of the shift
1748   // amount. If there is an AND on the shift amount, we can bypass it if it
1749   // doesn't affect any of those bits.
1750   if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(N.getOperand(1))) {
1751     const APInt &AndMask = N->getConstantOperandAPInt(1);
1752 
1753     // Since the max shift amount is a power of 2 we can subtract 1 to make a
1754     // mask that covers the bits needed to represent all shift amounts.
1755     assert(isPowerOf2_32(ShiftWidth) && "Unexpected max shift amount!");
1756     APInt ShMask(AndMask.getBitWidth(), ShiftWidth - 1);
1757 
1758     if (ShMask.isSubsetOf(AndMask)) {
1759       ShAmt = N.getOperand(0);
1760       return true;
1761     }
1762 
1763     // SimplifyDemandedBits may have optimized the mask so try restoring any
1764     // bits that are known zero.
1765     KnownBits Known = CurDAG->computeKnownBits(N->getOperand(0));
1766     if (ShMask.isSubsetOf(AndMask | Known.Zero)) {
1767       ShAmt = N.getOperand(0);
1768       return true;
1769     }
1770   } else if (N.getOpcode() == ISD::SUB &&
1771              isa<ConstantSDNode>(N.getOperand(0))) {
1772     uint64_t Imm = N.getConstantOperandVal(0);
1773     // If we are shifting by N-X where N == 0 mod Size, then just shift by -X to
1774     // generate a NEG instead of a SUB of a constant.
1775     if (Imm != 0 && Imm % ShiftWidth == 0) {
1776       SDLoc DL(N);
1777       EVT VT = N.getValueType();
1778       SDValue Zero =
1779           CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, RISCV::X0, VT);
1780       unsigned NegOpc = VT == MVT::i64 ? RISCV::SUBW : RISCV::SUB;
1781       MachineSDNode *Neg = CurDAG->getMachineNode(NegOpc, DL, VT, Zero,
1782                                                   N.getOperand(1));
1783       ShAmt = SDValue(Neg, 0);
1784       return true;
1785     }
1786   }
1787 
1788   ShAmt = N;
1789   return true;
1790 }
1791 
1792 bool RISCVDAGToDAGISel::selectSExti32(SDValue N, SDValue &Val) {
1793   if (N.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1794       cast<VTSDNode>(N.getOperand(1))->getVT() == MVT::i32) {
1795     Val = N.getOperand(0);
1796     return true;
1797   }
1798   MVT VT = N.getSimpleValueType();
1799   if (CurDAG->ComputeNumSignBits(N) > (VT.getSizeInBits() - 32)) {
1800     Val = N;
1801     return true;
1802   }
1803 
1804   return false;
1805 }
1806 
1807 bool RISCVDAGToDAGISel::selectZExti32(SDValue N, SDValue &Val) {
1808   if (N.getOpcode() == ISD::AND) {
1809     auto *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
1810     if (C && C->getZExtValue() == UINT64_C(0xFFFFFFFF)) {
1811       Val = N.getOperand(0);
1812       return true;
1813     }
1814   }
1815   MVT VT = N.getSimpleValueType();
1816   APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(), 32);
1817   if (CurDAG->MaskedValueIsZero(N, Mask)) {
1818     Val = N;
1819     return true;
1820   }
1821 
1822   return false;
1823 }
1824 
1825 // Return true if all users of this SDNode* only consume the lower \p Bits.
1826 // This can be used to form W instructions for add/sub/mul/shl even when the
1827 // root isn't a sext_inreg. This can allow the ADDW/SUBW/MULW/SLLIW to CSE if
1828 // SimplifyDemandedBits has made it so some users see a sext_inreg and some
1829 // don't. The sext_inreg+add/sub/mul/shl will get selected, but still leave
1830 // the add/sub/mul/shl to become non-W instructions. By checking the users we
1831 // may be able to use a W instruction and CSE with the other instruction if
1832 // this has happened. We could try to detect that the CSE opportunity exists
1833 // before doing this, but that would be more complicated.
1834 // TODO: Does this need to look through AND/OR/XOR to their users to find more
1835 // opportunities.
1836 bool RISCVDAGToDAGISel::hasAllNBitUsers(SDNode *Node, unsigned Bits) const {
1837   assert((Node->getOpcode() == ISD::ADD || Node->getOpcode() == ISD::SUB ||
1838           Node->getOpcode() == ISD::MUL || Node->getOpcode() == ISD::SHL ||
1839           Node->getOpcode() == ISD::SRL ||
1840           Node->getOpcode() == ISD::SIGN_EXTEND_INREG ||
1841           Node->getOpcode() == RISCVISD::GREV ||
1842           Node->getOpcode() == RISCVISD::GORC ||
1843           isa<ConstantSDNode>(Node)) &&
1844          "Unexpected opcode");
1845 
1846   for (auto UI = Node->use_begin(), UE = Node->use_end(); UI != UE; ++UI) {
1847     SDNode *User = *UI;
1848     // Users of this node should have already been instruction selected
1849     if (!User->isMachineOpcode())
1850       return false;
1851 
1852     // TODO: Add more opcodes?
1853     switch (User->getMachineOpcode()) {
1854     default:
1855       return false;
1856     case RISCV::ADDW:
1857     case RISCV::ADDIW:
1858     case RISCV::SUBW:
1859     case RISCV::MULW:
1860     case RISCV::SLLW:
1861     case RISCV::SLLIW:
1862     case RISCV::SRAW:
1863     case RISCV::SRAIW:
1864     case RISCV::SRLW:
1865     case RISCV::SRLIW:
1866     case RISCV::DIVW:
1867     case RISCV::DIVUW:
1868     case RISCV::REMW:
1869     case RISCV::REMUW:
1870     case RISCV::ROLW:
1871     case RISCV::RORW:
1872     case RISCV::RORIW:
1873     case RISCV::CLZW:
1874     case RISCV::CTZW:
1875     case RISCV::CPOPW:
1876     case RISCV::SLLI_UW:
1877     case RISCV::FMV_W_X:
1878     case RISCV::FCVT_H_W:
1879     case RISCV::FCVT_H_WU:
1880     case RISCV::FCVT_S_W:
1881     case RISCV::FCVT_S_WU:
1882     case RISCV::FCVT_D_W:
1883     case RISCV::FCVT_D_WU:
1884       if (Bits < 32)
1885         return false;
1886       break;
1887     case RISCV::SLLI:
1888       // SLLI only uses the lower (XLen - ShAmt) bits.
1889       if (Bits < Subtarget->getXLen() - User->getConstantOperandVal(1))
1890         return false;
1891       break;
1892     case RISCV::ANDI:
1893       if (Bits < (64 - countLeadingZeros(User->getConstantOperandVal(1))))
1894         return false;
1895       break;
1896     case RISCV::SEXT_B:
1897       if (Bits < 8)
1898         return false;
1899       break;
1900     case RISCV::SEXT_H:
1901     case RISCV::FMV_H_X:
1902     case RISCV::ZEXT_H_RV32:
1903     case RISCV::ZEXT_H_RV64:
1904       if (Bits < 16)
1905         return false;
1906       break;
1907     case RISCV::ADD_UW:
1908     case RISCV::SH1ADD_UW:
1909     case RISCV::SH2ADD_UW:
1910     case RISCV::SH3ADD_UW:
1911       // The first operand to add.uw/shXadd.uw is implicitly zero extended from
1912       // 32 bits.
1913       if (UI.getOperandNo() != 0 || Bits < 32)
1914         return false;
1915       break;
1916     case RISCV::SB:
1917       if (UI.getOperandNo() != 0 || Bits < 8)
1918         return false;
1919       break;
1920     case RISCV::SH:
1921       if (UI.getOperandNo() != 0 || Bits < 16)
1922         return false;
1923       break;
1924     case RISCV::SW:
1925       if (UI.getOperandNo() != 0 || Bits < 32)
1926         return false;
1927       break;
1928     }
1929   }
1930 
1931   return true;
1932 }
1933 
1934 // Select VL as a 5 bit immediate or a value that will become a register. This
1935 // allows us to choose betwen VSETIVLI or VSETVLI later.
1936 bool RISCVDAGToDAGISel::selectVLOp(SDValue N, SDValue &VL) {
1937   auto *C = dyn_cast<ConstantSDNode>(N);
1938   if (C && isUInt<5>(C->getZExtValue())) {
1939     VL = CurDAG->getTargetConstant(C->getZExtValue(), SDLoc(N),
1940                                    N->getValueType(0));
1941   } else if (C && C->isAllOnesValue()) {
1942     // Treat all ones as VLMax.
1943     VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
1944                                    N->getValueType(0));
1945   } else if (isa<RegisterSDNode>(N) &&
1946              cast<RegisterSDNode>(N)->getReg() == RISCV::X0) {
1947     // All our VL operands use an operand that allows GPRNoX0 or an immediate
1948     // as the register class. Convert X0 to a special immediate to pass the
1949     // MachineVerifier. This is recognized specially by the vsetvli insertion
1950     // pass.
1951     VL = CurDAG->getTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
1952                                    N->getValueType(0));
1953   } else {
1954     VL = N;
1955   }
1956 
1957   return true;
1958 }
1959 
1960 bool RISCVDAGToDAGISel::selectVSplat(SDValue N, SDValue &SplatVal) {
1961   if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef())
1962     return false;
1963   SplatVal = N.getOperand(1);
1964   return true;
1965 }
1966 
1967 using ValidateFn = bool (*)(int64_t);
1968 
1969 static bool selectVSplatSimmHelper(SDValue N, SDValue &SplatVal,
1970                                    SelectionDAG &DAG,
1971                                    const RISCVSubtarget &Subtarget,
1972                                    ValidateFn ValidateImm) {
1973   if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef() ||
1974       !isa<ConstantSDNode>(N.getOperand(1)))
1975     return false;
1976 
1977   int64_t SplatImm =
1978       cast<ConstantSDNode>(N.getOperand(1))->getSExtValue();
1979 
1980   // The semantics of RISCVISD::VMV_V_X_VL is that when the operand
1981   // type is wider than the resulting vector element type: an implicit
1982   // truncation first takes place. Therefore, perform a manual
1983   // truncation/sign-extension in order to ignore any truncated bits and catch
1984   // any zero-extended immediate.
1985   // For example, we wish to match (i8 -1) -> (XLenVT 255) as a simm5 by first
1986   // sign-extending to (XLenVT -1).
1987   MVT XLenVT = Subtarget.getXLenVT();
1988   assert(XLenVT == N.getOperand(1).getSimpleValueType() &&
1989          "Unexpected splat operand type");
1990   MVT EltVT = N.getSimpleValueType().getVectorElementType();
1991   if (EltVT.bitsLT(XLenVT))
1992     SplatImm = SignExtend64(SplatImm, EltVT.getSizeInBits());
1993 
1994   if (!ValidateImm(SplatImm))
1995     return false;
1996 
1997   SplatVal = DAG.getTargetConstant(SplatImm, SDLoc(N), XLenVT);
1998   return true;
1999 }
2000 
2001 bool RISCVDAGToDAGISel::selectVSplatSimm5(SDValue N, SDValue &SplatVal) {
2002   return selectVSplatSimmHelper(N, SplatVal, *CurDAG, *Subtarget,
2003                                 [](int64_t Imm) { return isInt<5>(Imm); });
2004 }
2005 
2006 bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1(SDValue N, SDValue &SplatVal) {
2007   return selectVSplatSimmHelper(
2008       N, SplatVal, *CurDAG, *Subtarget,
2009       [](int64_t Imm) { return (isInt<5>(Imm) && Imm != -16) || Imm == 16; });
2010 }
2011 
2012 bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1NonZero(SDValue N,
2013                                                       SDValue &SplatVal) {
2014   return selectVSplatSimmHelper(
2015       N, SplatVal, *CurDAG, *Subtarget, [](int64_t Imm) {
2016         return Imm != 0 && ((isInt<5>(Imm) && Imm != -16) || Imm == 16);
2017       });
2018 }
2019 
2020 bool RISCVDAGToDAGISel::selectVSplatUimm5(SDValue N, SDValue &SplatVal) {
2021   if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef() ||
2022       !isa<ConstantSDNode>(N.getOperand(1)))
2023     return false;
2024 
2025   int64_t SplatImm =
2026       cast<ConstantSDNode>(N.getOperand(1))->getSExtValue();
2027 
2028   if (!isUInt<5>(SplatImm))
2029     return false;
2030 
2031   SplatVal =
2032       CurDAG->getTargetConstant(SplatImm, SDLoc(N), Subtarget->getXLenVT());
2033 
2034   return true;
2035 }
2036 
2037 bool RISCVDAGToDAGISel::selectRVVSimm5(SDValue N, unsigned Width,
2038                                        SDValue &Imm) {
2039   if (auto *C = dyn_cast<ConstantSDNode>(N)) {
2040     int64_t ImmVal = SignExtend64(C->getSExtValue(), Width);
2041 
2042     if (!isInt<5>(ImmVal))
2043       return false;
2044 
2045     Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), Subtarget->getXLenVT());
2046     return true;
2047   }
2048 
2049   return false;
2050 }
2051 
2052 // Merge an ADDI into the offset of a load/store instruction where possible.
2053 // (load (addi base, off1), off2) -> (load base, off1+off2)
2054 // (store val, (addi base, off1), off2) -> (store val, base, off1+off2)
2055 // (load (add base, (addi src, off1)), off2)
2056 //    -> (load (add base, src), off1+off2)
2057 // (store val, (add base, (addi src, off1)), off2)
2058 //    -> (store val, (add base, src), off1+off2)
2059 // This is possible when off1+off2 fits a 12-bit immediate.
2060 bool RISCVDAGToDAGISel::doPeepholeLoadStoreADDI(SDNode *N) {
2061   int OffsetOpIdx;
2062   int BaseOpIdx;
2063 
2064   // Only attempt this optimisation for I-type loads and S-type stores.
2065   switch (N->getMachineOpcode()) {
2066   default:
2067     return false;
2068   case RISCV::LB:
2069   case RISCV::LH:
2070   case RISCV::LW:
2071   case RISCV::LBU:
2072   case RISCV::LHU:
2073   case RISCV::LWU:
2074   case RISCV::LD:
2075   case RISCV::FLH:
2076   case RISCV::FLW:
2077   case RISCV::FLD:
2078     BaseOpIdx = 0;
2079     OffsetOpIdx = 1;
2080     break;
2081   case RISCV::SB:
2082   case RISCV::SH:
2083   case RISCV::SW:
2084   case RISCV::SD:
2085   case RISCV::FSH:
2086   case RISCV::FSW:
2087   case RISCV::FSD:
2088     BaseOpIdx = 1;
2089     OffsetOpIdx = 2;
2090     break;
2091   }
2092 
2093   if (!isa<ConstantSDNode>(N->getOperand(OffsetOpIdx)))
2094     return false;
2095 
2096   SDValue Base = N->getOperand(BaseOpIdx);
2097 
2098   if (!Base.isMachineOpcode())
2099     return false;
2100 
2101   // There is a ADD between ADDI and load/store. We can only fold ADDI that
2102   // do not have a FrameIndex operand.
2103   SDValue Add;
2104   int AddBaseIdx;
2105   if (Base.getMachineOpcode() == RISCV::ADD) {
2106     if (!Base.hasOneUse())
2107       return false;
2108     Add = Base;
2109     SDValue Op0 = Base.getOperand(0);
2110     SDValue Op1 = Base.getOperand(1);
2111     if (Op0.isMachineOpcode() && Op0.getMachineOpcode() == RISCV::ADDI &&
2112         !isa<FrameIndexSDNode>(Op0.getOperand(0)) &&
2113         isa<ConstantSDNode>(Op0.getOperand(1))) {
2114       AddBaseIdx = 1;
2115       Base = Op0;
2116     } else if (Op1.isMachineOpcode() && Op1.getMachineOpcode() == RISCV::ADDI &&
2117                !isa<FrameIndexSDNode>(Op1.getOperand(0)) &&
2118                isa<ConstantSDNode>(Op1.getOperand(1))) {
2119       AddBaseIdx = 0;
2120       Base = Op1;
2121     } else
2122       return false;
2123   }
2124 
2125   // If the base is an ADDI, we can merge it in to the load/store.
2126   if (Base.getMachineOpcode() != RISCV::ADDI)
2127     return false;
2128 
2129   SDValue ImmOperand = Base.getOperand(1);
2130   uint64_t Offset2 = N->getConstantOperandVal(OffsetOpIdx);
2131 
2132   if (auto *Const = dyn_cast<ConstantSDNode>(ImmOperand)) {
2133     int64_t Offset1 = Const->getSExtValue();
2134     int64_t CombinedOffset = Offset1 + Offset2;
2135     if (!isInt<12>(CombinedOffset))
2136       return false;
2137     ImmOperand = CurDAG->getTargetConstant(CombinedOffset, SDLoc(ImmOperand),
2138                                            ImmOperand.getValueType());
2139   } else if (auto *GA = dyn_cast<GlobalAddressSDNode>(ImmOperand)) {
2140     // If the off1 in (addi base, off1) is a global variable's address (its
2141     // low part, really), then we can rely on the alignment of that variable
2142     // to provide a margin of safety before off1 can overflow the 12 bits.
2143     // Check if off2 falls within that margin; if so off1+off2 can't overflow.
2144     const DataLayout &DL = CurDAG->getDataLayout();
2145     Align Alignment = GA->getGlobal()->getPointerAlignment(DL);
2146     if (Offset2 != 0 && Alignment <= Offset2)
2147       return false;
2148     int64_t Offset1 = GA->getOffset();
2149     int64_t CombinedOffset = Offset1 + Offset2;
2150     ImmOperand = CurDAG->getTargetGlobalAddress(
2151         GA->getGlobal(), SDLoc(ImmOperand), ImmOperand.getValueType(),
2152         CombinedOffset, GA->getTargetFlags());
2153   } else if (auto *CP = dyn_cast<ConstantPoolSDNode>(ImmOperand)) {
2154     // Ditto.
2155     Align Alignment = CP->getAlign();
2156     if (Offset2 != 0 && Alignment <= Offset2)
2157       return false;
2158     int64_t Offset1 = CP->getOffset();
2159     int64_t CombinedOffset = Offset1 + Offset2;
2160     ImmOperand = CurDAG->getTargetConstantPool(
2161         CP->getConstVal(), ImmOperand.getValueType(), CP->getAlign(),
2162         CombinedOffset, CP->getTargetFlags());
2163   } else {
2164     return false;
2165   }
2166 
2167   LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase:    ");
2168   LLVM_DEBUG(Base->dump(CurDAG));
2169   LLVM_DEBUG(dbgs() << "\nN: ");
2170   LLVM_DEBUG(N->dump(CurDAG));
2171   LLVM_DEBUG(dbgs() << "\n");
2172 
2173   if (Add)
2174     Add = SDValue(CurDAG->UpdateNodeOperands(Add.getNode(),
2175                                              Add.getOperand(AddBaseIdx),
2176                                              Base.getOperand(0)),
2177                   0);
2178 
2179   // Modify the offset operand of the load/store.
2180   if (BaseOpIdx == 0) { // Load
2181     if (Add)
2182       N = CurDAG->UpdateNodeOperands(N, Add, ImmOperand, N->getOperand(2));
2183     else
2184       N = CurDAG->UpdateNodeOperands(N, Base.getOperand(0), ImmOperand,
2185                                      N->getOperand(2));
2186   } else { // Store
2187     if (Add)
2188       N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), Add, ImmOperand,
2189                                      N->getOperand(3));
2190     else
2191       N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), Base.getOperand(0),
2192                                      ImmOperand, N->getOperand(3));
2193   }
2194 
2195   return true;
2196 }
2197 
2198 // Try to remove sext.w if the input is a W instruction or can be made into
2199 // a W instruction cheaply.
2200 bool RISCVDAGToDAGISel::doPeepholeSExtW(SDNode *N) {
2201   // Look for the sext.w pattern, addiw rd, rs1, 0.
2202   if (N->getMachineOpcode() != RISCV::ADDIW ||
2203       !isNullConstant(N->getOperand(1)))
2204     return false;
2205 
2206   SDValue N0 = N->getOperand(0);
2207   if (!N0.isMachineOpcode())
2208     return false;
2209 
2210   switch (N0.getMachineOpcode()) {
2211   default:
2212     break;
2213   case RISCV::ADD:
2214   case RISCV::ADDI:
2215   case RISCV::SUB:
2216   case RISCV::MUL:
2217   case RISCV::SLLI: {
2218     // Convert sext.w+add/sub/mul to their W instructions. This will create
2219     // a new independent instruction. This improves latency.
2220     unsigned Opc;
2221     switch (N0.getMachineOpcode()) {
2222     default:
2223       llvm_unreachable("Unexpected opcode!");
2224     case RISCV::ADD:  Opc = RISCV::ADDW;  break;
2225     case RISCV::ADDI: Opc = RISCV::ADDIW; break;
2226     case RISCV::SUB:  Opc = RISCV::SUBW;  break;
2227     case RISCV::MUL:  Opc = RISCV::MULW;  break;
2228     case RISCV::SLLI: Opc = RISCV::SLLIW; break;
2229     }
2230 
2231     SDValue N00 = N0.getOperand(0);
2232     SDValue N01 = N0.getOperand(1);
2233 
2234     // Shift amount needs to be uimm5.
2235     if (N0.getMachineOpcode() == RISCV::SLLI &&
2236         !isUInt<5>(cast<ConstantSDNode>(N01)->getSExtValue()))
2237       break;
2238 
2239     SDNode *Result =
2240         CurDAG->getMachineNode(Opc, SDLoc(N), N->getValueType(0),
2241                                N00, N01);
2242     ReplaceUses(N, Result);
2243     return true;
2244   }
2245   case RISCV::ADDW:
2246   case RISCV::ADDIW:
2247   case RISCV::SUBW:
2248   case RISCV::MULW:
2249   case RISCV::SLLIW:
2250   case RISCV::GREVIW:
2251   case RISCV::GORCIW:
2252     // Result is already sign extended just remove the sext.w.
2253     // NOTE: We only handle the nodes that are selected with hasAllWUsers.
2254     ReplaceUses(N, N0.getNode());
2255     return true;
2256   }
2257 
2258   return false;
2259 }
2260 
2261 // Optimize masked RVV pseudo instructions with a known all-ones mask to their
2262 // corresponding "unmasked" pseudo versions. The mask we're interested in will
2263 // take the form of a V0 physical register operand, with a glued
2264 // register-setting instruction.
2265 bool RISCVDAGToDAGISel::doPeepholeMaskedRVV(SDNode *N) {
2266   const RISCV::RISCVMaskedPseudoInfo *I =
2267       RISCV::getMaskedPseudoInfo(N->getMachineOpcode());
2268   if (!I)
2269     return false;
2270 
2271   unsigned MaskOpIdx = I->MaskOpIdx;
2272 
2273   // Check that we're using V0 as a mask register.
2274   if (!isa<RegisterSDNode>(N->getOperand(MaskOpIdx)) ||
2275       cast<RegisterSDNode>(N->getOperand(MaskOpIdx))->getReg() != RISCV::V0)
2276     return false;
2277 
2278   // The glued user defines V0.
2279   const auto *Glued = N->getGluedNode();
2280 
2281   if (!Glued || Glued->getOpcode() != ISD::CopyToReg)
2282     return false;
2283 
2284   // Check that we're defining V0 as a mask register.
2285   if (!isa<RegisterSDNode>(Glued->getOperand(1)) ||
2286       cast<RegisterSDNode>(Glued->getOperand(1))->getReg() != RISCV::V0)
2287     return false;
2288 
2289   // Check the instruction defining V0; it needs to be a VMSET pseudo.
2290   SDValue MaskSetter = Glued->getOperand(2);
2291 
2292   const auto IsVMSet = [](unsigned Opc) {
2293     return Opc == RISCV::PseudoVMSET_M_B1 || Opc == RISCV::PseudoVMSET_M_B16 ||
2294            Opc == RISCV::PseudoVMSET_M_B2 || Opc == RISCV::PseudoVMSET_M_B32 ||
2295            Opc == RISCV::PseudoVMSET_M_B4 || Opc == RISCV::PseudoVMSET_M_B64 ||
2296            Opc == RISCV::PseudoVMSET_M_B8;
2297   };
2298 
2299   // TODO: Check that the VMSET is the expected bitwidth? The pseudo has
2300   // undefined behaviour if it's the wrong bitwidth, so we could choose to
2301   // assume that it's all-ones? Same applies to its VL.
2302   if (!MaskSetter->isMachineOpcode() || !IsVMSet(MaskSetter.getMachineOpcode()))
2303     return false;
2304 
2305   // Retrieve the tail policy operand index, if any.
2306   Optional<unsigned> TailPolicyOpIdx;
2307   const RISCVInstrInfo *TII = static_cast<const RISCVInstrInfo *>(
2308       CurDAG->getSubtarget().getInstrInfo());
2309 
2310   const MCInstrDesc &MaskedMCID = TII->get(N->getMachineOpcode());
2311 
2312   bool IsTA = true;
2313   if (RISCVII::hasVecPolicyOp(MaskedMCID.TSFlags)) {
2314     // The last operand of the pseudo is the policy op, but we might have a
2315     // Glue operand last. We might also have a chain.
2316     TailPolicyOpIdx = N->getNumOperands() - 1;
2317     if (N->getOperand(*TailPolicyOpIdx).getValueType() == MVT::Glue)
2318       (*TailPolicyOpIdx)--;
2319     if (N->getOperand(*TailPolicyOpIdx).getValueType() == MVT::Other)
2320       (*TailPolicyOpIdx)--;
2321 
2322     if (!(N->getConstantOperandVal(*TailPolicyOpIdx) &
2323           RISCVII::TAIL_AGNOSTIC)) {
2324       // Keep the true-masked instruction when there is no unmasked TU
2325       // instruction
2326       if (I->UnmaskedTUPseudo == I->MaskedPseudo && !N->getOperand(0).isUndef())
2327         return false;
2328       // We can't use TA if the tie-operand is not IMPLICIT_DEF
2329       if (!N->getOperand(0).isUndef())
2330         IsTA = false;
2331     }
2332   }
2333 
2334   if (IsTA) {
2335     uint64_t TSFlags = TII->get(I->UnmaskedPseudo).TSFlags;
2336 
2337     // Check that we're dropping the merge operand, the mask operand, and any
2338     // policy operand when we transform to this unmasked pseudo.
2339     assert(!RISCVII::hasMergeOp(TSFlags) && RISCVII::hasDummyMaskOp(TSFlags) &&
2340            !RISCVII::hasVecPolicyOp(TSFlags) &&
2341            "Unexpected pseudo to transform to");
2342     (void)TSFlags;
2343   } else {
2344     uint64_t TSFlags = TII->get(I->UnmaskedTUPseudo).TSFlags;
2345 
2346     // Check that we're dropping the mask operand, and any policy operand
2347     // when we transform to this unmasked tu pseudo.
2348     assert(RISCVII::hasMergeOp(TSFlags) && RISCVII::hasDummyMaskOp(TSFlags) &&
2349            !RISCVII::hasVecPolicyOp(TSFlags) &&
2350            "Unexpected pseudo to transform to");
2351     (void)TSFlags;
2352   }
2353 
2354   unsigned Opc = IsTA ? I->UnmaskedPseudo : I->UnmaskedTUPseudo;
2355   SmallVector<SDValue, 8> Ops;
2356   // Skip the merge operand at index 0 if IsTA
2357   for (unsigned I = IsTA, E = N->getNumOperands(); I != E; I++) {
2358     // Skip the mask, the policy, and the Glue.
2359     SDValue Op = N->getOperand(I);
2360     if (I == MaskOpIdx || I == TailPolicyOpIdx ||
2361         Op.getValueType() == MVT::Glue)
2362       continue;
2363     Ops.push_back(Op);
2364   }
2365 
2366   // Transitively apply any node glued to our new node.
2367   if (auto *TGlued = Glued->getGluedNode())
2368     Ops.push_back(SDValue(TGlued, TGlued->getNumValues() - 1));
2369 
2370   SDNode *Result = CurDAG->getMachineNode(Opc, SDLoc(N), N->getVTList(), Ops);
2371   ReplaceUses(N, Result);
2372 
2373   return true;
2374 }
2375 
2376 // This pass converts a legalized DAG into a RISCV-specific DAG, ready
2377 // for instruction scheduling.
2378 FunctionPass *llvm::createRISCVISelDag(RISCVTargetMachine &TM) {
2379   return new RISCVDAGToDAGISel(TM);
2380 }
2381