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 MachineRegisterInfo & MRI = BB->getParent()->getRegInfo();
66 MachineBasicBlock::iterator I = MI;
68 switch (MI->getOpcode()) {
70 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
71 case AMDGPU::BRANCH: return BB;
72 case AMDGPU::SHADER_TYPE:
73 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
74 MI->getOperand(0).getImm();
75 MI->eraseFromParent();
78 case AMDGPU::SI_INTERP:
79 LowerSI_INTERP(MI, *BB, I, MRI);
82 LowerSI_WQM(MI, *BB, I, MRI);
84 case AMDGPU::SI_V_CNDLT:
85 LowerSI_V_CNDLT(MI, *BB, I, MRI);
91 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
92 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
93 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
94 .addReg(AMDGPU::EXEC);
96 MI->eraseFromParent();
99 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
100 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
101 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
102 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
103 MachineOperand dst = MI->getOperand(0);
104 MachineOperand iReg = MI->getOperand(1);
105 MachineOperand jReg = MI->getOperand(2);
106 MachineOperand attr_chan = MI->getOperand(3);
107 MachineOperand attr = MI->getOperand(4);
108 MachineOperand params = MI->getOperand(5);
110 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
113 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
115 .addOperand(attr_chan)
119 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
123 .addOperand(attr_chan)
127 MI->eraseFromParent();
130 void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
131 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
132 unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
134 BuildMI(BB, I, BB.findDebugLoc(I),
135 TII->get(AMDGPU::V_CMP_GT_F32_e32),
138 .addOperand(MI->getOperand(1));
140 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
141 .addOperand(MI->getOperand(0))
142 .addOperand(MI->getOperand(3))
143 .addOperand(MI->getOperand(2))
146 MI->eraseFromParent();
149 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
153 //===----------------------------------------------------------------------===//
154 // Custom DAG Lowering Operations
155 //===----------------------------------------------------------------------===//
157 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
158 switch (Op.getOpcode()) {
159 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
160 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
161 case ISD::LOAD: return LowerLOAD(Op, DAG);
162 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
163 case ISD::INTRINSIC_WO_CHAIN: {
164 unsigned IntrinsicID =
165 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
166 EVT VT = Op.getValueType();
167 switch (IntrinsicID) {
168 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
169 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
171 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
179 /// \brief Helper function for LowerBRCOND
180 static SDNode *findUser(SDValue Value, unsigned Opcode) {
182 SDNode *Parent = Value.getNode();
183 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
186 if (I.getUse().get() != Value)
189 if (I->getOpcode() == Opcode)
195 /// This transforms the control flow intrinsics to get the branch destination as
196 /// last parameter, also switches branch target with BR if the need arise
197 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
198 SelectionDAG &DAG) const {
200 DebugLoc DL = BRCOND.getDebugLoc();
202 SDNode *Intr = BRCOND.getOperand(1).getNode();
203 SDValue Target = BRCOND.getOperand(2);
206 if (Intr->getOpcode() == ISD::SETCC) {
207 // As long as we negate the condition everything is fine
208 SDNode *SetCC = Intr;
209 assert(SetCC->getConstantOperandVal(1) == 1);
210 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
212 Intr = SetCC->getOperand(0).getNode();
215 // Get the target from BR if we don't negate the condition
216 BR = findUser(BRCOND, ISD::BR);
217 Target = BR->getOperand(1);
220 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
222 // Build the result and
223 SmallVector<EVT, 4> Res;
224 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
225 Res.push_back(Intr->getValueType(i));
227 // operands of the new intrinsic call
228 SmallVector<SDValue, 4> Ops;
229 Ops.push_back(BRCOND.getOperand(0));
230 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
231 Ops.push_back(Intr->getOperand(i));
232 Ops.push_back(Target);
234 // build the new intrinsic call
235 SDNode *Result = DAG.getNode(
236 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
237 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
240 // Give the branch instruction our target
245 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
248 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
250 // Copy the intrinsic results to registers
251 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
252 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
256 Chain = DAG.getCopyToReg(
258 CopyToReg->getOperand(1),
259 SDValue(Result, i - 1),
262 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
265 // Remove the old intrinsic from the chain
266 DAG.ReplaceAllUsesOfValueWith(
267 SDValue(Intr, Intr->getNumValues() - 1),
268 Intr->getOperand(0));
273 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
274 EVT VT = Op.getValueType();
275 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
279 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
281 // We only need to lower USER_SGPR address space loads
282 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
286 // Loads from the USER_SGPR address space can only have constant value
288 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
291 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
292 const TargetRegisterClass * dstClass;
293 switch (TypeDwordWidth) {
295 assert(!"USER_SGPR value size not implemented");
298 dstClass = &AMDGPU::SReg_32RegClass;
301 dstClass = &AMDGPU::SReg_64RegClass;
304 uint64_t Index = BasePtr->getZExtValue();
305 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
306 unsigned SGPRIndex = Index / TypeDwordWidth;
307 unsigned Reg = dstClass->getRegister(SGPRIndex);
309 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
314 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
315 SDValue LHS = Op.getOperand(0);
316 SDValue RHS = Op.getOperand(1);
317 SDValue True = Op.getOperand(2);
318 SDValue False = Op.getOperand(3);
319 SDValue CC = Op.getOperand(4);
320 EVT VT = Op.getValueType();
321 DebugLoc DL = Op.getDebugLoc();
323 // Possible Min/Max pattern
324 SDValue MinMax = LowerMinMax(Op, DAG);
325 if (MinMax.getNode()) {
329 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
330 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
333 //===----------------------------------------------------------------------===//
334 // Custom DAG optimizations
335 //===----------------------------------------------------------------------===//
337 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
338 DAGCombinerInfo &DCI) const {
339 SelectionDAG &DAG = DCI.DAG;
340 DebugLoc DL = N->getDebugLoc();
341 EVT VT = N->getValueType(0);
343 switch (N->getOpcode()) {
345 case ISD::SELECT_CC: {
347 ConstantSDNode *True, *False;
348 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
349 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
350 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
351 && True->isAllOnesValue()
352 && False->isNullValue()
354 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
355 N->getOperand(1), N->getOperand(4));
361 SDValue Arg0 = N->getOperand(0);
362 SDValue Arg1 = N->getOperand(1);
363 SDValue CC = N->getOperand(2);
364 ConstantSDNode * C = NULL;
365 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
367 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
369 && Arg0.getOpcode() == ISD::SIGN_EXTEND
370 && Arg0.getOperand(0).getValueType() == MVT::i1
371 && (C = dyn_cast<ConstantSDNode>(Arg1))
373 && CCOp == ISD::SETNE) {
374 return SimplifySetCC(VT, Arg0.getOperand(0),
375 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);