1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Custom DAG lowering for SI
13 //===----------------------------------------------------------------------===//
15 #include "SIISelLowering.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"
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);
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);
42 computeRegisterProperties();
44 setOperationAction(ISD::ADD, MVT::i64, Legal);
45 setOperationAction(ISD::ADD, MVT::i32, Legal);
47 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
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);
54 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
55 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
57 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
58 setTargetDAGCombine(ISD::SELECT_CC);
60 setTargetDAGCombine(ISD::SETCC);
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;
69 switch (MI->getOpcode()) {
71 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
72 case AMDGPU::BRANCH: return BB;
73 case AMDGPU::CLAMP_SI:
74 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
75 .addOperand(MI->getOperand(0))
76 .addOperand(MI->getOperand(1))
82 MI->eraseFromParent();
86 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
87 .addOperand(MI->getOperand(0))
88 .addOperand(MI->getOperand(1))
94 MI->eraseFromParent();
98 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_ADD_F32_e64))
99 .addOperand(MI->getOperand(0))
100 .addOperand(MI->getOperand(1))
101 .addImm(0x80) // SRC1
106 MI->eraseFromParent();
108 case AMDGPU::SHADER_TYPE:
109 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
110 MI->getOperand(0).getImm();
111 MI->eraseFromParent();
114 case AMDGPU::SI_INTERP:
115 LowerSI_INTERP(MI, *BB, I, MRI);
118 LowerSI_WQM(MI, *BB, I, MRI);
120 case AMDGPU::SI_V_CNDLT:
121 LowerSI_V_CNDLT(MI, *BB, I, MRI);
127 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
128 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
129 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
130 .addReg(AMDGPU::EXEC);
132 MI->eraseFromParent();
135 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
136 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
137 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
138 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
139 MachineOperand dst = MI->getOperand(0);
140 MachineOperand iReg = MI->getOperand(1);
141 MachineOperand jReg = MI->getOperand(2);
142 MachineOperand attr_chan = MI->getOperand(3);
143 MachineOperand attr = MI->getOperand(4);
144 MachineOperand params = MI->getOperand(5);
146 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
149 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
151 .addOperand(attr_chan)
155 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
159 .addOperand(attr_chan)
163 MI->eraseFromParent();
166 void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
167 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
168 unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
170 BuildMI(BB, I, BB.findDebugLoc(I),
171 TII->get(AMDGPU::V_CMP_GT_F32_e32),
174 .addOperand(MI->getOperand(1));
176 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
177 .addOperand(MI->getOperand(0))
178 .addOperand(MI->getOperand(3))
179 .addOperand(MI->getOperand(2))
182 MI->eraseFromParent();
185 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
189 //===----------------------------------------------------------------------===//
190 // Custom DAG Lowering Operations
191 //===----------------------------------------------------------------------===//
193 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
194 switch (Op.getOpcode()) {
195 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
196 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
197 case ISD::LOAD: return LowerLOAD(Op, DAG);
198 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
199 case ISD::INTRINSIC_WO_CHAIN: {
200 unsigned IntrinsicID =
201 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
202 EVT VT = Op.getValueType();
203 switch (IntrinsicID) {
204 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
205 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
207 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
215 /// \brief Helper function for LowerBRCOND
216 static SDNode *findUser(SDValue Value, unsigned Opcode) {
218 SDNode *Parent = Value.getNode();
219 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
222 if (I.getUse().get() != Value)
225 if (I->getOpcode() == Opcode)
231 /// This transforms the control flow intrinsics to get the branch destination as
232 /// last parameter, also switches branch target with BR if the need arise
233 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
234 SelectionDAG &DAG) const {
236 DebugLoc DL = BRCOND.getDebugLoc();
238 SDNode *Intr = BRCOND.getOperand(1).getNode();
239 SDValue Target = BRCOND.getOperand(2);
242 if (Intr->getOpcode() == ISD::SETCC) {
243 // As long as we negate the condition everything is fine
244 SDNode *SetCC = Intr;
245 assert(SetCC->getConstantOperandVal(1) == 1);
246 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
248 Intr = SetCC->getOperand(0).getNode();
251 // Get the target from BR if we don't negate the condition
252 BR = findUser(BRCOND, ISD::BR);
253 Target = BR->getOperand(1);
256 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
258 // Build the result and
259 SmallVector<EVT, 4> Res;
260 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
261 Res.push_back(Intr->getValueType(i));
263 // operands of the new intrinsic call
264 SmallVector<SDValue, 4> Ops;
265 Ops.push_back(BRCOND.getOperand(0));
266 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
267 Ops.push_back(Intr->getOperand(i));
268 Ops.push_back(Target);
270 // build the new intrinsic call
271 SDNode *Result = DAG.getNode(
272 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
273 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
276 // Give the branch instruction our target
281 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
284 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
286 // Copy the intrinsic results to registers
287 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
288 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
292 Chain = DAG.getCopyToReg(
294 CopyToReg->getOperand(1),
295 SDValue(Result, i - 1),
298 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
301 // Remove the old intrinsic from the chain
302 DAG.ReplaceAllUsesOfValueWith(
303 SDValue(Intr, Intr->getNumValues() - 1),
304 Intr->getOperand(0));
309 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
310 EVT VT = Op.getValueType();
311 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
315 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
317 // We only need to lower USER_SGPR address space loads
318 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
322 // Loads from the USER_SGPR address space can only have constant value
324 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
327 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
328 const TargetRegisterClass * dstClass;
329 switch (TypeDwordWidth) {
331 assert(!"USER_SGPR value size not implemented");
334 dstClass = &AMDGPU::SReg_32RegClass;
337 dstClass = &AMDGPU::SReg_64RegClass;
340 uint64_t Index = BasePtr->getZExtValue();
341 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
342 unsigned SGPRIndex = Index / TypeDwordWidth;
343 unsigned Reg = dstClass->getRegister(SGPRIndex);
345 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
350 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
351 SDValue LHS = Op.getOperand(0);
352 SDValue RHS = Op.getOperand(1);
353 SDValue True = Op.getOperand(2);
354 SDValue False = Op.getOperand(3);
355 SDValue CC = Op.getOperand(4);
356 EVT VT = Op.getValueType();
357 DebugLoc DL = Op.getDebugLoc();
359 // Possible Min/Max pattern
360 SDValue MinMax = LowerMinMax(Op, DAG);
361 if (MinMax.getNode()) {
365 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
366 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
369 //===----------------------------------------------------------------------===//
370 // Custom DAG optimizations
371 //===----------------------------------------------------------------------===//
373 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
374 DAGCombinerInfo &DCI) const {
375 SelectionDAG &DAG = DCI.DAG;
376 DebugLoc DL = N->getDebugLoc();
377 EVT VT = N->getValueType(0);
379 switch (N->getOpcode()) {
381 case ISD::SELECT_CC: {
383 ConstantSDNode *True, *False;
384 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
385 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
386 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
387 && True->isAllOnesValue()
388 && False->isNullValue()
390 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
391 N->getOperand(1), N->getOperand(4));
397 SDValue Arg0 = N->getOperand(0);
398 SDValue Arg1 = N->getOperand(1);
399 SDValue CC = N->getOperand(2);
400 ConstantSDNode * C = NULL;
401 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
403 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
405 && Arg0.getOpcode() == ISD::SIGN_EXTEND
406 && Arg0.getOperand(0).getValueType() == MVT::i1
407 && (C = dyn_cast<ConstantSDNode>(Arg1))
409 && CCOp == ISD::SETNE) {
410 return SimplifySetCC(VT, Arg0.getOperand(0),
411 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);