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);
88 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
89 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
90 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
91 .addReg(AMDGPU::EXEC);
93 MI->eraseFromParent();
96 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
97 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
98 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
99 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
100 MachineOperand dst = MI->getOperand(0);
101 MachineOperand iReg = MI->getOperand(1);
102 MachineOperand jReg = MI->getOperand(2);
103 MachineOperand attr_chan = MI->getOperand(3);
104 MachineOperand attr = MI->getOperand(4);
105 MachineOperand params = MI->getOperand(5);
107 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
110 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
112 .addOperand(attr_chan)
116 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
120 .addOperand(attr_chan)
124 MI->eraseFromParent();
127 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
131 //===----------------------------------------------------------------------===//
132 // Custom DAG Lowering Operations
133 //===----------------------------------------------------------------------===//
135 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
136 switch (Op.getOpcode()) {
137 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
138 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
139 case ISD::LOAD: return LowerLOAD(Op, DAG);
140 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
141 case ISD::INTRINSIC_WO_CHAIN: {
142 unsigned IntrinsicID =
143 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
144 EVT VT = Op.getValueType();
145 switch (IntrinsicID) {
146 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
147 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
149 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
157 /// \brief Helper function for LowerBRCOND
158 static SDNode *findUser(SDValue Value, unsigned Opcode) {
160 SDNode *Parent = Value.getNode();
161 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
164 if (I.getUse().get() != Value)
167 if (I->getOpcode() == Opcode)
173 /// This transforms the control flow intrinsics to get the branch destination as
174 /// last parameter, also switches branch target with BR if the need arise
175 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
176 SelectionDAG &DAG) const {
178 DebugLoc DL = BRCOND.getDebugLoc();
180 SDNode *Intr = BRCOND.getOperand(1).getNode();
181 SDValue Target = BRCOND.getOperand(2);
184 if (Intr->getOpcode() == ISD::SETCC) {
185 // As long as we negate the condition everything is fine
186 SDNode *SetCC = Intr;
187 assert(SetCC->getConstantOperandVal(1) == 1);
188 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
190 Intr = SetCC->getOperand(0).getNode();
193 // Get the target from BR if we don't negate the condition
194 BR = findUser(BRCOND, ISD::BR);
195 Target = BR->getOperand(1);
198 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
200 // Build the result and
201 SmallVector<EVT, 4> Res;
202 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
203 Res.push_back(Intr->getValueType(i));
205 // operands of the new intrinsic call
206 SmallVector<SDValue, 4> Ops;
207 Ops.push_back(BRCOND.getOperand(0));
208 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
209 Ops.push_back(Intr->getOperand(i));
210 Ops.push_back(Target);
212 // build the new intrinsic call
213 SDNode *Result = DAG.getNode(
214 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
215 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
218 // Give the branch instruction our target
223 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
226 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
228 // Copy the intrinsic results to registers
229 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
230 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
234 Chain = DAG.getCopyToReg(
236 CopyToReg->getOperand(1),
237 SDValue(Result, i - 1),
240 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
243 // Remove the old intrinsic from the chain
244 DAG.ReplaceAllUsesOfValueWith(
245 SDValue(Intr, Intr->getNumValues() - 1),
246 Intr->getOperand(0));
251 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
252 EVT VT = Op.getValueType();
253 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
257 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
259 // We only need to lower USER_SGPR address space loads
260 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
264 // Loads from the USER_SGPR address space can only have constant value
266 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
269 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
270 const TargetRegisterClass * dstClass;
271 switch (TypeDwordWidth) {
273 assert(!"USER_SGPR value size not implemented");
276 dstClass = &AMDGPU::SReg_32RegClass;
279 dstClass = &AMDGPU::SReg_64RegClass;
282 uint64_t Index = BasePtr->getZExtValue();
283 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
284 unsigned SGPRIndex = Index / TypeDwordWidth;
285 unsigned Reg = dstClass->getRegister(SGPRIndex);
287 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
292 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
293 SDValue LHS = Op.getOperand(0);
294 SDValue RHS = Op.getOperand(1);
295 SDValue True = Op.getOperand(2);
296 SDValue False = Op.getOperand(3);
297 SDValue CC = Op.getOperand(4);
298 EVT VT = Op.getValueType();
299 DebugLoc DL = Op.getDebugLoc();
301 // Possible Min/Max pattern
302 SDValue MinMax = LowerMinMax(Op, DAG);
303 if (MinMax.getNode()) {
307 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
308 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
311 //===----------------------------------------------------------------------===//
312 // Custom DAG optimizations
313 //===----------------------------------------------------------------------===//
315 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
316 DAGCombinerInfo &DCI) const {
317 SelectionDAG &DAG = DCI.DAG;
318 DebugLoc DL = N->getDebugLoc();
319 EVT VT = N->getValueType(0);
321 switch (N->getOpcode()) {
323 case ISD::SELECT_CC: {
325 ConstantSDNode *True, *False;
326 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
327 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
328 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
329 && True->isAllOnesValue()
330 && False->isNullValue()
332 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
333 N->getOperand(1), N->getOperand(4));
339 SDValue Arg0 = N->getOperand(0);
340 SDValue Arg1 = N->getOperand(1);
341 SDValue CC = N->getOperand(2);
342 ConstantSDNode * C = NULL;
343 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
345 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
347 && Arg0.getOpcode() == ISD::SIGN_EXTEND
348 && Arg0.getOperand(0).getValueType() == MVT::i1
349 && (C = dyn_cast<ConstantSDNode>(Arg1))
351 && CCOp == ISD::SETNE) {
352 return SimplifySetCC(VT, Arg0.getOperand(0),
353 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);