1 //===-- PTXISelLowering.cpp - PTX DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PTXTargetLowering class.
12 //===----------------------------------------------------------------------===//
15 #include "PTXISelLowering.h"
16 #include "PTXMachineFunctionInfo.h"
17 #include "PTXRegisterInfo.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
26 PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
27 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
28 // Set up the register classes.
29 addRegisterClass(MVT::i1, PTX::PredsRegisterClass);
30 addRegisterClass(MVT::i32, PTX::RRegs32RegisterClass);
32 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
34 // Customize translation of memory addresses
35 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
37 // Compute derived properties from the register classes
38 computeRegisterProperties();
41 SDValue 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);
48 const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
50 default: llvm_unreachable("Unknown opcode");
51 case PTXISD::EXIT: return "PTXISD::EXIT";
52 case PTXISD::RET: return "PTXISD::RET";
56 //===----------------------------------------------------------------------===//
57 // Custom Lower Operation
58 //===----------------------------------------------------------------------===//
60 SDValue PTXTargetLowering::
61 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
62 EVT PtrVT = getPointerTy();
63 DebugLoc dl = Op.getDebugLoc();
64 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
65 return DAG.getTargetGlobalAddress(GV, dl, PtrVT);
68 //===----------------------------------------------------------------------===//
69 // Calling Convention Implementation
70 //===----------------------------------------------------------------------===//
74 MVT::SimpleValueType VT;
75 TargetRegisterClass *RC;
76 TargetRegisterClass::iterator loc;
78 argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC)
79 : VT(_VT), RC(_RC), loc(_RC->begin()) {}
81 void reset() { loc = RC->begin(); }
82 bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; }
84 argmap_entry(MVT::i1, PTX::PredsRegisterClass),
85 argmap_entry(MVT::i32, PTX::RRegs32RegisterClass)
87 } // end anonymous namespace
89 static SDValue lower_kernel_argument(int i,
92 MVT::SimpleValueType VT,
97 llvm_unreachable("Not implemented yet");
100 static SDValue lower_device_argument(int i,
103 MVT::SimpleValueType VT,
107 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
109 unsigned preg = *++(entry->loc); // allocate start from register 1
110 unsigned vreg = RegInfo.createVirtualRegister(entry->RC);
111 RegInfo.addLiveIn(preg, vreg);
114 return DAG.getCopyFromReg(Chain, dl, vreg, VT);
117 typedef SDValue (*lower_argument_func)(int i,
120 MVT::SimpleValueType VT,
125 SDValue PTXTargetLowering::
126 LowerFormalArguments(SDValue Chain,
127 CallingConv::ID CallConv,
129 const SmallVectorImpl<ISD::InputArg> &Ins,
132 SmallVectorImpl<SDValue> &InVals) const {
133 if (isVarArg) llvm_unreachable("PTX does not support varargs");
135 MachineFunction &MF = DAG.getMachineFunction();
136 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
138 lower_argument_func lower_argument;
142 llvm_unreachable("Unsupported calling convention");
144 case CallingConv::PTX_Kernel:
146 lower_argument = lower_kernel_argument;
148 case CallingConv::PTX_Device:
149 MFI->setKernel(false);
150 lower_argument = lower_device_argument;
154 // Reset argmap before allocation
155 for (struct argmap_entry *i = argmap, *e = argmap + array_lengthof(argmap);
159 for (int i = 0, e = Ins.size(); i != e; ++ i) {
160 MVT::SimpleValueType VT = Ins[i].VT.SimpleTy;
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");
168 SDValue arg = lower_argument(i, Chain, dl, VT, entry, DAG, ®);
169 InVals.push_back(arg);
171 if (!MFI->isDoneAddArg())
175 // Make sure we don't add argument registers twice
176 if (!MFI->isDoneAddArg())
182 SDValue PTXTargetLowering::
183 LowerReturn(SDValue Chain,
184 CallingConv::ID CallConv,
186 const SmallVectorImpl<ISD::OutputArg> &Outs,
187 const SmallVectorImpl<SDValue> &OutVals,
189 SelectionDAG &DAG) const {
190 if (isVarArg) llvm_unreachable("PTX does not support varargs");
194 llvm_unreachable("Unsupported calling convention.");
195 case CallingConv::PTX_Kernel:
196 assert(Outs.size() == 0 && "Kernel must return void.");
197 return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
198 case CallingConv::PTX_Device:
199 assert(Outs.size() <= 1 && "Can at most return one value.");
206 if (Outs.size() == 0)
207 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
209 assert(Outs[0].VT == MVT::i32 && "Can return only basic types");
212 unsigned reg = PTX::R0;
214 MachineFunction &MF = DAG.getMachineFunction();
215 PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
218 // If this is the first return lowered for this function, add the regs to the
219 // liveout set for the function
220 if (DAG.getMachineFunction().getRegInfo().liveout_empty())
221 DAG.getMachineFunction().getRegInfo().addLiveOut(reg);
223 // Copy the result values into the output registers
224 Chain = DAG.getCopyToReg(Chain, dl, reg, OutVals[0], Flag);
226 // Guarantee that all emitted copies are stuck together,
227 // avoiding something bad
228 Flag = Chain.getValue(1);
230 return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);