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::v4i32, &AMDGPU::SReg_128RegClass);
38 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
40 computeRegisterProperties();
42 setOperationAction(ISD::AND, MVT::i1, Custom);
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 if (TII->get(MI->getOpcode()).TSFlags & SIInstrFlags::NEED_WAIT) {
70 AppendS_WAITCNT(MI, *BB, llvm::next(I));
74 switch (MI->getOpcode()) {
76 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
77 case AMDGPU::BRANCH: return BB;
78 case AMDGPU::CLAMP_SI:
79 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
80 .addOperand(MI->getOperand(0))
81 .addOperand(MI->getOperand(1))
82 // VSRC1-2 are unused, but we still need to fill all the
83 // operand slots, so we just reuse the VSRC0 operand
84 .addOperand(MI->getOperand(1))
85 .addOperand(MI->getOperand(1))
90 MI->eraseFromParent();
94 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
95 .addOperand(MI->getOperand(0))
96 .addOperand(MI->getOperand(1))
97 // VSRC1-2 are unused, but we still need to fill all the
98 // operand slots, so we just reuse the VSRC0 operand
99 .addOperand(MI->getOperand(1))
100 .addOperand(MI->getOperand(1))
105 MI->eraseFromParent();
108 case AMDGPU::FNEG_SI:
109 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::V_MOV_B32_e64))
110 .addOperand(MI->getOperand(0))
111 .addOperand(MI->getOperand(1))
112 // VSRC1-2 are unused, but we still need to fill all the
113 // operand slots, so we just reuse the VSRC0 operand
114 .addOperand(MI->getOperand(1))
115 .addOperand(MI->getOperand(1))
120 MI->eraseFromParent();
122 case AMDGPU::SHADER_TYPE:
123 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
124 MI->getOperand(0).getImm();
125 MI->eraseFromParent();
128 case AMDGPU::SI_INTERP:
129 LowerSI_INTERP(MI, *BB, I, MRI);
131 case AMDGPU::SI_INTERP_CONST:
132 LowerSI_INTERP_CONST(MI, *BB, I, MRI);
135 LowerSI_KIL(MI, *BB, I, MRI);
138 LowerSI_WQM(MI, *BB, I, MRI);
140 case AMDGPU::SI_V_CNDLT:
141 LowerSI_V_CNDLT(MI, *BB, I, MRI);
147 void SITargetLowering::AppendS_WAITCNT(MachineInstr *MI, MachineBasicBlock &BB,
148 MachineBasicBlock::iterator I) const {
149 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WAITCNT))
154 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
155 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
156 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
157 .addReg(AMDGPU::EXEC);
159 MI->eraseFromParent();
162 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
163 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
164 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
165 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
166 MachineOperand dst = MI->getOperand(0);
167 MachineOperand iReg = MI->getOperand(1);
168 MachineOperand jReg = MI->getOperand(2);
169 MachineOperand attr_chan = MI->getOperand(3);
170 MachineOperand attr = MI->getOperand(4);
171 MachineOperand params = MI->getOperand(5);
173 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
176 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
178 .addOperand(attr_chan)
182 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
186 .addOperand(attr_chan)
190 MI->eraseFromParent();
193 void SITargetLowering::LowerSI_INTERP_CONST(MachineInstr *MI,
194 MachineBasicBlock &BB, MachineBasicBlock::iterator I,
195 MachineRegisterInfo &MRI) const {
196 MachineOperand dst = MI->getOperand(0);
197 MachineOperand attr_chan = MI->getOperand(1);
198 MachineOperand attr = MI->getOperand(2);
199 MachineOperand params = MI->getOperand(3);
200 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
202 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
205 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_MOV_F32))
207 .addOperand(attr_chan)
211 MI->eraseFromParent();
214 void SITargetLowering::LowerSI_KIL(MachineInstr *MI, MachineBasicBlock &BB,
215 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
216 // Clear this pixel from the exec mask if the operand is negative
217 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CMPX_LE_F32_e32),
219 .addReg(AMDGPU::SREG_LIT_0)
220 .addOperand(MI->getOperand(0));
222 MI->eraseFromParent();
225 void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
226 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
227 unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
229 BuildMI(BB, I, BB.findDebugLoc(I),
230 TII->get(AMDGPU::V_CMP_GT_F32_e32),
232 .addReg(AMDGPU::SREG_LIT_0)
233 .addOperand(MI->getOperand(1));
235 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
236 .addOperand(MI->getOperand(0))
237 .addOperand(MI->getOperand(3))
238 .addOperand(MI->getOperand(2))
241 MI->eraseFromParent();
244 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
248 //===----------------------------------------------------------------------===//
249 // Custom DAG Lowering Operations
250 //===----------------------------------------------------------------------===//
252 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
253 switch (Op.getOpcode()) {
254 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
255 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
256 case ISD::LOAD: return LowerLOAD(Op, DAG);
257 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
258 case ISD::AND: return Loweri1ContextSwitch(Op, DAG, ISD::AND);
259 case ISD::INTRINSIC_WO_CHAIN: {
260 unsigned IntrinsicID =
261 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
262 EVT VT = Op.getValueType();
263 switch (IntrinsicID) {
264 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
265 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
267 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
275 /// \brief The function is for lowering i1 operations on the
278 /// In the VALU context, VCC is a one bit register, but in the
279 /// SALU context the VCC is a 64-bit register (1-bit per thread). Since only
280 /// the SALU can perform operations on the VCC register, we need to promote
281 /// the operand types from i1 to i64 in order for tablegen to be able to match
282 /// this operation to the correct SALU instruction. We do this promotion by
283 /// wrapping the operands in a CopyToReg node.
285 SDValue SITargetLowering::Loweri1ContextSwitch(SDValue Op,
287 unsigned VCCNode) const {
288 DebugLoc DL = Op.getDebugLoc();
290 SDValue OpNode = DAG.getNode(VCCNode, DL, MVT::i64,
291 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
293 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
296 return DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i1, OpNode);
299 /// \brief Helper function for LowerBRCOND
300 static SDNode *findUser(SDValue Value, unsigned Opcode) {
302 SDNode *Parent = Value.getNode();
303 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
306 if (I.getUse().get() != Value)
309 if (I->getOpcode() == Opcode)
315 /// This transforms the control flow intrinsics to get the branch destination as
316 /// last parameter, also switches branch target with BR if the need arise
317 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
318 SelectionDAG &DAG) const {
320 DebugLoc DL = BRCOND.getDebugLoc();
322 SDNode *Intr = BRCOND.getOperand(1).getNode();
323 SDValue Target = BRCOND.getOperand(2);
326 if (Intr->getOpcode() == ISD::SETCC) {
327 // As long as we negate the condition everything is fine
328 SDNode *SetCC = Intr;
329 assert(SetCC->getConstantOperandVal(1) == 1);
330 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
332 Intr = SetCC->getOperand(0).getNode();
335 // Get the target from BR if we don't negate the condition
336 BR = findUser(BRCOND, ISD::BR);
337 Target = BR->getOperand(1);
340 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
342 // Build the result and
343 SmallVector<EVT, 4> Res;
344 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
345 Res.push_back(Intr->getValueType(i));
347 // operands of the new intrinsic call
348 SmallVector<SDValue, 4> Ops;
349 Ops.push_back(BRCOND.getOperand(0));
350 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
351 Ops.push_back(Intr->getOperand(i));
352 Ops.push_back(Target);
354 // build the new intrinsic call
355 SDNode *Result = DAG.getNode(
356 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
357 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
360 // Give the branch instruction our target
365 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
368 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
370 // Copy the intrinsic results to registers
371 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
372 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
376 Chain = DAG.getCopyToReg(
378 CopyToReg->getOperand(1),
379 SDValue(Result, i - 1),
382 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
385 // Remove the old intrinsic from the chain
386 DAG.ReplaceAllUsesOfValueWith(
387 SDValue(Intr, Intr->getNumValues() - 1),
388 Intr->getOperand(0));
393 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
394 EVT VT = Op.getValueType();
395 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
399 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
401 // We only need to lower USER_SGPR address space loads
402 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
406 // Loads from the USER_SGPR address space can only have constant value
408 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
411 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
412 const TargetRegisterClass * dstClass;
413 switch (TypeDwordWidth) {
415 assert(!"USER_SGPR value size not implemented");
418 dstClass = &AMDGPU::SReg_32RegClass;
421 dstClass = &AMDGPU::SReg_64RegClass;
424 uint64_t Index = BasePtr->getZExtValue();
425 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
426 unsigned SGPRIndex = Index / TypeDwordWidth;
427 unsigned Reg = dstClass->getRegister(SGPRIndex);
429 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
434 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
435 SDValue LHS = Op.getOperand(0);
436 SDValue RHS = Op.getOperand(1);
437 SDValue True = Op.getOperand(2);
438 SDValue False = Op.getOperand(3);
439 SDValue CC = Op.getOperand(4);
440 EVT VT = Op.getValueType();
441 DebugLoc DL = Op.getDebugLoc();
443 // Possible Min/Max pattern
444 SDValue MinMax = LowerMinMax(Op, DAG);
445 if (MinMax.getNode()) {
449 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
450 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
453 //===----------------------------------------------------------------------===//
454 // Custom DAG optimizations
455 //===----------------------------------------------------------------------===//
457 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
458 DAGCombinerInfo &DCI) const {
459 SelectionDAG &DAG = DCI.DAG;
460 DebugLoc DL = N->getDebugLoc();
461 EVT VT = N->getValueType(0);
463 switch (N->getOpcode()) {
465 case ISD::SELECT_CC: {
467 ConstantSDNode *True, *False;
468 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
469 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
470 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
471 && True->isAllOnesValue()
472 && False->isNullValue()
474 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
475 N->getOperand(1), N->getOperand(4));
481 SDValue Arg0 = N->getOperand(0);
482 SDValue Arg1 = N->getOperand(1);
483 SDValue CC = N->getOperand(2);
484 ConstantSDNode * C = NULL;
485 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
487 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
489 && Arg0.getOpcode() == ISD::SIGN_EXTEND
490 && Arg0.getOperand(0).getValueType() == MVT::i1
491 && (C = dyn_cast<ConstantSDNode>(Arg1))
493 && CCOp == ISD::SETNE) {
494 return SimplifySetCC(VT, Arg0.getOperand(0),
495 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
503 #define NODE_NAME_CASE(node) case SIISD::node: return #node;
505 const char* SITargetLowering::getTargetNodeName(unsigned Opcode) const {
507 default: return AMDGPUTargetLowering::getTargetNodeName(Opcode);
508 NODE_NAME_CASE(VCC_AND)
509 NODE_NAME_CASE(VCC_BITCAST)