blob: c3cdabad51dc4a4f05937896293e13c8118dd45b [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"
18#include "llvm/Support/ErrorHandling.h"
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000019#include "llvm/CodeGen/CallingConvLower.h"
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000024#include "llvm/Support/raw_ostream.h"
Eric Christopher50880d02010-09-18 18:52:28 +000025
26using namespace llvm;
27
Justin Holewinskie0aef2d2011-06-16 17:50:00 +000028//===----------------------------------------------------------------------===//
29// Calling Convention Implementation
30//===----------------------------------------------------------------------===//
31
32#include "PTXGenCallingConv.inc"
33
34//===----------------------------------------------------------------------===//
35// TargetLowering Implementation
36//===----------------------------------------------------------------------===//
37
Eric Christopher50880d02010-09-18 18:52:28 +000038PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
39 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
40 // Set up the register classes.
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000041 addRegisterClass(MVT::i1, PTX::RegPredRegisterClass);
42 addRegisterClass(MVT::i16, PTX::RegI16RegisterClass);
43 addRegisterClass(MVT::i32, PTX::RegI32RegisterClass);
44 addRegisterClass(MVT::i64, PTX::RegI64RegisterClass);
45 addRegisterClass(MVT::f32, PTX::RegF32RegisterClass);
46 addRegisterClass(MVT::f64, PTX::RegF64RegisterClass);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000047
Justin Holewinski4fea05a2011-04-28 00:19:52 +000048 setBooleanContents(ZeroOrOneBooleanContent);
Justin Holewinskiec3141b2011-06-16 15:17:11 +000049
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000050 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
51
Che-Liang Chiouf7172022011-02-28 06:34:09 +000052 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000053 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
Justin Holewinskiec3141b2011-06-16 15:17:11 +000054
Justin Holewinski4fea05a2011-04-28 00:19:52 +000055 // Turn i16 (z)extload into load + (z)extend
56 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand);
57 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000058
Justin Holewinski4fea05a2011-04-28 00:19:52 +000059 // Turn f32 extload into load + fextend
60 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Justin Holewinskiec3141b2011-06-16 15:17:11 +000061
Justin Holewinski4fea05a2011-04-28 00:19:52 +000062 // Turn f64 truncstore into trunc + store.
63 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Justin Holewinskiec3141b2011-06-16 15:17:11 +000064
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000065 // Customize translation of memory addresses
66 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Justin Holewinskid6625762011-03-23 16:58:51 +000067 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000068
Che-Liang Chiou88d33672011-03-18 11:08:52 +000069 // Expand BR_CC into BRCOND
70 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
71
Justin Holewinski2d525c52011-04-28 00:19:56 +000072 // Expand SELECT_CC into SETCC
73 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
74 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
75 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Justin Holewinskiec3141b2011-06-16 15:17:11 +000076
Justin Holewinski1b91bcd2011-06-16 17:49:58 +000077 // need to lower SETCC of RegPred into bitwise logic
Justin Holewinski2d525c52011-04-28 00:19:56 +000078 setOperationAction(ISD::SETCC, MVT::i1, Custom);
Eli Friedmanfc5d3052011-05-06 20:34:06 +000079
80 setMinFunctionAlignment(2);
81
Eric Christopher50880d02010-09-18 18:52:28 +000082 // Compute derived properties from the register classes
83 computeRegisterProperties();
84}
85
Justin Holewinski2d525c52011-04-28 00:19:56 +000086MVT::SimpleValueType PTXTargetLowering::getSetCCResultType(EVT VT) const {
87 return MVT::i1;
88}
89
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000090SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
91 switch (Op.getOpcode()) {
Che-Liang Chiou88d33672011-03-18 11:08:52 +000092 default:
93 llvm_unreachable("Unimplemented operand");
Justin Holewinski2d525c52011-04-28 00:19:56 +000094 case ISD::SETCC:
95 return LowerSETCC(Op, DAG);
Che-Liang Chiou88d33672011-03-18 11:08:52 +000096 case ISD::GlobalAddress:
97 return LowerGlobalAddress(Op, DAG);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000098 }
99}
100
Eric Christopher50880d02010-09-18 18:52:28 +0000101const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
102 switch (Opcode) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000103 default:
104 llvm_unreachable("Unknown opcode");
Justin Holewinski8af78c92011-03-18 19:24:28 +0000105 case PTXISD::COPY_ADDRESS:
106 return "PTXISD::COPY_ADDRESS";
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000107 case PTXISD::READ_PARAM:
108 return "PTXISD::READ_PARAM";
109 case PTXISD::EXIT:
110 return "PTXISD::EXIT";
111 case PTXISD::RET:
112 return "PTXISD::RET";
Eric Christopher50880d02010-09-18 18:52:28 +0000113 }
114}
115
116//===----------------------------------------------------------------------===//
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000117// Custom Lower Operation
118//===----------------------------------------------------------------------===//
119
Justin Holewinski2d525c52011-04-28 00:19:56 +0000120SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
121 assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer");
122 SDValue Op0 = Op.getOperand(0);
123 SDValue Op1 = Op.getOperand(1);
124 SDValue Op2 = Op.getOperand(2);
125 DebugLoc dl = Op.getDebugLoc();
126 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000127
Justin Holewinski2d525c52011-04-28 00:19:56 +0000128 // Look for X == 0, X == 1, X != 0, or X != 1
129 // We can simplify these to bitwise logic
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000130
Justin Holewinski2d525c52011-04-28 00:19:56 +0000131 if (Op1.getOpcode() == ISD::Constant &&
132 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
133 cast<ConstantSDNode>(Op1)->isNullValue()) &&
134 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
135
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000136 return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1);
Justin Holewinski2d525c52011-04-28 00:19:56 +0000137 }
Justin Holewinskiec3141b2011-06-16 15:17:11 +0000138
Justin Holewinski2d525c52011-04-28 00:19:56 +0000139 return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2);
140}
141
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000142SDValue PTXTargetLowering::
143LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
144 EVT PtrVT = getPointerTy();
145 DebugLoc dl = Op.getDebugLoc();
146 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Justin Holewinski8af78c92011-03-18 19:24:28 +0000147
Justin Holewinskid6625762011-03-23 16:58:51 +0000148 assert(PtrVT.isSimple() && "Pointer must be to primitive type.");
149
Justin Holewinski8af78c92011-03-18 19:24:28 +0000150 SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
151 SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS,
152 dl,
Justin Holewinskid6625762011-03-23 16:58:51 +0000153 PtrVT.getSimpleVT(),
Justin Holewinski8af78c92011-03-18 19:24:28 +0000154 targetGlobal);
155
156 return movInstr;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +0000157}
158
159//===----------------------------------------------------------------------===//
Eric Christopher50880d02010-09-18 18:52:28 +0000160// Calling Convention Implementation
161//===----------------------------------------------------------------------===//
162
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000163namespace {
164struct argmap_entry {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000165 MVT::SimpleValueType VT;
166 TargetRegisterClass *RC;
167 TargetRegisterClass::iterator loc;
168
169 argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC)
170 : VT(_VT), RC(_RC), loc(_RC->begin()) {}
171
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000172 void reset() { loc = RC->begin(); }
173 bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000174} argmap[] = {
Justin Holewinski1b91bcd2011-06-16 17:49:58 +0000175 argmap_entry(MVT::i1, PTX::RegPredRegisterClass),
176 argmap_entry(MVT::i16, PTX::RegI16RegisterClass),
177 argmap_entry(MVT::i32, PTX::RegI32RegisterClass),
178 argmap_entry(MVT::i64, PTX::RegI64RegisterClass),
179 argmap_entry(MVT::f32, PTX::RegF32RegisterClass),
180 argmap_entry(MVT::f64, PTX::RegF64RegisterClass)
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000181};
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000182} // end anonymous namespace
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000183
Eric Christopher50880d02010-09-18 18:52:28 +0000184SDValue PTXTargetLowering::
185 LowerFormalArguments(SDValue Chain,
186 CallingConv::ID CallConv,
187 bool isVarArg,
188 const SmallVectorImpl<ISD::InputArg> &Ins,
189 DebugLoc dl,
190 SelectionDAG &DAG,
191 SmallVectorImpl<SDValue> &InVals) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000192 if (isVarArg) llvm_unreachable("PTX does not support varargs");
193
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000194 MachineFunction &MF = DAG.getMachineFunction();
195 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
196
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000197 switch (CallConv) {
198 default:
199 llvm_unreachable("Unsupported calling convention");
200 break;
201 case CallingConv::PTX_Kernel:
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000202 MFI->setKernel(true);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000203 break;
204 case CallingConv::PTX_Device:
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000205 MFI->setKernel(false);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000206 break;
207 }
208
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000209 if (MFI->isKernel()) {
210 // For kernel functions, we just need to emit the proper READ_PARAM ISDs
211 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000212
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000213 assert(Ins[i].VT != MVT::i1 && "Kernels cannot take pred operands");
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000214
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000215 SDValue ArgValue = DAG.getNode(PTXISD::READ_PARAM, dl, Ins[i].VT, Chain,
216 DAG.getTargetConstant(i, MVT::i32));
217 InVals.push_back(ArgValue);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000218
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000219 // Instead of storing a physical register in our argument list, we just
220 // store the total size of the parameter, in bits. The ASM printer
221 // knows how to process this.
222 MFI->addArgReg(Ins[i].VT.getStoreSizeInBits());
223 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000224 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000225 else {
226 // For device functions, we use the PTX calling convention to do register
227 // assignments then create CopyFromReg ISDs for the allocated registers
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000228
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000229 SmallVector<CCValAssign, 16> ArgLocs;
230 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), ArgLocs,
231 *DAG.getContext());
232
233 CCInfo.AnalyzeFormalArguments(Ins, CC_PTX);
234
235 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
236
237 CCValAssign& VA = ArgLocs[i];
238 EVT RegVT = VA.getLocVT();
239 TargetRegisterClass* TRC = 0;
240
241 assert(VA.isRegLoc() && "CCValAssign must be RegLoc");
242
243 // Determine which register class we need
244 if (RegVT == MVT::i1) {
245 TRC = PTX::RegPredRegisterClass;
246 }
247 else if (RegVT == MVT::i16) {
248 TRC = PTX::RegI16RegisterClass;
249 }
250 else if (RegVT == MVT::i32) {
251 TRC = PTX::RegI32RegisterClass;
252 }
253 else if (RegVT == MVT::i64) {
254 TRC = PTX::RegI64RegisterClass;
255 }
256 else if (RegVT == MVT::f32) {
257 TRC = PTX::RegF32RegisterClass;
258 }
259 else if (RegVT == MVT::f64) {
260 TRC = PTX::RegF64RegisterClass;
261 }
262 else {
263 llvm_unreachable("Unknown parameter type");
264 }
265
266 unsigned Reg = MF.getRegInfo().createVirtualRegister(TRC);
267 MF.getRegInfo().addLiveIn(VA.getLocReg(), Reg);
268
269 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
270 InVals.push_back(ArgValue);
271
272 MFI->addArgReg(VA.getLocReg());
273 }
274 }
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000275
Eric Christopher50880d02010-09-18 18:52:28 +0000276 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 {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000287 if (isVarArg) llvm_unreachable("PTX does not support varargs");
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000288
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:
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000296 //assert(Outs.size() <= 1 && "Can at most return one value.");
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000297 break;
298 }
299
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000300 MachineFunction& MF = DAG.getMachineFunction();
301 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
302 SmallVector<CCValAssign, 16> RVLocs;
303 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
304 getTargetMachine(), RVLocs, *DAG.getContext());
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000305
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000306 SDValue Flag;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000307
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000308 CCInfo.AnalyzeReturn(Outs, RetCC_PTX);
309
310 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
311
312 CCValAssign& VA = RVLocs[i];
313
314 assert(VA.isRegLoc() && "CCValAssign must be RegLoc");
315
316 unsigned Reg = VA.getLocReg();
317
318 DAG.getMachineFunction().getRegInfo().addLiveOut(Reg);
319
320 Chain = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i], Flag);
321
322 // Guarantee that all emitted copies are stuck together,
323 // avoiding something bad
324 Flag = Chain.getValue(1);
325
326 MFI->addRetReg(Reg);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000327 }
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000328
329 if (Flag.getNode() == 0) {
330 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000331 }
332 else {
Justin Holewinskie0aef2d2011-06-16 17:50:00 +0000333 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000334 }
Eric Christopher50880d02010-09-18 18:52:28 +0000335}