blob: 6fcf710e3f1fe72e8e1b9697879d38ee0d06aa8d [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"
Eric Christopher50880d02010-09-18 18:52:28 +000019#include "llvm/Support/ErrorHandling.h"
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000020#include "llvm/CodeGen/CallingConvLower.h"
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000023#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000025#include "llvm/Support/raw_ostream.h"
Eric Christopher50880d02010-09-18 18:52:28 +000026
27using namespace llvm;
28
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000029//===----------------------------------------------------------------------===//
30// Calling Convention Implementation
31//===----------------------------------------------------------------------===//
32
33#include "PTXGenCallingConv.inc"
34
35//===----------------------------------------------------------------------===//
36// TargetLowering Implementation
37//===----------------------------------------------------------------------===//
38
Eric Christopher50880d02010-09-18 18:52:28 +000039PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
40 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
41 // Set up the register classes.
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000042 addRegisterClass(MVT::i1, PTX::RegPredRegisterClass);
43 addRegisterClass(MVT::i16, PTX::RegI16RegisterClass);
44 addRegisterClass(MVT::i32, PTX::RegI32RegisterClass);
45 addRegisterClass(MVT::i64, PTX::RegI64RegisterClass);
46 addRegisterClass(MVT::f32, PTX::RegF32RegisterClass);
47 addRegisterClass(MVT::f64, PTX::RegF64RegisterClass);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000048
Justin Holewinski4fea05a2011-04-28 00:19:52 +000049 setBooleanContents(ZeroOrOneBooleanContent);
Dan Bailey84149462011-06-25 18:16:28 +000050 setMinFunctionAlignment(2);
Dan Baileyb05a8a82011-06-24 19:27:10 +000051
Dan Bailey84149462011-06-25 18:16:28 +000052 ////////////////////////////////////
53 /////////// Expansion //////////////
54 ////////////////////////////////////
Dan Baileyb05a8a82011-06-24 19:27:10 +000055
Dan Bailey84149462011-06-25 18:16:28 +000056 // (any/zero/sign) extload => load + (any/zero/sign) extend
Dan Baileyb05a8a82011-06-24 19:27:10 +000057
Justin Holewinski4fea05a2011-04-28 00:19:52 +000058 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand);
59 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
Dan Baileyb05a8a82011-06-24 19:27:10 +000060 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Dan Bailey84149462011-06-25 18:16:28 +000061
62 // f32 extload => load + fextend
63
64 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
65
66 // f64 truncstore => trunc + store
67
68 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
69
70 // sign_extend_inreg => sign_extend
71
72 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
73
74 // br_cc => brcond
75
Che-Liang Chiou88d33672011-03-18 11:08:52 +000076 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
77
Dan Bailey84149462011-06-25 18:16:28 +000078 // select_cc => setcc
79
Justin Holewinski2d525c52011-04-28 00:19:56 +000080 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
81 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
82 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Dan Bailey84149462011-06-25 18:16:28 +000083
84 ////////////////////////////////////
85 //////////// Legal /////////////////
86 ////////////////////////////////////
87
88 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
89 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
90
91 ////////////////////////////////////
92 //////////// Custom ////////////////
93 ////////////////////////////////////
94
95 // customise setcc to use bitwise logic if possible
96
Justin Holewinski2d525c52011-04-28 00:19:56 +000097 setOperationAction(ISD::SETCC, MVT::i1, Custom);
Eli Friedmanfc5d3052011-05-06 20:34:06 +000098
Dan Bailey84149462011-06-25 18:16:28 +000099 // customize translation of memory addresses
100
101 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
102 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000103
Eric Christopher50880d02010-09-18 18:52:28 +0000104 // Compute derived properties from the register classes
105 computeRegisterProperties();
106}
107
Justin Holewinski2d525c52011-04-28 00:19:56 +0000108MVT::SimpleValueType PTXTargetLowering::getSetCCResultType(EVT VT) const {
109 return MVT::i1;
110}
111
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000112SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
113 switch (Op.getOpcode()) {
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000114 default:
115 llvm_unreachable("Unimplemented operand");
Justin Holewinski2d525c52011-04-28 00:19:56 +0000116 case ISD::SETCC:
117 return LowerSETCC(Op, DAG);
Che-Liang Chiou88d33672011-03-18 11:08:52 +0000118 case ISD::GlobalAddress:
119 return LowerGlobalAddress(Op, DAG);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000120 }
121}
122
Eric Christopher50880d02010-09-18 18:52:28 +0000123const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
124 switch (Opcode) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000125 default:
126 llvm_unreachable("Unknown opcode");
Justin Holewinski8af78c92011-03-18 19:24:28 +0000127 case PTXISD::COPY_ADDRESS:
128 return "PTXISD::COPY_ADDRESS";
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000129 case PTXISD::LOAD_PARAM:
130 return "PTXISD::LOAD_PARAM";
Justin Holewinski67a91842011-06-23 18:10:03 +0000131 case PTXISD::STORE_PARAM:
132 return "PTXISD::STORE_PARAM";
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000133 case PTXISD::EXIT:
134 return "PTXISD::EXIT";
135 case PTXISD::RET:
136 return "PTXISD::RET";
Eric Christopher50880d02010-09-18 18:52:28 +0000137 }
138}
139
140//===----------------------------------------------------------------------===//
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000141// Custom Lower Operation
142//===----------------------------------------------------------------------===//
143
Justin Holewinski2d525c52011-04-28 00:19:56 +0000144SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
145 assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer");
146 SDValue Op0 = Op.getOperand(0);
147 SDValue Op1 = Op.getOperand(1);
148 SDValue Op2 = Op.getOperand(2);
149 DebugLoc dl = Op.getDebugLoc();
150 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000151
Justin Holewinski2d525c52011-04-28 00:19:56 +0000152 // Look for X == 0, X == 1, X != 0, or X != 1
153 // We can simplify these to bitwise logic
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000154
Justin Holewinski2d525c52011-04-28 00:19:56 +0000155 if (Op1.getOpcode() == ISD::Constant &&
156 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
157 cast<ConstantSDNode>(Op1)->isNullValue()) &&
158 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
159
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000160 return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1);
Justin Holewinski2d525c52011-04-28 00:19:56 +0000161 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000162
Justin Holewinski2d525c52011-04-28 00:19:56 +0000163 return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2);
164}
165
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000166SDValue PTXTargetLowering::
167LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
168 EVT PtrVT = getPointerTy();
169 DebugLoc dl = Op.getDebugLoc();
170 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Justin Holewinski8af78c92011-03-18 19:24:28 +0000171
Justin Holewinskid6625762011-03-23 16:58:51 +0000172 assert(PtrVT.isSimple() && "Pointer must be to primitive type.");
173
Justin Holewinski8af78c92011-03-18 19:24:28 +0000174 SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
175 SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS,
176 dl,
Justin Holewinskid6625762011-03-23 16:58:51 +0000177 PtrVT.getSimpleVT(),
Justin Holewinski8af78c92011-03-18 19:24:28 +0000178 targetGlobal);
179
180 return movInstr;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000181}
182
183//===----------------------------------------------------------------------===//
Eric Christopher50880d02010-09-18 18:52:28 +0000184// Calling Convention Implementation
185//===----------------------------------------------------------------------===//
186
187SDValue PTXTargetLowering::
188 LowerFormalArguments(SDValue Chain,
189 CallingConv::ID CallConv,
190 bool isVarArg,
191 const SmallVectorImpl<ISD::InputArg> &Ins,
192 DebugLoc dl,
193 SelectionDAG &DAG,
194 SmallVectorImpl<SDValue> &InVals) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000195 if (isVarArg) llvm_unreachable("PTX does not support varargs");
196
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000197 MachineFunction &MF = DAG.getMachineFunction();
Justin Holewinski67a91842011-06-23 18:10:03 +0000198 const PTXSubtarget& ST = getTargetMachine().getSubtarget<PTXSubtarget>();
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000199 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
200
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000201 switch (CallConv) {
202 default:
203 llvm_unreachable("Unsupported calling convention");
204 break;
205 case CallingConv::PTX_Kernel:
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000206 MFI->setKernel(true);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000207 break;
208 case CallingConv::PTX_Device:
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000209 MFI->setKernel(false);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000210 break;
211 }
212
Justin Holewinski67a91842011-06-23 18:10:03 +0000213 // We do one of two things here:
214 // IsKernel || SM >= 2.0 -> Use param space for arguments
215 // SM < 2.0 -> Use registers for arguments
Justin Holewinski35f4fb32011-06-24 16:27:49 +0000216 if (MFI->isKernel() || ST.useParamSpaceForDeviceArgs()) {
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000217 // We just need to emit the proper LOAD_PARAM ISDs
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000218 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000219
Justin Holewinski67a91842011-06-23 18:10:03 +0000220 assert((!MFI->isKernel() || Ins[i].VT != MVT::i1) &&
221 "Kernels cannot take pred operands");
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000222
Justin Holewinskia5ccb4e2011-06-23 18:10:05 +0000223 SDValue ArgValue = DAG.getNode(PTXISD::LOAD_PARAM, dl, Ins[i].VT, Chain,
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000224 DAG.getTargetConstant(i, MVT::i32));
225 InVals.push_back(ArgValue);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000226
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000227 // Instead of storing a physical register in our argument list, we just
228 // store the total size of the parameter, in bits. The ASM printer
229 // knows how to process this.
230 MFI->addArgReg(Ins[i].VT.getStoreSizeInBits());
231 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000232 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000233 else {
234 // For device functions, we use the PTX calling convention to do register
235 // assignments then create CopyFromReg ISDs for the allocated registers
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000236
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000237 SmallVector<CCValAssign, 16> ArgLocs;
238 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), ArgLocs,
239 *DAG.getContext());
240
241 CCInfo.AnalyzeFormalArguments(Ins, CC_PTX);
242
243 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
244
245 CCValAssign& VA = ArgLocs[i];
246 EVT RegVT = VA.getLocVT();
247 TargetRegisterClass* TRC = 0;
248
249 assert(VA.isRegLoc() && "CCValAssign must be RegLoc");
250
251 // Determine which register class we need
252 if (RegVT == MVT::i1) {
253 TRC = PTX::RegPredRegisterClass;
254 }
255 else if (RegVT == MVT::i16) {
256 TRC = PTX::RegI16RegisterClass;
257 }
258 else if (RegVT == MVT::i32) {
259 TRC = PTX::RegI32RegisterClass;
260 }
261 else if (RegVT == MVT::i64) {
262 TRC = PTX::RegI64RegisterClass;
263 }
264 else if (RegVT == MVT::f32) {
265 TRC = PTX::RegF32RegisterClass;
266 }
267 else if (RegVT == MVT::f64) {
268 TRC = PTX::RegF64RegisterClass;
269 }
270 else {
271 llvm_unreachable("Unknown parameter type");
272 }
273
274 unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
275 MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg);
276
277 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
278 InVals.push_back(ArgValue);
279
280 MFI->addArgReg(VA.getLocReg());
281 }
282 }
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000283
Eric Christopher50880d02010-09-18 18:52:28 +0000284 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 {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000295 if (isVarArg) llvm_unreachable("PTX does not support varargs");
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000296
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:
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000304 //assert(Outs.size() <= 1 && "Can at most return one value.");
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000305 break;
306 }
307
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000308 MachineFunction& MF = DAG.getMachineFunction();
309 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000310
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000311 SDValue Flag;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000312
Justin Holewinskid8149c12011-06-23 18:10:13 +0000313 // Even though we could use the .param space for return arguments for
314 // device functions if SM >= 2.0 and the number of return arguments is
315 // only 1, we just always use registers since this makes the codegen
316 // easier.
317 SmallVector<CCValAssign, 16> RVLocs;
318 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
319 getTargetMachine(), RVLocs, *DAG.getContext());
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000320
Justin Holewinskid8149c12011-06-23 18:10:13 +0000321 CCInfo.AnalyzeReturn(Outs, RetCC_PTX);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000322
Justin Holewinskid8149c12011-06-23 18:10:13 +0000323 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
324 CCValAssign& VA = RVLocs[i];
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000325
Justin Holewinskid8149c12011-06-23 18:10:13 +0000326 assert(VA.isRegLoc() && "CCValAssign must be RegLoc");
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000327
Justin Holewinskid8149c12011-06-23 18:10:13 +0000328 unsigned Reg = VA.getLocReg();
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000329
Justin Holewinskid8149c12011-06-23 18:10:13 +0000330 DAG.getMachineFunction().getRegInfo().addLiveOut(Reg);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000331
Justin Holewinskid8149c12011-06-23 18:10:13 +0000332 Chain = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i], Flag);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000333
Justin Holewinskid8149c12011-06-23 18:10:13 +0000334 // Guarantee that all emitted copies are stuck together,
335 // avoiding something bad
336 Flag = Chain.getValue(1);
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000337
Justin Holewinskid8149c12011-06-23 18:10:13 +0000338 MFI->addRetReg(Reg);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000339 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000340
341 if (Flag.getNode() == 0) {
342 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000343 }
344 else {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000345 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000346 }
Eric Christopher50880d02010-09-18 18:52:28 +0000347}