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 "SIDefines.h"
20 #include "SIISelLowering.h"
21 #include "SIMachineFunctionInfo.h"
22 #include "llvm/CodeGen/FunctionLoweringInfo.h"
23 #include "llvm/CodeGen/PseudoSourceValue.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/SelectionDAGISel.h"
28 #include "llvm/IR/Function.h"
32 //===----------------------------------------------------------------------===//
33 // Instruction Selector Implementation
34 //===----------------------------------------------------------------------===//
37 /// AMDGPU specific code to select AMDGPU machine instructions for
38 /// SelectionDAG operations.
39 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
40 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
41 // make the right decision when generating code for different targets.
42 const AMDGPUSubtarget &Subtarget;
44 AMDGPUDAGToDAGISel(TargetMachine &TM);
45 virtual ~AMDGPUDAGToDAGISel();
47 SDNode *Select(SDNode *N) override;
48 const char *getPassName() const override;
49 void PostprocessISelDAG() override;
52 bool isInlineImmediate(SDNode *N) const;
53 inline SDValue getSmallIPtrImm(unsigned Imm);
54 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
55 const R600InstrInfo *TII);
56 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
57 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
59 // Complex pattern selectors
60 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
61 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
62 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
64 static bool checkType(const Value *ptr, unsigned int addrspace);
65 static bool checkPrivateAddress(const MachineMemOperand *Op);
67 static bool isGlobalStore(const StoreSDNode *N);
68 static bool isPrivateStore(const StoreSDNode *N);
69 static bool isLocalStore(const StoreSDNode *N);
70 static bool isRegionStore(const StoreSDNode *N);
72 bool isCPLoad(const LoadSDNode *N) const;
73 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
74 bool isGlobalLoad(const LoadSDNode *N) const;
75 bool isParamLoad(const LoadSDNode *N) const;
76 bool isPrivateLoad(const LoadSDNode *N) const;
77 bool isLocalLoad(const LoadSDNode *N) const;
78 bool isRegionLoad(const LoadSDNode *N) const;
80 /// \returns True if the current basic block being selected is at control
81 /// flow depth 0. Meaning that the current block dominates the
83 bool isCFDepth0() const;
85 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
86 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
87 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
89 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
90 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
91 bool isDSOffsetLegal(const SDValue &Base, unsigned Offset,
92 unsigned OffsetBits) const;
93 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
94 void SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
95 SDValue &SOffset, SDValue &Offset, SDValue &Offen,
96 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
98 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
99 SDValue &Offset) const;
100 bool SelectMUBUFScratch(SDValue Addr, SDValue &RSrc, SDValue &VAddr,
101 SDValue &SOffset, SDValue &ImmOffset) const;
102 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
103 SDValue &Offset, SDValue &GLC, SDValue &SLC,
105 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
106 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
107 SDValue &Clamp, SDValue &Omod) const;
109 SDNode *SelectADD_SUB_I64(SDNode *N);
110 SDNode *SelectDIV_SCALE(SDNode *N);
112 // Include the pieces autogenerated from the target description.
113 #include "AMDGPUGenDAGISel.inc"
115 } // end anonymous namespace
117 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
118 // DAG, ready for instruction scheduling.
119 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) {
120 return new AMDGPUDAGToDAGISel(TM);
123 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
124 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
127 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
130 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
131 const SITargetLowering *TL
132 = static_cast<const SITargetLowering *>(getTargetLowering());
133 return TL->analyzeImmediate(N) == 0;
136 /// \brief Determine the register class for \p OpNo
137 /// \returns The register class of the virtual register that will be used for
138 /// the given operand number \OpNo or NULL if the register class cannot be
140 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
141 unsigned OpNo) const {
142 if (!N->isMachineOpcode())
145 switch (N->getMachineOpcode()) {
147 const MCInstrDesc &Desc =
148 TM.getSubtargetImpl()->getInstrInfo()->get(N->getMachineOpcode());
149 unsigned OpIdx = Desc.getNumDefs() + OpNo;
150 if (OpIdx >= Desc.getNumOperands())
152 int RegClass = Desc.OpInfo[OpIdx].RegClass;
156 return TM.getSubtargetImpl()->getRegisterInfo()->getRegClass(RegClass);
158 case AMDGPU::REG_SEQUENCE: {
159 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
160 const TargetRegisterClass *SuperRC =
161 TM.getSubtargetImpl()->getRegisterInfo()->getRegClass(RCID);
163 SDValue SubRegOp = N->getOperand(OpNo + 1);
164 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
165 return TM.getSubtargetImpl()->getRegisterInfo()->getSubClassWithSubReg(
171 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
172 return CurDAG->getTargetConstant(Imm, MVT::i32);
175 bool AMDGPUDAGToDAGISel::SelectADDRParam(
176 SDValue Addr, SDValue& R1, SDValue& R2) {
178 if (Addr.getOpcode() == ISD::FrameIndex) {
179 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
180 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
181 R2 = CurDAG->getTargetConstant(0, MVT::i32);
184 R2 = CurDAG->getTargetConstant(0, MVT::i32);
186 } else if (Addr.getOpcode() == ISD::ADD) {
187 R1 = Addr.getOperand(0);
188 R2 = Addr.getOperand(1);
191 R2 = CurDAG->getTargetConstant(0, MVT::i32);
196 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
197 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
198 Addr.getOpcode() == ISD::TargetGlobalAddress) {
201 return SelectADDRParam(Addr, R1, R2);
205 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
206 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
207 Addr.getOpcode() == ISD::TargetGlobalAddress) {
211 if (Addr.getOpcode() == ISD::FrameIndex) {
212 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
213 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
214 R2 = CurDAG->getTargetConstant(0, MVT::i64);
217 R2 = CurDAG->getTargetConstant(0, MVT::i64);
219 } else if (Addr.getOpcode() == ISD::ADD) {
220 R1 = Addr.getOperand(0);
221 R2 = Addr.getOperand(1);
224 R2 = CurDAG->getTargetConstant(0, MVT::i64);
229 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
230 unsigned int Opc = N->getOpcode();
231 if (N->isMachineOpcode()) {
233 return nullptr; // Already selected.
236 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
239 // We are selecting i64 ADD here instead of custom lower it during
240 // DAG legalization, so we can fold some i64 ADDs used for address
241 // calculation into the LOAD and STORE instructions.
244 if (N->getValueType(0) != MVT::i64 ||
245 ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
248 return SelectADD_SUB_I64(N);
250 case ISD::SCALAR_TO_VECTOR:
251 case AMDGPUISD::BUILD_VERTICAL_VECTOR:
252 case ISD::BUILD_VECTOR: {
254 const AMDGPURegisterInfo *TRI = static_cast<const AMDGPURegisterInfo *>(
255 TM.getSubtargetImpl()->getRegisterInfo());
256 const SIRegisterInfo *SIRI = static_cast<const SIRegisterInfo *>(
257 TM.getSubtargetImpl()->getRegisterInfo());
258 EVT VT = N->getValueType(0);
259 unsigned NumVectorElts = VT.getVectorNumElements();
260 EVT EltVT = VT.getVectorElementType();
261 assert(EltVT.bitsEq(MVT::i32));
262 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
264 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
266 if (!U->isMachineOpcode()) {
269 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
273 if (SIRI->isSGPRClass(RC)) {
277 switch(NumVectorElts) {
278 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
279 AMDGPU::SReg_32RegClassID;
281 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
282 AMDGPU::SReg_64RegClassID;
284 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
285 AMDGPU::SReg_128RegClassID;
287 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
288 AMDGPU::SReg_256RegClassID;
290 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
291 AMDGPU::SReg_512RegClassID;
293 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
296 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
297 // that adds a 128 bits reg copy when going through TwoAddressInstructions
298 // pass. We want to avoid 128 bits copies as much as possible because they
299 // can't be bundled by our scheduler.
300 switch(NumVectorElts) {
301 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
303 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
304 RegClassID = AMDGPU::R600_Reg128VerticalRegClassID;
306 RegClassID = AMDGPU::R600_Reg128RegClassID;
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, EltVT,
316 N->getOperand(0), RegClass);
319 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
321 // 16 = Max Num Vector Elements
322 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
323 // 1 = Vector Register Class
324 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
326 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
327 bool IsRegSeq = true;
328 unsigned NOps = N->getNumOperands();
329 for (unsigned i = 0; i < NOps; 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);
340 if (NOps != NumVectorElts) {
341 // Fill in the missing undef elements if this was a scalar_to_vector.
342 assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
344 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
346 for (unsigned i = NOps; i < NumVectorElts; ++i) {
347 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
348 RegSeqArgs[1 + (2 * i) + 1] =
349 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
355 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
358 case ISD::BUILD_PAIR: {
359 SDValue RC, SubReg0, SubReg1;
360 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
363 if (N->getValueType(0) == MVT::i128) {
364 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
365 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
366 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
367 } else if (N->getValueType(0) == MVT::i64) {
368 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
369 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
370 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
372 llvm_unreachable("Unhandled value type for BUILD_PAIR");
374 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
375 N->getOperand(1), SubReg1 };
376 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
377 SDLoc(N), N->getValueType(0), Ops);
381 case ISD::ConstantFP: {
382 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
383 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
384 N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
388 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
389 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
391 ConstantSDNode *C = cast<ConstantSDNode>(N);
392 Imm = C->getZExtValue();
395 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
396 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
397 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
398 CurDAG->getConstant(Imm >> 32, MVT::i32));
399 const SDValue Ops[] = {
400 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
401 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
402 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
405 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
406 N->getValueType(0), Ops);
409 case AMDGPUISD::REGISTER_LOAD: {
410 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
412 SDValue Addr, Offset;
414 SelectADDRIndirect(N->getOperand(1), Addr, Offset);
415 const SDValue Ops[] = {
418 CurDAG->getTargetConstant(0, MVT::i32),
421 return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
422 CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
425 case AMDGPUISD::REGISTER_STORE: {
426 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
428 SDValue Addr, Offset;
429 SelectADDRIndirect(N->getOperand(2), Addr, Offset);
430 const SDValue Ops[] = {
434 CurDAG->getTargetConstant(0, MVT::i32),
437 return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
438 CurDAG->getVTList(MVT::Other),
442 case AMDGPUISD::BFE_I32:
443 case AMDGPUISD::BFE_U32: {
444 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
447 // There is a scalar version available, but unlike the vector version which
448 // has a separate operand for the offset and width, the scalar version packs
449 // the width and offset into a single operand. Try to move to the scalar
450 // version if the offsets are constant, so that we can try to keep extended
451 // loads of kernel arguments in SGPRs.
453 // TODO: Technically we could try to pattern match scalar bitshifts of
454 // dynamic values, but it's probably not useful.
455 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
459 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
463 bool Signed = Opc == AMDGPUISD::BFE_I32;
465 // Transformation function, pack the offset and width of a BFE into
466 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
467 // source, bits [5:0] contain the offset and bits [22:16] the width.
469 uint32_t OffsetVal = Offset->getZExtValue();
470 uint32_t WidthVal = Width->getZExtValue();
472 uint32_t PackedVal = OffsetVal | WidthVal << 16;
474 SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
475 return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
482 case AMDGPUISD::DIV_SCALE: {
483 return SelectDIV_SCALE(N);
486 return SelectCode(N);
490 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
491 assert(AS != 0 && "Use checkPrivateAddress instead.");
495 return Ptr->getType()->getPointerAddressSpace() == AS;
498 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
499 if (Op->getPseudoValue())
502 if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
503 return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
508 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
509 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
512 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
513 const Value *MemVal = N->getMemOperand()->getValue();
514 return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
515 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
516 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
519 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
520 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
523 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
524 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
527 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
528 const Value *MemVal = N->getMemOperand()->getValue();
530 return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
532 return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
535 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
536 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
537 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
538 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
539 N->getMemoryVT().bitsLT(MVT::i32)) {
543 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
546 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
547 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
550 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
551 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
554 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
555 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
558 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
559 MachineMemOperand *MMO = N->getMemOperand();
560 if (checkPrivateAddress(N->getMemOperand())) {
562 const PseudoSourceValue *PSV = MMO->getPseudoValue();
563 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
571 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
572 if (checkPrivateAddress(N->getMemOperand())) {
573 // Check to make sure we are not a constant pool load or a constant load
574 // that is marked as a private load
575 if (isCPLoad(N) || isConstantLoad(N, -1)) {
580 const Value *MemVal = N->getMemOperand()->getValue();
581 if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
582 !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
583 !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
584 !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
585 !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
586 !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
592 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
593 // FIXME: Figure out a way to use DominatorTree analysis here.
594 const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
595 const Function *Fn = FuncInfo->Fn;
596 return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
600 const char *AMDGPUDAGToDAGISel::getPassName() const {
601 return "AMDGPU DAG->DAG Pattern Instruction Selection";
609 //===----------------------------------------------------------------------===//
611 //===----------------------------------------------------------------------===//
613 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
615 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
616 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
622 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
623 SDValue& BaseReg, SDValue &Offset) {
624 if (!isa<ConstantSDNode>(Addr)) {
626 Offset = CurDAG->getIntPtrConstant(0, true);
632 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
634 ConstantSDNode *IMMOffset;
636 if (Addr.getOpcode() == ISD::ADD
637 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
638 && isInt<16>(IMMOffset->getZExtValue())) {
640 Base = Addr.getOperand(0);
641 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
643 // If the pointer address is constant, we can move it to the offset field.
644 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
645 && isInt<16>(IMMOffset->getZExtValue())) {
646 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
647 SDLoc(CurDAG->getEntryNode()),
648 AMDGPU::ZERO, MVT::i32);
649 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
653 // Default case, no offset
655 Offset = CurDAG->getTargetConstant(0, MVT::i32);
659 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
663 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
664 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
665 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
666 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
667 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
668 Base = Addr.getOperand(0);
669 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
672 Offset = CurDAG->getTargetConstant(0, MVT::i32);
678 SDNode *AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
680 SDValue LHS = N->getOperand(0);
681 SDValue RHS = N->getOperand(1);
683 bool IsAdd = (N->getOpcode() == ISD::ADD);
685 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
686 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
688 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
689 DL, MVT::i32, LHS, Sub0);
690 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
691 DL, MVT::i32, LHS, Sub1);
693 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
694 DL, MVT::i32, RHS, Sub0);
695 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
696 DL, MVT::i32, RHS, Sub1);
698 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
699 SDValue AddLoArgs[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
702 unsigned Opc = IsAdd ? AMDGPU::S_ADD_I32 : AMDGPU::S_SUB_I32;
703 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
706 Opc = IsAdd ? AMDGPU::V_ADD_I32_e32 : AMDGPU::V_SUB_I32_e32;
707 CarryOpc = IsAdd ? AMDGPU::V_ADDC_U32_e32 : AMDGPU::V_SUBB_U32_e32;
710 SDNode *AddLo = CurDAG->getMachineNode( Opc, DL, VTList, AddLoArgs);
711 SDValue Carry(AddLo, 1);
713 = CurDAG->getMachineNode(CarryOpc, DL, MVT::i32,
714 SDValue(Hi0, 0), SDValue(Hi1, 0), Carry);
717 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
723 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args);
726 SDNode *AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
728 EVT VT = N->getValueType(0);
730 assert(VT == MVT::f32 || VT == MVT::f64);
733 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
735 const SDValue Zero = CurDAG->getTargetConstant(0, MVT::i32);
747 return CurDAG->SelectNodeTo(N, Opc, VT, MVT::i1, Ops);
750 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset,
751 unsigned OffsetBits) const {
752 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
753 if ((OffsetBits == 16 && !isUInt<16>(Offset)) ||
754 (OffsetBits == 8 && !isUInt<8>(Offset)))
757 if (ST.getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS)
760 // On Southern Islands instruction with a negative base value and an offset
761 // don't seem to work.
762 return CurDAG->SignBitIsZero(Base);
765 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
766 SDValue &Offset) const {
767 if (CurDAG->isBaseWithConstantOffset(Addr)) {
768 SDValue N0 = Addr.getOperand(0);
769 SDValue N1 = Addr.getOperand(1);
770 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
771 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) {
781 Offset = CurDAG->getTargetConstant(0, MVT::i16);
785 static SDValue wrapAddr64Rsrc(SelectionDAG *DAG, SDLoc DL, SDValue Ptr) {
786 return SDValue(DAG->getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::v4i32,
790 static bool isLegalMUBUFImmOffset(const ConstantSDNode *Imm) {
791 return isUInt<12>(Imm->getZExtValue());
794 void AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
795 SDValue &VAddr, SDValue &SOffset,
796 SDValue &Offset, SDValue &Offen,
797 SDValue &Idxen, SDValue &Addr64,
798 SDValue &GLC, SDValue &SLC,
799 SDValue &TFE) const {
802 GLC = CurDAG->getTargetConstant(0, MVT::i1);
803 SLC = CurDAG->getTargetConstant(0, MVT::i1);
804 TFE = CurDAG->getTargetConstant(0, MVT::i1);
806 Idxen = CurDAG->getTargetConstant(0, MVT::i1);
807 Offen = CurDAG->getTargetConstant(0, MVT::i1);
808 Addr64 = CurDAG->getTargetConstant(0, MVT::i1);
809 SOffset = CurDAG->getTargetConstant(0, MVT::i32);
811 if (CurDAG->isBaseWithConstantOffset(Addr)) {
812 SDValue N0 = Addr.getOperand(0);
813 SDValue N1 = Addr.getOperand(1);
814 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
816 if (isLegalMUBUFImmOffset(C1)) {
818 if (N0.getOpcode() == ISD::ADD) {
819 // (add (add N2, N3), C1) -> addr64
820 SDValue N2 = N0.getOperand(0);
821 SDValue N3 = N0.getOperand(1);
822 Addr64 = CurDAG->getTargetConstant(1, MVT::i1);
825 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
829 // (add N0, C1) -> offset
830 VAddr = CurDAG->getTargetConstant(0, MVT::i32);
832 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
836 if (Addr.getOpcode() == ISD::ADD) {
837 // (add N0, N1) -> addr64
838 SDValue N0 = Addr.getOperand(0);
839 SDValue N1 = Addr.getOperand(1);
840 Addr64 = CurDAG->getTargetConstant(1, MVT::i1);
843 Offset = CurDAG->getTargetConstant(0, MVT::i16);
847 // default case -> offset
848 VAddr = CurDAG->getTargetConstant(0, MVT::i32);
850 Offset = CurDAG->getTargetConstant(0, MVT::i16);
854 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
856 SDValue &Offset) const {
857 SDValue Ptr, SOffset, Offen, Idxen, Addr64, GLC, SLC, TFE;
859 SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
862 ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
863 if (C->getSExtValue()) {
865 SRsrc = wrapAddr64Rsrc(CurDAG, DL, Ptr);
871 static SDValue buildRSRC(SelectionDAG *DAG, SDLoc DL, SDValue Ptr,
872 uint32_t RsrcDword1, uint64_t RsrcDword2And3) {
874 SDValue PtrLo = DAG->getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
875 SDValue PtrHi = DAG->getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
877 PtrHi = SDValue(DAG->getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
878 DAG->getConstant(RsrcDword1, MVT::i32)), 0);
880 SDValue DataLo = DAG->getTargetConstant(
881 RsrcDword2And3 & APInt::getAllOnesValue(32).getZExtValue(), MVT::i32);
882 SDValue DataHi = DAG->getTargetConstant(RsrcDword2And3 >> 32, MVT::i32);
884 const SDValue Ops[] = { PtrLo, PtrHi, DataLo, DataHi };
885 return SDValue(DAG->getMachineNode(AMDGPU::SI_BUFFER_RSRC, DL,
886 MVT::v4i32, Ops), 0);
889 /// \brief Return a resource descriptor with the 'Add TID' bit enabled
890 /// The TID (Thread ID) is multipled by the stride value (bits [61:48]
891 /// of the resource descriptor) to create an offset, which is added to the
893 static SDValue buildScratchRSRC(SelectionDAG *DAG, SDLoc DL, SDValue Ptr) {
895 uint64_t Rsrc = AMDGPU::RSRC_DATA_FORMAT | AMDGPU::RSRC_TID_ENABLE |
898 return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
901 bool AMDGPUDAGToDAGISel::SelectMUBUFScratch(SDValue Addr, SDValue &Rsrc,
902 SDValue &VAddr, SDValue &SOffset,
903 SDValue &ImmOffset) const {
906 MachineFunction &MF = CurDAG->getMachineFunction();
907 const SIRegisterInfo *TRI =
908 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
909 MachineRegisterInfo &MRI = MF.getRegInfo();
910 const SITargetLowering& Lowering =
911 *static_cast<const SITargetLowering*>(getTargetLowering());
913 unsigned ScratchPtrReg =
914 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
915 unsigned ScratchOffsetReg =
916 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_WAVE_OFFSET);
917 Lowering.CreateLiveInRegister(*CurDAG, &AMDGPU::SReg_32RegClass,
918 ScratchOffsetReg, MVT::i32);
920 Rsrc = buildScratchRSRC(CurDAG, DL,
921 CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
922 MRI.getLiveInVirtReg(ScratchPtrReg), MVT::i64));
923 SOffset = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
924 MRI.getLiveInVirtReg(ScratchOffsetReg), MVT::i32);
927 if (CurDAG->isBaseWithConstantOffset(Addr)) {
928 SDValue N1 = Addr.getOperand(1);
929 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
931 if (isLegalMUBUFImmOffset(C1)) {
932 VAddr = Addr.getOperand(0);
933 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), MVT::i16);
939 if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
940 isa<FrameIndexSDNode>(Addr.getOperand(0))) {
941 VAddr = Addr.getOperand(1);
942 ImmOffset = Addr.getOperand(0);
947 if (isa<FrameIndexSDNode>(Addr)) {
948 VAddr = SDValue(CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, DL, MVT::i32,
949 CurDAG->getConstant(0, MVT::i32)), 0);
956 ImmOffset = CurDAG->getTargetConstant(0, MVT::i16);
960 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
961 SDValue &SOffset, SDValue &Offset,
962 SDValue &GLC, SDValue &SLC,
963 SDValue &TFE) const {
964 SDValue Ptr, VAddr, Offen, Idxen, Addr64;
966 SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
969 if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
970 !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
971 !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
972 uint64_t Rsrc = AMDGPU::RSRC_DATA_FORMAT |
973 APInt::getAllOnesValue(32).getZExtValue(); // Size
975 SRsrc = buildRSRC(CurDAG, DL, Ptr, 0, Rsrc);
981 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
982 SDValue &SrcMods) const {
988 if (Src.getOpcode() == ISD::FNEG) {
989 Mods |= SISrcMods::NEG;
990 Src = Src.getOperand(0);
993 if (Src.getOpcode() == ISD::FABS) {
994 Mods |= SISrcMods::ABS;
995 Src = Src.getOperand(0);
998 SrcMods = CurDAG->getTargetConstant(Mods, MVT::i32);
1003 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
1004 SDValue &SrcMods, SDValue &Clamp,
1005 SDValue &Omod) const {
1006 // FIXME: Handle Clamp and Omod
1007 Clamp = CurDAG->getTargetConstant(0, MVT::i32);
1008 Omod = CurDAG->getTargetConstant(0, MVT::i32);
1010 return SelectVOP3Mods(In, Src, SrcMods);
1013 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
1014 const AMDGPUTargetLowering& Lowering =
1015 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
1016 bool IsModified = false;
1019 // Go over all selected nodes and try to fold them a bit more
1020 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1021 E = CurDAG->allnodes_end(); I != E; ++I) {
1025 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1029 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
1030 if (ResNode != Node) {
1031 ReplaceUses(Node, ResNode);
1035 CurDAG->RemoveDeadNodes();
1036 } while (IsModified);