Enable printing of immediates that do not fit in 16-bit. .cprestore can have
[oota-llvm.git] / lib / Target / PTX / PTXISelLowering.cpp
1 //===-- 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
14 #include "PTX.h"
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"
23 #include "llvm/Support/raw_ostream.h"
24
25 using namespace llvm;
26
27 PTXTargetLowering::PTXTargetLowering(TargetMachine &TM)
28   : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
29   // Set up the register classes.
30   addRegisterClass(MVT::i1,  PTX::PredsRegisterClass);
31   addRegisterClass(MVT::i16, PTX::RRegu16RegisterClass);
32   addRegisterClass(MVT::i32, PTX::RRegu32RegisterClass);
33   addRegisterClass(MVT::i64, PTX::RRegu64RegisterClass);
34   addRegisterClass(MVT::f32, PTX::RRegf32RegisterClass);
35   addRegisterClass(MVT::f64, PTX::RRegf64RegisterClass);
36
37   setBooleanContents(ZeroOrOneBooleanContent);
38   
39   setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
40
41   setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
42   setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
43   
44   // Turn i16 (z)extload into load + (z)extend
45   setLoadExtAction(ISD::EXTLOAD, MVT::i16, Expand);
46   setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
47
48   // Turn f32 extload into load + fextend
49   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
50   
51   // Turn f64 truncstore into trunc + store.
52   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
53   
54   // Customize translation of memory addresses
55   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
56   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
57
58   // Expand BR_CC into BRCOND
59   setOperationAction(ISD::BR_CC, MVT::Other, Expand);
60
61   // Expand SELECT_CC into SETCC
62   setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
63   setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
64   setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
65   
66   // need to lower SETCC of Preds into bitwise logic
67   setOperationAction(ISD::SETCC, MVT::i1, Custom);
68
69   setMinFunctionAlignment(2);
70
71   // Compute derived properties from the register classes
72   computeRegisterProperties();
73 }
74
75 MVT::SimpleValueType PTXTargetLowering::getSetCCResultType(EVT VT) const {
76   return MVT::i1;
77 }
78
79 SDValue PTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
80   switch (Op.getOpcode()) {
81     default:
82       llvm_unreachable("Unimplemented operand");
83     case ISD::SETCC:
84       return LowerSETCC(Op, DAG);
85     case ISD::GlobalAddress:
86       return LowerGlobalAddress(Op, DAG);
87   }
88 }
89
90 const char *PTXTargetLowering::getTargetNodeName(unsigned Opcode) const {
91   switch (Opcode) {
92     default:
93       llvm_unreachable("Unknown opcode");
94     case PTXISD::COPY_ADDRESS:
95       return "PTXISD::COPY_ADDRESS";
96     case PTXISD::READ_PARAM:
97       return "PTXISD::READ_PARAM";
98     case PTXISD::EXIT:
99       return "PTXISD::EXIT";
100     case PTXISD::RET:
101       return "PTXISD::RET";
102   }
103 }
104
105 //===----------------------------------------------------------------------===//
106 //                      Custom Lower Operation
107 //===----------------------------------------------------------------------===//
108
109 SDValue PTXTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
110   assert(Op.getValueType() == MVT::i1 && "SetCC type must be 1-bit integer");
111   SDValue Op0 = Op.getOperand(0);
112   SDValue Op1 = Op.getOperand(1);
113   SDValue Op2 = Op.getOperand(2);
114   DebugLoc dl = Op.getDebugLoc();
115   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
116   
117   // Look for X == 0, X == 1, X != 0, or X != 1  
118   // We can simplify these to bitwise logic
119   
120   if (Op1.getOpcode() == ISD::Constant &&
121       (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
122        cast<ConstantSDNode>(Op1)->isNullValue()) &&
123       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
124
125           return DAG.getNode(ISD::AND, dl, MVT::i1, Op0, Op1);
126   }
127   
128   return DAG.getNode(ISD::SETCC, dl, MVT::i1, Op0, Op1, Op2);
129 }
130
131 SDValue PTXTargetLowering::
132 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
133   EVT PtrVT = getPointerTy();
134   DebugLoc dl = Op.getDebugLoc();
135   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
136
137   assert(PtrVT.isSimple() && "Pointer must be to primitive type.");
138
139   SDValue targetGlobal = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
140   SDValue movInstr = DAG.getNode(PTXISD::COPY_ADDRESS,
141                                  dl,
142                                  PtrVT.getSimpleVT(),
143                                  targetGlobal);
144
145   return movInstr;
146 }
147
148 //===----------------------------------------------------------------------===//
149 //                      Calling Convention Implementation
150 //===----------------------------------------------------------------------===//
151
152 namespace {
153 struct argmap_entry {
154   MVT::SimpleValueType VT;
155   TargetRegisterClass *RC;
156   TargetRegisterClass::iterator loc;
157
158   argmap_entry(MVT::SimpleValueType _VT, TargetRegisterClass *_RC)
159     : VT(_VT), RC(_RC), loc(_RC->begin()) {}
160
161   void reset() { loc = RC->begin(); }
162   bool operator==(MVT::SimpleValueType _VT) const { return VT == _VT; }
163 } argmap[] = {
164   argmap_entry(MVT::i1,  PTX::PredsRegisterClass),
165   argmap_entry(MVT::i16, PTX::RRegu16RegisterClass),
166   argmap_entry(MVT::i32, PTX::RRegu32RegisterClass),
167   argmap_entry(MVT::i64, PTX::RRegu64RegisterClass),
168   argmap_entry(MVT::f32, PTX::RRegf32RegisterClass),
169   argmap_entry(MVT::f64, PTX::RRegf64RegisterClass)
170 };
171 }                               // end anonymous namespace
172
173 SDValue PTXTargetLowering::
174   LowerFormalArguments(SDValue Chain,
175                        CallingConv::ID CallConv,
176                        bool isVarArg,
177                        const SmallVectorImpl<ISD::InputArg> &Ins,
178                        DebugLoc dl,
179                        SelectionDAG &DAG,
180                        SmallVectorImpl<SDValue> &InVals) const {
181   if (isVarArg) llvm_unreachable("PTX does not support varargs");
182
183   MachineFunction &MF = DAG.getMachineFunction();
184   PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
185
186   switch (CallConv) {
187     default:
188       llvm_unreachable("Unsupported calling convention");
189       break;
190     case CallingConv::PTX_Kernel:
191       MFI->setKernel(true);
192       break;
193     case CallingConv::PTX_Device:
194       MFI->setKernel(false);
195       break;
196   }
197
198   // Make sure we don't add argument registers twice
199   if (MFI->isDoneAddArg())
200     llvm_unreachable("cannot add argument registers twice");
201
202   // Reset argmap before allocation
203   for (struct argmap_entry *i = argmap, *e = argmap + array_lengthof(argmap);
204        i != e; ++ i)
205     i->reset();
206
207   for (int i = 0, e = Ins.size(); i != e; ++ i) {
208     MVT::SimpleValueType VT = Ins[i].VT.SimpleTy;
209
210     struct argmap_entry *entry = std::find(argmap,
211                                            argmap + array_lengthof(argmap), VT);
212     if (entry == argmap + array_lengthof(argmap))
213       llvm_unreachable("Type of argument is not supported");
214
215     if (MFI->isKernel() && entry->RC == PTX::PredsRegisterClass)
216       llvm_unreachable("cannot pass preds to kernel");
217
218     MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
219
220     unsigned preg = *++(entry->loc); // allocate start from register 1
221     unsigned vreg = RegInfo.createVirtualRegister(entry->RC);
222     RegInfo.addLiveIn(preg, vreg);
223
224     MFI->addArgReg(preg);
225
226     SDValue inval;
227     if (MFI->isKernel())
228       inval = DAG.getNode(PTXISD::READ_PARAM, dl, VT, Chain,
229                           DAG.getTargetConstant(i, MVT::i32));
230     else
231       inval = DAG.getCopyFromReg(Chain, dl, vreg, VT);
232     InVals.push_back(inval);
233   }
234
235   MFI->doneAddArg();
236
237   return Chain;
238 }
239
240 SDValue PTXTargetLowering::
241   LowerReturn(SDValue Chain,
242               CallingConv::ID CallConv,
243               bool isVarArg,
244               const SmallVectorImpl<ISD::OutputArg> &Outs,
245               const SmallVectorImpl<SDValue> &OutVals,
246               DebugLoc dl,
247               SelectionDAG &DAG) const {
248   if (isVarArg) llvm_unreachable("PTX does not support varargs");
249
250   switch (CallConv) {
251     default:
252       llvm_unreachable("Unsupported calling convention.");
253     case CallingConv::PTX_Kernel:
254       assert(Outs.size() == 0 && "Kernel must return void.");
255       return DAG.getNode(PTXISD::EXIT, dl, MVT::Other, Chain);
256     case CallingConv::PTX_Device:
257       assert(Outs.size() <= 1 && "Can at most return one value.");
258       break;
259   }
260
261   // PTX_Device
262
263   // return void
264   if (Outs.size() == 0)
265     return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain);
266
267   SDValue Flag;
268   unsigned reg;
269
270   if (Outs[0].VT == MVT::i16) {
271     reg = PTX::RH0;
272   }
273   else if (Outs[0].VT == MVT::i32) {
274     reg = PTX::R0;
275   }
276   else if (Outs[0].VT == MVT::i64) {
277     reg = PTX::RD0;
278   }
279   else if (Outs[0].VT == MVT::f32) {
280     reg = PTX::F0;
281   }
282   else {
283     assert(Outs[0].VT == MVT::f64 && "Can return only basic types");
284     reg = PTX::FD0;
285   }
286
287   MachineFunction &MF = DAG.getMachineFunction();
288   PTXMachineFunctionInfo *MFI = MF.getInfo<PTXMachineFunctionInfo>();
289   MFI->setRetReg(reg);
290
291   // If this is the first return lowered for this function, add the regs to the
292   // liveout set for the function
293   if (DAG.getMachineFunction().getRegInfo().liveout_empty())
294     DAG.getMachineFunction().getRegInfo().addLiveOut(reg);
295
296   // Copy the result values into the output registers
297   Chain = DAG.getCopyToReg(Chain, dl, reg, OutVals[0], Flag);
298
299   // Guarantee that all emitted copies are stuck together,
300   // avoiding something bad
301   Flag = Chain.getValue(1);
302
303   return DAG.getNode(PTXISD::RET, dl, MVT::Other, Chain, Flag);
304 }