Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 1 | //===-- PTXISelLowering.cpp - PTX 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 implements the PTXTargetLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 14 | #include "PTX.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 15 | #include "PTXISelLowering.h" |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 16 | #include "PTXMachineFunctionInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 17 | #include "PTXRegisterInfo.h" |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 18 | #include "PTXSubtarget.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/CallingConvLower.h" |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/SelectionDAG.h" |
| 24 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Calling Convention Implementation |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | #include "PTXGenCallingConv.inc" |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // TargetLowering Implementation |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 40 | PTXTargetLowering::PTXTargetLowering(TargetMachine &TM) |
| 41 | : TargetLowering(TM, new TargetLoweringObjectFileELF()) { |
| 42 | // Set up the register classes. |
Justin Holewinski | 1b91bcd | 2011-06-16 17:49:58 +0000 | [diff] [blame] | 43 | addRegisterClass(MVT::i1, PTX::RegPredRegisterClass); |
| 44 | addRegisterClass(MVT::i16, PTX::RegI16RegisterClass); |
| 45 | addRegisterClass(MVT::i32, PTX::RegI32RegisterClass); |
| 46 | addRegisterClass(MVT::i64, PTX::RegI64RegisterClass); |
| 47 | addRegisterClass(MVT::f32, PTX::RegF32RegisterClass); |
| 48 | addRegisterClass(MVT::f64, PTX::RegF64RegisterClass); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 49 | |
Justin Holewinski | 4fea05a | 2011-04-28 00:19:52 +0000 | [diff] [blame] | 50 | setBooleanContents(ZeroOrOneBooleanContent); |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 51 | setMinFunctionAlignment(2); |
Dan Bailey | b05a8a8 | 2011-06-24 19:27:10 +0000 | [diff] [blame] | 52 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 53 | //////////////////////////////////// |
| 54 | /////////// Expansion ////////////// |
| 55 | //////////////////////////////////// |
Dan Bailey | b05a8a8 | 2011-06-24 19:27:10 +0000 | [diff] [blame] | 56 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 57 | // (any/zero/sign) extload => load + (any/zero/sign) extend |
Dan Bailey | b05a8a8 | 2011-06-24 19:27:10 +0000 | [diff] [blame] | 58 | |
Justin Holewinski | 4fea05a | 2011-04-28 00:19:52 +0000 | [diff] [blame] | 59 | setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand); |
| 60 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand); |
Dan Bailey | b05a8a8 | 2011-06-24 19:27:10 +0000 | [diff] [blame] | 61 | setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand); |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 62 | |
| 63 | // f32 extload => load + fextend |
| 64 | |
| 65 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 66 | |
| 67 | // f64 truncstore => trunc + store |
| 68 | |
| 69 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 70 | |
| 71 | // sign_extend_inreg => sign_extend |
| 72 | |
| 73 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
| 74 | |
| 75 | // br_cc => brcond |
| 76 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 77 | setOperationAction(ISD::BR_CC, MVT::Other, Expand); |
| 78 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 79 | // select_cc => setcc |
| 80 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 81 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
| 82 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 83 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 84 | |
| 85 | //////////////////////////////////// |
| 86 | //////////// Legal ///////////////// |
| 87 | //////////////////////////////////// |
| 88 | |
| 89 | setOperationAction(ISD::ConstantFP, MVT::f32, Legal); |
| 90 | setOperationAction(ISD::ConstantFP, MVT::f64, Legal); |
| 91 | |
| 92 | //////////////////////////////////// |
| 93 | //////////// Custom //////////////// |
| 94 | //////////////////////////////////// |
| 95 | |
| 96 | // customise setcc to use bitwise logic if possible |
| 97 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::SETCC, MVT::i1, Custom); |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 99 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 100 | // customize translation of memory addresses |
| 101 | |
| 102 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 103 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 104 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 105 | // Compute derived properties from the register classes |
| 106 | computeRegisterProperties(); |
| 107 | } |
| 108 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 109 | MVT::SimpleValueType PTXTargetLowering::getSetCCResultType(EVT VT) const { |
| 110 | return MVT::i1; |
| 111 | } |
| 112 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 113 | SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 114 | switch (Op.getOpcode()) { |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 115 | default: |
| 116 | llvm_unreachable("Unimplemented operand"); |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 117 | case ISD::SETCC: |
| 118 | return LowerSETCC(Op, DAG); |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 119 | case ISD::GlobalAddress: |
| 120 | return LowerGlobalAddress(Op, DAG); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 124 | const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 125 | switch (Opcode) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 126 | default: |
| 127 | llvm_unreachable("Unknown opcode"); |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 128 | case PTXISD::COPY_ADDRESS: |
| 129 | return "PTXISD::COPY_ADDRESS"; |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 130 | case PTXISD::LOAD_PARAM: |
| 131 | return "PTXISD::LOAD_PARAM"; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 132 | case PTXISD::STORE_PARAM: |
| 133 | return "PTXISD::STORE_PARAM"; |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 134 | case PTXISD::EXIT: |
| 135 | return "PTXISD::EXIT"; |
| 136 | case PTXISD::RET: |
| 137 | return "PTXISD::RET"; |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 138 | case PTXISD::CALL: |
| 139 | return "PTXISD::CALL"; |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | //===----------------------------------------------------------------------===// |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 144 | // Custom Lower Operation |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 147 | SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { |
| 148 | assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer"); |
| 149 | SDValue Op0 = Op.getOperand(0); |
| 150 | SDValue Op1 = Op.getOperand(1); |
| 151 | SDValue Op2 = Op.getOperand(2); |
| 152 | DebugLoc dl = Op.getDebugLoc(); |
| 153 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 154 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 155 | // Look for X == 0, X == 1, X != 0, or X != 1 |
| 156 | // We can simplify these to bitwise logic |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 157 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 158 | if (Op1.getOpcode() == ISD::Constant && |
| 159 | (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 || |
| 160 | cast<ConstantSDNode>(Op1)->isNullValue()) && |
| 161 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| 162 | |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 163 | return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1); |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 164 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 165 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 166 | return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2); |
| 167 | } |
| 168 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 169 | SDValue PTXTargetLowering:: |
| 170 | LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { |
| 171 | EVT PtrVT = getPointerTy(); |
| 172 | DebugLoc dl = Op.getDebugLoc(); |
| 173 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 174 | |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 175 | assert(PtrVT.isSimple() && "Pointer must be to primitive type."); |
| 176 | |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 177 | SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT); |
| 178 | SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS, |
| 179 | dl, |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 180 | PtrVT.getSimpleVT(), |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 181 | targetGlobal); |
| 182 | |
| 183 | return movInstr; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | //===----------------------------------------------------------------------===// |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 187 | // Calling Convention Implementation |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | |
| 190 | SDValue PTXTargetLowering:: |
| 191 | LowerFormalArguments(SDValue Chain, |
| 192 | CallingConv::ID CallConv, |
| 193 | bool isVarArg, |
| 194 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 195 | DebugLoc dl, |
| 196 | SelectionDAG &DAG, |
| 197 | SmallVectorImpl<SDValue> &InVals) const { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 198 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
| 199 | |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 200 | MachineFunction &MF = DAG.getMachineFunction(); |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 201 | const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>(); |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 202 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
| 203 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 204 | switch (CallConv) { |
| 205 | default: |
| 206 | llvm_unreachable("Unsupported calling convention"); |
| 207 | break; |
| 208 | case CallingConv::PTX_Kernel: |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 209 | MFI->setKernel(true); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 210 | break; |
| 211 | case CallingConv::PTX_Device: |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 212 | MFI->setKernel(false); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 216 | // We do one of two things here: |
| 217 | // IsKernel || SM >= 2.0 -> Use param space for arguments |
| 218 | // SM < 2.0 -> Use registers for arguments |
Justin Holewinski | 35f4fb3 | 2011-06-24 16:27:49 +0000 | [diff] [blame] | 219 | if (MFI->isKernel() || ST.useParamSpaceForDeviceArgs()) { |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 220 | // We just need to emit the proper LOAD_PARAM ISDs |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 221 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 222 | |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 223 | assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) && |
| 224 | "Kernels cannot take pred operands"); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 225 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 226 | SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain, |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 227 | DAG.getTargetConstant(i, MVT::i32)); |
| 228 | InVals.push_back(ArgValue); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 229 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 230 | // Instead of storing a physical register in our argument list, we just |
| 231 | // store the total size of the parameter, in bits. The ASM printer |
| 232 | // knows how to process this. |
| 233 | MFI->addArgReg(Ins[i].VT.getStoreSizeInBits()); |
| 234 | } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 235 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 236 | else { |
| 237 | // For device functions, we use the PTX calling convention to do register |
| 238 | // assignments then create CopyFromReg ISDs for the allocated registers |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 239 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 240 | SmallVector<CCValAssign, 16> ArgLocs; |
| 241 | CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), ArgLocs, |
| 242 | *DAG.getContext()); |
| 243 | |
| 244 | CCInfo.AnalyzeFormalArguments(Ins, CC_PTX); |
| 245 | |
| 246 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 247 | |
| 248 | CCValAssign& VA = ArgLocs[i]; |
| 249 | EVT RegVT = VA.getLocVT(); |
| 250 | TargetRegisterClass* TRC = 0; |
| 251 | |
| 252 | assert(VA.isRegLoc() && "CCValAssign must be RegLoc"); |
| 253 | |
| 254 | // Determine which register class we need |
| 255 | if (RegVT == MVT::i1) { |
| 256 | TRC = PTX::RegPredRegisterClass; |
| 257 | } |
| 258 | else if (RegVT == MVT::i16) { |
| 259 | TRC = PTX::RegI16RegisterClass; |
| 260 | } |
| 261 | else if (RegVT == MVT::i32) { |
| 262 | TRC = PTX::RegI32RegisterClass; |
| 263 | } |
| 264 | else if (RegVT == MVT::i64) { |
| 265 | TRC = PTX::RegI64RegisterClass; |
| 266 | } |
| 267 | else if (RegVT == MVT::f32) { |
| 268 | TRC = PTX::RegF32RegisterClass; |
| 269 | } |
| 270 | else if (RegVT == MVT::f64) { |
| 271 | TRC = PTX::RegF64RegisterClass; |
| 272 | } |
| 273 | else { |
| 274 | llvm_unreachable("Unknown parameter type"); |
| 275 | } |
| 276 | |
| 277 | unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC); |
| 278 | MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg); |
| 279 | |
| 280 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); |
| 281 | InVals.push_back(ArgValue); |
| 282 | |
| 283 | MFI->addArgReg(VA.getLocReg()); |
| 284 | } |
| 285 | } |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 286 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 287 | return Chain; |
| 288 | } |
| 289 | |
| 290 | SDValue PTXTargetLowering:: |
| 291 | LowerReturn(SDValue Chain, |
| 292 | CallingConv::ID CallConv, |
| 293 | bool isVarArg, |
| 294 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 295 | const SmallVectorImpl<SDValue> &OutVals, |
| 296 | DebugLoc dl, |
| 297 | SelectionDAG &DAG) const { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 298 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 299 | |
| 300 | switch (CallConv) { |
| 301 | default: |
| 302 | llvm_unreachable("Unsupported calling convention."); |
| 303 | case CallingConv::PTX_Kernel: |
| 304 | assert(Outs.size() == 0 && "Kernel must return void."); |
| 305 | return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain); |
| 306 | case CallingConv::PTX_Device: |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 307 | //assert(Outs.size() <= 1 && "Can at most return one value."); |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 311 | MachineFunction& MF = DAG.getMachineFunction(); |
| 312 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 313 | |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 314 | SDValue Flag; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 315 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 316 | // Even though we could use the .param space for return arguments for |
| 317 | // device functions if SM >= 2.0 and the number of return arguments is |
| 318 | // only 1, we just always use registers since this makes the codegen |
| 319 | // easier. |
| 320 | SmallVector<CCValAssign, 16> RVLocs; |
| 321 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
| 322 | getTargetMachine(), RVLocs, *DAG.getContext()); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 323 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 324 | CCInfo.AnalyzeReturn(Outs, RetCC_PTX); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 325 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 326 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { |
| 327 | CCValAssign& VA = RVLocs[i]; |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 328 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 329 | assert(VA.isRegLoc() && "CCValAssign must be RegLoc"); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 330 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 331 | unsigned Reg = VA.getLocReg(); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 332 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 333 | DAG.getMachineFunction().getRegInfo().addLiveOut(Reg); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 334 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 335 | Chain = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i], Flag); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 336 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 337 | // Guarantee that all emitted copies are stuck together, |
| 338 | // avoiding something bad |
| 339 | Flag = Chain.getValue(1); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 340 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 341 | MFI->addRetReg(Reg); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 342 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 343 | |
| 344 | if (Flag.getNode() == 0) { |
| 345 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain); |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 346 | } |
| 347 | else { |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 348 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag); |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 349 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 350 | } |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 351 | |
| 352 | SDValue |
| 353 | PTXTargetLowering::LowerCall(SDValue Chain, SDValue Callee, |
| 354 | CallingConv::ID CallConv, bool isVarArg, |
| 355 | bool &isTailCall, |
| 356 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 357 | const SmallVectorImpl<SDValue> &OutVals, |
| 358 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 359 | DebugLoc dl, SelectionDAG &DAG, |
| 360 | SmallVectorImpl<SDValue> &InVals) const { |
| 361 | |
| 362 | MachineFunction& MF = DAG.getMachineFunction(); |
| 363 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 364 | |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 365 | assert(getTargetMachine().getSubtarget<PTXSubtarget>().callsAreHandled() && |
| 366 | "Calls are not handled for the target device"); |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 367 | |
| 368 | // Is there a more "LLVM"-way to create a variable-length array of values? |
| 369 | SDValue* ops = new SDValue[OutVals.size() + 2]; |
| 370 | |
| 371 | ops[0] = Chain; |
| 372 | |
| 373 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 374 | const GlobalValue *GV = G->getGlobal(); |
| 375 | Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy()); |
| 376 | ops[1] = Callee; |
| 377 | } else { |
| 378 | assert(false && "Function must be a GlobalAddressSDNode"); |
| 379 | } |
| 380 | |
| 381 | for (unsigned i = 0; i != OutVals.size(); ++i) { |
| 382 | unsigned Size = OutVals[i].getValueType().getSizeInBits(); |
| 383 | SDValue Index = DAG.getTargetConstant(MFI->getNextParam(Size), MVT::i32); |
| 384 | Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain, |
| 385 | Index, OutVals[i]); |
| 386 | ops[i+2] = Index; |
| 387 | } |
| 388 | |
| 389 | ops[0] = Chain; |
| 390 | |
| 391 | Chain = DAG.getNode(PTXISD::CALL, dl, MVT::Other, ops, OutVals.size()+2); |
| 392 | |
| 393 | delete [] ops; |
| 394 | |
| 395 | return Chain; |
| 396 | } |