]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
MFV r357712: file 5.38.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / Target / RISCV / RISCVISelLowering.cpp
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 "RISCV.h"
16 #include "RISCVMachineFunctionInfo.h"
17 #include "RISCVRegisterInfo.h"
18 #include "RISCVSubtarget.h"
19 #include "RISCVTargetMachine.h"
20 #include "Utils/RISCVMatInt.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/SelectionDAGISel.h"
29 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/IR/DiagnosticInfo.h"
32 #include "llvm/IR/DiagnosticPrinter.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36
37 using namespace llvm;
38
39 #define DEBUG_TYPE "riscv-lower"
40
41 STATISTIC(NumTailCalls, "Number of tail calls");
42
43 RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
44                                          const RISCVSubtarget &STI)
45     : TargetLowering(TM), Subtarget(STI) {
46
47   if (Subtarget.isRV32E())
48     report_fatal_error("Codegen not yet implemented for RV32E");
49
50   RISCVABI::ABI ABI = Subtarget.getTargetABI();
51   assert(ABI != RISCVABI::ABI_Unknown && "Improperly initialised target ABI");
52
53   switch (ABI) {
54   default:
55     report_fatal_error("Don't know how to lower this ABI");
56   case RISCVABI::ABI_ILP32:
57   case RISCVABI::ABI_ILP32F:
58   case RISCVABI::ABI_ILP32D:
59   case RISCVABI::ABI_LP64:
60   case RISCVABI::ABI_LP64F:
61   case RISCVABI::ABI_LP64D:
62     break;
63   }
64
65   MVT XLenVT = Subtarget.getXLenVT();
66
67   // Set up the register classes.
68   addRegisterClass(XLenVT, &RISCV::GPRRegClass);
69
70   if (Subtarget.hasStdExtF())
71     addRegisterClass(MVT::f32, &RISCV::FPR32RegClass);
72   if (Subtarget.hasStdExtD())
73     addRegisterClass(MVT::f64, &RISCV::FPR64RegClass);
74
75   // Compute derived properties from the register classes.
76   computeRegisterProperties(STI.getRegisterInfo());
77
78   setStackPointerRegisterToSaveRestore(RISCV::X2);
79
80   for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD})
81     setLoadExtAction(N, XLenVT, MVT::i1, Promote);
82
83   // TODO: add all necessary setOperationAction calls.
84   setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand);
85
86   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
87   setOperationAction(ISD::BR_CC, XLenVT, Expand);
88   setOperationAction(ISD::SELECT, XLenVT, Custom);
89   setOperationAction(ISD::SELECT_CC, XLenVT, Expand);
90
91   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
92   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
93
94   setOperationAction(ISD::VASTART, MVT::Other, Custom);
95   setOperationAction(ISD::VAARG, MVT::Other, Expand);
96   setOperationAction(ISD::VACOPY, MVT::Other, Expand);
97   setOperationAction(ISD::VAEND, MVT::Other, Expand);
98
99   for (auto VT : {MVT::i1, MVT::i8, MVT::i16})
100     setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
101
102   if (Subtarget.is64Bit()) {
103     setOperationAction(ISD::SHL, MVT::i32, Custom);
104     setOperationAction(ISD::SRA, MVT::i32, Custom);
105     setOperationAction(ISD::SRL, MVT::i32, Custom);
106   }
107
108   if (!Subtarget.hasStdExtM()) {
109     setOperationAction(ISD::MUL, XLenVT, Expand);
110     setOperationAction(ISD::MULHS, XLenVT, Expand);
111     setOperationAction(ISD::MULHU, XLenVT, Expand);
112     setOperationAction(ISD::SDIV, XLenVT, Expand);
113     setOperationAction(ISD::UDIV, XLenVT, Expand);
114     setOperationAction(ISD::SREM, XLenVT, Expand);
115     setOperationAction(ISD::UREM, XLenVT, Expand);
116   }
117
118   if (Subtarget.is64Bit() && Subtarget.hasStdExtM()) {
119     setOperationAction(ISD::SDIV, MVT::i32, Custom);
120     setOperationAction(ISD::UDIV, MVT::i32, Custom);
121     setOperationAction(ISD::UREM, MVT::i32, Custom);
122   }
123
124   setOperationAction(ISD::SDIVREM, XLenVT, Expand);
125   setOperationAction(ISD::UDIVREM, XLenVT, Expand);
126   setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand);
127   setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand);
128
129   setOperationAction(ISD::SHL_PARTS, XLenVT, Custom);
130   setOperationAction(ISD::SRL_PARTS, XLenVT, Custom);
131   setOperationAction(ISD::SRA_PARTS, XLenVT, Custom);
132
133   setOperationAction(ISD::ROTL, XLenVT, Expand);
134   setOperationAction(ISD::ROTR, XLenVT, Expand);
135   setOperationAction(ISD::BSWAP, XLenVT, Expand);
136   setOperationAction(ISD::CTTZ, XLenVT, Expand);
137   setOperationAction(ISD::CTLZ, XLenVT, Expand);
138   setOperationAction(ISD::CTPOP, XLenVT, Expand);
139
140   ISD::CondCode FPCCToExtend[] = {
141       ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETUEQ, ISD::SETUGT,
142       ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE, ISD::SETGT,
143       ISD::SETGE,  ISD::SETNE};
144
145   ISD::NodeType FPOpToExtend[] = {
146       ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM};
147
148   if (Subtarget.hasStdExtF()) {
149     setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
150     setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
151     for (auto CC : FPCCToExtend)
152       setCondCodeAction(CC, MVT::f32, Expand);
153     setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
154     setOperationAction(ISD::SELECT, MVT::f32, Custom);
155     setOperationAction(ISD::BR_CC, MVT::f32, Expand);
156     for (auto Op : FPOpToExtend)
157       setOperationAction(Op, MVT::f32, Expand);
158   }
159
160   if (Subtarget.hasStdExtF() && Subtarget.is64Bit())
161     setOperationAction(ISD::BITCAST, MVT::i32, Custom);
162
163   if (Subtarget.hasStdExtD()) {
164     setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
165     setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
166     for (auto CC : FPCCToExtend)
167       setCondCodeAction(CC, MVT::f64, Expand);
168     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
169     setOperationAction(ISD::SELECT, MVT::f64, Custom);
170     setOperationAction(ISD::BR_CC, MVT::f64, Expand);
171     setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
172     setTruncStoreAction(MVT::f64, MVT::f32, Expand);
173     for (auto Op : FPOpToExtend)
174       setOperationAction(Op, MVT::f64, Expand);
175   }
176
177   setOperationAction(ISD::GlobalAddress, XLenVT, Custom);
178   setOperationAction(ISD::BlockAddress, XLenVT, Custom);
179   setOperationAction(ISD::ConstantPool, XLenVT, Custom);
180
181   setOperationAction(ISD::GlobalTLSAddress, XLenVT, Custom);
182
183   // TODO: On M-mode only targets, the cycle[h] CSR may not be present.
184   // Unfortunately this can't be determined just from the ISA naming string.
185   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64,
186                      Subtarget.is64Bit() ? Legal : Custom);
187
188   if (Subtarget.hasStdExtA()) {
189     setMaxAtomicSizeInBitsSupported(Subtarget.getXLen());
190     setMinCmpXchgSizeInBits(32);
191   } else {
192     setMaxAtomicSizeInBitsSupported(0);
193   }
194
195   setBooleanContents(ZeroOrOneBooleanContent);
196
197   // Function alignments (log2).
198   unsigned FunctionAlignment = Subtarget.hasStdExtC() ? 1 : 2;
199   setMinFunctionAlignment(FunctionAlignment);
200   setPrefFunctionAlignment(FunctionAlignment);
201
202   // Effectively disable jump table generation.
203   setMinimumJumpTableEntries(INT_MAX);
204 }
205
206 EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
207                                             EVT VT) const {
208   if (!VT.isVector())
209     return getPointerTy(DL);
210   return VT.changeVectorElementTypeToInteger();
211 }
212
213 bool RISCVTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
214                                              const CallInst &I,
215                                              MachineFunction &MF,
216                                              unsigned Intrinsic) const {
217   switch (Intrinsic) {
218   default:
219     return false;
220   case Intrinsic::riscv_masked_atomicrmw_xchg_i32:
221   case Intrinsic::riscv_masked_atomicrmw_add_i32:
222   case Intrinsic::riscv_masked_atomicrmw_sub_i32:
223   case Intrinsic::riscv_masked_atomicrmw_nand_i32:
224   case Intrinsic::riscv_masked_atomicrmw_max_i32:
225   case Intrinsic::riscv_masked_atomicrmw_min_i32:
226   case Intrinsic::riscv_masked_atomicrmw_umax_i32:
227   case Intrinsic::riscv_masked_atomicrmw_umin_i32:
228   case Intrinsic::riscv_masked_cmpxchg_i32:
229     PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
230     Info.opc = ISD::INTRINSIC_W_CHAIN;
231     Info.memVT = MVT::getVT(PtrTy->getElementType());
232     Info.ptrVal = I.getArgOperand(0);
233     Info.offset = 0;
234     Info.align = 4;
235     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore |
236                  MachineMemOperand::MOVolatile;
237     return true;
238   }
239 }
240
241 bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL,
242                                                 const AddrMode &AM, Type *Ty,
243                                                 unsigned AS,
244                                                 Instruction *I) const {
245   // No global is ever allowed as a base.
246   if (AM.BaseGV)
247     return false;
248
249   // Require a 12-bit signed offset.
250   if (!isInt<12>(AM.BaseOffs))
251     return false;
252
253   switch (AM.Scale) {
254   case 0: // "r+i" or just "i", depending on HasBaseReg.
255     break;
256   case 1:
257     if (!AM.HasBaseReg) // allow "r+i".
258       break;
259     return false; // disallow "r+r" or "r+r+i".
260   default:
261     return false;
262   }
263
264   return true;
265 }
266
267 bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
268   return isInt<12>(Imm);
269 }
270
271 bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
272   return isInt<12>(Imm);
273 }
274
275 // On RV32, 64-bit integers are split into their high and low parts and held
276 // in two different registers, so the trunc is free since the low register can
277 // just be used.
278 bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
279   if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
280     return false;
281   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
282   unsigned DestBits = DstTy->getPrimitiveSizeInBits();
283   return (SrcBits == 64 && DestBits == 32);
284 }
285
286 bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
287   if (Subtarget.is64Bit() || SrcVT.isVector() || DstVT.isVector() ||
288       !SrcVT.isInteger() || !DstVT.isInteger())
289     return false;
290   unsigned SrcBits = SrcVT.getSizeInBits();
291   unsigned DestBits = DstVT.getSizeInBits();
292   return (SrcBits == 64 && DestBits == 32);
293 }
294
295 bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
296   // Zexts are free if they can be combined with a load.
297   if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
298     EVT MemVT = LD->getMemoryVT();
299     if ((MemVT == MVT::i8 || MemVT == MVT::i16 ||
300          (Subtarget.is64Bit() && MemVT == MVT::i32)) &&
301         (LD->getExtensionType() == ISD::NON_EXTLOAD ||
302          LD->getExtensionType() == ISD::ZEXTLOAD))
303       return true;
304   }
305
306   return TargetLowering::isZExtFree(Val, VT2);
307 }
308
309 bool RISCVTargetLowering::isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const {
310   return Subtarget.is64Bit() && SrcVT == MVT::i32 && DstVT == MVT::i64;
311 }
312
313 bool RISCVTargetLowering::hasBitPreservingFPLogic(EVT VT) const {
314   return (VT == MVT::f32 && Subtarget.hasStdExtF()) ||
315          (VT == MVT::f64 && Subtarget.hasStdExtD());
316 }
317
318 // Changes the condition code and swaps operands if necessary, so the SetCC
319 // operation matches one of the comparisons supported directly in the RISC-V
320 // ISA.
321 static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
322   switch (CC) {
323   default:
324     break;
325   case ISD::SETGT:
326   case ISD::SETLE:
327   case ISD::SETUGT:
328   case ISD::SETULE:
329     CC = ISD::getSetCCSwappedOperands(CC);
330     std::swap(LHS, RHS);
331     break;
332   }
333 }
334
335 // Return the RISC-V branch opcode that matches the given DAG integer
336 // condition code. The CondCode must be one of those supported by the RISC-V
337 // ISA (see normaliseSetCC).
338 static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
339   switch (CC) {
340   default:
341     llvm_unreachable("Unsupported CondCode");
342   case ISD::SETEQ:
343     return RISCV::BEQ;
344   case ISD::SETNE:
345     return RISCV::BNE;
346   case ISD::SETLT:
347     return RISCV::BLT;
348   case ISD::SETGE:
349     return RISCV::BGE;
350   case ISD::SETULT:
351     return RISCV::BLTU;
352   case ISD::SETUGE:
353     return RISCV::BGEU;
354   }
355 }
356
357 SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
358                                             SelectionDAG &DAG) const {
359   switch (Op.getOpcode()) {
360   default:
361     report_fatal_error("unimplemented operand");
362   case ISD::GlobalAddress:
363     return lowerGlobalAddress(Op, DAG);
364   case ISD::BlockAddress:
365     return lowerBlockAddress(Op, DAG);
366   case ISD::ConstantPool:
367     return lowerConstantPool(Op, DAG);
368   case ISD::GlobalTLSAddress:
369     return lowerGlobalTLSAddress(Op, DAG);
370   case ISD::SELECT:
371     return lowerSELECT(Op, DAG);
372   case ISD::VASTART:
373     return lowerVASTART(Op, DAG);
374   case ISD::FRAMEADDR:
375     return lowerFRAMEADDR(Op, DAG);
376   case ISD::RETURNADDR:
377     return lowerRETURNADDR(Op, DAG);
378   case ISD::SHL_PARTS:
379     return lowerShiftLeftParts(Op, DAG);
380   case ISD::SRA_PARTS:
381     return lowerShiftRightParts(Op, DAG, true);
382   case ISD::SRL_PARTS:
383     return lowerShiftRightParts(Op, DAG, false);
384   case ISD::BITCAST: {
385     assert(Subtarget.is64Bit() && Subtarget.hasStdExtF() &&
386            "Unexpected custom legalisation");
387     SDLoc DL(Op);
388     SDValue Op0 = Op.getOperand(0);
389     if (Op.getValueType() != MVT::f32 || Op0.getValueType() != MVT::i32)
390       return SDValue();
391     SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
392     SDValue FPConv = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, NewOp0);
393     return FPConv;
394   }
395   }
396 }
397
398 static SDValue getTargetNode(GlobalAddressSDNode *N, SDLoc DL, EVT Ty,
399                              SelectionDAG &DAG, unsigned Flags) {
400   return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
401 }
402
403 static SDValue getTargetNode(BlockAddressSDNode *N, SDLoc DL, EVT Ty,
404                              SelectionDAG &DAG, unsigned Flags) {
405   return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, N->getOffset(),
406                                    Flags);
407 }
408
409 static SDValue getTargetNode(ConstantPoolSDNode *N, SDLoc DL, EVT Ty,
410                              SelectionDAG &DAG, unsigned Flags) {
411   return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
412                                    N->getOffset(), Flags);
413 }
414
415 template <class NodeTy>
416 SDValue RISCVTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
417                                      bool IsLocal) const {
418   SDLoc DL(N);
419   EVT Ty = getPointerTy(DAG.getDataLayout());
420
421   if (isPositionIndependent()) {
422     SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
423     if (IsLocal)
424       // Use PC-relative addressing to access the symbol. This generates the
425       // pattern (PseudoLLA sym), which expands to (addi (auipc %pcrel_hi(sym))
426       // %pcrel_lo(auipc)).
427       return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
428
429     // Use PC-relative addressing to access the GOT for this symbol, then load
430     // the address from the GOT. This generates the pattern (PseudoLA sym),
431     // which expands to (ld (addi (auipc %got_pcrel_hi(sym)) %pcrel_lo(auipc))).
432     return SDValue(DAG.getMachineNode(RISCV::PseudoLA, DL, Ty, Addr), 0);
433   }
434
435   switch (getTargetMachine().getCodeModel()) {
436   default:
437     report_fatal_error("Unsupported code model for lowering");
438   case CodeModel::Small: {
439     // Generate a sequence for accessing addresses within the first 2 GiB of
440     // address space. This generates the pattern (addi (lui %hi(sym)) %lo(sym)).
441     SDValue AddrHi = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_HI);
442     SDValue AddrLo = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_LO);
443     SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
444     return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, AddrLo), 0);
445   }
446   case CodeModel::Medium: {
447     // Generate a sequence for accessing addresses within any 2GiB range within
448     // the address space. This generates the pattern (PseudoLLA sym), which
449     // expands to (addi (auipc %pcrel_hi(sym)) %pcrel_lo(auipc)).
450     SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
451     return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
452   }
453   }
454 }
455
456 SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
457                                                 SelectionDAG &DAG) const {
458   SDLoc DL(Op);
459   EVT Ty = Op.getValueType();
460   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
461   int64_t Offset = N->getOffset();
462   MVT XLenVT = Subtarget.getXLenVT();
463
464   const GlobalValue *GV = N->getGlobal();
465   bool IsLocal = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
466   SDValue Addr = getAddr(N, DAG, IsLocal);
467
468   // In order to maximise the opportunity for common subexpression elimination,
469   // emit a separate ADD node for the global address offset instead of folding
470   // it in the global address node. Later peephole optimisations may choose to
471   // fold it back in when profitable.
472   if (Offset != 0)
473     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
474                        DAG.getConstant(Offset, DL, XLenVT));
475   return Addr;
476 }
477
478 SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
479                                                SelectionDAG &DAG) const {
480   BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
481
482   return getAddr(N, DAG);
483 }
484
485 SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
486                                                SelectionDAG &DAG) const {
487   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
488
489   return getAddr(N, DAG);
490 }
491
492 SDValue RISCVTargetLowering::getStaticTLSAddr(GlobalAddressSDNode *N,
493                                               SelectionDAG &DAG,
494                                               bool UseGOT) const {
495   SDLoc DL(N);
496   EVT Ty = getPointerTy(DAG.getDataLayout());
497   const GlobalValue *GV = N->getGlobal();
498   MVT XLenVT = Subtarget.getXLenVT();
499
500   if (UseGOT) {
501     // Use PC-relative addressing to access the GOT for this TLS symbol, then
502     // load the address from the GOT and add the thread pointer. This generates
503     // the pattern (PseudoLA_TLS_IE sym), which expands to
504     // (ld (auipc %tls_ie_pcrel_hi(sym)) %pcrel_lo(auipc)).
505     SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
506     SDValue Load =
507         SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_IE, DL, Ty, Addr), 0);
508
509     // Add the thread pointer.
510     SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
511     return DAG.getNode(ISD::ADD, DL, Ty, Load, TPReg);
512   }
513
514   // Generate a sequence for accessing the address relative to the thread
515   // pointer, with the appropriate adjustment for the thread pointer offset.
516   // This generates the pattern
517   // (add (add_tprel (lui %tprel_hi(sym)) tp %tprel_add(sym)) %tprel_lo(sym))
518   SDValue AddrHi =
519       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_HI);
520   SDValue AddrAdd =
521       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_ADD);
522   SDValue AddrLo =
523       DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_LO);
524
525   SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
526   SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
527   SDValue MNAdd = SDValue(
528       DAG.getMachineNode(RISCV::PseudoAddTPRel, DL, Ty, MNHi, TPReg, AddrAdd),
529       0);
530   return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNAdd, AddrLo), 0);
531 }
532
533 SDValue RISCVTargetLowering::getDynamicTLSAddr(GlobalAddressSDNode *N,
534                                                SelectionDAG &DAG) const {
535   SDLoc DL(N);
536   EVT Ty = getPointerTy(DAG.getDataLayout());
537   IntegerType *CallTy = Type::getIntNTy(*DAG.getContext(), Ty.getSizeInBits());
538   const GlobalValue *GV = N->getGlobal();
539
540   // Use a PC-relative addressing mode to access the global dynamic GOT address.
541   // This generates the pattern (PseudoLA_TLS_GD sym), which expands to
542   // (addi (auipc %tls_gd_pcrel_hi(sym)) %pcrel_lo(auipc)).
543   SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
544   SDValue Load =
545       SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_GD, DL, Ty, Addr), 0);
546
547   // Prepare argument list to generate call.
548   ArgListTy Args;
549   ArgListEntry Entry;
550   Entry.Node = Load;
551   Entry.Ty = CallTy;
552   Args.push_back(Entry);
553
554   // Setup call to __tls_get_addr.
555   TargetLowering::CallLoweringInfo CLI(DAG);
556   CLI.setDebugLoc(DL)
557       .setChain(DAG.getEntryNode())
558       .setLibCallee(CallingConv::C, CallTy,
559                     DAG.getExternalSymbol("__tls_get_addr", Ty),
560                     std::move(Args));
561
562   return LowerCallTo(CLI).first;
563 }
564
565 SDValue RISCVTargetLowering::lowerGlobalTLSAddress(SDValue Op,
566                                                    SelectionDAG &DAG) const {
567   SDLoc DL(Op);
568   EVT Ty = Op.getValueType();
569   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
570   int64_t Offset = N->getOffset();
571   MVT XLenVT = Subtarget.getXLenVT();
572
573   TLSModel::Model Model = getTargetMachine().getTLSModel(N->getGlobal());
574
575   SDValue Addr;
576   switch (Model) {
577   case TLSModel::LocalExec:
578     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/false);
579     break;
580   case TLSModel::InitialExec:
581     Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/true);
582     break;
583   case TLSModel::LocalDynamic:
584   case TLSModel::GeneralDynamic:
585     Addr = getDynamicTLSAddr(N, DAG);
586     break;
587   }
588
589   // In order to maximise the opportunity for common subexpression elimination,
590   // emit a separate ADD node for the global address offset instead of folding
591   // it in the global address node. Later peephole optimisations may choose to
592   // fold it back in when profitable.
593   if (Offset != 0)
594     return DAG.getNode(ISD::ADD, DL, Ty, Addr,
595                        DAG.getConstant(Offset, DL, XLenVT));
596   return Addr;
597 }
598
599 SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
600   SDValue CondV = Op.getOperand(0);
601   SDValue TrueV = Op.getOperand(1);
602   SDValue FalseV = Op.getOperand(2);
603   SDLoc DL(Op);
604   MVT XLenVT = Subtarget.getXLenVT();
605
606   // If the result type is XLenVT and CondV is the output of a SETCC node
607   // which also operated on XLenVT inputs, then merge the SETCC node into the
608   // lowered RISCVISD::SELECT_CC to take advantage of the integer
609   // compare+branch instructions. i.e.:
610   // (select (setcc lhs, rhs, cc), truev, falsev)
611   // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
612   if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
613       CondV.getOperand(0).getSimpleValueType() == XLenVT) {
614     SDValue LHS = CondV.getOperand(0);
615     SDValue RHS = CondV.getOperand(1);
616     auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
617     ISD::CondCode CCVal = CC->get();
618
619     normaliseSetCC(LHS, RHS, CCVal);
620
621     SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
622     SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
623     SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
624     return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
625   }
626
627   // Otherwise:
628   // (select condv, truev, falsev)
629   // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
630   SDValue Zero = DAG.getConstant(0, DL, XLenVT);
631   SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
632
633   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
634   SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
635
636   return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
637 }
638
639 SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
640   MachineFunction &MF = DAG.getMachineFunction();
641   RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
642
643   SDLoc DL(Op);
644   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
645                                  getPointerTy(MF.getDataLayout()));
646
647   // vastart just stores the address of the VarArgsFrameIndex slot into the
648   // memory location argument.
649   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
650   return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
651                       MachinePointerInfo(SV));
652 }
653
654 SDValue RISCVTargetLowering::lowerFRAMEADDR(SDValue Op,
655                                             SelectionDAG &DAG) const {
656   const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
657   MachineFunction &MF = DAG.getMachineFunction();
658   MachineFrameInfo &MFI = MF.getFrameInfo();
659   MFI.setFrameAddressIsTaken(true);
660   unsigned FrameReg = RI.getFrameRegister(MF);
661   int XLenInBytes = Subtarget.getXLen() / 8;
662
663   EVT VT = Op.getValueType();
664   SDLoc DL(Op);
665   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
666   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
667   while (Depth--) {
668     int Offset = -(XLenInBytes * 2);
669     SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
670                               DAG.getIntPtrConstant(Offset, DL));
671     FrameAddr =
672         DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
673   }
674   return FrameAddr;
675 }
676
677 SDValue RISCVTargetLowering::lowerRETURNADDR(SDValue Op,
678                                              SelectionDAG &DAG) const {
679   const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
680   MachineFunction &MF = DAG.getMachineFunction();
681   MachineFrameInfo &MFI = MF.getFrameInfo();
682   MFI.setReturnAddressIsTaken(true);
683   MVT XLenVT = Subtarget.getXLenVT();
684   int XLenInBytes = Subtarget.getXLen() / 8;
685
686   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
687     return SDValue();
688
689   EVT VT = Op.getValueType();
690   SDLoc DL(Op);
691   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
692   if (Depth) {
693     int Off = -XLenInBytes;
694     SDValue FrameAddr = lowerFRAMEADDR(Op, DAG);
695     SDValue Offset = DAG.getConstant(Off, DL, VT);
696     return DAG.getLoad(VT, DL, DAG.getEntryNode(),
697                        DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
698                        MachinePointerInfo());
699   }
700
701   // Return the value of the return address register, marking it an implicit
702   // live-in.
703   unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
704   return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
705 }
706
707 SDValue RISCVTargetLowering::lowerShiftLeftParts(SDValue Op,
708                                                  SelectionDAG &DAG) const {
709   SDLoc DL(Op);
710   SDValue Lo = Op.getOperand(0);
711   SDValue Hi = Op.getOperand(1);
712   SDValue Shamt = Op.getOperand(2);
713   EVT VT = Lo.getValueType();
714
715   // if Shamt-XLEN < 0: // Shamt < XLEN
716   //   Lo = Lo << Shamt
717   //   Hi = (Hi << Shamt) | ((Lo >>u 1) >>u (XLEN-1 - Shamt))
718   // else:
719   //   Lo = 0
720   //   Hi = Lo << (Shamt-XLEN)
721
722   SDValue Zero = DAG.getConstant(0, DL, VT);
723   SDValue One = DAG.getConstant(1, DL, VT);
724   SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
725   SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
726   SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
727   SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
728
729   SDValue LoTrue = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
730   SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo, One);
731   SDValue ShiftRightLo =
732       DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, XLenMinus1Shamt);
733   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
734   SDValue HiTrue = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
735   SDValue HiFalse = DAG.getNode(ISD::SHL, DL, VT, Lo, ShamtMinusXLen);
736
737   SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
738
739   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, Zero);
740   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
741
742   SDValue Parts[2] = {Lo, Hi};
743   return DAG.getMergeValues(Parts, DL);
744 }
745
746 SDValue RISCVTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
747                                                   bool IsSRA) const {
748   SDLoc DL(Op);
749   SDValue Lo = Op.getOperand(0);
750   SDValue Hi = Op.getOperand(1);
751   SDValue Shamt = Op.getOperand(2);
752   EVT VT = Lo.getValueType();
753
754   // SRA expansion:
755   //   if Shamt-XLEN < 0: // Shamt < XLEN
756   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
757   //     Hi = Hi >>s Shamt
758   //   else:
759   //     Lo = Hi >>s (Shamt-XLEN);
760   //     Hi = Hi >>s (XLEN-1)
761   //
762   // SRL expansion:
763   //   if Shamt-XLEN < 0: // Shamt < XLEN
764   //     Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
765   //     Hi = Hi >>u Shamt
766   //   else:
767   //     Lo = Hi >>u (Shamt-XLEN);
768   //     Hi = 0;
769
770   unsigned ShiftRightOp = IsSRA ? ISD::SRA : ISD::SRL;
771
772   SDValue Zero = DAG.getConstant(0, DL, VT);
773   SDValue One = DAG.getConstant(1, DL, VT);
774   SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
775   SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
776   SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
777   SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
778
779   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
780   SDValue ShiftLeftHi1 = DAG.getNode(ISD::SHL, DL, VT, Hi, One);
781   SDValue ShiftLeftHi =
782       DAG.getNode(ISD::SHL, DL, VT, ShiftLeftHi1, XLenMinus1Shamt);
783   SDValue LoTrue = DAG.getNode(ISD::OR, DL, VT, ShiftRightLo, ShiftLeftHi);
784   SDValue HiTrue = DAG.getNode(ShiftRightOp, DL, VT, Hi, Shamt);
785   SDValue LoFalse = DAG.getNode(ShiftRightOp, DL, VT, Hi, ShamtMinusXLen);
786   SDValue HiFalse =
787       IsSRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, XLenMinus1) : Zero;
788
789   SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
790
791   Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, LoFalse);
792   Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
793
794   SDValue Parts[2] = {Lo, Hi};
795   return DAG.getMergeValues(Parts, DL);
796 }
797
798 // Returns the opcode of the target-specific SDNode that implements the 32-bit
799 // form of the given Opcode.
800 static RISCVISD::NodeType getRISCVWOpcode(unsigned Opcode) {
801   switch (Opcode) {
802   default:
803     llvm_unreachable("Unexpected opcode");
804   case ISD::SHL:
805     return RISCVISD::SLLW;
806   case ISD::SRA:
807     return RISCVISD::SRAW;
808   case ISD::SRL:
809     return RISCVISD::SRLW;
810   case ISD::SDIV:
811     return RISCVISD::DIVW;
812   case ISD::UDIV:
813     return RISCVISD::DIVUW;
814   case ISD::UREM:
815     return RISCVISD::REMUW;
816   }
817 }
818
819 // Converts the given 32-bit operation to a target-specific SelectionDAG node.
820 // Because i32 isn't a legal type for RV64, these operations would otherwise
821 // be promoted to i64, making it difficult to select the SLLW/DIVUW/.../*W
822 // later one because the fact the operation was originally of type i32 is
823 // lost.
824 static SDValue customLegalizeToWOp(SDNode *N, SelectionDAG &DAG) {
825   SDLoc DL(N);
826   RISCVISD::NodeType WOpcode = getRISCVWOpcode(N->getOpcode());
827   SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
828   SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
829   SDValue NewRes = DAG.getNode(WOpcode, DL, MVT::i64, NewOp0, NewOp1);
830   // ReplaceNodeResults requires we maintain the same type for the return value.
831   return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes);
832 }
833
834 void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
835                                              SmallVectorImpl<SDValue> &Results,
836                                              SelectionDAG &DAG) const {
837   SDLoc DL(N);
838   switch (N->getOpcode()) {
839   default:
840     llvm_unreachable("Don't know how to custom type legalize this operation!");
841   case ISD::READCYCLECOUNTER: {
842     assert(!Subtarget.is64Bit() &&
843            "READCYCLECOUNTER only has custom type legalization on riscv32");
844
845     SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
846     SDValue RCW =
847         DAG.getNode(RISCVISD::READ_CYCLE_WIDE, DL, VTs, N->getOperand(0));
848
849     Results.push_back(RCW);
850     Results.push_back(RCW.getValue(1));
851     Results.push_back(RCW.getValue(2));
852     break;
853   }
854   case ISD::SHL:
855   case ISD::SRA:
856   case ISD::SRL:
857     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
858            "Unexpected custom legalisation");
859     if (N->getOperand(1).getOpcode() == ISD::Constant)
860       return;
861     Results.push_back(customLegalizeToWOp(N, DAG));
862     break;
863   case ISD::SDIV:
864   case ISD::UDIV:
865   case ISD::UREM:
866     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
867            Subtarget.hasStdExtM() && "Unexpected custom legalisation");
868     if (N->getOperand(0).getOpcode() == ISD::Constant ||
869         N->getOperand(1).getOpcode() == ISD::Constant)
870       return;
871     Results.push_back(customLegalizeToWOp(N, DAG));
872     break;
873   case ISD::BITCAST: {
874     assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
875            Subtarget.hasStdExtF() && "Unexpected custom legalisation");
876     SDLoc DL(N);
877     SDValue Op0 = N->getOperand(0);
878     if (Op0.getValueType() != MVT::f32)
879       return;
880     SDValue FPConv =
881         DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Op0);
882     Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, FPConv));
883     break;
884   }
885   }
886 }
887
888 SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
889                                                DAGCombinerInfo &DCI) const {
890   SelectionDAG &DAG = DCI.DAG;
891
892   switch (N->getOpcode()) {
893   default:
894     break;
895   case RISCVISD::SplitF64: {
896     SDValue Op0 = N->getOperand(0);
897     // If the input to SplitF64 is just BuildPairF64 then the operation is
898     // redundant. Instead, use BuildPairF64's operands directly.
899     if (Op0->getOpcode() == RISCVISD::BuildPairF64)
900       return DCI.CombineTo(N, Op0.getOperand(0), Op0.getOperand(1));
901
902     SDLoc DL(N);
903
904     // It's cheaper to materialise two 32-bit integers than to load a double
905     // from the constant pool and transfer it to integer registers through the
906     // stack.
907     if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op0)) {
908       APInt V = C->getValueAPF().bitcastToAPInt();
909       SDValue Lo = DAG.getConstant(V.trunc(32), DL, MVT::i32);
910       SDValue Hi = DAG.getConstant(V.lshr(32).trunc(32), DL, MVT::i32);
911       return DCI.CombineTo(N, Lo, Hi);
912     }
913
914     // This is a target-specific version of a DAGCombine performed in
915     // DAGCombiner::visitBITCAST. It performs the equivalent of:
916     // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
917     // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
918     if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
919         !Op0.getNode()->hasOneUse())
920       break;
921     SDValue NewSplitF64 =
922         DAG.getNode(RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32),
923                     Op0.getOperand(0));
924     SDValue Lo = NewSplitF64.getValue(0);
925     SDValue Hi = NewSplitF64.getValue(1);
926     APInt SignBit = APInt::getSignMask(32);
927     if (Op0.getOpcode() == ISD::FNEG) {
928       SDValue NewHi = DAG.getNode(ISD::XOR, DL, MVT::i32, Hi,
929                                   DAG.getConstant(SignBit, DL, MVT::i32));
930       return DCI.CombineTo(N, Lo, NewHi);
931     }
932     assert(Op0.getOpcode() == ISD::FABS);
933     SDValue NewHi = DAG.getNode(ISD::AND, DL, MVT::i32, Hi,
934                                 DAG.getConstant(~SignBit, DL, MVT::i32));
935     return DCI.CombineTo(N, Lo, NewHi);
936   }
937   case RISCVISD::SLLW:
938   case RISCVISD::SRAW:
939   case RISCVISD::SRLW: {
940     // Only the lower 32 bits of LHS and lower 5 bits of RHS are read.
941     SDValue LHS = N->getOperand(0);
942     SDValue RHS = N->getOperand(1);
943     APInt LHSMask = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 32);
944     APInt RHSMask = APInt::getLowBitsSet(RHS.getValueSizeInBits(), 5);
945     if ((SimplifyDemandedBits(N->getOperand(0), LHSMask, DCI)) ||
946         (SimplifyDemandedBits(N->getOperand(1), RHSMask, DCI)))
947       return SDValue();
948     break;
949   }
950   case RISCVISD::FMV_X_ANYEXTW_RV64: {
951     SDLoc DL(N);
952     SDValue Op0 = N->getOperand(0);
953     // If the input to FMV_X_ANYEXTW_RV64 is just FMV_W_X_RV64 then the
954     // conversion is unnecessary and can be replaced with an ANY_EXTEND
955     // of the FMV_W_X_RV64 operand.
956     if (Op0->getOpcode() == RISCVISD::FMV_W_X_RV64) {
957       SDValue AExtOp =
958           DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0.getOperand(0));
959       return DCI.CombineTo(N, AExtOp);
960     }
961
962     // This is a target-specific version of a DAGCombine performed in
963     // DAGCombiner::visitBITCAST. It performs the equivalent of:
964     // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
965     // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
966     if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
967         !Op0.getNode()->hasOneUse())
968       break;
969     SDValue NewFMV = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64,
970                                  Op0.getOperand(0));
971     APInt SignBit = APInt::getSignMask(32).sext(64);
972     if (Op0.getOpcode() == ISD::FNEG) {
973       return DCI.CombineTo(N,
974                            DAG.getNode(ISD::XOR, DL, MVT::i64, NewFMV,
975                                        DAG.getConstant(SignBit, DL, MVT::i64)));
976     }
977     assert(Op0.getOpcode() == ISD::FABS);
978     return DCI.CombineTo(N,
979                          DAG.getNode(ISD::AND, DL, MVT::i64, NewFMV,
980                                      DAG.getConstant(~SignBit, DL, MVT::i64)));
981   }
982   }
983
984   return SDValue();
985 }
986
987 bool RISCVTargetLowering::isDesirableToCommuteWithShift(
988     const SDNode *N, CombineLevel Level) const {
989   // The following folds are only desirable if `(OP _, c1 << c2)` can be
990   // materialised in fewer instructions than `(OP _, c1)`:
991   //
992   //   (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
993   //   (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
994   SDValue N0 = N->getOperand(0);
995   EVT Ty = N0.getValueType();
996   if (Ty.isScalarInteger() &&
997       (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::OR)) {
998     auto *C1 = dyn_cast<ConstantSDNode>(N0->getOperand(1));
999     auto *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1));
1000     if (C1 && C2) {
1001       APInt C1Int = C1->getAPIntValue();
1002       APInt ShiftedC1Int = C1Int << C2->getAPIntValue();
1003
1004       // We can materialise `c1 << c2` into an add immediate, so it's "free",
1005       // and the combine should happen, to potentially allow further combines
1006       // later.
1007       if (ShiftedC1Int.getMinSignedBits() <= 64 &&
1008           isLegalAddImmediate(ShiftedC1Int.getSExtValue()))
1009         return true;
1010
1011       // We can materialise `c1` in an add immediate, so it's "free", and the
1012       // combine should be prevented.
1013       if (C1Int.getMinSignedBits() <= 64 &&
1014           isLegalAddImmediate(C1Int.getSExtValue()))
1015         return false;
1016
1017       // Neither constant will fit into an immediate, so find materialisation
1018       // costs.
1019       int C1Cost = RISCVMatInt::getIntMatCost(C1Int, Ty.getSizeInBits(),
1020                                               Subtarget.is64Bit());
1021       int ShiftedC1Cost = RISCVMatInt::getIntMatCost(
1022           ShiftedC1Int, Ty.getSizeInBits(), Subtarget.is64Bit());
1023
1024       // Materialising `c1` is cheaper than materialising `c1 << c2`, so the
1025       // combine should be prevented.
1026       if (C1Cost < ShiftedC1Cost)
1027         return false;
1028     }
1029   }
1030   return true;
1031 }
1032
1033 unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
1034     SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
1035     unsigned Depth) const {
1036   switch (Op.getOpcode()) {
1037   default:
1038     break;
1039   case RISCVISD::SLLW:
1040   case RISCVISD::SRAW:
1041   case RISCVISD::SRLW:
1042   case RISCVISD::DIVW:
1043   case RISCVISD::DIVUW:
1044   case RISCVISD::REMUW:
1045     // TODO: As the result is sign-extended, this is conservatively correct. A
1046     // more precise answer could be calculated for SRAW depending on known
1047     // bits in the shift amount.
1048     return 33;
1049   }
1050
1051   return 1;
1052 }
1053
1054 MachineBasicBlock *emitReadCycleWidePseudo(MachineInstr &MI,
1055                                            MachineBasicBlock *BB) {
1056   assert(MI.getOpcode() == RISCV::ReadCycleWide && "Unexpected instruction");
1057
1058   // To read the 64-bit cycle CSR on a 32-bit target, we read the two halves.
1059   // Should the count have wrapped while it was being read, we need to try
1060   // again.
1061   // ...
1062   // read:
1063   // rdcycleh x3 # load high word of cycle
1064   // rdcycle  x2 # load low word of cycle
1065   // rdcycleh x4 # load high word of cycle
1066   // bne x3, x4, read # check if high word reads match, otherwise try again
1067   // ...
1068
1069   MachineFunction &MF = *BB->getParent();
1070   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1071   MachineFunction::iterator It = ++BB->getIterator();
1072
1073   MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB);
1074   MF.insert(It, LoopMBB);
1075
1076   MachineBasicBlock *DoneMBB = MF.CreateMachineBasicBlock(LLVM_BB);
1077   MF.insert(It, DoneMBB);
1078
1079   // Transfer the remainder of BB and its successor edges to DoneMBB.
1080   DoneMBB->splice(DoneMBB->begin(), BB,
1081                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
1082   DoneMBB->transferSuccessorsAndUpdatePHIs(BB);
1083
1084   BB->addSuccessor(LoopMBB);
1085
1086   MachineRegisterInfo &RegInfo = MF.getRegInfo();
1087   unsigned ReadAgainReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
1088   unsigned LoReg = MI.getOperand(0).getReg();
1089   unsigned HiReg = MI.getOperand(1).getReg();
1090   DebugLoc DL = MI.getDebugLoc();
1091
1092   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
1093   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), HiReg)
1094       .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
1095       .addReg(RISCV::X0);
1096   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), LoReg)
1097       .addImm(RISCVSysReg::lookupSysRegByName("CYCLE")->Encoding)
1098       .addReg(RISCV::X0);
1099   BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), ReadAgainReg)
1100       .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
1101       .addReg(RISCV::X0);
1102
1103   BuildMI(LoopMBB, DL, TII->get(RISCV::BNE))
1104       .addReg(HiReg)
1105       .addReg(ReadAgainReg)
1106       .addMBB(LoopMBB);
1107
1108   LoopMBB->addSuccessor(LoopMBB);
1109   LoopMBB->addSuccessor(DoneMBB);
1110
1111   MI.eraseFromParent();
1112
1113   return DoneMBB;
1114 }
1115
1116 static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
1117                                              MachineBasicBlock *BB) {
1118   assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
1119
1120   MachineFunction &MF = *BB->getParent();
1121   DebugLoc DL = MI.getDebugLoc();
1122   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1123   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
1124   unsigned LoReg = MI.getOperand(0).getReg();
1125   unsigned HiReg = MI.getOperand(1).getReg();
1126   unsigned SrcReg = MI.getOperand(2).getReg();
1127   const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
1128   int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
1129
1130   TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
1131                           RI);
1132   MachineMemOperand *MMO =
1133       MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
1134                               MachineMemOperand::MOLoad, 8, 8);
1135   BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
1136       .addFrameIndex(FI)
1137       .addImm(0)
1138       .addMemOperand(MMO);
1139   BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
1140       .addFrameIndex(FI)
1141       .addImm(4)
1142       .addMemOperand(MMO);
1143   MI.eraseFromParent(); // The pseudo instruction is gone now.
1144   return BB;
1145 }
1146
1147 static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
1148                                                  MachineBasicBlock *BB) {
1149   assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
1150          "Unexpected instruction");
1151
1152   MachineFunction &MF = *BB->getParent();
1153   DebugLoc DL = MI.getDebugLoc();
1154   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1155   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
1156   unsigned DstReg = MI.getOperand(0).getReg();
1157   unsigned LoReg = MI.getOperand(1).getReg();
1158   unsigned HiReg = MI.getOperand(2).getReg();
1159   const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
1160   int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
1161
1162   MachineMemOperand *MMO =
1163       MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
1164                               MachineMemOperand::MOStore, 8, 8);
1165   BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
1166       .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
1167       .addFrameIndex(FI)
1168       .addImm(0)
1169       .addMemOperand(MMO);
1170   BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
1171       .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
1172       .addFrameIndex(FI)
1173       .addImm(4)
1174       .addMemOperand(MMO);
1175   TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
1176   MI.eraseFromParent(); // The pseudo instruction is gone now.
1177   return BB;
1178 }
1179
1180 static bool isSelectPseudo(MachineInstr &MI) {
1181   switch (MI.getOpcode()) {
1182   default:
1183     return false;
1184   case RISCV::Select_GPR_Using_CC_GPR:
1185   case RISCV::Select_FPR32_Using_CC_GPR:
1186   case RISCV::Select_FPR64_Using_CC_GPR:
1187     return true;
1188   }
1189 }
1190
1191 static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
1192                                            MachineBasicBlock *BB) {
1193   // To "insert" Select_* instructions, we actually have to insert the triangle
1194   // control-flow pattern.  The incoming instructions know the destination vreg
1195   // to set, the condition code register to branch on, the true/false values to
1196   // select between, and the condcode to use to select the appropriate branch.
1197   //
1198   // We produce the following control flow:
1199   //     HeadMBB
1200   //     |  \
1201   //     |  IfFalseMBB
1202   //     | /
1203   //    TailMBB
1204   //
1205   // When we find a sequence of selects we attempt to optimize their emission
1206   // by sharing the control flow. Currently we only handle cases where we have
1207   // multiple selects with the exact same condition (same LHS, RHS and CC).
1208   // The selects may be interleaved with other instructions if the other
1209   // instructions meet some requirements we deem safe:
1210   // - They are debug instructions. Otherwise,
1211   // - They do not have side-effects, do not access memory and their inputs do
1212   //   not depend on the results of the select pseudo-instructions.
1213   // The TrueV/FalseV operands of the selects cannot depend on the result of
1214   // previous selects in the sequence.
1215   // These conditions could be further relaxed. See the X86 target for a
1216   // related approach and more information.
1217   unsigned LHS = MI.getOperand(1).getReg();
1218   unsigned RHS = MI.getOperand(2).getReg();
1219   auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
1220
1221   SmallVector<MachineInstr *, 4> SelectDebugValues;
1222   SmallSet<unsigned, 4> SelectDests;
1223   SelectDests.insert(MI.getOperand(0).getReg());
1224
1225   MachineInstr *LastSelectPseudo = &MI;
1226
1227   for (auto E = BB->end(), SequenceMBBI = MachineBasicBlock::iterator(MI);
1228        SequenceMBBI != E; ++SequenceMBBI) {
1229     if (SequenceMBBI->isDebugInstr())
1230       continue;
1231     else if (isSelectPseudo(*SequenceMBBI)) {
1232       if (SequenceMBBI->getOperand(1).getReg() != LHS ||
1233           SequenceMBBI->getOperand(2).getReg() != RHS ||
1234           SequenceMBBI->getOperand(3).getImm() != CC ||
1235           SelectDests.count(SequenceMBBI->getOperand(4).getReg()) ||
1236           SelectDests.count(SequenceMBBI->getOperand(5).getReg()))
1237         break;
1238       LastSelectPseudo = &*SequenceMBBI;
1239       SequenceMBBI->collectDebugValues(SelectDebugValues);
1240       SelectDests.insert(SequenceMBBI->getOperand(0).getReg());
1241     } else {
1242       if (SequenceMBBI->hasUnmodeledSideEffects() ||
1243           SequenceMBBI->mayLoadOrStore())
1244         break;
1245       if (llvm::any_of(SequenceMBBI->operands(), [&](MachineOperand &MO) {
1246             return MO.isReg() && MO.isUse() && SelectDests.count(MO.getReg());
1247           }))
1248         break;
1249     }
1250   }
1251
1252   const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
1253   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1254   DebugLoc DL = MI.getDebugLoc();
1255   MachineFunction::iterator I = ++BB->getIterator();
1256
1257   MachineBasicBlock *HeadMBB = BB;
1258   MachineFunction *F = BB->getParent();
1259   MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
1260   MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
1261
1262   F->insert(I, IfFalseMBB);
1263   F->insert(I, TailMBB);
1264
1265   // Transfer debug instructions associated with the selects to TailMBB.
1266   for (MachineInstr *DebugInstr : SelectDebugValues) {
1267     TailMBB->push_back(DebugInstr->removeFromParent());
1268   }
1269
1270   // Move all instructions after the sequence to TailMBB.
1271   TailMBB->splice(TailMBB->end(), HeadMBB,
1272                   std::next(LastSelectPseudo->getIterator()), HeadMBB->end());
1273   // Update machine-CFG edges by transferring all successors of the current
1274   // block to the new block which will contain the Phi nodes for the selects.
1275   TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
1276   // Set the successors for HeadMBB.
1277   HeadMBB->addSuccessor(IfFalseMBB);
1278   HeadMBB->addSuccessor(TailMBB);
1279
1280   // Insert appropriate branch.
1281   unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
1282
1283   BuildMI(HeadMBB, DL, TII.get(Opcode))
1284     .addReg(LHS)
1285     .addReg(RHS)
1286     .addMBB(TailMBB);
1287
1288   // IfFalseMBB just falls through to TailMBB.
1289   IfFalseMBB->addSuccessor(TailMBB);
1290
1291   // Create PHIs for all of the select pseudo-instructions.
1292   auto SelectMBBI = MI.getIterator();
1293   auto SelectEnd = std::next(LastSelectPseudo->getIterator());
1294   auto InsertionPoint = TailMBB->begin();
1295   while (SelectMBBI != SelectEnd) {
1296     auto Next = std::next(SelectMBBI);
1297     if (isSelectPseudo(*SelectMBBI)) {
1298       // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
1299       BuildMI(*TailMBB, InsertionPoint, SelectMBBI->getDebugLoc(),
1300               TII.get(RISCV::PHI), SelectMBBI->getOperand(0).getReg())
1301           .addReg(SelectMBBI->getOperand(4).getReg())
1302           .addMBB(HeadMBB)
1303           .addReg(SelectMBBI->getOperand(5).getReg())
1304           .addMBB(IfFalseMBB);
1305       SelectMBBI->eraseFromParent();
1306     }
1307     SelectMBBI = Next;
1308   }
1309
1310   F->getProperties().reset(MachineFunctionProperties::Property::NoPHIs);
1311   return TailMBB;
1312 }
1313
1314 MachineBasicBlock *
1315 RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
1316                                                  MachineBasicBlock *BB) const {
1317   switch (MI.getOpcode()) {
1318   default:
1319     llvm_unreachable("Unexpected instr type to insert");
1320   case RISCV::ReadCycleWide:
1321     assert(!Subtarget.is64Bit() &&
1322            "ReadCycleWrite is only to be used on riscv32");
1323     return emitReadCycleWidePseudo(MI, BB);
1324   case RISCV::Select_GPR_Using_CC_GPR:
1325   case RISCV::Select_FPR32_Using_CC_GPR:
1326   case RISCV::Select_FPR64_Using_CC_GPR:
1327     return emitSelectPseudo(MI, BB);
1328   case RISCV::BuildPairF64Pseudo:
1329     return emitBuildPairF64Pseudo(MI, BB);
1330   case RISCV::SplitF64Pseudo:
1331     return emitSplitF64Pseudo(MI, BB);
1332   }
1333 }
1334
1335 // Calling Convention Implementation.
1336 // The expectations for frontend ABI lowering vary from target to target.
1337 // Ideally, an LLVM frontend would be able to avoid worrying about many ABI
1338 // details, but this is a longer term goal. For now, we simply try to keep the
1339 // role of the frontend as simple and well-defined as possible. The rules can
1340 // be summarised as:
1341 // * Never split up large scalar arguments. We handle them here.
1342 // * If a hardfloat calling convention is being used, and the struct may be
1343 // passed in a pair of registers (fp+fp, int+fp), and both registers are
1344 // available, then pass as two separate arguments. If either the GPRs or FPRs
1345 // are exhausted, then pass according to the rule below.
1346 // * If a struct could never be passed in registers or directly in a stack
1347 // slot (as it is larger than 2*XLEN and the floating point rules don't
1348 // apply), then pass it using a pointer with the byval attribute.
1349 // * If a struct is less than 2*XLEN, then coerce to either a two-element
1350 // word-sized array or a 2*XLEN scalar (depending on alignment).
1351 // * The frontend can determine whether a struct is returned by reference or
1352 // not based on its size and fields. If it will be returned by reference, the
1353 // frontend must modify the prototype so a pointer with the sret annotation is
1354 // passed as the first argument. This is not necessary for large scalar
1355 // returns.
1356 // * Struct return values and varargs should be coerced to structs containing
1357 // register-size fields in the same situations they would be for fixed
1358 // arguments.
1359
1360 static const MCPhysReg ArgGPRs[] = {
1361   RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
1362   RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
1363 };
1364 static const MCPhysReg ArgFPR32s[] = {
1365   RISCV::F10_32, RISCV::F11_32, RISCV::F12_32, RISCV::F13_32,
1366   RISCV::F14_32, RISCV::F15_32, RISCV::F16_32, RISCV::F17_32
1367 };
1368 static const MCPhysReg ArgFPR64s[] = {
1369   RISCV::F10_64, RISCV::F11_64, RISCV::F12_64, RISCV::F13_64,
1370   RISCV::F14_64, RISCV::F15_64, RISCV::F16_64, RISCV::F17_64
1371 };
1372
1373 // Pass a 2*XLEN argument that has been split into two XLEN values through
1374 // registers or the stack as necessary.
1375 static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
1376                                 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
1377                                 MVT ValVT2, MVT LocVT2,
1378                                 ISD::ArgFlagsTy ArgFlags2) {
1379   unsigned XLenInBytes = XLen / 8;
1380   if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
1381     // At least one half can be passed via register.
1382     State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
1383                                      VA1.getLocVT(), CCValAssign::Full));
1384   } else {
1385     // Both halves must be passed on the stack, with proper alignment.
1386     unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
1387     State.addLoc(
1388         CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
1389                             State.AllocateStack(XLenInBytes, StackAlign),
1390                             VA1.getLocVT(), CCValAssign::Full));
1391     State.addLoc(CCValAssign::getMem(
1392         ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
1393         CCValAssign::Full));
1394     return false;
1395   }
1396
1397   if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
1398     // The second half can also be passed via register.
1399     State.addLoc(
1400         CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
1401   } else {
1402     // The second half is passed via the stack, without additional alignment.
1403     State.addLoc(CCValAssign::getMem(
1404         ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
1405         CCValAssign::Full));
1406   }
1407
1408   return false;
1409 }
1410
1411 // Implements the RISC-V calling convention. Returns true upon failure.
1412 static bool CC_RISCV(const DataLayout &DL, RISCVABI::ABI ABI, unsigned ValNo,
1413                      MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo,
1414                      ISD::ArgFlagsTy ArgFlags, CCState &State, bool IsFixed,
1415                      bool IsRet, Type *OrigTy) {
1416   unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
1417   assert(XLen == 32 || XLen == 64);
1418   MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
1419
1420   // Any return value split in to more than two values can't be returned
1421   // directly.
1422   if (IsRet && ValNo > 1)
1423     return true;
1424
1425   // UseGPRForF32 if targeting one of the soft-float ABIs, if passing a
1426   // variadic argument, or if no F32 argument registers are available.
1427   bool UseGPRForF32 = true;
1428   // UseGPRForF64 if targeting soft-float ABIs or an FLEN=32 ABI, if passing a
1429   // variadic argument, or if no F64 argument registers are available.
1430   bool UseGPRForF64 = true;
1431
1432   switch (ABI) {
1433   default:
1434     llvm_unreachable("Unexpected ABI");
1435   case RISCVABI::ABI_ILP32:
1436   case RISCVABI::ABI_LP64:
1437     break;
1438   case RISCVABI::ABI_ILP32F:
1439   case RISCVABI::ABI_LP64F:
1440     UseGPRForF32 = !IsFixed;
1441     break;
1442   case RISCVABI::ABI_ILP32D:
1443   case RISCVABI::ABI_LP64D:
1444     UseGPRForF32 = !IsFixed;
1445     UseGPRForF64 = !IsFixed;
1446     break;
1447   }
1448
1449   if (State.getFirstUnallocated(ArgFPR32s) == array_lengthof(ArgFPR32s))
1450     UseGPRForF32 = true;
1451   if (State.getFirstUnallocated(ArgFPR64s) == array_lengthof(ArgFPR64s))
1452     UseGPRForF64 = true;
1453
1454   // From this point on, rely on UseGPRForF32, UseGPRForF64 and similar local
1455   // variables rather than directly checking against the target ABI.
1456
1457   if (UseGPRForF32 && ValVT == MVT::f32) {
1458     LocVT = XLenVT;
1459     LocInfo = CCValAssign::BCvt;
1460   } else if (UseGPRForF64 && XLen == 64 && ValVT == MVT::f64) {
1461     LocVT = MVT::i64;
1462     LocInfo = CCValAssign::BCvt;
1463   }
1464
1465   // If this is a variadic argument, the RISC-V calling convention requires
1466   // that it is assigned an 'even' or 'aligned' register if it has 8-byte
1467   // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
1468   // be used regardless of whether the original argument was split during
1469   // legalisation or not. The argument will not be passed by registers if the
1470   // original type is larger than 2*XLEN, so the register alignment rule does
1471   // not apply.
1472   unsigned TwoXLenInBytes = (2 * XLen) / 8;
1473   if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
1474       DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
1475     unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
1476     // Skip 'odd' register if necessary.
1477     if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
1478       State.AllocateReg(ArgGPRs);
1479   }
1480
1481   SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
1482   SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
1483       State.getPendingArgFlags();
1484
1485   assert(PendingLocs.size() == PendingArgFlags.size() &&
1486          "PendingLocs and PendingArgFlags out of sync");
1487
1488   // Handle passing f64 on RV32D with a soft float ABI or when floating point
1489   // registers are exhausted.
1490   if (UseGPRForF64 && XLen == 32 && ValVT == MVT::f64) {
1491     assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
1492            "Can't lower f64 if it is split");
1493     // Depending on available argument GPRS, f64 may be passed in a pair of
1494     // GPRs, split between a GPR and the stack, or passed completely on the
1495     // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
1496     // cases.
1497     unsigned Reg = State.AllocateReg(ArgGPRs);
1498     LocVT = MVT::i32;
1499     if (!Reg) {
1500       unsigned StackOffset = State.AllocateStack(8, 8);
1501       State.addLoc(
1502           CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
1503       return false;
1504     }
1505     if (!State.AllocateReg(ArgGPRs))
1506       State.AllocateStack(4, 4);
1507     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1508     return false;
1509   }
1510
1511   // Split arguments might be passed indirectly, so keep track of the pending
1512   // values.
1513   if (ArgFlags.isSplit() || !PendingLocs.empty()) {
1514     LocVT = XLenVT;
1515     LocInfo = CCValAssign::Indirect;
1516     PendingLocs.push_back(
1517         CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
1518     PendingArgFlags.push_back(ArgFlags);
1519     if (!ArgFlags.isSplitEnd()) {
1520       return false;
1521     }
1522   }
1523
1524   // If the split argument only had two elements, it should be passed directly
1525   // in registers or on the stack.
1526   if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
1527     assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
1528     // Apply the normal calling convention rules to the first half of the
1529     // split argument.
1530     CCValAssign VA = PendingLocs[0];
1531     ISD::ArgFlagsTy AF = PendingArgFlags[0];
1532     PendingLocs.clear();
1533     PendingArgFlags.clear();
1534     return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
1535                                ArgFlags);
1536   }
1537
1538   // Allocate to a register if possible, or else a stack slot.
1539   unsigned Reg;
1540   if (ValVT == MVT::f32 && !UseGPRForF32)
1541     Reg = State.AllocateReg(ArgFPR32s, ArgFPR64s);
1542   else if (ValVT == MVT::f64 && !UseGPRForF64)
1543     Reg = State.AllocateReg(ArgFPR64s, ArgFPR32s);
1544   else
1545     Reg = State.AllocateReg(ArgGPRs);
1546   unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
1547
1548   // If we reach this point and PendingLocs is non-empty, we must be at the
1549   // end of a split argument that must be passed indirectly.
1550   if (!PendingLocs.empty()) {
1551     assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
1552     assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
1553
1554     for (auto &It : PendingLocs) {
1555       if (Reg)
1556         It.convertToReg(Reg);
1557       else
1558         It.convertToMem(StackOffset);
1559       State.addLoc(It);
1560     }
1561     PendingLocs.clear();
1562     PendingArgFlags.clear();
1563     return false;
1564   }
1565
1566   assert((!UseGPRForF32 || !UseGPRForF64 || LocVT == XLenVT) &&
1567          "Expected an XLenVT at this stage");
1568
1569   if (Reg) {
1570     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1571     return false;
1572   }
1573
1574   // When an f32 or f64 is passed on the stack, no bit-conversion is needed.
1575   if (ValVT == MVT::f32 || ValVT == MVT::f64) {
1576     LocVT = ValVT;
1577     LocInfo = CCValAssign::Full;
1578   }
1579   State.addLoc(CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
1580   return false;
1581 }
1582
1583 void RISCVTargetLowering::analyzeInputArgs(
1584     MachineFunction &MF, CCState &CCInfo,
1585     const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
1586   unsigned NumArgs = Ins.size();
1587   FunctionType *FType = MF.getFunction().getFunctionType();
1588
1589   for (unsigned i = 0; i != NumArgs; ++i) {
1590     MVT ArgVT = Ins[i].VT;
1591     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
1592
1593     Type *ArgTy = nullptr;
1594     if (IsRet)
1595       ArgTy = FType->getReturnType();
1596     else if (Ins[i].isOrigArg())
1597       ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
1598
1599     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
1600     if (CC_RISCV(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
1601                  ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
1602       LLVM_DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
1603                         << EVT(ArgVT).getEVTString() << '\n');
1604       llvm_unreachable(nullptr);
1605     }
1606   }
1607 }
1608
1609 void RISCVTargetLowering::analyzeOutputArgs(
1610     MachineFunction &MF, CCState &CCInfo,
1611     const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
1612     CallLoweringInfo *CLI) const {
1613   unsigned NumArgs = Outs.size();
1614
1615   for (unsigned i = 0; i != NumArgs; i++) {
1616     MVT ArgVT = Outs[i].VT;
1617     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1618     Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
1619
1620     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
1621     if (CC_RISCV(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
1622                  ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
1623       LLVM_DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
1624                         << EVT(ArgVT).getEVTString() << "\n");
1625       llvm_unreachable(nullptr);
1626     }
1627   }
1628 }
1629
1630 // Convert Val to a ValVT. Should not be called for CCValAssign::Indirect
1631 // values.
1632 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
1633                                    const CCValAssign &VA, const SDLoc &DL) {
1634   switch (VA.getLocInfo()) {
1635   default:
1636     llvm_unreachable("Unexpected CCValAssign::LocInfo");
1637   case CCValAssign::Full:
1638     break;
1639   case CCValAssign::BCvt:
1640     if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32) {
1641       Val = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, Val);
1642       break;
1643     }
1644     Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
1645     break;
1646   }
1647   return Val;
1648 }
1649
1650 // The caller is responsible for loading the full value if the argument is
1651 // passed with CCValAssign::Indirect.
1652 static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
1653                                 const CCValAssign &VA, const SDLoc &DL) {
1654   MachineFunction &MF = DAG.getMachineFunction();
1655   MachineRegisterInfo &RegInfo = MF.getRegInfo();
1656   EVT LocVT = VA.getLocVT();
1657   SDValue Val;
1658   const TargetRegisterClass *RC;
1659
1660   switch (LocVT.getSimpleVT().SimpleTy) {
1661   default:
1662     llvm_unreachable("Unexpected register type");
1663   case MVT::i32:
1664   case MVT::i64:
1665     RC = &RISCV::GPRRegClass;
1666     break;
1667   case MVT::f32:
1668     RC = &RISCV::FPR32RegClass;
1669     break;
1670   case MVT::f64:
1671     RC = &RISCV::FPR64RegClass;
1672     break;
1673   }
1674
1675   unsigned VReg = RegInfo.createVirtualRegister(RC);
1676   RegInfo.addLiveIn(VA.getLocReg(), VReg);
1677   Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1678
1679   if (VA.getLocInfo() == CCValAssign::Indirect)
1680     return Val;
1681
1682   return convertLocVTToValVT(DAG, Val, VA, DL);
1683 }
1684
1685 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val,
1686                                    const CCValAssign &VA, const SDLoc &DL) {
1687   EVT LocVT = VA.getLocVT();
1688
1689   switch (VA.getLocInfo()) {
1690   default:
1691     llvm_unreachable("Unexpected CCValAssign::LocInfo");
1692   case CCValAssign::Full:
1693     break;
1694   case CCValAssign::BCvt:
1695     if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32) {
1696       Val = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Val);
1697       break;
1698     }
1699     Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
1700     break;
1701   }
1702   return Val;
1703 }
1704
1705 // The caller is responsible for loading the full value if the argument is
1706 // passed with CCValAssign::Indirect.
1707 static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
1708                                 const CCValAssign &VA, const SDLoc &DL) {
1709   MachineFunction &MF = DAG.getMachineFunction();
1710   MachineFrameInfo &MFI = MF.getFrameInfo();
1711   EVT LocVT = VA.getLocVT();
1712   EVT ValVT = VA.getValVT();
1713   EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
1714   int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
1715                                  VA.getLocMemOffset(), /*Immutable=*/true);
1716   SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1717   SDValue Val;
1718
1719   ISD::LoadExtType ExtType;
1720   switch (VA.getLocInfo()) {
1721   default:
1722     llvm_unreachable("Unexpected CCValAssign::LocInfo");
1723   case CCValAssign::Full:
1724   case CCValAssign::Indirect:
1725   case CCValAssign::BCvt:
1726     ExtType = ISD::NON_EXTLOAD;
1727     break;
1728   }
1729   Val = DAG.getExtLoad(
1730       ExtType, DL, LocVT, Chain, FIN,
1731       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
1732   return Val;
1733 }
1734
1735 static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
1736                                        const CCValAssign &VA, const SDLoc &DL) {
1737   assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
1738          "Unexpected VA");
1739   MachineFunction &MF = DAG.getMachineFunction();
1740   MachineFrameInfo &MFI = MF.getFrameInfo();
1741   MachineRegisterInfo &RegInfo = MF.getRegInfo();
1742
1743   if (VA.isMemLoc()) {
1744     // f64 is passed on the stack.
1745     int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
1746     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1747     return DAG.getLoad(MVT::f64, DL, Chain, FIN,
1748                        MachinePointerInfo::getFixedStack(MF, FI));
1749   }
1750
1751   assert(VA.isRegLoc() && "Expected register VA assignment");
1752
1753   unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
1754   RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
1755   SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
1756   SDValue Hi;
1757   if (VA.getLocReg() == RISCV::X17) {
1758     // Second half of f64 is passed on the stack.
1759     int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
1760     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1761     Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
1762                      MachinePointerInfo::getFixedStack(MF, FI));
1763   } else {
1764     // Second half of f64 is passed in another GPR.
1765     unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
1766     RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
1767     Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
1768   }
1769   return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1770 }
1771
1772 // Transform physical registers into virtual registers.
1773 SDValue RISCVTargetLowering::LowerFormalArguments(
1774     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1775     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1776     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1777
1778   switch (CallConv) {
1779   default:
1780     report_fatal_error("Unsupported calling convention");
1781   case CallingConv::C:
1782   case CallingConv::Fast:
1783     break;
1784   }
1785
1786   MachineFunction &MF = DAG.getMachineFunction();
1787
1788   const Function &Func = MF.getFunction();
1789   if (Func.hasFnAttribute("interrupt")) {
1790     if (!Func.arg_empty())
1791       report_fatal_error(
1792         "Functions with the interrupt attribute cannot have arguments!");
1793
1794     StringRef Kind =
1795       MF.getFunction().getFnAttribute("interrupt").getValueAsString();
1796
1797     if (!(Kind == "user" || Kind == "supervisor" || Kind == "machine"))
1798       report_fatal_error(
1799         "Function interrupt attribute argument not supported!");
1800   }
1801
1802   EVT PtrVT = getPointerTy(DAG.getDataLayout());
1803   MVT XLenVT = Subtarget.getXLenVT();
1804   unsigned XLenInBytes = Subtarget.getXLen() / 8;
1805   // Used with vargs to acumulate store chains.
1806   std::vector<SDValue> OutChains;
1807
1808   // Assign locations to all of the incoming arguments.
1809   SmallVector<CCValAssign, 16> ArgLocs;
1810   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
1811   analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
1812
1813   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1814     CCValAssign &VA = ArgLocs[i];
1815     SDValue ArgValue;
1816     // Passing f64 on RV32D with a soft float ABI must be handled as a special
1817     // case.
1818     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
1819       ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
1820     else if (VA.isRegLoc())
1821       ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
1822     else
1823       ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
1824
1825     if (VA.getLocInfo() == CCValAssign::Indirect) {
1826       // If the original argument was split and passed by reference (e.g. i128
1827       // on RV32), we need to load all parts of it here (using the same
1828       // address).
1829       InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1830                                    MachinePointerInfo()));
1831       unsigned ArgIndex = Ins[i].OrigArgIndex;
1832       assert(Ins[i].PartOffset == 0);
1833       while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
1834         CCValAssign &PartVA = ArgLocs[i + 1];
1835         unsigned PartOffset = Ins[i + 1].PartOffset;
1836         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1837                                       DAG.getIntPtrConstant(PartOffset, DL));
1838         InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1839                                      MachinePointerInfo()));
1840         ++i;
1841       }
1842       continue;
1843     }
1844     InVals.push_back(ArgValue);
1845   }
1846
1847   if (IsVarArg) {
1848     ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
1849     unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
1850     const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1851     MachineFrameInfo &MFI = MF.getFrameInfo();
1852     MachineRegisterInfo &RegInfo = MF.getRegInfo();
1853     RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1854
1855     // Offset of the first variable argument from stack pointer, and size of
1856     // the vararg save area. For now, the varargs save area is either zero or
1857     // large enough to hold a0-a7.
1858     int VaArgOffset, VarArgsSaveSize;
1859
1860     // If all registers are allocated, then all varargs must be passed on the
1861     // stack and we don't need to save any argregs.
1862     if (ArgRegs.size() == Idx) {
1863       VaArgOffset = CCInfo.getNextStackOffset();
1864       VarArgsSaveSize = 0;
1865     } else {
1866       VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
1867       VaArgOffset = -VarArgsSaveSize;
1868     }
1869
1870     // Record the frame index of the first variable argument
1871     // which is a value necessary to VASTART.
1872     int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
1873     RVFI->setVarArgsFrameIndex(FI);
1874
1875     // If saving an odd number of registers then create an extra stack slot to
1876     // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
1877     // offsets to even-numbered registered remain 2*XLEN-aligned.
1878     if (Idx % 2) {
1879       FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes,
1880                                  true);
1881       VarArgsSaveSize += XLenInBytes;
1882     }
1883
1884     // Copy the integer registers that may have been used for passing varargs
1885     // to the vararg save area.
1886     for (unsigned I = Idx; I < ArgRegs.size();
1887          ++I, VaArgOffset += XLenInBytes) {
1888       const unsigned Reg = RegInfo.createVirtualRegister(RC);
1889       RegInfo.addLiveIn(ArgRegs[I], Reg);
1890       SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
1891       FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
1892       SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1893       SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
1894                                    MachinePointerInfo::getFixedStack(MF, FI));
1895       cast<StoreSDNode>(Store.getNode())
1896           ->getMemOperand()
1897           ->setValue((Value *)nullptr);
1898       OutChains.push_back(Store);
1899     }
1900     RVFI->setVarArgsSaveSize(VarArgsSaveSize);
1901   }
1902
1903   // All stores are grouped in one node to allow the matching between
1904   // the size of Ins and InVals. This only happens for vararg functions.
1905   if (!OutChains.empty()) {
1906     OutChains.push_back(Chain);
1907     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
1908   }
1909
1910   return Chain;
1911 }
1912
1913 /// isEligibleForTailCallOptimization - Check whether the call is eligible
1914 /// for tail call optimization.
1915 /// Note: This is modelled after ARM's IsEligibleForTailCallOptimization.
1916 bool RISCVTargetLowering::isEligibleForTailCallOptimization(
1917     CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
1918     const SmallVector<CCValAssign, 16> &ArgLocs) const {
1919
1920   auto &Callee = CLI.Callee;
1921   auto CalleeCC = CLI.CallConv;
1922   auto IsVarArg = CLI.IsVarArg;
1923   auto &Outs = CLI.Outs;
1924   auto &Caller = MF.getFunction();
1925   auto CallerCC = Caller.getCallingConv();
1926
1927   // Do not tail call opt functions with "disable-tail-calls" attribute.
1928   if (Caller.getFnAttribute("disable-tail-calls").getValueAsString() == "true")
1929     return false;
1930
1931   // Exception-handling functions need a special set of instructions to
1932   // indicate a return to the hardware. Tail-calling another function would
1933   // probably break this.
1934   // TODO: The "interrupt" attribute isn't currently defined by RISC-V. This
1935   // should be expanded as new function attributes are introduced.
1936   if (Caller.hasFnAttribute("interrupt"))
1937     return false;
1938
1939   // Do not tail call opt functions with varargs.
1940   if (IsVarArg)
1941     return false;
1942
1943   // Do not tail call opt if the stack is used to pass parameters.
1944   if (CCInfo.getNextStackOffset() != 0)
1945     return false;
1946
1947   // Do not tail call opt if any parameters need to be passed indirectly.
1948   // Since long doubles (fp128) and i128 are larger than 2*XLEN, they are
1949   // passed indirectly. So the address of the value will be passed in a
1950   // register, or if not available, then the address is put on the stack. In
1951   // order to pass indirectly, space on the stack often needs to be allocated
1952   // in order to store the value. In this case the CCInfo.getNextStackOffset()
1953   // != 0 check is not enough and we need to check if any CCValAssign ArgsLocs
1954   // are passed CCValAssign::Indirect.
1955   for (auto &VA : ArgLocs)
1956     if (VA.getLocInfo() == CCValAssign::Indirect)
1957       return false;
1958
1959   // Do not tail call opt if either caller or callee uses struct return
1960   // semantics.
1961   auto IsCallerStructRet = Caller.hasStructRetAttr();
1962   auto IsCalleeStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
1963   if (IsCallerStructRet || IsCalleeStructRet)
1964     return false;
1965
1966   // Externally-defined functions with weak linkage should not be
1967   // tail-called. The behaviour of branch instructions in this situation (as
1968   // used for tail calls) is implementation-defined, so we cannot rely on the
1969   // linker replacing the tail call with a return.
1970   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1971     const GlobalValue *GV = G->getGlobal();
1972     if (GV->hasExternalWeakLinkage())
1973       return false;
1974   }
1975
1976   // The callee has to preserve all registers the caller needs to preserve.
1977   const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
1978   const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
1979   if (CalleeCC != CallerCC) {
1980     const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
1981     if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
1982       return false;
1983   }
1984
1985   // Byval parameters hand the function a pointer directly into the stack area
1986   // we want to reuse during a tail call. Working around this *is* possible
1987   // but less efficient and uglier in LowerCall.
1988   for (auto &Arg : Outs)
1989     if (Arg.Flags.isByVal())
1990       return false;
1991
1992   return true;
1993 }
1994
1995 // Lower a call to a callseq_start + CALL + callseq_end chain, and add input
1996 // and output parameter nodes.
1997 SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1998                                        SmallVectorImpl<SDValue> &InVals) const {
1999   SelectionDAG &DAG = CLI.DAG;
2000   SDLoc &DL = CLI.DL;
2001   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2002   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2003   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
2004   SDValue Chain = CLI.Chain;
2005   SDValue Callee = CLI.Callee;
2006   bool &IsTailCall = CLI.IsTailCall;
2007   CallingConv::ID CallConv = CLI.CallConv;
2008   bool IsVarArg = CLI.IsVarArg;
2009   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2010   MVT XLenVT = Subtarget.getXLenVT();
2011
2012   MachineFunction &MF = DAG.getMachineFunction();
2013
2014   // Analyze the operands of the call, assigning locations to each operand.
2015   SmallVector<CCValAssign, 16> ArgLocs;
2016   CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
2017   analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
2018
2019   // Check if it's really possible to do a tail call.
2020   if (IsTailCall)
2021     IsTailCall = isEligibleForTailCallOptimization(ArgCCInfo, CLI, MF, ArgLocs);
2022
2023   if (IsTailCall)
2024     ++NumTailCalls;
2025   else if (CLI.CS && CLI.CS.isMustTailCall())
2026     report_fatal_error("failed to perform tail call elimination on a call "
2027                        "site marked musttail");
2028
2029   // Get a count of how many bytes are to be pushed on the stack.
2030   unsigned NumBytes = ArgCCInfo.getNextStackOffset();
2031
2032   // Create local copies for byval args
2033   SmallVector<SDValue, 8> ByValArgs;
2034   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
2035     ISD::ArgFlagsTy Flags = Outs[i].Flags;
2036     if (!Flags.isByVal())
2037       continue;
2038
2039     SDValue Arg = OutVals[i];
2040     unsigned Size = Flags.getByValSize();
2041     unsigned Align = Flags.getByValAlign();
2042
2043     int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
2044     SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2045     SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
2046
2047     Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
2048                           /*IsVolatile=*/false,
2049                           /*AlwaysInline=*/false,
2050                           IsTailCall, MachinePointerInfo(),
2051                           MachinePointerInfo());
2052     ByValArgs.push_back(FIPtr);
2053   }
2054
2055   if (!IsTailCall)
2056     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
2057
2058   // Copy argument values to their designated locations.
2059   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2060   SmallVector<SDValue, 8> MemOpChains;
2061   SDValue StackPtr;
2062   for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
2063     CCValAssign &VA = ArgLocs[i];
2064     SDValue ArgValue = OutVals[i];
2065     ISD::ArgFlagsTy Flags = Outs[i].Flags;
2066
2067     // Handle passing f64 on RV32D with a soft float ABI as a special case.
2068     bool IsF64OnRV32DSoftABI =
2069         VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
2070     if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
2071       SDValue SplitF64 = DAG.getNode(
2072           RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
2073       SDValue Lo = SplitF64.getValue(0);
2074       SDValue Hi = SplitF64.getValue(1);
2075
2076       unsigned RegLo = VA.getLocReg();
2077       RegsToPass.push_back(std::make_pair(RegLo, Lo));
2078
2079       if (RegLo == RISCV::X17) {
2080         // Second half of f64 is passed on the stack.
2081         // Work out the address of the stack slot.
2082         if (!StackPtr.getNode())
2083           StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
2084         // Emit the store.
2085         MemOpChains.push_back(
2086             DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
2087       } else {
2088         // Second half of f64 is passed in another GPR.
2089         unsigned RegHigh = RegLo + 1;
2090         RegsToPass.push_back(std::make_pair(RegHigh, Hi));
2091       }
2092       continue;
2093     }
2094
2095     // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
2096     // as any other MemLoc.
2097
2098     // Promote the value if needed.
2099     // For now, only handle fully promoted and indirect arguments.
2100     if (VA.getLocInfo() == CCValAssign::Indirect) {
2101       // Store the argument in a stack slot and pass its address.
2102       SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
2103       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
2104       MemOpChains.push_back(
2105           DAG.getStore(Chain, DL, ArgValue, SpillSlot,
2106                        MachinePointerInfo::getFixedStack(MF, FI)));
2107       // If the original argument was split (e.g. i128), we need
2108       // to store all parts of it here (and pass just one address).
2109       unsigned ArgIndex = Outs[i].OrigArgIndex;
2110       assert(Outs[i].PartOffset == 0);
2111       while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
2112         SDValue PartValue = OutVals[i + 1];
2113         unsigned PartOffset = Outs[i + 1].PartOffset;
2114         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
2115                                       DAG.getIntPtrConstant(PartOffset, DL));
2116         MemOpChains.push_back(
2117             DAG.getStore(Chain, DL, PartValue, Address,
2118                          MachinePointerInfo::getFixedStack(MF, FI)));
2119         ++i;
2120       }
2121       ArgValue = SpillSlot;
2122     } else {
2123       ArgValue = convertValVTToLocVT(DAG, ArgValue, VA, DL);
2124     }
2125
2126     // Use local copy if it is a byval arg.
2127     if (Flags.isByVal())
2128       ArgValue = ByValArgs[j++];
2129
2130     if (VA.isRegLoc()) {
2131       // Queue up the argument copies and emit them at the end.
2132       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
2133     } else {
2134       assert(VA.isMemLoc() && "Argument not register or memory");
2135       assert(!IsTailCall && "Tail call not allowed if stack is used "
2136                             "for passing parameters");
2137
2138       // Work out the address of the stack slot.
2139       if (!StackPtr.getNode())
2140         StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
2141       SDValue Address =
2142           DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
2143                       DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
2144
2145       // Emit the store.
2146       MemOpChains.push_back(
2147           DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
2148     }
2149   }
2150
2151   // Join the stores, which are independent of one another.
2152   if (!MemOpChains.empty())
2153     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
2154
2155   SDValue Glue;
2156
2157   // Build a sequence of copy-to-reg nodes, chained and glued together.
2158   for (auto &Reg : RegsToPass) {
2159     Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
2160     Glue = Chain.getValue(1);
2161   }
2162
2163   // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
2164   // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
2165   // split it and then direct call can be matched by PseudoCALL.
2166   if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
2167     const GlobalValue *GV = S->getGlobal();
2168
2169     unsigned OpFlags = RISCVII::MO_CALL;
2170     if (!getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV))
2171       OpFlags = RISCVII::MO_PLT;
2172
2173     Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, OpFlags);
2174   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2175     unsigned OpFlags = RISCVII::MO_CALL;
2176
2177     if (!getTargetMachine().shouldAssumeDSOLocal(*MF.getFunction().getParent(),
2178                                                  nullptr))
2179       OpFlags = RISCVII::MO_PLT;
2180
2181     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, OpFlags);
2182   }
2183
2184   // The first call operand is the chain and the second is the target address.
2185   SmallVector<SDValue, 8> Ops;
2186   Ops.push_back(Chain);
2187   Ops.push_back(Callee);
2188
2189   // Add argument registers to the end of the list so that they are
2190   // known live into the call.
2191   for (auto &Reg : RegsToPass)
2192     Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
2193
2194   if (!IsTailCall) {
2195     // Add a register mask operand representing the call-preserved registers.
2196     const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
2197     const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
2198     assert(Mask && "Missing call preserved mask for calling convention");
2199     Ops.push_back(DAG.getRegisterMask(Mask));
2200   }
2201
2202   // Glue the call to the argument copies, if any.
2203   if (Glue.getNode())
2204     Ops.push_back(Glue);
2205
2206   // Emit the call.
2207   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2208
2209   if (IsTailCall) {
2210     MF.getFrameInfo().setHasTailCall();
2211     return DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops);
2212   }
2213
2214   Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
2215   Glue = Chain.getValue(1);
2216
2217   // Mark the end of the call, which is glued to the call itself.
2218   Chain = DAG.getCALLSEQ_END(Chain,
2219                              DAG.getConstant(NumBytes, DL, PtrVT, true),
2220                              DAG.getConstant(0, DL, PtrVT, true),
2221                              Glue, DL);
2222   Glue = Chain.getValue(1);
2223
2224   // Assign locations to each value returned by this call.
2225   SmallVector<CCValAssign, 16> RVLocs;
2226   CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
2227   analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
2228
2229   // Copy all of the result registers out of their specified physreg.
2230   for (auto &VA : RVLocs) {
2231     // Copy the value out
2232     SDValue RetValue =
2233         DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
2234     // Glue the RetValue to the end of the call sequence
2235     Chain = RetValue.getValue(1);
2236     Glue = RetValue.getValue(2);
2237
2238     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
2239       assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
2240       SDValue RetValue2 =
2241           DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
2242       Chain = RetValue2.getValue(1);
2243       Glue = RetValue2.getValue(2);
2244       RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
2245                              RetValue2);
2246     }
2247
2248     RetValue = convertLocVTToValVT(DAG, RetValue, VA, DL);
2249
2250     InVals.push_back(RetValue);
2251   }
2252
2253   return Chain;
2254 }
2255
2256 bool RISCVTargetLowering::CanLowerReturn(
2257     CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
2258     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
2259   SmallVector<CCValAssign, 16> RVLocs;
2260   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
2261   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
2262     MVT VT = Outs[i].VT;
2263     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
2264     RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
2265     if (CC_RISCV(MF.getDataLayout(), ABI, i, VT, VT, CCValAssign::Full,
2266                  ArgFlags, CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
2267       return false;
2268   }
2269   return true;
2270 }
2271
2272 SDValue
2273 RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2274                                  bool IsVarArg,
2275                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
2276                                  const SmallVectorImpl<SDValue> &OutVals,
2277                                  const SDLoc &DL, SelectionDAG &DAG) const {
2278   // Stores the assignment of the return value to a location.
2279   SmallVector<CCValAssign, 16> RVLocs;
2280
2281   // Info about the registers and stack slot.
2282   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2283                  *DAG.getContext());
2284
2285   analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
2286                     nullptr);
2287
2288   SDValue Glue;
2289   SmallVector<SDValue, 4> RetOps(1, Chain);
2290
2291   // Copy the result values into the output registers.
2292   for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
2293     SDValue Val = OutVals[i];
2294     CCValAssign &VA = RVLocs[i];
2295     assert(VA.isRegLoc() && "Can only return in registers!");
2296
2297     if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
2298       // Handle returning f64 on RV32D with a soft float ABI.
2299       assert(VA.isRegLoc() && "Expected return via registers");
2300       SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
2301                                      DAG.getVTList(MVT::i32, MVT::i32), Val);
2302       SDValue Lo = SplitF64.getValue(0);
2303       SDValue Hi = SplitF64.getValue(1);
2304       unsigned RegLo = VA.getLocReg();
2305       unsigned RegHi = RegLo + 1;
2306       Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
2307       Glue = Chain.getValue(1);
2308       RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
2309       Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
2310       Glue = Chain.getValue(1);
2311       RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
2312     } else {
2313       // Handle a 'normal' return.
2314       Val = convertValVTToLocVT(DAG, Val, VA, DL);
2315       Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
2316
2317       // Guarantee that all emitted copies are stuck together.
2318       Glue = Chain.getValue(1);
2319       RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2320     }
2321   }
2322
2323   RetOps[0] = Chain; // Update chain.
2324
2325   // Add the glue node if we have it.
2326   if (Glue.getNode()) {
2327     RetOps.push_back(Glue);
2328   }
2329
2330   // Interrupt service routines use different return instructions.
2331   const Function &Func = DAG.getMachineFunction().getFunction();
2332   if (Func.hasFnAttribute("interrupt")) {
2333     if (!Func.getReturnType()->isVoidTy())
2334       report_fatal_error(
2335           "Functions with the interrupt attribute must have void return type!");
2336
2337     MachineFunction &MF = DAG.getMachineFunction();
2338     StringRef Kind =
2339       MF.getFunction().getFnAttribute("interrupt").getValueAsString();
2340
2341     unsigned RetOpc;
2342     if (Kind == "user")
2343       RetOpc = RISCVISD::URET_FLAG;
2344     else if (Kind == "supervisor")
2345       RetOpc = RISCVISD::SRET_FLAG;
2346     else
2347       RetOpc = RISCVISD::MRET_FLAG;
2348
2349     return DAG.getNode(RetOpc, DL, MVT::Other, RetOps);
2350   }
2351
2352   return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
2353 }
2354
2355 const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
2356   switch ((RISCVISD::NodeType)Opcode) {
2357   case RISCVISD::FIRST_NUMBER:
2358     break;
2359   case RISCVISD::RET_FLAG:
2360     return "RISCVISD::RET_FLAG";
2361   case RISCVISD::URET_FLAG:
2362     return "RISCVISD::URET_FLAG";
2363   case RISCVISD::SRET_FLAG:
2364     return "RISCVISD::SRET_FLAG";
2365   case RISCVISD::MRET_FLAG:
2366     return "RISCVISD::MRET_FLAG";
2367   case RISCVISD::CALL:
2368     return "RISCVISD::CALL";
2369   case RISCVISD::SELECT_CC:
2370     return "RISCVISD::SELECT_CC";
2371   case RISCVISD::BuildPairF64:
2372     return "RISCVISD::BuildPairF64";
2373   case RISCVISD::SplitF64:
2374     return "RISCVISD::SplitF64";
2375   case RISCVISD::TAIL:
2376     return "RISCVISD::TAIL";
2377   case RISCVISD::SLLW:
2378     return "RISCVISD::SLLW";
2379   case RISCVISD::SRAW:
2380     return "RISCVISD::SRAW";
2381   case RISCVISD::SRLW:
2382     return "RISCVISD::SRLW";
2383   case RISCVISD::DIVW:
2384     return "RISCVISD::DIVW";
2385   case RISCVISD::DIVUW:
2386     return "RISCVISD::DIVUW";
2387   case RISCVISD::REMUW:
2388     return "RISCVISD::REMUW";
2389   case RISCVISD::FMV_W_X_RV64:
2390     return "RISCVISD::FMV_W_X_RV64";
2391   case RISCVISD::FMV_X_ANYEXTW_RV64:
2392     return "RISCVISD::FMV_X_ANYEXTW_RV64";
2393   case RISCVISD::READ_CYCLE_WIDE:
2394     return "RISCVISD::READ_CYCLE_WIDE";
2395   }
2396   return nullptr;
2397 }
2398
2399 /// getConstraintType - Given a constraint letter, return the type of
2400 /// constraint it is for this target.
2401 RISCVTargetLowering::ConstraintType
2402 RISCVTargetLowering::getConstraintType(StringRef Constraint) const {
2403   if (Constraint.size() == 1) {
2404     switch (Constraint[0]) {
2405     default:
2406       break;
2407     case 'f':
2408       return C_RegisterClass;
2409     case 'I':
2410     case 'J':
2411     case 'K':
2412       return C_Immediate;
2413     case 'A':
2414       return C_Memory;
2415     }
2416   }
2417   return TargetLowering::getConstraintType(Constraint);
2418 }
2419
2420 std::pair<unsigned, const TargetRegisterClass *>
2421 RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
2422                                                   StringRef Constraint,
2423                                                   MVT VT) const {
2424   // First, see if this is a constraint that directly corresponds to a
2425   // RISCV register class.
2426   if (Constraint.size() == 1) {
2427     switch (Constraint[0]) {
2428     case 'r':
2429       return std::make_pair(0U, &RISCV::GPRRegClass);
2430     case 'f':
2431       if (Subtarget.hasStdExtF() && VT == MVT::f32)
2432         return std::make_pair(0U, &RISCV::FPR32RegClass);
2433       if (Subtarget.hasStdExtD() && VT == MVT::f64)
2434         return std::make_pair(0U, &RISCV::FPR64RegClass);
2435       break;
2436     default:
2437       break;
2438     }
2439   }
2440
2441   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2442 }
2443
2444 unsigned
2445 RISCVTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const {
2446   // Currently only support length 1 constraints.
2447   if (ConstraintCode.size() == 1) {
2448     switch (ConstraintCode[0]) {
2449     case 'A':
2450       return InlineAsm::Constraint_A;
2451     default:
2452       break;
2453     }
2454   }
2455
2456   return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
2457 }
2458
2459 void RISCVTargetLowering::LowerAsmOperandForConstraint(
2460     SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
2461     SelectionDAG &DAG) const {
2462   // Currently only support length 1 constraints.
2463   if (Constraint.length() == 1) {
2464     switch (Constraint[0]) {
2465     case 'I':
2466       // Validate & create a 12-bit signed immediate operand.
2467       if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
2468         uint64_t CVal = C->getSExtValue();
2469         if (isInt<12>(CVal))
2470           Ops.push_back(
2471               DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
2472       }
2473       return;
2474     case 'J':
2475       // Validate & create an integer zero operand.
2476       if (auto *C = dyn_cast<ConstantSDNode>(Op))
2477         if (C->getZExtValue() == 0)
2478           Ops.push_back(
2479               DAG.getTargetConstant(0, SDLoc(Op), Subtarget.getXLenVT()));
2480       return;
2481     case 'K':
2482       // Validate & create a 5-bit unsigned immediate operand.
2483       if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
2484         uint64_t CVal = C->getZExtValue();
2485         if (isUInt<5>(CVal))
2486           Ops.push_back(
2487               DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
2488       }
2489       return;
2490     default:
2491       break;
2492     }
2493   }
2494   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
2495 }
2496
2497 Instruction *RISCVTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
2498                                                    Instruction *Inst,
2499                                                    AtomicOrdering Ord) const {
2500   if (isa<LoadInst>(Inst) && Ord == AtomicOrdering::SequentiallyConsistent)
2501     return Builder.CreateFence(Ord);
2502   if (isa<StoreInst>(Inst) && isReleaseOrStronger(Ord))
2503     return Builder.CreateFence(AtomicOrdering::Release);
2504   return nullptr;
2505 }
2506
2507 Instruction *RISCVTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
2508                                                     Instruction *Inst,
2509                                                     AtomicOrdering Ord) const {
2510   if (isa<LoadInst>(Inst) && isAcquireOrStronger(Ord))
2511     return Builder.CreateFence(AtomicOrdering::Acquire);
2512   return nullptr;
2513 }
2514
2515 TargetLowering::AtomicExpansionKind
2516 RISCVTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
2517   // atomicrmw {fadd,fsub} must be expanded to use compare-exchange, as floating
2518   // point operations can't be used in an lr/sc sequence without breaking the
2519   // forward-progress guarantee.
2520   if (AI->isFloatingPointOperation())
2521     return AtomicExpansionKind::CmpXChg;
2522
2523   unsigned Size = AI->getType()->getPrimitiveSizeInBits();
2524   if (Size == 8 || Size == 16)
2525     return AtomicExpansionKind::MaskedIntrinsic;
2526   return AtomicExpansionKind::None;
2527 }
2528
2529 static Intrinsic::ID
2530 getIntrinsicForMaskedAtomicRMWBinOp(unsigned XLen, AtomicRMWInst::BinOp BinOp) {
2531   if (XLen == 32) {
2532     switch (BinOp) {
2533     default:
2534       llvm_unreachable("Unexpected AtomicRMW BinOp");
2535     case AtomicRMWInst::Xchg:
2536       return Intrinsic::riscv_masked_atomicrmw_xchg_i32;
2537     case AtomicRMWInst::Add:
2538       return Intrinsic::riscv_masked_atomicrmw_add_i32;
2539     case AtomicRMWInst::Sub:
2540       return Intrinsic::riscv_masked_atomicrmw_sub_i32;
2541     case AtomicRMWInst::Nand:
2542       return Intrinsic::riscv_masked_atomicrmw_nand_i32;
2543     case AtomicRMWInst::Max:
2544       return Intrinsic::riscv_masked_atomicrmw_max_i32;
2545     case AtomicRMWInst::Min:
2546       return Intrinsic::riscv_masked_atomicrmw_min_i32;
2547     case AtomicRMWInst::UMax:
2548       return Intrinsic::riscv_masked_atomicrmw_umax_i32;
2549     case AtomicRMWInst::UMin:
2550       return Intrinsic::riscv_masked_atomicrmw_umin_i32;
2551     }
2552   }
2553
2554   if (XLen == 64) {
2555     switch (BinOp) {
2556     default:
2557       llvm_unreachable("Unexpected AtomicRMW BinOp");
2558     case AtomicRMWInst::Xchg:
2559       return Intrinsic::riscv_masked_atomicrmw_xchg_i64;
2560     case AtomicRMWInst::Add:
2561       return Intrinsic::riscv_masked_atomicrmw_add_i64;
2562     case AtomicRMWInst::Sub:
2563       return Intrinsic::riscv_masked_atomicrmw_sub_i64;
2564     case AtomicRMWInst::Nand:
2565       return Intrinsic::riscv_masked_atomicrmw_nand_i64;
2566     case AtomicRMWInst::Max:
2567       return Intrinsic::riscv_masked_atomicrmw_max_i64;
2568     case AtomicRMWInst::Min:
2569       return Intrinsic::riscv_masked_atomicrmw_min_i64;
2570     case AtomicRMWInst::UMax:
2571       return Intrinsic::riscv_masked_atomicrmw_umax_i64;
2572     case AtomicRMWInst::UMin:
2573       return Intrinsic::riscv_masked_atomicrmw_umin_i64;
2574     }
2575   }
2576
2577   llvm_unreachable("Unexpected XLen\n");
2578 }
2579
2580 Value *RISCVTargetLowering::emitMaskedAtomicRMWIntrinsic(
2581     IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr,
2582     Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const {
2583   unsigned XLen = Subtarget.getXLen();
2584   Value *Ordering =
2585       Builder.getIntN(XLen, static_cast<uint64_t>(AI->getOrdering()));
2586   Type *Tys[] = {AlignedAddr->getType()};
2587   Function *LrwOpScwLoop = Intrinsic::getDeclaration(
2588       AI->getModule(),
2589       getIntrinsicForMaskedAtomicRMWBinOp(XLen, AI->getOperation()), Tys);
2590
2591   if (XLen == 64) {
2592     Incr = Builder.CreateSExt(Incr, Builder.getInt64Ty());
2593     Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
2594     ShiftAmt = Builder.CreateSExt(ShiftAmt, Builder.getInt64Ty());
2595   }
2596
2597   Value *Result;
2598
2599   // Must pass the shift amount needed to sign extend the loaded value prior
2600   // to performing a signed comparison for min/max. ShiftAmt is the number of
2601   // bits to shift the value into position. Pass XLen-ShiftAmt-ValWidth, which
2602   // is the number of bits to left+right shift the value in order to
2603   // sign-extend.
2604   if (AI->getOperation() == AtomicRMWInst::Min ||
2605       AI->getOperation() == AtomicRMWInst::Max) {
2606     const DataLayout &DL = AI->getModule()->getDataLayout();
2607     unsigned ValWidth =
2608         DL.getTypeStoreSizeInBits(AI->getValOperand()->getType());
2609     Value *SextShamt =
2610         Builder.CreateSub(Builder.getIntN(XLen, XLen - ValWidth), ShiftAmt);
2611     Result = Builder.CreateCall(LrwOpScwLoop,
2612                                 {AlignedAddr, Incr, Mask, SextShamt, Ordering});
2613   } else {
2614     Result =
2615         Builder.CreateCall(LrwOpScwLoop, {AlignedAddr, Incr, Mask, Ordering});
2616   }
2617
2618   if (XLen == 64)
2619     Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
2620   return Result;
2621 }
2622
2623 TargetLowering::AtomicExpansionKind
2624 RISCVTargetLowering::shouldExpandAtomicCmpXchgInIR(
2625     AtomicCmpXchgInst *CI) const {
2626   unsigned Size = CI->getCompareOperand()->getType()->getPrimitiveSizeInBits();
2627   if (Size == 8 || Size == 16)
2628     return AtomicExpansionKind::MaskedIntrinsic;
2629   return AtomicExpansionKind::None;
2630 }
2631
2632 Value *RISCVTargetLowering::emitMaskedAtomicCmpXchgIntrinsic(
2633     IRBuilder<> &Builder, AtomicCmpXchgInst *CI, Value *AlignedAddr,
2634     Value *CmpVal, Value *NewVal, Value *Mask, AtomicOrdering Ord) const {
2635   unsigned XLen = Subtarget.getXLen();
2636   Value *Ordering = Builder.getIntN(XLen, static_cast<uint64_t>(Ord));
2637   Intrinsic::ID CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i32;
2638   if (XLen == 64) {
2639     CmpVal = Builder.CreateSExt(CmpVal, Builder.getInt64Ty());
2640     NewVal = Builder.CreateSExt(NewVal, Builder.getInt64Ty());
2641     Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
2642     CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i64;
2643   }
2644   Type *Tys[] = {AlignedAddr->getType()};
2645   Function *MaskedCmpXchg =
2646       Intrinsic::getDeclaration(CI->getModule(), CmpXchgIntrID, Tys);
2647   Value *Result = Builder.CreateCall(
2648       MaskedCmpXchg, {AlignedAddr, CmpVal, NewVal, Mask, Ordering});
2649   if (XLen == 64)
2650     Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
2651   return Result;
2652 }
2653
2654 unsigned RISCVTargetLowering::getExceptionPointerRegister(
2655     const Constant *PersonalityFn) const {
2656   return RISCV::X10;
2657 }
2658
2659 unsigned RISCVTargetLowering::getExceptionSelectorRegister(
2660     const Constant *PersonalityFn) const {
2661   return RISCV::X11;
2662 }