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()) {
31 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
32 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
33 addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass);
34 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
35 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
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::ADD, MVT::i64, Legal);
46 setOperationAction(ISD::ADD, MVT::i32, Legal);
48 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
50 // We need to custom lower loads from the USER_SGPR address space, so we can
51 // add the SGPRs as livein registers.
52 setOperationAction(ISD::LOAD, MVT::i32, Custom);
53 setOperationAction(ISD::LOAD, MVT::i64, Custom);
55 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
56 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
58 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
59 setTargetDAGCombine(ISD::SELECT_CC);
61 setTargetDAGCombine(ISD::SETCC);
64 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
65 MachineInstr * MI, MachineBasicBlock * BB) const {
66 MachineRegisterInfo & MRI = BB->getParent()->getRegInfo();
67 MachineBasicBlock::iterator I = MI;
69 switch (MI->getOpcode()) {
71 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
72 case AMDGPU::BRANCH: return BB;
73 case AMDGPU::SHADER_TYPE:
74 BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType =
75 MI->getOperand(0).getImm();
76 MI->eraseFromParent();
79 case AMDGPU::SI_INTERP:
80 LowerSI_INTERP(MI, *BB, I, MRI);
83 LowerSI_WQM(MI, *BB, I, MRI);
89 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
90 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
91 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
92 .addReg(AMDGPU::EXEC);
94 MI->eraseFromParent();
97 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
98 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
99 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
100 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
101 MachineOperand dst = MI->getOperand(0);
102 MachineOperand iReg = MI->getOperand(1);
103 MachineOperand jReg = MI->getOperand(2);
104 MachineOperand attr_chan = MI->getOperand(3);
105 MachineOperand attr = MI->getOperand(4);
106 MachineOperand params = MI->getOperand(5);
108 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
111 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
113 .addOperand(attr_chan)
117 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
121 .addOperand(attr_chan)
125 MI->eraseFromParent();
128 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
132 //===----------------------------------------------------------------------===//
133 // Custom DAG Lowering Operations
134 //===----------------------------------------------------------------------===//
136 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
137 switch (Op.getOpcode()) {
138 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
139 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
140 case ISD::LOAD: return LowerLOAD(Op, DAG);
141 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
142 case ISD::INTRINSIC_WO_CHAIN: {
143 unsigned IntrinsicID =
144 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
145 EVT VT = Op.getValueType();
146 switch (IntrinsicID) {
147 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
148 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
150 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
158 /// \brief Helper function for LowerBRCOND
159 static SDNode *findUser(SDValue Value, unsigned Opcode) {
161 SDNode *Parent = Value.getNode();
162 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
165 if (I.getUse().get() != Value)
168 if (I->getOpcode() == Opcode)
174 /// This transforms the control flow intrinsics to get the branch destination as
175 /// last parameter, also switches branch target with BR if the need arise
176 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
177 SelectionDAG &DAG) const {
179 DebugLoc DL = BRCOND.getDebugLoc();
181 SDNode *Intr = BRCOND.getOperand(1).getNode();
182 SDValue Target = BRCOND.getOperand(2);
185 if (Intr->getOpcode() == ISD::SETCC) {
186 // As long as we negate the condition everything is fine
187 SDNode *SetCC = Intr;
188 assert(SetCC->getConstantOperandVal(1) == 1);
189 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
191 Intr = SetCC->getOperand(0).getNode();
194 // Get the target from BR if we don't negate the condition
195 BR = findUser(BRCOND, ISD::BR);
196 Target = BR->getOperand(1);
199 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
201 // Build the result and
202 SmallVector<EVT, 4> Res;
203 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
204 Res.push_back(Intr->getValueType(i));
206 // operands of the new intrinsic call
207 SmallVector<SDValue, 4> Ops;
208 Ops.push_back(BRCOND.getOperand(0));
209 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
210 Ops.push_back(Intr->getOperand(i));
211 Ops.push_back(Target);
213 // build the new intrinsic call
214 SDNode *Result = DAG.getNode(
215 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
216 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
219 // Give the branch instruction our target
224 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
227 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
229 // Copy the intrinsic results to registers
230 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
231 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
235 Chain = DAG.getCopyToReg(
237 CopyToReg->getOperand(1),
238 SDValue(Result, i - 1),
241 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
244 // Remove the old intrinsic from the chain
245 DAG.ReplaceAllUsesOfValueWith(
246 SDValue(Intr, Intr->getNumValues() - 1),
247 Intr->getOperand(0));
252 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
253 EVT VT = Op.getValueType();
254 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
258 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
260 // We only need to lower USER_SGPR address space loads
261 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
265 // Loads from the USER_SGPR address space can only have constant value
267 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
270 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
271 const TargetRegisterClass * dstClass;
272 switch (TypeDwordWidth) {
274 assert(!"USER_SGPR value size not implemented");
277 dstClass = &AMDGPU::SReg_32RegClass;
280 dstClass = &AMDGPU::SReg_64RegClass;
283 uint64_t Index = BasePtr->getZExtValue();
284 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
285 unsigned SGPRIndex = Index / TypeDwordWidth;
286 unsigned Reg = dstClass->getRegister(SGPRIndex);
288 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
293 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
294 SDValue LHS = Op.getOperand(0);
295 SDValue RHS = Op.getOperand(1);
296 SDValue True = Op.getOperand(2);
297 SDValue False = Op.getOperand(3);
298 SDValue CC = Op.getOperand(4);
299 EVT VT = Op.getValueType();
300 DebugLoc DL = Op.getDebugLoc();
302 // Possible Min/Max pattern
303 SDValue MinMax = LowerMinMax(Op, DAG);
304 if (MinMax.getNode()) {
308 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
309 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
312 //===----------------------------------------------------------------------===//
313 // Custom DAG optimizations
314 //===----------------------------------------------------------------------===//
316 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
317 DAGCombinerInfo &DCI) const {
318 SelectionDAG &DAG = DCI.DAG;
319 DebugLoc DL = N->getDebugLoc();
320 EVT VT = N->getValueType(0);
322 switch (N->getOpcode()) {
324 case ISD::SELECT_CC: {
326 ConstantSDNode *True, *False;
327 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
328 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
329 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
330 && True->isAllOnesValue()
331 && False->isNullValue()
333 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
334 N->getOperand(1), N->getOperand(4));
340 SDValue Arg0 = N->getOperand(0);
341 SDValue Arg1 = N->getOperand(1);
342 SDValue CC = N->getOperand(2);
343 ConstantSDNode * C = NULL;
344 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
346 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
348 && Arg0.getOpcode() == ISD::SIGN_EXTEND
349 && Arg0.getOperand(0).getValueType() == MVT::i1
350 && (C = dyn_cast<ConstantSDNode>(Arg1))
352 && CCOp == ISD::SETNE) {
353 return SimplifySetCC(VT, Arg0.getOperand(0),
354 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
362 /// \brief Test if RegClass is one of the VSrc classes
363 static bool isVSrc(unsigned RegClass) {
364 return AMDGPU::VSrc_32RegClassID == RegClass ||
365 AMDGPU::VSrc_64RegClassID == RegClass;
368 /// \brief Test if RegClass is one of the SSrc classes
369 static bool isSSrc(unsigned RegClass) {
370 return AMDGPU::SSrc_32RegClassID == RegClass ||
371 AMDGPU::SSrc_64RegClassID == RegClass;
374 /// \brief Analyze the possible immediate value Op
376 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
377 /// and the immediate value if it's a literal immediate
378 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
385 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N))
386 Imm.I = Node->getSExtValue();
387 else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N))
388 Imm.F = Node->getValueAPF().convertToFloat();
390 return -1; // It isn't an immediate
392 if ((Imm.I >= -16 && Imm.I <= 64) ||
393 Imm.F == 0.5f || Imm.F == -0.5f ||
394 Imm.F == 1.0f || Imm.F == -1.0f ||
395 Imm.F == 2.0f || Imm.F == -2.0f ||
396 Imm.F == 4.0f || Imm.F == -4.0f)
397 return 0; // It's an inline immediate
399 return Imm.I; // It's a literal immediate
402 /// \brief Try to fold an immediate directly into an instruction
403 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
404 bool &ScalarSlotUsed) const {
406 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
407 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
410 const SDValue &Op = Mov->getOperand(0);
411 int32_t Value = analyzeImmediate(Op.getNode());
413 // Not an immediate at all
416 } else if (Value == 0) {
417 // Inline immediates can always be fold
421 } else if (Value == Immediate) {
422 // Already fold literal immediate
426 } else if (!ScalarSlotUsed && !Immediate) {
427 // Fold this literal immediate
428 ScalarSlotUsed = true;
438 /// \brief Does "Op" fit into register class "RegClass" ?
439 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, SDValue &Op,
440 unsigned RegClass) const {
442 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
443 SDNode *Node = Op.getNode();
446 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) {
447 const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode());
448 OpClass = Desc.OpInfo[Op.getResNo()].RegClass;
450 } else if (Node->getOpcode() == ISD::CopyFromReg) {
451 RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode());
452 OpClass = MRI.getRegClass(Reg->getReg())->getID();
460 return TRI->getRegClass(RegClass)->hasSubClassEq(TRI->getRegClass(OpClass));
463 /// \brief Make sure that we don't exeed the number of allowed scalars
464 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
466 bool &ScalarSlotUsed) const {
468 // First map the operands register class to a destination class
469 if (RegClass == AMDGPU::VSrc_32RegClassID)
470 RegClass = AMDGPU::VReg_32RegClassID;
471 else if (RegClass == AMDGPU::VSrc_64RegClassID)
472 RegClass = AMDGPU::VReg_64RegClassID;
476 // Nothing todo if they fit naturaly
477 if (fitsRegClass(DAG, Operand, RegClass))
480 // If the scalar slot isn't used yet use it now
481 if (!ScalarSlotUsed) {
482 ScalarSlotUsed = true;
486 // This is a conservative aproach, it is possible that we can't determine
487 // the correct register class and copy too often, but better save than sorry.
488 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
489 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DebugLoc(),
490 Operand.getValueType(), Operand, RC);
491 Operand = SDValue(Node, 0);
494 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
495 SelectionDAG &DAG) const {
497 // Original encoding (either e32 or e64)
498 int Opcode = Node->getMachineOpcode();
499 const MCInstrDesc *Desc = &TII->get(Opcode);
501 unsigned NumDefs = Desc->getNumDefs();
502 unsigned NumOps = Desc->getNumOperands();
504 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
505 bool HaveVSrc = false, HaveSSrc = false;
507 // First figure out what we alread have in this instruction
508 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
509 i != e && Op < NumOps; ++i, ++Op) {
511 unsigned RegClass = Desc->OpInfo[Op].RegClass;
512 if (isVSrc(RegClass))
514 else if (isSSrc(RegClass))
519 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
520 if (Imm != -1 && Imm != 0) {
526 // If we neither have VSrc nor SSrc it makes no sense to continue
527 if (!HaveVSrc && !HaveSSrc)
530 // No scalar allowed when we have both VSrc and SSrc
531 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
533 // Second go over the operands and try to fold them
534 std::vector<SDValue> Ops;
535 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
536 i != e && Op < NumOps; ++i, ++Op) {
538 const SDValue &Operand = Node->getOperand(i);
539 Ops.push_back(Operand);
541 // Already folded immediate ?
542 if (isa<ConstantSDNode>(Operand.getNode()) ||
543 isa<ConstantFPSDNode>(Operand.getNode()))
546 // Is this a VSrc or SSrc operand ?
547 unsigned RegClass = Desc->OpInfo[Op].RegClass;
548 if (!isVSrc(RegClass) && !isSSrc(RegClass)) {
550 if (i == 1 && Desc->isCommutable() &&
551 fitsRegClass(DAG, Ops[0], RegClass) &&
552 foldImm(Ops[1], Immediate, ScalarSlotUsed)) {
554 assert(isVSrc(Desc->OpInfo[NumDefs].RegClass) ||
555 isSSrc(Desc->OpInfo[NumDefs].RegClass));
557 // Swap commutable operands
558 SDValue Tmp = Ops[1];
565 // Try to fold the immediates
566 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
567 // Folding didn't worked, make sure we don't hit the SReg limit
568 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
572 // Add optional chain and glue
573 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
574 Ops.push_back(Node->getOperand(i));
576 // Update the instruction parameters
577 return DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());