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