R600/SI: add folding helper
[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   MachineRegisterInfo & MRI = BB->getParent()->getRegInfo();
66   MachineBasicBlock::iterator I = MI;
67
68   switch (MI->getOpcode()) {
69   default:
70     return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
71   case AMDGPU::BRANCH: return BB;
72   case AMDGPU::SHADER_TYPE:
73     BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
74                                         MI->getOperand(0).getImm();
75     MI->eraseFromParent();
76     break;
77
78   case AMDGPU::SI_INTERP:
79     LowerSI_INTERP(MI, *BB, I, MRI);
80     break;
81   case AMDGPU::SI_WQM:
82     LowerSI_WQM(MI, *BB, I, MRI);
83     break;
84   }
85   return BB;
86 }
87
88 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
89     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
90   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
91           .addReg(AMDGPU::EXEC);
92
93   MI->eraseFromParent();
94 }
95
96 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
97     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
98   unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
99   unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
100   MachineOperand dst = MI->getOperand(0);
101   MachineOperand iReg = MI->getOperand(1);
102   MachineOperand jReg = MI->getOperand(2);
103   MachineOperand attr_chan = MI->getOperand(3);
104   MachineOperand attr = MI->getOperand(4);
105   MachineOperand params = MI->getOperand(5);
106
107   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
108           .addOperand(params);
109
110   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
111           .addOperand(iReg)
112           .addOperand(attr_chan)
113           .addOperand(attr)
114           .addReg(M0);
115
116   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
117           .addOperand(dst)
118           .addReg(tmp)
119           .addOperand(jReg)
120           .addOperand(attr_chan)
121           .addOperand(attr)
122           .addReg(M0);
123
124   MI->eraseFromParent();
125 }
126
127 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
128   return MVT::i1;
129 }
130
131 //===----------------------------------------------------------------------===//
132 // Custom DAG Lowering Operations
133 //===----------------------------------------------------------------------===//
134
135 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
136   switch (Op.getOpcode()) {
137   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
138   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
139   case ISD::LOAD: return LowerLOAD(Op, DAG);
140   case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
141   case ISD::INTRINSIC_WO_CHAIN: {
142     unsigned IntrinsicID =
143                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
144     EVT VT = Op.getValueType();
145     switch (IntrinsicID) {
146     case AMDGPUIntrinsic::SI_vs_load_buffer_index:
147       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
148                                   AMDGPU::VGPR0, VT);
149     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
150     }
151     break;
152   }
153   }
154   return SDValue();
155 }
156
157 /// \brief Helper function for LowerBRCOND
158 static SDNode *findUser(SDValue Value, unsigned Opcode) {
159
160   SDNode *Parent = Value.getNode();
161   for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
162        I != E; ++I) {
163
164     if (I.getUse().get() != Value)
165       continue;
166
167     if (I->getOpcode() == Opcode)
168       return *I;
169   }
170   return 0;
171 }
172
173 /// This transforms the control flow intrinsics to get the branch destination as
174 /// last parameter, also switches branch target with BR if the need arise
175 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
176                                       SelectionDAG &DAG) const {
177
178   DebugLoc DL = BRCOND.getDebugLoc();
179
180   SDNode *Intr = BRCOND.getOperand(1).getNode();
181   SDValue Target = BRCOND.getOperand(2);
182   SDNode *BR = 0;
183
184   if (Intr->getOpcode() == ISD::SETCC) {
185     // As long as we negate the condition everything is fine
186     SDNode *SetCC = Intr;
187     assert(SetCC->getConstantOperandVal(1) == 1);
188     assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
189            ISD::SETNE);
190     Intr = SetCC->getOperand(0).getNode();
191
192   } else {
193     // Get the target from BR if we don't negate the condition
194     BR = findUser(BRCOND, ISD::BR);
195     Target = BR->getOperand(1);
196   }
197
198   assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
199
200   // Build the result and
201   SmallVector<EVT, 4> Res;
202   for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
203     Res.push_back(Intr->getValueType(i));
204
205   // operands of the new intrinsic call
206   SmallVector<SDValue, 4> Ops;
207   Ops.push_back(BRCOND.getOperand(0));
208   for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
209     Ops.push_back(Intr->getOperand(i));
210   Ops.push_back(Target);
211
212   // build the new intrinsic call
213   SDNode *Result = DAG.getNode(
214     Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
215     DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
216
217   if (BR) {
218     // Give the branch instruction our target
219     SDValue Ops[] = {
220       BR->getOperand(0),
221       BRCOND.getOperand(2)
222     };
223     DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
224   }
225
226   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
227
228   // Copy the intrinsic results to registers
229   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
230     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
231     if (!CopyToReg)
232       continue;
233
234     Chain = DAG.getCopyToReg(
235       Chain, DL,
236       CopyToReg->getOperand(1),
237       SDValue(Result, i - 1),
238       SDValue());
239
240     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
241   }
242
243   // Remove the old intrinsic from the chain
244   DAG.ReplaceAllUsesOfValueWith(
245     SDValue(Intr, Intr->getNumValues() - 1),
246     Intr->getOperand(0));
247
248   return Chain;
249 }
250
251 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
252   EVT VT = Op.getValueType();
253   LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
254
255   assert(Ptr);
256
257   unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
258
259   // We only need to lower USER_SGPR address space loads
260   if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
261     return SDValue();
262   }
263
264   // Loads from the USER_SGPR address space can only have constant value
265   // pointers.
266   ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
267   assert(BasePtr);
268
269   unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
270   const TargetRegisterClass * dstClass;
271   switch (TypeDwordWidth) {
272     default:
273       assert(!"USER_SGPR value size not implemented");
274       return SDValue();
275     case 1:
276       dstClass = &AMDGPU::SReg_32RegClass;
277       break;
278     case 2:
279       dstClass = &AMDGPU::SReg_64RegClass;
280       break;
281   }
282   uint64_t Index = BasePtr->getZExtValue();
283   assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
284   unsigned SGPRIndex = Index / TypeDwordWidth;
285   unsigned Reg = dstClass->getRegister(SGPRIndex);
286
287   DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
288                                                          VT));
289   return SDValue();
290 }
291
292 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
293   SDValue LHS = Op.getOperand(0);
294   SDValue RHS = Op.getOperand(1);
295   SDValue True = Op.getOperand(2);
296   SDValue False = Op.getOperand(3);
297   SDValue CC = Op.getOperand(4);
298   EVT VT = Op.getValueType();
299   DebugLoc DL = Op.getDebugLoc();
300
301   // Possible Min/Max pattern
302   SDValue MinMax = LowerMinMax(Op, DAG);
303   if (MinMax.getNode()) {
304     return MinMax;
305   }
306
307   SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
308   return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
309 }
310
311 //===----------------------------------------------------------------------===//
312 // Custom DAG optimizations
313 //===----------------------------------------------------------------------===//
314
315 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
316                                             DAGCombinerInfo &DCI) const {
317   SelectionDAG &DAG = DCI.DAG;
318   DebugLoc DL = N->getDebugLoc();
319   EVT VT = N->getValueType(0);
320
321   switch (N->getOpcode()) {
322     default: break;
323     case ISD::SELECT_CC: {
324       N->dump();
325       ConstantSDNode *True, *False;
326       // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
327       if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
328           && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
329           && True->isAllOnesValue()
330           && False->isNullValue()
331           && VT == MVT::i1) {
332         return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
333                            N->getOperand(1), N->getOperand(4));
334
335       }
336       break;
337     }
338     case ISD::SETCC: {
339       SDValue Arg0 = N->getOperand(0);
340       SDValue Arg1 = N->getOperand(1);
341       SDValue CC = N->getOperand(2);
342       ConstantSDNode * C = NULL;
343       ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
344
345       // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
346       if (VT == MVT::i1
347           && Arg0.getOpcode() == ISD::SIGN_EXTEND
348           && Arg0.getOperand(0).getValueType() == MVT::i1
349           && (C = dyn_cast<ConstantSDNode>(Arg1))
350           && C->isNullValue()
351           && CCOp == ISD::SETNE) {
352         return SimplifySetCC(VT, Arg0.getOperand(0),
353                              DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
354       }
355       break;
356     }
357   }
358   return SDValue();
359 }
360
361 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
362                                           SelectionDAG &DAG) const {
363   // TODO: Implement immediate folding
364   return Node;
365 }