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::SHADER_TYPE:
87 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
88 MI->getOperand(0).getImm();
89 MI->eraseFromParent();
92 case AMDGPU::SI_INTERP:
93 LowerSI_INTERP(MI, *BB, I, MRI);
96 LowerSI_WQM(MI, *BB, I, MRI);
102 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
103 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
104 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
105 .addReg(AMDGPU::EXEC);
107 MI->eraseFromParent();
110 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
111 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
112 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
113 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
114 MachineOperand dst = MI->getOperand(0);
115 MachineOperand iReg = MI->getOperand(1);
116 MachineOperand jReg = MI->getOperand(2);
117 MachineOperand attr_chan = MI->getOperand(3);
118 MachineOperand attr = MI->getOperand(4);
119 MachineOperand params = MI->getOperand(5);
121 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
124 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
126 .addOperand(attr_chan)
130 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
134 .addOperand(attr_chan)
138 MI->eraseFromParent();
141 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
145 //===----------------------------------------------------------------------===//
146 // Custom DAG Lowering Operations
147 //===----------------------------------------------------------------------===//
149 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
150 switch (Op.getOpcode()) {
151 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
152 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
153 case ISD::LOAD: return LowerLOAD(Op, DAG);
154 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
155 case ISD::INTRINSIC_WO_CHAIN: {
156 unsigned IntrinsicID =
157 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
158 EVT VT = Op.getValueType();
159 switch (IntrinsicID) {
160 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
161 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
163 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
171 /// \brief Helper function for LowerBRCOND
172 static SDNode *findUser(SDValue Value, unsigned Opcode) {
174 SDNode *Parent = Value.getNode();
175 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
178 if (I.getUse().get() != Value)
181 if (I->getOpcode() == Opcode)
187 /// This transforms the control flow intrinsics to get the branch destination as
188 /// last parameter, also switches branch target with BR if the need arise
189 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
190 SelectionDAG &DAG) const {
192 DebugLoc DL = BRCOND.getDebugLoc();
194 SDNode *Intr = BRCOND.getOperand(1).getNode();
195 SDValue Target = BRCOND.getOperand(2);
198 if (Intr->getOpcode() == ISD::SETCC) {
199 // As long as we negate the condition everything is fine
200 SDNode *SetCC = Intr;
201 assert(SetCC->getConstantOperandVal(1) == 1);
202 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
204 Intr = SetCC->getOperand(0).getNode();
207 // Get the target from BR if we don't negate the condition
208 BR = findUser(BRCOND, ISD::BR);
209 Target = BR->getOperand(1);
212 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
214 // Build the result and
215 SmallVector<EVT, 4> Res;
216 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
217 Res.push_back(Intr->getValueType(i));
219 // operands of the new intrinsic call
220 SmallVector<SDValue, 4> Ops;
221 Ops.push_back(BRCOND.getOperand(0));
222 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
223 Ops.push_back(Intr->getOperand(i));
224 Ops.push_back(Target);
226 // build the new intrinsic call
227 SDNode *Result = DAG.getNode(
228 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
229 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
232 // Give the branch instruction our target
237 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
240 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
242 // Copy the intrinsic results to registers
243 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
244 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
248 Chain = DAG.getCopyToReg(
250 CopyToReg->getOperand(1),
251 SDValue(Result, i - 1),
254 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
257 // Remove the old intrinsic from the chain
258 DAG.ReplaceAllUsesOfValueWith(
259 SDValue(Intr, Intr->getNumValues() - 1),
260 Intr->getOperand(0));
265 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
266 EVT VT = Op.getValueType();
267 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
271 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
273 // We only need to lower USER_SGPR address space loads
274 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
278 // Loads from the USER_SGPR address space can only have constant value
280 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
283 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
284 const TargetRegisterClass * dstClass;
285 switch (TypeDwordWidth) {
287 assert(!"USER_SGPR value size not implemented");
290 dstClass = &AMDGPU::SReg_32RegClass;
293 dstClass = &AMDGPU::SReg_64RegClass;
296 uint64_t Index = BasePtr->getZExtValue();
297 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
298 unsigned SGPRIndex = Index / TypeDwordWidth;
299 unsigned Reg = dstClass->getRegister(SGPRIndex);
301 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
306 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
307 SDValue LHS = Op.getOperand(0);
308 SDValue RHS = Op.getOperand(1);
309 SDValue True = Op.getOperand(2);
310 SDValue False = Op.getOperand(3);
311 SDValue CC = Op.getOperand(4);
312 EVT VT = Op.getValueType();
313 DebugLoc DL = Op.getDebugLoc();
315 // Possible Min/Max pattern
316 SDValue MinMax = LowerMinMax(Op, DAG);
317 if (MinMax.getNode()) {
321 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
322 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
325 //===----------------------------------------------------------------------===//
326 // Custom DAG optimizations
327 //===----------------------------------------------------------------------===//
329 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
330 DAGCombinerInfo &DCI) const {
331 SelectionDAG &DAG = DCI.DAG;
332 DebugLoc DL = N->getDebugLoc();
333 EVT VT = N->getValueType(0);
335 switch (N->getOpcode()) {
337 case ISD::SELECT_CC: {
339 ConstantSDNode *True, *False;
340 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
341 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
342 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
343 && True->isAllOnesValue()
344 && False->isNullValue()
346 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
347 N->getOperand(1), N->getOperand(4));
353 SDValue Arg0 = N->getOperand(0);
354 SDValue Arg1 = N->getOperand(1);
355 SDValue CC = N->getOperand(2);
356 ConstantSDNode * C = NULL;
357 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
359 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
361 && Arg0.getOpcode() == ISD::SIGN_EXTEND
362 && Arg0.getOperand(0).getValueType() == MVT::i1
363 && (C = dyn_cast<ConstantSDNode>(Arg1))
365 && CCOp == ISD::SETNE) {
366 return SimplifySetCC(VT, Arg0.getOperand(0),
367 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
375 /// \brief Test if RegClass is one of the VSrc classes
376 static bool isVSrc(unsigned RegClass) {
377 return AMDGPU::VSrc_32RegClassID == RegClass ||
378 AMDGPU::VSrc_64RegClassID == RegClass;
381 /// \brief Test if RegClass is one of the SSrc classes
382 static bool isSSrc(unsigned RegClass) {
383 return AMDGPU::SSrc_32RegClassID == RegClass ||
384 AMDGPU::SSrc_64RegClassID == RegClass;
387 /// \brief Analyze the possible immediate value Op
389 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
390 /// and the immediate value if it's a literal immediate
391 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
398 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N))
399 Imm.I = Node->getSExtValue();
400 else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N))
401 Imm.F = Node->getValueAPF().convertToFloat();
403 return -1; // It isn't an immediate
405 if ((Imm.I >= -16 && Imm.I <= 64) ||
406 Imm.F == 0.5f || Imm.F == -0.5f ||
407 Imm.F == 1.0f || Imm.F == -1.0f ||
408 Imm.F == 2.0f || Imm.F == -2.0f ||
409 Imm.F == 4.0f || Imm.F == -4.0f)
410 return 0; // It's an inline immediate
412 return Imm.I; // It's a literal immediate
415 /// \brief Try to fold an immediate directly into an instruction
416 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
417 bool &ScalarSlotUsed) const {
419 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
420 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
423 const SDValue &Op = Mov->getOperand(0);
424 int32_t Value = analyzeImmediate(Op.getNode());
426 // Not an immediate at all
429 } else if (Value == 0) {
430 // Inline immediates can always be fold
434 } else if (Value == Immediate) {
435 // Already fold literal immediate
439 } else if (!ScalarSlotUsed && !Immediate) {
440 // Fold this literal immediate
441 ScalarSlotUsed = true;
451 /// \brief Does "Op" fit into register class "RegClass" ?
452 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, SDValue &Op,
453 unsigned RegClass) const {
455 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
456 SDNode *Node = Op.getNode();
459 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) {
460 const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode());
461 OpClass = Desc.OpInfo[Op.getResNo()].RegClass;
463 } else if (Node->getOpcode() == ISD::CopyFromReg) {
464 RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode());
465 OpClass = MRI.getRegClass(Reg->getReg())->getID();
473 return TRI->getRegClass(RegClass)->hasSubClassEq(TRI->getRegClass(OpClass));
476 /// \brief Make sure that we don't exeed the number of allowed scalars
477 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
479 bool &ScalarSlotUsed) const {
481 // First map the operands register class to a destination class
482 if (RegClass == AMDGPU::VSrc_32RegClassID)
483 RegClass = AMDGPU::VReg_32RegClassID;
484 else if (RegClass == AMDGPU::VSrc_64RegClassID)
485 RegClass = AMDGPU::VReg_64RegClassID;
489 // Nothing todo if they fit naturaly
490 if (fitsRegClass(DAG, Operand, RegClass))
493 // If the scalar slot isn't used yet use it now
494 if (!ScalarSlotUsed) {
495 ScalarSlotUsed = true;
499 // This is a conservative aproach, it is possible that we can't determine
500 // the correct register class and copy too often, but better save than sorry.
501 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
502 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DebugLoc(),
503 Operand.getValueType(), Operand, RC);
504 Operand = SDValue(Node, 0);
507 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
508 SelectionDAG &DAG) const {
510 // Original encoding (either e32 or e64)
511 int Opcode = Node->getMachineOpcode();
512 const MCInstrDesc *Desc = &TII->get(Opcode);
514 unsigned NumDefs = Desc->getNumDefs();
515 unsigned NumOps = Desc->getNumOperands();
517 // e64 version if available, -1 otherwise
518 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
519 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64);
521 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
522 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
524 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
525 bool HaveVSrc = false, HaveSSrc = false;
527 // First figure out what we alread have in this instruction
528 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
529 i != e && Op < NumOps; ++i, ++Op) {
531 unsigned RegClass = Desc->OpInfo[Op].RegClass;
532 if (isVSrc(RegClass))
534 else if (isSSrc(RegClass))
539 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
540 if (Imm != -1 && Imm != 0) {
546 // If we neither have VSrc nor SSrc it makes no sense to continue
547 if (!HaveVSrc && !HaveSSrc)
550 // No scalar allowed when we have both VSrc and SSrc
551 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
553 // Second go over the operands and try to fold them
554 std::vector<SDValue> Ops;
555 bool Promote2e64 = false;
556 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
557 i != e && Op < NumOps; ++i, ++Op) {
559 const SDValue &Operand = Node->getOperand(i);
560 Ops.push_back(Operand);
562 // Already folded immediate ?
563 if (isa<ConstantSDNode>(Operand.getNode()) ||
564 isa<ConstantFPSDNode>(Operand.getNode()))
567 // Is this a VSrc or SSrc operand ?
568 unsigned RegClass = Desc->OpInfo[Op].RegClass;
569 if (!isVSrc(RegClass) && !isSSrc(RegClass)) {
571 if (i == 1 && Desc->isCommutable() &&
572 fitsRegClass(DAG, Ops[0], RegClass) &&
573 foldImm(Ops[1], Immediate, ScalarSlotUsed)) {
575 assert(isVSrc(Desc->OpInfo[NumDefs].RegClass) ||
576 isSSrc(Desc->OpInfo[NumDefs].RegClass));
578 // Swap commutable operands
579 SDValue Tmp = Ops[1];
583 } else if (DescE64 && !Immediate) {
584 // Test if it makes sense to switch to e64 encoding
586 RegClass = DescE64->OpInfo[Op].RegClass;
588 if ((isVSrc(RegClass) || isSSrc(RegClass)) &&
589 foldImm(Ops[i], TmpImm, ScalarSlotUsed)) {
600 // Try to fold the immediates
601 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
602 // Folding didn't worked, make sure we don't hit the SReg limit
603 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
608 // Add the modifier flags while promoting
609 for (unsigned i = 0; i < 4; ++i)
610 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
613 // Add optional chain and glue
614 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
615 Ops.push_back(Node->getOperand(i));
617 // Either create a complete new or update the current instruction
619 return DAG.getMachineNode(OpcodeE64, Node->getDebugLoc(),
620 Node->getVTList(), Ops.data(), Ops.size());
622 return DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());