cd6e0e991607129d9bd3715a753b20466de190c3
[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
331     CondCodeSDNode *CC = cast<CondCodeSDNode>(SetCC->getOperand(2).getNode());
332     assert(CC->get() == ISD::SETNE);
333     Intr = SetCC->getOperand(0).getNode();
334
335   } else {
336     // Get the target from BR if we don't negate the condition
337     BR = findUser(BRCOND, ISD::BR);
338     Target = BR->getOperand(1);
339   }
340
341   assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
342
343   // Build the result and
344   SmallVector<EVT, 4> Res;
345   for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
346     Res.push_back(Intr->getValueType(i));
347
348   // operands of the new intrinsic call
349   SmallVector<SDValue, 4> Ops;
350   Ops.push_back(BRCOND.getOperand(0));
351   for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
352     Ops.push_back(Intr->getOperand(i));
353   Ops.push_back(Target);
354
355   // build the new intrinsic call
356   SDNode *Result = DAG.getNode(
357     Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
358     DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
359
360   if (BR) {
361     // Give the branch instruction our target
362     SDValue Ops[] = {
363       BR->getOperand(0),
364       BRCOND.getOperand(2)
365     };
366     DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
367   }
368
369   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
370
371   // Copy the intrinsic results to registers
372   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
373     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
374     if (!CopyToReg)
375       continue;
376
377     Chain = DAG.getCopyToReg(
378       Chain, DL,
379       CopyToReg->getOperand(1),
380       SDValue(Result, i - 1),
381       SDValue());
382
383     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
384   }
385
386   // Remove the old intrinsic from the chain
387   DAG.ReplaceAllUsesOfValueWith(
388     SDValue(Intr, Intr->getNumValues() - 1),
389     Intr->getOperand(0));
390
391   return Chain;
392 }
393
394 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
395   EVT VT = Op.getValueType();
396   LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
397
398   assert(Ptr);
399
400   unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
401
402   // We only need to lower USER_SGPR address space loads
403   if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
404     return SDValue();
405   }
406
407   // Loads from the USER_SGPR address space can only have constant value
408   // pointers.
409   ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
410   assert(BasePtr);
411
412   unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
413   const TargetRegisterClass * dstClass;
414   switch (TypeDwordWidth) {
415     default:
416       assert(!"USER_SGPR value size not implemented");
417       return SDValue();
418     case 1:
419       dstClass = &AMDGPU::SReg_32RegClass;
420       break;
421     case 2:
422       dstClass = &AMDGPU::SReg_64RegClass;
423       break;
424   }
425   uint64_t Index = BasePtr->getZExtValue();
426   assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
427   unsigned SGPRIndex = Index / TypeDwordWidth;
428   unsigned Reg = dstClass->getRegister(SGPRIndex);
429
430   DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
431                                                          VT));
432   return SDValue();
433 }
434
435 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
436   SDValue LHS = Op.getOperand(0);
437   SDValue RHS = Op.getOperand(1);
438   SDValue True = Op.getOperand(2);
439   SDValue False = Op.getOperand(3);
440   SDValue CC = Op.getOperand(4);
441   EVT VT = Op.getValueType();
442   DebugLoc DL = Op.getDebugLoc();
443
444   // Possible Min/Max pattern
445   SDValue MinMax = LowerMinMax(Op, DAG);
446   if (MinMax.getNode()) {
447     return MinMax;
448   }
449
450   SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
451   return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
452 }
453
454 //===----------------------------------------------------------------------===//
455 // Custom DAG optimizations
456 //===----------------------------------------------------------------------===//
457
458 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
459                                             DAGCombinerInfo &DCI) const {
460   SelectionDAG &DAG = DCI.DAG;
461   DebugLoc DL = N->getDebugLoc();
462   EVT VT = N->getValueType(0);
463
464   switch (N->getOpcode()) {
465     default: break;
466     case ISD::SELECT_CC: {
467       N->dump();
468       ConstantSDNode *True, *False;
469       // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
470       if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
471           && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
472           && True->isAllOnesValue()
473           && False->isNullValue()
474           && VT == MVT::i1) {
475         return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
476                            N->getOperand(1), N->getOperand(4));
477
478       }
479       break;
480     }
481     case ISD::SETCC: {
482       SDValue Arg0 = N->getOperand(0);
483       SDValue Arg1 = N->getOperand(1);
484       SDValue CC = N->getOperand(2);
485       ConstantSDNode * C = NULL;
486       ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
487
488       // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
489       if (VT == MVT::i1
490           && Arg0.getOpcode() == ISD::SIGN_EXTEND
491           && Arg0.getOperand(0).getValueType() == MVT::i1
492           && (C = dyn_cast<ConstantSDNode>(Arg1))
493           && C->isNullValue()
494           && CCOp == ISD::SETNE) {
495         return SimplifySetCC(VT, Arg0.getOperand(0),
496                              DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
497       }
498       break;
499     }
500   }
501   return SDValue();
502 }
503
504 #define NODE_NAME_CASE(node) case SIISD::node: return #node;
505
506 const char* SITargetLowering::getTargetNodeName(unsigned Opcode) const {
507   switch (Opcode) {
508   default: return AMDGPUTargetLowering::getTargetNodeName(Opcode);
509   NODE_NAME_CASE(VCC_AND)
510   NODE_NAME_CASE(VCC_BITCAST)
511   }
512 }