Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 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" |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 17 | #include "RISCVMachineFunctionInfo.h" |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 18 | #include "RISCVRegisterInfo.h" |
| 19 | #include "RISCVSubtarget.h" |
| 20 | #include "RISCVTargetMachine.h" |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 22 | #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 Topper | 2fa1436 | 2018-03-29 17:21:10 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/ValueTypes.h" |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 30 | #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 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | #define DEBUG_TYPE "riscv-lower" |
| 39 | |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 40 | STATISTIC(NumTailCalls, "Number of tail calls"); |
| 41 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 42 | RISCVTargetLowering::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 Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 51 | if (Subtarget.hasStdExtF()) |
| 52 | addRegisterClass(MVT::f32, &RISCV::FPR32RegClass); |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 53 | if (Subtarget.hasStdExtD()) |
| 54 | addRegisterClass(MVT::f64, &RISCV::FPR64RegClass); |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 55 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 56 | // Compute derived properties from the register classes. |
| 57 | computeRegisterProperties(STI.getRegisterInfo()); |
| 58 | |
| 59 | setStackPointerRegisterToSaveRestore(RISCV::X2); |
| 60 | |
Alex Bradbury | cfa6291 | 2017-11-08 12:20:01 +0000 | [diff] [blame] | 61 | for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}) |
| 62 | setLoadExtAction(N, XLenVT, MVT::i1, Promote); |
| 63 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 64 | // TODO: add all necessary setOperationAction calls. |
Alex Bradbury | bfb00d4 | 2017-12-11 12:38:17 +0000 | [diff] [blame] | 65 | setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand); |
| 66 | |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 67 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
Alex Bradbury | 74913e1 | 2017-11-08 13:31:40 +0000 | [diff] [blame] | 68 | setOperationAction(ISD::BR_CC, XLenVT, Expand); |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 69 | setOperationAction(ISD::SELECT, XLenVT, Custom); |
| 70 | setOperationAction(ISD::SELECT_CC, XLenVT, Expand); |
| 71 | |
Alex Bradbury | bfb00d4 | 2017-12-11 12:38:17 +0000 | [diff] [blame] | 72 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 73 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| 74 | |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 75 | 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 Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 80 | for (auto VT : {MVT::i1, MVT::i8, MVT::i16}) |
| 81 | setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); |
| 82 | |
Alex Bradbury | 9213838 | 2018-01-18 12:36:38 +0000 | [diff] [blame] | 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 | } |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 92 | |
Alex Bradbury | 9213838 | 2018-01-18 12:36:38 +0000 | [diff] [blame] | 93 | setOperationAction(ISD::SDIVREM, XLenVT, Expand); |
| 94 | setOperationAction(ISD::UDIVREM, XLenVT, Expand); |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 95 | setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand); |
| 96 | setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand); |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 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 | |
Alex Bradbury | 21d28fe | 2018-04-12 05:50:06 +0000 | [diff] [blame] | 109 | 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 Bradbury | 52c2778 | 2018-11-02 19:50:38 +0000 | [diff] [blame] | 114 | // TODO: add proper support for the various FMA variants |
| 115 | // (FMADD.S, FMSUB.S, FNMSUB.S, FNMADD.S). |
| 116 | ISD::NodeType FPOpToExtend[] = { |
Alex Bradbury | 7727240 | 2018-11-15 14:46:11 +0000 | [diff] [blame] | 117 | ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FMA, ISD::FREM}; |
Alex Bradbury | 52c2778 | 2018-11-02 19:50:38 +0000 | [diff] [blame] | 118 | |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 119 | if (Subtarget.hasStdExtF()) { |
| 120 | setOperationAction(ISD::FMINNUM, MVT::f32, Legal); |
| 121 | setOperationAction(ISD::FMAXNUM, MVT::f32, Legal); |
Alex Bradbury | 21d28fe | 2018-04-12 05:50:06 +0000 | [diff] [blame] | 122 | for (auto CC : FPCCToExtend) |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 123 | setCondCodeAction(CC, MVT::f32, Expand); |
| 124 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 125 | setOperationAction(ISD::SELECT, MVT::f32, Custom); |
| 126 | setOperationAction(ISD::BR_CC, MVT::f32, Expand); |
Alex Bradbury | 52c2778 | 2018-11-02 19:50:38 +0000 | [diff] [blame] | 127 | for (auto Op : FPOpToExtend) |
| 128 | setOperationAction(Op, MVT::f32, Expand); |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Alex Bradbury | 5d0dfa5 | 2018-04-12 05:42:42 +0000 | [diff] [blame] | 131 | if (Subtarget.hasStdExtD()) { |
| 132 | setOperationAction(ISD::FMINNUM, MVT::f64, Legal); |
| 133 | setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); |
Alex Bradbury | 21d28fe | 2018-04-12 05:50:06 +0000 | [diff] [blame] | 134 | for (auto CC : FPCCToExtend) |
| 135 | setCondCodeAction(CC, MVT::f64, Expand); |
| 136 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
| 137 | setOperationAction(ISD::SELECT, MVT::f64, Custom); |
| 138 | setOperationAction(ISD::BR_CC, MVT::f64, Expand); |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 139 | setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); |
Alex Bradbury | 60baa2e | 2018-04-12 05:47:15 +0000 | [diff] [blame] | 140 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Alex Bradbury | 52c2778 | 2018-11-02 19:50:38 +0000 | [diff] [blame] | 141 | for (auto Op : FPOpToExtend) |
| 142 | setOperationAction(Op, MVT::f64, Expand); |
Alex Bradbury | 5d0dfa5 | 2018-04-12 05:42:42 +0000 | [diff] [blame] | 143 | } |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 144 | |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 145 | setOperationAction(ISD::GlobalAddress, XLenVT, Custom); |
| 146 | setOperationAction(ISD::BlockAddress, XLenVT, Custom); |
Alex Bradbury | 80c8eb7 | 2018-03-20 13:26:12 +0000 | [diff] [blame] | 147 | setOperationAction(ISD::ConstantPool, XLenVT, Custom); |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 148 | |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 149 | if (Subtarget.hasStdExtA()) { |
Alex Bradbury | 96f492d | 2018-06-13 12:04:51 +0000 | [diff] [blame] | 150 | setMaxAtomicSizeInBitsSupported(Subtarget.getXLen()); |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 151 | setMinCmpXchgSizeInBits(32); |
| 152 | } else { |
Alex Bradbury | 96f492d | 2018-06-13 12:04:51 +0000 | [diff] [blame] | 153 | setMaxAtomicSizeInBitsSupported(0); |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 154 | } |
Alex Bradbury | dc790dd | 2018-06-13 11:58:46 +0000 | [diff] [blame] | 155 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 156 | setBooleanContents(ZeroOrOneBooleanContent); |
| 157 | |
| 158 | // Function alignments (log2). |
Shiva Chen | b48b027 | 2018-04-12 11:30:59 +0000 | [diff] [blame] | 159 | unsigned FunctionAlignment = Subtarget.hasStdExtC() ? 1 : 2; |
| 160 | setMinFunctionAlignment(FunctionAlignment); |
| 161 | setPrefFunctionAlignment(FunctionAlignment); |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 162 | |
| 163 | // Effectively disable jump table generation. |
| 164 | setMinimumJumpTableEntries(INT_MAX); |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Shiva Chen | bbf4c5c | 2018-02-02 02:43:18 +0000 | [diff] [blame] | 167 | EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &, |
| 168 | EVT VT) const { |
| 169 | if (!VT.isVector()) |
| 170 | return getPointerTy(DL); |
| 171 | return VT.changeVectorElementTypeToInteger(); |
| 172 | } |
| 173 | |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 174 | bool RISCVTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, |
| 175 | const CallInst &I, |
| 176 | MachineFunction &MF, |
| 177 | unsigned Intrinsic) const { |
| 178 | switch (Intrinsic) { |
| 179 | default: |
| 180 | return false; |
| 181 | case Intrinsic::riscv_masked_atomicrmw_xchg_i32: |
| 182 | case Intrinsic::riscv_masked_atomicrmw_add_i32: |
| 183 | case Intrinsic::riscv_masked_atomicrmw_sub_i32: |
| 184 | case Intrinsic::riscv_masked_atomicrmw_nand_i32: |
| 185 | case Intrinsic::riscv_masked_atomicrmw_max_i32: |
| 186 | case Intrinsic::riscv_masked_atomicrmw_min_i32: |
| 187 | case Intrinsic::riscv_masked_atomicrmw_umax_i32: |
| 188 | case Intrinsic::riscv_masked_atomicrmw_umin_i32: |
Alex Bradbury | 66d9a75 | 2018-11-29 20:43:42 +0000 | [diff] [blame] | 189 | case Intrinsic::riscv_masked_cmpxchg_i32: |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 190 | PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType()); |
| 191 | Info.opc = ISD::INTRINSIC_W_CHAIN; |
| 192 | Info.memVT = MVT::getVT(PtrTy->getElementType()); |
| 193 | Info.ptrVal = I.getArgOperand(0); |
| 194 | Info.offset = 0; |
| 195 | Info.align = 4; |
| 196 | Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore | |
| 197 | MachineMemOperand::MOVolatile; |
| 198 | return true; |
| 199 | } |
| 200 | } |
| 201 | |
Alex Bradbury | 0992629 | 2018-04-26 12:13:48 +0000 | [diff] [blame] | 202 | bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL, |
| 203 | const AddrMode &AM, Type *Ty, |
| 204 | unsigned AS, |
| 205 | Instruction *I) const { |
| 206 | // No global is ever allowed as a base. |
| 207 | if (AM.BaseGV) |
| 208 | return false; |
| 209 | |
| 210 | // Require a 12-bit signed offset. |
| 211 | if (!isInt<12>(AM.BaseOffs)) |
| 212 | return false; |
| 213 | |
| 214 | switch (AM.Scale) { |
| 215 | case 0: // "r+i" or just "i", depending on HasBaseReg. |
| 216 | break; |
| 217 | case 1: |
| 218 | if (!AM.HasBaseReg) // allow "r+i". |
| 219 | break; |
| 220 | return false; // disallow "r+r" or "r+r+i". |
| 221 | default: |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | |
Alex Bradbury | dcbff63 | 2018-04-26 13:15:17 +0000 | [diff] [blame] | 228 | bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const { |
| 229 | return isInt<12>(Imm); |
| 230 | } |
| 231 | |
Alex Bradbury | 5c41ece | 2018-04-26 13:00:37 +0000 | [diff] [blame] | 232 | bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const { |
| 233 | return isInt<12>(Imm); |
| 234 | } |
| 235 | |
Alex Bradbury | 130b8b3 | 2018-04-26 13:37:00 +0000 | [diff] [blame] | 236 | // On RV32, 64-bit integers are split into their high and low parts and held |
| 237 | // in two different registers, so the trunc is free since the low register can |
| 238 | // just be used. |
| 239 | bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const { |
| 240 | if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy()) |
| 241 | return false; |
| 242 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); |
| 243 | unsigned DestBits = DstTy->getPrimitiveSizeInBits(); |
| 244 | return (SrcBits == 64 && DestBits == 32); |
| 245 | } |
| 246 | |
| 247 | bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const { |
| 248 | if (Subtarget.is64Bit() || SrcVT.isVector() || DstVT.isVector() || |
| 249 | !SrcVT.isInteger() || !DstVT.isInteger()) |
| 250 | return false; |
| 251 | unsigned SrcBits = SrcVT.getSizeInBits(); |
| 252 | unsigned DestBits = DstVT.getSizeInBits(); |
| 253 | return (SrcBits == 64 && DestBits == 32); |
| 254 | } |
| 255 | |
Alex Bradbury | 15e894b | 2018-04-26 14:04:18 +0000 | [diff] [blame] | 256 | bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { |
| 257 | // Zexts are free if they can be combined with a load. |
| 258 | if (auto *LD = dyn_cast<LoadSDNode>(Val)) { |
| 259 | EVT MemVT = LD->getMemoryVT(); |
| 260 | if ((MemVT == MVT::i8 || MemVT == MVT::i16 || |
| 261 | (Subtarget.is64Bit() && MemVT == MVT::i32)) && |
| 262 | (LD->getExtensionType() == ISD::NON_EXTLOAD || |
| 263 | LD->getExtensionType() == ISD::ZEXTLOAD)) |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | return TargetLowering::isZExtFree(Val, VT2); |
| 268 | } |
| 269 | |
Alex Bradbury | e0e62e9 | 2018-11-30 09:56:54 +0000 | [diff] [blame] | 270 | bool RISCVTargetLowering::isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const { |
| 271 | return Subtarget.is64Bit() && SrcVT == MVT::i32 && DstVT == MVT::i64; |
| 272 | } |
| 273 | |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 274 | // Changes the condition code and swaps operands if necessary, so the SetCC |
| 275 | // operation matches one of the comparisons supported directly in the RISC-V |
| 276 | // ISA. |
| 277 | static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) { |
| 278 | switch (CC) { |
| 279 | default: |
| 280 | break; |
| 281 | case ISD::SETGT: |
| 282 | case ISD::SETLE: |
| 283 | case ISD::SETUGT: |
| 284 | case ISD::SETULE: |
| 285 | CC = ISD::getSetCCSwappedOperands(CC); |
| 286 | std::swap(LHS, RHS); |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Return the RISC-V branch opcode that matches the given DAG integer |
| 292 | // condition code. The CondCode must be one of those supported by the RISC-V |
| 293 | // ISA (see normaliseSetCC). |
| 294 | static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) { |
| 295 | switch (CC) { |
| 296 | default: |
| 297 | llvm_unreachable("Unsupported CondCode"); |
| 298 | case ISD::SETEQ: |
| 299 | return RISCV::BEQ; |
| 300 | case ISD::SETNE: |
| 301 | return RISCV::BNE; |
| 302 | case ISD::SETLT: |
| 303 | return RISCV::BLT; |
| 304 | case ISD::SETGE: |
| 305 | return RISCV::BGE; |
| 306 | case ISD::SETULT: |
| 307 | return RISCV::BLTU; |
| 308 | case ISD::SETUGE: |
| 309 | return RISCV::BGEU; |
| 310 | } |
| 311 | } |
| 312 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 313 | SDValue RISCVTargetLowering::LowerOperation(SDValue Op, |
| 314 | SelectionDAG &DAG) const { |
| 315 | switch (Op.getOpcode()) { |
| 316 | default: |
| 317 | report_fatal_error("unimplemented operand"); |
Alex Bradbury | ec8aa91 | 2017-11-08 13:24:21 +0000 | [diff] [blame] | 318 | case ISD::GlobalAddress: |
| 319 | return lowerGlobalAddress(Op, DAG); |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 320 | case ISD::BlockAddress: |
| 321 | return lowerBlockAddress(Op, DAG); |
Alex Bradbury | 80c8eb7 | 2018-03-20 13:26:12 +0000 | [diff] [blame] | 322 | case ISD::ConstantPool: |
| 323 | return lowerConstantPool(Op, DAG); |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 324 | case ISD::SELECT: |
| 325 | return lowerSELECT(Op, DAG); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 326 | case ISD::VASTART: |
| 327 | return lowerVASTART(Op, DAG); |
Alex Bradbury | 70f137b | 2018-01-10 20:12:00 +0000 | [diff] [blame] | 328 | case ISD::FRAMEADDR: |
Alex Bradbury | 0e16766 | 2018-10-04 05:27:50 +0000 | [diff] [blame] | 329 | return lowerFRAMEADDR(Op, DAG); |
Alex Bradbury | 70f137b | 2018-01-10 20:12:00 +0000 | [diff] [blame] | 330 | case ISD::RETURNADDR: |
Alex Bradbury | 0e16766 | 2018-10-04 05:27:50 +0000 | [diff] [blame] | 331 | return lowerRETURNADDR(Op, DAG); |
Alex Bradbury | ec8aa91 | 2017-11-08 13:24:21 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op, |
| 336 | SelectionDAG &DAG) const { |
| 337 | SDLoc DL(Op); |
| 338 | EVT Ty = Op.getValueType(); |
| 339 | GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op); |
| 340 | const GlobalValue *GV = N->getGlobal(); |
| 341 | int64_t Offset = N->getOffset(); |
Sameer AbuAsal | 1dc0a8f | 2018-05-17 18:14:53 +0000 | [diff] [blame] | 342 | MVT XLenVT = Subtarget.getXLenVT(); |
Alex Bradbury | ec8aa91 | 2017-11-08 13:24:21 +0000 | [diff] [blame] | 343 | |
Alex Bradbury | 5bf3b20 | 2018-10-04 14:30:03 +0000 | [diff] [blame] | 344 | if (isPositionIndependent()) |
Alex Bradbury | ec8aa91 | 2017-11-08 13:24:21 +0000 | [diff] [blame] | 345 | report_fatal_error("Unable to lowerGlobalAddress"); |
Sameer AbuAsal | 1dc0a8f | 2018-05-17 18:14:53 +0000 | [diff] [blame] | 346 | // In order to maximise the opportunity for common subexpression elimination, |
| 347 | // emit a separate ADD node for the global address offset instead of folding |
| 348 | // it in the global address node. Later peephole optimisations may choose to |
| 349 | // fold it back in when profitable. |
| 350 | SDValue GAHi = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_HI); |
| 351 | SDValue GALo = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_LO); |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 352 | SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, GAHi), 0); |
| 353 | SDValue MNLo = |
| 354 | SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, GALo), 0); |
Sameer AbuAsal | 1dc0a8f | 2018-05-17 18:14:53 +0000 | [diff] [blame] | 355 | if (Offset != 0) |
| 356 | return DAG.getNode(ISD::ADD, DL, Ty, MNLo, |
| 357 | DAG.getConstant(Offset, DL, XLenVT)); |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 358 | return MNLo; |
| 359 | } |
| 360 | |
| 361 | SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op, |
| 362 | SelectionDAG &DAG) const { |
| 363 | SDLoc DL(Op); |
| 364 | EVT Ty = Op.getValueType(); |
| 365 | BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op); |
| 366 | const BlockAddress *BA = N->getBlockAddress(); |
| 367 | int64_t Offset = N->getOffset(); |
| 368 | |
Alex Bradbury | 5bf3b20 | 2018-10-04 14:30:03 +0000 | [diff] [blame] | 369 | if (isPositionIndependent()) |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 370 | report_fatal_error("Unable to lowerBlockAddress"); |
| 371 | |
| 372 | SDValue BAHi = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_HI); |
| 373 | SDValue BALo = DAG.getTargetBlockAddress(BA, Ty, Offset, RISCVII::MO_LO); |
| 374 | SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, BAHi), 0); |
| 375 | SDValue MNLo = |
| 376 | SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, BALo), 0); |
| 377 | return MNLo; |
| 378 | } |
| 379 | |
Alex Bradbury | 80c8eb7 | 2018-03-20 13:26:12 +0000 | [diff] [blame] | 380 | SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op, |
| 381 | SelectionDAG &DAG) const { |
| 382 | SDLoc DL(Op); |
| 383 | EVT Ty = Op.getValueType(); |
| 384 | ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op); |
| 385 | const Constant *CPA = N->getConstVal(); |
| 386 | int64_t Offset = N->getOffset(); |
| 387 | unsigned Alignment = N->getAlignment(); |
| 388 | |
| 389 | if (!isPositionIndependent()) { |
| 390 | SDValue CPAHi = |
| 391 | DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_HI); |
| 392 | SDValue CPALo = |
| 393 | DAG.getTargetConstantPool(CPA, Ty, Alignment, Offset, RISCVII::MO_LO); |
| 394 | SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, CPAHi), 0); |
| 395 | SDValue MNLo = |
| 396 | SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, CPALo), 0); |
| 397 | return MNLo; |
| 398 | } else { |
| 399 | report_fatal_error("Unable to lowerConstantPool"); |
| 400 | } |
| 401 | } |
| 402 | |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 403 | SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const { |
| 404 | SDValue CondV = Op.getOperand(0); |
| 405 | SDValue TrueV = Op.getOperand(1); |
| 406 | SDValue FalseV = Op.getOperand(2); |
| 407 | SDLoc DL(Op); |
| 408 | MVT XLenVT = Subtarget.getXLenVT(); |
| 409 | |
| 410 | // If the result type is XLenVT and CondV is the output of a SETCC node |
| 411 | // which also operated on XLenVT inputs, then merge the SETCC node into the |
| 412 | // lowered RISCVISD::SELECT_CC to take advantage of the integer |
| 413 | // compare+branch instructions. i.e.: |
| 414 | // (select (setcc lhs, rhs, cc), truev, falsev) |
| 415 | // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev) |
| 416 | if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC && |
| 417 | CondV.getOperand(0).getSimpleValueType() == XLenVT) { |
| 418 | SDValue LHS = CondV.getOperand(0); |
| 419 | SDValue RHS = CondV.getOperand(1); |
| 420 | auto CC = cast<CondCodeSDNode>(CondV.getOperand(2)); |
| 421 | ISD::CondCode CCVal = CC->get(); |
| 422 | |
| 423 | normaliseSetCC(LHS, RHS, CCVal); |
| 424 | |
| 425 | SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT); |
| 426 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); |
| 427 | SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV}; |
| 428 | return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops); |
| 429 | } |
| 430 | |
| 431 | // Otherwise: |
| 432 | // (select condv, truev, falsev) |
| 433 | // -> (riscvisd::select_cc condv, zero, setne, truev, falsev) |
| 434 | SDValue Zero = DAG.getConstant(0, DL, XLenVT); |
| 435 | SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT); |
| 436 | |
| 437 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); |
| 438 | SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV}; |
| 439 | |
| 440 | return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops); |
| 441 | } |
| 442 | |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 443 | SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const { |
| 444 | MachineFunction &MF = DAG.getMachineFunction(); |
| 445 | RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>(); |
| 446 | |
| 447 | SDLoc DL(Op); |
| 448 | SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 449 | getPointerTy(MF.getDataLayout())); |
| 450 | |
| 451 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 452 | // memory location argument. |
| 453 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
| 454 | return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1), |
| 455 | MachinePointerInfo(SV)); |
| 456 | } |
| 457 | |
Alex Bradbury | 0e16766 | 2018-10-04 05:27:50 +0000 | [diff] [blame] | 458 | SDValue RISCVTargetLowering::lowerFRAMEADDR(SDValue Op, |
Alex Bradbury | 70f137b | 2018-01-10 20:12:00 +0000 | [diff] [blame] | 459 | SelectionDAG &DAG) const { |
| 460 | const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo(); |
| 461 | MachineFunction &MF = DAG.getMachineFunction(); |
| 462 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 463 | MFI.setFrameAddressIsTaken(true); |
| 464 | unsigned FrameReg = RI.getFrameRegister(MF); |
| 465 | int XLenInBytes = Subtarget.getXLen() / 8; |
| 466 | |
| 467 | EVT VT = Op.getValueType(); |
| 468 | SDLoc DL(Op); |
| 469 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT); |
| 470 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 471 | while (Depth--) { |
| 472 | int Offset = -(XLenInBytes * 2); |
| 473 | SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr, |
| 474 | DAG.getIntPtrConstant(Offset, DL)); |
| 475 | FrameAddr = |
| 476 | DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo()); |
| 477 | } |
| 478 | return FrameAddr; |
| 479 | } |
| 480 | |
Alex Bradbury | 0e16766 | 2018-10-04 05:27:50 +0000 | [diff] [blame] | 481 | SDValue RISCVTargetLowering::lowerRETURNADDR(SDValue Op, |
Alex Bradbury | 70f137b | 2018-01-10 20:12:00 +0000 | [diff] [blame] | 482 | SelectionDAG &DAG) const { |
| 483 | const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo(); |
| 484 | MachineFunction &MF = DAG.getMachineFunction(); |
| 485 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 486 | MFI.setReturnAddressIsTaken(true); |
| 487 | MVT XLenVT = Subtarget.getXLenVT(); |
| 488 | int XLenInBytes = Subtarget.getXLen() / 8; |
| 489 | |
| 490 | if (verifyReturnAddressArgumentIsConstant(Op, DAG)) |
| 491 | return SDValue(); |
| 492 | |
| 493 | EVT VT = Op.getValueType(); |
| 494 | SDLoc DL(Op); |
| 495 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 496 | if (Depth) { |
| 497 | int Off = -XLenInBytes; |
Alex Bradbury | 0e16766 | 2018-10-04 05:27:50 +0000 | [diff] [blame] | 498 | SDValue FrameAddr = lowerFRAMEADDR(Op, DAG); |
Alex Bradbury | 70f137b | 2018-01-10 20:12:00 +0000 | [diff] [blame] | 499 | SDValue Offset = DAG.getConstant(Off, DL, VT); |
| 500 | return DAG.getLoad(VT, DL, DAG.getEntryNode(), |
| 501 | DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset), |
| 502 | MachinePointerInfo()); |
| 503 | } |
| 504 | |
| 505 | // Return the value of the return address register, marking it an implicit |
| 506 | // live-in. |
| 507 | unsigned Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT)); |
| 508 | return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT); |
| 509 | } |
| 510 | |
Alex Bradbury | 5ac0a2f | 2018-10-03 23:30:16 +0000 | [diff] [blame] | 511 | SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N, |
| 512 | DAGCombinerInfo &DCI) const { |
| 513 | switch (N->getOpcode()) { |
| 514 | default: |
| 515 | break; |
| 516 | case RISCVISD::SplitF64: { |
| 517 | // If the input to SplitF64 is just BuildPairF64 then the operation is |
| 518 | // redundant. Instead, use BuildPairF64's operands directly. |
| 519 | SDValue Op0 = N->getOperand(0); |
| 520 | if (Op0->getOpcode() != RISCVISD::BuildPairF64) |
| 521 | break; |
| 522 | return DCI.CombineTo(N, Op0.getOperand(0), Op0.getOperand(1)); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return SDValue(); |
| 527 | } |
| 528 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 529 | static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI, |
| 530 | MachineBasicBlock *BB) { |
| 531 | assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction"); |
| 532 | |
| 533 | MachineFunction &MF = *BB->getParent(); |
| 534 | DebugLoc DL = MI.getDebugLoc(); |
| 535 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
| 536 | const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); |
| 537 | unsigned LoReg = MI.getOperand(0).getReg(); |
| 538 | unsigned HiReg = MI.getOperand(1).getReg(); |
| 539 | unsigned SrcReg = MI.getOperand(2).getReg(); |
| 540 | const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass; |
| 541 | int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex(); |
| 542 | |
| 543 | TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC, |
| 544 | RI); |
| 545 | MachineMemOperand *MMO = |
| 546 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI), |
| 547 | MachineMemOperand::MOLoad, 8, 8); |
| 548 | BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg) |
| 549 | .addFrameIndex(FI) |
| 550 | .addImm(0) |
| 551 | .addMemOperand(MMO); |
| 552 | BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg) |
| 553 | .addFrameIndex(FI) |
| 554 | .addImm(4) |
| 555 | .addMemOperand(MMO); |
| 556 | MI.eraseFromParent(); // The pseudo instruction is gone now. |
| 557 | return BB; |
| 558 | } |
| 559 | |
| 560 | static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI, |
| 561 | MachineBasicBlock *BB) { |
| 562 | assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo && |
| 563 | "Unexpected instruction"); |
| 564 | |
| 565 | MachineFunction &MF = *BB->getParent(); |
| 566 | DebugLoc DL = MI.getDebugLoc(); |
| 567 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
| 568 | const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); |
| 569 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 570 | unsigned LoReg = MI.getOperand(1).getReg(); |
| 571 | unsigned HiReg = MI.getOperand(2).getReg(); |
| 572 | const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass; |
| 573 | int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex(); |
| 574 | |
| 575 | MachineMemOperand *MMO = |
| 576 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI), |
| 577 | MachineMemOperand::MOStore, 8, 8); |
| 578 | BuildMI(*BB, MI, DL, TII.get(RISCV::SW)) |
| 579 | .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill())) |
| 580 | .addFrameIndex(FI) |
| 581 | .addImm(0) |
| 582 | .addMemOperand(MMO); |
| 583 | BuildMI(*BB, MI, DL, TII.get(RISCV::SW)) |
| 584 | .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill())) |
| 585 | .addFrameIndex(FI) |
| 586 | .addImm(4) |
| 587 | .addMemOperand(MMO); |
| 588 | TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI); |
| 589 | MI.eraseFromParent(); // The pseudo instruction is gone now. |
| 590 | return BB; |
| 591 | } |
| 592 | |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 593 | MachineBasicBlock * |
| 594 | RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, |
| 595 | MachineBasicBlock *BB) const { |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 596 | switch (MI.getOpcode()) { |
| 597 | default: |
| 598 | llvm_unreachable("Unexpected instr type to insert"); |
| 599 | case RISCV::Select_GPR_Using_CC_GPR: |
| 600 | case RISCV::Select_FPR32_Using_CC_GPR: |
Alex Bradbury | 21d28fe | 2018-04-12 05:50:06 +0000 | [diff] [blame] | 601 | case RISCV::Select_FPR64_Using_CC_GPR: |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 602 | break; |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 603 | case RISCV::BuildPairF64Pseudo: |
| 604 | return emitBuildPairF64Pseudo(MI, BB); |
| 605 | case RISCV::SplitF64Pseudo: |
| 606 | return emitSplitF64Pseudo(MI, BB); |
Alex Bradbury | 65d6ea5 | 2018-03-21 15:11:02 +0000 | [diff] [blame] | 607 | } |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 608 | |
| 609 | // To "insert" a SELECT instruction, we actually have to insert the triangle |
| 610 | // control-flow pattern. The incoming instruction knows the destination vreg |
| 611 | // to set, the condition code register to branch on, the true/false values to |
| 612 | // select between, and the condcode to use to select the appropriate branch. |
| 613 | // |
| 614 | // We produce the following control flow: |
| 615 | // HeadMBB |
| 616 | // | \ |
| 617 | // | IfFalseMBB |
| 618 | // | / |
| 619 | // TailMBB |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 620 | const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo(); |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 621 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 622 | DebugLoc DL = MI.getDebugLoc(); |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 623 | MachineFunction::iterator I = ++BB->getIterator(); |
| 624 | |
| 625 | MachineBasicBlock *HeadMBB = BB; |
| 626 | MachineFunction *F = BB->getParent(); |
| 627 | MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 628 | MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 629 | |
| 630 | F->insert(I, IfFalseMBB); |
| 631 | F->insert(I, TailMBB); |
| 632 | // Move all remaining instructions to TailMBB. |
| 633 | TailMBB->splice(TailMBB->begin(), HeadMBB, |
| 634 | std::next(MachineBasicBlock::iterator(MI)), HeadMBB->end()); |
| 635 | // Update machine-CFG edges by transferring all successors of the current |
| 636 | // block to the new block which will contain the Phi node for the select. |
| 637 | TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB); |
| 638 | // Set the successors for HeadMBB. |
| 639 | HeadMBB->addSuccessor(IfFalseMBB); |
| 640 | HeadMBB->addSuccessor(TailMBB); |
| 641 | |
| 642 | // Insert appropriate branch. |
| 643 | unsigned LHS = MI.getOperand(1).getReg(); |
| 644 | unsigned RHS = MI.getOperand(2).getReg(); |
| 645 | auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm()); |
| 646 | unsigned Opcode = getBranchOpcodeForIntCondCode(CC); |
| 647 | |
| 648 | BuildMI(HeadMBB, DL, TII.get(Opcode)) |
| 649 | .addReg(LHS) |
| 650 | .addReg(RHS) |
| 651 | .addMBB(TailMBB); |
| 652 | |
| 653 | // IfFalseMBB just falls through to TailMBB. |
| 654 | IfFalseMBB->addSuccessor(TailMBB); |
| 655 | |
| 656 | // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ] |
| 657 | BuildMI(*TailMBB, TailMBB->begin(), DL, TII.get(RISCV::PHI), |
| 658 | MI.getOperand(0).getReg()) |
| 659 | .addReg(MI.getOperand(4).getReg()) |
| 660 | .addMBB(HeadMBB) |
| 661 | .addReg(MI.getOperand(5).getReg()) |
| 662 | .addMBB(IfFalseMBB); |
| 663 | |
| 664 | MI.eraseFromParent(); // The pseudo instruction is gone now. |
| 665 | return TailMBB; |
| 666 | } |
| 667 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 668 | // Calling Convention Implementation. |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 669 | // The expectations for frontend ABI lowering vary from target to target. |
| 670 | // Ideally, an LLVM frontend would be able to avoid worrying about many ABI |
| 671 | // details, but this is a longer term goal. For now, we simply try to keep the |
| 672 | // role of the frontend as simple and well-defined as possible. The rules can |
| 673 | // be summarised as: |
| 674 | // * Never split up large scalar arguments. We handle them here. |
| 675 | // * If a hardfloat calling convention is being used, and the struct may be |
| 676 | // passed in a pair of registers (fp+fp, int+fp), and both registers are |
| 677 | // available, then pass as two separate arguments. If either the GPRs or FPRs |
| 678 | // are exhausted, then pass according to the rule below. |
| 679 | // * If a struct could never be passed in registers or directly in a stack |
| 680 | // slot (as it is larger than 2*XLEN and the floating point rules don't |
| 681 | // apply), then pass it using a pointer with the byval attribute. |
| 682 | // * If a struct is less than 2*XLEN, then coerce to either a two-element |
| 683 | // word-sized array or a 2*XLEN scalar (depending on alignment). |
| 684 | // * The frontend can determine whether a struct is returned by reference or |
| 685 | // not based on its size and fields. If it will be returned by reference, the |
| 686 | // frontend must modify the prototype so a pointer with the sret annotation is |
| 687 | // passed as the first argument. This is not necessary for large scalar |
| 688 | // returns. |
| 689 | // * Struct return values and varargs should be coerced to structs containing |
| 690 | // register-size fields in the same situations they would be for fixed |
| 691 | // arguments. |
| 692 | |
| 693 | static const MCPhysReg ArgGPRs[] = { |
| 694 | RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13, |
| 695 | RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17 |
| 696 | }; |
| 697 | |
| 698 | // Pass a 2*XLEN argument that has been split into two XLEN values through |
| 699 | // registers or the stack as necessary. |
| 700 | static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1, |
| 701 | ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2, |
| 702 | MVT ValVT2, MVT LocVT2, |
| 703 | ISD::ArgFlagsTy ArgFlags2) { |
| 704 | unsigned XLenInBytes = XLen / 8; |
| 705 | if (unsigned Reg = State.AllocateReg(ArgGPRs)) { |
| 706 | // At least one half can be passed via register. |
| 707 | State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg, |
| 708 | VA1.getLocVT(), CCValAssign::Full)); |
| 709 | } else { |
| 710 | // Both halves must be passed on the stack, with proper alignment. |
| 711 | unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign()); |
| 712 | State.addLoc( |
| 713 | CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(), |
| 714 | State.AllocateStack(XLenInBytes, StackAlign), |
| 715 | VA1.getLocVT(), CCValAssign::Full)); |
| 716 | State.addLoc(CCValAssign::getMem( |
| 717 | ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2, |
| 718 | CCValAssign::Full)); |
| 719 | return false; |
| 720 | } |
| 721 | |
| 722 | if (unsigned Reg = State.AllocateReg(ArgGPRs)) { |
| 723 | // The second half can also be passed via register. |
| 724 | State.addLoc( |
| 725 | CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full)); |
| 726 | } else { |
| 727 | // The second half is passed via the stack, without additional alignment. |
| 728 | State.addLoc(CCValAssign::getMem( |
| 729 | ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2, |
| 730 | CCValAssign::Full)); |
| 731 | } |
| 732 | |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | // Implements the RISC-V calling convention. Returns true upon failure. |
| 737 | static bool CC_RISCV(const DataLayout &DL, unsigned ValNo, MVT ValVT, MVT LocVT, |
| 738 | CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 739 | CCState &State, bool IsFixed, bool IsRet, Type *OrigTy) { |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 740 | unsigned XLen = DL.getLargestLegalIntTypeSizeInBits(); |
| 741 | assert(XLen == 32 || XLen == 64); |
| 742 | MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64; |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 743 | if (ValVT == MVT::f32) { |
| 744 | LocVT = MVT::i32; |
| 745 | LocInfo = CCValAssign::BCvt; |
| 746 | } |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 747 | |
| 748 | // Any return value split in to more than two values can't be returned |
| 749 | // directly. |
| 750 | if (IsRet && ValNo > 1) |
| 751 | return true; |
| 752 | |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 753 | // If this is a variadic argument, the RISC-V calling convention requires |
| 754 | // that it is assigned an 'even' or 'aligned' register if it has 8-byte |
| 755 | // alignment (RV32) or 16-byte alignment (RV64). An aligned register should |
| 756 | // be used regardless of whether the original argument was split during |
| 757 | // legalisation or not. The argument will not be passed by registers if the |
| 758 | // original type is larger than 2*XLEN, so the register alignment rule does |
| 759 | // not apply. |
| 760 | unsigned TwoXLenInBytes = (2 * XLen) / 8; |
| 761 | if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes && |
| 762 | DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) { |
| 763 | unsigned RegIdx = State.getFirstUnallocated(ArgGPRs); |
| 764 | // Skip 'odd' register if necessary. |
| 765 | if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1) |
| 766 | State.AllocateReg(ArgGPRs); |
| 767 | } |
| 768 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 769 | SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs(); |
| 770 | SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags = |
| 771 | State.getPendingArgFlags(); |
| 772 | |
| 773 | assert(PendingLocs.size() == PendingArgFlags.size() && |
| 774 | "PendingLocs and PendingArgFlags out of sync"); |
| 775 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 776 | // Handle passing f64 on RV32D with a soft float ABI. |
| 777 | if (XLen == 32 && ValVT == MVT::f64) { |
Mandeep Singh Grang | 88a8b26 | 2018-04-16 18:56:10 +0000 | [diff] [blame] | 778 | assert(!ArgFlags.isSplit() && PendingLocs.empty() && |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 779 | "Can't lower f64 if it is split"); |
| 780 | // Depending on available argument GPRS, f64 may be passed in a pair of |
| 781 | // GPRs, split between a GPR and the stack, or passed completely on the |
| 782 | // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these |
| 783 | // cases. |
| 784 | unsigned Reg = State.AllocateReg(ArgGPRs); |
| 785 | LocVT = MVT::i32; |
| 786 | if (!Reg) { |
| 787 | unsigned StackOffset = State.AllocateStack(8, 8); |
| 788 | State.addLoc( |
| 789 | CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo)); |
| 790 | return false; |
| 791 | } |
| 792 | if (!State.AllocateReg(ArgGPRs)) |
| 793 | State.AllocateStack(4, 4); |
| 794 | State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); |
| 795 | return false; |
| 796 | } |
| 797 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 798 | // Split arguments might be passed indirectly, so keep track of the pending |
| 799 | // values. |
| 800 | if (ArgFlags.isSplit() || !PendingLocs.empty()) { |
| 801 | LocVT = XLenVT; |
| 802 | LocInfo = CCValAssign::Indirect; |
| 803 | PendingLocs.push_back( |
| 804 | CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo)); |
| 805 | PendingArgFlags.push_back(ArgFlags); |
| 806 | if (!ArgFlags.isSplitEnd()) { |
| 807 | return false; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // If the split argument only had two elements, it should be passed directly |
| 812 | // in registers or on the stack. |
| 813 | if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) { |
| 814 | assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()"); |
| 815 | // Apply the normal calling convention rules to the first half of the |
| 816 | // split argument. |
| 817 | CCValAssign VA = PendingLocs[0]; |
| 818 | ISD::ArgFlagsTy AF = PendingArgFlags[0]; |
| 819 | PendingLocs.clear(); |
| 820 | PendingArgFlags.clear(); |
| 821 | return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT, |
| 822 | ArgFlags); |
| 823 | } |
| 824 | |
| 825 | // Allocate to a register if possible, or else a stack slot. |
| 826 | unsigned Reg = State.AllocateReg(ArgGPRs); |
| 827 | unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8); |
| 828 | |
| 829 | // If we reach this point and PendingLocs is non-empty, we must be at the |
| 830 | // end of a split argument that must be passed indirectly. |
| 831 | if (!PendingLocs.empty()) { |
| 832 | assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()"); |
| 833 | assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()"); |
| 834 | |
| 835 | for (auto &It : PendingLocs) { |
| 836 | if (Reg) |
| 837 | It.convertToReg(Reg); |
| 838 | else |
| 839 | It.convertToMem(StackOffset); |
| 840 | State.addLoc(It); |
| 841 | } |
| 842 | PendingLocs.clear(); |
| 843 | PendingArgFlags.clear(); |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | assert(LocVT == XLenVT && "Expected an XLenVT at this stage"); |
| 848 | |
| 849 | if (Reg) { |
| 850 | State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); |
Alex Bradbury | e96b7c88 | 2018-10-04 07:28:49 +0000 | [diff] [blame] | 851 | return false; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 852 | } |
Alex Bradbury | e96b7c88 | 2018-10-04 07:28:49 +0000 | [diff] [blame] | 853 | |
| 854 | if (ValVT == MVT::f32) { |
| 855 | LocVT = MVT::f32; |
| 856 | LocInfo = CCValAssign::Full; |
| 857 | } |
| 858 | State.addLoc(CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo)); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 859 | return false; |
| 860 | } |
| 861 | |
| 862 | void RISCVTargetLowering::analyzeInputArgs( |
| 863 | MachineFunction &MF, CCState &CCInfo, |
| 864 | const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const { |
| 865 | unsigned NumArgs = Ins.size(); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 866 | FunctionType *FType = MF.getFunction().getFunctionType(); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 867 | |
| 868 | for (unsigned i = 0; i != NumArgs; ++i) { |
| 869 | MVT ArgVT = Ins[i].VT; |
| 870 | ISD::ArgFlagsTy ArgFlags = Ins[i].Flags; |
| 871 | |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 872 | Type *ArgTy = nullptr; |
| 873 | if (IsRet) |
| 874 | ArgTy = FType->getReturnType(); |
| 875 | else if (Ins[i].isOrigArg()) |
| 876 | ArgTy = FType->getParamType(Ins[i].getOrigArgIndex()); |
| 877 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 878 | if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full, |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 879 | ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 880 | LLVM_DEBUG(dbgs() << "InputArg #" << i << " has unhandled type " |
| 881 | << EVT(ArgVT).getEVTString() << '\n'); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 882 | llvm_unreachable(nullptr); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | void RISCVTargetLowering::analyzeOutputArgs( |
| 888 | MachineFunction &MF, CCState &CCInfo, |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 889 | const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet, |
| 890 | CallLoweringInfo *CLI) const { |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 891 | unsigned NumArgs = Outs.size(); |
| 892 | |
| 893 | for (unsigned i = 0; i != NumArgs; i++) { |
| 894 | MVT ArgVT = Outs[i].VT; |
| 895 | ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 896 | Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 897 | |
| 898 | if (CC_RISCV(MF.getDataLayout(), i, ArgVT, ArgVT, CCValAssign::Full, |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 899 | ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 900 | LLVM_DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type " |
| 901 | << EVT(ArgVT).getEVTString() << "\n"); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 902 | llvm_unreachable(nullptr); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 907 | // Convert Val to a ValVT. Should not be called for CCValAssign::Indirect |
| 908 | // values. |
| 909 | static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val, |
| 910 | const CCValAssign &VA, const SDLoc &DL) { |
| 911 | switch (VA.getLocInfo()) { |
| 912 | default: |
| 913 | llvm_unreachable("Unexpected CCValAssign::LocInfo"); |
| 914 | case CCValAssign::Full: |
| 915 | break; |
| 916 | case CCValAssign::BCvt: |
| 917 | Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val); |
| 918 | break; |
| 919 | } |
| 920 | return Val; |
| 921 | } |
| 922 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 923 | // The caller is responsible for loading the full value if the argument is |
| 924 | // passed with CCValAssign::Indirect. |
| 925 | static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain, |
| 926 | const CCValAssign &VA, const SDLoc &DL) { |
| 927 | MachineFunction &MF = DAG.getMachineFunction(); |
| 928 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 929 | EVT LocVT = VA.getLocVT(); |
| 930 | SDValue Val; |
| 931 | |
| 932 | unsigned VReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass); |
| 933 | RegInfo.addLiveIn(VA.getLocReg(), VReg); |
| 934 | Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT); |
| 935 | |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 936 | if (VA.getLocInfo() == CCValAssign::Indirect) |
| 937 | return Val; |
| 938 | |
| 939 | return convertLocVTToValVT(DAG, Val, VA, DL); |
| 940 | } |
| 941 | |
| 942 | static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val, |
| 943 | const CCValAssign &VA, const SDLoc &DL) { |
| 944 | EVT LocVT = VA.getLocVT(); |
| 945 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 946 | switch (VA.getLocInfo()) { |
| 947 | default: |
| 948 | llvm_unreachable("Unexpected CCValAssign::LocInfo"); |
| 949 | case CCValAssign::Full: |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 950 | break; |
| 951 | case CCValAssign::BCvt: |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 952 | Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val); |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 953 | break; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 954 | } |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 955 | return Val; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | // The caller is responsible for loading the full value if the argument is |
| 959 | // passed with CCValAssign::Indirect. |
| 960 | static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain, |
| 961 | const CCValAssign &VA, const SDLoc &DL) { |
| 962 | MachineFunction &MF = DAG.getMachineFunction(); |
| 963 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 964 | EVT LocVT = VA.getLocVT(); |
| 965 | EVT ValVT = VA.getValVT(); |
| 966 | EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0)); |
| 967 | int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8, |
| 968 | VA.getLocMemOffset(), /*Immutable=*/true); |
| 969 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 970 | SDValue Val; |
| 971 | |
| 972 | ISD::LoadExtType ExtType; |
| 973 | switch (VA.getLocInfo()) { |
| 974 | default: |
| 975 | llvm_unreachable("Unexpected CCValAssign::LocInfo"); |
| 976 | case CCValAssign::Full: |
| 977 | case CCValAssign::Indirect: |
| 978 | ExtType = ISD::NON_EXTLOAD; |
| 979 | break; |
| 980 | } |
| 981 | Val = DAG.getExtLoad( |
| 982 | ExtType, DL, LocVT, Chain, FIN, |
| 983 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT); |
| 984 | return Val; |
| 985 | } |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 986 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 987 | static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain, |
| 988 | const CCValAssign &VA, const SDLoc &DL) { |
| 989 | assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 && |
| 990 | "Unexpected VA"); |
| 991 | MachineFunction &MF = DAG.getMachineFunction(); |
| 992 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 993 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 994 | |
| 995 | if (VA.isMemLoc()) { |
| 996 | // f64 is passed on the stack. |
| 997 | int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true); |
| 998 | SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 999 | return DAG.getLoad(MVT::f64, DL, Chain, FIN, |
| 1000 | MachinePointerInfo::getFixedStack(MF, FI)); |
| 1001 | } |
| 1002 | |
| 1003 | assert(VA.isRegLoc() && "Expected register VA assignment"); |
| 1004 | |
| 1005 | unsigned LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass); |
| 1006 | RegInfo.addLiveIn(VA.getLocReg(), LoVReg); |
| 1007 | SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32); |
| 1008 | SDValue Hi; |
| 1009 | if (VA.getLocReg() == RISCV::X17) { |
| 1010 | // Second half of f64 is passed on the stack. |
| 1011 | int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true); |
| 1012 | SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 1013 | Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN, |
| 1014 | MachinePointerInfo::getFixedStack(MF, FI)); |
| 1015 | } else { |
| 1016 | // Second half of f64 is passed in another GPR. |
| 1017 | unsigned HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass); |
| 1018 | RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg); |
| 1019 | Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32); |
| 1020 | } |
| 1021 | return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi); |
| 1022 | } |
| 1023 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1024 | // Transform physical registers into virtual registers. |
| 1025 | SDValue RISCVTargetLowering::LowerFormalArguments( |
| 1026 | SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, |
| 1027 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, |
| 1028 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
| 1029 | |
| 1030 | switch (CallConv) { |
| 1031 | default: |
| 1032 | report_fatal_error("Unsupported calling convention"); |
| 1033 | case CallingConv::C: |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1034 | case CallingConv::Fast: |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | MachineFunction &MF = DAG.getMachineFunction(); |
Ana Pazos | 2e4106b | 2018-07-26 17:49:43 +0000 | [diff] [blame] | 1039 | |
| 1040 | const Function &Func = MF.getFunction(); |
| 1041 | if (Func.hasFnAttribute("interrupt")) { |
| 1042 | if (!Func.arg_empty()) |
| 1043 | report_fatal_error( |
| 1044 | "Functions with the interrupt attribute cannot have arguments!"); |
| 1045 | |
| 1046 | StringRef Kind = |
| 1047 | MF.getFunction().getFnAttribute("interrupt").getValueAsString(); |
| 1048 | |
| 1049 | if (!(Kind == "user" || Kind == "supervisor" || Kind == "machine")) |
| 1050 | report_fatal_error( |
| 1051 | "Function interrupt attribute argument not supported!"); |
| 1052 | } |
| 1053 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1054 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 1055 | MVT XLenVT = Subtarget.getXLenVT(); |
| 1056 | unsigned XLenInBytes = Subtarget.getXLen() / 8; |
| 1057 | // Used with vargs to acumulate store chains. |
| 1058 | std::vector<SDValue> OutChains; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Assign locations to all of the incoming arguments. |
| 1061 | SmallVector<CCValAssign, 16> ArgLocs; |
| 1062 | CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1063 | analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false); |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1064 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1065 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1066 | CCValAssign &VA = ArgLocs[i]; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1067 | SDValue ArgValue; |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1068 | // Passing f64 on RV32D with a soft float ABI must be handled as a special |
| 1069 | // case. |
| 1070 | if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) |
| 1071 | ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL); |
| 1072 | else if (VA.isRegLoc()) |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1073 | ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL); |
| 1074 | else |
| 1075 | ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL); |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1076 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1077 | if (VA.getLocInfo() == CCValAssign::Indirect) { |
| 1078 | // If the original argument was split and passed by reference (e.g. i128 |
| 1079 | // on RV32), we need to load all parts of it here (using the same |
| 1080 | // address). |
| 1081 | InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue, |
| 1082 | MachinePointerInfo())); |
| 1083 | unsigned ArgIndex = Ins[i].OrigArgIndex; |
| 1084 | assert(Ins[i].PartOffset == 0); |
| 1085 | while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) { |
| 1086 | CCValAssign &PartVA = ArgLocs[i + 1]; |
| 1087 | unsigned PartOffset = Ins[i + 1].PartOffset; |
| 1088 | SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue, |
| 1089 | DAG.getIntPtrConstant(PartOffset, DL)); |
| 1090 | InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address, |
| 1091 | MachinePointerInfo())); |
| 1092 | ++i; |
| 1093 | } |
| 1094 | continue; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1095 | } |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1096 | InVals.push_back(ArgValue); |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1097 | } |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (IsVarArg) { |
| 1100 | ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs); |
| 1101 | unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs); |
| 1102 | const TargetRegisterClass *RC = &RISCV::GPRRegClass; |
| 1103 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1104 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
| 1105 | RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); |
| 1106 | |
| 1107 | // Offset of the first variable argument from stack pointer, and size of |
| 1108 | // the vararg save area. For now, the varargs save area is either zero or |
| 1109 | // large enough to hold a0-a7. |
| 1110 | int VaArgOffset, VarArgsSaveSize; |
| 1111 | |
| 1112 | // If all registers are allocated, then all varargs must be passed on the |
| 1113 | // stack and we don't need to save any argregs. |
| 1114 | if (ArgRegs.size() == Idx) { |
| 1115 | VaArgOffset = CCInfo.getNextStackOffset(); |
| 1116 | VarArgsSaveSize = 0; |
| 1117 | } else { |
| 1118 | VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx); |
| 1119 | VaArgOffset = -VarArgsSaveSize; |
| 1120 | } |
| 1121 | |
| 1122 | // Record the frame index of the first variable argument |
| 1123 | // which is a value necessary to VASTART. |
| 1124 | int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true); |
| 1125 | RVFI->setVarArgsFrameIndex(FI); |
| 1126 | |
| 1127 | // If saving an odd number of registers then create an extra stack slot to |
| 1128 | // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures |
| 1129 | // offsets to even-numbered registered remain 2*XLEN-aligned. |
| 1130 | if (Idx % 2) { |
| 1131 | FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes, |
| 1132 | true); |
| 1133 | VarArgsSaveSize += XLenInBytes; |
| 1134 | } |
| 1135 | |
| 1136 | // Copy the integer registers that may have been used for passing varargs |
| 1137 | // to the vararg save area. |
| 1138 | for (unsigned I = Idx; I < ArgRegs.size(); |
| 1139 | ++I, VaArgOffset += XLenInBytes) { |
| 1140 | const unsigned Reg = RegInfo.createVirtualRegister(RC); |
| 1141 | RegInfo.addLiveIn(ArgRegs[I], Reg); |
| 1142 | SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT); |
| 1143 | FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true); |
| 1144 | SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); |
| 1145 | SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff, |
| 1146 | MachinePointerInfo::getFixedStack(MF, FI)); |
| 1147 | cast<StoreSDNode>(Store.getNode()) |
| 1148 | ->getMemOperand() |
| 1149 | ->setValue((Value *)nullptr); |
| 1150 | OutChains.push_back(Store); |
| 1151 | } |
| 1152 | RVFI->setVarArgsSaveSize(VarArgsSaveSize); |
| 1153 | } |
| 1154 | |
| 1155 | // All stores are grouped in one node to allow the matching between |
| 1156 | // the size of Ins and InVals. This only happens for vararg functions. |
| 1157 | if (!OutChains.empty()) { |
| 1158 | OutChains.push_back(Chain); |
| 1159 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains); |
| 1160 | } |
| 1161 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1162 | return Chain; |
| 1163 | } |
| 1164 | |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1165 | /// IsEligibleForTailCallOptimization - Check whether the call is eligible |
| 1166 | /// for tail call optimization. |
| 1167 | /// Note: This is modelled after ARM's IsEligibleForTailCallOptimization. |
| 1168 | bool RISCVTargetLowering::IsEligibleForTailCallOptimization( |
| 1169 | CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF, |
| 1170 | const SmallVector<CCValAssign, 16> &ArgLocs) const { |
| 1171 | |
| 1172 | auto &Callee = CLI.Callee; |
| 1173 | auto CalleeCC = CLI.CallConv; |
| 1174 | auto IsVarArg = CLI.IsVarArg; |
| 1175 | auto &Outs = CLI.Outs; |
| 1176 | auto &Caller = MF.getFunction(); |
| 1177 | auto CallerCC = Caller.getCallingConv(); |
| 1178 | |
| 1179 | // Do not tail call opt functions with "disable-tail-calls" attribute. |
| 1180 | if (Caller.getFnAttribute("disable-tail-calls").getValueAsString() == "true") |
| 1181 | return false; |
| 1182 | |
| 1183 | // Exception-handling functions need a special set of instructions to |
| 1184 | // indicate a return to the hardware. Tail-calling another function would |
| 1185 | // probably break this. |
| 1186 | // TODO: The "interrupt" attribute isn't currently defined by RISC-V. This |
| 1187 | // should be expanded as new function attributes are introduced. |
| 1188 | if (Caller.hasFnAttribute("interrupt")) |
| 1189 | return false; |
| 1190 | |
| 1191 | // Do not tail call opt functions with varargs. |
| 1192 | if (IsVarArg) |
| 1193 | return false; |
| 1194 | |
| 1195 | // Do not tail call opt if the stack is used to pass parameters. |
| 1196 | if (CCInfo.getNextStackOffset() != 0) |
| 1197 | return false; |
| 1198 | |
| 1199 | // Do not tail call opt if any parameters need to be passed indirectly. |
| 1200 | // Since long doubles (fp128) and i128 are larger than 2*XLEN, they are |
| 1201 | // passed indirectly. So the address of the value will be passed in a |
| 1202 | // register, or if not available, then the address is put on the stack. In |
| 1203 | // order to pass indirectly, space on the stack often needs to be allocated |
| 1204 | // in order to store the value. In this case the CCInfo.getNextStackOffset() |
| 1205 | // != 0 check is not enough and we need to check if any CCValAssign ArgsLocs |
| 1206 | // are passed CCValAssign::Indirect. |
| 1207 | for (auto &VA : ArgLocs) |
| 1208 | if (VA.getLocInfo() == CCValAssign::Indirect) |
| 1209 | return false; |
| 1210 | |
| 1211 | // Do not tail call opt if either caller or callee uses struct return |
| 1212 | // semantics. |
| 1213 | auto IsCallerStructRet = Caller.hasStructRetAttr(); |
| 1214 | auto IsCalleeStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet(); |
| 1215 | if (IsCallerStructRet || IsCalleeStructRet) |
| 1216 | return false; |
| 1217 | |
| 1218 | // Externally-defined functions with weak linkage should not be |
| 1219 | // tail-called. The behaviour of branch instructions in this situation (as |
| 1220 | // used for tail calls) is implementation-defined, so we cannot rely on the |
| 1221 | // linker replacing the tail call with a return. |
| 1222 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 1223 | const GlobalValue *GV = G->getGlobal(); |
| 1224 | if (GV->hasExternalWeakLinkage()) |
| 1225 | return false; |
| 1226 | } |
| 1227 | |
| 1228 | // The callee has to preserve all registers the caller needs to preserve. |
| 1229 | const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| 1230 | const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); |
| 1231 | if (CalleeCC != CallerCC) { |
| 1232 | const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); |
| 1233 | if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
| 1237 | // Byval parameters hand the function a pointer directly into the stack area |
| 1238 | // we want to reuse during a tail call. Working around this *is* possible |
| 1239 | // but less efficient and uglier in LowerCall. |
| 1240 | for (auto &Arg : Outs) |
| 1241 | if (Arg.Flags.isByVal()) |
| 1242 | return false; |
| 1243 | |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1247 | // Lower a call to a callseq_start + CALL + callseq_end chain, and add input |
| 1248 | // and output parameter nodes. |
| 1249 | SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI, |
| 1250 | SmallVectorImpl<SDValue> &InVals) const { |
| 1251 | SelectionDAG &DAG = CLI.DAG; |
| 1252 | SDLoc &DL = CLI.DL; |
| 1253 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; |
| 1254 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; |
| 1255 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; |
| 1256 | SDValue Chain = CLI.Chain; |
| 1257 | SDValue Callee = CLI.Callee; |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1258 | bool &IsTailCall = CLI.IsTailCall; |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1259 | CallingConv::ID CallConv = CLI.CallConv; |
| 1260 | bool IsVarArg = CLI.IsVarArg; |
| 1261 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1262 | MVT XLenVT = Subtarget.getXLenVT(); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1263 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1264 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1265 | |
| 1266 | // Analyze the operands of the call, assigning locations to each operand. |
| 1267 | SmallVector<CCValAssign, 16> ArgLocs; |
| 1268 | CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 1269 | analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1270 | |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1271 | // Check if it's really possible to do a tail call. |
| 1272 | if (IsTailCall) |
| 1273 | IsTailCall = IsEligibleForTailCallOptimization(ArgCCInfo, CLI, MF, |
| 1274 | ArgLocs); |
| 1275 | |
| 1276 | if (IsTailCall) |
| 1277 | ++NumTailCalls; |
| 1278 | else if (CLI.CS && CLI.CS.isMustTailCall()) |
| 1279 | report_fatal_error("failed to perform tail call elimination on a call " |
| 1280 | "site marked musttail"); |
| 1281 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1282 | // Get a count of how many bytes are to be pushed on the stack. |
| 1283 | unsigned NumBytes = ArgCCInfo.getNextStackOffset(); |
| 1284 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1285 | // Create local copies for byval args |
| 1286 | SmallVector<SDValue, 8> ByValArgs; |
| 1287 | for (unsigned i = 0, e = Outs.size(); i != e; ++i) { |
| 1288 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| 1289 | if (!Flags.isByVal()) |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1290 | continue; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1291 | |
| 1292 | SDValue Arg = OutVals[i]; |
| 1293 | unsigned Size = Flags.getByValSize(); |
| 1294 | unsigned Align = Flags.getByValAlign(); |
| 1295 | |
| 1296 | int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false); |
| 1297 | SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); |
| 1298 | SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT); |
| 1299 | |
| 1300 | Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align, |
| 1301 | /*IsVolatile=*/false, |
| 1302 | /*AlwaysInline=*/false, |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1303 | IsTailCall, MachinePointerInfo(), |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1304 | MachinePointerInfo()); |
| 1305 | ByValArgs.push_back(FIPtr); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1308 | if (!IsTailCall) |
| 1309 | Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Copy argument values to their designated locations. |
| 1312 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1313 | SmallVector<SDValue, 8> MemOpChains; |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1314 | SDValue StackPtr; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1315 | for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1316 | CCValAssign &VA = ArgLocs[i]; |
| 1317 | SDValue ArgValue = OutVals[i]; |
| 1318 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1319 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1320 | // Handle passing f64 on RV32D with a soft float ABI as a special case. |
| 1321 | bool IsF64OnRV32DSoftABI = |
| 1322 | VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64; |
| 1323 | if (IsF64OnRV32DSoftABI && VA.isRegLoc()) { |
| 1324 | SDValue SplitF64 = DAG.getNode( |
| 1325 | RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue); |
| 1326 | SDValue Lo = SplitF64.getValue(0); |
| 1327 | SDValue Hi = SplitF64.getValue(1); |
| 1328 | |
| 1329 | unsigned RegLo = VA.getLocReg(); |
| 1330 | RegsToPass.push_back(std::make_pair(RegLo, Lo)); |
| 1331 | |
| 1332 | if (RegLo == RISCV::X17) { |
| 1333 | // Second half of f64 is passed on the stack. |
| 1334 | // Work out the address of the stack slot. |
| 1335 | if (!StackPtr.getNode()) |
| 1336 | StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT); |
| 1337 | // Emit the store. |
| 1338 | MemOpChains.push_back( |
| 1339 | DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo())); |
| 1340 | } else { |
| 1341 | // Second half of f64 is passed in another GPR. |
| 1342 | unsigned RegHigh = RegLo + 1; |
| 1343 | RegsToPass.push_back(std::make_pair(RegHigh, Hi)); |
| 1344 | } |
| 1345 | continue; |
| 1346 | } |
| 1347 | |
| 1348 | // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way |
| 1349 | // as any other MemLoc. |
| 1350 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1351 | // Promote the value if needed. |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1352 | // For now, only handle fully promoted and indirect arguments. |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 1353 | if (VA.getLocInfo() == CCValAssign::Indirect) { |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1354 | // Store the argument in a stack slot and pass its address. |
| 1355 | SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT); |
| 1356 | int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); |
| 1357 | MemOpChains.push_back( |
| 1358 | DAG.getStore(Chain, DL, ArgValue, SpillSlot, |
| 1359 | MachinePointerInfo::getFixedStack(MF, FI))); |
| 1360 | // If the original argument was split (e.g. i128), we need |
| 1361 | // to store all parts of it here (and pass just one address). |
| 1362 | unsigned ArgIndex = Outs[i].OrigArgIndex; |
| 1363 | assert(Outs[i].PartOffset == 0); |
| 1364 | while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) { |
| 1365 | SDValue PartValue = OutVals[i + 1]; |
| 1366 | unsigned PartOffset = Outs[i + 1].PartOffset; |
| 1367 | SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot, |
| 1368 | DAG.getIntPtrConstant(PartOffset, DL)); |
| 1369 | MemOpChains.push_back( |
| 1370 | DAG.getStore(Chain, DL, PartValue, Address, |
| 1371 | MachinePointerInfo::getFixedStack(MF, FI))); |
| 1372 | ++i; |
| 1373 | } |
| 1374 | ArgValue = SpillSlot; |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 1375 | } else { |
| 1376 | ArgValue = convertValVTToLocVT(DAG, ArgValue, VA, DL); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1379 | // Use local copy if it is a byval arg. |
| 1380 | if (Flags.isByVal()) |
| 1381 | ArgValue = ByValArgs[j++]; |
| 1382 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1383 | if (VA.isRegLoc()) { |
| 1384 | // Queue up the argument copies and emit them at the end. |
| 1385 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue)); |
| 1386 | } else { |
| 1387 | assert(VA.isMemLoc() && "Argument not register or memory"); |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1388 | assert(!IsTailCall && "Tail call not allowed if stack is used " |
| 1389 | "for passing parameters"); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Work out the address of the stack slot. |
| 1392 | if (!StackPtr.getNode()) |
| 1393 | StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT); |
| 1394 | SDValue Address = |
| 1395 | DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, |
| 1396 | DAG.getIntPtrConstant(VA.getLocMemOffset(), DL)); |
| 1397 | |
| 1398 | // Emit the store. |
| 1399 | MemOpChains.push_back( |
| 1400 | DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo())); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1404 | // Join the stores, which are independent of one another. |
| 1405 | if (!MemOpChains.empty()) |
| 1406 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); |
| 1407 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1408 | SDValue Glue; |
| 1409 | |
| 1410 | // Build a sequence of copy-to-reg nodes, chained and glued together. |
| 1411 | for (auto &Reg : RegsToPass) { |
| 1412 | Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue); |
| 1413 | Glue = Chain.getValue(1); |
| 1414 | } |
| 1415 | |
Shiva Chen | d58bd8d | 2018-04-25 14:19:12 +0000 | [diff] [blame] | 1416 | // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a |
| 1417 | // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't |
| 1418 | // split it and then direct call can be matched by PseudoCALL. |
| 1419 | if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 1420 | Callee = DAG.getTargetGlobalAddress(S->getGlobal(), DL, PtrVT, 0, 0); |
| 1421 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
| 1422 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, 0); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | // The first call operand is the chain and the second is the target address. |
| 1426 | SmallVector<SDValue, 8> Ops; |
| 1427 | Ops.push_back(Chain); |
| 1428 | Ops.push_back(Callee); |
| 1429 | |
| 1430 | // Add argument registers to the end of the list so that they are |
| 1431 | // known live into the call. |
| 1432 | for (auto &Reg : RegsToPass) |
| 1433 | Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType())); |
| 1434 | |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1435 | if (!IsTailCall) { |
| 1436 | // Add a register mask operand representing the call-preserved registers. |
| 1437 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| 1438 | const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv); |
| 1439 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 1440 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| 1441 | } |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1442 | |
| 1443 | // Glue the call to the argument copies, if any. |
| 1444 | if (Glue.getNode()) |
| 1445 | Ops.push_back(Glue); |
| 1446 | |
| 1447 | // Emit the call. |
| 1448 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (IsTailCall) { |
| 1451 | MF.getFrameInfo().setHasTailCall(); |
| 1452 | return DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops); |
| 1453 | } |
| 1454 | |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1455 | Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops); |
| 1456 | Glue = Chain.getValue(1); |
| 1457 | |
| 1458 | // Mark the end of the call, which is glued to the call itself. |
| 1459 | Chain = DAG.getCALLSEQ_END(Chain, |
| 1460 | DAG.getConstant(NumBytes, DL, PtrVT, true), |
| 1461 | DAG.getConstant(0, DL, PtrVT, true), |
| 1462 | Glue, DL); |
| 1463 | Glue = Chain.getValue(1); |
| 1464 | |
| 1465 | // Assign locations to each value returned by this call. |
| 1466 | SmallVector<CCValAssign, 16> RVLocs; |
| 1467 | CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext()); |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1468 | analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Copy all of the result registers out of their specified physreg. |
| 1471 | for (auto &VA : RVLocs) { |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1472 | // Copy the value out |
| 1473 | SDValue RetValue = |
| 1474 | DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue); |
| 1475 | // Glue the RetValue to the end of the call sequence |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1476 | Chain = RetValue.getValue(1); |
| 1477 | Glue = RetValue.getValue(2); |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 1478 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1479 | if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) { |
| 1480 | assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment"); |
| 1481 | SDValue RetValue2 = |
| 1482 | DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue); |
| 1483 | Chain = RetValue2.getValue(1); |
| 1484 | Glue = RetValue2.getValue(2); |
| 1485 | RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue, |
| 1486 | RetValue2); |
| 1487 | } |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1488 | |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 1489 | RetValue = convertLocVTToValVT(DAG, RetValue, VA, DL); |
Alex Bradbury | 76c29ee | 2018-03-20 12:45:35 +0000 | [diff] [blame] | 1490 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1491 | InVals.push_back(RetValue); |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | return Chain; |
| 1495 | } |
| 1496 | |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1497 | bool RISCVTargetLowering::CanLowerReturn( |
| 1498 | CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg, |
| 1499 | const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const { |
| 1500 | SmallVector<CCValAssign, 16> RVLocs; |
| 1501 | CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context); |
| 1502 | for (unsigned i = 0, e = Outs.size(); i != e; ++i) { |
| 1503 | MVT VT = Outs[i].VT; |
| 1504 | ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; |
| 1505 | if (CC_RISCV(MF.getDataLayout(), i, VT, VT, CCValAssign::Full, ArgFlags, |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 1506 | CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr)) |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1507 | return false; |
| 1508 | } |
| 1509 | return true; |
| 1510 | } |
| 1511 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1512 | SDValue |
| 1513 | RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, |
| 1514 | bool IsVarArg, |
| 1515 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 1516 | const SmallVectorImpl<SDValue> &OutVals, |
| 1517 | const SDLoc &DL, SelectionDAG &DAG) const { |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1518 | // Stores the assignment of the return value to a location. |
| 1519 | SmallVector<CCValAssign, 16> RVLocs; |
| 1520 | |
| 1521 | // Info about the registers and stack slot. |
| 1522 | CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, |
| 1523 | *DAG.getContext()); |
| 1524 | |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 1525 | analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true, |
| 1526 | nullptr); |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1527 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1528 | SDValue Glue; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1529 | SmallVector<SDValue, 4> RetOps(1, Chain); |
| 1530 | |
| 1531 | // Copy the result values into the output registers. |
| 1532 | for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) { |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 1533 | SDValue Val = OutVals[i]; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1534 | CCValAssign &VA = RVLocs[i]; |
| 1535 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 1536 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1537 | if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) { |
| 1538 | // Handle returning f64 on RV32D with a soft float ABI. |
| 1539 | assert(VA.isRegLoc() && "Expected return via registers"); |
| 1540 | SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL, |
| 1541 | DAG.getVTList(MVT::i32, MVT::i32), Val); |
| 1542 | SDValue Lo = SplitF64.getValue(0); |
| 1543 | SDValue Hi = SplitF64.getValue(1); |
| 1544 | unsigned RegLo = VA.getLocReg(); |
| 1545 | unsigned RegHi = RegLo + 1; |
| 1546 | Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue); |
| 1547 | Glue = Chain.getValue(1); |
| 1548 | RetOps.push_back(DAG.getRegister(RegLo, MVT::i32)); |
| 1549 | Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue); |
| 1550 | Glue = Chain.getValue(1); |
| 1551 | RetOps.push_back(DAG.getRegister(RegHi, MVT::i32)); |
| 1552 | } else { |
| 1553 | // Handle a 'normal' return. |
Alex Bradbury | 1dbfdeb | 2018-10-03 22:53:25 +0000 | [diff] [blame] | 1554 | Val = convertValVTToLocVT(DAG, Val, VA, DL); |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1555 | Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue); |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1556 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1557 | // Guarantee that all emitted copies are stuck together. |
| 1558 | Glue = Chain.getValue(1); |
| 1559 | RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); |
| 1560 | } |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | RetOps[0] = Chain; // Update chain. |
| 1564 | |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1565 | // Add the glue node if we have it. |
| 1566 | if (Glue.getNode()) { |
| 1567 | RetOps.push_back(Glue); |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Ana Pazos | 2e4106b | 2018-07-26 17:49:43 +0000 | [diff] [blame] | 1570 | // Interrupt service routines use different return instructions. |
| 1571 | const Function &Func = DAG.getMachineFunction().getFunction(); |
| 1572 | if (Func.hasFnAttribute("interrupt")) { |
| 1573 | if (!Func.getReturnType()->isVoidTy()) |
| 1574 | report_fatal_error( |
| 1575 | "Functions with the interrupt attribute must have void return type!"); |
| 1576 | |
| 1577 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1578 | StringRef Kind = |
| 1579 | MF.getFunction().getFnAttribute("interrupt").getValueAsString(); |
| 1580 | |
| 1581 | unsigned RetOpc; |
| 1582 | if (Kind == "user") |
| 1583 | RetOpc = RISCVISD::URET_FLAG; |
| 1584 | else if (Kind == "supervisor") |
| 1585 | RetOpc = RISCVISD::SRET_FLAG; |
| 1586 | else |
| 1587 | RetOpc = RISCVISD::MRET_FLAG; |
| 1588 | |
| 1589 | return DAG.getNode(RetOpc, DL, MVT::Other, RetOps); |
| 1590 | } |
| 1591 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1592 | return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps); |
| 1593 | } |
| 1594 | |
| 1595 | const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 1596 | switch ((RISCVISD::NodeType)Opcode) { |
| 1597 | case RISCVISD::FIRST_NUMBER: |
| 1598 | break; |
| 1599 | case RISCVISD::RET_FLAG: |
| 1600 | return "RISCVISD::RET_FLAG"; |
Ana Pazos | 2e4106b | 2018-07-26 17:49:43 +0000 | [diff] [blame] | 1601 | case RISCVISD::URET_FLAG: |
| 1602 | return "RISCVISD::URET_FLAG"; |
| 1603 | case RISCVISD::SRET_FLAG: |
| 1604 | return "RISCVISD::SRET_FLAG"; |
| 1605 | case RISCVISD::MRET_FLAG: |
| 1606 | return "RISCVISD::MRET_FLAG"; |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 1607 | case RISCVISD::CALL: |
| 1608 | return "RISCVISD::CALL"; |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 1609 | case RISCVISD::SELECT_CC: |
| 1610 | return "RISCVISD::SELECT_CC"; |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 1611 | case RISCVISD::BuildPairF64: |
| 1612 | return "RISCVISD::BuildPairF64"; |
| 1613 | case RISCVISD::SplitF64: |
| 1614 | return "RISCVISD::SplitF64"; |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 1615 | case RISCVISD::TAIL: |
| 1616 | return "RISCVISD::TAIL"; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1617 | } |
| 1618 | return nullptr; |
| 1619 | } |
Alex Bradbury | 9330e64 | 2018-01-10 20:05:09 +0000 | [diff] [blame] | 1620 | |
| 1621 | std::pair<unsigned, const TargetRegisterClass *> |
| 1622 | RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, |
| 1623 | StringRef Constraint, |
| 1624 | MVT VT) const { |
| 1625 | // First, see if this is a constraint that directly corresponds to a |
| 1626 | // RISCV register class. |
| 1627 | if (Constraint.size() == 1) { |
| 1628 | switch (Constraint[0]) { |
| 1629 | case 'r': |
| 1630 | return std::make_pair(0U, &RISCV::GPRRegClass); |
| 1631 | default: |
| 1632 | break; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
| 1637 | } |
Alex Bradbury | 96f492d | 2018-06-13 12:04:51 +0000 | [diff] [blame] | 1638 | |
| 1639 | Instruction *RISCVTargetLowering::emitLeadingFence(IRBuilder<> &Builder, |
| 1640 | Instruction *Inst, |
| 1641 | AtomicOrdering Ord) const { |
| 1642 | if (isa<LoadInst>(Inst) && Ord == AtomicOrdering::SequentiallyConsistent) |
| 1643 | return Builder.CreateFence(Ord); |
| 1644 | if (isa<StoreInst>(Inst) && isReleaseOrStronger(Ord)) |
| 1645 | return Builder.CreateFence(AtomicOrdering::Release); |
| 1646 | return nullptr; |
| 1647 | } |
| 1648 | |
| 1649 | Instruction *RISCVTargetLowering::emitTrailingFence(IRBuilder<> &Builder, |
| 1650 | Instruction *Inst, |
| 1651 | AtomicOrdering Ord) const { |
| 1652 | if (isa<LoadInst>(Inst) && isAcquireOrStronger(Ord)) |
| 1653 | return Builder.CreateFence(AtomicOrdering::Acquire); |
| 1654 | return nullptr; |
| 1655 | } |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 1656 | |
| 1657 | TargetLowering::AtomicExpansionKind |
| 1658 | RISCVTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { |
| 1659 | unsigned Size = AI->getType()->getPrimitiveSizeInBits(); |
| 1660 | if (Size == 8 || Size == 16) |
| 1661 | return AtomicExpansionKind::MaskedIntrinsic; |
| 1662 | return AtomicExpansionKind::None; |
| 1663 | } |
| 1664 | |
| 1665 | static Intrinsic::ID |
| 1666 | getIntrinsicForMaskedAtomicRMWBinOp32(AtomicRMWInst::BinOp BinOp) { |
| 1667 | switch (BinOp) { |
| 1668 | default: |
| 1669 | llvm_unreachable("Unexpected AtomicRMW BinOp"); |
| 1670 | case AtomicRMWInst::Xchg: |
| 1671 | return Intrinsic::riscv_masked_atomicrmw_xchg_i32; |
| 1672 | case AtomicRMWInst::Add: |
| 1673 | return Intrinsic::riscv_masked_atomicrmw_add_i32; |
| 1674 | case AtomicRMWInst::Sub: |
| 1675 | return Intrinsic::riscv_masked_atomicrmw_sub_i32; |
| 1676 | case AtomicRMWInst::Nand: |
| 1677 | return Intrinsic::riscv_masked_atomicrmw_nand_i32; |
| 1678 | case AtomicRMWInst::Max: |
| 1679 | return Intrinsic::riscv_masked_atomicrmw_max_i32; |
| 1680 | case AtomicRMWInst::Min: |
| 1681 | return Intrinsic::riscv_masked_atomicrmw_min_i32; |
| 1682 | case AtomicRMWInst::UMax: |
| 1683 | return Intrinsic::riscv_masked_atomicrmw_umax_i32; |
| 1684 | case AtomicRMWInst::UMin: |
| 1685 | return Intrinsic::riscv_masked_atomicrmw_umin_i32; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | Value *RISCVTargetLowering::emitMaskedAtomicRMWIntrinsic( |
| 1690 | IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr, |
| 1691 | Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const { |
| 1692 | Value *Ordering = Builder.getInt32(static_cast<uint32_t>(AI->getOrdering())); |
| 1693 | Type *Tys[] = {AlignedAddr->getType()}; |
| 1694 | Function *LrwOpScwLoop = Intrinsic::getDeclaration( |
| 1695 | AI->getModule(), |
| 1696 | getIntrinsicForMaskedAtomicRMWBinOp32(AI->getOperation()), Tys); |
| 1697 | |
| 1698 | // Must pass the shift amount needed to sign extend the loaded value prior |
| 1699 | // to performing a signed comparison for min/max. ShiftAmt is the number of |
| 1700 | // bits to shift the value into position. Pass XLen-ShiftAmt-ValWidth, which |
| 1701 | // is the number of bits to left+right shift the value in order to |
| 1702 | // sign-extend. |
| 1703 | if (AI->getOperation() == AtomicRMWInst::Min || |
| 1704 | AI->getOperation() == AtomicRMWInst::Max) { |
| 1705 | const DataLayout &DL = AI->getModule()->getDataLayout(); |
| 1706 | unsigned ValWidth = |
| 1707 | DL.getTypeStoreSizeInBits(AI->getValOperand()->getType()); |
| 1708 | Value *SextShamt = Builder.CreateSub( |
| 1709 | Builder.getInt32(Subtarget.getXLen() - ValWidth), ShiftAmt); |
| 1710 | return Builder.CreateCall(LrwOpScwLoop, |
| 1711 | {AlignedAddr, Incr, Mask, SextShamt, Ordering}); |
| 1712 | } |
| 1713 | |
| 1714 | return Builder.CreateCall(LrwOpScwLoop, {AlignedAddr, Incr, Mask, Ordering}); |
| 1715 | } |
Alex Bradbury | 66d9a75 | 2018-11-29 20:43:42 +0000 | [diff] [blame] | 1716 | |
| 1717 | TargetLowering::AtomicExpansionKind |
| 1718 | RISCVTargetLowering::shouldExpandAtomicCmpXchgInIR( |
| 1719 | AtomicCmpXchgInst *CI) const { |
| 1720 | unsigned Size = CI->getCompareOperand()->getType()->getPrimitiveSizeInBits(); |
| 1721 | if (Size == 8 || Size == 16) |
| 1722 | return AtomicExpansionKind::MaskedIntrinsic; |
| 1723 | return AtomicExpansionKind::None; |
| 1724 | } |
| 1725 | |
| 1726 | Value *RISCVTargetLowering::emitMaskedAtomicCmpXchgIntrinsic( |
| 1727 | IRBuilder<> &Builder, AtomicCmpXchgInst *CI, Value *AlignedAddr, |
| 1728 | Value *CmpVal, Value *NewVal, Value *Mask, AtomicOrdering Ord) const { |
| 1729 | Value *Ordering = Builder.getInt32(static_cast<uint32_t>(Ord)); |
| 1730 | Type *Tys[] = {AlignedAddr->getType()}; |
| 1731 | Function *MaskedCmpXchg = Intrinsic::getDeclaration( |
| 1732 | CI->getModule(), Intrinsic::riscv_masked_cmpxchg_i32, Tys); |
| 1733 | return Builder.CreateCall(MaskedCmpXchg, |
| 1734 | {AlignedAddr, CmpVal, NewVal, Mask, Ordering}); |
| 1735 | } |