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 "SIISelLowering.h"
20 #include "llvm/CodeGen/FunctionLoweringInfo.h"
21 #include "llvm/CodeGen/PseudoSourceValue.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/IR/Function.h"
28 //===----------------------------------------------------------------------===//
29 // Instruction Selector Implementation
30 //===----------------------------------------------------------------------===//
33 /// AMDGPU specific code to select AMDGPU machine instructions for
34 /// SelectionDAG operations.
35 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
36 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
37 // make the right decision when generating code for different targets.
38 const AMDGPUSubtarget &Subtarget;
40 AMDGPUDAGToDAGISel(TargetMachine &TM);
41 virtual ~AMDGPUDAGToDAGISel();
43 SDNode *Select(SDNode *N) override;
44 const char *getPassName() const override;
45 void PostprocessISelDAG() override;
48 bool isInlineImmediate(SDNode *N) const;
49 inline SDValue getSmallIPtrImm(unsigned Imm);
50 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
51 const R600InstrInfo *TII);
52 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
53 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
55 // Complex pattern selectors
56 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
57 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
58 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
60 static bool checkType(const Value *ptr, unsigned int addrspace);
61 static bool checkPrivateAddress(const MachineMemOperand *Op);
63 static bool isGlobalStore(const StoreSDNode *N);
64 static bool isPrivateStore(const StoreSDNode *N);
65 static bool isLocalStore(const StoreSDNode *N);
66 static bool isRegionStore(const StoreSDNode *N);
68 bool isCPLoad(const LoadSDNode *N) const;
69 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
70 bool isGlobalLoad(const LoadSDNode *N) const;
71 bool isParamLoad(const LoadSDNode *N) const;
72 bool isPrivateLoad(const LoadSDNode *N) const;
73 bool isLocalLoad(const LoadSDNode *N) const;
74 bool isRegionLoad(const LoadSDNode *N) const;
76 /// \returns True if the current basic block being selected is at control
77 /// flow depth 0. Meaning that the current block dominates the
79 bool isCFDepth0() const;
81 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
82 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
83 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
85 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
86 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
88 // Include the pieces autogenerated from the target description.
89 #include "AMDGPUGenDAGISel.inc"
91 } // end anonymous namespace
93 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
94 // DAG, ready for instruction scheduling.
95 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) {
96 return new AMDGPUDAGToDAGISel(TM);
99 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
100 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
103 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
106 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
107 const SITargetLowering *TL
108 = static_cast<const SITargetLowering *>(getTargetLowering());
109 return TL->analyzeImmediate(N) == 0;
112 /// \brief Determine the register class for \p OpNo
113 /// \returns The register class of the virtual register that will be used for
114 /// the given operand number \OpNo or NULL if the register class cannot be
116 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
117 unsigned OpNo) const {
118 if (!N->isMachineOpcode())
121 switch (N->getMachineOpcode()) {
123 const MCInstrDesc &Desc = TM.getInstrInfo()->get(N->getMachineOpcode());
124 unsigned OpIdx = Desc.getNumDefs() + OpNo;
125 if (OpIdx >= Desc.getNumOperands())
127 int RegClass = Desc.OpInfo[OpIdx].RegClass;
131 return TM.getRegisterInfo()->getRegClass(RegClass);
133 case AMDGPU::REG_SEQUENCE: {
134 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
135 const TargetRegisterClass *SuperRC = TM.getRegisterInfo()->getRegClass(RCID);
137 SDValue SubRegOp = N->getOperand(OpNo + 1);
138 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
139 return TM.getRegisterInfo()->getSubClassWithSubReg(SuperRC, SubRegIdx);
144 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
145 return CurDAG->getTargetConstant(Imm, MVT::i32);
148 bool AMDGPUDAGToDAGISel::SelectADDRParam(
149 SDValue Addr, SDValue& R1, SDValue& R2) {
151 if (Addr.getOpcode() == ISD::FrameIndex) {
152 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
153 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
154 R2 = CurDAG->getTargetConstant(0, MVT::i32);
157 R2 = CurDAG->getTargetConstant(0, MVT::i32);
159 } else if (Addr.getOpcode() == ISD::ADD) {
160 R1 = Addr.getOperand(0);
161 R2 = Addr.getOperand(1);
164 R2 = CurDAG->getTargetConstant(0, MVT::i32);
169 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
170 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
171 Addr.getOpcode() == ISD::TargetGlobalAddress) {
174 return SelectADDRParam(Addr, R1, R2);
178 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
179 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
180 Addr.getOpcode() == ISD::TargetGlobalAddress) {
184 if (Addr.getOpcode() == ISD::FrameIndex) {
185 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
186 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
187 R2 = CurDAG->getTargetConstant(0, MVT::i64);
190 R2 = CurDAG->getTargetConstant(0, MVT::i64);
192 } else if (Addr.getOpcode() == ISD::ADD) {
193 R1 = Addr.getOperand(0);
194 R2 = Addr.getOperand(1);
197 R2 = CurDAG->getTargetConstant(0, MVT::i64);
202 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
203 unsigned int Opc = N->getOpcode();
204 if (N->isMachineOpcode()) {
206 return nullptr; // Already selected.
209 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
212 // We are selecting i64 ADD here instead of custom lower it during
213 // DAG legalization, so we can fold some i64 ADDs used for address
214 // calculation into the LOAD and STORE instructions.
216 if (N->getValueType(0) != MVT::i64 ||
217 ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
221 SDValue LHS = N->getOperand(0);
222 SDValue RHS = N->getOperand(1);
224 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
225 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
227 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
228 DL, MVT::i32, LHS, Sub0);
229 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
230 DL, MVT::i32, LHS, Sub1);
232 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
233 DL, MVT::i32, RHS, Sub0);
234 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
235 DL, MVT::i32, RHS, Sub1);
237 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
239 SmallVector<SDValue, 8> AddLoArgs;
240 AddLoArgs.push_back(SDValue(Lo0, 0));
241 AddLoArgs.push_back(SDValue(Lo1, 0));
243 SDNode *AddLo = CurDAG->getMachineNode(
244 isCFDepth0() ? AMDGPU::S_ADD_I32 : AMDGPU::V_ADD_I32_e32,
245 DL, VTList, AddLoArgs);
246 SDValue Carry = SDValue(AddLo, 1);
247 SDNode *AddHi = CurDAG->getMachineNode(
248 isCFDepth0() ? AMDGPU::S_ADDC_U32 : AMDGPU::V_ADDC_U32_e32,
249 DL, MVT::i32, SDValue(Hi0, 0), SDValue(Hi1, 0), Carry);
252 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
258 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args);
260 case ISD::SCALAR_TO_VECTOR:
261 case ISD::BUILD_VECTOR: {
263 const AMDGPURegisterInfo *TRI =
264 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
265 const SIRegisterInfo *SIRI =
266 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
267 EVT VT = N->getValueType(0);
268 unsigned NumVectorElts = VT.getVectorNumElements();
269 EVT EltVT = VT.getVectorElementType();
270 assert(EltVT.bitsEq(MVT::i32));
271 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
273 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
275 if (!U->isMachineOpcode()) {
278 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
282 if (SIRI->isSGPRClass(RC)) {
286 switch(NumVectorElts) {
287 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
288 AMDGPU::SReg_32RegClassID;
290 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
291 AMDGPU::SReg_64RegClassID;
293 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
294 AMDGPU::SReg_128RegClassID;
296 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
297 AMDGPU::SReg_256RegClassID;
299 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
300 AMDGPU::SReg_512RegClassID;
302 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
305 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
306 // that adds a 128 bits reg copy when going through TwoAddressInstructions
307 // pass. We want to avoid 128 bits copies as much as possible because they
308 // can't be bundled by our scheduler.
309 switch(NumVectorElts) {
310 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
311 case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
312 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
316 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
318 if (NumVectorElts == 1) {
319 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT,
320 N->getOperand(0), RegClass);
323 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
325 // 16 = Max Num Vector Elements
326 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
327 // 1 = Vector Register Class
328 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
330 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
331 bool IsRegSeq = true;
332 unsigned NOps = N->getNumOperands();
333 for (unsigned i = 0; i < NOps; i++) {
334 // XXX: Why is this here?
335 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
339 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
340 RegSeqArgs[1 + (2 * i) + 1] =
341 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
344 if (NOps != NumVectorElts) {
345 // Fill in the missing undef elements if this was a scalar_to_vector.
346 assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
348 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
350 for (unsigned i = NOps; i < NumVectorElts; ++i) {
351 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
352 RegSeqArgs[1 + (2 * i) + 1] =
353 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
359 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
362 case ISD::BUILD_PAIR: {
363 SDValue RC, SubReg0, SubReg1;
364 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
367 if (N->getValueType(0) == MVT::i128) {
368 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
369 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
370 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
371 } else if (N->getValueType(0) == MVT::i64) {
372 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
373 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
374 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
376 llvm_unreachable("Unhandled value type for BUILD_PAIR");
378 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
379 N->getOperand(1), SubReg1 };
380 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
381 SDLoc(N), N->getValueType(0), Ops);
385 case ISD::ConstantFP: {
386 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
387 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
388 N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
392 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
393 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
395 ConstantSDNode *C = cast<ConstantSDNode>(N);
396 Imm = C->getZExtValue();
399 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
400 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
401 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
402 CurDAG->getConstant(Imm >> 32, MVT::i32));
403 const SDValue Ops[] = {
404 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
405 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
406 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
409 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
410 N->getValueType(0), Ops);
413 case AMDGPUISD::REGISTER_LOAD: {
414 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
416 SDValue Addr, Offset;
418 SelectADDRIndirect(N->getOperand(1), Addr, Offset);
419 const SDValue Ops[] = {
422 CurDAG->getTargetConstant(0, MVT::i32),
425 return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
426 CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
429 case AMDGPUISD::REGISTER_STORE: {
430 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
432 SDValue Addr, Offset;
433 SelectADDRIndirect(N->getOperand(2), Addr, Offset);
434 const SDValue Ops[] = {
438 CurDAG->getTargetConstant(0, MVT::i32),
441 return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
442 CurDAG->getVTList(MVT::Other),
446 case AMDGPUISD::BFE_I32:
447 case AMDGPUISD::BFE_U32: {
448 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
451 // There is a scalar version available, but unlike the vector version which
452 // has a separate operand for the offset and width, the scalar version packs
453 // the width and offset into a single operand. Try to move to the scalar
454 // version if the offsets are constant, so that we can try to keep extended
455 // loads of kernel arguments in SGPRs.
457 // TODO: Technically we could try to pattern match scalar bitshifts of
458 // dynamic values, but it's probably not useful.
459 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
463 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
467 bool Signed = Opc == AMDGPUISD::BFE_I32;
469 // Transformation function, pack the offset and width of a BFE into
470 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
471 // source, bits [5:0] contain the offset and bits [22:16] the width.
473 uint32_t OffsetVal = Offset->getZExtValue();
474 uint32_t WidthVal = Width->getZExtValue();
476 uint32_t PackedVal = OffsetVal | WidthVal << 16;
478 SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
479 return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
487 return SelectCode(N);
491 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
492 assert(AS != 0 && "Use checkPrivateAddress instead.");
496 return Ptr->getType()->getPointerAddressSpace() == AS;
499 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
500 if (Op->getPseudoValue())
503 if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
504 return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
509 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
510 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
513 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
514 const Value *MemVal = N->getMemOperand()->getValue();
515 return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
516 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
517 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
520 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
521 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
524 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
525 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
528 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
529 const Value *MemVal = N->getMemOperand()->getValue();
531 return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
533 return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
536 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
537 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
538 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
539 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
540 N->getMemoryVT().bitsLT(MVT::i32)) {
544 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
547 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
548 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
551 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
552 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
555 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
556 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
559 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
560 MachineMemOperand *MMO = N->getMemOperand();
561 if (checkPrivateAddress(N->getMemOperand())) {
563 const PseudoSourceValue *PSV = MMO->getPseudoValue();
564 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
572 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
573 if (checkPrivateAddress(N->getMemOperand())) {
574 // Check to make sure we are not a constant pool load or a constant load
575 // that is marked as a private load
576 if (isCPLoad(N) || isConstantLoad(N, -1)) {
581 const Value *MemVal = N->getMemOperand()->getValue();
582 if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
583 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
584 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
585 !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
586 !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
587 !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
593 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
594 // FIXME: Figure out a way to use DominatorTree analysis here.
595 const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
596 const Function *Fn = FuncInfo->Fn;
597 return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
601 const char *AMDGPUDAGToDAGISel::getPassName() const {
602 return "AMDGPU DAG->DAG Pattern Instruction Selection";
610 //===----------------------------------------------------------------------===//
612 //===----------------------------------------------------------------------===//
614 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
616 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
617 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
623 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
624 SDValue& BaseReg, SDValue &Offset) {
625 if (!isa<ConstantSDNode>(Addr)) {
627 Offset = CurDAG->getIntPtrConstant(0, true);
633 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
635 ConstantSDNode *IMMOffset;
637 if (Addr.getOpcode() == ISD::ADD
638 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
639 && isInt<16>(IMMOffset->getZExtValue())) {
641 Base = Addr.getOperand(0);
642 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
644 // If the pointer address is constant, we can move it to the offset field.
645 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
646 && isInt<16>(IMMOffset->getZExtValue())) {
647 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
648 SDLoc(CurDAG->getEntryNode()),
649 AMDGPU::ZERO, MVT::i32);
650 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
654 // Default case, no offset
656 Offset = CurDAG->getTargetConstant(0, MVT::i32);
660 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
664 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
665 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
666 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
667 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
668 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
669 Base = Addr.getOperand(0);
670 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
673 Offset = CurDAG->getTargetConstant(0, MVT::i32);
679 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
680 const AMDGPUTargetLowering& Lowering =
681 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
682 bool IsModified = false;
685 // Go over all selected nodes and try to fold them a bit more
686 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
687 E = CurDAG->allnodes_end(); I != E; ++I) {
691 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
695 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
696 if (ResNode != Node) {
697 ReplaceUses(Node, ResNode);
701 CurDAG->RemoveDeadNodes();
702 } while (IsModified);