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