5a468aeed3449100c10e18452645739e0fd3d331
[oota-llvm.git] / lib / Target / R600 / SIISelLowering.cpp
1 //===-- SIISelLowering.cpp - SI 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 /// \file
11 /// \brief Custom DAG lowering for SI
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "SIISelLowering.h"
16 #include "AMDIL.h"
17 #include "AMDILIntrinsicInfo.h"
18 #include "SIInstrInfo.h"
19 #include "SIMachineFunctionInfo.h"
20 #include "SIRegisterInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24
25 using namespace llvm;
26
27 SITargetLowering::SITargetLowering(TargetMachine &TM) :
28     AMDGPUTargetLowering(TM),
29     TII(static_cast<const SIInstrInfo*>(TM.getInstrInfo())) {
30   addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
31   addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
32   addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass);
33   addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
34   addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
35
36   addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass);
37   addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass);
38   addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
39   addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
40   addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
41
42   computeRegisterProperties();
43
44   setOperationAction(ISD::ADD, MVT::i64, Legal);
45   setOperationAction(ISD::ADD, MVT::i32, Legal);
46
47   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
48
49   // We need to custom lower loads from the USER_SGPR address space, so we can
50   // add the SGPRs as livein registers.
51   setOperationAction(ISD::LOAD, MVT::i32, Custom);
52   setOperationAction(ISD::LOAD, MVT::i64, Custom);
53
54   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
55   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
56
57   setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
58   setTargetDAGCombine(ISD::SELECT_CC);
59
60   setTargetDAGCombine(ISD::SETCC);
61 }
62
63 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
64     MachineInstr * MI, MachineBasicBlock * BB) const {
65   const TargetInstrInfo * TII = getTargetMachine().getInstrInfo();
66   MachineRegisterInfo & MRI = BB->getParent()->getRegInfo();
67   MachineBasicBlock::iterator I = MI;
68
69   switch (MI->getOpcode()) {
70   default:
71     return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
72   case AMDGPU::BRANCH: return BB;
73   case AMDGPU::CLAMP_SI:
74     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
75            .addOperand(MI->getOperand(0))
76            .addOperand(MI->getOperand(1))
77            .addImm(0x80) // SRC1
78            .addImm(0) // ABS
79            .addImm(1) // CLAMP
80            .addImm(0) // OMOD
81            .addImm(0); // NEG
82     MI->eraseFromParent();
83     break;
84
85   case AMDGPU::FABS_SI:
86     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
87                  .addOperand(MI->getOperand(0))
88                  .addOperand(MI->getOperand(1))
89                  .addImm(0x80) // SRC1
90                  .addImm(1) // ABS
91                  .addImm(0) // CLAMP
92                  .addImm(0) // OMOD
93                  .addImm(0); // NEG
94     MI->eraseFromParent();
95     break;
96
97   case AMDGPU::FNEG_SI:
98     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
99                  .addOperand(MI->getOperand(0))
100                  .addOperand(MI->getOperand(1))
101                  .addImm(0x80) // SRC1
102                  .addImm(0) // ABS
103                  .addImm(0) // CLAMP
104                  .addImm(0) // OMOD
105                  .addImm(1); // NEG
106     MI->eraseFromParent();
107     break;
108   case AMDGPU::SHADER_TYPE:
109     BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
110                                         MI->getOperand(0).getImm();
111     MI->eraseFromParent();
112     break;
113
114   case AMDGPU::SI_INTERP:
115     LowerSI_INTERP(MI, *BB, I, MRI);
116     break;
117   case AMDGPU::SI_WQM:
118     LowerSI_WQM(MI, *BB, I, MRI);
119     break;
120   case AMDGPU::SI_V_CNDLT:
121     LowerSI_V_CNDLT(MI, *BB, I, MRI);
122     break;
123   }
124   return BB;
125 }
126
127 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
128     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
129   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
130           .addReg(AMDGPU::EXEC);
131
132   MI->eraseFromParent();
133 }
134
135 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
136     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
137   unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
138   unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
139   MachineOperand dst = MI->getOperand(0);
140   MachineOperand iReg = MI->getOperand(1);
141   MachineOperand jReg = MI->getOperand(2);
142   MachineOperand attr_chan = MI->getOperand(3);
143   MachineOperand attr = MI->getOperand(4);
144   MachineOperand params = MI->getOperand(5);
145
146   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
147           .addOperand(params);
148
149   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
150           .addOperand(iReg)
151           .addOperand(attr_chan)
152           .addOperand(attr)
153           .addReg(M0);
154
155   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
156           .addOperand(dst)
157           .addReg(tmp)
158           .addOperand(jReg)
159           .addOperand(attr_chan)
160           .addOperand(attr)
161           .addReg(M0);
162
163   MI->eraseFromParent();
164 }
165
166 void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
167     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
168   unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
169
170   BuildMI(BB, I, BB.findDebugLoc(I),
171           TII->get(AMDGPU::V_CMP_GT_F32_e32),
172           VCC)
173           .addImm(0)
174           .addOperand(MI->getOperand(1));
175
176   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
177           .addOperand(MI->getOperand(0))
178           .addOperand(MI->getOperand(3))
179           .addOperand(MI->getOperand(2))
180           .addReg(VCC);
181
182   MI->eraseFromParent();
183 }
184
185 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
186   return MVT::i1;
187 }
188
189 //===----------------------------------------------------------------------===//
190 // Custom DAG Lowering Operations
191 //===----------------------------------------------------------------------===//
192
193 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
194   switch (Op.getOpcode()) {
195   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
196   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
197   case ISD::LOAD: return LowerLOAD(Op, DAG);
198   case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
199   case ISD::INTRINSIC_WO_CHAIN: {
200     unsigned IntrinsicID =
201                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
202     EVT VT = Op.getValueType();
203     switch (IntrinsicID) {
204     case AMDGPUIntrinsic::SI_vs_load_buffer_index:
205       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
206                                   AMDGPU::VGPR0, VT);
207     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
208     }
209     break;
210   }
211   }
212   return SDValue();
213 }
214
215 /// \brief Helper function for LowerBRCOND
216 static SDNode *findUser(SDValue Value, unsigned Opcode) {
217
218   SDNode *Parent = Value.getNode();
219   for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
220        I != E; ++I) {
221
222     if (I.getUse().get() != Value)
223       continue;
224
225     if (I->getOpcode() == Opcode)
226       return *I;
227   }
228   return 0;
229 }
230
231 /// This transforms the control flow intrinsics to get the branch destination as
232 /// last parameter, also switches branch target with BR if the need arise
233 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
234                                       SelectionDAG &DAG) const {
235
236   DebugLoc DL = BRCOND.getDebugLoc();
237
238   SDNode *Intr = BRCOND.getOperand(1).getNode();
239   SDValue Target = BRCOND.getOperand(2);
240   SDNode *BR = 0;
241
242   if (Intr->getOpcode() == ISD::SETCC) {
243     // As long as we negate the condition everything is fine
244     SDNode *SetCC = Intr;
245     assert(SetCC->getConstantOperandVal(1) == 1);
246     assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
247            ISD::SETNE);
248     Intr = SetCC->getOperand(0).getNode();
249
250   } else {
251     // Get the target from BR if we don't negate the condition
252     BR = findUser(BRCOND, ISD::BR);
253     Target = BR->getOperand(1);
254   }
255
256   assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
257
258   // Build the result and
259   SmallVector<EVT, 4> Res;
260   for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
261     Res.push_back(Intr->getValueType(i));
262
263   // operands of the new intrinsic call
264   SmallVector<SDValue, 4> Ops;
265   Ops.push_back(BRCOND.getOperand(0));
266   for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
267     Ops.push_back(Intr->getOperand(i));
268   Ops.push_back(Target);
269
270   // build the new intrinsic call
271   SDNode *Result = DAG.getNode(
272     Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
273     DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
274
275   if (BR) {
276     // Give the branch instruction our target
277     SDValue Ops[] = {
278       BR->getOperand(0),
279       BRCOND.getOperand(2)
280     };
281     DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
282   }
283
284   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
285
286   // Copy the intrinsic results to registers
287   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
288     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
289     if (!CopyToReg)
290       continue;
291
292     Chain = DAG.getCopyToReg(
293       Chain, DL,
294       CopyToReg->getOperand(1),
295       SDValue(Result, i - 1),
296       SDValue());
297
298     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
299   }
300
301   // Remove the old intrinsic from the chain
302   DAG.ReplaceAllUsesOfValueWith(
303     SDValue(Intr, Intr->getNumValues() - 1),
304     Intr->getOperand(0));
305
306   return Chain;
307 }
308
309 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
310   EVT VT = Op.getValueType();
311   LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
312
313   assert(Ptr);
314
315   unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
316
317   // We only need to lower USER_SGPR address space loads
318   if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
319     return SDValue();
320   }
321
322   // Loads from the USER_SGPR address space can only have constant value
323   // pointers.
324   ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
325   assert(BasePtr);
326
327   unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
328   const TargetRegisterClass * dstClass;
329   switch (TypeDwordWidth) {
330     default:
331       assert(!"USER_SGPR value size not implemented");
332       return SDValue();
333     case 1:
334       dstClass = &AMDGPU::SReg_32RegClass;
335       break;
336     case 2:
337       dstClass = &AMDGPU::SReg_64RegClass;
338       break;
339   }
340   uint64_t Index = BasePtr->getZExtValue();
341   assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
342   unsigned SGPRIndex = Index / TypeDwordWidth;
343   unsigned Reg = dstClass->getRegister(SGPRIndex);
344
345   DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
346                                                          VT));
347   return SDValue();
348 }
349
350 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
351   SDValue LHS = Op.getOperand(0);
352   SDValue RHS = Op.getOperand(1);
353   SDValue True = Op.getOperand(2);
354   SDValue False = Op.getOperand(3);
355   SDValue CC = Op.getOperand(4);
356   EVT VT = Op.getValueType();
357   DebugLoc DL = Op.getDebugLoc();
358
359   // Possible Min/Max pattern
360   SDValue MinMax = LowerMinMax(Op, DAG);
361   if (MinMax.getNode()) {
362     return MinMax;
363   }
364
365   SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
366   return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
367 }
368
369 //===----------------------------------------------------------------------===//
370 // Custom DAG optimizations
371 //===----------------------------------------------------------------------===//
372
373 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
374                                             DAGCombinerInfo &DCI) const {
375   SelectionDAG &DAG = DCI.DAG;
376   DebugLoc DL = N->getDebugLoc();
377   EVT VT = N->getValueType(0);
378
379   switch (N->getOpcode()) {
380     default: break;
381     case ISD::SELECT_CC: {
382       N->dump();
383       ConstantSDNode *True, *False;
384       // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
385       if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
386           && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
387           && True->isAllOnesValue()
388           && False->isNullValue()
389           && VT == MVT::i1) {
390         return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
391                            N->getOperand(1), N->getOperand(4));
392
393       }
394       break;
395     }
396     case ISD::SETCC: {
397       SDValue Arg0 = N->getOperand(0);
398       SDValue Arg1 = N->getOperand(1);
399       SDValue CC = N->getOperand(2);
400       ConstantSDNode * C = NULL;
401       ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
402
403       // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
404       if (VT == MVT::i1
405           && Arg0.getOpcode() == ISD::SIGN_EXTEND
406           && Arg0.getOperand(0).getValueType() == MVT::i1
407           && (C = dyn_cast<ConstantSDNode>(Arg1))
408           && C->isNullValue()
409           && CCOp == ISD::SETNE) {
410         return SimplifySetCC(VT, Arg0.getOperand(0),
411                              DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
412       }
413       break;
414     }
415   }
416   return SDValue();
417 }