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(),
284 AMDGPU::OpName::literal);
285 assert(ImmIdx != -1);
287 // subtract one from ImmIdx, because the DST operand is usually index
288 // 0 for MachineInstrs, but we have no DST in the Ops vector.
291 // Check that we aren't already using an immediate.
292 // XXX: It's possible for an instruction to have more than one
293 // immediate operand, but this is not supported yet.
294 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
295 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
298 if (C->getZExtValue() != 0) {
299 // This instruction is already using an immediate.
303 // Set the immediate value
304 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
307 // Set the immediate register
308 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
310 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
315 SDNode *Result = SelectCode(N);
317 // Fold operands of selected node
319 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
320 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
321 const R600InstrInfo *TII =
322 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
323 if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
324 bool IsModified = false;
326 std::vector<SDValue> Ops;
327 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
330 IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
332 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
334 } while (IsModified);
337 if (Result && Result->isMachineOpcode() &&
338 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
339 && TII->isALUInstr(Result->getMachineOpcode())) {
340 // Fold FNEG/FABS/CONST_ADDRESS
341 // TODO: Isel can generate multiple MachineInst, we need to recursively
343 bool IsModified = false;
345 std::vector<SDValue> Ops;
346 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
349 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
351 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
353 } while (IsModified);
355 // If node has a single use which is CLAMP_R600, folds it
356 if (Result->hasOneUse() && Result->isMachineOpcode()) {
357 SDNode *PotentialClamp = *Result->use_begin();
358 if (PotentialClamp->isMachineOpcode() &&
359 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
361 TII->getOperandIdx(Result->getMachineOpcode(), AMDGPU::OpName::clamp);
362 std::vector<SDValue> Ops;
363 unsigned NumOp = Result->getNumOperands();
364 for (unsigned i = 0; i < NumOp; ++i) {
365 Ops.push_back(Result->getOperand(i));
367 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
368 Result = CurDAG->SelectNodeTo(PotentialClamp,
369 Result->getMachineOpcode(), PotentialClamp->getVTList(),
379 bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
380 SDValue &Abs, const R600InstrInfo *TII,
381 std::vector<unsigned> Consts) {
382 switch (Src.getOpcode()) {
383 case AMDGPUISD::CONST_ADDRESS: {
385 if (Src.getValueType().isVector() ||
386 !SelectGlobalValueConstantOffset(Src.getOperand(0), CstOffset))
389 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
390 Consts.push_back(Cst->getZExtValue());
391 if (!TII->fitsConstReadLimitations(Consts))
394 Src = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
399 Src = Src.getOperand(0);
400 Neg = CurDAG->getTargetConstant(1, MVT::i32);
405 Src = Src.getOperand(0);
406 Abs = CurDAG->getTargetConstant(1, MVT::i32);
409 Src = Src.getOperand(0);
416 bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
417 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
419 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
420 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
421 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
424 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel),
425 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel),
426 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_sel)
429 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
430 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
431 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
434 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
435 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
439 // Gather constants values
440 std::vector<unsigned> Consts;
441 for (unsigned j = 0; j < 3; j++) {
442 int SrcIdx = OperandIdx[j];
445 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
446 if (Reg->getReg() == AMDGPU::ALU_CONST) {
447 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
448 Consts.push_back(Cst->getZExtValue());
453 for (unsigned i = 0; i < 3; i++) {
454 if (OperandIdx[i] < 0)
456 SDValue &Src = Ops[OperandIdx[i] - 1];
457 SDValue &Sel = Ops[SelIdx[i] - 1];
458 SDValue &Neg = Ops[NegIdx[i] - 1];
460 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
461 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
467 bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
468 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
470 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
471 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
472 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
473 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
474 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
475 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
476 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
477 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
480 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_X),
481 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Y),
482 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Z),
483 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_W),
484 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_X),
485 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Y),
486 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Z),
487 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_W)
490 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
491 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
492 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
493 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
494 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
495 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
496 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
497 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
500 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
501 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
502 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
503 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
504 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
505 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
506 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
507 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
510 // Gather constants values
511 std::vector<unsigned> Consts;
512 for (unsigned j = 0; j < 8; j++) {
513 int SrcIdx = OperandIdx[j];
516 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
517 if (Reg->getReg() == AMDGPU::ALU_CONST) {
518 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
519 Consts.push_back(Cst->getZExtValue());
524 for (unsigned i = 0; i < 8; i++) {
525 if (OperandIdx[i] < 0)
527 SDValue &Src = Ops[OperandIdx[i] - 1];
528 SDValue &Sel = Ops[SelIdx[i] - 1];
529 SDValue &Neg = Ops[NegIdx[i] - 1];
530 SDValue &Abs = Ops[AbsIdx[i] - 1];
531 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
537 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
541 Type *ptrType = ptr->getType();
542 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
545 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
546 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
549 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
550 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
551 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
552 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
555 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
556 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
559 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
560 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
563 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) const {
564 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
568 const DataLayout *DL = TM.getDataLayout();
569 MachineMemOperand *MMO = N->getMemOperand();
570 const Value *V = MMO->getValue();
571 const Value *BV = GetUnderlyingObject(V, DL, 0);
574 && ((V && dyn_cast<GlobalValue>(V))
575 || (BV && dyn_cast<GlobalValue>(
576 GetUnderlyingObject(MMO->getValue(), DL, 0))))) {
577 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
583 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
584 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
587 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
588 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
591 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
592 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
595 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
596 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
599 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
600 MachineMemOperand *MMO = N->getMemOperand();
601 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
603 const Value *V = MMO->getValue();
604 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
605 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
613 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
614 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
615 // Check to make sure we are not a constant pool load or a constant load
616 // that is marked as a private load
617 if (isCPLoad(N) || isConstantLoad(N, -1)) {
621 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
622 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
623 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
624 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
625 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
626 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
632 const char *AMDGPUDAGToDAGISel::getPassName() const {
633 return "AMDGPU DAG->DAG Pattern Instruction Selection";
641 ///==== AMDGPU Functions ====///
643 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
645 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
646 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
652 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
653 SDValue& BaseReg, SDValue &Offset) {
654 if (!dyn_cast<ConstantSDNode>(Addr)) {
656 Offset = CurDAG->getIntPtrConstant(0, true);
662 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
664 ConstantSDNode * IMMOffset;
666 if (Addr.getOpcode() == ISD::ADD
667 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
668 && isInt<16>(IMMOffset->getZExtValue())) {
670 Base = Addr.getOperand(0);
671 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
673 // If the pointer address is constant, we can move it to the offset field.
674 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
675 && isInt<16>(IMMOffset->getZExtValue())) {
676 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
677 SDLoc(CurDAG->getEntryNode()),
678 AMDGPU::ZERO, MVT::i32);
679 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
683 // Default case, no offset
685 Offset = CurDAG->getTargetConstant(0, MVT::i32);
689 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
693 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
694 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
695 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
696 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
697 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
698 Base = Addr.getOperand(0);
699 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
702 Offset = CurDAG->getTargetConstant(0, MVT::i32);
708 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
710 if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
714 // Go over all selected nodes and try to fold them a bit more
715 const AMDGPUTargetLowering& Lowering =
716 (*(const AMDGPUTargetLowering*)getTargetLowering());
717 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
718 E = CurDAG->allnodes_end(); I != E; ++I) {
721 switch (Node->getOpcode()) {
722 // Fix the register class in copy to CopyToReg nodes - ISel will always
723 // use SReg classes for 64-bit copies, but this is not always what we want.
724 case ISD::CopyToReg: {
725 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
726 SDValue Val = Node->getOperand(2);
727 const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
728 if (RC != &AMDGPU::SReg_64RegClass) {
732 if (!Val.getNode()->isMachineOpcode() ||
733 Val.getNode()->getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
737 const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
738 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
739 RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
744 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
748 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
749 if (ResNode != Node) {
750 ReplaceUses(Node, ResNode);