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); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 50 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 51 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); |
| 52 | |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 53 | setOperationAction(ISD::ConstantFP, MVT::f32, Legal); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 54 | setOperationAction(ISD::ConstantFP, MVT::f64, Legal); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 55 | |
Justin Holewinski | 4fea05a | 2011-04-28 00:19:52 +0000 | [diff] [blame] | 56 | // Turn i16 (z)extload into load + (z)extend |
| 57 | setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand); |
| 58 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 59 | |
Justin Holewinski | 4fea05a | 2011-04-28 00:19:52 +0000 | [diff] [blame] | 60 | // Turn f32 extload into load + fextend |
| 61 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 62 | |
Justin Holewinski | 4fea05a | 2011-04-28 00:19:52 +0000 | [diff] [blame] | 63 | // Turn f64 truncstore into trunc + store. |
| 64 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 65 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 66 | // Customize translation of memory addresses |
| 67 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 68 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 69 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 70 | // Expand BR_CC into BRCOND |
| 71 | setOperationAction(ISD::BR_CC, MVT::Other, Expand); |
| 72 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 73 | // Expand SELECT_CC into SETCC |
| 74 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
| 75 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 76 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 77 | |
Justin Holewinski | 1b91bcd | 2011-06-16 17:49:58 +0000 | [diff] [blame] | 78 | // need to lower SETCC of RegPred into bitwise logic |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 79 | setOperationAction(ISD::SETCC, MVT::i1, Custom); |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 80 | |
| 81 | setMinFunctionAlignment(2); |
| 82 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 83 | // Compute derived properties from the register classes |
| 84 | computeRegisterProperties(); |
| 85 | } |
| 86 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 87 | MVT::SimpleValueType PTXTargetLowering::getSetCCResultType(EVT VT) const { |
| 88 | return MVT::i1; |
| 89 | } |
| 90 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 91 | SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 92 | switch (Op.getOpcode()) { |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 93 | default: |
| 94 | llvm_unreachable("Unimplemented operand"); |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 95 | case ISD::SETCC: |
| 96 | return LowerSETCC(Op, DAG); |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 97 | case ISD::GlobalAddress: |
| 98 | return LowerGlobalAddress(Op, DAG); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 102 | const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 103 | switch (Opcode) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 104 | default: |
| 105 | llvm_unreachable("Unknown opcode"); |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 106 | case PTXISD::COPY_ADDRESS: |
| 107 | return "PTXISD::COPY_ADDRESS"; |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 108 | case PTXISD::LOAD_PARAM: |
| 109 | return "PTXISD::LOAD_PARAM"; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 110 | case PTXISD::STORE_PARAM: |
| 111 | return "PTXISD::STORE_PARAM"; |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 112 | case PTXISD::EXIT: |
| 113 | return "PTXISD::EXIT"; |
| 114 | case PTXISD::RET: |
| 115 | return "PTXISD::RET"; |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | //===----------------------------------------------------------------------===// |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 120 | // Custom Lower Operation |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 123 | SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { |
| 124 | assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer"); |
| 125 | SDValue Op0 = Op.getOperand(0); |
| 126 | SDValue Op1 = Op.getOperand(1); |
| 127 | SDValue Op2 = Op.getOperand(2); |
| 128 | DebugLoc dl = Op.getDebugLoc(); |
| 129 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 130 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 131 | // Look for X == 0, X == 1, X != 0, or X != 1 |
| 132 | // We can simplify these to bitwise logic |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 133 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 134 | if (Op1.getOpcode() == ISD::Constant && |
| 135 | (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 || |
| 136 | cast<ConstantSDNode>(Op1)->isNullValue()) && |
| 137 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| 138 | |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 139 | return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1); |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 140 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 141 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 142 | return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2); |
| 143 | } |
| 144 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 145 | SDValue PTXTargetLowering:: |
| 146 | LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { |
| 147 | EVT PtrVT = getPointerTy(); |
| 148 | DebugLoc dl = Op.getDebugLoc(); |
| 149 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 150 | |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 151 | assert(PtrVT.isSimple() && "Pointer must be to primitive type."); |
| 152 | |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 153 | SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT); |
| 154 | SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS, |
| 155 | dl, |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 156 | PtrVT.getSimpleVT(), |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 157 | targetGlobal); |
| 158 | |
| 159 | return movInstr; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | //===----------------------------------------------------------------------===// |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 163 | // Calling Convention Implementation |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | |
Benjamin Kramer | a3ac427 | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 166 | namespace { |
| 167 | struct argmap_entry { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 168 | MVT::SimpleValueType VT; |
| 169 | TargetRegisterClass *RC; |
| 170 | TargetRegisterClass::iterator loc; |
| 171 | |
| 172 | argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC) |
| 173 | : VT(_VT), RC(_RC), loc(_RC->begin()) {} |
| 174 | |
Benjamin Kramer | a3ac427 | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 175 | void reset() { loc = RC->begin(); } |
| 176 | bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 177 | } argmap[] = { |
Justin Holewinski | 1b91bcd | 2011-06-16 17:49:58 +0000 | [diff] [blame] | 178 | argmap_entry(MVT::i1, PTX::RegPredRegisterClass), |
| 179 | argmap_entry(MVT::i16, PTX::RegI16RegisterClass), |
| 180 | argmap_entry(MVT::i32, PTX::RegI32RegisterClass), |
| 181 | argmap_entry(MVT::i64, PTX::RegI64RegisterClass), |
| 182 | argmap_entry(MVT::f32, PTX::RegF32RegisterClass), |
| 183 | argmap_entry(MVT::f64, PTX::RegF64RegisterClass) |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 184 | }; |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 185 | } // end anonymous namespace |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 186 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 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 | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 216 | if (MFI->isKernel() || ST.getShaderModel() >= PTXSubtarget::PTX_SM_2_0) { |
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 | } |