blob: d5ce5a8e3f836a46cbb2dac8658c8c8a1cca486f [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) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000243 EVT RegVT = Ins[i].VT;
Justin Holewinski591c1c62011-10-05 18:32:25 +0000244 TargetRegisterClass* TRC = getRegClassFor(RegVT);
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000245 unsigned RegType;
246
247 // Determine which register class we need
248 if (RegVT == MVT::i1) {
249 RegType = PTXRegisterType::Pred;
250 }
251 else if (RegVT == MVT::i16) {
252 RegType = PTXRegisterType::B16;
253 }
254 else if (RegVT == MVT::i32) {
255 RegType = PTXRegisterType::B32;
256 }
257 else if (RegVT == MVT::i64) {
258 RegType = PTXRegisterType::B64;
259 }
260 else if (RegVT == MVT::f32) {
261 RegType = PTXRegisterType::F32;
262 }
263 else if (RegVT == MVT::f64) {
264 RegType = PTXRegisterType::F64;
265 }
266 else {
267 llvm_unreachable("Unknown parameter type");
268 }
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000269
270 // Use a unique index in the instruction to prevent instruction folding.
271 // Yes, this is a hack.
272 SDValue Index = DAG.getTargetConstant(i, MVT::i32);
273 unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
274 SDValue ArgValue = DAG.getNode(PTXISD::READ_PARAM, dl, RegVT, Chain,
275 Index);
276
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000277 InVals.push_back(ArgValue);
278
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000279 MFI->addRegister(Reg, RegType, PTXRegisterSpace::Argument);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000280 }
281 }
282
283 return Chain;
284}
285
286SDValue PTXTargetLowering::
287 LowerReturn(SDValue Chain,
288 CallingConv::ID CallConv,
289 bool isVarArg,
290 const SmallVectorImpl<ISD::OutputArg> &Outs,
291 const SmallVectorImpl<SDValue> &OutVals,
292 DebugLoc dl,
293 SelectionDAG &DAG) const {
294 if (isVarArg) llvm_unreachable("PTX does not support varargs");
295
296 switch (CallConv) {
297 default:
298 llvm_unreachable("Unsupported calling convention.");
299 case CallingConv::PTX_Kernel:
300 assert(Outs.size() == 0 && "Kernel must return void.");
301 return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
302 case CallingConv::PTX_Device:
303 assert(Outs.size() <= 1 && "Can at most return one value.");
304 break;
305 }
306
307 MachineFunction& MF = DAG.getMachineFunction();
308 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000309 PTXParamManager &PM = MFI->getParamManager();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000310
311 SDValue Flag;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000312 const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>();
313
314 if (ST.useParamSpaceForDeviceArgs()) {
315 assert(Outs.size() < 2 && "Device functions can return at most one value");
316
317 if (Outs.size() == 1) {
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000318 unsigned ParamSize = OutVals[0].getValueType().getSizeInBits();
319 unsigned Param = PM.addReturnParam(ParamSize);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000320 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000321 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
322 MVT::Other);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000323 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000324 ParamValue, OutVals[0]);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000325 }
326 } else {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000327 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000328 EVT RegVT = Outs[i].VT;
329 TargetRegisterClass* TRC = 0;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000330 unsigned RegType;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000331
332 // Determine which register class we need
333 if (RegVT == MVT::i1) {
334 TRC = PTX::RegPredRegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000335 RegType = PTXRegisterType::Pred;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000336 }
337 else if (RegVT == MVT::i16) {
338 TRC = PTX::RegI16RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000339 RegType = PTXRegisterType::B16;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000340 }
341 else if (RegVT == MVT::i32) {
342 TRC = PTX::RegI32RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000343 RegType = PTXRegisterType::B32;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000344 }
345 else if (RegVT == MVT::i64) {
346 TRC = PTX::RegI64RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000347 RegType = PTXRegisterType::B64;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000348 }
349 else if (RegVT == MVT::f32) {
350 TRC = PTX::RegF32RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000351 RegType = PTXRegisterType::F32;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000352 }
353 else if (RegVT == MVT::f64) {
354 TRC = PTX::RegF64RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000355 RegType = PTXRegisterType::F64;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000356 }
357 else {
358 llvm_unreachable("Unknown parameter type");
359 }
360
361 unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000362
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000363 SDValue Copy = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i]/*, Flag*/);
364 SDValue OutReg = DAG.getRegister(Reg, RegVT);
365
366 Chain = DAG.getNode(PTXISD::WRITE_PARAM, dl, MVT::Other, Copy, OutReg);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000367
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000368 MFI->addRegister(Reg, RegType, PTXRegisterSpace::Return);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000369 }
370 }
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000371
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000372 if (Flag.getNode() == 0) {
373 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000374 }
375 else {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000376 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000377 }
Eric Christopher50880d02010-09-18 18:52:28 +0000378}
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000379
380SDValue
381PTXTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
382 CallingConv::ID CallConv, bool isVarArg,
383 bool &isTailCall,
384 const SmallVectorImpl<ISD::OutputArg> &Outs,
385 const SmallVectorImpl<SDValue> &OutVals,
386 const SmallVectorImpl<ISD::InputArg> &Ins,
387 DebugLoc dl, SelectionDAG &DAG,
388 SmallVectorImpl<SDValue> &InVals) const {
389
390 MachineFunction& MF = DAG.getMachineFunction();
Dan Bailey96e64582011-11-11 14:45:12 +0000391 PTXMachineFunctionInfo *PTXMFI = MF.getInfo<PTXMachineFunctionInfo>();
392 PTXParamManager &PM = PTXMFI->getParamManager();
393 MachineFrameInfo *MFI = MF.getFrameInfo();
394
Duncan Sands1f6a3292011-08-12 14:54:45 +0000395 assert(getTargetMachine().getSubtarget<PTXSubtarget>().callsAreHandled() &&
396 "Calls are not handled for the target device");
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000397
Dan Bailey96e64582011-11-11 14:45:12 +0000398 // Identify the callee function
399 const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
400 const Function *function = cast<Function>(GV);
401
402 // allow non-device calls only for printf
403 bool isPrintf = function->getName() == "printf" || function->getName() == "puts";
404
405 assert((isPrintf || function->getCallingConv() == CallingConv::PTX_Device) &&
406 "PTX function calls must be to PTX device functions");
407
408 unsigned outSize = isPrintf ? 2 : Outs.size();
409
Justin Holewinski75d80952011-09-23 16:48:41 +0000410 std::vector<SDValue> Ops;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000411 // The layout of the ops will be [Chain, #Ins, Ins, Callee, #Outs, Outs]
Dan Bailey96e64582011-11-11 14:45:12 +0000412 Ops.resize(outSize + Ins.size() + 4);
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000413
Justin Holewinski75d80952011-09-23 16:48:41 +0000414 Ops[0] = Chain;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000415
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000416 // Identify the callee function
Benjamin Kramerca921592011-09-28 04:32:36 +0000417 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000418 Ops[Ins.size()+2] = Callee;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000419
Dan Bailey96e64582011-11-11 14:45:12 +0000420 // #Outs
421 Ops[Ins.size()+3] = DAG.getTargetConstant(outSize, MVT::i32);
422
423 if (isPrintf) {
424 // first argument is the address of the global string variable in memory
425 unsigned Param0 = PM.addLocalParam(getPointerTy().getSizeInBits());
426 SDValue ParamValue0 = DAG.getTargetExternalSymbol(PM.getParamName(Param0).c_str(),
427 MVT::Other);
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000428 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
Dan Bailey96e64582011-11-11 14:45:12 +0000429 ParamValue0, OutVals[0]);
430 Ops[Ins.size()+4] = ParamValue0;
431
432 // alignment is the maximum size of all the arguments
433 unsigned alignment = 0;
434 for (unsigned i = 1; i < OutVals.size(); ++i) {
435 alignment = std::max(alignment,
436 OutVals[i].getValueType().getSizeInBits());
437 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000438
Dan Bailey96e64582011-11-11 14:45:12 +0000439 // size is the alignment multiplied by the number of arguments
440 unsigned size = alignment * (OutVals.size() - 1);
441
442 // second argument is the address of the stack object (unless no arguments)
443 unsigned Param1 = PM.addLocalParam(getPointerTy().getSizeInBits());
444 SDValue ParamValue1 = DAG.getTargetExternalSymbol(PM.getParamName(Param1).c_str(),
445 MVT::Other);
446 Ops[Ins.size()+5] = ParamValue1;
447
448 if (size > 0)
449 {
450 // create a local stack object to store the arguments
451 unsigned StackObject = MFI->CreateStackObject(size / 8, alignment / 8, false);
452 SDValue FrameIndex = DAG.getFrameIndex(StackObject, getPointerTy());
453
454 // store each of the arguments to the stack in turn
455 for (unsigned int i = 1; i != OutVals.size(); i++) {
456 SDValue FrameAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FrameIndex, DAG.getTargetConstant((i - 1) * 8, getPointerTy()));
457 Chain = DAG.getStore(Chain, dl, OutVals[i], FrameAddr,
458 MachinePointerInfo(),
459 false, false, 0);
460 }
461
462 // copy the address of the local frame index to get the address in non-local space
463 SDValue genericAddr = DAG.getNode(PTXISD::COPY_ADDRESS, dl, getPointerTy(), FrameIndex);
464
465 // store this address in the second argument
466 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain, ParamValue1, genericAddr);
467 }
468 }
469 else
470 {
471 // Generate STORE_PARAM nodes for each function argument. In PTX, function
472 // arguments are explicitly stored into .param variables and passed as
473 // arguments. There is no register/stack-based calling convention in PTX.
474 for (unsigned i = 0; i != OutVals.size(); ++i) {
475 unsigned Size = OutVals[i].getValueType().getSizeInBits();
476 unsigned Param = PM.addLocalParam(Size);
477 const std::string &ParamName = PM.getParamName(Param);
478 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
479 MVT::Other);
480 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
481 ParamValue, OutVals[i]);
482 Ops[i+Ins.size()+4] = ParamValue;
483 }
484 }
485
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000486 std::vector<SDValue> InParams;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000487
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000488 // Generate list of .param variables to hold the return value(s).
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000489 Ops[1] = DAG.getTargetConstant(Ins.size(), MVT::i32);
Justin Holewinski75d80952011-09-23 16:48:41 +0000490 for (unsigned i = 0; i < Ins.size(); ++i) {
491 unsigned Size = Ins[i].VT.getStoreSizeInBits();
492 unsigned Param = PM.addLocalParam(Size);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000493 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000494 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
495 MVT::Other);
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000496 Ops[i+2] = ParamValue;
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000497 InParams.push_back(ParamValue);
Justin Holewinski75d80952011-09-23 16:48:41 +0000498 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000499
Justin Holewinski75d80952011-09-23 16:48:41 +0000500 Ops[0] = Chain;
501
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000502 // Create the CALL node.
Justin Holewinski75d80952011-09-23 16:48:41 +0000503 Chain = DAG.getNode(PTXISD::CALL, dl, MVT::Other, &Ops[0], Ops.size());
504
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000505 // Create the LOAD_PARAM nodes that retrieve the function return value(s).
Justin Holewinski75d80952011-09-23 16:48:41 +0000506 for (unsigned i = 0; i < Ins.size(); ++i) {
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000507 SDValue Load = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain,
508 InParams[i]);
Justin Holewinski75d80952011-09-23 16:48:41 +0000509 InVals.push_back(Load);
510 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000511
512 return Chain;
513}
Justin Holewinski68226a42011-10-09 15:42:02 +0000514
515unsigned PTXTargetLowering::getNumRegisters(LLVMContext &Context, EVT VT) {
516 // All arguments consist of one "register," regardless of the type.
517 return 1;
518}
519