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 | |
Benjamin Kramer | a3ac427 | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 187 | namespace { |
| 188 | struct argmap_entry { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 189 | MVT::SimpleValueType VT; |
| 190 | TargetRegisterClass *RC; |
| 191 | TargetRegisterClass::iterator loc; |
| 192 | |
| 193 | argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC) |
| 194 | : VT(_VT), RC(_RC), loc(_RC->begin()) {} |
| 195 | |
Benjamin Kramer | a3ac427 | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 196 | void reset() { loc = RC->begin(); } |
| 197 | bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 198 | } argmap[] = { |
Justin Holewinski | 1b91bcd | 2011-06-16 17:49:58 +0000 | [diff] [blame] | 199 | argmap_entry(MVT::i1, PTX::RegPredRegisterClass), |
| 200 | argmap_entry(MVT::i16, PTX::RegI16RegisterClass), |
| 201 | argmap_entry(MVT::i32, PTX::RegI32RegisterClass), |
| 202 | argmap_entry(MVT::i64, PTX::RegI64RegisterClass), |
| 203 | argmap_entry(MVT::f32, PTX::RegF32RegisterClass), |
| 204 | argmap_entry(MVT::f64, PTX::RegF64RegisterClass) |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 205 | }; |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 206 | } // end anonymous namespace |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 207 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 208 | SDValue PTXTargetLowering:: |
| 209 | LowerFormalArguments(SDValue Chain, |
| 210 | CallingConv::ID CallConv, |
| 211 | bool isVarArg, |
| 212 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 213 | DebugLoc dl, |
| 214 | SelectionDAG &DAG, |
| 215 | SmallVectorImpl<SDValue> &InVals) const { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 216 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
| 217 | |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 218 | MachineFunction &MF = DAG.getMachineFunction(); |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 219 | const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>(); |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 220 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
| 221 | |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 222 | switch (CallConv) { |
| 223 | default: |
| 224 | llvm_unreachable("Unsupported calling convention"); |
| 225 | break; |
| 226 | case CallingConv::PTX_Kernel: |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 227 | MFI->setKernel(true); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 228 | break; |
| 229 | case CallingConv::PTX_Device: |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 230 | MFI->setKernel(false); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 234 | // We do one of two things here: |
| 235 | // IsKernel || SM >= 2.0 -> Use param space for arguments |
| 236 | // SM < 2.0 -> Use registers for arguments |
Justin Holewinski | 35f4fb3 | 2011-06-24 16:27:49 +0000 | [diff] [blame] | 237 | if (MFI->isKernel() || ST.useParamSpaceForDeviceArgs()) { |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 238 | // We just need to emit the proper LOAD_PARAM ISDs |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 239 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 240 | |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 241 | assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) && |
| 242 | "Kernels cannot take pred operands"); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 243 | |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 244 | SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain, |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 245 | DAG.getTargetConstant(i, MVT::i32)); |
| 246 | InVals.push_back(ArgValue); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 247 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 248 | // Instead of storing a physical register in our argument list, we just |
| 249 | // store the total size of the parameter, in bits. The ASM printer |
| 250 | // knows how to process this. |
| 251 | MFI->addArgReg(Ins[i].VT.getStoreSizeInBits()); |
| 252 | } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 253 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 254 | else { |
| 255 | // For device functions, we use the PTX calling convention to do register |
| 256 | // assignments then create CopyFromReg ISDs for the allocated registers |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 257 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 258 | SmallVector<CCValAssign, 16> ArgLocs; |
| 259 | CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), ArgLocs, |
| 260 | *DAG.getContext()); |
| 261 | |
| 262 | CCInfo.AnalyzeFormalArguments(Ins, CC_PTX); |
| 263 | |
| 264 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 265 | |
| 266 | CCValAssign& VA = ArgLocs[i]; |
| 267 | EVT RegVT = VA.getLocVT(); |
| 268 | TargetRegisterClass* TRC = 0; |
| 269 | |
| 270 | assert(VA.isRegLoc() && "CCValAssign must be RegLoc"); |
| 271 | |
| 272 | // Determine which register class we need |
| 273 | if (RegVT == MVT::i1) { |
| 274 | TRC = PTX::RegPredRegisterClass; |
| 275 | } |
| 276 | else if (RegVT == MVT::i16) { |
| 277 | TRC = PTX::RegI16RegisterClass; |
| 278 | } |
| 279 | else if (RegVT == MVT::i32) { |
| 280 | TRC = PTX::RegI32RegisterClass; |
| 281 | } |
| 282 | else if (RegVT == MVT::i64) { |
| 283 | TRC = PTX::RegI64RegisterClass; |
| 284 | } |
| 285 | else if (RegVT == MVT::f32) { |
| 286 | TRC = PTX::RegF32RegisterClass; |
| 287 | } |
| 288 | else if (RegVT == MVT::f64) { |
| 289 | TRC = PTX::RegF64RegisterClass; |
| 290 | } |
| 291 | else { |
| 292 | llvm_unreachable("Unknown parameter type"); |
| 293 | } |
| 294 | |
| 295 | unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC); |
| 296 | MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg); |
| 297 | |
| 298 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); |
| 299 | InVals.push_back(ArgValue); |
| 300 | |
| 301 | MFI->addArgReg(VA.getLocReg()); |
| 302 | } |
| 303 | } |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 304 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 305 | return Chain; |
| 306 | } |
| 307 | |
| 308 | SDValue PTXTargetLowering:: |
| 309 | LowerReturn(SDValue Chain, |
| 310 | CallingConv::ID CallConv, |
| 311 | bool isVarArg, |
| 312 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 313 | const SmallVectorImpl<SDValue> &OutVals, |
| 314 | DebugLoc dl, |
| 315 | SelectionDAG &DAG) const { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 316 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 317 | |
| 318 | switch (CallConv) { |
| 319 | default: |
| 320 | llvm_unreachable("Unsupported calling convention."); |
| 321 | case CallingConv::PTX_Kernel: |
| 322 | assert(Outs.size() == 0 && "Kernel must return void."); |
| 323 | return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain); |
| 324 | case CallingConv::PTX_Device: |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 325 | //assert(Outs.size() <= 1 && "Can at most return one value."); |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 329 | MachineFunction& MF = DAG.getMachineFunction(); |
| 330 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 331 | |
Che-Liang Chiou | f9930da | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 332 | SDValue Flag; |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 333 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 334 | // Even though we could use the .param space for return arguments for |
| 335 | // device functions if SM >= 2.0 and the number of return arguments is |
| 336 | // only 1, we just always use registers since this makes the codegen |
| 337 | // easier. |
| 338 | SmallVector<CCValAssign, 16> RVLocs; |
| 339 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
| 340 | getTargetMachine(), RVLocs, *DAG.getContext()); |
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 | CCInfo.AnalyzeReturn(Outs, RetCC_PTX); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 343 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 344 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { |
| 345 | CCValAssign& VA = RVLocs[i]; |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 346 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 347 | assert(VA.isRegLoc() && "CCValAssign must be RegLoc"); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 348 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 349 | unsigned Reg = VA.getLocReg(); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 350 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 351 | DAG.getMachineFunction().getRegInfo().addLiveOut(Reg); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 352 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 353 | Chain = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i], Flag); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 354 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 355 | // Guarantee that all emitted copies are stuck together, |
| 356 | // avoiding something bad |
| 357 | Flag = Chain.getValue(1); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 358 | |
Justin Holewinski | d8149c1 | 2011-06-23 18:10:13 +0000 | [diff] [blame] | 359 | MFI->addRetReg(Reg); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 360 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 361 | |
| 362 | if (Flag.getNode() == 0) { |
| 363 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain); |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 364 | } |
| 365 | else { |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 366 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag); |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 367 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 368 | } |