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