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 "R600InstrInfo.h"
18 #include "SIISelLowering.h"
19 #include "llvm/CodeGen/FunctionLoweringInfo.h"
20 #include "llvm/CodeGen/PseudoSourceValue.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/SelectionDAGISel.h"
23 #include "llvm/IR/Function.h"
27 //===----------------------------------------------------------------------===//
28 // Instruction Selector Implementation
29 //===----------------------------------------------------------------------===//
32 /// AMDGPU specific code to select AMDGPU machine instructions for
33 /// SelectionDAG operations.
34 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
35 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
36 // make the right decision when generating code for different targets.
37 const AMDGPUSubtarget &Subtarget;
39 AMDGPUDAGToDAGISel(TargetMachine &TM);
40 virtual ~AMDGPUDAGToDAGISel();
42 SDNode *Select(SDNode *N) override;
43 const char *getPassName() const override;
44 void PostprocessISelDAG() override;
47 bool isInlineImmediate(SDNode *N) const;
48 inline SDValue getSmallIPtrImm(unsigned Imm);
49 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
50 const R600InstrInfo *TII);
51 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
52 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
54 // Complex pattern selectors
55 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
56 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
57 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
59 static bool checkType(const Value *ptr, unsigned int addrspace);
60 static bool checkPrivateAddress(const MachineMemOperand *Op);
62 static bool isGlobalStore(const StoreSDNode *N);
63 static bool isPrivateStore(const StoreSDNode *N);
64 static bool isLocalStore(const StoreSDNode *N);
65 static bool isRegionStore(const StoreSDNode *N);
67 bool isCPLoad(const LoadSDNode *N) const;
68 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
69 bool isGlobalLoad(const LoadSDNode *N) const;
70 bool isParamLoad(const LoadSDNode *N) const;
71 bool isPrivateLoad(const LoadSDNode *N) const;
72 bool isLocalLoad(const LoadSDNode *N) const;
73 bool isRegionLoad(const LoadSDNode *N) const;
75 /// \returns True if the current basic block being selected is at control
76 /// flow depth 0. Meaning that the current block dominates the
78 bool isCFDepth0() const;
80 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
81 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
82 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
84 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
85 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
87 // Include the pieces autogenerated from the target description.
88 #include "AMDGPUGenDAGISel.inc"
90 } // end anonymous namespace
92 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
93 // DAG, ready for instruction scheduling.
94 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) {
95 return new AMDGPUDAGToDAGISel(TM);
98 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
99 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
102 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
105 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
106 const SITargetLowering *TL
107 = static_cast<const SITargetLowering *>(getTargetLowering());
108 return TL->analyzeImmediate(N) == 0;
111 /// \brief Determine the register class for \p OpNo
112 /// \returns The register class of the virtual register that will be used for
113 /// the given operand number \OpNo or NULL if the register class cannot be
115 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
116 unsigned OpNo) const {
117 if (!N->isMachineOpcode())
120 switch (N->getMachineOpcode()) {
122 const MCInstrDesc &Desc = TM.getInstrInfo()->get(N->getMachineOpcode());
123 unsigned OpIdx = Desc.getNumDefs() + OpNo;
124 if (OpIdx >= Desc.getNumOperands())
126 int RegClass = Desc.OpInfo[OpIdx].RegClass;
130 return TM.getRegisterInfo()->getRegClass(RegClass);
132 case AMDGPU::REG_SEQUENCE: {
133 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
134 const TargetRegisterClass *SuperRC = TM.getRegisterInfo()->getRegClass(RCID);
136 SDValue SubRegOp = N->getOperand(OpNo + 1);
137 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
138 return TM.getRegisterInfo()->getSubClassWithSubReg(SuperRC, SubRegIdx);
143 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
144 return CurDAG->getTargetConstant(Imm, MVT::i32);
147 bool AMDGPUDAGToDAGISel::SelectADDRParam(
148 SDValue Addr, SDValue& R1, SDValue& R2) {
150 if (Addr.getOpcode() == ISD::FrameIndex) {
151 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
152 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
153 R2 = CurDAG->getTargetConstant(0, MVT::i32);
156 R2 = CurDAG->getTargetConstant(0, MVT::i32);
158 } else if (Addr.getOpcode() == ISD::ADD) {
159 R1 = Addr.getOperand(0);
160 R2 = Addr.getOperand(1);
163 R2 = CurDAG->getTargetConstant(0, MVT::i32);
168 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
169 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
170 Addr.getOpcode() == ISD::TargetGlobalAddress) {
173 return SelectADDRParam(Addr, R1, R2);
177 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
178 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
179 Addr.getOpcode() == ISD::TargetGlobalAddress) {
183 if (Addr.getOpcode() == ISD::FrameIndex) {
184 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
185 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
186 R2 = CurDAG->getTargetConstant(0, MVT::i64);
189 R2 = CurDAG->getTargetConstant(0, MVT::i64);
191 } else if (Addr.getOpcode() == ISD::ADD) {
192 R1 = Addr.getOperand(0);
193 R2 = Addr.getOperand(1);
196 R2 = CurDAG->getTargetConstant(0, MVT::i64);
201 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
202 unsigned int Opc = N->getOpcode();
203 if (N->isMachineOpcode()) {
205 return nullptr; // Already selected.
208 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
211 // We are selecting i64 ADD here instead of custom lower it during
212 // DAG legalization, so we can fold some i64 ADDs used for address
213 // calculation into the LOAD and STORE instructions.
215 if (N->getValueType(0) != MVT::i64 ||
216 ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
220 SDValue LHS = N->getOperand(0);
221 SDValue RHS = N->getOperand(1);
223 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
224 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
226 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
227 DL, MVT::i32, LHS, Sub0);
228 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
229 DL, MVT::i32, LHS, Sub1);
231 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
232 DL, MVT::i32, RHS, Sub0);
233 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
234 DL, MVT::i32, RHS, Sub1);
236 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
238 SmallVector<SDValue, 8> AddLoArgs;
239 AddLoArgs.push_back(SDValue(Lo0, 0));
240 AddLoArgs.push_back(SDValue(Lo1, 0));
242 SDNode *AddLo = CurDAG->getMachineNode(AMDGPU::S_ADD_I32, DL,
244 SDValue Carry = SDValue(AddLo, 1);
245 SDNode *AddHi = CurDAG->getMachineNode(AMDGPU::S_ADDC_U32, DL,
246 MVT::i32, SDValue(Hi0, 0),
247 SDValue(Hi1, 0), Carry);
250 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
256 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args);
258 case ISD::BUILD_VECTOR: {
260 const AMDGPURegisterInfo *TRI =
261 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
262 const SIRegisterInfo *SIRI =
263 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
264 EVT VT = N->getValueType(0);
265 unsigned NumVectorElts = VT.getVectorNumElements();
266 assert(VT.getVectorElementType().bitsEq(MVT::i32));
267 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
269 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
271 if (!U->isMachineOpcode()) {
274 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
278 if (SIRI->isSGPRClass(RC)) {
282 switch(NumVectorElts) {
283 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
284 AMDGPU::SReg_32RegClassID;
286 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
287 AMDGPU::SReg_64RegClassID;
289 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
290 AMDGPU::SReg_128RegClassID;
292 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
293 AMDGPU::SReg_256RegClassID;
295 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
296 AMDGPU::SReg_512RegClassID;
298 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
301 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
302 // that adds a 128 bits reg copy when going through TwoAddressInstructions
303 // pass. We want to avoid 128 bits copies as much as possible because they
304 // can't be bundled by our scheduler.
305 switch(NumVectorElts) {
306 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
307 case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
308 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
312 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
314 if (NumVectorElts == 1) {
315 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS,
316 VT.getVectorElementType(),
317 N->getOperand(0), RegClass);
320 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
322 // 16 = Max Num Vector Elements
323 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
324 // 1 = Vector Register Class
325 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(N->getNumOperands() * 2 + 1);
327 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
328 bool IsRegSeq = true;
329 for (unsigned i = 0; i < N->getNumOperands(); i++) {
330 // XXX: Why is this here?
331 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
335 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
336 RegSeqArgs[1 + (2 * i) + 1] =
337 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
341 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
344 case ISD::BUILD_PAIR: {
345 SDValue RC, SubReg0, SubReg1;
346 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
349 if (N->getValueType(0) == MVT::i128) {
350 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
351 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
352 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
353 } else if (N->getValueType(0) == MVT::i64) {
354 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
355 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
356 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
358 llvm_unreachable("Unhandled value type for BUILD_PAIR");
360 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
361 N->getOperand(1), SubReg1 };
362 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
363 SDLoc(N), N->getValueType(0), Ops);
367 case ISD::ConstantFP: {
368 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
369 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
370 N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
374 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
375 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
377 ConstantSDNode *C = cast<ConstantSDNode>(N);
378 Imm = C->getZExtValue();
381 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
382 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
383 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
384 CurDAG->getConstant(Imm >> 32, MVT::i32));
385 const SDValue Ops[] = {
386 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
387 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
388 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
391 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
392 N->getValueType(0), Ops);
395 case AMDGPUISD::REGISTER_LOAD: {
396 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
398 SDValue Addr, Offset;
400 SelectADDRIndirect(N->getOperand(1), Addr, Offset);
401 const SDValue Ops[] = {
404 CurDAG->getTargetConstant(0, MVT::i32),
407 return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
408 CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
411 case AMDGPUISD::REGISTER_STORE: {
412 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
414 SDValue Addr, Offset;
415 SelectADDRIndirect(N->getOperand(2), Addr, Offset);
416 const SDValue Ops[] = {
420 CurDAG->getTargetConstant(0, MVT::i32),
423 return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
424 CurDAG->getVTList(MVT::Other),
428 case AMDGPUISD::BFE_I32:
429 case AMDGPUISD::BFE_U32: {
430 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
433 // There is a scalar version available, but unlike the vector version which
434 // has a separate operand for the offset and width, the scalar version packs
435 // the width and offset into a single operand. Try to move to the scalar
436 // version if the offsets are constant, so that we can try to keep extended
437 // loads of kernel arguments in SGPRs.
439 // TODO: Technically we could try to pattern match scalar bitshifts of
440 // dynamic values, but it's probably not useful.
441 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
445 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
449 bool Signed = Opc == AMDGPUISD::BFE_I32;
451 // Transformation function, pack the offset and width of a BFE into
452 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
453 // source, bits [5:0] contain the offset and bits [22:16] the width.
455 uint32_t OffsetVal = Offset->getZExtValue();
456 uint32_t WidthVal = Width->getZExtValue();
458 uint32_t PackedVal = OffsetVal | WidthVal << 16;
460 SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
461 return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
469 return SelectCode(N);
473 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
474 assert(AS != 0 && "Use checkPrivateAddress instead.");
478 return Ptr->getType()->getPointerAddressSpace() == AS;
481 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
482 if (Op->getPseudoValue())
485 if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
486 return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
491 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
492 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
495 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
496 const Value *MemVal = N->getMemOperand()->getValue();
497 return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
498 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
499 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
502 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
503 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
506 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
507 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
510 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
511 const Value *MemVal = N->getMemOperand()->getValue();
513 return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
515 return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
518 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
519 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
520 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
521 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
522 N->getMemoryVT().bitsLT(MVT::i32)) {
526 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
529 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
530 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
533 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
534 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
537 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
538 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
541 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
542 MachineMemOperand *MMO = N->getMemOperand();
543 if (checkPrivateAddress(N->getMemOperand())) {
545 const PseudoSourceValue *PSV = MMO->getPseudoValue();
546 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
554 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
555 if (checkPrivateAddress(N->getMemOperand())) {
556 // Check to make sure we are not a constant pool load or a constant load
557 // that is marked as a private load
558 if (isCPLoad(N) || isConstantLoad(N, -1)) {
563 const Value *MemVal = N->getMemOperand()->getValue();
564 if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
565 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
566 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
567 !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
568 !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
569 !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
575 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
576 // FIXME: Figure out a way to use DominatorTree analysis here.
577 const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
578 const Function *Fn = FuncInfo->Fn;
579 return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
583 const char *AMDGPUDAGToDAGISel::getPassName() const {
584 return "AMDGPU DAG->DAG Pattern Instruction Selection";
592 //===----------------------------------------------------------------------===//
594 //===----------------------------------------------------------------------===//
596 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
598 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
599 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
605 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
606 SDValue& BaseReg, SDValue &Offset) {
607 if (!isa<ConstantSDNode>(Addr)) {
609 Offset = CurDAG->getIntPtrConstant(0, true);
615 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
617 ConstantSDNode *IMMOffset;
619 if (Addr.getOpcode() == ISD::ADD
620 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
621 && isInt<16>(IMMOffset->getZExtValue())) {
623 Base = Addr.getOperand(0);
624 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
626 // If the pointer address is constant, we can move it to the offset field.
627 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
628 && isInt<16>(IMMOffset->getZExtValue())) {
629 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
630 SDLoc(CurDAG->getEntryNode()),
631 AMDGPU::ZERO, MVT::i32);
632 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
636 // Default case, no offset
638 Offset = CurDAG->getTargetConstant(0, MVT::i32);
642 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
646 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
647 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
648 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
649 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
650 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
651 Base = Addr.getOperand(0);
652 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
655 Offset = CurDAG->getTargetConstant(0, MVT::i32);
661 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
662 const AMDGPUTargetLowering& Lowering =
663 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
664 bool IsModified = false;
667 // Go over all selected nodes and try to fold them a bit more
668 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
669 E = CurDAG->allnodes_end(); I != E; ++I) {
673 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
677 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
678 if (ResNode != Node) {
679 ReplaceUses(Node, ResNode);
683 CurDAG->RemoveDeadNodes();
684 } while (IsModified);