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