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 TRI(TM.getRegisterInfo()) {
32 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
33 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
35 addRegisterClass(MVT::v16i8, &AMDGPU::SReg_128RegClass);
36 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
37 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
39 addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass);
40 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
42 addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass);
44 addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass);
45 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
47 addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
48 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
50 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
51 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
53 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
54 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
56 computeRegisterProperties();
58 setOperationAction(ISD::ADD, MVT::i64, Legal);
59 setOperationAction(ISD::ADD, MVT::i32, Legal);
61 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
63 // We need to custom lower loads from the USER_SGPR address space, so we can
64 // add the SGPRs as livein registers.
65 setOperationAction(ISD::LOAD, MVT::i32, Custom);
66 setOperationAction(ISD::LOAD, MVT::i64, Custom);
68 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
69 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
71 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
72 setTargetDAGCombine(ISD::SELECT_CC);
74 setTargetDAGCombine(ISD::SETCC);
77 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
78 MachineInstr * MI, MachineBasicBlock * BB) const {
79 MachineRegisterInfo & MRI = BB->getParent()->getRegInfo();
80 MachineBasicBlock::iterator I = MI;
82 switch (MI->getOpcode()) {
84 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
85 case AMDGPU::BRANCH: return BB;
86 case AMDGPU::SI_INTERP:
87 LowerSI_INTERP(MI, *BB, I, MRI);
90 LowerSI_WQM(MI, *BB, I, MRI);
96 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
97 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
98 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
99 .addReg(AMDGPU::EXEC);
101 MI->eraseFromParent();
104 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
105 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
106 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
107 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
108 MachineOperand dst = MI->getOperand(0);
109 MachineOperand iReg = MI->getOperand(1);
110 MachineOperand jReg = MI->getOperand(2);
111 MachineOperand attr_chan = MI->getOperand(3);
112 MachineOperand attr = MI->getOperand(4);
113 MachineOperand params = MI->getOperand(5);
115 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
118 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
120 .addOperand(attr_chan)
124 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
128 .addOperand(attr_chan)
132 MI->eraseFromParent();
135 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
139 //===----------------------------------------------------------------------===//
140 // Custom DAG Lowering Operations
141 //===----------------------------------------------------------------------===//
143 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
144 switch (Op.getOpcode()) {
145 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
146 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
147 case ISD::LOAD: return LowerLOAD(Op, DAG);
148 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
149 case ISD::INTRINSIC_WO_CHAIN: {
150 unsigned IntrinsicID =
151 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
152 EVT VT = Op.getValueType();
153 switch (IntrinsicID) {
154 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
155 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
157 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
165 /// \brief Helper function for LowerBRCOND
166 static SDNode *findUser(SDValue Value, unsigned Opcode) {
168 SDNode *Parent = Value.getNode();
169 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
172 if (I.getUse().get() != Value)
175 if (I->getOpcode() == Opcode)
181 /// This transforms the control flow intrinsics to get the branch destination as
182 /// last parameter, also switches branch target with BR if the need arise
183 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
184 SelectionDAG &DAG) const {
186 DebugLoc DL = BRCOND.getDebugLoc();
188 SDNode *Intr = BRCOND.getOperand(1).getNode();
189 SDValue Target = BRCOND.getOperand(2);
192 if (Intr->getOpcode() == ISD::SETCC) {
193 // As long as we negate the condition everything is fine
194 SDNode *SetCC = Intr;
195 assert(SetCC->getConstantOperandVal(1) == 1);
196 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
198 Intr = SetCC->getOperand(0).getNode();
201 // Get the target from BR if we don't negate the condition
202 BR = findUser(BRCOND, ISD::BR);
203 Target = BR->getOperand(1);
206 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
208 // Build the result and
209 SmallVector<EVT, 4> Res;
210 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
211 Res.push_back(Intr->getValueType(i));
213 // operands of the new intrinsic call
214 SmallVector<SDValue, 4> Ops;
215 Ops.push_back(BRCOND.getOperand(0));
216 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
217 Ops.push_back(Intr->getOperand(i));
218 Ops.push_back(Target);
220 // build the new intrinsic call
221 SDNode *Result = DAG.getNode(
222 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
223 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
226 // Give the branch instruction our target
231 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
234 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
236 // Copy the intrinsic results to registers
237 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
238 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
242 Chain = DAG.getCopyToReg(
244 CopyToReg->getOperand(1),
245 SDValue(Result, i - 1),
248 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
251 // Remove the old intrinsic from the chain
252 DAG.ReplaceAllUsesOfValueWith(
253 SDValue(Intr, Intr->getNumValues() - 1),
254 Intr->getOperand(0));
259 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
260 EVT VT = Op.getValueType();
261 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
265 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
267 // We only need to lower USER_SGPR address space loads
268 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
272 // Loads from the USER_SGPR address space can only have constant value
274 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
277 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
278 const TargetRegisterClass * dstClass;
279 switch (TypeDwordWidth) {
281 assert(!"USER_SGPR value size not implemented");
284 dstClass = &AMDGPU::SReg_32RegClass;
287 dstClass = &AMDGPU::SReg_64RegClass;
290 uint64_t Index = BasePtr->getZExtValue();
291 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
292 unsigned SGPRIndex = Index / TypeDwordWidth;
293 unsigned Reg = dstClass->getRegister(SGPRIndex);
295 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
300 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
301 SDValue LHS = Op.getOperand(0);
302 SDValue RHS = Op.getOperand(1);
303 SDValue True = Op.getOperand(2);
304 SDValue False = Op.getOperand(3);
305 SDValue CC = Op.getOperand(4);
306 EVT VT = Op.getValueType();
307 DebugLoc DL = Op.getDebugLoc();
309 // Possible Min/Max pattern
310 SDValue MinMax = LowerMinMax(Op, DAG);
311 if (MinMax.getNode()) {
315 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
316 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
319 //===----------------------------------------------------------------------===//
320 // Custom DAG optimizations
321 //===----------------------------------------------------------------------===//
323 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
324 DAGCombinerInfo &DCI) const {
325 SelectionDAG &DAG = DCI.DAG;
326 DebugLoc DL = N->getDebugLoc();
327 EVT VT = N->getValueType(0);
329 switch (N->getOpcode()) {
331 case ISD::SELECT_CC: {
333 ConstantSDNode *True, *False;
334 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
335 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
336 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
337 && True->isAllOnesValue()
338 && False->isNullValue()
340 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
341 N->getOperand(1), N->getOperand(4));
347 SDValue Arg0 = N->getOperand(0);
348 SDValue Arg1 = N->getOperand(1);
349 SDValue CC = N->getOperand(2);
350 ConstantSDNode * C = NULL;
351 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
353 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
355 && Arg0.getOpcode() == ISD::SIGN_EXTEND
356 && Arg0.getOperand(0).getValueType() == MVT::i1
357 && (C = dyn_cast<ConstantSDNode>(Arg1))
359 && CCOp == ISD::SETNE) {
360 return SimplifySetCC(VT, Arg0.getOperand(0),
361 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
369 /// \brief Test if RegClass is one of the VSrc classes
370 static bool isVSrc(unsigned RegClass) {
371 return AMDGPU::VSrc_32RegClassID == RegClass ||
372 AMDGPU::VSrc_64RegClassID == RegClass;
375 /// \brief Test if RegClass is one of the SSrc classes
376 static bool isSSrc(unsigned RegClass) {
377 return AMDGPU::SSrc_32RegClassID == RegClass ||
378 AMDGPU::SSrc_64RegClassID == RegClass;
381 /// \brief Analyze the possible immediate value Op
383 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
384 /// and the immediate value if it's a literal immediate
385 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
392 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N))
393 Imm.I = Node->getSExtValue();
394 else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N))
395 Imm.F = Node->getValueAPF().convertToFloat();
397 return -1; // It isn't an immediate
399 if ((Imm.I >= -16 && Imm.I <= 64) ||
400 Imm.F == 0.5f || Imm.F == -0.5f ||
401 Imm.F == 1.0f || Imm.F == -1.0f ||
402 Imm.F == 2.0f || Imm.F == -2.0f ||
403 Imm.F == 4.0f || Imm.F == -4.0f)
404 return 0; // It's an inline immediate
406 return Imm.I; // It's a literal immediate
409 /// \brief Try to fold an immediate directly into an instruction
410 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
411 bool &ScalarSlotUsed) const {
413 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
414 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
417 const SDValue &Op = Mov->getOperand(0);
418 int32_t Value = analyzeImmediate(Op.getNode());
420 // Not an immediate at all
423 } else if (Value == 0) {
424 // Inline immediates can always be fold
428 } else if (Value == Immediate) {
429 // Already fold literal immediate
433 } else if (!ScalarSlotUsed && !Immediate) {
434 // Fold this literal immediate
435 ScalarSlotUsed = true;
445 /// \brief Does "Op" fit into register class "RegClass" ?
446 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, SDValue &Op,
447 unsigned RegClass) const {
449 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
450 SDNode *Node = Op.getNode();
453 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) {
454 const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode());
455 OpClass = Desc.OpInfo[Op.getResNo()].RegClass;
457 } else if (Node->getOpcode() == ISD::CopyFromReg) {
458 RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode());
459 OpClass = MRI.getRegClass(Reg->getReg())->getID();
467 return TRI->getRegClass(RegClass)->hasSubClassEq(TRI->getRegClass(OpClass));
470 /// \brief Make sure that we don't exeed the number of allowed scalars
471 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
473 bool &ScalarSlotUsed) const {
475 // First map the operands register class to a destination class
476 if (RegClass == AMDGPU::VSrc_32RegClassID)
477 RegClass = AMDGPU::VReg_32RegClassID;
478 else if (RegClass == AMDGPU::VSrc_64RegClassID)
479 RegClass = AMDGPU::VReg_64RegClassID;
483 // Nothing todo if they fit naturaly
484 if (fitsRegClass(DAG, Operand, RegClass))
487 // If the scalar slot isn't used yet use it now
488 if (!ScalarSlotUsed) {
489 ScalarSlotUsed = true;
493 // This is a conservative aproach, it is possible that we can't determine
494 // the correct register class and copy too often, but better save than sorry.
495 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
496 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DebugLoc(),
497 Operand.getValueType(), Operand, RC);
498 Operand = SDValue(Node, 0);
501 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
502 SelectionDAG &DAG) const {
504 // Original encoding (either e32 or e64)
505 int Opcode = Node->getMachineOpcode();
506 const MCInstrDesc *Desc = &TII->get(Opcode);
508 unsigned NumDefs = Desc->getNumDefs();
509 unsigned NumOps = Desc->getNumOperands();
511 // e64 version if available, -1 otherwise
512 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
513 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64);
515 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
516 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
518 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
519 bool HaveVSrc = false, HaveSSrc = false;
521 // First figure out what we alread have in this instruction
522 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
523 i != e && Op < NumOps; ++i, ++Op) {
525 unsigned RegClass = Desc->OpInfo[Op].RegClass;
526 if (isVSrc(RegClass))
528 else if (isSSrc(RegClass))
533 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
534 if (Imm != -1 && Imm != 0) {
540 // If we neither have VSrc nor SSrc it makes no sense to continue
541 if (!HaveVSrc && !HaveSSrc)
544 // No scalar allowed when we have both VSrc and SSrc
545 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
547 // Second go over the operands and try to fold them
548 std::vector<SDValue> Ops;
549 bool Promote2e64 = false;
550 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
551 i != e && Op < NumOps; ++i, ++Op) {
553 const SDValue &Operand = Node->getOperand(i);
554 Ops.push_back(Operand);
556 // Already folded immediate ?
557 if (isa<ConstantSDNode>(Operand.getNode()) ||
558 isa<ConstantFPSDNode>(Operand.getNode()))
561 // Is this a VSrc or SSrc operand ?
562 unsigned RegClass = Desc->OpInfo[Op].RegClass;
563 if (!isVSrc(RegClass) && !isSSrc(RegClass)) {
565 if (i == 1 && Desc->isCommutable() &&
566 fitsRegClass(DAG, Ops[0], RegClass) &&
567 foldImm(Ops[1], Immediate, ScalarSlotUsed)) {
569 assert(isVSrc(Desc->OpInfo[NumDefs].RegClass) ||
570 isSSrc(Desc->OpInfo[NumDefs].RegClass));
572 // Swap commutable operands
573 SDValue Tmp = Ops[1];
577 } else if (DescE64 && !Immediate) {
578 // Test if it makes sense to switch to e64 encoding
580 RegClass = DescE64->OpInfo[Op].RegClass;
582 if ((isVSrc(RegClass) || isSSrc(RegClass)) &&
583 foldImm(Ops[i], TmpImm, ScalarSlotUsed)) {
594 // Try to fold the immediates
595 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
596 // Folding didn't worked, make sure we don't hit the SReg limit
597 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
602 // Add the modifier flags while promoting
603 for (unsigned i = 0; i < 4; ++i)
604 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
607 // Add optional chain and glue
608 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
609 Ops.push_back(Node->getOperand(i));
611 // Either create a complete new or update the current instruction
613 return DAG.getMachineNode(OpcodeE64, Node->getDebugLoc(),
614 Node->getVTList(), Ops.data(), Ops.size());
616 return DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());