blob: e5d4edc405d17f69dae052d84975eaba3d699686 [file] [log] [blame]
Eric Christopher50880d02010-09-18 18:52:28 +00001//===-- 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 Chioub48f2c22010-10-19 13:14:40 +000014#include "PTX.h"
Eric Christopher50880d02010-09-18 18:52:28 +000015#include "PTXISelLowering.h"
Che-Liang Chiou3278c422010-11-08 03:00:52 +000016#include "PTXMachineFunctionInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000017#include "PTXRegisterInfo.h"
Justin Holewinski67a91842011-06-23 18:10:03 +000018#include "PTXSubtarget.h"
Justin Holewinski75d80952011-09-23 16:48:41 +000019#include "llvm/Function.h"
Eric Christopher50880d02010-09-18 18:52:28 +000020#include "llvm/Support/ErrorHandling.h"
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000021#include "llvm/CodeGen/CallingConvLower.h"
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000022#include "llvm/CodeGen/MachineFunction.h"
Dan Bailey96e64582011-11-11 14:45:12 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +000027#include "llvm/Support/Debug.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000028#include "llvm/Support/raw_ostream.h"
Eric Christopher50880d02010-09-18 18:52:28 +000029
30using namespace llvm;
31
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000032//===----------------------------------------------------------------------===//
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000033// TargetLowering Implementation
34//===----------------------------------------------------------------------===//
35
Eric Christopher50880d02010-09-18 18:52:28 +000036PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
37 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
38 // Set up the register classes.
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000039 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 Chioufd8978b2011-03-02 03:20:28 +000045
Justin Holewinski4fea05a2011-04-28 00:19:52 +000046 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000047 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Dan Bailey84149462011-06-25 18:16:28 +000048 setMinFunctionAlignment(2);
Justin Holewinski05591be2011-09-22 16:45:43 +000049
Justin Holewinskia3f7e222011-11-14 18:58:20 +000050 // Let LLVM use loads/stores for all mem* operations
51 maxStoresPerMemcpy = 4096;
52 maxStoresPerMemmove = 4096;
53 maxStoresPerMemset = 4096;
54
Dan Bailey84149462011-06-25 18:16:28 +000055 ////////////////////////////////////
56 /////////// Expansion //////////////
57 ////////////////////////////////////
Justin Holewinski05591be2011-09-22 16:45:43 +000058
Dan Bailey84149462011-06-25 18:16:28 +000059 // (any/zero/sign) extload => load + (any/zero/sign) extend
Justin Holewinski05591be2011-09-22 16:45:43 +000060
Justin Holewinski4fea05a2011-04-28 00:19:52 +000061 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand);
62 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
Dan Baileyb05a8a82011-06-24 19:27:10 +000063 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Justin Holewinski05591be2011-09-22 16:45:43 +000064
Dan Bailey84149462011-06-25 18:16:28 +000065 // f32 extload => load + fextend
Justin Holewinski05591be2011-09-22 16:45:43 +000066
67 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
68
Dan Bailey84149462011-06-25 18:16:28 +000069 // f64 truncstore => trunc + store
Justin Holewinski05591be2011-09-22 16:45:43 +000070
71 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
72
Dan Bailey84149462011-06-25 18:16:28 +000073 // sign_extend_inreg => sign_extend
Justin Holewinski05591be2011-09-22 16:45:43 +000074
Dan Bailey84149462011-06-25 18:16:28 +000075 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Justin Holewinski05591be2011-09-22 16:45:43 +000076
Dan Bailey84149462011-06-25 18:16:28 +000077 // br_cc => brcond
Justin Holewinski05591be2011-09-22 16:45:43 +000078
Che-Liang Chiou88d33672011-03-18 11:08:52 +000079 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
80
Dan Bailey84149462011-06-25 18:16:28 +000081 // select_cc => setcc
Justin Holewinski05591be2011-09-22 16:45:43 +000082
Justin Holewinski2d525c52011-04-28 00:19:56 +000083 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
84 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
85 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Justin Holewinski05591be2011-09-22 16:45:43 +000086
Dan Bailey84149462011-06-25 18:16:28 +000087 ////////////////////////////////////
88 //////////// Legal /////////////////
89 ////////////////////////////////////
Justin Holewinski05591be2011-09-22 16:45:43 +000090
Dan Bailey84149462011-06-25 18:16:28 +000091 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
92 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
Justin Holewinski05591be2011-09-22 16:45:43 +000093
Dan Bailey84149462011-06-25 18:16:28 +000094 ////////////////////////////////////
95 //////////// Custom ////////////////
96 ////////////////////////////////////
Justin Holewinski05591be2011-09-22 16:45:43 +000097
Dan Bailey84149462011-06-25 18:16:28 +000098 // customise setcc to use bitwise logic if possible
Justin Holewinski05591be2011-09-22 16:45:43 +000099
Justin Holewinski2d525c52011-04-28 00:19:56 +0000100 setOperationAction(ISD::SETCC, MVT::i1, Custom);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000101
Dan Bailey84149462011-06-25 18:16:28 +0000102 // customize translation of memory addresses
Justin Holewinski05591be2011-09-22 16:45:43 +0000103
Dan Bailey84149462011-06-25 18:16:28 +0000104 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
105 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000106
Eric Christopher50880d02010-09-18 18:52:28 +0000107 // Compute derived properties from the register classes
108 computeRegisterProperties();
109}
110
Duncan Sands28b77e92011-09-06 19:07:46 +0000111EVT PTXTargetLowering::getSetCCResultType(EVT VT) const {
Justin Holewinski2d525c52011-04-28 00:19:56 +0000112 return MVT::i1;
113}
114
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000115SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
116 switch (Op.getOpcode()) {
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000117 default:
118 llvm_unreachable("Unimplemented operand");
Justin Holewinski2d525c52011-04-28 00:19:56 +0000119 case ISD::SETCC:
120 return LowerSETCC(Op, DAG);
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000121 case ISD::GlobalAddress:
122 return LowerGlobalAddress(Op, DAG);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000123 }
124}
125
Eric Christopher50880d02010-09-18 18:52:28 +0000126const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
127 switch (Opcode) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000128 default:
129 llvm_unreachable("Unknown opcode");
Justin Holewinski8af78c92011-03-18 19:24:28 +0000130 case PTXISD::COPY_ADDRESS:
131 return "PTXISD::COPY_ADDRESS";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000132 case PTXISD::LOAD_PARAM:
133 return "PTXISD::LOAD_PARAM";
Justin Holewinski67a91842011-06-23 18:10:03 +0000134 case PTXISD::STORE_PARAM:
135 return "PTXISD::STORE_PARAM";
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000136 case PTXISD::READ_PARAM:
137 return "PTXISD::READ_PARAM";
138 case PTXISD::WRITE_PARAM:
139 return "PTXISD::WRITE_PARAM";
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000140 case PTXISD::EXIT:
141 return "PTXISD::EXIT";
142 case PTXISD::RET:
143 return "PTXISD::RET";
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000144 case PTXISD::CALL:
145 return "PTXISD::CALL";
Eric Christopher50880d02010-09-18 18:52:28 +0000146 }
147}
148
149//===----------------------------------------------------------------------===//
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000150// Custom Lower Operation
151//===----------------------------------------------------------------------===//
152
Justin Holewinski2d525c52011-04-28 00:19:56 +0000153SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
154 assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer");
155 SDValue Op0 = Op.getOperand(0);
156 SDValue Op1 = Op.getOperand(1);
157 SDValue Op2 = Op.getOperand(2);
158 DebugLoc dl = Op.getDebugLoc();
159 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000160
Justin Holewinski05591be2011-09-22 16:45:43 +0000161 // Look for X == 0, X == 1, X != 0, or X != 1
Justin Holewinski2d525c52011-04-28 00:19:56 +0000162 // We can simplify these to bitwise logic
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000163
Justin Holewinski2d525c52011-04-28 00:19:56 +0000164 if (Op1.getOpcode() == ISD::Constant &&
165 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
166 cast<ConstantSDNode>(Op1)->isNullValue()) &&
167 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
168
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000169 return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1);
Justin Holewinski2d525c52011-04-28 00:19:56 +0000170 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000171
Justin Holewinski2d525c52011-04-28 00:19:56 +0000172 return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2);
173}
174
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000175SDValue PTXTargetLowering::
176LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
177 EVT PtrVT = getPointerTy();
178 DebugLoc dl = Op.getDebugLoc();
179 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Justin Holewinski8af78c92011-03-18 19:24:28 +0000180
Justin Holewinskid6625762011-03-23 16:58:51 +0000181 assert(PtrVT.isSimple() && "Pointer must be to primitive type.");
182
Justin Holewinski8af78c92011-03-18 19:24:28 +0000183 SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
184 SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS,
185 dl,
Justin Holewinskid6625762011-03-23 16:58:51 +0000186 PtrVT.getSimpleVT(),
Justin Holewinski8af78c92011-03-18 19:24:28 +0000187 targetGlobal);
188
189 return movInstr;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000190}
191
192//===----------------------------------------------------------------------===//
Eric Christopher50880d02010-09-18 18:52:28 +0000193// Calling Convention Implementation
194//===----------------------------------------------------------------------===//
195
196SDValue PTXTargetLowering::
197 LowerFormalArguments(SDValue Chain,
198 CallingConv::ID CallConv,
199 bool isVarArg,
200 const SmallVectorImpl<ISD::InputArg> &Ins,
201 DebugLoc dl,
202 SelectionDAG &DAG,
203 SmallVectorImpl<SDValue> &InVals) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000204 if (isVarArg) llvm_unreachable("PTX does not support varargs");
205
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000206 MachineFunction &MF = DAG.getMachineFunction();
Justin Holewinski67a91842011-06-23 18:10:03 +0000207 const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>();
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000208 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000209 PTXParamManager &PM = MFI->getParamManager();
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000210
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000211 switch (CallConv) {
212 default:
213 llvm_unreachable("Unsupported calling convention");
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000214 case CallingConv::PTX_Kernel:
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000215 MFI->setKernel(true);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000216 break;
217 case CallingConv::PTX_Device:
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000218 MFI->setKernel(false);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000219 break;
220 }
221
Justin Holewinski67a91842011-06-23 18:10:03 +0000222 // We do one of two things here:
223 // IsKernel || SM >= 2.0 -> Use param space for arguments
224 // SM < 2.0 -> Use registers for arguments
Justin Holewinski35f4fb32011-06-24 16:27:49 +0000225 if (MFI->isKernel() || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000226 // We just need to emit the proper LOAD_PARAM ISDs
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000227 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Justin Holewinski67a91842011-06-23 18:10:03 +0000228 assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) &&
229 "Kernels cannot take pred operands");
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000230
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000231 unsigned ParamSize = Ins[i].VT.getStoreSizeInBits();
232 unsigned Param = PM.addArgumentParam(ParamSize);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000233 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000234 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
235 MVT::Other);
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000236 SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain,
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000237 ParamValue);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000238 InVals.push_back(ArgValue);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000239 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000240 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000241 else {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000242 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Craig Topper44d23822012-02-22 05:59:10 +0000243 EVT RegVT = Ins[i].VT;
244 const TargetRegisterClass* TRC = getRegClassFor(RegVT);
245 unsigned RegType;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000246
247 // Determine which register class we need
Craig Topper44d23822012-02-22 05:59:10 +0000248 if (RegVT == MVT::i1)
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000249 RegType = PTXRegisterType::Pred;
Craig Topper44d23822012-02-22 05:59:10 +0000250 else if (RegVT == MVT::i16)
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000251 RegType = PTXRegisterType::B16;
Craig Topper44d23822012-02-22 05:59:10 +0000252 else if (RegVT == MVT::i32)
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000253 RegType = PTXRegisterType::B32;
Craig Topper44d23822012-02-22 05:59:10 +0000254 else if (RegVT == MVT::i64)
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000255 RegType = PTXRegisterType::B64;
Craig Topper44d23822012-02-22 05:59:10 +0000256 else if (RegVT == MVT::f32)
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000257 RegType = PTXRegisterType::F32;
Craig Topper44d23822012-02-22 05:59:10 +0000258 else if (RegVT == MVT::f64)
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000259 RegType = PTXRegisterType::F64;
Craig Topper44d23822012-02-22 05:59:10 +0000260 else
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000261 llvm_unreachable("Unknown parameter type");
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000262
263 // Use a unique index in the instruction to prevent instruction folding.
264 // Yes, this is a hack.
265 SDValue Index = DAG.getTargetConstant(i, MVT::i32);
266 unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
267 SDValue ArgValue = DAG.getNode(PTXISD::READ_PARAM, dl, RegVT, Chain,
268 Index);
269
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000270 InVals.push_back(ArgValue);
271
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000272 MFI->addRegister(Reg, RegType, PTXRegisterSpace::Argument);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000273 }
274 }
275
276 return Chain;
277}
278
279SDValue PTXTargetLowering::
280 LowerReturn(SDValue Chain,
281 CallingConv::ID CallConv,
282 bool isVarArg,
283 const SmallVectorImpl<ISD::OutputArg> &Outs,
284 const SmallVectorImpl<SDValue> &OutVals,
285 DebugLoc dl,
286 SelectionDAG &DAG) const {
287 if (isVarArg) llvm_unreachable("PTX does not support varargs");
288
289 switch (CallConv) {
290 default:
291 llvm_unreachable("Unsupported calling convention.");
292 case CallingConv::PTX_Kernel:
293 assert(Outs.size() == 0 && "Kernel must return void.");
294 return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
295 case CallingConv::PTX_Device:
296 assert(Outs.size() <= 1 && "Can at most return one value.");
297 break;
298 }
299
300 MachineFunction& MF = DAG.getMachineFunction();
301 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000302 PTXParamManager &PM = MFI->getParamManager();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000303
304 SDValue Flag;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000305 const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>();
306
307 if (ST.useParamSpaceForDeviceArgs()) {
308 assert(Outs.size() < 2 && "Device functions can return at most one value");
309
310 if (Outs.size() == 1) {
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000311 unsigned ParamSize = OutVals[0].getValueType().getSizeInBits();
312 unsigned Param = PM.addReturnParam(ParamSize);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000313 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000314 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
315 MVT::Other);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000316 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000317 ParamValue, OutVals[0]);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000318 }
319 } else {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000320 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000321 EVT RegVT = Outs[i].VT;
Craig Topper44d23822012-02-22 05:59:10 +0000322 const TargetRegisterClass* TRC;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000323 unsigned RegType;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000324
325 // Determine which register class we need
326 if (RegVT == MVT::i1) {
327 TRC = PTX::RegPredRegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000328 RegType = PTXRegisterType::Pred;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000329 }
330 else if (RegVT == MVT::i16) {
331 TRC = PTX::RegI16RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000332 RegType = PTXRegisterType::B16;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000333 }
334 else if (RegVT == MVT::i32) {
335 TRC = PTX::RegI32RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000336 RegType = PTXRegisterType::B32;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000337 }
338 else if (RegVT == MVT::i64) {
339 TRC = PTX::RegI64RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000340 RegType = PTXRegisterType::B64;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000341 }
342 else if (RegVT == MVT::f32) {
343 TRC = PTX::RegF32RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000344 RegType = PTXRegisterType::F32;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000345 }
346 else if (RegVT == MVT::f64) {
347 TRC = PTX::RegF64RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000348 RegType = PTXRegisterType::F64;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000349 }
350 else {
351 llvm_unreachable("Unknown parameter type");
352 }
353
354 unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000355
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000356 SDValue Copy = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i]/*, Flag*/);
357 SDValue OutReg = DAG.getRegister(Reg, RegVT);
358
359 Chain = DAG.getNode(PTXISD::WRITE_PARAM, dl, MVT::Other, Copy, OutReg);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000360
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000361 MFI->addRegister(Reg, RegType, PTXRegisterSpace::Return);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000362 }
363 }
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000364
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000365 if (Flag.getNode() == 0) {
366 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000367 }
368 else {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000369 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000370 }
Eric Christopher50880d02010-09-18 18:52:28 +0000371}
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000372
373SDValue
374PTXTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
375 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +0000376 bool doesNotRet, bool &isTailCall,
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000377 const SmallVectorImpl<ISD::OutputArg> &Outs,
378 const SmallVectorImpl<SDValue> &OutVals,
379 const SmallVectorImpl<ISD::InputArg> &Ins,
380 DebugLoc dl, SelectionDAG &DAG,
381 SmallVectorImpl<SDValue> &InVals) const {
382
383 MachineFunction& MF = DAG.getMachineFunction();
Dan Bailey96e64582011-11-11 14:45:12 +0000384 PTXMachineFunctionInfo *PTXMFI = MF.getInfo<PTXMachineFunctionInfo>();
385 PTXParamManager &PM = PTXMFI->getParamManager();
386 MachineFrameInfo *MFI = MF.getFrameInfo();
387
Duncan Sands1f6a3292011-08-12 14:54:45 +0000388 assert(getTargetMachine().getSubtarget<PTXSubtarget>().callsAreHandled() &&
389 "Calls are not handled for the target device");
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000390
Dan Bailey96e64582011-11-11 14:45:12 +0000391 // Identify the callee function
392 const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
393 const Function *function = cast<Function>(GV);
394
395 // allow non-device calls only for printf
396 bool isPrintf = function->getName() == "printf" || function->getName() == "puts";
397
398 assert((isPrintf || function->getCallingConv() == CallingConv::PTX_Device) &&
399 "PTX function calls must be to PTX device functions");
400
401 unsigned outSize = isPrintf ? 2 : Outs.size();
402
Justin Holewinski75d80952011-09-23 16:48:41 +0000403 std::vector<SDValue> Ops;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000404 // The layout of the ops will be [Chain, #Ins, Ins, Callee, #Outs, Outs]
Dan Bailey96e64582011-11-11 14:45:12 +0000405 Ops.resize(outSize + Ins.size() + 4);
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000406
Justin Holewinski75d80952011-09-23 16:48:41 +0000407 Ops[0] = Chain;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000408
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000409 // Identify the callee function
Benjamin Kramerca921592011-09-28 04:32:36 +0000410 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000411 Ops[Ins.size()+2] = Callee;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000412
Dan Bailey96e64582011-11-11 14:45:12 +0000413 // #Outs
414 Ops[Ins.size()+3] = DAG.getTargetConstant(outSize, MVT::i32);
415
416 if (isPrintf) {
417 // first argument is the address of the global string variable in memory
418 unsigned Param0 = PM.addLocalParam(getPointerTy().getSizeInBits());
419 SDValue ParamValue0 = DAG.getTargetExternalSymbol(PM.getParamName(Param0).c_str(),
420 MVT::Other);
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000421 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
Dan Bailey96e64582011-11-11 14:45:12 +0000422 ParamValue0, OutVals[0]);
423 Ops[Ins.size()+4] = ParamValue0;
424
425 // alignment is the maximum size of all the arguments
426 unsigned alignment = 0;
427 for (unsigned i = 1; i < OutVals.size(); ++i) {
428 alignment = std::max(alignment,
429 OutVals[i].getValueType().getSizeInBits());
430 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000431
Dan Bailey96e64582011-11-11 14:45:12 +0000432 // size is the alignment multiplied by the number of arguments
433 unsigned size = alignment * (OutVals.size() - 1);
434
435 // second argument is the address of the stack object (unless no arguments)
436 unsigned Param1 = PM.addLocalParam(getPointerTy().getSizeInBits());
437 SDValue ParamValue1 = DAG.getTargetExternalSymbol(PM.getParamName(Param1).c_str(),
438 MVT::Other);
439 Ops[Ins.size()+5] = ParamValue1;
440
441 if (size > 0)
442 {
443 // create a local stack object to store the arguments
444 unsigned StackObject = MFI->CreateStackObject(size / 8, alignment / 8, false);
445 SDValue FrameIndex = DAG.getFrameIndex(StackObject, getPointerTy());
446
447 // store each of the arguments to the stack in turn
448 for (unsigned int i = 1; i != OutVals.size(); i++) {
449 SDValue FrameAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FrameIndex, DAG.getTargetConstant((i - 1) * 8, getPointerTy()));
450 Chain = DAG.getStore(Chain, dl, OutVals[i], FrameAddr,
451 MachinePointerInfo(),
452 false, false, 0);
453 }
454
455 // copy the address of the local frame index to get the address in non-local space
456 SDValue genericAddr = DAG.getNode(PTXISD::COPY_ADDRESS, dl, getPointerTy(), FrameIndex);
457
458 // store this address in the second argument
459 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain, ParamValue1, genericAddr);
460 }
461 }
462 else
463 {
464 // Generate STORE_PARAM nodes for each function argument. In PTX, function
465 // arguments are explicitly stored into .param variables and passed as
466 // arguments. There is no register/stack-based calling convention in PTX.
467 for (unsigned i = 0; i != OutVals.size(); ++i) {
468 unsigned Size = OutVals[i].getValueType().getSizeInBits();
469 unsigned Param = PM.addLocalParam(Size);
470 const std::string &ParamName = PM.getParamName(Param);
471 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
472 MVT::Other);
473 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
474 ParamValue, OutVals[i]);
475 Ops[i+Ins.size()+4] = ParamValue;
476 }
477 }
478
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000479 std::vector<SDValue> InParams;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000480
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000481 // Generate list of .param variables to hold the return value(s).
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000482 Ops[1] = DAG.getTargetConstant(Ins.size(), MVT::i32);
Justin Holewinski75d80952011-09-23 16:48:41 +0000483 for (unsigned i = 0; i < Ins.size(); ++i) {
484 unsigned Size = Ins[i].VT.getStoreSizeInBits();
485 unsigned Param = PM.addLocalParam(Size);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000486 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000487 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
488 MVT::Other);
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000489 Ops[i+2] = ParamValue;
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000490 InParams.push_back(ParamValue);
Justin Holewinski75d80952011-09-23 16:48:41 +0000491 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000492
Justin Holewinski75d80952011-09-23 16:48:41 +0000493 Ops[0] = Chain;
494
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000495 // Create the CALL node.
Justin Holewinski75d80952011-09-23 16:48:41 +0000496 Chain = DAG.getNode(PTXISD::CALL, dl, MVT::Other, &Ops[0], Ops.size());
497
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000498 // Create the LOAD_PARAM nodes that retrieve the function return value(s).
Justin Holewinski75d80952011-09-23 16:48:41 +0000499 for (unsigned i = 0; i < Ins.size(); ++i) {
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000500 SDValue Load = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain,
501 InParams[i]);
Justin Holewinski75d80952011-09-23 16:48:41 +0000502 InVals.push_back(Load);
503 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000504
505 return Chain;
506}
Justin Holewinski68226a42011-10-09 15:42:02 +0000507
508unsigned PTXTargetLowering::getNumRegisters(LLVMContext &Context, EVT VT) {
509 // All arguments consist of one "register," regardless of the type.
510 return 1;
511}
512