4c672ca7c1f9ed79c7fb9a0dd9a7ac11a67da02a
[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::SCCRegRegClass);
35   addRegisterClass(MVT::i1, &AMDGPU::VCCRegRegClass);
36
37   addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
38   addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
39
40   computeRegisterProperties();
41
42   setOperationAction(ISD::AND, MVT::i1, Custom);
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   if (TII->get(MI->getOpcode()).TSFlags & SIInstrFlags::NEED_WAIT) {
70     AppendS_WAITCNT(MI, *BB, llvm::next(I));
71     return BB;
72   }
73
74   switch (MI->getOpcode()) {
75   default:
76     return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
77   case AMDGPU::BRANCH: return BB;
78   case AMDGPU::CLAMP_SI:
79     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
80            .addOperand(MI->getOperand(0))
81            .addOperand(MI->getOperand(1))
82            // VSRC1-2 are unused, but we still need to fill all the
83            // operand slots, so we just reuse the VSRC0 operand
84            .addOperand(MI->getOperand(1))
85            .addOperand(MI->getOperand(1))
86            .addImm(0) // ABS
87            .addImm(1) // CLAMP
88            .addImm(0) // OMOD
89            .addImm(0); // NEG
90     MI->eraseFromParent();
91     break;
92
93   case AMDGPU::FABS_SI:
94     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
95                  .addOperand(MI->getOperand(0))
96                  .addOperand(MI->getOperand(1))
97                  // VSRC1-2 are unused, but we still need to fill all the
98                  // operand slots, so we just reuse the VSRC0 operand
99                  .addOperand(MI->getOperand(1))
100                  .addOperand(MI->getOperand(1))
101                  .addImm(1) // ABS
102                  .addImm(0) // CLAMP
103                  .addImm(0) // OMOD
104                  .addImm(0); // NEG
105     MI->eraseFromParent();
106     break;
107
108   case AMDGPU::FNEG_SI:
109     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
110                  .addOperand(MI->getOperand(0))
111                  .addOperand(MI->getOperand(1))
112                  // VSRC1-2 are unused, but we still need to fill all the
113                  // operand slots, so we just reuse the VSRC0 operand
114                  .addOperand(MI->getOperand(1))
115                  .addOperand(MI->getOperand(1))
116                  .addImm(0) // ABS
117                  .addImm(0) // CLAMP
118                  .addImm(0) // OMOD
119                  .addImm(1); // NEG
120     MI->eraseFromParent();
121     break;
122   case AMDGPU::SHADER_TYPE:
123     BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
124                                         MI->getOperand(0).getImm();
125     MI->eraseFromParent();
126     break;
127
128   case AMDGPU::SI_INTERP:
129     LowerSI_INTERP(MI, *BB, I, MRI);
130     break;
131   case AMDGPU::SI_INTERP_CONST:
132     LowerSI_INTERP_CONST(MI, *BB, I, MRI);
133     break;
134   case AMDGPU::SI_KIL:
135     LowerSI_KIL(MI, *BB, I, MRI);
136     break;
137   case AMDGPU::SI_WQM:
138     LowerSI_WQM(MI, *BB, I, MRI);
139     break;
140   case AMDGPU::SI_V_CNDLT:
141     LowerSI_V_CNDLT(MI, *BB, I, MRI);
142     break;
143   }
144   return BB;
145 }
146
147 void SITargetLowering::AppendS_WAITCNT(MachineInstr *MI, MachineBasicBlock &BB,
148     MachineBasicBlock::iterator I) const {
149   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WAITCNT))
150           .addImm(0);
151 }
152
153
154 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
155     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
156   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
157           .addReg(AMDGPU::EXEC);
158
159   MI->eraseFromParent();
160 }
161
162 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
163     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
164   unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
165   unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
166   MachineOperand dst = MI->getOperand(0);
167   MachineOperand iReg = MI->getOperand(1);
168   MachineOperand jReg = MI->getOperand(2);
169   MachineOperand attr_chan = MI->getOperand(3);
170   MachineOperand attr = MI->getOperand(4);
171   MachineOperand params = MI->getOperand(5);
172
173   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
174           .addOperand(params);
175
176   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
177           .addOperand(iReg)
178           .addOperand(attr_chan)
179           .addOperand(attr)
180           .addReg(M0);
181
182   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
183           .addOperand(dst)
184           .addReg(tmp)
185           .addOperand(jReg)
186           .addOperand(attr_chan)
187           .addOperand(attr)
188           .addReg(M0);
189
190   MI->eraseFromParent();
191 }
192
193 void SITargetLowering::LowerSI_INTERP_CONST(MachineInstr *MI,
194     MachineBasicBlock &BB, MachineBasicBlock::iterator I,
195     MachineRegisterInfo &MRI) const {
196   MachineOperand dst = MI->getOperand(0);
197   MachineOperand attr_chan = MI->getOperand(1);
198   MachineOperand attr = MI->getOperand(2);
199   MachineOperand params = MI->getOperand(3);
200   unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
201
202   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
203           .addOperand(params);
204
205   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_MOV_F32))
206           .addOperand(dst)
207           .addOperand(attr_chan)
208           .addOperand(attr)
209           .addReg(M0);
210
211   MI->eraseFromParent();
212 }
213
214 void SITargetLowering::LowerSI_KIL(MachineInstr *MI, MachineBasicBlock &BB,
215     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
216   // Clear this pixel from the exec mask if the operand is negative
217   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CMPX_LE_F32_e32),
218           AMDGPU::VCC)
219           .addReg(AMDGPU::SREG_LIT_0)
220           .addOperand(MI->getOperand(0));
221
222   MI->eraseFromParent();
223 }
224
225 void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
226     MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
227   unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
228
229   BuildMI(BB, I, BB.findDebugLoc(I),
230           TII->get(AMDGPU::V_CMP_GT_F32_e32),
231           VCC)
232           .addReg(AMDGPU::SREG_LIT_0)
233           .addOperand(MI->getOperand(1));
234
235   BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
236           .addOperand(MI->getOperand(0))
237           .addOperand(MI->getOperand(3))
238           .addOperand(MI->getOperand(2))
239           .addReg(VCC);
240
241   MI->eraseFromParent();
242 }
243
244 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
245   return MVT::i1;
246 }
247
248 //===----------------------------------------------------------------------===//
249 // Custom DAG Lowering Operations
250 //===----------------------------------------------------------------------===//
251
252 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
253   switch (Op.getOpcode()) {
254   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
255   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
256   case ISD::LOAD: return LowerLOAD(Op, DAG);
257   case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
258   case ISD::AND: return Loweri1ContextSwitch(Op, DAG, ISD::AND);
259   case ISD::INTRINSIC_WO_CHAIN: {
260     unsigned IntrinsicID =
261                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
262     EVT VT = Op.getValueType();
263     switch (IntrinsicID) {
264     case AMDGPUIntrinsic::SI_vs_load_buffer_index:
265       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
266                                   AMDGPU::VGPR0, VT);
267     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
268     }
269     break;
270   }
271   }
272   return SDValue();
273 }
274
275 /// \brief The function is for lowering i1 operations on the
276 /// VCC register.
277 ///
278 /// In the VALU context, VCC is a one bit register, but in the
279 /// SALU context the VCC is a 64-bit register (1-bit per thread).  Since only
280 /// the SALU can perform operations on the VCC register, we need to promote
281 /// the operand types from i1 to i64 in order for tablegen to be able to match
282 /// this operation to the correct SALU instruction.  We do this promotion by
283 /// wrapping the operands in a CopyToReg node.
284 ///
285 SDValue SITargetLowering::Loweri1ContextSwitch(SDValue Op,
286                                                SelectionDAG &DAG,
287                                                unsigned VCCNode) const {
288   DebugLoc DL = Op.getDebugLoc();
289
290   SDValue OpNode = DAG.getNode(VCCNode, DL, MVT::i64,
291                                DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
292                                            Op.getOperand(0)),
293                                DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
294                                            Op.getOperand(1)));
295
296   return DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i1, OpNode);
297 }
298
299 /// \brief Helper function for LowerBRCOND
300 static SDNode *findUser(SDValue Value, unsigned Opcode) {
301
302   SDNode *Parent = Value.getNode();
303   for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
304        I != E; ++I) {
305
306     if (I.getUse().get() != Value)
307       continue;
308
309     if (I->getOpcode() == Opcode)
310       return *I;
311   }
312   return 0;
313 }
314
315 /// This transforms the control flow intrinsics to get the branch destination as
316 /// last parameter, also switches branch target with BR if the need arise
317 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
318                                       SelectionDAG &DAG) const {
319
320   DebugLoc DL = BRCOND.getDebugLoc();
321
322   SDNode *Intr = BRCOND.getOperand(1).getNode();
323   SDValue Target = BRCOND.getOperand(2);
324   SDNode *BR = 0;
325
326   if (Intr->getOpcode() == ISD::SETCC) {
327     // As long as we negate the condition everything is fine
328     SDNode *SetCC = Intr;
329     assert(SetCC->getConstantOperandVal(1) == 1);
330     assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
331            ISD::SETNE);
332     Intr = SetCC->getOperand(0).getNode();
333
334   } else {
335     // Get the target from BR if we don't negate the condition
336     BR = findUser(BRCOND, ISD::BR);
337     Target = BR->getOperand(1);
338   }
339
340   assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
341
342   // Build the result and
343   SmallVector<EVT, 4> Res;
344   for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
345     Res.push_back(Intr->getValueType(i));
346
347   // operands of the new intrinsic call
348   SmallVector<SDValue, 4> Ops;
349   Ops.push_back(BRCOND.getOperand(0));
350   for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
351     Ops.push_back(Intr->getOperand(i));
352   Ops.push_back(Target);
353
354   // build the new intrinsic call
355   SDNode *Result = DAG.getNode(
356     Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
357     DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
358
359   if (BR) {
360     // Give the branch instruction our target
361     SDValue Ops[] = {
362       BR->getOperand(0),
363       BRCOND.getOperand(2)
364     };
365     DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
366   }
367
368   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
369
370   // Copy the intrinsic results to registers
371   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
372     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
373     if (!CopyToReg)
374       continue;
375
376     Chain = DAG.getCopyToReg(
377       Chain, DL,
378       CopyToReg->getOperand(1),
379       SDValue(Result, i - 1),
380       SDValue());
381
382     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
383   }
384
385   // Remove the old intrinsic from the chain
386   DAG.ReplaceAllUsesOfValueWith(
387     SDValue(Intr, Intr->getNumValues() - 1),
388     Intr->getOperand(0));
389
390   return Chain;
391 }
392
393 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
394   EVT VT = Op.getValueType();
395   LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
396
397   assert(Ptr);
398
399   unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
400
401   // We only need to lower USER_SGPR address space loads
402   if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
403     return SDValue();
404   }
405
406   // Loads from the USER_SGPR address space can only have constant value
407   // pointers.
408   ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
409   assert(BasePtr);
410
411   unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
412   const TargetRegisterClass * dstClass;
413   switch (TypeDwordWidth) {
414     default:
415       assert(!"USER_SGPR value size not implemented");
416       return SDValue();
417     case 1:
418       dstClass = &AMDGPU::SReg_32RegClass;
419       break;
420     case 2:
421       dstClass = &AMDGPU::SReg_64RegClass;
422       break;
423   }
424   uint64_t Index = BasePtr->getZExtValue();
425   assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
426   unsigned SGPRIndex = Index / TypeDwordWidth;
427   unsigned Reg = dstClass->getRegister(SGPRIndex);
428
429   DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
430                                                          VT));
431   return SDValue();
432 }
433
434 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
435   SDValue LHS = Op.getOperand(0);
436   SDValue RHS = Op.getOperand(1);
437   SDValue True = Op.getOperand(2);
438   SDValue False = Op.getOperand(3);
439   SDValue CC = Op.getOperand(4);
440   EVT VT = Op.getValueType();
441   DebugLoc DL = Op.getDebugLoc();
442
443   // Possible Min/Max pattern
444   SDValue MinMax = LowerMinMax(Op, DAG);
445   if (MinMax.getNode()) {
446     return MinMax;
447   }
448
449   SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
450   return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
451 }
452
453 //===----------------------------------------------------------------------===//
454 // Custom DAG optimizations
455 //===----------------------------------------------------------------------===//
456
457 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
458                                             DAGCombinerInfo &DCI) const {
459   SelectionDAG &DAG = DCI.DAG;
460   DebugLoc DL = N->getDebugLoc();
461   EVT VT = N->getValueType(0);
462
463   switch (N->getOpcode()) {
464     default: break;
465     case ISD::SELECT_CC: {
466       N->dump();
467       ConstantSDNode *True, *False;
468       // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
469       if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
470           && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
471           && True->isAllOnesValue()
472           && False->isNullValue()
473           && VT == MVT::i1) {
474         return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
475                            N->getOperand(1), N->getOperand(4));
476
477       }
478       break;
479     }
480     case ISD::SETCC: {
481       SDValue Arg0 = N->getOperand(0);
482       SDValue Arg1 = N->getOperand(1);
483       SDValue CC = N->getOperand(2);
484       ConstantSDNode * C = NULL;
485       ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
486
487       // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
488       if (VT == MVT::i1
489           && Arg0.getOpcode() == ISD::SIGN_EXTEND
490           && Arg0.getOperand(0).getValueType() == MVT::i1
491           && (C = dyn_cast<ConstantSDNode>(Arg1))
492           && C->isNullValue()
493           && CCOp == ISD::SETNE) {
494         return SimplifySetCC(VT, Arg0.getOperand(0),
495                              DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
496       }
497       break;
498     }
499   }
500   return SDValue();
501 }
502
503 #define NODE_NAME_CASE(node) case SIISD::node: return #node;
504
505 const char* SITargetLowering::getTargetNodeName(unsigned Opcode) const {
506   switch (Opcode) {
507   default: return AMDGPUTargetLowering::getTargetNodeName(Opcode);
508   NODE_NAME_CASE(VCC_AND)
509   NODE_NAME_CASE(VCC_BITCAST)
510   }
511 }