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/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
29 #include "llvm/CodeGen/ValueTypes.h"
30 #include "llvm/IR/DiagnosticInfo.h"
31 #include "llvm/IR/DiagnosticPrinter.h"
32 #include "llvm/IR/IntrinsicsRISCV.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   if (Subtarget.hasStdExtV()) {
94     addRegisterClass(RISCVVMVTs::vbool64_t, &RISCV::VRRegClass);
95     addRegisterClass(RISCVVMVTs::vbool32_t, &RISCV::VRRegClass);
96     addRegisterClass(RISCVVMVTs::vbool16_t, &RISCV::VRRegClass);
97     addRegisterClass(RISCVVMVTs::vbool8_t, &RISCV::VRRegClass);
98     addRegisterClass(RISCVVMVTs::vbool4_t, &RISCV::VRRegClass);
99     addRegisterClass(RISCVVMVTs::vbool2_t, &RISCV::VRRegClass);
100     addRegisterClass(RISCVVMVTs::vbool1_t, &RISCV::VRRegClass);
101 
102     addRegisterClass(RISCVVMVTs::vint8mf8_t, &RISCV::VRRegClass);
103     addRegisterClass(RISCVVMVTs::vint8mf4_t, &RISCV::VRRegClass);
104     addRegisterClass(RISCVVMVTs::vint8mf2_t, &RISCV::VRRegClass);
105     addRegisterClass(RISCVVMVTs::vint8m1_t, &RISCV::VRRegClass);
106     addRegisterClass(RISCVVMVTs::vint8m2_t, &RISCV::VRM2RegClass);
107     addRegisterClass(RISCVVMVTs::vint8m4_t, &RISCV::VRM4RegClass);
108     addRegisterClass(RISCVVMVTs::vint8m8_t, &RISCV::VRM8RegClass);
109 
110     addRegisterClass(RISCVVMVTs::vint16mf4_t, &RISCV::VRRegClass);
111     addRegisterClass(RISCVVMVTs::vint16mf2_t, &RISCV::VRRegClass);
112     addRegisterClass(RISCVVMVTs::vint16m1_t, &RISCV::VRRegClass);
113     addRegisterClass(RISCVVMVTs::vint16m2_t, &RISCV::VRM2RegClass);
114     addRegisterClass(RISCVVMVTs::vint16m4_t, &RISCV::VRM4RegClass);
115     addRegisterClass(RISCVVMVTs::vint16m8_t, &RISCV::VRM8RegClass);
116 
117     addRegisterClass(RISCVVMVTs::vint32mf2_t, &RISCV::VRRegClass);
118     addRegisterClass(RISCVVMVTs::vint32m1_t, &RISCV::VRRegClass);
119     addRegisterClass(RISCVVMVTs::vint32m2_t, &RISCV::VRM2RegClass);
120     addRegisterClass(RISCVVMVTs::vint32m4_t, &RISCV::VRM4RegClass);
121     addRegisterClass(RISCVVMVTs::vint32m8_t, &RISCV::VRM8RegClass);
122 
123     addRegisterClass(RISCVVMVTs::vint64m1_t, &RISCV::VRRegClass);
124     addRegisterClass(RISCVVMVTs::vint64m2_t, &RISCV::VRM2RegClass);
125     addRegisterClass(RISCVVMVTs::vint64m4_t, &RISCV::VRM4RegClass);
126     addRegisterClass(RISCVVMVTs::vint64m8_t, &RISCV::VRM8RegClass);
127 
128     if (Subtarget.hasStdExtZfh()) {
129       addRegisterClass(RISCVVMVTs::vfloat16mf4_t, &RISCV::VRRegClass);
130       addRegisterClass(RISCVVMVTs::vfloat16mf2_t, &RISCV::VRRegClass);
131       addRegisterClass(RISCVVMVTs::vfloat16m1_t, &RISCV::VRRegClass);
132       addRegisterClass(RISCVVMVTs::vfloat16m2_t, &RISCV::VRM2RegClass);
133       addRegisterClass(RISCVVMVTs::vfloat16m4_t, &RISCV::VRM4RegClass);
134       addRegisterClass(RISCVVMVTs::vfloat16m8_t, &RISCV::VRM8RegClass);
135     }
136 
137     if (Subtarget.hasStdExtF()) {
138       addRegisterClass(RISCVVMVTs::vfloat32mf2_t, &RISCV::VRRegClass);
139       addRegisterClass(RISCVVMVTs::vfloat32m1_t, &RISCV::VRRegClass);
140       addRegisterClass(RISCVVMVTs::vfloat32m2_t, &RISCV::VRM2RegClass);
141       addRegisterClass(RISCVVMVTs::vfloat32m4_t, &RISCV::VRM4RegClass);
142       addRegisterClass(RISCVVMVTs::vfloat32m8_t, &RISCV::VRM8RegClass);
143     }
144 
145     if (Subtarget.hasStdExtD()) {
146       addRegisterClass(RISCVVMVTs::vfloat64m1_t, &RISCV::VRRegClass);
147       addRegisterClass(RISCVVMVTs::vfloat64m2_t, &RISCV::VRM2RegClass);
148       addRegisterClass(RISCVVMVTs::vfloat64m4_t, &RISCV::VRM4RegClass);
149       addRegisterClass(RISCVVMVTs::vfloat64m8_t, &RISCV::VRM8RegClass);
150     }
151   }
152 
153   // Compute derived properties from the register classes.
154   computeRegisterProperties(STI.getRegisterInfo());
155 
156   setStackPointerRegisterToSaveRestore(RISCV::X2);
157 
158   for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD})
159     setLoadExtAction(N, XLenVT, MVT::i1, Promote);
160 
161   // TODO: add all necessary setOperationAction calls.
162   setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand);
163 
164   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
165   setOperationAction(ISD::BR_CC, XLenVT, Expand);
166   setOperationAction(ISD::SELECT, XLenVT, Custom);
167   setOperationAction(ISD::SELECT_CC, XLenVT, Expand);
168 
169   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
170   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
171 
172   setOperationAction(ISD::VASTART, MVT::Other, Custom);
173   setOperationAction(ISD::VAARG, MVT::Other, Expand);
174   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
175   setOperationAction(ISD::VAEND, MVT::Other, Expand);
176 
177   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
178   if (!Subtarget.hasStdExtZbb()) {
179     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
180     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
181   }
182 
183   if (Subtarget.is64Bit()) {
184     setOperationAction(ISD::ADD, MVT::i32, Custom);
185     setOperationAction(ISD::SUB, MVT::i32, Custom);
186     setOperationAction(ISD::SHL, MVT::i32, Custom);
187     setOperationAction(ISD::SRA, MVT::i32, Custom);
188     setOperationAction(ISD::SRL, MVT::i32, Custom);
189   }
190 
191   if (!Subtarget.hasStdExtM()) {
192     setOperationAction(ISD::MUL, XLenVT, Expand);
193     setOperationAction(ISD::MULHS, XLenVT, Expand);
194     setOperationAction(ISD::MULHU, XLenVT, Expand);
195     setOperationAction(ISD::SDIV, XLenVT, Expand);
196     setOperationAction(ISD::UDIV, XLenVT, Expand);
197     setOperationAction(ISD::SREM, XLenVT, Expand);
198     setOperationAction(ISD::UREM, XLenVT, Expand);
199   }
200 
201   if (Subtarget.is64Bit() && Subtarget.hasStdExtM()) {
202     setOperationAction(ISD::MUL, MVT::i32, Custom);
203     setOperationAction(ISD::SDIV, MVT::i32, Custom);
204     setOperationAction(ISD::UDIV, MVT::i32, Custom);
205     setOperationAction(ISD::UREM, MVT::i32, Custom);
206   }
207 
208   setOperationAction(ISD::SDIVREM, XLenVT, Expand);
209   setOperationAction(ISD::UDIVREM, XLenVT, Expand);
210   setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand);
211   setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand);
212 
213   setOperationAction(ISD::SHL_PARTS, XLenVT, Custom);
214   setOperationAction(ISD::SRL_PARTS, XLenVT, Custom);
215   setOperationAction(ISD::SRA_PARTS, XLenVT, Custom);
216 
217   if (Subtarget.hasStdExtZbb() || Subtarget.hasStdExtZbp()) {
218     if (Subtarget.is64Bit()) {
219       setOperationAction(ISD::ROTL, MVT::i32, Custom);
220       setOperationAction(ISD::ROTR, MVT::i32, Custom);
221     }
222   } else {
223     setOperationAction(ISD::ROTL, XLenVT, Expand);
224     setOperationAction(ISD::ROTR, XLenVT, Expand);
225   }
226 
227   if (Subtarget.hasStdExtZbp()) {
228     setOperationAction(ISD::BITREVERSE, XLenVT, Custom);
229     setOperationAction(ISD::BSWAP, XLenVT, Custom);
230 
231     if (Subtarget.is64Bit()) {
232       setOperationAction(ISD::BITREVERSE, MVT::i32, Custom);
233       setOperationAction(ISD::BSWAP, MVT::i32, Custom);
234     }
235   } else {
236     setOperationAction(ISD::BSWAP, XLenVT, Expand);
237   }
238 
239   if (Subtarget.hasStdExtZbb()) {
240     setOperationAction(ISD::SMIN, XLenVT, Legal);
241     setOperationAction(ISD::SMAX, XLenVT, Legal);
242     setOperationAction(ISD::UMIN, XLenVT, Legal);
243     setOperationAction(ISD::UMAX, XLenVT, Legal);
244   } else {
245     setOperationAction(ISD::CTTZ, XLenVT, Expand);
246     setOperationAction(ISD::CTLZ, XLenVT, Expand);
247     setOperationAction(ISD::CTPOP, XLenVT, Expand);
248   }
249 
250   if (Subtarget.hasStdExtZbt()) {
251     setOperationAction(ISD::FSHL, XLenVT, Legal);
252     setOperationAction(ISD::FSHR, XLenVT, Legal);
253 
254     if (Subtarget.is64Bit()) {
255       setOperationAction(ISD::FSHL, MVT::i32, Custom);
256       setOperationAction(ISD::FSHR, MVT::i32, Custom);
257     }
258   }
259 
260   ISD::CondCode FPCCToExpand[] = {
261       ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETUEQ, ISD::SETUGT,
262       ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE, ISD::SETGT,
263       ISD::SETGE,  ISD::SETNE,  ISD::SETO,   ISD::SETUO};
264 
265   ISD::NodeType FPOpToExpand[] = {
266       ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FP16_TO_FP,
267       ISD::FP_TO_FP16};
268 
269   if (Subtarget.hasStdExtZfh())
270     setOperationAction(ISD::BITCAST, MVT::i16, Custom);
271 
272   if (Subtarget.hasStdExtZfh()) {
273     setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
274     setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
275     for (auto CC : FPCCToExpand)
276       setCondCodeAction(CC, MVT::f16, Expand);
277     setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
278     setOperationAction(ISD::SELECT, MVT::f16, Custom);
279     setOperationAction(ISD::BR_CC, MVT::f16, Expand);
280     for (auto Op : FPOpToExpand)
281       setOperationAction(Op, MVT::f16, Expand);
282   }
283 
284   if (Subtarget.hasStdExtF()) {
285     setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
286     setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
287     for (auto CC : FPCCToExpand)
288       setCondCodeAction(CC, MVT::f32, Expand);
289     setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
290     setOperationAction(ISD::SELECT, MVT::f32, Custom);
291     setOperationAction(ISD::BR_CC, MVT::f32, Expand);
292     for (auto Op : FPOpToExpand)
293       setOperationAction(Op, MVT::f32, Expand);
294     setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
295     setTruncStoreAction(MVT::f32, MVT::f16, Expand);
296   }
297 
298   if (Subtarget.hasStdExtF() && Subtarget.is64Bit())
299     setOperationAction(ISD::BITCAST, MVT::i32, Custom);
300 
301   if (Subtarget.hasStdExtD()) {
302     setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
303     setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
304     for (auto CC : FPCCToExpand)
305       setCondCodeAction(CC, MVT::f64, Expand);
306     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
307     setOperationAction(ISD::SELECT, MVT::f64, Custom);
308     setOperationAction(ISD::BR_CC, MVT::f64, Expand);
309     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
310     setTruncStoreAction(MVT::f64, MVT::f32, Expand);
311     for (auto Op : FPOpToExpand)
312       setOperationAction(Op, MVT::f64, Expand);
313     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
314     setTruncStoreAction(MVT::f64, MVT::f16, Expand);
315   }
316 
317   if (Subtarget.is64Bit()) {
318     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
319     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
320     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Custom);
321     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Custom);
322   }
323 
324   setOperationAction(ISD::GlobalAddress, XLenVT, Custom);
325   setOperationAction(ISD::BlockAddress, XLenVT, Custom);
326   setOperationAction(ISD::ConstantPool, XLenVT, Custom);
327   setOperationAction(ISD::JumpTable, XLenVT, Custom);
328 
329   setOperationAction(ISD::GlobalTLSAddress, XLenVT, Custom);
330 
331   // TODO: On M-mode only targets, the cycle[h] CSR may not be present.
332   // Unfortunately this can't be determined just from the ISA naming string.
333   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64,
334                      Subtarget.is64Bit() ? Legal : Custom);
335 
336   setOperationAction(ISD::TRAP, MVT::Other, Legal);
337   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
338   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
339 
340   if (Subtarget.hasStdExtA()) {
341     setMaxAtomicSizeInBitsSupported(Subtarget.getXLen());
342     setMinCmpXchgSizeInBits(32);
343   } else {
344     setMaxAtomicSizeInBitsSupported(0);
345   }
346 
347   setBooleanContents(ZeroOrOneBooleanContent);
348 
349   if (Subtarget.hasStdExtV()) {
350     setBooleanVectorContents(ZeroOrOneBooleanContent);
351 
352     setOperationAction(ISD::VSCALE, XLenVT, Custom);
353 
354     // RVV intrinsics may have illegal operands.
355     // We also need to custom legalize vmv.x.s.
356     setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i8, Custom);
357     setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i16, Custom);
358     setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom);
359     setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i16, Custom);
360     setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i32, Custom);
361     setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i32, Custom);
362 
363     if (Subtarget.is64Bit()) {
364       setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
365       setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
366     }
367 
368     for (auto VT : MVT::integer_scalable_vector_valuetypes()) {
369       setOperationAction(ISD::SPLAT_VECTOR, VT, Legal);
370 
371       setOperationAction(ISD::SMIN, VT, Legal);
372       setOperationAction(ISD::SMAX, VT, Legal);
373       setOperationAction(ISD::UMIN, VT, Legal);
374       setOperationAction(ISD::UMAX, VT, Legal);
375     }
376 
377     // We must custom-lower SPLAT_VECTOR vXi64 on RV32
378     if (!Subtarget.is64Bit())
379       setOperationAction(ISD::SPLAT_VECTOR, MVT::i64, Custom);
380 
381     // Expand various CCs to best match the RVV ISA, which natively supports UNE
382     // but no other unordered comparisons, and supports all ordered comparisons
383     // except ONE. Additionally, we expand GT,OGT,GE,OGE for optimization
384     // purposes; they are expanded to their swapped-operand CCs (LT,OLT,LE,OLE),
385     // and we pattern-match those back to the "original", swapping operands once
386     // more. This way we catch both operations and both "vf" and "fv" forms with
387     // fewer patterns.
388     ISD::CondCode VFPCCToExpand[] = {
389         ISD::SETO,   ISD::SETONE, ISD::SETUEQ, ISD::SETUGT,
390         ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUO,
391         ISD::SETGT,  ISD::SETOGT, ISD::SETGE,  ISD::SETOGE,
392     };
393 
394     if (Subtarget.hasStdExtZfh()) {
395       for (auto VT : {RISCVVMVTs::vfloat16mf4_t, RISCVVMVTs::vfloat16mf2_t,
396                       RISCVVMVTs::vfloat16m1_t, RISCVVMVTs::vfloat16m2_t,
397                       RISCVVMVTs::vfloat16m4_t, RISCVVMVTs::vfloat16m8_t}) {
398         setOperationAction(ISD::SPLAT_VECTOR, VT, Legal);
399         for (auto CC : VFPCCToExpand)
400           setCondCodeAction(CC, VT, Expand);
401       }
402     }
403 
404     if (Subtarget.hasStdExtF()) {
405       for (auto VT : {RISCVVMVTs::vfloat32mf2_t, RISCVVMVTs::vfloat32m1_t,
406                       RISCVVMVTs::vfloat32m2_t, RISCVVMVTs::vfloat32m4_t,
407                       RISCVVMVTs::vfloat32m8_t}) {
408         setOperationAction(ISD::SPLAT_VECTOR, VT, Legal);
409         for (auto CC : VFPCCToExpand)
410           setCondCodeAction(CC, VT, Expand);
411       }
412     }
413 
414     if (Subtarget.hasStdExtD()) {
415       for (auto VT : {RISCVVMVTs::vfloat64m1_t, RISCVVMVTs::vfloat64m2_t,
416                       RISCVVMVTs::vfloat64m4_t, RISCVVMVTs::vfloat64m8_t}) {
417         setOperationAction(ISD::SPLAT_VECTOR, VT, Legal);
418         for (auto CC : VFPCCToExpand)
419           setCondCodeAction(CC, VT, Expand);
420       }
421     }
422   }
423 
424   // Function alignments.
425   const Align FunctionAlignment(Subtarget.hasStdExtC() ? 2 : 4);
426   setMinFunctionAlignment(FunctionAlignment);
427   setPrefFunctionAlignment(FunctionAlignment);
428 
429   setMinimumJumpTableEntries(5);
430 
431   // Jumps are expensive, compared to logic
432   setJumpIsExpensive();
433 
434   // We can use any register for comparisons
435   setHasMultipleConditionRegisters();
436 
437   if (Subtarget.hasStdExtZbp()) {
438     setTargetDAGCombine(ISD::OR);
439   }
440 }
441 
442 EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
443                                             EVT VT) const {
444   if (!VT.isVector())
445     return getPointerTy(DL);
446   if (Subtarget.hasStdExtV())
447     return MVT::getVectorVT(MVT::i1, VT.getVectorElementCount());
448   return VT.changeVectorElementTypeToInteger();
449 }
450 
451 bool RISCVTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
452                                              const CallInst &I,
453                                              MachineFunction &MF,
454                                              unsigned Intrinsic) const {
455   switch (Intrinsic) {
456   default:
457     return false;
458   case Intrinsic::riscv_masked_atomicrmw_xchg_i32:
459   case Intrinsic::riscv_masked_atomicrmw_add_i32:
460   case Intrinsic::riscv_masked_atomicrmw_sub_i32:
461   case Intrinsic::riscv_masked_atomicrmw_nand_i32:
462   case Intrinsic::riscv_masked_atomicrmw_max_i32:
463   case Intrinsic::riscv_masked_atomicrmw_min_i32:
464   case Intrinsic::riscv_masked_atomicrmw_umax_i32:
465   case Intrinsic::riscv_masked_atomicrmw_umin_i32:
466   case Intrinsic::riscv_masked_cmpxchg_i32:
467     PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
468     Info.opc = ISD::INTRINSIC_W_CHAIN;
469     Info.memVT = MVT::getVT(PtrTy->getElementType());
470     Info.ptrVal = I.getArgOperand(0);
471     Info.offset = 0;
472     Info.align = Align(4);
473     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore |
474                  MachineMemOperand::MOVolatile;
475     return true;
476   }
477 }
478 
479 bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL,
480                                                 const AddrMode &AM, Type *Ty,
481                                                 unsigned AS,
482                                                 Instruction *I) const {
483   // No global is ever allowed as a base.
484   if (AM.BaseGV)
485     return false;
486 
487   // Require a 12-bit signed offset.
488   if (!isInt<12>(AM.BaseOffs))
489     return false;
490 
491   switch (AM.Scale) {
492   case 0: // "r+i" or just "i", depending on HasBaseReg.
493     break;
494   case 1:
495     if (!AM.HasBaseReg) // allow "r+i".
496       break;
497     return false; // disallow "r+r" or "r+r+i".
498   default:
499     return false;
500   }
501 
502   return true;
503 }
504 
505 bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
506   return isInt<12>(Imm);
507 }
508 
509 bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
510   return isInt<12>(Imm);
511 }
512 
513 // On RV32, 64-bit integers are split into their high and low parts and held
514 // in two different registers, so the trunc is free since the low register can
515 // just be used.
516 bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
517   if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
518     return false;
519   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
520   unsigned DestBits = DstTy->getPrimitiveSizeInBits();
521   return (SrcBits == 64 && DestBits == 32);
522 }
523 
524 bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
525   if (Subtarget.is64Bit() || SrcVT.isVector() || DstVT.isVector() ||
526       !SrcVT.isInteger() || !DstVT.isInteger())
527     return false;
528   unsigned SrcBits = SrcVT.getSizeInBits();
529   unsigned DestBits = DstVT.getSizeInBits();
530   return (SrcBits == 64 && DestBits == 32);
531 }
532 
533 bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
534   // Zexts are free if they can be combined with a load.
535   if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
536     EVT MemVT = LD->getMemoryVT();
537     if ((MemVT == MVT::i8 || MemVT == MVT::i16 ||
538          (Subtarget.is64Bit() && MemVT == MVT::i32)) &&
539         (LD->getExtensionType() == ISD::NON_EXTLOAD ||
540          LD->getExtensionType() == ISD::ZEXTLOAD))
541       return true;
542   }
543 
544   return TargetLowering::isZExtFree(Val, VT2);
545 }
546 
547 bool RISCVTargetLowering::isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const {
548   return Subtarget.is64Bit() && SrcVT == MVT::i32 && DstVT == MVT::i64;
549 }
550 
551 bool RISCVTargetLowering::isCheapToSpeculateCttz() const {
552   return Subtarget.hasStdExtZbb();
553 }
554 
555 bool RISCVTargetLowering::isCheapToSpeculateCtlz() const {
556   return Subtarget.hasStdExtZbb();
557 }
558 
559 bool RISCVTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
560                                        bool ForCodeSize) const {
561   if (VT == MVT::f16 && !Subtarget.hasStdExtZfh())
562     return false;
563   if (VT == MVT::f32 && !Subtarget.hasStdExtF())
564     return false;
565   if (VT == MVT::f64 && !Subtarget.hasStdExtD())
566     return false;
567   if (Imm.isNegZero())
568     return false;
569   return Imm.isZero();
570 }
571 
572 bool RISCVTargetLowering::hasBitPreservingFPLogic(EVT VT) const {
573   return (VT == MVT::f16 && Subtarget.hasStdExtZfh()) ||
574          (VT == MVT::f32 && Subtarget.hasStdExtF()) ||
575          (VT == MVT::f64 && Subtarget.hasStdExtD());
576 }
577 
578 // Changes the condition code and swaps operands if necessary, so the SetCC
579 // operation matches one of the comparisons supported directly in the RISC-V
580 // ISA.
581 static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
582   switch (CC) {
583   default:
584     break;
585   case ISD::SETGT:
586   case ISD::SETLE:
587   case ISD::SETUGT:
588   case ISD::SETULE:
589     CC = ISD::getSetCCSwappedOperands(CC);
590     std::swap(LHS, RHS);
591     break;
592   }
593 }
594 
595 // Return the RISC-V branch opcode that matches the given DAG integer
596 // condition code. The CondCode must be one of those supported by the RISC-V
597 // ISA (see normaliseSetCC).
598 static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
599   switch (CC) {
600   default:
601     llvm_unreachable("Unsupported CondCode");
602   case ISD::SETEQ:
603     return RISCV::BEQ;
604   case ISD::SETNE:
605     return RISCV::BNE;
606   case ISD::SETLT:
607     return RISCV::BLT;
608   case ISD::SETGE:
609     return RISCV::BGE;
610   case ISD::SETULT:
611     return RISCV::BLTU;
612   case ISD::SETUGE:
613     return RISCV::BGEU;
614   }
615 }
616 
617 SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
618                                             SelectionDAG &DAG) const {
619   switch (Op.getOpcode()) {
620   default:
621     report_fatal_error("unimplemented operand");
622   case ISD::GlobalAddress:
623     return lowerGlobalAddress(Op, DAG);
624   case ISD::BlockAddress:
625     return lowerBlockAddress(Op, DAG);
626   case ISD::ConstantPool:
627     return lowerConstantPool(Op, DAG);
628   case ISD::JumpTable:
629     return lowerJumpTable(Op, DAG);
630   case ISD::GlobalTLSAddress:
631     return lowerGlobalTLSAddress(Op, DAG);
632   case ISD::SELECT:
633     return lowerSELECT(Op, DAG);
634   case ISD::VASTART:
635     return lowerVASTART(Op, DAG);
636   case ISD::FRAMEADDR:
637     return lowerFRAMEADDR(Op, DAG);
638   case ISD::RETURNADDR:
639     return lowerRETURNADDR(Op, DAG);
640   case ISD::SHL_PARTS:
641     return lowerShiftLeftParts(Op, DAG);
642   case ISD::SRA_PARTS:
643     return lowerShiftRightParts(Op, DAG, true);
644   case ISD::SRL_PARTS:
645     return lowerShiftRightParts(Op, DAG, false);
646   case ISD::BITCAST: {
647     assert(((Subtarget.is64Bit() && Subtarget.hasStdExtF()) ||
648             Subtarget.hasStdExtZfh()) &&
649            "Unexpected custom legalisation");
650     SDLoc DL(Op);
651     SDValue Op0 = Op.getOperand(0);
652     if (Op.getValueType() == MVT::f16 && Subtarget.hasStdExtZfh()) {
653       if (Op0.getValueType() != MVT::i16)
654         return SDValue();
655       SDValue NewOp0 =
656           DAG.getNode(ISD::ANY_EXTEND, DL, Subtarget.getXLenVT(), Op0);
657       SDValue FPConv = DAG.getNode(RISCVISD::FMV_H_X, DL, MVT::f16, NewOp0);
658       return FPConv;
659     } else if (Op.getValueType() == MVT::f32 && Subtarget.is64Bit() &&
660                Subtarget.hasStdExtF()) {
661       if (Op0.getValueType() != MVT::i32)
662         return SDValue();
663       SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
664       SDValue FPConv =
665           DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, NewOp0);
666       return FPConv;
667     }
668     return SDValue();
669   }
670   case ISD::INTRINSIC_WO_CHAIN:
671     return LowerINTRINSIC_WO_CHAIN(Op, DAG);
672   case ISD::INTRINSIC_W_CHAIN:
673     return LowerINTRINSIC_W_CHAIN(Op, DAG);
674   case ISD::BSWAP:
675   case ISD::BITREVERSE: {
676     // Convert BSWAP/BITREVERSE to GREVI to enable GREVI combinining.
677     assert(Subtarget.hasStdExtZbp() && "Unexpected custom legalisation");
678     MVT VT = Op.getSimpleValueType();
679     SDLoc DL(Op);
680     // Start with the maximum immediate value which is the bitwidth - 1.
681     unsigned Imm = VT.getSizeInBits() - 1;
682     // If this is BSWAP rather than BITREVERSE, clear the lower 3 bits.
683     if (Op.getOpcode() == ISD::BSWAP)
684       Imm &= ~0x7U;
685     return DAG.getNode(RISCVISD::GREVI, DL, VT, Op.getOperand(0),
686                        DAG.getTargetConstant(Imm, DL, Subtarget.getXLenVT()));
687   }
688   case ISD::SPLAT_VECTOR:
689     return lowerSPLATVECTOR(Op, DAG);
690   case ISD::VSCALE: {
691     MVT VT = Op.getSimpleValueType();
692     SDLoc DL(Op);
693     SDValue VLENB = DAG.getNode(RISCVISD::READ_VLENB, DL, VT);
694     // We define our scalable vector types for lmul=1 to use a 64 bit known
695     // minimum size. e.g. <vscale x 2 x i32>. VLENB is in bytes so we calculate
696     // vscale as VLENB / 8.
697     SDValue VScale = DAG.getNode(ISD::SRL, DL, VT, VLENB,
698                                  DAG.getConstant(3, DL, VT));
699     return DAG.getNode(ISD::MUL, DL, VT, VScale, Op.getOperand(0));
700   }
701   }
702 }
703 
704 static SDValue getTargetNode(GlobalAddressSDNode *N, SDLoc DL, EVT Ty,
705                              SelectionDAG &DAG, unsigned Flags) {
706   return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
707 }
708 
709 static SDValue getTargetNode(BlockAddressSDNode *N, SDLoc DL, EVT Ty,
710                              SelectionDAG &DAG, unsigned Flags) {
711   return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, N->getOffset(),
712                                    Flags);
713 }
714 
715 static SDValue getTargetNode(ConstantPoolSDNode *N, SDLoc DL, EVT Ty,
716                              SelectionDAG &DAG, unsigned Flags) {
717   return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
718                                    N->getOffset(), Flags);
719 }
720 
721 static SDValue getTargetNode(JumpTableSDNode *N, SDLoc DL, EVT Ty,
722                              SelectionDAG &DAG, unsigned Flags) {
723   return DAG.getTargetJumpTable(N->getIndex(), Ty, Flags);
724 }
725 
726 template <class NodeTy>
727 SDValue RISCVTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
728                                      bool IsLocal) const {
729   SDLoc DL(N);
730   EVT Ty = getPointerTy(DAG.getDataLayout());
731 
732   if (isPositionIndependent()) {
733     SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
734     if (IsLocal)
735       // Use PC-relative addressing to access the symbol. This generates the
736       // pattern (PseudoLLA sym), which expands to (addi (auipc %pcrel_hi(sym))
737       // %pcrel_lo(auipc)).
738       return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
739 
740     // Use PC-relative addressing to access the GOT for this symbol, then load
741     // the address from the GOT. This generates the pattern (PseudoLA sym),
742     // which expands to (ld (addi (auipc %got_pcrel_hi(sym)) %pcrel_lo(auipc))).
743     return SDValue(DAG.getMachineNode(RISCV::PseudoLA, DL, Ty, Addr), 0);
744   }
745 
746   switch (getTargetMachine().getCodeModel()) {
747   default:
748     report_fatal_error("Unsupported code model for lowering");
749   case CodeModel::Small: {
750     // Generate a sequence for accessing addresses within the first 2 GiB of
751     // address space. This generates the pattern (addi (lui %hi(sym)) %lo(sym)).
752     SDValue AddrHi = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_HI);
753     SDValue AddrLo = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_LO);
754     SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
755     return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, AddrLo), 0);
756   }
757   case CodeModel::Medium: {
758     // Generate a sequence for accessing addresses within any 2GiB range within
759     // the address space. This generates the pattern (PseudoLLA sym), which
760     // expands to (addi (auipc %pcrel_hi(sym)) %pcrel_lo(auipc)).
761     SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
762     return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
763   }
764   }
765 }
766 
767 SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
768                                                 SelectionDAG &DAG) const {
769   SDLoc DL(Op);
770   EVT Ty = Op.getValueType();
771   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
772   int64_t Offset = N->getOffset();
773   MVT XLenVT = Subtarget.getXLenVT();
774 
775   const GlobalValue *GV = N->getGlobal();
776   bool IsLocal = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
777   SDValue Addr = getAddr(N, DAG, IsLocal);
778 
779   // In order to maximise the opportunity for common subexpression elimination,
780   // emit a separate ADD node for the global address offset instead of folding
781   // it in the global address node. Later peephole optimisations may choose to
782   // fold it back in when profitable.
783   if (Offset != 0)
784     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
785                        DAG.getConstant(Offset, DL, XLenVT));
786   return Addr;
787 }
788 
789 SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
790                                                SelectionDAG &DAG) const {
791   BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
792 
793   return getAddr(N, DAG);
794 }
795 
796 SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
797                                                SelectionDAG &DAG) const {
798   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
799 
800   return getAddr(N, DAG);
801 }
802 
803 SDValue RISCVTargetLowering::lowerJumpTable(SDValue Op,
804                                             SelectionDAG &DAG) const {
805   JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
806 
807   return getAddr(N, DAG);
808 }
809 
810 SDValue RISCVTargetLowering::getStaticTLSAddr(GlobalAddressSDNode *N,
811                                               SelectionDAG &DAG,
812                                               bool UseGOT) const {
813   SDLoc DL(N);
814   EVT Ty = getPointerTy(DAG.getDataLayout());
815   const GlobalValue *GV = N->getGlobal();
816   MVT XLenVT = Subtarget.getXLenVT();
817 
818   if (UseGOT) {
819     // Use PC-relative addressing to access the GOT for this TLS symbol, then
820     // load the address from the GOT and add the thread pointer. This generates
821     // the pattern (PseudoLA_TLS_IE sym), which expands to
822     // (ld (auipc %tls_ie_pcrel_hi(sym)) %pcrel_lo(auipc)).
823     SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
824     SDValue Load =
825         SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_IE, DL, Ty, Addr), 0);
826 
827     // Add the thread pointer.
828     SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
829     return DAG.getNode(ISD::ADD, DL, Ty, Load, TPReg);
830   }
831 
832   // Generate a sequence for accessing the address relative to the thread
833   // pointer, with the appropriate adjustment for the thread pointer offset.
834   // This generates the pattern
835   // (add (add_tprel (lui %tprel_hi(sym)) tp %tprel_add(sym)) %tprel_lo(sym))
836   SDValue AddrHi =
837       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_HI);
838   SDValue AddrAdd =
839       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_ADD);
840   SDValue AddrLo =
841       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_LO);
842 
843   SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
844   SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
845   SDValue MNAdd = SDValue(
846       DAG.getMachineNode(RISCV::PseudoAddTPRel, DL, Ty, MNHi, TPReg, AddrAdd),
847       0);
848   return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNAdd, AddrLo), 0);
849 }
850 
851 SDValue RISCVTargetLowering::getDynamicTLSAddr(GlobalAddressSDNode *N,
852                                                SelectionDAG &DAG) const {
853   SDLoc DL(N);
854   EVT Ty = getPointerTy(DAG.getDataLayout());
855   IntegerType *CallTy = Type::getIntNTy(*DAG.getContext(), Ty.getSizeInBits());
856   const GlobalValue *GV = N->getGlobal();
857 
858   // Use a PC-relative addressing mode to access the global dynamic GOT address.
859   // This generates the pattern (PseudoLA_TLS_GD sym), which expands to
860   // (addi (auipc %tls_gd_pcrel_hi(sym)) %pcrel_lo(auipc)).
861   SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
862   SDValue Load =
863       SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_GD, DL, Ty, Addr), 0);
864 
865   // Prepare argument list to generate call.
866   ArgListTy Args;
867   ArgListEntry Entry;
868   Entry.Node = Load;
869   Entry.Ty = CallTy;
870   Args.push_back(Entry);
871 
872   // Setup call to __tls_get_addr.
873   TargetLowering::CallLoweringInfo CLI(DAG);
874   CLI.setDebugLoc(DL)
875       .setChain(DAG.getEntryNode())
876       .setLibCallee(CallingConv::C, CallTy,
877                     DAG.getExternalSymbol("__tls_get_addr", Ty),
878                     std::move(Args));
879 
880   return LowerCallTo(CLI).first;
881 }
882 
883 SDValue RISCVTargetLowering::lowerGlobalTLSAddress(SDValue Op,
884                                                    SelectionDAG &DAG) const {
885   SDLoc DL(Op);
886   EVT Ty = Op.getValueType();
887   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
888   int64_t Offset = N->getOffset();
889   MVT XLenVT = Subtarget.getXLenVT();
890 
891   TLSModel::Model Model = getTargetMachine().getTLSModel(N->getGlobal());
892 
893   if (DAG.getMachineFunction().getFunction().getCallingConv() ==
894       CallingConv::GHC)
895     report_fatal_error("In GHC calling convention TLS is not supported");
896 
897   SDValue Addr;
898   switch (Model) {
899   case TLSModel::LocalExec:
900     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/false);
901     break;
902   case TLSModel::InitialExec:
903     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/true);
904     break;
905   case TLSModel::LocalDynamic:
906   case TLSModel::GeneralDynamic:
907     Addr = getDynamicTLSAddr(N, DAG);
908     break;
909   }
910 
911   // In order to maximise the opportunity for common subexpression elimination,
912   // emit a separate ADD node for the global address offset instead of folding
913   // it in the global address node. Later peephole optimisations may choose to
914   // fold it back in when profitable.
915   if (Offset != 0)
916     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
917                        DAG.getConstant(Offset, DL, XLenVT));
918   return Addr;
919 }
920 
921 SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
922   SDValue CondV = Op.getOperand(0);
923   SDValue TrueV = Op.getOperand(1);
924   SDValue FalseV = Op.getOperand(2);
925   SDLoc DL(Op);
926   MVT XLenVT = Subtarget.getXLenVT();
927 
928   // If the result type is XLenVT and CondV is the output of a SETCC node
929   // which also operated on XLenVT inputs, then merge the SETCC node into the
930   // lowered RISCVISD::SELECT_CC to take advantage of the integer
931   // compare+branch instructions. i.e.:
932   // (select (setcc lhs, rhs, cc), truev, falsev)
933   // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
934   if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
935       CondV.getOperand(0).getSimpleValueType() == XLenVT) {
936     SDValue LHS = CondV.getOperand(0);
937     SDValue RHS = CondV.getOperand(1);
938     auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
939     ISD::CondCode CCVal = CC->get();
940 
941     normaliseSetCC(LHS, RHS, CCVal);
942 
943     SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
944     SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
945     return DAG.getNode(RISCVISD::SELECT_CC, DL, Op.getValueType(), Ops);
946   }
947 
948   // Otherwise:
949   // (select condv, truev, falsev)
950   // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
951   SDValue Zero = DAG.getConstant(0, DL, XLenVT);
952   SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
953 
954   SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
955 
956   return DAG.getNode(RISCVISD::SELECT_CC, DL, Op.getValueType(), Ops);
957 }
958 
959 SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
960   MachineFunction &MF = DAG.getMachineFunction();
961   RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
962 
963   SDLoc DL(Op);
964   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
965                                  getPointerTy(MF.getDataLayout()));
966 
967   // vastart just stores the address of the VarArgsFrameIndex slot into the
968   // memory location argument.
969   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
970   return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
971                       MachinePointerInfo(SV));
972 }
973 
974 SDValue RISCVTargetLowering::lowerFRAMEADDR(SDValue Op,
975                                             SelectionDAG &DAG) const {
976   const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
977   MachineFunction &MF = DAG.getMachineFunction();
978   MachineFrameInfo &MFI = MF.getFrameInfo();
979   MFI.setFrameAddressIsTaken(true);
980   Register FrameReg = RI.getFrameRegister(MF);
981   int XLenInBytes = Subtarget.getXLen() / 8;
982 
983   EVT VT = Op.getValueType();
984   SDLoc DL(Op);
985   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
986   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
987   while (Depth--) {
988     int Offset = -(XLenInBytes * 2);
989     SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
990                               DAG.getIntPtrConstant(Offset, DL));
991     FrameAddr =
992         DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
993   }
994   return FrameAddr;
995 }
996 
997 SDValue RISCVTargetLowering::lowerRETURNADDR(SDValue Op,
998                                              SelectionDAG &DAG) const {
999   const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
1000   MachineFunction &MF = DAG.getMachineFunction();
1001   MachineFrameInfo &MFI = MF.getFrameInfo();
1002   MFI.setReturnAddressIsTaken(true);
1003   MVT XLenVT = Subtarget.getXLenVT();
1004   int XLenInBytes = Subtarget.getXLen() / 8;
1005 
1006   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1007     return SDValue();
1008 
1009   EVT VT = Op.getValueType();
1010   SDLoc DL(Op);
1011   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1012   if (Depth) {
1013     int Off = -XLenInBytes;
1014     SDValue FrameAddr = lowerFRAMEADDR(Op, DAG);
1015     SDValue Offset = DAG.getConstant(Off, DL, VT);
1016     return DAG.getLoad(VT, DL, DAG.getEntryNode(),
1017                        DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
1018                        MachinePointerInfo());
1019   }
1020 
1021   // Return the value of the return address register, marking it an implicit
1022   // live-in.
1023   Register Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
1024   return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
1025 }
1026 
1027 SDValue RISCVTargetLowering::lowerShiftLeftParts(SDValue Op,
1028                                                  SelectionDAG &DAG) const {
1029   SDLoc DL(Op);
1030   SDValue Lo = Op.getOperand(0);
1031   SDValue Hi = Op.getOperand(1);
1032   SDValue Shamt = Op.getOperand(2);
1033   EVT VT = Lo.getValueType();
1034 
1035   // if Shamt-XLEN < 0: // Shamt < XLEN
1036   //   Lo = Lo << Shamt
1037   //   Hi = (Hi << Shamt) | ((Lo >>u 1) >>u (XLEN-1 - Shamt))
1038   // else:
1039   //   Lo = 0
1040   //   Hi = Lo << (Shamt-XLEN)
1041 
1042   SDValue Zero = DAG.getConstant(0, DL, VT);
1043   SDValue One = DAG.getConstant(1, DL, VT);
1044   SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
1045   SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
1046   SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
1047   SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
1048 
1049   SDValue LoTrue = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
1050   SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo, One);
1051   SDValue ShiftRightLo =
1052       DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, XLenMinus1Shamt);
1053   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
1054   SDValue HiTrue = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
1055   SDValue HiFalse = DAG.getNode(ISD::SHL, DL, VT, Lo, ShamtMinusXLen);
1056 
1057   SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
1058 
1059   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, Zero);
1060   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
1061 
1062   SDValue Parts[2] = {Lo, Hi};
1063   return DAG.getMergeValues(Parts, DL);
1064 }
1065 
1066 SDValue RISCVTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
1067                                                   bool IsSRA) const {
1068   SDLoc DL(Op);
1069   SDValue Lo = Op.getOperand(0);
1070   SDValue Hi = Op.getOperand(1);
1071   SDValue Shamt = Op.getOperand(2);
1072   EVT VT = Lo.getValueType();
1073 
1074   // SRA expansion:
1075   //   if Shamt-XLEN < 0: // Shamt < XLEN
1076   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
1077   //     Hi = Hi >>s Shamt
1078   //   else:
1079   //     Lo = Hi >>s (Shamt-XLEN);
1080   //     Hi = Hi >>s (XLEN-1)
1081   //
1082   // SRL expansion:
1083   //   if Shamt-XLEN < 0: // Shamt < XLEN
1084   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
1085   //     Hi = Hi >>u Shamt
1086   //   else:
1087   //     Lo = Hi >>u (Shamt-XLEN);
1088   //     Hi = 0;
1089 
1090   unsigned ShiftRightOp = IsSRA ? ISD::SRA : ISD::SRL;
1091 
1092   SDValue Zero = DAG.getConstant(0, DL, VT);
1093   SDValue One = DAG.getConstant(1, DL, VT);
1094   SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
1095   SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
1096   SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
1097   SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
1098 
1099   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
1100   SDValue ShiftLeftHi1 = DAG.getNode(ISD::SHL, DL, VT, Hi, One);
1101   SDValue ShiftLeftHi =
1102       DAG.getNode(ISD::SHL, DL, VT, ShiftLeftHi1, XLenMinus1Shamt);
1103   SDValue LoTrue = DAG.getNode(ISD::OR, DL, VT, ShiftRightLo, ShiftLeftHi);
1104   SDValue HiTrue = DAG.getNode(ShiftRightOp, DL, VT, Hi, Shamt);
1105   SDValue LoFalse = DAG.getNode(ShiftRightOp, DL, VT, Hi, ShamtMinusXLen);
1106   SDValue HiFalse =
1107       IsSRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, XLenMinus1) : Zero;
1108 
1109   SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
1110 
1111   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, LoFalse);
1112   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
1113 
1114   SDValue Parts[2] = {Lo, Hi};
1115   return DAG.getMergeValues(Parts, DL);
1116 }
1117 
1118 // Custom-lower a SPLAT_VECTOR where XLEN<SEW, as the SEW element type is
1119 // illegal (currently only vXi64 RV32).
1120 // FIXME: We could also catch non-constant sign-extended i32 values and lower
1121 // them to SPLAT_VECTOR_I64
1122 SDValue RISCVTargetLowering::lowerSPLATVECTOR(SDValue Op,
1123                                               SelectionDAG &DAG) const {
1124   SDLoc DL(Op);
1125   EVT VecVT = Op.getValueType();
1126   assert(!Subtarget.is64Bit() && VecVT.getVectorElementType() == MVT::i64 &&
1127          "Unexpected SPLAT_VECTOR lowering");
1128   SDValue SplatVal = Op.getOperand(0);
1129 
1130   // If we can prove that the value is a sign-extended 32-bit value, lower this
1131   // as a custom node in order to try and match RVV vector/scalar instructions.
1132   if (auto *CVal = dyn_cast<ConstantSDNode>(SplatVal)) {
1133     if (isInt<32>(CVal->getSExtValue()))
1134       return DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VecVT,
1135                          DAG.getConstant(CVal->getSExtValue(), DL, MVT::i32));
1136   }
1137 
1138   // Else, on RV32 we lower an i64-element SPLAT_VECTOR thus, being careful not
1139   // to accidentally sign-extend the 32-bit halves to the e64 SEW:
1140   // vmv.v.x vX, hi
1141   // vsll.vx vX, vX, /*32*/
1142   // vmv.v.x vY, lo
1143   // vsll.vx vY, vY, /*32*/
1144   // vsrl.vx vY, vY, /*32*/
1145   // vor.vv vX, vX, vY
1146   SDValue One = DAG.getConstant(1, DL, MVT::i32);
1147   SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1148   SDValue ThirtyTwoV = DAG.getConstant(32, DL, VecVT);
1149   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, SplatVal, Zero);
1150   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, SplatVal, One);
1151 
1152   Lo = DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VecVT, Lo);
1153   Lo = DAG.getNode(ISD::SHL, DL, VecVT, Lo, ThirtyTwoV);
1154   Lo = DAG.getNode(ISD::SRL, DL, VecVT, Lo, ThirtyTwoV);
1155 
1156   if (isNullConstant(Hi))
1157     return Lo;
1158 
1159   Hi = DAG.getNode(RISCVISD::SPLAT_VECTOR_I64, DL, VecVT, Hi);
1160   Hi = DAG.getNode(ISD::SHL, DL, VecVT, Hi, ThirtyTwoV);
1161 
1162   return DAG.getNode(ISD::OR, DL, VecVT, Lo, Hi);
1163 }
1164 
1165 SDValue RISCVTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1166                                                      SelectionDAG &DAG) const {
1167   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1168   SDLoc DL(Op);
1169 
1170   if (Subtarget.hasStdExtV()) {
1171     // Some RVV intrinsics may claim that they want an integer operand to be
1172     // extended.
1173     if (const RISCVVIntrinsicsTable::RISCVVIntrinsicInfo *II =
1174             RISCVVIntrinsicsTable::getRISCVVIntrinsicInfo(IntNo)) {
1175       if (II->ExtendedOperand) {
1176         assert(II->ExtendedOperand < Op.getNumOperands());
1177         SmallVector<SDValue, 8> Operands(Op->op_begin(), Op->op_end());
1178         SDValue &ScalarOp = Operands[II->ExtendedOperand];
1179         EVT OpVT = ScalarOp.getValueType();
1180         if (OpVT == MVT::i8 || OpVT == MVT::i16 ||
1181             (OpVT == MVT::i32 && Subtarget.is64Bit())) {
1182           // If the operand is a constant, sign extend to increase our chances
1183           // of being able to use a .vi instruction. ANY_EXTEND would become a
1184           // a zero extend and the simm5 check in isel would fail.
1185           // FIXME: Should we ignore the upper bits in isel instead?
1186           unsigned ExtOpc = isa<ConstantSDNode>(ScalarOp) ? ISD::SIGN_EXTEND
1187                                                           : ISD::ANY_EXTEND;
1188           ScalarOp = DAG.getNode(ExtOpc, DL, Subtarget.getXLenVT(), ScalarOp);
1189           return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, Op.getValueType(),
1190                              Operands);
1191         }
1192       }
1193     }
1194   }
1195 
1196   switch (IntNo) {
1197   default:
1198     return SDValue();    // Don't custom lower most intrinsics.
1199   case Intrinsic::thread_pointer: {
1200     EVT PtrVT = getPointerTy(DAG.getDataLayout());
1201     return DAG.getRegister(RISCV::X4, PtrVT);
1202   }
1203   case Intrinsic::riscv_vmv_x_s:
1204     assert(Op.getValueType() == Subtarget.getXLenVT() && "Unexpected VT!");
1205     return DAG.getNode(RISCVISD::VMV_X_S, DL, Op.getValueType(),
1206                        Op.getOperand(1));
1207   }
1208 }
1209 
1210 SDValue RISCVTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
1211                                                     SelectionDAG &DAG) const {
1212   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1213   SDLoc DL(Op);
1214 
1215   if (Subtarget.hasStdExtV()) {
1216     // Some RVV intrinsics may claim that they want an integer operand to be
1217     // extended.
1218     if (const RISCVVIntrinsicsTable::RISCVVIntrinsicInfo *II =
1219             RISCVVIntrinsicsTable::getRISCVVIntrinsicInfo(IntNo)) {
1220       if (II->ExtendedOperand) {
1221         // The operands start from the second argument in INTRINSIC_W_CHAIN.
1222         unsigned ExtendOp = II->ExtendedOperand + 1;
1223         assert(ExtendOp < Op.getNumOperands());
1224         SmallVector<SDValue, 8> Operands(Op->op_begin(), Op->op_end());
1225         SDValue &ScalarOp = Operands[ExtendOp];
1226         EVT OpVT = ScalarOp.getValueType();
1227         if (OpVT == MVT::i8 || OpVT == MVT::i16 ||
1228             (OpVT == MVT::i32 && Subtarget.is64Bit())) {
1229           // If the operand is a constant, sign extend to increase our chances
1230           // of being able to use a .vi instruction. ANY_EXTEND would become a
1231           // a zero extend and the simm5 check in isel would fail.
1232           // FIXME: Should we ignore the upper bits in isel instead?
1233           unsigned ExtOpc = isa<ConstantSDNode>(ScalarOp) ? ISD::SIGN_EXTEND
1234                                                           : ISD::ANY_EXTEND;
1235           ScalarOp = DAG.getNode(ExtOpc, DL, Subtarget.getXLenVT(), ScalarOp);
1236           return DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL, Op->getVTList(),
1237                              Operands);
1238         }
1239       }
1240     }
1241   }
1242 
1243   return SDValue();
1244 }
1245 
1246 // Returns the opcode of the target-specific SDNode that implements the 32-bit
1247 // form of the given Opcode.
1248 static RISCVISD::NodeType getRISCVWOpcode(unsigned Opcode) {
1249   switch (Opcode) {
1250   default:
1251     llvm_unreachable("Unexpected opcode");
1252   case ISD::SHL:
1253     return RISCVISD::SLLW;
1254   case ISD::SRA:
1255     return RISCVISD::SRAW;
1256   case ISD::SRL:
1257     return RISCVISD::SRLW;
1258   case ISD::SDIV:
1259     return RISCVISD::DIVW;
1260   case ISD::UDIV:
1261     return RISCVISD::DIVUW;
1262   case ISD::UREM:
1263     return RISCVISD::REMUW;
1264   case ISD::ROTL:
1265     return RISCVISD::ROLW;
1266   case ISD::ROTR:
1267     return RISCVISD::RORW;
1268   case RISCVISD::GREVI:
1269     return RISCVISD::GREVIW;
1270   case RISCVISD::GORCI:
1271     return RISCVISD::GORCIW;
1272   }
1273 }
1274 
1275 // Converts the given 32-bit operation to a target-specific SelectionDAG node.
1276 // Because i32 isn't a legal type for RV64, these operations would otherwise
1277 // be promoted to i64, making it difficult to select the SLLW/DIVUW/.../*W
1278 // later one because the fact the operation was originally of type i32 is
1279 // lost.
1280 static SDValue customLegalizeToWOp(SDNode *N, SelectionDAG &DAG) {
1281   SDLoc DL(N);
1282   RISCVISD::NodeType WOpcode = getRISCVWOpcode(N->getOpcode());
1283   SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
1284   SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
1285   SDValue NewRes = DAG.getNode(WOpcode, DL, MVT::i64, NewOp0, NewOp1);
1286   // ReplaceNodeResults requires we maintain the same type for the return value.
1287   return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes);
1288 }
1289 
1290 // Converts the given 32-bit operation to a i64 operation with signed extension
1291 // semantic to reduce the signed extension instructions.
1292 static SDValue customLegalizeToWOpWithSExt(SDNode *N, SelectionDAG &DAG) {
1293   SDLoc DL(N);
1294   SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
1295   SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
1296   SDValue NewWOp = DAG.getNode(N->getOpcode(), DL, MVT::i64, NewOp0, NewOp1);
1297   SDValue NewRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i64, NewWOp,
1298                                DAG.getValueType(MVT::i32));
1299   return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes);
1300 }
1301 
1302 void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
1303                                              SmallVectorImpl<SDValue> &Results,
1304                                              SelectionDAG &DAG) const {
1305   SDLoc DL(N);
1306   switch (N->getOpcode()) {
1307   default:
1308     llvm_unreachable("Don't know how to custom type legalize this operation!");
1309   case ISD::STRICT_FP_TO_SINT:
1310   case ISD::STRICT_FP_TO_UINT:
1311   case ISD::FP_TO_SINT:
1312   case ISD::FP_TO_UINT: {
1313     bool IsStrict = N->isStrictFPOpcode();
1314     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1315            "Unexpected custom legalisation");
1316     SDValue Op0 = IsStrict ? N->getOperand(1) : N->getOperand(0);
1317     // If the FP type needs to be softened, emit a library call using the 'si'
1318     // version. If we left it to default legalization we'd end up with 'di'. If
1319     // the FP type doesn't need to be softened just let generic type
1320     // legalization promote the result type.
1321     if (getTypeAction(*DAG.getContext(), Op0.getValueType()) !=
1322         TargetLowering::TypeSoftenFloat)
1323       return;
1324     RTLIB::Libcall LC;
1325     if (N->getOpcode() == ISD::FP_TO_SINT ||
1326         N->getOpcode() == ISD::STRICT_FP_TO_SINT)
1327       LC = RTLIB::getFPTOSINT(Op0.getValueType(), N->getValueType(0));
1328     else
1329       LC = RTLIB::getFPTOUINT(Op0.getValueType(), N->getValueType(0));
1330     MakeLibCallOptions CallOptions;
1331     EVT OpVT = Op0.getValueType();
1332     CallOptions.setTypeListBeforeSoften(OpVT, N->getValueType(0), true);
1333     SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
1334     SDValue Result;
1335     std::tie(Result, Chain) =
1336         makeLibCall(DAG, LC, N->getValueType(0), Op0, CallOptions, DL, Chain);
1337     Results.push_back(Result);
1338     if (IsStrict)
1339       Results.push_back(Chain);
1340     break;
1341   }
1342   case ISD::READCYCLECOUNTER: {
1343     assert(!Subtarget.is64Bit() &&
1344            "READCYCLECOUNTER only has custom type legalization on riscv32");
1345 
1346     SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
1347     SDValue RCW =
1348         DAG.getNode(RISCVISD::READ_CYCLE_WIDE, DL, VTs, N->getOperand(0));
1349 
1350     Results.push_back(
1351         DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, RCW, RCW.getValue(1)));
1352     Results.push_back(RCW.getValue(2));
1353     break;
1354   }
1355   case ISD::ADD:
1356   case ISD::SUB:
1357   case ISD::MUL:
1358     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1359            "Unexpected custom legalisation");
1360     if (N->getOperand(1).getOpcode() == ISD::Constant)
1361       return;
1362     Results.push_back(customLegalizeToWOpWithSExt(N, DAG));
1363     break;
1364   case ISD::SHL:
1365   case ISD::SRA:
1366   case ISD::SRL:
1367     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1368            "Unexpected custom legalisation");
1369     if (N->getOperand(1).getOpcode() == ISD::Constant)
1370       return;
1371     Results.push_back(customLegalizeToWOp(N, DAG));
1372     break;
1373   case ISD::ROTL:
1374   case ISD::ROTR:
1375     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1376            "Unexpected custom legalisation");
1377     Results.push_back(customLegalizeToWOp(N, DAG));
1378     break;
1379   case ISD::SDIV:
1380   case ISD::UDIV:
1381   case ISD::UREM:
1382     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1383            Subtarget.hasStdExtM() && "Unexpected custom legalisation");
1384     if (N->getOperand(0).getOpcode() == ISD::Constant ||
1385         N->getOperand(1).getOpcode() == ISD::Constant)
1386       return;
1387     Results.push_back(customLegalizeToWOp(N, DAG));
1388     break;
1389   case ISD::BITCAST: {
1390     assert(((N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1391              Subtarget.hasStdExtF()) ||
1392             (N->getValueType(0) == MVT::i16 && Subtarget.hasStdExtZfh())) &&
1393            "Unexpected custom legalisation");
1394     SDValue Op0 = N->getOperand(0);
1395     if (N->getValueType(0) == MVT::i16 && Subtarget.hasStdExtZfh()) {
1396       if (Op0.getValueType() != MVT::f16)
1397         return;
1398       SDValue FPConv =
1399           DAG.getNode(RISCVISD::FMV_X_ANYEXTH, DL, Subtarget.getXLenVT(), Op0);
1400       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FPConv));
1401     } else if (N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1402                Subtarget.hasStdExtF()) {
1403       if (Op0.getValueType() != MVT::f32)
1404         return;
1405       SDValue FPConv =
1406           DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Op0);
1407       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, FPConv));
1408     }
1409     break;
1410   }
1411   case RISCVISD::GREVI:
1412   case RISCVISD::GORCI: {
1413     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1414            "Unexpected custom legalisation");
1415     // This is similar to customLegalizeToWOp, except that we pass the second
1416     // operand (a TargetConstant) straight through: it is already of type
1417     // XLenVT.
1418     SDLoc DL(N);
1419     RISCVISD::NodeType WOpcode = getRISCVWOpcode(N->getOpcode());
1420     SDValue NewOp0 =
1421         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
1422     SDValue NewRes =
1423         DAG.getNode(WOpcode, DL, MVT::i64, NewOp0, N->getOperand(1));
1424     // ReplaceNodeResults requires we maintain the same type for the return
1425     // value.
1426     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes));
1427     break;
1428   }
1429   case ISD::BSWAP:
1430   case ISD::BITREVERSE: {
1431     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1432            Subtarget.hasStdExtZbp() && "Unexpected custom legalisation");
1433     SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64,
1434                                  N->getOperand(0));
1435     unsigned Imm = N->getOpcode() == ISD::BITREVERSE ? 31 : 24;
1436     SDValue GREVIW = DAG.getNode(RISCVISD::GREVIW, DL, MVT::i64, NewOp0,
1437                                  DAG.getTargetConstant(Imm, DL,
1438                                                        Subtarget.getXLenVT()));
1439     // ReplaceNodeResults requires we maintain the same type for the return
1440     // value.
1441     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, GREVIW));
1442     break;
1443   }
1444   case ISD::FSHL:
1445   case ISD::FSHR: {
1446     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
1447            Subtarget.hasStdExtZbt() && "Unexpected custom legalisation");
1448     SDValue NewOp0 =
1449         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
1450     SDValue NewOp1 =
1451         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
1452     SDValue NewOp2 =
1453         DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(2));
1454     // FSLW/FSRW take a 6 bit shift amount but i32 FSHL/FSHR only use 5 bits.
1455     // Mask the shift amount to 5 bits.
1456     NewOp2 = DAG.getNode(ISD::AND, DL, MVT::i64, NewOp2,
1457                          DAG.getConstant(0x1f, DL, MVT::i64));
1458     unsigned Opc =
1459         N->getOpcode() == ISD::FSHL ? RISCVISD::FSLW : RISCVISD::FSRW;
1460     SDValue NewOp = DAG.getNode(Opc, DL, MVT::i64, NewOp0, NewOp1, NewOp2);
1461     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewOp));
1462     break;
1463   }
1464   case ISD::INTRINSIC_WO_CHAIN: {
1465     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
1466     switch (IntNo) {
1467     default:
1468       llvm_unreachable(
1469           "Don't know how to custom type legalize this intrinsic!");
1470     case Intrinsic::riscv_vmv_x_s: {
1471       EVT VT = N->getValueType(0);
1472       assert((VT == MVT::i8 || VT == MVT::i16 ||
1473               (Subtarget.is64Bit() && VT == MVT::i32)) &&
1474              "Unexpected custom legalisation!");
1475       SDValue Extract = DAG.getNode(RISCVISD::VMV_X_S, DL,
1476                                     Subtarget.getXLenVT(), N->getOperand(1));
1477       Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, VT, Extract));
1478       break;
1479     }
1480     }
1481     break;
1482   }
1483   }
1484 }
1485 
1486 // A structure to hold one of the bit-manipulation patterns below. Together, a
1487 // SHL and non-SHL pattern may form a bit-manipulation pair on a single source:
1488 //   (or (and (shl x, 1), 0xAAAAAAAA),
1489 //       (and (srl x, 1), 0x55555555))
1490 struct RISCVBitmanipPat {
1491   SDValue Op;
1492   unsigned ShAmt;
1493   bool IsSHL;
1494 
1495   bool formsPairWith(const RISCVBitmanipPat &Other) const {
1496     return Op == Other.Op && ShAmt == Other.ShAmt && IsSHL != Other.IsSHL;
1497   }
1498 };
1499 
1500 // Matches any of the following bit-manipulation patterns:
1501 //   (and (shl x, 1), (0x55555555 << 1))
1502 //   (and (srl x, 1), 0x55555555)
1503 //   (shl (and x, 0x55555555), 1)
1504 //   (srl (and x, (0x55555555 << 1)), 1)
1505 // where the shift amount and mask may vary thus:
1506 //   [1]  = 0x55555555 / 0xAAAAAAAA
1507 //   [2]  = 0x33333333 / 0xCCCCCCCC
1508 //   [4]  = 0x0F0F0F0F / 0xF0F0F0F0
1509 //   [8]  = 0x00FF00FF / 0xFF00FF00
1510 //   [16] = 0x0000FFFF / 0xFFFFFFFF
1511 //   [32] = 0x00000000FFFFFFFF / 0xFFFFFFFF00000000 (for RV64)
1512 static Optional<RISCVBitmanipPat> matchRISCVBitmanipPat(SDValue Op) {
1513   Optional<uint64_t> Mask;
1514   // Optionally consume a mask around the shift operation.
1515   if (Op.getOpcode() == ISD::AND && isa<ConstantSDNode>(Op.getOperand(1))) {
1516     Mask = Op.getConstantOperandVal(1);
1517     Op = Op.getOperand(0);
1518   }
1519   if (Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRL)
1520     return None;
1521   bool IsSHL = Op.getOpcode() == ISD::SHL;
1522 
1523   if (!isa<ConstantSDNode>(Op.getOperand(1)))
1524     return None;
1525   auto ShAmt = Op.getConstantOperandVal(1);
1526 
1527   if (!isPowerOf2_64(ShAmt))
1528     return None;
1529 
1530   // These are the unshifted masks which we use to match bit-manipulation
1531   // patterns. They may be shifted left in certain circumstances.
1532   static const uint64_t BitmanipMasks[] = {
1533       0x5555555555555555ULL, 0x3333333333333333ULL, 0x0F0F0F0F0F0F0F0FULL,
1534       0x00FF00FF00FF00FFULL, 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL,
1535   };
1536 
1537   unsigned MaskIdx = Log2_64(ShAmt);
1538   if (MaskIdx >= array_lengthof(BitmanipMasks))
1539     return None;
1540 
1541   auto Src = Op.getOperand(0);
1542 
1543   unsigned Width = Op.getValueType() == MVT::i64 ? 64 : 32;
1544   auto ExpMask = BitmanipMasks[MaskIdx] & maskTrailingOnes<uint64_t>(Width);
1545 
1546   // The expected mask is shifted left when the AND is found around SHL
1547   // patterns.
1548   //   ((x >> 1) & 0x55555555)
1549   //   ((x << 1) & 0xAAAAAAAA)
1550   bool SHLExpMask = IsSHL;
1551 
1552   if (!Mask) {
1553     // Sometimes LLVM keeps the mask as an operand of the shift, typically when
1554     // the mask is all ones: consume that now.
1555     if (Src.getOpcode() == ISD::AND && isa<ConstantSDNode>(Src.getOperand(1))) {
1556       Mask = Src.getConstantOperandVal(1);
1557       Src = Src.getOperand(0);
1558       // The expected mask is now in fact shifted left for SRL, so reverse the
1559       // decision.
1560       //   ((x & 0xAAAAAAAA) >> 1)
1561       //   ((x & 0x55555555) << 1)
1562       SHLExpMask = !SHLExpMask;
1563     } else {
1564       // Use a default shifted mask of all-ones if there's no AND, truncated
1565       // down to the expected width. This simplifies the logic later on.
1566       Mask = maskTrailingOnes<uint64_t>(Width);
1567       *Mask &= (IsSHL ? *Mask << ShAmt : *Mask >> ShAmt);
1568     }
1569   }
1570 
1571   if (SHLExpMask)
1572     ExpMask <<= ShAmt;
1573 
1574   if (Mask != ExpMask)
1575     return None;
1576 
1577   return RISCVBitmanipPat{Src, (unsigned)ShAmt, IsSHL};
1578 }
1579 
1580 // Match the following pattern as a GREVI(W) operation
1581 //   (or (BITMANIP_SHL x), (BITMANIP_SRL x))
1582 static SDValue combineORToGREV(SDValue Op, SelectionDAG &DAG,
1583                                const RISCVSubtarget &Subtarget) {
1584   EVT VT = Op.getValueType();
1585 
1586   if (VT == Subtarget.getXLenVT() || (Subtarget.is64Bit() && VT == MVT::i32)) {
1587     auto LHS = matchRISCVBitmanipPat(Op.getOperand(0));
1588     auto RHS = matchRISCVBitmanipPat(Op.getOperand(1));
1589     if (LHS && RHS && LHS->formsPairWith(*RHS)) {
1590       SDLoc DL(Op);
1591       return DAG.getNode(
1592           RISCVISD::GREVI, DL, VT, LHS->Op,
1593           DAG.getTargetConstant(LHS->ShAmt, DL, Subtarget.getXLenVT()));
1594     }
1595   }
1596   return SDValue();
1597 }
1598 
1599 // Matches any the following pattern as a GORCI(W) operation
1600 // 1.  (or (GREVI x, shamt), x) if shamt is a power of 2
1601 // 2.  (or x, (GREVI x, shamt)) if shamt is a power of 2
1602 // 3.  (or (or (BITMANIP_SHL x), x), (BITMANIP_SRL x))
1603 // Note that with the variant of 3.,
1604 //     (or (or (BITMANIP_SHL x), (BITMANIP_SRL x)), x)
1605 // the inner pattern will first be matched as GREVI and then the outer
1606 // pattern will be matched to GORC via the first rule above.
1607 // 4.  (or (rotl/rotr x, bitwidth/2), x)
1608 static SDValue combineORToGORC(SDValue Op, SelectionDAG &DAG,
1609                                const RISCVSubtarget &Subtarget) {
1610   EVT VT = Op.getValueType();
1611 
1612   if (VT == Subtarget.getXLenVT() || (Subtarget.is64Bit() && VT == MVT::i32)) {
1613     SDLoc DL(Op);
1614     SDValue Op0 = Op.getOperand(0);
1615     SDValue Op1 = Op.getOperand(1);
1616 
1617     auto MatchOROfReverse = [&](SDValue Reverse, SDValue X) {
1618       if (Reverse.getOpcode() == RISCVISD::GREVI && Reverse.getOperand(0) == X &&
1619           isPowerOf2_32(Reverse.getConstantOperandVal(1)))
1620         return DAG.getNode(RISCVISD::GORCI, DL, VT, X, Reverse.getOperand(1));
1621       // We can also form GORCI from ROTL/ROTR by half the bitwidth.
1622       if ((Reverse.getOpcode() == ISD::ROTL ||
1623            Reverse.getOpcode() == ISD::ROTR) &&
1624           Reverse.getOperand(0) == X &&
1625           isa<ConstantSDNode>(Reverse.getOperand(1))) {
1626         uint64_t RotAmt = Reverse.getConstantOperandVal(1);
1627         if (RotAmt == (VT.getSizeInBits() / 2))
1628           return DAG.getNode(
1629               RISCVISD::GORCI, DL, VT, X,
1630               DAG.getTargetConstant(RotAmt, DL, Subtarget.getXLenVT()));
1631       }
1632       return SDValue();
1633     };
1634 
1635     // Check for either commutable permutation of (or (GREVI x, shamt), x)
1636     if (SDValue V = MatchOROfReverse(Op0, Op1))
1637       return V;
1638     if (SDValue V = MatchOROfReverse(Op1, Op0))
1639       return V;
1640 
1641     // OR is commutable so canonicalize its OR operand to the left
1642     if (Op0.getOpcode() != ISD::OR && Op1.getOpcode() == ISD::OR)
1643       std::swap(Op0, Op1);
1644     if (Op0.getOpcode() != ISD::OR)
1645       return SDValue();
1646     SDValue OrOp0 = Op0.getOperand(0);
1647     SDValue OrOp1 = Op0.getOperand(1);
1648     auto LHS = matchRISCVBitmanipPat(OrOp0);
1649     // OR is commutable so swap the operands and try again: x might have been
1650     // on the left
1651     if (!LHS) {
1652       std::swap(OrOp0, OrOp1);
1653       LHS = matchRISCVBitmanipPat(OrOp0);
1654     }
1655     auto RHS = matchRISCVBitmanipPat(Op1);
1656     if (LHS && RHS && LHS->formsPairWith(*RHS) && LHS->Op == OrOp1) {
1657       return DAG.getNode(
1658           RISCVISD::GORCI, DL, VT, LHS->Op,
1659           DAG.getTargetConstant(LHS->ShAmt, DL, Subtarget.getXLenVT()));
1660     }
1661   }
1662   return SDValue();
1663 }
1664 
1665 // Combine (GREVI (GREVI x, C2), C1) -> (GREVI x, C1^C2) when C1^C2 is
1666 // non-zero, and to x when it is. Any repeated GREVI stage undoes itself.
1667 // Combine (GORCI (GORCI x, C2), C1) -> (GORCI x, C1|C2). Repeated stage does
1668 // not undo itself, but they are redundant.
1669 static SDValue combineGREVI_GORCI(SDNode *N, SelectionDAG &DAG) {
1670   unsigned ShAmt1 = N->getConstantOperandVal(1);
1671   SDValue Src = N->getOperand(0);
1672 
1673   if (Src.getOpcode() != N->getOpcode())
1674     return SDValue();
1675 
1676   unsigned ShAmt2 = Src.getConstantOperandVal(1);
1677   Src = Src.getOperand(0);
1678 
1679   unsigned CombinedShAmt;
1680   if (N->getOpcode() == RISCVISD::GORCI || N->getOpcode() == RISCVISD::GORCIW)
1681     CombinedShAmt = ShAmt1 | ShAmt2;
1682   else
1683     CombinedShAmt = ShAmt1 ^ ShAmt2;
1684 
1685   if (CombinedShAmt == 0)
1686     return Src;
1687 
1688   SDLoc DL(N);
1689   return DAG.getNode(N->getOpcode(), DL, N->getValueType(0), Src,
1690                      DAG.getTargetConstant(CombinedShAmt, DL,
1691                                            N->getOperand(1).getValueType()));
1692 }
1693 
1694 SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
1695                                                DAGCombinerInfo &DCI) const {
1696   SelectionDAG &DAG = DCI.DAG;
1697 
1698   switch (N->getOpcode()) {
1699   default:
1700     break;
1701   case RISCVISD::SplitF64: {
1702     SDValue Op0 = N->getOperand(0);
1703     // If the input to SplitF64 is just BuildPairF64 then the operation is
1704     // redundant. Instead, use BuildPairF64's operands directly.
1705     if (Op0->getOpcode() == RISCVISD::BuildPairF64)
1706       return DCI.CombineTo(N, Op0.getOperand(0), Op0.getOperand(1));
1707 
1708     SDLoc DL(N);
1709 
1710     // It's cheaper to materialise two 32-bit integers than to load a double
1711     // from the constant pool and transfer it to integer registers through the
1712     // stack.
1713     if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op0)) {
1714       APInt V = C->getValueAPF().bitcastToAPInt();
1715       SDValue Lo = DAG.getConstant(V.trunc(32), DL, MVT::i32);
1716       SDValue Hi = DAG.getConstant(V.lshr(32).trunc(32), DL, MVT::i32);
1717       return DCI.CombineTo(N, Lo, Hi);
1718     }
1719 
1720     // This is a target-specific version of a DAGCombine performed in
1721     // DAGCombiner::visitBITCAST. It performs the equivalent of:
1722     // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
1723     // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
1724     if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
1725         !Op0.getNode()->hasOneUse())
1726       break;
1727     SDValue NewSplitF64 =
1728         DAG.getNode(RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32),
1729                     Op0.getOperand(0));
1730     SDValue Lo = NewSplitF64.getValue(0);
1731     SDValue Hi = NewSplitF64.getValue(1);
1732     APInt SignBit = APInt::getSignMask(32);
1733     if (Op0.getOpcode() == ISD::FNEG) {
1734       SDValue NewHi = DAG.getNode(ISD::XOR, DL, MVT::i32, Hi,
1735                                   DAG.getConstant(SignBit, DL, MVT::i32));
1736       return DCI.CombineTo(N, Lo, NewHi);
1737     }
1738     assert(Op0.getOpcode() == ISD::FABS);
1739     SDValue NewHi = DAG.getNode(ISD::AND, DL, MVT::i32, Hi,
1740                                 DAG.getConstant(~SignBit, DL, MVT::i32));
1741     return DCI.CombineTo(N, Lo, NewHi);
1742   }
1743   case RISCVISD::SLLW:
1744   case RISCVISD::SRAW:
1745   case RISCVISD::SRLW:
1746   case RISCVISD::ROLW:
1747   case RISCVISD::RORW: {
1748     // Only the lower 32 bits of LHS and lower 5 bits of RHS are read.
1749     SDValue LHS = N->getOperand(0);
1750     SDValue RHS = N->getOperand(1);
1751     APInt LHSMask = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 32);
1752     APInt RHSMask = APInt::getLowBitsSet(RHS.getValueSizeInBits(), 5);
1753     if (SimplifyDemandedBits(N->getOperand(0), LHSMask, DCI) ||
1754         SimplifyDemandedBits(N->getOperand(1), RHSMask, DCI)) {
1755       if (N->getOpcode() != ISD::DELETED_NODE)
1756         DCI.AddToWorklist(N);
1757       return SDValue(N, 0);
1758     }
1759     break;
1760   }
1761   case RISCVISD::FSLW:
1762   case RISCVISD::FSRW: {
1763     // Only the lower 32 bits of Values and lower 6 bits of shift amount are
1764     // read.
1765     SDValue Op0 = N->getOperand(0);
1766     SDValue Op1 = N->getOperand(1);
1767     SDValue ShAmt = N->getOperand(2);
1768     APInt OpMask = APInt::getLowBitsSet(Op0.getValueSizeInBits(), 32);
1769     APInt ShAmtMask = APInt::getLowBitsSet(ShAmt.getValueSizeInBits(), 6);
1770     if (SimplifyDemandedBits(Op0, OpMask, DCI) ||
1771         SimplifyDemandedBits(Op1, OpMask, DCI) ||
1772         SimplifyDemandedBits(ShAmt, ShAmtMask, DCI)) {
1773       if (N->getOpcode() != ISD::DELETED_NODE)
1774         DCI.AddToWorklist(N);
1775       return SDValue(N, 0);
1776     }
1777     break;
1778   }
1779   case RISCVISD::GREVIW:
1780   case RISCVISD::GORCIW: {
1781     // Only the lower 32 bits of the first operand are read
1782     SDValue Op0 = N->getOperand(0);
1783     APInt Mask = APInt::getLowBitsSet(Op0.getValueSizeInBits(), 32);
1784     if (SimplifyDemandedBits(Op0, Mask, DCI)) {
1785       if (N->getOpcode() != ISD::DELETED_NODE)
1786         DCI.AddToWorklist(N);
1787       return SDValue(N, 0);
1788     }
1789 
1790     return combineGREVI_GORCI(N, DCI.DAG);
1791   }
1792   case RISCVISD::FMV_X_ANYEXTW_RV64: {
1793     SDLoc DL(N);
1794     SDValue Op0 = N->getOperand(0);
1795     // If the input to FMV_X_ANYEXTW_RV64 is just FMV_W_X_RV64 then the
1796     // conversion is unnecessary and can be replaced with an ANY_EXTEND
1797     // of the FMV_W_X_RV64 operand.
1798     if (Op0->getOpcode() == RISCVISD::FMV_W_X_RV64) {
1799       assert(Op0.getOperand(0).getValueType() == MVT::i64 &&
1800              "Unexpected value type!");
1801       return Op0.getOperand(0);
1802     }
1803 
1804     // This is a target-specific version of a DAGCombine performed in
1805     // DAGCombiner::visitBITCAST. It performs the equivalent of:
1806     // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
1807     // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
1808     if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
1809         !Op0.getNode()->hasOneUse())
1810       break;
1811     SDValue NewFMV = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64,
1812                                  Op0.getOperand(0));
1813     APInt SignBit = APInt::getSignMask(32).sext(64);
1814     if (Op0.getOpcode() == ISD::FNEG)
1815       return DAG.getNode(ISD::XOR, DL, MVT::i64, NewFMV,
1816                          DAG.getConstant(SignBit, DL, MVT::i64));
1817 
1818     assert(Op0.getOpcode() == ISD::FABS);
1819     return DAG.getNode(ISD::AND, DL, MVT::i64, NewFMV,
1820                        DAG.getConstant(~SignBit, DL, MVT::i64));
1821   }
1822   case RISCVISD::GREVI:
1823   case RISCVISD::GORCI:
1824     return combineGREVI_GORCI(N, DCI.DAG);
1825   case ISD::OR:
1826     if (auto GREV = combineORToGREV(SDValue(N, 0), DCI.DAG, Subtarget))
1827       return GREV;
1828     if (auto GORC = combineORToGORC(SDValue(N, 0), DCI.DAG, Subtarget))
1829       return GORC;
1830     break;
1831   case RISCVISD::SELECT_CC: {
1832     // Transform
1833     // (select_cc (xor X, 1), 0, setne, trueV, falseV) ->
1834     // (select_cc X, 0, seteq, trueV, falseV) if we can prove X is 0/1.
1835     // This can occur when legalizing some floating point comparisons.
1836     SDValue LHS = N->getOperand(0);
1837     SDValue RHS = N->getOperand(1);
1838     auto CCVal = static_cast<ISD::CondCode>(N->getConstantOperandVal(2));
1839     APInt Mask = APInt::getBitsSetFrom(LHS.getValueSizeInBits(), 1);
1840     if ((CCVal == ISD::SETNE || CCVal == ISD::SETEQ) && isNullConstant(RHS) &&
1841         LHS.getOpcode() == ISD::XOR && isOneConstant(LHS.getOperand(1)) &&
1842         DAG.MaskedValueIsZero(LHS.getOperand(0), Mask)) {
1843       SDLoc DL(N);
1844       CCVal = ISD::getSetCCInverse(CCVal, LHS.getValueType());
1845       SDValue TargetCC = DAG.getConstant(CCVal, DL, Subtarget.getXLenVT());
1846       return DAG.getNode(RISCVISD::SELECT_CC, DL, N->getValueType(0),
1847                          {LHS.getOperand(0), RHS, TargetCC, N->getOperand(3),
1848                           N->getOperand(4)});
1849     }
1850     break;
1851   }
1852   }
1853 
1854   return SDValue();
1855 }
1856 
1857 bool RISCVTargetLowering::isDesirableToCommuteWithShift(
1858     const SDNode *N, CombineLevel Level) const {
1859   // The following folds are only desirable if `(OP _, c1 << c2)` can be
1860   // materialised in fewer instructions than `(OP _, c1)`:
1861   //
1862   //   (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
1863   //   (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
1864   SDValue N0 = N->getOperand(0);
1865   EVT Ty = N0.getValueType();
1866   if (Ty.isScalarInteger() &&
1867       (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::OR)) {
1868     auto *C1 = dyn_cast<ConstantSDNode>(N0->getOperand(1));
1869     auto *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1));
1870     if (C1 && C2) {
1871       APInt C1Int = C1->getAPIntValue();
1872       APInt ShiftedC1Int = C1Int << C2->getAPIntValue();
1873 
1874       // We can materialise `c1 << c2` into an add immediate, so it's "free",
1875       // and the combine should happen, to potentially allow further combines
1876       // later.
1877       if (ShiftedC1Int.getMinSignedBits() <= 64 &&
1878           isLegalAddImmediate(ShiftedC1Int.getSExtValue()))
1879         return true;
1880 
1881       // We can materialise `c1` in an add immediate, so it's "free", and the
1882       // combine should be prevented.
1883       if (C1Int.getMinSignedBits() <= 64 &&
1884           isLegalAddImmediate(C1Int.getSExtValue()))
1885         return false;
1886 
1887       // Neither constant will fit into an immediate, so find materialisation
1888       // costs.
1889       int C1Cost = RISCVMatInt::getIntMatCost(C1Int, Ty.getSizeInBits(),
1890                                               Subtarget.is64Bit());
1891       int ShiftedC1Cost = RISCVMatInt::getIntMatCost(
1892           ShiftedC1Int, Ty.getSizeInBits(), Subtarget.is64Bit());
1893 
1894       // Materialising `c1` is cheaper than materialising `c1 << c2`, so the
1895       // combine should be prevented.
1896       if (C1Cost < ShiftedC1Cost)
1897         return false;
1898     }
1899   }
1900   return true;
1901 }
1902 
1903 bool RISCVTargetLowering::targetShrinkDemandedConstant(
1904     SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,
1905     TargetLoweringOpt &TLO) const {
1906   // Delay this optimization as late as possible.
1907   if (!TLO.LegalOps)
1908     return false;
1909 
1910   EVT VT = Op.getValueType();
1911   if (VT.isVector())
1912     return false;
1913 
1914   // Only handle AND for now.
1915   if (Op.getOpcode() != ISD::AND)
1916     return false;
1917 
1918   ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
1919   if (!C)
1920     return false;
1921 
1922   const APInt &Mask = C->getAPIntValue();
1923 
1924   // Clear all non-demanded bits initially.
1925   APInt ShrunkMask = Mask & DemandedBits;
1926 
1927   // If the shrunk mask fits in sign extended 12 bits, let the target
1928   // independent code apply it.
1929   if (ShrunkMask.isSignedIntN(12))
1930     return false;
1931 
1932   // Try to make a smaller immediate by setting undemanded bits.
1933 
1934   // We need to be able to make a negative number through a combination of mask
1935   // and undemanded bits.
1936   APInt ExpandedMask = Mask | ~DemandedBits;
1937   if (!ExpandedMask.isNegative())
1938     return false;
1939 
1940   // What is the fewest number of bits we need to represent the negative number.
1941   unsigned MinSignedBits = ExpandedMask.getMinSignedBits();
1942 
1943   // Try to make a 12 bit negative immediate. If that fails try to make a 32
1944   // bit negative immediate unless the shrunk immediate already fits in 32 bits.
1945   APInt NewMask = ShrunkMask;
1946   if (MinSignedBits <= 12)
1947     NewMask.setBitsFrom(11);
1948   else if (MinSignedBits <= 32 && !ShrunkMask.isSignedIntN(32))
1949     NewMask.setBitsFrom(31);
1950   else
1951     return false;
1952 
1953   // Sanity check that our new mask is a subset of the demanded mask.
1954   assert(NewMask.isSubsetOf(ExpandedMask));
1955 
1956   // If we aren't changing the mask, just return true to keep it and prevent
1957   // the caller from optimizing.
1958   if (NewMask == Mask)
1959     return true;
1960 
1961   // Replace the constant with the new mask.
1962   SDLoc DL(Op);
1963   SDValue NewC = TLO.DAG.getConstant(NewMask, DL, VT);
1964   SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC);
1965   return TLO.CombineTo(Op, NewOp);
1966 }
1967 
1968 void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
1969                                                         KnownBits &Known,
1970                                                         const APInt &DemandedElts,
1971                                                         const SelectionDAG &DAG,
1972                                                         unsigned Depth) const {
1973   unsigned Opc = Op.getOpcode();
1974   assert((Opc >= ISD::BUILTIN_OP_END ||
1975           Opc == ISD::INTRINSIC_WO_CHAIN ||
1976           Opc == ISD::INTRINSIC_W_CHAIN ||
1977           Opc == ISD::INTRINSIC_VOID) &&
1978          "Should use MaskedValueIsZero if you don't know whether Op"
1979          " is a target node!");
1980 
1981   Known.resetAll();
1982   switch (Opc) {
1983   default: break;
1984   case RISCVISD::READ_VLENB:
1985     // We assume VLENB is at least 8 bytes.
1986     // FIXME: The 1.0 draft spec defines minimum VLEN as 128 bits.
1987     Known.Zero.setLowBits(3);
1988     break;
1989   }
1990 }
1991 
1992 unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
1993     SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
1994     unsigned Depth) const {
1995   switch (Op.getOpcode()) {
1996   default:
1997     break;
1998   case RISCVISD::SLLW:
1999   case RISCVISD::SRAW:
2000   case RISCVISD::SRLW:
2001   case RISCVISD::DIVW:
2002   case RISCVISD::DIVUW:
2003   case RISCVISD::REMUW:
2004   case RISCVISD::ROLW:
2005   case RISCVISD::RORW:
2006   case RISCVISD::GREVIW:
2007   case RISCVISD::GORCIW:
2008   case RISCVISD::FSLW:
2009   case RISCVISD::FSRW:
2010     // TODO: As the result is sign-extended, this is conservatively correct. A
2011     // more precise answer could be calculated for SRAW depending on known
2012     // bits in the shift amount.
2013     return 33;
2014   case RISCVISD::VMV_X_S:
2015     // The number of sign bits of the scalar result is computed by obtaining the
2016     // element type of the input vector operand, substracting its width from the
2017     // XLEN, and then adding one (sign bit within the element type).
2018     return Subtarget.getXLen() - Op.getOperand(0).getScalarValueSizeInBits() + 1;
2019   }
2020 
2021   return 1;
2022 }
2023 
2024 static MachineBasicBlock *emitReadCycleWidePseudo(MachineInstr &MI,
2025                                                   MachineBasicBlock *BB) {
2026   assert(MI.getOpcode() == RISCV::ReadCycleWide && "Unexpected instruction");
2027 
2028   // To read the 64-bit cycle CSR on a 32-bit target, we read the two halves.
2029   // Should the count have wrapped while it was being read, we need to try
2030   // again.
2031   // ...
2032   // read:
2033   // rdcycleh x3 # load high word of cycle
2034   // rdcycle  x2 # load low word of cycle
2035   // rdcycleh x4 # load high word of cycle
2036   // bne x3, x4, read # check if high word reads match, otherwise try again
2037   // ...
2038 
2039   MachineFunction &MF = *BB->getParent();
2040   const BasicBlock *LLVM_BB = BB->getBasicBlock();
2041   MachineFunction::iterator It = ++BB->getIterator();
2042 
2043   MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB);
2044   MF.insert(It, LoopMBB);
2045 
2046   MachineBasicBlock *DoneMBB = MF.CreateMachineBasicBlock(LLVM_BB);
2047   MF.insert(It, DoneMBB);
2048 
2049   // Transfer the remainder of BB and its successor edges to DoneMBB.
2050   DoneMBB->splice(DoneMBB->begin(), BB,
2051                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
2052   DoneMBB->transferSuccessorsAndUpdatePHIs(BB);
2053 
2054   BB->addSuccessor(LoopMBB);
2055 
2056   MachineRegisterInfo &RegInfo = MF.getRegInfo();
2057   Register ReadAgainReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
2058   Register LoReg = MI.getOperand(0).getReg();
2059   Register HiReg = MI.getOperand(1).getReg();
2060   DebugLoc DL = MI.getDebugLoc();
2061 
2062   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
2063   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), HiReg)
2064       .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
2065       .addReg(RISCV::X0);
2066   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), LoReg)
2067       .addImm(RISCVSysReg::lookupSysRegByName("CYCLE")->Encoding)
2068       .addReg(RISCV::X0);
2069   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), ReadAgainReg)
2070       .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
2071       .addReg(RISCV::X0);
2072 
2073   BuildMI(LoopMBB, DL, TII->get(RISCV::BNE))
2074       .addReg(HiReg)
2075       .addReg(ReadAgainReg)
2076       .addMBB(LoopMBB);
2077 
2078   LoopMBB->addSuccessor(LoopMBB);
2079   LoopMBB->addSuccessor(DoneMBB);
2080 
2081   MI.eraseFromParent();
2082 
2083   return DoneMBB;
2084 }
2085 
2086 static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
2087                                              MachineBasicBlock *BB) {
2088   assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
2089 
2090   MachineFunction &MF = *BB->getParent();
2091   DebugLoc DL = MI.getDebugLoc();
2092   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
2093   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
2094   Register LoReg = MI.getOperand(0).getReg();
2095   Register HiReg = MI.getOperand(1).getReg();
2096   Register SrcReg = MI.getOperand(2).getReg();
2097   const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
2098   int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex(MF);
2099 
2100   TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
2101                           RI);
2102   MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, FI);
2103   MachineMemOperand *MMOLo =
2104       MF.getMachineMemOperand(MPI, MachineMemOperand::MOLoad, 4, Align(8));
2105   MachineMemOperand *MMOHi = MF.getMachineMemOperand(
2106       MPI.getWithOffset(4), MachineMemOperand::MOLoad, 4, Align(8));
2107   BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
2108       .addFrameIndex(FI)
2109       .addImm(0)
2110       .addMemOperand(MMOLo);
2111   BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
2112       .addFrameIndex(FI)
2113       .addImm(4)
2114       .addMemOperand(MMOHi);
2115   MI.eraseFromParent(); // The pseudo instruction is gone now.
2116   return BB;
2117 }
2118 
2119 static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
2120                                                  MachineBasicBlock *BB) {
2121   assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
2122          "Unexpected instruction");
2123 
2124   MachineFunction &MF = *BB->getParent();
2125   DebugLoc DL = MI.getDebugLoc();
2126   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
2127   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
2128   Register DstReg = MI.getOperand(0).getReg();
2129   Register LoReg = MI.getOperand(1).getReg();
2130   Register HiReg = MI.getOperand(2).getReg();
2131   const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
2132   int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex(MF);
2133 
2134   MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, FI);
2135   MachineMemOperand *MMOLo =
2136       MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, Align(8));
2137   MachineMemOperand *MMOHi = MF.getMachineMemOperand(
2138       MPI.getWithOffset(4), MachineMemOperand::MOStore, 4, Align(8));
2139   BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
2140       .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
2141       .addFrameIndex(FI)
2142       .addImm(0)
2143       .addMemOperand(MMOLo);
2144   BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
2145       .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
2146       .addFrameIndex(FI)
2147       .addImm(4)
2148       .addMemOperand(MMOHi);
2149   TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
2150   MI.eraseFromParent(); // The pseudo instruction is gone now.
2151   return BB;
2152 }
2153 
2154 static bool isSelectPseudo(MachineInstr &MI) {
2155   switch (MI.getOpcode()) {
2156   default:
2157     return false;
2158   case RISCV::Select_GPR_Using_CC_GPR:
2159   case RISCV::Select_FPR16_Using_CC_GPR:
2160   case RISCV::Select_FPR32_Using_CC_GPR:
2161   case RISCV::Select_FPR64_Using_CC_GPR:
2162     return true;
2163   }
2164 }
2165 
2166 static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
2167                                            MachineBasicBlock *BB) {
2168   // To "insert" Select_* instructions, we actually have to insert the triangle
2169   // control-flow pattern.  The incoming instructions know the destination vreg
2170   // to set, the condition code register to branch on, the true/false values to
2171   // select between, and the condcode to use to select the appropriate branch.
2172   //
2173   // We produce the following control flow:
2174   //     HeadMBB
2175   //     |  \
2176   //     |  IfFalseMBB
2177   //     | /
2178   //    TailMBB
2179   //
2180   // When we find a sequence of selects we attempt to optimize their emission
2181   // by sharing the control flow. Currently we only handle cases where we have
2182   // multiple selects with the exact same condition (same LHS, RHS and CC).
2183   // The selects may be interleaved with other instructions if the other
2184   // instructions meet some requirements we deem safe:
2185   // - They are debug instructions. Otherwise,
2186   // - They do not have side-effects, do not access memory and their inputs do
2187   //   not depend on the results of the select pseudo-instructions.
2188   // The TrueV/FalseV operands of the selects cannot depend on the result of
2189   // previous selects in the sequence.
2190   // These conditions could be further relaxed. See the X86 target for a
2191   // related approach and more information.
2192   Register LHS = MI.getOperand(1).getReg();
2193   Register RHS = MI.getOperand(2).getReg();
2194   auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
2195 
2196   SmallVector<MachineInstr *, 4> SelectDebugValues;
2197   SmallSet<Register, 4> SelectDests;
2198   SelectDests.insert(MI.getOperand(0).getReg());
2199 
2200   MachineInstr *LastSelectPseudo = &MI;
2201 
2202   for (auto E = BB->end(), SequenceMBBI = MachineBasicBlock::iterator(MI);
2203        SequenceMBBI != E; ++SequenceMBBI) {
2204     if (SequenceMBBI->isDebugInstr())
2205       continue;
2206     else if (isSelectPseudo(*SequenceMBBI)) {
2207       if (SequenceMBBI->getOperand(1).getReg() != LHS ||
2208           SequenceMBBI->getOperand(2).getReg() != RHS ||
2209           SequenceMBBI->getOperand(3).getImm() != CC ||
2210           SelectDests.count(SequenceMBBI->getOperand(4).getReg()) ||
2211           SelectDests.count(SequenceMBBI->getOperand(5).getReg()))
2212         break;
2213       LastSelectPseudo = &*SequenceMBBI;
2214       SequenceMBBI->collectDebugValues(SelectDebugValues);
2215       SelectDests.insert(SequenceMBBI->getOperand(0).getReg());
2216     } else {
2217       if (SequenceMBBI->hasUnmodeledSideEffects() ||
2218           SequenceMBBI->mayLoadOrStore())
2219         break;
2220       if (llvm::any_of(SequenceMBBI->operands(), [&](MachineOperand &MO) {
2221             return MO.isReg() && MO.isUse() && SelectDests.count(MO.getReg());
2222           }))
2223         break;
2224     }
2225   }
2226 
2227   const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
2228   const BasicBlock *LLVM_BB = BB->getBasicBlock();
2229   DebugLoc DL = MI.getDebugLoc();
2230   MachineFunction::iterator I = ++BB->getIterator();
2231 
2232   MachineBasicBlock *HeadMBB = BB;
2233   MachineFunction *F = BB->getParent();
2234   MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
2235   MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
2236 
2237   F->insert(I, IfFalseMBB);
2238   F->insert(I, TailMBB);
2239 
2240   // Transfer debug instructions associated with the selects to TailMBB.
2241   for (MachineInstr *DebugInstr : SelectDebugValues) {
2242     TailMBB->push_back(DebugInstr->removeFromParent());
2243   }
2244 
2245   // Move all instructions after the sequence to TailMBB.
2246   TailMBB->splice(TailMBB->end(), HeadMBB,
2247                   std::next(LastSelectPseudo->getIterator()), HeadMBB->end());
2248   // Update machine-CFG edges by transferring all successors of the current
2249   // block to the new block which will contain the Phi nodes for the selects.
2250   TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
2251   // Set the successors for HeadMBB.
2252   HeadMBB->addSuccessor(IfFalseMBB);
2253   HeadMBB->addSuccessor(TailMBB);
2254 
2255   // Insert appropriate branch.
2256   unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
2257 
2258   BuildMI(HeadMBB, DL, TII.get(Opcode))
2259     .addReg(LHS)
2260     .addReg(RHS)
2261     .addMBB(TailMBB);
2262 
2263   // IfFalseMBB just falls through to TailMBB.
2264   IfFalseMBB->addSuccessor(TailMBB);
2265 
2266   // Create PHIs for all of the select pseudo-instructions.
2267   auto SelectMBBI = MI.getIterator();
2268   auto SelectEnd = std::next(LastSelectPseudo->getIterator());
2269   auto InsertionPoint = TailMBB->begin();
2270   while (SelectMBBI != SelectEnd) {
2271     auto Next = std::next(SelectMBBI);
2272     if (isSelectPseudo(*SelectMBBI)) {
2273       // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
2274       BuildMI(*TailMBB, InsertionPoint, SelectMBBI->getDebugLoc(),
2275               TII.get(RISCV::PHI), SelectMBBI->getOperand(0).getReg())
2276           .addReg(SelectMBBI->getOperand(4).getReg())
2277           .addMBB(HeadMBB)
2278           .addReg(SelectMBBI->getOperand(5).getReg())
2279           .addMBB(IfFalseMBB);
2280       SelectMBBI->eraseFromParent();
2281     }
2282     SelectMBBI = Next;
2283   }
2284 
2285   F->getProperties().reset(MachineFunctionProperties::Property::NoPHIs);
2286   return TailMBB;
2287 }
2288 
2289 static MachineBasicBlock *addVSetVL(MachineInstr &MI, MachineBasicBlock *BB,
2290                                     int VLIndex, unsigned SEWIndex,
2291                                     RISCVVLMUL VLMul, bool WritesElement0) {
2292   MachineFunction &MF = *BB->getParent();
2293   DebugLoc DL = MI.getDebugLoc();
2294   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
2295 
2296   unsigned SEW = MI.getOperand(SEWIndex).getImm();
2297   assert(RISCVVType::isValidSEW(SEW) && "Unexpected SEW");
2298   RISCVVSEW ElementWidth = static_cast<RISCVVSEW>(Log2_32(SEW / 8));
2299 
2300   MachineRegisterInfo &MRI = MF.getRegInfo();
2301 
2302   // VL and VTYPE are alive here.
2303   MachineInstrBuilder MIB = BuildMI(*BB, MI, DL, TII.get(RISCV::PseudoVSETVLI));
2304 
2305   if (VLIndex >= 0) {
2306     // Set VL (rs1 != X0).
2307     Register DestReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
2308     MIB.addReg(DestReg, RegState::Define | RegState::Dead)
2309         .addReg(MI.getOperand(VLIndex).getReg());
2310   } else
2311     // With no VL operator in the pseudo, do not modify VL (rd = X0, rs1 = X0).
2312     MIB.addReg(RISCV::X0, RegState::Define | RegState::Dead)
2313         .addReg(RISCV::X0, RegState::Kill);
2314 
2315   // Default to tail agnostic unless the destination is tied to a source. In
2316   // that case the user would have some control over the tail values. The tail
2317   // policy is also ignored on instructions that only update element 0 like
2318   // vmv.s.x or reductions so use agnostic there to match the common case.
2319   // FIXME: This is conservatively correct, but we might want to detect that
2320   // the input is undefined.
2321   bool TailAgnostic = true;
2322   if (MI.isRegTiedToUseOperand(0) && !WritesElement0)
2323     TailAgnostic = false;
2324 
2325   // For simplicity we reuse the vtype representation here.
2326   MIB.addImm(RISCVVType::encodeVTYPE(VLMul, ElementWidth,
2327                                      /*TailAgnostic*/ TailAgnostic,
2328                                      /*MaskAgnostic*/ false));
2329 
2330   // Remove (now) redundant operands from pseudo
2331   MI.getOperand(SEWIndex).setImm(-1);
2332   if (VLIndex >= 0) {
2333     MI.getOperand(VLIndex).setReg(RISCV::NoRegister);
2334     MI.getOperand(VLIndex).setIsKill(false);
2335   }
2336 
2337   return BB;
2338 }
2339 
2340 MachineBasicBlock *
2341 RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
2342                                                  MachineBasicBlock *BB) const {
2343   uint64_t TSFlags = MI.getDesc().TSFlags;
2344 
2345   if (TSFlags & RISCVII::HasSEWOpMask) {
2346     unsigned NumOperands = MI.getNumExplicitOperands();
2347     int VLIndex = (TSFlags & RISCVII::HasVLOpMask) ? NumOperands - 2 : -1;
2348     unsigned SEWIndex = NumOperands - 1;
2349     bool WritesElement0 = TSFlags & RISCVII::WritesElement0Mask;
2350 
2351     RISCVVLMUL VLMul = static_cast<RISCVVLMUL>((TSFlags & RISCVII::VLMulMask) >>
2352                                                RISCVII::VLMulShift);
2353     return addVSetVL(MI, BB, VLIndex, SEWIndex, VLMul, WritesElement0);
2354   }
2355 
2356   switch (MI.getOpcode()) {
2357   default:
2358     llvm_unreachable("Unexpected instr type to insert");
2359   case RISCV::ReadCycleWide:
2360     assert(!Subtarget.is64Bit() &&
2361            "ReadCycleWrite is only to be used on riscv32");
2362     return emitReadCycleWidePseudo(MI, BB);
2363   case RISCV::Select_GPR_Using_CC_GPR:
2364   case RISCV::Select_FPR16_Using_CC_GPR:
2365   case RISCV::Select_FPR32_Using_CC_GPR:
2366   case RISCV::Select_FPR64_Using_CC_GPR:
2367     return emitSelectPseudo(MI, BB);
2368   case RISCV::BuildPairF64Pseudo:
2369     return emitBuildPairF64Pseudo(MI, BB);
2370   case RISCV::SplitF64Pseudo:
2371     return emitSplitF64Pseudo(MI, BB);
2372   }
2373 }
2374 
2375 // Calling Convention Implementation.
2376 // The expectations for frontend ABI lowering vary from target to target.
2377 // Ideally, an LLVM frontend would be able to avoid worrying about many ABI
2378 // details, but this is a longer term goal. For now, we simply try to keep the
2379 // role of the frontend as simple and well-defined as possible. The rules can
2380 // be summarised as:
2381 // * Never split up large scalar arguments. We handle them here.
2382 // * If a hardfloat calling convention is being used, and the struct may be
2383 // passed in a pair of registers (fp+fp, int+fp), and both registers are
2384 // available, then pass as two separate arguments. If either the GPRs or FPRs
2385 // are exhausted, then pass according to the rule below.
2386 // * If a struct could never be passed in registers or directly in a stack
2387 // slot (as it is larger than 2*XLEN and the floating point rules don't
2388 // apply), then pass it using a pointer with the byval attribute.
2389 // * If a struct is less than 2*XLEN, then coerce to either a two-element
2390 // word-sized array or a 2*XLEN scalar (depending on alignment).
2391 // * The frontend can determine whether a struct is returned by reference or
2392 // not based on its size and fields. If it will be returned by reference, the
2393 // frontend must modify the prototype so a pointer with the sret annotation is
2394 // passed as the first argument. This is not necessary for large scalar
2395 // returns.
2396 // * Struct return values and varargs should be coerced to structs containing
2397 // register-size fields in the same situations they would be for fixed
2398 // arguments.
2399 
2400 static const MCPhysReg ArgGPRs[] = {
2401   RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
2402   RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
2403 };
2404 static const MCPhysReg ArgFPR16s[] = {
2405   RISCV::F10_H, RISCV::F11_H, RISCV::F12_H, RISCV::F13_H,
2406   RISCV::F14_H, RISCV::F15_H, RISCV::F16_H, RISCV::F17_H
2407 };
2408 static const MCPhysReg ArgFPR32s[] = {
2409   RISCV::F10_F, RISCV::F11_F, RISCV::F12_F, RISCV::F13_F,
2410   RISCV::F14_F, RISCV::F15_F, RISCV::F16_F, RISCV::F17_F
2411 };
2412 static const MCPhysReg ArgFPR64s[] = {
2413   RISCV::F10_D, RISCV::F11_D, RISCV::F12_D, RISCV::F13_D,
2414   RISCV::F14_D, RISCV::F15_D, RISCV::F16_D, RISCV::F17_D
2415 };
2416 // This is an interim calling convention and it may be changed in the future.
2417 static const MCPhysReg ArgVRs[] = {
2418   RISCV::V16, RISCV::V17, RISCV::V18, RISCV::V19, RISCV::V20,
2419   RISCV::V21, RISCV::V22, RISCV::V23
2420 };
2421 static const MCPhysReg ArgVRM2s[] = {
2422   RISCV::V16M2, RISCV::V18M2, RISCV::V20M2, RISCV::V22M2
2423 };
2424 static const MCPhysReg ArgVRM4s[] = {RISCV::V16M4, RISCV::V20M4};
2425 static const MCPhysReg ArgVRM8s[] = {RISCV::V16M8};
2426 
2427 // Pass a 2*XLEN argument that has been split into two XLEN values through
2428 // registers or the stack as necessary.
2429 static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
2430                                 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
2431                                 MVT ValVT2, MVT LocVT2,
2432                                 ISD::ArgFlagsTy ArgFlags2) {
2433   unsigned XLenInBytes = XLen / 8;
2434   if (Register Reg = State.AllocateReg(ArgGPRs)) {
2435     // At least one half can be passed via register.
2436     State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
2437                                      VA1.getLocVT(), CCValAssign::Full));
2438   } else {
2439     // Both halves must be passed on the stack, with proper alignment.
2440     Align StackAlign =
2441         std::max(Align(XLenInBytes), ArgFlags1.getNonZeroOrigAlign());
2442     State.addLoc(
2443         CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
2444                             State.AllocateStack(XLenInBytes, StackAlign),
2445                             VA1.getLocVT(), CCValAssign::Full));
2446     State.addLoc(CCValAssign::getMem(
2447         ValNo2, ValVT2, State.AllocateStack(XLenInBytes, Align(XLenInBytes)),
2448         LocVT2, CCValAssign::Full));
2449     return false;
2450   }
2451 
2452   if (Register Reg = State.AllocateReg(ArgGPRs)) {
2453     // The second half can also be passed via register.
2454     State.addLoc(
2455         CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
2456   } else {
2457     // The second half is passed via the stack, without additional alignment.
2458     State.addLoc(CCValAssign::getMem(
2459         ValNo2, ValVT2, State.AllocateStack(XLenInBytes, Align(XLenInBytes)),
2460         LocVT2, CCValAssign::Full));
2461   }
2462 
2463   return false;
2464 }
2465 
2466 // Implements the RISC-V calling convention. Returns true upon failure.
2467 static bool CC_RISCV(const DataLayout &DL, RISCVABI::ABI ABI, unsigned ValNo,
2468                      MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo,
2469                      ISD::ArgFlagsTy ArgFlags, CCState &State, bool IsFixed,
2470                      bool IsRet, Type *OrigTy, const RISCVTargetLowering &TLI,
2471                      Optional<unsigned> FirstMaskArgument) {
2472   unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
2473   assert(XLen == 32 || XLen == 64);
2474   MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
2475 
2476   // Any return value split in to more than two values can't be returned
2477   // directly.
2478   if (IsRet && ValNo > 1)
2479     return true;
2480 
2481   // UseGPRForF16_F32 if targeting one of the soft-float ABIs, if passing a
2482   // variadic argument, or if no F16/F32 argument registers are available.
2483   bool UseGPRForF16_F32 = true;
2484   // UseGPRForF64 if targeting soft-float ABIs or an FLEN=32 ABI, if passing a
2485   // variadic argument, or if no F64 argument registers are available.
2486   bool UseGPRForF64 = true;
2487 
2488   switch (ABI) {
2489   default:
2490     llvm_unreachable("Unexpected ABI");
2491   case RISCVABI::ABI_ILP32:
2492   case RISCVABI::ABI_LP64:
2493     break;
2494   case RISCVABI::ABI_ILP32F:
2495   case RISCVABI::ABI_LP64F:
2496     UseGPRForF16_F32 = !IsFixed;
2497     break;
2498   case RISCVABI::ABI_ILP32D:
2499   case RISCVABI::ABI_LP64D:
2500     UseGPRForF16_F32 = !IsFixed;
2501     UseGPRForF64 = !IsFixed;
2502     break;
2503   }
2504 
2505   // FPR16, FPR32, and FPR64 alias each other.
2506   if (State.getFirstUnallocated(ArgFPR32s) == array_lengthof(ArgFPR32s)) {
2507     UseGPRForF16_F32 = true;
2508     UseGPRForF64 = true;
2509   }
2510 
2511   // From this point on, rely on UseGPRForF16_F32, UseGPRForF64 and
2512   // similar local variables rather than directly checking against the target
2513   // ABI.
2514 
2515   if (UseGPRForF16_F32 && (ValVT == MVT::f16 || ValVT == MVT::f32)) {
2516     LocVT = XLenVT;
2517     LocInfo = CCValAssign::BCvt;
2518   } else if (UseGPRForF64 && XLen == 64 && ValVT == MVT::f64) {
2519     LocVT = MVT::i64;
2520     LocInfo = CCValAssign::BCvt;
2521   }
2522 
2523   // If this is a variadic argument, the RISC-V calling convention requires
2524   // that it is assigned an 'even' or 'aligned' register if it has 8-byte
2525   // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
2526   // be used regardless of whether the original argument was split during
2527   // legalisation or not. The argument will not be passed by registers if the
2528   // original type is larger than 2*XLEN, so the register alignment rule does
2529   // not apply.
2530   unsigned TwoXLenInBytes = (2 * XLen) / 8;
2531   if (!IsFixed && ArgFlags.getNonZeroOrigAlign() == TwoXLenInBytes &&
2532       DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
2533     unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
2534     // Skip 'odd' register if necessary.
2535     if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
2536       State.AllocateReg(ArgGPRs);
2537   }
2538 
2539   SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
2540   SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
2541       State.getPendingArgFlags();
2542 
2543   assert(PendingLocs.size() == PendingArgFlags.size() &&
2544          "PendingLocs and PendingArgFlags out of sync");
2545 
2546   // Handle passing f64 on RV32D with a soft float ABI or when floating point
2547   // registers are exhausted.
2548   if (UseGPRForF64 && XLen == 32 && ValVT == MVT::f64) {
2549     assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
2550            "Can't lower f64 if it is split");
2551     // Depending on available argument GPRS, f64 may be passed in a pair of
2552     // GPRs, split between a GPR and the stack, or passed completely on the
2553     // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
2554     // cases.
2555     Register Reg = State.AllocateReg(ArgGPRs);
2556     LocVT = MVT::i32;
2557     if (!Reg) {
2558       unsigned StackOffset = State.AllocateStack(8, Align(8));
2559       State.addLoc(
2560           CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
2561       return false;
2562     }
2563     if (!State.AllocateReg(ArgGPRs))
2564       State.AllocateStack(4, Align(4));
2565     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2566     return false;
2567   }
2568 
2569   // Split arguments might be passed indirectly, so keep track of the pending
2570   // values.
2571   if (ArgFlags.isSplit() || !PendingLocs.empty()) {
2572     LocVT = XLenVT;
2573     LocInfo = CCValAssign::Indirect;
2574     PendingLocs.push_back(
2575         CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
2576     PendingArgFlags.push_back(ArgFlags);
2577     if (!ArgFlags.isSplitEnd()) {
2578       return false;
2579     }
2580   }
2581 
2582   // If the split argument only had two elements, it should be passed directly
2583   // in registers or on the stack.
2584   if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
2585     assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
2586     // Apply the normal calling convention rules to the first half of the
2587     // split argument.
2588     CCValAssign VA = PendingLocs[0];
2589     ISD::ArgFlagsTy AF = PendingArgFlags[0];
2590     PendingLocs.clear();
2591     PendingArgFlags.clear();
2592     return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
2593                                ArgFlags);
2594   }
2595 
2596   // Allocate to a register if possible, or else a stack slot.
2597   Register Reg;
2598   if (ValVT == MVT::f16 && !UseGPRForF16_F32)
2599     Reg = State.AllocateReg(ArgFPR16s);
2600   else if (ValVT == MVT::f32 && !UseGPRForF16_F32)
2601     Reg = State.AllocateReg(ArgFPR32s);
2602   else if (ValVT == MVT::f64 && !UseGPRForF64)
2603     Reg = State.AllocateReg(ArgFPR64s);
2604   else if (ValVT.isScalableVector()) {
2605     const TargetRegisterClass *RC = TLI.getRegClassFor(ValVT);
2606     if (RC == &RISCV::VRRegClass) {
2607       // Assign the first mask argument to V0.
2608       // This is an interim calling convention and it may be changed in the
2609       // future.
2610       if (FirstMaskArgument.hasValue() &&
2611           ValNo == FirstMaskArgument.getValue()) {
2612         Reg = State.AllocateReg(RISCV::V0);
2613       } else {
2614         Reg = State.AllocateReg(ArgVRs);
2615       }
2616     } else if (RC == &RISCV::VRM2RegClass) {
2617       Reg = State.AllocateReg(ArgVRM2s);
2618     } else if (RC == &RISCV::VRM4RegClass) {
2619       Reg = State.AllocateReg(ArgVRM4s);
2620     } else if (RC == &RISCV::VRM8RegClass) {
2621       Reg = State.AllocateReg(ArgVRM8s);
2622     } else {
2623       llvm_unreachable("Unhandled class register for ValueType");
2624     }
2625     if (!Reg) {
2626       LocInfo = CCValAssign::Indirect;
2627       // Try using a GPR to pass the address
2628       Reg = State.AllocateReg(ArgGPRs);
2629       LocVT = XLenVT;
2630     }
2631   } else
2632     Reg = State.AllocateReg(ArgGPRs);
2633   unsigned StackOffset =
2634       Reg ? 0 : State.AllocateStack(XLen / 8, Align(XLen / 8));
2635 
2636   // If we reach this point and PendingLocs is non-empty, we must be at the
2637   // end of a split argument that must be passed indirectly.
2638   if (!PendingLocs.empty()) {
2639     assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
2640     assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
2641 
2642     for (auto &It : PendingLocs) {
2643       if (Reg)
2644         It.convertToReg(Reg);
2645       else
2646         It.convertToMem(StackOffset);
2647       State.addLoc(It);
2648     }
2649     PendingLocs.clear();
2650     PendingArgFlags.clear();
2651     return false;
2652   }
2653 
2654   assert((!UseGPRForF16_F32 || !UseGPRForF64 || LocVT == XLenVT ||
2655           (TLI.getSubtarget().hasStdExtV() && ValVT.isScalableVector())) &&
2656          "Expected an XLenVT or scalable vector types at this stage");
2657 
2658   if (Reg) {
2659     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2660     return false;
2661   }
2662 
2663   // When a floating-point value is passed on the stack, no bit-conversion is
2664   // needed.
2665   if (ValVT.isFloatingPoint()) {
2666     LocVT = ValVT;
2667     LocInfo = CCValAssign::Full;
2668   }
2669   State.addLoc(CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
2670   return false;
2671 }
2672 
2673 template <typename ArgTy>
2674 static Optional<unsigned> preAssignMask(const ArgTy &Args) {
2675   for (const auto &ArgIdx : enumerate(Args)) {
2676     MVT ArgVT = ArgIdx.value().VT;
2677     if (ArgVT.isScalableVector() &&
2678         ArgVT.getVectorElementType().SimpleTy == MVT::i1)
2679       return ArgIdx.index();
2680   }
2681   return None;
2682 }
2683 
2684 void RISCVTargetLowering::analyzeInputArgs(
2685     MachineFunction &MF, CCState &CCInfo,
2686     const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
2687   unsigned NumArgs = Ins.size();
2688   FunctionType *FType = MF.getFunction().getFunctionType();
2689 
2690   Optional<unsigned> FirstMaskArgument;
2691   if (Subtarget.hasStdExtV())
2692     FirstMaskArgument = preAssignMask(Ins);
2693 
2694   for (unsigned i = 0; i != NumArgs; ++i) {
2695     MVT ArgVT = Ins[i].VT;
2696     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
2697 
2698     Type *ArgTy = nullptr;
2699     if (IsRet)
2700       ArgTy = FType->getReturnType();
2701     else if (Ins[i].isOrigArg())
2702       ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
2703 
2704     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
2705     if (CC_RISCV(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
2706                  ArgFlags, CCInfo, /*IsFixed=*/true, IsRet, ArgTy, *this,
2707                  FirstMaskArgument)) {
2708       LLVM_DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
2709                         << EVT(ArgVT).getEVTString() << '\n');
2710       llvm_unreachable(nullptr);
2711     }
2712   }
2713 }
2714 
2715 void RISCVTargetLowering::analyzeOutputArgs(
2716     MachineFunction &MF, CCState &CCInfo,
2717     const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
2718     CallLoweringInfo *CLI) const {
2719   unsigned NumArgs = Outs.size();
2720 
2721   Optional<unsigned> FirstMaskArgument;
2722   if (Subtarget.hasStdExtV())
2723     FirstMaskArgument = preAssignMask(Outs);
2724 
2725   for (unsigned i = 0; i != NumArgs; i++) {
2726     MVT ArgVT = Outs[i].VT;
2727     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
2728     Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
2729 
2730     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
2731     if (CC_RISCV(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
2732                  ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy, *this,
2733                  FirstMaskArgument)) {
2734       LLVM_DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
2735                         << EVT(ArgVT).getEVTString() << "\n");
2736       llvm_unreachable(nullptr);
2737     }
2738   }
2739 }
2740 
2741 // Convert Val to a ValVT. Should not be called for CCValAssign::Indirect
2742 // values.
2743 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
2744                                    const CCValAssign &VA, const SDLoc &DL) {
2745   switch (VA.getLocInfo()) {
2746   default:
2747     llvm_unreachable("Unexpected CCValAssign::LocInfo");
2748   case CCValAssign::Full:
2749     break;
2750   case CCValAssign::BCvt:
2751     if (VA.getLocVT().isInteger() && VA.getValVT() == MVT::f16)
2752       Val = DAG.getNode(RISCVISD::FMV_H_X, DL, MVT::f16, Val);
2753     else if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32)
2754       Val = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, Val);
2755     else
2756       Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2757     break;
2758   }
2759   return Val;
2760 }
2761 
2762 // The caller is responsible for loading the full value if the argument is
2763 // passed with CCValAssign::Indirect.
2764 static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
2765                                 const CCValAssign &VA, const SDLoc &DL,
2766                                 const RISCVTargetLowering &TLI) {
2767   MachineFunction &MF = DAG.getMachineFunction();
2768   MachineRegisterInfo &RegInfo = MF.getRegInfo();
2769   EVT LocVT = VA.getLocVT();
2770   SDValue Val;
2771   const TargetRegisterClass *RC = TLI.getRegClassFor(LocVT.getSimpleVT());
2772   Register VReg = RegInfo.createVirtualRegister(RC);
2773   RegInfo.addLiveIn(VA.getLocReg(), VReg);
2774   Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
2775 
2776   if (VA.getLocInfo() == CCValAssign::Indirect)
2777     return Val;
2778 
2779   return convertLocVTToValVT(DAG, Val, VA, DL);
2780 }
2781 
2782 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val,
2783                                    const CCValAssign &VA, const SDLoc &DL) {
2784   EVT LocVT = VA.getLocVT();
2785 
2786   switch (VA.getLocInfo()) {
2787   default:
2788     llvm_unreachable("Unexpected CCValAssign::LocInfo");
2789   case CCValAssign::Full:
2790     break;
2791   case CCValAssign::BCvt:
2792     if (VA.getLocVT().isInteger() && VA.getValVT() == MVT::f16)
2793       Val = DAG.getNode(RISCVISD::FMV_X_ANYEXTH, DL, VA.getLocVT(), Val);
2794     else if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32)
2795       Val = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Val);
2796     else
2797       Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
2798     break;
2799   }
2800   return Val;
2801 }
2802 
2803 // The caller is responsible for loading the full value if the argument is
2804 // passed with CCValAssign::Indirect.
2805 static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
2806                                 const CCValAssign &VA, const SDLoc &DL) {
2807   MachineFunction &MF = DAG.getMachineFunction();
2808   MachineFrameInfo &MFI = MF.getFrameInfo();
2809   EVT LocVT = VA.getLocVT();
2810   EVT ValVT = VA.getValVT();
2811   EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
2812   int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
2813                                  VA.getLocMemOffset(), /*Immutable=*/true);
2814   SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2815   SDValue Val;
2816 
2817   ISD::LoadExtType ExtType;
2818   switch (VA.getLocInfo()) {
2819   default:
2820     llvm_unreachable("Unexpected CCValAssign::LocInfo");
2821   case CCValAssign::Full:
2822   case CCValAssign::Indirect:
2823   case CCValAssign::BCvt:
2824     ExtType = ISD::NON_EXTLOAD;
2825     break;
2826   }
2827   Val = DAG.getExtLoad(
2828       ExtType, DL, LocVT, Chain, FIN,
2829       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
2830   return Val;
2831 }
2832 
2833 static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
2834                                        const CCValAssign &VA, const SDLoc &DL) {
2835   assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
2836          "Unexpected VA");
2837   MachineFunction &MF = DAG.getMachineFunction();
2838   MachineFrameInfo &MFI = MF.getFrameInfo();
2839   MachineRegisterInfo &RegInfo = MF.getRegInfo();
2840 
2841   if (VA.isMemLoc()) {
2842     // f64 is passed on the stack.
2843     int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
2844     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
2845     return DAG.getLoad(MVT::f64, DL, Chain, FIN,
2846                        MachinePointerInfo::getFixedStack(MF, FI));
2847   }
2848 
2849   assert(VA.isRegLoc() && "Expected register VA assignment");
2850 
2851   Register LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
2852   RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
2853   SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
2854   SDValue Hi;
2855   if (VA.getLocReg() == RISCV::X17) {
2856     // Second half of f64 is passed on the stack.
2857     int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
2858     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
2859     Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
2860                      MachinePointerInfo::getFixedStack(MF, FI));
2861   } else {
2862     // Second half of f64 is passed in another GPR.
2863     Register HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
2864     RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
2865     Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
2866   }
2867   return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
2868 }
2869 
2870 // FastCC has less than 1% performance improvement for some particular
2871 // benchmark. But theoretically, it may has benenfit for some cases.
2872 static bool CC_RISCV_FastCC(unsigned ValNo, MVT ValVT, MVT LocVT,
2873                             CCValAssign::LocInfo LocInfo,
2874                             ISD::ArgFlagsTy ArgFlags, CCState &State) {
2875 
2876   if (LocVT == MVT::i32 || LocVT == MVT::i64) {
2877     // X5 and X6 might be used for save-restore libcall.
2878     static const MCPhysReg GPRList[] = {
2879         RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13, RISCV::X14,
2880         RISCV::X15, RISCV::X16, RISCV::X17, RISCV::X7,  RISCV::X28,
2881         RISCV::X29, RISCV::X30, RISCV::X31};
2882     if (unsigned Reg = State.AllocateReg(GPRList)) {
2883       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2884       return false;
2885     }
2886   }
2887 
2888   if (LocVT == MVT::f16) {
2889     static const MCPhysReg FPR16List[] = {
2890         RISCV::F10_H, RISCV::F11_H, RISCV::F12_H, RISCV::F13_H, RISCV::F14_H,
2891         RISCV::F15_H, RISCV::F16_H, RISCV::F17_H, RISCV::F0_H,  RISCV::F1_H,
2892         RISCV::F2_H,  RISCV::F3_H,  RISCV::F4_H,  RISCV::F5_H,  RISCV::F6_H,
2893         RISCV::F7_H,  RISCV::F28_H, RISCV::F29_H, RISCV::F30_H, RISCV::F31_H};
2894     if (unsigned Reg = State.AllocateReg(FPR16List)) {
2895       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2896       return false;
2897     }
2898   }
2899 
2900   if (LocVT == MVT::f32) {
2901     static const MCPhysReg FPR32List[] = {
2902         RISCV::F10_F, RISCV::F11_F, RISCV::F12_F, RISCV::F13_F, RISCV::F14_F,
2903         RISCV::F15_F, RISCV::F16_F, RISCV::F17_F, RISCV::F0_F,  RISCV::F1_F,
2904         RISCV::F2_F,  RISCV::F3_F,  RISCV::F4_F,  RISCV::F5_F,  RISCV::F6_F,
2905         RISCV::F7_F,  RISCV::F28_F, RISCV::F29_F, RISCV::F30_F, RISCV::F31_F};
2906     if (unsigned Reg = State.AllocateReg(FPR32List)) {
2907       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2908       return false;
2909     }
2910   }
2911 
2912   if (LocVT == MVT::f64) {
2913     static const MCPhysReg FPR64List[] = {
2914         RISCV::F10_D, RISCV::F11_D, RISCV::F12_D, RISCV::F13_D, RISCV::F14_D,
2915         RISCV::F15_D, RISCV::F16_D, RISCV::F17_D, RISCV::F0_D,  RISCV::F1_D,
2916         RISCV::F2_D,  RISCV::F3_D,  RISCV::F4_D,  RISCV::F5_D,  RISCV::F6_D,
2917         RISCV::F7_D,  RISCV::F28_D, RISCV::F29_D, RISCV::F30_D, RISCV::F31_D};
2918     if (unsigned Reg = State.AllocateReg(FPR64List)) {
2919       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2920       return false;
2921     }
2922   }
2923 
2924   if (LocVT == MVT::i32 || LocVT == MVT::f32) {
2925     unsigned Offset4 = State.AllocateStack(4, Align(4));
2926     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset4, LocVT, LocInfo));
2927     return false;
2928   }
2929 
2930   if (LocVT == MVT::i64 || LocVT == MVT::f64) {
2931     unsigned Offset5 = State.AllocateStack(8, Align(8));
2932     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset5, LocVT, LocInfo));
2933     return false;
2934   }
2935 
2936   return true; // CC didn't match.
2937 }
2938 
2939 static bool CC_RISCV_GHC(unsigned ValNo, MVT ValVT, MVT LocVT,
2940                          CCValAssign::LocInfo LocInfo,
2941                          ISD::ArgFlagsTy ArgFlags, CCState &State) {
2942 
2943   if (LocVT == MVT::i32 || LocVT == MVT::i64) {
2944     // Pass in STG registers: Base, Sp, Hp, R1, R2, R3, R4, R5, R6, R7, SpLim
2945     //                        s1    s2  s3  s4  s5  s6  s7  s8  s9  s10 s11
2946     static const MCPhysReg GPRList[] = {
2947         RISCV::X9, RISCV::X18, RISCV::X19, RISCV::X20, RISCV::X21, RISCV::X22,
2948         RISCV::X23, RISCV::X24, RISCV::X25, RISCV::X26, RISCV::X27};
2949     if (unsigned Reg = State.AllocateReg(GPRList)) {
2950       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2951       return false;
2952     }
2953   }
2954 
2955   if (LocVT == MVT::f32) {
2956     // Pass in STG registers: F1, ..., F6
2957     //                        fs0 ... fs5
2958     static const MCPhysReg FPR32List[] = {RISCV::F8_F, RISCV::F9_F,
2959                                           RISCV::F18_F, RISCV::F19_F,
2960                                           RISCV::F20_F, RISCV::F21_F};
2961     if (unsigned Reg = State.AllocateReg(FPR32List)) {
2962       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2963       return false;
2964     }
2965   }
2966 
2967   if (LocVT == MVT::f64) {
2968     // Pass in STG registers: D1, ..., D6
2969     //                        fs6 ... fs11
2970     static const MCPhysReg FPR64List[] = {RISCV::F22_D, RISCV::F23_D,
2971                                           RISCV::F24_D, RISCV::F25_D,
2972                                           RISCV::F26_D, RISCV::F27_D};
2973     if (unsigned Reg = State.AllocateReg(FPR64List)) {
2974       State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2975       return false;
2976     }
2977   }
2978 
2979   report_fatal_error("No registers left in GHC calling convention");
2980   return true;
2981 }
2982 
2983 // Transform physical registers into virtual registers.
2984 SDValue RISCVTargetLowering::LowerFormalArguments(
2985     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
2986     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2987     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
2988 
2989   MachineFunction &MF = DAG.getMachineFunction();
2990 
2991   switch (CallConv) {
2992   default:
2993     report_fatal_error("Unsupported calling convention");
2994   case CallingConv::C:
2995   case CallingConv::Fast:
2996     break;
2997   case CallingConv::GHC:
2998     if (!MF.getSubtarget().getFeatureBits()[RISCV::FeatureStdExtF] ||
2999         !MF.getSubtarget().getFeatureBits()[RISCV::FeatureStdExtD])
3000       report_fatal_error(
3001         "GHC calling convention requires the F and D instruction set extensions");
3002   }
3003 
3004   const Function &Func = MF.getFunction();
3005   if (Func.hasFnAttribute("interrupt")) {
3006     if (!Func.arg_empty())
3007       report_fatal_error(
3008         "Functions with the interrupt attribute cannot have arguments!");
3009 
3010     StringRef Kind =
3011       MF.getFunction().getFnAttribute("interrupt").getValueAsString();
3012 
3013     if (!(Kind == "user" || Kind == "supervisor" || Kind == "machine"))
3014       report_fatal_error(
3015         "Function interrupt attribute argument not supported!");
3016   }
3017 
3018   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3019   MVT XLenVT = Subtarget.getXLenVT();
3020   unsigned XLenInBytes = Subtarget.getXLen() / 8;
3021   // Used with vargs to acumulate store chains.
3022   std::vector<SDValue> OutChains;
3023 
3024   // Assign locations to all of the incoming arguments.
3025   SmallVector<CCValAssign, 16> ArgLocs;
3026   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
3027 
3028   if (CallConv == CallingConv::Fast)
3029     CCInfo.AnalyzeFormalArguments(Ins, CC_RISCV_FastCC);
3030   else if (CallConv == CallingConv::GHC)
3031     CCInfo.AnalyzeFormalArguments(Ins, CC_RISCV_GHC);
3032   else
3033     analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
3034 
3035   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3036     CCValAssign &VA = ArgLocs[i];
3037     SDValue ArgValue;
3038     // Passing f64 on RV32D with a soft float ABI must be handled as a special
3039     // case.
3040     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
3041       ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
3042     else if (VA.isRegLoc())
3043       ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL, *this);
3044     else
3045       ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
3046 
3047     if (VA.getLocInfo() == CCValAssign::Indirect) {
3048       // If the original argument was split and passed by reference (e.g. i128
3049       // on RV32), we need to load all parts of it here (using the same
3050       // address).
3051       InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
3052                                    MachinePointerInfo()));
3053       unsigned ArgIndex = Ins[i].OrigArgIndex;
3054       assert(Ins[i].PartOffset == 0);
3055       while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
3056         CCValAssign &PartVA = ArgLocs[i + 1];
3057         unsigned PartOffset = Ins[i + 1].PartOffset;
3058         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
3059                                       DAG.getIntPtrConstant(PartOffset, DL));
3060         InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
3061                                      MachinePointerInfo()));
3062         ++i;
3063       }
3064       continue;
3065     }
3066     InVals.push_back(ArgValue);
3067   }
3068 
3069   if (IsVarArg) {
3070     ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
3071     unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
3072     const TargetRegisterClass *RC = &RISCV::GPRRegClass;
3073     MachineFrameInfo &MFI = MF.getFrameInfo();
3074     MachineRegisterInfo &RegInfo = MF.getRegInfo();
3075     RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
3076 
3077     // Offset of the first variable argument from stack pointer, and size of
3078     // the vararg save area. For now, the varargs save area is either zero or
3079     // large enough to hold a0-a7.
3080     int VaArgOffset, VarArgsSaveSize;
3081 
3082     // If all registers are allocated, then all varargs must be passed on the
3083     // stack and we don't need to save any argregs.
3084     if (ArgRegs.size() == Idx) {
3085       VaArgOffset = CCInfo.getNextStackOffset();
3086       VarArgsSaveSize = 0;
3087     } else {
3088       VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
3089       VaArgOffset = -VarArgsSaveSize;
3090     }
3091 
3092     // Record the frame index of the first variable argument
3093     // which is a value necessary to VASTART.
3094     int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
3095     RVFI->setVarArgsFrameIndex(FI);
3096 
3097     // If saving an odd number of registers then create an extra stack slot to
3098     // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
3099     // offsets to even-numbered registered remain 2*XLEN-aligned.
3100     if (Idx % 2) {
3101       MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes, true);
3102       VarArgsSaveSize += XLenInBytes;
3103     }
3104 
3105     // Copy the integer registers that may have been used for passing varargs
3106     // to the vararg save area.
3107     for (unsigned I = Idx; I < ArgRegs.size();
3108          ++I, VaArgOffset += XLenInBytes) {
3109       const Register Reg = RegInfo.createVirtualRegister(RC);
3110       RegInfo.addLiveIn(ArgRegs[I], Reg);
3111       SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
3112       FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
3113       SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3114       SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3115                                    MachinePointerInfo::getFixedStack(MF, FI));
3116       cast<StoreSDNode>(Store.getNode())
3117           ->getMemOperand()
3118           ->setValue((Value *)nullptr);
3119       OutChains.push_back(Store);
3120     }
3121     RVFI->setVarArgsSaveSize(VarArgsSaveSize);
3122   }
3123 
3124   // All stores are grouped in one node to allow the matching between
3125   // the size of Ins and InVals. This only happens for vararg functions.
3126   if (!OutChains.empty()) {
3127     OutChains.push_back(Chain);
3128     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
3129   }
3130 
3131   return Chain;
3132 }
3133 
3134 /// isEligibleForTailCallOptimization - Check whether the call is eligible
3135 /// for tail call optimization.
3136 /// Note: This is modelled after ARM's IsEligibleForTailCallOptimization.
3137 bool RISCVTargetLowering::isEligibleForTailCallOptimization(
3138     CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
3139     const SmallVector<CCValAssign, 16> &ArgLocs) const {
3140 
3141   auto &Callee = CLI.Callee;
3142   auto CalleeCC = CLI.CallConv;
3143   auto &Outs = CLI.Outs;
3144   auto &Caller = MF.getFunction();
3145   auto CallerCC = Caller.getCallingConv();
3146 
3147   // Exception-handling functions need a special set of instructions to
3148   // indicate a return to the hardware. Tail-calling another function would
3149   // probably break this.
3150   // TODO: The "interrupt" attribute isn't currently defined by RISC-V. This
3151   // should be expanded as new function attributes are introduced.
3152   if (Caller.hasFnAttribute("interrupt"))
3153     return false;
3154 
3155   // Do not tail call opt if the stack is used to pass parameters.
3156   if (CCInfo.getNextStackOffset() != 0)
3157     return false;
3158 
3159   // Do not tail call opt if any parameters need to be passed indirectly.
3160   // Since long doubles (fp128) and i128 are larger than 2*XLEN, they are
3161   // passed indirectly. So the address of the value will be passed in a
3162   // register, or if not available, then the address is put on the stack. In
3163   // order to pass indirectly, space on the stack often needs to be allocated
3164   // in order to store the value. In this case the CCInfo.getNextStackOffset()
3165   // != 0 check is not enough and we need to check if any CCValAssign ArgsLocs
3166   // are passed CCValAssign::Indirect.
3167   for (auto &VA : ArgLocs)
3168     if (VA.getLocInfo() == CCValAssign::Indirect)
3169       return false;
3170 
3171   // Do not tail call opt if either caller or callee uses struct return
3172   // semantics.
3173   auto IsCallerStructRet = Caller.hasStructRetAttr();
3174   auto IsCalleeStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
3175   if (IsCallerStructRet || IsCalleeStructRet)
3176     return false;
3177 
3178   // Externally-defined functions with weak linkage should not be
3179   // tail-called. The behaviour of branch instructions in this situation (as
3180   // used for tail calls) is implementation-defined, so we cannot rely on the
3181   // linker replacing the tail call with a return.
3182   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3183     const GlobalValue *GV = G->getGlobal();
3184     if (GV->hasExternalWeakLinkage())
3185       return false;
3186   }
3187 
3188   // The callee has to preserve all registers the caller needs to preserve.
3189   const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
3190   const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
3191   if (CalleeCC != CallerCC) {
3192     const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
3193     if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
3194       return false;
3195   }
3196 
3197   // Byval parameters hand the function a pointer directly into the stack area
3198   // we want to reuse during a tail call. Working around this *is* possible
3199   // but less efficient and uglier in LowerCall.
3200   for (auto &Arg : Outs)
3201     if (Arg.Flags.isByVal())
3202       return false;
3203 
3204   return true;
3205 }
3206 
3207 // Lower a call to a callseq_start + CALL + callseq_end chain, and add input
3208 // and output parameter nodes.
3209 SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
3210                                        SmallVectorImpl<SDValue> &InVals) const {
3211   SelectionDAG &DAG = CLI.DAG;
3212   SDLoc &DL = CLI.DL;
3213   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3214   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3215   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
3216   SDValue Chain = CLI.Chain;
3217   SDValue Callee = CLI.Callee;
3218   bool &IsTailCall = CLI.IsTailCall;
3219   CallingConv::ID CallConv = CLI.CallConv;
3220   bool IsVarArg = CLI.IsVarArg;
3221   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3222   MVT XLenVT = Subtarget.getXLenVT();
3223 
3224   MachineFunction &MF = DAG.getMachineFunction();
3225 
3226   // Analyze the operands of the call, assigning locations to each operand.
3227   SmallVector<CCValAssign, 16> ArgLocs;
3228   CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
3229 
3230   if (CallConv == CallingConv::Fast)
3231     ArgCCInfo.AnalyzeCallOperands(Outs, CC_RISCV_FastCC);
3232   else if (CallConv == CallingConv::GHC)
3233     ArgCCInfo.AnalyzeCallOperands(Outs, CC_RISCV_GHC);
3234   else
3235     analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
3236 
3237   // Check if it's really possible to do a tail call.
3238   if (IsTailCall)
3239     IsTailCall = isEligibleForTailCallOptimization(ArgCCInfo, CLI, MF, ArgLocs);
3240 
3241   if (IsTailCall)
3242     ++NumTailCalls;
3243   else if (CLI.CB && CLI.CB->isMustTailCall())
3244     report_fatal_error("failed to perform tail call elimination on a call "
3245                        "site marked musttail");
3246 
3247   // Get a count of how many bytes are to be pushed on the stack.
3248   unsigned NumBytes = ArgCCInfo.getNextStackOffset();
3249 
3250   // Create local copies for byval args
3251   SmallVector<SDValue, 8> ByValArgs;
3252   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
3253     ISD::ArgFlagsTy Flags = Outs[i].Flags;
3254     if (!Flags.isByVal())
3255       continue;
3256 
3257     SDValue Arg = OutVals[i];
3258     unsigned Size = Flags.getByValSize();
3259     Align Alignment = Flags.getNonZeroByValAlign();
3260 
3261     int FI =
3262         MF.getFrameInfo().CreateStackObject(Size, Alignment, /*isSS=*/false);
3263     SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3264     SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
3265 
3266     Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Alignment,
3267                           /*IsVolatile=*/false,
3268                           /*AlwaysInline=*/false, IsTailCall,
3269                           MachinePointerInfo(), MachinePointerInfo());
3270     ByValArgs.push_back(FIPtr);
3271   }
3272 
3273   if (!IsTailCall)
3274     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
3275 
3276   // Copy argument values to their designated locations.
3277   SmallVector<std::pair<Register, SDValue>, 8> RegsToPass;
3278   SmallVector<SDValue, 8> MemOpChains;
3279   SDValue StackPtr;
3280   for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
3281     CCValAssign &VA = ArgLocs[i];
3282     SDValue ArgValue = OutVals[i];
3283     ISD::ArgFlagsTy Flags = Outs[i].Flags;
3284 
3285     // Handle passing f64 on RV32D with a soft float ABI as a special case.
3286     bool IsF64OnRV32DSoftABI =
3287         VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
3288     if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
3289       SDValue SplitF64 = DAG.getNode(
3290           RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
3291       SDValue Lo = SplitF64.getValue(0);
3292       SDValue Hi = SplitF64.getValue(1);
3293 
3294       Register RegLo = VA.getLocReg();
3295       RegsToPass.push_back(std::make_pair(RegLo, Lo));
3296 
3297       if (RegLo == RISCV::X17) {
3298         // Second half of f64 is passed on the stack.
3299         // Work out the address of the stack slot.
3300         if (!StackPtr.getNode())
3301           StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
3302         // Emit the store.
3303         MemOpChains.push_back(
3304             DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
3305       } else {
3306         // Second half of f64 is passed in another GPR.
3307         assert(RegLo < RISCV::X31 && "Invalid register pair");
3308         Register RegHigh = RegLo + 1;
3309         RegsToPass.push_back(std::make_pair(RegHigh, Hi));
3310       }
3311       continue;
3312     }
3313 
3314     // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
3315     // as any other MemLoc.
3316 
3317     // Promote the value if needed.
3318     // For now, only handle fully promoted and indirect arguments.
3319     if (VA.getLocInfo() == CCValAssign::Indirect) {
3320       // Store the argument in a stack slot and pass its address.
3321       SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
3322       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
3323       MemOpChains.push_back(
3324           DAG.getStore(Chain, DL, ArgValue, SpillSlot,
3325                        MachinePointerInfo::getFixedStack(MF, FI)));
3326       // If the original argument was split (e.g. i128), we need
3327       // to store all parts of it here (and pass just one address).
3328       unsigned ArgIndex = Outs[i].OrigArgIndex;
3329       assert(Outs[i].PartOffset == 0);
3330       while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
3331         SDValue PartValue = OutVals[i + 1];
3332         unsigned PartOffset = Outs[i + 1].PartOffset;
3333         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
3334                                       DAG.getIntPtrConstant(PartOffset, DL));
3335         MemOpChains.push_back(
3336             DAG.getStore(Chain, DL, PartValue, Address,
3337                          MachinePointerInfo::getFixedStack(MF, FI)));
3338         ++i;
3339       }
3340       ArgValue = SpillSlot;
3341     } else {
3342       ArgValue = convertValVTToLocVT(DAG, ArgValue, VA, DL);
3343     }
3344 
3345     // Use local copy if it is a byval arg.
3346     if (Flags.isByVal())
3347       ArgValue = ByValArgs[j++];
3348 
3349     if (VA.isRegLoc()) {
3350       // Queue up the argument copies and emit them at the end.
3351       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
3352     } else {
3353       assert(VA.isMemLoc() && "Argument not register or memory");
3354       assert(!IsTailCall && "Tail call not allowed if stack is used "
3355                             "for passing parameters");
3356 
3357       // Work out the address of the stack slot.
3358       if (!StackPtr.getNode())
3359         StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
3360       SDValue Address =
3361           DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
3362                       DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3363 
3364       // Emit the store.
3365       MemOpChains.push_back(
3366           DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
3367     }
3368   }
3369 
3370   // Join the stores, which are independent of one another.
3371   if (!MemOpChains.empty())
3372     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3373 
3374   SDValue Glue;
3375 
3376   // Build a sequence of copy-to-reg nodes, chained and glued together.
3377   for (auto &Reg : RegsToPass) {
3378     Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
3379     Glue = Chain.getValue(1);
3380   }
3381 
3382   // Validate that none of the argument registers have been marked as
3383   // reserved, if so report an error. Do the same for the return address if this
3384   // is not a tailcall.
3385   validateCCReservedRegs(RegsToPass, MF);
3386   if (!IsTailCall &&
3387       MF.getSubtarget<RISCVSubtarget>().isRegisterReservedByUser(RISCV::X1))
3388     MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
3389         MF.getFunction(),
3390         "Return address register required, but has been reserved."});
3391 
3392   // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
3393   // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
3394   // split it and then direct call can be matched by PseudoCALL.
3395   if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
3396     const GlobalValue *GV = S->getGlobal();
3397 
3398     unsigned OpFlags = RISCVII::MO_CALL;
3399     if (!getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV))
3400       OpFlags = RISCVII::MO_PLT;
3401 
3402     Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, OpFlags);
3403   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3404     unsigned OpFlags = RISCVII::MO_CALL;
3405 
3406     if (!getTargetMachine().shouldAssumeDSOLocal(*MF.getFunction().getParent(),
3407                                                  nullptr))
3408       OpFlags = RISCVII::MO_PLT;
3409 
3410     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, OpFlags);
3411   }
3412 
3413   // The first call operand is the chain and the second is the target address.
3414   SmallVector<SDValue, 8> Ops;
3415   Ops.push_back(Chain);
3416   Ops.push_back(Callee);
3417 
3418   // Add argument registers to the end of the list so that they are
3419   // known live into the call.
3420   for (auto &Reg : RegsToPass)
3421     Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
3422 
3423   if (!IsTailCall) {
3424     // Add a register mask operand representing the call-preserved registers.
3425     const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
3426     const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
3427     assert(Mask && "Missing call preserved mask for calling convention");
3428     Ops.push_back(DAG.getRegisterMask(Mask));
3429   }
3430 
3431   // Glue the call to the argument copies, if any.
3432   if (Glue.getNode())
3433     Ops.push_back(Glue);
3434 
3435   // Emit the call.
3436   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3437 
3438   if (IsTailCall) {
3439     MF.getFrameInfo().setHasTailCall();
3440     return DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops);
3441   }
3442 
3443   Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
3444   DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge);
3445   Glue = Chain.getValue(1);
3446 
3447   // Mark the end of the call, which is glued to the call itself.
3448   Chain = DAG.getCALLSEQ_END(Chain,
3449                              DAG.getConstant(NumBytes, DL, PtrVT, true),
3450                              DAG.getConstant(0, DL, PtrVT, true),
3451                              Glue, DL);
3452   Glue = Chain.getValue(1);
3453 
3454   // Assign locations to each value returned by this call.
3455   SmallVector<CCValAssign, 16> RVLocs;
3456   CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
3457   analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
3458 
3459   // Copy all of the result registers out of their specified physreg.
3460   for (auto &VA : RVLocs) {
3461     // Copy the value out
3462     SDValue RetValue =
3463         DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
3464     // Glue the RetValue to the end of the call sequence
3465     Chain = RetValue.getValue(1);
3466     Glue = RetValue.getValue(2);
3467 
3468     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
3469       assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
3470       SDValue RetValue2 =
3471           DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
3472       Chain = RetValue2.getValue(1);
3473       Glue = RetValue2.getValue(2);
3474       RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
3475                              RetValue2);
3476     }
3477 
3478     RetValue = convertLocVTToValVT(DAG, RetValue, VA, DL);
3479 
3480     InVals.push_back(RetValue);
3481   }
3482 
3483   return Chain;
3484 }
3485 
3486 bool RISCVTargetLowering::CanLowerReturn(
3487     CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
3488     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
3489   SmallVector<CCValAssign, 16> RVLocs;
3490   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3491 
3492   Optional<unsigned> FirstMaskArgument;
3493   if (Subtarget.hasStdExtV())
3494     FirstMaskArgument = preAssignMask(Outs);
3495 
3496   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
3497     MVT VT = Outs[i].VT;
3498     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
3499     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
3500     if (CC_RISCV(MF.getDataLayout(), ABI, i, VT, VT, CCValAssign::Full,
3501                  ArgFlags, CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr,
3502                  *this, FirstMaskArgument))
3503       return false;
3504   }
3505   return true;
3506 }
3507 
3508 SDValue
3509 RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3510                                  bool IsVarArg,
3511                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
3512                                  const SmallVectorImpl<SDValue> &OutVals,
3513                                  const SDLoc &DL, SelectionDAG &DAG) const {
3514   const MachineFunction &MF = DAG.getMachineFunction();
3515   const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
3516 
3517   // Stores the assignment of the return value to a location.
3518   SmallVector<CCValAssign, 16> RVLocs;
3519 
3520   // Info about the registers and stack slot.
3521   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3522                  *DAG.getContext());
3523 
3524   analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
3525                     nullptr);
3526 
3527   if (CallConv == CallingConv::GHC && !RVLocs.empty())
3528     report_fatal_error("GHC functions return void only");
3529 
3530   SDValue Glue;
3531   SmallVector<SDValue, 4> RetOps(1, Chain);
3532 
3533   // Copy the result values into the output registers.
3534   for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
3535     SDValue Val = OutVals[i];
3536     CCValAssign &VA = RVLocs[i];
3537     assert(VA.isRegLoc() && "Can only return in registers!");
3538 
3539     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
3540       // Handle returning f64 on RV32D with a soft float ABI.
3541       assert(VA.isRegLoc() && "Expected return via registers");
3542       SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
3543                                      DAG.getVTList(MVT::i32, MVT::i32), Val);
3544       SDValue Lo = SplitF64.getValue(0);
3545       SDValue Hi = SplitF64.getValue(1);
3546       Register RegLo = VA.getLocReg();
3547       assert(RegLo < RISCV::X31 && "Invalid register pair");
3548       Register RegHi = RegLo + 1;
3549 
3550       if (STI.isRegisterReservedByUser(RegLo) ||
3551           STI.isRegisterReservedByUser(RegHi))
3552         MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
3553             MF.getFunction(),
3554             "Return value register required, but has been reserved."});
3555 
3556       Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
3557       Glue = Chain.getValue(1);
3558       RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
3559       Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
3560       Glue = Chain.getValue(1);
3561       RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
3562     } else {
3563       // Handle a 'normal' return.
3564       Val = convertValVTToLocVT(DAG, Val, VA, DL);
3565       Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
3566 
3567       if (STI.isRegisterReservedByUser(VA.getLocReg()))
3568         MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
3569             MF.getFunction(),
3570             "Return value register required, but has been reserved."});
3571 
3572       // Guarantee that all emitted copies are stuck together.
3573       Glue = Chain.getValue(1);
3574       RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3575     }
3576   }
3577 
3578   RetOps[0] = Chain; // Update chain.
3579 
3580   // Add the glue node if we have it.
3581   if (Glue.getNode()) {
3582     RetOps.push_back(Glue);
3583   }
3584 
3585   // Interrupt service routines use different return instructions.
3586   const Function &Func = DAG.getMachineFunction().getFunction();
3587   if (Func.hasFnAttribute("interrupt")) {
3588     if (!Func.getReturnType()->isVoidTy())
3589       report_fatal_error(
3590           "Functions with the interrupt attribute must have void return type!");
3591 
3592     MachineFunction &MF = DAG.getMachineFunction();
3593     StringRef Kind =
3594       MF.getFunction().getFnAttribute("interrupt").getValueAsString();
3595 
3596     unsigned RetOpc;
3597     if (Kind == "user")
3598       RetOpc = RISCVISD::URET_FLAG;
3599     else if (Kind == "supervisor")
3600       RetOpc = RISCVISD::SRET_FLAG;
3601     else
3602       RetOpc = RISCVISD::MRET_FLAG;
3603 
3604     return DAG.getNode(RetOpc, DL, MVT::Other, RetOps);
3605   }
3606 
3607   return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
3608 }
3609 
3610 void RISCVTargetLowering::validateCCReservedRegs(
3611     const SmallVectorImpl<std::pair<llvm::Register, llvm::SDValue>> &Regs,
3612     MachineFunction &MF) const {
3613   const Function &F = MF.getFunction();
3614   const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
3615 
3616   if (llvm::any_of(Regs, [&STI](auto Reg) {
3617         return STI.isRegisterReservedByUser(Reg.first);
3618       }))
3619     F.getContext().diagnose(DiagnosticInfoUnsupported{
3620         F, "Argument register required, but has been reserved."});
3621 }
3622 
3623 bool RISCVTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
3624   return CI->isTailCall();
3625 }
3626 
3627 const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
3628 #define NODE_NAME_CASE(NODE)                                                   \
3629   case RISCVISD::NODE:                                                         \
3630     return "RISCVISD::" #NODE;
3631   // clang-format off
3632   switch ((RISCVISD::NodeType)Opcode) {
3633   case RISCVISD::FIRST_NUMBER:
3634     break;
3635   NODE_NAME_CASE(RET_FLAG)
3636   NODE_NAME_CASE(URET_FLAG)
3637   NODE_NAME_CASE(SRET_FLAG)
3638   NODE_NAME_CASE(MRET_FLAG)
3639   NODE_NAME_CASE(CALL)
3640   NODE_NAME_CASE(SELECT_CC)
3641   NODE_NAME_CASE(BuildPairF64)
3642   NODE_NAME_CASE(SplitF64)
3643   NODE_NAME_CASE(TAIL)
3644   NODE_NAME_CASE(SLLW)
3645   NODE_NAME_CASE(SRAW)
3646   NODE_NAME_CASE(SRLW)
3647   NODE_NAME_CASE(DIVW)
3648   NODE_NAME_CASE(DIVUW)
3649   NODE_NAME_CASE(REMUW)
3650   NODE_NAME_CASE(ROLW)
3651   NODE_NAME_CASE(RORW)
3652   NODE_NAME_CASE(FSLW)
3653   NODE_NAME_CASE(FSRW)
3654   NODE_NAME_CASE(FMV_H_X)
3655   NODE_NAME_CASE(FMV_X_ANYEXTH)
3656   NODE_NAME_CASE(FMV_W_X_RV64)
3657   NODE_NAME_CASE(FMV_X_ANYEXTW_RV64)
3658   NODE_NAME_CASE(READ_CYCLE_WIDE)
3659   NODE_NAME_CASE(GREVI)
3660   NODE_NAME_CASE(GREVIW)
3661   NODE_NAME_CASE(GORCI)
3662   NODE_NAME_CASE(GORCIW)
3663   NODE_NAME_CASE(VMV_X_S)
3664   NODE_NAME_CASE(SPLAT_VECTOR_I64)
3665   NODE_NAME_CASE(READ_VLENB)
3666   }
3667   // clang-format on
3668   return nullptr;
3669 #undef NODE_NAME_CASE
3670 }
3671 
3672 /// getConstraintType - Given a constraint letter, return the type of
3673 /// constraint it is for this target.
3674 RISCVTargetLowering::ConstraintType
3675 RISCVTargetLowering::getConstraintType(StringRef Constraint) const {
3676   if (Constraint.size() == 1) {
3677     switch (Constraint[0]) {
3678     default:
3679       break;
3680     case 'f':
3681       return C_RegisterClass;
3682     case 'I':
3683     case 'J':
3684     case 'K':
3685       return C_Immediate;
3686     case 'A':
3687       return C_Memory;
3688     }
3689   }
3690   return TargetLowering::getConstraintType(Constraint);
3691 }
3692 
3693 std::pair<unsigned, const TargetRegisterClass *>
3694 RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
3695                                                   StringRef Constraint,
3696                                                   MVT VT) const {
3697   // First, see if this is a constraint that directly corresponds to a
3698   // RISCV register class.
3699   if (Constraint.size() == 1) {
3700     switch (Constraint[0]) {
3701     case 'r':
3702       return std::make_pair(0U, &RISCV::GPRRegClass);
3703     case 'f':
3704       if (Subtarget.hasStdExtZfh() && VT == MVT::f16)
3705         return std::make_pair(0U, &RISCV::FPR16RegClass);
3706       if (Subtarget.hasStdExtF() && VT == MVT::f32)
3707         return std::make_pair(0U, &RISCV::FPR32RegClass);
3708       if (Subtarget.hasStdExtD() && VT == MVT::f64)
3709         return std::make_pair(0U, &RISCV::FPR64RegClass);
3710       break;
3711     default:
3712       break;
3713     }
3714   }
3715 
3716   // Clang will correctly decode the usage of register name aliases into their
3717   // official names. However, other frontends like `rustc` do not. This allows
3718   // users of these frontends to use the ABI names for registers in LLVM-style
3719   // register constraints.
3720   unsigned XRegFromAlias = StringSwitch<unsigned>(Constraint.lower())
3721                                .Case("{zero}", RISCV::X0)
3722                                .Case("{ra}", RISCV::X1)
3723                                .Case("{sp}", RISCV::X2)
3724                                .Case("{gp}", RISCV::X3)
3725                                .Case("{tp}", RISCV::X4)
3726                                .Case("{t0}", RISCV::X5)
3727                                .Case("{t1}", RISCV::X6)
3728                                .Case("{t2}", RISCV::X7)
3729                                .Cases("{s0}", "{fp}", RISCV::X8)
3730                                .Case("{s1}", RISCV::X9)
3731                                .Case("{a0}", RISCV::X10)
3732                                .Case("{a1}", RISCV::X11)
3733                                .Case("{a2}", RISCV::X12)
3734                                .Case("{a3}", RISCV::X13)
3735                                .Case("{a4}", RISCV::X14)
3736                                .Case("{a5}", RISCV::X15)
3737                                .Case("{a6}", RISCV::X16)
3738                                .Case("{a7}", RISCV::X17)
3739                                .Case("{s2}", RISCV::X18)
3740                                .Case("{s3}", RISCV::X19)
3741                                .Case("{s4}", RISCV::X20)
3742                                .Case("{s5}", RISCV::X21)
3743                                .Case("{s6}", RISCV::X22)
3744                                .Case("{s7}", RISCV::X23)
3745                                .Case("{s8}", RISCV::X24)
3746                                .Case("{s9}", RISCV::X25)
3747                                .Case("{s10}", RISCV::X26)
3748                                .Case("{s11}", RISCV::X27)
3749                                .Case("{t3}", RISCV::X28)
3750                                .Case("{t4}", RISCV::X29)
3751                                .Case("{t5}", RISCV::X30)
3752                                .Case("{t6}", RISCV::X31)
3753                                .Default(RISCV::NoRegister);
3754   if (XRegFromAlias != RISCV::NoRegister)
3755     return std::make_pair(XRegFromAlias, &RISCV::GPRRegClass);
3756 
3757   // Since TargetLowering::getRegForInlineAsmConstraint uses the name of the
3758   // TableGen record rather than the AsmName to choose registers for InlineAsm
3759   // constraints, plus we want to match those names to the widest floating point
3760   // register type available, manually select floating point registers here.
3761   //
3762   // The second case is the ABI name of the register, so that frontends can also
3763   // use the ABI names in register constraint lists.
3764   if (Subtarget.hasStdExtF()) {
3765     unsigned FReg = StringSwitch<unsigned>(Constraint.lower())
3766                         .Cases("{f0}", "{ft0}", RISCV::F0_F)
3767                         .Cases("{f1}", "{ft1}", RISCV::F1_F)
3768                         .Cases("{f2}", "{ft2}", RISCV::F2_F)
3769                         .Cases("{f3}", "{ft3}", RISCV::F3_F)
3770                         .Cases("{f4}", "{ft4}", RISCV::F4_F)
3771                         .Cases("{f5}", "{ft5}", RISCV::F5_F)
3772                         .Cases("{f6}", "{ft6}", RISCV::F6_F)
3773                         .Cases("{f7}", "{ft7}", RISCV::F7_F)
3774                         .Cases("{f8}", "{fs0}", RISCV::F8_F)
3775                         .Cases("{f9}", "{fs1}", RISCV::F9_F)
3776                         .Cases("{f10}", "{fa0}", RISCV::F10_F)
3777                         .Cases("{f11}", "{fa1}", RISCV::F11_F)
3778                         .Cases("{f12}", "{fa2}", RISCV::F12_F)
3779                         .Cases("{f13}", "{fa3}", RISCV::F13_F)
3780                         .Cases("{f14}", "{fa4}", RISCV::F14_F)
3781                         .Cases("{f15}", "{fa5}", RISCV::F15_F)
3782                         .Cases("{f16}", "{fa6}", RISCV::F16_F)
3783                         .Cases("{f17}", "{fa7}", RISCV::F17_F)
3784                         .Cases("{f18}", "{fs2}", RISCV::F18_F)
3785                         .Cases("{f19}", "{fs3}", RISCV::F19_F)
3786                         .Cases("{f20}", "{fs4}", RISCV::F20_F)
3787                         .Cases("{f21}", "{fs5}", RISCV::F21_F)
3788                         .Cases("{f22}", "{fs6}", RISCV::F22_F)
3789                         .Cases("{f23}", "{fs7}", RISCV::F23_F)
3790                         .Cases("{f24}", "{fs8}", RISCV::F24_F)
3791                         .Cases("{f25}", "{fs9}", RISCV::F25_F)
3792                         .Cases("{f26}", "{fs10}", RISCV::F26_F)
3793                         .Cases("{f27}", "{fs11}", RISCV::F27_F)
3794                         .Cases("{f28}", "{ft8}", RISCV::F28_F)
3795                         .Cases("{f29}", "{ft9}", RISCV::F29_F)
3796                         .Cases("{f30}", "{ft10}", RISCV::F30_F)
3797                         .Cases("{f31}", "{ft11}", RISCV::F31_F)
3798                         .Default(RISCV::NoRegister);
3799     if (FReg != RISCV::NoRegister) {
3800       assert(RISCV::F0_F <= FReg && FReg <= RISCV::F31_F && "Unknown fp-reg");
3801       if (Subtarget.hasStdExtD()) {
3802         unsigned RegNo = FReg - RISCV::F0_F;
3803         unsigned DReg = RISCV::F0_D + RegNo;
3804         return std::make_pair(DReg, &RISCV::FPR64RegClass);
3805       }
3806       return std::make_pair(FReg, &RISCV::FPR32RegClass);
3807     }
3808   }
3809 
3810   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3811 }
3812 
3813 unsigned
3814 RISCVTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const {
3815   // Currently only support length 1 constraints.
3816   if (ConstraintCode.size() == 1) {
3817     switch (ConstraintCode[0]) {
3818     case 'A':
3819       return InlineAsm::Constraint_A;
3820     default:
3821       break;
3822     }
3823   }
3824 
3825   return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
3826 }
3827 
3828 void RISCVTargetLowering::LowerAsmOperandForConstraint(
3829     SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
3830     SelectionDAG &DAG) const {
3831   // Currently only support length 1 constraints.
3832   if (Constraint.length() == 1) {
3833     switch (Constraint[0]) {
3834     case 'I':
3835       // Validate & create a 12-bit signed immediate operand.
3836       if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3837         uint64_t CVal = C->getSExtValue();
3838         if (isInt<12>(CVal))
3839           Ops.push_back(
3840               DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
3841       }
3842       return;
3843     case 'J':
3844       // Validate & create an integer zero operand.
3845       if (auto *C = dyn_cast<ConstantSDNode>(Op))
3846         if (C->getZExtValue() == 0)
3847           Ops.push_back(
3848               DAG.getTargetConstant(0, SDLoc(Op), Subtarget.getXLenVT()));
3849       return;
3850     case 'K':
3851       // Validate & create a 5-bit unsigned immediate operand.
3852       if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3853         uint64_t CVal = C->getZExtValue();
3854         if (isUInt<5>(CVal))
3855           Ops.push_back(
3856               DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
3857       }
3858       return;
3859     default:
3860       break;
3861     }
3862   }
3863   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3864 }
3865 
3866 Instruction *RISCVTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
3867                                                    Instruction *Inst,
3868                                                    AtomicOrdering Ord) const {
3869   if (isa<LoadInst>(Inst) && Ord == AtomicOrdering::SequentiallyConsistent)
3870     return Builder.CreateFence(Ord);
3871   if (isa<StoreInst>(Inst) && isReleaseOrStronger(Ord))
3872     return Builder.CreateFence(AtomicOrdering::Release);
3873   return nullptr;
3874 }
3875 
3876 Instruction *RISCVTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
3877                                                     Instruction *Inst,
3878                                                     AtomicOrdering Ord) const {
3879   if (isa<LoadInst>(Inst) && isAcquireOrStronger(Ord))
3880     return Builder.CreateFence(AtomicOrdering::Acquire);
3881   return nullptr;
3882 }
3883 
3884 TargetLowering::AtomicExpansionKind
3885 RISCVTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
3886   // atomicrmw {fadd,fsub} must be expanded to use compare-exchange, as floating
3887   // point operations can't be used in an lr/sc sequence without breaking the
3888   // forward-progress guarantee.
3889   if (AI->isFloatingPointOperation())
3890     return AtomicExpansionKind::CmpXChg;
3891 
3892   unsigned Size = AI->getType()->getPrimitiveSizeInBits();
3893   if (Size == 8 || Size == 16)
3894     return AtomicExpansionKind::MaskedIntrinsic;
3895   return AtomicExpansionKind::None;
3896 }
3897 
3898 static Intrinsic::ID
3899 getIntrinsicForMaskedAtomicRMWBinOp(unsigned XLen, AtomicRMWInst::BinOp BinOp) {
3900   if (XLen == 32) {
3901     switch (BinOp) {
3902     default:
3903       llvm_unreachable("Unexpected AtomicRMW BinOp");
3904     case AtomicRMWInst::Xchg:
3905       return Intrinsic::riscv_masked_atomicrmw_xchg_i32;
3906     case AtomicRMWInst::Add:
3907       return Intrinsic::riscv_masked_atomicrmw_add_i32;
3908     case AtomicRMWInst::Sub:
3909       return Intrinsic::riscv_masked_atomicrmw_sub_i32;
3910     case AtomicRMWInst::Nand:
3911       return Intrinsic::riscv_masked_atomicrmw_nand_i32;
3912     case AtomicRMWInst::Max:
3913       return Intrinsic::riscv_masked_atomicrmw_max_i32;
3914     case AtomicRMWInst::Min:
3915       return Intrinsic::riscv_masked_atomicrmw_min_i32;
3916     case AtomicRMWInst::UMax:
3917       return Intrinsic::riscv_masked_atomicrmw_umax_i32;
3918     case AtomicRMWInst::UMin:
3919       return Intrinsic::riscv_masked_atomicrmw_umin_i32;
3920     }
3921   }
3922 
3923   if (XLen == 64) {
3924     switch (BinOp) {
3925     default:
3926       llvm_unreachable("Unexpected AtomicRMW BinOp");
3927     case AtomicRMWInst::Xchg:
3928       return Intrinsic::riscv_masked_atomicrmw_xchg_i64;
3929     case AtomicRMWInst::Add:
3930       return Intrinsic::riscv_masked_atomicrmw_add_i64;
3931     case AtomicRMWInst::Sub:
3932       return Intrinsic::riscv_masked_atomicrmw_sub_i64;
3933     case AtomicRMWInst::Nand:
3934       return Intrinsic::riscv_masked_atomicrmw_nand_i64;
3935     case AtomicRMWInst::Max:
3936       return Intrinsic::riscv_masked_atomicrmw_max_i64;
3937     case AtomicRMWInst::Min:
3938       return Intrinsic::riscv_masked_atomicrmw_min_i64;
3939     case AtomicRMWInst::UMax:
3940       return Intrinsic::riscv_masked_atomicrmw_umax_i64;
3941     case AtomicRMWInst::UMin:
3942       return Intrinsic::riscv_masked_atomicrmw_umin_i64;
3943     }
3944   }
3945 
3946   llvm_unreachable("Unexpected XLen\n");
3947 }
3948 
3949 Value *RISCVTargetLowering::emitMaskedAtomicRMWIntrinsic(
3950     IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr,
3951     Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const {
3952   unsigned XLen = Subtarget.getXLen();
3953   Value *Ordering =
3954       Builder.getIntN(XLen, static_cast<uint64_t>(AI->getOrdering()));
3955   Type *Tys[] = {AlignedAddr->getType()};
3956   Function *LrwOpScwLoop = Intrinsic::getDeclaration(
3957       AI->getModule(),
3958       getIntrinsicForMaskedAtomicRMWBinOp(XLen, AI->getOperation()), Tys);
3959 
3960   if (XLen == 64) {
3961     Incr = Builder.CreateSExt(Incr, Builder.getInt64Ty());
3962     Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
3963     ShiftAmt = Builder.CreateSExt(ShiftAmt, Builder.getInt64Ty());
3964   }
3965 
3966   Value *Result;
3967 
3968   // Must pass the shift amount needed to sign extend the loaded value prior
3969   // to performing a signed comparison for min/max. ShiftAmt is the number of
3970   // bits to shift the value into position. Pass XLen-ShiftAmt-ValWidth, which
3971   // is the number of bits to left+right shift the value in order to
3972   // sign-extend.
3973   if (AI->getOperation() == AtomicRMWInst::Min ||
3974       AI->getOperation() == AtomicRMWInst::Max) {
3975     const DataLayout &DL = AI->getModule()->getDataLayout();
3976     unsigned ValWidth =
3977         DL.getTypeStoreSizeInBits(AI->getValOperand()->getType());
3978     Value *SextShamt =
3979         Builder.CreateSub(Builder.getIntN(XLen, XLen - ValWidth), ShiftAmt);
3980     Result = Builder.CreateCall(LrwOpScwLoop,
3981                                 {AlignedAddr, Incr, Mask, SextShamt, Ordering});
3982   } else {
3983     Result =
3984         Builder.CreateCall(LrwOpScwLoop, {AlignedAddr, Incr, Mask, Ordering});
3985   }
3986 
3987   if (XLen == 64)
3988     Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
3989   return Result;
3990 }
3991 
3992 TargetLowering::AtomicExpansionKind
3993 RISCVTargetLowering::shouldExpandAtomicCmpXchgInIR(
3994     AtomicCmpXchgInst *CI) const {
3995   unsigned Size = CI->getCompareOperand()->getType()->getPrimitiveSizeInBits();
3996   if (Size == 8 || Size == 16)
3997     return AtomicExpansionKind::MaskedIntrinsic;
3998   return AtomicExpansionKind::None;
3999 }
4000 
4001 Value *RISCVTargetLowering::emitMaskedAtomicCmpXchgIntrinsic(
4002     IRBuilder<> &Builder, AtomicCmpXchgInst *CI, Value *AlignedAddr,
4003     Value *CmpVal, Value *NewVal, Value *Mask, AtomicOrdering Ord) const {
4004   unsigned XLen = Subtarget.getXLen();
4005   Value *Ordering = Builder.getIntN(XLen, static_cast<uint64_t>(Ord));
4006   Intrinsic::ID CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i32;
4007   if (XLen == 64) {
4008     CmpVal = Builder.CreateSExt(CmpVal, Builder.getInt64Ty());
4009     NewVal = Builder.CreateSExt(NewVal, Builder.getInt64Ty());
4010     Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
4011     CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i64;
4012   }
4013   Type *Tys[] = {AlignedAddr->getType()};
4014   Function *MaskedCmpXchg =
4015       Intrinsic::getDeclaration(CI->getModule(), CmpXchgIntrID, Tys);
4016   Value *Result = Builder.CreateCall(
4017       MaskedCmpXchg, {AlignedAddr, CmpVal, NewVal, Mask, Ordering});
4018   if (XLen == 64)
4019     Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
4020   return Result;
4021 }
4022 
4023 bool RISCVTargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
4024                                                      EVT VT) const {
4025   VT = VT.getScalarType();
4026 
4027   if (!VT.isSimple())
4028     return false;
4029 
4030   switch (VT.getSimpleVT().SimpleTy) {
4031   case MVT::f16:
4032     return Subtarget.hasStdExtZfh();
4033   case MVT::f32:
4034     return Subtarget.hasStdExtF();
4035   case MVT::f64:
4036     return Subtarget.hasStdExtD();
4037   default:
4038     break;
4039   }
4040 
4041   return false;
4042 }
4043 
4044 Register RISCVTargetLowering::getExceptionPointerRegister(
4045     const Constant *PersonalityFn) const {
4046   return RISCV::X10;
4047 }
4048 
4049 Register RISCVTargetLowering::getExceptionSelectorRegister(
4050     const Constant *PersonalityFn) const {
4051   return RISCV::X11;
4052 }
4053 
4054 bool RISCVTargetLowering::shouldExtendTypeInLibCall(EVT Type) const {
4055   // Return false to suppress the unnecessary extensions if the LibCall
4056   // arguments or return value is f32 type for LP64 ABI.
4057   RISCVABI::ABI ABI = Subtarget.getTargetABI();
4058   if (ABI == RISCVABI::ABI_LP64 && (Type == MVT::f32))
4059     return false;
4060 
4061   return true;
4062 }
4063 
4064 bool RISCVTargetLowering::decomposeMulByConstant(LLVMContext &Context, EVT VT,
4065                                                  SDValue C) const {
4066   // Check integral scalar types.
4067   if (VT.isScalarInteger()) {
4068     // Omit the optimization if the sub target has the M extension and the data
4069     // size exceeds XLen.
4070     if (Subtarget.hasStdExtM() && VT.getSizeInBits() > Subtarget.getXLen())
4071       return false;
4072     if (auto *ConstNode = dyn_cast<ConstantSDNode>(C.getNode())) {
4073       // Break the MUL to a SLLI and an ADD/SUB.
4074       const APInt &Imm = ConstNode->getAPIntValue();
4075       if ((Imm + 1).isPowerOf2() || (Imm - 1).isPowerOf2() ||
4076           (1 - Imm).isPowerOf2() || (-1 - Imm).isPowerOf2())
4077         return true;
4078       // Omit the following optimization if the sub target has the M extension
4079       // and the data size >= XLen.
4080       if (Subtarget.hasStdExtM() && VT.getSizeInBits() >= Subtarget.getXLen())
4081         return false;
4082       // Break the MUL to two SLLI instructions and an ADD/SUB, if Imm needs
4083       // a pair of LUI/ADDI.
4084       if (!Imm.isSignedIntN(12) && Imm.countTrailingZeros() < 12) {
4085         APInt ImmS = Imm.ashr(Imm.countTrailingZeros());
4086         if ((ImmS + 1).isPowerOf2() || (ImmS - 1).isPowerOf2() ||
4087             (1 - ImmS).isPowerOf2())
4088         return true;
4089       }
4090     }
4091   }
4092 
4093   return false;
4094 }
4095 
4096 #define GET_REGISTER_MATCHER
4097 #include "RISCVGenAsmMatcher.inc"
4098 
4099 Register
4100 RISCVTargetLowering::getRegisterByName(const char *RegName, LLT VT,
4101                                        const MachineFunction &MF) const {
4102   Register Reg = MatchRegisterAltName(RegName);
4103   if (Reg == RISCV::NoRegister)
4104     Reg = MatchRegisterName(RegName);
4105   if (Reg == RISCV::NoRegister)
4106     report_fatal_error(
4107         Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
4108   BitVector ReservedRegs = Subtarget.getRegisterInfo()->getReservedRegs(MF);
4109   if (!ReservedRegs.test(Reg) && !Subtarget.isRegisterReservedByUser(Reg))
4110     report_fatal_error(Twine("Trying to obtain non-reserved register \"" +
4111                              StringRef(RegName) + "\"."));
4112   return Reg;
4113 }
4114 
4115 namespace llvm {
4116 namespace RISCVVIntrinsicsTable {
4117 
4118 #define GET_RISCVVIntrinsicsTable_IMPL
4119 #include "RISCVGenSearchableTables.inc"
4120 
4121 } // namespace RISCVVIntrinsicsTable
4122 } // namespace llvm
4123