blob: a58cb80b287463723219672855e78c88bb7cc123 [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);
59 case ISD::BRCOND:
60 return LowerGlobalAddress(Op, DAG);
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000061 }
62}
63
Eric Christopher50880d02010-09-18 18:52:28 +000064const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
65 switch (Opcode) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000066 default:
67 llvm_unreachable("Unknown opcode");
Justin Holewinski8af78c92011-03-18 19:24:28 +000068 case PTXISD::COPY_ADDRESS:
69 return "PTXISD::COPY_ADDRESS";
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000070 case PTXISD::READ_PARAM:
71 return "PTXISD::READ_PARAM";
72 case PTXISD::EXIT:
73 return "PTXISD::EXIT";
74 case PTXISD::RET:
75 return "PTXISD::RET";
Eric Christopher50880d02010-09-18 18:52:28 +000076 }
77}
78
79//===----------------------------------------------------------------------===//
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000080// Custom Lower Operation
81//===----------------------------------------------------------------------===//
82
83SDValue PTXTargetLowering::
84LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
85 EVT PtrVT = getPointerTy();
86 DebugLoc dl = Op.getDebugLoc();
87 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Justin Holewinski8af78c92011-03-18 19:24:28 +000088
Justin Holewinskid6625762011-03-23 16:58:51 +000089 assert(PtrVT.isSimple() && "Pointer must be to primitive type.");
90
Justin Holewinski8af78c92011-03-18 19:24:28 +000091 SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
92 SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS,
93 dl,
Justin Holewinskid6625762011-03-23 16:58:51 +000094 PtrVT.getSimpleVT(),
Justin Holewinski8af78c92011-03-18 19:24:28 +000095 targetGlobal);
96
97 return movInstr;
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000098}
99
100//===----------------------------------------------------------------------===//
Eric Christopher50880d02010-09-18 18:52:28 +0000101// Calling Convention Implementation
102//===----------------------------------------------------------------------===//
103
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000104namespace {
105struct argmap_entry {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000106 MVT::SimpleValueType VT;
107 TargetRegisterClass *RC;
108 TargetRegisterClass::iterator loc;
109
110 argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC)
111 : VT(_VT), RC(_RC), loc(_RC->begin()) {}
112
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000113 void reset() { loc = RC->begin(); }
114 bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000115} argmap[] = {
116 argmap_entry(MVT::i1, PTX::PredsRegisterClass),
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000117 argmap_entry(MVT::i16, PTX::RRegu16RegisterClass),
118 argmap_entry(MVT::i32, PTX::RRegu32RegisterClass),
119 argmap_entry(MVT::i64, PTX::RRegu64RegisterClass),
120 argmap_entry(MVT::f32, PTX::RRegf32RegisterClass),
121 argmap_entry(MVT::f64, PTX::RRegf64RegisterClass)
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000122};
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000123} // end anonymous namespace
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000124
Eric Christopher50880d02010-09-18 18:52:28 +0000125SDValue PTXTargetLowering::
126 LowerFormalArguments(SDValue Chain,
127 CallingConv::ID CallConv,
128 bool isVarArg,
129 const SmallVectorImpl<ISD::InputArg> &Ins,
130 DebugLoc dl,
131 SelectionDAG &DAG,
132 SmallVectorImpl<SDValue> &InVals) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000133 if (isVarArg) llvm_unreachable("PTX does not support varargs");
134
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000135 MachineFunction &MF = DAG.getMachineFunction();
136 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
137
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000138 switch (CallConv) {
139 default:
140 llvm_unreachable("Unsupported calling convention");
141 break;
142 case CallingConv::PTX_Kernel:
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000143 MFI->setKernel(true);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000144 break;
145 case CallingConv::PTX_Device:
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000146 MFI->setKernel(false);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000147 break;
148 }
149
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000150 // Make sure we don't add argument registers twice
151 if (MFI->isDoneAddArg())
152 llvm_unreachable("cannot add argument registers twice");
153
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000154 // Reset argmap before allocation
155 for (struct argmap_entry *i = argmap, *e = argmap + array_lengthof(argmap);
156 i != e; ++ i)
157 i->reset();
158
159 for (int i = 0, e = Ins.size(); i != e; ++ i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000160 MVT::SimpleValueType VT = Ins[i].VT.SimpleTy;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000161
162 struct argmap_entry *entry = std::find(argmap,
163 argmap + array_lengthof(argmap), VT);
164 if (entry == argmap + array_lengthof(argmap))
165 llvm_unreachable("Type of argument is not supported");
166
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000167 if (MFI->isKernel() && entry->RC == PTX::PredsRegisterClass)
168 llvm_unreachable("cannot pass preds to kernel");
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000169
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000170 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
171
172 unsigned preg = *++(entry->loc); // allocate start from register 1
173 unsigned vreg = RegInfo.createVirtualRegister(entry->RC);
174 RegInfo.addLiveIn(preg, vreg);
175
176 MFI->addArgReg(preg);
177
178 SDValue inval;
179 if (MFI->isKernel())
180 inval = DAG.getNode(PTXISD::READ_PARAM, dl, VT, Chain,
181 DAG.getTargetConstant(i, MVT::i32));
182 else
183 inval = DAG.getCopyFromReg(Chain, dl, vreg, VT);
184 InVals.push_back(inval);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000185 }
186
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000187 MFI->doneAddArg();
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000188
Eric Christopher50880d02010-09-18 18:52:28 +0000189 return Chain;
190}
191
192SDValue PTXTargetLowering::
193 LowerReturn(SDValue Chain,
194 CallingConv::ID CallConv,
195 bool isVarArg,
196 const SmallVectorImpl<ISD::OutputArg> &Outs,
197 const SmallVectorImpl<SDValue> &OutVals,
198 DebugLoc dl,
199 SelectionDAG &DAG) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000200 if (isVarArg) llvm_unreachable("PTX does not support varargs");
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000201
202 switch (CallConv) {
203 default:
204 llvm_unreachable("Unsupported calling convention.");
205 case CallingConv::PTX_Kernel:
206 assert(Outs.size() == 0 && "Kernel must return void.");
207 return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
208 case CallingConv::PTX_Device:
209 assert(Outs.size() <= 1 && "Can at most return one value.");
210 break;
211 }
212
213 // PTX_Device
214
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000215 // return void
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000216 if (Outs.size() == 0)
217 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
218
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000219 SDValue Flag;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000220 unsigned reg;
221
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000222 if (Outs[0].VT == MVT::i16) {
223 reg = PTX::RH0;
224 }
225 else if (Outs[0].VT == MVT::i32) {
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000226 reg = PTX::R0;
227 }
Che-Liang Chioufd8978b2011-03-02 03:20:28 +0000228 else if (Outs[0].VT == MVT::i64) {
229 reg = PTX::RD0;
230 }
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000231 else if (Outs[0].VT == MVT::f32) {
232 reg = PTX::F0;
233 }
234 else {
Duncan Sands75548de2011-03-15 08:41:24 +0000235 assert(Outs[0].VT == MVT::f64 && "Can return only basic types");
236 reg = PTX::FD0;
Che-Liang Chiouf7172022011-02-28 06:34:09 +0000237 }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000238
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000239 MachineFunction &MF = DAG.getMachineFunction();
240 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
241 MFI->setRetReg(reg);
242
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000243 // If this is the first return lowered for this function, add the regs to the
244 // liveout set for the function
245 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
246 DAG.getMachineFunction().getRegInfo().addLiveOut(reg);
247
248 // Copy the result values into the output registers
249 Chain = DAG.getCopyToReg(Chain, dl, reg, OutVals[0], Flag);
250
251 // Guarantee that all emitted copies are stuck together,
252 // avoiding something bad
253 Flag = Chain.getValue(1);
254
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000255 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Eric Christopher50880d02010-09-18 18:52:28 +0000256}