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