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