1 //===-- RISCVISelLowering.cpp - RISCV DAG Lowering Implementation  --------===//
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 the interfaces that RISCV uses to lower LLVM code into a
10 // selection DAG.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "RISCVISelLowering.h"
15 #include "MCTargetDesc/RISCVMatInt.h"
16 #include "RISCV.h"
17 #include "RISCVMachineFunctionInfo.h"
18 #include "RISCVRegisterInfo.h"
19 #include "RISCVSubtarget.h"
20 #include "RISCVTargetMachine.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "llvm/CodeGen/ValueTypes.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/DiagnosticPrinter.h"
31 #include "llvm/IR/IntrinsicsRISCV.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/KnownBits.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/raw_ostream.h"
38 
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "riscv-lower"
42 
43 STATISTIC(NumTailCalls, "Number of tail calls");
44 
45 RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
46                                          const RISCVSubtarget &STI)
47     : TargetLowering(TM), Subtarget(STI) {
48 
49   if (Subtarget.isRV32E())
50     report_fatal_error("Codegen not yet implemented for RV32E");
51 
52   RISCVABI::ABI ABI = Subtarget.getTargetABI();
53   assert(ABI != RISCVABI::ABI_Unknown && "Improperly initialised target ABI");
54 
55   if ((ABI == RISCVABI::ABI_ILP32F || ABI == RISCVABI::ABI_LP64F) &&
56       !Subtarget.hasStdExtF()) {
57     errs() << "Hard-float 'f' ABI can't be used for a target that "
58                 "doesn't support the F instruction set extension (ignoring "
59                           "target-abi)\n";
60     ABI = Subtarget.is64Bit() ? RISCVABI::ABI_LP64 : RISCVABI::ABI_ILP32;
61   } else if ((ABI == RISCVABI::ABI_ILP32D || ABI == RISCVABI::ABI_LP64D) &&
62              !Subtarget.hasStdExtD()) {
63     errs() << "Hard-float 'd' ABI can't be used for a target that "
64               "doesn't support the D instruction set extension (ignoring "
65               "target-abi)\n";
66     ABI = Subtarget.is64Bit() ? RISCVABI::ABI_LP64 : RISCVABI::ABI_ILP32;
67   }
68 
69   switch (ABI) {
70   default:
71     report_fatal_error("Don't know how to lower this ABI");
72   case RISCVABI::ABI_ILP32:
73   case RISCVABI::ABI_ILP32F:
74   case RISCVABI::ABI_ILP32D:
75   case RISCVABI::ABI_LP64:
76   case RISCVABI::ABI_LP64F:
77   case RISCVABI::ABI_LP64D:
78     break;
79   }
80 
81   MVT XLenVT = Subtarget.getXLenVT();
82 
83   // Set up the register classes.
84   addRegisterClass(XLenVT, &RISCV::GPRRegClass);
85 
86   if (Subtarget.hasStdExtZfh())
87     addRegisterClass(MVT::f16, &RISCV::FPR16RegClass);
88   if (Subtarget.hasStdExtF())
89     addRegisterClass(MVT::f32, &RISCV::FPR32RegClass);
90   if (Subtarget.hasStdExtD())
91     addRegisterClass(MVT::f64, &RISCV::FPR64RegClass);
92 
93   static const MVT::SimpleValueType BoolVecVTs[] = {
94       MVT::nxv1i1,  MVT::nxv2i1,  MVT::nxv4i1, MVT::nxv8i1,
95       MVT::nxv16i1, MVT::nxv32i1, MVT::nxv64i1};
96   static const MVT::SimpleValueType IntVecVTs[] = {
97       MVT::nxv1i8,  MVT::nxv2i8,   MVT::nxv4i8,   MVT::nxv8i8,  MVT::nxv16i8,
98       MVT::nxv32i8, MVT::nxv64i8,  MVT::nxv1i16,  MVT::nxv2i16, MVT::nxv4i16,
99       MVT::nxv8i16, MVT::nxv16i16, MVT::nxv32i16, MVT::nxv1i32, MVT::nxv2i32,
100       MVT::nxv4i32, MVT::nxv8i32,  MVT::nxv16i32, MVT::nxv1i64, MVT::nxv2i64,
101       MVT::nxv4i64, MVT::nxv8i64};
102   static const MVT::SimpleValueType F16VecVTs[] = {
103       MVT::nxv1f16, MVT::nxv2f16,  MVT::nxv4f16,
104       MVT::nxv8f16, MVT::nxv16f16, MVT::nxv32f16};
105   static const MVT::SimpleValueType F32VecVTs[] = {
106       MVT::nxv1f32, MVT::nxv2f32, MVT::nxv4f32, MVT::nxv8f32, MVT::nxv16f32};
107   static const MVT::SimpleValueType F64VecVTs[] = {
108       MVT::nxv1f64, MVT::nxv2f64, MVT::nxv4f64, MVT::nxv8f64};
109 
110   if (Subtarget.hasStdExtV()) {
111     auto addRegClassForRVV = [this](MVT VT) {
112       unsigned Size = VT.getSizeInBits().getKnownMinValue();
113       assert(Size <= 512 && isPowerOf2_32(Size));
114       const TargetRegisterClass *RC;
115       if (Size <= 64)
116         RC = &RISCV::VRRegClass;
117       else if (Size == 128)
118         RC = &RISCV::VRM2RegClass;
119       else if (Size == 256)
120         RC = &RISCV::VRM4RegClass;
121       else
122         RC = &RISCV::VRM8RegClass;
123 
124       addRegisterClass(VT, RC);
125     };
126 
127     for (MVT VT : BoolVecVTs)
128       addRegClassForRVV(VT);
129     for (MVT VT : IntVecVTs)
130       addRegClassForRVV(VT);
131 
132     if (Subtarget.hasStdExtZfh())
133       for (MVT VT : F16VecVTs)
134         addRegClassForRVV(VT);
135 
136     if (Subtarget.hasStdExtF())
137       for (MVT VT : F32VecVTs)
138         addRegClassForRVV(VT);
139 
140     if (Subtarget.hasStdExtD())
141       for (MVT VT : F64VecVTs)
142         addRegClassForRVV(VT);
143 
144     if (Subtarget.useRVVForFixedLengthVectors()) {
145       auto addRegClassForFixedVectors = [this](MVT VT) {
146         MVT ContainerVT = getContainerForFixedLengthVector(VT);
147         unsigned RCID = getRegClassIDForVecVT(ContainerVT);
148         const RISCVRegisterInfo &TRI = *Subtarget.getRegisterInfo();
149         addRegisterClass(VT, TRI.getRegClass(RCID));
150       };
151       for (MVT VT : MVT::integer_fixedlen_vector_valuetypes())
152         if (useRVVForFixedLengthVectorVT(VT))
153           addRegClassForFixedVectors(VT);
154 
155       for (MVT VT : MVT::fp_fixedlen_vector_valuetypes())
156         if (useRVVForFixedLengthVectorVT(VT))
157           addRegClassForFixedVectors(VT);
158     }
159   }
160 
161   // Compute derived properties from the register classes.
162   computeRegisterProperties(STI.getRegisterInfo());
163 
164   setStackPointerRegisterToSaveRestore(RISCV::X2);
165 
166   for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD})
167     setLoadExtAction(N, XLenVT, MVT::i1, Promote);
168 
169   // TODO: add all necessary setOperationAction calls.
170   setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand);
171 
172   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
173   setOperationAction(ISD::BR_CC, XLenVT, Expand);
174   setOperationAction(ISD::BRCOND, MVT::Other, Custom);
175   setOperationAction(ISD::SELECT_CC, XLenVT, Expand);
176 
177   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
178   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
179 
180   setOperationAction(ISD::VASTART, MVT::Other, Custom);
181   setOperationAction(ISD::VAARG, MVT::Other, Expand);
182   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
183   setOperationAction(ISD::VAEND, MVT::Other, Expand);
184 
185   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
186   if (!Subtarget.hasStdExtZbb()) {
187     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
188     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
189   }
190 
191   if (Subtarget.is64Bit()) {
192     setOperationAction(ISD::ADD, MVT::i32, Custom);
193     setOperationAction(ISD::SUB, MVT::i32, Custom);
194     setOperationAction(ISD::SHL, MVT::i32, Custom);
195     setOperationAction(ISD::SRA, MVT::i32, Custom);
196     setOperationAction(ISD::SRL, MVT::i32, Custom);
197 
198     setOperationAction(ISD::UADDO, MVT::i32, Custom);
199     setOperationAction(ISD::USUBO, MVT::i32, Custom);
200     setOperationAction(ISD::UADDSAT, MVT::i32, Custom);
201     setOperationAction(ISD::USUBSAT, MVT::i32, Custom);
202   }
203 
204   if (!Subtarget.hasStdExtM()) {
205     setOperationAction(ISD::MUL, XLenVT, Expand);
206     setOperationAction(ISD::MULHS, XLenVT, Expand);
207     setOperationAction(ISD::MULHU, XLenVT, Expand);
208     setOperationAction(ISD::SDIV, XLenVT, Expand);
209     setOperationAction(ISD::UDIV, XLenVT, Expand);
210     setOperationAction(ISD::SREM, XLenVT, Expand);
211     setOperationAction(ISD::UREM, XLenVT, Expand);
212   } else {
213     if (Subtarget.is64Bit()) {
214       setOperationAction(ISD::MUL, MVT::i32, Custom);
215       setOperationAction(ISD::MUL, MVT::i128, Custom);
216 
217       setOperationAction(ISD::SDIV, MVT::i8, Custom);
218       setOperationAction(ISD::UDIV, MVT::i8, Custom);
219       setOperationAction(ISD::UREM, MVT::i8, Custom);
220       setOperationAction(ISD::SDIV, MVT::i16, Custom);
221       setOperationAction(ISD::UDIV, MVT::i16, Custom);
222       setOperationAction(ISD::UREM, MVT::i16, Custom);
223       setOperationAction(ISD::SDIV, MVT::i32, Custom);
224       setOperationAction(ISD::UDIV, MVT::i32, Custom);
225       setOperationAction(ISD::UREM, MVT::i32, Custom);
226     } else {
227       setOperationAction(ISD::MUL, MVT::i64, Custom);
228     }
229   }
230 
231   setOperationAction(ISD::SDIVREM, XLenVT, Expand);
232   setOperationAction(ISD::UDIVREM, XLenVT, Expand);
233   setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand);
234   setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand);
235 
236   setOperationAction(ISD::SHL_PARTS, XLenVT, Custom);
237   setOperationAction(ISD::SRL_PARTS, XLenVT, Custom);
238   setOperationAction(ISD::SRA_PARTS, XLenVT, Custom);
239 
240   if (Subtarget.hasStdExtZbb() || Subtarget.hasStdExtZbp()) {
241     if (Subtarget.is64Bit()) {
242       setOperationAction(ISD::ROTL, MVT::i32, Custom);
243       setOperationAction(ISD::ROTR, MVT::i32, Custom);
244     }
245   } else {
246     setOperationAction(ISD::ROTL, XLenVT, Expand);
247     setOperationAction(ISD::ROTR, XLenVT, Expand);
248   }
249 
250   if (Subtarget.hasStdExtZbp()) {
251     // Custom lower bswap/bitreverse so we can convert them to GREVI to enable
252     // more combining.
253     setOperationAction(ISD::BITREVERSE, XLenVT,   Custom);
254     setOperationAction(ISD::BSWAP,      XLenVT,   Custom);
255     setOperationAction(ISD::BITREVERSE, MVT::i8,  Custom);
256     // BSWAP i8 doesn't exist.
257     setOperationAction(ISD::BITREVERSE, MVT::i16, Custom);
258     setOperationAction(ISD::BSWAP,      MVT::i16, Custom);
259 
260     if (Subtarget.is64Bit()) {
261       setOperationAction(ISD::BITREVERSE, MVT::i32, Custom);
262       setOperationAction(ISD::BSWAP,      MVT::i32, Custom);
263     }
264   } else {
265     // With Zbb we have an XLen rev8 instruction, but not GREVI. So we'll
266     // pattern match it directly in isel.
267     setOperationAction(ISD::BSWAP, XLenVT,
268                        Subtarget.hasStdExtZbb() ? Legal : Expand);
269   }
270 
271   if (Subtarget.hasStdExtZbb()) {
272     setOperationAction(ISD::SMIN, XLenVT, Legal);
273     setOperationAction(ISD::SMAX, XLenVT, Legal);
274     setOperationAction(ISD::UMIN, XLenVT, Legal);
275     setOperationAction(ISD::UMAX, XLenVT, Legal);
276 
277     if (Subtarget.is64Bit()) {
278       setOperationAction(ISD::CTTZ, MVT::i32, Custom);
279       setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Custom);
280       setOperationAction(ISD::CTLZ, MVT::i32, Custom);
281       setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
282     }
283   } else {
284     setOperationAction(ISD::CTTZ, XLenVT, Expand);
285     setOperationAction(ISD::CTLZ, XLenVT, Expand);
286     setOperationAction(ISD::CTPOP, XLenVT, Expand);
287   }
288 
289   if (Subtarget.hasStdExtZbt()) {
290     setOperationAction(ISD::FSHL, XLenVT, Custom);
291     setOperationAction(ISD::FSHR, XLenVT, Custom);
292     setOperationAction(ISD::SELECT, XLenVT, Legal);
293 
294     if (Subtarget.is64Bit()) {
295       setOperationAction(ISD::FSHL, MVT::i32, Custom);
296       setOperationAction(ISD::FSHR, MVT::i32, Custom);
297     }
298   } else {
299     setOperationAction(ISD::SELECT, XLenVT, Custom);
300   }
301 
302   ISD::CondCode FPCCToExpand[] = {
303       ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETUEQ, ISD::SETUGT,
304       ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE, ISD::SETGT,
305       ISD::SETGE,  ISD::SETNE,  ISD::SETO,   ISD::SETUO};
306 
307   ISD::NodeType FPOpToExpand[] = {
308       ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FP16_TO_FP,
309       ISD::FP_TO_FP16};
310 
311   if (Subtarget.hasStdExtZfh())
312     setOperationAction(ISD::BITCAST, MVT::i16, Custom);
313 
314   if (Subtarget.hasStdExtZfh()) {
315     setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
316     setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
317     setOperationAction(ISD::LRINT, MVT::f16, Legal);
318     setOperationAction(ISD::LLRINT, MVT::f16, Legal);
319     setOperationAction(ISD::LROUND, MVT::f16, Legal);
320     setOperationAction(ISD::LLROUND, MVT::f16, Legal);
321     for (auto CC : FPCCToExpand)
322       setCondCodeAction(CC, MVT::f16, Expand);
323     setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
324     setOperationAction(ISD::SELECT, MVT::f16, Custom);
325     setOperationAction(ISD::BR_CC, MVT::f16, Expand);
326     for (auto Op : FPOpToExpand)
327       setOperationAction(Op, MVT::f16, Expand);
328   }
329 
330   if (Subtarget.hasStdExtF()) {
331     setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
332     setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
333     setOperationAction(ISD::LRINT, MVT::f32, Legal);
334     setOperationAction(ISD::LLRINT, MVT::f32, Legal);
335     setOperationAction(ISD::LROUND, MVT::f32, Legal);
336     setOperationAction(ISD::LLROUND, MVT::f32, Legal);
337     for (auto CC : FPCCToExpand)
338       setCondCodeAction(CC, MVT::f32, Expand);
339     setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
340     setOperationAction(ISD::SELECT, MVT::f32, Custom);
341     setOperationAction(ISD::BR_CC, MVT::f32, Expand);
342     for (auto Op : FPOpToExpand)
343       setOperationAction(Op, MVT::f32, Expand);
344     setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
345     setTruncStoreAction(MVT::f32, MVT::f16, Expand);
346   }
347 
348   if (Subtarget.hasStdExtF() && Subtarget.is64Bit())
349     setOperationAction(ISD::BITCAST, MVT::i32, Custom);
350 
351   if (Subtarget.hasStdExtD()) {
352     setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
353     setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
354     setOperationAction(ISD::LRINT, MVT::f64, Legal);
355     setOperationAction(ISD::LLRINT, MVT::f64, Legal);
356     setOperationAction(ISD::LROUND, MVT::f64, Legal);
357     setOperationAction(ISD::LLROUND, MVT::f64, Legal);
358     for (auto CC : FPCCToExpand)
359       setCondCodeAction(CC, MVT::f64, Expand);
360     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
361     setOperationAction(ISD::SELECT, MVT::f64, Custom);
362     setOperationAction(ISD::BR_CC, MVT::f64, Expand);
363     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
364     setTruncStoreAction(MVT::f64, MVT::f32, Expand);
365     for (auto Op : FPOpToExpand)
366       setOperationAction(Op, MVT::f64, Expand);
367     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
368     setTruncStoreAction(MVT::f64, MVT::f16, Expand);
369   }
370 
371   if (Subtarget.is64Bit()) {
372     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
373     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
374     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Custom);
375     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Custom);
376   }
377 
378   if (Subtarget.hasStdExtF()) {
379     setOperationAction(ISD::FP_TO_UINT_SAT, XLenVT, Custom);
380     setOperationAction(ISD::FP_TO_SINT_SAT, XLenVT, Custom);
381 
382     setOperationAction(ISD::FLT_ROUNDS_, XLenVT, Custom);
383     setOperationAction(ISD::SET_ROUNDING, MVT::Other, Custom);
384   }
385 
386   setOperationAction(ISD::GlobalAddress, XLenVT, Custom);
387   setOperationAction(ISD::BlockAddress, XLenVT, Custom);
388   setOperationAction(ISD::ConstantPool, XLenVT, Custom);
389   setOperationAction(ISD::JumpTable, XLenVT, Custom);
390 
391   setOperationAction(ISD::GlobalTLSAddress, XLenVT, Custom);
392 
393   // TODO: On M-mode only targets, the cycle[h] CSR may not be present.
394   // Unfortunately this can't be determined just from the ISA naming string.
395   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64,
396                      Subtarget.is64Bit() ? Legal : Custom);
397 
398   setOperationAction(ISD::TRAP, MVT::Other, Legal);
399   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
400   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
401   if (Subtarget.is64Bit())
402     setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i32, Custom);
403 
404   if (Subtarget.hasStdExtA()) {
405     setMaxAtomicSizeInBitsSupported(Subtarget.getXLen());
406     setMinCmpXchgSizeInBits(32);
407   } else {
408     setMaxAtomicSizeInBitsSupported(0);
409   }
410 
411   setBooleanContents(ZeroOrOneBooleanContent);
412 
413   if (Subtarget.hasStdExtV()) {
414     setBooleanVectorContents(ZeroOrOneBooleanContent);
415 
416     setOperationAction(ISD::VSCALE, XLenVT, Custom);
417 
418     // RVV intrinsics may have illegal operands.
419     // We also need to custom legalize vmv.x.s.
420     setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i8, Custom);
421     setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i16, Custom);
422     setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom);
423     setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i16, Custom);
424     if (Subtarget.is64Bit()) {
425       setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i32, Custom);
426     } else {
427       setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
428       setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
429     }
430 
431     setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
432 
433     static unsigned IntegerVPOps[] = {
434         ISD::VP_ADD,  ISD::VP_SUB,  ISD::VP_MUL, ISD::VP_SDIV, ISD::VP_UDIV,
435         ISD::VP_SREM, ISD::VP_UREM, ISD::VP_AND, ISD::VP_OR,   ISD::VP_XOR,
436         ISD::VP_ASHR, ISD::VP_LSHR, ISD::VP_SHL};
437 
438     static unsigned FloatingPointVPOps[] = {ISD::VP_FADD, ISD::VP_FSUB,
439                                             ISD::VP_FMUL, ISD::VP_FDIV};
440 
441     if (!Subtarget.is64Bit()) {
442       // We must custom-lower certain vXi64 operations on RV32 due to the vector
443       // element type being illegal.
444       setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::i64, Custom);
445       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::i64, Custom);
446 
447       setOperationAction(ISD::VECREDUCE_ADD, MVT::i64, Custom);
448       setOperationAction(ISD::VECREDUCE_AND, MVT::i64, Custom);
449       setOperationAction(ISD::VECREDUCE_OR, MVT::i64, Custom);
450       setOperationAction(ISD::VECREDUCE_XOR, MVT::i64, Custom);
451       setOperationAction(ISD::VECREDUCE_SMAX, MVT::i64, Custom);
452       setOperationAction(ISD::VECREDUCE_SMIN, MVT::i64, Custom);
453       setOperationAction(ISD::VECREDUCE_UMAX, MVT::i64, Custom);
454       setOperationAction(ISD::VECREDUCE_UMIN, MVT::i64, Custom);
455     }
456 
457     for (MVT VT : BoolVecVTs) {
458       setOperationAction(ISD::SPLAT_VECTOR, VT, Custom);
459 
460       // Mask VTs are custom-expanded into a series of standard nodes
461       setOperationAction(ISD::TRUNCATE, VT, Custom);
462       setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
463       setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
464       setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
465 
466       setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
467       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
468 
469       setOperationAction(ISD::SELECT, VT, Custom);
470       setOperationAction(ISD::SELECT_CC, VT, Expand);
471       setOperationAction(ISD::VSELECT, VT, Expand);
472 
473       setOperationAction(ISD::VECREDUCE_AND, VT, Custom);
474       setOperationAction(ISD::VECREDUCE_OR, VT, Custom);
475       setOperationAction(ISD::VECREDUCE_XOR, VT, Custom);
476 
477       // RVV has native int->float & float->int conversions where the
478       // element type sizes are within one power-of-two of each other. Any
479       // wider distances between type sizes have to be lowered as sequences
480       // which progressively narrow the gap in stages.
481       setOperationAction(ISD::SINT_TO_FP, VT, Custom);
482       setOperationAction(ISD::UINT_TO_FP, VT, Custom);
483       setOperationAction(ISD::FP_TO_SINT, VT, Custom);
484       setOperationAction(ISD::FP_TO_UINT, VT, Custom);
485 
486       // Expand all extending loads to types larger than this, and truncating
487       // stores from types larger than this.
488       for (MVT OtherVT : MVT::integer_scalable_vector_valuetypes()) {
489         setTruncStoreAction(OtherVT, VT, Expand);
490         setLoadExtAction(ISD::EXTLOAD, OtherVT, VT, Expand);
491         setLoadExtAction(ISD::SEXTLOAD, OtherVT, VT, Expand);
492         setLoadExtAction(ISD::ZEXTLOAD, OtherVT, VT, Expand);
493       }
494     }
495 
496     for (MVT VT : IntVecVTs) {
497       setOperationAction(ISD::SPLAT_VECTOR, VT, Legal);
498       setOperationAction(ISD::SPLAT_VECTOR_PARTS, VT, Custom);
499 
500       setOperationAction(ISD::SMIN, VT, Legal);
501       setOperationAction(ISD::SMAX, VT, Legal);
502       setOperationAction(ISD::UMIN, VT, Legal);
503       setOperationAction(ISD::UMAX, VT, Legal);
504 
505       setOperationAction(ISD::ROTL, VT, Expand);
506       setOperationAction(ISD::ROTR, VT, Expand);
507 
508       // Custom-lower extensions and truncations from/to mask types.
509       setOperationAction(ISD::ANY_EXTEND, VT, Custom);
510       setOperationAction(ISD::SIGN_EXTEND, VT, Custom);
511       setOperationAction(ISD::ZERO_EXTEND, VT, Custom);
512 
513       // RVV has native int->float & float->int conversions where the
514       // element type sizes are within one power-of-two of each other. Any
515       // wider distances between type sizes have to be lowered as sequences
516       // which progressively narrow the gap in stages.
517       setOperationAction(ISD::SINT_TO_FP, VT, Custom);
518       setOperationAction(ISD::UINT_TO_FP, VT, Custom);
519       setOperationAction(ISD::FP_TO_SINT, VT, Custom);
520       setOperationAction(ISD::FP_TO_UINT, VT, Custom);
521 
522       setOperationAction(ISD::SADDSAT, VT, Legal);
523       setOperationAction(ISD::UADDSAT, VT, Legal);
524       setOperationAction(ISD::SSUBSAT, VT, Legal);
525       setOperationAction(ISD::USUBSAT, VT, Legal);
526 
527       // Integer VTs are lowered as a series of "RISCVISD::TRUNCATE_VECTOR_VL"
528       // nodes which truncate by one power of two at a time.
529       setOperationAction(ISD::TRUNCATE, VT, Custom);
530 
531       // Custom-lower insert/extract operations to simplify patterns.
532       setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
533       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
534 
535       // Custom-lower reduction operations to set up the corresponding custom
536       // nodes' operands.
537       setOperationAction(ISD::VECREDUCE_ADD, VT, Custom);
538       setOperationAction(ISD::VECREDUCE_AND, VT, Custom);
539       setOperationAction(ISD::VECREDUCE_OR, VT, Custom);
540       setOperationAction(ISD::VECREDUCE_XOR, VT, Custom);
541       setOperationAction(ISD::VECREDUCE_SMAX, VT, Custom);
542       setOperationAction(ISD::VECREDUCE_SMIN, VT, Custom);
543       setOperationAction(ISD::VECREDUCE_UMAX, VT, Custom);
544       setOperationAction(ISD::VECREDUCE_UMIN, VT, Custom);
545 
546       for (unsigned VPOpc : IntegerVPOps)
547         setOperationAction(VPOpc, VT, Custom);
548 
549       setOperationAction(ISD::LOAD, VT, Custom);
550       setOperationAction(ISD::STORE, VT, Custom);
551 
552       setOperationAction(ISD::MLOAD, VT, Custom);
553       setOperationAction(ISD::MSTORE, VT, Custom);
554       setOperationAction(ISD::MGATHER, VT, Custom);
555       setOperationAction(ISD::MSCATTER, VT, Custom);
556 
557       setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
558       setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
559       setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
560 
561       setOperationAction(ISD::SELECT, VT, Custom);
562       setOperationAction(ISD::SELECT_CC, VT, Expand);
563 
564       setOperationAction(ISD::STEP_VECTOR, VT, Custom);
565       setOperationAction(ISD::VECTOR_REVERSE, VT, Custom);
566 
567       for (MVT OtherVT : MVT::integer_scalable_vector_valuetypes()) {
568         setTruncStoreAction(VT, OtherVT, Expand);
569         setLoadExtAction(ISD::EXTLOAD, OtherVT, VT, Expand);
570         setLoadExtAction(ISD::SEXTLOAD, OtherVT, VT, Expand);
571         setLoadExtAction(ISD::ZEXTLOAD, OtherVT, VT, Expand);
572       }
573     }
574 
575     // Expand various CCs to best match the RVV ISA, which natively supports UNE
576     // but no other unordered comparisons, and supports all ordered comparisons
577     // except ONE. Additionally, we expand GT,OGT,GE,OGE for optimization
578     // purposes; they are expanded to their swapped-operand CCs (LT,OLT,LE,OLE),
579     // and we pattern-match those back to the "original", swapping operands once
580     // more. This way we catch both operations and both "vf" and "fv" forms with
581     // fewer patterns.
582     ISD::CondCode VFPCCToExpand[] = {
583         ISD::SETO,   ISD::SETONE, ISD::SETUEQ, ISD::SETUGT,
584         ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUO,
585         ISD::SETGT,  ISD::SETOGT, ISD::SETGE,  ISD::SETOGE,
586     };
587 
588     // Sets common operation actions on RVV floating-point vector types.
589     const auto SetCommonVFPActions = [&](MVT VT) {
590       setOperationAction(ISD::SPLAT_VECTOR, VT, Legal);
591       // RVV has native FP_ROUND & FP_EXTEND conversions where the element type
592       // sizes are within one power-of-two of each other. Therefore conversions
593       // between vXf16 and vXf64 must be lowered as sequences which convert via
594       // vXf32.
595       setOperationAction(ISD::FP_ROUND, VT, Custom);
596       setOperationAction(ISD::FP_EXTEND, VT, Custom);
597       // Custom-lower insert/extract operations to simplify patterns.
598       setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
599       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
600       // Expand various condition codes (explained above).
601       for (auto CC : VFPCCToExpand)
602         setCondCodeAction(CC, VT, Expand);
603 
604       setOperationAction(ISD::FMINNUM, VT, Legal);
605       setOperationAction(ISD::FMAXNUM, VT, Legal);
606 
607       setOperationAction(ISD::VECREDUCE_FADD, VT, Custom);
608       setOperationAction(ISD::VECREDUCE_SEQ_FADD, VT, Custom);
609       setOperationAction(ISD::VECREDUCE_FMIN, VT, Custom);
610       setOperationAction(ISD::VECREDUCE_FMAX, VT, Custom);
611       setOperationAction(ISD::FCOPYSIGN, VT, Legal);
612 
613       setOperationAction(ISD::LOAD, VT, Custom);
614       setOperationAction(ISD::STORE, VT, Custom);
615 
616       setOperationAction(ISD::MLOAD, VT, Custom);
617       setOperationAction(ISD::MSTORE, VT, Custom);
618       setOperationAction(ISD::MGATHER, VT, Custom);
619       setOperationAction(ISD::MSCATTER, VT, Custom);
620 
621       setOperationAction(ISD::SELECT, VT, Custom);
622       setOperationAction(ISD::SELECT_CC, VT, Expand);
623 
624       setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
625       setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
626       setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
627 
628       setOperationAction(ISD::VECTOR_REVERSE, VT, Custom);
629 
630       for (unsigned VPOpc : FloatingPointVPOps)
631         setOperationAction(VPOpc, VT, Custom);
632     };
633 
634     // Sets common extload/truncstore actions on RVV floating-point vector
635     // types.
636     const auto SetCommonVFPExtLoadTruncStoreActions =
637         [&](MVT VT, ArrayRef<MVT::SimpleValueType> SmallerVTs) {
638           for (auto SmallVT : SmallerVTs) {
639             setTruncStoreAction(VT, SmallVT, Expand);
640             setLoadExtAction(ISD::EXTLOAD, VT, SmallVT, Expand);
641           }
642         };
643 
644     if (Subtarget.hasStdExtZfh())
645       for (MVT VT : F16VecVTs)
646         SetCommonVFPActions(VT);
647 
648     for (MVT VT : F32VecVTs) {
649       if (Subtarget.hasStdExtF())
650         SetCommonVFPActions(VT);
651       SetCommonVFPExtLoadTruncStoreActions(VT, F16VecVTs);
652     }
653 
654     for (MVT VT : F64VecVTs) {
655       if (Subtarget.hasStdExtD())
656         SetCommonVFPActions(VT);
657       SetCommonVFPExtLoadTruncStoreActions(VT, F16VecVTs);
658       SetCommonVFPExtLoadTruncStoreActions(VT, F32VecVTs);
659     }
660 
661     if (Subtarget.useRVVForFixedLengthVectors()) {
662       for (MVT VT : MVT::integer_fixedlen_vector_valuetypes()) {
663         if (!useRVVForFixedLengthVectorVT(VT))
664           continue;
665 
666         // By default everything must be expanded.
667         for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op)
668           setOperationAction(Op, VT, Expand);
669         for (MVT OtherVT : MVT::integer_fixedlen_vector_valuetypes()) {
670           setTruncStoreAction(VT, OtherVT, Expand);
671           setLoadExtAction(ISD::EXTLOAD, OtherVT, VT, Expand);
672           setLoadExtAction(ISD::SEXTLOAD, OtherVT, VT, Expand);
673           setLoadExtAction(ISD::ZEXTLOAD, OtherVT, VT, Expand);
674         }
675 
676         // We use EXTRACT_SUBVECTOR as a "cast" from scalable to fixed.
677         setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
678         setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
679 
680         setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
681         setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
682 
683         setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
684         setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
685 
686         setOperationAction(ISD::LOAD, VT, Custom);
687         setOperationAction(ISD::STORE, VT, Custom);
688 
689         setOperationAction(ISD::SETCC, VT, Custom);
690 
691         setOperationAction(ISD::SELECT, VT, Custom);
692 
693         setOperationAction(ISD::TRUNCATE, VT, Custom);
694 
695         setOperationAction(ISD::BITCAST, VT, Custom);
696 
697         setOperationAction(ISD::VECREDUCE_AND, VT, Custom);
698         setOperationAction(ISD::VECREDUCE_OR, VT, Custom);
699         setOperationAction(ISD::VECREDUCE_XOR, VT, Custom);
700 
701         setOperationAction(ISD::SINT_TO_FP, VT, Custom);
702         setOperationAction(ISD::UINT_TO_FP, VT, Custom);
703         setOperationAction(ISD::FP_TO_SINT, VT, Custom);
704         setOperationAction(ISD::FP_TO_UINT, VT, Custom);
705 
706         // Operations below are different for between masks and other vectors.
707         if (VT.getVectorElementType() == MVT::i1) {
708           setOperationAction(ISD::AND, VT, Custom);
709           setOperationAction(ISD::OR, VT, Custom);
710           setOperationAction(ISD::XOR, VT, Custom);
711           continue;
712         }
713 
714         // Use SPLAT_VECTOR to prevent type legalization from destroying the
715         // splats when type legalizing i64 scalar on RV32.
716         // FIXME: Use SPLAT_VECTOR for all types? DAGCombine probably needs
717         // improvements first.
718         if (!Subtarget.is64Bit() && VT.getVectorElementType() == MVT::i64) {
719           setOperationAction(ISD::SPLAT_VECTOR, VT, Custom);
720           setOperationAction(ISD::SPLAT_VECTOR_PARTS, VT, Custom);
721         }
722 
723         setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
724         setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
725 
726         setOperationAction(ISD::MLOAD, VT, Custom);
727         setOperationAction(ISD::MSTORE, VT, Custom);
728         setOperationAction(ISD::MGATHER, VT, Custom);
729         setOperationAction(ISD::MSCATTER, VT, Custom);
730         setOperationAction(ISD::ADD, VT, Custom);
731         setOperationAction(ISD::MUL, VT, Custom);
732         setOperationAction(ISD::SUB, VT, Custom);
733         setOperationAction(ISD::AND, VT, Custom);
734         setOperationAction(ISD::OR, VT, Custom);
735         setOperationAction(ISD::XOR, VT, Custom);
736         setOperationAction(ISD::SDIV, VT, Custom);
737         setOperationAction(ISD::SREM, VT, Custom);
738         setOperationAction(ISD::UDIV, VT, Custom);
739         setOperationAction(ISD::UREM, VT, Custom);
740         setOperationAction(ISD::SHL, VT, Custom);
741         setOperationAction(ISD::SRA, VT, Custom);
742         setOperationAction(ISD::SRL, VT, Custom);
743 
744         setOperationAction(ISD::SMIN, VT, Custom);
745         setOperationAction(ISD::SMAX, VT, Custom);
746         setOperationAction(ISD::UMIN, VT, Custom);
747         setOperationAction(ISD::UMAX, VT, Custom);
748         setOperationAction(ISD::ABS,  VT, Custom);
749 
750         setOperationAction(ISD::MULHS, VT, Custom);
751         setOperationAction(ISD::MULHU, VT, Custom);
752 
753         setOperationAction(ISD::SADDSAT, VT, Custom);
754         setOperationAction(ISD::UADDSAT, VT, Custom);
755         setOperationAction(ISD::SSUBSAT, VT, Custom);
756         setOperationAction(ISD::USUBSAT, VT, Custom);
757 
758         setOperationAction(ISD::VSELECT, VT, Custom);
759         setOperationAction(ISD::SELECT_CC, VT, Expand);
760 
761         setOperationAction(ISD::ANY_EXTEND, VT, Custom);
762         setOperationAction(ISD::SIGN_EXTEND, VT, Custom);
763         setOperationAction(ISD::ZERO_EXTEND, VT, Custom);
764 
765         // Custom-lower reduction operations to set up the corresponding custom
766         // nodes' operands.
767         setOperationAction(ISD::VECREDUCE_ADD, VT, Custom);
768         setOperationAction(ISD::VECREDUCE_SMAX, VT, Custom);
769         setOperationAction(ISD::VECREDUCE_SMIN, VT, Custom);
770         setOperationAction(ISD::VECREDUCE_UMAX, VT, Custom);
771         setOperationAction(ISD::VECREDUCE_UMIN, VT, Custom);
772 
773         for (unsigned VPOpc : IntegerVPOps)
774           setOperationAction(VPOpc, VT, Custom);
775       }
776 
777       for (MVT VT : MVT::fp_fixedlen_vector_valuetypes()) {
778         if (!useRVVForFixedLengthVectorVT(VT))
779           continue;
780 
781         // By default everything must be expanded.
782         for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op)
783           setOperationAction(Op, VT, Expand);
784         for (MVT OtherVT : MVT::fp_fixedlen_vector_valuetypes()) {
785           setLoadExtAction(ISD::EXTLOAD, OtherVT, VT, Expand);
786           setTruncStoreAction(VT, OtherVT, Expand);
787         }
788 
789         // We use EXTRACT_SUBVECTOR as a "cast" from scalable to fixed.
790         setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
791         setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
792 
793         setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
794         setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
795         setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
796         setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
797 
798         setOperationAction(ISD::LOAD, VT, Custom);
799         setOperationAction(ISD::STORE, VT, Custom);
800         setOperationAction(ISD::MLOAD, VT, Custom);
801         setOperationAction(ISD::MSTORE, VT, Custom);
802         setOperationAction(ISD::MGATHER, VT, Custom);
803         setOperationAction(ISD::MSCATTER, VT, Custom);
804         setOperationAction(ISD::FADD, VT, Custom);
805         setOperationAction(ISD::FSUB, VT, Custom);
806         setOperationAction(ISD::FMUL, VT, Custom);
807         setOperationAction(ISD::FDIV, VT, Custom);
808         setOperationAction(ISD::FNEG, VT, Custom);
809         setOperationAction(ISD::FABS, VT, Custom);
810         setOperationAction(ISD::FCOPYSIGN, VT, Custom);
811         setOperationAction(ISD::FSQRT, VT, Custom);
812         setOperationAction(ISD::FMA, VT, Custom);
813         setOperationAction(ISD::FMINNUM, VT, Custom);
814         setOperationAction(ISD::FMAXNUM, VT, Custom);
815 
816         setOperationAction(ISD::FP_ROUND, VT, Custom);
817         setOperationAction(ISD::FP_EXTEND, VT, Custom);
818 
819         for (auto CC : VFPCCToExpand)
820           setCondCodeAction(CC, VT, Expand);
821 
822         setOperationAction(ISD::VSELECT, VT, Custom);
823         setOperationAction(ISD::SELECT, VT, Custom);
824         setOperationAction(ISD::SELECT_CC, VT, Expand);
825 
826         setOperationAction(ISD::BITCAST, VT, Custom);
827 
828         setOperationAction(ISD::VECREDUCE_FADD, VT, Custom);
829         setOperationAction(ISD::VECREDUCE_SEQ_FADD, VT, Custom);
830         setOperationAction(ISD::VECREDUCE_FMIN, VT, Custom);
831         setOperationAction(ISD::VECREDUCE_FMAX, VT, Custom);
832 
833         for (unsigned VPOpc : FloatingPointVPOps)
834           setOperationAction(VPOpc, VT, Custom);
835       }
836 
837       // Custom-legalize bitcasts from fixed-length vectors to scalar types.
838       setOperationAction(ISD::BITCAST, MVT::i8, Custom);
839       setOperationAction(ISD::BITCAST, MVT::i16, Custom);
840       setOperationAction(ISD::BITCAST, MVT::i32, Custom);
841       setOperationAction(ISD::BITCAST, MVT::i64, Custom);
842       setOperationAction(ISD::BITCAST, MVT::f16, Custom);
843       setOperationAction(ISD::BITCAST, MVT::f32, Custom);
844       setOperationAction(ISD::BITCAST, MVT::f64, Custom);
845     }
846   }
847 
848   // Function alignments.
849   const Align FunctionAlignment(Subtarget.hasStdExtC() ? 2 : 4);
850   setMinFunctionAlignment(FunctionAlignment);
851   setPrefFunctionAlignment(FunctionAlignment);
852 
853   setMinimumJumpTableEntries(5);
854 
855   // Jumps are expensive, compared to logic
856   setJumpIsExpensive();
857 
858   // We can use any register for comparisons
859   setHasMultipleConditionRegisters();
860 
861   setTargetDAGCombine(ISD::ADD);
862   setTargetDAGCombine(ISD::SUB);
863   setTargetDAGCombine(ISD::AND);
864   setTargetDAGCombine(ISD::OR);
865   setTargetDAGCombine(ISD::XOR);
866   setTargetDAGCombine(ISD::ANY_EXTEND);
867   setTargetDAGCombine(ISD::ZERO_EXTEND);
868   if (Subtarget.hasStdExtV()) {
869     setTargetDAGCombine(ISD::FCOPYSIGN);
870     setTargetDAGCombine(ISD::MGATHER);
871     setTargetDAGCombine(ISD::MSCATTER);
872     setTargetDAGCombine(ISD::SRA);
873     setTargetDAGCombine(ISD::SRL);
874     setTargetDAGCombine(ISD::SHL);
875   }
876 }
877 
878 EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL,
879                                             LLVMContext &Context,
880                                             EVT VT) const {
881   if (!VT.isVector())
882     return getPointerTy(DL);
883   if (Subtarget.hasStdExtV() &&
884       (VT.isScalableVector() || Subtarget.useRVVForFixedLengthVectors()))
885     return EVT::getVectorVT(Context, MVT::i1, VT.getVectorElementCount());
886   return VT.changeVectorElementTypeToInteger();
887 }
888 
889 MVT RISCVTargetLowering::getVPExplicitVectorLengthTy() const {
890   return Subtarget.getXLenVT();
891 }
892 
893 bool RISCVTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
894                                              const CallInst &I,
895                                              MachineFunction &MF,
896                                              unsigned Intrinsic) const {
897   switch (Intrinsic) {
898   default:
899     return false;
900   case Intrinsic::riscv_masked_atomicrmw_xchg_i32:
901   case Intrinsic::riscv_masked_atomicrmw_add_i32:
902   case Intrinsic::riscv_masked_atomicrmw_sub_i32:
903   case Intrinsic::riscv_masked_atomicrmw_nand_i32:
904   case Intrinsic::riscv_masked_atomicrmw_max_i32:
905   case Intrinsic::riscv_masked_atomicrmw_min_i32:
906   case Intrinsic::riscv_masked_atomicrmw_umax_i32:
907   case Intrinsic::riscv_masked_atomicrmw_umin_i32:
908   case Intrinsic::riscv_masked_cmpxchg_i32: {
909     PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
910     Info.opc = ISD::INTRINSIC_W_CHAIN;
911     Info.memVT = MVT::getVT(PtrTy->getElementType());
912     Info.ptrVal = I.getArgOperand(0);
913     Info.offset = 0;
914     Info.align = Align(4);
915     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore |
916                  MachineMemOperand::MOVolatile;
917     return true;
918   }
919   }
920 }
921 
922 bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL,
923                                                 const AddrMode &AM, Type *Ty,
924                                                 unsigned AS,
925                                                 Instruction *I) const {
926   // No global is ever allowed as a base.
927   if (AM.BaseGV)
928     return false;
929 
930   // Require a 12-bit signed offset.
931   if (!isInt<12>(AM.BaseOffs))
932     return false;
933 
934   switch (AM.Scale) {
935   case 0: // "r+i" or just "i", depending on HasBaseReg.
936     break;
937   case 1:
938     if (!AM.HasBaseReg) // allow "r+i".
939       break;
940     return false; // disallow "r+r" or "r+r+i".
941   default:
942     return false;
943   }
944 
945   return true;
946 }
947 
948 bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
949   return isInt<12>(Imm);
950 }
951 
952 bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
953   return isInt<12>(Imm);
954 }
955 
956 // On RV32, 64-bit integers are split into their high and low parts and held
957 // in two different registers, so the trunc is free since the low register can
958 // just be used.
959 bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
960   if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
961     return false;
962   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
963   unsigned DestBits = DstTy->getPrimitiveSizeInBits();
964   return (SrcBits == 64 && DestBits == 32);
965 }
966 
967 bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
968   if (Subtarget.is64Bit() || SrcVT.isVector() || DstVT.isVector() ||
969       !SrcVT.isInteger() || !DstVT.isInteger())
970     return false;
971   unsigned SrcBits = SrcVT.getSizeInBits();
972   unsigned DestBits = DstVT.getSizeInBits();
973   return (SrcBits == 64 && DestBits == 32);
974 }
975 
976 bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
977   // Zexts are free if they can be combined with a load.
978   if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
979     EVT MemVT = LD->getMemoryVT();
980     if ((MemVT == MVT::i8 || MemVT == MVT::i16 ||
981          (Subtarget.is64Bit() && MemVT == MVT::i32)) &&
982         (LD->getExtensionType() == ISD::NON_EXTLOAD ||
983          LD->getExtensionType() == ISD::ZEXTLOAD))
984       return true;
985   }
986 
987   return TargetLowering::isZExtFree(Val, VT2);
988 }
989 
990 bool RISCVTargetLowering::isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const {
991   return Subtarget.is64Bit() && SrcVT == MVT::i32 && DstVT == MVT::i64;
992 }
993 
994 bool RISCVTargetLowering::isCheapToSpeculateCttz() const {
995   return Subtarget.hasStdExtZbb();
996 }
997 
998 bool RISCVTargetLowering::isCheapToSpeculateCtlz() const {
999   return Subtarget.hasStdExtZbb();
1000 }
1001 
1002 bool RISCVTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
1003                                        bool ForCodeSize) const {
1004   if (VT == MVT::f16 && !Subtarget.hasStdExtZfh())
1005     return false;
1006   if (VT == MVT::f32 && !Subtarget.hasStdExtF())
1007     return false;
1008   if (VT == MVT::f64 && !Subtarget.hasStdExtD())
1009     return false;
1010   if (Imm.isNegZero())
1011     return false;
1012   return Imm.isZero();
1013 }
1014 
1015 bool RISCVTargetLowering::hasBitPreservingFPLogic(EVT VT) const {
1016   return (VT == MVT::f16 && Subtarget.hasStdExtZfh()) ||
1017          (VT == MVT::f32 && Subtarget.hasStdExtF()) ||
1018          (VT == MVT::f64 && Subtarget.hasStdExtD());
1019 }
1020 
1021 MVT RISCVTargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context,
1022                                                       CallingConv::ID CC,
1023                                                       EVT VT) const {
1024   // Use f32 to pass f16 if it is legal and Zfh is not enabled. We might still
1025   // end up using a GPR but that will be decided based on ABI.
1026   if (VT == MVT::f16 && Subtarget.hasStdExtF() && !Subtarget.hasStdExtZfh())
1027     return MVT::f32;
1028 
1029   return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
1030 }
1031 
1032 unsigned RISCVTargetLowering::getNumRegistersForCallingConv(LLVMContext &Context,
1033                                                            CallingConv::ID CC,
1034                                                            EVT VT) const {
1035   // Use f32 to pass f16 if it is legal and Zfh is not enabled. We might still
1036   // end up using a GPR but that will be decided based on ABI.
1037   if (VT == MVT::f16 && Subtarget.hasStdExtF() && !Subtarget.hasStdExtZfh())
1038     return 1;
1039 
1040   return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
1041 }
1042 
1043 // Changes the condition code and swaps operands if necessary, so the SetCC
1044 // operation matches one of the comparisons supported directly by branches
1045 // in the RISC-V ISA. May adjust compares to favor compare with 0 over compare
1046 // with 1/-1.
1047 static void translateSetCCForBranch(const SDLoc &DL, SDValue &LHS, SDValue &RHS,
1048                                     ISD::CondCode &CC, SelectionDAG &DAG) {
1049   // Convert X > -1 to X >= 0.
1050   if (CC == ISD::SETGT && isAllOnesConstant(RHS)) {
1051     RHS = DAG.getConstant(0, DL, RHS.getValueType());
1052     CC = ISD::SETGE;
1053     return;
1054   }
1055   // Convert X < 1 to 0 >= X.
1056   if (CC == ISD::SETLT && isOneConstant(RHS)) {
1057     RHS = LHS;
1058     LHS = DAG.getConstant(0, DL, RHS.getValueType());
1059     CC = ISD::SETGE;
1060     return;
1061   }
1062 
1063   switch (CC) {
1064   default:
1065     break;
1066   case ISD::SETGT:
1067   case ISD::SETLE:
1068   case ISD::SETUGT:
1069   case ISD::SETULE:
1070     CC = ISD::getSetCCSwappedOperands(CC);
1071     std::swap(LHS, RHS);
1072     break;
1073   }
1074 }
1075 
1076 RISCVII::VLMUL RISCVTargetLowering::getLMUL(MVT VT) {
1077   assert(VT.isScalableVector() && "Expecting a scalable vector type");
1078   unsigned KnownSize = VT.getSizeInBits().getKnownMinValue();
1079   if (VT.getVectorElementType() == MVT::i1)
1080     KnownSize *= 8;
1081 
1082   switch (KnownSize) {
1083   default:
1084     llvm_unreachable("Invalid LMUL.");
1085   case 8:
1086     return RISCVII::VLMUL::LMUL_F8;
1087   case 16:
1088     return RISCVII::VLMUL::LMUL_F4;
1089   case 32:
1090     return RISCVII::VLMUL::LMUL_F2;
1091   case 64:
1092     return RISCVII::VLMUL::LMUL_1;
1093   case 128:
1094     return RISCVII::VLMUL::LMUL_2;
1095   case 256:
1096     return RISCVII::VLMUL::LMUL_4;
1097   case 512:
1098     return RISCVII::VLMUL::LMUL_8;
1099   }
1100 }
1101 
1102 unsigned RISCVTargetLowering::getRegClassIDForLMUL(RISCVII::VLMUL LMul) {
1103   switch (LMul) {
1104   default:
1105     llvm_unreachable("Invalid LMUL.");
1106   case RISCVII::VLMUL::LMUL_F8:
1107   case RISCVII::VLMUL::LMUL_F4:
1108   case RISCVII::VLMUL::LMUL_F2:
1109   case RISCVII::VLMUL::LMUL_1:
1110     return RISCV::VRRegClassID;
1111   case RISCVII::VLMUL::LMUL_2:
1112     return RISCV::VRM2RegClassID;
1113   case RISCVII::VLMUL::LMUL_4:
1114     return RISCV::VRM4RegClassID;
1115   case RISCVII::VLMUL::LMUL_8:
1116     return RISCV::VRM8RegClassID;
1117   }
1118 }
1119 
1120 unsigned RISCVTargetLowering::getSubregIndexByMVT(MVT VT, unsigned Index) {
1121   RISCVII::VLMUL LMUL = getLMUL(VT);
1122   if (LMUL == RISCVII::VLMUL::LMUL_F8 ||
1123       LMUL == RISCVII::VLMUL::LMUL_F4 ||
1124       LMUL == RISCVII::VLMUL::LMUL_F2 ||
1125       LMUL == RISCVII::VLMUL::LMUL_1) {
1126     static_assert(RISCV::sub_vrm1_7 == RISCV::sub_vrm1_0 + 7,
1127                   "Unexpected subreg numbering");
1128     return RISCV::sub_vrm1_0 + Index;
1129   }
1130   if (LMUL == RISCVII::VLMUL::LMUL_2) {
1131     static_assert(RISCV::sub_vrm2_3 == RISCV::sub_vrm2_0 + 3,
1132                   "Unexpected subreg numbering");
1133     return RISCV::sub_vrm2_0 + Index;
1134   }
1135   if (LMUL == RISCVII::VLMUL::LMUL_4) {
1136     static_assert(RISCV::sub_vrm4_1 == RISCV::sub_vrm4_0 + 1,
1137                   "Unexpected subreg numbering");
1138     return RISCV::sub_vrm4_0 + Index;
1139   }
1140   llvm_unreachable("Invalid vector type.");
1141 }
1142 
1143 unsigned RISCVTargetLowering::getRegClassIDForVecVT(MVT VT) {
1144   if (VT.getVectorElementType() == MVT::i1)
1145     return RISCV::VRRegClassID;
1146   return getRegClassIDForLMUL(getLMUL(VT));
1147 }
1148 
1149 // Attempt to decompose a subvector insert/extract between VecVT and
1150 // SubVecVT via subregister indices. Returns the subregister index that
1151 // can perform the subvector insert/extract with the given element index, as
1152 // well as the index corresponding to any leftover subvectors that must be
1153 // further inserted/extracted within the register class for SubVecVT.
1154 std::pair<unsigned, unsigned>
1155 RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
1156     MVT VecVT, MVT SubVecVT, unsigned InsertExtractIdx,
1157     const RISCVRegisterInfo *TRI) {
1158   static_assert((RISCV::VRM8RegClassID > RISCV::VRM4RegClassID &&
1159                  RISCV::VRM4RegClassID > RISCV::VRM2RegClassID &&
1160                  RISCV::VRM2RegClassID > RISCV::VRRegClassID),
1161                 "Register classes not ordered");
1162   unsigned VecRegClassID = getRegClassIDForVecVT(VecVT);
1163   unsigned SubRegClassID = getRegClassIDForVecVT(SubVecVT);
1164   // Try to compose a subregister index that takes us from the incoming
1165   // LMUL>1 register class down to the outgoing one. At each step we half
1166   // the LMUL:
1167   //   nxv16i32@12 -> nxv2i32: sub_vrm4_1_then_sub_vrm2_1_then_sub_vrm1_0
1168   // Note that this is not guaranteed to find a subregister index, such as
1169   // when we are extracting from one VR type to another.
1170   unsigned SubRegIdx = RISCV::NoSubRegister;
1171   for (const unsigned RCID :
1172        {RISCV::VRM4RegClassID, RISCV::VRM2RegClassID, RISCV::VRRegClassID})
1173     if (VecRegClassID > RCID && SubRegClassID <= RCID) {
1174       VecVT = VecVT.getHalfNumVectorElementsVT();
1175       bool IsHi =
1176           InsertExtractIdx >= VecVT.getVectorElementCount().getKnownMinValue();
1177       SubRegIdx = TRI->composeSubRegIndices(SubRegIdx,
1178                                             getSubregIndexByMVT(VecVT, IsHi));
1179       if (IsHi)
1180         InsertExtractIdx -= VecVT.getVectorElementCount().getKnownMinValue();
1181     }
1182   return {SubRegIdx, InsertExtractIdx};
1183 }
1184 
1185 // Permit combining of mask vectors as BUILD_VECTOR never expands to scalar
1186 // stores for those types.
1187 bool RISCVTargetLowering::mergeStoresAfterLegalization(EVT VT) const {
1188   return !Subtarget.useRVVForFixedLengthVectors() ||
1189          (VT.isFixedLengthVector() && VT.getVectorElementType() == MVT::i1);
1190 }
1191 
1192 static bool useRVVForFixedLengthVectorVT(MVT VT,
1193                                          const RISCVSubtarget &Subtarget) {
1194   assert(VT.isFixedLengthVector() && "Expected a fixed length vector type!");
1195   if (!Subtarget.useRVVForFixedLengthVectors())
1196     return false;
1197 
1198   // We only support a set of vector types with a consistent maximum fixed size
1199   // across all supported vector element types to avoid legalization issues.
1200   // Therefore -- since the largest is v1024i8/v512i16/etc -- the largest
1201   // fixed-length vector type we support is 1024 bytes.
1202   if (VT.getFixedSizeInBits() > 1024 * 8)
1203     return false;
1204 
1205   unsigned MinVLen = Subtarget.getMinRVVVectorSizeInBits();
1206 
1207   // Don't use RVV for vectors we cannot scalarize if required.
1208   switch (VT.getVectorElementType().SimpleTy) {
1209   // i1 is supported but has different rules.
1210   default:
1211     return false;
1212   case MVT::i1:
1213     // Masks can only use a single register.
1214     if (VT.getVectorNumElements() > MinVLen)
1215       return false;
1216     MinVLen /= 8;
1217     break;
1218   case MVT::i8:
1219   case MVT::i16:
1220   case MVT::i32:
1221   case MVT::i64:
1222     break;
1223   case MVT::f16:
1224     if (!Subtarget.hasStdExtZfh())
1225       return false;
1226     break;
1227   case MVT::f32:
1228     if (!Subtarget.hasStdExtF())
1229       return false;
1230     break;
1231   case MVT::f64:
1232     if (!Subtarget.hasStdExtD())
1233       return false;
1234     break;
1235   }
1236 
1237   unsigned LMul = divideCeil(VT.getSizeInBits(), MinVLen);
1238   // Don't use RVV for types that don't fit.
1239   if (LMul > Subtarget.getMaxLMULForFixedLengthVectors())
1240     return false;
1241 
1242   // TODO: Perhaps an artificial restriction, but worth having whilst getting
1243   // the base fixed length RVV support in place.
1244   if (!VT.isPow2VectorType())
1245     return false;
1246 
1247   return true;
1248 }
1249 
1250 bool RISCVTargetLowering::useRVVForFixedLengthVectorVT(MVT VT) const {
1251   return ::useRVVForFixedLengthVectorVT(VT, Subtarget);
1252 }
1253 
1254 // Return the largest legal scalable vector type that matches VT's element type.
1255 static MVT getContainerForFixedLengthVector(const TargetLowering &TLI, MVT VT,
1256                                             const RISCVSubtarget &Subtarget) {
1257   // This may be called before legal types are setup.
1258   assert(((VT.isFixedLengthVector() && TLI.isTypeLegal(VT)) ||
1259           useRVVForFixedLengthVectorVT(VT, Subtarget)) &&
1260          "Expected legal fixed length vector!");
1261 
1262   unsigned MinVLen = Subtarget.getMinRVVVectorSizeInBits();
1263 
1264   MVT EltVT = VT.getVectorElementType();
1265   switch (EltVT.SimpleTy) {
1266   default:
1267     llvm_unreachable("unexpected element type for RVV container");
1268   case MVT::i1:
1269   case MVT::i8:
1270   case MVT::i16:
1271   case MVT::i32:
1272   case MVT::i64:
1273   case MVT::f16:
1274   case MVT::f32:
1275   case MVT::f64: {
1276     // We prefer to use LMUL=1 for VLEN sized types. Use fractional lmuls for
1277     // narrower types, but we can't have a fractional LMUL with demoninator less
1278     // than 64/SEW.
1279     unsigned NumElts =
1280         divideCeil(VT.getVectorNumElements(), MinVLen / RISCV::RVVBitsPerBlock);
1281     return MVT::getScalableVectorVT(EltVT, NumElts);
1282   }
1283   }
1284 }
1285 
1286 static MVT getContainerForFixedLengthVector(SelectionDAG &DAG, MVT VT,
1287                                             const RISCVSubtarget &Subtarget) {
1288   return getContainerForFixedLengthVector(DAG.getTargetLoweringInfo(), VT,
1289                                           Subtarget);
1290 }
1291 
1292 MVT RISCVTargetLowering::getContainerForFixedLengthVector(MVT VT) const {
1293   return ::getContainerForFixedLengthVector(*this, VT, getSubtarget());
1294 }
1295 
1296 // Grow V to consume an entire RVV register.
1297 static SDValue convertToScalableVector(EVT VT, SDValue V, SelectionDAG &DAG,
1298                                        const RISCVSubtarget &Subtarget) {
1299   assert(VT.isScalableVector() &&
1300          "Expected to convert into a scalable vector!");
1301   assert(V.getValueType().isFixedLengthVector() &&
1302          "Expected a fixed length vector operand!");
1303   SDLoc DL(V);
1304   SDValue Zero = DAG.getConstant(0, DL, Subtarget.getXLenVT());
1305   return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), V, Zero);
1306 }
1307 
1308 // Shrink V so it's just big enough to maintain a VT's worth of data.
1309 static SDValue convertFromScalableVector(EVT VT, SDValue V, SelectionDAG &DAG,
1310                                          const RISCVSubtarget &Subtarget) {
1311   assert(VT.isFixedLengthVector() &&
1312          "Expected to convert into a fixed length vector!");
1313   assert(V.getValueType().isScalableVector() &&
1314          "Expected a scalable vector operand!");
1315   SDLoc DL(V);
1316   SDValue Zero = DAG.getConstant(0, DL, Subtarget.getXLenVT());
1317   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V, Zero);
1318 }
1319 
1320 // Gets the two common "VL" operands: an all-ones mask and the vector length.
1321 // VecVT is a vector type, either fixed-length or scalable, and ContainerVT is
1322 // the vector type that it is contained in.
1323 static std::pair<SDValue, SDValue>
1324 getDefaultVLOps(MVT VecVT, MVT ContainerVT, SDLoc DL, SelectionDAG &DAG,
1325                 const RISCVSubtarget &Subtarget) {
1326   assert(ContainerVT.isScalableVector() && "Expecting scalable container type");
1327   MVT XLenVT = Subtarget.getXLenVT();
1328   SDValue VL = VecVT.isFixedLengthVector()
1329                    ? DAG.getConstant(VecVT.getVectorNumElements(), DL, XLenVT)
1330                    : DAG.getRegister(RISCV::X0, XLenVT);
1331   MVT MaskVT = MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
1332   SDValue Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, MaskVT, VL);
1333   return {Mask, VL};
1334 }
1335 
1336 // As above but assuming the given type is a scalable vector type.
1337 static std::pair<SDValue, SDValue>
1338 getDefaultScalableVLOps(MVT VecVT, SDLoc DL, SelectionDAG &DAG,
1339                         const RISCVSubtarget &Subtarget) {
1340   assert(VecVT.isScalableVector() && "Expecting a scalable vector");
1341   return getDefaultVLOps(VecVT, VecVT, DL, DAG, Subtarget);
1342 }
1343 
1344 // The state of RVV BUILD_VECTOR and VECTOR_SHUFFLE lowering is that very few
1345 // of either is (currently) supported. This can get us into an infinite loop
1346 // where we try to lower a BUILD_VECTOR as a VECTOR_SHUFFLE as a BUILD_VECTOR
1347 // as a ..., etc.
1348 // Until either (or both) of these can reliably lower any node, reporting that
1349 // we don't want to expand BUILD_VECTORs via VECTOR_SHUFFLEs at least breaks
1350 // the infinite loop. Note that this lowers BUILD_VECTOR through the stack,
1351 // which is not desirable.
1352 bool RISCVTargetLowering::shouldExpandBuildVectorWithShuffles(
1353     EVT VT, unsigned DefinedValues) const {
1354   return false;
1355 }
1356 
1357 bool RISCVTargetLowering::isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const {
1358   // Only splats are currently supported.
1359   if (ShuffleVectorSDNode::isSplatMask(M.data(), VT))
1360     return true;
1361 
1362   return false;
1363 }
1364 
1365 static SDValue lowerFP_TO_INT_SAT(SDValue Op, SelectionDAG &DAG) {
1366   // RISCV FP-to-int conversions saturate to the destination register size, but
1367   // don't produce 0 for nan. We can use a conversion instruction and fix the
1368   // nan case with a compare and a select.
1369   SDValue Src = Op.getOperand(0);
1370 
1371   EVT DstVT = Op.getValueType();
1372   EVT SatVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1373 
1374   bool IsSigned = Op.getOpcode() == ISD::FP_TO_SINT_SAT;
1375   unsigned Opc;
1376   if (SatVT == DstVT)
1377     Opc = IsSigned ? RISCVISD::FCVT_X_RTZ : RISCVISD::FCVT_XU_RTZ;
1378   else if (DstVT == MVT::i64 && SatVT == MVT::i32)
1379     Opc = IsSigned ? RISCVISD::FCVT_W_RTZ_RV64 : RISCVISD::FCVT_WU_RTZ_RV64;
1380   else
1381     return SDValue();
1382   // FIXME: Support other SatVTs by clamping before or after the conversion.
1383 
1384   SDLoc DL(Op);
1385   SDValue FpToInt = DAG.getNode(Opc, DL, DstVT, Src);
1386 
1387   SDValue ZeroInt = DAG.getConstant(0, DL, DstVT);
1388   return DAG.getSelectCC(DL, Src, Src, ZeroInt, FpToInt, ISD::CondCode::SETUO);
1389 }
1390 
1391 static SDValue lowerSPLAT_VECTOR(SDValue Op, SelectionDAG &DAG,
1392                                  const RISCVSubtarget &Subtarget) {
1393   MVT VT = Op.getSimpleValueType();
1394   assert(VT.isFixedLengthVector() && "Unexpected vector!");
1395 
1396   MVT ContainerVT = getContainerForFixedLengthVector(DAG, VT, Subtarget);
1397 
1398   SDLoc DL(Op);
1399   SDValue Mask, VL;
1400   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
1401 
1402   unsigned Opc =
1403       VT.isFloatingPoint() ? RISCVISD::VFMV_V_F_VL : RISCVISD::VMV_V_X_VL;
1404   SDValue Splat = DAG.getNode(Opc, DL, ContainerVT, Op.getOperand(0), VL);
1405   return convertFromScalableVector(VT, Splat, DAG, Subtarget);
1406 }
1407 
1408 struct VIDSequence {
1409   int64_t StepNumerator;
1410   unsigned StepDenominator;
1411   int64_t Addend;
1412 };
1413 
1414 // Try to match an arithmetic-sequence BUILD_VECTOR [X,X+S,X+2*S,...,X+(N-1)*S]
1415 // to the (non-zero) step S and start value X. This can be then lowered as the
1416 // RVV sequence (VID * S) + X, for example.
1417 // The step S is represented as an integer numerator divided by a positive
1418 // denominator. Note that the implementation currently only identifies
1419 // sequences in which either the numerator is +/- 1 or the denominator is 1. It
1420 // cannot detect 2/3, for example.
1421 // Note that this method will also match potentially unappealing index
1422 // sequences, like <i32 0, i32 50939494>, however it is left to the caller to
1423 // determine whether this is worth generating code for.
1424 static Optional<VIDSequence> isSimpleVIDSequence(SDValue Op) {
1425   unsigned NumElts = Op.getNumOperands();
1426   assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unexpected BUILD_VECTOR");
1427   if (!Op.getValueType().isInteger())
1428     return None;
1429 
1430   Optional<unsigned> SeqStepDenom;
1431   Optional<int64_t> SeqStepNum, SeqAddend;
1432   Optional<std::pair<uint64_t, unsigned>> PrevElt;
1433   unsigned EltSizeInBits = Op.getValueType().getScalarSizeInBits();
1434   for (unsigned Idx = 0; Idx < NumElts; Idx++) {
1435     // Assume undef elements match the sequence; we just have to be careful
1436     // when interpolating across them.
1437     if (Op.getOperand(Idx).isUndef())
1438       continue;
1439     // The BUILD_VECTOR must be all constants.
1440     if (!isa<ConstantSDNode>(Op.getOperand(Idx)))
1441       return None;
1442 
1443     uint64_t Val = Op.getConstantOperandVal(Idx) &
1444                    maskTrailingOnes<uint64_t>(EltSizeInBits);
1445 
1446     if (PrevElt) {
1447       // Calculate the step since the last non-undef element, and ensure
1448       // it's consistent across the entire sequence.
1449       unsigned IdxDiff = Idx - PrevElt->second;
1450       int64_t ValDiff = SignExtend64(Val - PrevElt->first, EltSizeInBits);
1451 
1452       // A zero-value value difference means that we're somewhere in the middle
1453       // of a fractional step, e.g. <0,0,0*,0,1,1,1,1>. Wait until we notice a
1454       // step change before evaluating the sequence.
1455       if (ValDiff != 0) {
1456         int64_t Remainder = ValDiff % IdxDiff;
1457         // Normalize the step if it's greater than 1.
1458         if (Remainder != ValDiff) {
1459           // The difference must cleanly divide the element span.
1460           if (Remainder != 0)
1461             return None;
1462           ValDiff /= IdxDiff;
1463           IdxDiff = 1;
1464         }
1465 
1466         if (!SeqStepNum)
1467           SeqStepNum = ValDiff;
1468         else if (ValDiff != SeqStepNum)
1469           return None;
1470 
1471         if (!SeqStepDenom)
1472           SeqStepDenom = IdxDiff;
1473         else if (IdxDiff != *SeqStepDenom)
1474           return None;
1475       }
1476     }
1477 
1478     // Record and/or check any addend.
1479     if (SeqStepNum && SeqStepDenom) {
1480       uint64_t ExpectedVal =
1481           (int64_t)(Idx * (uint64_t)*SeqStepNum) / *SeqStepDenom;
1482       int64_t Addend = SignExtend64(Val - ExpectedVal, EltSizeInBits);
1483       if (!SeqAddend)
1484         SeqAddend = Addend;
1485       else if (SeqAddend != Addend)
1486         return None;
1487     }
1488 
1489     // Record this non-undef element for later.
1490     if (!PrevElt || PrevElt->first != Val)
1491       PrevElt = std::make_pair(Val, Idx);
1492   }
1493   // We need to have logged both a step and an addend for this to count as
1494   // a legal index sequence.
1495   if (!SeqStepNum || !SeqStepDenom || !SeqAddend)
1496     return None;
1497 
1498   return VIDSequence{*SeqStepNum, *SeqStepDenom, *SeqAddend};
1499 }
1500 
1501 static SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
1502                                  const RISCVSubtarget &Subtarget) {
1503   MVT VT = Op.getSimpleValueType();
1504   assert(VT.isFixedLengthVector() && "Unexpected vector!");
1505 
1506   MVT ContainerVT = getContainerForFixedLengthVector(DAG, VT, Subtarget);
1507 
1508   SDLoc DL(Op);
1509   SDValue Mask, VL;
1510   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
1511 
1512   MVT XLenVT = Subtarget.getXLenVT();
1513   unsigned NumElts = Op.getNumOperands();
1514 
1515   if (VT.getVectorElementType() == MVT::i1) {
1516     if (ISD::isBuildVectorAllZeros(Op.getNode())) {
1517       SDValue VMClr = DAG.getNode(RISCVISD::VMCLR_VL, DL, ContainerVT, VL);
1518       return convertFromScalableVector(VT, VMClr, DAG, Subtarget);
1519     }
1520 
1521     if (ISD::isBuildVectorAllOnes(Op.getNode())) {
1522       SDValue VMSet = DAG.getNode(RISCVISD::VMSET_VL, DL, ContainerVT, VL);
1523       return convertFromScalableVector(VT, VMSet, DAG, Subtarget);
1524     }
1525 
1526     // Lower constant mask BUILD_VECTORs via an integer vector type, in
1527     // scalar integer chunks whose bit-width depends on the number of mask
1528     // bits and XLEN.
1529     // First, determine the most appropriate scalar integer type to use. This
1530     // is at most XLenVT, but may be shrunk to a smaller vector element type
1531     // according to the size of the final vector - use i8 chunks rather than
1532     // XLenVT if we're producing a v8i1. This results in more consistent
1533     // codegen across RV32 and RV64.
1534     unsigned NumViaIntegerBits =
1535         std::min(std::max(NumElts, 8u), Subtarget.getXLen());
1536     if (ISD::isBuildVectorOfConstantSDNodes(Op.getNode())) {
1537       // If we have to use more than one INSERT_VECTOR_ELT then this
1538       // optimization is likely to increase code size; avoid peforming it in
1539       // such a case. We can use a load from a constant pool in this case.
1540       if (DAG.shouldOptForSize() && NumElts > NumViaIntegerBits)
1541         return SDValue();
1542       // Now we can create our integer vector type. Note that it may be larger
1543       // than the resulting mask type: v4i1 would use v1i8 as its integer type.
1544       MVT IntegerViaVecVT =
1545           MVT::getVectorVT(MVT::getIntegerVT(NumViaIntegerBits),
1546                            divideCeil(NumElts, NumViaIntegerBits));
1547 
1548       uint64_t Bits = 0;
1549       unsigned BitPos = 0, IntegerEltIdx = 0;
1550       SDValue Vec = DAG.getUNDEF(IntegerViaVecVT);
1551 
1552       for (unsigned I = 0; I < NumElts; I++, BitPos++) {
1553         // Once we accumulate enough bits to fill our scalar type, insert into
1554         // our vector and clear our accumulated data.
1555         if (I != 0 && I % NumViaIntegerBits == 0) {
1556           if (NumViaIntegerBits <= 32)
1557             Bits = SignExtend64(Bits, 32);
1558           SDValue Elt = DAG.getConstant(Bits, DL, XLenVT);
1559           Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntegerViaVecVT, Vec,
1560                             Elt, DAG.getConstant(IntegerEltIdx, DL, XLenVT));
1561           Bits = 0;
1562           BitPos = 0;
1563           IntegerEltIdx++;
1564         }
1565         SDValue V = Op.getOperand(I);
1566         bool BitValue = !V.isUndef() && cast<ConstantSDNode>(V)->getZExtValue();
1567         Bits |= ((uint64_t)BitValue << BitPos);
1568       }
1569 
1570       // Insert the (remaining) scalar value into position in our integer
1571       // vector type.
1572       if (NumViaIntegerBits <= 32)
1573         Bits = SignExtend64(Bits, 32);
1574       SDValue Elt = DAG.getConstant(Bits, DL, XLenVT);
1575       Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntegerViaVecVT, Vec, Elt,
1576                         DAG.getConstant(IntegerEltIdx, DL, XLenVT));
1577 
1578       if (NumElts < NumViaIntegerBits) {
1579         // If we're producing a smaller vector than our minimum legal integer
1580         // type, bitcast to the equivalent (known-legal) mask type, and extract
1581         // our final mask.
1582         assert(IntegerViaVecVT == MVT::v1i8 && "Unexpected mask vector type");
1583         Vec = DAG.getBitcast(MVT::v8i1, Vec);
1584         Vec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Vec,
1585                           DAG.getConstant(0, DL, XLenVT));
1586       } else {
1587         // Else we must have produced an integer type with the same size as the
1588         // mask type; bitcast for the final result.
1589         assert(VT.getSizeInBits() == IntegerViaVecVT.getSizeInBits());
1590         Vec = DAG.getBitcast(VT, Vec);
1591       }
1592 
1593       return Vec;
1594     }
1595 
1596     // A BUILD_VECTOR can be lowered as a SETCC. For each fixed-length mask
1597     // vector type, we have a legal equivalently-sized i8 type, so we can use
1598     // that.
1599     MVT WideVecVT = VT.changeVectorElementType(MVT::i8);
1600     SDValue VecZero = DAG.getConstant(0, DL, WideVecVT);
1601 
1602     SDValue WideVec;
1603     if (SDValue Splat = cast<BuildVectorSDNode>(Op)->getSplatValue()) {
1604       // For a splat, perform a scalar truncate before creating the wider
1605       // vector.
1606       assert(Splat.getValueType() == XLenVT &&
1607              "Unexpected type for i1 splat value");
1608       Splat = DAG.getNode(ISD::AND, DL, XLenVT, Splat,
1609                           DAG.getConstant(1, DL, XLenVT));
1610       WideVec = DAG.getSplatBuildVector(WideVecVT, DL, Splat);
1611     } else {
1612       SmallVector<SDValue, 8> Ops(Op->op_values());
1613       WideVec = DAG.getBuildVector(WideVecVT, DL, Ops);
1614       SDValue VecOne = DAG.getConstant(1, DL, WideVecVT);
1615       WideVec = DAG.getNode(ISD::AND, DL, WideVecVT, WideVec, VecOne);
1616     }
1617 
1618     return DAG.getSetCC(DL, VT, WideVec, VecZero, ISD::SETNE);
1619   }
1620 
1621   if (SDValue Splat = cast<BuildVectorSDNode>(Op)->getSplatValue()) {
1622     unsigned Opc = VT.isFloatingPoint() ? RISCVISD::VFMV_V_F_VL
1623                                         : RISCVISD::VMV_V_X_VL;
1624     Splat = DAG.getNode(Opc, DL, ContainerVT, Splat, VL);
1625     return convertFromScalableVector(VT, Splat, DAG, Subtarget);
1626   }
1627 
1628   // Try and match index sequences, which we can lower to the vid instruction
1629   // with optional modifications. An all-undef vector is matched by
1630   // getSplatValue, above.
1631   if (auto SimpleVID = isSimpleVIDSequence(Op)) {
1632     int64_t StepNumerator = SimpleVID->StepNumerator;
1633     unsigned StepDenominator = SimpleVID->StepDenominator;
1634     int64_t Addend = SimpleVID->Addend;
1635     // Only emit VIDs with suitably-small steps/addends. We use imm5 is a
1636     // threshold since it's the immediate value many RVV instructions accept.
1637     if (isInt<5>(StepNumerator) && isPowerOf2_32(StepDenominator) &&
1638         isInt<5>(Addend)) {
1639       SDValue VID = DAG.getNode(RISCVISD::VID_VL, DL, ContainerVT, Mask, VL);
1640       // Convert right out of the scalable type so we can use standard ISD
1641       // nodes for the rest of the computation. If we used scalable types with
1642       // these, we'd lose the fixed-length vector info and generate worse
1643       // vsetvli code.
1644       VID = convertFromScalableVector(VT, VID, DAG, Subtarget);
1645       assert(StepNumerator != 0 && "Invalid step");
1646       bool Negate = false;
1647       if (StepNumerator != 1) {
1648         int64_t SplatStepVal = StepNumerator;
1649         unsigned Opcode = ISD::MUL;
1650         if (isPowerOf2_64(std::abs(StepNumerator))) {
1651           Negate = StepNumerator < 0;
1652           Opcode = ISD::SHL;
1653           SplatStepVal = Log2_64(std::abs(StepNumerator));
1654         }
1655         SDValue SplatStep = DAG.getSplatVector(
1656             VT, DL, DAG.getConstant(SplatStepVal, DL, XLenVT));
1657         VID = DAG.getNode(Opcode, DL, VT, VID, SplatStep);
1658       }
1659       if (StepDenominator != 1) {
1660         SDValue SplatStep = DAG.getSplatVector(
1661             VT, DL, DAG.getConstant(Log2_64(StepDenominator), DL, XLenVT));
1662         VID = DAG.getNode(ISD::SRL, DL, VT, VID, SplatStep);
1663       }
1664       if (Addend != 0 || Negate) {
1665         SDValue SplatAddend =
1666             DAG.getSplatVector(VT, DL, DAG.getConstant(Addend, DL, XLenVT));
1667         VID = DAG.getNode(Negate ? ISD::SUB : ISD::ADD, DL, VT, SplatAddend, VID);
1668       }
1669       return VID;
1670     }
1671   }
1672 
1673   // Attempt to detect "hidden" splats, which only reveal themselves as splats
1674   // when re-interpreted as a vector with a larger element type. For example,
1675   //   v4i16 = build_vector i16 0, i16 1, i16 0, i16 1
1676   // could be instead splat as
1677   //   v2i32 = build_vector i32 0x00010000, i32 0x00010000
1678   // TODO: This optimization could also work on non-constant splats, but it
1679   // would require bit-manipulation instructions to construct the splat value.
1680   SmallVector<SDValue> Sequence;
1681   unsigned EltBitSize = VT.getScalarSizeInBits();
1682   const auto *BV = cast<BuildVectorSDNode>(Op);
1683   if (VT.isInteger() && EltBitSize < 64 &&
1684       ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) &&
1685       BV->getRepeatedSequence(Sequence) &&
1686       (Sequence.size() * EltBitSize) <= 64) {
1687     unsigned SeqLen = Sequence.size();
1688     MVT ViaIntVT = MVT::getIntegerVT(EltBitSize * SeqLen);
1689     MVT ViaVecVT = MVT::getVectorVT(ViaIntVT, NumElts / SeqLen);
1690     assert((ViaIntVT == MVT::i16 || ViaIntVT == MVT::i32 ||
1691             ViaIntVT == MVT::i64) &&
1692            "Unexpected sequence type");
1693 
1694     unsigned EltIdx = 0;
1695     uint64_t EltMask = maskTrailingOnes<uint64_t>(EltBitSize);
1696     uint64_t SplatValue = 0;
1697     // Construct the amalgamated value which can be splatted as this larger
1698     // vector type.
1699     for (const auto &SeqV : Sequence) {
1700       if (!SeqV.isUndef())
1701         SplatValue |= ((cast<ConstantSDNode>(SeqV)->getZExtValue() & EltMask)
1702                        << (EltIdx * EltBitSize));
1703       EltIdx++;
1704     }
1705 
1706     // On RV64, sign-extend from 32 to 64 bits where possible in order to
1707     // achieve better constant materializion.
1708     if (Subtarget.is64Bit() && ViaIntVT == MVT::i32)
1709       SplatValue = SignExtend64(SplatValue, 32);
1710 
1711     // Since we can't introduce illegal i64 types at this stage, we can only
1712     // perform an i64 splat on RV32 if it is its own sign-extended value. That
1713     // way we can use RVV instructions to splat.
1714     assert((ViaIntVT.bitsLE(XLenVT) ||
1715             (!Subtarget.is64Bit() && ViaIntVT == MVT::i64)) &&
1716            "Unexpected bitcast sequence");
1717     if (ViaIntVT.bitsLE(XLenVT) || isInt<32>(SplatValue)) {
1718       SDValue ViaVL =
1719           DAG.getConstant(ViaVecVT.getVectorNumElements(), DL, XLenVT);
1720       MVT ViaContainerVT =
1721           getContainerForFixedLengthVector(DAG, ViaVecVT, Subtarget);
1722       SDValue Splat =
1723           DAG.getNode(RISCVISD::VMV_V_X_VL, DL, ViaContainerVT,
1724                       DAG.getConstant(SplatValue, DL, XLenVT), ViaVL);
1725       Splat = convertFromScalableVector(ViaVecVT, Splat, DAG, Subtarget);
1726       return DAG.getBitcast(VT, Splat);
1727     }
1728   }
1729 
1730   // Try and optimize BUILD_VECTORs with "dominant values" - these are values
1731   // which constitute a large proportion of the elements. In such cases we can
1732   // splat a vector with the dominant element and make up the shortfall with
1733   // INSERT_VECTOR_ELTs.
1734   // Note that this includes vectors of 2 elements by association. The
1735   // upper-most element is the "dominant" one, allowing us to use a splat to
1736   // "insert" the upper element, and an insert of the lower element at position
1737   // 0, which improves codegen.
1738   SDValue DominantValue;
1739   unsigned MostCommonCount = 0;
1740   DenseMap<SDValue, unsigned> ValueCounts;
1741   unsigned NumUndefElts =
1742       count_if(Op->op_values(), [](const SDValue &V) { return V.isUndef(); });
1743 
1744   // Track the number of scalar loads we know we'd be inserting, estimated as
1745   // any non-zero floating-point constant. Other kinds of element are either
1746   // already in registers or are materialized on demand. The threshold at which
1747   // a vector load is more desirable than several scalar materializion and
1748   // vector-insertion instructions is not known.
1749   unsigned NumScalarLoads = 0;
1750 
1751   for (SDValue V : Op->op_values()) {
1752     if (V.isUndef())
1753       continue;
1754 
1755     ValueCounts.insert(std::make_pair(V, 0));
1756     unsigned &Count = ValueCounts[V];
1757 
1758     if (auto *CFP = dyn_cast<ConstantFPSDNode>(V))
1759       NumScalarLoads += !CFP->isExactlyValue(+0.0);
1760 
1761     // Is this value dominant? In case of a tie, prefer the highest element as
1762     // it's cheaper to insert near the beginning of a vector than it is at the
1763     // end.
1764     if (++Count >= MostCommonCount) {
1765       DominantValue = V;
1766       MostCommonCount = Count;
1767     }
1768   }
1769 
1770   assert(DominantValue && "Not expecting an all-undef BUILD_VECTOR");
1771   unsigned NumDefElts = NumElts - NumUndefElts;
1772   unsigned DominantValueCountThreshold = NumDefElts <= 2 ? 0 : NumDefElts - 2;
1773 
1774   // Don't perform this optimization when optimizing for size, since
1775   // materializing elements and inserting them tends to cause code bloat.
1776   if (!DAG.shouldOptForSize() && NumScalarLoads < NumElts &&
1777       ((MostCommonCount > DominantValueCountThreshold) ||
1778        (ValueCounts.size() <= Log2_32(NumDefElts)))) {
1779     // Start by splatting the most common element.
1780     SDValue Vec = DAG.getSplatBuildVector(VT, DL, DominantValue);
1781 
1782     DenseSet<SDValue> Processed{DominantValue};
1783     MVT SelMaskTy = VT.changeVectorElementType(MVT::i1);
1784     for (const auto &OpIdx : enumerate(Op->ops())) {
1785       const SDValue &V = OpIdx.value();
1786       if (V.isUndef() || !Processed.insert(V).second)
1787         continue;
1788       if (ValueCounts[V] == 1) {
1789         Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Vec, V,
1790                           DAG.getConstant(OpIdx.index(), DL, XLenVT));
1791       } else {
1792         // Blend in all instances of this value using a VSELECT, using a
1793         // mask where each bit signals whether that element is the one
1794         // we're after.
1795         SmallVector<SDValue> Ops;
1796         transform(Op->op_values(), std::back_inserter(Ops), [&](SDValue V1) {
1797           return DAG.getConstant(V == V1, DL, XLenVT);
1798         });
1799         Vec = DAG.getNode(ISD::VSELECT, DL, VT,
1800                           DAG.getBuildVector(SelMaskTy, DL, Ops),
1801                           DAG.getSplatBuildVector(VT, DL, V), Vec);
1802       }
1803     }
1804 
1805     return Vec;
1806   }
1807 
1808   return SDValue();
1809 }
1810 
1811 static SDValue splatPartsI64WithVL(const SDLoc &DL, MVT VT, SDValue Lo,
1812                                    SDValue Hi, SDValue VL, SelectionDAG &DAG) {
1813   if (isa<ConstantSDNode>(Lo) && isa<ConstantSDNode>(Hi)) {
1814     int32_t LoC = cast<ConstantSDNode>(Lo)->getSExtValue();
1815     int32_t HiC = cast<ConstantSDNode>(Hi)->getSExtValue();
1816     // If Hi constant is all the same sign bit as Lo, lower this as a custom
1817     // node in order to try and match RVV vector/scalar instructions.
1818     if ((LoC >> 31) == HiC)
1819       return DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VT, Lo, VL);
1820   }
1821 
1822   // Fall back to a stack store and stride x0 vector load.
1823   return DAG.getNode(RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL, DL, VT, Lo, Hi, VL);
1824 }
1825 
1826 // Called by type legalization to handle splat of i64 on RV32.
1827 // FIXME: We can optimize this when the type has sign or zero bits in one
1828 // of the halves.
1829 static SDValue splatSplitI64WithVL(const SDLoc &DL, MVT VT, SDValue Scalar,
1830                                    SDValue VL, SelectionDAG &DAG) {
1831   assert(Scalar.getValueType() == MVT::i64 && "Unexpected VT!");
1832   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Scalar,
1833                            DAG.getConstant(0, DL, MVT::i32));
1834   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Scalar,
1835                            DAG.getConstant(1, DL, MVT::i32));
1836   return splatPartsI64WithVL(DL, VT, Lo, Hi, VL, DAG);
1837 }
1838 
1839 // This function lowers a splat of a scalar operand Splat with the vector
1840 // length VL. It ensures the final sequence is type legal, which is useful when
1841 // lowering a splat after type legalization.
1842 static SDValue lowerScalarSplat(SDValue Scalar, SDValue VL, MVT VT, SDLoc DL,
1843                                 SelectionDAG &DAG,
1844                                 const RISCVSubtarget &Subtarget) {
1845   if (VT.isFloatingPoint())
1846     return DAG.getNode(RISCVISD::VFMV_V_F_VL, DL, VT, Scalar, VL);
1847 
1848   MVT XLenVT = Subtarget.getXLenVT();
1849 
1850   // Simplest case is that the operand needs to be promoted to XLenVT.
1851   if (Scalar.getValueType().bitsLE(XLenVT)) {
1852     // If the operand is a constant, sign extend to increase our chances
1853     // of being able to use a .vi instruction. ANY_EXTEND would become a
1854     // a zero extend and the simm5 check in isel would fail.
1855     // FIXME: Should we ignore the upper bits in isel instead?
1856     unsigned ExtOpc =
1857         isa<ConstantSDNode>(Scalar) ? ISD::SIGN_EXTEND : ISD::ANY_EXTEND;
1858     Scalar = DAG.getNode(ExtOpc, DL, XLenVT, Scalar);
1859     return DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VT, Scalar, VL);
1860   }
1861 
1862   assert(XLenVT == MVT::i32 && Scalar.getValueType() == MVT::i64 &&
1863          "Unexpected scalar for splat lowering!");
1864 
1865   // Otherwise use the more complicated splatting algorithm.
1866   return splatSplitI64WithVL(DL, VT, Scalar, VL, DAG);
1867 }
1868 
1869 static SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG,
1870                                    const RISCVSubtarget &Subtarget) {
1871   SDValue V1 = Op.getOperand(0);
1872   SDValue V2 = Op.getOperand(1);
1873   SDLoc DL(Op);
1874   MVT XLenVT = Subtarget.getXLenVT();
1875   MVT VT = Op.getSimpleValueType();
1876   unsigned NumElts = VT.getVectorNumElements();
1877   ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
1878 
1879   MVT ContainerVT = getContainerForFixedLengthVector(DAG, VT, Subtarget);
1880 
1881   SDValue TrueMask, VL;
1882   std::tie(TrueMask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
1883 
1884   if (SVN->isSplat()) {
1885     const int Lane = SVN->getSplatIndex();
1886     if (Lane >= 0) {
1887       MVT SVT = VT.getVectorElementType();
1888 
1889       // Turn splatted vector load into a strided load with an X0 stride.
1890       SDValue V = V1;
1891       // Peek through CONCAT_VECTORS as VectorCombine can concat a vector
1892       // with undef.
1893       // FIXME: Peek through INSERT_SUBVECTOR, EXTRACT_SUBVECTOR, bitcasts?
1894       int Offset = Lane;
1895       if (V.getOpcode() == ISD::CONCAT_VECTORS) {
1896         int OpElements =
1897             V.getOperand(0).getSimpleValueType().getVectorNumElements();
1898         V = V.getOperand(Offset / OpElements);
1899         Offset %= OpElements;
1900       }
1901 
1902       // We need to ensure the load isn't atomic or volatile.
1903       if (ISD::isNormalLoad(V.getNode()) && cast<LoadSDNode>(V)->isSimple()) {
1904         auto *Ld = cast<LoadSDNode>(V);
1905         Offset *= SVT.getStoreSize();
1906         SDValue NewAddr = DAG.getMemBasePlusOffset(Ld->getBasePtr(),
1907                                                    TypeSize::Fixed(Offset), DL);
1908 
1909         // If this is SEW=64 on RV32, use a strided load with a stride of x0.
1910         if (SVT.isInteger() && SVT.bitsGT(XLenVT)) {
1911           SDVTList VTs = DAG.getVTList({ContainerVT, MVT::Other});
1912           SDValue IntID =
1913               DAG.getTargetConstant(Intrinsic::riscv_vlse, DL, XLenVT);
1914           SDValue Ops[] = {Ld->getChain(), IntID, NewAddr,
1915                            DAG.getRegister(RISCV::X0, XLenVT), VL};
1916           SDValue NewLoad = DAG.getMemIntrinsicNode(
1917               ISD::INTRINSIC_W_CHAIN, DL, VTs, Ops, SVT,
1918               DAG.getMachineFunction().getMachineMemOperand(
1919                   Ld->getMemOperand(), Offset, SVT.getStoreSize()));
1920           DAG.makeEquivalentMemoryOrdering(Ld, NewLoad);
1921           return convertFromScalableVector(VT, NewLoad, DAG, Subtarget);
1922         }
1923 
1924         // Otherwise use a scalar load and splat. This will give the best
1925         // opportunity to fold a splat into the operation. ISel can turn it into
1926         // the x0 strided load if we aren't able to fold away the select.
1927         if (SVT.isFloatingPoint())
1928           V = DAG.getLoad(SVT, DL, Ld->getChain(), NewAddr,
1929                           Ld->getPointerInfo().getWithOffset(Offset),
1930                           Ld->getOriginalAlign(),
1931                           Ld->getMemOperand()->getFlags());
1932         else
1933           V = DAG.getExtLoad(ISD::SEXTLOAD, DL, XLenVT, Ld->getChain(), NewAddr,
1934                              Ld->getPointerInfo().getWithOffset(Offset), SVT,
1935                              Ld->getOriginalAlign(),
1936                              Ld->getMemOperand()->getFlags());
1937         DAG.makeEquivalentMemoryOrdering(Ld, V);
1938 
1939         unsigned Opc =
1940             VT.isFloatingPoint() ? RISCVISD::VFMV_V_F_VL : RISCVISD::VMV_V_X_VL;
1941         SDValue Splat = DAG.getNode(Opc, DL, ContainerVT, V, VL);
1942         return convertFromScalableVector(VT, Splat, DAG, Subtarget);
1943       }
1944 
1945       V1 = convertToScalableVector(ContainerVT, V1, DAG, Subtarget);
1946       assert(Lane < (int)NumElts && "Unexpected lane!");
1947       SDValue Gather =
1948           DAG.getNode(RISCVISD::VRGATHER_VX_VL, DL, ContainerVT, V1,
1949                       DAG.getConstant(Lane, DL, XLenVT), TrueMask, VL);
1950       return convertFromScalableVector(VT, Gather, DAG, Subtarget);
1951     }
1952   }
1953 
1954   // Detect shuffles which can be re-expressed as vector selects; these are
1955   // shuffles in which each element in the destination is taken from an element
1956   // at the corresponding index in either source vectors.
1957   bool IsSelect = all_of(enumerate(SVN->getMask()), [&](const auto &MaskIdx) {
1958     int MaskIndex = MaskIdx.value();
1959     return MaskIndex < 0 || MaskIdx.index() == (unsigned)MaskIndex % NumElts;
1960   });
1961 
1962   assert(!V1.isUndef() && "Unexpected shuffle canonicalization");
1963 
1964   SmallVector<SDValue> MaskVals;
1965   // As a backup, shuffles can be lowered via a vrgather instruction, possibly
1966   // merged with a second vrgather.
1967   SmallVector<SDValue> GatherIndicesLHS, GatherIndicesRHS;
1968 
1969   // By default we preserve the original operand order, and use a mask to
1970   // select LHS as true and RHS as false. However, since RVV vector selects may
1971   // feature splats but only on the LHS, we may choose to invert our mask and
1972   // instead select between RHS and LHS.
1973   bool SwapOps = DAG.isSplatValue(V2) && !DAG.isSplatValue(V1);
1974   bool InvertMask = IsSelect == SwapOps;
1975 
1976   // Keep a track of which non-undef indices are used by each LHS/RHS shuffle
1977   // half.
1978   DenseMap<int, unsigned> LHSIndexCounts, RHSIndexCounts;
1979 
1980   // Now construct the mask that will be used by the vselect or blended
1981   // vrgather operation. For vrgathers, construct the appropriate indices into
1982   // each vector.
1983   for (int MaskIndex : SVN->getMask()) {
1984     bool SelectMaskVal = (MaskIndex < (int)NumElts) ^ InvertMask;
1985     MaskVals.push_back(DAG.getConstant(SelectMaskVal, DL, XLenVT));
1986     if (!IsSelect) {
1987       bool IsLHSOrUndefIndex = MaskIndex < (int)NumElts;
1988       GatherIndicesLHS.push_back(IsLHSOrUndefIndex && MaskIndex >= 0
1989                                      ? DAG.getConstant(MaskIndex, DL, XLenVT)
1990                                      : DAG.getUNDEF(XLenVT));
1991       GatherIndicesRHS.push_back(
1992           IsLHSOrUndefIndex ? DAG.getUNDEF(XLenVT)
1993                             : DAG.getConstant(MaskIndex - NumElts, DL, XLenVT));
1994       if (IsLHSOrUndefIndex && MaskIndex >= 0)
1995         ++LHSIndexCounts[MaskIndex];
1996       if (!IsLHSOrUndefIndex)
1997         ++RHSIndexCounts[MaskIndex - NumElts];
1998     }
1999   }
2000 
2001   if (SwapOps) {
2002     std::swap(V1, V2);
2003     std::swap(GatherIndicesLHS, GatherIndicesRHS);
2004   }
2005 
2006   assert(MaskVals.size() == NumElts && "Unexpected select-like shuffle");
2007   MVT MaskVT = MVT::getVectorVT(MVT::i1, NumElts);
2008   SDValue SelectMask = DAG.getBuildVector(MaskVT, DL, MaskVals);
2009 
2010   if (IsSelect)
2011     return DAG.getNode(ISD::VSELECT, DL, VT, SelectMask, V1, V2);
2012 
2013   if (VT.getScalarSizeInBits() == 8 && VT.getVectorNumElements() > 256) {
2014     // On such a large vector we're unable to use i8 as the index type.
2015     // FIXME: We could promote the index to i16 and use vrgatherei16, but that
2016     // may involve vector splitting if we're already at LMUL=8, or our
2017     // user-supplied maximum fixed-length LMUL.
2018     return SDValue();
2019   }
2020 
2021   unsigned GatherVXOpc = RISCVISD::VRGATHER_VX_VL;
2022   unsigned GatherVVOpc = RISCVISD::VRGATHER_VV_VL;
2023   MVT IndexVT = VT.changeTypeToInteger();
2024   // Since we can't introduce illegal index types at this stage, use i16 and
2025   // vrgatherei16 if the corresponding index type for plain vrgather is greater
2026   // than XLenVT.
2027   if (IndexVT.getScalarType().bitsGT(XLenVT)) {
2028     GatherVVOpc = RISCVISD::VRGATHEREI16_VV_VL;
2029     IndexVT = IndexVT.changeVectorElementType(MVT::i16);
2030   }
2031 
2032   MVT IndexContainerVT =
2033       ContainerVT.changeVectorElementType(IndexVT.getScalarType());
2034 
2035   SDValue Gather;
2036   // TODO: This doesn't trigger for i64 vectors on RV32, since there we
2037   // encounter a bitcasted BUILD_VECTOR with low/high i32 values.
2038   if (SDValue SplatValue = DAG.getSplatValue(V1, /*LegalTypes*/ true)) {
2039     Gather = lowerScalarSplat(SplatValue, VL, ContainerVT, DL, DAG, Subtarget);
2040   } else {
2041     V1 = convertToScalableVector(ContainerVT, V1, DAG, Subtarget);
2042     // If only one index is used, we can use a "splat" vrgather.
2043     // TODO: We can splat the most-common index and fix-up any stragglers, if
2044     // that's beneficial.
2045     if (LHSIndexCounts.size() == 1) {
2046       int SplatIndex = LHSIndexCounts.begin()->getFirst();
2047       Gather =
2048           DAG.getNode(GatherVXOpc, DL, ContainerVT, V1,
2049                       DAG.getConstant(SplatIndex, DL, XLenVT), TrueMask, VL);
2050     } else {
2051       SDValue LHSIndices = DAG.getBuildVector(IndexVT, DL, GatherIndicesLHS);
2052       LHSIndices =
2053           convertToScalableVector(IndexContainerVT, LHSIndices, DAG, Subtarget);
2054 
2055       Gather = DAG.getNode(GatherVVOpc, DL, ContainerVT, V1, LHSIndices,
2056                            TrueMask, VL);
2057     }
2058   }
2059 
2060   // If a second vector operand is used by this shuffle, blend it in with an
2061   // additional vrgather.
2062   if (!V2.isUndef()) {
2063     V2 = convertToScalableVector(ContainerVT, V2, DAG, Subtarget);
2064     // If only one index is used, we can use a "splat" vrgather.
2065     // TODO: We can splat the most-common index and fix-up any stragglers, if
2066     // that's beneficial.
2067     if (RHSIndexCounts.size() == 1) {
2068       int SplatIndex = RHSIndexCounts.begin()->getFirst();
2069       V2 = DAG.getNode(GatherVXOpc, DL, ContainerVT, V2,
2070                        DAG.getConstant(SplatIndex, DL, XLenVT), TrueMask, VL);
2071     } else {
2072       SDValue RHSIndices = DAG.getBuildVector(IndexVT, DL, GatherIndicesRHS);
2073       RHSIndices =
2074           convertToScalableVector(IndexContainerVT, RHSIndices, DAG, Subtarget);
2075       V2 = DAG.getNode(GatherVVOpc, DL, ContainerVT, V2, RHSIndices, TrueMask,
2076                        VL);
2077     }
2078 
2079     MVT MaskContainerVT = ContainerVT.changeVectorElementType(MVT::i1);
2080     SelectMask =
2081         convertToScalableVector(MaskContainerVT, SelectMask, DAG, Subtarget);
2082 
2083     Gather = DAG.getNode(RISCVISD::VSELECT_VL, DL, ContainerVT, SelectMask, V2,
2084                          Gather, VL);
2085   }
2086 
2087   return convertFromScalableVector(VT, Gather, DAG, Subtarget);
2088 }
2089 
2090 static SDValue getRVVFPExtendOrRound(SDValue Op, MVT VT, MVT ContainerVT,
2091                                      SDLoc DL, SelectionDAG &DAG,
2092                                      const RISCVSubtarget &Subtarget) {
2093   if (VT.isScalableVector())
2094     return DAG.getFPExtendOrRound(Op, DL, VT);
2095   assert(VT.isFixedLengthVector() &&
2096          "Unexpected value type for RVV FP extend/round lowering");
2097   SDValue Mask, VL;
2098   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
2099   unsigned RVVOpc = ContainerVT.bitsGT(Op.getSimpleValueType())
2100                         ? RISCVISD::FP_EXTEND_VL
2101                         : RISCVISD::FP_ROUND_VL;
2102   return DAG.getNode(RVVOpc, DL, ContainerVT, Op, Mask, VL);
2103 }
2104 
2105 // While RVV has alignment restrictions, we should always be able to load as a
2106 // legal equivalently-sized byte-typed vector instead. This method is
2107 // responsible for re-expressing a ISD::LOAD via a correctly-aligned type. If
2108 // the load is already correctly-aligned, it returns SDValue().
2109 SDValue RISCVTargetLowering::expandUnalignedRVVLoad(SDValue Op,
2110                                                     SelectionDAG &DAG) const {
2111   auto *Load = cast<LoadSDNode>(Op);
2112   assert(Load && Load->getMemoryVT().isVector() && "Expected vector load");
2113 
2114   if (allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
2115                                      Load->getMemoryVT(),
2116                                      *Load->getMemOperand()))
2117     return SDValue();
2118 
2119   SDLoc DL(Op);
2120   MVT VT = Op.getSimpleValueType();
2121   unsigned EltSizeBits = VT.getScalarSizeInBits();
2122   assert((EltSizeBits == 16 || EltSizeBits == 32 || EltSizeBits == 64) &&
2123          "Unexpected unaligned RVV load type");
2124   MVT NewVT =
2125       MVT::getVectorVT(MVT::i8, VT.getVectorElementCount() * (EltSizeBits / 8));
2126   assert(NewVT.isValid() &&
2127          "Expecting equally-sized RVV vector types to be legal");
2128   SDValue L = DAG.getLoad(NewVT, DL, Load->getChain(), Load->getBasePtr(),
2129                           Load->getPointerInfo(), Load->getOriginalAlign(),
2130                           Load->getMemOperand()->getFlags());
2131   return DAG.getMergeValues({DAG.getBitcast(VT, L), L.getValue(1)}, DL);
2132 }
2133 
2134 // While RVV has alignment restrictions, we should always be able to store as a
2135 // legal equivalently-sized byte-typed vector instead. This method is
2136 // responsible for re-expressing a ISD::STORE via a correctly-aligned type. It
2137 // returns SDValue() if the store is already correctly aligned.
2138 SDValue RISCVTargetLowering::expandUnalignedRVVStore(SDValue Op,
2139                                                      SelectionDAG &DAG) const {
2140   auto *Store = cast<StoreSDNode>(Op);
2141   assert(Store && Store->getValue().getValueType().isVector() &&
2142          "Expected vector store");
2143 
2144   if (allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
2145                                      Store->getMemoryVT(),
2146                                      *Store->getMemOperand()))
2147     return SDValue();
2148 
2149   SDLoc DL(Op);
2150   SDValue StoredVal = Store->getValue();
2151   MVT VT = StoredVal.getSimpleValueType();
2152   unsigned EltSizeBits = VT.getScalarSizeInBits();
2153   assert((EltSizeBits == 16 || EltSizeBits == 32 || EltSizeBits == 64) &&
2154          "Unexpected unaligned RVV store type");
2155   MVT NewVT =
2156       MVT::getVectorVT(MVT::i8, VT.getVectorElementCount() * (EltSizeBits / 8));
2157   assert(NewVT.isValid() &&
2158          "Expecting equally-sized RVV vector types to be legal");
2159   StoredVal = DAG.getBitcast(NewVT, StoredVal);
2160   return DAG.getStore(Store->getChain(), DL, StoredVal, Store->getBasePtr(),
2161                       Store->getPointerInfo(), Store->getOriginalAlign(),
2162                       Store->getMemOperand()->getFlags());
2163 }
2164 
2165 SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
2166                                             SelectionDAG &DAG) const {
2167   switch (Op.getOpcode()) {
2168   default:
2169     report_fatal_error("unimplemented operand");
2170   case ISD::GlobalAddress:
2171     return lowerGlobalAddress(Op, DAG);
2172   case ISD::BlockAddress:
2173     return lowerBlockAddress(Op, DAG);
2174   case ISD::ConstantPool:
2175     return lowerConstantPool(Op, DAG);
2176   case ISD::JumpTable:
2177     return lowerJumpTable(Op, DAG);
2178   case ISD::GlobalTLSAddress:
2179     return lowerGlobalTLSAddress(Op, DAG);
2180   case ISD::SELECT:
2181     return lowerSELECT(Op, DAG);
2182   case ISD::BRCOND:
2183     return lowerBRCOND(Op, DAG);
2184   case ISD::VASTART:
2185     return lowerVASTART(Op, DAG);
2186   case ISD::FRAMEADDR:
2187     return lowerFRAMEADDR(Op, DAG);
2188   case ISD::RETURNADDR:
2189     return lowerRETURNADDR(Op, DAG);
2190   case ISD::SHL_PARTS:
2191     return lowerShiftLeftParts(Op, DAG);
2192   case ISD::SRA_PARTS:
2193     return lowerShiftRightParts(Op, DAG, true);
2194   case ISD::SRL_PARTS:
2195     return lowerShiftRightParts(Op, DAG, false);
2196   case ISD::BITCAST: {
2197     SDLoc DL(Op);
2198     EVT VT = Op.getValueType();
2199     SDValue Op0 = Op.getOperand(0);
2200     EVT Op0VT = Op0.getValueType();
2201     MVT XLenVT = Subtarget.getXLenVT();
2202     if (VT.isFixedLengthVector()) {
2203       // We can handle fixed length vector bitcasts with a simple replacement
2204       // in isel.
2205       if (Op0VT.isFixedLengthVector())
2206         return Op;
2207       // When bitcasting from scalar to fixed-length vector, insert the scalar
2208       // into a one-element vector of the result type, and perform a vector
2209       // bitcast.
2210       if (!Op0VT.isVector()) {
2211         auto BVT = EVT::getVectorVT(*DAG.getContext(), Op0VT, 1);
2212         return DAG.getBitcast(VT, DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, BVT,
2213                                               DAG.getUNDEF(BVT), Op0,
2214                                               DAG.getConstant(0, DL, XLenVT)));
2215       }
2216       return SDValue();
2217     }
2218     // Custom-legalize bitcasts from fixed-length vector types to scalar types
2219     // thus: bitcast the vector to a one-element vector type whose element type
2220     // is the same as the result type, and extract the first element.
2221     if (!VT.isVector() && Op0VT.isFixedLengthVector()) {
2222       LLVMContext &Context = *DAG.getContext();
2223       SDValue BVec = DAG.getBitcast(EVT::getVectorVT(Context, VT, 1), Op0);
2224       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, BVec,
2225                          DAG.getConstant(0, DL, XLenVT));
2226     }
2227     if (VT == MVT::f16 && Op0VT == MVT::i16 && Subtarget.hasStdExtZfh()) {
2228       SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, XLenVT, Op0);
2229       SDValue FPConv = DAG.getNode(RISCVISD::FMV_H_X, DL, MVT::f16, NewOp0);
2230       return FPConv;
2231     }
2232     if (VT == MVT::f32 && Op0VT == MVT::i32 && Subtarget.is64Bit() &&
2233         Subtarget.hasStdExtF()) {
2234       SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
2235       SDValue FPConv =
2236           DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, NewOp0);
2237       return FPConv;
2238     }
2239     return SDValue();
2240   }
2241   case ISD::INTRINSIC_WO_CHAIN:
2242     return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2243   case ISD::INTRINSIC_W_CHAIN:
2244     return LowerINTRINSIC_W_CHAIN(Op, DAG);
2245   case ISD::BSWAP:
2246   case ISD::BITREVERSE: {
2247     // Convert BSWAP/BITREVERSE to GREVI to enable GREVI combinining.
2248     assert(Subtarget.hasStdExtZbp() && "Unexpected custom legalisation");
2249     MVT VT = Op.getSimpleValueType();
2250     SDLoc DL(Op);
2251     // Start with the maximum immediate value which is the bitwidth - 1.
2252     unsigned Imm = VT.getSizeInBits() - 1;
2253     // If this is BSWAP rather than BITREVERSE, clear the lower 3 bits.
2254     if (Op.getOpcode() == ISD::BSWAP)
2255       Imm &= ~0x7U;
2256     return DAG.getNode(RISCVISD::GREV, DL, VT, Op.getOperand(0),
2257                        DAG.getConstant(Imm, DL, VT));
2258   }
2259   case ISD::FSHL:
2260   case ISD::FSHR: {
2261     MVT VT = Op.getSimpleValueType();
2262     assert(VT == Subtarget.getXLenVT() && "Unexpected custom legalization");
2263     SDLoc DL(Op);
2264     if (Op.getOperand(2).getOpcode() == ISD::Constant)
2265       return Op;
2266     // FSL/FSR take a log2(XLen)+1 bit shift amount but XLenVT FSHL/FSHR only
2267     // use log(XLen) bits. Mask the shift amount accordingly.
2268     unsigned ShAmtWidth = Subtarget.getXLen() - 1;
2269     SDValue ShAmt = DAG.getNode(ISD::AND, DL, VT, Op.getOperand(2),
2270                                 DAG.getConstant(ShAmtWidth, DL, VT));
2271     unsigned Opc = Op.getOpcode() == ISD::FSHL ? RISCVISD::FSL : RISCVISD::FSR;
2272     return DAG.getNode(Opc, DL, VT, Op.getOperand(0), Op.getOperand(1), ShAmt);
2273   }
2274   case ISD::TRUNCATE: {
2275     SDLoc DL(Op);
2276     MVT VT = Op.getSimpleValueType();
2277     // Only custom-lower vector truncates
2278     if (!VT.isVector())
2279       return Op;
2280 
2281     // Truncates to mask types are handled differently
2282     if (VT.getVectorElementType() == MVT::i1)
2283       return lowerVectorMaskTrunc(Op, DAG);
2284 
2285     // RVV only has truncates which operate from SEW*2->SEW, so lower arbitrary
2286     // truncates as a series of "RISCVISD::TRUNCATE_VECTOR_VL" nodes which
2287     // truncate by one power of two at a time.
2288     MVT DstEltVT = VT.getVectorElementType();
2289 
2290     SDValue Src = Op.getOperand(0);
2291     MVT SrcVT = Src.getSimpleValueType();
2292     MVT SrcEltVT = SrcVT.getVectorElementType();
2293 
2294     assert(DstEltVT.bitsLT(SrcEltVT) &&
2295            isPowerOf2_64(DstEltVT.getSizeInBits()) &&
2296            isPowerOf2_64(SrcEltVT.getSizeInBits()) &&
2297            "Unexpected vector truncate lowering");
2298 
2299     MVT ContainerVT = SrcVT;
2300     if (SrcVT.isFixedLengthVector()) {
2301       ContainerVT = getContainerForFixedLengthVector(SrcVT);
2302       Src = convertToScalableVector(ContainerVT, Src, DAG, Subtarget);
2303     }
2304 
2305     SDValue Result = Src;
2306     SDValue Mask, VL;
2307     std::tie(Mask, VL) =
2308         getDefaultVLOps(SrcVT, ContainerVT, DL, DAG, Subtarget);
2309     LLVMContext &Context = *DAG.getContext();
2310     const ElementCount Count = ContainerVT.getVectorElementCount();
2311     do {
2312       SrcEltVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits() / 2);
2313       EVT ResultVT = EVT::getVectorVT(Context, SrcEltVT, Count);
2314       Result = DAG.getNode(RISCVISD::TRUNCATE_VECTOR_VL, DL, ResultVT, Result,
2315                            Mask, VL);
2316     } while (SrcEltVT != DstEltVT);
2317 
2318     if (SrcVT.isFixedLengthVector())
2319       Result = convertFromScalableVector(VT, Result, DAG, Subtarget);
2320 
2321     return Result;
2322   }
2323   case ISD::ANY_EXTEND:
2324   case ISD::ZERO_EXTEND:
2325     if (Op.getOperand(0).getValueType().isVector() &&
2326         Op.getOperand(0).getValueType().getVectorElementType() == MVT::i1)
2327       return lowerVectorMaskExt(Op, DAG, /*ExtVal*/ 1);
2328     return lowerFixedLengthVectorExtendToRVV(Op, DAG, RISCVISD::VZEXT_VL);
2329   case ISD::SIGN_EXTEND:
2330     if (Op.getOperand(0).getValueType().isVector() &&
2331         Op.getOperand(0).getValueType().getVectorElementType() == MVT::i1)
2332       return lowerVectorMaskExt(Op, DAG, /*ExtVal*/ -1);
2333     return lowerFixedLengthVectorExtendToRVV(Op, DAG, RISCVISD::VSEXT_VL);
2334   case ISD::SPLAT_VECTOR_PARTS:
2335     return lowerSPLAT_VECTOR_PARTS(Op, DAG);
2336   case ISD::INSERT_VECTOR_ELT:
2337     return lowerINSERT_VECTOR_ELT(Op, DAG);
2338   case ISD::EXTRACT_VECTOR_ELT:
2339     return lowerEXTRACT_VECTOR_ELT(Op, DAG);
2340   case ISD::VSCALE: {
2341     MVT VT = Op.getSimpleValueType();
2342     SDLoc DL(Op);
2343     SDValue VLENB = DAG.getNode(RISCVISD::READ_VLENB, DL, VT);
2344     // We define our scalable vector types for lmul=1 to use a 64 bit known
2345     // minimum size. e.g. <vscale x 2 x i32>. VLENB is in bytes so we calculate
2346     // vscale as VLENB / 8.
2347     assert(RISCV::RVVBitsPerBlock == 64 && "Unexpected bits per block!");
2348     if (isa<ConstantSDNode>(Op.getOperand(0))) {
2349       // We assume VLENB is a multiple of 8. We manually choose the best shift
2350       // here because SimplifyDemandedBits isn't always able to simplify it.
2351       uint64_t Val = Op.getConstantOperandVal(0);
2352       if (isPowerOf2_64(Val)) {
2353         uint64_t Log2 = Log2_64(Val);
2354         if (Log2 < 3)
2355           return DAG.getNode(ISD::SRL, DL, VT, VLENB,
2356                              DAG.getConstant(3 - Log2, DL, VT));
2357         if (Log2 > 3)
2358           return DAG.getNode(ISD::SHL, DL, VT, VLENB,
2359                              DAG.getConstant(Log2 - 3, DL, VT));
2360         return VLENB;
2361       }
2362       // If the multiplier is a multiple of 8, scale it down to avoid needing
2363       // to shift the VLENB value.
2364       if ((Val % 8) == 0)
2365         return DAG.getNode(ISD::MUL, DL, VT, VLENB,
2366                            DAG.getConstant(Val / 8, DL, VT));
2367     }
2368 
2369     SDValue VScale = DAG.getNode(ISD::SRL, DL, VT, VLENB,
2370                                  DAG.getConstant(3, DL, VT));
2371     return DAG.getNode(ISD::MUL, DL, VT, VScale, Op.getOperand(0));
2372   }
2373   case ISD::FP_EXTEND: {
2374     // RVV can only do fp_extend to types double the size as the source. We
2375     // custom-lower f16->f64 extensions to two hops of ISD::FP_EXTEND, going
2376     // via f32.
2377     SDLoc DL(Op);
2378     MVT VT = Op.getSimpleValueType();
2379     SDValue Src = Op.getOperand(0);
2380     MVT SrcVT = Src.getSimpleValueType();
2381 
2382     // Prepare any fixed-length vector operands.
2383     MVT ContainerVT = VT;
2384     if (SrcVT.isFixedLengthVector()) {
2385       ContainerVT = getContainerForFixedLengthVector(VT);
2386       MVT SrcContainerVT =
2387           ContainerVT.changeVectorElementType(SrcVT.getVectorElementType());
2388       Src = convertToScalableVector(SrcContainerVT, Src, DAG, Subtarget);
2389     }
2390 
2391     if (!VT.isVector() || VT.getVectorElementType() != MVT::f64 ||
2392         SrcVT.getVectorElementType() != MVT::f16) {
2393       // For scalable vectors, we only need to close the gap between
2394       // vXf16->vXf64.
2395       if (!VT.isFixedLengthVector())
2396         return Op;
2397       // For fixed-length vectors, lower the FP_EXTEND to a custom "VL" version.
2398       Src = getRVVFPExtendOrRound(Src, VT, ContainerVT, DL, DAG, Subtarget);
2399       return convertFromScalableVector(VT, Src, DAG, Subtarget);
2400     }
2401 
2402     MVT InterVT = VT.changeVectorElementType(MVT::f32);
2403     MVT InterContainerVT = ContainerVT.changeVectorElementType(MVT::f32);
2404     SDValue IntermediateExtend = getRVVFPExtendOrRound(
2405         Src, InterVT, InterContainerVT, DL, DAG, Subtarget);
2406 
2407     SDValue Extend = getRVVFPExtendOrRound(IntermediateExtend, VT, ContainerVT,
2408                                            DL, DAG, Subtarget);
2409     if (VT.isFixedLengthVector())
2410       return convertFromScalableVector(VT, Extend, DAG, Subtarget);
2411     return Extend;
2412   }
2413   case ISD::FP_ROUND: {
2414     // RVV can only do fp_round to types half the size as the source. We
2415     // custom-lower f64->f16 rounds via RVV's round-to-odd float
2416     // conversion instruction.
2417     SDLoc DL(Op);
2418     MVT VT = Op.getSimpleValueType();
2419     SDValue Src = Op.getOperand(0);
2420     MVT SrcVT = Src.getSimpleValueType();
2421 
2422     // Prepare any fixed-length vector operands.
2423     MVT ContainerVT = VT;
2424     if (VT.isFixedLengthVector()) {
2425       MVT SrcContainerVT = getContainerForFixedLengthVector(SrcVT);
2426       ContainerVT =
2427           SrcContainerVT.changeVectorElementType(VT.getVectorElementType());
2428       Src = convertToScalableVector(SrcContainerVT, Src, DAG, Subtarget);
2429     }
2430 
2431     if (!VT.isVector() || VT.getVectorElementType() != MVT::f16 ||
2432         SrcVT.getVectorElementType() != MVT::f64) {
2433       // For scalable vectors, we only need to close the gap between
2434       // vXf64<->vXf16.
2435       if (!VT.isFixedLengthVector())
2436         return Op;
2437       // For fixed-length vectors, lower the FP_ROUND to a custom "VL" version.
2438       Src = getRVVFPExtendOrRound(Src, VT, ContainerVT, DL, DAG, Subtarget);
2439       return convertFromScalableVector(VT, Src, DAG, Subtarget);
2440     }
2441 
2442     SDValue Mask, VL;
2443     std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
2444 
2445     MVT InterVT = ContainerVT.changeVectorElementType(MVT::f32);
2446     SDValue IntermediateRound =
2447         DAG.getNode(RISCVISD::VFNCVT_ROD_VL, DL, InterVT, Src, Mask, VL);
2448     SDValue Round = getRVVFPExtendOrRound(IntermediateRound, VT, ContainerVT,
2449                                           DL, DAG, Subtarget);
2450 
2451     if (VT.isFixedLengthVector())
2452       return convertFromScalableVector(VT, Round, DAG, Subtarget);
2453     return Round;
2454   }
2455   case ISD::FP_TO_SINT:
2456   case ISD::FP_TO_UINT:
2457   case ISD::SINT_TO_FP:
2458   case ISD::UINT_TO_FP: {
2459     // RVV can only do fp<->int conversions to types half/double the size as
2460     // the source. We custom-lower any conversions that do two hops into
2461     // sequences.
2462     MVT VT = Op.getSimpleValueType();
2463     if (!VT.isVector())
2464       return Op;
2465     SDLoc DL(Op);
2466     SDValue Src = Op.getOperand(0);
2467     MVT EltVT = VT.getVectorElementType();
2468     MVT SrcVT = Src.getSimpleValueType();
2469     MVT SrcEltVT = SrcVT.getVectorElementType();
2470     unsigned EltSize = EltVT.getSizeInBits();
2471     unsigned SrcEltSize = SrcEltVT.getSizeInBits();
2472     assert(isPowerOf2_32(EltSize) && isPowerOf2_32(SrcEltSize) &&
2473            "Unexpected vector element types");
2474 
2475     bool IsInt2FP = SrcEltVT.isInteger();
2476     // Widening conversions
2477     if (EltSize > SrcEltSize && (EltSize / SrcEltSize >= 4)) {
2478       if (IsInt2FP) {
2479         // Do a regular integer sign/zero extension then convert to float.
2480         MVT IVecVT = MVT::getVectorVT(MVT::getIntegerVT(EltVT.getSizeInBits()),
2481                                       VT.getVectorElementCount());
2482         unsigned ExtOpcode = Op.getOpcode() == ISD::UINT_TO_FP
2483                                  ? ISD::ZERO_EXTEND
2484                                  : ISD::SIGN_EXTEND;
2485         SDValue Ext = DAG.getNode(ExtOpcode, DL, IVecVT, Src);
2486         return DAG.getNode(Op.getOpcode(), DL, VT, Ext);
2487       }
2488       // FP2Int
2489       assert(SrcEltVT == MVT::f16 && "Unexpected FP_TO_[US]INT lowering");
2490       // Do one doubling fp_extend then complete the operation by converting
2491       // to int.
2492       MVT InterimFVT = MVT::getVectorVT(MVT::f32, VT.getVectorElementCount());
2493       SDValue FExt = DAG.getFPExtendOrRound(Src, DL, InterimFVT);
2494       return DAG.getNode(Op.getOpcode(), DL, VT, FExt);
2495     }
2496 
2497     // Narrowing conversions
2498     if (SrcEltSize > EltSize && (SrcEltSize / EltSize >= 4)) {
2499       if (IsInt2FP) {
2500         // One narrowing int_to_fp, then an fp_round.
2501         assert(EltVT == MVT::f16 && "Unexpected [US]_TO_FP lowering");
2502         MVT InterimFVT = MVT::getVectorVT(MVT::f32, VT.getVectorElementCount());
2503         SDValue Int2FP = DAG.getNode(Op.getOpcode(), DL, InterimFVT, Src);
2504         return DAG.getFPExtendOrRound(Int2FP, DL, VT);
2505       }
2506       // FP2Int
2507       // One narrowing fp_to_int, then truncate the integer. If the float isn't
2508       // representable by the integer, the result is poison.
2509       MVT IVecVT =
2510           MVT::getVectorVT(MVT::getIntegerVT(SrcEltVT.getSizeInBits() / 2),
2511                            VT.getVectorElementCount());
2512       SDValue FP2Int = DAG.getNode(Op.getOpcode(), DL, IVecVT, Src);
2513       return DAG.getNode(ISD::TRUNCATE, DL, VT, FP2Int);
2514     }
2515 
2516     // Scalable vectors can exit here. Patterns will handle equally-sized
2517     // conversions halving/doubling ones.
2518     if (!VT.isFixedLengthVector())
2519       return Op;
2520 
2521     // For fixed-length vectors we lower to a custom "VL" node.
2522     unsigned RVVOpc = 0;
2523     switch (Op.getOpcode()) {
2524     default:
2525       llvm_unreachable("Impossible opcode");
2526     case ISD::FP_TO_SINT:
2527       RVVOpc = RISCVISD::FP_TO_SINT_VL;
2528       break;
2529     case ISD::FP_TO_UINT:
2530       RVVOpc = RISCVISD::FP_TO_UINT_VL;
2531       break;
2532     case ISD::SINT_TO_FP:
2533       RVVOpc = RISCVISD::SINT_TO_FP_VL;
2534       break;
2535     case ISD::UINT_TO_FP:
2536       RVVOpc = RISCVISD::UINT_TO_FP_VL;
2537       break;
2538     }
2539 
2540     MVT ContainerVT, SrcContainerVT;
2541     // Derive the reference container type from the larger vector type.
2542     if (SrcEltSize > EltSize) {
2543       SrcContainerVT = getContainerForFixedLengthVector(SrcVT);
2544       ContainerVT =
2545           SrcContainerVT.changeVectorElementType(VT.getVectorElementType());
2546     } else {
2547       ContainerVT = getContainerForFixedLengthVector(VT);
2548       SrcContainerVT = ContainerVT.changeVectorElementType(SrcEltVT);
2549     }
2550 
2551     SDValue Mask, VL;
2552     std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
2553 
2554     Src = convertToScalableVector(SrcContainerVT, Src, DAG, Subtarget);
2555     Src = DAG.getNode(RVVOpc, DL, ContainerVT, Src, Mask, VL);
2556     return convertFromScalableVector(VT, Src, DAG, Subtarget);
2557   }
2558   case ISD::FP_TO_SINT_SAT:
2559   case ISD::FP_TO_UINT_SAT:
2560     return lowerFP_TO_INT_SAT(Op, DAG);
2561   case ISD::VECREDUCE_ADD:
2562   case ISD::VECREDUCE_UMAX:
2563   case ISD::VECREDUCE_SMAX:
2564   case ISD::VECREDUCE_UMIN:
2565   case ISD::VECREDUCE_SMIN:
2566     return lowerVECREDUCE(Op, DAG);
2567   case ISD::VECREDUCE_AND:
2568   case ISD::VECREDUCE_OR:
2569   case ISD::VECREDUCE_XOR:
2570     if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i1)
2571       return lowerVectorMaskVECREDUCE(Op, DAG);
2572     return lowerVECREDUCE(Op, DAG);
2573   case ISD::VECREDUCE_FADD:
2574   case ISD::VECREDUCE_SEQ_FADD:
2575   case ISD::VECREDUCE_FMIN:
2576   case ISD::VECREDUCE_FMAX:
2577     return lowerFPVECREDUCE(Op, DAG);
2578   case ISD::INSERT_SUBVECTOR:
2579     return lowerINSERT_SUBVECTOR(Op, DAG);
2580   case ISD::EXTRACT_SUBVECTOR:
2581     return lowerEXTRACT_SUBVECTOR(Op, DAG);
2582   case ISD::STEP_VECTOR:
2583     return lowerSTEP_VECTOR(Op, DAG);
2584   case ISD::VECTOR_REVERSE:
2585     return lowerVECTOR_REVERSE(Op, DAG);
2586   case ISD::BUILD_VECTOR:
2587     return lowerBUILD_VECTOR(Op, DAG, Subtarget);
2588   case ISD::SPLAT_VECTOR:
2589     if (Op.getValueType().getVectorElementType() == MVT::i1)
2590       return lowerVectorMaskSplat(Op, DAG);
2591     return lowerSPLAT_VECTOR(Op, DAG, Subtarget);
2592   case ISD::VECTOR_SHUFFLE:
2593     return lowerVECTOR_SHUFFLE(Op, DAG, Subtarget);
2594   case ISD::CONCAT_VECTORS: {
2595     // Split CONCAT_VECTORS into a series of INSERT_SUBVECTOR nodes. This is
2596     // better than going through the stack, as the default expansion does.
2597     SDLoc DL(Op);
2598     MVT VT = Op.getSimpleValueType();
2599     unsigned NumOpElts =
2600         Op.getOperand(0).getSimpleValueType().getVectorMinNumElements();
2601     SDValue Vec = DAG.getUNDEF(VT);
2602     for (const auto &OpIdx : enumerate(Op->ops()))
2603       Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, Vec, OpIdx.value(),
2604                         DAG.getIntPtrConstant(OpIdx.index() * NumOpElts, DL));
2605     return Vec;
2606   }
2607   case ISD::LOAD:
2608     if (auto V = expandUnalignedRVVLoad(Op, DAG))
2609       return V;
2610     if (Op.getValueType().isFixedLengthVector())
2611       return lowerFixedLengthVectorLoadToRVV(Op, DAG);
2612     return Op;
2613   case ISD::STORE:
2614     if (auto V = expandUnalignedRVVStore(Op, DAG))
2615       return V;
2616     if (Op.getOperand(1).getValueType().isFixedLengthVector())
2617       return lowerFixedLengthVectorStoreToRVV(Op, DAG);
2618     return Op;
2619   case ISD::MLOAD:
2620     return lowerMLOAD(Op, DAG);
2621   case ISD::MSTORE:
2622     return lowerMSTORE(Op, DAG);
2623   case ISD::SETCC:
2624     return lowerFixedLengthVectorSetccToRVV(Op, DAG);
2625   case ISD::ADD:
2626     return lowerToScalableOp(Op, DAG, RISCVISD::ADD_VL);
2627   case ISD::SUB:
2628     return lowerToScalableOp(Op, DAG, RISCVISD::SUB_VL);
2629   case ISD::MUL:
2630     return lowerToScalableOp(Op, DAG, RISCVISD::MUL_VL);
2631   case ISD::MULHS:
2632     return lowerToScalableOp(Op, DAG, RISCVISD::MULHS_VL);
2633   case ISD::MULHU:
2634     return lowerToScalableOp(Op, DAG, RISCVISD::MULHU_VL);
2635   case ISD::AND:
2636     return lowerFixedLengthVectorLogicOpToRVV(Op, DAG, RISCVISD::VMAND_VL,
2637                                               RISCVISD::AND_VL);
2638   case ISD::OR:
2639     return lowerFixedLengthVectorLogicOpToRVV(Op, DAG, RISCVISD::VMOR_VL,
2640                                               RISCVISD::OR_VL);
2641   case ISD::XOR:
2642     return lowerFixedLengthVectorLogicOpToRVV(Op, DAG, RISCVISD::VMXOR_VL,
2643                                               RISCVISD::XOR_VL);
2644   case ISD::SDIV:
2645     return lowerToScalableOp(Op, DAG, RISCVISD::SDIV_VL);
2646   case ISD::SREM:
2647     return lowerToScalableOp(Op, DAG, RISCVISD::SREM_VL);
2648   case ISD::UDIV:
2649     return lowerToScalableOp(Op, DAG, RISCVISD::UDIV_VL);
2650   case ISD::UREM:
2651     return lowerToScalableOp(Op, DAG, RISCVISD::UREM_VL);
2652   case ISD::SHL:
2653   case ISD::SRA:
2654   case ISD::SRL:
2655     if (Op.getSimpleValueType().isFixedLengthVector())
2656       return lowerFixedLengthVectorShiftToRVV(Op, DAG);
2657     // This can be called for an i32 shift amount that needs to be promoted.
2658     assert(Op.getOperand(1).getValueType() == MVT::i32 && Subtarget.is64Bit() &&
2659            "Unexpected custom legalisation");
2660     return SDValue();
2661   case ISD::SADDSAT:
2662     return lowerToScalableOp(Op, DAG, RISCVISD::SADDSAT_VL);
2663   case ISD::UADDSAT:
2664     return lowerToScalableOp(Op, DAG, RISCVISD::UADDSAT_VL);
2665   case ISD::SSUBSAT:
2666     return lowerToScalableOp(Op, DAG, RISCVISD::SSUBSAT_VL);
2667   case ISD::USUBSAT:
2668     return lowerToScalableOp(Op, DAG, RISCVISD::USUBSAT_VL);
2669   case ISD::FADD:
2670     return lowerToScalableOp(Op, DAG, RISCVISD::FADD_VL);
2671   case ISD::FSUB:
2672     return lowerToScalableOp(Op, DAG, RISCVISD::FSUB_VL);
2673   case ISD::FMUL:
2674     return lowerToScalableOp(Op, DAG, RISCVISD::FMUL_VL);
2675   case ISD::FDIV:
2676     return lowerToScalableOp(Op, DAG, RISCVISD::FDIV_VL);
2677   case ISD::FNEG:
2678     return lowerToScalableOp(Op, DAG, RISCVISD::FNEG_VL);
2679   case ISD::FABS:
2680     return lowerToScalableOp(Op, DAG, RISCVISD::FABS_VL);
2681   case ISD::FSQRT:
2682     return lowerToScalableOp(Op, DAG, RISCVISD::FSQRT_VL);
2683   case ISD::FMA:
2684     return lowerToScalableOp(Op, DAG, RISCVISD::FMA_VL);
2685   case ISD::SMIN:
2686     return lowerToScalableOp(Op, DAG, RISCVISD::SMIN_VL);
2687   case ISD::SMAX:
2688     return lowerToScalableOp(Op, DAG, RISCVISD::SMAX_VL);
2689   case ISD::UMIN:
2690     return lowerToScalableOp(Op, DAG, RISCVISD::UMIN_VL);
2691   case ISD::UMAX:
2692     return lowerToScalableOp(Op, DAG, RISCVISD::UMAX_VL);
2693   case ISD::FMINNUM:
2694     return lowerToScalableOp(Op, DAG, RISCVISD::FMINNUM_VL);
2695   case ISD::FMAXNUM:
2696     return lowerToScalableOp(Op, DAG, RISCVISD::FMAXNUM_VL);
2697   case ISD::ABS:
2698     return lowerABS(Op, DAG);
2699   case ISD::VSELECT:
2700     return lowerFixedLengthVectorSelectToRVV(Op, DAG);
2701   case ISD::FCOPYSIGN:
2702     return lowerFixedLengthVectorFCOPYSIGNToRVV(Op, DAG);
2703   case ISD::MGATHER:
2704     return lowerMGATHER(Op, DAG);
2705   case ISD::MSCATTER:
2706     return lowerMSCATTER(Op, DAG);
2707   case ISD::FLT_ROUNDS_:
2708     return lowerGET_ROUNDING(Op, DAG);
2709   case ISD::SET_ROUNDING:
2710     return lowerSET_ROUNDING(Op, DAG);
2711   case ISD::VP_ADD:
2712     return lowerVPOp(Op, DAG, RISCVISD::ADD_VL);
2713   case ISD::VP_SUB:
2714     return lowerVPOp(Op, DAG, RISCVISD::SUB_VL);
2715   case ISD::VP_MUL:
2716     return lowerVPOp(Op, DAG, RISCVISD::MUL_VL);
2717   case ISD::VP_SDIV:
2718     return lowerVPOp(Op, DAG, RISCVISD::SDIV_VL);
2719   case ISD::VP_UDIV:
2720     return lowerVPOp(Op, DAG, RISCVISD::UDIV_VL);
2721   case ISD::VP_SREM:
2722     return lowerVPOp(Op, DAG, RISCVISD::SREM_VL);
2723   case ISD::VP_UREM:
2724     return lowerVPOp(Op, DAG, RISCVISD::UREM_VL);
2725   case ISD::VP_AND:
2726     return lowerVPOp(Op, DAG, RISCVISD::AND_VL);
2727   case ISD::VP_OR:
2728     return lowerVPOp(Op, DAG, RISCVISD::OR_VL);
2729   case ISD::VP_XOR:
2730     return lowerVPOp(Op, DAG, RISCVISD::XOR_VL);
2731   case ISD::VP_ASHR:
2732     return lowerVPOp(Op, DAG, RISCVISD::SRA_VL);
2733   case ISD::VP_LSHR:
2734     return lowerVPOp(Op, DAG, RISCVISD::SRL_VL);
2735   case ISD::VP_SHL:
2736     return lowerVPOp(Op, DAG, RISCVISD::SHL_VL);
2737   case ISD::VP_FADD:
2738     return lowerVPOp(Op, DAG, RISCVISD::FADD_VL);
2739   case ISD::VP_FSUB:
2740     return lowerVPOp(Op, DAG, RISCVISD::FSUB_VL);
2741   case ISD::VP_FMUL:
2742     return lowerVPOp(Op, DAG, RISCVISD::FMUL_VL);
2743   case ISD::VP_FDIV:
2744     return lowerVPOp(Op, DAG, RISCVISD::FDIV_VL);
2745   }
2746 }
2747 
2748 static SDValue getTargetNode(GlobalAddressSDNode *N, SDLoc DL, EVT Ty,
2749                              SelectionDAG &DAG, unsigned Flags) {
2750   return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
2751 }
2752 
2753 static SDValue getTargetNode(BlockAddressSDNode *N, SDLoc DL, EVT Ty,
2754                              SelectionDAG &DAG, unsigned Flags) {
2755   return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, N->getOffset(),
2756                                    Flags);
2757 }
2758 
2759 static SDValue getTargetNode(ConstantPoolSDNode *N, SDLoc DL, EVT Ty,
2760                              SelectionDAG &DAG, unsigned Flags) {
2761   return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
2762                                    N->getOffset(), Flags);
2763 }
2764 
2765 static SDValue getTargetNode(JumpTableSDNode *N, SDLoc DL, EVT Ty,
2766                              SelectionDAG &DAG, unsigned Flags) {
2767   return DAG.getTargetJumpTable(N->getIndex(), Ty, Flags);
2768 }
2769 
2770 template <class NodeTy>
2771 SDValue RISCVTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
2772                                      bool IsLocal) const {
2773   SDLoc DL(N);
2774   EVT Ty = getPointerTy(DAG.getDataLayout());
2775 
2776   if (isPositionIndependent()) {
2777     SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
2778     if (IsLocal)
2779       // Use PC-relative addressing to access the symbol. This generates the
2780       // pattern (PseudoLLA sym), which expands to (addi (auipc %pcrel_hi(sym))
2781       // %pcrel_lo(auipc)).
2782       return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
2783 
2784     // Use PC-relative addressing to access the GOT for this symbol, then load
2785     // the address from the GOT. This generates the pattern (PseudoLA sym),
2786     // which expands to (ld (addi (auipc %got_pcrel_hi(sym)) %pcrel_lo(auipc))).
2787     return SDValue(DAG.getMachineNode(RISCV::PseudoLA, DL, Ty, Addr), 0);
2788   }
2789 
2790   switch (getTargetMachine().getCodeModel()) {
2791   default:
2792     report_fatal_error("Unsupported code model for lowering");
2793   case CodeModel::Small: {
2794     // Generate a sequence for accessing addresses within the first 2 GiB of
2795     // address space. This generates the pattern (addi (lui %hi(sym)) %lo(sym)).
2796     SDValue AddrHi = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_HI);
2797     SDValue AddrLo = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_LO);
2798     SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
2799     return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, AddrLo), 0);
2800   }
2801   case CodeModel::Medium: {
2802     // Generate a sequence for accessing addresses within any 2GiB range within
2803     // the address space. This generates the pattern (PseudoLLA sym), which
2804     // expands to (addi (auipc %pcrel_hi(sym)) %pcrel_lo(auipc)).
2805     SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
2806     return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
2807   }
2808   }
2809 }
2810 
2811 SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
2812                                                 SelectionDAG &DAG) const {
2813   SDLoc DL(Op);
2814   EVT Ty = Op.getValueType();
2815   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
2816   int64_t Offset = N->getOffset();
2817   MVT XLenVT = Subtarget.getXLenVT();
2818 
2819   const GlobalValue *GV = N->getGlobal();
2820   bool IsLocal = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
2821   SDValue Addr = getAddr(N, DAG, IsLocal);
2822 
2823   // In order to maximise the opportunity for common subexpression elimination,
2824   // emit a separate ADD node for the global address offset instead of folding
2825   // it in the global address node. Later peephole optimisations may choose to
2826   // fold it back in when profitable.
2827   if (Offset != 0)
2828     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
2829                        DAG.getConstant(Offset, DL, XLenVT));
2830   return Addr;
2831 }
2832 
2833 SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
2834                                                SelectionDAG &DAG) const {
2835   BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
2836 
2837   return getAddr(N, DAG);
2838 }
2839 
2840 SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
2841                                                SelectionDAG &DAG) const {
2842   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
2843 
2844   return getAddr(N, DAG);
2845 }
2846 
2847 SDValue RISCVTargetLowering::lowerJumpTable(SDValue Op,
2848                                             SelectionDAG &DAG) const {
2849   JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
2850 
2851   return getAddr(N, DAG);
2852 }
2853 
2854 SDValue RISCVTargetLowering::getStaticTLSAddr(GlobalAddressSDNode *N,
2855                                               SelectionDAG &DAG,
2856                                               bool UseGOT) const {
2857   SDLoc DL(N);
2858   EVT Ty = getPointerTy(DAG.getDataLayout());
2859   const GlobalValue *GV = N->getGlobal();
2860   MVT XLenVT = Subtarget.getXLenVT();
2861 
2862   if (UseGOT) {
2863     // Use PC-relative addressing to access the GOT for this TLS symbol, then
2864     // load the address from the GOT and add the thread pointer. This generates
2865     // the pattern (PseudoLA_TLS_IE sym), which expands to
2866     // (ld (auipc %tls_ie_pcrel_hi(sym)) %pcrel_lo(auipc)).
2867     SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
2868     SDValue Load =
2869         SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_IE, DL, Ty, Addr), 0);
2870 
2871     // Add the thread pointer.
2872     SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
2873     return DAG.getNode(ISD::ADD, DL, Ty, Load, TPReg);
2874   }
2875 
2876   // Generate a sequence for accessing the address relative to the thread
2877   // pointer, with the appropriate adjustment for the thread pointer offset.
2878   // This generates the pattern
2879   // (add (add_tprel (lui %tprel_hi(sym)) tp %tprel_add(sym)) %tprel_lo(sym))
2880   SDValue AddrHi =
2881       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_HI);
2882   SDValue AddrAdd =
2883       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_ADD);
2884   SDValue AddrLo =
2885       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_LO);
2886 
2887   SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
2888   SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
2889   SDValue MNAdd = SDValue(
2890       DAG.getMachineNode(RISCV::PseudoAddTPRel, DL, Ty, MNHi, TPReg, AddrAdd),
2891       0);
2892   return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNAdd, AddrLo), 0);
2893 }
2894 
2895 SDValue RISCVTargetLowering::getDynamicTLSAddr(GlobalAddressSDNode *N,
2896                                                SelectionDAG &DAG) const {
2897   SDLoc DL(N);
2898   EVT Ty = getPointerTy(DAG.getDataLayout());
2899   IntegerType *CallTy = Type::getIntNTy(*DAG.getContext(), Ty.getSizeInBits());
2900   const GlobalValue *GV = N->getGlobal();
2901 
2902   // Use a PC-relative addressing mode to access the global dynamic GOT address.
2903   // This generates the pattern (PseudoLA_TLS_GD sym), which expands to
2904   // (addi (auipc %tls_gd_pcrel_hi(sym)) %pcrel_lo(auipc)).
2905   SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
2906   SDValue Load =
2907       SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_GD, DL, Ty, Addr), 0);
2908 
2909   // Prepare argument list to generate call.
2910   ArgListTy Args;
2911   ArgListEntry Entry;
2912   Entry.Node = Load;
2913   Entry.Ty = CallTy;
2914   Args.push_back(Entry);
2915 
2916   // Setup call to __tls_get_addr.
2917   TargetLowering::CallLoweringInfo CLI(DAG);
2918   CLI.setDebugLoc(DL)
2919       .setChain(DAG.getEntryNode())
2920       .setLibCallee(CallingConv::C, CallTy,
2921                     DAG.getExternalSymbol("__tls_get_addr", Ty),
2922                     std::move(Args));
2923 
2924   return LowerCallTo(CLI).first;
2925 }
2926 
2927 SDValue RISCVTargetLowering::lowerGlobalTLSAddress(SDValue Op,
2928                                                    SelectionDAG &DAG) const {
2929   SDLoc DL(Op);
2930   EVT Ty = Op.getValueType();
2931   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
2932   int64_t Offset = N->getOffset();
2933   MVT XLenVT = Subtarget.getXLenVT();
2934 
2935   TLSModel::Model Model = getTargetMachine().getTLSModel(N->getGlobal());
2936 
2937   if (DAG.getMachineFunction().getFunction().getCallingConv() ==
2938       CallingConv::GHC)
2939     report_fatal_error("In GHC calling convention TLS is not supported");
2940 
2941   SDValue Addr;
2942   switch (Model) {
2943   case TLSModel::LocalExec:
2944     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/false);
2945     break;
2946   case TLSModel::InitialExec:
2947     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/true);
2948     break;
2949   case TLSModel::LocalDynamic:
2950   case TLSModel::GeneralDynamic:
2951     Addr = getDynamicTLSAddr(N, DAG);
2952     break;
2953   }
2954 
2955   // In order to maximise the opportunity for common subexpression elimination,
2956   // emit a separate ADD node for the global address offset instead of folding
2957   // it in the global address node. Later peephole optimisations may choose to
2958   // fold it back in when profitable.
2959   if (Offset != 0)
2960     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
2961                        DAG.getConstant(Offset, DL, XLenVT));
2962   return Addr;
2963 }
2964 
2965 SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2966   SDValue CondV = Op.getOperand(0);
2967   SDValue TrueV = Op.getOperand(1);
2968   SDValue FalseV = Op.getOperand(2);
2969   SDLoc DL(Op);
2970   MVT VT = Op.getSimpleValueType();
2971   MVT XLenVT = Subtarget.getXLenVT();
2972 
2973   // Lower vector SELECTs to VSELECTs by splatting the condition.
2974   if (VT.isVector()) {
2975     MVT SplatCondVT = VT.changeVectorElementType(MVT::i1);
2976     SDValue CondSplat = VT.isScalableVector()
2977                             ? DAG.getSplatVector(SplatCondVT, DL, CondV)
2978                             : DAG.getSplatBuildVector(SplatCondVT, DL, CondV);
2979     return DAG.getNode(ISD::VSELECT, DL, VT, CondSplat, TrueV, FalseV);
2980   }
2981 
2982   // If the result type is XLenVT and CondV is the output of a SETCC node
2983   // which also operated on XLenVT inputs, then merge the SETCC node into the
2984   // lowered RISCVISD::SELECT_CC to take advantage of the integer
2985   // compare+branch instructions. i.e.:
2986   // (select (setcc lhs, rhs, cc), truev, falsev)
2987   // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
2988   if (VT == XLenVT && CondV.getOpcode() == ISD::SETCC &&
2989       CondV.getOperand(0).getSimpleValueType() == XLenVT) {
2990     SDValue LHS = CondV.getOperand(0);
2991     SDValue RHS = CondV.getOperand(1);
2992     const auto *CC = cast<CondCodeSDNode>(CondV.getOperand(2));
2993     ISD::CondCode CCVal = CC->get();
2994 
2995     // Special case for a select of 2 constants that have a diffence of 1.
2996     // Normally this is done by DAGCombine, but if the select is introduced by
2997     // type legalization or op legalization, we miss it. Restricting to SETLT
2998     // case for now because that is what signed saturating add/sub need.
2999     // FIXME: We don't need the condition to be SETLT or even a SETCC,
3000     // but we would probably want to swap the true/false values if the condition
3001     // is SETGE/SETLE to avoid an XORI.
3002     if (isa<ConstantSDNode>(TrueV) && isa<ConstantSDNode>(FalseV) &&
3003         CCVal == ISD::SETLT) {
3004       const APInt &TrueVal = cast<ConstantSDNode>(TrueV)->getAPIntValue();
3005       const APInt &FalseVal = cast<ConstantSDNode>(FalseV)->getAPIntValue();
3006       if (TrueVal - 1 == FalseVal)
3007         return DAG.getNode(ISD::ADD, DL, Op.getValueType(), CondV, FalseV);
3008       if (TrueVal + 1 == FalseVal)
3009         return DAG.getNode(ISD::SUB, DL, Op.getValueType(), FalseV, CondV);
3010     }
3011 
3012     translateSetCCForBranch(DL, LHS, RHS, CCVal, DAG);
3013 
3014     SDValue TargetCC = DAG.getCondCode(CCVal);
3015     SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
3016     return DAG.getNode(RISCVISD::SELECT_CC, DL, Op.getValueType(), Ops);
3017   }
3018 
3019   // Otherwise:
3020   // (select condv, truev, falsev)
3021   // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
3022   SDValue Zero = DAG.getConstant(0, DL, XLenVT);
3023   SDValue SetNE = DAG.getCondCode(ISD::SETNE);
3024 
3025   SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
3026 
3027   return DAG.getNode(RISCVISD::SELECT_CC, DL, Op.getValueType(), Ops);
3028 }
3029 
3030 SDValue RISCVTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
3031   SDValue CondV = Op.getOperand(1);
3032   SDLoc DL(Op);
3033   MVT XLenVT = Subtarget.getXLenVT();
3034 
3035   if (CondV.getOpcode() == ISD::SETCC &&
3036       CondV.getOperand(0).getValueType() == XLenVT) {
3037     SDValue LHS = CondV.getOperand(0);
3038     SDValue RHS = CondV.getOperand(1);
3039     ISD::CondCode CCVal = cast<CondCodeSDNode>(CondV.getOperand(2))->get();
3040 
3041     translateSetCCForBranch(DL, LHS, RHS, CCVal, DAG);
3042 
3043     SDValue TargetCC = DAG.getCondCode(CCVal);
3044     return DAG.getNode(RISCVISD::BR_CC, DL, Op.getValueType(), Op.getOperand(0),
3045                        LHS, RHS, TargetCC, Op.getOperand(2));
3046   }
3047 
3048   return DAG.getNode(RISCVISD::BR_CC, DL, Op.getValueType(), Op.getOperand(0),
3049                      CondV, DAG.getConstant(0, DL, XLenVT),
3050                      DAG.getCondCode(ISD::SETNE), Op.getOperand(2));
3051 }
3052 
3053 SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
3054   MachineFunction &MF = DAG.getMachineFunction();
3055   RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
3056 
3057   SDLoc DL(Op);
3058   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
3059                                  getPointerTy(MF.getDataLayout()));
3060 
3061   // vastart just stores the address of the VarArgsFrameIndex slot into the
3062   // memory location argument.
3063   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
3064   return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
3065                       MachinePointerInfo(SV));
3066 }
3067 
3068 SDValue RISCVTargetLowering::lowerFRAMEADDR(SDValue Op,
3069                                             SelectionDAG &DAG) const {
3070   const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
3071   MachineFunction &MF = DAG.getMachineFunction();
3072   MachineFrameInfo &MFI = MF.getFrameInfo();
3073   MFI.setFrameAddressIsTaken(true);
3074   Register FrameReg = RI.getFrameRegister(MF);
3075   int XLenInBytes = Subtarget.getXLen() / 8;
3076 
3077   EVT VT = Op.getValueType();
3078   SDLoc DL(Op);
3079   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
3080   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3081   while (Depth--) {
3082     int Offset = -(XLenInBytes * 2);
3083     SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
3084                               DAG.getIntPtrConstant(Offset, DL));
3085     FrameAddr =
3086         DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
3087   }
3088   return FrameAddr;
3089 }
3090 
3091 SDValue RISCVTargetLowering::lowerRETURNADDR(SDValue Op,
3092                                              SelectionDAG &DAG) const {
3093   const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
3094   MachineFunction &MF = DAG.getMachineFunction();
3095   MachineFrameInfo &MFI = MF.getFrameInfo();
3096   MFI.setReturnAddressIsTaken(true);
3097   MVT XLenVT = Subtarget.getXLenVT();
3098   int XLenInBytes = Subtarget.getXLen() / 8;
3099 
3100   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
3101     return SDValue();
3102 
3103   EVT VT = Op.getValueType();
3104   SDLoc DL(Op);
3105   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3106   if (Depth) {
3107     int Off = -XLenInBytes;
3108     SDValue FrameAddr = lowerFRAMEADDR(Op, DAG);
3109     SDValue Offset = DAG.getConstant(Off, DL, VT);
3110     return DAG.getLoad(VT, DL, DAG.getEntryNode(),
3111                        DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
3112                        MachinePointerInfo());
3113   }
3114 
3115   // Return the value of the return address register, marking it an implicit
3116   // live-in.
3117   Register Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
3118   return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
3119 }
3120 
3121 SDValue RISCVTargetLowering::lowerShiftLeftParts(SDValue Op,
3122                                                  SelectionDAG &DAG) const {
3123   SDLoc DL(Op);
3124   SDValue Lo = Op.getOperand(0);
3125   SDValue Hi = Op.getOperand(1);
3126   SDValue Shamt = Op.getOperand(2);
3127   EVT VT = Lo.getValueType();
3128 
3129   // if Shamt-XLEN < 0: // Shamt < XLEN
3130   //   Lo = Lo << Shamt
3131   //   Hi = (Hi << Shamt) | ((Lo >>u 1) >>u (XLEN-1 - Shamt))
3132   // else:
3133   //   Lo = 0
3134   //   Hi = Lo << (Shamt-XLEN)
3135 
3136   SDValue Zero = DAG.getConstant(0, DL, VT);
3137   SDValue One = DAG.getConstant(1, DL, VT);
3138   SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
3139   SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
3140   SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
3141   SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
3142 
3143   SDValue LoTrue = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
3144   SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo, One);
3145   SDValue ShiftRightLo =
3146       DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, XLenMinus1Shamt);
3147   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
3148   SDValue HiTrue = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
3149   SDValue HiFalse = DAG.getNode(ISD::SHL, DL, VT, Lo, ShamtMinusXLen);
3150 
3151   SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
3152 
3153   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, Zero);
3154   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
3155 
3156   SDValue Parts[2] = {Lo, Hi};
3157   return DAG.getMergeValues(Parts, DL);
3158 }
3159 
3160 SDValue RISCVTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
3161                                                   bool IsSRA) const {
3162   SDLoc DL(Op);
3163   SDValue Lo = Op.getOperand(0);
3164   SDValue Hi = Op.getOperand(1);
3165   SDValue Shamt = Op.getOperand(2);
3166   EVT VT = Lo.getValueType();
3167 
3168   // SRA expansion:
3169   //   if Shamt-XLEN < 0: // Shamt < XLEN
3170   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
3171   //     Hi = Hi >>s Shamt
3172   //   else:
3173   //     Lo = Hi >>s (Shamt-XLEN);
3174   //     Hi = Hi >>s (XLEN-1)
3175   //
3176   // SRL expansion:
3177   //   if Shamt-XLEN < 0: // Shamt < XLEN
3178   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
3179   //     Hi = Hi >>u Shamt
3180   //   else:
3181   //     Lo = Hi >>u (Shamt-XLEN);
3182   //     Hi = 0;
3183 
3184   unsigned ShiftRightOp = IsSRA ? ISD::SRA : ISD::SRL;
3185 
3186   SDValue Zero = DAG.getConstant(0, DL, VT);
3187   SDValue One = DAG.getConstant(1, DL, VT);
3188   SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
3189   SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
3190   SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
3191   SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
3192 
3193   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
3194   SDValue ShiftLeftHi1 = DAG.getNode(ISD::SHL, DL, VT, Hi, One);
3195   SDValue ShiftLeftHi =
3196       DAG.getNode(ISD::SHL, DL, VT, ShiftLeftHi1, XLenMinus1Shamt);
3197   SDValue LoTrue = DAG.getNode(ISD::OR, DL, VT, ShiftRightLo, ShiftLeftHi);
3198   SDValue HiTrue = DAG.getNode(ShiftRightOp, DL, VT, Hi, Shamt);
3199   SDValue LoFalse = DAG.getNode(ShiftRightOp, DL, VT, Hi, ShamtMinusXLen);
3200   SDValue HiFalse =
3201       IsSRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, XLenMinus1) : Zero;
3202 
3203   SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
3204 
3205   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, LoFalse);
3206   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
3207 
3208   SDValue Parts[2] = {Lo, Hi};
3209   return DAG.getMergeValues(Parts, DL);
3210 }
3211 
3212 // Lower splats of i1 types to SETCC. For each mask vector type, we have a
3213 // legal equivalently-sized i8 type, so we can use that as a go-between.
3214 SDValue RISCVTargetLowering::lowerVectorMaskSplat(SDValue Op,
3215                                                   SelectionDAG &DAG) const {
3216   SDLoc DL(Op);
3217   MVT VT = Op.getSimpleValueType();
3218   SDValue SplatVal = Op.getOperand(0);
3219   // All-zeros or all-ones splats are handled specially.
3220   if (ISD::isConstantSplatVectorAllOnes(Op.getNode())) {
3221     SDValue VL = getDefaultScalableVLOps(VT, DL, DAG, Subtarget).second;
3222     return DAG.getNode(RISCVISD::VMSET_VL, DL, VT, VL);
3223   }
3224   if (ISD::isConstantSplatVectorAllZeros(Op.getNode())) {
3225     SDValue VL = getDefaultScalableVLOps(VT, DL, DAG, Subtarget).second;
3226     return DAG.getNode(RISCVISD::VMCLR_VL, DL, VT, VL);
3227   }
3228   MVT XLenVT = Subtarget.getXLenVT();
3229   assert(SplatVal.getValueType() == XLenVT &&
3230          "Unexpected type for i1 splat value");
3231   MVT InterVT = VT.changeVectorElementType(MVT::i8);
3232   SplatVal = DAG.getNode(ISD::AND, DL, XLenVT, SplatVal,
3233                          DAG.getConstant(1, DL, XLenVT));
3234   SDValue LHS = DAG.getSplatVector(InterVT, DL, SplatVal);
3235   SDValue Zero = DAG.getConstant(0, DL, InterVT);
3236   return DAG.getSetCC(DL, VT, LHS, Zero, ISD::SETNE);
3237 }
3238 
3239 // Custom-lower a SPLAT_VECTOR_PARTS where XLEN<SEW, as the SEW element type is
3240 // illegal (currently only vXi64 RV32).
3241 // FIXME: We could also catch non-constant sign-extended i32 values and lower
3242 // them to SPLAT_VECTOR_I64
3243 SDValue RISCVTargetLowering::lowerSPLAT_VECTOR_PARTS(SDValue Op,
3244                                                      SelectionDAG &DAG) const {
3245   SDLoc DL(Op);
3246   MVT VecVT = Op.getSimpleValueType();
3247   assert(!Subtarget.is64Bit() && VecVT.getVectorElementType() == MVT::i64 &&
3248          "Unexpected SPLAT_VECTOR_PARTS lowering");
3249 
3250   assert(Op.getNumOperands() == 2 && "Unexpected number of operands!");
3251   SDValue Lo = Op.getOperand(0);
3252   SDValue Hi = Op.getOperand(1);
3253 
3254   if (VecVT.isFixedLengthVector()) {
3255     MVT ContainerVT = getContainerForFixedLengthVector(VecVT);
3256     SDLoc DL(Op);
3257     SDValue Mask, VL;
3258     std::tie(Mask, VL) =
3259         getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget);
3260 
3261     SDValue Res = splatPartsI64WithVL(DL, ContainerVT, Lo, Hi, VL, DAG);
3262     return convertFromScalableVector(VecVT, Res, DAG, Subtarget);
3263   }
3264 
3265   if (isa<ConstantSDNode>(Lo) && isa<ConstantSDNode>(Hi)) {
3266     int32_t LoC = cast<ConstantSDNode>(Lo)->getSExtValue();
3267     int32_t HiC = cast<ConstantSDNode>(Hi)->getSExtValue();
3268     // If Hi constant is all the same sign bit as Lo, lower this as a custom
3269     // node in order to try and match RVV vector/scalar instructions.
3270     if ((LoC >> 31) == HiC)
3271       return DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VecVT, Lo);
3272   }
3273 
3274   // Detect cases where Hi is (SRA Lo, 31) which means Hi is Lo sign extended.
3275   if (Hi.getOpcode() == ISD::SRA && Hi.getOperand(0) == Lo &&
3276       isa<ConstantSDNode>(Hi.getOperand(1)) &&
3277       Hi.getConstantOperandVal(1) == 31)
3278     return DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VecVT, Lo);
3279 
3280   // Fall back to use a stack store and stride x0 vector load. Use X0 as VL.
3281   return DAG.getNode(RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL, DL, VecVT, Lo, Hi,
3282                      DAG.getRegister(RISCV::X0, MVT::i64));
3283 }
3284 
3285 // Custom-lower extensions from mask vectors by using a vselect either with 1
3286 // for zero/any-extension or -1 for sign-extension:
3287 //   (vXiN = (s|z)ext vXi1:vmask) -> (vXiN = vselect vmask, (-1 or 1), 0)
3288 // Note that any-extension is lowered identically to zero-extension.
3289 SDValue RISCVTargetLowering::lowerVectorMaskExt(SDValue Op, SelectionDAG &DAG,
3290                                                 int64_t ExtTrueVal) const {
3291   SDLoc DL(Op);
3292   MVT VecVT = Op.getSimpleValueType();
3293   SDValue Src = Op.getOperand(0);
3294   // Only custom-lower extensions from mask types
3295   assert(Src.getValueType().isVector() &&
3296          Src.getValueType().getVectorElementType() == MVT::i1);
3297 
3298   MVT XLenVT = Subtarget.getXLenVT();
3299   SDValue SplatZero = DAG.getConstant(0, DL, XLenVT);
3300   SDValue SplatTrueVal = DAG.getConstant(ExtTrueVal, DL, XLenVT);
3301 
3302   if (VecVT.isScalableVector()) {
3303     // Be careful not to introduce illegal scalar types at this stage, and be
3304     // careful also about splatting constants as on RV32, vXi64 SPLAT_VECTOR is
3305     // illegal and must be expanded. Since we know that the constants are
3306     // sign-extended 32-bit values, we use SPLAT_VECTOR_I64 directly.
3307     bool IsRV32E64 =
3308         !Subtarget.is64Bit() && VecVT.getVectorElementType() == MVT::i64;
3309 
3310     if (!IsRV32E64) {
3311       SplatZero = DAG.getSplatVector(VecVT, DL, SplatZero);
3312       SplatTrueVal = DAG.getSplatVector(VecVT, DL, SplatTrueVal);
3313     } else {
3314       SplatZero = DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VecVT, SplatZero);
3315       SplatTrueVal =
3316           DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VecVT, SplatTrueVal);
3317     }
3318 
3319     return DAG.getNode(ISD::VSELECT, DL, VecVT, Src, SplatTrueVal, SplatZero);
3320   }
3321 
3322   MVT ContainerVT = getContainerForFixedLengthVector(VecVT);
3323   MVT I1ContainerVT =
3324       MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
3325 
3326   SDValue CC = convertToScalableVector(I1ContainerVT, Src, DAG, Subtarget);
3327 
3328   SDValue Mask, VL;
3329   std::tie(Mask, VL) = getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget);
3330 
3331   SplatZero = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, ContainerVT, SplatZero, VL);
3332   SplatTrueVal =
3333       DAG.getNode(RISCVISD::VMV_V_X_VL, DL, ContainerVT, SplatTrueVal, VL);
3334   SDValue Select = DAG.getNode(RISCVISD::VSELECT_VL, DL, ContainerVT, CC,
3335                                SplatTrueVal, SplatZero, VL);
3336 
3337   return convertFromScalableVector(VecVT, Select, DAG, Subtarget);
3338 }
3339 
3340 SDValue RISCVTargetLowering::lowerFixedLengthVectorExtendToRVV(
3341     SDValue Op, SelectionDAG &DAG, unsigned ExtendOpc) const {
3342   MVT ExtVT = Op.getSimpleValueType();
3343   // Only custom-lower extensions from fixed-length vector types.
3344   if (!ExtVT.isFixedLengthVector())
3345     return Op;
3346   MVT VT = Op.getOperand(0).getSimpleValueType();
3347   // Grab the canonical container type for the extended type. Infer the smaller
3348   // type from that to ensure the same number of vector elements, as we know
3349   // the LMUL will be sufficient to hold the smaller type.
3350   MVT ContainerExtVT = getContainerForFixedLengthVector(ExtVT);
3351   // Get the extended container type manually to ensure the same number of
3352   // vector elements between source and dest.
3353   MVT ContainerVT = MVT::getVectorVT(VT.getVectorElementType(),
3354                                      ContainerExtVT.getVectorElementCount());
3355 
3356   SDValue Op1 =
3357       convertToScalableVector(ContainerVT, Op.getOperand(0), DAG, Subtarget);
3358 
3359   SDLoc DL(Op);
3360   SDValue Mask, VL;
3361   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
3362 
3363   SDValue Ext = DAG.getNode(ExtendOpc, DL, ContainerExtVT, Op1, Mask, VL);
3364 
3365   return convertFromScalableVector(ExtVT, Ext, DAG, Subtarget);
3366 }
3367 
3368 // Custom-lower truncations from vectors to mask vectors by using a mask and a
3369 // setcc operation:
3370 //   (vXi1 = trunc vXiN vec) -> (vXi1 = setcc (and vec, 1), 0, ne)
3371 SDValue RISCVTargetLowering::lowerVectorMaskTrunc(SDValue Op,
3372                                                   SelectionDAG &DAG) const {
3373   SDLoc DL(Op);
3374   EVT MaskVT = Op.getValueType();
3375   // Only expect to custom-lower truncations to mask types
3376   assert(MaskVT.isVector() && MaskVT.getVectorElementType() == MVT::i1 &&
3377          "Unexpected type for vector mask lowering");
3378   SDValue Src = Op.getOperand(0);
3379   MVT VecVT = Src.getSimpleValueType();
3380 
3381   // If this is a fixed vector, we need to convert it to a scalable vector.
3382   MVT ContainerVT = VecVT;
3383   if (VecVT.isFixedLengthVector()) {
3384     ContainerVT = getContainerForFixedLengthVector(VecVT);
3385     Src = convertToScalableVector(ContainerVT, Src, DAG, Subtarget);
3386   }
3387 
3388   SDValue SplatOne = DAG.getConstant(1, DL, Subtarget.getXLenVT());
3389   SDValue SplatZero = DAG.getConstant(0, DL, Subtarget.getXLenVT());
3390 
3391   SplatOne = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, ContainerVT, SplatOne);
3392   SplatZero = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, ContainerVT, SplatZero);
3393 
3394   if (VecVT.isScalableVector()) {
3395     SDValue Trunc = DAG.getNode(ISD::AND, DL, VecVT, Src, SplatOne);
3396     return DAG.getSetCC(DL, MaskVT, Trunc, SplatZero, ISD::SETNE);
3397   }
3398 
3399   SDValue Mask, VL;
3400   std::tie(Mask, VL) = getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget);
3401 
3402   MVT MaskContainerVT = ContainerVT.changeVectorElementType(MVT::i1);
3403   SDValue Trunc =
3404       DAG.getNode(RISCVISD::AND_VL, DL, ContainerVT, Src, SplatOne, Mask, VL);
3405   Trunc = DAG.getNode(RISCVISD::SETCC_VL, DL, MaskContainerVT, Trunc, SplatZero,
3406                       DAG.getCondCode(ISD::SETNE), Mask, VL);
3407   return convertFromScalableVector(MaskVT, Trunc, DAG, Subtarget);
3408 }
3409 
3410 // Custom-legalize INSERT_VECTOR_ELT so that the value is inserted into the
3411 // first position of a vector, and that vector is slid up to the insert index.
3412 // By limiting the active vector length to index+1 and merging with the
3413 // original vector (with an undisturbed tail policy for elements >= VL), we
3414 // achieve the desired result of leaving all elements untouched except the one
3415 // at VL-1, which is replaced with the desired value.
3416 SDValue RISCVTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
3417                                                     SelectionDAG &DAG) const {
3418   SDLoc DL(Op);
3419   MVT VecVT = Op.getSimpleValueType();
3420   SDValue Vec = Op.getOperand(0);
3421   SDValue Val = Op.getOperand(1);
3422   SDValue Idx = Op.getOperand(2);
3423 
3424   if (VecVT.getVectorElementType() == MVT::i1) {
3425     // FIXME: For now we just promote to an i8 vector and insert into that,
3426     // but this is probably not optimal.
3427     MVT WideVT = MVT::getVectorVT(MVT::i8, VecVT.getVectorElementCount());
3428     Vec = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT, Vec);
3429     Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, WideVT, Vec, Val, Idx);
3430     return DAG.getNode(ISD::TRUNCATE, DL, VecVT, Vec);
3431   }
3432 
3433   MVT ContainerVT = VecVT;
3434   // If the operand is a fixed-length vector, convert to a scalable one.
3435   if (VecVT.isFixedLengthVector()) {
3436     ContainerVT = getContainerForFixedLengthVector(VecVT);
3437     Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
3438   }
3439 
3440   MVT XLenVT = Subtarget.getXLenVT();
3441 
3442   SDValue Zero = DAG.getConstant(0, DL, XLenVT);
3443   bool IsLegalInsert = Subtarget.is64Bit() || Val.getValueType() != MVT::i64;
3444   // Even i64-element vectors on RV32 can be lowered without scalar
3445   // legalization if the most-significant 32 bits of the value are not affected
3446   // by the sign-extension of the lower 32 bits.
3447   // TODO: We could also catch sign extensions of a 32-bit value.
3448   if (!IsLegalInsert && isa<ConstantSDNode>(Val)) {
3449     const auto *CVal = cast<ConstantSDNode>(Val);
3450     if (isInt<32>(CVal->getSExtValue())) {
3451       IsLegalInsert = true;
3452       Val = DAG.getConstant(CVal->getSExtValue(), DL, MVT::i32);
3453     }
3454   }
3455 
3456   SDValue Mask, VL;
3457   std::tie(Mask, VL) = getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget);
3458 
3459   SDValue ValInVec;
3460 
3461   if (IsLegalInsert) {
3462     unsigned Opc =
3463         VecVT.isFloatingPoint() ? RISCVISD::VFMV_S_F_VL : RISCVISD::VMV_S_X_VL;
3464     if (isNullConstant(Idx)) {
3465       Vec = DAG.getNode(Opc, DL, ContainerVT, Vec, Val, VL);
3466       if (!VecVT.isFixedLengthVector())
3467         return Vec;
3468       return convertFromScalableVector(VecVT, Vec, DAG, Subtarget);
3469     }
3470     ValInVec =
3471         DAG.getNode(Opc, DL, ContainerVT, DAG.getUNDEF(ContainerVT), Val, VL);
3472   } else {
3473     // On RV32, i64-element vectors must be specially handled to place the
3474     // value at element 0, by using two vslide1up instructions in sequence on
3475     // the i32 split lo/hi value. Use an equivalently-sized i32 vector for
3476     // this.
3477     SDValue One = DAG.getConstant(1, DL, XLenVT);
3478     SDValue ValLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Val, Zero);
3479     SDValue ValHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Val, One);
3480     MVT I32ContainerVT =
3481         MVT::getVectorVT(MVT::i32, ContainerVT.getVectorElementCount() * 2);
3482     SDValue I32Mask =
3483         getDefaultScalableVLOps(I32ContainerVT, DL, DAG, Subtarget).first;
3484     // Limit the active VL to two.
3485     SDValue InsertI64VL = DAG.getConstant(2, DL, XLenVT);
3486     // Note: We can't pass a UNDEF to the first VSLIDE1UP_VL since an untied
3487     // undef doesn't obey the earlyclobber constraint. Just splat a zero value.
3488     ValInVec = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, I32ContainerVT, Zero,
3489                            InsertI64VL);
3490     // First slide in the hi value, then the lo in underneath it.
3491     ValInVec = DAG.getNode(RISCVISD::VSLIDE1UP_VL, DL, I32ContainerVT, ValInVec,
3492                            ValHi, I32Mask, InsertI64VL);
3493     ValInVec = DAG.getNode(RISCVISD::VSLIDE1UP_VL, DL, I32ContainerVT, ValInVec,
3494                            ValLo, I32Mask, InsertI64VL);
3495     // Bitcast back to the right container type.
3496     ValInVec = DAG.getBitcast(ContainerVT, ValInVec);
3497   }
3498 
3499   // Now that the value is in a vector, slide it into position.
3500   SDValue InsertVL =
3501       DAG.getNode(ISD::ADD, DL, XLenVT, Idx, DAG.getConstant(1, DL, XLenVT));
3502   SDValue Slideup = DAG.getNode(RISCVISD::VSLIDEUP_VL, DL, ContainerVT, Vec,
3503                                 ValInVec, Idx, Mask, InsertVL);
3504   if (!VecVT.isFixedLengthVector())
3505     return Slideup;
3506   return convertFromScalableVector(VecVT, Slideup, DAG, Subtarget);
3507 }
3508 
3509 // Custom-lower EXTRACT_VECTOR_ELT operations to slide the vector down, then
3510 // extract the first element: (extractelt (slidedown vec, idx), 0). For integer
3511 // types this is done using VMV_X_S to allow us to glean information about the
3512 // sign bits of the result.
3513 SDValue RISCVTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
3514                                                      SelectionDAG &DAG) const {
3515   SDLoc DL(Op);
3516   SDValue Idx = Op.getOperand(1);
3517   SDValue Vec = Op.getOperand(0);
3518   EVT EltVT = Op.getValueType();
3519   MVT VecVT = Vec.getSimpleValueType();
3520   MVT XLenVT = Subtarget.getXLenVT();
3521 
3522   if (VecVT.getVectorElementType() == MVT::i1) {
3523     // FIXME: For now we just promote to an i8 vector and extract from that,
3524     // but this is probably not optimal.
3525     MVT WideVT = MVT::getVectorVT(MVT::i8, VecVT.getVectorElementCount());
3526     Vec = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT, Vec);
3527     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vec, Idx);
3528   }
3529 
3530   // If this is a fixed vector, we need to convert it to a scalable vector.
3531   MVT ContainerVT = VecVT;
3532   if (VecVT.isFixedLengthVector()) {
3533     ContainerVT = getContainerForFixedLengthVector(VecVT);
3534     Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
3535   }
3536 
3537   // If the index is 0, the vector is already in the right position.
3538   if (!isNullConstant(Idx)) {
3539     // Use a VL of 1 to avoid processing more elements than we need.
3540     SDValue VL = DAG.getConstant(1, DL, XLenVT);
3541     MVT MaskVT = MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
3542     SDValue Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, MaskVT, VL);
3543     Vec = DAG.getNode(RISCVISD::VSLIDEDOWN_VL, DL, ContainerVT,
3544                       DAG.getUNDEF(ContainerVT), Vec, Idx, Mask, VL);
3545   }
3546 
3547   if (!EltVT.isInteger()) {
3548     // Floating-point extracts are handled in TableGen.
3549     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vec,
3550                        DAG.getConstant(0, DL, XLenVT));
3551   }
3552 
3553   SDValue Elt0 = DAG.getNode(RISCVISD::VMV_X_S, DL, XLenVT, Vec);
3554   return DAG.getNode(ISD::TRUNCATE, DL, EltVT, Elt0);
3555 }
3556 
3557 // Some RVV intrinsics may claim that they want an integer operand to be
3558 // promoted or expanded.
3559 static SDValue lowerVectorIntrinsicSplats(SDValue Op, SelectionDAG &DAG,
3560                                           const RISCVSubtarget &Subtarget) {
3561   assert((Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
3562           Op.getOpcode() == ISD::INTRINSIC_W_CHAIN) &&
3563          "Unexpected opcode");
3564 
3565   if (!Subtarget.hasStdExtV())
3566     return SDValue();
3567 
3568   bool HasChain = Op.getOpcode() == ISD::INTRINSIC_W_CHAIN;
3569   unsigned IntNo = Op.getConstantOperandVal(HasChain ? 1 : 0);
3570   SDLoc DL(Op);
3571 
3572   const RISCVVIntrinsicsTable::RISCVVIntrinsicInfo *II =
3573       RISCVVIntrinsicsTable::getRISCVVIntrinsicInfo(IntNo);
3574   if (!II || !II->SplatOperand)
3575     return SDValue();
3576 
3577   unsigned SplatOp = II->SplatOperand + HasChain;
3578   assert(SplatOp < Op.getNumOperands());
3579 
3580   SmallVector<SDValue, 8> Operands(Op->op_begin(), Op->op_end());
3581   SDValue &ScalarOp = Operands[SplatOp];
3582   MVT OpVT = ScalarOp.getSimpleValueType();
3583   MVT XLenVT = Subtarget.getXLenVT();
3584 
3585   // If this isn't a scalar, or its type is XLenVT we're done.
3586   if (!OpVT.isScalarInteger() || OpVT == XLenVT)
3587     return SDValue();
3588 
3589   // Simplest case is that the operand needs to be promoted to XLenVT.
3590   if (OpVT.bitsLT(XLenVT)) {
3591     // If the operand is a constant, sign extend to increase our chances
3592     // of being able to use a .vi instruction. ANY_EXTEND would become a
3593     // a zero extend and the simm5 check in isel would fail.
3594     // FIXME: Should we ignore the upper bits in isel instead?
3595     unsigned ExtOpc =
3596         isa<ConstantSDNode>(ScalarOp) ? ISD::SIGN_EXTEND : ISD::ANY_EXTEND;
3597     ScalarOp = DAG.getNode(ExtOpc, DL, XLenVT, ScalarOp);
3598     return DAG.getNode(Op->getOpcode(), DL, Op->getVTList(), Operands);
3599   }
3600 
3601   // Use the previous operand to get the vXi64 VT. The result might be a mask
3602   // VT for compares. Using the previous operand assumes that the previous
3603   // operand will never have a smaller element size than a scalar operand and
3604   // that a widening operation never uses SEW=64.
3605   // NOTE: If this fails the below assert, we can probably just find the
3606   // element count from any operand or result and use it to construct the VT.
3607   assert(II->SplatOperand > 1 && "Unexpected splat operand!");
3608   MVT VT = Op.getOperand(SplatOp - 1).getSimpleValueType();
3609 
3610   // The more complex case is when the scalar is larger than XLenVT.
3611   assert(XLenVT == MVT::i32 && OpVT == MVT::i64 &&
3612          VT.getVectorElementType() == MVT::i64 && "Unexpected VTs!");
3613 
3614   // If this is a sign-extended 32-bit constant, we can truncate it and rely
3615   // on the instruction to sign-extend since SEW>XLEN.
3616   if (auto *CVal = dyn_cast<ConstantSDNode>(ScalarOp)) {
3617     if (isInt<32>(CVal->getSExtValue())) {
3618       ScalarOp = DAG.getConstant(CVal->getSExtValue(), DL, MVT::i32);
3619       return DAG.getNode(Op->getOpcode(), DL, Op->getVTList(), Operands);
3620     }
3621   }
3622 
3623   // We need to convert the scalar to a splat vector.
3624   // FIXME: Can we implicitly truncate the scalar if it is known to
3625   // be sign extended?
3626   // VL should be the last operand.
3627   SDValue VL = Op.getOperand(Op.getNumOperands() - 1);
3628   assert(VL.getValueType() == XLenVT);
3629   ScalarOp = splatSplitI64WithVL(DL, VT, ScalarOp, VL, DAG);
3630   return DAG.getNode(Op->getOpcode(), DL, Op->getVTList(), Operands);
3631 }
3632 
3633 SDValue RISCVTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
3634                                                      SelectionDAG &DAG) const {
3635   unsigned IntNo = Op.getConstantOperandVal(0);
3636   SDLoc DL(Op);
3637   MVT XLenVT = Subtarget.getXLenVT();
3638 
3639   switch (IntNo) {
3640   default:
3641     break; // Don't custom lower most intrinsics.
3642   case Intrinsic::thread_pointer: {
3643     EVT PtrVT = getPointerTy(DAG.getDataLayout());
3644     return DAG.getRegister(RISCV::X4, PtrVT);
3645   }
3646   case Intrinsic::riscv_orc_b:
3647     // Lower to the GORCI encoding for orc.b.
3648     return DAG.getNode(RISCVISD::GORC, DL, XLenVT, Op.getOperand(1),
3649                        DAG.getConstant(7, DL, XLenVT));
3650   case Intrinsic::riscv_grev:
3651   case Intrinsic::riscv_gorc: {
3652     unsigned Opc =
3653         IntNo == Intrinsic::riscv_grev ? RISCVISD::GREV : RISCVISD::GORC;
3654     return DAG.getNode(Opc, DL, XLenVT, Op.getOperand(1), Op.getOperand(2));
3655   }
3656   case Intrinsic::riscv_shfl:
3657   case Intrinsic::riscv_unshfl: {
3658     unsigned Opc =
3659         IntNo == Intrinsic::riscv_shfl ? RISCVISD::SHFL : RISCVISD::UNSHFL;
3660     return DAG.getNode(Opc, DL, XLenVT, Op.getOperand(1), Op.getOperand(2));
3661   }
3662   case Intrinsic::riscv_bcompress:
3663   case Intrinsic::riscv_bdecompress: {
3664     unsigned Opc = IntNo == Intrinsic::riscv_bcompress ? RISCVISD::BCOMPRESS
3665                                                        : RISCVISD::BDECOMPRESS;
3666     return DAG.getNode(Opc, DL, XLenVT, Op.getOperand(1), Op.getOperand(2));
3667   }
3668   case Intrinsic::riscv_vmv_x_s:
3669     assert(Op.getValueType() == XLenVT && "Unexpected VT!");
3670     return DAG.getNode(RISCVISD::VMV_X_S, DL, Op.getValueType(),
3671                        Op.getOperand(1));
3672   case Intrinsic::riscv_vmv_v_x:
3673     return lowerScalarSplat(Op.getOperand(1), Op.getOperand(2),
3674                             Op.getSimpleValueType(), DL, DAG, Subtarget);
3675   case Intrinsic::riscv_vfmv_v_f:
3676     return DAG.getNode(RISCVISD::VFMV_V_F_VL, DL, Op.getValueType(),
3677                        Op.getOperand(1), Op.getOperand(2));
3678   case Intrinsic::riscv_vmv_s_x: {
3679     SDValue Scalar = Op.getOperand(2);
3680 
3681     if (Scalar.getValueType().bitsLE(XLenVT)) {
3682       Scalar = DAG.getNode(ISD::ANY_EXTEND, DL, XLenVT, Scalar);
3683       return DAG.getNode(RISCVISD::VMV_S_X_VL, DL, Op.getValueType(),
3684                          Op.getOperand(1), Scalar, Op.getOperand(3));
3685     }
3686 
3687     assert(Scalar.getValueType() == MVT::i64 && "Unexpected scalar VT!");
3688 
3689     // This is an i64 value that lives in two scalar registers. We have to
3690     // insert this in a convoluted way. First we build vXi64 splat containing
3691     // the/ two values that we assemble using some bit math. Next we'll use
3692     // vid.v and vmseq to build a mask with bit 0 set. Then we'll use that mask
3693     // to merge element 0 from our splat into the source vector.
3694     // FIXME: This is probably not the best way to do this, but it is
3695     // consistent with INSERT_VECTOR_ELT lowering so it is a good starting
3696     // point.
3697     //   sw lo, (a0)
3698     //   sw hi, 4(a0)
3699     //   vlse vX, (a0)
3700     //
3701     //   vid.v      vVid
3702     //   vmseq.vx   mMask, vVid, 0
3703     //   vmerge.vvm vDest, vSrc, vVal, mMask
3704     MVT VT = Op.getSimpleValueType();
3705     SDValue Vec = Op.getOperand(1);
3706     SDValue VL = Op.getOperand(3);
3707 
3708     SDValue SplattedVal = splatSplitI64WithVL(DL, VT, Scalar, VL, DAG);
3709     SDValue SplattedIdx = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VT,
3710                                       DAG.getConstant(0, DL, MVT::i32), VL);
3711 
3712     MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorElementCount());
3713     SDValue Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, MaskVT, VL);
3714     SDValue VID = DAG.getNode(RISCVISD::VID_VL, DL, VT, Mask, VL);
3715     SDValue SelectCond =
3716         DAG.getNode(RISCVISD::SETCC_VL, DL, MaskVT, VID, SplattedIdx,
3717                     DAG.getCondCode(ISD::SETEQ), Mask, VL);
3718     return DAG.getNode(RISCVISD::VSELECT_VL, DL, VT, SelectCond, SplattedVal,
3719                        Vec, VL);
3720   }
3721   case Intrinsic::riscv_vslide1up:
3722   case Intrinsic::riscv_vslide1down:
3723   case Intrinsic::riscv_vslide1up_mask:
3724   case Intrinsic::riscv_vslide1down_mask: {
3725     // We need to special case these when the scalar is larger than XLen.
3726     unsigned NumOps = Op.getNumOperands();
3727     bool IsMasked = NumOps == 6;
3728     unsigned OpOffset = IsMasked ? 1 : 0;
3729     SDValue Scalar = Op.getOperand(2 + OpOffset);
3730     if (Scalar.getValueType().bitsLE(XLenVT))
3731       break;
3732 
3733     // Splatting a sign extended constant is fine.
3734     if (auto *CVal = dyn_cast<ConstantSDNode>(Scalar))
3735       if (isInt<32>(CVal->getSExtValue()))
3736         break;
3737 
3738     MVT VT = Op.getSimpleValueType();
3739     assert(VT.getVectorElementType() == MVT::i64 &&
3740            Scalar.getValueType() == MVT::i64 && "Unexpected VTs");
3741 
3742     // Convert the vector source to the equivalent nxvXi32 vector.
3743     MVT I32VT = MVT::getVectorVT(MVT::i32, VT.getVectorElementCount() * 2);
3744     SDValue Vec = DAG.getBitcast(I32VT, Op.getOperand(1 + OpOffset));
3745 
3746     SDValue ScalarLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Scalar,
3747                                    DAG.getConstant(0, DL, XLenVT));
3748     SDValue ScalarHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Scalar,
3749                                    DAG.getConstant(1, DL, XLenVT));
3750 
3751     // Double the VL since we halved SEW.
3752     SDValue VL = Op.getOperand(NumOps - 1);
3753     SDValue I32VL =
3754         DAG.getNode(ISD::SHL, DL, XLenVT, VL, DAG.getConstant(1, DL, XLenVT));
3755 
3756     MVT I32MaskVT = MVT::getVectorVT(MVT::i1, I32VT.getVectorElementCount());
3757     SDValue I32Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, I32MaskVT, VL);
3758 
3759     // Shift the two scalar parts in using SEW=32 slide1up/slide1down
3760     // instructions.
3761     if (IntNo == Intrinsic::riscv_vslide1up ||
3762         IntNo == Intrinsic::riscv_vslide1up_mask) {
3763       Vec = DAG.getNode(RISCVISD::VSLIDE1UP_VL, DL, I32VT, Vec, ScalarHi,
3764                         I32Mask, I32VL);
3765       Vec = DAG.getNode(RISCVISD::VSLIDE1UP_VL, DL, I32VT, Vec, ScalarLo,
3766                         I32Mask, I32VL);
3767     } else {
3768       Vec = DAG.getNode(RISCVISD::VSLIDE1DOWN_VL, DL, I32VT, Vec, ScalarLo,
3769                         I32Mask, I32VL);
3770       Vec = DAG.getNode(RISCVISD::VSLIDE1DOWN_VL, DL, I32VT, Vec, ScalarHi,
3771                         I32Mask, I32VL);
3772     }
3773 
3774     // Convert back to nxvXi64.
3775     Vec = DAG.getBitcast(VT, Vec);
3776 
3777     if (!IsMasked)
3778       return Vec;
3779 
3780     // Apply mask after the operation.
3781     SDValue Mask = Op.getOperand(NumOps - 2);
3782     SDValue MaskedOff = Op.getOperand(1);
3783     return DAG.getNode(RISCVISD::VSELECT_VL, DL, VT, Mask, Vec, MaskedOff, VL);
3784   }
3785   }
3786 
3787   return lowerVectorIntrinsicSplats(Op, DAG, Subtarget);
3788 }
3789 
3790 SDValue RISCVTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
3791                                                     SelectionDAG &DAG) const {
3792   return lowerVectorIntrinsicSplats(Op, DAG, Subtarget);
3793 }
3794 
3795 static MVT getLMUL1VT(MVT VT) {
3796   assert(VT.getVectorElementType().getSizeInBits() <= 64 &&
3797          "Unexpected vector MVT");
3798   return MVT::getScalableVectorVT(
3799       VT.getVectorElementType(),
3800       RISCV::RVVBitsPerBlock / VT.getVectorElementType().getSizeInBits());
3801 }
3802 
3803 static unsigned getRVVReductionOp(unsigned ISDOpcode) {
3804   switch (ISDOpcode) {
3805   default:
3806     llvm_unreachable("Unhandled reduction");
3807   case ISD::VECREDUCE_ADD:
3808     return RISCVISD::VECREDUCE_ADD_VL;
3809   case ISD::VECREDUCE_UMAX:
3810     return RISCVISD::VECREDUCE_UMAX_VL;
3811   case ISD::VECREDUCE_SMAX:
3812     return RISCVISD::VECREDUCE_SMAX_VL;
3813   case ISD::VECREDUCE_UMIN:
3814     return RISCVISD::VECREDUCE_UMIN_VL;
3815   case ISD::VECREDUCE_SMIN:
3816     return RISCVISD::VECREDUCE_SMIN_VL;
3817   case ISD::VECREDUCE_AND:
3818     return RISCVISD::VECREDUCE_AND_VL;
3819   case ISD::VECREDUCE_OR:
3820     return RISCVISD::VECREDUCE_OR_VL;
3821   case ISD::VECREDUCE_XOR:
3822     return RISCVISD::VECREDUCE_XOR_VL;
3823   }
3824 }
3825 
3826 SDValue RISCVTargetLowering::lowerVectorMaskVECREDUCE(SDValue Op,
3827                                                       SelectionDAG &DAG) const {
3828   SDLoc DL(Op);
3829   SDValue Vec = Op.getOperand(0);
3830   MVT VecVT = Vec.getSimpleValueType();
3831   assert((Op.getOpcode() == ISD::VECREDUCE_AND ||
3832           Op.getOpcode() == ISD::VECREDUCE_OR ||
3833           Op.getOpcode() == ISD::VECREDUCE_XOR) &&
3834          "Unexpected reduction lowering");
3835 
3836   MVT XLenVT = Subtarget.getXLenVT();
3837   assert(Op.getValueType() == XLenVT &&
3838          "Expected reduction output to be legalized to XLenVT");
3839 
3840   MVT ContainerVT = VecVT;
3841   if (VecVT.isFixedLengthVector()) {
3842     ContainerVT = getContainerForFixedLengthVector(VecVT);
3843     Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
3844   }
3845 
3846   SDValue Mask, VL;
3847   std::tie(Mask, VL) = getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget);
3848   SDValue Zero = DAG.getConstant(0, DL, XLenVT);
3849 
3850   switch (Op.getOpcode()) {
3851   default:
3852     llvm_unreachable("Unhandled reduction");
3853   case ISD::VECREDUCE_AND:
3854     // vpopc ~x == 0
3855     Vec = DAG.getNode(RISCVISD::VMXOR_VL, DL, ContainerVT, Vec, Mask, VL);
3856     Vec = DAG.getNode(RISCVISD::VPOPC_VL, DL, XLenVT, Vec, Mask, VL);
3857     return DAG.getSetCC(DL, XLenVT, Vec, Zero, ISD::SETEQ);
3858   case ISD::VECREDUCE_OR:
3859     // vpopc x != 0
3860     Vec = DAG.getNode(RISCVISD::VPOPC_VL, DL, XLenVT, Vec, Mask, VL);
3861     return DAG.getSetCC(DL, XLenVT, Vec, Zero, ISD::SETNE);
3862   case ISD::VECREDUCE_XOR: {
3863     // ((vpopc x) & 1) != 0
3864     SDValue One = DAG.getConstant(1, DL, XLenVT);
3865     Vec = DAG.getNode(RISCVISD::VPOPC_VL, DL, XLenVT, Vec, Mask, VL);
3866     Vec = DAG.getNode(ISD::AND, DL, XLenVT, Vec, One);
3867     return DAG.getSetCC(DL, XLenVT, Vec, Zero, ISD::SETNE);
3868   }
3869   }
3870 }
3871 
3872 SDValue RISCVTargetLowering::lowerVECREDUCE(SDValue Op,
3873                                             SelectionDAG &DAG) const {
3874   SDLoc DL(Op);
3875   SDValue Vec = Op.getOperand(0);
3876   EVT VecEVT = Vec.getValueType();
3877 
3878   unsigned BaseOpc = ISD::getVecReduceBaseOpcode(Op.getOpcode());
3879 
3880   // Due to ordering in legalize types we may have a vector type that needs to
3881   // be split. Do that manually so we can get down to a legal type.
3882   while (getTypeAction(*DAG.getContext(), VecEVT) ==
3883          TargetLowering::TypeSplitVector) {
3884     SDValue Lo, Hi;
3885     std::tie(Lo, Hi) = DAG.SplitVector(Vec, DL);
3886     VecEVT = Lo.getValueType();
3887     Vec = DAG.getNode(BaseOpc, DL, VecEVT, Lo, Hi);
3888   }
3889 
3890   // TODO: The type may need to be widened rather than split. Or widened before
3891   // it can be split.
3892   if (!isTypeLegal(VecEVT))
3893     return SDValue();
3894 
3895   MVT VecVT = VecEVT.getSimpleVT();
3896   MVT VecEltVT = VecVT.getVectorElementType();
3897   unsigned RVVOpcode = getRVVReductionOp(Op.getOpcode());
3898 
3899   MVT ContainerVT = VecVT;
3900   if (VecVT.isFixedLengthVector()) {
3901     ContainerVT = getContainerForFixedLengthVector(VecVT);
3902     Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
3903   }
3904 
3905   MVT M1VT = getLMUL1VT(ContainerVT);
3906 
3907   SDValue Mask, VL;
3908   std::tie(Mask, VL) = getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget);
3909 
3910   // FIXME: This is a VLMAX splat which might be too large and can prevent
3911   // vsetvli removal.
3912   SDValue NeutralElem =
3913       DAG.getNeutralElement(BaseOpc, DL, VecEltVT, SDNodeFlags());
3914   SDValue IdentitySplat = DAG.getSplatVector(M1VT, DL, NeutralElem);
3915   SDValue Reduction =
3916       DAG.getNode(RVVOpcode, DL, M1VT, Vec, IdentitySplat, Mask, VL);
3917   SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VecEltVT, Reduction,
3918                              DAG.getConstant(0, DL, Subtarget.getXLenVT()));
3919   return DAG.getSExtOrTrunc(Elt0, DL, Op.getValueType());
3920 }
3921 
3922 // Given a reduction op, this function returns the matching reduction opcode,
3923 // the vector SDValue and the scalar SDValue required to lower this to a
3924 // RISCVISD node.
3925 static std::tuple<unsigned, SDValue, SDValue>
3926 getRVVFPReductionOpAndOperands(SDValue Op, SelectionDAG &DAG, EVT EltVT) {
3927   SDLoc DL(Op);
3928   auto Flags = Op->getFlags();
3929   unsigned Opcode = Op.getOpcode();
3930   unsigned BaseOpcode = ISD::getVecReduceBaseOpcode(Opcode);
3931   switch (Opcode) {
3932   default:
3933     llvm_unreachable("Unhandled reduction");
3934   case ISD::VECREDUCE_FADD:
3935     return std::make_tuple(RISCVISD::VECREDUCE_FADD_VL, Op.getOperand(0),
3936                            DAG.getNeutralElement(BaseOpcode, DL, EltVT, Flags));
3937   case ISD::VECREDUCE_SEQ_FADD:
3938     return std::make_tuple(RISCVISD::VECREDUCE_SEQ_FADD_VL, Op.getOperand(1),
3939                            Op.getOperand(0));
3940   case ISD::VECREDUCE_FMIN:
3941     return std::make_tuple(RISCVISD::VECREDUCE_FMIN_VL, Op.getOperand(0),
3942                            DAG.getNeutralElement(BaseOpcode, DL, EltVT, Flags));
3943   case ISD::VECREDUCE_FMAX:
3944     return std::make_tuple(RISCVISD::VECREDUCE_FMAX_VL, Op.getOperand(0),
3945                            DAG.getNeutralElement(BaseOpcode, DL, EltVT, Flags));
3946   }
3947 }
3948 
3949 SDValue RISCVTargetLowering::lowerFPVECREDUCE(SDValue Op,
3950                                               SelectionDAG &DAG) const {
3951   SDLoc DL(Op);
3952   MVT VecEltVT = Op.getSimpleValueType();
3953 
3954   unsigned RVVOpcode;
3955   SDValue VectorVal, ScalarVal;
3956   std::tie(RVVOpcode, VectorVal, ScalarVal) =
3957       getRVVFPReductionOpAndOperands(Op, DAG, VecEltVT);
3958   MVT VecVT = VectorVal.getSimpleValueType();
3959 
3960   MVT ContainerVT = VecVT;
3961   if (VecVT.isFixedLengthVector()) {
3962     ContainerVT = getContainerForFixedLengthVector(VecVT);
3963     VectorVal = convertToScalableVector(ContainerVT, VectorVal, DAG, Subtarget);
3964   }
3965 
3966   MVT M1VT = getLMUL1VT(VectorVal.getSimpleValueType());
3967 
3968   SDValue Mask, VL;
3969   std::tie(Mask, VL) = getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget);
3970 
3971   // FIXME: This is a VLMAX splat which might be too large and can prevent
3972   // vsetvli removal.
3973   SDValue ScalarSplat = DAG.getSplatVector(M1VT, DL, ScalarVal);
3974   SDValue Reduction =
3975       DAG.getNode(RVVOpcode, DL, M1VT, VectorVal, ScalarSplat, Mask, VL);
3976   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VecEltVT, Reduction,
3977                      DAG.getConstant(0, DL, Subtarget.getXLenVT()));
3978 }
3979 
3980 SDValue RISCVTargetLowering::lowerINSERT_SUBVECTOR(SDValue Op,
3981                                                    SelectionDAG &DAG) const {
3982   SDValue Vec = Op.getOperand(0);
3983   SDValue SubVec = Op.getOperand(1);
3984   MVT VecVT = Vec.getSimpleValueType();
3985   MVT SubVecVT = SubVec.getSimpleValueType();
3986 
3987   SDLoc DL(Op);
3988   MVT XLenVT = Subtarget.getXLenVT();
3989   unsigned OrigIdx = Op.getConstantOperandVal(2);
3990   const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
3991 
3992   // We don't have the ability to slide mask vectors up indexed by their i1
3993   // elements; the smallest we can do is i8. Often we are able to bitcast to
3994   // equivalent i8 vectors. Note that when inserting a fixed-length vector
3995   // into a scalable one, we might not necessarily have enough scalable
3996   // elements to safely divide by 8: nxv1i1 = insert nxv1i1, v4i1 is valid.
3997   if (SubVecVT.getVectorElementType() == MVT::i1 &&
3998       (OrigIdx != 0 || !Vec.isUndef())) {
3999     if (VecVT.getVectorMinNumElements() >= 8 &&
4000         SubVecVT.getVectorMinNumElements() >= 8) {
4001       assert(OrigIdx % 8 == 0 && "Invalid index");
4002       assert(VecVT.getVectorMinNumElements() % 8 == 0 &&
4003              SubVecVT.getVectorMinNumElements() % 8 == 0 &&
4004              "Unexpected mask vector lowering");
4005       OrigIdx /= 8;
4006       SubVecVT =
4007           MVT::getVectorVT(MVT::i8, SubVecVT.getVectorMinNumElements() / 8,
4008                            SubVecVT.isScalableVector());
4009       VecVT = MVT::getVectorVT(MVT::i8, VecVT.getVectorMinNumElements() / 8,
4010                                VecVT.isScalableVector());
4011       Vec = DAG.getBitcast(VecVT, Vec);
4012       SubVec = DAG.getBitcast(SubVecVT, SubVec);
4013     } else {
4014       // We can't slide this mask vector up indexed by its i1 elements.
4015       // This poses a problem when we wish to insert a scalable vector which
4016       // can't be re-expressed as a larger type. Just choose the slow path and
4017       // extend to a larger type, then truncate back down.
4018       MVT ExtVecVT = VecVT.changeVectorElementType(MVT::i8);
4019       MVT ExtSubVecVT = SubVecVT.changeVectorElementType(MVT::i8);
4020       Vec = DAG.getNode(ISD::ZERO_EXTEND, DL, ExtVecVT, Vec);
4021       SubVec = DAG.getNode(ISD::ZERO_EXTEND, DL, ExtSubVecVT, SubVec);
4022       Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, ExtVecVT, Vec, SubVec,
4023                         Op.getOperand(2));
4024       SDValue SplatZero = DAG.getConstant(0, DL, ExtVecVT);
4025       return DAG.getSetCC(DL, VecVT, Vec, SplatZero, ISD::SETNE);
4026     }
4027   }
4028 
4029   // If the subvector vector is a fixed-length type, we cannot use subregister
4030   // manipulation to simplify the codegen; we don't know which register of a
4031   // LMUL group contains the specific subvector as we only know the minimum
4032   // register size. Therefore we must slide the vector group up the full
4033   // amount.
4034   if (SubVecVT.isFixedLengthVector()) {
4035     if (OrigIdx == 0 && Vec.isUndef())
4036       return Op;
4037     MVT ContainerVT = VecVT;
4038     if (VecVT.isFixedLengthVector()) {
4039       ContainerVT = getContainerForFixedLengthVector(VecVT);
4040       Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
4041     }
4042     SubVec = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, ContainerVT,
4043                          DAG.getUNDEF(ContainerVT), SubVec,
4044                          DAG.getConstant(0, DL, XLenVT));
4045     SDValue Mask =
4046         getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget).first;
4047     // Set the vector length to only the number of elements we care about. Note
4048     // that for slideup this includes the offset.
4049     SDValue VL =
4050         DAG.getConstant(OrigIdx + SubVecVT.getVectorNumElements(), DL, XLenVT);
4051     SDValue SlideupAmt = DAG.getConstant(OrigIdx, DL, XLenVT);
4052     SDValue Slideup = DAG.getNode(RISCVISD::VSLIDEUP_VL, DL, ContainerVT, Vec,
4053                                   SubVec, SlideupAmt, Mask, VL);
4054     if (VecVT.isFixedLengthVector())
4055       Slideup = convertFromScalableVector(VecVT, Slideup, DAG, Subtarget);
4056     return DAG.getBitcast(Op.getValueType(), Slideup);
4057   }
4058 
4059   unsigned SubRegIdx, RemIdx;
4060   std::tie(SubRegIdx, RemIdx) =
4061       RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
4062           VecVT, SubVecVT, OrigIdx, TRI);
4063 
4064   RISCVII::VLMUL SubVecLMUL = RISCVTargetLowering::getLMUL(SubVecVT);
4065   bool IsSubVecPartReg = SubVecLMUL == RISCVII::VLMUL::LMUL_F2 ||
4066                          SubVecLMUL == RISCVII::VLMUL::LMUL_F4 ||
4067                          SubVecLMUL == RISCVII::VLMUL::LMUL_F8;
4068 
4069   // 1. If the Idx has been completely eliminated and this subvector's size is
4070   // a vector register or a multiple thereof, or the surrounding elements are
4071   // undef, then this is a subvector insert which naturally aligns to a vector
4072   // register. These can easily be handled using subregister manipulation.
4073   // 2. If the subvector is smaller than a vector register, then the insertion
4074   // must preserve the undisturbed elements of the register. We do this by
4075   // lowering to an EXTRACT_SUBVECTOR grabbing the nearest LMUL=1 vector type
4076   // (which resolves to a subregister copy), performing a VSLIDEUP to place the
4077   // subvector within the vector register, and an INSERT_SUBVECTOR of that
4078   // LMUL=1 type back into the larger vector (resolving to another subregister
4079   // operation). See below for how our VSLIDEUP works. We go via a LMUL=1 type
4080   // to avoid allocating a large register group to hold our subvector.
4081   if (RemIdx == 0 && (!IsSubVecPartReg || Vec.isUndef()))
4082     return Op;
4083 
4084   // VSLIDEUP works by leaving elements 0<i<OFFSET undisturbed, elements
4085   // OFFSET<=i<VL set to the "subvector" and vl<=i<VLMAX set to the tail policy
4086   // (in our case undisturbed). This means we can set up a subvector insertion
4087   // where OFFSET is the insertion offset, and the VL is the OFFSET plus the
4088   // size of the subvector.
4089   MVT InterSubVT = VecVT;
4090   SDValue AlignedExtract = Vec;
4091   unsigned AlignedIdx = OrigIdx - RemIdx;
4092   if (VecVT.bitsGT(getLMUL1VT(VecVT))) {
4093     InterSubVT = getLMUL1VT(VecVT);
4094     // Extract a subvector equal to the nearest full vector register type. This
4095     // should resolve to a EXTRACT_SUBREG instruction.
4096     AlignedExtract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InterSubVT, Vec,
4097                                  DAG.getConstant(AlignedIdx, DL, XLenVT));
4098   }
4099 
4100   SDValue SlideupAmt = DAG.getConstant(RemIdx, DL, XLenVT);
4101   // For scalable vectors this must be further multiplied by vscale.
4102   SlideupAmt = DAG.getNode(ISD::VSCALE, DL, XLenVT, SlideupAmt);
4103 
4104   SDValue Mask, VL;
4105   std::tie(Mask, VL) = getDefaultScalableVLOps(VecVT, DL, DAG, Subtarget);
4106 
4107   // Construct the vector length corresponding to RemIdx + length(SubVecVT).
4108   VL = DAG.getConstant(SubVecVT.getVectorMinNumElements(), DL, XLenVT);
4109   VL = DAG.getNode(ISD::VSCALE, DL, XLenVT, VL);
4110   VL = DAG.getNode(ISD::ADD, DL, XLenVT, SlideupAmt, VL);
4111 
4112   SubVec = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, InterSubVT,
4113                        DAG.getUNDEF(InterSubVT), SubVec,
4114                        DAG.getConstant(0, DL, XLenVT));
4115 
4116   SDValue Slideup = DAG.getNode(RISCVISD::VSLIDEUP_VL, DL, InterSubVT,
4117                                 AlignedExtract, SubVec, SlideupAmt, Mask, VL);
4118 
4119   // If required, insert this subvector back into the correct vector register.
4120   // This should resolve to an INSERT_SUBREG instruction.
4121   if (VecVT.bitsGT(InterSubVT))
4122     Slideup = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VecVT, Vec, Slideup,
4123                           DAG.getConstant(AlignedIdx, DL, XLenVT));
4124 
4125   // We might have bitcast from a mask type: cast back to the original type if
4126   // required.
4127   return DAG.getBitcast(Op.getSimpleValueType(), Slideup);
4128 }
4129 
4130 SDValue RISCVTargetLowering::lowerEXTRACT_SUBVECTOR(SDValue Op,
4131                                                     SelectionDAG &DAG) const {
4132   SDValue Vec = Op.getOperand(0);
4133   MVT SubVecVT = Op.getSimpleValueType();
4134   MVT VecVT = Vec.getSimpleValueType();
4135 
4136   SDLoc DL(Op);
4137   MVT XLenVT = Subtarget.getXLenVT();
4138   unsigned OrigIdx = Op.getConstantOperandVal(1);
4139   const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
4140 
4141   // We don't have the ability to slide mask vectors down indexed by their i1
4142   // elements; the smallest we can do is i8. Often we are able to bitcast to
4143   // equivalent i8 vectors. Note that when extracting a fixed-length vector
4144   // from a scalable one, we might not necessarily have enough scalable
4145   // elements to safely divide by 8: v8i1 = extract nxv1i1 is valid.
4146   if (SubVecVT.getVectorElementType() == MVT::i1 && OrigIdx != 0) {
4147     if (VecVT.getVectorMinNumElements() >= 8 &&
4148         SubVecVT.getVectorMinNumElements() >= 8) {
4149       assert(OrigIdx % 8 == 0 && "Invalid index");
4150       assert(VecVT.getVectorMinNumElements() % 8 == 0 &&
4151              SubVecVT.getVectorMinNumElements() % 8 == 0 &&
4152              "Unexpected mask vector lowering");
4153       OrigIdx /= 8;
4154       SubVecVT =
4155           MVT::getVectorVT(MVT::i8, SubVecVT.getVectorMinNumElements() / 8,
4156                            SubVecVT.isScalableVector());
4157       VecVT = MVT::getVectorVT(MVT::i8, VecVT.getVectorMinNumElements() / 8,
4158                                VecVT.isScalableVector());
4159       Vec = DAG.getBitcast(VecVT, Vec);
4160     } else {
4161       // We can't slide this mask vector down, indexed by its i1 elements.
4162       // This poses a problem when we wish to extract a scalable vector which
4163       // can't be re-expressed as a larger type. Just choose the slow path and
4164       // extend to a larger type, then truncate back down.
4165       // TODO: We could probably improve this when extracting certain fixed
4166       // from fixed, where we can extract as i8 and shift the correct element
4167       // right to reach the desired subvector?
4168       MVT ExtVecVT = VecVT.changeVectorElementType(MVT::i8);
4169       MVT ExtSubVecVT = SubVecVT.changeVectorElementType(MVT::i8);
4170       Vec = DAG.getNode(ISD::ZERO_EXTEND, DL, ExtVecVT, Vec);
4171       Vec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ExtSubVecVT, Vec,
4172                         Op.getOperand(1));
4173       SDValue SplatZero = DAG.getConstant(0, DL, ExtSubVecVT);
4174       return DAG.getSetCC(DL, SubVecVT, Vec, SplatZero, ISD::SETNE);
4175     }
4176   }
4177 
4178   // If the subvector vector is a fixed-length type, we cannot use subregister
4179   // manipulation to simplify the codegen; we don't know which register of a
4180   // LMUL group contains the specific subvector as we only know the minimum
4181   // register size. Therefore we must slide the vector group down the full
4182   // amount.
4183   if (SubVecVT.isFixedLengthVector()) {
4184     // With an index of 0 this is a cast-like subvector, which can be performed
4185     // with subregister operations.
4186     if (OrigIdx == 0)
4187       return Op;
4188     MVT ContainerVT = VecVT;
4189     if (VecVT.isFixedLengthVector()) {
4190       ContainerVT = getContainerForFixedLengthVector(VecVT);
4191       Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
4192     }
4193     SDValue Mask =
4194         getDefaultVLOps(VecVT, ContainerVT, DL, DAG, Subtarget).first;
4195     // Set the vector length to only the number of elements we care about. This
4196     // avoids sliding down elements we're going to discard straight away.
4197     SDValue VL = DAG.getConstant(SubVecVT.getVectorNumElements(), DL, XLenVT);
4198     SDValue SlidedownAmt = DAG.getConstant(OrigIdx, DL, XLenVT);
4199     SDValue Slidedown =
4200         DAG.getNode(RISCVISD::VSLIDEDOWN_VL, DL, ContainerVT,
4201                     DAG.getUNDEF(ContainerVT), Vec, SlidedownAmt, Mask, VL);
4202     // Now we can use a cast-like subvector extract to get the result.
4203     Slidedown = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, Slidedown,
4204                             DAG.getConstant(0, DL, XLenVT));
4205     return DAG.getBitcast(Op.getValueType(), Slidedown);
4206   }
4207 
4208   unsigned SubRegIdx, RemIdx;
4209   std::tie(SubRegIdx, RemIdx) =
4210       RISCVTargetLowering::decomposeSubvectorInsertExtractToSubRegs(
4211           VecVT, SubVecVT, OrigIdx, TRI);
4212 
4213   // If the Idx has been completely eliminated then this is a subvector extract
4214   // which naturally aligns to a vector register. These can easily be handled
4215   // using subregister manipulation.
4216   if (RemIdx == 0)
4217     return Op;
4218 
4219   // Else we must shift our vector register directly to extract the subvector.
4220   // Do this using VSLIDEDOWN.
4221 
4222   // If the vector type is an LMUL-group type, extract a subvector equal to the
4223   // nearest full vector register type. This should resolve to a EXTRACT_SUBREG
4224   // instruction.
4225   MVT InterSubVT = VecVT;
4226   if (VecVT.bitsGT(getLMUL1VT(VecVT))) {
4227     InterSubVT = getLMUL1VT(VecVT);
4228     Vec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InterSubVT, Vec,
4229                       DAG.getConstant(OrigIdx - RemIdx, DL, XLenVT));
4230   }
4231 
4232   // Slide this vector register down by the desired number of elements in order
4233   // to place the desired subvector starting at element 0.
4234   SDValue SlidedownAmt = DAG.getConstant(RemIdx, DL, XLenVT);
4235   // For scalable vectors this must be further multiplied by vscale.
4236   SlidedownAmt = DAG.getNode(ISD::VSCALE, DL, XLenVT, SlidedownAmt);
4237 
4238   SDValue Mask, VL;
4239   std::tie(Mask, VL) = getDefaultScalableVLOps(InterSubVT, DL, DAG, Subtarget);
4240   SDValue Slidedown =
4241       DAG.getNode(RISCVISD::VSLIDEDOWN_VL, DL, InterSubVT,
4242                   DAG.getUNDEF(InterSubVT), Vec, SlidedownAmt, Mask, VL);
4243 
4244   // Now the vector is in the right position, extract our final subvector. This
4245   // should resolve to a COPY.
4246   Slidedown = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, Slidedown,
4247                           DAG.getConstant(0, DL, XLenVT));
4248 
4249   // We might have bitcast from a mask type: cast back to the original type if
4250   // required.
4251   return DAG.getBitcast(Op.getSimpleValueType(), Slidedown);
4252 }
4253 
4254 // Lower step_vector to the vid instruction. Any non-identity step value must
4255 // be accounted for my manual expansion.
4256 SDValue RISCVTargetLowering::lowerSTEP_VECTOR(SDValue Op,
4257                                               SelectionDAG &DAG) const {
4258   SDLoc DL(Op);
4259   MVT VT = Op.getSimpleValueType();
4260   MVT XLenVT = Subtarget.getXLenVT();
4261   SDValue Mask, VL;
4262   std::tie(Mask, VL) = getDefaultScalableVLOps(VT, DL, DAG, Subtarget);
4263   SDValue StepVec = DAG.getNode(RISCVISD::VID_VL, DL, VT, Mask, VL);
4264   uint64_t StepValImm = Op.getConstantOperandVal(0);
4265   if (StepValImm != 1) {
4266     if (isPowerOf2_64(StepValImm)) {
4267       SDValue StepVal =
4268           DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VT,
4269                       DAG.getConstant(Log2_64(StepValImm), DL, XLenVT));
4270       StepVec = DAG.getNode(ISD::SHL, DL, VT, StepVec, StepVal);
4271     } else {
4272       SDValue StepVal = lowerScalarSplat(
4273           DAG.getConstant(StepValImm, DL, VT.getVectorElementType()), VL, VT,
4274           DL, DAG, Subtarget);
4275       StepVec = DAG.getNode(ISD::MUL, DL, VT, StepVec, StepVal);
4276     }
4277   }
4278   return StepVec;
4279 }
4280 
4281 // Implement vector_reverse using vrgather.vv with indices determined by
4282 // subtracting the id of each element from (VLMAX-1). This will convert
4283 // the indices like so:
4284 // (0, 1,..., VLMAX-2, VLMAX-1) -> (VLMAX-1, VLMAX-2,..., 1, 0).
4285 // TODO: This code assumes VLMAX <= 65536 for LMUL=8 SEW=16.
4286 SDValue RISCVTargetLowering::lowerVECTOR_REVERSE(SDValue Op,
4287                                                  SelectionDAG &DAG) const {
4288   SDLoc DL(Op);
4289   MVT VecVT = Op.getSimpleValueType();
4290   unsigned EltSize = VecVT.getScalarSizeInBits();
4291   unsigned MinSize = VecVT.getSizeInBits().getKnownMinValue();
4292 
4293   unsigned MaxVLMAX = 0;
4294   unsigned VectorBitsMax = Subtarget.getMaxRVVVectorSizeInBits();
4295   if (VectorBitsMax != 0)
4296     MaxVLMAX = ((VectorBitsMax / EltSize) * MinSize) / RISCV::RVVBitsPerBlock;
4297 
4298   unsigned GatherOpc = RISCVISD::VRGATHER_VV_VL;
4299   MVT IntVT = VecVT.changeVectorElementTypeToInteger();
4300 
4301   // If this is SEW=8 and VLMAX is unknown or more than 256, we need
4302   // to use vrgatherei16.vv.
4303   // TODO: It's also possible to use vrgatherei16.vv for other types to
4304   // decrease register width for the index calculation.
4305   if ((MaxVLMAX == 0 || MaxVLMAX > 256) && EltSize == 8) {
4306     // If this is LMUL=8, we have to split before can use vrgatherei16.vv.
4307     // Reverse each half, then reassemble them in reverse order.
4308     // NOTE: It's also possible that after splitting that VLMAX no longer
4309     // requires vrgatherei16.vv.
4310     if (MinSize == (8 * RISCV::RVVBitsPerBlock)) {
4311       SDValue Lo, Hi;
4312       std::tie(Lo, Hi) = DAG.SplitVectorOperand(Op.getNode(), 0);
4313       EVT LoVT, HiVT;
4314       std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VecVT);
4315       Lo = DAG.getNode(ISD::VECTOR_REVERSE, DL, LoVT, Lo);
4316       Hi = DAG.getNode(ISD::VECTOR_REVERSE, DL, HiVT, Hi);
4317       // Reassemble the low and high pieces reversed.
4318       // FIXME: This is a CONCAT_VECTORS.
4319       SDValue Res =
4320           DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VecVT, DAG.getUNDEF(VecVT), Hi,
4321                       DAG.getIntPtrConstant(0, DL));
4322       return DAG.getNode(
4323           ISD::INSERT_SUBVECTOR, DL, VecVT, Res, Lo,
4324           DAG.getIntPtrConstant(LoVT.getVectorMinNumElements(), DL));
4325     }
4326 
4327     // Just promote the int type to i16 which will double the LMUL.
4328     IntVT = MVT::getVectorVT(MVT::i16, VecVT.getVectorElementCount());
4329     GatherOpc = RISCVISD::VRGATHEREI16_VV_VL;
4330   }
4331 
4332   MVT XLenVT = Subtarget.getXLenVT();
4333   SDValue Mask, VL;
4334   std::tie(Mask, VL) = getDefaultScalableVLOps(VecVT, DL, DAG, Subtarget);
4335 
4336   // Calculate VLMAX-1 for the desired SEW.
4337   unsigned MinElts = VecVT.getVectorMinNumElements();
4338   SDValue VLMax = DAG.getNode(ISD::VSCALE, DL, XLenVT,
4339                               DAG.getConstant(MinElts, DL, XLenVT));
4340   SDValue VLMinus1 =
4341       DAG.getNode(ISD::SUB, DL, XLenVT, VLMax, DAG.getConstant(1, DL, XLenVT));
4342 
4343   // Splat VLMAX-1 taking care to handle SEW==64 on RV32.
4344   bool IsRV32E64 =
4345       !Subtarget.is64Bit() && IntVT.getVectorElementType() == MVT::i64;
4346   SDValue SplatVL;
4347   if (!IsRV32E64)
4348     SplatVL = DAG.getSplatVector(IntVT, DL, VLMinus1);
4349   else
4350     SplatVL = DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, IntVT, VLMinus1);
4351 
4352   SDValue VID = DAG.getNode(RISCVISD::VID_VL, DL, IntVT, Mask, VL);
4353   SDValue Indices =
4354       DAG.getNode(RISCVISD::SUB_VL, DL, IntVT, SplatVL, VID, Mask, VL);
4355 
4356   return DAG.getNode(GatherOpc, DL, VecVT, Op.getOperand(0), Indices, Mask, VL);
4357 }
4358 
4359 SDValue
4360 RISCVTargetLowering::lowerFixedLengthVectorLoadToRVV(SDValue Op,
4361                                                      SelectionDAG &DAG) const {
4362   SDLoc DL(Op);
4363   auto *Load = cast<LoadSDNode>(Op);
4364 
4365   assert(allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
4366                                         Load->getMemoryVT(),
4367                                         *Load->getMemOperand()) &&
4368          "Expecting a correctly-aligned load");
4369 
4370   MVT VT = Op.getSimpleValueType();
4371   MVT ContainerVT = getContainerForFixedLengthVector(VT);
4372 
4373   SDValue VL =
4374       DAG.getConstant(VT.getVectorNumElements(), DL, Subtarget.getXLenVT());
4375 
4376   SDVTList VTs = DAG.getVTList({ContainerVT, MVT::Other});
4377   SDValue NewLoad = DAG.getMemIntrinsicNode(
4378       RISCVISD::VLE_VL, DL, VTs, {Load->getChain(), Load->getBasePtr(), VL},
4379       Load->getMemoryVT(), Load->getMemOperand());
4380 
4381   SDValue Result = convertFromScalableVector(VT, NewLoad, DAG, Subtarget);
4382   return DAG.getMergeValues({Result, Load->getChain()}, DL);
4383 }
4384 
4385 SDValue
4386 RISCVTargetLowering::lowerFixedLengthVectorStoreToRVV(SDValue Op,
4387                                                       SelectionDAG &DAG) const {
4388   SDLoc DL(Op);
4389   auto *Store = cast<StoreSDNode>(Op);
4390 
4391   assert(allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
4392                                         Store->getMemoryVT(),
4393                                         *Store->getMemOperand()) &&
4394          "Expecting a correctly-aligned store");
4395 
4396   SDValue StoreVal = Store->getValue();
4397   MVT VT = StoreVal.getSimpleValueType();
4398 
4399   // If the size less than a byte, we need to pad with zeros to make a byte.
4400   if (VT.getVectorElementType() == MVT::i1 && VT.getVectorNumElements() < 8) {
4401     VT = MVT::v8i1;
4402     StoreVal = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT,
4403                            DAG.getConstant(0, DL, VT), StoreVal,
4404                            DAG.getIntPtrConstant(0, DL));
4405   }
4406 
4407   MVT ContainerVT = getContainerForFixedLengthVector(VT);
4408 
4409   SDValue VL =
4410       DAG.getConstant(VT.getVectorNumElements(), DL, Subtarget.getXLenVT());
4411 
4412   SDValue NewValue =
4413       convertToScalableVector(ContainerVT, StoreVal, DAG, Subtarget);
4414   return DAG.getMemIntrinsicNode(
4415       RISCVISD::VSE_VL, DL, DAG.getVTList(MVT::Other),
4416       {Store->getChain(), NewValue, Store->getBasePtr(), VL},
4417       Store->getMemoryVT(), Store->getMemOperand());
4418 }
4419 
4420 SDValue RISCVTargetLowering::lowerMLOAD(SDValue Op, SelectionDAG &DAG) const {
4421   auto *Load = cast<MaskedLoadSDNode>(Op);
4422 
4423   SDLoc DL(Op);
4424   MVT VT = Op.getSimpleValueType();
4425   MVT XLenVT = Subtarget.getXLenVT();
4426 
4427   SDValue Mask = Load->getMask();
4428   SDValue PassThru = Load->getPassThru();
4429   SDValue VL;
4430 
4431   MVT ContainerVT = VT;
4432   if (VT.isFixedLengthVector()) {
4433     ContainerVT = getContainerForFixedLengthVector(VT);
4434     MVT MaskVT = MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
4435 
4436     Mask = convertToScalableVector(MaskVT, Mask, DAG, Subtarget);
4437     PassThru = convertToScalableVector(ContainerVT, PassThru, DAG, Subtarget);
4438     VL = DAG.getConstant(VT.getVectorNumElements(), DL, XLenVT);
4439   } else
4440     VL = DAG.getRegister(RISCV::X0, XLenVT);
4441 
4442   SDVTList VTs = DAG.getVTList({ContainerVT, MVT::Other});
4443   SDValue IntID = DAG.getTargetConstant(Intrinsic::riscv_vle_mask, DL, XLenVT);
4444   SDValue Ops[] = {Load->getChain(),   IntID, PassThru,
4445                    Load->getBasePtr(), Mask,  VL};
4446   SDValue Result =
4447       DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, DL, VTs, Ops,
4448                               Load->getMemoryVT(), Load->getMemOperand());
4449   SDValue Chain = Result.getValue(1);
4450 
4451   if (VT.isFixedLengthVector())
4452     Result = convertFromScalableVector(VT, Result, DAG, Subtarget);
4453 
4454   return DAG.getMergeValues({Result, Chain}, DL);
4455 }
4456 
4457 SDValue RISCVTargetLowering::lowerMSTORE(SDValue Op, SelectionDAG &DAG) const {
4458   auto *Store = cast<MaskedStoreSDNode>(Op);
4459 
4460   SDLoc DL(Op);
4461   SDValue Val = Store->getValue();
4462   SDValue Mask = Store->getMask();
4463   MVT VT = Val.getSimpleValueType();
4464   MVT XLenVT = Subtarget.getXLenVT();
4465   SDValue VL;
4466 
4467   MVT ContainerVT = VT;
4468   if (VT.isFixedLengthVector()) {
4469     ContainerVT = getContainerForFixedLengthVector(VT);
4470     MVT MaskVT = MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
4471 
4472     Val = convertToScalableVector(ContainerVT, Val, DAG, Subtarget);
4473     Mask = convertToScalableVector(MaskVT, Mask, DAG, Subtarget);
4474     VL = DAG.getConstant(VT.getVectorNumElements(), DL, XLenVT);
4475   } else
4476     VL = DAG.getRegister(RISCV::X0, XLenVT);
4477 
4478   SDValue IntID = DAG.getTargetConstant(Intrinsic::riscv_vse_mask, DL, XLenVT);
4479   return DAG.getMemIntrinsicNode(
4480       ISD::INTRINSIC_VOID, DL, DAG.getVTList(MVT::Other),
4481       {Store->getChain(), IntID, Val, Store->getBasePtr(), Mask, VL},
4482       Store->getMemoryVT(), Store->getMemOperand());
4483 }
4484 
4485 SDValue
4486 RISCVTargetLowering::lowerFixedLengthVectorSetccToRVV(SDValue Op,
4487                                                       SelectionDAG &DAG) const {
4488   MVT InVT = Op.getOperand(0).getSimpleValueType();
4489   MVT ContainerVT = getContainerForFixedLengthVector(InVT);
4490 
4491   MVT VT = Op.getSimpleValueType();
4492 
4493   SDValue Op1 =
4494       convertToScalableVector(ContainerVT, Op.getOperand(0), DAG, Subtarget);
4495   SDValue Op2 =
4496       convertToScalableVector(ContainerVT, Op.getOperand(1), DAG, Subtarget);
4497 
4498   SDLoc DL(Op);
4499   SDValue VL =
4500       DAG.getConstant(VT.getVectorNumElements(), DL, Subtarget.getXLenVT());
4501 
4502   MVT MaskVT = MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
4503   SDValue Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, MaskVT, VL);
4504 
4505   SDValue Cmp = DAG.getNode(RISCVISD::SETCC_VL, DL, MaskVT, Op1, Op2,
4506                             Op.getOperand(2), Mask, VL);
4507 
4508   return convertFromScalableVector(VT, Cmp, DAG, Subtarget);
4509 }
4510 
4511 SDValue RISCVTargetLowering::lowerFixedLengthVectorLogicOpToRVV(
4512     SDValue Op, SelectionDAG &DAG, unsigned MaskOpc, unsigned VecOpc) const {
4513   MVT VT = Op.getSimpleValueType();
4514 
4515   if (VT.getVectorElementType() == MVT::i1)
4516     return lowerToScalableOp(Op, DAG, MaskOpc, /*HasMask*/ false);
4517 
4518   return lowerToScalableOp(Op, DAG, VecOpc, /*HasMask*/ true);
4519 }
4520 
4521 SDValue
4522 RISCVTargetLowering::lowerFixedLengthVectorShiftToRVV(SDValue Op,
4523                                                       SelectionDAG &DAG) const {
4524   unsigned Opc;
4525   switch (Op.getOpcode()) {
4526   default: llvm_unreachable("Unexpected opcode!");
4527   case ISD::SHL: Opc = RISCVISD::SHL_VL; break;
4528   case ISD::SRA: Opc = RISCVISD::SRA_VL; break;
4529   case ISD::SRL: Opc = RISCVISD::SRL_VL; break;
4530   }
4531 
4532   return lowerToScalableOp(Op, DAG, Opc);
4533 }
4534 
4535 // Lower vector ABS to smax(X, sub(0, X)).
4536 SDValue RISCVTargetLowering::lowerABS(SDValue Op, SelectionDAG &DAG) const {
4537   SDLoc DL(Op);
4538   MVT VT = Op.getSimpleValueType();
4539   SDValue X = Op.getOperand(0);
4540 
4541   assert(VT.isFixedLengthVector() && "Unexpected type");
4542 
4543   MVT ContainerVT = getContainerForFixedLengthVector(VT);
4544   X = convertToScalableVector(ContainerVT, X, DAG, Subtarget);
4545 
4546   SDValue Mask, VL;
4547   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
4548 
4549   SDValue SplatZero =
4550       DAG.getNode(RISCVISD::VMV_V_X_VL, DL, ContainerVT,
4551                   DAG.getConstant(0, DL, Subtarget.getXLenVT()));
4552   SDValue NegX =
4553       DAG.getNode(RISCVISD::SUB_VL, DL, ContainerVT, SplatZero, X, Mask, VL);
4554   SDValue Max =
4555       DAG.getNode(RISCVISD::SMAX_VL, DL, ContainerVT, X, NegX, Mask, VL);
4556 
4557   return convertFromScalableVector(VT, Max, DAG, Subtarget);
4558 }
4559 
4560 SDValue RISCVTargetLowering::lowerFixedLengthVectorFCOPYSIGNToRVV(
4561     SDValue Op, SelectionDAG &DAG) const {
4562   SDLoc DL(Op);
4563   MVT VT = Op.getSimpleValueType();
4564   SDValue Mag = Op.getOperand(0);
4565   SDValue Sign = Op.getOperand(1);
4566   assert(Mag.getValueType() == Sign.getValueType() &&
4567          "Can only handle COPYSIGN with matching types.");
4568 
4569   MVT ContainerVT = getContainerForFixedLengthVector(VT);
4570   Mag = convertToScalableVector(ContainerVT, Mag, DAG, Subtarget);
4571   Sign = convertToScalableVector(ContainerVT, Sign, DAG, Subtarget);
4572 
4573   SDValue Mask, VL;
4574   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
4575 
4576   SDValue CopySign =
4577       DAG.getNode(RISCVISD::FCOPYSIGN_VL, DL, ContainerVT, Mag, Sign, Mask, VL);
4578 
4579   return convertFromScalableVector(VT, CopySign, DAG, Subtarget);
4580 }
4581 
4582 SDValue RISCVTargetLowering::lowerFixedLengthVectorSelectToRVV(
4583     SDValue Op, SelectionDAG &DAG) const {
4584   MVT VT = Op.getSimpleValueType();
4585   MVT ContainerVT = getContainerForFixedLengthVector(VT);
4586 
4587   MVT I1ContainerVT =
4588       MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
4589 
4590   SDValue CC =
4591       convertToScalableVector(I1ContainerVT, Op.getOperand(0), DAG, Subtarget);
4592   SDValue Op1 =
4593       convertToScalableVector(ContainerVT, Op.getOperand(1), DAG, Subtarget);
4594   SDValue Op2 =
4595       convertToScalableVector(ContainerVT, Op.getOperand(2), DAG, Subtarget);
4596 
4597   SDLoc DL(Op);
4598   SDValue Mask, VL;
4599   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
4600 
4601   SDValue Select =
4602       DAG.getNode(RISCVISD::VSELECT_VL, DL, ContainerVT, CC, Op1, Op2, VL);
4603 
4604   return convertFromScalableVector(VT, Select, DAG, Subtarget);
4605 }
4606 
4607 SDValue RISCVTargetLowering::lowerToScalableOp(SDValue Op, SelectionDAG &DAG,
4608                                                unsigned NewOpc,
4609                                                bool HasMask) const {
4610   MVT VT = Op.getSimpleValueType();
4611   MVT ContainerVT = getContainerForFixedLengthVector(VT);
4612 
4613   // Create list of operands by converting existing ones to scalable types.
4614   SmallVector<SDValue, 6> Ops;
4615   for (const SDValue &V : Op->op_values()) {
4616     assert(!isa<VTSDNode>(V) && "Unexpected VTSDNode node!");
4617 
4618     // Pass through non-vector operands.
4619     if (!V.getValueType().isVector()) {
4620       Ops.push_back(V);
4621       continue;
4622     }
4623 
4624     // "cast" fixed length vector to a scalable vector.
4625     assert(useRVVForFixedLengthVectorVT(V.getSimpleValueType()) &&
4626            "Only fixed length vectors are supported!");
4627     Ops.push_back(convertToScalableVector(ContainerVT, V, DAG, Subtarget));
4628   }
4629 
4630   SDLoc DL(Op);
4631   SDValue Mask, VL;
4632   std::tie(Mask, VL) = getDefaultVLOps(VT, ContainerVT, DL, DAG, Subtarget);
4633   if (HasMask)
4634     Ops.push_back(Mask);
4635   Ops.push_back(VL);
4636 
4637   SDValue ScalableRes = DAG.getNode(NewOpc, DL, ContainerVT, Ops);
4638   return convertFromScalableVector(VT, ScalableRes, DAG, Subtarget);
4639 }
4640 
4641 // Lower a VP_* ISD node to the corresponding RISCVISD::*_VL node:
4642 // * Operands of each node are assumed to be in the same order.
4643 // * The EVL operand is promoted from i32 to i64 on RV64.
4644 // * Fixed-length vectors are converted to their scalable-vector container
4645 //   types.
4646 SDValue RISCVTargetLowering::lowerVPOp(SDValue Op, SelectionDAG &DAG,
4647                                        unsigned RISCVISDOpc) const {
4648   SDLoc DL(Op);
4649   MVT VT = Op.getSimpleValueType();
4650   SmallVector<SDValue, 4> Ops;
4651 
4652   for (const auto &OpIdx : enumerate(Op->ops())) {
4653     SDValue V = OpIdx.value();
4654     assert(!isa<VTSDNode>(V) && "Unexpected VTSDNode node!");
4655     // Pass through operands which aren't fixed-length vectors.
4656     if (!V.getValueType().isFixedLengthVector()) {
4657       Ops.push_back(V);
4658       continue;
4659     }
4660     // "cast" fixed length vector to a scalable vector.
4661     MVT OpVT = V.getSimpleValueType();
4662     MVT ContainerVT = getContainerForFixedLengthVector(OpVT);
4663     assert(useRVVForFixedLengthVectorVT(OpVT) &&
4664            "Only fixed length vectors are supported!");
4665     Ops.push_back(convertToScalableVector(ContainerVT, V, DAG, Subtarget));
4666   }
4667 
4668   if (!VT.isFixedLengthVector())
4669     return DAG.getNode(RISCVISDOpc, DL, VT, Ops);
4670 
4671   MVT ContainerVT = getContainerForFixedLengthVector(VT);
4672 
4673   SDValue VPOp = DAG.getNode(RISCVISDOpc, DL, ContainerVT, Ops);
4674 
4675   return convertFromScalableVector(VT, VPOp, DAG, Subtarget);
4676 }
4677 
4678 // Custom lower MGATHER to a legalized form for RVV. It will then be matched to
4679 // a RVV indexed load. The RVV indexed load instructions only support the
4680 // "unsigned unscaled" addressing mode; indices are implicitly zero-extended or
4681 // truncated to XLEN and are treated as byte offsets. Any signed or scaled
4682 // indexing is extended to the XLEN value type and scaled accordingly.
4683 SDValue RISCVTargetLowering::lowerMGATHER(SDValue Op, SelectionDAG &DAG) const {
4684   auto *MGN = cast<MaskedGatherSDNode>(Op.getNode());
4685   SDLoc DL(Op);
4686 
4687   SDValue Index = MGN->getIndex();
4688   SDValue Mask = MGN->getMask();
4689   SDValue PassThru = MGN->getPassThru();
4690 
4691   MVT VT = Op.getSimpleValueType();
4692   MVT IndexVT = Index.getSimpleValueType();
4693   MVT XLenVT = Subtarget.getXLenVT();
4694 
4695   assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
4696          "Unexpected VTs!");
4697   assert(MGN->getBasePtr().getSimpleValueType() == XLenVT &&
4698          "Unexpected pointer type");
4699   // Targets have to explicitly opt-in for extending vector loads.
4700   assert(MGN->getExtensionType() == ISD::NON_EXTLOAD &&
4701          "Unexpected extending MGATHER");
4702 
4703   // If the mask is known to be all ones, optimize to an unmasked intrinsic;
4704   // the selection of the masked intrinsics doesn't do this for us.
4705   bool IsUnmasked = ISD::isConstantSplatVectorAllOnes(Mask.getNode());
4706 
4707   SDValue VL;
4708   MVT ContainerVT = VT;
4709   if (VT.isFixedLengthVector()) {
4710     // We need to use the larger of the result and index type to determine the
4711     // scalable type to use so we don't increase LMUL for any operand/result.
4712     if (VT.bitsGE(IndexVT)) {
4713       ContainerVT = getContainerForFixedLengthVector(VT);
4714       IndexVT = MVT::getVectorVT(IndexVT.getVectorElementType(),
4715                                  ContainerVT.getVectorElementCount());
4716     } else {
4717       IndexVT = getContainerForFixedLengthVector(IndexVT);
4718       ContainerVT = MVT::getVectorVT(ContainerVT.getVectorElementType(),
4719                                      IndexVT.getVectorElementCount());
4720     }
4721 
4722     Index = convertToScalableVector(IndexVT, Index, DAG, Subtarget);
4723 
4724     if (!IsUnmasked) {
4725       MVT MaskVT =
4726           MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
4727       Mask = convertToScalableVector(MaskVT, Mask, DAG, Subtarget);
4728       PassThru = convertToScalableVector(ContainerVT, PassThru, DAG, Subtarget);
4729     }
4730 
4731     VL = DAG.getConstant(VT.getVectorNumElements(), DL, XLenVT);
4732   } else
4733     VL = DAG.getRegister(RISCV::X0, XLenVT);
4734 
4735   unsigned IntID =
4736       IsUnmasked ? Intrinsic::riscv_vluxei : Intrinsic::riscv_vluxei_mask;
4737   SmallVector<SDValue, 8> Ops{MGN->getChain(),
4738                               DAG.getTargetConstant(IntID, DL, XLenVT)};
4739   if (!IsUnmasked)
4740     Ops.push_back(PassThru);
4741   Ops.push_back(MGN->getBasePtr());
4742   Ops.push_back(Index);
4743   if (!IsUnmasked)
4744     Ops.push_back(Mask);
4745   Ops.push_back(VL);
4746 
4747   SDVTList VTs = DAG.getVTList({ContainerVT, MVT::Other});
4748   SDValue Result =
4749       DAG.getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, DL, VTs, Ops,
4750                               MGN->getMemoryVT(), MGN->getMemOperand());
4751   SDValue Chain = Result.getValue(1);
4752 
4753   if (VT.isFixedLengthVector())
4754     Result = convertFromScalableVector(VT, Result, DAG, Subtarget);
4755 
4756   return DAG.getMergeValues({Result, Chain}, DL);
4757 }
4758 
4759 // Custom lower MSCATTER to a legalized form for RVV. It will then be matched to
4760 // a RVV indexed store. The RVV indexed store instructions only support the
4761 // "unsigned unscaled" addressing mode; indices are implicitly zero-extended or
4762 // truncated to XLEN and are treated as byte offsets. Any signed or scaled
4763 // indexing is extended to the XLEN value type and scaled accordingly.
4764 SDValue RISCVTargetLowering::lowerMSCATTER(SDValue Op,
4765                                            SelectionDAG &DAG) const {
4766   auto *MSN = cast<MaskedScatterSDNode>(Op.getNode());
4767   SDLoc DL(Op);
4768   SDValue Index = MSN->getIndex();
4769   SDValue Mask = MSN->getMask();
4770   SDValue Val = MSN->getValue();
4771 
4772   MVT VT = Val.getSimpleValueType();
4773   MVT IndexVT = Index.getSimpleValueType();
4774   MVT XLenVT = Subtarget.getXLenVT();
4775 
4776   assert(VT.getVectorElementCount() == IndexVT.getVectorElementCount() &&
4777          "Unexpected VTs!");
4778   assert(MSN->getBasePtr().getSimpleValueType() == XLenVT &&
4779          "Unexpected pointer type");
4780   // Targets have to explicitly opt-in for extending vector loads and
4781   // truncating vector stores.
4782   assert(!MSN->isTruncatingStore() && "Unexpected extending MSCATTER");
4783 
4784   // If the mask is known to be all ones, optimize to an unmasked intrinsic;
4785   // the selection of the masked intrinsics doesn't do this for us.
4786   bool IsUnmasked = ISD::isConstantSplatVectorAllOnes(Mask.getNode());
4787 
4788   SDValue VL;
4789   if (VT.isFixedLengthVector()) {
4790     // We need to use the larger of the value and index type to determine the
4791     // scalable type to use so we don't increase LMUL for any operand/result.
4792     MVT ContainerVT;
4793     if (VT.bitsGE(IndexVT)) {
4794       ContainerVT = getContainerForFixedLengthVector(VT);
4795       IndexVT = MVT::getVectorVT(IndexVT.getVectorElementType(),
4796                                  ContainerVT.getVectorElementCount());
4797     } else {
4798       IndexVT = getContainerForFixedLengthVector(IndexVT);
4799       ContainerVT = MVT::getVectorVT(VT.getVectorElementType(),
4800                                      IndexVT.getVectorElementCount());
4801     }
4802 
4803     Index = convertToScalableVector(IndexVT, Index, DAG, Subtarget);
4804     Val = convertToScalableVector(ContainerVT, Val, DAG, Subtarget);
4805 
4806     if (!IsUnmasked) {
4807       MVT MaskVT =
4808           MVT::getVectorVT(MVT::i1, ContainerVT.getVectorElementCount());
4809       Mask = convertToScalableVector(MaskVT, Mask, DAG, Subtarget);
4810     }
4811 
4812     VL = DAG.getConstant(VT.getVectorNumElements(), DL, XLenVT);
4813   } else
4814     VL = DAG.getRegister(RISCV::X0, XLenVT);
4815 
4816   unsigned IntID =
4817       IsUnmasked ? Intrinsic::riscv_vsoxei : Intrinsic::riscv_vsoxei_mask;
4818   SmallVector<SDValue, 8> Ops{MSN->getChain(),
4819                               DAG.getTargetConstant(IntID, DL, XLenVT)};
4820   Ops.push_back(Val);
4821   Ops.push_back(MSN->getBasePtr());
4822   Ops.push_back(Index);
4823   if (!IsUnmasked)
4824     Ops.push_back(Mask);
4825   Ops.push_back(VL);
4826 
4827   return DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, DL, MSN->getVTList(), Ops,
4828                                  MSN->getMemoryVT(), MSN->getMemOperand());
4829 }
4830 
4831 SDValue RISCVTargetLowering::lowerGET_ROUNDING(SDValue Op,
4832                                                SelectionDAG &DAG) const {
4833   const MVT XLenVT = Subtarget.getXLenVT();
4834   SDLoc DL(Op);
4835   SDValue Chain = Op->getOperand(0);
4836   SDValue SysRegNo = DAG.getConstant(
4837       RISCVSysReg::lookupSysRegByName("FRM")->Encoding, DL, XLenVT);
4838   SDVTList VTs = DAG.getVTList(XLenVT, MVT::Other);
4839   SDValue RM = DAG.getNode(RISCVISD::READ_CSR, DL, VTs, Chain, SysRegNo);
4840 
4841   // Encoding used for rounding mode in RISCV differs from that used in
4842   // FLT_ROUNDS. To convert it the RISCV rounding mode is used as an index in a
4843   // table, which consists of a sequence of 4-bit fields, each representing
4844   // corresponding FLT_ROUNDS mode.
4845   static const int Table =
4846       (int(RoundingMode::NearestTiesToEven) << 4 * RISCVFPRndMode::RNE) |
4847       (int(RoundingMode::TowardZero) << 4 * RISCVFPRndMode::RTZ) |
4848       (int(RoundingMode::TowardNegative) << 4 * RISCVFPRndMode::RDN) |
4849       (int(RoundingMode::TowardPositive) << 4 * RISCVFPRndMode::RUP) |
4850       (int(RoundingMode::NearestTiesToAway) << 4 * RISCVFPRndMode::RMM);
4851 
4852   SDValue Shift =
4853       DAG.getNode(ISD::SHL, DL, XLenVT, RM, DAG.getConstant(2, DL, XLenVT));
4854   SDValue Shifted = DAG.getNode(ISD::SRL, DL, XLenVT,
4855                                 DAG.getConstant(Table, DL, XLenVT), Shift);
4856   SDValue Masked = DAG.getNode(ISD::AND, DL, XLenVT, Shifted,
4857                                DAG.getConstant(7, DL, XLenVT));
4858 
4859   return DAG.getMergeValues({Masked, Chain}, DL);
4860 }
4861 
4862 SDValue RISCVTargetLowering::lowerSET_ROUNDING(SDValue Op,
4863                                                SelectionDAG &DAG) const {
4864   const MVT XLenVT = Subtarget.getXLenVT();
4865   SDLoc DL(Op);
4866   SDValue Chain = Op->getOperand(0);
4867   SDValue RMValue = Op->getOperand(1);
4868   SDValue SysRegNo = DAG.getConstant(
4869       RISCVSysReg::lookupSysRegByName("FRM")->Encoding, DL, XLenVT);
4870 
4871   // Encoding used for rounding mode in RISCV differs from that used in
4872   // FLT_ROUNDS. To convert it the C rounding mode is used as an index in
4873   // a table, which consists of a sequence of 4-bit fields, each representing
4874   // corresponding RISCV mode.
4875   static const unsigned Table =
4876       (RISCVFPRndMode::RNE << 4 * int(RoundingMode::NearestTiesToEven)) |
4877       (RISCVFPRndMode::RTZ << 4 * int(RoundingMode::TowardZero)) |
4878       (RISCVFPRndMode::RDN << 4 * int(RoundingMode::TowardNegative)) |
4879       (RISCVFPRndMode::RUP << 4 * int(RoundingMode::TowardPositive)) |
4880       (RISCVFPRndMode::RMM << 4 * int(RoundingMode::NearestTiesToAway));
4881 
4882   SDValue Shift = DAG.getNode(ISD::SHL, DL, XLenVT, RMValue,
4883                               DAG.getConstant(2, DL, XLenVT));
4884   SDValue Shifted = DAG.getNode(ISD::SRL, DL, XLenVT,
4885                                 DAG.getConstant(Table, DL, XLenVT), Shift);
4886   RMValue = DAG.getNode(ISD::AND, DL, XLenVT, Shifted,
4887                         DAG.getConstant(0x7, DL, XLenVT));
4888   return DAG.getNode(RISCVISD::WRITE_CSR, DL, MVT::Other, Chain, SysRegNo,
4889                      RMValue);
4890 }
4891 
4892 // Returns the opcode of the target-specific SDNode that implements the 32-bit
4893 // form of the given Opcode.
4894 static RISCVISD::NodeType getRISCVWOpcode(unsigned Opcode) {
4895   switch (Opcode) {
4896   default:
4897     llvm_unreachable("Unexpected opcode");
4898   case ISD::SHL:
4899     return RISCVISD::SLLW;
4900   case ISD::SRA:
4901     return RISCVISD::SRAW;
4902   case ISD::SRL:
4903     return RISCVISD::SRLW;
4904   case ISD::SDIV:
4905     return RISCVISD::DIVW;
4906   case ISD::UDIV:
4907     return RISCVISD::DIVUW;
4908   case ISD::UREM:
4909     return RISCVISD::REMUW;
4910   case ISD::ROTL:
4911     return RISCVISD::ROLW;
4912   case ISD::ROTR:
4913     return RISCVISD::RORW;
4914   case RISCVISD::GREV:
4915     return RISCVISD::GREVW;
4916   case RISCVISD::GORC:
4917     return RISCVISD::GORCW;
4918   }
4919 }
4920 
4921 // Converts the given i8/i16/i32 operation to a target-specific SelectionDAG
4922 // node. Because i8/i16/i32 isn't a legal type for RV64, these operations would
4923 // otherwise be promoted to i64, making it difficult to select the
4924 // SLLW/DIVUW/.../*W later one because the fact the operation was originally of
4925 // type i8/i16/i32 is lost.
4926 static SDValue customLegalizeToWOp(SDNode *N, SelectionDAG &DAG,
4927                                    unsigned ExtOpc = ISD::ANY_EXTEND) {
4928   SDLoc DL(N);
4929   RISCVISD::NodeType WOpcode = getRISCVWOpcode(N->getOpcode());
4930   SDValue NewOp0 = DAG.getNode(ExtOpc, DL, MVT::i64, N->getOperand(0));
4931   SDValue NewOp1 = DAG.getNode(ExtOpc, DL, MVT::i64, N->getOperand(1));
4932   SDValue NewRes = DAG.getNode(WOpcode, DL, MVT::i64, NewOp0, NewOp1);
4933   // ReplaceNodeResults requires we maintain the same type for the return value.
4934   return DAG.getNode(ISD::TRUNCATE, DL, N->getValueType(0), NewRes);
4935 }
4936 
4937 // Converts the given 32-bit operation to a i64 operation with signed extension
4938 // semantic to reduce the signed extension instructions.
4939 static SDValue customLegalizeToWOpWithSExt(SDNode *N, SelectionDAG &DAG) {
4940   SDLoc DL(N);
4941   SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
4942   SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
4943   SDValue NewWOp = DAG.getNode(N->getOpcode(), DL, MVT::i64, NewOp0, NewOp1);
4944   SDValue NewRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i64, NewWOp,
4945                                DAG.getValueType(MVT::i32));
4946   return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes);
4947 }
4948 
4949 void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
4950                                              SmallVectorImpl<SDValue> &Results,
4951                                              SelectionDAG &DAG) const {
4952   SDLoc DL(N);
4953   switch (N->getOpcode()) {
4954   default:
4955     llvm_unreachable("Don't know how to custom type legalize this operation!");
4956   case ISD::STRICT_FP_TO_SINT:
4957   case ISD::STRICT_FP_TO_UINT:
4958   case ISD::FP_TO_SINT:
4959   case ISD::FP_TO_UINT: {
4960     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
4961            "Unexpected custom legalisation");
4962     bool IsStrict = N->isStrictFPOpcode();
4963     bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
4964                     N->getOpcode() == ISD::STRICT_FP_TO_SINT;
4965     SDValue Op0 = IsStrict ? N->getOperand(1) : N->getOperand(0);
4966     if (getTypeAction(*DAG.getContext(), Op0.getValueType()) !=
4967         TargetLowering::TypeSoftenFloat) {
4968       // FIXME: Support strict FP.
4969       if (IsStrict)
4970         return;
4971       if (!isTypeLegal(Op0.getValueType()))
4972         return;
4973       unsigned Opc =
4974           IsSigned ? RISCVISD::FCVT_W_RTZ_RV64 : RISCVISD::FCVT_WU_RTZ_RV64;
4975       SDValue Res = DAG.getNode(Opc, DL, MVT::i64, Op0);
4976       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
4977       return;
4978     }
4979     // If the FP type needs to be softened, emit a library call using the 'si'
4980     // version. If we left it to default legalization we'd end up with 'di'. If
4981     // the FP type doesn't need to be softened just let generic type
4982     // legalization promote the result type.
4983     RTLIB::Libcall LC;
4984     if (IsSigned)
4985       LC = RTLIB::getFPTOSINT(Op0.getValueType(), N->getValueType(0));
4986     else
4987       LC = RTLIB::getFPTOUINT(Op0.getValueType(), N->getValueType(0));
4988     MakeLibCallOptions CallOptions;
4989     EVT OpVT = Op0.getValueType();
4990     CallOptions.setTypeListBeforeSoften(OpVT, N->getValueType(0), true);
4991     SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4992     SDValue Result;
4993     std::tie(Result, Chain) =
4994         makeLibCall(DAG, LC, N->getValueType(0), Op0, CallOptions, DL, Chain);
4995     Results.push_back(Result);
4996     if (IsStrict)
4997       Results.push_back(Chain);
4998     break;
4999   }
5000   case ISD::READCYCLECOUNTER: {
5001     assert(!Subtarget.is64Bit() &&
5002            "READCYCLECOUNTER only has custom type legalization on riscv32");
5003 
5004     SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5005     SDValue RCW =
5006         DAG.getNode(RISCVISD::READ_CYCLE_WIDE, DL, VTs, N->getOperand(0));
5007 
5008     Results.push_back(
5009         DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, RCW, RCW.getValue(1)));
5010     Results.push_back(RCW.getValue(2));
5011     break;
5012   }
5013   case ISD::MUL: {
5014     unsigned Size = N->getSimpleValueType(0).getSizeInBits();
5015     unsigned XLen = Subtarget.getXLen();
5016     // This multiply needs to be expanded, try to use MULHSU+MUL if possible.
5017     if (Size > XLen) {
5018       assert(Size == (XLen * 2) && "Unexpected custom legalisation");
5019       SDValue LHS = N->getOperand(0);
5020       SDValue RHS = N->getOperand(1);
5021       APInt HighMask = APInt::getHighBitsSet(Size, XLen);
5022 
5023       bool LHSIsU = DAG.MaskedValueIsZero(LHS, HighMask);
5024       bool RHSIsU = DAG.MaskedValueIsZero(RHS, HighMask);
5025       // We need exactly one side to be unsigned.
5026       if (LHSIsU == RHSIsU)
5027         return;
5028 
5029       auto MakeMULPair = [&](SDValue S, SDValue U) {
5030         MVT XLenVT = Subtarget.getXLenVT();
5031         S = DAG.getNode(ISD::TRUNCATE, DL, XLenVT, S);
5032         U = DAG.getNode(ISD::TRUNCATE, DL, XLenVT, U);
5033         SDValue Lo = DAG.getNode(ISD::MUL, DL, XLenVT, S, U);
5034         SDValue Hi = DAG.getNode(RISCVISD::MULHSU, DL, XLenVT, S, U);
5035         return DAG.getNode(ISD::BUILD_PAIR, DL, N->getValueType(0), Lo, Hi);
5036       };
5037 
5038       bool LHSIsS = DAG.ComputeNumSignBits(LHS) > XLen;
5039       bool RHSIsS = DAG.ComputeNumSignBits(RHS) > XLen;
5040 
5041       // The other operand should be signed, but still prefer MULH when
5042       // possible.
5043       if (RHSIsU && LHSIsS && !RHSIsS)
5044         Results.push_back(MakeMULPair(LHS, RHS));
5045       else if (LHSIsU && RHSIsS && !LHSIsS)
5046         Results.push_back(MakeMULPair(RHS, LHS));
5047 
5048       return;
5049     }
5050     LLVM_FALLTHROUGH;
5051   }
5052   case ISD::ADD:
5053   case ISD::SUB:
5054     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5055            "Unexpected custom legalisation");
5056     if (N->getOperand(1).getOpcode() == ISD::Constant)
5057       return;
5058     Results.push_back(customLegalizeToWOpWithSExt(N, DAG));
5059     break;
5060   case ISD::SHL:
5061   case ISD::SRA:
5062   case ISD::SRL:
5063     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5064            "Unexpected custom legalisation");
5065     if (N->getOperand(1).getOpcode() == ISD::Constant)
5066       return;
5067     Results.push_back(customLegalizeToWOp(N, DAG));
5068     break;
5069   case ISD::ROTL:
5070   case ISD::ROTR:
5071     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5072            "Unexpected custom legalisation");
5073     Results.push_back(customLegalizeToWOp(N, DAG));
5074     break;
5075   case ISD::CTTZ:
5076   case ISD::CTTZ_ZERO_UNDEF:
5077   case ISD::CTLZ:
5078   case ISD::CTLZ_ZERO_UNDEF: {
5079     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5080            "Unexpected custom legalisation");
5081 
5082     SDValue NewOp0 =
5083         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
5084     bool IsCTZ =
5085         N->getOpcode() == ISD::CTTZ || N->getOpcode() == ISD::CTTZ_ZERO_UNDEF;
5086     unsigned Opc = IsCTZ ? RISCVISD::CTZW : RISCVISD::CLZW;
5087     SDValue Res = DAG.getNode(Opc, DL, MVT::i64, NewOp0);
5088     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
5089     return;
5090   }
5091   case ISD::SDIV:
5092   case ISD::UDIV:
5093   case ISD::UREM: {
5094     MVT VT = N->getSimpleValueType(0);
5095     assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
5096            Subtarget.is64Bit() && Subtarget.hasStdExtM() &&
5097            "Unexpected custom legalisation");
5098     // Don't promote division/remainder by constant since we should expand those
5099     // to multiply by magic constant.
5100     // FIXME: What if the expansion is disabled for minsize.
5101     if (N->getOperand(1).getOpcode() == ISD::Constant)
5102       return;
5103 
5104     // If the input is i32, use ANY_EXTEND since the W instructions don't read
5105     // the upper 32 bits. For other types we need to sign or zero extend
5106     // based on the opcode.
5107     unsigned ExtOpc = ISD::ANY_EXTEND;
5108     if (VT != MVT::i32)
5109       ExtOpc = N->getOpcode() == ISD::SDIV ? ISD::SIGN_EXTEND
5110                                            : ISD::ZERO_EXTEND;
5111 
5112     Results.push_back(customLegalizeToWOp(N, DAG, ExtOpc));
5113     break;
5114   }
5115   case ISD::UADDO:
5116   case ISD::USUBO: {
5117     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5118            "Unexpected custom legalisation");
5119     bool IsAdd = N->getOpcode() == ISD::UADDO;
5120     // Create an ADDW or SUBW.
5121     SDValue LHS = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
5122     SDValue RHS = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5123     SDValue Res =
5124         DAG.getNode(IsAdd ? ISD::ADD : ISD::SUB, DL, MVT::i64, LHS, RHS);
5125     Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i64, Res,
5126                       DAG.getValueType(MVT::i32));
5127 
5128     // Sign extend the LHS and perform an unsigned compare with the ADDW result.
5129     // Since the inputs are sign extended from i32, this is equivalent to
5130     // comparing the lower 32 bits.
5131     LHS = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, N->getOperand(0));
5132     SDValue Overflow = DAG.getSetCC(DL, N->getValueType(1), Res, LHS,
5133                                     IsAdd ? ISD::SETULT : ISD::SETUGT);
5134 
5135     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
5136     Results.push_back(Overflow);
5137     return;
5138   }
5139   case ISD::UADDSAT:
5140   case ISD::USUBSAT: {
5141     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5142            "Unexpected custom legalisation");
5143     if (Subtarget.hasStdExtZbb()) {
5144       // With Zbb we can sign extend and let LegalizeDAG use minu/maxu. Using
5145       // sign extend allows overflow of the lower 32 bits to be detected on
5146       // the promoted size.
5147       SDValue LHS =
5148           DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, N->getOperand(0));
5149       SDValue RHS =
5150           DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, N->getOperand(1));
5151       SDValue Res = DAG.getNode(N->getOpcode(), DL, MVT::i64, LHS, RHS);
5152       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
5153       return;
5154     }
5155 
5156     // Without Zbb, expand to UADDO/USUBO+select which will trigger our custom
5157     // promotion for UADDO/USUBO.
5158     Results.push_back(expandAddSubSat(N, DAG));
5159     return;
5160   }
5161   case ISD::BITCAST: {
5162     EVT VT = N->getValueType(0);
5163     assert(VT.isInteger() && !VT.isVector() && "Unexpected VT!");
5164     SDValue Op0 = N->getOperand(0);
5165     EVT Op0VT = Op0.getValueType();
5166     MVT XLenVT = Subtarget.getXLenVT();
5167     if (VT == MVT::i16 && Op0VT == MVT::f16 && Subtarget.hasStdExtZfh()) {
5168       SDValue FPConv = DAG.getNode(RISCVISD::FMV_X_ANYEXTH, DL, XLenVT, Op0);
5169       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FPConv));
5170     } else if (VT == MVT::i32 && Op0VT == MVT::f32 && Subtarget.is64Bit() &&
5171                Subtarget.hasStdExtF()) {
5172       SDValue FPConv =
5173           DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Op0);
5174       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, FPConv));
5175     } else if (!VT.isVector() && Op0VT.isFixedLengthVector() &&
5176                isTypeLegal(Op0VT)) {
5177       // Custom-legalize bitcasts from fixed-length vector types to illegal
5178       // scalar types in order to improve codegen. Bitcast the vector to a
5179       // one-element vector type whose element type is the same as the result
5180       // type, and extract the first element.
5181       LLVMContext &Context = *DAG.getContext();
5182       SDValue BVec = DAG.getBitcast(EVT::getVectorVT(Context, VT, 1), Op0);
5183       Results.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, BVec,
5184                                     DAG.getConstant(0, DL, XLenVT)));
5185     }
5186     break;
5187   }
5188   case RISCVISD::GREV:
5189   case RISCVISD::GORC: {
5190     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5191            "Unexpected custom legalisation");
5192     assert(isa<ConstantSDNode>(N->getOperand(1)) && "Expected constant");
5193     // This is similar to customLegalizeToWOp, except that we pass the second
5194     // operand (a TargetConstant) straight through: it is already of type
5195     // XLenVT.
5196     RISCVISD::NodeType WOpcode = getRISCVWOpcode(N->getOpcode());
5197     SDValue NewOp0 =
5198         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
5199     SDValue NewOp1 =
5200         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5201     SDValue NewRes = DAG.getNode(WOpcode, DL, MVT::i64, NewOp0, NewOp1);
5202     // ReplaceNodeResults requires we maintain the same type for the return
5203     // value.
5204     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes));
5205     break;
5206   }
5207   case RISCVISD::SHFL: {
5208     // There is no SHFLIW instruction, but we can just promote the operation.
5209     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5210            "Unexpected custom legalisation");
5211     assert(isa<ConstantSDNode>(N->getOperand(1)) && "Expected constant");
5212     SDValue NewOp0 =
5213         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
5214     SDValue NewOp1 =
5215         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5216     SDValue NewRes = DAG.getNode(RISCVISD::SHFL, DL, MVT::i64, NewOp0, NewOp1);
5217     // ReplaceNodeResults requires we maintain the same type for the return
5218     // value.
5219     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes));
5220     break;
5221   }
5222   case ISD::BSWAP:
5223   case ISD::BITREVERSE: {
5224     MVT VT = N->getSimpleValueType(0);
5225     MVT XLenVT = Subtarget.getXLenVT();
5226     assert((VT == MVT::i8 || VT == MVT::i16 ||
5227             (VT == MVT::i32 && Subtarget.is64Bit())) &&
5228            Subtarget.hasStdExtZbp() && "Unexpected custom legalisation");
5229     SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, XLenVT, N->getOperand(0));
5230     unsigned Imm = VT.getSizeInBits() - 1;
5231     // If this is BSWAP rather than BITREVERSE, clear the lower 3 bits.
5232     if (N->getOpcode() == ISD::BSWAP)
5233       Imm &= ~0x7U;
5234     unsigned Opc = Subtarget.is64Bit() ? RISCVISD::GREVW : RISCVISD::GREV;
5235     SDValue GREVI =
5236         DAG.getNode(Opc, DL, XLenVT, NewOp0, DAG.getConstant(Imm, DL, XLenVT));
5237     // ReplaceNodeResults requires we maintain the same type for the return
5238     // value.
5239     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, VT, GREVI));
5240     break;
5241   }
5242   case ISD::FSHL:
5243   case ISD::FSHR: {
5244     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5245            Subtarget.hasStdExtZbt() && "Unexpected custom legalisation");
5246     SDValue NewOp0 =
5247         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
5248     SDValue NewOp1 =
5249         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5250     SDValue NewOp2 =
5251         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(2));
5252     // FSLW/FSRW take a 6 bit shift amount but i32 FSHL/FSHR only use 5 bits.
5253     // Mask the shift amount to 5 bits.
5254     NewOp2 = DAG.getNode(ISD::AND, DL, MVT::i64, NewOp2,
5255                          DAG.getConstant(0x1f, DL, MVT::i64));
5256     unsigned Opc =
5257         N->getOpcode() == ISD::FSHL ? RISCVISD::FSLW : RISCVISD::FSRW;
5258     SDValue NewOp = DAG.getNode(Opc, DL, MVT::i64, NewOp0, NewOp1, NewOp2);
5259     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewOp));
5260     break;
5261   }
5262   case ISD::EXTRACT_VECTOR_ELT: {
5263     // Custom-legalize an EXTRACT_VECTOR_ELT where XLEN<SEW, as the SEW element
5264     // type is illegal (currently only vXi64 RV32).
5265     // With vmv.x.s, when SEW > XLEN, only the least-significant XLEN bits are
5266     // transferred to the destination register. We issue two of these from the
5267     // upper- and lower- halves of the SEW-bit vector element, slid down to the
5268     // first element.
5269     SDValue Vec = N->getOperand(0);
5270     SDValue Idx = N->getOperand(1);
5271 
5272     // The vector type hasn't been legalized yet so we can't issue target
5273     // specific nodes if it needs legalization.
5274     // FIXME: We would manually legalize if it's important.
5275     if (!isTypeLegal(Vec.getValueType()))
5276       return;
5277 
5278     MVT VecVT = Vec.getSimpleValueType();
5279 
5280     assert(!Subtarget.is64Bit() && N->getValueType(0) == MVT::i64 &&
5281            VecVT.getVectorElementType() == MVT::i64 &&
5282            "Unexpected EXTRACT_VECTOR_ELT legalization");
5283 
5284     // If this is a fixed vector, we need to convert it to a scalable vector.
5285     MVT ContainerVT = VecVT;
5286     if (VecVT.isFixedLengthVector()) {
5287       ContainerVT = getContainerForFixedLengthVector(VecVT);
5288       Vec = convertToScalableVector(ContainerVT, Vec, DAG, Subtarget);
5289     }
5290 
5291     MVT XLenVT = Subtarget.getXLenVT();
5292 
5293     // Use a VL of 1 to avoid processing more elements than we need.
5294     MVT MaskVT = MVT::getVectorVT(MVT::i1, VecVT.getVectorElementCount());
5295     SDValue VL = DAG.getConstant(1, DL, XLenVT);
5296     SDValue Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, MaskVT, VL);
5297 
5298     // Unless the index is known to be 0, we must slide the vector down to get
5299     // the desired element into index 0.
5300     if (!isNullConstant(Idx)) {
5301       Vec = DAG.getNode(RISCVISD::VSLIDEDOWN_VL, DL, ContainerVT,
5302                         DAG.getUNDEF(ContainerVT), Vec, Idx, Mask, VL);
5303     }
5304 
5305     // Extract the lower XLEN bits of the correct vector element.
5306     SDValue EltLo = DAG.getNode(RISCVISD::VMV_X_S, DL, XLenVT, Vec);
5307 
5308     // To extract the upper XLEN bits of the vector element, shift the first
5309     // element right by 32 bits and re-extract the lower XLEN bits.
5310     SDValue ThirtyTwoV = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, ContainerVT,
5311                                      DAG.getConstant(32, DL, XLenVT), VL);
5312     SDValue LShr32 = DAG.getNode(RISCVISD::SRL_VL, DL, ContainerVT, Vec,
5313                                  ThirtyTwoV, Mask, VL);
5314 
5315     SDValue EltHi = DAG.getNode(RISCVISD::VMV_X_S, DL, XLenVT, LShr32);
5316 
5317     Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, EltLo, EltHi));
5318     break;
5319   }
5320   case ISD::INTRINSIC_WO_CHAIN: {
5321     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5322     switch (IntNo) {
5323     default:
5324       llvm_unreachable(
5325           "Don't know how to custom type legalize this intrinsic!");
5326     case Intrinsic::riscv_orc_b: {
5327       // Lower to the GORCI encoding for orc.b with the operand extended.
5328       SDValue NewOp =
5329           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5330       // If Zbp is enabled, use GORCIW which will sign extend the result.
5331       unsigned Opc =
5332           Subtarget.hasStdExtZbp() ? RISCVISD::GORCW : RISCVISD::GORC;
5333       SDValue Res = DAG.getNode(Opc, DL, MVT::i64, NewOp,
5334                                 DAG.getConstant(7, DL, MVT::i64));
5335       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
5336       return;
5337     }
5338     case Intrinsic::riscv_grev:
5339     case Intrinsic::riscv_gorc: {
5340       assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5341              "Unexpected custom legalisation");
5342       SDValue NewOp1 =
5343           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5344       SDValue NewOp2 =
5345           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(2));
5346       unsigned Opc =
5347           IntNo == Intrinsic::riscv_grev ? RISCVISD::GREVW : RISCVISD::GORCW;
5348       SDValue Res = DAG.getNode(Opc, DL, MVT::i64, NewOp1, NewOp2);
5349       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
5350       break;
5351     }
5352     case Intrinsic::riscv_shfl:
5353     case Intrinsic::riscv_unshfl: {
5354       assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5355              "Unexpected custom legalisation");
5356       SDValue NewOp1 =
5357           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5358       SDValue NewOp2 =
5359           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(2));
5360       unsigned Opc =
5361           IntNo == Intrinsic::riscv_shfl ? RISCVISD::SHFLW : RISCVISD::UNSHFLW;
5362       if (isa<ConstantSDNode>(N->getOperand(2))) {
5363         NewOp2 = DAG.getNode(ISD::AND, DL, MVT::i64, NewOp2,
5364                              DAG.getConstant(0xf, DL, MVT::i64));
5365         Opc =
5366             IntNo == Intrinsic::riscv_shfl ? RISCVISD::SHFL : RISCVISD::UNSHFL;
5367       }
5368       SDValue Res = DAG.getNode(Opc, DL, MVT::i64, NewOp1, NewOp2);
5369       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
5370       break;
5371     }
5372     case Intrinsic::riscv_bcompress:
5373     case Intrinsic::riscv_bdecompress: {
5374       assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
5375              "Unexpected custom legalisation");
5376       SDValue NewOp1 =
5377           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
5378       SDValue NewOp2 =
5379           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(2));
5380       unsigned Opc = IntNo == Intrinsic::riscv_bcompress
5381                          ? RISCVISD::BCOMPRESSW
5382                          : RISCVISD::BDECOMPRESSW;
5383       SDValue Res = DAG.getNode(Opc, DL, MVT::i64, NewOp1, NewOp2);
5384       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Res));
5385       break;
5386     }
5387     case Intrinsic::riscv_vmv_x_s: {
5388       EVT VT = N->getValueType(0);
5389       MVT XLenVT = Subtarget.getXLenVT();
5390       if (VT.bitsLT(XLenVT)) {
5391         // Simple case just extract using vmv.x.s and truncate.
5392         SDValue Extract = DAG.getNode(RISCVISD::VMV_X_S, DL,
5393                                       Subtarget.getXLenVT(), N->getOperand(1));
5394         Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, VT, Extract));
5395         return;
5396       }
5397 
5398       assert(VT == MVT::i64 && !Subtarget.is64Bit() &&
5399              "Unexpected custom legalization");
5400 
5401       // We need to do the move in two steps.
5402       SDValue Vec = N->getOperand(1);
5403       MVT VecVT = Vec.getSimpleValueType();
5404 
5405       // First extract the lower XLEN bits of the element.
5406       SDValue EltLo = DAG.getNode(RISCVISD::VMV_X_S, DL, XLenVT, Vec);
5407 
5408       // To extract the upper XLEN bits of the vector element, shift the first
5409       // element right by 32 bits and re-extract the lower XLEN bits.
5410       SDValue VL = DAG.getConstant(1, DL, XLenVT);
5411       MVT MaskVT = MVT::getVectorVT(MVT::i1, VecVT.getVectorElementCount());
5412       SDValue Mask = DAG.getNode(RISCVISD::VMSET_VL, DL, MaskVT, VL);
5413       SDValue ThirtyTwoV = DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VecVT,
5414                                        DAG.getConstant(32, DL, XLenVT), VL);
5415       SDValue LShr32 =
5416           DAG.getNode(RISCVISD::SRL_VL, DL, VecVT, Vec, ThirtyTwoV, Mask, VL);
5417       SDValue EltHi = DAG.getNode(RISCVISD::VMV_X_S, DL, XLenVT, LShr32);
5418 
5419       Results.push_back(
5420           DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, EltLo, EltHi));
5421       break;
5422     }
5423     }
5424     break;
5425   }
5426   case ISD::VECREDUCE_ADD:
5427   case ISD::VECREDUCE_AND:
5428   case ISD::VECREDUCE_OR:
5429   case ISD::VECREDUCE_XOR:
5430   case ISD::VECREDUCE_SMAX:
5431   case ISD::VECREDUCE_UMAX:
5432   case ISD::VECREDUCE_SMIN:
5433   case ISD::VECREDUCE_UMIN:
5434     if (SDValue V = lowerVECREDUCE(SDValue(N, 0), DAG))
5435       Results.push_back(V);
5436     break;
5437   case ISD::FLT_ROUNDS_: {
5438     SDVTList VTs = DAG.getVTList(Subtarget.getXLenVT(), MVT::Other);
5439     SDValue Res = DAG.getNode(ISD::FLT_ROUNDS_, DL, VTs, N->getOperand(0));
5440     Results.push_back(Res.getValue(0));
5441     Results.push_back(Res.getValue(1));
5442     break;
5443   }
5444   }
5445 }
5446 
5447 // A structure to hold one of the bit-manipulation patterns below. Together, a
5448 // SHL and non-SHL pattern may form a bit-manipulation pair on a single source:
5449 //   (or (and (shl x, 1), 0xAAAAAAAA),
5450 //       (and (srl x, 1), 0x55555555))
5451 struct RISCVBitmanipPat {
5452   SDValue Op;
5453   unsigned ShAmt;
5454   bool IsSHL;
5455 
5456   bool formsPairWith(const RISCVBitmanipPat &Other) const {
5457     return Op == Other.Op && ShAmt == Other.ShAmt && IsSHL != Other.IsSHL;
5458   }
5459 };
5460 
5461 // Matches patterns of the form
5462 //   (and (shl x, C2), (C1 << C2))
5463 //   (and (srl x, C2), C1)
5464 //   (shl (and x, C1), C2)
5465 //   (srl (and x, (C1 << C2)), C2)
5466 // Where C2 is a power of 2 and C1 has at least that many leading zeroes.
5467 // The expected masks for each shift amount are specified in BitmanipMasks where
5468 // BitmanipMasks[log2(C2)] specifies the expected C1 value.
5469 // The max allowed shift amount is either XLen/2 or XLen/4 determined by whether
5470 // BitmanipMasks contains 6 or 5 entries assuming that the maximum possible
5471 // XLen is 64.
5472 static Optional<RISCVBitmanipPat>
5473 matchRISCVBitmanipPat(SDValue Op, ArrayRef<uint64_t> BitmanipMasks) {
5474   assert((BitmanipMasks.size() == 5 || BitmanipMasks.size() == 6) &&
5475          "Unexpected number of masks");
5476   Optional<uint64_t> Mask;
5477   // Optionally consume a mask around the shift operation.
5478   if (Op.getOpcode() == ISD::AND && isa<ConstantSDNode>(Op.getOperand(1))) {
5479     Mask = Op.getConstantOperandVal(1);
5480     Op = Op.getOperand(0);
5481   }
5482   if (Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRL)
5483     return None;
5484   bool IsSHL = Op.getOpcode() == ISD::SHL;
5485 
5486   if (!isa<ConstantSDNode>(Op.getOperand(1)))
5487     return None;
5488   uint64_t ShAmt = Op.getConstantOperandVal(1);
5489 
5490   unsigned Width = Op.getValueType() == MVT::i64 ? 64 : 32;
5491   if (ShAmt >= Width || !isPowerOf2_64(ShAmt))
5492     return None;
5493   // If we don't have enough masks for 64 bit, then we must be trying to
5494   // match SHFL so we're only allowed to shift 1/4 of the width.
5495   if (BitmanipMasks.size() == 5 && ShAmt >= (Width / 2))
5496     return None;
5497 
5498   SDValue Src = Op.getOperand(0);
5499 
5500   // The expected mask is shifted left when the AND is found around SHL
5501   // patterns.
5502   //   ((x >> 1) & 0x55555555)
5503   //   ((x << 1) & 0xAAAAAAAA)
5504   bool SHLExpMask = IsSHL;
5505 
5506   if (!Mask) {
5507     // Sometimes LLVM keeps the mask as an operand of the shift, typically when
5508     // the mask is all ones: consume that now.
5509     if (Src.getOpcode() == ISD::AND && isa<ConstantSDNode>(Src.getOperand(1))) {
5510       Mask = Src.getConstantOperandVal(1);
5511       Src = Src.getOperand(0);
5512       // The expected mask is now in fact shifted left for SRL, so reverse the
5513       // decision.
5514       //   ((x & 0xAAAAAAAA) >> 1)
5515       //   ((x & 0x55555555) << 1)
5516       SHLExpMask = !SHLExpMask;
5517     } else {
5518       // Use a default shifted mask of all-ones if there's no AND, truncated
5519       // down to the expected width. This simplifies the logic later on.
5520       Mask = maskTrailingOnes<uint64_t>(Width);
5521       *Mask &= (IsSHL ? *Mask << ShAmt : *Mask >> ShAmt);
5522     }
5523   }
5524 
5525   unsigned MaskIdx = Log2_32(ShAmt);
5526   uint64_t ExpMask = BitmanipMasks[MaskIdx] & maskTrailingOnes<uint64_t>(Width);
5527 
5528   if (SHLExpMask)
5529     ExpMask <<= ShAmt;
5530 
5531   if (Mask != ExpMask)
5532     return None;
5533 
5534   return RISCVBitmanipPat{Src, (unsigned)ShAmt, IsSHL};
5535 }
5536 
5537 // Matches any of the following bit-manipulation patterns:
5538 //   (and (shl x, 1), (0x55555555 << 1))
5539 //   (and (srl x, 1), 0x55555555)
5540 //   (shl (and x, 0x55555555), 1)
5541 //   (srl (and x, (0x55555555 << 1)), 1)
5542 // where the shift amount and mask may vary thus:
5543 //   [1]  = 0x55555555 / 0xAAAAAAAA
5544 //   [2]  = 0x33333333 / 0xCCCCCCCC
5545 //   [4]  = 0x0F0F0F0F / 0xF0F0F0F0
5546 //   [8]  = 0x00FF00FF / 0xFF00FF00
5547 //   [16] = 0x0000FFFF / 0xFFFFFFFF
5548 //   [32] = 0x00000000FFFFFFFF / 0xFFFFFFFF00000000 (for RV64)
5549 static Optional<RISCVBitmanipPat> matchGREVIPat(SDValue Op) {
5550   // These are the unshifted masks which we use to match bit-manipulation
5551   // patterns. They may be shifted left in certain circumstances.
5552   static const uint64_t BitmanipMasks[] = {
5553       0x5555555555555555ULL, 0x3333333333333333ULL, 0x0F0F0F0F0F0F0F0FULL,
5554       0x00FF00FF00FF00FFULL, 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL};
5555 
5556   return matchRISCVBitmanipPat(Op, BitmanipMasks);
5557 }
5558 
5559 // Match the following pattern as a GREVI(W) operation
5560 //   (or (BITMANIP_SHL x), (BITMANIP_SRL x))
5561 static SDValue combineORToGREV(SDValue Op, SelectionDAG &DAG,
5562                                const RISCVSubtarget &Subtarget) {
5563   assert(Subtarget.hasStdExtZbp() && "Expected Zbp extenson");
5564   EVT VT = Op.getValueType();
5565 
5566   if (VT == Subtarget.getXLenVT() || (Subtarget.is64Bit() && VT == MVT::i32)) {
5567     auto LHS = matchGREVIPat(Op.getOperand(0));
5568     auto RHS = matchGREVIPat(Op.getOperand(1));
5569     if (LHS && RHS && LHS->formsPairWith(*RHS)) {
5570       SDLoc DL(Op);
5571       return DAG.getNode(RISCVISD::GREV, DL, VT, LHS->Op,
5572                          DAG.getConstant(LHS->ShAmt, DL, VT));
5573     }
5574   }
5575   return SDValue();
5576 }
5577 
5578 // Matches any the following pattern as a GORCI(W) operation
5579 // 1.  (or (GREVI x, shamt), x) if shamt is a power of 2
5580 // 2.  (or x, (GREVI x, shamt)) if shamt is a power of 2
5581 // 3.  (or (or (BITMANIP_SHL x), x), (BITMANIP_SRL x))
5582 // Note that with the variant of 3.,
5583 //     (or (or (BITMANIP_SHL x), (BITMANIP_SRL x)), x)
5584 // the inner pattern will first be matched as GREVI and then the outer
5585 // pattern will be matched to GORC via the first rule above.
5586 // 4.  (or (rotl/rotr x, bitwidth/2), x)
5587 static SDValue combineORToGORC(SDValue Op, SelectionDAG &DAG,
5588                                const RISCVSubtarget &Subtarget) {
5589   assert(Subtarget.hasStdExtZbp() && "Expected Zbp extenson");
5590   EVT VT = Op.getValueType();
5591 
5592   if (VT == Subtarget.getXLenVT() || (Subtarget.is64Bit() && VT == MVT::i32)) {
5593     SDLoc DL(Op);
5594     SDValue Op0 = Op.getOperand(0);
5595     SDValue Op1 = Op.getOperand(1);
5596 
5597     auto MatchOROfReverse = [&](SDValue Reverse, SDValue X) {
5598       if (Reverse.getOpcode() == RISCVISD::GREV && Reverse.getOperand(0) == X &&
5599           isa<ConstantSDNode>(Reverse.getOperand(1)) &&
5600           isPowerOf2_32(Reverse.getConstantOperandVal(1)))
5601         return DAG.getNode(RISCVISD::GORC, DL, VT, X, Reverse.getOperand(1));
5602       // We can also form GORCI from ROTL/ROTR by half the bitwidth.
5603       if ((Reverse.getOpcode() == ISD::ROTL ||
5604            Reverse.getOpcode() == ISD::ROTR) &&
5605           Reverse.getOperand(0) == X &&
5606           isa<ConstantSDNode>(Reverse.getOperand(1))) {
5607         uint64_t RotAmt = Reverse.getConstantOperandVal(1);
5608         if (RotAmt == (VT.getSizeInBits() / 2))
5609           return DAG.getNode(RISCVISD::GORC, DL, VT, X,
5610                              DAG.getConstant(RotAmt, DL, VT));
5611       }
5612       return SDValue();
5613     };
5614 
5615     // Check for either commutable permutation of (or (GREVI x, shamt), x)
5616     if (SDValue V = MatchOROfReverse(Op0, Op1))
5617       return V;
5618     if (SDValue V = MatchOROfReverse(Op1, Op0))
5619       return V;
5620 
5621     // OR is commutable so canonicalize its OR operand to the left
5622     if (Op0.getOpcode() != ISD::OR && Op1.getOpcode() == ISD::OR)
5623       std::swap(Op0, Op1);
5624     if (Op0.getOpcode() != ISD::OR)
5625       return SDValue();
5626     SDValue OrOp0 = Op0.getOperand(0);
5627     SDValue OrOp1 = Op0.getOperand(1);
5628     auto LHS = matchGREVIPat(OrOp0);
5629     // OR is commutable so swap the operands and try again: x might have been
5630     // on the left
5631     if (!LHS) {
5632       std::swap(OrOp0, OrOp1);
5633       LHS = matchGREVIPat(OrOp0);
5634     }
5635     auto RHS = matchGREVIPat(Op1);
5636     if (LHS && RHS && LHS->formsPairWith(*RHS) && LHS->Op == OrOp1) {
5637       return DAG.getNode(RISCVISD::GORC, DL, VT, LHS->Op,
5638                          DAG.getConstant(LHS->ShAmt, DL, VT));
5639     }
5640   }
5641   return SDValue();
5642 }
5643 
5644 // Matches any of the following bit-manipulation patterns:
5645 //   (and (shl x, 1), (0x22222222 << 1))
5646 //   (and (srl x, 1), 0x22222222)
5647 //   (shl (and x, 0x22222222), 1)
5648 //   (srl (and x, (0x22222222 << 1)), 1)
5649 // where the shift amount and mask may vary thus:
5650 //   [1]  = 0x22222222 / 0x44444444
5651 //   [2]  = 0x0C0C0C0C / 0x3C3C3C3C
5652 //   [4]  = 0x00F000F0 / 0x0F000F00
5653 //   [8]  = 0x0000FF00 / 0x00FF0000
5654 //   [16] = 0x00000000FFFF0000 / 0x0000FFFF00000000 (for RV64)
5655 static Optional<RISCVBitmanipPat> matchSHFLPat(SDValue Op) {
5656   // These are the unshifted masks which we use to match bit-manipulation
5657   // patterns. They may be shifted left in certain circumstances.
5658   static const uint64_t BitmanipMasks[] = {
5659       0x2222222222222222ULL, 0x0C0C0C0C0C0C0C0CULL, 0x00F000F000F000F0ULL,
5660       0x0000FF000000FF00ULL, 0x00000000FFFF0000ULL};
5661 
5662   return matchRISCVBitmanipPat(Op, BitmanipMasks);
5663 }
5664 
5665 // Match (or (or (SHFL_SHL x), (SHFL_SHR x)), (SHFL_AND x)
5666 static SDValue combineORToSHFL(SDValue Op, SelectionDAG &DAG,
5667                                const RISCVSubtarget &Subtarget) {
5668   assert(Subtarget.hasStdExtZbp() && "Expected Zbp extenson");
5669   EVT VT = Op.getValueType();
5670 
5671   if (VT != MVT::i32 && VT != Subtarget.getXLenVT())
5672     return SDValue();
5673 
5674   SDValue Op0 = Op.getOperand(0);
5675   SDValue Op1 = Op.getOperand(1);
5676 
5677   // Or is commutable so canonicalize the second OR to the LHS.
5678   if (Op0.getOpcode() != ISD::OR)
5679     std::swap(Op0, Op1);
5680   if (Op0.getOpcode() != ISD::OR)
5681     return SDValue();
5682 
5683   // We found an inner OR, so our operands are the operands of the inner OR
5684   // and the other operand of the outer OR.
5685   SDValue A = Op0.getOperand(0);
5686   SDValue B = Op0.getOperand(1);
5687   SDValue C = Op1;
5688 
5689   auto Match1 = matchSHFLPat(A);
5690   auto Match2 = matchSHFLPat(B);
5691 
5692   // If neither matched, we failed.
5693   if (!Match1 && !Match2)
5694     return SDValue();
5695 
5696   // We had at least one match. if one failed, try the remaining C operand.
5697   if (!Match1) {
5698     std::swap(A, C);
5699     Match1 = matchSHFLPat(A);
5700     if (!Match1)
5701       return SDValue();
5702   } else if (!Match2) {
5703     std::swap(B, C);
5704     Match2 = matchSHFLPat(B);
5705     if (!Match2)
5706       return SDValue();
5707   }
5708   assert(Match1 && Match2);
5709 
5710   // Make sure our matches pair up.
5711   if (!Match1->formsPairWith(*Match2))
5712     return SDValue();
5713 
5714   // All the remains is to make sure C is an AND with the same input, that masks
5715   // out the bits that are being shuffled.
5716   if (C.getOpcode() != ISD::AND || !isa<ConstantSDNode>(C.getOperand(1)) ||
5717       C.getOperand(0) != Match1->Op)
5718     return SDValue();
5719 
5720   uint64_t Mask = C.getConstantOperandVal(1);
5721 
5722   static const uint64_t BitmanipMasks[] = {
5723       0x9999999999999999ULL, 0xC3C3C3C3C3C3C3C3ULL, 0xF00FF00FF00FF00FULL,
5724       0xFF0000FFFF0000FFULL, 0xFFFF00000000FFFFULL,
5725   };
5726 
5727   unsigned Width = Op.getValueType() == MVT::i64 ? 64 : 32;
5728   unsigned MaskIdx = Log2_32(Match1->ShAmt);
5729   uint64_t ExpMask = BitmanipMasks[MaskIdx] & maskTrailingOnes<uint64_t>(Width);
5730 
5731   if (Mask != ExpMask)
5732     return SDValue();
5733 
5734   SDLoc DL(Op);
5735   return DAG.getNode(RISCVISD::SHFL, DL, VT, Match1->Op,
5736                      DAG.getConstant(Match1->ShAmt, DL, VT));
5737 }
5738 
5739 // Combine (GREVI (GREVI x, C2), C1) -> (GREVI x, C1^C2) when C1^C2 is
5740 // non-zero, and to x when it is. Any repeated GREVI stage undoes itself.
5741 // Combine (GORCI (GORCI x, C2), C1) -> (GORCI x, C1|C2). Repeated stage does
5742 // not undo itself, but they are redundant.
5743 static SDValue combineGREVI_GORCI(SDNode *N, SelectionDAG &DAG) {
5744   SDValue Src = N->getOperand(0);
5745 
5746   if (Src.getOpcode() != N->getOpcode())
5747     return SDValue();
5748 
5749   if (!isa<ConstantSDNode>(N->getOperand(1)) ||
5750       !isa<ConstantSDNode>(Src.getOperand(1)))
5751     return SDValue();
5752 
5753   unsigned ShAmt1 = N->getConstantOperandVal(1);
5754   unsigned ShAmt2 = Src.getConstantOperandVal(1);
5755   Src = Src.getOperand(0);
5756 
5757   unsigned CombinedShAmt;
5758   if (N->getOpcode() == RISCVISD::GORC || N->getOpcode() == RISCVISD::GORCW)
5759     CombinedShAmt = ShAmt1 | ShAmt2;
5760   else
5761     CombinedShAmt = ShAmt1 ^ ShAmt2;
5762 
5763   if (CombinedShAmt == 0)
5764     return Src;
5765 
5766   SDLoc DL(N);
5767   return DAG.getNode(
5768       N->getOpcode(), DL, N->getValueType(0), Src,
5769       DAG.getConstant(CombinedShAmt, DL, N->getOperand(1).getValueType()));
5770 }
5771 
5772 // Combine a constant select operand into its use:
5773 //
5774 // (and (select cond, -1, c), x)
5775 //   -> (select cond, x, (and x, c))  [AllOnes=1]
5776 // (or  (select cond, 0, c), x)
5777 //   -> (select cond, x, (or x, c))  [AllOnes=0]
5778 // (xor (select cond, 0, c), x)
5779 //   -> (select cond, x, (xor x, c))  [AllOnes=0]
5780 // (add (select cond, 0, c), x)
5781 //   -> (select cond, x, (add x, c))  [AllOnes=0]
5782 // (sub x, (select cond, 0, c))
5783 //   -> (select cond, x, (sub x, c))  [AllOnes=0]
5784 static SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5785                                    SelectionDAG &DAG, bool AllOnes) {
5786   EVT VT = N->getValueType(0);
5787 
5788   // Skip vectors.
5789   if (VT.isVector())
5790     return SDValue();
5791 
5792   if ((Slct.getOpcode() != ISD::SELECT &&
5793        Slct.getOpcode() != RISCVISD::SELECT_CC) ||
5794       !Slct.hasOneUse())
5795     return SDValue();
5796 
5797   auto isZeroOrAllOnes = [](SDValue N, bool AllOnes) {
5798     return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
5799   };
5800 
5801   bool SwapSelectOps;
5802   unsigned OpOffset = Slct.getOpcode() == RISCVISD::SELECT_CC ? 2 : 0;
5803   SDValue TrueVal = Slct.getOperand(1 + OpOffset);
5804   SDValue FalseVal = Slct.getOperand(2 + OpOffset);
5805   SDValue NonConstantVal;
5806   if (isZeroOrAllOnes(TrueVal, AllOnes)) {
5807     SwapSelectOps = false;
5808     NonConstantVal = FalseVal;
5809   } else if (isZeroOrAllOnes(FalseVal, AllOnes)) {
5810     SwapSelectOps = true;
5811     NonConstantVal = TrueVal;
5812   } else
5813     return SDValue();
5814 
5815   // Slct is now know to be the desired identity constant when CC is true.
5816   TrueVal = OtherOp;
5817   FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT, OtherOp, NonConstantVal);
5818   // Unless SwapSelectOps says the condition should be false.
5819   if (SwapSelectOps)
5820     std::swap(TrueVal, FalseVal);
5821 
5822   if (Slct.getOpcode() == RISCVISD::SELECT_CC)
5823     return DAG.getNode(RISCVISD::SELECT_CC, SDLoc(N), VT,
5824                        {Slct.getOperand(0), Slct.getOperand(1),
5825                         Slct.getOperand(2), TrueVal, FalseVal});
5826 
5827   return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
5828                      {Slct.getOperand(0), TrueVal, FalseVal});
5829 }
5830 
5831 // Attempt combineSelectAndUse on each operand of a commutative operator N.
5832 static SDValue combineSelectAndUseCommutative(SDNode *N, SelectionDAG &DAG,
5833                                               bool AllOnes) {
5834   SDValue N0 = N->getOperand(0);
5835   SDValue N1 = N->getOperand(1);
5836   if (SDValue Result = combineSelectAndUse(N, N0, N1, DAG, AllOnes))
5837     return Result;
5838   if (SDValue Result = combineSelectAndUse(N, N1, N0, DAG, AllOnes))
5839     return Result;
5840   return SDValue();
5841 }
5842 
5843 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG) {
5844   // fold (add (select lhs, rhs, cc, 0, y), x) ->
5845   //      (select lhs, rhs, cc, x, (add x, y))
5846   return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false);
5847 }
5848 
5849 static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG) {
5850   // fold (sub x, (select lhs, rhs, cc, 0, y)) ->
5851   //      (select lhs, rhs, cc, x, (sub x, y))
5852   SDValue N0 = N->getOperand(0);
5853   SDValue N1 = N->getOperand(1);
5854   return combineSelectAndUse(N, N1, N0, DAG, /*AllOnes*/ false);
5855 }
5856 
5857 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG) {
5858   // fold (and (select lhs, rhs, cc, -1, y), x) ->
5859   //      (select lhs, rhs, cc, x, (and x, y))
5860   return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ true);
5861 }
5862 
5863 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
5864                                 const RISCVSubtarget &Subtarget) {
5865   if (Subtarget.hasStdExtZbp()) {
5866     if (auto GREV = combineORToGREV(SDValue(N, 0), DAG, Subtarget))
5867       return GREV;
5868     if (auto GORC = combineORToGORC(SDValue(N, 0), DAG, Subtarget))
5869       return GORC;
5870     if (auto SHFL = combineORToSHFL(SDValue(N, 0), DAG, Subtarget))
5871       return SHFL;
5872   }
5873 
5874   // fold (or (select cond, 0, y), x) ->
5875   //      (select cond, x, (or x, y))
5876   return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false);
5877 }
5878 
5879 static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG) {
5880   // fold (xor (select cond, 0, y), x) ->
5881   //      (select cond, x, (xor x, y))
5882   return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false);
5883 }
5884 
5885 // Attempt to turn ANY_EXTEND into SIGN_EXTEND if the input to the ANY_EXTEND
5886 // has users that require SIGN_EXTEND and the SIGN_EXTEND can be done for free
5887 // by an instruction like ADDW/SUBW/MULW. Without this the ANY_EXTEND would be
5888 // removed during type legalization leaving an ADD/SUB/MUL use that won't use
5889 // ADDW/SUBW/MULW.
5890 static SDValue performANY_EXTENDCombine(SDNode *N,
5891                                         TargetLowering::DAGCombinerInfo &DCI,
5892                                         const RISCVSubtarget &Subtarget) {
5893   if (!Subtarget.is64Bit())
5894     return SDValue();
5895 
5896   SelectionDAG &DAG = DCI.DAG;
5897 
5898   SDValue Src = N->getOperand(0);
5899   EVT VT = N->getValueType(0);
5900   if (VT != MVT::i64 || Src.getValueType() != MVT::i32)
5901     return SDValue();
5902 
5903   // The opcode must be one that can implicitly sign_extend.
5904   // FIXME: Additional opcodes.
5905   switch (Src.getOpcode()) {
5906   default:
5907     return SDValue();
5908   case ISD::MUL:
5909     if (!Subtarget.hasStdExtM())
5910       return SDValue();
5911     LLVM_FALLTHROUGH;
5912   case ISD::ADD:
5913   case ISD::SUB:
5914     break;
5915   }
5916 
5917   // Only handle cases where the result is used by a CopyToReg. That likely
5918   // means the value is a liveout of the basic block. This helps prevent
5919   // infinite combine loops like PR51206.
5920   if (none_of(N->uses(),
5921               [](SDNode *User) { return User->getOpcode() == ISD::CopyToReg; }))
5922     return SDValue();
5923 
5924   SmallVector<SDNode *, 4> SetCCs;
5925   for (SDNode::use_iterator UI = Src.getNode()->use_begin(),
5926                             UE = Src.getNode()->use_end();
5927        UI != UE; ++UI) {
5928     SDNode *User = *UI;
5929     if (User == N)
5930       continue;
5931     if (UI.getUse().getResNo() != Src.getResNo())
5932       continue;
5933     // All i32 setccs are legalized by sign extending operands.
5934     if (User->getOpcode() == ISD::SETCC) {
5935       SetCCs.push_back(User);
5936       continue;
5937     }
5938     // We don't know if we can extend this user.
5939     break;
5940   }
5941 
5942   // If we don't have any SetCCs, this isn't worthwhile.
5943   if (SetCCs.empty())
5944     return SDValue();
5945 
5946   SDLoc DL(N);
5947   SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Src);
5948   DCI.CombineTo(N, SExt);
5949 
5950   // Promote all the setccs.
5951   for (SDNode *SetCC : SetCCs) {
5952     SmallVector<SDValue, 4> Ops;
5953 
5954     for (unsigned j = 0; j != 2; ++j) {
5955       SDValue SOp = SetCC->getOperand(j);
5956       if (SOp == Src)
5957         Ops.push_back(SExt);
5958       else
5959         Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, SOp));
5960     }
5961 
5962     Ops.push_back(SetCC->getOperand(2));
5963     DCI.CombineTo(SetCC,
5964                   DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0), Ops));
5965   }
5966   return SDValue(N, 0);
5967 }
5968 
5969 SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
5970                                                DAGCombinerInfo &DCI) const {
5971   SelectionDAG &DAG = DCI.DAG;
5972 
5973   switch (N->getOpcode()) {
5974   default:
5975     break;
5976   case RISCVISD::SplitF64: {
5977     SDValue Op0 = N->getOperand(0);
5978     // If the input to SplitF64 is just BuildPairF64 then the operation is
5979     // redundant. Instead, use BuildPairF64's operands directly.
5980     if (Op0->getOpcode() == RISCVISD::BuildPairF64)
5981       return DCI.CombineTo(N, Op0.getOperand(0), Op0.getOperand(1));
5982 
5983     SDLoc DL(N);
5984 
5985     // It's cheaper to materialise two 32-bit integers than to load a double
5986     // from the constant pool and transfer it to integer registers through the
5987     // stack.
5988     if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op0)) {
5989       APInt V = C->getValueAPF().bitcastToAPInt();
5990       SDValue Lo = DAG.getConstant(V.trunc(32), DL, MVT::i32);
5991       SDValue Hi = DAG.getConstant(V.lshr(32).trunc(32), DL, MVT::i32);
5992       return DCI.CombineTo(N, Lo, Hi);
5993     }
5994 
5995     // This is a target-specific version of a DAGCombine performed in
5996     // DAGCombiner::visitBITCAST. It performs the equivalent of:
5997     // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5998     // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
5999     if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
6000         !Op0.getNode()->hasOneUse())
6001       break;
6002     SDValue NewSplitF64 =
6003         DAG.getNode(RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32),
6004                     Op0.getOperand(0));
6005     SDValue Lo = NewSplitF64.getValue(0);
6006     SDValue Hi = NewSplitF64.getValue(1);
6007     APInt SignBit = APInt::getSignMask(32);
6008     if (Op0.getOpcode() == ISD::FNEG) {
6009       SDValue NewHi = DAG.getNode(ISD::XOR, DL, MVT::i32, Hi,
6010                                   DAG.getConstant(SignBit, DL, MVT::i32));
6011       return DCI.CombineTo(N, Lo, NewHi);
6012     }
6013     assert(Op0.getOpcode() == ISD::FABS);
6014     SDValue NewHi = DAG.getNode(ISD::AND, DL, MVT::i32, Hi,
6015                                 DAG.getConstant(~SignBit, DL, MVT::i32));
6016     return DCI.CombineTo(N, Lo, NewHi);
6017   }
6018   case RISCVISD::SLLW:
6019   case RISCVISD::SRAW:
6020   case RISCVISD::SRLW:
6021   case RISCVISD::ROLW:
6022   case RISCVISD::RORW: {
6023     // Only the lower 32 bits of LHS and lower 5 bits of RHS are read.
6024     SDValue LHS = N->getOperand(0);
6025     SDValue RHS = N->getOperand(1);
6026     APInt LHSMask = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 32);
6027     APInt RHSMask = APInt::getLowBitsSet(RHS.getValueSizeInBits(), 5);
6028     if (SimplifyDemandedBits(N->getOperand(0), LHSMask, DCI) ||
6029         SimplifyDemandedBits(N->getOperand(1), RHSMask, DCI)) {
6030       if (N->getOpcode() != ISD::DELETED_NODE)
6031         DCI.AddToWorklist(N);
6032       return SDValue(N, 0);
6033     }
6034     break;
6035   }
6036   case RISCVISD::CLZW:
6037   case RISCVISD::CTZW: {
6038     // Only the lower 32 bits of the first operand are read
6039     SDValue Op0 = N->getOperand(0);
6040     APInt Mask = APInt::getLowBitsSet(Op0.getValueSizeInBits(), 32);
6041     if (SimplifyDemandedBits(Op0, Mask, DCI)) {
6042       if (N->getOpcode() != ISD::DELETED_NODE)
6043         DCI.AddToWorklist(N);
6044       return SDValue(N, 0);
6045     }
6046     break;
6047   }
6048   case RISCVISD::FSL:
6049   case RISCVISD::FSR: {
6050     // Only the lower log2(Bitwidth)+1 bits of the the shift amount are read.
6051     SDValue ShAmt = N->getOperand(2);
6052     unsigned BitWidth = ShAmt.getValueSizeInBits();
6053     assert(isPowerOf2_32(BitWidth) && "Unexpected bit width");
6054     APInt ShAmtMask(BitWidth, (BitWidth * 2) - 1);
6055     if (SimplifyDemandedBits(ShAmt, ShAmtMask, DCI)) {
6056       if (N->getOpcode() != ISD::DELETED_NODE)
6057         DCI.AddToWorklist(N);
6058       return SDValue(N, 0);
6059     }
6060     break;
6061   }
6062   case RISCVISD::FSLW:
6063   case RISCVISD::FSRW: {
6064     // Only the lower 32 bits of Values and lower 6 bits of shift amount are
6065     // read.
6066     SDValue Op0 = N->getOperand(0);
6067     SDValue Op1 = N->getOperand(1);
6068     SDValue ShAmt = N->getOperand(2);
6069     APInt OpMask = APInt::getLowBitsSet(Op0.getValueSizeInBits(), 32);
6070     APInt ShAmtMask = APInt::getLowBitsSet(ShAmt.getValueSizeInBits(), 6);
6071     if (SimplifyDemandedBits(Op0, OpMask, DCI) ||
6072         SimplifyDemandedBits(Op1, OpMask, DCI) ||
6073         SimplifyDemandedBits(ShAmt, ShAmtMask, DCI)) {
6074       if (N->getOpcode() != ISD::DELETED_NODE)
6075         DCI.AddToWorklist(N);
6076       return SDValue(N, 0);
6077     }
6078     break;
6079   }
6080   case RISCVISD::GREV:
6081   case RISCVISD::GORC: {
6082     // Only the lower log2(Bitwidth) bits of the the shift amount are read.
6083     SDValue ShAmt = N->getOperand(1);
6084     unsigned BitWidth = ShAmt.getValueSizeInBits();
6085     assert(isPowerOf2_32(BitWidth) && "Unexpected bit width");
6086     APInt ShAmtMask(BitWidth, BitWidth - 1);
6087     if (SimplifyDemandedBits(ShAmt, ShAmtMask, DCI)) {
6088       if (N->getOpcode() != ISD::DELETED_NODE)
6089         DCI.AddToWorklist(N);
6090       return SDValue(N, 0);
6091     }
6092 
6093     return combineGREVI_GORCI(N, DCI.DAG);
6094   }
6095   case RISCVISD::GREVW:
6096   case RISCVISD::GORCW: {
6097     // Only the lower 32 bits of LHS and lower 5 bits of RHS are read.
6098     SDValue LHS = N->getOperand(0);
6099     SDValue RHS = N->getOperand(1);
6100     APInt LHSMask = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 32);
6101     APInt RHSMask = APInt::getLowBitsSet(RHS.getValueSizeInBits(), 5);
6102     if (SimplifyDemandedBits(LHS, LHSMask, DCI) ||
6103         SimplifyDemandedBits(RHS, RHSMask, DCI)) {
6104       if (N->getOpcode() != ISD::DELETED_NODE)
6105         DCI.AddToWorklist(N);
6106       return SDValue(N, 0);
6107     }
6108 
6109     return combineGREVI_GORCI(N, DCI.DAG);
6110   }
6111   case RISCVISD::SHFL:
6112   case RISCVISD::UNSHFL: {
6113     // Only the lower log2(Bitwidth) bits of the the shift amount are read.
6114     SDValue ShAmt = N->getOperand(1);
6115     unsigned BitWidth = ShAmt.getValueSizeInBits();
6116     assert(isPowerOf2_32(BitWidth) && "Unexpected bit width");
6117     APInt ShAmtMask(BitWidth, (BitWidth / 2) - 1);
6118     if (SimplifyDemandedBits(ShAmt, ShAmtMask, DCI)) {
6119       if (N->getOpcode() != ISD::DELETED_NODE)
6120         DCI.AddToWorklist(N);
6121       return SDValue(N, 0);
6122     }
6123 
6124     break;
6125   }
6126   case RISCVISD::SHFLW:
6127   case RISCVISD::UNSHFLW: {
6128     // Only the lower 32 bits of LHS and lower 5 bits of RHS are read.
6129     SDValue LHS = N->getOperand(0);
6130     SDValue RHS = N->getOperand(1);
6131     APInt LHSMask = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 32);
6132     APInt RHSMask = APInt::getLowBitsSet(RHS.getValueSizeInBits(), 4);
6133     if (SimplifyDemandedBits(LHS, LHSMask, DCI) ||
6134         SimplifyDemandedBits(RHS, RHSMask, DCI)) {
6135       if (N->getOpcode() != ISD::DELETED_NODE)
6136         DCI.AddToWorklist(N);
6137       return SDValue(N, 0);
6138     }
6139 
6140     break;
6141   }
6142   case RISCVISD::BCOMPRESSW:
6143   case RISCVISD::BDECOMPRESSW: {
6144     // Only the lower 32 bits of LHS and RHS are read.
6145     SDValue LHS = N->getOperand(0);
6146     SDValue RHS = N->getOperand(1);
6147     APInt Mask = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 32);
6148     if (SimplifyDemandedBits(LHS, Mask, DCI) ||
6149         SimplifyDemandedBits(RHS, Mask, DCI)) {
6150       if (N->getOpcode() != ISD::DELETED_NODE)
6151         DCI.AddToWorklist(N);
6152       return SDValue(N, 0);
6153     }
6154 
6155     break;
6156   }
6157   case RISCVISD::FMV_X_ANYEXTH:
6158   case RISCVISD::FMV_X_ANYEXTW_RV64: {
6159     SDLoc DL(N);
6160     SDValue Op0 = N->getOperand(0);
6161     MVT VT = N->getSimpleValueType(0);
6162     // If the input to FMV_X_ANYEXTW_RV64 is just FMV_W_X_RV64 then the
6163     // conversion is unnecessary and can be replaced with the FMV_W_X_RV64
6164     // operand. Similar for FMV_X_ANYEXTH and FMV_H_X.
6165     if ((N->getOpcode() == RISCVISD::FMV_X_ANYEXTW_RV64 &&
6166          Op0->getOpcode() == RISCVISD::FMV_W_X_RV64) ||
6167         (N->getOpcode() == RISCVISD::FMV_X_ANYEXTH &&
6168          Op0->getOpcode() == RISCVISD::FMV_H_X)) {
6169       assert(Op0.getOperand(0).getValueType() == VT &&
6170              "Unexpected value type!");
6171       return Op0.getOperand(0);
6172     }
6173 
6174     // This is a target-specific version of a DAGCombine performed in
6175     // DAGCombiner::visitBITCAST. It performs the equivalent of:
6176     // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
6177     // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
6178     if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
6179         !Op0.getNode()->hasOneUse())
6180       break;
6181     SDValue NewFMV = DAG.getNode(N->getOpcode(), DL, VT, Op0.getOperand(0));
6182     unsigned FPBits = N->getOpcode() == RISCVISD::FMV_X_ANYEXTW_RV64 ? 32 : 16;
6183     APInt SignBit = APInt::getSignMask(FPBits).sextOrSelf(VT.getSizeInBits());
6184     if (Op0.getOpcode() == ISD::FNEG)
6185       return DAG.getNode(ISD::XOR, DL, VT, NewFMV,
6186                          DAG.getConstant(SignBit, DL, VT));
6187 
6188     assert(Op0.getOpcode() == ISD::FABS);
6189     return DAG.getNode(ISD::AND, DL, VT, NewFMV,
6190                        DAG.getConstant(~SignBit, DL, VT));
6191   }
6192   case ISD::ADD:
6193     return performADDCombine(N, DAG);
6194   case ISD::SUB:
6195     return performSUBCombine(N, DAG);
6196   case ISD::AND:
6197     return performANDCombine(N, DAG);
6198   case ISD::OR:
6199     return performORCombine(N, DAG, Subtarget);
6200   case ISD::XOR:
6201     return performXORCombine(N, DAG);
6202   case ISD::ANY_EXTEND:
6203     return performANY_EXTENDCombine(N, DCI, Subtarget);
6204   case ISD::ZERO_EXTEND:
6205     // Fold (zero_extend (fp_to_uint X)) to prevent forming fcvt+zexti32 during
6206     // type legalization. This is safe because fp_to_uint produces poison if
6207     // it overflows.
6208     if (N->getValueType(0) == MVT::i64 && Subtarget.is64Bit() &&
6209         N->getOperand(0).getOpcode() == ISD::FP_TO_UINT &&
6210         isTypeLegal(N->getOperand(0).getOperand(0).getValueType()))
6211       return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), MVT::i64,
6212                          N->getOperand(0).getOperand(0));
6213     return SDValue();
6214   case RISCVISD::SELECT_CC: {
6215     // Transform
6216     SDValue LHS = N->getOperand(0);
6217     SDValue RHS = N->getOperand(1);
6218     ISD::CondCode CCVal = cast<CondCodeSDNode>(N->getOperand(2))->get();
6219     if (!ISD::isIntEqualitySetCC(CCVal))
6220       break;
6221 
6222     // Fold (select_cc (setlt X, Y), 0, ne, trueV, falseV) ->
6223     //      (select_cc X, Y, lt, trueV, falseV)
6224     // Sometimes the setcc is introduced after select_cc has been formed.
6225     if (LHS.getOpcode() == ISD::SETCC && isNullConstant(RHS) &&
6226         LHS.getOperand(0).getValueType() == Subtarget.getXLenVT()) {
6227       // If we're looking for eq 0 instead of ne 0, we need to invert the
6228       // condition.
6229       bool Invert = CCVal == ISD::SETEQ;
6230       CCVal = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
6231       if (Invert)
6232         CCVal = ISD::getSetCCInverse(CCVal, LHS.getValueType());
6233 
6234       SDLoc DL(N);
6235       RHS = LHS.getOperand(1);
6236       LHS = LHS.getOperand(0);
6237       translateSetCCForBranch(DL, LHS, RHS, CCVal, DAG);
6238 
6239       SDValue TargetCC = DAG.getCondCode(CCVal);
6240       return DAG.getNode(
6241           RISCVISD::SELECT_CC, DL, N->getValueType(0),
6242           {LHS, RHS, TargetCC, N->getOperand(3), N->getOperand(4)});
6243     }
6244 
6245     // Fold (select_cc (xor X, Y), 0, eq/ne, trueV, falseV) ->
6246     //      (select_cc X, Y, eq/ne, trueV, falseV)
6247     if (LHS.getOpcode() == ISD::XOR && isNullConstant(RHS))
6248       return DAG.getNode(RISCVISD::SELECT_CC, SDLoc(N), N->getValueType(0),
6249                          {LHS.getOperand(0), LHS.getOperand(1),
6250                           N->getOperand(2), N->getOperand(3),
6251                           N->getOperand(4)});
6252     // (select_cc X, 1, setne, trueV, falseV) ->
6253     // (select_cc X, 0, seteq, trueV, falseV) if we can prove X is 0/1.
6254     // This can occur when legalizing some floating point comparisons.
6255     APInt Mask = APInt::getBitsSetFrom(LHS.getValueSizeInBits(), 1);
6256     if (isOneConstant(RHS) && DAG.MaskedValueIsZero(LHS, Mask)) {
6257       SDLoc DL(N);
6258       CCVal = ISD::getSetCCInverse(CCVal, LHS.getValueType());
6259       SDValue TargetCC = DAG.getCondCode(CCVal);
6260       RHS = DAG.getConstant(0, DL, LHS.getValueType());
6261       return DAG.getNode(
6262           RISCVISD::SELECT_CC, DL, N->getValueType(0),
6263           {LHS, RHS, TargetCC, N->getOperand(3), N->getOperand(4)});
6264     }
6265 
6266     break;
6267   }
6268   case RISCVISD::BR_CC: {
6269     SDValue LHS = N->getOperand(1);
6270     SDValue RHS = N->getOperand(2);
6271     ISD::CondCode CCVal = cast<CondCodeSDNode>(N->getOperand(3))->get();
6272     if (!ISD::isIntEqualitySetCC(CCVal))
6273       break;
6274 
6275     // Fold (br_cc (setlt X, Y), 0, ne, dest) ->
6276     //      (br_cc X, Y, lt, dest)
6277     // Sometimes the setcc is introduced after br_cc has been formed.
6278     if (LHS.getOpcode() == ISD::SETCC && isNullConstant(RHS) &&
6279         LHS.getOperand(0).getValueType() == Subtarget.getXLenVT()) {
6280       // If we're looking for eq 0 instead of ne 0, we need to invert the
6281       // condition.
6282       bool Invert = CCVal == ISD::SETEQ;
6283       CCVal = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
6284       if (Invert)
6285         CCVal = ISD::getSetCCInverse(CCVal, LHS.getValueType());
6286 
6287       SDLoc DL(N);
6288       RHS = LHS.getOperand(1);
6289       LHS = LHS.getOperand(0);
6290       translateSetCCForBranch(DL, LHS, RHS, CCVal, DAG);
6291 
6292       return DAG.getNode(RISCVISD::BR_CC, DL, N->getValueType(0),
6293                          N->getOperand(0), LHS, RHS, DAG.getCondCode(CCVal),
6294                          N->getOperand(4));
6295     }
6296 
6297     // Fold (br_cc (xor X, Y), 0, eq/ne, dest) ->
6298     //      (br_cc X, Y, eq/ne, trueV, falseV)
6299     if (LHS.getOpcode() == ISD::XOR && isNullConstant(RHS))
6300       return DAG.getNode(RISCVISD::BR_CC, SDLoc(N), N->getValueType(0),
6301                          N->getOperand(0), LHS.getOperand(0), LHS.getOperand(1),
6302                          N->getOperand(3), N->getOperand(4));
6303 
6304     // (br_cc X, 1, setne, br_cc) ->
6305     // (br_cc X, 0, seteq, br_cc) if we can prove X is 0/1.
6306     // This can occur when legalizing some floating point comparisons.
6307     APInt Mask = APInt::getBitsSetFrom(LHS.getValueSizeInBits(), 1);
6308     if (isOneConstant(RHS) && DAG.MaskedValueIsZero(LHS, Mask)) {
6309       SDLoc DL(N);
6310       CCVal = ISD::getSetCCInverse(CCVal, LHS.getValueType());
6311       SDValue TargetCC = DAG.getCondCode(CCVal);
6312       RHS = DAG.getConstant(0, DL, LHS.getValueType());
6313       return DAG.getNode(RISCVISD::BR_CC, DL, N->getValueType(0),
6314                          N->getOperand(0), LHS, RHS, TargetCC,
6315                          N->getOperand(4));
6316     }
6317     break;
6318   }
6319   case ISD::FCOPYSIGN: {
6320     EVT VT = N->getValueType(0);
6321     if (!VT.isVector())
6322       break;
6323     // There is a form of VFSGNJ which injects the negated sign of its second
6324     // operand. Try and bubble any FNEG up after the extend/round to produce
6325     // this optimized pattern. Avoid modifying cases where FP_ROUND and
6326     // TRUNC=1.
6327     SDValue In2 = N->getOperand(1);
6328     // Avoid cases where the extend/round has multiple uses, as duplicating
6329     // those is typically more expensive than removing a fneg.
6330     if (!In2.hasOneUse())
6331       break;
6332     if (In2.getOpcode() != ISD::FP_EXTEND &&
6333         (In2.getOpcode() != ISD::FP_ROUND || In2.getConstantOperandVal(1) != 0))
6334       break;
6335     In2 = In2.getOperand(0);
6336     if (In2.getOpcode() != ISD::FNEG)
6337       break;
6338     SDLoc DL(N);
6339     SDValue NewFPExtRound = DAG.getFPExtendOrRound(In2.getOperand(0), DL, VT);
6340     return DAG.getNode(ISD::FCOPYSIGN, DL, VT, N->getOperand(0),
6341                        DAG.getNode(ISD::FNEG, DL, VT, NewFPExtRound));
6342   }
6343   case ISD::MGATHER:
6344   case ISD::MSCATTER: {
6345     if (!DCI.isBeforeLegalize())
6346       break;
6347     MaskedGatherScatterSDNode *MGSN = cast<MaskedGatherScatterSDNode>(N);
6348     SDValue Index = MGSN->getIndex();
6349     EVT IndexVT = Index.getValueType();
6350     MVT XLenVT = Subtarget.getXLenVT();
6351     // RISCV indexed loads only support the "unsigned unscaled" addressing
6352     // mode, so anything else must be manually legalized.
6353     bool NeedsIdxLegalization = MGSN->isIndexScaled() ||
6354                                 (MGSN->isIndexSigned() &&
6355                                  IndexVT.getVectorElementType().bitsLT(XLenVT));
6356     if (!NeedsIdxLegalization)
6357       break;
6358 
6359     SDLoc DL(N);
6360 
6361     // Any index legalization should first promote to XLenVT, so we don't lose
6362     // bits when scaling. This may create an illegal index type so we let
6363     // LLVM's legalization take care of the splitting.
6364     if (IndexVT.getVectorElementType().bitsLT(XLenVT)) {
6365       IndexVT = IndexVT.changeVectorElementType(XLenVT);
6366       Index = DAG.getNode(MGSN->isIndexSigned() ? ISD::SIGN_EXTEND
6367                                                 : ISD::ZERO_EXTEND,
6368                           DL, IndexVT, Index);
6369     }
6370 
6371     unsigned Scale = N->getConstantOperandVal(5);
6372     if (MGSN->isIndexScaled() && Scale != 1) {
6373       // Manually scale the indices by the element size.
6374       // TODO: Sanitize the scale operand here?
6375       assert(isPowerOf2_32(Scale) && "Expecting power-of-two types");
6376       SDValue SplatScale = DAG.getConstant(Log2_32(Scale), DL, IndexVT);
6377       Index = DAG.getNode(ISD::SHL, DL, IndexVT, Index, SplatScale);
6378     }
6379 
6380     ISD::MemIndexType NewIndexTy = ISD::UNSIGNED_UNSCALED;
6381     if (const auto *MGN = dyn_cast<MaskedGatherSDNode>(N)) {
6382       return DAG.getMaskedGather(
6383           N->getVTList(), MGSN->getMemoryVT(), DL,
6384           {MGSN->getChain(), MGN->getPassThru(), MGSN->getMask(),
6385            MGSN->getBasePtr(), Index, MGN->getScale()},
6386           MGN->getMemOperand(), NewIndexTy, MGN->getExtensionType());
6387     }
6388     const auto *MSN = cast<MaskedScatterSDNode>(N);
6389     return DAG.getMaskedScatter(
6390         N->getVTList(), MGSN->getMemoryVT(), DL,
6391         {MGSN->getChain(), MSN->getValue(), MGSN->getMask(), MGSN->getBasePtr(),
6392          Index, MGSN->getScale()},
6393         MGSN->getMemOperand(), NewIndexTy, MSN->isTruncatingStore());
6394   }
6395   case RISCVISD::SRA_VL:
6396   case RISCVISD::SRL_VL:
6397   case RISCVISD::SHL_VL: {
6398     SDValue ShAmt = N->getOperand(1);
6399     if (ShAmt.getOpcode() == RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL) {
6400       // We don't need the upper 32 bits of a 64-bit element for a shift amount.
6401       SDLoc DL(N);
6402       SDValue VL = N->getOperand(3);
6403       EVT VT = N->getValueType(0);
6404       ShAmt =
6405           DAG.getNode(RISCVISD::VMV_V_X_VL, DL, VT, ShAmt.getOperand(0), VL);
6406       return DAG.getNode(N->getOpcode(), DL, VT, N->getOperand(0), ShAmt,
6407                          N->getOperand(2), N->getOperand(3));
6408     }
6409     break;
6410   }
6411   case ISD::SRA:
6412   case ISD::SRL:
6413   case ISD::SHL: {
6414     SDValue ShAmt = N->getOperand(1);
6415     if (ShAmt.getOpcode() == RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL) {
6416       // We don't need the upper 32 bits of a 64-bit element for a shift amount.
6417       SDLoc DL(N);
6418       EVT VT = N->getValueType(0);
6419       ShAmt =
6420           DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VT, ShAmt.getOperand(0));
6421       return DAG.getNode(N->getOpcode(), DL, VT, N->getOperand(0), ShAmt);
6422     }
6423     break;
6424   }
6425   case RISCVISD::MUL_VL: {
6426     // Try to form VWMUL or VWMULU.
6427     // FIXME: Look for splat of extended scalar as well.
6428     // FIXME: Support VWMULSU.
6429     SDValue Op0 = N->getOperand(0);
6430     SDValue Op1 = N->getOperand(1);
6431     bool IsSignExt = Op0.getOpcode() == RISCVISD::VSEXT_VL;
6432     bool IsZeroExt = Op0.getOpcode() == RISCVISD::VZEXT_VL;
6433     if ((!IsSignExt && !IsZeroExt) || Op0.getOpcode() != Op1.getOpcode())
6434       return SDValue();
6435 
6436     // Make sure the extends have a single use.
6437     if (!Op0.hasOneUse() || !Op1.hasOneUse())
6438       return SDValue();
6439 
6440     SDValue Mask = N->getOperand(2);
6441     SDValue VL = N->getOperand(3);
6442     if (Op0.getOperand(1) != Mask || Op1.getOperand(1) != Mask ||
6443         Op0.getOperand(2) != VL || Op1.getOperand(2) != VL)
6444       return SDValue();
6445 
6446     Op0 = Op0.getOperand(0);
6447     Op1 = Op1.getOperand(0);
6448 
6449     MVT VT = N->getSimpleValueType(0);
6450     MVT NarrowVT =
6451         MVT::getVectorVT(MVT::getIntegerVT(VT.getScalarSizeInBits() / 2),
6452                          VT.getVectorElementCount());
6453 
6454     SDLoc DL(N);
6455 
6456     // Re-introduce narrower extends if needed.
6457     unsigned ExtOpc = IsSignExt ? RISCVISD::VSEXT_VL : RISCVISD::VZEXT_VL;
6458     if (Op0.getValueType() != NarrowVT)
6459       Op0 = DAG.getNode(ExtOpc, DL, NarrowVT, Op0, Mask, VL);
6460     if (Op1.getValueType() != NarrowVT)
6461       Op1 = DAG.getNode(ExtOpc, DL, NarrowVT, Op1, Mask, VL);
6462 
6463     unsigned WMulOpc = IsSignExt ? RISCVISD::VWMUL_VL : RISCVISD::VWMULU_VL;
6464     return DAG.getNode(WMulOpc, DL, VT, Op0, Op1, Mask, VL);
6465   }
6466   }
6467 
6468   return SDValue();
6469 }
6470 
6471 bool RISCVTargetLowering::isDesirableToCommuteWithShift(
6472     const SDNode *N, CombineLevel Level) const {
6473   // The following folds are only desirable if `(OP _, c1 << c2)` can be
6474   // materialised in fewer instructions than `(OP _, c1)`:
6475   //
6476   //   (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
6477   //   (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
6478   SDValue N0 = N->getOperand(0);
6479   EVT Ty = N0.getValueType();
6480   if (Ty.isScalarInteger() &&
6481       (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::OR)) {
6482     auto *C1 = dyn_cast<ConstantSDNode>(N0->getOperand(1));
6483     auto *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1));
6484     if (C1 && C2) {
6485       const APInt &C1Int = C1->getAPIntValue();
6486       APInt ShiftedC1Int = C1Int << C2->getAPIntValue();
6487 
6488       // We can materialise `c1 << c2` into an add immediate, so it's "free",
6489       // and the combine should happen, to potentially allow further combines
6490       // later.
6491       if (ShiftedC1Int.getMinSignedBits() <= 64 &&
6492           isLegalAddImmediate(ShiftedC1Int.getSExtValue()))
6493         return true;
6494 
6495       // We can materialise `c1` in an add immediate, so it's "free", and the
6496       // combine should be prevented.
6497       if (C1Int.getMinSignedBits() <= 64 &&
6498           isLegalAddImmediate(C1Int.getSExtValue()))
6499         return false;
6500 
6501       // Neither constant will fit into an immediate, so find materialisation
6502       // costs.
6503       int C1Cost = RISCVMatInt::getIntMatCost(C1Int, Ty.getSizeInBits(),
6504                                               Subtarget.getFeatureBits(),
6505                                               /*CompressionCost*/true);
6506       int ShiftedC1Cost = RISCVMatInt::getIntMatCost(
6507           ShiftedC1Int, Ty.getSizeInBits(), Subtarget.getFeatureBits(),
6508           /*CompressionCost*/true);
6509 
6510       // Materialising `c1` is cheaper than materialising `c1 << c2`, so the
6511       // combine should be prevented.
6512       if (C1Cost < ShiftedC1Cost)
6513         return false;
6514     }
6515   }
6516   return true;
6517 }
6518 
6519 bool RISCVTargetLowering::targetShrinkDemandedConstant(
6520     SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,
6521     TargetLoweringOpt &TLO) const {
6522   // Delay this optimization as late as possible.
6523   if (!TLO.LegalOps)
6524     return false;
6525 
6526   EVT VT = Op.getValueType();
6527   if (VT.isVector())
6528     return false;
6529 
6530   // Only handle AND for now.
6531   if (Op.getOpcode() != ISD::AND)
6532     return false;
6533 
6534   ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6535   if (!C)
6536     return false;
6537 
6538   const APInt &Mask = C->getAPIntValue();
6539 
6540   // Clear all non-demanded bits initially.
6541   APInt ShrunkMask = Mask & DemandedBits;
6542 
6543   // Try to make a smaller immediate by setting undemanded bits.
6544 
6545   APInt ExpandedMask = Mask | ~DemandedBits;
6546 
6547   auto IsLegalMask = [ShrunkMask, ExpandedMask](const APInt &Mask) -> bool {
6548     return ShrunkMask.isSubsetOf(Mask) && Mask.isSubsetOf(ExpandedMask);
6549   };
6550   auto UseMask = [Mask, Op, VT, &TLO](const APInt &NewMask) -> bool {
6551     if (NewMask == Mask)
6552       return true;
6553     SDLoc DL(Op);
6554     SDValue NewC = TLO.DAG.getConstant(NewMask, DL, VT);
6555     SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC);
6556     return TLO.CombineTo(Op, NewOp);
6557   };
6558 
6559   // If the shrunk mask fits in sign extended 12 bits, let the target
6560   // independent code apply it.
6561   if (ShrunkMask.isSignedIntN(12))
6562     return false;
6563 
6564   // Preserve (and X, 0xffff) when zext.h is supported.
6565   if (Subtarget.hasStdExtZbb() || Subtarget.hasStdExtZbp()) {
6566     APInt NewMask = APInt(Mask.getBitWidth(), 0xffff);
6567     if (IsLegalMask(NewMask))
6568       return UseMask(NewMask);
6569   }
6570 
6571   // Try to preserve (and X, 0xffffffff), the (zext_inreg X, i32) pattern.
6572   if (VT == MVT::i64) {
6573     APInt NewMask = APInt(64, 0xffffffff);
6574     if (IsLegalMask(NewMask))
6575       return UseMask(NewMask);
6576   }
6577 
6578   // For the remaining optimizations, we need to be able to make a negative
6579   // number through a combination of mask and undemanded bits.
6580   if (!ExpandedMask.isNegative())
6581     return false;
6582 
6583   // What is the fewest number of bits we need to represent the negative number.
6584   unsigned MinSignedBits = ExpandedMask.getMinSignedBits();
6585 
6586   // Try to make a 12 bit negative immediate. If that fails try to make a 32
6587   // bit negative immediate unless the shrunk immediate already fits in 32 bits.
6588   APInt NewMask = ShrunkMask;
6589   if (MinSignedBits <= 12)
6590     NewMask.setBitsFrom(11);
6591   else if (MinSignedBits <= 32 && !ShrunkMask.isSignedIntN(32))
6592     NewMask.setBitsFrom(31);
6593   else
6594     return false;
6595 
6596   // Sanity check that our new mask is a subset of the demanded mask.
6597   assert(IsLegalMask(NewMask));
6598   return UseMask(NewMask);
6599 }
6600 
6601 static void computeGREV(APInt &Src, unsigned ShAmt) {
6602   ShAmt &= Src.getBitWidth() - 1;
6603   uint64_t x = Src.getZExtValue();
6604   if (ShAmt & 1)
6605     x = ((x & 0x5555555555555555LL) << 1) | ((x & 0xAAAAAAAAAAAAAAAALL) >> 1);
6606   if (ShAmt & 2)
6607     x = ((x & 0x3333333333333333LL) << 2) | ((x & 0xCCCCCCCCCCCCCCCCLL) >> 2);
6608   if (ShAmt & 4)
6609     x = ((x & 0x0F0F0F0F0F0F0F0FLL) << 4) | ((x & 0xF0F0F0F0F0F0F0F0LL) >> 4);
6610   if (ShAmt & 8)
6611     x = ((x & 0x00FF00FF00FF00FFLL) << 8) | ((x & 0xFF00FF00FF00FF00LL) >> 8);
6612   if (ShAmt & 16)
6613     x = ((x & 0x0000FFFF0000FFFFLL) << 16) | ((x & 0xFFFF0000FFFF0000LL) >> 16);
6614   if (ShAmt & 32)
6615     x = ((x & 0x00000000FFFFFFFFLL) << 32) | ((x & 0xFFFFFFFF00000000LL) >> 32);
6616   Src = x;
6617 }
6618 
6619 void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
6620                                                         KnownBits &Known,
6621                                                         const APInt &DemandedElts,
6622                                                         const SelectionDAG &DAG,
6623                                                         unsigned Depth) const {
6624   unsigned BitWidth = Known.getBitWidth();
6625   unsigned Opc = Op.getOpcode();
6626   assert((Opc >= ISD::BUILTIN_OP_END ||
6627           Opc == ISD::INTRINSIC_WO_CHAIN ||
6628           Opc == ISD::INTRINSIC_W_CHAIN ||
6629           Opc == ISD::INTRINSIC_VOID) &&
6630          "Should use MaskedValueIsZero if you don't know whether Op"
6631          " is a target node!");
6632 
6633   Known.resetAll();
6634   switch (Opc) {
6635   default: break;
6636   case RISCVISD::SELECT_CC: {
6637     Known = DAG.computeKnownBits(Op.getOperand(4), Depth + 1);
6638     // If we don't know any bits, early out.
6639     if (Known.isUnknown())
6640       break;
6641     KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(3), Depth + 1);
6642 
6643     // Only known if known in both the LHS and RHS.
6644     Known = KnownBits::commonBits(Known, Known2);
6645     break;
6646   }
6647   case RISCVISD::REMUW: {
6648     KnownBits Known2;
6649     Known = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6650     Known2 = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6651     // We only care about the lower 32 bits.
6652     Known = KnownBits::urem(Known.trunc(32), Known2.trunc(32));
6653     // Restore the original width by sign extending.
6654     Known = Known.sext(BitWidth);
6655     break;
6656   }
6657   case RISCVISD::DIVUW: {
6658     KnownBits Known2;
6659     Known = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6660     Known2 = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6661     // We only care about the lower 32 bits.
6662     Known = KnownBits::udiv(Known.trunc(32), Known2.trunc(32));
6663     // Restore the original width by sign extending.
6664     Known = Known.sext(BitWidth);
6665     break;
6666   }
6667   case RISCVISD::CTZW: {
6668     KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
6669     unsigned PossibleTZ = Known2.trunc(32).countMaxTrailingZeros();
6670     unsigned LowBits = Log2_32(PossibleTZ) + 1;
6671     Known.Zero.setBitsFrom(LowBits);
6672     break;
6673   }
6674   case RISCVISD::CLZW: {
6675     KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
6676     unsigned PossibleLZ = Known2.trunc(32).countMaxLeadingZeros();
6677     unsigned LowBits = Log2_32(PossibleLZ) + 1;
6678     Known.Zero.setBitsFrom(LowBits);
6679     break;
6680   }
6681   case RISCVISD::GREV:
6682   case RISCVISD::GREVW: {
6683     if (auto *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
6684       Known = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
6685       if (Opc == RISCVISD::GREVW)
6686         Known = Known.trunc(32);
6687       unsigned ShAmt = C->getZExtValue();
6688       computeGREV(Known.Zero, ShAmt);
6689       computeGREV(Known.One, ShAmt);
6690       if (Opc == RISCVISD::GREVW)
6691         Known = Known.sext(BitWidth);
6692     }
6693     break;
6694   }
6695   case RISCVISD::READ_VLENB:
6696     // We assume VLENB is at least 16 bytes.
6697     Known.Zero.setLowBits(4);
6698     // We assume VLENB is no more than 65536 / 8 bytes.
6699     Known.Zero.setBitsFrom(14);
6700     break;
6701   case ISD::INTRINSIC_W_CHAIN: {
6702     unsigned IntNo = Op.getConstantOperandVal(1);
6703     switch (IntNo) {
6704     default:
6705       // We can't do anything for most intrinsics.
6706       break;
6707     case Intrinsic::riscv_vsetvli:
6708     case Intrinsic::riscv_vsetvlimax:
6709       // Assume that VL output is positive and would fit in an int32_t.
6710       // TODO: VLEN might be capped at 16 bits in a future V spec update.
6711       if (BitWidth >= 32)
6712         Known.Zero.setBitsFrom(31);
6713       break;
6714     }
6715     break;
6716   }
6717   }
6718 }
6719 
6720 unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
6721     SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
6722     unsigned Depth) const {
6723   switch (Op.getOpcode()) {
6724   default:
6725     break;
6726   case RISCVISD::SELECT_CC: {
6727     unsigned Tmp = DAG.ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth + 1);
6728     if (Tmp == 1) return 1;  // Early out.
6729     unsigned Tmp2 = DAG.ComputeNumSignBits(Op.getOperand(4), DemandedElts, Depth + 1);
6730     return std::min(Tmp, Tmp2);
6731   }
6732   case RISCVISD::SLLW:
6733   case RISCVISD::SRAW:
6734   case RISCVISD::SRLW:
6735   case RISCVISD::DIVW:
6736   case RISCVISD::DIVUW:
6737   case RISCVISD::REMUW:
6738   case RISCVISD::ROLW:
6739   case RISCVISD::RORW:
6740   case RISCVISD::GREVW:
6741   case RISCVISD::GORCW:
6742   case RISCVISD::FSLW:
6743   case RISCVISD::FSRW:
6744   case RISCVISD::SHFLW:
6745   case RISCVISD::UNSHFLW:
6746   case RISCVISD::BCOMPRESSW:
6747   case RISCVISD::BDECOMPRESSW:
6748   case RISCVISD::FCVT_W_RTZ_RV64:
6749   case RISCVISD::FCVT_WU_RTZ_RV64:
6750     // TODO: As the result is sign-extended, this is conservatively correct. A
6751     // more precise answer could be calculated for SRAW depending on known
6752     // bits in the shift amount.
6753     return 33;
6754   case RISCVISD::SHFL:
6755   case RISCVISD::UNSHFL: {
6756     // There is no SHFLIW, but a i64 SHFLI with bit 4 of the control word
6757     // cleared doesn't affect bit 31. The upper 32 bits will be shuffled, but
6758     // will stay within the upper 32 bits. If there were more than 32 sign bits
6759     // before there will be at least 33 sign bits after.
6760     if (Op.getValueType() == MVT::i64 &&
6761         isa<ConstantSDNode>(Op.getOperand(1)) &&
6762         (Op.getConstantOperandVal(1) & 0x10) == 0) {
6763       unsigned Tmp = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
6764       if (Tmp > 32)
6765         return 33;
6766     }
6767     break;
6768   }
6769   case RISCVISD::VMV_X_S:
6770     // The number of sign bits of the scalar result is computed by obtaining the
6771     // element type of the input vector operand, subtracting its width from the
6772     // XLEN, and then adding one (sign bit within the element type). If the
6773     // element type is wider than XLen, the least-significant XLEN bits are
6774     // taken.
6775     if (Op.getOperand(0).getScalarValueSizeInBits() > Subtarget.getXLen())
6776       return 1;
6777     return Subtarget.getXLen() - Op.getOperand(0).getScalarValueSizeInBits() + 1;
6778   }
6779 
6780   return 1;
6781 }
6782 
6783 static MachineBasicBlock *emitReadCycleWidePseudo(MachineInstr &MI,
6784                                                   MachineBasicBlock *BB) {
6785   assert(MI.getOpcode() == RISCV::ReadCycleWide && "Unexpected instruction");
6786 
6787   // To read the 64-bit cycle CSR on a 32-bit target, we read the two halves.
6788   // Should the count have wrapped while it was being read, we need to try
6789   // again.
6790   // ...
6791   // read:
6792   // rdcycleh x3 # load high word of cycle
6793   // rdcycle  x2 # load low word of cycle
6794   // rdcycleh x4 # load high word of cycle
6795   // bne x3, x4, read # check if high word reads match, otherwise try again
6796   // ...
6797 
6798   MachineFunction &MF = *BB->getParent();
6799   const BasicBlock *LLVM_BB = BB->getBasicBlock();
6800   MachineFunction::iterator It = ++BB->getIterator();
6801 
6802   MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB);
6803   MF.insert(It, LoopMBB);
6804 
6805   MachineBasicBlock *DoneMBB = MF.CreateMachineBasicBlock(LLVM_BB);
6806   MF.insert(It, DoneMBB);
6807 
6808   // Transfer the remainder of BB and its successor edges to DoneMBB.
6809   DoneMBB->splice(DoneMBB->begin(), BB,
6810                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
6811   DoneMBB->transferSuccessorsAndUpdatePHIs(BB);
6812 
6813   BB->addSuccessor(LoopMBB);
6814 
6815   MachineRegisterInfo &RegInfo = MF.getRegInfo();
6816   Register ReadAgainReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
6817   Register LoReg = MI.getOperand(0).getReg();
6818   Register HiReg = MI.getOperand(1).getReg();
6819   DebugLoc DL = MI.getDebugLoc();
6820 
6821   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
6822   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), HiReg)
6823       .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
6824       .addReg(RISCV::X0);
6825   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), LoReg)
6826       .addImm(RISCVSysReg::lookupSysRegByName("CYCLE")->Encoding)
6827       .addReg(RISCV::X0);
6828   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), ReadAgainReg)
6829       .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
6830       .addReg(RISCV::X0);
6831 
6832   BuildMI(LoopMBB, DL, TII->get(RISCV::BNE))
6833       .addReg(HiReg)
6834       .addReg(ReadAgainReg)
6835       .addMBB(LoopMBB);
6836 
6837   LoopMBB->addSuccessor(LoopMBB);
6838   LoopMBB->addSuccessor(DoneMBB);
6839 
6840   MI.eraseFromParent();
6841 
6842   return DoneMBB;
6843 }
6844 
6845 static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
6846                                              MachineBasicBlock *BB) {
6847   assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
6848 
6849   MachineFunction &MF = *BB->getParent();
6850   DebugLoc DL = MI.getDebugLoc();
6851   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
6852   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
6853   Register LoReg = MI.getOperand(0).getReg();
6854   Register HiReg = MI.getOperand(1).getReg();
6855   Register SrcReg = MI.getOperand(2).getReg();
6856   const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
6857   int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex(MF);
6858 
6859   TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
6860                           RI);
6861   MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, FI);
6862   MachineMemOperand *MMOLo =
6863       MF.getMachineMemOperand(MPI, MachineMemOperand::MOLoad, 4, Align(8));
6864   MachineMemOperand *MMOHi = MF.getMachineMemOperand(
6865       MPI.getWithOffset(4), MachineMemOperand::MOLoad, 4, Align(8));
6866   BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
6867       .addFrameIndex(FI)
6868       .addImm(0)
6869       .addMemOperand(MMOLo);
6870   BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
6871       .addFrameIndex(FI)
6872       .addImm(4)
6873       .addMemOperand(MMOHi);
6874   MI.eraseFromParent(); // The pseudo instruction is gone now.
6875   return BB;
6876 }
6877 
6878 static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
6879                                                  MachineBasicBlock *BB) {
6880   assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
6881          "Unexpected instruction");
6882 
6883   MachineFunction &MF = *BB->getParent();
6884   DebugLoc DL = MI.getDebugLoc();
6885   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
6886   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
6887   Register DstReg = MI.getOperand(0).getReg();
6888   Register LoReg = MI.getOperand(1).getReg();
6889   Register HiReg = MI.getOperand(2).getReg();
6890   const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
6891   int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex(MF);
6892 
6893   MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, FI);
6894   MachineMemOperand *MMOLo =
6895       MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, Align(8));
6896   MachineMemOperand *MMOHi = MF.getMachineMemOperand(
6897       MPI.getWithOffset(4), MachineMemOperand::MOStore, 4, Align(8));
6898   BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
6899       .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
6900       .addFrameIndex(FI)
6901       .addImm(0)
6902       .addMemOperand(MMOLo);
6903   BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
6904       .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
6905       .addFrameIndex(FI)
6906       .addImm(4)
6907       .addMemOperand(MMOHi);
6908   TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
6909   MI.eraseFromParent(); // The pseudo instruction is gone now.
6910   return BB;
6911 }
6912 
6913 static bool isSelectPseudo(MachineInstr &MI) {
6914   switch (MI.getOpcode()) {
6915   default:
6916     return false;
6917   case RISCV::Select_GPR_Using_CC_GPR:
6918   case RISCV::Select_FPR16_Using_CC_GPR:
6919   case RISCV::Select_FPR32_Using_CC_GPR:
6920   case RISCV::Select_FPR64_Using_CC_GPR:
6921     return true;
6922   }
6923 }
6924 
6925 static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
6926                                            MachineBasicBlock *BB,
6927                                            const RISCVSubtarget &Subtarget) {
6928   // To "insert" Select_* instructions, we actually have to insert the triangle
6929   // control-flow pattern.  The incoming instructions know the destination vreg
6930   // to set, the condition code register to branch on, the true/false values to
6931   // select between, and the condcode to use to select the appropriate branch.
6932   //
6933   // We produce the following control flow:
6934   //     HeadMBB
6935   //     |  \
6936   //     |  IfFalseMBB
6937   //     | /
6938   //    TailMBB
6939   //
6940   // When we find a sequence of selects we attempt to optimize their emission
6941   // by sharing the control flow. Currently we only handle cases where we have
6942   // multiple selects with the exact same condition (same LHS, RHS and CC).
6943   // The selects may be interleaved with other instructions if the other
6944   // instructions meet some requirements we deem safe:
6945   // - They are debug instructions. Otherwise,
6946   // - They do not have side-effects, do not access memory and their inputs do
6947   //   not depend on the results of the select pseudo-instructions.
6948   // The TrueV/FalseV operands of the selects cannot depend on the result of
6949   // previous selects in the sequence.
6950   // These conditions could be further relaxed. See the X86 target for a
6951   // related approach and more information.
6952   Register LHS = MI.getOperand(1).getReg();
6953   Register RHS = MI.getOperand(2).getReg();
6954   auto CC = static_cast<RISCVCC::CondCode>(MI.getOperand(3).getImm());
6955 
6956   SmallVector<MachineInstr *, 4> SelectDebugValues;
6957   SmallSet<Register, 4> SelectDests;
6958   SelectDests.insert(MI.getOperand(0).getReg());
6959 
6960   MachineInstr *LastSelectPseudo = &MI;
6961 
6962   for (auto E = BB->end(), SequenceMBBI = MachineBasicBlock::iterator(MI);
6963        SequenceMBBI != E; ++SequenceMBBI) {
6964     if (SequenceMBBI->isDebugInstr())
6965       continue;
6966     else if (isSelectPseudo(*SequenceMBBI)) {
6967       if (SequenceMBBI->getOperand(1).getReg() != LHS ||
6968           SequenceMBBI->getOperand(2).getReg() != RHS ||
6969           SequenceMBBI->getOperand(3).getImm() != CC ||
6970           SelectDests.count(SequenceMBBI->getOperand(4).getReg()) ||
6971           SelectDests.count(SequenceMBBI->getOperand(5).getReg()))
6972         break;
6973       LastSelectPseudo = &*SequenceMBBI;
6974       SequenceMBBI->collectDebugValues(SelectDebugValues);
6975       SelectDests.insert(SequenceMBBI->getOperand(0).getReg());
6976     } else {
6977       if (SequenceMBBI->hasUnmodeledSideEffects() ||
6978           SequenceMBBI->mayLoadOrStore())
6979         break;
6980       if (llvm::any_of(SequenceMBBI->operands(), [&](MachineOperand &MO) {
6981             return MO.isReg() && MO.isUse() && SelectDests.count(MO.getReg());
6982           }))
6983         break;
6984     }
6985   }
6986 
6987   const RISCVInstrInfo &TII = *Subtarget.getInstrInfo();
6988   const BasicBlock *LLVM_BB = BB->getBasicBlock();
6989   DebugLoc DL = MI.getDebugLoc();
6990   MachineFunction::iterator I = ++BB->getIterator();
6991 
6992   MachineBasicBlock *HeadMBB = BB;
6993   MachineFunction *F = BB->getParent();
6994   MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
6995   MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
6996 
6997   F->insert(I, IfFalseMBB);
6998   F->insert(I, TailMBB);
6999 
7000   // Transfer debug instructions associated with the selects to TailMBB.
7001   for (MachineInstr *DebugInstr : SelectDebugValues) {
7002     TailMBB->push_back(DebugInstr->removeFromParent());
7003   }
7004 
7005   // Move all instructions after the sequence to TailMBB.
7006   TailMBB->splice(TailMBB->end(), HeadMBB,
7007                   std::next(LastSelectPseudo->getIterator()), HeadMBB->end());
7008   // Update machine-CFG edges by transferring all successors of the current
7009   // block to the new block which will contain the Phi nodes for the selects.
7010   TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
7011   // Set the successors for HeadMBB.
7012   HeadMBB->addSuccessor(IfFalseMBB);
7013   HeadMBB->addSuccessor(TailMBB);
7014 
7015   // Insert appropriate branch.
7016   BuildMI(HeadMBB, DL, TII.getBrCond(CC))
7017     .addReg(LHS)
7018     .addReg(RHS)
7019     .addMBB(TailMBB);
7020 
7021   // IfFalseMBB just falls through to TailMBB.
7022   IfFalseMBB->addSuccessor(TailMBB);
7023 
7024   // Create PHIs for all of the select pseudo-instructions.
7025   auto SelectMBBI = MI.getIterator();
7026   auto SelectEnd = std::next(LastSelectPseudo->getIterator());
7027   auto InsertionPoint = TailMBB->begin();
7028   while (SelectMBBI != SelectEnd) {
7029     auto Next = std::next(SelectMBBI);
7030     if (isSelectPseudo(*SelectMBBI)) {
7031       // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
7032       BuildMI(*TailMBB, InsertionPoint, SelectMBBI->getDebugLoc(),
7033               TII.get(RISCV::PHI), SelectMBBI->getOperand(0).getReg())
7034           .addReg(SelectMBBI->getOperand(4).getReg())
7035           .addMBB(HeadMBB)
7036           .addReg(SelectMBBI->getOperand(5).getReg())
7037           .addMBB(IfFalseMBB);
7038       SelectMBBI->eraseFromParent();
7039     }
7040     SelectMBBI = Next;
7041   }
7042 
7043   F->getProperties().reset(MachineFunctionProperties::Property::NoPHIs);
7044   return TailMBB;
7045 }
7046 
7047 MachineBasicBlock *
7048 RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
7049                                                  MachineBasicBlock *BB) const {
7050   switch (MI.getOpcode()) {
7051   default:
7052     llvm_unreachable("Unexpected instr type to insert");
7053   case RISCV::ReadCycleWide:
7054     assert(!Subtarget.is64Bit() &&
7055            "ReadCycleWrite is only to be used on riscv32");
7056     return emitReadCycleWidePseudo(MI, BB);
7057   case RISCV::Select_GPR_Using_CC_GPR:
7058   case RISCV::Select_FPR16_Using_CC_GPR:
7059   case RISCV::Select_FPR32_Using_CC_GPR:
7060   case RISCV::Select_FPR64_Using_CC_GPR:
7061     return emitSelectPseudo(MI, BB, Subtarget);
7062   case RISCV::BuildPairF64Pseudo:
7063     return emitBuildPairF64Pseudo(MI, BB);
7064   case RISCV::SplitF64Pseudo:
7065     return emitSplitF64Pseudo(MI, BB);
7066   }
7067 }
7068 
7069 // Calling Convention Implementation.
7070 // The expectations for frontend ABI lowering vary from target to target.
7071 // Ideally, an LLVM frontend would be able to avoid worrying about many ABI
7072 // details, but this is a longer term goal. For now, we simply try to keep the
7073 // role of the frontend as simple and well-defined as possible. The rules can
7074 // be summarised as:
7075 // * Never split up large scalar arguments. We handle them here.
7076 // * If a hardfloat calling convention is being used, and the struct may be
7077 // passed in a pair of registers (fp+fp, int+fp), and both registers are
7078 // available, then pass as two separate arguments. If either the GPRs or FPRs
7079 // are exhausted, then pass according to the rule below.
7080 // * If a struct could never be passed in registers or directly in a stack
7081 // slot (as it is larger than 2*XLEN and the floating point rules don't
7082 // apply), then pass it using a pointer with the byval attribute.
7083 // * If a struct is less than 2*XLEN, then coerce to either a two-element
7084 // word-sized array or a 2*XLEN scalar (depending on alignment).
7085 // * The frontend can determine whether a struct is returned by reference or
7086 // not based on its size and fields. If it will be returned by reference, the
7087 // frontend must modify the prototype so a pointer with the sret annotation is
7088 // passed as the first argument. This is not necessary for large scalar
7089 // returns.
7090 // * Struct return values and varargs should be coerced to structs containing
7091 // register-size fields in the same situations they would be for fixed
7092 // arguments.
7093 
7094 static const MCPhysReg ArgGPRs[] = {
7095   RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
7096   RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
7097 };
7098 static const MCPhysReg ArgFPR16s[] = {
7099   RISCV::F10_H, RISCV::F11_H, RISCV::F12_H, RISCV::F13_H,
7100   RISCV::F14_H, RISCV::F15_H, RISCV::F16_H, RISCV::F17_H
7101 };
7102 static const MCPhysReg ArgFPR32s[] = {
7103   RISCV::F10_F, RISCV::F11_F, RISCV::F12_F, RISCV::F13_F,
7104   RISCV::F14_F, RISCV::F15_F, RISCV::F16_F, RISCV::F17_F
7105 };
7106 static const MCPhysReg ArgFPR64s[] = {
7107   RISCV::F10_D, RISCV::F11_D, RISCV::F12_D, RISCV::F13_D,
7108   RISCV::F14_D, RISCV::F15_D, RISCV::F16_D, RISCV::F17_D
7109 };
7110 // This is an interim calling convention and it may be changed in the future.
7111 static const MCPhysReg ArgVRs[] = {
7112     RISCV::V8,  RISCV::V9,  RISCV::V10, RISCV::V11, RISCV::V12, RISCV::V13,
7113     RISCV::V14, RISCV::V15, RISCV::V16, RISCV::V17, RISCV::V18, RISCV::V19,
7114     RISCV::V20, RISCV::V21, RISCV::V22, RISCV::V23};
7115 static const MCPhysReg ArgVRM2s[] = {RISCV::V8M2,  RISCV::V10M2, RISCV::V12M2,
7116                                      RISCV::V14M2, RISCV::V16M2, RISCV::V18M2,
7117                                      RISCV::V20M2, RISCV::V22M2};
7118 static const MCPhysReg ArgVRM4s[] = {RISCV::V8M4, RISCV::V12M4, RISCV::V16M4,
7119                                      RISCV::V20M4};
7120 static const MCPhysReg ArgVRM8s[] = {RISCV::V8M8, RISCV::V16M8};
7121 
7122 // Pass a 2*XLEN argument that has been split into two XLEN values through
7123 // registers or the stack as necessary.
7124 static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
7125                                 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
7126                                 MVT ValVT2, MVT LocVT2,
7127                                 ISD::ArgFlagsTy ArgFlags2) {
7128   unsigned XLenInBytes = XLen / 8;
7129   if (Register Reg = State.AllocateReg(ArgGPRs)) {
7130     // At least one half can be passed via register.
7131     State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
7132                                      VA1.getLocVT(), CCValAssign::Full));
7133   } else {
7134     // Both halves must be passed on the stack, with proper alignment.
7135     Align StackAlign =
7136         std::max(Align(XLenInBytes), ArgFlags1.getNonZeroOrigAlign());
7137     State.addLoc(
7138         CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
7139                             State.AllocateStack(XLenInBytes, StackAlign),
7140                             VA1.getLocVT(), CCValAssign::Full));
7141     State.addLoc(CCValAssign::getMem(
7142         ValNo2, ValVT2, State.AllocateStack(XLenInBytes, Align(XLenInBytes)),
7143         LocVT2, CCValAssign::Full));
7144     return false;
7145   }
7146 
7147   if (Register Reg = State.AllocateReg(ArgGPRs)) {
7148     // The second half can also be passed via register.
7149     State.addLoc(
7150         CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
7151   } else {
7152     // The second half is passed via the stack, without additional alignment.
7153     State.addLoc(CCValAssign::getMem(
7154         ValNo2, ValVT2, State.AllocateStack(XLenInBytes, Align(XLenInBytes)),
7155         LocVT2, CCValAssign::Full));
7156   }
7157 
7158   return false;
7159 }
7160 
7161 static unsigned allocateRVVReg(MVT ValVT, unsigned ValNo,
7162                                Optional<unsigned> FirstMaskArgument,
7163                                CCState &State, const RISCVTargetLowering &TLI) {
7164   const TargetRegisterClass *RC = TLI.getRegClassFor(ValVT);
7165   if (RC == &RISCV::VRRegClass) {
7166     // Assign the first mask argument to V0.
7167     // This is an interim calling convention and it may be changed in the
7168     // future.
7169     if (FirstMaskArgument.hasValue() && ValNo == FirstMaskArgument.getValue())
7170       return State.AllocateReg(RISCV::V0);
7171     return State.AllocateReg(ArgVRs);
7172   }
7173   if (RC == &RISCV::VRM2RegClass)
7174     return State.AllocateReg(ArgVRM2s);
7175   if (RC == &RISCV::VRM4RegClass)
7176     return State.AllocateReg(ArgVRM4s);
7177   if (RC == &RISCV::VRM8RegClass)
7178     return State.AllocateReg(ArgVRM8s);
7179   llvm_unreachable("Unhandled register class for ValueType");
7180 }
7181 
7182 // Implements the RISC-V calling convention. Returns true upon failure.
7183 static bool CC_RISCV(const DataLayout &DL, RISCVABI::ABI ABI, unsigned ValNo,
7184                      MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo,
7185                      ISD::ArgFlagsTy ArgFlags, CCState &State, bool IsFixed,
7186                      bool IsRet, Type *OrigTy, const RISCVTargetLowering &TLI,
7187                      Optional<unsigned> FirstMaskArgument) {
7188   unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
7189   assert(XLen == 32 || XLen == 64);
7190   MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
7191 
7192   // Any return value split in to more than two values can't be returned
7193   // directly. Vectors are returned via the available vector registers.
7194   if (!LocVT.isVector() && IsRet && ValNo > 1)
7195     return true;
7196 
7197   // UseGPRForF16_F32 if targeting one of the soft-float ABIs, if passing a
7198   // variadic argument, or if no F16/F32 argument registers are available.
7199   bool UseGPRForF16_F32 = true;
7200   // UseGPRForF64 if targeting soft-float ABIs or an FLEN=32 ABI, if passing a
7201   // variadic argument, or if no F64 argument registers are available.
7202   bool UseGPRForF64 = true;
7203 
7204   switch (ABI) {
7205   default:
7206     llvm_unreachable("Unexpected ABI");
7207   case RISCVABI::ABI_ILP32:
7208   case RISCVABI::ABI_LP64:
7209     break;
7210   case RISCVABI::ABI_ILP32F:
7211   case RISCVABI::ABI_LP64F:
7212     UseGPRForF16_F32 = !IsFixed;
7213     break;
7214   case RISCVABI::ABI_ILP32D:
7215   case RISCVABI::ABI_LP64D:
7216     UseGPRForF16_F32 = !IsFixed;
7217     UseGPRForF64 = !IsFixed;
7218     break;
7219   }
7220 
7221   // FPR16, FPR32, and FPR64 alias each other.
7222   if (State.getFirstUnallocated(ArgFPR32s) == array_lengthof(ArgFPR32s)) {
7223     UseGPRForF16_F32 = true;
7224     UseGPRForF64 = true;
7225   }
7226 
7227   // From this point on, rely on UseGPRForF16_F32, UseGPRForF64 and
7228   // similar local variables rather than directly checking against the target
7229   // ABI.
7230 
7231   if (UseGPRForF16_F32 && (ValVT == MVT::f16 || ValVT == MVT::f32)) {
7232     LocVT = XLenVT;
7233     LocInfo = CCValAssign::BCvt;
7234   } else if (UseGPRForF64 && XLen == 64 && ValVT == MVT::f64) {
7235     LocVT = MVT::i64;
7236     LocInfo = CCValAssign::BCvt;
7237   }
7238 
7239   // If this is a variadic argument, the RISC-V calling convention requires
7240   // that it is assigned an 'even' or 'aligned' register if it has 8-byte
7241   // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
7242   // be used regardless of whether the original argument was split during
7243   // legalisation or not. The argument will not be passed by registers if the
7244   // original type is larger than 2*XLEN, so the register alignment rule does
7245   // not apply.
7246   unsigned TwoXLenInBytes = (2 * XLen) / 8;
7247   if (!IsFixed && ArgFlags.getNonZeroOrigAlign() == TwoXLenInBytes &&
7248       DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
7249     unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
7250     // Skip 'odd' register if necessary.
7251     if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
7252       State.AllocateReg(ArgGPRs);
7253   }
7254 
7255   SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
7256   SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
7257       State.getPendingArgFlags();
7258 
7259   assert(PendingLocs.size() == PendingArgFlags.size() &&
7260          "PendingLocs and PendingArgFlags out of sync");
7261 
7262   // Handle passing f64 on RV32D with a soft float ABI or when floating point
7263   // registers are exhausted.
7264   if (UseGPRForF64 && XLen == 32 && ValVT == MVT::f64) {
7265     assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
7266            "Can't lower f64 if it is split");
7267     // Depending on available argument GPRS, f64 may be passed in a pair of
7268     // GPRs, split between a GPR and the stack, or passed completely on the
7269     // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
7270     // cases.
7271     Register Reg = State.AllocateReg(ArgGPRs);
7272     LocVT = MVT::i32;
7273     if (!Reg) {
7274       unsigned StackOffset = State.AllocateStack(8, Align(8));
7275       State.addLoc(
7276           CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
7277       return false;
7278     }
7279     if (!State.AllocateReg(ArgGPRs))
7280       State.AllocateStack(4, Align(4));
7281     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7282     return false;
7283   }
7284 
7285   // Fixed-length vectors are located in the corresponding scalable-vector
7286   // container types.
7287   if (ValVT.isFixedLengthVector())
7288     LocVT = TLI.getContainerForFixedLengthVector(LocVT);
7289 
7290   // Split arguments might be passed indirectly, so keep track of the pending
7291   // values. Split vectors are passed via a mix of registers and indirectly, so
7292   // treat them as we would any other argument.
7293   if (ValVT.isScalarInteger() && (ArgFlags.isSplit() || !PendingLocs.empty())) {
7294     LocVT = XLenVT;
7295     LocInfo = CCValAssign::Indirect;
7296     PendingLocs.push_back(
7297         CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
7298     PendingArgFlags.push_back(ArgFlags);
7299     if (!ArgFlags.isSplitEnd()) {
7300       return false;
7301     }
7302   }
7303 
7304   // If the split argument only had two elements, it should be passed directly
7305   // in registers or on the stack.
7306   if (ValVT.isScalarInteger() && ArgFlags.isSplitEnd() &&
7307       PendingLocs.size() <= 2) {
7308     assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
7309     // Apply the normal calling convention rules to the first half of the
7310     // split argument.
7311     CCValAssign VA = PendingLocs[0];
7312     ISD::ArgFlagsTy AF = PendingArgFlags[0];
7313     PendingLocs.clear();
7314     PendingArgFlags.clear();
7315     return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
7316                                ArgFlags);
7317   }
7318 
7319   // Allocate to a register if possible, or else a stack slot.
7320   Register Reg;
7321   unsigned StoreSizeBytes = XLen / 8;
7322   Align StackAlign = Align(XLen / 8);
7323 
7324   if (ValVT == MVT::f16 && !UseGPRForF16_F32)
7325     Reg = State.AllocateReg(ArgFPR16s);
7326   else if (ValVT == MVT::f32 && !UseGPRForF16_F32)
7327     Reg = State.AllocateReg(ArgFPR32s);
7328   else if (ValVT == MVT::f64 && !UseGPRForF64)
7329     Reg = State.AllocateReg(ArgFPR64s);
7330   else if (ValVT.isVector()) {
7331     Reg = allocateRVVReg(ValVT, ValNo, FirstMaskArgument, State, TLI);
7332     if (!Reg) {
7333       // For return values, the vector must be passed fully via registers or
7334       // via the stack.
7335       // FIXME: The proposed vector ABI only mandates v8-v15 for return values,
7336       // but we're using all of them.
7337       if (IsRet)
7338         return true;
7339       // Try using a GPR to pass the address
7340       if ((Reg = State.AllocateReg(ArgGPRs))) {
7341         LocVT = XLenVT;
7342         LocInfo = CCValAssign::Indirect;
7343       } else if (ValVT.isScalableVector()) {
7344         report_fatal_error("Unable to pass scalable vector types on the stack");
7345       } else {
7346         // Pass fixed-length vectors on the stack.
7347         LocVT = ValVT;
7348         StoreSizeBytes = ValVT.getStoreSize();
7349         // Align vectors to their element sizes, being careful for vXi1
7350         // vectors.
7351         StackAlign = MaybeAlign(ValVT.getScalarSizeInBits() / 8).valueOrOne();
7352       }
7353     }
7354   } else {
7355     Reg = State.AllocateReg(ArgGPRs);
7356   }
7357 
7358   unsigned StackOffset =
7359       Reg ? 0 : State.AllocateStack(StoreSizeBytes, StackAlign);
7360 
7361   // If we reach this point and PendingLocs is non-empty, we must be at the
7362   // end of a split argument that must be passed indirectly.
7363   if (!PendingLocs.empty()) {
7364     assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
7365     assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
7366 
7367     for (auto &It : PendingLocs) {
7368       if (Reg)
7369         It.convertToReg(Reg);
7370       else
7371         It.convertToMem(StackOffset);
7372       State.addLoc(It);
7373     }
7374     PendingLocs.clear();
7375     PendingArgFlags.clear();
7376     return false;
7377   }
7378 
7379   assert((!UseGPRForF16_F32 || !UseGPRForF64 || LocVT == XLenVT ||
7380           (TLI.getSubtarget().hasStdExtV() && ValVT.isVector())) &&
7381          "Expected an XLenVT or vector types at this stage");
7382 
7383   if (Reg) {
7384     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7385     return false;
7386   }
7387 
7388   // When a floating-point value is passed on the stack, no bit-conversion is
7389   // needed.
7390   if (ValVT.isFloatingPoint()) {
7391     LocVT = ValVT;
7392     LocInfo = CCValAssign::Full;
7393   }
7394   State.addLoc(CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
7395   return false;
7396 }
7397 
7398 template <typename ArgTy>
7399 static Optional<unsigned> preAssignMask(const ArgTy &Args) {
7400   for (const auto &ArgIdx : enumerate(Args)) {
7401     MVT ArgVT = ArgIdx.value().VT;
7402     if (ArgVT.isVector() && ArgVT.getVectorElementType() == MVT::i1)
7403       return ArgIdx.index();
7404   }
7405   return None;
7406 }
7407 
7408 void RISCVTargetLowering::analyzeInputArgs(
7409     MachineFunction &MF, CCState &CCInfo,
7410     const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet,
7411     RISCVCCAssignFn Fn) const {
7412   unsigned NumArgs = Ins.size();
7413   FunctionType *FType = MF.getFunction().getFunctionType();
7414 
7415   Optional<unsigned> FirstMaskArgument;
7416   if (Subtarget.hasStdExtV())
7417     FirstMaskArgument = preAssignMask(Ins);
7418 
7419   for (unsigned i = 0; i != NumArgs; ++i) {
7420     MVT ArgVT = Ins[i].VT;
7421     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
7422 
7423     Type *ArgTy = nullptr;
7424     if (IsRet)
7425       ArgTy = FType->getReturnType();
7426     else if (Ins[i].isOrigArg())
7427       ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
7428 
7429     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
7430     if (Fn(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
7431            ArgFlags, CCInfo, /*IsFixed=*/true, IsRet, ArgTy, *this,
7432            FirstMaskArgument)) {
7433       LLVM_DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
7434                         << EVT(ArgVT).getEVTString() << '\n');
7435       llvm_unreachable(nullptr);
7436     }
7437   }
7438 }
7439 
7440 void RISCVTargetLowering::analyzeOutputArgs(
7441     MachineFunction &MF, CCState &CCInfo,
7442     const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
7443     CallLoweringInfo *CLI, RISCVCCAssignFn Fn) const {
7444   unsigned NumArgs = Outs.size();
7445 
7446   Optional<unsigned> FirstMaskArgument;
7447   if (Subtarget.hasStdExtV())
7448     FirstMaskArgument = preAssignMask(Outs);
7449 
7450   for (unsigned i = 0; i != NumArgs; i++) {
7451     MVT ArgVT = Outs[i].VT;
7452     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
7453     Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
7454 
7455     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
7456     if (Fn(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
7457            ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy, *this,
7458            FirstMaskArgument)) {
7459       LLVM_DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
7460                         << EVT(ArgVT).getEVTString() << "\n");
7461       llvm_unreachable(nullptr);
7462     }
7463   }
7464 }
7465 
7466 // Convert Val to a ValVT. Should not be called for CCValAssign::Indirect
7467 // values.
7468 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
7469                                    const CCValAssign &VA, const SDLoc &DL,
7470                                    const RISCVSubtarget &Subtarget) {
7471   switch (VA.getLocInfo()) {
7472   default:
7473     llvm_unreachable("Unexpected CCValAssign::LocInfo");
7474   case CCValAssign::Full:
7475     if (VA.getValVT().isFixedLengthVector() && VA.getLocVT().isScalableVector())
7476       Val = convertFromScalableVector(VA.getValVT(), Val, DAG, Subtarget);
7477     break;
7478   case CCValAssign::BCvt:
7479     if (VA.getLocVT().isInteger() && VA.getValVT() == MVT::f16)
7480       Val = DAG.getNode(RISCVISD::FMV_H_X, DL, MVT::f16, Val);
7481     else if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32)
7482       Val = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, Val);
7483     else
7484       Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
7485     break;
7486   }
7487   return Val;
7488 }
7489 
7490 // The caller is responsible for loading the full value if the argument is
7491 // passed with CCValAssign::Indirect.
7492 static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
7493                                 const CCValAssign &VA, const SDLoc &DL,
7494                                 const RISCVTargetLowering &TLI) {
7495   MachineFunction &MF = DAG.getMachineFunction();
7496   MachineRegisterInfo &RegInfo = MF.getRegInfo();
7497   EVT LocVT = VA.getLocVT();
7498   SDValue Val;
7499   const TargetRegisterClass *RC = TLI.getRegClassFor(LocVT.getSimpleVT());
7500   Register VReg = RegInfo.createVirtualRegister(RC);
7501   RegInfo.addLiveIn(VA.getLocReg(), VReg);
7502   Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
7503 
7504   if (VA.getLocInfo() == CCValAssign::Indirect)
7505     return Val;
7506 
7507   return convertLocVTToValVT(DAG, Val, VA, DL, TLI.getSubtarget());
7508 }
7509 
7510 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val,
7511                                    const CCValAssign &VA, const SDLoc &DL,
7512                                    const RISCVSubtarget &Subtarget) {
7513   EVT LocVT = VA.getLocVT();
7514 
7515   switch (VA.getLocInfo()) {
7516   default:
7517     llvm_unreachable("Unexpected CCValAssign::LocInfo");
7518   case CCValAssign::Full:
7519     if (VA.getValVT().isFixedLengthVector() && LocVT.isScalableVector())
7520       Val = convertToScalableVector(LocVT, Val, DAG, Subtarget);
7521     break;
7522   case CCValAssign::BCvt:
7523     if (VA.getLocVT().isInteger() && VA.getValVT() == MVT::f16)
7524       Val = DAG.getNode(RISCVISD::FMV_X_ANYEXTH, DL, VA.getLocVT(), Val);
7525     else if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32)
7526       Val = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Val);
7527     else
7528       Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
7529     break;
7530   }
7531   return Val;
7532 }
7533 
7534 // The caller is responsible for loading the full value if the argument is
7535 // passed with CCValAssign::Indirect.
7536 static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
7537                                 const CCValAssign &VA, const SDLoc &DL) {
7538   MachineFunction &MF = DAG.getMachineFunction();
7539   MachineFrameInfo &MFI = MF.getFrameInfo();
7540   EVT LocVT = VA.getLocVT();
7541   EVT ValVT = VA.getValVT();
7542   EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
7543   int FI = MFI.CreateFixedObject(ValVT.getStoreSize(), VA.getLocMemOffset(),
7544                                  /*Immutable=*/true);
7545   SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
7546   SDValue Val;
7547 
7548   ISD::LoadExtType ExtType;
7549   switch (VA.getLocInfo()) {
7550   default:
7551     llvm_unreachable("Unexpected CCValAssign::LocInfo");
7552   case CCValAssign::Full:
7553   case CCValAssign::Indirect:
7554   case CCValAssign::BCvt:
7555     ExtType = ISD::NON_EXTLOAD;
7556     break;
7557   }
7558   Val = DAG.getExtLoad(
7559       ExtType, DL, LocVT, Chain, FIN,
7560       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
7561   return Val;
7562 }
7563 
7564 static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
7565                                        const CCValAssign &VA, const SDLoc &DL) {
7566   assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
7567          "Unexpected VA");
7568   MachineFunction &MF = DAG.getMachineFunction();
7569   MachineFrameInfo &MFI = MF.getFrameInfo();
7570   MachineRegisterInfo &RegInfo = MF.getRegInfo();
7571 
7572   if (VA.isMemLoc()) {
7573     // f64 is passed on the stack.
7574     int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
7575     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
7576     return DAG.getLoad(MVT::f64, DL, Chain, FIN,
7577                        MachinePointerInfo::getFixedStack(MF, FI));
7578   }
7579 
7580   assert(VA.isRegLoc() && "Expected register VA assignment");
7581 
7582   Register LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
7583   RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
7584   SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
7585   SDValue Hi;
7586   if (VA.getLocReg() == RISCV::X17) {
7587     // Second half of f64 is passed on the stack.
7588     int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
7589     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
7590     Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
7591                      MachinePointerInfo::getFixedStack(MF, FI));
7592   } else {
7593     // Second half of f64 is passed in another GPR.
7594     Register HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
7595     RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
7596     Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
7597   }
7598   return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
7599 }
7600 
7601 // FastCC has less than 1% performance improvement for some particular
7602 // benchmark. But theoretically, it may has benenfit for some cases.
7603 static bool CC_RISCV_FastCC(const DataLayout &DL, RISCVABI::ABI ABI,
7604                             unsigned ValNo, MVT ValVT, MVT LocVT,
7605                             CCValAssign::LocInfo LocInfo,
7606                             ISD::ArgFlagsTy ArgFlags, CCState &State,
7607                             bool IsFixed, bool IsRet, Type *OrigTy,
7608                             const RISCVTargetLowering &TLI,
7609                             Optional<unsigned> FirstMaskArgument) {
7610 
7611   // X5 and X6 might be used for save-restore libcall.
7612   static const MCPhysReg GPRList[] = {
7613       RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13, RISCV::X14,
7614       RISCV::X15, RISCV::X16, RISCV::X17, RISCV::X7,  RISCV::X28,
7615       RISCV::X29, RISCV::X30, RISCV::X31};
7616 
7617   if (LocVT == MVT::i32 || LocVT == MVT::i64) {
7618     if (unsigned Reg = State.AllocateReg(GPRList)) {
7619       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7620       return false;
7621     }
7622   }
7623 
7624   if (LocVT == MVT::f16) {
7625     static const MCPhysReg FPR16List[] = {
7626         RISCV::F10_H, RISCV::F11_H, RISCV::F12_H, RISCV::F13_H, RISCV::F14_H,
7627         RISCV::F15_H, RISCV::F16_H, RISCV::F17_H, RISCV::F0_H,  RISCV::F1_H,
7628         RISCV::F2_H,  RISCV::F3_H,  RISCV::F4_H,  RISCV::F5_H,  RISCV::F6_H,
7629         RISCV::F7_H,  RISCV::F28_H, RISCV::F29_H, RISCV::F30_H, RISCV::F31_H};
7630     if (unsigned Reg = State.AllocateReg(FPR16List)) {
7631       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7632       return false;
7633     }
7634   }
7635 
7636   if (LocVT == MVT::f32) {
7637     static const MCPhysReg FPR32List[] = {
7638         RISCV::F10_F, RISCV::F11_F, RISCV::F12_F, RISCV::F13_F, RISCV::F14_F,
7639         RISCV::F15_F, RISCV::F16_F, RISCV::F17_F, RISCV::F0_F,  RISCV::F1_F,
7640         RISCV::F2_F,  RISCV::F3_F,  RISCV::F4_F,  RISCV::F5_F,  RISCV::F6_F,
7641         RISCV::F7_F,  RISCV::F28_F, RISCV::F29_F, RISCV::F30_F, RISCV::F31_F};
7642     if (unsigned Reg = State.AllocateReg(FPR32List)) {
7643       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7644       return false;
7645     }
7646   }
7647 
7648   if (LocVT == MVT::f64) {
7649     static const MCPhysReg FPR64List[] = {
7650         RISCV::F10_D, RISCV::F11_D, RISCV::F12_D, RISCV::F13_D, RISCV::F14_D,
7651         RISCV::F15_D, RISCV::F16_D, RISCV::F17_D, RISCV::F0_D,  RISCV::F1_D,
7652         RISCV::F2_D,  RISCV::F3_D,  RISCV::F4_D,  RISCV::F5_D,  RISCV::F6_D,
7653         RISCV::F7_D,  RISCV::F28_D, RISCV::F29_D, RISCV::F30_D, RISCV::F31_D};
7654     if (unsigned Reg = State.AllocateReg(FPR64List)) {
7655       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7656       return false;
7657     }
7658   }
7659 
7660   if (LocVT == MVT::i32 || LocVT == MVT::f32) {
7661     unsigned Offset4 = State.AllocateStack(4, Align(4));
7662     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset4, LocVT, LocInfo));
7663     return false;
7664   }
7665 
7666   if (LocVT == MVT::i64 || LocVT == MVT::f64) {
7667     unsigned Offset5 = State.AllocateStack(8, Align(8));
7668     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset5, LocVT, LocInfo));
7669     return false;
7670   }
7671 
7672   if (LocVT.isVector()) {
7673     if (unsigned Reg =
7674             allocateRVVReg(ValVT, ValNo, FirstMaskArgument, State, TLI)) {
7675       // Fixed-length vectors are located in the corresponding scalable-vector
7676       // container types.
7677       if (ValVT.isFixedLengthVector())
7678         LocVT = TLI.getContainerForFixedLengthVector(LocVT);
7679       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7680     } else {
7681       // Try and pass the address via a "fast" GPR.
7682       if (unsigned GPRReg = State.AllocateReg(GPRList)) {
7683         LocInfo = CCValAssign::Indirect;
7684         LocVT = TLI.getSubtarget().getXLenVT();
7685         State.addLoc(CCValAssign::getReg(ValNo, ValVT, GPRReg, LocVT, LocInfo));
7686       } else if (ValVT.isFixedLengthVector()) {
7687         auto StackAlign =
7688             MaybeAlign(ValVT.getScalarSizeInBits() / 8).valueOrOne();
7689         unsigned StackOffset =
7690             State.AllocateStack(ValVT.getStoreSize(), StackAlign);
7691         State.addLoc(
7692             CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
7693       } else {
7694         // Can't pass scalable vectors on the stack.
7695         return true;
7696       }
7697     }
7698 
7699     return false;
7700   }
7701 
7702   return true; // CC didn't match.
7703 }
7704 
7705 static bool CC_RISCV_GHC(unsigned ValNo, MVT ValVT, MVT LocVT,
7706                          CCValAssign::LocInfo LocInfo,
7707                          ISD::ArgFlagsTy ArgFlags, CCState &State) {
7708 
7709   if (LocVT == MVT::i32 || LocVT == MVT::i64) {
7710     // Pass in STG registers: Base, Sp, Hp, R1, R2, R3, R4, R5, R6, R7, SpLim
7711     //                        s1    s2  s3  s4  s5  s6  s7  s8  s9  s10 s11
7712     static const MCPhysReg GPRList[] = {
7713         RISCV::X9, RISCV::X18, RISCV::X19, RISCV::X20, RISCV::X21, RISCV::X22,
7714         RISCV::X23, RISCV::X24, RISCV::X25, RISCV::X26, RISCV::X27};
7715     if (unsigned Reg = State.AllocateReg(GPRList)) {
7716       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7717       return false;
7718     }
7719   }
7720 
7721   if (LocVT == MVT::f32) {
7722     // Pass in STG registers: F1, ..., F6
7723     //                        fs0 ... fs5
7724     static const MCPhysReg FPR32List[] = {RISCV::F8_F, RISCV::F9_F,
7725                                           RISCV::F18_F, RISCV::F19_F,
7726                                           RISCV::F20_F, RISCV::F21_F};
7727     if (unsigned Reg = State.AllocateReg(FPR32List)) {
7728       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7729       return false;
7730     }
7731   }
7732 
7733   if (LocVT == MVT::f64) {
7734     // Pass in STG registers: D1, ..., D6
7735     //                        fs6 ... fs11
7736     static const MCPhysReg FPR64List[] = {RISCV::F22_D, RISCV::F23_D,
7737                                           RISCV::F24_D, RISCV::F25_D,
7738                                           RISCV::F26_D, RISCV::F27_D};
7739     if (unsigned Reg = State.AllocateReg(FPR64List)) {
7740       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7741       return false;
7742     }
7743   }
7744 
7745   report_fatal_error("No registers left in GHC calling convention");
7746   return true;
7747 }
7748 
7749 // Transform physical registers into virtual registers.
7750 SDValue RISCVTargetLowering::LowerFormalArguments(
7751     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
7752     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
7753     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
7754 
7755   MachineFunction &MF = DAG.getMachineFunction();
7756 
7757   switch (CallConv) {
7758   default:
7759     report_fatal_error("Unsupported calling convention");
7760   case CallingConv::C:
7761   case CallingConv::Fast:
7762     break;
7763   case CallingConv::GHC:
7764     if (!MF.getSubtarget().getFeatureBits()[RISCV::FeatureStdExtF] ||
7765         !MF.getSubtarget().getFeatureBits()[RISCV::FeatureStdExtD])
7766       report_fatal_error(
7767         "GHC calling convention requires the F and D instruction set extensions");
7768   }
7769 
7770   const Function &Func = MF.getFunction();
7771   if (Func.hasFnAttribute("interrupt")) {
7772     if (!Func.arg_empty())
7773       report_fatal_error(
7774         "Functions with the interrupt attribute cannot have arguments!");
7775 
7776     StringRef Kind =
7777       MF.getFunction().getFnAttribute("interrupt").getValueAsString();
7778 
7779     if (!(Kind == "user" || Kind == "supervisor" || Kind == "machine"))
7780       report_fatal_error(
7781         "Function interrupt attribute argument not supported!");
7782   }
7783 
7784   EVT PtrVT = getPointerTy(DAG.getDataLayout());
7785   MVT XLenVT = Subtarget.getXLenVT();
7786   unsigned XLenInBytes = Subtarget.getXLen() / 8;
7787   // Used with vargs to acumulate store chains.
7788   std::vector<SDValue> OutChains;
7789 
7790   // Assign locations to all of the incoming arguments.
7791   SmallVector<CCValAssign, 16> ArgLocs;
7792   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
7793 
7794   if (CallConv == CallingConv::GHC)
7795     CCInfo.AnalyzeFormalArguments(Ins, CC_RISCV_GHC);
7796   else
7797     analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false,
7798                      CallConv == CallingConv::Fast ? CC_RISCV_FastCC
7799                                                    : CC_RISCV);
7800 
7801   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
7802     CCValAssign &VA = ArgLocs[i];
7803     SDValue ArgValue;
7804     // Passing f64 on RV32D with a soft float ABI must be handled as a special
7805     // case.
7806     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
7807       ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
7808     else if (VA.isRegLoc())
7809       ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL, *this);
7810     else
7811       ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
7812 
7813     if (VA.getLocInfo() == CCValAssign::Indirect) {
7814       // If the original argument was split and passed by reference (e.g. i128
7815       // on RV32), we need to load all parts of it here (using the same
7816       // address). Vectors may be partly split to registers and partly to the
7817       // stack, in which case the base address is partly offset and subsequent
7818       // stores are relative to that.
7819       InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
7820                                    MachinePointerInfo()));
7821       unsigned ArgIndex = Ins[i].OrigArgIndex;
7822       unsigned ArgPartOffset = Ins[i].PartOffset;
7823       assert(VA.getValVT().isVector() || ArgPartOffset == 0);
7824       while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
7825         CCValAssign &PartVA = ArgLocs[i + 1];
7826         unsigned PartOffset = Ins[i + 1].PartOffset - ArgPartOffset;
7827         SDValue Offset = DAG.getIntPtrConstant(PartOffset, DL);
7828         if (PartVA.getValVT().isScalableVector())
7829           Offset = DAG.getNode(ISD::VSCALE, DL, XLenVT, Offset);
7830         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue, Offset);
7831         InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
7832                                      MachinePointerInfo()));
7833         ++i;
7834       }
7835       continue;
7836     }
7837     InVals.push_back(ArgValue);
7838   }
7839 
7840   if (IsVarArg) {
7841     ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
7842     unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
7843     const TargetRegisterClass *RC = &RISCV::GPRRegClass;
7844     MachineFrameInfo &MFI = MF.getFrameInfo();
7845     MachineRegisterInfo &RegInfo = MF.getRegInfo();
7846     RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
7847 
7848     // Offset of the first variable argument from stack pointer, and size of
7849     // the vararg save area. For now, the varargs save area is either zero or
7850     // large enough to hold a0-a7.
7851     int VaArgOffset, VarArgsSaveSize;
7852 
7853     // If all registers are allocated, then all varargs must be passed on the
7854     // stack and we don't need to save any argregs.
7855     if (ArgRegs.size() == Idx) {
7856       VaArgOffset = CCInfo.getNextStackOffset();
7857       VarArgsSaveSize = 0;
7858     } else {
7859       VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
7860       VaArgOffset = -VarArgsSaveSize;
7861     }
7862 
7863     // Record the frame index of the first variable argument
7864     // which is a value necessary to VASTART.
7865     int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
7866     RVFI->setVarArgsFrameIndex(FI);
7867 
7868     // If saving an odd number of registers then create an extra stack slot to
7869     // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
7870     // offsets to even-numbered registered remain 2*XLEN-aligned.
7871     if (Idx % 2) {
7872       MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes, true);
7873       VarArgsSaveSize += XLenInBytes;
7874     }
7875 
7876     // Copy the integer registers that may have been used for passing varargs
7877     // to the vararg save area.
7878     for (unsigned I = Idx; I < ArgRegs.size();
7879          ++I, VaArgOffset += XLenInBytes) {
7880       const Register Reg = RegInfo.createVirtualRegister(RC);
7881       RegInfo.addLiveIn(ArgRegs[I], Reg);
7882       SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
7883       FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
7884       SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
7885       SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
7886                                    MachinePointerInfo::getFixedStack(MF, FI));
7887       cast<StoreSDNode>(Store.getNode())
7888           ->getMemOperand()
7889           ->setValue((Value *)nullptr);
7890       OutChains.push_back(Store);
7891     }
7892     RVFI->setVarArgsSaveSize(VarArgsSaveSize);
7893   }
7894 
7895   // All stores are grouped in one node to allow the matching between
7896   // the size of Ins and InVals. This only happens for vararg functions.
7897   if (!OutChains.empty()) {
7898     OutChains.push_back(Chain);
7899     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
7900   }
7901 
7902   return Chain;
7903 }
7904 
7905 /// isEligibleForTailCallOptimization - Check whether the call is eligible
7906 /// for tail call optimization.
7907 /// Note: This is modelled after ARM's IsEligibleForTailCallOptimization.
7908 bool RISCVTargetLowering::isEligibleForTailCallOptimization(
7909     CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
7910     const SmallVector<CCValAssign, 16> &ArgLocs) const {
7911 
7912   auto &Callee = CLI.Callee;
7913   auto CalleeCC = CLI.CallConv;
7914   auto &Outs = CLI.Outs;
7915   auto &Caller = MF.getFunction();
7916   auto CallerCC = Caller.getCallingConv();
7917 
7918   // Exception-handling functions need a special set of instructions to
7919   // indicate a return to the hardware. Tail-calling another function would
7920   // probably break this.
7921   // TODO: The "interrupt" attribute isn't currently defined by RISC-V. This
7922   // should be expanded as new function attributes are introduced.
7923   if (Caller.hasFnAttribute("interrupt"))
7924     return false;
7925 
7926   // Do not tail call opt if the stack is used to pass parameters.
7927   if (CCInfo.getNextStackOffset() != 0)
7928     return false;
7929 
7930   // Do not tail call opt if any parameters need to be passed indirectly.
7931   // Since long doubles (fp128) and i128 are larger than 2*XLEN, they are
7932   // passed indirectly. So the address of the value will be passed in a
7933   // register, or if not available, then the address is put on the stack. In
7934   // order to pass indirectly, space on the stack often needs to be allocated
7935   // in order to store the value. In this case the CCInfo.getNextStackOffset()
7936   // != 0 check is not enough and we need to check if any CCValAssign ArgsLocs
7937   // are passed CCValAssign::Indirect.
7938   for (auto &VA : ArgLocs)
7939     if (VA.getLocInfo() == CCValAssign::Indirect)
7940       return false;
7941 
7942   // Do not tail call opt if either caller or callee uses struct return
7943   // semantics.
7944   auto IsCallerStructRet = Caller.hasStructRetAttr();
7945   auto IsCalleeStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
7946   if (IsCallerStructRet || IsCalleeStructRet)
7947     return false;
7948 
7949   // Externally-defined functions with weak linkage should not be
7950   // tail-called. The behaviour of branch instructions in this situation (as
7951   // used for tail calls) is implementation-defined, so we cannot rely on the
7952   // linker replacing the tail call with a return.
7953   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
7954     const GlobalValue *GV = G->getGlobal();
7955     if (GV->hasExternalWeakLinkage())
7956       return false;
7957   }
7958 
7959   // The callee has to preserve all registers the caller needs to preserve.
7960   const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
7961   const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
7962   if (CalleeCC != CallerCC) {
7963     const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
7964     if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
7965       return false;
7966   }
7967 
7968   // Byval parameters hand the function a pointer directly into the stack area
7969   // we want to reuse during a tail call. Working around this *is* possible
7970   // but less efficient and uglier in LowerCall.
7971   for (auto &Arg : Outs)
7972     if (Arg.Flags.isByVal())
7973       return false;
7974 
7975   return true;
7976 }
7977 
7978 static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG) {
7979   return DAG.getDataLayout().getPrefTypeAlign(
7980       VT.getTypeForEVT(*DAG.getContext()));
7981 }
7982 
7983 // Lower a call to a callseq_start + CALL + callseq_end chain, and add input
7984 // and output parameter nodes.
7985 SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
7986                                        SmallVectorImpl<SDValue> &InVals) const {
7987   SelectionDAG &DAG = CLI.DAG;
7988   SDLoc &DL = CLI.DL;
7989   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
7990   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
7991   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
7992   SDValue Chain = CLI.Chain;
7993   SDValue Callee = CLI.Callee;
7994   bool &IsTailCall = CLI.IsTailCall;
7995   CallingConv::ID CallConv = CLI.CallConv;
7996   bool IsVarArg = CLI.IsVarArg;
7997   EVT PtrVT = getPointerTy(DAG.getDataLayout());
7998   MVT XLenVT = Subtarget.getXLenVT();
7999 
8000   MachineFunction &MF = DAG.getMachineFunction();
8001 
8002   // Analyze the operands of the call, assigning locations to each operand.
8003   SmallVector<CCValAssign, 16> ArgLocs;
8004   CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
8005 
8006   if (CallConv == CallingConv::GHC)
8007     ArgCCInfo.AnalyzeCallOperands(Outs, CC_RISCV_GHC);
8008   else
8009     analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI,
8010                       CallConv == CallingConv::Fast ? CC_RISCV_FastCC
8011                                                     : CC_RISCV);
8012 
8013   // Check if it's really possible to do a tail call.
8014   if (IsTailCall)
8015     IsTailCall = isEligibleForTailCallOptimization(ArgCCInfo, CLI, MF, ArgLocs);
8016 
8017   if (IsTailCall)
8018     ++NumTailCalls;
8019   else if (CLI.CB && CLI.CB->isMustTailCall())
8020     report_fatal_error("failed to perform tail call elimination on a call "
8021                        "site marked musttail");
8022 
8023   // Get a count of how many bytes are to be pushed on the stack.
8024   unsigned NumBytes = ArgCCInfo.getNextStackOffset();
8025 
8026   // Create local copies for byval args
8027   SmallVector<SDValue, 8> ByValArgs;
8028   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
8029     ISD::ArgFlagsTy Flags = Outs[i].Flags;
8030     if (!Flags.isByVal())
8031       continue;
8032 
8033     SDValue Arg = OutVals[i];
8034     unsigned Size = Flags.getByValSize();
8035     Align Alignment = Flags.getNonZeroByValAlign();
8036 
8037     int FI =
8038         MF.getFrameInfo().CreateStackObject(Size, Alignment, /*isSS=*/false);
8039     SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
8040     SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
8041 
8042     Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Alignment,
8043                           /*IsVolatile=*/false,
8044                           /*AlwaysInline=*/false, IsTailCall,
8045                           MachinePointerInfo(), MachinePointerInfo());
8046     ByValArgs.push_back(FIPtr);
8047   }
8048 
8049   if (!IsTailCall)
8050     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
8051 
8052   // Copy argument values to their designated locations.
8053   SmallVector<std::pair<Register, SDValue>, 8> RegsToPass;
8054   SmallVector<SDValue, 8> MemOpChains;
8055   SDValue StackPtr;
8056   for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
8057     CCValAssign &VA = ArgLocs[i];
8058     SDValue ArgValue = OutVals[i];
8059     ISD::ArgFlagsTy Flags = Outs[i].Flags;
8060 
8061     // Handle passing f64 on RV32D with a soft float ABI as a special case.
8062     bool IsF64OnRV32DSoftABI =
8063         VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
8064     if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
8065       SDValue SplitF64 = DAG.getNode(
8066           RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
8067       SDValue Lo = SplitF64.getValue(0);
8068       SDValue Hi = SplitF64.getValue(1);
8069 
8070       Register RegLo = VA.getLocReg();
8071       RegsToPass.push_back(std::make_pair(RegLo, Lo));
8072 
8073       if (RegLo == RISCV::X17) {
8074         // Second half of f64 is passed on the stack.
8075         // Work out the address of the stack slot.
8076         if (!StackPtr.getNode())
8077           StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
8078         // Emit the store.
8079         MemOpChains.push_back(
8080             DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
8081       } else {
8082         // Second half of f64 is passed in another GPR.
8083         assert(RegLo < RISCV::X31 && "Invalid register pair");
8084         Register RegHigh = RegLo + 1;
8085         RegsToPass.push_back(std::make_pair(RegHigh, Hi));
8086       }
8087       continue;
8088     }
8089 
8090     // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
8091     // as any other MemLoc.
8092 
8093     // Promote the value if needed.
8094     // For now, only handle fully promoted and indirect arguments.
8095     if (VA.getLocInfo() == CCValAssign::Indirect) {
8096       // Store the argument in a stack slot and pass its address.
8097       Align StackAlign =
8098           std::max(getPrefTypeAlign(Outs[i].ArgVT, DAG),
8099                    getPrefTypeAlign(ArgValue.getValueType(), DAG));
8100       TypeSize StoredSize = ArgValue.getValueType().getStoreSize();
8101       // If the original argument was split (e.g. i128), we need
8102       // to store the required parts of it here (and pass just one address).
8103       // Vectors may be partly split to registers and partly to the stack, in
8104       // which case the base address is partly offset and subsequent stores are
8105       // relative to that.
8106       unsigned ArgIndex = Outs[i].OrigArgIndex;
8107       unsigned ArgPartOffset = Outs[i].PartOffset;
8108       assert(VA.getValVT().isVector() || ArgPartOffset == 0);
8109       // Calculate the total size to store. We don't have access to what we're
8110       // actually storing other than performing the loop and collecting the
8111       // info.
8112       SmallVector<std::pair<SDValue, SDValue>> Parts;
8113       while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
8114         SDValue PartValue = OutVals[i + 1];
8115         unsigned PartOffset = Outs[i + 1].PartOffset - ArgPartOffset;
8116         SDValue Offset = DAG.getIntPtrConstant(PartOffset, DL);
8117         EVT PartVT = PartValue.getValueType();
8118         if (PartVT.isScalableVector())
8119           Offset = DAG.getNode(ISD::VSCALE, DL, XLenVT, Offset);
8120         StoredSize += PartVT.getStoreSize();
8121         StackAlign = std::max(StackAlign, getPrefTypeAlign(PartVT, DAG));
8122         Parts.push_back(std::make_pair(PartValue, Offset));
8123         ++i;
8124       }
8125       SDValue SpillSlot = DAG.CreateStackTemporary(StoredSize, StackAlign);
8126       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
8127       MemOpChains.push_back(
8128           DAG.getStore(Chain, DL, ArgValue, SpillSlot,
8129                        MachinePointerInfo::getFixedStack(MF, FI)));
8130       for (const auto &Part : Parts) {
8131         SDValue PartValue = Part.first;
8132         SDValue PartOffset = Part.second;
8133         SDValue Address =
8134             DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot, PartOffset);
8135         MemOpChains.push_back(
8136             DAG.getStore(Chain, DL, PartValue, Address,
8137                          MachinePointerInfo::getFixedStack(MF, FI)));
8138       }
8139       ArgValue = SpillSlot;
8140     } else {
8141       ArgValue = convertValVTToLocVT(DAG, ArgValue, VA, DL, Subtarget);
8142     }
8143 
8144     // Use local copy if it is a byval arg.
8145     if (Flags.isByVal())
8146       ArgValue = ByValArgs[j++];
8147 
8148     if (VA.isRegLoc()) {
8149       // Queue up the argument copies and emit them at the end.
8150       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
8151     } else {
8152       assert(VA.isMemLoc() && "Argument not register or memory");
8153       assert(!IsTailCall && "Tail call not allowed if stack is used "
8154                             "for passing parameters");
8155 
8156       // Work out the address of the stack slot.
8157       if (!StackPtr.getNode())
8158         StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
8159       SDValue Address =
8160           DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
8161                       DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
8162 
8163       // Emit the store.
8164       MemOpChains.push_back(
8165           DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
8166     }
8167   }
8168 
8169   // Join the stores, which are independent of one another.
8170   if (!MemOpChains.empty())
8171     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
8172 
8173   SDValue Glue;
8174 
8175   // Build a sequence of copy-to-reg nodes, chained and glued together.
8176   for (auto &Reg : RegsToPass) {
8177     Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
8178     Glue = Chain.getValue(1);
8179   }
8180 
8181   // Validate that none of the argument registers have been marked as
8182   // reserved, if so report an error. Do the same for the return address if this
8183   // is not a tailcall.
8184   validateCCReservedRegs(RegsToPass, MF);
8185   if (!IsTailCall &&
8186       MF.getSubtarget<RISCVSubtarget>().isRegisterReservedByUser(RISCV::X1))
8187     MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
8188         MF.getFunction(),
8189         "Return address register required, but has been reserved."});
8190 
8191   // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
8192   // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
8193   // split it and then direct call can be matched by PseudoCALL.
8194   if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
8195     const GlobalValue *GV = S->getGlobal();
8196 
8197     unsigned OpFlags = RISCVII::MO_CALL;
8198     if (!getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV))
8199       OpFlags = RISCVII::MO_PLT;
8200 
8201     Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, OpFlags);
8202   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
8203     unsigned OpFlags = RISCVII::MO_CALL;
8204 
8205     if (!getTargetMachine().shouldAssumeDSOLocal(*MF.getFunction().getParent(),
8206                                                  nullptr))
8207       OpFlags = RISCVII::MO_PLT;
8208 
8209     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, OpFlags);
8210   }
8211 
8212   // The first call operand is the chain and the second is the target address.
8213   SmallVector<SDValue, 8> Ops;
8214   Ops.push_back(Chain);
8215   Ops.push_back(Callee);
8216 
8217   // Add argument registers to the end of the list so that they are
8218   // known live into the call.
8219   for (auto &Reg : RegsToPass)
8220     Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
8221 
8222   if (!IsTailCall) {
8223     // Add a register mask operand representing the call-preserved registers.
8224     const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
8225     const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
8226     assert(Mask && "Missing call preserved mask for calling convention");
8227     Ops.push_back(DAG.getRegisterMask(Mask));
8228   }
8229 
8230   // Glue the call to the argument copies, if any.
8231   if (Glue.getNode())
8232     Ops.push_back(Glue);
8233 
8234   // Emit the call.
8235   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
8236 
8237   if (IsTailCall) {
8238     MF.getFrameInfo().setHasTailCall();
8239     return DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops);
8240   }
8241 
8242   Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
8243   DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge);
8244   Glue = Chain.getValue(1);
8245 
8246   // Mark the end of the call, which is glued to the call itself.
8247   Chain = DAG.getCALLSEQ_END(Chain,
8248                              DAG.getConstant(NumBytes, DL, PtrVT, true),
8249                              DAG.getConstant(0, DL, PtrVT, true),
8250                              Glue, DL);
8251   Glue = Chain.getValue(1);
8252 
8253   // Assign locations to each value returned by this call.
8254   SmallVector<CCValAssign, 16> RVLocs;
8255   CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
8256   analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true, CC_RISCV);
8257 
8258   // Copy all of the result registers out of their specified physreg.
8259   for (auto &VA : RVLocs) {
8260     // Copy the value out
8261     SDValue RetValue =
8262         DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
8263     // Glue the RetValue to the end of the call sequence
8264     Chain = RetValue.getValue(1);
8265     Glue = RetValue.getValue(2);
8266 
8267     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
8268       assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
8269       SDValue RetValue2 =
8270           DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
8271       Chain = RetValue2.getValue(1);
8272       Glue = RetValue2.getValue(2);
8273       RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
8274                              RetValue2);
8275     }
8276 
8277     RetValue = convertLocVTToValVT(DAG, RetValue, VA, DL, Subtarget);
8278 
8279     InVals.push_back(RetValue);
8280   }
8281 
8282   return Chain;
8283 }
8284 
8285 bool RISCVTargetLowering::CanLowerReturn(
8286     CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
8287     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
8288   SmallVector<CCValAssign, 16> RVLocs;
8289   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
8290 
8291   Optional<unsigned> FirstMaskArgument;
8292   if (Subtarget.hasStdExtV())
8293     FirstMaskArgument = preAssignMask(Outs);
8294 
8295   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
8296     MVT VT = Outs[i].VT;
8297     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
8298     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
8299     if (CC_RISCV(MF.getDataLayout(), ABI, i, VT, VT, CCValAssign::Full,
8300                  ArgFlags, CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr,
8301                  *this, FirstMaskArgument))
8302       return false;
8303   }
8304   return true;
8305 }
8306 
8307 SDValue
8308 RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
8309                                  bool IsVarArg,
8310                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
8311                                  const SmallVectorImpl<SDValue> &OutVals,
8312                                  const SDLoc &DL, SelectionDAG &DAG) const {
8313   const MachineFunction &MF = DAG.getMachineFunction();
8314   const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
8315 
8316   // Stores the assignment of the return value to a location.
8317   SmallVector<CCValAssign, 16> RVLocs;
8318 
8319   // Info about the registers and stack slot.
8320   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
8321                  *DAG.getContext());
8322 
8323   analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
8324                     nullptr, CC_RISCV);
8325 
8326   if (CallConv == CallingConv::GHC && !RVLocs.empty())
8327     report_fatal_error("GHC functions return void only");
8328 
8329   SDValue Glue;
8330   SmallVector<SDValue, 4> RetOps(1, Chain);
8331 
8332   // Copy the result values into the output registers.
8333   for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
8334     SDValue Val = OutVals[i];
8335     CCValAssign &VA = RVLocs[i];
8336     assert(VA.isRegLoc() && "Can only return in registers!");
8337 
8338     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
8339       // Handle returning f64 on RV32D with a soft float ABI.
8340       assert(VA.isRegLoc() && "Expected return via registers");
8341       SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
8342                                      DAG.getVTList(MVT::i32, MVT::i32), Val);
8343       SDValue Lo = SplitF64.getValue(0);
8344       SDValue Hi = SplitF64.getValue(1);
8345       Register RegLo = VA.getLocReg();
8346       assert(RegLo < RISCV::X31 && "Invalid register pair");
8347       Register RegHi = RegLo + 1;
8348 
8349       if (STI.isRegisterReservedByUser(RegLo) ||
8350           STI.isRegisterReservedByUser(RegHi))
8351         MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
8352             MF.getFunction(),
8353             "Return value register required, but has been reserved."});
8354 
8355       Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
8356       Glue = Chain.getValue(1);
8357       RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
8358       Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
8359       Glue = Chain.getValue(1);
8360       RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
8361     } else {
8362       // Handle a 'normal' return.
8363       Val = convertValVTToLocVT(DAG, Val, VA, DL, Subtarget);
8364       Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
8365 
8366       if (STI.isRegisterReservedByUser(VA.getLocReg()))
8367         MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
8368             MF.getFunction(),
8369             "Return value register required, but has been reserved."});
8370 
8371       // Guarantee that all emitted copies are stuck together.
8372       Glue = Chain.getValue(1);
8373       RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
8374     }
8375   }
8376 
8377   RetOps[0] = Chain; // Update chain.
8378 
8379   // Add the glue node if we have it.
8380   if (Glue.getNode()) {
8381     RetOps.push_back(Glue);
8382   }
8383 
8384   unsigned RetOpc = RISCVISD::RET_FLAG;
8385   // Interrupt service routines use different return instructions.
8386   const Function &Func = DAG.getMachineFunction().getFunction();
8387   if (Func.hasFnAttribute("interrupt")) {
8388     if (!Func.getReturnType()->isVoidTy())
8389       report_fatal_error(
8390           "Functions with the interrupt attribute must have void return type!");
8391 
8392     MachineFunction &MF = DAG.getMachineFunction();
8393     StringRef Kind =
8394       MF.getFunction().getFnAttribute("interrupt").getValueAsString();
8395 
8396     if (Kind == "user")
8397       RetOpc = RISCVISD::URET_FLAG;
8398     else if (Kind == "supervisor")
8399       RetOpc = RISCVISD::SRET_FLAG;
8400     else
8401       RetOpc = RISCVISD::MRET_FLAG;
8402   }
8403 
8404   return DAG.getNode(RetOpc, DL, MVT::Other, RetOps);
8405 }
8406 
8407 void RISCVTargetLowering::validateCCReservedRegs(
8408     const SmallVectorImpl<std::pair<llvm::Register, llvm::SDValue>> &Regs,
8409     MachineFunction &MF) const {
8410   const Function &F = MF.getFunction();
8411   const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
8412 
8413   if (llvm::any_of(Regs, [&STI](auto Reg) {
8414         return STI.isRegisterReservedByUser(Reg.first);
8415       }))
8416     F.getContext().diagnose(DiagnosticInfoUnsupported{
8417         F, "Argument register required, but has been reserved."});
8418 }
8419 
8420 bool RISCVTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
8421   return CI->isTailCall();
8422 }
8423 
8424 const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
8425 #define NODE_NAME_CASE(NODE)                                                   \
8426   case RISCVISD::NODE:                                                         \
8427     return "RISCVISD::" #NODE;
8428   // clang-format off
8429   switch ((RISCVISD::NodeType)Opcode) {
8430   case RISCVISD::FIRST_NUMBER:
8431     break;
8432   NODE_NAME_CASE(RET_FLAG)
8433   NODE_NAME_CASE(URET_FLAG)
8434   NODE_NAME_CASE(SRET_FLAG)
8435   NODE_NAME_CASE(MRET_FLAG)
8436   NODE_NAME_CASE(CALL)
8437   NODE_NAME_CASE(SELECT_CC)
8438   NODE_NAME_CASE(BR_CC)
8439   NODE_NAME_CASE(BuildPairF64)
8440   NODE_NAME_CASE(SplitF64)
8441   NODE_NAME_CASE(TAIL)
8442   NODE_NAME_CASE(MULHSU)
8443   NODE_NAME_CASE(SLLW)
8444   NODE_NAME_CASE(SRAW)
8445   NODE_NAME_CASE(SRLW)
8446   NODE_NAME_CASE(DIVW)
8447   NODE_NAME_CASE(DIVUW)
8448   NODE_NAME_CASE(REMUW)
8449   NODE_NAME_CASE(ROLW)
8450   NODE_NAME_CASE(RORW)
8451   NODE_NAME_CASE(CLZW)
8452   NODE_NAME_CASE(CTZW)
8453   NODE_NAME_CASE(FSLW)
8454   NODE_NAME_CASE(FSRW)
8455   NODE_NAME_CASE(FSL)
8456   NODE_NAME_CASE(FSR)
8457   NODE_NAME_CASE(FMV_H_X)
8458   NODE_NAME_CASE(FMV_X_ANYEXTH)
8459   NODE_NAME_CASE(FMV_W_X_RV64)
8460   NODE_NAME_CASE(FMV_X_ANYEXTW_RV64)
8461   NODE_NAME_CASE(FCVT_X_RTZ)
8462   NODE_NAME_CASE(FCVT_XU_RTZ)
8463   NODE_NAME_CASE(FCVT_W_RTZ_RV64)
8464   NODE_NAME_CASE(FCVT_WU_RTZ_RV64)
8465   NODE_NAME_CASE(READ_CYCLE_WIDE)
8466   NODE_NAME_CASE(GREV)
8467   NODE_NAME_CASE(GREVW)
8468   NODE_NAME_CASE(GORC)
8469   NODE_NAME_CASE(GORCW)
8470   NODE_NAME_CASE(SHFL)
8471   NODE_NAME_CASE(SHFLW)
8472   NODE_NAME_CASE(UNSHFL)
8473   NODE_NAME_CASE(UNSHFLW)
8474   NODE_NAME_CASE(BCOMPRESS)
8475   NODE_NAME_CASE(BCOMPRESSW)
8476   NODE_NAME_CASE(BDECOMPRESS)
8477   NODE_NAME_CASE(BDECOMPRESSW)
8478   NODE_NAME_CASE(VMV_V_X_VL)
8479   NODE_NAME_CASE(VFMV_V_F_VL)
8480   NODE_NAME_CASE(VMV_X_S)
8481   NODE_NAME_CASE(VMV_S_X_VL)
8482   NODE_NAME_CASE(VFMV_S_F_VL)
8483   NODE_NAME_CASE(SPLAT_VECTOR_I64)
8484   NODE_NAME_CASE(SPLAT_VECTOR_SPLIT_I64_VL)
8485   NODE_NAME_CASE(READ_VLENB)
8486   NODE_NAME_CASE(TRUNCATE_VECTOR_VL)
8487   NODE_NAME_CASE(VSLIDEUP_VL)
8488   NODE_NAME_CASE(VSLIDE1UP_VL)
8489   NODE_NAME_CASE(VSLIDEDOWN_VL)
8490   NODE_NAME_CASE(VSLIDE1DOWN_VL)
8491   NODE_NAME_CASE(VID_VL)
8492   NODE_NAME_CASE(VFNCVT_ROD_VL)
8493   NODE_NAME_CASE(VECREDUCE_ADD_VL)
8494   NODE_NAME_CASE(VECREDUCE_UMAX_VL)
8495   NODE_NAME_CASE(VECREDUCE_SMAX_VL)
8496   NODE_NAME_CASE(VECREDUCE_UMIN_VL)
8497   NODE_NAME_CASE(VECREDUCE_SMIN_VL)
8498   NODE_NAME_CASE(VECREDUCE_AND_VL)
8499   NODE_NAME_CASE(VECREDUCE_OR_VL)
8500   NODE_NAME_CASE(VECREDUCE_XOR_VL)
8501   NODE_NAME_CASE(VECREDUCE_FADD_VL)
8502   NODE_NAME_CASE(VECREDUCE_SEQ_FADD_VL)
8503   NODE_NAME_CASE(VECREDUCE_FMIN_VL)
8504   NODE_NAME_CASE(VECREDUCE_FMAX_VL)
8505   NODE_NAME_CASE(ADD_VL)
8506   NODE_NAME_CASE(AND_VL)
8507   NODE_NAME_CASE(MUL_VL)
8508   NODE_NAME_CASE(OR_VL)
8509   NODE_NAME_CASE(SDIV_VL)
8510   NODE_NAME_CASE(SHL_VL)
8511   NODE_NAME_CASE(SREM_VL)
8512   NODE_NAME_CASE(SRA_VL)
8513   NODE_NAME_CASE(SRL_VL)
8514   NODE_NAME_CASE(SUB_VL)
8515   NODE_NAME_CASE(UDIV_VL)
8516   NODE_NAME_CASE(UREM_VL)
8517   NODE_NAME_CASE(XOR_VL)
8518   NODE_NAME_CASE(SADDSAT_VL)
8519   NODE_NAME_CASE(UADDSAT_VL)
8520   NODE_NAME_CASE(SSUBSAT_VL)
8521   NODE_NAME_CASE(USUBSAT_VL)
8522   NODE_NAME_CASE(FADD_VL)
8523   NODE_NAME_CASE(FSUB_VL)
8524   NODE_NAME_CASE(FMUL_VL)
8525   NODE_NAME_CASE(FDIV_VL)
8526   NODE_NAME_CASE(FNEG_VL)
8527   NODE_NAME_CASE(FABS_VL)
8528   NODE_NAME_CASE(FSQRT_VL)
8529   NODE_NAME_CASE(FMA_VL)
8530   NODE_NAME_CASE(FCOPYSIGN_VL)
8531   NODE_NAME_CASE(SMIN_VL)
8532   NODE_NAME_CASE(SMAX_VL)
8533   NODE_NAME_CASE(UMIN_VL)
8534   NODE_NAME_CASE(UMAX_VL)
8535   NODE_NAME_CASE(FMINNUM_VL)
8536   NODE_NAME_CASE(FMAXNUM_VL)
8537   NODE_NAME_CASE(MULHS_VL)
8538   NODE_NAME_CASE(MULHU_VL)
8539   NODE_NAME_CASE(FP_TO_SINT_VL)
8540   NODE_NAME_CASE(FP_TO_UINT_VL)
8541   NODE_NAME_CASE(SINT_TO_FP_VL)
8542   NODE_NAME_CASE(UINT_TO_FP_VL)
8543   NODE_NAME_CASE(FP_EXTEND_VL)
8544   NODE_NAME_CASE(FP_ROUND_VL)
8545   NODE_NAME_CASE(VWMUL_VL)
8546   NODE_NAME_CASE(VWMULU_VL)
8547   NODE_NAME_CASE(SETCC_VL)
8548   NODE_NAME_CASE(VSELECT_VL)
8549   NODE_NAME_CASE(VMAND_VL)
8550   NODE_NAME_CASE(VMOR_VL)
8551   NODE_NAME_CASE(VMXOR_VL)
8552   NODE_NAME_CASE(VMCLR_VL)
8553   NODE_NAME_CASE(VMSET_VL)
8554   NODE_NAME_CASE(VRGATHER_VX_VL)
8555   NODE_NAME_CASE(VRGATHER_VV_VL)
8556   NODE_NAME_CASE(VRGATHEREI16_VV_VL)
8557   NODE_NAME_CASE(VSEXT_VL)
8558   NODE_NAME_CASE(VZEXT_VL)
8559   NODE_NAME_CASE(VPOPC_VL)
8560   NODE_NAME_CASE(VLE_VL)
8561   NODE_NAME_CASE(VSE_VL)
8562   NODE_NAME_CASE(READ_CSR)
8563   NODE_NAME_CASE(WRITE_CSR)
8564   NODE_NAME_CASE(SWAP_CSR)
8565   }
8566   // clang-format on
8567   return nullptr;
8568 #undef NODE_NAME_CASE
8569 }
8570 
8571 /// getConstraintType - Given a constraint letter, return the type of
8572 /// constraint it is for this target.
8573 RISCVTargetLowering::ConstraintType
8574 RISCVTargetLowering::getConstraintType(StringRef Constraint) const {
8575   if (Constraint.size() == 1) {
8576     switch (Constraint[0]) {
8577     default:
8578       break;
8579     case 'f':
8580       return C_RegisterClass;
8581     case 'I':
8582     case 'J':
8583     case 'K':
8584       return C_Immediate;
8585     case 'A':
8586       return C_Memory;
8587     case 'S': // A symbolic address
8588       return C_Other;
8589     }
8590   } else {
8591     if (Constraint == "vr" || Constraint == "vm")
8592       return C_RegisterClass;
8593   }
8594   return TargetLowering::getConstraintType(Constraint);
8595 }
8596 
8597 std::pair<unsigned, const TargetRegisterClass *>
8598 RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
8599                                                   StringRef Constraint,
8600                                                   MVT VT) const {
8601   // First, see if this is a constraint that directly corresponds to a
8602   // RISCV register class.
8603   if (Constraint.size() == 1) {
8604     switch (Constraint[0]) {
8605     case 'r':
8606       return std::make_pair(0U, &RISCV::GPRRegClass);
8607     case 'f':
8608       if (Subtarget.hasStdExtZfh() && VT == MVT::f16)
8609         return std::make_pair(0U, &RISCV::FPR16RegClass);
8610       if (Subtarget.hasStdExtF() && VT == MVT::f32)
8611         return std::make_pair(0U, &RISCV::FPR32RegClass);
8612       if (Subtarget.hasStdExtD() && VT == MVT::f64)
8613         return std::make_pair(0U, &RISCV::FPR64RegClass);
8614       break;
8615     default:
8616       break;
8617     }
8618   } else {
8619     if (Constraint == "vr") {
8620       for (const auto *RC : {&RISCV::VRRegClass, &RISCV::VRM2RegClass,
8621                              &RISCV::VRM4RegClass, &RISCV::VRM8RegClass}) {
8622         if (TRI->isTypeLegalForClass(*RC, VT.SimpleTy))
8623           return std::make_pair(0U, RC);
8624       }
8625     } else if (Constraint == "vm") {
8626       if (TRI->isTypeLegalForClass(RISCV::VMRegClass, VT.SimpleTy))
8627         return std::make_pair(0U, &RISCV::VMRegClass);
8628     }
8629   }
8630 
8631   // Clang will correctly decode the usage of register name aliases into their
8632   // official names. However, other frontends like `rustc` do not. This allows
8633   // users of these frontends to use the ABI names for registers in LLVM-style
8634   // register constraints.
8635   unsigned XRegFromAlias = StringSwitch<unsigned>(Constraint.lower())
8636                                .Case("{zero}", RISCV::X0)
8637                                .Case("{ra}", RISCV::X1)
8638                                .Case("{sp}", RISCV::X2)
8639                                .Case("{gp}", RISCV::X3)
8640                                .Case("{tp}", RISCV::X4)
8641                                .Case("{t0}", RISCV::X5)
8642                                .Case("{t1}", RISCV::X6)
8643                                .Case("{t2}", RISCV::X7)
8644                                .Cases("{s0}", "{fp}", RISCV::X8)
8645                                .Case("{s1}", RISCV::X9)
8646                                .Case("{a0}", RISCV::X10)
8647                                .Case("{a1}", RISCV::X11)
8648                                .Case("{a2}", RISCV::X12)
8649                                .Case("{a3}", RISCV::X13)
8650                                .Case("{a4}", RISCV::X14)
8651                                .Case("{a5}", RISCV::X15)
8652                                .Case("{a6}", RISCV::X16)
8653                                .Case("{a7}", RISCV::X17)
8654                                .Case("{s2}", RISCV::X18)
8655                                .Case("{s3}", RISCV::X19)
8656                                .Case("{s4}", RISCV::X20)
8657                                .Case("{s5}", RISCV::X21)
8658                                .Case("{s6}", RISCV::X22)
8659                                .Case("{s7}", RISCV::X23)
8660                                .Case("{s8}", RISCV::X24)
8661                                .Case("{s9}", RISCV::X25)
8662                                .Case("{s10}", RISCV::X26)
8663                                .Case("{s11}", RISCV::X27)
8664                                .Case("{t3}", RISCV::X28)
8665                                .Case("{t4}", RISCV::X29)
8666                                .Case("{t5}", RISCV::X30)
8667                                .Case("{t6}", RISCV::X31)
8668                                .Default(RISCV::NoRegister);
8669   if (XRegFromAlias != RISCV::NoRegister)
8670     return std::make_pair(XRegFromAlias, &RISCV::GPRRegClass);
8671 
8672   // Since TargetLowering::getRegForInlineAsmConstraint uses the name of the
8673   // TableGen record rather than the AsmName to choose registers for InlineAsm
8674   // constraints, plus we want to match those names to the widest floating point
8675   // register type available, manually select floating point registers here.
8676   //
8677   // The second case is the ABI name of the register, so that frontends can also
8678   // use the ABI names in register constraint lists.
8679   if (Subtarget.hasStdExtF()) {
8680     unsigned FReg = StringSwitch<unsigned>(Constraint.lower())
8681                         .Cases("{f0}", "{ft0}", RISCV::F0_F)
8682                         .Cases("{f1}", "{ft1}", RISCV::F1_F)
8683                         .Cases("{f2}", "{ft2}", RISCV::F2_F)
8684                         .Cases("{f3}", "{ft3}", RISCV::F3_F)
8685                         .Cases("{f4}", "{ft4}", RISCV::F4_F)
8686                         .Cases("{f5}", "{ft5}", RISCV::F5_F)
8687                         .Cases("{f6}", "{ft6}", RISCV::F6_F)
8688                         .Cases("{f7}", "{ft7}", RISCV::F7_F)
8689                         .Cases("{f8}", "{fs0}", RISCV::F8_F)
8690                         .Cases("{f9}", "{fs1}", RISCV::F9_F)
8691                         .Cases("{f10}", "{fa0}", RISCV::F10_F)
8692                         .Cases("{f11}", "{fa1}", RISCV::F11_F)
8693                         .Cases("{f12}", "{fa2}", RISCV::F12_F)
8694                         .Cases("{f13}", "{fa3}", RISCV::F13_F)
8695                         .Cases("{f14}", "{fa4}", RISCV::F14_F)
8696                         .Cases("{f15}", "{fa5}", RISCV::F15_F)
8697                         .Cases("{f16}", "{fa6}", RISCV::F16_F)
8698                         .Cases("{f17}", "{fa7}", RISCV::F17_F)
8699                         .Cases("{f18}", "{fs2}", RISCV::F18_F)
8700                         .Cases("{f19}", "{fs3}", RISCV::F19_F)
8701                         .Cases("{f20}", "{fs4}", RISCV::F20_F)
8702                         .Cases("{f21}", "{fs5}", RISCV::F21_F)
8703                         .Cases("{f22}", "{fs6}", RISCV::F22_F)
8704                         .Cases("{f23}", "{fs7}", RISCV::F23_F)
8705                         .Cases("{f24}", "{fs8}", RISCV::F24_F)
8706                         .Cases("{f25}", "{fs9}", RISCV::F25_F)
8707                         .Cases("{f26}", "{fs10}", RISCV::F26_F)
8708                         .Cases("{f27}", "{fs11}", RISCV::F27_F)
8709                         .Cases("{f28}", "{ft8}", RISCV::F28_F)
8710                         .Cases("{f29}", "{ft9}", RISCV::F29_F)
8711                         .Cases("{f30}", "{ft10}", RISCV::F30_F)
8712                         .Cases("{f31}", "{ft11}", RISCV::F31_F)
8713                         .Default(RISCV::NoRegister);
8714     if (FReg != RISCV::NoRegister) {
8715       assert(RISCV::F0_F <= FReg && FReg <= RISCV::F31_F && "Unknown fp-reg");
8716       if (Subtarget.hasStdExtD()) {
8717         unsigned RegNo = FReg - RISCV::F0_F;
8718         unsigned DReg = RISCV::F0_D + RegNo;
8719         return std::make_pair(DReg, &RISCV::FPR64RegClass);
8720       }
8721       return std::make_pair(FReg, &RISCV::FPR32RegClass);
8722     }
8723   }
8724 
8725   if (Subtarget.hasStdExtV()) {
8726     Register VReg = StringSwitch<Register>(Constraint.lower())
8727                         .Case("{v0}", RISCV::V0)
8728                         .Case("{v1}", RISCV::V1)
8729                         .Case("{v2}", RISCV::V2)
8730                         .Case("{v3}", RISCV::V3)
8731                         .Case("{v4}", RISCV::V4)
8732                         .Case("{v5}", RISCV::V5)
8733                         .Case("{v6}", RISCV::V6)
8734                         .Case("{v7}", RISCV::V7)
8735                         .Case("{v8}", RISCV::V8)
8736                         .Case("{v9}", RISCV::V9)
8737                         .Case("{v10}", RISCV::V10)
8738                         .Case("{v11}", RISCV::V11)
8739                         .Case("{v12}", RISCV::V12)
8740                         .Case("{v13}", RISCV::V13)
8741                         .Case("{v14}", RISCV::V14)
8742                         .Case("{v15}", RISCV::V15)
8743                         .Case("{v16}", RISCV::V16)
8744                         .Case("{v17}", RISCV::V17)
8745                         .Case("{v18}", RISCV::V18)
8746                         .Case("{v19}", RISCV::V19)
8747                         .Case("{v20}", RISCV::V20)
8748                         .Case("{v21}", RISCV::V21)
8749                         .Case("{v22}", RISCV::V22)
8750                         .Case("{v23}", RISCV::V23)
8751                         .Case("{v24}", RISCV::V24)
8752                         .Case("{v25}", RISCV::V25)
8753                         .Case("{v26}", RISCV::V26)
8754                         .Case("{v27}", RISCV::V27)
8755                         .Case("{v28}", RISCV::V28)
8756                         .Case("{v29}", RISCV::V29)
8757                         .Case("{v30}", RISCV::V30)
8758                         .Case("{v31}", RISCV::V31)
8759                         .Default(RISCV::NoRegister);
8760     if (VReg != RISCV::NoRegister) {
8761       if (TRI->isTypeLegalForClass(RISCV::VMRegClass, VT.SimpleTy))
8762         return std::make_pair(VReg, &RISCV::VMRegClass);
8763       if (TRI->isTypeLegalForClass(RISCV::VRRegClass, VT.SimpleTy))
8764         return std::make_pair(VReg, &RISCV::VRRegClass);
8765       for (const auto *RC :
8766            {&RISCV::VRM2RegClass, &RISCV::VRM4RegClass, &RISCV::VRM8RegClass}) {
8767         if (TRI->isTypeLegalForClass(*RC, VT.SimpleTy)) {
8768           VReg = TRI->getMatchingSuperReg(VReg, RISCV::sub_vrm1_0, RC);
8769           return std::make_pair(VReg, RC);
8770         }
8771       }
8772     }
8773   }
8774 
8775   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
8776 }
8777 
8778 unsigned
8779 RISCVTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const {
8780   // Currently only support length 1 constraints.
8781   if (ConstraintCode.size() == 1) {
8782     switch (ConstraintCode[0]) {
8783     case 'A':
8784       return InlineAsm::Constraint_A;
8785     default:
8786       break;
8787     }
8788   }
8789 
8790   return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
8791 }
8792 
8793 void RISCVTargetLowering::LowerAsmOperandForConstraint(
8794     SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
8795     SelectionDAG &DAG) const {
8796   // Currently only support length 1 constraints.
8797   if (Constraint.length() == 1) {
8798     switch (Constraint[0]) {
8799     case 'I':
8800       // Validate & create a 12-bit signed immediate operand.
8801       if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
8802         uint64_t CVal = C->getSExtValue();
8803         if (isInt<12>(CVal))
8804           Ops.push_back(
8805               DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
8806       }
8807       return;
8808     case 'J':
8809       // Validate & create an integer zero operand.
8810       if (auto *C = dyn_cast<ConstantSDNode>(Op))
8811         if (C->getZExtValue() == 0)
8812           Ops.push_back(
8813               DAG.getTargetConstant(0, SDLoc(Op), Subtarget.getXLenVT()));
8814       return;
8815     case 'K':
8816       // Validate & create a 5-bit unsigned immediate operand.
8817       if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
8818         uint64_t CVal = C->getZExtValue();
8819         if (isUInt<5>(CVal))
8820           Ops.push_back(
8821               DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
8822       }
8823       return;
8824     case 'S':
8825       if (const auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
8826         Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(), SDLoc(Op),
8827                                                  GA->getValueType(0)));
8828       } else if (const auto *BA = dyn_cast<BlockAddressSDNode>(Op)) {
8829         Ops.push_back(DAG.getTargetBlockAddress(BA->getBlockAddress(),
8830                                                 BA->getValueType(0)));
8831       }
8832       return;
8833     default:
8834       break;
8835     }
8836   }
8837   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
8838 }
8839 
8840 Instruction *RISCVTargetLowering::emitLeadingFence(IRBuilderBase &Builder,
8841                                                    Instruction *Inst,
8842                                                    AtomicOrdering Ord) const {
8843   if (isa<LoadInst>(Inst) && Ord == AtomicOrdering::SequentiallyConsistent)
8844     return Builder.CreateFence(Ord);
8845   if (isa<StoreInst>(Inst) && isReleaseOrStronger(Ord))
8846     return Builder.CreateFence(AtomicOrdering::Release);
8847   return nullptr;
8848 }
8849 
8850 Instruction *RISCVTargetLowering::emitTrailingFence(IRBuilderBase &Builder,
8851                                                     Instruction *Inst,
8852                                                     AtomicOrdering Ord) const {
8853   if (isa<LoadInst>(Inst) && isAcquireOrStronger(Ord))
8854     return Builder.CreateFence(AtomicOrdering::Acquire);
8855   return nullptr;
8856 }
8857 
8858 TargetLowering::AtomicExpansionKind
8859 RISCVTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
8860   // atomicrmw {fadd,fsub} must be expanded to use compare-exchange, as floating
8861   // point operations can't be used in an lr/sc sequence without breaking the
8862   // forward-progress guarantee.
8863   if (AI->isFloatingPointOperation())
8864     return AtomicExpansionKind::CmpXChg;
8865 
8866   unsigned Size = AI->getType()->getPrimitiveSizeInBits();
8867   if (Size == 8 || Size == 16)
8868     return AtomicExpansionKind::MaskedIntrinsic;
8869   return AtomicExpansionKind::None;
8870 }
8871 
8872 static Intrinsic::ID
8873 getIntrinsicForMaskedAtomicRMWBinOp(unsigned XLen, AtomicRMWInst::BinOp BinOp) {
8874   if (XLen == 32) {
8875     switch (BinOp) {
8876     default:
8877       llvm_unreachable("Unexpected AtomicRMW BinOp");
8878     case AtomicRMWInst::Xchg:
8879       return Intrinsic::riscv_masked_atomicrmw_xchg_i32;
8880     case AtomicRMWInst::Add:
8881       return Intrinsic::riscv_masked_atomicrmw_add_i32;
8882     case AtomicRMWInst::Sub:
8883       return Intrinsic::riscv_masked_atomicrmw_sub_i32;
8884     case AtomicRMWInst::Nand:
8885       return Intrinsic::riscv_masked_atomicrmw_nand_i32;
8886     case AtomicRMWInst::Max:
8887       return Intrinsic::riscv_masked_atomicrmw_max_i32;
8888     case AtomicRMWInst::Min:
8889       return Intrinsic::riscv_masked_atomicrmw_min_i32;
8890     case AtomicRMWInst::UMax:
8891       return Intrinsic::riscv_masked_atomicrmw_umax_i32;
8892     case AtomicRMWInst::UMin:
8893       return Intrinsic::riscv_masked_atomicrmw_umin_i32;
8894     }
8895   }
8896 
8897   if (XLen == 64) {
8898     switch (BinOp) {
8899     default:
8900       llvm_unreachable("Unexpected AtomicRMW BinOp");
8901     case AtomicRMWInst::Xchg:
8902       return Intrinsic::riscv_masked_atomicrmw_xchg_i64;
8903     case AtomicRMWInst::Add:
8904       return Intrinsic::riscv_masked_atomicrmw_add_i64;
8905     case AtomicRMWInst::Sub:
8906       return Intrinsic::riscv_masked_atomicrmw_sub_i64;
8907     case AtomicRMWInst::Nand:
8908       return Intrinsic::riscv_masked_atomicrmw_nand_i64;
8909     case AtomicRMWInst::Max:
8910       return Intrinsic::riscv_masked_atomicrmw_max_i64;
8911     case AtomicRMWInst::Min:
8912       return Intrinsic::riscv_masked_atomicrmw_min_i64;
8913     case AtomicRMWInst::UMax:
8914       return Intrinsic::riscv_masked_atomicrmw_umax_i64;
8915     case AtomicRMWInst::UMin:
8916       return Intrinsic::riscv_masked_atomicrmw_umin_i64;
8917     }
8918   }
8919 
8920   llvm_unreachable("Unexpected XLen\n");
8921 }
8922 
8923 Value *RISCVTargetLowering::emitMaskedAtomicRMWIntrinsic(
8924     IRBuilderBase &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr,
8925     Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const {
8926   unsigned XLen = Subtarget.getXLen();
8927   Value *Ordering =
8928       Builder.getIntN(XLen, static_cast<uint64_t>(AI->getOrdering()));
8929   Type *Tys[] = {AlignedAddr->getType()};
8930   Function *LrwOpScwLoop = Intrinsic::getDeclaration(
8931       AI->getModule(),
8932       getIntrinsicForMaskedAtomicRMWBinOp(XLen, AI->getOperation()), Tys);
8933 
8934   if (XLen == 64) {
8935     Incr = Builder.CreateSExt(Incr, Builder.getInt64Ty());
8936     Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
8937     ShiftAmt = Builder.CreateSExt(ShiftAmt, Builder.getInt64Ty());
8938   }
8939 
8940   Value *Result;
8941 
8942   // Must pass the shift amount needed to sign extend the loaded value prior
8943   // to performing a signed comparison for min/max. ShiftAmt is the number of
8944   // bits to shift the value into position. Pass XLen-ShiftAmt-ValWidth, which
8945   // is the number of bits to left+right shift the value in order to
8946   // sign-extend.
8947   if (AI->getOperation() == AtomicRMWInst::Min ||
8948       AI->getOperation() == AtomicRMWInst::Max) {
8949     const DataLayout &DL = AI->getModule()->getDataLayout();
8950     unsigned ValWidth =
8951         DL.getTypeStoreSizeInBits(AI->getValOperand()->getType());
8952     Value *SextShamt =
8953         Builder.CreateSub(Builder.getIntN(XLen, XLen - ValWidth), ShiftAmt);
8954     Result = Builder.CreateCall(LrwOpScwLoop,
8955                                 {AlignedAddr, Incr, Mask, SextShamt, Ordering});
8956   } else {
8957     Result =
8958         Builder.CreateCall(LrwOpScwLoop, {AlignedAddr, Incr, Mask, Ordering});
8959   }
8960 
8961   if (XLen == 64)
8962     Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
8963   return Result;
8964 }
8965 
8966 TargetLowering::AtomicExpansionKind
8967 RISCVTargetLowering::shouldExpandAtomicCmpXchgInIR(
8968     AtomicCmpXchgInst *CI) const {
8969   unsigned Size = CI->getCompareOperand()->getType()->getPrimitiveSizeInBits();
8970   if (Size == 8 || Size == 16)
8971     return AtomicExpansionKind::MaskedIntrinsic;
8972   return AtomicExpansionKind::None;
8973 }
8974 
8975 Value *RISCVTargetLowering::emitMaskedAtomicCmpXchgIntrinsic(
8976     IRBuilderBase &Builder, AtomicCmpXchgInst *CI, Value *AlignedAddr,
8977     Value *CmpVal, Value *NewVal, Value *Mask, AtomicOrdering Ord) const {
8978   unsigned XLen = Subtarget.getXLen();
8979   Value *Ordering = Builder.getIntN(XLen, static_cast<uint64_t>(Ord));
8980   Intrinsic::ID CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i32;
8981   if (XLen == 64) {
8982     CmpVal = Builder.CreateSExt(CmpVal, Builder.getInt64Ty());
8983     NewVal = Builder.CreateSExt(NewVal, Builder.getInt64Ty());
8984     Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
8985     CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i64;
8986   }
8987   Type *Tys[] = {AlignedAddr->getType()};
8988   Function *MaskedCmpXchg =
8989       Intrinsic::getDeclaration(CI->getModule(), CmpXchgIntrID, Tys);
8990   Value *Result = Builder.CreateCall(
8991       MaskedCmpXchg, {AlignedAddr, CmpVal, NewVal, Mask, Ordering});
8992   if (XLen == 64)
8993     Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
8994   return Result;
8995 }
8996 
8997 bool RISCVTargetLowering::shouldRemoveExtendFromGSIndex(EVT VT) const {
8998   return false;
8999 }
9000 
9001 bool RISCVTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
9002                                                      EVT VT) const {
9003   VT = VT.getScalarType();
9004 
9005   if (!VT.isSimple())
9006     return false;
9007 
9008   switch (VT.getSimpleVT().SimpleTy) {
9009   case MVT::f16:
9010     return Subtarget.hasStdExtZfh();
9011   case MVT::f32:
9012     return Subtarget.hasStdExtF();
9013   case MVT::f64:
9014     return Subtarget.hasStdExtD();
9015   default:
9016     break;
9017   }
9018 
9019   return false;
9020 }
9021 
9022 Register RISCVTargetLowering::getExceptionPointerRegister(
9023     const Constant *PersonalityFn) const {
9024   return RISCV::X10;
9025 }
9026 
9027 Register RISCVTargetLowering::getExceptionSelectorRegister(
9028     const Constant *PersonalityFn) const {
9029   return RISCV::X11;
9030 }
9031 
9032 bool RISCVTargetLowering::shouldExtendTypeInLibCall(EVT Type) const {
9033   // Return false to suppress the unnecessary extensions if the LibCall
9034   // arguments or return value is f32 type for LP64 ABI.
9035   RISCVABI::ABI ABI = Subtarget.getTargetABI();
9036   if (ABI == RISCVABI::ABI_LP64 && (Type == MVT::f32))
9037     return false;
9038 
9039   return true;
9040 }
9041 
9042 bool RISCVTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
9043   if (Subtarget.is64Bit() && Type == MVT::i32)
9044     return true;
9045 
9046   return IsSigned;
9047 }
9048 
9049 bool RISCVTargetLowering::decomposeMulByConstant(LLVMContext &Context, EVT VT,
9050                                                  SDValue C) const {
9051   // Check integral scalar types.
9052   if (VT.isScalarInteger()) {
9053     // Omit the optimization if the sub target has the M extension and the data
9054     // size exceeds XLen.
9055     if (Subtarget.hasStdExtM() && VT.getSizeInBits() > Subtarget.getXLen())
9056       return false;
9057     if (auto *ConstNode = dyn_cast<ConstantSDNode>(C.getNode())) {
9058       // Break the MUL to a SLLI and an ADD/SUB.
9059       const APInt &Imm = ConstNode->getAPIntValue();
9060       if ((Imm + 1).isPowerOf2() || (Imm - 1).isPowerOf2() ||
9061           (1 - Imm).isPowerOf2() || (-1 - Imm).isPowerOf2())
9062         return true;
9063       // Optimize the MUL to (SH*ADD x, (SLLI x, bits)) if Imm is not simm12.
9064       if (Subtarget.hasStdExtZba() && !Imm.isSignedIntN(12) &&
9065           ((Imm - 2).isPowerOf2() || (Imm - 4).isPowerOf2() ||
9066            (Imm - 8).isPowerOf2()))
9067         return true;
9068       // Omit the following optimization if the sub target has the M extension
9069       // and the data size >= XLen.
9070       if (Subtarget.hasStdExtM() && VT.getSizeInBits() >= Subtarget.getXLen())
9071         return false;
9072       // Break the MUL to two SLLI instructions and an ADD/SUB, if Imm needs
9073       // a pair of LUI/ADDI.
9074       if (!Imm.isSignedIntN(12) && Imm.countTrailingZeros() < 12) {
9075         APInt ImmS = Imm.ashr(Imm.countTrailingZeros());
9076         if ((ImmS + 1).isPowerOf2() || (ImmS - 1).isPowerOf2() ||
9077             (1 - ImmS).isPowerOf2())
9078         return true;
9079       }
9080     }
9081   }
9082 
9083   return false;
9084 }
9085 
9086 bool RISCVTargetLowering::allowsMisalignedMemoryAccesses(
9087     EVT VT, unsigned AddrSpace, Align Alignment, MachineMemOperand::Flags Flags,
9088     bool *Fast) const {
9089   if (!VT.isVector())
9090     return false;
9091 
9092   EVT ElemVT = VT.getVectorElementType();
9093   if (Alignment >= ElemVT.getStoreSize()) {
9094     if (Fast)
9095       *Fast = true;
9096     return true;
9097   }
9098 
9099   return false;
9100 }
9101 
9102 bool RISCVTargetLowering::splitValueIntoRegisterParts(
9103     SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts,
9104     unsigned NumParts, MVT PartVT, Optional<CallingConv::ID> CC) const {
9105   bool IsABIRegCopy = CC.hasValue();
9106   EVT ValueVT = Val.getValueType();
9107   if (IsABIRegCopy && ValueVT == MVT::f16 && PartVT == MVT::f32) {
9108     // Cast the f16 to i16, extend to i32, pad with ones to make a float nan,
9109     // and cast to f32.
9110     Val = DAG.getNode(ISD::BITCAST, DL, MVT::i16, Val);
9111     Val = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Val);
9112     Val = DAG.getNode(ISD::OR, DL, MVT::i32, Val,
9113                       DAG.getConstant(0xFFFF0000, DL, MVT::i32));
9114     Val = DAG.getNode(ISD::BITCAST, DL, MVT::f32, Val);
9115     Parts[0] = Val;
9116     return true;
9117   }
9118 
9119   if (ValueVT.isScalableVector() && PartVT.isScalableVector()) {
9120     LLVMContext &Context = *DAG.getContext();
9121     EVT ValueEltVT = ValueVT.getVectorElementType();
9122     EVT PartEltVT = PartVT.getVectorElementType();
9123     unsigned ValueVTBitSize = ValueVT.getSizeInBits().getKnownMinSize();
9124     unsigned PartVTBitSize = PartVT.getSizeInBits().getKnownMinSize();
9125     if (PartVTBitSize % ValueVTBitSize == 0) {
9126       // If the element types are different, bitcast to the same element type of
9127       // PartVT first.
9128       if (ValueEltVT != PartEltVT) {
9129         unsigned Count = ValueVTBitSize / PartEltVT.getSizeInBits();
9130         assert(Count != 0 && "The number of element should not be zero.");
9131         EVT SameEltTypeVT =
9132             EVT::getVectorVT(Context, PartEltVT, Count, /*IsScalable=*/true);
9133         Val = DAG.getNode(ISD::BITCAST, DL, SameEltTypeVT, Val);
9134       }
9135       Val = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
9136                         Val, DAG.getConstant(0, DL, Subtarget.getXLenVT()));
9137       Parts[0] = Val;
9138       return true;
9139     }
9140   }
9141   return false;
9142 }
9143 
9144 SDValue RISCVTargetLowering::joinRegisterPartsIntoValue(
9145     SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts,
9146     MVT PartVT, EVT ValueVT, Optional<CallingConv::ID> CC) const {
9147   bool IsABIRegCopy = CC.hasValue();
9148   if (IsABIRegCopy && ValueVT == MVT::f16 && PartVT == MVT::f32) {
9149     SDValue Val = Parts[0];
9150 
9151     // Cast the f32 to i32, truncate to i16, and cast back to f16.
9152     Val = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Val);
9153     Val = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Val);
9154     Val = DAG.getNode(ISD::BITCAST, DL, MVT::f16, Val);
9155     return Val;
9156   }
9157 
9158   if (ValueVT.isScalableVector() && PartVT.isScalableVector()) {
9159     LLVMContext &Context = *DAG.getContext();
9160     SDValue Val = Parts[0];
9161     EVT ValueEltVT = ValueVT.getVectorElementType();
9162     EVT PartEltVT = PartVT.getVectorElementType();
9163     unsigned ValueVTBitSize = ValueVT.getSizeInBits().getKnownMinSize();
9164     unsigned PartVTBitSize = PartVT.getSizeInBits().getKnownMinSize();
9165     if (PartVTBitSize % ValueVTBitSize == 0) {
9166       EVT SameEltTypeVT = ValueVT;
9167       // If the element types are different, convert it to the same element type
9168       // of PartVT.
9169       if (ValueEltVT != PartEltVT) {
9170         unsigned Count = ValueVTBitSize / PartEltVT.getSizeInBits();
9171         assert(Count != 0 && "The number of element should not be zero.");
9172         SameEltTypeVT =
9173             EVT::getVectorVT(Context, PartEltVT, Count, /*IsScalable=*/true);
9174       }
9175       Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SameEltTypeVT, Val,
9176                         DAG.getConstant(0, DL, Subtarget.getXLenVT()));
9177       if (ValueEltVT != PartEltVT)
9178         Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
9179       return Val;
9180     }
9181   }
9182   return SDValue();
9183 }
9184 
9185 #define GET_REGISTER_MATCHER
9186 #include "RISCVGenAsmMatcher.inc"
9187 
9188 Register
9189 RISCVTargetLowering::getRegisterByName(const char *RegName, LLT VT,
9190                                        const MachineFunction &MF) const {
9191   Register Reg = MatchRegisterAltName(RegName);
9192   if (Reg == RISCV::NoRegister)
9193     Reg = MatchRegisterName(RegName);
9194   if (Reg == RISCV::NoRegister)
9195     report_fatal_error(
9196         Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
9197   BitVector ReservedRegs = Subtarget.getRegisterInfo()->getReservedRegs(MF);
9198   if (!ReservedRegs.test(Reg) && !Subtarget.isRegisterReservedByUser(Reg))
9199     report_fatal_error(Twine("Trying to obtain non-reserved register \"" +
9200                              StringRef(RegName) + "\"."));
9201   return Reg;
9202 }
9203 
9204 namespace llvm {
9205 namespace RISCVVIntrinsicsTable {
9206 
9207 #define GET_RISCVVIntrinsicsTable_IMPL
9208 #include "RISCVGenSearchableTables.inc"
9209 
9210 } // namespace RISCVVIntrinsicsTable
9211 
9212 } // namespace llvm
9213