1 //===-- AMDILISelDAGToDAG.cpp - A dag to dag inst selector for AMDIL ------===//
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 Defines an instruction selector for the AMDGPU target.
13 //===----------------------------------------------------------------------===//
14 #include "AMDGPUInstrInfo.h"
15 #include "AMDGPUISelLowering.h" // For AMDGPUISD
16 #include "AMDGPURegisterInfo.h"
17 #include "AMDGPUSubtarget.h"
18 #include "R600InstrInfo.h"
19 #include "SIDefines.h"
20 #include "SIISelLowering.h"
21 #include "SIMachineFunctionInfo.h"
22 #include "llvm/CodeGen/FunctionLoweringInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/SelectionDAGISel.h"
28 #include "llvm/IR/Function.h"
32 //===----------------------------------------------------------------------===//
33 // Instruction Selector Implementation
34 //===----------------------------------------------------------------------===//
37 /// AMDGPU specific code to select AMDGPU machine instructions for
38 /// SelectionDAG operations.
39 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
40 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
41 // make the right decision when generating code for different targets.
42 const AMDGPUSubtarget &Subtarget;
44 AMDGPUDAGToDAGISel(TargetMachine &TM);
45 virtual ~AMDGPUDAGToDAGISel();
47 SDNode *Select(SDNode *N) override;
48 const char *getPassName() const override;
49 void PostprocessISelDAG() override;
52 bool isInlineImmediate(SDNode *N) const;
53 inline SDValue getSmallIPtrImm(unsigned Imm);
54 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
55 const R600InstrInfo *TII);
56 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
57 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
59 // Complex pattern selectors
60 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
61 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
62 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
64 static bool checkType(const Value *ptr, unsigned int addrspace);
65 static bool checkPrivateAddress(const MachineMemOperand *Op);
67 static bool isGlobalStore(const StoreSDNode *N);
68 static bool isPrivateStore(const StoreSDNode *N);
69 static bool isLocalStore(const StoreSDNode *N);
70 static bool isRegionStore(const StoreSDNode *N);
72 bool isCPLoad(const LoadSDNode *N) const;
73 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
74 bool isGlobalLoad(const LoadSDNode *N) const;
75 bool isParamLoad(const LoadSDNode *N) const;
76 bool isPrivateLoad(const LoadSDNode *N) const;
77 bool isLocalLoad(const LoadSDNode *N) const;
78 bool isRegionLoad(const LoadSDNode *N) const;
80 /// \returns True if the current basic block being selected is at control
81 /// flow depth 0. Meaning that the current block dominates the
83 bool isCFDepth0() const;
85 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
86 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
87 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
89 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
90 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
91 bool SelectMUBUFAddr64(SDValue Addr, SDValue &Ptr, SDValue &Offset,
92 SDValue &ImmOffset) const;
93 bool SelectMUBUFScratch(SDValue Addr, SDValue &RSrc, SDValue &VAddr,
94 SDValue &SOffset, SDValue &ImmOffset) const;
95 bool SelectMUBUFAddr32(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
96 SDValue &SOffset, SDValue &Offset, SDValue &Offen,
97 SDValue &Idxen, SDValue &GLC, SDValue &SLC,
99 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
100 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
101 SDValue &Clamp, SDValue &Omod) const;
103 SDNode *SelectADD_SUB_I64(SDNode *N);
104 SDNode *SelectDIV_SCALE(SDNode *N);
106 // Include the pieces autogenerated from the target description.
107 #include "AMDGPUGenDAGISel.inc"
109 } // end anonymous namespace
111 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
112 // DAG, ready for instruction scheduling.
113 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) {
114 return new AMDGPUDAGToDAGISel(TM);
117 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
118 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
121 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
124 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
125 const SITargetLowering *TL
126 = static_cast<const SITargetLowering *>(getTargetLowering());
127 return TL->analyzeImmediate(N) == 0;
130 /// \brief Determine the register class for \p OpNo
131 /// \returns The register class of the virtual register that will be used for
132 /// the given operand number \OpNo or NULL if the register class cannot be
134 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
135 unsigned OpNo) const {
136 if (!N->isMachineOpcode())
139 switch (N->getMachineOpcode()) {
141 const MCInstrDesc &Desc =
142 TM.getSubtargetImpl()->getInstrInfo()->get(N->getMachineOpcode());
143 unsigned OpIdx = Desc.getNumDefs() + OpNo;
144 if (OpIdx >= Desc.getNumOperands())
146 int RegClass = Desc.OpInfo[OpIdx].RegClass;
150 return TM.getSubtargetImpl()->getRegisterInfo()->getRegClass(RegClass);
152 case AMDGPU::REG_SEQUENCE: {
153 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
154 const TargetRegisterClass *SuperRC =
155 TM.getSubtargetImpl()->getRegisterInfo()->getRegClass(RCID);
157 SDValue SubRegOp = N->getOperand(OpNo + 1);
158 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
159 return TM.getSubtargetImpl()->getRegisterInfo()->getSubClassWithSubReg(
165 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
166 return CurDAG->getTargetConstant(Imm, MVT::i32);
169 bool AMDGPUDAGToDAGISel::SelectADDRParam(
170 SDValue Addr, SDValue& R1, SDValue& R2) {
172 if (Addr.getOpcode() == ISD::FrameIndex) {
173 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
174 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
175 R2 = CurDAG->getTargetConstant(0, MVT::i32);
178 R2 = CurDAG->getTargetConstant(0, MVT::i32);
180 } else if (Addr.getOpcode() == ISD::ADD) {
181 R1 = Addr.getOperand(0);
182 R2 = Addr.getOperand(1);
185 R2 = CurDAG->getTargetConstant(0, MVT::i32);
190 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
191 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
192 Addr.getOpcode() == ISD::TargetGlobalAddress) {
195 return SelectADDRParam(Addr, R1, R2);
199 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
200 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
201 Addr.getOpcode() == ISD::TargetGlobalAddress) {
205 if (Addr.getOpcode() == ISD::FrameIndex) {
206 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
207 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
208 R2 = CurDAG->getTargetConstant(0, MVT::i64);
211 R2 = CurDAG->getTargetConstant(0, MVT::i64);
213 } else if (Addr.getOpcode() == ISD::ADD) {
214 R1 = Addr.getOperand(0);
215 R2 = Addr.getOperand(1);
218 R2 = CurDAG->getTargetConstant(0, MVT::i64);
223 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
224 unsigned int Opc = N->getOpcode();
225 if (N->isMachineOpcode()) {
227 return nullptr; // Already selected.
230 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
233 // We are selecting i64 ADD here instead of custom lower it during
234 // DAG legalization, so we can fold some i64 ADDs used for address
235 // calculation into the LOAD and STORE instructions.
238 if (N->getValueType(0) != MVT::i64 ||
239 ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
242 return SelectADD_SUB_I64(N);
244 case ISD::SCALAR_TO_VECTOR:
245 case AMDGPUISD::BUILD_VERTICAL_VECTOR:
246 case ISD::BUILD_VECTOR: {
248 const AMDGPURegisterInfo *TRI = static_cast<const AMDGPURegisterInfo *>(
249 TM.getSubtargetImpl()->getRegisterInfo());
250 const SIRegisterInfo *SIRI = static_cast<const SIRegisterInfo *>(
251 TM.getSubtargetImpl()->getRegisterInfo());
252 EVT VT = N->getValueType(0);
253 unsigned NumVectorElts = VT.getVectorNumElements();
254 EVT EltVT = VT.getVectorElementType();
255 assert(EltVT.bitsEq(MVT::i32));
256 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
258 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
260 if (!U->isMachineOpcode()) {
263 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
267 if (SIRI->isSGPRClass(RC)) {
271 switch(NumVectorElts) {
272 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
273 AMDGPU::SReg_32RegClassID;
275 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
276 AMDGPU::SReg_64RegClassID;
278 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
279 AMDGPU::SReg_128RegClassID;
281 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
282 AMDGPU::SReg_256RegClassID;
284 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
285 AMDGPU::SReg_512RegClassID;
287 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
290 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
291 // that adds a 128 bits reg copy when going through TwoAddressInstructions
292 // pass. We want to avoid 128 bits copies as much as possible because they
293 // can't be bundled by our scheduler.
294 switch(NumVectorElts) {
295 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
297 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
298 RegClassID = AMDGPU::R600_Reg128VerticalRegClassID;
300 RegClassID = AMDGPU::R600_Reg128RegClassID;
302 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
306 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
308 if (NumVectorElts == 1) {
309 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT,
310 N->getOperand(0), RegClass);
313 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
315 // 16 = Max Num Vector Elements
316 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
317 // 1 = Vector Register Class
318 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
320 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
321 bool IsRegSeq = true;
322 unsigned NOps = N->getNumOperands();
323 for (unsigned i = 0; i < NOps; i++) {
324 // XXX: Why is this here?
325 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
329 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
330 RegSeqArgs[1 + (2 * i) + 1] =
331 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
334 if (NOps != NumVectorElts) {
335 // Fill in the missing undef elements if this was a scalar_to_vector.
336 assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
338 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
340 for (unsigned i = NOps; i < NumVectorElts; ++i) {
341 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
342 RegSeqArgs[1 + (2 * i) + 1] =
343 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
349 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
352 case ISD::BUILD_PAIR: {
353 SDValue RC, SubReg0, SubReg1;
354 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
357 if (N->getValueType(0) == MVT::i128) {
358 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
359 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
360 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
361 } else if (N->getValueType(0) == MVT::i64) {
362 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
363 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
364 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
366 llvm_unreachable("Unhandled value type for BUILD_PAIR");
368 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
369 N->getOperand(1), SubReg1 };
370 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
371 SDLoc(N), N->getValueType(0), Ops);
375 case ISD::ConstantFP: {
376 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
377 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
378 N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
382 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
383 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
385 ConstantSDNode *C = cast<ConstantSDNode>(N);
386 Imm = C->getZExtValue();
389 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
390 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
391 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
392 CurDAG->getConstant(Imm >> 32, MVT::i32));
393 const SDValue Ops[] = {
394 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
395 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
396 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
399 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
400 N->getValueType(0), Ops);
403 case AMDGPUISD::REGISTER_LOAD: {
404 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
406 SDValue Addr, Offset;
408 SelectADDRIndirect(N->getOperand(1), Addr, Offset);
409 const SDValue Ops[] = {
412 CurDAG->getTargetConstant(0, MVT::i32),
415 return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
416 CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
419 case AMDGPUISD::REGISTER_STORE: {
420 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
422 SDValue Addr, Offset;
423 SelectADDRIndirect(N->getOperand(2), Addr, Offset);
424 const SDValue Ops[] = {
428 CurDAG->getTargetConstant(0, MVT::i32),
431 return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
432 CurDAG->getVTList(MVT::Other),
436 case AMDGPUISD::BFE_I32:
437 case AMDGPUISD::BFE_U32: {
438 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
441 // There is a scalar version available, but unlike the vector version which
442 // has a separate operand for the offset and width, the scalar version packs
443 // the width and offset into a single operand. Try to move to the scalar
444 // version if the offsets are constant, so that we can try to keep extended
445 // loads of kernel arguments in SGPRs.
447 // TODO: Technically we could try to pattern match scalar bitshifts of
448 // dynamic values, but it's probably not useful.
449 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
453 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
457 bool Signed = Opc == AMDGPUISD::BFE_I32;
459 // Transformation function, pack the offset and width of a BFE into
460 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
461 // source, bits [5:0] contain the offset and bits [22:16] the width.
463 uint32_t OffsetVal = Offset->getZExtValue();
464 uint32_t WidthVal = Width->getZExtValue();
466 uint32_t PackedVal = OffsetVal | WidthVal << 16;
468 SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
469 return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
476 case AMDGPUISD::DIV_SCALE: {
477 return SelectDIV_SCALE(N);
480 return SelectCode(N);
484 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
485 assert(AS != 0 && "Use checkPrivateAddress instead.");
489 return Ptr->getType()->getPointerAddressSpace() == AS;
492 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
493 if (Op->getPseudoValue())
496 if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
497 return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
502 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
503 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
506 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
507 const Value *MemVal = N->getMemOperand()->getValue();
508 return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
509 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
510 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
513 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
514 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
517 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
518 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
521 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
522 const Value *MemVal = N->getMemOperand()->getValue();
524 return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
526 return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
529 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
530 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
531 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
532 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
533 N->getMemoryVT().bitsLT(MVT::i32)) {
537 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
540 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
541 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
544 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
545 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
548 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
549 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
552 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
553 MachineMemOperand *MMO = N->getMemOperand();
554 if (checkPrivateAddress(N->getMemOperand())) {
556 const PseudoSourceValue *PSV = MMO->getPseudoValue();
557 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
565 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
566 if (checkPrivateAddress(N->getMemOperand())) {
567 // Check to make sure we are not a constant pool load or a constant load
568 // that is marked as a private load
569 if (isCPLoad(N) || isConstantLoad(N, -1)) {
574 const Value *MemVal = N->getMemOperand()->getValue();
575 if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
576 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
577 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
578 !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
579 !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
580 !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
586 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
587 // FIXME: Figure out a way to use DominatorTree analysis here.
588 const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
589 const Function *Fn = FuncInfo->Fn;
590 return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
594 const char *AMDGPUDAGToDAGISel::getPassName() const {
595 return "AMDGPU DAG->DAG Pattern Instruction Selection";
603 //===----------------------------------------------------------------------===//
605 //===----------------------------------------------------------------------===//
607 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
609 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
610 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
616 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
617 SDValue& BaseReg, SDValue &Offset) {
618 if (!isa<ConstantSDNode>(Addr)) {
620 Offset = CurDAG->getIntPtrConstant(0, true);
626 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
628 ConstantSDNode *IMMOffset;
630 if (Addr.getOpcode() == ISD::ADD
631 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
632 && isInt<16>(IMMOffset->getZExtValue())) {
634 Base = Addr.getOperand(0);
635 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
637 // If the pointer address is constant, we can move it to the offset field.
638 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
639 && isInt<16>(IMMOffset->getZExtValue())) {
640 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
641 SDLoc(CurDAG->getEntryNode()),
642 AMDGPU::ZERO, MVT::i32);
643 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
647 // Default case, no offset
649 Offset = CurDAG->getTargetConstant(0, MVT::i32);
653 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
657 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
658 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
659 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
660 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
661 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
662 Base = Addr.getOperand(0);
663 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
666 Offset = CurDAG->getTargetConstant(0, MVT::i32);
672 SDNode *AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
674 SDValue LHS = N->getOperand(0);
675 SDValue RHS = N->getOperand(1);
677 bool IsAdd = (N->getOpcode() == ISD::ADD);
679 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
680 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
682 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
683 DL, MVT::i32, LHS, Sub0);
684 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
685 DL, MVT::i32, LHS, Sub1);
687 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
688 DL, MVT::i32, RHS, Sub0);
689 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
690 DL, MVT::i32, RHS, Sub1);
692 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
693 SDValue AddLoArgs[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
696 unsigned Opc = IsAdd ? AMDGPU::S_ADD_I32 : AMDGPU::S_SUB_I32;
697 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
700 Opc = IsAdd ? AMDGPU::V_ADD_I32_e32 : AMDGPU::V_SUB_I32_e32;
701 CarryOpc = IsAdd ? AMDGPU::V_ADDC_U32_e32 : AMDGPU::V_SUBB_U32_e32;
704 SDNode *AddLo = CurDAG->getMachineNode( Opc, DL, VTList, AddLoArgs);
705 SDValue Carry(AddLo, 1);
707 = CurDAG->getMachineNode(CarryOpc, DL, MVT::i32,
708 SDValue(Hi0, 0), SDValue(Hi1, 0), Carry);
711 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
717 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args);
720 SDNode *AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
722 EVT VT = N->getValueType(0);
724 assert(VT == MVT::f32 || VT == MVT::f64);
727 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
729 const SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
741 return CurDAG->SelectNodeTo(N, Opc, VT, MVT::i1, Ops);
744 static SDValue wrapAddr64Rsrc(SelectionDAG *DAG, SDLoc DL, SDValue Ptr) {
745 return SDValue(DAG->getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::v4i32,
749 static bool isLegalMUBUFImmOffset(const ConstantSDNode *Imm) {
750 return isUInt<12>(Imm->getZExtValue());
753 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &Ptr,
755 SDValue &ImmOffset) const {
758 if (CurDAG->isBaseWithConstantOffset(Addr)) {
759 SDValue N0 = Addr.getOperand(0);
760 SDValue N1 = Addr.getOperand(1);
761 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
763 if (isLegalMUBUFImmOffset(C1)) {
765 if (N0.getOpcode() == ISD::ADD) {
766 // (add (add N2, N3), C1)
767 SDValue N2 = N0.getOperand(0);
768 SDValue N3 = N0.getOperand(1);
769 Ptr = wrapAddr64Rsrc(CurDAG, DL, N2);
771 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
776 Ptr = wrapAddr64Rsrc(CurDAG, DL, CurDAG->getTargetConstant(0, MVT::i64));;
778 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
782 if (Addr.getOpcode() == ISD::ADD) {
784 SDValue N0 = Addr.getOperand(0);
785 SDValue N1 = Addr.getOperand(1);
786 Ptr = wrapAddr64Rsrc(CurDAG, DL, N0);
788 ImmOffset = CurDAG->getTargetConstant(0, MVT::i16);
793 Ptr = wrapAddr64Rsrc(CurDAG, DL, CurDAG->getConstant(0, MVT::i64));
795 ImmOffset = CurDAG->getTargetConstant(0, MVT::i16);
799 /// \brief Return a resource descriptor with the 'Add TID' bit enabled
800 /// The TID (Thread ID) is multipled by the stride value (bits [61:48]
801 /// of the resource descriptor) to create an offset, which is added to the
803 static SDValue buildScratchRSRC(SelectionDAG *DAG, SDLoc DL, SDValue Ptr) {
805 uint64_t Rsrc = AMDGPU::RSRC_DATA_FORMAT | AMDGPU::RSRC_TID_ENABLE |
808 SDValue PtrLo = DAG->getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
809 SDValue PtrHi = DAG->getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
810 SDValue DataLo = DAG->getTargetConstant(
811 Rsrc & APInt::getAllOnesValue(32).getZExtValue(), MVT::i32);
812 SDValue DataHi = DAG->getTargetConstant(Rsrc >> 32, MVT::i32);
814 const SDValue Ops[] = { PtrLo, PtrHi, DataLo, DataHi };
815 return SDValue(DAG->getMachineNode(AMDGPU::SI_BUFFER_RSRC, DL,
816 MVT::v4i32, Ops), 0);
819 bool AMDGPUDAGToDAGISel::SelectMUBUFScratch(SDValue Addr, SDValue &Rsrc,
820 SDValue &VAddr, SDValue &SOffset,
821 SDValue &ImmOffset) const {
824 MachineFunction &MF = CurDAG->getMachineFunction();
825 const SIRegisterInfo *TRI =
826 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
827 MachineRegisterInfo &MRI = MF.getRegInfo();
830 unsigned ScratchPtrReg =
831 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
832 unsigned ScratchOffsetReg =
833 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_WAVE_OFFSET);
835 Rsrc = buildScratchRSRC(CurDAG, DL, CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, MRI.getLiveInVirtReg(ScratchPtrReg), MVT::i64));
836 SOffset = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
837 MRI.getLiveInVirtReg(ScratchOffsetReg), MVT::i32);
840 if (CurDAG->isBaseWithConstantOffset(Addr)) {
841 SDValue N1 = Addr.getOperand(1);
842 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
844 if (isLegalMUBUFImmOffset(C1)) {
845 VAddr = Addr.getOperand(0);
846 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
852 if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
853 isa<FrameIndexSDNode>(Addr.getOperand(0))) {
854 VAddr = Addr.getOperand(1);
855 ImmOffset = Addr.getOperand(0);
860 if (isa<FrameIndexSDNode>(Addr)) {
861 VAddr = SDValue(CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, DL, MVT::i32,
862 CurDAG->getConstant(0, MVT::i32)), 0);
869 ImmOffset = CurDAG->getTargetConstant(0, MVT::i16);
873 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr32(SDValue Addr, SDValue &SRsrc,
874 SDValue &VAddr, SDValue &SOffset,
875 SDValue &Offset, SDValue &Offen,
876 SDValue &Idxen, SDValue &GLC,
877 SDValue &SLC, SDValue &TFE) const {
879 GLC = CurDAG->getTargetConstant(0, MVT::i1);
880 SLC = CurDAG->getTargetConstant(0, MVT::i1);
881 TFE = CurDAG->getTargetConstant(0, MVT::i1);
883 Idxen = CurDAG->getTargetConstant(0, MVT::i1);
884 Offen = CurDAG->getTargetConstant(1, MVT::i1);
886 return SelectMUBUFScratch(Addr, SRsrc, VAddr, SOffset, Offset);
889 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
890 SDValue &SrcMods) const {
896 if (Src.getOpcode() == ISD::FNEG) {
897 Mods |= SISrcMods::NEG;
898 Src = Src.getOperand(0);
901 if (Src.getOpcode() == ISD::FABS) {
902 Mods |= SISrcMods::ABS;
903 Src = Src.getOperand(0);
906 SrcMods = CurDAG->getTargetConstant(Mods, MVT::i32);
911 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
912 SDValue &SrcMods, SDValue &Clamp,
913 SDValue &Omod) const {
914 // FIXME: Handle Clamp and Omod
915 Clamp = CurDAG->getTargetConstant(0, MVT::i32);
916 Omod = CurDAG->getTargetConstant(0, MVT::i32);
918 return SelectVOP3Mods(In, Src, SrcMods);
921 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
922 const AMDGPUTargetLowering& Lowering =
923 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
924 bool IsModified = false;
927 // Go over all selected nodes and try to fold them a bit more
928 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
929 E = CurDAG->allnodes_end(); I != E; ++I) {
933 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
937 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
938 if (ResNode != Node) {
939 ReplaceUses(Node, ResNode);
943 CurDAG->RemoveDeadNodes();
944 } while (IsModified);