1 //===-- 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" 17 #include "RISCVMachineFunctionInfo.h" 18 #include "RISCVRegisterInfo.h" 19 #include "RISCVSubtarget.h" 20 #include "RISCVTargetMachine.h" 21 #include "llvm/CodeGen/CallingConvLower.h" 22 #include "llvm/CodeGen/MachineFrameInfo.h" 23 #include "llvm/CodeGen/MachineFunction.h" 24 #include "llvm/CodeGen/MachineInstrBuilder.h" 25 #include "llvm/CodeGen/MachineRegisterInfo.h" 26 #include "llvm/CodeGen/SelectionDAGISel.h" 27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 28 #include "llvm/CodeGen/ValueTypes.h" 29 #include "llvm/IR/DiagnosticInfo.h" 30 #include "llvm/IR/DiagnosticPrinter.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/raw_ostream.h" 34 35 using namespace llvm; 36 37 #define DEBUG_TYPE "riscv-lower" 38 39 RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM, 40 const RISCVSubtarget &STI) 41 : TargetLowering(TM), Subtarget(STI) { 42 43 MVT XLenVT = Subtarget.getXLenVT(); 44 45 // Set up the register classes. 46 addRegisterClass(XLenVT, &RISCV::GPRRegClass); 47 48 if (Subtarget.hasStdExtF()) 49 addRegisterClass(MVT::f32, &RISCV::FPR32RegClass); 50 51 // Compute derived properties from the register classes. 52 computeRegisterProperties(STI.getRegisterInfo()); 53 54 setStackPointerRegisterToSaveRestore(RISCV::X2); 55 56 for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) 57 setLoadExtAction(N, XLenVT, MVT::i1, Promote); 58 59 // TODO: add all necessary setOperationAction calls. 60 setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand); 61 62 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 63 setOperationAction(ISD::BR_CC, XLenVT, Expand); 64 setOperationAction(ISD::SELECT, XLenVT, Custom); 65 setOperationAction(ISD::SELECT_CC, XLenVT, Expand); 66 67 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); 68 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); 69 70 setOperationAction(ISD::VASTART, MVT::Other, Custom); 71 setOperationAction(ISD::VAARG, MVT::Other, Expand); 72 setOperationAction(ISD::VACOPY, MVT::Other, Expand); 73 setOperationAction(ISD::VAEND, MVT::Other, Expand); 74 75 for (auto VT : {MVT::i1, MVT::i8, MVT::i16}) 76 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); 77 78 setOperationAction(ISD::ADDC, XLenVT, Expand); 79 setOperationAction(ISD::ADDE, XLenVT, Expand); 80 setOperationAction(ISD::SUBC, XLenVT, Expand); 81 setOperationAction(ISD::SUBE, XLenVT, Expand); 82 83 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 } 92 93 setOperationAction(ISD::SDIVREM, XLenVT, Expand); 94 setOperationAction(ISD::UDIVREM, XLenVT, Expand); 95 setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand); 96 setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand); 97 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 109 if (Subtarget.hasStdExtF()) { 110 setOperationAction(ISD::FMINNUM, MVT::f32, Legal); 111 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); 112 for (auto CC : 113 {ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETO, ISD::SETUEQ, 114 ISD::SETUGT, ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE, 115 ISD::SETGT, ISD::SETGE, ISD::SETNE}) 116 setCondCodeAction(CC, MVT::f32, Expand); 117 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); 118 setOperationAction(ISD::SELECT, MVT::f32, Custom); 119 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 120 } 121 122 setOperationAction(ISD::GlobalAddress, XLenVT, Custom); 123 setOperationAction(ISD::BlockAddress, XLenVT, Custom); 124 setOperationAction(ISD::ConstantPool, XLenVT, Custom); 125 126 setBooleanContents(ZeroOrOneBooleanContent); 127 128 // Function alignments (log2). 129 setMinFunctionAlignment(3); 130 setPrefFunctionAlignment(3); 131 132 // Effectively disable jump table generation. 133 setMinimumJumpTableEntries(INT_MAX); 134 } 135 136 EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, 137 EVT VT) const { 138 if (!VT.isVector()) 139 return getPointerTy(DL); 140 return VT.changeVectorElementTypeToInteger(); 141 } 142 143 // Changes the condition code and swaps operands if necessary, so the SetCC 144 // operation matches one of the comparisons supported directly in the RISC-V 145 // ISA. 146 static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) { 147 switch (CC) { 148 default: 149 break; 150 case ISD::SETGT: 151 case ISD::SETLE: 152 case ISD::SETUGT: 153 case ISD::SETULE: 154 CC = ISD::getSetCCSwappedOperands(CC); 155 std::swap(LHS, RHS); 156 break; 157 } 158 } 159 160 // Return the RISC-V branch opcode that matches the given DAG integer 161 // condition code. The CondCode must be one of those supported by the RISC-V 162 // ISA (see normaliseSetCC). 163 static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) { 164 switch (CC) { 165 default: 166 llvm_unreachable("Unsupported CondCode"); 167 case ISD::SETEQ: 168 return RISCV::BEQ; 169 case ISD::SETNE: 170 return RISCV::BNE; 171 case ISD::SETLT: 172 return RISCV::BLT; 173 case ISD::SETGE: 174 return RISCV::BGE; 175 case ISD::SETULT: 176 return RISCV::BLTU; 177 case ISD::SETUGE: 178 return RISCV::BGEU; 179 } 180 } 181 182 SDValue RISCVTargetLowering::LowerOperation(SDValue Op, 183 SelectionDAG &DAG) const { 184 switch (Op.getOpcode()) { 185 default: 186 report_fatal_error("unimplemented operand"); 187 case ISD::GlobalAddress: 188 return lowerGlobalAddress(Op, DAG); 189 case ISD::BlockAddress: 190 return lowerBlockAddress(Op, DAG); 191 case ISD::ConstantPool: 192 return lowerConstantPool(Op, DAG); 193 case ISD::SELECT: 194 return lowerSELECT(Op, DAG); 195 case ISD::VASTART: 196 return lowerVASTART(Op, DAG); 197 case ISD::FRAMEADDR: 198 return LowerFRAMEADDR(Op, DAG); 199 case ISD::RETURNADDR: 200 return LowerRETURNADDR(Op, DAG); 201 } 202 } 203 204 SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op, 205 SelectionDAG &DAG) const { 206 SDLoc DL(Op); 207 EVT Ty = Op.getValueType(); 208 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op); 209 const GlobalValue *GV = N->getGlobal(); 210 int64_t Offset = N->getOffset(); 211 212 if (isPositionIndependent() || Subtarget.is64Bit()) 213 report_fatal_error("Unable to lowerGlobalAddress"); 214 215 SDValue GAHi = 216 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_HI); 217 SDValue GALo = 218 DAG.getTargetGlobalAddress(GV, DL, Ty, Offset, RISCVII::MO_LO); 219 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0); 220 SDValue MNLo = 221 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0); 222 return MNLo; 223 } 224 225 SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op, 226 SelectionDAG &DAG) const { 227 SDLoc DL(Op); 228 EVT Ty = Op.getValueType(); 229 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op); 230 const BlockAddress *BA = N->getBlockAddress(); 231 int64_t Offset = N->getOffset(); 232 233 if (isPositionIndependent() || Subtarget.is64Bit()) 234 report_fatal_error("Unable to lowerBlockAddress"); 235 236 SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI); 237 SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO); 238 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0); 239 SDValue MNLo = 240 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0); 241 return MNLo; 242 } 243 244 SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op, 245 SelectionDAG &DAG) const { 246 SDLoc DL(Op); 247 EVT Ty = Op.getValueType(); 248 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op); 249 const Constant *CPA = N->getConstVal(); 250 int64_t Offset = N->getOffset(); 251 unsigned Alignment = N->getAlignment(); 252 253 if (!isPositionIndependent()) { 254 SDValue CPAHi = 255 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI); 256 SDValue CPALo = 257 DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO); 258 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0); 259 SDValue MNLo = 260 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0); 261 return MNLo; 262 } else { 263 report_fatal_error("Unable to lowerConstantPool"); 264 } 265 } 266 267 SDValue RISCVTargetLowering::lowerExternalSymbol(SDValue Op, 268 SelectionDAG &DAG) const { 269 SDLoc DL(Op); 270 EVT Ty = Op.getValueType(); 271 ExternalSymbolSDNode *N = cast<ExternalSymbolSDNode>(Op); 272 const char *Sym = N->getSymbol(); 273 274 // TODO: should also handle gp-relative loads. 275 276 if (isPositionIndependent() || Subtarget.is64Bit()) 277 report_fatal_error("Unable to lowerExternalSymbol"); 278 279 SDValue GAHi = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_HI); 280 SDValue GALo = DAG.getTargetExternalSymbol(Sym, Ty, RISCVII::MO_LO); 281 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0); 282 SDValue MNLo = 283 SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0); 284 return MNLo; 285 } 286 287 SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const { 288 SDValue CondV = Op.getOperand(0); 289 SDValue TrueV = Op.getOperand(1); 290 SDValue FalseV = Op.getOperand(2); 291 SDLoc DL(Op); 292 MVT XLenVT = Subtarget.getXLenVT(); 293 294 // If the result type is XLenVT and CondV is the output of a SETCC node 295 // which also operated on XLenVT inputs, then merge the SETCC node into the 296 // lowered RISCVISD::SELECT_CC to take advantage of the integer 297 // compare+branch instructions. i.e.: 298 // (select (setcc lhs, rhs, cc), truev, falsev) 299 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev) 300 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC && 301 CondV.getOperand(0).getSimpleValueType() == XLenVT) { 302 SDValue LHS = CondV.getOperand(0); 303 SDValue RHS = CondV.getOperand(1); 304 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2)); 305 ISD::CondCode CCVal = CC->get(); 306 307 normaliseSetCC(LHS, RHS, CCVal); 308 309 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT); 310 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 311 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV}; 312 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops); 313 } 314 315 // Otherwise: 316 // (select condv, truev, falsev) 317 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev) 318 SDValue Zero = DAG.getConstant(0, DL, XLenVT); 319 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT); 320 321 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 322 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV}; 323 324 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops); 325 } 326 327 SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const { 328 MachineFunction &MF = DAG.getMachineFunction(); 329 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>(); 330 331 SDLoc DL(Op); 332 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), 333 getPointerTy(MF.getDataLayout())); 334 335 // vastart just stores the address of the VarArgsFrameIndex slot into the 336 // memory location argument. 337 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 338 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1), 339 MachinePointerInfo(SV)); 340 } 341 342 SDValue RISCVTargetLowering::LowerFRAMEADDR(SDValue Op, 343 SelectionDAG &DAG) const { 344 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo(); 345 MachineFunction &MF = DAG.getMachineFunction(); 346 MachineFrameInfo &MFI = MF.getFrameInfo(); 347 MFI.setFrameAddressIsTaken(true); 348 unsigned FrameReg = RI.getFrameRegister(MF); 349 int XLenInBytes = Subtarget.getXLen() / 8; 350 351 EVT VT = Op.getValueType(); 352 SDLoc DL(Op); 353 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT); 354 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 355 while (Depth--) { 356 int Offset = -(XLenInBytes * 2); 357 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr, 358 DAG.getIntPtrConstant(Offset, DL)); 359 FrameAddr = 360 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo()); 361 } 362 return FrameAddr; 363 } 364 365 SDValue RISCVTargetLowering::LowerRETURNADDR(SDValue Op, 366 SelectionDAG &DAG) const { 367 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo(); 368 MachineFunction &MF = DAG.getMachineFunction(); 369 MachineFrameInfo &MFI = MF.getFrameInfo(); 370 MFI.setReturnAddressIsTaken(true); 371 MVT XLenVT = Subtarget.getXLenVT(); 372 int XLenInBytes = Subtarget.getXLen() / 8; 373 374 if (verifyReturnAddressArgumentIsConstant(Op, DAG)) 375 return SDValue(); 376 377 EVT VT = Op.getValueType(); 378 SDLoc DL(Op); 379 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 380 if (Depth) { 381 int Off = -XLenInBytes; 382 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); 383 SDValue Offset = DAG.getConstant(Off, DL, VT); 384 return DAG.getLoad(VT, DL, DAG.getEntryNode(), 385 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset), 386 MachinePointerInfo()); 387 } 388 389 // Return the value of the return address register, marking it an implicit 390 // live-in. 391 unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT)); 392 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT); 393 } 394 395 MachineBasicBlock * 396 RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, 397 MachineBasicBlock *BB) const { 398 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo(); 399 DebugLoc DL = MI.getDebugLoc(); 400 401 switch (MI.getOpcode()) { 402 default: 403 llvm_unreachable("Unexpected instr type to insert"); 404 case RISCV::Select_GPR_Using_CC_GPR: 405 case RISCV::Select_FPR32_Using_CC_GPR: 406 break; 407 } 408 409 // To "insert" a SELECT instruction, we actually have to insert the triangle 410 // control-flow pattern. The incoming instruction knows the destination vreg 411 // to set, the condition code register to branch on, the true/false values to 412 // select between, and the condcode to use to select the appropriate branch. 413 // 414 // We produce the following control flow: 415 // HeadMBB 416 // | \ 417 // | IfFalseMBB 418 // | / 419 // TailMBB 420 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 421 MachineFunction::iterator I = ++BB->getIterator(); 422 423 MachineBasicBlock *HeadMBB = BB; 424 MachineFunction *F = BB->getParent(); 425 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB); 426 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB); 427 428 F->insert(I, IfFalseMBB); 429 F->insert(I, TailMBB); 430 // Move all remaining instructions to TailMBB. 431 TailMBB->splice(TailMBB->begin(), HeadMBB, 432 std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end()); 433 // Update machine-CFG edges by transferring all successors of the current 434 // block to the new block which will contain the Phi node for the select. 435 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB); 436 // Set the successors for HeadMBB. 437 HeadMBB->addSuccessor(IfFalseMBB); 438 HeadMBB->addSuccessor(TailMBB); 439 440 // Insert appropriate branch. 441 unsigned LHS = MI.getOperand(1).getReg(); 442 unsigned RHS = MI.getOperand(2).getReg(); 443 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm()); 444 unsigned Opcode = getBranchOpcodeForIntCondCode(CC); 445 446 BuildMI(HeadMBB, DL, TII.get(Opcode)) 447 .addReg(LHS) 448 .addReg(RHS) 449 .addMBB(TailMBB); 450 451 // IfFalseMBB just falls through to TailMBB. 452 IfFalseMBB->addSuccessor(TailMBB); 453 454 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ] 455 BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI), 456 MI.getOperand(0).getReg()) 457 .addReg(MI.getOperand(4).getReg()) 458 .addMBB(HeadMBB) 459 .addReg(MI.getOperand(5).getReg()) 460 .addMBB(IfFalseMBB); 461 462 MI.eraseFromParent(); // The pseudo instruction is gone now. 463 return TailMBB; 464 } 465 466 // Calling Convention Implementation. 467 // The expectations for frontend ABI lowering vary from target to target. 468 // Ideally, an LLVM frontend would be able to avoid worrying about many ABI 469 // details, but this is a longer term goal. For now, we simply try to keep the 470 // role of the frontend as simple and well-defined as possible. The rules can 471 // be summarised as: 472 // * Never split up large scalar arguments. We handle them here. 473 // * If a hardfloat calling convention is being used, and the struct may be 474 // passed in a pair of registers (fp+fp, int+fp), and both registers are 475 // available, then pass as two separate arguments. If either the GPRs or FPRs 476 // are exhausted, then pass according to the rule below. 477 // * If a struct could never be passed in registers or directly in a stack 478 // slot (as it is larger than 2*XLEN and the floating point rules don't 479 // apply), then pass it using a pointer with the byval attribute. 480 // * If a struct is less than 2*XLEN, then coerce to either a two-element 481 // word-sized array or a 2*XLEN scalar (depending on alignment). 482 // * The frontend can determine whether a struct is returned by reference or 483 // not based on its size and fields. If it will be returned by reference, the 484 // frontend must modify the prototype so a pointer with the sret annotation is 485 // passed as the first argument. This is not necessary for large scalar 486 // returns. 487 // * Struct return values and varargs should be coerced to structs containing 488 // register-size fields in the same situations they would be for fixed 489 // arguments. 490 491 static const MCPhysReg ArgGPRs[] = { 492 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13, 493 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17 494 }; 495 496 // Pass a 2*XLEN argument that has been split into two XLEN values through 497 // registers or the stack as necessary. 498 static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1, 499 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2, 500 MVT ValVT2, MVT LocVT2, 501 ISD::ArgFlagsTy ArgFlags2) { 502 unsigned XLenInBytes = XLen / 8; 503 if (unsigned Reg = State.AllocateReg(ArgGPRs)) { 504 // At least one half can be passed via register. 505 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg, 506 VA1.getLocVT(), CCValAssign::Full)); 507 } else { 508 // Both halves must be passed on the stack, with proper alignment. 509 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign()); 510 State.addLoc( 511 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(), 512 State.AllocateStack(XLenInBytes, StackAlign), 513 VA1.getLocVT(), CCValAssign::Full)); 514 State.addLoc(CCValAssign::getMem( 515 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2, 516 CCValAssign::Full)); 517 return false; 518 } 519 520 if (unsigned Reg = State.AllocateReg(ArgGPRs)) { 521 // The second half can also be passed via register. 522 State.addLoc( 523 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full)); 524 } else { 525 // The second half is passed via the stack, without additional alignment. 526 State.addLoc(CCValAssign::getMem( 527 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2, 528 CCValAssign::Full)); 529 } 530 531 return false; 532 } 533 534 // Implements the RISC-V calling convention. Returns true upon failure. 535 static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT, 536 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, 537 CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) { 538 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits(); 539 assert(XLen == 32 || XLen == 64); 540 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64; 541 if (ValVT == MVT::f32) { 542 LocVT = MVT::i32; 543 LocInfo = CCValAssign::BCvt; 544 } 545 assert(LocVT == XLenVT && "Unexpected LocVT"); 546 547 // Any return value split in to more than two values can't be returned 548 // directly. 549 if (IsRet && ValNo > 1) 550 return true; 551 552 // If this is a variadic argument, the RISC-V calling convention requires 553 // that it is assigned an 'even' or 'aligned' register if it has 8-byte 554 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should 555 // be used regardless of whether the original argument was split during 556 // legalisation or not. The argument will not be passed by registers if the 557 // original type is larger than 2*XLEN, so the register alignment rule does 558 // not apply. 559 unsigned TwoXLenInBytes = (2 * XLen) / 8; 560 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes && 561 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) { 562 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs); 563 // Skip 'odd' register if necessary. 564 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1) 565 State.AllocateReg(ArgGPRs); 566 } 567 568 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs(); 569 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags = 570 State.getPendingArgFlags(); 571 572 assert(PendingLocs.size() == PendingArgFlags.size() && 573 "PendingLocs and PendingArgFlags out of sync"); 574 575 // Split arguments might be passed indirectly, so keep track of the pending 576 // values. 577 if (ArgFlags.isSplit() || !PendingLocs.empty()) { 578 LocVT = XLenVT; 579 LocInfo = CCValAssign::Indirect; 580 PendingLocs.push_back( 581 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo)); 582 PendingArgFlags.push_back(ArgFlags); 583 if (!ArgFlags.isSplitEnd()) { 584 return false; 585 } 586 } 587 588 // If the split argument only had two elements, it should be passed directly 589 // in registers or on the stack. 590 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) { 591 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()"); 592 // Apply the normal calling convention rules to the first half of the 593 // split argument. 594 CCValAssign VA = PendingLocs[0]; 595 ISD::ArgFlagsTy AF = PendingArgFlags[0]; 596 PendingLocs.clear(); 597 PendingArgFlags.clear(); 598 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT, 599 ArgFlags); 600 } 601 602 // Allocate to a register if possible, or else a stack slot. 603 unsigned Reg = State.AllocateReg(ArgGPRs); 604 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8); 605 606 // If we reach this point and PendingLocs is non-empty, we must be at the 607 // end of a split argument that must be passed indirectly. 608 if (!PendingLocs.empty()) { 609 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()"); 610 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()"); 611 612 for (auto &It : PendingLocs) { 613 if (Reg) 614 It.convertToReg(Reg); 615 else 616 It.convertToMem(StackOffset); 617 State.addLoc(It); 618 } 619 PendingLocs.clear(); 620 PendingArgFlags.clear(); 621 return false; 622 } 623 624 assert(LocVT == XLenVT && "Expected an XLenVT at this stage"); 625 626 if (Reg) { 627 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 628 } else { 629 State.addLoc( 630 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo)); 631 } 632 return false; 633 } 634 635 void RISCVTargetLowering::analyzeInputArgs( 636 MachineFunction &MF, CCState &CCInfo, 637 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const { 638 unsigned NumArgs = Ins.size(); 639 FunctionType *FType = MF.getFunction().getFunctionType(); 640 641 for (unsigned i = 0; i != NumArgs; ++i) { 642 MVT ArgVT = Ins[i].VT; 643 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags; 644 645 Type *ArgTy = nullptr; 646 if (IsRet) 647 ArgTy = FType->getReturnType(); 648 else if (Ins[i].isOrigArg()) 649 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex()); 650 651 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full, 652 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) { 653 DEBUG(dbgs() << "InputArg #" << i << " has unhandled type " 654 << EVT(ArgVT).getEVTString() << '\n'); 655 llvm_unreachable(nullptr); 656 } 657 } 658 } 659 660 void RISCVTargetLowering::analyzeOutputArgs( 661 MachineFunction &MF, CCState &CCInfo, 662 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet, 663 CallLoweringInfo *CLI) const { 664 unsigned NumArgs = Outs.size(); 665 666 for (unsigned i = 0; i != NumArgs; i++) { 667 MVT ArgVT = Outs[i].VT; 668 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 669 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr; 670 671 if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full, 672 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) { 673 DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type " 674 << EVT(ArgVT).getEVTString() << "\n"); 675 llvm_unreachable(nullptr); 676 } 677 } 678 } 679 680 // The caller is responsible for loading the full value if the argument is 681 // passed with CCValAssign::Indirect. 682 static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain, 683 const CCValAssign &VA, const SDLoc &DL) { 684 MachineFunction &MF = DAG.getMachineFunction(); 685 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 686 EVT LocVT = VA.getLocVT(); 687 EVT ValVT = VA.getValVT(); 688 SDValue Val; 689 690 unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass); 691 RegInfo.addLiveIn(VA.getLocReg(), VReg); 692 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT); 693 694 switch (VA.getLocInfo()) { 695 default: 696 llvm_unreachable("Unexpected CCValAssign::LocInfo"); 697 case CCValAssign::Full: 698 case CCValAssign::Indirect: 699 break; 700 case CCValAssign::BCvt: 701 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val); 702 break; 703 } 704 return Val; 705 } 706 707 // The caller is responsible for loading the full value if the argument is 708 // passed with CCValAssign::Indirect. 709 static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain, 710 const CCValAssign &VA, const SDLoc &DL) { 711 MachineFunction &MF = DAG.getMachineFunction(); 712 MachineFrameInfo &MFI = MF.getFrameInfo(); 713 EVT LocVT = VA.getLocVT(); 714 EVT ValVT = VA.getValVT(); 715 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0)); 716 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8, 717 VA.getLocMemOffset(), /*Immutable=*/true); 718 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 719 SDValue Val; 720 721 ISD::LoadExtType ExtType; 722 switch (VA.getLocInfo()) { 723 default: 724 llvm_unreachable("Unexpected CCValAssign::LocInfo"); 725 case CCValAssign::Full: 726 case CCValAssign::Indirect: 727 ExtType = ISD::NON_EXTLOAD; 728 break; 729 } 730 Val = DAG.getExtLoad( 731 ExtType, DL, LocVT, Chain, FIN, 732 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT); 733 return Val; 734 } 735 736 // Transform physical registers into virtual registers. 737 SDValue RISCVTargetLowering::LowerFormalArguments( 738 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 739 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 740 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 741 742 switch (CallConv) { 743 default: 744 report_fatal_error("Unsupported calling convention"); 745 case CallingConv::C: 746 case CallingConv::Fast: 747 break; 748 } 749 750 MachineFunction &MF = DAG.getMachineFunction(); 751 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 752 MVT XLenVT = Subtarget.getXLenVT(); 753 unsigned XLenInBytes = Subtarget.getXLen() / 8; 754 // Used with vargs to acumulate store chains. 755 std::vector<SDValue> OutChains; 756 757 // Assign locations to all of the incoming arguments. 758 SmallVector<CCValAssign, 16> ArgLocs; 759 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 760 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false); 761 762 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 763 CCValAssign &VA = ArgLocs[i]; 764 assert(VA.getLocVT() == XLenVT && "Unhandled argument type"); 765 SDValue ArgValue; 766 if (VA.isRegLoc()) 767 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL); 768 else 769 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL); 770 771 if (VA.getLocInfo() == CCValAssign::Indirect) { 772 // If the original argument was split and passed by reference (e.g. i128 773 // on RV32), we need to load all parts of it here (using the same 774 // address). 775 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue, 776 MachinePointerInfo())); 777 unsigned ArgIndex = Ins[i].OrigArgIndex; 778 assert(Ins[i].PartOffset == 0); 779 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) { 780 CCValAssign &PartVA = ArgLocs[i + 1]; 781 unsigned PartOffset = Ins[i + 1].PartOffset; 782 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue, 783 DAG.getIntPtrConstant(PartOffset, DL)); 784 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address, 785 MachinePointerInfo())); 786 ++i; 787 } 788 continue; 789 } 790 InVals.push_back(ArgValue); 791 } 792 793 if (IsVarArg) { 794 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs); 795 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs); 796 const TargetRegisterClass *RC = &RISCV::GPRRegClass; 797 MachineFrameInfo &MFI = MF.getFrameInfo(); 798 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 799 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); 800 801 // Offset of the first variable argument from stack pointer, and size of 802 // the vararg save area. For now, the varargs save area is either zero or 803 // large enough to hold a0-a7. 804 int VaArgOffset, VarArgsSaveSize; 805 806 // If all registers are allocated, then all varargs must be passed on the 807 // stack and we don't need to save any argregs. 808 if (ArgRegs.size() == Idx) { 809 VaArgOffset = CCInfo.getNextStackOffset(); 810 VarArgsSaveSize = 0; 811 } else { 812 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx); 813 VaArgOffset = -VarArgsSaveSize; 814 } 815 816 // Record the frame index of the first variable argument 817 // which is a value necessary to VASTART. 818 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true); 819 RVFI->setVarArgsFrameIndex(FI); 820 821 // If saving an odd number of registers then create an extra stack slot to 822 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures 823 // offsets to even-numbered registered remain 2*XLEN-aligned. 824 if (Idx % 2) { 825 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes, 826 true); 827 VarArgsSaveSize += XLenInBytes; 828 } 829 830 // Copy the integer registers that may have been used for passing varargs 831 // to the vararg save area. 832 for (unsigned I = Idx; I < ArgRegs.size(); 833 ++I, VaArgOffset += XLenInBytes) { 834 const unsigned Reg = RegInfo.createVirtualRegister(RC); 835 RegInfo.addLiveIn(ArgRegs[I], Reg); 836 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT); 837 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true); 838 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 839 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff, 840 MachinePointerInfo::getFixedStack(MF, FI)); 841 cast<StoreSDNode>(Store.getNode()) 842 ->getMemOperand() 843 ->setValue((Value *)nullptr); 844 OutChains.push_back(Store); 845 } 846 RVFI->setVarArgsSaveSize(VarArgsSaveSize); 847 } 848 849 // All stores are grouped in one node to allow the matching between 850 // the size of Ins and InVals. This only happens for vararg functions. 851 if (!OutChains.empty()) { 852 OutChains.push_back(Chain); 853 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains); 854 } 855 856 return Chain; 857 } 858 859 // Lower a call to a callseq_start + CALL + callseq_end chain, and add input 860 // and output parameter nodes. 861 SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI, 862 SmallVectorImpl<SDValue> &InVals) const { 863 SelectionDAG &DAG = CLI.DAG; 864 SDLoc &DL = CLI.DL; 865 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 866 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 867 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 868 SDValue Chain = CLI.Chain; 869 SDValue Callee = CLI.Callee; 870 CLI.IsTailCall = false; 871 CallingConv::ID CallConv = CLI.CallConv; 872 bool IsVarArg = CLI.IsVarArg; 873 EVT PtrVT = getPointerTy(DAG.getDataLayout()); 874 MVT XLenVT = Subtarget.getXLenVT(); 875 876 MachineFunction &MF = DAG.getMachineFunction(); 877 878 // Analyze the operands of the call, assigning locations to each operand. 879 SmallVector<CCValAssign, 16> ArgLocs; 880 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 881 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI); 882 883 // Get a count of how many bytes are to be pushed on the stack. 884 unsigned NumBytes = ArgCCInfo.getNextStackOffset(); 885 886 // Create local copies for byval args 887 SmallVector<SDValue, 8> ByValArgs; 888 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 889 ISD::ArgFlagsTy Flags = Outs[i].Flags; 890 if (!Flags.isByVal()) 891 continue; 892 893 SDValue Arg = OutVals[i]; 894 unsigned Size = Flags.getByValSize(); 895 unsigned Align = Flags.getByValAlign(); 896 897 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false); 898 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); 899 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT); 900 901 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align, 902 /*IsVolatile=*/false, 903 /*AlwaysInline=*/false, 904 /*isTailCall=*/false, MachinePointerInfo(), 905 MachinePointerInfo()); 906 ByValArgs.push_back(FIPtr); 907 } 908 909 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL); 910 911 // Copy argument values to their designated locations. 912 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 913 SmallVector<SDValue, 8> MemOpChains; 914 SDValue StackPtr; 915 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) { 916 CCValAssign &VA = ArgLocs[i]; 917 SDValue ArgValue = OutVals[i]; 918 ISD::ArgFlagsTy Flags = Outs[i].Flags; 919 920 // Promote the value if needed. 921 // For now, only handle fully promoted and indirect arguments. 922 switch (VA.getLocInfo()) { 923 case CCValAssign::Full: 924 break; 925 case CCValAssign::BCvt: 926 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), ArgValue); 927 break; 928 case CCValAssign::Indirect: { 929 // Store the argument in a stack slot and pass its address. 930 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT); 931 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); 932 MemOpChains.push_back( 933 DAG.getStore(Chain, DL, ArgValue, SpillSlot, 934 MachinePointerInfo::getFixedStack(MF, FI))); 935 // If the original argument was split (e.g. i128), we need 936 // to store all parts of it here (and pass just one address). 937 unsigned ArgIndex = Outs[i].OrigArgIndex; 938 assert(Outs[i].PartOffset == 0); 939 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) { 940 SDValue PartValue = OutVals[i + 1]; 941 unsigned PartOffset = Outs[i + 1].PartOffset; 942 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot, 943 DAG.getIntPtrConstant(PartOffset, DL)); 944 MemOpChains.push_back( 945 DAG.getStore(Chain, DL, PartValue, Address, 946 MachinePointerInfo::getFixedStack(MF, FI))); 947 ++i; 948 } 949 ArgValue = SpillSlot; 950 break; 951 } 952 default: 953 llvm_unreachable("Unknown loc info!"); 954 } 955 956 // Use local copy if it is a byval arg. 957 if (Flags.isByVal()) 958 ArgValue = ByValArgs[j++]; 959 960 if (VA.isRegLoc()) { 961 // Queue up the argument copies and emit them at the end. 962 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue)); 963 } else { 964 assert(VA.isMemLoc() && "Argument not register or memory"); 965 966 // Work out the address of the stack slot. 967 if (!StackPtr.getNode()) 968 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT); 969 SDValue Address = 970 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, 971 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL)); 972 973 // Emit the store. 974 MemOpChains.push_back( 975 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo())); 976 } 977 } 978 979 // Join the stores, which are independent of one another. 980 if (!MemOpChains.empty()) 981 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 982 983 SDValue Glue; 984 985 // Build a sequence of copy-to-reg nodes, chained and glued together. 986 for (auto &Reg : RegsToPass) { 987 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue); 988 Glue = Chain.getValue(1); 989 } 990 991 if (isa<GlobalAddressSDNode>(Callee)) { 992 Callee = lowerGlobalAddress(Callee, DAG); 993 } else if (isa<ExternalSymbolSDNode>(Callee)) { 994 Callee = lowerExternalSymbol(Callee, DAG); 995 } 996 997 // The first call operand is the chain and the second is the target address. 998 SmallVector<SDValue, 8> Ops; 999 Ops.push_back(Chain); 1000 Ops.push_back(Callee); 1001 1002 // Add argument registers to the end of the list so that they are 1003 // known live into the call. 1004 for (auto &Reg : RegsToPass) 1005 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType())); 1006 1007 // Add a register mask operand representing the call-preserved registers. 1008 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 1009 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv); 1010 assert(Mask && "Missing call preserved mask for calling convention"); 1011 Ops.push_back(DAG.getRegisterMask(Mask)); 1012 1013 // Glue the call to the argument copies, if any. 1014 if (Glue.getNode()) 1015 Ops.push_back(Glue); 1016 1017 // Emit the call. 1018 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 1019 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops); 1020 Glue = Chain.getValue(1); 1021 1022 // Mark the end of the call, which is glued to the call itself. 1023 Chain = DAG.getCALLSEQ_END(Chain, 1024 DAG.getConstant(NumBytes, DL, PtrVT, true), 1025 DAG.getConstant(0, DL, PtrVT, true), 1026 Glue, DL); 1027 Glue = Chain.getValue(1); 1028 1029 // Assign locations to each value returned by this call. 1030 SmallVector<CCValAssign, 16> RVLocs; 1031 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext()); 1032 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true); 1033 1034 // Copy all of the result registers out of their specified physreg. 1035 for (auto &VA : RVLocs) { 1036 // Copy the value out, gluing the copy to the end of the call sequence. 1037 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), 1038 VA.getLocVT(), Glue); 1039 Chain = RetValue.getValue(1); 1040 Glue = RetValue.getValue(2); 1041 1042 switch (VA.getLocInfo()) { 1043 default: 1044 llvm_unreachable("Unknown loc info!"); 1045 case CCValAssign::Full: 1046 break; 1047 case CCValAssign::BCvt: 1048 RetValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), RetValue); 1049 break; 1050 } 1051 1052 InVals.push_back(RetValue); 1053 } 1054 1055 return Chain; 1056 } 1057 1058 bool RISCVTargetLowering::CanLowerReturn( 1059 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg, 1060 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const { 1061 SmallVector<CCValAssign, 16> RVLocs; 1062 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context); 1063 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 1064 MVT VT = Outs[i].VT; 1065 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; 1066 if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags, 1067 CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr)) 1068 return false; 1069 } 1070 return true; 1071 } 1072 1073 static SDValue packIntoRegLoc(SelectionDAG &DAG, SDValue Val, 1074 const CCValAssign &VA, const SDLoc &DL) { 1075 EVT LocVT = VA.getLocVT(); 1076 1077 switch (VA.getLocInfo()) { 1078 default: 1079 llvm_unreachable("Unexpected CCValAssign::LocInfo"); 1080 case CCValAssign::Full: 1081 break; 1082 case CCValAssign::BCvt: 1083 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val); 1084 break; 1085 } 1086 return Val; 1087 } 1088 1089 SDValue 1090 RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 1091 bool IsVarArg, 1092 const SmallVectorImpl<ISD::OutputArg> &Outs, 1093 const SmallVectorImpl<SDValue> &OutVals, 1094 const SDLoc &DL, SelectionDAG &DAG) const { 1095 // Stores the assignment of the return value to a location. 1096 SmallVector<CCValAssign, 16> RVLocs; 1097 1098 // Info about the registers and stack slot. 1099 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, 1100 *DAG.getContext()); 1101 1102 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true, 1103 nullptr); 1104 1105 SDValue Flag; 1106 SmallVector<SDValue, 4> RetOps(1, Chain); 1107 1108 // Copy the result values into the output registers. 1109 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) { 1110 SDValue Val = OutVals[i]; 1111 CCValAssign &VA = RVLocs[i]; 1112 assert(VA.isRegLoc() && "Can only return in registers!"); 1113 Val = packIntoRegLoc(DAG, Val, VA, DL); 1114 1115 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag); 1116 1117 // Guarantee that all emitted copies are stuck together. 1118 Flag = Chain.getValue(1); 1119 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 1120 } 1121 1122 RetOps[0] = Chain; // Update chain. 1123 1124 // Add the flag if we have it. 1125 if (Flag.getNode()) { 1126 RetOps.push_back(Flag); 1127 } 1128 1129 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps); 1130 } 1131 1132 const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const { 1133 switch ((RISCVISD::NodeType)Opcode) { 1134 case RISCVISD::FIRST_NUMBER: 1135 break; 1136 case RISCVISD::RET_FLAG: 1137 return "RISCVISD::RET_FLAG"; 1138 case RISCVISD::CALL: 1139 return "RISCVISD::CALL"; 1140 case RISCVISD::SELECT_CC: 1141 return "RISCVISD::SELECT_CC"; 1142 } 1143 return nullptr; 1144 } 1145 1146 std::pair<unsigned, const TargetRegisterClass *> 1147 RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 1148 StringRef Constraint, 1149 MVT VT) const { 1150 // First, see if this is a constraint that directly corresponds to a 1151 // RISCV register class. 1152 if (Constraint.size() == 1) { 1153 switch (Constraint[0]) { 1154 case 'r': 1155 return std::make_pair(0U, &RISCV::GPRRegClass); 1156 default: 1157 break; 1158 } 1159 } 1160 1161 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 1162 } 1163