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/ADT/ValueMap.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/PseudoSourceValue.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/Support/Compiler.h"
31 //===----------------------------------------------------------------------===//
32 // Instruction Selector Implementation
33 //===----------------------------------------------------------------------===//
36 /// AMDGPU specific code to select AMDGPU machine instructions for
37 /// SelectionDAG operations.
38 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
39 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
40 // make the right decision when generating code for different targets.
41 const AMDGPUSubtarget &Subtarget;
43 AMDGPUDAGToDAGISel(TargetMachine &TM);
44 virtual ~AMDGPUDAGToDAGISel();
46 SDNode *Select(SDNode *N);
47 virtual const char *getPassName() const;
48 virtual void PostprocessISelDAG();
51 inline SDValue getSmallIPtrImm(unsigned Imm);
52 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
53 const R600InstrInfo *TII, std::vector<unsigned> Cst);
54 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
55 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
57 // Complex pattern selectors
58 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
59 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
60 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
62 static bool checkType(const Value *ptr, unsigned int addrspace);
64 static bool isGlobalStore(const StoreSDNode *N);
65 static bool isPrivateStore(const StoreSDNode *N);
66 static bool isLocalStore(const StoreSDNode *N);
67 static bool isRegionStore(const StoreSDNode *N);
69 bool isCPLoad(const LoadSDNode *N) const;
70 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
71 bool isGlobalLoad(const LoadSDNode *N) const;
72 bool isParamLoad(const LoadSDNode *N) const;
73 bool isPrivateLoad(const LoadSDNode *N) const;
74 bool isLocalLoad(const LoadSDNode *N) const;
75 bool isRegionLoad(const LoadSDNode *N) const;
77 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
78 bool SelectGlobalValueVariableOffset(SDValue Addr,
79 SDValue &BaseReg, SDValue& Offset);
80 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
81 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
83 // Include the pieces autogenerated from the target description.
84 #include "AMDGPUGenDAGISel.inc"
86 } // end anonymous namespace
88 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
89 // DAG, ready for instruction scheduling.
90 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
92 return new AMDGPUDAGToDAGISel(TM);
95 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
96 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
99 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
102 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
103 return CurDAG->getTargetConstant(Imm, MVT::i32);
106 bool AMDGPUDAGToDAGISel::SelectADDRParam(
107 SDValue Addr, SDValue& R1, SDValue& R2) {
109 if (Addr.getOpcode() == ISD::FrameIndex) {
110 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
111 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
112 R2 = CurDAG->getTargetConstant(0, MVT::i32);
115 R2 = CurDAG->getTargetConstant(0, MVT::i32);
117 } else if (Addr.getOpcode() == ISD::ADD) {
118 R1 = Addr.getOperand(0);
119 R2 = Addr.getOperand(1);
122 R2 = CurDAG->getTargetConstant(0, MVT::i32);
127 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
128 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
129 Addr.getOpcode() == ISD::TargetGlobalAddress) {
132 return SelectADDRParam(Addr, R1, R2);
136 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
137 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
138 Addr.getOpcode() == ISD::TargetGlobalAddress) {
142 if (Addr.getOpcode() == ISD::FrameIndex) {
143 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
144 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
145 R2 = CurDAG->getTargetConstant(0, MVT::i64);
148 R2 = CurDAG->getTargetConstant(0, MVT::i64);
150 } else if (Addr.getOpcode() == ISD::ADD) {
151 R1 = Addr.getOperand(0);
152 R2 = Addr.getOperand(1);
155 R2 = CurDAG->getTargetConstant(0, MVT::i64);
160 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
161 unsigned int Opc = N->getOpcode();
162 if (N->isMachineOpcode()) {
163 return NULL; // Already selected.
167 case ISD::BUILD_VECTOR: {
168 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
169 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
172 // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
173 // that adds a 128 bits reg copy when going through TwoAddressInstructions
174 // pass. We want to avoid 128 bits copies as much as possible because they
175 // can't be bundled by our scheduler.
176 SDValue RegSeqArgs[9] = {
177 CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
178 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
179 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
180 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
181 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
183 bool IsRegSeq = true;
184 for (unsigned i = 0; i < N->getNumOperands(); i++) {
185 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
189 RegSeqArgs[2 * i + 1] = N->getOperand(i);
193 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
194 RegSeqArgs, 2 * N->getNumOperands() + 1);
196 case ISD::BUILD_PAIR: {
197 SDValue RC, SubReg0, SubReg1;
198 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
199 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
202 if (N->getValueType(0) == MVT::i128) {
203 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
204 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
205 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
206 } else if (N->getValueType(0) == MVT::i64) {
207 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
208 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
209 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
211 llvm_unreachable("Unhandled value type for BUILD_PAIR");
213 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
214 N->getOperand(1), SubReg1 };
215 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
216 SDLoc(N), N->getValueType(0), Ops);
219 case ISD::ConstantFP:
220 case ISD::Constant: {
221 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
222 // XXX: Custom immediate lowering not implemented yet. Instead we use
223 // pseudo instructions defined in SIInstructions.td
224 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
227 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
229 uint64_t ImmValue = 0;
230 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
232 if (N->getOpcode() == ISD::ConstantFP) {
233 // XXX: 64-bit Immediates not supported yet
234 assert(N->getValueType(0) != MVT::f64);
236 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
237 APFloat Value = C->getValueAPF();
238 float FloatValue = Value.convertToFloat();
239 if (FloatValue == 0.0) {
240 ImmReg = AMDGPU::ZERO;
241 } else if (FloatValue == 0.5) {
242 ImmReg = AMDGPU::HALF;
243 } else if (FloatValue == 1.0) {
244 ImmReg = AMDGPU::ONE;
246 ImmValue = Value.bitcastToAPInt().getZExtValue();
249 // XXX: 64-bit Immediates not supported yet
250 assert(N->getValueType(0) != MVT::i64);
252 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
253 if (C->getZExtValue() == 0) {
254 ImmReg = AMDGPU::ZERO;
255 } else if (C->getZExtValue() == 1) {
256 ImmReg = AMDGPU::ONE_INT;
258 ImmValue = C->getZExtValue();
262 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
263 Use != SDNode::use_end(); Use = Next) {
264 Next = llvm::next(Use);
265 std::vector<SDValue> Ops;
266 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
267 Ops.push_back(Use->getOperand(i));
270 if (!Use->isMachineOpcode()) {
271 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
272 // We can only use literal constants (e.g. AMDGPU::ZERO,
273 // AMDGPU::ONE, etc) in machine opcodes.
277 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
278 (TII->get(Use->getMachineOpcode()).TSFlags &
279 R600_InstFlag::VECTOR)) {
283 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
284 assert(ImmIdx != -1);
286 // subtract one from ImmIdx, because the DST operand is usually index
287 // 0 for MachineInstrs, but we have no DST in the Ops vector.
290 // Check that we aren't already using an immediate.
291 // XXX: It's possible for an instruction to have more than one
292 // immediate operand, but this is not supported yet.
293 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
294 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
297 if (C->getZExtValue() != 0) {
298 // This instruction is already using an immediate.
302 // Set the immediate value
303 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
306 // Set the immediate register
307 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
309 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
314 SDNode *Result = SelectCode(N);
316 // Fold operands of selected node
318 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
319 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
320 const R600InstrInfo *TII =
321 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
322 if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
323 bool IsModified = false;
325 std::vector<SDValue> Ops;
326 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
329 IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
331 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
333 } while (IsModified);
336 if (Result && Result->isMachineOpcode() &&
337 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
338 && TII->isALUInstr(Result->getMachineOpcode())) {
339 // Fold FNEG/FABS/CONST_ADDRESS
340 // TODO: Isel can generate multiple MachineInst, we need to recursively
342 bool IsModified = false;
344 std::vector<SDValue> Ops;
345 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
348 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
350 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
352 } while (IsModified);
354 // If node has a single use which is CLAMP_R600, folds it
355 if (Result->hasOneUse() && Result->isMachineOpcode()) {
356 SDNode *PotentialClamp = *Result->use_begin();
357 if (PotentialClamp->isMachineOpcode() &&
358 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
360 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
361 std::vector<SDValue> Ops;
362 unsigned NumOp = Result->getNumOperands();
363 for (unsigned i = 0; i < NumOp; ++i) {
364 Ops.push_back(Result->getOperand(i));
366 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
367 Result = CurDAG->SelectNodeTo(PotentialClamp,
368 Result->getMachineOpcode(), PotentialClamp->getVTList(),
378 bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
379 SDValue &Abs, const R600InstrInfo *TII,
380 std::vector<unsigned> Consts) {
381 switch (Src.getOpcode()) {
382 case AMDGPUISD::CONST_ADDRESS: {
384 if (Src.getValueType().isVector() ||
385 !SelectGlobalValueConstantOffset(Src.getOperand(0), CstOffset))
388 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
389 Consts.push_back(Cst->getZExtValue());
390 if (!TII->fitsConstReadLimitations(Consts))
393 Src = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
398 Src = Src.getOperand(0);
399 Neg = CurDAG->getTargetConstant(1, MVT::i32);
404 Src = Src.getOperand(0);
405 Abs = CurDAG->getTargetConstant(1, MVT::i32);
408 Src = Src.getOperand(0);
415 bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
416 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
418 TII->getOperandIdx(Opcode, R600Operands::SRC0),
419 TII->getOperandIdx(Opcode, R600Operands::SRC1),
420 TII->getOperandIdx(Opcode, R600Operands::SRC2)
423 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
424 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
425 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
428 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
429 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
430 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
433 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
434 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
438 // Gather constants values
439 std::vector<unsigned> Consts;
440 for (unsigned j = 0; j < 3; j++) {
441 int SrcIdx = OperandIdx[j];
444 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
445 if (Reg->getReg() == AMDGPU::ALU_CONST) {
446 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
447 Consts.push_back(Cst->getZExtValue());
452 for (unsigned i = 0; i < 3; i++) {
453 if (OperandIdx[i] < 0)
455 SDValue &Src = Ops[OperandIdx[i] - 1];
456 SDValue &Sel = Ops[SelIdx[i] - 1];
457 SDValue &Neg = Ops[NegIdx[i] - 1];
459 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
460 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
466 bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
467 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
469 TII->getOperandIdx(Opcode, R600Operands::SRC0_X),
470 TII->getOperandIdx(Opcode, R600Operands::SRC0_Y),
471 TII->getOperandIdx(Opcode, R600Operands::SRC0_Z),
472 TII->getOperandIdx(Opcode, R600Operands::SRC0_W),
473 TII->getOperandIdx(Opcode, R600Operands::SRC1_X),
474 TII->getOperandIdx(Opcode, R600Operands::SRC1_Y),
475 TII->getOperandIdx(Opcode, R600Operands::SRC1_Z),
476 TII->getOperandIdx(Opcode, R600Operands::SRC1_W)
479 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_X),
480 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_Y),
481 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_Z),
482 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_W),
483 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_X),
484 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_Y),
485 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_Z),
486 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_W)
489 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_X),
490 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_Y),
491 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_Z),
492 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_W),
493 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_X),
494 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_Y),
495 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_Z),
496 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_W)
499 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_X),
500 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_Y),
501 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_Z),
502 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_W),
503 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_X),
504 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_Y),
505 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_Z),
506 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_W)
509 // Gather constants values
510 std::vector<unsigned> Consts;
511 for (unsigned j = 0; j < 8; j++) {
512 int SrcIdx = OperandIdx[j];
515 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
516 if (Reg->getReg() == AMDGPU::ALU_CONST) {
517 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
518 Consts.push_back(Cst->getZExtValue());
523 for (unsigned i = 0; i < 8; i++) {
524 if (OperandIdx[i] < 0)
526 SDValue &Src = Ops[OperandIdx[i] - 1];
527 SDValue &Sel = Ops[SelIdx[i] - 1];
528 SDValue &Neg = Ops[NegIdx[i] - 1];
529 SDValue &Abs = Ops[AbsIdx[i] - 1];
530 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
536 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
540 Type *ptrType = ptr->getType();
541 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
544 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
545 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
548 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
549 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
550 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
551 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
554 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
555 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
558 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
559 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
562 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) const {
563 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
567 const DataLayout *DL = TM.getDataLayout();
568 MachineMemOperand *MMO = N->getMemOperand();
569 const Value *V = MMO->getValue();
570 const Value *BV = GetUnderlyingObject(V, DL, 0);
573 && ((V && dyn_cast<GlobalValue>(V))
574 || (BV && dyn_cast<GlobalValue>(
575 GetUnderlyingObject(MMO->getValue(), DL, 0))))) {
576 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
582 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
583 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
586 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
587 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
590 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
591 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
594 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
595 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
598 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
599 MachineMemOperand *MMO = N->getMemOperand();
600 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
602 const Value *V = MMO->getValue();
603 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
604 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
612 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
613 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
614 // Check to make sure we are not a constant pool load or a constant load
615 // that is marked as a private load
616 if (isCPLoad(N) || isConstantLoad(N, -1)) {
620 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
621 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
622 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
623 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
624 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
625 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
631 const char *AMDGPUDAGToDAGISel::getPassName() const {
632 return "AMDGPU DAG->DAG Pattern Instruction Selection";
640 ///==== AMDGPU Functions ====///
642 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
644 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
645 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
651 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
652 SDValue& BaseReg, SDValue &Offset) {
653 if (!dyn_cast<ConstantSDNode>(Addr)) {
655 Offset = CurDAG->getIntPtrConstant(0, true);
661 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
663 ConstantSDNode * IMMOffset;
665 if (Addr.getOpcode() == ISD::ADD
666 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
667 && isInt<16>(IMMOffset->getZExtValue())) {
669 Base = Addr.getOperand(0);
670 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
672 // If the pointer address is constant, we can move it to the offset field.
673 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
674 && isInt<16>(IMMOffset->getZExtValue())) {
675 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
676 SDLoc(CurDAG->getEntryNode()),
677 AMDGPU::ZERO, MVT::i32);
678 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
682 // Default case, no offset
684 Offset = CurDAG->getTargetConstant(0, MVT::i32);
688 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
692 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
693 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
694 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
695 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
696 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
697 Base = Addr.getOperand(0);
698 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
701 Offset = CurDAG->getTargetConstant(0, MVT::i32);
707 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
709 if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
713 // Go over all selected nodes and try to fold them a bit more
714 const AMDGPUTargetLowering& Lowering =
715 (*(const AMDGPUTargetLowering*)getTargetLowering());
716 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
717 E = CurDAG->allnodes_end(); I != E; ++I) {
720 switch (Node->getOpcode()) {
721 // Fix the register class in copy to CopyToReg nodes - ISel will always
722 // use SReg classes for 64-bit copies, but this is not always what we want.
723 case ISD::CopyToReg: {
724 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
725 SDValue Val = Node->getOperand(2);
726 const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
727 if (RC != &AMDGPU::SReg_64RegClass) {
731 if (!Val.getNode()->isMachineOpcode() ||
732 Val.getNode()->getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
736 const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
737 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
738 RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
743 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
747 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
748 if (ResNode != Node) {
749 ReplaceUses(Node, ResNode);