blob: a012297e8352fac67c663c9bd20c84bfa3e0b339 [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");
214 break;
215 case CallingConv::PTX_Kernel:
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000216 MFI->setKernel(true);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000217 break;
218 case CallingConv::PTX_Device:
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000219 MFI->setKernel(false);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000220 break;
221 }
222
Justin Holewinski67a91842011-06-23 18:10:03 +0000223 // We do one of two things here:
224 // IsKernel || SM >= 2.0 -> Use param space for arguments
225 // SM < 2.0 -> Use registers for arguments
Justin Holewinski35f4fb32011-06-24 16:27:49 +0000226 if (MFI->isKernel() || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000227 // We just need to emit the proper LOAD_PARAM ISDs
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000228 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Justin Holewinski67a91842011-06-23 18:10:03 +0000229 assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) &&
230 "Kernels cannot take pred operands");
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000231
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000232 unsigned ParamSize = Ins[i].VT.getStoreSizeInBits();
233 unsigned Param = PM.addArgumentParam(ParamSize);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000234 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000235 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
236 MVT::Other);
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000237 SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain,
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000238 ParamValue);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000239 InVals.push_back(ArgValue);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000240 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000241 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000242 else {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000243 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000244 EVT RegVT = Ins[i].VT;
Justin Holewinski591c1c62011-10-05 18:32:25 +0000245 TargetRegisterClass* TRC = getRegClassFor(RegVT);
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000246 unsigned RegType;
247
248 // Determine which register class we need
249 if (RegVT == MVT::i1) {
250 RegType = PTXRegisterType::Pred;
251 }
252 else if (RegVT == MVT::i16) {
253 RegType = PTXRegisterType::B16;
254 }
255 else if (RegVT == MVT::i32) {
256 RegType = PTXRegisterType::B32;
257 }
258 else if (RegVT == MVT::i64) {
259 RegType = PTXRegisterType::B64;
260 }
261 else if (RegVT == MVT::f32) {
262 RegType = PTXRegisterType::F32;
263 }
264 else if (RegVT == MVT::f64) {
265 RegType = PTXRegisterType::F64;
266 }
267 else {
268 llvm_unreachable("Unknown parameter type");
269 }
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000270
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
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000278 InVals.push_back(ArgValue);
279
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000280 MFI->addRegister(Reg, RegType, PTXRegisterSpace::Argument);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000281 }
282 }
283
284 return Chain;
285}
286
287SDValue PTXTargetLowering::
288 LowerReturn(SDValue Chain,
289 CallingConv::ID CallConv,
290 bool isVarArg,
291 const SmallVectorImpl<ISD::OutputArg> &Outs,
292 const SmallVectorImpl<SDValue> &OutVals,
293 DebugLoc dl,
294 SelectionDAG &DAG) const {
295 if (isVarArg) llvm_unreachable("PTX does not support varargs");
296
297 switch (CallConv) {
298 default:
299 llvm_unreachable("Unsupported calling convention.");
300 case CallingConv::PTX_Kernel:
301 assert(Outs.size() == 0 && "Kernel must return void.");
302 return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
303 case CallingConv::PTX_Device:
304 assert(Outs.size() <= 1 && "Can at most return one value.");
305 break;
306 }
307
308 MachineFunction& MF = DAG.getMachineFunction();
309 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000310 PTXParamManager &PM = MFI->getParamManager();
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000311
312 SDValue Flag;
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000313 const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>();
314
315 if (ST.useParamSpaceForDeviceArgs()) {
316 assert(Outs.size() < 2 && "Device functions can return at most one value");
317
318 if (Outs.size() == 1) {
Justin Holewinski27f08fc2011-09-23 14:18:22 +0000319 unsigned ParamSize = OutVals[0].getValueType().getSizeInBits();
320 unsigned Param = PM.addReturnParam(ParamSize);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000321 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000322 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
323 MVT::Other);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000324 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000325 ParamValue, OutVals[0]);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000326 }
327 } else {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000328 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000329 EVT RegVT = Outs[i].VT;
330 TargetRegisterClass* TRC = 0;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000331 unsigned RegType;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000332
333 // Determine which register class we need
334 if (RegVT == MVT::i1) {
335 TRC = PTX::RegPredRegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000336 RegType = PTXRegisterType::Pred;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000337 }
338 else if (RegVT == MVT::i16) {
339 TRC = PTX::RegI16RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000340 RegType = PTXRegisterType::B16;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000341 }
342 else if (RegVT == MVT::i32) {
343 TRC = PTX::RegI32RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000344 RegType = PTXRegisterType::B32;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000345 }
346 else if (RegVT == MVT::i64) {
347 TRC = PTX::RegI64RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000348 RegType = PTXRegisterType::B64;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000349 }
350 else if (RegVT == MVT::f32) {
351 TRC = PTX::RegF32RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000352 RegType = PTXRegisterType::F32;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000353 }
354 else if (RegVT == MVT::f64) {
355 TRC = PTX::RegF64RegisterClass;
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000356 RegType = PTXRegisterType::F64;
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000357 }
358 else {
359 llvm_unreachable("Unknown parameter type");
360 }
361
362 unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000363
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000364 SDValue Copy = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i]/*, Flag*/);
365 SDValue OutReg = DAG.getRegister(Reg, RegVT);
366
367 Chain = DAG.getNode(PTXISD::WRITE_PARAM, dl, MVT::Other, Copy, OutReg);
Justin Holewinski5422a0f2011-09-22 16:45:46 +0000368
Justin Holewinski4c7ffb62011-12-06 17:39:48 +0000369 MFI->addRegister(Reg, RegType, PTXRegisterSpace::Return);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000370 }
371 }
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000372
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000373 if (Flag.getNode() == 0) {
374 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000375 }
376 else {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000377 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000378 }
Eric Christopher50880d02010-09-18 18:52:28 +0000379}
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000380
381SDValue
382PTXTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
383 CallingConv::ID CallConv, bool isVarArg,
384 bool &isTailCall,
385 const SmallVectorImpl<ISD::OutputArg> &Outs,
386 const SmallVectorImpl<SDValue> &OutVals,
387 const SmallVectorImpl<ISD::InputArg> &Ins,
388 DebugLoc dl, SelectionDAG &DAG,
389 SmallVectorImpl<SDValue> &InVals) const {
390
391 MachineFunction& MF = DAG.getMachineFunction();
Dan Bailey96e64582011-11-11 14:45:12 +0000392 PTXMachineFunctionInfo *PTXMFI = MF.getInfo<PTXMachineFunctionInfo>();
393 PTXParamManager &PM = PTXMFI->getParamManager();
394 MachineFrameInfo *MFI = MF.getFrameInfo();
395
Duncan Sands1f6a3292011-08-12 14:54:45 +0000396 assert(getTargetMachine().getSubtarget<PTXSubtarget>().callsAreHandled() &&
397 "Calls are not handled for the target device");
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000398
Dan Bailey96e64582011-11-11 14:45:12 +0000399 // Identify the callee function
400 const GlobalValue *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
401 const Function *function = cast<Function>(GV);
402
403 // allow non-device calls only for printf
404 bool isPrintf = function->getName() == "printf" || function->getName() == "puts";
405
406 assert((isPrintf || function->getCallingConv() == CallingConv::PTX_Device) &&
407 "PTX function calls must be to PTX device functions");
408
409 unsigned outSize = isPrintf ? 2 : Outs.size();
410
Justin Holewinski75d80952011-09-23 16:48:41 +0000411 std::vector<SDValue> Ops;
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000412 // The layout of the ops will be [Chain, #Ins, Ins, Callee, #Outs, Outs]
Dan Bailey96e64582011-11-11 14:45:12 +0000413 Ops.resize(outSize + Ins.size() + 4);
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000414
Justin Holewinski75d80952011-09-23 16:48:41 +0000415 Ops[0] = Chain;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000416
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000417 // Identify the callee function
Benjamin Kramerca921592011-09-28 04:32:36 +0000418 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000419 Ops[Ins.size()+2] = Callee;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000420
Dan Bailey96e64582011-11-11 14:45:12 +0000421 // #Outs
422 Ops[Ins.size()+3] = DAG.getTargetConstant(outSize, MVT::i32);
423
424 if (isPrintf) {
425 // first argument is the address of the global string variable in memory
426 unsigned Param0 = PM.addLocalParam(getPointerTy().getSizeInBits());
427 SDValue ParamValue0 = DAG.getTargetExternalSymbol(PM.getParamName(Param0).c_str(),
428 MVT::Other);
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000429 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
Dan Bailey96e64582011-11-11 14:45:12 +0000430 ParamValue0, OutVals[0]);
431 Ops[Ins.size()+4] = ParamValue0;
432
433 // alignment is the maximum size of all the arguments
434 unsigned alignment = 0;
435 for (unsigned i = 1; i < OutVals.size(); ++i) {
436 alignment = std::max(alignment,
437 OutVals[i].getValueType().getSizeInBits());
438 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000439
Dan Bailey96e64582011-11-11 14:45:12 +0000440 // size is the alignment multiplied by the number of arguments
441 unsigned size = alignment * (OutVals.size() - 1);
442
443 // second argument is the address of the stack object (unless no arguments)
444 unsigned Param1 = PM.addLocalParam(getPointerTy().getSizeInBits());
445 SDValue ParamValue1 = DAG.getTargetExternalSymbol(PM.getParamName(Param1).c_str(),
446 MVT::Other);
447 Ops[Ins.size()+5] = ParamValue1;
448
449 if (size > 0)
450 {
451 // create a local stack object to store the arguments
452 unsigned StackObject = MFI->CreateStackObject(size / 8, alignment / 8, false);
453 SDValue FrameIndex = DAG.getFrameIndex(StackObject, getPointerTy());
454
455 // store each of the arguments to the stack in turn
456 for (unsigned int i = 1; i != OutVals.size(); i++) {
457 SDValue FrameAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FrameIndex, DAG.getTargetConstant((i - 1) * 8, getPointerTy()));
458 Chain = DAG.getStore(Chain, dl, OutVals[i], FrameAddr,
459 MachinePointerInfo(),
460 false, false, 0);
461 }
462
463 // copy the address of the local frame index to get the address in non-local space
464 SDValue genericAddr = DAG.getNode(PTXISD::COPY_ADDRESS, dl, getPointerTy(), FrameIndex);
465
466 // store this address in the second argument
467 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain, ParamValue1, genericAddr);
468 }
469 }
470 else
471 {
472 // Generate STORE_PARAM nodes for each function argument. In PTX, function
473 // arguments are explicitly stored into .param variables and passed as
474 // arguments. There is no register/stack-based calling convention in PTX.
475 for (unsigned i = 0; i != OutVals.size(); ++i) {
476 unsigned Size = OutVals[i].getValueType().getSizeInBits();
477 unsigned Param = PM.addLocalParam(Size);
478 const std::string &ParamName = PM.getParamName(Param);
479 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
480 MVT::Other);
481 Chain = DAG.getNode(PTXISD::STORE_PARAM, dl, MVT::Other, Chain,
482 ParamValue, OutVals[i]);
483 Ops[i+Ins.size()+4] = ParamValue;
484 }
485 }
486
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000487 std::vector<SDValue> InParams;
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000488
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000489 // Generate list of .param variables to hold the return value(s).
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000490 Ops[1] = DAG.getTargetConstant(Ins.size(), MVT::i32);
Justin Holewinski75d80952011-09-23 16:48:41 +0000491 for (unsigned i = 0; i < Ins.size(); ++i) {
492 unsigned Size = Ins[i].VT.getStoreSizeInBits();
493 unsigned Param = PM.addLocalParam(Size);
Benjamin Kramer8adae0c2011-09-28 04:08:02 +0000494 const std::string &ParamName = PM.getParamName(Param);
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000495 SDValue ParamValue = DAG.getTargetExternalSymbol(ParamName.c_str(),
496 MVT::Other);
Justin Holewinskid8e4ed22011-09-28 14:32:04 +0000497 Ops[i+2] = ParamValue;
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000498 InParams.push_back(ParamValue);
Justin Holewinski75d80952011-09-23 16:48:41 +0000499 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000500
Justin Holewinski75d80952011-09-23 16:48:41 +0000501 Ops[0] = Chain;
502
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000503 // Create the CALL node.
Justin Holewinski75d80952011-09-23 16:48:41 +0000504 Chain = DAG.getNode(PTXISD::CALL, dl, MVT::Other, &Ops[0], Ops.size());
505
Justin Holewinskidc0baf92011-09-23 17:15:53 +0000506 // Create the LOAD_PARAM nodes that retrieve the function return value(s).
Justin Holewinski75d80952011-09-23 16:48:41 +0000507 for (unsigned i = 0; i < Ins.size(); ++i) {
Justin Holewinskif47dfba2011-09-27 18:12:55 +0000508 SDValue Load = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain,
509 InParams[i]);
Justin Holewinski75d80952011-09-23 16:48:41 +0000510 InVals.push_back(Load);
511 }
Justin Holewinski4bdd4ed2011-08-09 17:36:31 +0000512
513 return Chain;
514}
Justin Holewinski68226a42011-10-09 15:42:02 +0000515
516unsigned PTXTargetLowering::getNumRegisters(LLVMContext &Context, EVT VT) {
517 // All arguments consist of one "register," regardless of the type.
518 return 1;
519}
520