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" |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/CallingConvLower.h" |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SelectionDAG.h" |
| 25 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 32 | // TargetLowering Implementation |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 35 | PTXTargetLowering::PTXTargetLowering(TargetMachine &TM) |
| 36 | : TargetLowering(TM, new TargetLoweringObjectFileELF()) { |
| 37 | // Set up the register classes. |
Justin Holewinski | 1b91bcd | 2011-06-16 17:49:58 +0000 | [diff] [blame] | 38 | addRegisterClass(MVT::i1, PTX::RegPredRegisterClass); |
| 39 | addRegisterClass(MVT::i16, PTX::RegI16RegisterClass); |
| 40 | addRegisterClass(MVT::i32, PTX::RegI32RegisterClass); |
| 41 | addRegisterClass(MVT::i64, PTX::RegI64RegisterClass); |
| 42 | addRegisterClass(MVT::f32, PTX::RegF32RegisterClass); |
| 43 | addRegisterClass(MVT::f64, PTX::RegF64RegisterClass); |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 44 | |
Justin Holewinski | 4fea05a | 2011-04-28 00:19:52 +0000 | [diff] [blame] | 45 | setBooleanContents(ZeroOrOneBooleanContent); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 46 | setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct? |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 47 | setMinFunctionAlignment(2); |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 48 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 49 | //////////////////////////////////// |
| 50 | /////////// Expansion ////////////// |
| 51 | //////////////////////////////////// |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 52 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 53 | // (any/zero/sign) extload => load + (any/zero/sign) extend |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 54 | |
Justin Holewinski | 4fea05a | 2011-04-28 00:19:52 +0000 | [diff] [blame] | 55 | setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand); |
| 56 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand); |
Dan Bailey | b05a8a8 | 2011-06-24 19:27:10 +0000 | [diff] [blame] | 57 | setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand); |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 58 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 59 | // f32 extload => load + fextend |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 60 | |
| 61 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 62 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 63 | // f64 truncstore => trunc + store |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 64 | |
| 65 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 66 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 67 | // sign_extend_inreg => sign_extend |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 68 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 69 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 70 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 71 | // br_cc => brcond |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 72 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 73 | setOperationAction(ISD::BR_CC, MVT::Other, Expand); |
| 74 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 75 | // select_cc => setcc |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 76 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 77 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
| 78 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 79 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 80 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 81 | //////////////////////////////////// |
| 82 | //////////// Legal ///////////////// |
| 83 | //////////////////////////////////// |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 84 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 85 | setOperationAction(ISD::ConstantFP, MVT::f32, Legal); |
| 86 | setOperationAction(ISD::ConstantFP, MVT::f64, Legal); |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 87 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 88 | //////////////////////////////////// |
| 89 | //////////// Custom //////////////// |
| 90 | //////////////////////////////////// |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 91 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 92 | // customise setcc to use bitwise logic if possible |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 93 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::SETCC, MVT::i1, Custom); |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 95 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 96 | // customize translation of memory addresses |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 97 | |
Dan Bailey | 8414946 | 2011-06-25 18:16:28 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 99 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 100 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 101 | // Compute derived properties from the register classes |
| 102 | computeRegisterProperties(); |
| 103 | } |
| 104 | |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 105 | EVT PTXTargetLowering::getSetCCResultType(EVT VT) const { |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 106 | return MVT::i1; |
| 107 | } |
| 108 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 109 | SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 110 | switch (Op.getOpcode()) { |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 111 | default: |
| 112 | llvm_unreachable("Unimplemented operand"); |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 113 | case ISD::SETCC: |
| 114 | return LowerSETCC(Op, DAG); |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 115 | case ISD::GlobalAddress: |
| 116 | return LowerGlobalAddress(Op, DAG); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 120 | const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 121 | switch (Opcode) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 122 | default: |
| 123 | llvm_unreachable("Unknown opcode"); |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 124 | case PTXISD::COPY_ADDRESS: |
| 125 | return "PTXISD::COPY_ADDRESS"; |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 126 | case PTXISD::LOAD_PARAM: |
| 127 | return "PTXISD::LOAD_PARAM"; |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 128 | case PTXISD::STORE_PARAM: |
| 129 | return "PTXISD::STORE_PARAM"; |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 130 | case PTXISD::READ_PARAM: |
| 131 | return "PTXISD::READ_PARAM"; |
| 132 | case PTXISD::WRITE_PARAM: |
| 133 | return "PTXISD::WRITE_PARAM"; |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 134 | case PTXISD::EXIT: |
| 135 | return "PTXISD::EXIT"; |
| 136 | case PTXISD::RET: |
| 137 | return "PTXISD::RET"; |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 138 | case PTXISD::CALL: |
| 139 | return "PTXISD::CALL"; |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | //===----------------------------------------------------------------------===// |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 144 | // Custom Lower Operation |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 147 | SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { |
| 148 | assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer"); |
| 149 | SDValue Op0 = Op.getOperand(0); |
| 150 | SDValue Op1 = Op.getOperand(1); |
| 151 | SDValue Op2 = Op.getOperand(2); |
| 152 | DebugLoc dl = Op.getDebugLoc(); |
| 153 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 154 | |
Justin Holewinski | 05591be | 2011-09-22 16:45:43 +0000 | [diff] [blame] | 155 | // Look for X == 0, X == 1, X != 0, or X != 1 |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 156 | // We can simplify these to bitwise logic |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 157 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 158 | if (Op1.getOpcode() == ISD::Constant && |
| 159 | (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 || |
| 160 | cast<ConstantSDNode>(Op1)->isNullValue()) && |
| 161 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| 162 | |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 163 | return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1); |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 164 | } |
Justin Holewinski | ec3141b | 2011-06-16 15:17:11 +0000 | [diff] [blame] | 165 | |
Justin Holewinski | 2d525c5 | 2011-04-28 00:19:56 +0000 | [diff] [blame] | 166 | return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2); |
| 167 | } |
| 168 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 169 | SDValue PTXTargetLowering:: |
| 170 | LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { |
| 171 | EVT PtrVT = getPointerTy(); |
| 172 | DebugLoc dl = Op.getDebugLoc(); |
| 173 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 174 | |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 175 | assert(PtrVT.isSimple() && "Pointer must be to primitive type."); |
| 176 | |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 177 | SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT); |
| 178 | SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS, |
| 179 | dl, |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 180 | PtrVT.getSimpleVT(), |
Justin Holewinski | 8af78c9 | 2011-03-18 19:24:28 +0000 | [diff] [blame] | 181 | targetGlobal); |
| 182 | |
| 183 | return movInstr; |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | //===----------------------------------------------------------------------===// |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 187 | // Calling Convention Implementation |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | |
| 190 | SDValue PTXTargetLowering:: |
| 191 | LowerFormalArguments(SDValue Chain, |
| 192 | CallingConv::ID CallConv, |
| 193 | bool isVarArg, |
| 194 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 195 | DebugLoc dl, |
| 196 | SelectionDAG &DAG, |
| 197 | SmallVectorImpl<SDValue> &InVals) const { |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 198 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
| 199 | |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 200 | MachineFunction &MF = DAG.getMachineFunction(); |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 201 | const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>(); |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 202 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 203 | PTXParamManager &PM = MFI->getParamManager(); |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 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) { |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 223 | assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) && |
| 224 | "Kernels cannot take pred operands"); |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 225 | |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 226 | unsigned ParamSize = Ins[i].VT.getStoreSizeInBits(); |
| 227 | unsigned Param = PM.addArgumentParam(ParamSize); |
Benjamin Kramer | 8adae0c | 2011-09-28 04:08:02 +0000 | [diff] [blame^] | 228 | const std::string &ParamName = PM.getParamName(Param); |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 229 | SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(), |
| 230 | MVT::Other); |
Justin Holewinski | a5ccb4e | 2011-06-23 18:10:05 +0000 | [diff] [blame] | 231 | SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain, |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 232 | ParamValue); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 233 | InVals.push_back(ArgValue); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 234 | } |
Che-Liang Chiou | b48f2c2 | 2010-10-19 13:14:40 +0000 | [diff] [blame] | 235 | } |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 236 | else { |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 237 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 238 | EVT RegVT = Ins[i].VT; |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 239 | TargetRegisterClass* TRC = 0; |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 240 | int OpCode; |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 241 | |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 242 | // Determine which register class we need |
| 243 | if (RegVT == MVT::i1) { |
| 244 | TRC = PTX::RegPredRegisterClass; |
| 245 | OpCode = PTX::READPARAMPRED; |
| 246 | } |
| 247 | else if (RegVT == MVT::i16) { |
| 248 | TRC = PTX::RegI16RegisterClass; |
| 249 | OpCode = PTX::READPARAMI16; |
| 250 | } |
| 251 | else if (RegVT == MVT::i32) { |
| 252 | TRC = PTX::RegI32RegisterClass; |
| 253 | OpCode = PTX::READPARAMI32; |
| 254 | } |
| 255 | else if (RegVT == MVT::i64) { |
| 256 | TRC = PTX::RegI64RegisterClass; |
| 257 | OpCode = PTX::READPARAMI64; |
| 258 | } |
| 259 | else if (RegVT == MVT::f32) { |
| 260 | TRC = PTX::RegF32RegisterClass; |
| 261 | OpCode = PTX::READPARAMF32; |
| 262 | } |
| 263 | else if (RegVT == MVT::f64) { |
| 264 | TRC = PTX::RegF64RegisterClass; |
| 265 | OpCode = PTX::READPARAMF64; |
| 266 | } |
| 267 | else { |
| 268 | llvm_unreachable("Unknown parameter type"); |
| 269 | } |
| 270 | |
| 271 | // Use a unique index in the instruction to prevent instruction folding. |
| 272 | // Yes, this is a hack. |
| 273 | SDValue Index = DAG.getTargetConstant(i, MVT::i32); |
| 274 | unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC); |
| 275 | SDValue ArgValue = DAG.getNode(PTXISD::READ_PARAM, dl, RegVT, Chain, |
| 276 | Index); |
| 277 | |
| 278 | SDValue Flag = ArgValue.getValue(1); |
| 279 | |
| 280 | SDValue Copy = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); |
| 281 | SDValue RegValue = DAG.getRegister(Reg, RegVT); |
| 282 | InVals.push_back(ArgValue); |
| 283 | |
| 284 | MFI->addArgReg(Reg); |
| 285 | } |
| 286 | } |
| 287 | |
| 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 { |
| 299 | if (isVarArg) llvm_unreachable("PTX does not support varargs"); |
| 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: |
| 308 | assert(Outs.size() <= 1 && "Can at most return one value."); |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | MachineFunction& MF = DAG.getMachineFunction(); |
| 313 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 314 | PTXParamManager &PM = MFI->getParamManager(); |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 315 | |
| 316 | SDValue Flag; |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 317 | const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>(); |
| 318 | |
| 319 | if (ST.useParamSpaceForDeviceArgs()) { |
| 320 | assert(Outs.size() < 2 && "Device functions can return at most one value"); |
| 321 | |
| 322 | if (Outs.size() == 1) { |
Justin Holewinski | 27f08fc | 2011-09-23 14:18:22 +0000 | [diff] [blame] | 323 | unsigned ParamSize = OutVals[0].getValueType().getSizeInBits(); |
| 324 | unsigned Param = PM.addReturnParam(ParamSize); |
Benjamin Kramer | 8adae0c | 2011-09-28 04:08:02 +0000 | [diff] [blame^] | 325 | const std::string &ParamName = PM.getParamName(Param); |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 326 | SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(), |
| 327 | MVT::Other); |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 328 | Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain, |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 329 | ParamValue, OutVals[0]); |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 330 | } |
| 331 | } else { |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 332 | for (unsigned i = 0, e = Outs.size(); i != e; ++i) { |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 333 | EVT RegVT = Outs[i].VT; |
| 334 | TargetRegisterClass* TRC = 0; |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 335 | |
| 336 | // Determine which register class we need |
| 337 | if (RegVT == MVT::i1) { |
| 338 | TRC = PTX::RegPredRegisterClass; |
| 339 | } |
| 340 | else if (RegVT == MVT::i16) { |
| 341 | TRC = PTX::RegI16RegisterClass; |
| 342 | } |
| 343 | else if (RegVT == MVT::i32) { |
| 344 | TRC = PTX::RegI32RegisterClass; |
| 345 | } |
| 346 | else if (RegVT == MVT::i64) { |
| 347 | TRC = PTX::RegI64RegisterClass; |
| 348 | } |
| 349 | else if (RegVT == MVT::f32) { |
| 350 | TRC = PTX::RegF32RegisterClass; |
| 351 | } |
| 352 | else if (RegVT == MVT::f64) { |
| 353 | TRC = PTX::RegF64RegisterClass; |
| 354 | } |
| 355 | else { |
| 356 | llvm_unreachable("Unknown parameter type"); |
| 357 | } |
| 358 | |
| 359 | unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 360 | |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 361 | SDValue Copy = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i]/*, Flag*/); |
| 362 | SDValue OutReg = DAG.getRegister(Reg, RegVT); |
| 363 | |
| 364 | Chain = DAG.getNode(PTXISD::WRITE_PARAM, dl, MVT::Other, Copy, OutReg); |
Justin Holewinski | 5422a0f | 2011-09-22 16:45:46 +0000 | [diff] [blame] | 365 | |
| 366 | MFI->addRetReg(Reg); |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
Che-Liang Chiou | 3278c42 | 2010-11-08 03:00:52 +0000 | [diff] [blame] | 369 | |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 370 | if (Flag.getNode() == 0) { |
| 371 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain); |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 372 | } |
| 373 | else { |
Justin Holewinski | e0aef2d | 2011-06-16 17:50:00 +0000 | [diff] [blame] | 374 | return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag); |
Che-Liang Chiou | f717202 | 2011-02-28 06:34:09 +0000 | [diff] [blame] | 375 | } |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 376 | } |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 377 | |
| 378 | SDValue |
| 379 | PTXTargetLowering::LowerCall(SDValue Chain, SDValue Callee, |
| 380 | CallingConv::ID CallConv, bool isVarArg, |
| 381 | bool &isTailCall, |
| 382 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 383 | const SmallVectorImpl<SDValue> &OutVals, |
| 384 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 385 | DebugLoc dl, SelectionDAG &DAG, |
| 386 | SmallVectorImpl<SDValue> &InVals) const { |
| 387 | |
| 388 | MachineFunction& MF = DAG.getMachineFunction(); |
| 389 | PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>(); |
Justin Holewinski | e953a64 | 2011-09-23 14:31:12 +0000 | [diff] [blame] | 390 | PTXParamManager &PM = MFI->getParamManager(); |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 391 | |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 392 | assert(getTargetMachine().getSubtarget<PTXSubtarget>().callsAreHandled() && |
| 393 | "Calls are not handled for the target device"); |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 394 | |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 395 | std::vector<SDValue> Ops; |
| 396 | // The layout of the ops will be [Chain, Ins, Callee, Outs] |
| 397 | Ops.resize(Outs.size() + Ins.size() + 2); |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 398 | |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 399 | Ops[0] = Chain; |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 400 | |
Justin Holewinski | dc0baf9 | 2011-09-23 17:15:53 +0000 | [diff] [blame] | 401 | // Identify the callee function |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 402 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 403 | const GlobalValue *GV = G->getGlobal(); |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 404 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 405 | assert(F->getCallingConv() == CallingConv::PTX_Device && |
| 406 | "PTX function calls must be to PTX device functions"); |
| 407 | Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy()); |
| 408 | Ops[Ins.size()+1] = Callee; |
| 409 | } else { |
| 410 | assert(false && "GlobalValue is not a function"); |
| 411 | } |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 412 | } else { |
| 413 | assert(false && "Function must be a GlobalAddressSDNode"); |
| 414 | } |
| 415 | |
Justin Holewinski | dc0baf9 | 2011-09-23 17:15:53 +0000 | [diff] [blame] | 416 | // Generate STORE_PARAM nodes for each function argument. In PTX, function |
| 417 | // arguments are explicitly stored into .param variables and passed as |
| 418 | // arguments. There is no register/stack-based calling convention in PTX. |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 419 | for (unsigned i = 0; i != OutVals.size(); ++i) { |
| 420 | unsigned Size = OutVals[i].getValueType().getSizeInBits(); |
Justin Holewinski | e953a64 | 2011-09-23 14:31:12 +0000 | [diff] [blame] | 421 | unsigned Param = PM.addLocalParam(Size); |
Benjamin Kramer | 8adae0c | 2011-09-28 04:08:02 +0000 | [diff] [blame^] | 422 | const std::string &ParamName = PM.getParamName(Param); |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 423 | SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(), |
| 424 | MVT::Other); |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 425 | Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain, |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 426 | ParamValue, OutVals[i]); |
| 427 | Ops[i+Ins.size()+2] = ParamValue; |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 430 | std::vector<SDValue> InParams; |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 431 | |
Justin Holewinski | dc0baf9 | 2011-09-23 17:15:53 +0000 | [diff] [blame] | 432 | // Generate list of .param variables to hold the return value(s). |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 433 | for (unsigned i = 0; i < Ins.size(); ++i) { |
| 434 | unsigned Size = Ins[i].VT.getStoreSizeInBits(); |
| 435 | unsigned Param = PM.addLocalParam(Size); |
Benjamin Kramer | 8adae0c | 2011-09-28 04:08:02 +0000 | [diff] [blame^] | 436 | const std::string &ParamName = PM.getParamName(Param); |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 437 | SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(), |
| 438 | MVT::Other); |
| 439 | Ops[i+1] = ParamValue; |
| 440 | InParams.push_back(ParamValue); |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 441 | } |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 442 | |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 443 | Ops[0] = Chain; |
| 444 | |
Justin Holewinski | dc0baf9 | 2011-09-23 17:15:53 +0000 | [diff] [blame] | 445 | // Create the CALL node. |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 446 | Chain = DAG.getNode(PTXISD::CALL, dl, MVT::Other, &Ops[0], Ops.size()); |
| 447 | |
Justin Holewinski | dc0baf9 | 2011-09-23 17:15:53 +0000 | [diff] [blame] | 448 | // Create the LOAD_PARAM nodes that retrieve the function return value(s). |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 449 | for (unsigned i = 0; i < Ins.size(); ++i) { |
Justin Holewinski | f47dfba | 2011-09-27 18:12:55 +0000 | [diff] [blame] | 450 | SDValue Load = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain, |
| 451 | InParams[i]); |
Justin Holewinski | 75d8095 | 2011-09-23 16:48:41 +0000 | [diff] [blame] | 452 | InVals.push_back(Load); |
| 453 | } |
Justin Holewinski | 4bdd4ed | 2011-08-09 17:36:31 +0000 | [diff] [blame] | 454 | |
| 455 | return Chain; |
| 456 | } |