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 AMDGPUISD::BUILD_VERTICAL_VECTOR:
262 case ISD::BUILD_VECTOR: {
264 const AMDGPURegisterInfo *TRI =
265 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
266 const SIRegisterInfo *SIRI =
267 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
268 EVT VT = N->getValueType(0);
269 unsigned NumVectorElts = VT.getVectorNumElements();
270 EVT EltVT = VT.getVectorElementType();
271 assert(EltVT.bitsEq(MVT::i32));
272 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
274 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
276 if (!U->isMachineOpcode()) {
279 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
283 if (SIRI->isSGPRClass(RC)) {
287 switch(NumVectorElts) {
288 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
289 AMDGPU::SReg_32RegClassID;
291 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
292 AMDGPU::SReg_64RegClassID;
294 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
295 AMDGPU::SReg_128RegClassID;
297 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
298 AMDGPU::SReg_256RegClassID;
300 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
301 AMDGPU::SReg_512RegClassID;
303 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
306 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
307 // that adds a 128 bits reg copy when going through TwoAddressInstructions
308 // pass. We want to avoid 128 bits copies as much as possible because they
309 // can't be bundled by our scheduler.
310 switch(NumVectorElts) {
311 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
313 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
314 RegClassID = AMDGPU::R600_Reg128VerticalRegClassID;
316 RegClassID = AMDGPU::R600_Reg128RegClassID;
318 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
322 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
324 if (NumVectorElts == 1) {
325 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT,
326 N->getOperand(0), RegClass);
329 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
331 // 16 = Max Num Vector Elements
332 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
333 // 1 = Vector Register Class
334 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
336 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
337 bool IsRegSeq = true;
338 unsigned NOps = N->getNumOperands();
339 for (unsigned i = 0; i < NOps; i++) {
340 // XXX: Why is this here?
341 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
345 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
346 RegSeqArgs[1 + (2 * i) + 1] =
347 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
350 if (NOps != NumVectorElts) {
351 // Fill in the missing undef elements if this was a scalar_to_vector.
352 assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
354 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
356 for (unsigned i = NOps; i < NumVectorElts; ++i) {
357 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
358 RegSeqArgs[1 + (2 * i) + 1] =
359 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
365 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
368 case ISD::BUILD_PAIR: {
369 SDValue RC, SubReg0, SubReg1;
370 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
373 if (N->getValueType(0) == MVT::i128) {
374 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
375 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
376 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
377 } else if (N->getValueType(0) == MVT::i64) {
378 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
379 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
380 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
382 llvm_unreachable("Unhandled value type for BUILD_PAIR");
384 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
385 N->getOperand(1), SubReg1 };
386 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
387 SDLoc(N), N->getValueType(0), Ops);
391 case ISD::ConstantFP: {
392 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
393 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
394 N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
398 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
399 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
401 ConstantSDNode *C = cast<ConstantSDNode>(N);
402 Imm = C->getZExtValue();
405 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
406 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
407 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
408 CurDAG->getConstant(Imm >> 32, MVT::i32));
409 const SDValue Ops[] = {
410 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
411 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
412 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
415 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
416 N->getValueType(0), Ops);
419 case AMDGPUISD::REGISTER_LOAD: {
420 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
422 SDValue Addr, Offset;
424 SelectADDRIndirect(N->getOperand(1), Addr, Offset);
425 const SDValue Ops[] = {
428 CurDAG->getTargetConstant(0, MVT::i32),
431 return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
432 CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
435 case AMDGPUISD::REGISTER_STORE: {
436 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
438 SDValue Addr, Offset;
439 SelectADDRIndirect(N->getOperand(2), Addr, Offset);
440 const SDValue Ops[] = {
444 CurDAG->getTargetConstant(0, MVT::i32),
447 return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
448 CurDAG->getVTList(MVT::Other),
452 case AMDGPUISD::BFE_I32:
453 case AMDGPUISD::BFE_U32: {
454 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
457 // There is a scalar version available, but unlike the vector version which
458 // has a separate operand for the offset and width, the scalar version packs
459 // the width and offset into a single operand. Try to move to the scalar
460 // version if the offsets are constant, so that we can try to keep extended
461 // loads of kernel arguments in SGPRs.
463 // TODO: Technically we could try to pattern match scalar bitshifts of
464 // dynamic values, but it's probably not useful.
465 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
469 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
473 bool Signed = Opc == AMDGPUISD::BFE_I32;
475 // Transformation function, pack the offset and width of a BFE into
476 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
477 // source, bits [5:0] contain the offset and bits [22:16] the width.
479 uint32_t OffsetVal = Offset->getZExtValue();
480 uint32_t WidthVal = Width->getZExtValue();
482 uint32_t PackedVal = OffsetVal | WidthVal << 16;
484 SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
485 return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
493 return SelectCode(N);
497 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
498 assert(AS != 0 && "Use checkPrivateAddress instead.");
502 return Ptr->getType()->getPointerAddressSpace() == AS;
505 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
506 if (Op->getPseudoValue())
509 if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
510 return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
515 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
516 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
519 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
520 const Value *MemVal = N->getMemOperand()->getValue();
521 return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
522 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
523 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
526 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
527 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
530 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
531 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
534 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
535 const Value *MemVal = N->getMemOperand()->getValue();
537 return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
539 return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
542 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
543 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
544 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
545 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
546 N->getMemoryVT().bitsLT(MVT::i32)) {
550 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
553 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
554 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
557 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
558 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
561 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
562 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
565 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
566 MachineMemOperand *MMO = N->getMemOperand();
567 if (checkPrivateAddress(N->getMemOperand())) {
569 const PseudoSourceValue *PSV = MMO->getPseudoValue();
570 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
578 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
579 if (checkPrivateAddress(N->getMemOperand())) {
580 // Check to make sure we are not a constant pool load or a constant load
581 // that is marked as a private load
582 if (isCPLoad(N) || isConstantLoad(N, -1)) {
587 const Value *MemVal = N->getMemOperand()->getValue();
588 if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
589 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
590 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
591 !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
592 !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
593 !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
599 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
600 // FIXME: Figure out a way to use DominatorTree analysis here.
601 const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
602 const Function *Fn = FuncInfo->Fn;
603 return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
607 const char *AMDGPUDAGToDAGISel::getPassName() const {
608 return "AMDGPU DAG->DAG Pattern Instruction Selection";
616 //===----------------------------------------------------------------------===//
618 //===----------------------------------------------------------------------===//
620 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
622 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
623 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
629 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
630 SDValue& BaseReg, SDValue &Offset) {
631 if (!isa<ConstantSDNode>(Addr)) {
633 Offset = CurDAG->getIntPtrConstant(0, true);
639 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
641 ConstantSDNode *IMMOffset;
643 if (Addr.getOpcode() == ISD::ADD
644 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
645 && isInt<16>(IMMOffset->getZExtValue())) {
647 Base = Addr.getOperand(0);
648 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
650 // If the pointer address is constant, we can move it to the offset field.
651 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
652 && isInt<16>(IMMOffset->getZExtValue())) {
653 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
654 SDLoc(CurDAG->getEntryNode()),
655 AMDGPU::ZERO, MVT::i32);
656 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
660 // Default case, no offset
662 Offset = CurDAG->getTargetConstant(0, MVT::i32);
666 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
670 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
671 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
672 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
673 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
674 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
675 Base = Addr.getOperand(0);
676 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
679 Offset = CurDAG->getTargetConstant(0, MVT::i32);
685 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
686 const AMDGPUTargetLowering& Lowering =
687 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
688 bool IsModified = false;
691 // Go over all selected nodes and try to fold them a bit more
692 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
693 E = CurDAG->allnodes_end(); I != E; ++I) {
697 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
701 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
702 if (ResNode != Node) {
703 ReplaceUses(Node, ResNode);
707 CurDAG->RemoveDeadNodes();
708 } while (IsModified);