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);
124 LowerSI_WQM(MI, *BB, I, MRI);
126 case AMDGPU::SI_V_CNDLT:
127 LowerSI_V_CNDLT(MI, *BB, I, MRI);
133 void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB,
134 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
135 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC)
136 .addReg(AMDGPU::EXEC);
138 MI->eraseFromParent();
141 void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB,
142 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
143 unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
144 unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass);
145 MachineOperand dst = MI->getOperand(0);
146 MachineOperand iReg = MI->getOperand(1);
147 MachineOperand jReg = MI->getOperand(2);
148 MachineOperand attr_chan = MI->getOperand(3);
149 MachineOperand attr = MI->getOperand(4);
150 MachineOperand params = MI->getOperand(5);
152 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0)
155 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp)
157 .addOperand(attr_chan)
161 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32))
165 .addOperand(attr_chan)
169 MI->eraseFromParent();
172 void SITargetLowering::LowerSI_V_CNDLT(MachineInstr *MI, MachineBasicBlock &BB,
173 MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const {
174 unsigned VCC = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
176 BuildMI(BB, I, BB.findDebugLoc(I),
177 TII->get(AMDGPU::V_CMP_GT_F32_e32),
179 .addReg(AMDGPU::SREG_LIT_0)
180 .addOperand(MI->getOperand(1));
182 BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_CNDMASK_B32_e32))
183 .addOperand(MI->getOperand(0))
184 .addOperand(MI->getOperand(3))
185 .addOperand(MI->getOperand(2))
188 MI->eraseFromParent();
191 EVT SITargetLowering::getSetCCResultType(EVT VT) const {
195 //===----------------------------------------------------------------------===//
196 // Custom DAG Lowering Operations
197 //===----------------------------------------------------------------------===//
199 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
200 switch (Op.getOpcode()) {
201 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
202 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
203 case ISD::LOAD: return LowerLOAD(Op, DAG);
204 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
205 case ISD::AND: return Loweri1ContextSwitch(Op, DAG, ISD::AND);
206 case ISD::INTRINSIC_WO_CHAIN: {
207 unsigned IntrinsicID =
208 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
209 EVT VT = Op.getValueType();
210 switch (IntrinsicID) {
211 case AMDGPUIntrinsic::SI_vs_load_buffer_index:
212 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
214 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
222 /// \brief The function is for lowering i1 operations on the
225 /// In the VALU context, VCC is a one bit register, but in the
226 /// SALU context the VCC is a 64-bit register (1-bit per thread). Since only
227 /// the SALU can perform operations on the VCC register, we need to promote
228 /// the operand types from i1 to i64 in order for tablegen to be able to match
229 /// this operation to the correct SALU instruction. We do this promotion by
230 /// wrapping the operands in a CopyToReg node.
232 SDValue SITargetLowering::Loweri1ContextSwitch(SDValue Op,
234 unsigned VCCNode) const {
235 DebugLoc DL = Op.getDebugLoc();
237 SDValue OpNode = DAG.getNode(VCCNode, DL, MVT::i64,
238 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
240 DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i64,
243 return DAG.getNode(SIISD::VCC_BITCAST, DL, MVT::i1, OpNode);
246 /// \brief Helper function for LowerBRCOND
247 static SDNode *findUser(SDValue Value, unsigned Opcode) {
249 SDNode *Parent = Value.getNode();
250 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
253 if (I.getUse().get() != Value)
256 if (I->getOpcode() == Opcode)
262 /// This transforms the control flow intrinsics to get the branch destination as
263 /// last parameter, also switches branch target with BR if the need arise
264 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
265 SelectionDAG &DAG) const {
267 DebugLoc DL = BRCOND.getDebugLoc();
269 SDNode *Intr = BRCOND.getOperand(1).getNode();
270 SDValue Target = BRCOND.getOperand(2);
273 if (Intr->getOpcode() == ISD::SETCC) {
274 // As long as we negate the condition everything is fine
275 SDNode *SetCC = Intr;
276 assert(SetCC->getConstantOperandVal(1) == 1);
277 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
279 Intr = SetCC->getOperand(0).getNode();
282 // Get the target from BR if we don't negate the condition
283 BR = findUser(BRCOND, ISD::BR);
284 Target = BR->getOperand(1);
287 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
289 // Build the result and
290 SmallVector<EVT, 4> Res;
291 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
292 Res.push_back(Intr->getValueType(i));
294 // operands of the new intrinsic call
295 SmallVector<SDValue, 4> Ops;
296 Ops.push_back(BRCOND.getOperand(0));
297 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
298 Ops.push_back(Intr->getOperand(i));
299 Ops.push_back(Target);
301 // build the new intrinsic call
302 SDNode *Result = DAG.getNode(
303 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
304 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
307 // Give the branch instruction our target
312 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
315 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
317 // Copy the intrinsic results to registers
318 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
319 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
323 Chain = DAG.getCopyToReg(
325 CopyToReg->getOperand(1),
326 SDValue(Result, i - 1),
329 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
332 // Remove the old intrinsic from the chain
333 DAG.ReplaceAllUsesOfValueWith(
334 SDValue(Intr, Intr->getNumValues() - 1),
335 Intr->getOperand(0));
340 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
341 EVT VT = Op.getValueType();
342 LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op);
346 unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace();
348 // We only need to lower USER_SGPR address space loads
349 if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) {
353 // Loads from the USER_SGPR address space can only have constant value
355 ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr());
358 unsigned TypeDwordWidth = VT.getSizeInBits() / 32;
359 const TargetRegisterClass * dstClass;
360 switch (TypeDwordWidth) {
362 assert(!"USER_SGPR value size not implemented");
365 dstClass = &AMDGPU::SReg_32RegClass;
368 dstClass = &AMDGPU::SReg_64RegClass;
371 uint64_t Index = BasePtr->getZExtValue();
372 assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned");
373 unsigned SGPRIndex = Index / TypeDwordWidth;
374 unsigned Reg = dstClass->getRegister(SGPRIndex);
376 DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg,
381 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
382 SDValue LHS = Op.getOperand(0);
383 SDValue RHS = Op.getOperand(1);
384 SDValue True = Op.getOperand(2);
385 SDValue False = Op.getOperand(3);
386 SDValue CC = Op.getOperand(4);
387 EVT VT = Op.getValueType();
388 DebugLoc DL = Op.getDebugLoc();
390 // Possible Min/Max pattern
391 SDValue MinMax = LowerMinMax(Op, DAG);
392 if (MinMax.getNode()) {
396 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
397 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
400 //===----------------------------------------------------------------------===//
401 // Custom DAG optimizations
402 //===----------------------------------------------------------------------===//
404 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
405 DAGCombinerInfo &DCI) const {
406 SelectionDAG &DAG = DCI.DAG;
407 DebugLoc DL = N->getDebugLoc();
408 EVT VT = N->getValueType(0);
410 switch (N->getOpcode()) {
412 case ISD::SELECT_CC: {
414 ConstantSDNode *True, *False;
415 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
416 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
417 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
418 && True->isAllOnesValue()
419 && False->isNullValue()
421 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
422 N->getOperand(1), N->getOperand(4));
428 SDValue Arg0 = N->getOperand(0);
429 SDValue Arg1 = N->getOperand(1);
430 SDValue CC = N->getOperand(2);
431 ConstantSDNode * C = NULL;
432 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
434 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
436 && Arg0.getOpcode() == ISD::SIGN_EXTEND
437 && Arg0.getOperand(0).getValueType() == MVT::i1
438 && (C = dyn_cast<ConstantSDNode>(Arg1))
440 && CCOp == ISD::SETNE) {
441 return SimplifySetCC(VT, Arg0.getOperand(0),
442 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
450 #define NODE_NAME_CASE(node) case SIISD::node: return #node;
452 const char* SITargetLowering::getTargetNodeName(unsigned Opcode) const {
454 default: return AMDGPUTargetLowering::getTargetNodeName(Opcode);
455 NODE_NAME_CASE(VCC_AND)
456 NODE_NAME_CASE(VCC_BITCAST)