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::SCCRegRegClass);
35 addRegisterClass(MVT::i1, &AMDGPU::VCCRegRegClass);
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);
43 computeRegisterProperties();
45 setOperationAction(ISD::AND, MVT::i1, Custom);
47 setOperationAction(ISD::ADD, MVT::i64, Legal);
48 setOperationAction(ISD::ADD, MVT::i32, Legal);
50 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
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);
57 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
58 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
60 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
61 setTargetDAGCombine(ISD::SELECT_CC);
63 setTargetDAGCombine(ISD::SETCC);
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;
72 switch (MI->getOpcode()) {
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 .addReg(AMDGPU::SREG_LIT_0)
81 .addReg(AMDGPU::SREG_LIT_0)
86 MI->eraseFromParent();
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 .addReg(AMDGPU::SREG_LIT_0)
94 .addReg(AMDGPU::SREG_LIT_0)
99 MI->eraseFromParent();
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 .addReg(AMDGPU::SREG_LIT_0)
107 .addReg(AMDGPU::SREG_LIT_0)
112 MI->eraseFromParent();
114 case AMDGPU::SHADER_TYPE:
115 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
116 MI->getOperand(0).getImm();
117 MI->eraseFromParent();
120 case AMDGPU::SI_INTERP:
121 LowerSI_INTERP(MI, *BB, I, MRI);
123 case AMDGPU::SI_INTERP_CONST:
124 LowerSI_INTERP_CONST(MI, *BB, I, MRI);
127 LowerSI_WQM(MI, *BB, I, MRI);
129 case AMDGPU::SI_V_CNDLT:
130 LowerSI_V_CNDLT(MI, *BB, I, MRI);
136 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
137 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
138 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
139 .addReg(AMDGPU::EXEC);
141 MI->eraseFromParent();
144 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
145 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
146 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
147 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
148 MachineOperand dst = MI->getOperand(0);
149 MachineOperand iReg = MI->getOperand(1);
150 MachineOperand jReg = MI->getOperand(2);
151 MachineOperand attr_chan = MI->getOperand(3);
152 MachineOperand attr = MI->getOperand(4);
153 MachineOperand params = MI->getOperand(5);
155 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
158 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
160 .addOperand(attr_chan)
164 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
168 .addOperand(attr_chan)
172 MI->eraseFromParent();
175 void SITargetLowering::LowerSI_INTERP_CONST(MachineInstr *MI,
176 MachineBasicBlock &BB, MachineBasicBlock::iterator I,
177 MachineRegisterInfo &MRI) const {
178 MachineOperand dst = MI->getOperand(0);
179 MachineOperand attr_chan = MI->getOperand(1);
180 MachineOperand attr = MI->getOperand(2);
181 MachineOperand params = MI->getOperand(3);
182 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
184 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
187 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_MOV_F32))
189 .addOperand(attr_chan)
193 MI->eraseFromParent();
196 void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
197 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
198 unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
200 BuildMI(BB, I, BB.findDebugLoc(I),
201 TII->get(AMDGPU::V_CMP_GT_F32_e32),
203 .addReg(AMDGPU::SREG_LIT_0)
204 .addOperand(MI->getOperand(1));
206 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
207 .addOperand(MI->getOperand(0))
208 .addOperand(MI->getOperand(3))
209 .addOperand(MI->getOperand(2))
212 MI->eraseFromParent();
215 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
219 //===----------------------------------------------------------------------===//
220 // Custom DAG Lowering Operations
221 //===----------------------------------------------------------------------===//
223 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
224 switch (Op.getOpcode()) {
225 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
226 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
227 case ISD::LOAD: return LowerLOAD(Op, DAG);
228 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
229 case ISD::AND: return Loweri1ContextSwitch(Op, DAG, ISD::AND);
230 case ISD::INTRINSIC_WO_CHAIN: {
231 unsigned IntrinsicID =
232 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
233 EVT VT = Op.getValueType();
234 switch (IntrinsicID) {
235 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
236 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
238 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
246 /// \brief The function is for lowering i1 operations on the
249 /// In the VALU context, VCC is a one bit register, but in the
250 /// SALU context the VCC is a 64-bit register (1-bit per thread). Since only
251 /// the SALU can perform operations on the VCC register, we need to promote
252 /// the operand types from i1 to i64 in order for tablegen to be able to match
253 /// this operation to the correct SALU instruction. We do this promotion by
254 /// wrapping the operands in a CopyToReg node.
256 SDValue SITargetLowering::Loweri1ContextSwitch(SDValue Op,
258 unsigned VCCNode) const {
259 DebugLoc DL = Op.getDebugLoc();
261 SDValue OpNode = DAG.getNode(VCCNode, DL, MVT::i64,
262 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
264 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
267 return DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i1, OpNode);
270 /// \brief Helper function for LowerBRCOND
271 static SDNode *findUser(SDValue Value, unsigned Opcode) {
273 SDNode *Parent = Value.getNode();
274 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
277 if (I.getUse().get() != Value)
280 if (I->getOpcode() == Opcode)
286 /// This transforms the control flow intrinsics to get the branch destination as
287 /// last parameter, also switches branch target with BR if the need arise
288 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
289 SelectionDAG &DAG) const {
291 DebugLoc DL = BRCOND.getDebugLoc();
293 SDNode *Intr = BRCOND.getOperand(1).getNode();
294 SDValue Target = BRCOND.getOperand(2);
297 if (Intr->getOpcode() == ISD::SETCC) {
298 // As long as we negate the condition everything is fine
299 SDNode *SetCC = Intr;
300 assert(SetCC->getConstantOperandVal(1) == 1);
301 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
303 Intr = SetCC->getOperand(0).getNode();
306 // Get the target from BR if we don't negate the condition
307 BR = findUser(BRCOND, ISD::BR);
308 Target = BR->getOperand(1);
311 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
313 // Build the result and
314 SmallVector<EVT, 4> Res;
315 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
316 Res.push_back(Intr->getValueType(i));
318 // operands of the new intrinsic call
319 SmallVector<SDValue, 4> Ops;
320 Ops.push_back(BRCOND.getOperand(0));
321 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
322 Ops.push_back(Intr->getOperand(i));
323 Ops.push_back(Target);
325 // build the new intrinsic call
326 SDNode *Result = DAG.getNode(
327 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
328 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
331 // Give the branch instruction our target
336 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
339 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
341 // Copy the intrinsic results to registers
342 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
343 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
347 Chain = DAG.getCopyToReg(
349 CopyToReg->getOperand(1),
350 SDValue(Result, i - 1),
353 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
356 // Remove the old intrinsic from the chain
357 DAG.ReplaceAllUsesOfValueWith(
358 SDValue(Intr, Intr->getNumValues() - 1),
359 Intr->getOperand(0));
364 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
365 EVT VT = Op.getValueType();
366 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
370 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
372 // We only need to lower USER_SGPR address space loads
373 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
377 // Loads from the USER_SGPR address space can only have constant value
379 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
382 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
383 const TargetRegisterClass * dstClass;
384 switch (TypeDwordWidth) {
386 assert(!"USER_SGPR value size not implemented");
389 dstClass = &AMDGPU::SReg_32RegClass;
392 dstClass = &AMDGPU::SReg_64RegClass;
395 uint64_t Index = BasePtr->getZExtValue();
396 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
397 unsigned SGPRIndex = Index / TypeDwordWidth;
398 unsigned Reg = dstClass->getRegister(SGPRIndex);
400 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
405 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
406 SDValue LHS = Op.getOperand(0);
407 SDValue RHS = Op.getOperand(1);
408 SDValue True = Op.getOperand(2);
409 SDValue False = Op.getOperand(3);
410 SDValue CC = Op.getOperand(4);
411 EVT VT = Op.getValueType();
412 DebugLoc DL = Op.getDebugLoc();
414 // Possible Min/Max pattern
415 SDValue MinMax = LowerMinMax(Op, DAG);
416 if (MinMax.getNode()) {
420 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
421 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
424 //===----------------------------------------------------------------------===//
425 // Custom DAG optimizations
426 //===----------------------------------------------------------------------===//
428 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
429 DAGCombinerInfo &DCI) const {
430 SelectionDAG &DAG = DCI.DAG;
431 DebugLoc DL = N->getDebugLoc();
432 EVT VT = N->getValueType(0);
434 switch (N->getOpcode()) {
436 case ISD::SELECT_CC: {
438 ConstantSDNode *True, *False;
439 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
440 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
441 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
442 && True->isAllOnesValue()
443 && False->isNullValue()
445 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
446 N->getOperand(1), N->getOperand(4));
452 SDValue Arg0 = N->getOperand(0);
453 SDValue Arg1 = N->getOperand(1);
454 SDValue CC = N->getOperand(2);
455 ConstantSDNode * C = NULL;
456 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
458 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
460 && Arg0.getOpcode() == ISD::SIGN_EXTEND
461 && Arg0.getOperand(0).getValueType() == MVT::i1
462 && (C = dyn_cast<ConstantSDNode>(Arg1))
464 && CCOp == ISD::SETNE) {
465 return SimplifySetCC(VT, Arg0.getOperand(0),
466 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
474 #define NODE_NAME_CASE(node) case SIISD::node: return #node;
476 const char* SITargetLowering::getTargetNodeName(unsigned Opcode) const {
478 default: return AMDGPUTargetLowering::getTargetNodeName(Opcode);
479 NODE_NAME_CASE(VCC_AND)
480 NODE_NAME_CASE(VCC_BITCAST)