blob: d29a80e22a662d2a03ef2ba9bc6d7c0735918d16 [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- RISCVISelLowering.cpp - RISCV DAG Lowering Implementation --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that RISCV uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "RISCVISelLowering.h"
16#include "RISCV.h"
Alex Bradburyc85be0d2018-01-10 19:41:03 +000017#include "RISCVMachineFunctionInfo.h"
Alex Bradbury89718422017-10-19 21:37:38 +000018#include "RISCVRegisterInfo.h"
19#include "RISCVSubtarget.h"
20#include "RISCVTargetMachine.h"
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +000021#include "llvm/ADT/Statistic.h"
Alex Bradbury89718422017-10-19 21:37:38 +000022#include "llvm/CodeGen/CallingConvLower.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Craig Topper2fa14362018-03-29 17:21:10 +000029#include "llvm/CodeGen/ValueTypes.h"
Alex Bradbury89718422017-10-19 21:37:38 +000030#include "llvm/IR/DiagnosticInfo.h"
31#include "llvm/IR/DiagnosticPrinter.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "riscv-lower"
39
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +000040STATISTIC(NumTailCalls, "Number of tail calls");
41
Alex Bradbury89718422017-10-19 21:37:38 +000042RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
43 const RISCVSubtarget &STI)
44 : TargetLowering(TM), Subtarget(STI) {
45
46 MVT XLenVT = Subtarget.getXLenVT();
47
48 // Set up the register classes.
49 addRegisterClass(XLenVT, &RISCV::GPRRegClass);
50
Alex Bradbury76c29ee2018-03-20 12:45:35 +000051 if (Subtarget.hasStdExtF())
52 addRegisterClass(MVT::f32, &RISCV::FPR32RegClass);
Alex Bradbury0b4175f2018-04-12 05:34:25 +000053 if (Subtarget.hasStdExtD())
54 addRegisterClass(MVT::f64, &RISCV::FPR64RegClass);
Alex Bradbury76c29ee2018-03-20 12:45:35 +000055
Alex Bradbury89718422017-10-19 21:37:38 +000056 // Compute derived properties from the register classes.
57 computeRegisterProperties(STI.getRegisterInfo());
58
59 setStackPointerRegisterToSaveRestore(RISCV::X2);
60
Alex Bradburycfa62912017-11-08 12:20:01 +000061 for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD})
62 setLoadExtAction(N, XLenVT, MVT::i1, Promote);
63
Alex Bradbury89718422017-10-19 21:37:38 +000064 // TODO: add all necessary setOperationAction calls.
Alex Bradburybfb00d42017-12-11 12:38:17 +000065 setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand);
66
Alex Bradburyffc435e2017-11-21 08:11:03 +000067 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Alex Bradbury74913e12017-11-08 13:31:40 +000068 setOperationAction(ISD::BR_CC, XLenVT, Expand);
Alex Bradbury65385162017-11-21 07:51:32 +000069 setOperationAction(ISD::SELECT, XLenVT, Custom);
70 setOperationAction(ISD::SELECT_CC, XLenVT, Expand);
71
Alex Bradburybfb00d42017-12-11 12:38:17 +000072 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
73 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
74
Alex Bradburyc85be0d2018-01-10 19:41:03 +000075 setOperationAction(ISD::VASTART, MVT::Other, Custom);
76 setOperationAction(ISD::VAARG, MVT::Other, Expand);
77 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
78 setOperationAction(ISD::VAEND, MVT::Other, Expand);
79
Alex Bradburyffc435e2017-11-21 08:11:03 +000080 for (auto VT : {MVT::i1, MVT::i8, MVT::i16})
81 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
82
Alex Bradbury92138382018-01-18 12:36:38 +000083 if (!Subtarget.hasStdExtM()) {
84 setOperationAction(ISD::MUL, XLenVT, Expand);
85 setOperationAction(ISD::MULHS, XLenVT, Expand);
86 setOperationAction(ISD::MULHU, XLenVT, Expand);
87 setOperationAction(ISD::SDIV, XLenVT, Expand);
88 setOperationAction(ISD::UDIV, XLenVT, Expand);
89 setOperationAction(ISD::SREM, XLenVT, Expand);
90 setOperationAction(ISD::UREM, XLenVT, Expand);
91 }
Alex Bradburyffc435e2017-11-21 08:11:03 +000092
Alex Bradbury92138382018-01-18 12:36:38 +000093 setOperationAction(ISD::SDIVREM, XLenVT, Expand);
94 setOperationAction(ISD::UDIVREM, XLenVT, Expand);
Alex Bradburyffc435e2017-11-21 08:11:03 +000095 setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand);
96 setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand);
Alex Bradburyffc435e2017-11-21 08:11:03 +000097
98 setOperationAction(ISD::SHL_PARTS, XLenVT, Expand);
99 setOperationAction(ISD::SRL_PARTS, XLenVT, Expand);
100 setOperationAction(ISD::SRA_PARTS, XLenVT, Expand);
101
102 setOperationAction(ISD::ROTL, XLenVT, Expand);
103 setOperationAction(ISD::ROTR, XLenVT, Expand);
104 setOperationAction(ISD::BSWAP, XLenVT, Expand);
105 setOperationAction(ISD::CTTZ, XLenVT, Expand);
106 setOperationAction(ISD::CTLZ, XLenVT, Expand);
107 setOperationAction(ISD::CTPOP, XLenVT, Expand);
108
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000109 ISD::CondCode FPCCToExtend[] = {
110 ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETO, ISD::SETUEQ,
111 ISD::SETUGT, ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE,
112 ISD::SETGT, ISD::SETGE, ISD::SETNE};
113
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000114 if (Subtarget.hasStdExtF()) {
115 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
116 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000117 for (auto CC : FPCCToExtend)
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000118 setCondCodeAction(CC, MVT::f32, Expand);
119 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
120 setOperationAction(ISD::SELECT, MVT::f32, Custom);
121 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000122 }
123
Alex Bradbury5d0dfa52018-04-12 05:42:42 +0000124 if (Subtarget.hasStdExtD()) {
125 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
126 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000127 for (auto CC : FPCCToExtend)
128 setCondCodeAction(CC, MVT::f64, Expand);
129 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
130 setOperationAction(ISD::SELECT, MVT::f64, Custom);
131 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000132 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
Alex Bradbury60baa2e2018-04-12 05:47:15 +0000133 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Alex Bradbury5d0dfa52018-04-12 05:42:42 +0000134 }
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000135
Alex Bradburyffc435e2017-11-21 08:11:03 +0000136 setOperationAction(ISD::GlobalAddress, XLenVT, Custom);
137 setOperationAction(ISD::BlockAddress, XLenVT, Custom);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000138 setOperationAction(ISD::ConstantPool, XLenVT, Custom);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000139
Alex Bradbury21aea512018-09-19 10:54:22 +0000140 if (Subtarget.hasStdExtA()) {
Alex Bradbury96f492d2018-06-13 12:04:51 +0000141 setMaxAtomicSizeInBitsSupported(Subtarget.getXLen());
Alex Bradbury21aea512018-09-19 10:54:22 +0000142 setMinCmpXchgSizeInBits(32);
143 } else {
Alex Bradbury96f492d2018-06-13 12:04:51 +0000144 setMaxAtomicSizeInBitsSupported(0);
Alex Bradbury21aea512018-09-19 10:54:22 +0000145 }
Alex Bradburydc790dd2018-06-13 11:58:46 +0000146
Alex Bradbury89718422017-10-19 21:37:38 +0000147 setBooleanContents(ZeroOrOneBooleanContent);
148
149 // Function alignments (log2).
Shiva Chenb48b0272018-04-12 11:30:59 +0000150 unsigned FunctionAlignment = Subtarget.hasStdExtC() ? 1 : 2;
151 setMinFunctionAlignment(FunctionAlignment);
152 setPrefFunctionAlignment(FunctionAlignment);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000153
154 // Effectively disable jump table generation.
155 setMinimumJumpTableEntries(INT_MAX);
Alex Bradbury89718422017-10-19 21:37:38 +0000156}
157
Shiva Chenbbf4c5c2018-02-02 02:43:18 +0000158EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
159 EVT VT) const {
160 if (!VT.isVector())
161 return getPointerTy(DL);
162 return VT.changeVectorElementTypeToInteger();
163}
164
Alex Bradbury21aea512018-09-19 10:54:22 +0000165bool RISCVTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
166 const CallInst &I,
167 MachineFunction &MF,
168 unsigned Intrinsic) const {
169 switch (Intrinsic) {
170 default:
171 return false;
172 case Intrinsic::riscv_masked_atomicrmw_xchg_i32:
173 case Intrinsic::riscv_masked_atomicrmw_add_i32:
174 case Intrinsic::riscv_masked_atomicrmw_sub_i32:
175 case Intrinsic::riscv_masked_atomicrmw_nand_i32:
176 case Intrinsic::riscv_masked_atomicrmw_max_i32:
177 case Intrinsic::riscv_masked_atomicrmw_min_i32:
178 case Intrinsic::riscv_masked_atomicrmw_umax_i32:
179 case Intrinsic::riscv_masked_atomicrmw_umin_i32:
180 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
181 Info.opc = ISD::INTRINSIC_W_CHAIN;
182 Info.memVT = MVT::getVT(PtrTy->getElementType());
183 Info.ptrVal = I.getArgOperand(0);
184 Info.offset = 0;
185 Info.align = 4;
186 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore |
187 MachineMemOperand::MOVolatile;
188 return true;
189 }
190}
191
Alex Bradbury09926292018-04-26 12:13:48 +0000192bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL,
193 const AddrMode &AM, Type *Ty,
194 unsigned AS,
195 Instruction *I) const {
196 // No global is ever allowed as a base.
197 if (AM.BaseGV)
198 return false;
199
200 // Require a 12-bit signed offset.
201 if (!isInt<12>(AM.BaseOffs))
202 return false;
203
204 switch (AM.Scale) {
205 case 0: // "r+i" or just "i", depending on HasBaseReg.
206 break;
207 case 1:
208 if (!AM.HasBaseReg) // allow "r+i".
209 break;
210 return false; // disallow "r+r" or "r+r+i".
211 default:
212 return false;
213 }
214
215 return true;
216}
217
Alex Bradburydcbff632018-04-26 13:15:17 +0000218bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
219 return isInt<12>(Imm);
220}
221
Alex Bradbury5c41ece2018-04-26 13:00:37 +0000222bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
223 return isInt<12>(Imm);
224}
225
Alex Bradbury130b8b32018-04-26 13:37:00 +0000226// On RV32, 64-bit integers are split into their high and low parts and held
227// in two different registers, so the trunc is free since the low register can
228// just be used.
229bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
230 if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
231 return false;
232 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
233 unsigned DestBits = DstTy->getPrimitiveSizeInBits();
234 return (SrcBits == 64 && DestBits == 32);
235}
236
237bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
238 if (Subtarget.is64Bit() || SrcVT.isVector() || DstVT.isVector() ||
239 !SrcVT.isInteger() || !DstVT.isInteger())
240 return false;
241 unsigned SrcBits = SrcVT.getSizeInBits();
242 unsigned DestBits = DstVT.getSizeInBits();
243 return (SrcBits == 64 && DestBits == 32);
244}
245
Alex Bradbury15e894b2018-04-26 14:04:18 +0000246bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
247 // Zexts are free if they can be combined with a load.
248 if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
249 EVT MemVT = LD->getMemoryVT();
250 if ((MemVT == MVT::i8 || MemVT == MVT::i16 ||
251 (Subtarget.is64Bit() && MemVT == MVT::i32)) &&
252 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
253 LD->getExtensionType() == ISD::ZEXTLOAD))
254 return true;
255 }
256
257 return TargetLowering::isZExtFree(Val, VT2);
258}
259
Alex Bradbury65385162017-11-21 07:51:32 +0000260// Changes the condition code and swaps operands if necessary, so the SetCC
261// operation matches one of the comparisons supported directly in the RISC-V
262// ISA.
263static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
264 switch (CC) {
265 default:
266 break;
267 case ISD::SETGT:
268 case ISD::SETLE:
269 case ISD::SETUGT:
270 case ISD::SETULE:
271 CC = ISD::getSetCCSwappedOperands(CC);
272 std::swap(LHS, RHS);
273 break;
274 }
275}
276
277// Return the RISC-V branch opcode that matches the given DAG integer
278// condition code. The CondCode must be one of those supported by the RISC-V
279// ISA (see normaliseSetCC).
280static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
281 switch (CC) {
282 default:
283 llvm_unreachable("Unsupported CondCode");
284 case ISD::SETEQ:
285 return RISCV::BEQ;
286 case ISD::SETNE:
287 return RISCV::BNE;
288 case ISD::SETLT:
289 return RISCV::BLT;
290 case ISD::SETGE:
291 return RISCV::BGE;
292 case ISD::SETULT:
293 return RISCV::BLTU;
294 case ISD::SETUGE:
295 return RISCV::BGEU;
296 }
297}
298
Alex Bradbury89718422017-10-19 21:37:38 +0000299SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
300 SelectionDAG &DAG) const {
301 switch (Op.getOpcode()) {
302 default:
303 report_fatal_error("unimplemented operand");
Alex Bradburyec8aa912017-11-08 13:24:21 +0000304 case ISD::GlobalAddress:
305 return lowerGlobalAddress(Op, DAG);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000306 case ISD::BlockAddress:
307 return lowerBlockAddress(Op, DAG);
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000308 case ISD::ConstantPool:
309 return lowerConstantPool(Op, DAG);
Alex Bradbury65385162017-11-21 07:51:32 +0000310 case ISD::SELECT:
311 return lowerSELECT(Op, DAG);
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000312 case ISD::VASTART:
313 return lowerVASTART(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000314 case ISD::FRAMEADDR:
Alex Bradbury0e167662018-10-04 05:27:50 +0000315 return lowerFRAMEADDR(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000316 case ISD::RETURNADDR:
Alex Bradbury0e167662018-10-04 05:27:50 +0000317 return lowerRETURNADDR(Op, DAG);
Alex Bradburyec8aa912017-11-08 13:24:21 +0000318 }
319}
320
321SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
322 SelectionDAG &DAG) const {
323 SDLoc DL(Op);
324 EVT Ty = Op.getValueType();
325 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
326 const GlobalValue *GV = N->getGlobal();
327 int64_t Offset = N->getOffset();
Sameer AbuAsal1dc0a8f2018-05-17 18:14:53 +0000328 MVT XLenVT = Subtarget.getXLenVT();
Alex Bradburyec8aa912017-11-08 13:24:21 +0000329
Alex Bradburyffc435e2017-11-21 08:11:03 +0000330 if (isPositionIndependent() || Subtarget.is64Bit())
Alex Bradburyec8aa912017-11-08 13:24:21 +0000331 report_fatal_error("Unable to lowerGlobalAddress");
Sameer AbuAsal1dc0a8f2018-05-17 18:14:53 +0000332 // In order to maximise the opportunity for common subexpression elimination,
333 // emit a separate ADD node for the global address offset instead of folding
334 // it in the global address node. Later peephole optimisations may choose to
335 // fold it back in when profitable.
336 SDValue GAHi = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_HI);
337 SDValue GALo = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_LO);
Alex Bradburyffc435e2017-11-21 08:11:03 +0000338 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0);
339 SDValue MNLo =
340 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0);
Sameer AbuAsal1dc0a8f2018-05-17 18:14:53 +0000341 if (Offset != 0)
342 return DAG.getNode(ISD::ADD, DL, Ty, MNLo,
343 DAG.getConstant(Offset, DL, XLenVT));
Alex Bradburyffc435e2017-11-21 08:11:03 +0000344 return MNLo;
345}
346
347SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
348 SelectionDAG &DAG) const {
349 SDLoc DL(Op);
350 EVT Ty = Op.getValueType();
351 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
352 const BlockAddress *BA = N->getBlockAddress();
353 int64_t Offset = N->getOffset();
354
355 if (isPositionIndependent() || Subtarget.is64Bit())
356 report_fatal_error("Unable to lowerBlockAddress");
357
358 SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI);
359 SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO);
360 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0);
361 SDValue MNLo =
362 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0);
363 return MNLo;
364}
365
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000366SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
367 SelectionDAG &DAG) const {
368 SDLoc DL(Op);
369 EVT Ty = Op.getValueType();
370 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
371 const Constant *CPA = N->getConstVal();
372 int64_t Offset = N->getOffset();
373 unsigned Alignment = N->getAlignment();
374
375 if (!isPositionIndependent()) {
376 SDValue CPAHi =
377 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI);
378 SDValue CPALo =
379 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO);
380 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0);
381 SDValue MNLo =
382 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0);
383 return MNLo;
384 } else {
385 report_fatal_error("Unable to lowerConstantPool");
386 }
387}
388
Alex Bradbury65385162017-11-21 07:51:32 +0000389SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
390 SDValue CondV = Op.getOperand(0);
391 SDValue TrueV = Op.getOperand(1);
392 SDValue FalseV = Op.getOperand(2);
393 SDLoc DL(Op);
394 MVT XLenVT = Subtarget.getXLenVT();
395
396 // If the result type is XLenVT and CondV is the output of a SETCC node
397 // which also operated on XLenVT inputs, then merge the SETCC node into the
398 // lowered RISCVISD::SELECT_CC to take advantage of the integer
399 // compare+branch instructions. i.e.:
400 // (select (setcc lhs, rhs, cc), truev, falsev)
401 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
402 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
403 CondV.getOperand(0).getSimpleValueType() == XLenVT) {
404 SDValue LHS = CondV.getOperand(0);
405 SDValue RHS = CondV.getOperand(1);
406 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
407 ISD::CondCode CCVal = CC->get();
408
409 normaliseSetCC(LHS, RHS, CCVal);
410
411 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
412 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
413 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
414 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
415 }
416
417 // Otherwise:
418 // (select condv, truev, falsev)
419 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
420 SDValue Zero = DAG.getConstant(0, DL, XLenVT);
421 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
422
423 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
424 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
425
426 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
427}
428
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000429SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
430 MachineFunction &MF = DAG.getMachineFunction();
431 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
432
433 SDLoc DL(Op);
434 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
435 getPointerTy(MF.getDataLayout()));
436
437 // vastart just stores the address of the VarArgsFrameIndex slot into the
438 // memory location argument.
439 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
440 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
441 MachinePointerInfo(SV));
442}
443
Alex Bradbury0e167662018-10-04 05:27:50 +0000444SDValue RISCVTargetLowering::lowerFRAMEADDR(SDValue Op,
Alex Bradbury70f137b2018-01-10 20:12:00 +0000445 SelectionDAG &DAG) const {
446 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
447 MachineFunction &MF = DAG.getMachineFunction();
448 MachineFrameInfo &MFI = MF.getFrameInfo();
449 MFI.setFrameAddressIsTaken(true);
450 unsigned FrameReg = RI.getFrameRegister(MF);
451 int XLenInBytes = Subtarget.getXLen() / 8;
452
453 EVT VT = Op.getValueType();
454 SDLoc DL(Op);
455 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
456 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
457 while (Depth--) {
458 int Offset = -(XLenInBytes * 2);
459 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
460 DAG.getIntPtrConstant(Offset, DL));
461 FrameAddr =
462 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
463 }
464 return FrameAddr;
465}
466
Alex Bradbury0e167662018-10-04 05:27:50 +0000467SDValue RISCVTargetLowering::lowerRETURNADDR(SDValue Op,
Alex Bradbury70f137b2018-01-10 20:12:00 +0000468 SelectionDAG &DAG) const {
469 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
470 MachineFunction &MF = DAG.getMachineFunction();
471 MachineFrameInfo &MFI = MF.getFrameInfo();
472 MFI.setReturnAddressIsTaken(true);
473 MVT XLenVT = Subtarget.getXLenVT();
474 int XLenInBytes = Subtarget.getXLen() / 8;
475
476 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
477 return SDValue();
478
479 EVT VT = Op.getValueType();
480 SDLoc DL(Op);
481 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
482 if (Depth) {
483 int Off = -XLenInBytes;
Alex Bradbury0e167662018-10-04 05:27:50 +0000484 SDValue FrameAddr = lowerFRAMEADDR(Op, DAG);
Alex Bradbury70f137b2018-01-10 20:12:00 +0000485 SDValue Offset = DAG.getConstant(Off, DL, VT);
486 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
487 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
488 MachinePointerInfo());
489 }
490
491 // Return the value of the return address register, marking it an implicit
492 // live-in.
493 unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
494 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
495}
496
Alex Bradbury5ac0a2f2018-10-03 23:30:16 +0000497SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
498 DAGCombinerInfo &DCI) const {
499 switch (N->getOpcode()) {
500 default:
501 break;
502 case RISCVISD::SplitF64: {
503 // If the input to SplitF64 is just BuildPairF64 then the operation is
504 // redundant. Instead, use BuildPairF64's operands directly.
505 SDValue Op0 = N->getOperand(0);
506 if (Op0->getOpcode() != RISCVISD::BuildPairF64)
507 break;
508 return DCI.CombineTo(N, Op0.getOperand(0), Op0.getOperand(1));
509 }
510 }
511
512 return SDValue();
513}
514
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000515static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
516 MachineBasicBlock *BB) {
517 assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
518
519 MachineFunction &MF = *BB->getParent();
520 DebugLoc DL = MI.getDebugLoc();
521 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
522 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
523 unsigned LoReg = MI.getOperand(0).getReg();
524 unsigned HiReg = MI.getOperand(1).getReg();
525 unsigned SrcReg = MI.getOperand(2).getReg();
526 const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
527 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
528
529 TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
530 RI);
531 MachineMemOperand *MMO =
532 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
533 MachineMemOperand::MOLoad, 8, 8);
534 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
535 .addFrameIndex(FI)
536 .addImm(0)
537 .addMemOperand(MMO);
538 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
539 .addFrameIndex(FI)
540 .addImm(4)
541 .addMemOperand(MMO);
542 MI.eraseFromParent(); // The pseudo instruction is gone now.
543 return BB;
544}
545
546static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
547 MachineBasicBlock *BB) {
548 assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
549 "Unexpected instruction");
550
551 MachineFunction &MF = *BB->getParent();
552 DebugLoc DL = MI.getDebugLoc();
553 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
554 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
555 unsigned DstReg = MI.getOperand(0).getReg();
556 unsigned LoReg = MI.getOperand(1).getReg();
557 unsigned HiReg = MI.getOperand(2).getReg();
558 const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
559 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
560
561 MachineMemOperand *MMO =
562 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
563 MachineMemOperand::MOStore, 8, 8);
564 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
565 .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
566 .addFrameIndex(FI)
567 .addImm(0)
568 .addMemOperand(MMO);
569 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
570 .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
571 .addFrameIndex(FI)
572 .addImm(4)
573 .addMemOperand(MMO);
574 TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
575 MI.eraseFromParent(); // The pseudo instruction is gone now.
576 return BB;
577}
578
Alex Bradbury65385162017-11-21 07:51:32 +0000579MachineBasicBlock *
580RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
581 MachineBasicBlock *BB) const {
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000582 switch (MI.getOpcode()) {
583 default:
584 llvm_unreachable("Unexpected instr type to insert");
585 case RISCV::Select_GPR_Using_CC_GPR:
586 case RISCV::Select_FPR32_Using_CC_GPR:
Alex Bradbury21d28fe2018-04-12 05:50:06 +0000587 case RISCV::Select_FPR64_Using_CC_GPR:
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000588 break;
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000589 case RISCV::BuildPairF64Pseudo:
590 return emitBuildPairF64Pseudo(MI, BB);
591 case RISCV::SplitF64Pseudo:
592 return emitSplitF64Pseudo(MI, BB);
Alex Bradbury65d6ea52018-03-21 15:11:02 +0000593 }
Alex Bradbury65385162017-11-21 07:51:32 +0000594
595 // To "insert" a SELECT instruction, we actually have to insert the triangle
596 // control-flow pattern. The incoming instruction knows the destination vreg
597 // to set, the condition code register to branch on, the true/false values to
598 // select between, and the condcode to use to select the appropriate branch.
599 //
600 // We produce the following control flow:
601 // HeadMBB
602 // | \
603 // | IfFalseMBB
604 // | /
605 // TailMBB
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000606 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
Alex Bradbury65385162017-11-21 07:51:32 +0000607 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000608 DebugLoc DL = MI.getDebugLoc();
Alex Bradbury65385162017-11-21 07:51:32 +0000609 MachineFunction::iterator I = ++BB->getIterator();
610
611 MachineBasicBlock *HeadMBB = BB;
612 MachineFunction *F = BB->getParent();
613 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
614 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
615
616 F->insert(I, IfFalseMBB);
617 F->insert(I, TailMBB);
618 // Move all remaining instructions to TailMBB.
619 TailMBB->splice(TailMBB->begin(), HeadMBB,
620 std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end());
621 // Update machine-CFG edges by transferring all successors of the current
622 // block to the new block which will contain the Phi node for the select.
623 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
624 // Set the successors for HeadMBB.
625 HeadMBB->addSuccessor(IfFalseMBB);
626 HeadMBB->addSuccessor(TailMBB);
627
628 // Insert appropriate branch.
629 unsigned LHS = MI.getOperand(1).getReg();
630 unsigned RHS = MI.getOperand(2).getReg();
631 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
632 unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
633
634 BuildMI(HeadMBB, DL, TII.get(Opcode))
635 .addReg(LHS)
636 .addReg(RHS)
637 .addMBB(TailMBB);
638
639 // IfFalseMBB just falls through to TailMBB.
640 IfFalseMBB->addSuccessor(TailMBB);
641
642 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
643 BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI),
644 MI.getOperand(0).getReg())
645 .addReg(MI.getOperand(4).getReg())
646 .addMBB(HeadMBB)
647 .addReg(MI.getOperand(5).getReg())
648 .addMBB(IfFalseMBB);
649
650 MI.eraseFromParent(); // The pseudo instruction is gone now.
651 return TailMBB;
652}
653
Alex Bradbury89718422017-10-19 21:37:38 +0000654// Calling Convention Implementation.
Alex Bradburydc31c612017-12-11 12:49:02 +0000655// The expectations for frontend ABI lowering vary from target to target.
656// Ideally, an LLVM frontend would be able to avoid worrying about many ABI
657// details, but this is a longer term goal. For now, we simply try to keep the
658// role of the frontend as simple and well-defined as possible. The rules can
659// be summarised as:
660// * Never split up large scalar arguments. We handle them here.
661// * If a hardfloat calling convention is being used, and the struct may be
662// passed in a pair of registers (fp+fp, int+fp), and both registers are
663// available, then pass as two separate arguments. If either the GPRs or FPRs
664// are exhausted, then pass according to the rule below.
665// * If a struct could never be passed in registers or directly in a stack
666// slot (as it is larger than 2*XLEN and the floating point rules don't
667// apply), then pass it using a pointer with the byval attribute.
668// * If a struct is less than 2*XLEN, then coerce to either a two-element
669// word-sized array or a 2*XLEN scalar (depending on alignment).
670// * The frontend can determine whether a struct is returned by reference or
671// not based on its size and fields. If it will be returned by reference, the
672// frontend must modify the prototype so a pointer with the sret annotation is
673// passed as the first argument. This is not necessary for large scalar
674// returns.
675// * Struct return values and varargs should be coerced to structs containing
676// register-size fields in the same situations they would be for fixed
677// arguments.
678
679static const MCPhysReg ArgGPRs[] = {
680 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
681 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
682};
683
684// Pass a 2*XLEN argument that has been split into two XLEN values through
685// registers or the stack as necessary.
686static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
687 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
688 MVT ValVT2, MVT LocVT2,
689 ISD::ArgFlagsTy ArgFlags2) {
690 unsigned XLenInBytes = XLen / 8;
691 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
692 // At least one half can be passed via register.
693 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
694 VA1.getLocVT(), CCValAssign::Full));
695 } else {
696 // Both halves must be passed on the stack, with proper alignment.
697 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
698 State.addLoc(
699 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
700 State.AllocateStack(XLenInBytes, StackAlign),
701 VA1.getLocVT(), CCValAssign::Full));
702 State.addLoc(CCValAssign::getMem(
703 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
704 CCValAssign::Full));
705 return false;
706 }
707
708 if (unsigned Reg = State.AllocateReg(ArgGPRs)) {
709 // The second half can also be passed via register.
710 State.addLoc(
711 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
712 } else {
713 // The second half is passed via the stack, without additional alignment.
714 State.addLoc(CCValAssign::getMem(
715 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
716 CCValAssign::Full));
717 }
718
719 return false;
720}
721
722// Implements the RISC-V calling convention. Returns true upon failure.
723static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT,
724 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000725 CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) {
Alex Bradburydc31c612017-12-11 12:49:02 +0000726 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
727 assert(XLen == 32 || XLen == 64);
728 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000729 if (ValVT == MVT::f32) {
730 LocVT = MVT::i32;
731 LocInfo = CCValAssign::BCvt;
732 }
Alex Bradburydc31c612017-12-11 12:49:02 +0000733
734 // Any return value split in to more than two values can't be returned
735 // directly.
736 if (IsRet && ValNo > 1)
737 return true;
738
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000739 // If this is a variadic argument, the RISC-V calling convention requires
740 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
741 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
742 // be used regardless of whether the original argument was split during
743 // legalisation or not. The argument will not be passed by registers if the
744 // original type is larger than 2*XLEN, so the register alignment rule does
745 // not apply.
746 unsigned TwoXLenInBytes = (2 * XLen) / 8;
747 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
748 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
749 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
750 // Skip 'odd' register if necessary.
751 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
752 State.AllocateReg(ArgGPRs);
753 }
754
Alex Bradburydc31c612017-12-11 12:49:02 +0000755 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
756 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
757 State.getPendingArgFlags();
758
759 assert(PendingLocs.size() == PendingArgFlags.size() &&
760 "PendingLocs and PendingArgFlags out of sync");
761
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000762 // Handle passing f64 on RV32D with a soft float ABI.
763 if (XLen == 32 && ValVT == MVT::f64) {
Mandeep Singh Grang88a8b262018-04-16 18:56:10 +0000764 assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000765 "Can't lower f64 if it is split");
766 // Depending on available argument GPRS, f64 may be passed in a pair of
767 // GPRs, split between a GPR and the stack, or passed completely on the
768 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
769 // cases.
770 unsigned Reg = State.AllocateReg(ArgGPRs);
771 LocVT = MVT::i32;
772 if (!Reg) {
773 unsigned StackOffset = State.AllocateStack(8, 8);
774 State.addLoc(
775 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
776 return false;
777 }
778 if (!State.AllocateReg(ArgGPRs))
779 State.AllocateStack(4, 4);
780 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
781 return false;
782 }
783
Alex Bradburydc31c612017-12-11 12:49:02 +0000784 // Split arguments might be passed indirectly, so keep track of the pending
785 // values.
786 if (ArgFlags.isSplit() || !PendingLocs.empty()) {
787 LocVT = XLenVT;
788 LocInfo = CCValAssign::Indirect;
789 PendingLocs.push_back(
790 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
791 PendingArgFlags.push_back(ArgFlags);
792 if (!ArgFlags.isSplitEnd()) {
793 return false;
794 }
795 }
796
797 // If the split argument only had two elements, it should be passed directly
798 // in registers or on the stack.
799 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
800 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
801 // Apply the normal calling convention rules to the first half of the
802 // split argument.
803 CCValAssign VA = PendingLocs[0];
804 ISD::ArgFlagsTy AF = PendingArgFlags[0];
805 PendingLocs.clear();
806 PendingArgFlags.clear();
807 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
808 ArgFlags);
809 }
810
811 // Allocate to a register if possible, or else a stack slot.
812 unsigned Reg = State.AllocateReg(ArgGPRs);
813 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
814
815 // If we reach this point and PendingLocs is non-empty, we must be at the
816 // end of a split argument that must be passed indirectly.
817 if (!PendingLocs.empty()) {
818 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
819 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
820
821 for (auto &It : PendingLocs) {
822 if (Reg)
823 It.convertToReg(Reg);
824 else
825 It.convertToMem(StackOffset);
826 State.addLoc(It);
827 }
828 PendingLocs.clear();
829 PendingArgFlags.clear();
830 return false;
831 }
832
833 assert(LocVT == XLenVT && "Expected an XLenVT at this stage");
834
835 if (Reg) {
836 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
837 } else {
838 State.addLoc(
839 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
840 }
841 return false;
842}
843
844void RISCVTargetLowering::analyzeInputArgs(
845 MachineFunction &MF, CCState &CCInfo,
846 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
847 unsigned NumArgs = Ins.size();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000848 FunctionType *FType = MF.getFunction().getFunctionType();
Alex Bradburydc31c612017-12-11 12:49:02 +0000849
850 for (unsigned i = 0; i != NumArgs; ++i) {
851 MVT ArgVT = Ins[i].VT;
852 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
853
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000854 Type *ArgTy = nullptr;
855 if (IsRet)
856 ArgTy = FType->getReturnType();
857 else if (Ins[i].isOrigArg())
858 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
859
Alex Bradburydc31c612017-12-11 12:49:02 +0000860 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000861 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000862 LLVM_DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
863 << EVT(ArgVT).getEVTString() << '\n');
Alex Bradburydc31c612017-12-11 12:49:02 +0000864 llvm_unreachable(nullptr);
865 }
866 }
867}
868
869void RISCVTargetLowering::analyzeOutputArgs(
870 MachineFunction &MF, CCState &CCInfo,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000871 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
872 CallLoweringInfo *CLI) const {
Alex Bradburydc31c612017-12-11 12:49:02 +0000873 unsigned NumArgs = Outs.size();
874
875 for (unsigned i = 0; i != NumArgs; i++) {
876 MVT ArgVT = Outs[i].VT;
877 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000878 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
Alex Bradburydc31c612017-12-11 12:49:02 +0000879
880 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000881 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000882 LLVM_DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
883 << EVT(ArgVT).getEVTString() << "\n");
Alex Bradburydc31c612017-12-11 12:49:02 +0000884 llvm_unreachable(nullptr);
885 }
886 }
887}
888
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +0000889// Convert Val to a ValVT. Should not be called for CCValAssign::Indirect
890// values.
891static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
892 const CCValAssign &VA, const SDLoc &DL) {
893 switch (VA.getLocInfo()) {
894 default:
895 llvm_unreachable("Unexpected CCValAssign::LocInfo");
896 case CCValAssign::Full:
897 break;
898 case CCValAssign::BCvt:
899 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
900 break;
901 }
902 return Val;
903}
904
Alex Bradburydc31c612017-12-11 12:49:02 +0000905// The caller is responsible for loading the full value if the argument is
906// passed with CCValAssign::Indirect.
907static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
908 const CCValAssign &VA, const SDLoc &DL) {
909 MachineFunction &MF = DAG.getMachineFunction();
910 MachineRegisterInfo &RegInfo = MF.getRegInfo();
911 EVT LocVT = VA.getLocVT();
912 SDValue Val;
913
914 unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
915 RegInfo.addLiveIn(VA.getLocReg(), VReg);
916 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
917
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +0000918 if (VA.getLocInfo() == CCValAssign::Indirect)
919 return Val;
920
921 return convertLocVTToValVT(DAG, Val, VA, DL);
922}
923
924static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val,
925 const CCValAssign &VA, const SDLoc &DL) {
926 EVT LocVT = VA.getLocVT();
927
Alex Bradburydc31c612017-12-11 12:49:02 +0000928 switch (VA.getLocInfo()) {
929 default:
930 llvm_unreachable("Unexpected CCValAssign::LocInfo");
931 case CCValAssign::Full:
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000932 break;
933 case CCValAssign::BCvt:
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +0000934 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000935 break;
Alex Bradburydc31c612017-12-11 12:49:02 +0000936 }
Alex Bradbury76c29ee2018-03-20 12:45:35 +0000937 return Val;
Alex Bradburydc31c612017-12-11 12:49:02 +0000938}
939
940// The caller is responsible for loading the full value if the argument is
941// passed with CCValAssign::Indirect.
942static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
943 const CCValAssign &VA, const SDLoc &DL) {
944 MachineFunction &MF = DAG.getMachineFunction();
945 MachineFrameInfo &MFI = MF.getFrameInfo();
946 EVT LocVT = VA.getLocVT();
947 EVT ValVT = VA.getValVT();
948 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
949 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
950 VA.getLocMemOffset(), /*Immutable=*/true);
951 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
952 SDValue Val;
953
954 ISD::LoadExtType ExtType;
955 switch (VA.getLocInfo()) {
956 default:
957 llvm_unreachable("Unexpected CCValAssign::LocInfo");
958 case CCValAssign::Full:
959 case CCValAssign::Indirect:
960 ExtType = ISD::NON_EXTLOAD;
961 break;
962 }
963 Val = DAG.getExtLoad(
964 ExtType, DL, LocVT, Chain, FIN,
965 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
966 return Val;
967}
Alex Bradbury89718422017-10-19 21:37:38 +0000968
Alex Bradbury0b4175f2018-04-12 05:34:25 +0000969static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
970 const CCValAssign &VA, const SDLoc &DL) {
971 assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
972 "Unexpected VA");
973 MachineFunction &MF = DAG.getMachineFunction();
974 MachineFrameInfo &MFI = MF.getFrameInfo();
975 MachineRegisterInfo &RegInfo = MF.getRegInfo();
976
977 if (VA.isMemLoc()) {
978 // f64 is passed on the stack.
979 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
980 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
981 return DAG.getLoad(MVT::f64, DL, Chain, FIN,
982 MachinePointerInfo::getFixedStack(MF, FI));
983 }
984
985 assert(VA.isRegLoc() && "Expected register VA assignment");
986
987 unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
988 RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
989 SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
990 SDValue Hi;
991 if (VA.getLocReg() == RISCV::X17) {
992 // Second half of f64 is passed on the stack.
993 int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
994 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
995 Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
996 MachinePointerInfo::getFixedStack(MF, FI));
997 } else {
998 // Second half of f64 is passed in another GPR.
999 unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
1000 RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
1001 Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
1002 }
1003 return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1004}
1005
Alex Bradbury89718422017-10-19 21:37:38 +00001006// Transform physical registers into virtual registers.
1007SDValue RISCVTargetLowering::LowerFormalArguments(
1008 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1009 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1010 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1011
1012 switch (CallConv) {
1013 default:
1014 report_fatal_error("Unsupported calling convention");
1015 case CallingConv::C:
Alex Bradburya3376752017-11-08 13:41:21 +00001016 case CallingConv::Fast:
Alex Bradbury89718422017-10-19 21:37:38 +00001017 break;
1018 }
1019
1020 MachineFunction &MF = DAG.getMachineFunction();
Ana Pazos2e4106b2018-07-26 17:49:43 +00001021
1022 const Function &Func = MF.getFunction();
1023 if (Func.hasFnAttribute("interrupt")) {
1024 if (!Func.arg_empty())
1025 report_fatal_error(
1026 "Functions with the interrupt attribute cannot have arguments!");
1027
1028 StringRef Kind =
1029 MF.getFunction().getFnAttribute("interrupt").getValueAsString();
1030
1031 if (!(Kind == "user" || Kind == "supervisor" || Kind == "machine"))
1032 report_fatal_error(
1033 "Function interrupt attribute argument not supported!");
1034 }
1035
Alex Bradburydc31c612017-12-11 12:49:02 +00001036 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001037 MVT XLenVT = Subtarget.getXLenVT();
1038 unsigned XLenInBytes = Subtarget.getXLen() / 8;
1039 // Used with vargs to acumulate store chains.
1040 std::vector<SDValue> OutChains;
Alex Bradbury89718422017-10-19 21:37:38 +00001041
1042 // Assign locations to all of the incoming arguments.
1043 SmallVector<CCValAssign, 16> ArgLocs;
1044 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +00001045 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
Alex Bradbury89718422017-10-19 21:37:38 +00001046
Alex Bradburydc31c612017-12-11 12:49:02 +00001047 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1048 CCValAssign &VA = ArgLocs[i];
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001049 assert(VA.getLocVT() == XLenVT && "Unhandled argument type");
Alex Bradburydc31c612017-12-11 12:49:02 +00001050 SDValue ArgValue;
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001051 // Passing f64 on RV32D with a soft float ABI must be handled as a special
1052 // case.
1053 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
1054 ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
1055 else if (VA.isRegLoc())
Alex Bradburydc31c612017-12-11 12:49:02 +00001056 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
1057 else
1058 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
Alex Bradbury89718422017-10-19 21:37:38 +00001059
Alex Bradburydc31c612017-12-11 12:49:02 +00001060 if (VA.getLocInfo() == CCValAssign::Indirect) {
1061 // If the original argument was split and passed by reference (e.g. i128
1062 // on RV32), we need to load all parts of it here (using the same
1063 // address).
1064 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1065 MachinePointerInfo()));
1066 unsigned ArgIndex = Ins[i].OrigArgIndex;
1067 assert(Ins[i].PartOffset == 0);
1068 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
1069 CCValAssign &PartVA = ArgLocs[i + 1];
1070 unsigned PartOffset = Ins[i + 1].PartOffset;
1071 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1072 DAG.getIntPtrConstant(PartOffset, DL));
1073 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1074 MachinePointerInfo()));
1075 ++i;
1076 }
1077 continue;
Alex Bradbury89718422017-10-19 21:37:38 +00001078 }
Alex Bradburydc31c612017-12-11 12:49:02 +00001079 InVals.push_back(ArgValue);
Alex Bradbury89718422017-10-19 21:37:38 +00001080 }
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001081
1082 if (IsVarArg) {
1083 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
1084 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
1085 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1086 MachineFrameInfo &MFI = MF.getFrameInfo();
1087 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1088 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1089
1090 // Offset of the first variable argument from stack pointer, and size of
1091 // the vararg save area. For now, the varargs save area is either zero or
1092 // large enough to hold a0-a7.
1093 int VaArgOffset, VarArgsSaveSize;
1094
1095 // If all registers are allocated, then all varargs must be passed on the
1096 // stack and we don't need to save any argregs.
1097 if (ArgRegs.size() == Idx) {
1098 VaArgOffset = CCInfo.getNextStackOffset();
1099 VarArgsSaveSize = 0;
1100 } else {
1101 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
1102 VaArgOffset = -VarArgsSaveSize;
1103 }
1104
1105 // Record the frame index of the first variable argument
1106 // which is a value necessary to VASTART.
1107 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
1108 RVFI->setVarArgsFrameIndex(FI);
1109
1110 // If saving an odd number of registers then create an extra stack slot to
1111 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
1112 // offsets to even-numbered registered remain 2*XLEN-aligned.
1113 if (Idx % 2) {
1114 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes,
1115 true);
1116 VarArgsSaveSize += XLenInBytes;
1117 }
1118
1119 // Copy the integer registers that may have been used for passing varargs
1120 // to the vararg save area.
1121 for (unsigned I = Idx; I < ArgRegs.size();
1122 ++I, VaArgOffset += XLenInBytes) {
1123 const unsigned Reg = RegInfo.createVirtualRegister(RC);
1124 RegInfo.addLiveIn(ArgRegs[I], Reg);
1125 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
1126 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
1127 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1128 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
1129 MachinePointerInfo::getFixedStack(MF, FI));
1130 cast<StoreSDNode>(Store.getNode())
1131 ->getMemOperand()
1132 ->setValue((Value *)nullptr);
1133 OutChains.push_back(Store);
1134 }
1135 RVFI->setVarArgsSaveSize(VarArgsSaveSize);
1136 }
1137
1138 // All stores are grouped in one node to allow the matching between
1139 // the size of Ins and InVals. This only happens for vararg functions.
1140 if (!OutChains.empty()) {
1141 OutChains.push_back(Chain);
1142 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
1143 }
1144
Alex Bradbury89718422017-10-19 21:37:38 +00001145 return Chain;
1146}
1147
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001148/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1149/// for tail call optimization.
1150/// Note: This is modelled after ARM's IsEligibleForTailCallOptimization.
1151bool RISCVTargetLowering::IsEligibleForTailCallOptimization(
1152 CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
1153 const SmallVector<CCValAssign, 16> &ArgLocs) const {
1154
1155 auto &Callee = CLI.Callee;
1156 auto CalleeCC = CLI.CallConv;
1157 auto IsVarArg = CLI.IsVarArg;
1158 auto &Outs = CLI.Outs;
1159 auto &Caller = MF.getFunction();
1160 auto CallerCC = Caller.getCallingConv();
1161
1162 // Do not tail call opt functions with "disable-tail-calls" attribute.
1163 if (Caller.getFnAttribute("disable-tail-calls").getValueAsString() == "true")
1164 return false;
1165
1166 // Exception-handling functions need a special set of instructions to
1167 // indicate a return to the hardware. Tail-calling another function would
1168 // probably break this.
1169 // TODO: The "interrupt" attribute isn't currently defined by RISC-V. This
1170 // should be expanded as new function attributes are introduced.
1171 if (Caller.hasFnAttribute("interrupt"))
1172 return false;
1173
1174 // Do not tail call opt functions with varargs.
1175 if (IsVarArg)
1176 return false;
1177
1178 // Do not tail call opt if the stack is used to pass parameters.
1179 if (CCInfo.getNextStackOffset() != 0)
1180 return false;
1181
1182 // Do not tail call opt if any parameters need to be passed indirectly.
1183 // Since long doubles (fp128) and i128 are larger than 2*XLEN, they are
1184 // passed indirectly. So the address of the value will be passed in a
1185 // register, or if not available, then the address is put on the stack. In
1186 // order to pass indirectly, space on the stack often needs to be allocated
1187 // in order to store the value. In this case the CCInfo.getNextStackOffset()
1188 // != 0 check is not enough and we need to check if any CCValAssign ArgsLocs
1189 // are passed CCValAssign::Indirect.
1190 for (auto &VA : ArgLocs)
1191 if (VA.getLocInfo() == CCValAssign::Indirect)
1192 return false;
1193
1194 // Do not tail call opt if either caller or callee uses struct return
1195 // semantics.
1196 auto IsCallerStructRet = Caller.hasStructRetAttr();
1197 auto IsCalleeStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
1198 if (IsCallerStructRet || IsCalleeStructRet)
1199 return false;
1200
1201 // Externally-defined functions with weak linkage should not be
1202 // tail-called. The behaviour of branch instructions in this situation (as
1203 // used for tail calls) is implementation-defined, so we cannot rely on the
1204 // linker replacing the tail call with a return.
1205 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1206 const GlobalValue *GV = G->getGlobal();
1207 if (GV->hasExternalWeakLinkage())
1208 return false;
1209 }
1210
1211 // The callee has to preserve all registers the caller needs to preserve.
1212 const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
1213 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
1214 if (CalleeCC != CallerCC) {
1215 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
1216 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
1217 return false;
1218 }
1219
1220 // Byval parameters hand the function a pointer directly into the stack area
1221 // we want to reuse during a tail call. Working around this *is* possible
1222 // but less efficient and uglier in LowerCall.
1223 for (auto &Arg : Outs)
1224 if (Arg.Flags.isByVal())
1225 return false;
1226
1227 return true;
1228}
1229
Alex Bradburya3376752017-11-08 13:41:21 +00001230// Lower a call to a callseq_start + CALL + callseq_end chain, and add input
1231// and output parameter nodes.
1232SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
1233 SmallVectorImpl<SDValue> &InVals) const {
1234 SelectionDAG &DAG = CLI.DAG;
1235 SDLoc &DL = CLI.DL;
1236 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1237 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1238 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1239 SDValue Chain = CLI.Chain;
1240 SDValue Callee = CLI.Callee;
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001241 bool &IsTailCall = CLI.IsTailCall;
Alex Bradburya3376752017-11-08 13:41:21 +00001242 CallingConv::ID CallConv = CLI.CallConv;
1243 bool IsVarArg = CLI.IsVarArg;
1244 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Alex Bradburydc31c612017-12-11 12:49:02 +00001245 MVT XLenVT = Subtarget.getXLenVT();
Alex Bradburya3376752017-11-08 13:41:21 +00001246
Alex Bradburya3376752017-11-08 13:41:21 +00001247 MachineFunction &MF = DAG.getMachineFunction();
1248
1249 // Analyze the operands of the call, assigning locations to each operand.
1250 SmallVector<CCValAssign, 16> ArgLocs;
1251 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001252 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
Alex Bradburya3376752017-11-08 13:41:21 +00001253
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001254 // Check if it's really possible to do a tail call.
1255 if (IsTailCall)
1256 IsTailCall = IsEligibleForTailCallOptimization(ArgCCInfo, CLI, MF,
1257 ArgLocs);
1258
1259 if (IsTailCall)
1260 ++NumTailCalls;
1261 else if (CLI.CS && CLI.CS.isMustTailCall())
1262 report_fatal_error("failed to perform tail call elimination on a call "
1263 "site marked musttail");
1264
Alex Bradburya3376752017-11-08 13:41:21 +00001265 // Get a count of how many bytes are to be pushed on the stack.
1266 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1267
Alex Bradburydc31c612017-12-11 12:49:02 +00001268 // Create local copies for byval args
1269 SmallVector<SDValue, 8> ByValArgs;
1270 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1271 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1272 if (!Flags.isByVal())
Alex Bradburya3376752017-11-08 13:41:21 +00001273 continue;
Alex Bradburydc31c612017-12-11 12:49:02 +00001274
1275 SDValue Arg = OutVals[i];
1276 unsigned Size = Flags.getByValSize();
1277 unsigned Align = Flags.getByValAlign();
1278
1279 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
1280 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
1281 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
1282
1283 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
1284 /*IsVolatile=*/false,
1285 /*AlwaysInline=*/false,
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001286 IsTailCall, MachinePointerInfo(),
Alex Bradburydc31c612017-12-11 12:49:02 +00001287 MachinePointerInfo());
1288 ByValArgs.push_back(FIPtr);
Alex Bradburya3376752017-11-08 13:41:21 +00001289 }
1290
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001291 if (!IsTailCall)
1292 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
Alex Bradburya3376752017-11-08 13:41:21 +00001293
1294 // Copy argument values to their designated locations.
1295 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Alex Bradburydc31c612017-12-11 12:49:02 +00001296 SmallVector<SDValue, 8> MemOpChains;
Alex Bradburya3376752017-11-08 13:41:21 +00001297 SDValue StackPtr;
Alex Bradburydc31c612017-12-11 12:49:02 +00001298 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
1299 CCValAssign &VA = ArgLocs[i];
1300 SDValue ArgValue = OutVals[i];
1301 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Alex Bradburya3376752017-11-08 13:41:21 +00001302
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001303 // Handle passing f64 on RV32D with a soft float ABI as a special case.
1304 bool IsF64OnRV32DSoftABI =
1305 VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
1306 if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
1307 SDValue SplitF64 = DAG.getNode(
1308 RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
1309 SDValue Lo = SplitF64.getValue(0);
1310 SDValue Hi = SplitF64.getValue(1);
1311
1312 unsigned RegLo = VA.getLocReg();
1313 RegsToPass.push_back(std::make_pair(RegLo, Lo));
1314
1315 if (RegLo == RISCV::X17) {
1316 // Second half of f64 is passed on the stack.
1317 // Work out the address of the stack slot.
1318 if (!StackPtr.getNode())
1319 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1320 // Emit the store.
1321 MemOpChains.push_back(
1322 DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
1323 } else {
1324 // Second half of f64 is passed in another GPR.
1325 unsigned RegHigh = RegLo + 1;
1326 RegsToPass.push_back(std::make_pair(RegHigh, Hi));
1327 }
1328 continue;
1329 }
1330
1331 // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
1332 // as any other MemLoc.
1333
Alex Bradburya3376752017-11-08 13:41:21 +00001334 // Promote the value if needed.
Alex Bradburydc31c612017-12-11 12:49:02 +00001335 // For now, only handle fully promoted and indirect arguments.
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +00001336 if (VA.getLocInfo() == CCValAssign::Indirect) {
Alex Bradburydc31c612017-12-11 12:49:02 +00001337 // Store the argument in a stack slot and pass its address.
1338 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
1339 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1340 MemOpChains.push_back(
1341 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1342 MachinePointerInfo::getFixedStack(MF, FI)));
1343 // If the original argument was split (e.g. i128), we need
1344 // to store all parts of it here (and pass just one address).
1345 unsigned ArgIndex = Outs[i].OrigArgIndex;
1346 assert(Outs[i].PartOffset == 0);
1347 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
1348 SDValue PartValue = OutVals[i + 1];
1349 unsigned PartOffset = Outs[i + 1].PartOffset;
1350 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1351 DAG.getIntPtrConstant(PartOffset, DL));
1352 MemOpChains.push_back(
1353 DAG.getStore(Chain, DL, PartValue, Address,
1354 MachinePointerInfo::getFixedStack(MF, FI)));
1355 ++i;
1356 }
1357 ArgValue = SpillSlot;
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +00001358 } else {
1359 ArgValue = convertValVTToLocVT(DAG, ArgValue, VA, DL);
Alex Bradburya3376752017-11-08 13:41:21 +00001360 }
1361
Alex Bradburydc31c612017-12-11 12:49:02 +00001362 // Use local copy if it is a byval arg.
1363 if (Flags.isByVal())
1364 ArgValue = ByValArgs[j++];
1365
Alex Bradburya3376752017-11-08 13:41:21 +00001366 if (VA.isRegLoc()) {
1367 // Queue up the argument copies and emit them at the end.
1368 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1369 } else {
1370 assert(VA.isMemLoc() && "Argument not register or memory");
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001371 assert(!IsTailCall && "Tail call not allowed if stack is used "
1372 "for passing parameters");
Alex Bradburydc31c612017-12-11 12:49:02 +00001373
1374 // Work out the address of the stack slot.
1375 if (!StackPtr.getNode())
1376 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
1377 SDValue Address =
1378 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
1379 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
1380
1381 // Emit the store.
1382 MemOpChains.push_back(
1383 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Alex Bradburya3376752017-11-08 13:41:21 +00001384 }
1385 }
1386
Alex Bradburydc31c612017-12-11 12:49:02 +00001387 // Join the stores, which are independent of one another.
1388 if (!MemOpChains.empty())
1389 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1390
Alex Bradburya3376752017-11-08 13:41:21 +00001391 SDValue Glue;
1392
1393 // Build a sequence of copy-to-reg nodes, chained and glued together.
1394 for (auto &Reg : RegsToPass) {
1395 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
1396 Glue = Chain.getValue(1);
1397 }
1398
Shiva Chend58bd8d2018-04-25 14:19:12 +00001399 // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
1400 // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
1401 // split it and then direct call can be matched by PseudoCALL.
1402 if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
1403 Callee = DAG.getTargetGlobalAddress(S->getGlobal(), DL, PtrVT, 0, 0);
1404 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1405 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, 0);
Alex Bradburya3376752017-11-08 13:41:21 +00001406 }
1407
1408 // The first call operand is the chain and the second is the target address.
1409 SmallVector<SDValue, 8> Ops;
1410 Ops.push_back(Chain);
1411 Ops.push_back(Callee);
1412
1413 // Add argument registers to the end of the list so that they are
1414 // known live into the call.
1415 for (auto &Reg : RegsToPass)
1416 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
1417
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001418 if (!IsTailCall) {
1419 // Add a register mask operand representing the call-preserved registers.
1420 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1421 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
1422 assert(Mask && "Missing call preserved mask for calling convention");
1423 Ops.push_back(DAG.getRegisterMask(Mask));
1424 }
Alex Bradburya3376752017-11-08 13:41:21 +00001425
1426 // Glue the call to the argument copies, if any.
1427 if (Glue.getNode())
1428 Ops.push_back(Glue);
1429
1430 // Emit the call.
1431 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001432
1433 if (IsTailCall) {
1434 MF.getFrameInfo().setHasTailCall();
1435 return DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops);
1436 }
1437
Alex Bradburya3376752017-11-08 13:41:21 +00001438 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
1439 Glue = Chain.getValue(1);
1440
1441 // Mark the end of the call, which is glued to the call itself.
1442 Chain = DAG.getCALLSEQ_END(Chain,
1443 DAG.getConstant(NumBytes, DL, PtrVT, true),
1444 DAG.getConstant(0, DL, PtrVT, true),
1445 Glue, DL);
1446 Glue = Chain.getValue(1);
1447
1448 // Assign locations to each value returned by this call.
1449 SmallVector<CCValAssign, 16> RVLocs;
1450 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Alex Bradburydc31c612017-12-11 12:49:02 +00001451 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
Alex Bradburya3376752017-11-08 13:41:21 +00001452
1453 // Copy all of the result registers out of their specified physreg.
1454 for (auto &VA : RVLocs) {
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001455 // Copy the value out
1456 SDValue RetValue =
1457 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
1458 // Glue the RetValue to the end of the call sequence
Alex Bradburya3376752017-11-08 13:41:21 +00001459 Chain = RetValue.getValue(1);
1460 Glue = RetValue.getValue(2);
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +00001461
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001462 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1463 assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
1464 SDValue RetValue2 =
1465 DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
1466 Chain = RetValue2.getValue(1);
1467 Glue = RetValue2.getValue(2);
1468 RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
1469 RetValue2);
1470 }
Alex Bradburya3376752017-11-08 13:41:21 +00001471
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +00001472 RetValue = convertLocVTToValVT(DAG, RetValue, VA, DL);
Alex Bradbury76c29ee2018-03-20 12:45:35 +00001473
Alex Bradburydc31c612017-12-11 12:49:02 +00001474 InVals.push_back(RetValue);
Alex Bradburya3376752017-11-08 13:41:21 +00001475 }
1476
1477 return Chain;
1478}
1479
Alex Bradburydc31c612017-12-11 12:49:02 +00001480bool RISCVTargetLowering::CanLowerReturn(
1481 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
1482 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
1483 SmallVector<CCValAssign, 16> RVLocs;
1484 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
1485 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1486 MVT VT = Outs[i].VT;
1487 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1488 if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags,
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001489 CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
Alex Bradburydc31c612017-12-11 12:49:02 +00001490 return false;
1491 }
1492 return true;
1493}
1494
Alex Bradbury89718422017-10-19 21:37:38 +00001495SDValue
1496RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1497 bool IsVarArg,
1498 const SmallVectorImpl<ISD::OutputArg> &Outs,
1499 const SmallVectorImpl<SDValue> &OutVals,
1500 const SDLoc &DL, SelectionDAG &DAG) const {
Alex Bradbury89718422017-10-19 21:37:38 +00001501 // Stores the assignment of the return value to a location.
1502 SmallVector<CCValAssign, 16> RVLocs;
1503
1504 // Info about the registers and stack slot.
1505 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
1506 *DAG.getContext());
1507
Alex Bradburyc85be0d2018-01-10 19:41:03 +00001508 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
1509 nullptr);
Alex Bradbury89718422017-10-19 21:37:38 +00001510
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001511 SDValue Glue;
Alex Bradbury89718422017-10-19 21:37:38 +00001512 SmallVector<SDValue, 4> RetOps(1, Chain);
1513
1514 // Copy the result values into the output registers.
1515 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
Alex Bradburydc31c612017-12-11 12:49:02 +00001516 SDValue Val = OutVals[i];
Alex Bradbury89718422017-10-19 21:37:38 +00001517 CCValAssign &VA = RVLocs[i];
1518 assert(VA.isRegLoc() && "Can only return in registers!");
1519
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001520 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
1521 // Handle returning f64 on RV32D with a soft float ABI.
1522 assert(VA.isRegLoc() && "Expected return via registers");
1523 SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
1524 DAG.getVTList(MVT::i32, MVT::i32), Val);
1525 SDValue Lo = SplitF64.getValue(0);
1526 SDValue Hi = SplitF64.getValue(1);
1527 unsigned RegLo = VA.getLocReg();
1528 unsigned RegHi = RegLo + 1;
1529 Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
1530 Glue = Chain.getValue(1);
1531 RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
1532 Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
1533 Glue = Chain.getValue(1);
1534 RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
1535 } else {
1536 // Handle a 'normal' return.
Alex Bradbury1dbfdeb2018-10-03 22:53:25 +00001537 Val = convertValVTToLocVT(DAG, Val, VA, DL);
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001538 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001539
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001540 // Guarantee that all emitted copies are stuck together.
1541 Glue = Chain.getValue(1);
1542 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1543 }
Alex Bradbury89718422017-10-19 21:37:38 +00001544 }
1545
1546 RetOps[0] = Chain; // Update chain.
1547
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001548 // Add the glue node if we have it.
1549 if (Glue.getNode()) {
1550 RetOps.push_back(Glue);
Alex Bradbury89718422017-10-19 21:37:38 +00001551 }
1552
Ana Pazos2e4106b2018-07-26 17:49:43 +00001553 // Interrupt service routines use different return instructions.
1554 const Function &Func = DAG.getMachineFunction().getFunction();
1555 if (Func.hasFnAttribute("interrupt")) {
1556 if (!Func.getReturnType()->isVoidTy())
1557 report_fatal_error(
1558 "Functions with the interrupt attribute must have void return type!");
1559
1560 MachineFunction &MF = DAG.getMachineFunction();
1561 StringRef Kind =
1562 MF.getFunction().getFnAttribute("interrupt").getValueAsString();
1563
1564 unsigned RetOpc;
1565 if (Kind == "user")
1566 RetOpc = RISCVISD::URET_FLAG;
1567 else if (Kind == "supervisor")
1568 RetOpc = RISCVISD::SRET_FLAG;
1569 else
1570 RetOpc = RISCVISD::MRET_FLAG;
1571
1572 return DAG.getNode(RetOpc, DL, MVT::Other, RetOps);
1573 }
1574
Alex Bradbury89718422017-10-19 21:37:38 +00001575 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
1576}
1577
1578const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
1579 switch ((RISCVISD::NodeType)Opcode) {
1580 case RISCVISD::FIRST_NUMBER:
1581 break;
1582 case RISCVISD::RET_FLAG:
1583 return "RISCVISD::RET_FLAG";
Ana Pazos2e4106b2018-07-26 17:49:43 +00001584 case RISCVISD::URET_FLAG:
1585 return "RISCVISD::URET_FLAG";
1586 case RISCVISD::SRET_FLAG:
1587 return "RISCVISD::SRET_FLAG";
1588 case RISCVISD::MRET_FLAG:
1589 return "RISCVISD::MRET_FLAG";
Alex Bradburya3376752017-11-08 13:41:21 +00001590 case RISCVISD::CALL:
1591 return "RISCVISD::CALL";
Alex Bradbury65385162017-11-21 07:51:32 +00001592 case RISCVISD::SELECT_CC:
1593 return "RISCVISD::SELECT_CC";
Alex Bradbury0b4175f2018-04-12 05:34:25 +00001594 case RISCVISD::BuildPairF64:
1595 return "RISCVISD::BuildPairF64";
1596 case RISCVISD::SplitF64:
1597 return "RISCVISD::SplitF64";
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +00001598 case RISCVISD::TAIL:
1599 return "RISCVISD::TAIL";
Alex Bradbury89718422017-10-19 21:37:38 +00001600 }
1601 return nullptr;
1602}
Alex Bradbury9330e642018-01-10 20:05:09 +00001603
1604std::pair<unsigned, const TargetRegisterClass *>
1605RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
1606 StringRef Constraint,
1607 MVT VT) const {
1608 // First, see if this is a constraint that directly corresponds to a
1609 // RISCV register class.
1610 if (Constraint.size() == 1) {
1611 switch (Constraint[0]) {
1612 case 'r':
1613 return std::make_pair(0U, &RISCV::GPRRegClass);
1614 default:
1615 break;
1616 }
1617 }
1618
1619 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
1620}
Alex Bradbury96f492d2018-06-13 12:04:51 +00001621
1622Instruction *RISCVTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
1623 Instruction *Inst,
1624 AtomicOrdering Ord) const {
1625 if (isa<LoadInst>(Inst) && Ord == AtomicOrdering::SequentiallyConsistent)
1626 return Builder.CreateFence(Ord);
1627 if (isa<StoreInst>(Inst) && isReleaseOrStronger(Ord))
1628 return Builder.CreateFence(AtomicOrdering::Release);
1629 return nullptr;
1630}
1631
1632Instruction *RISCVTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
1633 Instruction *Inst,
1634 AtomicOrdering Ord) const {
1635 if (isa<LoadInst>(Inst) && isAcquireOrStronger(Ord))
1636 return Builder.CreateFence(AtomicOrdering::Acquire);
1637 return nullptr;
1638}
Alex Bradbury21aea512018-09-19 10:54:22 +00001639
1640TargetLowering::AtomicExpansionKind
1641RISCVTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
1642 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
1643 if (Size == 8 || Size == 16)
1644 return AtomicExpansionKind::MaskedIntrinsic;
1645 return AtomicExpansionKind::None;
1646}
1647
1648static Intrinsic::ID
1649getIntrinsicForMaskedAtomicRMWBinOp32(AtomicRMWInst::BinOp BinOp) {
1650 switch (BinOp) {
1651 default:
1652 llvm_unreachable("Unexpected AtomicRMW BinOp");
1653 case AtomicRMWInst::Xchg:
1654 return Intrinsic::riscv_masked_atomicrmw_xchg_i32;
1655 case AtomicRMWInst::Add:
1656 return Intrinsic::riscv_masked_atomicrmw_add_i32;
1657 case AtomicRMWInst::Sub:
1658 return Intrinsic::riscv_masked_atomicrmw_sub_i32;
1659 case AtomicRMWInst::Nand:
1660 return Intrinsic::riscv_masked_atomicrmw_nand_i32;
1661 case AtomicRMWInst::Max:
1662 return Intrinsic::riscv_masked_atomicrmw_max_i32;
1663 case AtomicRMWInst::Min:
1664 return Intrinsic::riscv_masked_atomicrmw_min_i32;
1665 case AtomicRMWInst::UMax:
1666 return Intrinsic::riscv_masked_atomicrmw_umax_i32;
1667 case AtomicRMWInst::UMin:
1668 return Intrinsic::riscv_masked_atomicrmw_umin_i32;
1669 }
1670}
1671
1672Value *RISCVTargetLowering::emitMaskedAtomicRMWIntrinsic(
1673 IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr,
1674 Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const {
1675 Value *Ordering = Builder.getInt32(static_cast<uint32_t>(AI->getOrdering()));
1676 Type *Tys[] = {AlignedAddr->getType()};
1677 Function *LrwOpScwLoop = Intrinsic::getDeclaration(
1678 AI->getModule(),
1679 getIntrinsicForMaskedAtomicRMWBinOp32(AI->getOperation()), Tys);
1680
1681 // Must pass the shift amount needed to sign extend the loaded value prior
1682 // to performing a signed comparison for min/max. ShiftAmt is the number of
1683 // bits to shift the value into position. Pass XLen-ShiftAmt-ValWidth, which
1684 // is the number of bits to left+right shift the value in order to
1685 // sign-extend.
1686 if (AI->getOperation() == AtomicRMWInst::Min ||
1687 AI->getOperation() == AtomicRMWInst::Max) {
1688 const DataLayout &DL = AI->getModule()->getDataLayout();
1689 unsigned ValWidth =
1690 DL.getTypeStoreSizeInBits(AI->getValOperand()->getType());
1691 Value *SextShamt = Builder.CreateSub(
1692 Builder.getInt32(Subtarget.getXLen() - ValWidth), ShiftAmt);
1693 return Builder.CreateCall(LrwOpScwLoop,
1694 {AlignedAddr, Incr, Mask, SextShamt, Ordering});
1695 }
1696
1697 return Builder.CreateCall(LrwOpScwLoop, {AlignedAddr, Incr, Mask, Ordering});
1698}