blob: e6d44907ed375f8f677717f36cb473176ab721f8 [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"
23
24using namespace llvm;
25
26PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
27 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
28 // Set up the register classes.
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000029 addRegisterClass(MVT::i1, PTX::PredsRegisterClass);
30 addRegisterClass(MVT::i32, PTX::RRegs32RegisterClass);
Eric Christopher50880d02010-09-18 18:52:28 +000031
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000032 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
33
34 // Customize translation of memory addresses
35 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
36
Eric Christopher50880d02010-09-18 18:52:28 +000037 // Compute derived properties from the register classes
38 computeRegisterProperties();
39}
40
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000041SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
42 switch (Op.getOpcode()) {
43 default: llvm_unreachable("Unimplemented operand");
44 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
45 }
46}
47
Eric Christopher50880d02010-09-18 18:52:28 +000048const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
49 switch (Opcode) {
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +000050 default:
51 llvm_unreachable("Unknown opcode");
52 case PTXISD::READ_PARAM:
53 return "PTXISD::READ_PARAM";
54 case PTXISD::EXIT:
55 return "PTXISD::EXIT";
56 case PTXISD::RET:
57 return "PTXISD::RET";
Eric Christopher50880d02010-09-18 18:52:28 +000058 }
59}
60
61//===----------------------------------------------------------------------===//
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000062// Custom Lower Operation
63//===----------------------------------------------------------------------===//
64
65SDValue PTXTargetLowering::
66LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
67 EVT PtrVT = getPointerTy();
68 DebugLoc dl = Op.getDebugLoc();
69 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
70 return DAG.getTargetGlobalAddress(GV, dl, PtrVT);
71}
72
73//===----------------------------------------------------------------------===//
Eric Christopher50880d02010-09-18 18:52:28 +000074// Calling Convention Implementation
75//===----------------------------------------------------------------------===//
76
Benjamin Kramera3ac4272010-10-22 17:35:07 +000077namespace {
78struct argmap_entry {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000079 MVT::SimpleValueType VT;
80 TargetRegisterClass *RC;
81 TargetRegisterClass::iterator loc;
82
83 argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC)
84 : VT(_VT), RC(_RC), loc(_RC->begin()) {}
85
Benjamin Kramera3ac4272010-10-22 17:35:07 +000086 void reset() { loc = RC->begin(); }
87 bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; }
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000088} argmap[] = {
89 argmap_entry(MVT::i1, PTX::PredsRegisterClass),
90 argmap_entry(MVT::i32, PTX::RRegs32RegisterClass)
91};
Benjamin Kramera3ac4272010-10-22 17:35:07 +000092} // end anonymous namespace
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000093
Eric Christopher50880d02010-09-18 18:52:28 +000094SDValue PTXTargetLowering::
95 LowerFormalArguments(SDValue Chain,
96 CallingConv::ID CallConv,
97 bool isVarArg,
98 const SmallVectorImpl<ISD::InputArg> &Ins,
99 DebugLoc dl,
100 SelectionDAG &DAG,
101 SmallVectorImpl<SDValue> &InVals) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000102 if (isVarArg) llvm_unreachable("PTX does not support varargs");
103
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000104 MachineFunction &MF = DAG.getMachineFunction();
105 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
106
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000107 switch (CallConv) {
108 default:
109 llvm_unreachable("Unsupported calling convention");
110 break;
111 case CallingConv::PTX_Kernel:
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000112 MFI->setKernel(true);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000113 break;
114 case CallingConv::PTX_Device:
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000115 MFI->setKernel(false);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000116 break;
117 }
118
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000119 // Make sure we don't add argument registers twice
120 if (MFI->isDoneAddArg())
121 llvm_unreachable("cannot add argument registers twice");
122
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000123 // Reset argmap before allocation
124 for (struct argmap_entry *i = argmap, *e = argmap + array_lengthof(argmap);
125 i != e; ++ i)
126 i->reset();
127
128 for (int i = 0, e = Ins.size(); i != e; ++ i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000129 MVT::SimpleValueType VT = Ins[i].VT.SimpleTy;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000130
131 struct argmap_entry *entry = std::find(argmap,
132 argmap + array_lengthof(argmap), VT);
133 if (entry == argmap + array_lengthof(argmap))
134 llvm_unreachable("Type of argument is not supported");
135
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000136 if (MFI->isKernel() && entry->RC == PTX::PredsRegisterClass)
137 llvm_unreachable("cannot pass preds to kernel");
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000138
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000139 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
140
141 unsigned preg = *++(entry->loc); // allocate start from register 1
142 unsigned vreg = RegInfo.createVirtualRegister(entry->RC);
143 RegInfo.addLiveIn(preg, vreg);
144
145 MFI->addArgReg(preg);
146
147 SDValue inval;
148 if (MFI->isKernel())
149 inval = DAG.getNode(PTXISD::READ_PARAM, dl, VT, Chain,
150 DAG.getTargetConstant(i, MVT::i32));
151 else
152 inval = DAG.getCopyFromReg(Chain, dl, vreg, VT);
153 InVals.push_back(inval);
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000154 }
155
Che-Liang Chiou8e5d01c2011-02-10 12:01:24 +0000156 MFI->doneAddArg();
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000157
Eric Christopher50880d02010-09-18 18:52:28 +0000158 return Chain;
159}
160
161SDValue PTXTargetLowering::
162 LowerReturn(SDValue Chain,
163 CallingConv::ID CallConv,
164 bool isVarArg,
165 const SmallVectorImpl<ISD::OutputArg> &Outs,
166 const SmallVectorImpl<SDValue> &OutVals,
167 DebugLoc dl,
168 SelectionDAG &DAG) const {
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000169 if (isVarArg) llvm_unreachable("PTX does not support varargs");
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000170
171 switch (CallConv) {
172 default:
173 llvm_unreachable("Unsupported calling convention.");
174 case CallingConv::PTX_Kernel:
175 assert(Outs.size() == 0 && "Kernel must return void.");
176 return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
177 case CallingConv::PTX_Device:
178 assert(Outs.size() <= 1 && "Can at most return one value.");
179 break;
180 }
181
182 // PTX_Device
183
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000184 // return void
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000185 if (Outs.size() == 0)
186 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
187
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000188 assert(Outs[0].VT == MVT::i32 && "Can return only basic types");
189
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000190 SDValue Flag;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000191 unsigned reg = PTX::R0;
192
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000193 MachineFunction &MF = DAG.getMachineFunction();
194 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
195 MFI->setRetReg(reg);
196
Che-Liang Chioub48f2c22010-10-19 13:14:40 +0000197 // If this is the first return lowered for this function, add the regs to the
198 // liveout set for the function
199 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
200 DAG.getMachineFunction().getRegInfo().addLiveOut(reg);
201
202 // Copy the result values into the output registers
203 Chain = DAG.getCopyToReg(Chain, dl, reg, OutVals[0], Flag);
204
205 // Guarantee that all emitted copies are stuck together,
206 // avoiding something bad
207 Flag = Chain.getValue(1);
208
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000209 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
Eric Christopher50880d02010-09-18 18:52:28 +0000210}