blob: 7187518c52efa24bafc2ccb31d402e5b56b3baef [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"
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000019#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
Eric Christopher50880d02010-09-18 18:52:28 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000023#include "llvm/Support/raw_ostream.h"
Eric Christopher50880d02010-09-18 18:52:28 +000024
25using namespace llvm;
26
27PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
28 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
29 // Set up the register classes.
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000030 addRegisterClass(MVT::i1, PTX::PredsRegisterClass);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000031 addRegisterClass(MVT::i16, PTX::RRegu16RegisterClass);
32 addRegisterClass(MVT::i32, PTX::RRegu32RegisterClass);
33 addRegisterClass(MVT::i64, PTX::RRegu64RegisterClass);
Che-Liang Chiouf7172022011-02-28 06:34:09 +000034 addRegisterClass(MVT::f32, PTX::RRegf32RegisterClass);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000035 addRegisterClass(MVT::f64, PTX::RRegf64RegisterClass);
36
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000037 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
38
Che-Liang Chiouf7172022011-02-28 06:34:09 +000039 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
Che-Liang Chioufd8978b2011-03-02 03:20:28 +000040 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
41
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000042 // Customize translation of memory addresses
43 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Justin Holewinskid6625762011-03-23 16:58:51 +000044 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000045
Che-Liang Chiou88d33672011-03-18 11:08:52 +000046 // Expand BR_CC into BRCOND
47 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
48
Eric Christopher50880d02010-09-18 18:52:28 +000049 // Compute derived properties from the register classes
50 computeRegisterProperties();
51}
52
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000053SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
54 switch (Op.getOpcode()) {
Che-Liang Chiou88d33672011-03-18 11:08:52 +000055 default:
56 llvm_unreachable("Unimplemented operand");
57 case ISD::GlobalAddress:
58 return LowerGlobalAddress(Op, DAG);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000059 }
60}
61
Eric Christopher50880d02010-09-18 18:52:28 +000062const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
63 switch (Opcode) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000064 default:
65 llvm_unreachable("Unknown opcode");
Justin Holewinski8af78c92011-03-18 19:24:28 +000066 case PTXISD::COPY_ADDRESS:
67 return "PTXISD::COPY_ADDRESS";
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000068 case PTXISD::READ_PARAM:
69 return "PTXISD::READ_PARAM";
70 case PTXISD::EXIT:
71 return "PTXISD::EXIT";
72 case PTXISD::RET:
73 return "PTXISD::RET";
Eric Christopher50880d02010-09-18 18:52:28 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000078// Custom Lower Operation
79//===----------------------------------------------------------------------===//
80
81SDValue PTXTargetLowering::
82LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
83 EVT PtrVT = getPointerTy();
84 DebugLoc dl = Op.getDebugLoc();
85 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Justin Holewinski8af78c92011-03-18 19:24:28 +000086
Justin Holewinskid6625762011-03-23 16:58:51 +000087 assert(PtrVT.isSimple() && "Pointer must be to primitive type.");
88
Justin Holewinski8af78c92011-03-18 19:24:28 +000089 SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
90 SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS,
91 dl,
Justin Holewinskid6625762011-03-23 16:58:51 +000092 PtrVT.getSimpleVT(),
Justin Holewinski8af78c92011-03-18 19:24:28 +000093 targetGlobal);
94
95 return movInstr;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000096}
97
98//===----------------------------------------------------------------------===//
Eric Christopher50880d02010-09-18 18:52:28 +000099// Calling Convention Implementation
100//===----------------------------------------------------------------------===//
101
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000102namespace {
103struct argmap_entry {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000104 MVT::SimpleValueType VT;
105 TargetRegisterClass *RC;
106 TargetRegisterClass::iterator loc;
107
108 argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC)
109 : VT(_VT), RC(_RC), loc(_RC->begin()) {}
110
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000111 void reset() { loc = RC->begin(); }
112 bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000113} argmap[] = {
114 argmap_entry(MVT::i1, PTX::PredsRegisterClass),
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000115 argmap_entry(MVT::i16, PTX::RRegu16RegisterClass),
116 argmap_entry(MVT::i32, PTX::RRegu32RegisterClass),
117 argmap_entry(MVT::i64, PTX::RRegu64RegisterClass),
118 argmap_entry(MVT::f32, PTX::RRegf32RegisterClass),
119 argmap_entry(MVT::f64, PTX::RRegf64RegisterClass)
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000120};
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000121} // end anonymous namespace
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000122
Eric Christopher50880d02010-09-18 18:52:28 +0000123SDValue PTXTargetLowering::
124 LowerFormalArguments(SDValue Chain,
125 CallingConv::ID CallConv,
126 bool isVarArg,
127 const SmallVectorImpl<ISD::InputArg> &Ins,
128 DebugLoc dl,
129 SelectionDAG &DAG,
130 SmallVectorImpl<SDValue> &InVals) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000131 if (isVarArg) llvm_unreachable("PTX does not support varargs");
132
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000133 MachineFunction &MF = DAG.getMachineFunction();
134 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
135
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000136 switch (CallConv) {
137 default:
138 llvm_unreachable("Unsupported calling convention");
139 break;
140 case CallingConv::PTX_Kernel:
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000141 MFI->setKernel(true);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000142 break;
143 case CallingConv::PTX_Device:
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000144 MFI->setKernel(false);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000145 break;
146 }
147
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000148 // Make sure we don't add argument registers twice
149 if (MFI->isDoneAddArg())
150 llvm_unreachable("cannot add argument registers twice");
151
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000152 // Reset argmap before allocation
153 for (struct argmap_entry *i = argmap, *e = argmap + array_lengthof(argmap);
154 i != e; ++ i)
155 i->reset();
156
157 for (int i = 0, e = Ins.size(); i != e; ++ i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000158 MVT::SimpleValueType VT = Ins[i].VT.SimpleTy;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000159
160 struct argmap_entry *entry = std::find(argmap,
161 argmap + array_lengthof(argmap), VT);
162 if (entry == argmap + array_lengthof(argmap))
163 llvm_unreachable("Type of argument is not supported");
164
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000165 if (MFI->isKernel() && entry->RC == PTX::PredsRegisterClass)
166 llvm_unreachable("cannot pass preds to kernel");
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000167
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000168 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
169
170 unsigned preg = *++(entry->loc); // allocate start from register 1
171 unsigned vreg = RegInfo.createVirtualRegister(entry->RC);
172 RegInfo.addLiveIn(preg, vreg);
173
174 MFI->addArgReg(preg);
175
176 SDValue inval;
177 if (MFI->isKernel())
178 inval = DAG.getNode(PTXISD::READ_PARAM, dl, VT, Chain,
179 DAG.getTargetConstant(i, MVT::i32));
180 else
181 inval = DAG.getCopyFromReg(Chain, dl, vreg, VT);
182 InVals.push_back(inval);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000183 }
184
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000185 MFI->doneAddArg();
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000186
Eric Christopher50880d02010-09-18 18:52:28 +0000187 return Chain;
188}
189
190SDValue PTXTargetLowering::
191 LowerReturn(SDValue Chain,
192 CallingConv::ID CallConv,
193 bool isVarArg,
194 const SmallVectorImpl<ISD::OutputArg> &Outs,
195 const SmallVectorImpl<SDValue> &OutVals,
196 DebugLoc dl,
197 SelectionDAG &DAG) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000198 if (isVarArg) llvm_unreachable("PTX does not support varargs");
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000199
200 switch (CallConv) {
201 default:
202 llvm_unreachable("Unsupported calling convention.");
203 case CallingConv::PTX_Kernel:
204 assert(Outs.size() == 0 && "Kernel must return void.");
205 return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
206 case CallingConv::PTX_Device:
207 assert(Outs.size() <= 1 && "Can at most return one value.");
208 break;
209 }
210
211 // PTX_Device
212
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000213 // return void
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000214 if (Outs.size() == 0)
215 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
216
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000217 SDValue Flag;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000218 unsigned reg;
219
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000220 if (Outs[0].VT == MVT::i16) {
221 reg = PTX::RH0;
222 }
223 else if (Outs[0].VT == MVT::i32) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000224 reg = PTX::R0;
225 }
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000226 else if (Outs[0].VT == MVT::i64) {
227 reg = PTX::RD0;
228 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000229 else if (Outs[0].VT == MVT::f32) {
230 reg = PTX::F0;
231 }
232 else {
Duncan Sands75548de2011-03-15 08:41:24 +0000233 assert(Outs[0].VT == MVT::f64 && "Can return only basic types");
234 reg = PTX::FD0;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000235 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000236
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000237 MachineFunction &MF = DAG.getMachineFunction();
238 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
239 MFI->setRetReg(reg);
240
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000241 // If this is the first return lowered for this function, add the regs to the
242 // liveout set for the function
243 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
244 DAG.getMachineFunction().getRegInfo().addLiveOut(reg);
245
246 // Copy the result values into the output registers
247 Chain = DAG.getCopyToReg(Chain, dl, reg, OutVals[0], Flag);
248
249 // Guarantee that all emitted copies are stuck together,
250 // avoiding something bad
251 Flag = Chain.getValue(1);
252
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000253 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Eric Christopher50880d02010-09-18 18:52:28 +0000254}