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