1 //===-- AMDGPUISelDAGToDAG.cpp - A dag to dag inst selector for AMDGPU ----===//
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 /// Defines an instruction selector for the AMDGPU target.
13 //===----------------------------------------------------------------------===//
16 #include "AMDGPUArgumentUsageInfo.h"
17 #include "AMDGPUISelLowering.h" // For AMDGPUISD
18 #include "AMDGPUInstrInfo.h"
19 #include "AMDGPUPerfHintAnalysis.h"
20 #include "AMDGPURegisterInfo.h"
21 #include "AMDGPUSubtarget.h"
22 #include "AMDGPUTargetMachine.h"
23 #include "SIDefines.h"
24 #include "SIISelLowering.h"
25 #include "SIInstrInfo.h"
26 #include "SIMachineFunctionInfo.h"
27 #include "SIRegisterInfo.h"
28 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
29 #include "llvm/ADT/APInt.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/Analysis/DivergenceAnalysis.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/CodeGen/FunctionLoweringInfo.h"
35 #include "llvm/CodeGen/ISDOpcodes.h"
36 #include "llvm/CodeGen/MachineFunction.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/SelectionDAG.h"
39 #include "llvm/CodeGen/SelectionDAGISel.h"
40 #include "llvm/CodeGen/SelectionDAGNodes.h"
41 #include "llvm/CodeGen/ValueTypes.h"
42 #include "llvm/IR/BasicBlock.h"
43 #include "llvm/IR/Instruction.h"
44 #include "llvm/MC/MCInstrDesc.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/CodeGen.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/MachineValueType.h"
49 #include "llvm/Support/MathExtras.h"
61 } // end namespace llvm
63 //===----------------------------------------------------------------------===//
64 // Instruction Selector Implementation
65 //===----------------------------------------------------------------------===//
69 /// AMDGPU specific code to select AMDGPU machine instructions for
70 /// SelectionDAG operations.
71 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
72 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
73 // make the right decision when generating code for different targets.
74 const GCNSubtarget *Subtarget;
76 bool EnableLateStructurizeCFG;
79 explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr,
80 CodeGenOpt::Level OptLevel = CodeGenOpt::Default)
81 : SelectionDAGISel(*TM, OptLevel) {
82 AMDGPUASI = AMDGPU::getAMDGPUAS(*TM);
83 EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG;
85 ~AMDGPUDAGToDAGISel() override = default;
87 void getAnalysisUsage(AnalysisUsage &AU) const override {
88 AU.addRequired<AMDGPUArgumentUsageInfo>();
89 AU.addRequired<AMDGPUPerfHintAnalysis>();
90 AU.addRequired<DivergenceAnalysis>();
91 SelectionDAGISel::getAnalysisUsage(AU);
94 bool runOnMachineFunction(MachineFunction &MF) override;
95 void Select(SDNode *N) override;
96 StringRef getPassName() const override;
97 void PostprocessISelDAG() override;
100 void SelectBuildVector(SDNode *N, unsigned RegClassID);
103 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const;
104 bool isNoNanSrc(SDValue N) const;
105 bool isInlineImmediate(const SDNode *N) const;
107 bool isUniformBr(const SDNode *N) const;
109 SDNode *glueCopyToM0(SDNode *N) const;
111 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
112 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
113 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
114 bool isDSOffsetLegal(const SDValue &Base, unsigned Offset,
115 unsigned OffsetBits) const;
116 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const;
117 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0,
118 SDValue &Offset1) const;
119 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
120 SDValue &SOffset, SDValue &Offset, SDValue &Offen,
121 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC,
123 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr,
124 SDValue &SOffset, SDValue &Offset, SDValue &GLC,
125 SDValue &SLC, SDValue &TFE) const;
126 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
127 SDValue &VAddr, SDValue &SOffset, SDValue &Offset,
129 bool SelectMUBUFScratchOffen(SDNode *Parent,
130 SDValue Addr, SDValue &RSrc, SDValue &VAddr,
131 SDValue &SOffset, SDValue &ImmOffset) const;
132 bool SelectMUBUFScratchOffset(SDNode *Parent,
133 SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
134 SDValue &Offset) const;
136 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset,
137 SDValue &Offset, SDValue &GLC, SDValue &SLC,
139 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
140 SDValue &Offset, SDValue &SLC) const;
141 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset,
142 SDValue &Offset) const;
143 bool SelectMUBUFConstant(SDValue Constant,
145 SDValue &ImmOffset) const;
146 bool SelectMUBUFIntrinsicOffset(SDValue Offset, SDValue &SOffset,
147 SDValue &ImmOffset) const;
148 bool SelectMUBUFIntrinsicVOffset(SDValue Offset, SDValue &SOffset,
149 SDValue &ImmOffset, SDValue &VOffset) const;
151 bool SelectFlatAtomic(SDValue Addr, SDValue &VAddr,
152 SDValue &Offset, SDValue &SLC) const;
153 bool SelectFlatAtomicSigned(SDValue Addr, SDValue &VAddr,
154 SDValue &Offset, SDValue &SLC) const;
156 template <bool IsSigned>
157 bool SelectFlatOffset(SDValue Addr, SDValue &VAddr,
158 SDValue &Offset, SDValue &SLC) const;
160 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset,
162 SDValue Expand32BitAddress(SDValue Addr) const;
163 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset,
165 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
166 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
167 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const;
168 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const;
169 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const;
170 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const;
172 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const;
173 bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods) const;
174 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
175 bool SelectVOP3NoMods(SDValue In, SDValue &Src) const;
176 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods,
177 SDValue &Clamp, SDValue &Omod) const;
178 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
179 SDValue &Clamp, SDValue &Omod) const;
181 bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods,
183 SDValue &Omod) const;
185 bool SelectVOP3OMods(SDValue In, SDValue &Src,
186 SDValue &Clamp, SDValue &Omod) const;
188 bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
189 bool SelectVOP3PMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
190 SDValue &Clamp) const;
192 bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const;
193 bool SelectVOP3OpSel0(SDValue In, SDValue &Src, SDValue &SrcMods,
194 SDValue &Clamp) const;
196 bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
197 bool SelectVOP3OpSelMods0(SDValue In, SDValue &Src, SDValue &SrcMods,
198 SDValue &Clamp) const;
199 bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const;
200 bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const;
202 bool SelectHi16Elt(SDValue In, SDValue &Src) const;
204 void SelectADD_SUB_I64(SDNode *N);
205 void SelectUADDO_USUBO(SDNode *N);
206 void SelectDIV_SCALE(SDNode *N);
207 void SelectMAD_64_32(SDNode *N);
208 void SelectFMA_W_CHAIN(SDNode *N);
209 void SelectFMUL_W_CHAIN(SDNode *N);
211 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val,
212 uint32_t Offset, uint32_t Width);
213 void SelectS_BFEFromShifts(SDNode *N);
214 void SelectS_BFE(SDNode *N);
215 bool isCBranchSCC(const SDNode *N) const;
216 void SelectBRCOND(SDNode *N);
217 void SelectFMAD_FMA(SDNode *N);
218 void SelectATOMIC_CMP_SWAP(SDNode *N);
221 // Include the pieces autogenerated from the target description.
222 #include "AMDGPUGenDAGISel.inc"
225 class R600DAGToDAGISel : public AMDGPUDAGToDAGISel {
226 const R600Subtarget *Subtarget;
229 bool isConstantLoad(const MemSDNode *N, int cbID) const;
230 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
231 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
234 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) :
235 AMDGPUDAGToDAGISel(TM, OptLevel) {
236 AMDGPUASI = AMDGPU::getAMDGPUAS(*TM);
239 void Select(SDNode *N) override;
241 bool SelectADDRIndirect(SDValue Addr, SDValue &Base,
242 SDValue &Offset) override;
243 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
244 SDValue &Offset) override;
246 bool runOnMachineFunction(MachineFunction &MF) override;
248 // Include the pieces autogenerated from the target description.
249 #include "R600GenDAGISel.inc"
252 } // end anonymous namespace
254 INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "isel",
255 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
256 INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo)
257 INITIALIZE_PASS_DEPENDENCY(AMDGPUPerfHintAnalysis)
258 INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
259 INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "isel",
260 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false)
262 /// This pass converts a legalized DAG into a AMDGPU-specific
263 // DAG, ready for instruction scheduling.
264 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM,
265 CodeGenOpt::Level OptLevel) {
266 return new AMDGPUDAGToDAGISel(TM, OptLevel);
269 /// This pass converts a legalized DAG into a R600-specific
270 // DAG, ready for instruction scheduling.
271 FunctionPass *llvm::createR600ISelDag(TargetMachine *TM,
272 CodeGenOpt::Level OptLevel) {
273 return new R600DAGToDAGISel(TM, OptLevel);
276 bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
277 Subtarget = &MF.getSubtarget<GCNSubtarget>();
278 return SelectionDAGISel::runOnMachineFunction(MF);
281 bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const {
282 if (TM.Options.NoNaNsFPMath)
285 // TODO: Move into isKnownNeverNaN
286 if (N->getFlags().isDefined())
287 return N->getFlags().hasNoNaNs();
289 return CurDAG->isKnownNeverNaN(N);
292 bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N) const {
293 const SIInstrInfo *TII = Subtarget->getInstrInfo();
295 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N))
296 return TII->isInlineConstant(C->getAPIntValue());
298 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N))
299 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt());
304 /// Determine the register class for \p OpNo
305 /// \returns The register class of the virtual register that will be used for
306 /// the given operand number \OpNo or NULL if the register class cannot be
308 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
309 unsigned OpNo) const {
310 if (!N->isMachineOpcode()) {
311 if (N->getOpcode() == ISD::CopyToReg) {
312 unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg();
313 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
314 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo();
315 return MRI.getRegClass(Reg);
318 const SIRegisterInfo *TRI
319 = static_cast<const GCNSubtarget *>(Subtarget)->getRegisterInfo();
320 return TRI->getPhysRegClass(Reg);
326 switch (N->getMachineOpcode()) {
328 const MCInstrDesc &Desc =
329 Subtarget->getInstrInfo()->get(N->getMachineOpcode());
330 unsigned OpIdx = Desc.getNumDefs() + OpNo;
331 if (OpIdx >= Desc.getNumOperands())
333 int RegClass = Desc.OpInfo[OpIdx].RegClass;
337 return Subtarget->getRegisterInfo()->getRegClass(RegClass);
339 case AMDGPU::REG_SEQUENCE: {
340 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
341 const TargetRegisterClass *SuperRC =
342 Subtarget->getRegisterInfo()->getRegClass(RCID);
344 SDValue SubRegOp = N->getOperand(OpNo + 1);
345 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
346 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC,
352 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N) const {
353 if (cast<MemSDNode>(N)->getAddressSpace() != AMDGPUASI.LOCAL_ADDRESS ||
354 !Subtarget->ldsRequiresM0Init())
357 const SITargetLowering& Lowering =
358 *static_cast<const SITargetLowering*>(getTargetLowering());
360 // Write max value to m0 before each load operation
362 SDValue M0 = Lowering.copyToM0(*CurDAG, CurDAG->getEntryNode(), SDLoc(N),
363 CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32));
365 SDValue Glue = M0.getValue(1);
367 SmallVector <SDValue, 8> Ops;
368 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
369 Ops.push_back(N->getOperand(i));
372 return CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops);
375 static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) {
376 switch (NumVectorElts) {
378 return AMDGPU::SReg_32_XM0RegClassID;
380 return AMDGPU::SReg_64RegClassID;
382 return AMDGPU::SReg_128RegClassID;
384 return AMDGPU::SReg_256RegClassID;
386 return AMDGPU::SReg_512RegClassID;
389 llvm_unreachable("invalid vector size");
392 static bool getConstantValue(SDValue N, uint32_t &Out) {
393 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
394 Out = C->getAPIntValue().getZExtValue();
398 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) {
399 Out = C->getValueAPF().bitcastToAPInt().getZExtValue();
406 void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) {
407 EVT VT = N->getValueType(0);
408 unsigned NumVectorElts = VT.getVectorNumElements();
409 EVT EltVT = VT.getVectorElementType();
411 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
413 if (NumVectorElts == 1) {
414 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0),
419 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
421 // 16 = Max Num Vector Elements
422 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
423 // 1 = Vector Register Class
424 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
426 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32);
427 bool IsRegSeq = true;
428 unsigned NOps = N->getNumOperands();
429 for (unsigned i = 0; i < NOps; i++) {
430 // XXX: Why is this here?
431 if (isa<RegisterSDNode>(N->getOperand(i))) {
435 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
436 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
437 RegSeqArgs[1 + (2 * i) + 1] = CurDAG->getTargetConstant(Sub, DL, MVT::i32);
439 if (NOps != NumVectorElts) {
440 // Fill in the missing undef elements if this was a scalar_to_vector.
441 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
442 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
444 for (unsigned i = NOps; i < NumVectorElts; ++i) {
445 unsigned Sub = AMDGPURegisterInfo::getSubRegFromChannel(i);
446 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
447 RegSeqArgs[1 + (2 * i) + 1] =
448 CurDAG->getTargetConstant(Sub, DL, MVT::i32);
454 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs);
457 void AMDGPUDAGToDAGISel::Select(SDNode *N) {
458 unsigned int Opc = N->getOpcode();
459 if (N->isMachineOpcode()) {
461 return; // Already selected.
464 if (isa<AtomicSDNode>(N) ||
465 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC ||
466 Opc == AMDGPUISD::ATOMIC_LOAD_FADD ||
467 Opc == AMDGPUISD::ATOMIC_LOAD_FMIN ||
468 Opc == AMDGPUISD::ATOMIC_LOAD_FMAX))
474 // We are selecting i64 ADD here instead of custom lower it during
475 // DAG legalization, so we can fold some i64 ADDs used for address
476 // calculation into the LOAD and STORE instructions.
481 if (N->getValueType(0) != MVT::i64)
484 SelectADD_SUB_I64(N);
489 SelectUADDO_USUBO(N);
492 case AMDGPUISD::FMUL_W_CHAIN: {
493 SelectFMUL_W_CHAIN(N);
496 case AMDGPUISD::FMA_W_CHAIN: {
497 SelectFMA_W_CHAIN(N);
501 case ISD::SCALAR_TO_VECTOR:
502 case ISD::BUILD_VECTOR: {
503 EVT VT = N->getValueType(0);
504 unsigned NumVectorElts = VT.getVectorNumElements();
505 if (VT.getScalarSizeInBits() == 16) {
506 if (Opc == ISD::BUILD_VECTOR && NumVectorElts == 2) {
507 uint32_t LHSVal, RHSVal;
508 if (getConstantValue(N->getOperand(0), LHSVal) &&
509 getConstantValue(N->getOperand(1), RHSVal)) {
510 uint32_t K = LHSVal | (RHSVal << 16);
511 CurDAG->SelectNodeTo(N, AMDGPU::S_MOV_B32, VT,
512 CurDAG->getTargetConstant(K, SDLoc(N), MVT::i32));
520 assert(VT.getVectorElementType().bitsEq(MVT::i32));
521 unsigned RegClassID = selectSGPRVectorRegClassID(NumVectorElts);
522 SelectBuildVector(N, RegClassID);
525 case ISD::BUILD_PAIR: {
526 SDValue RC, SubReg0, SubReg1;
528 if (N->getValueType(0) == MVT::i128) {
529 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32);
530 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32);
531 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32);
532 } else if (N->getValueType(0) == MVT::i64) {
533 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32);
534 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
535 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
537 llvm_unreachable("Unhandled value type for BUILD_PAIR");
539 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
540 N->getOperand(1), SubReg1 };
541 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
542 N->getValueType(0), Ops));
547 case ISD::ConstantFP: {
548 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
552 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
553 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
555 ConstantSDNode *C = cast<ConstantSDNode>(N);
556 Imm = C->getZExtValue();
560 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
561 CurDAG->getConstant(Imm & 0xFFFFFFFF, DL,
563 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
564 CurDAG->getConstant(Imm >> 32, DL, MVT::i32));
565 const SDValue Ops[] = {
566 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
567 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
568 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
571 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL,
572 N->getValueType(0), Ops));
577 case ISD::ATOMIC_LOAD:
578 case ISD::ATOMIC_STORE: {
583 case AMDGPUISD::BFE_I32:
584 case AMDGPUISD::BFE_U32: {
585 // There is a scalar version available, but unlike the vector version which
586 // has a separate operand for the offset and width, the scalar version packs
587 // the width and offset into a single operand. Try to move to the scalar
588 // version if the offsets are constant, so that we can try to keep extended
589 // loads of kernel arguments in SGPRs.
591 // TODO: Technically we could try to pattern match scalar bitshifts of
592 // dynamic values, but it's probably not useful.
593 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
597 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
601 bool Signed = Opc == AMDGPUISD::BFE_I32;
603 uint32_t OffsetVal = Offset->getZExtValue();
604 uint32_t WidthVal = Width->getZExtValue();
606 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
607 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal));
610 case AMDGPUISD::DIV_SCALE: {
614 case AMDGPUISD::MAD_I64_I32:
615 case AMDGPUISD::MAD_U64_U32: {
619 case ISD::CopyToReg: {
620 const SITargetLowering& Lowering =
621 *static_cast<const SITargetLowering*>(getTargetLowering());
622 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG);
628 case ISD::SIGN_EXTEND_INREG:
629 if (N->getValueType(0) != MVT::i32)
641 case AMDGPUISD::ATOMIC_CMP_SWAP:
642 SelectATOMIC_CMP_SWAP(N);
649 bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const {
650 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
651 const Instruction *Term = BB->getTerminator();
652 return Term->getMetadata("amdgpu.uniform") ||
653 Term->getMetadata("structurizecfg.uniform");
656 StringRef AMDGPUDAGToDAGISel::getPassName() const {
657 return "AMDGPU DAG->DAG Pattern Instruction Selection";
660 //===----------------------------------------------------------------------===//
662 //===----------------------------------------------------------------------===//
664 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
669 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
674 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
675 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
676 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
677 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
678 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
679 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
680 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
681 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
682 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
683 Base = Addr.getOperand(0);
684 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
687 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
693 // FIXME: Should only handle addcarry/subcarry
694 void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) {
696 SDValue LHS = N->getOperand(0);
697 SDValue RHS = N->getOperand(1);
699 unsigned Opcode = N->getOpcode();
700 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE);
702 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC;
703 bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE;
705 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32);
706 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32);
708 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
709 DL, MVT::i32, LHS, Sub0);
710 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
711 DL, MVT::i32, LHS, Sub1);
713 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
714 DL, MVT::i32, RHS, Sub0);
715 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
716 DL, MVT::i32, RHS, Sub1);
718 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
720 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
721 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
725 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) };
726 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args);
728 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) };
729 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args);
731 SDValue AddHiArgs[] = {
736 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs);
738 SDValue RegSequenceArgs[] = {
739 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32),
745 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL,
746 MVT::i64, RegSequenceArgs);
749 // Replace the carry-use
750 ReplaceUses(SDValue(N, 1), SDValue(AddHi, 1));
753 // Replace the remaining uses.
754 ReplaceNode(N, RegSequence);
757 void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) {
758 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned
759 // carry out despite the _i32 name. These were renamed in VI to _U32.
760 // FIXME: We should probably rename the opcodes here.
761 unsigned Opc = N->getOpcode() == ISD::UADDO ?
762 AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64;
764 CurDAG->SelectNodeTo(N, Opc, N->getVTList(),
765 { N->getOperand(0), N->getOperand(1) });
768 void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) {
770 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod
773 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]);
774 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
775 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]);
776 Ops[8] = N->getOperand(0);
777 Ops[9] = N->getOperand(4);
779 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops);
782 void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) {
784 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod
787 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]);
788 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]);
789 Ops[6] = N->getOperand(0);
790 Ops[7] = N->getOperand(3);
792 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops);
795 // We need to handle this here because tablegen doesn't support matching
796 // instructions with multiple outputs.
797 void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) {
799 EVT VT = N->getValueType(0);
801 assert(VT == MVT::f32 || VT == MVT::f64);
804 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32;
806 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
807 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
810 // We need to handle this here because tablegen doesn't support matching
811 // instructions with multiple outputs.
812 void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) {
814 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32;
815 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32 : AMDGPU::V_MAD_U64_U32;
817 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1);
818 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
820 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops);
823 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset,
824 unsigned OffsetBits) const {
825 if ((OffsetBits == 16 && !isUInt<16>(Offset)) ||
826 (OffsetBits == 8 && !isUInt<8>(Offset)))
829 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS ||
830 Subtarget->unsafeDSOffsetFoldingEnabled())
833 // On Southern Islands instruction with a negative base value and an offset
834 // don't seem to work.
835 return CurDAG->SignBitIsZero(Base);
838 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base,
839 SDValue &Offset) const {
841 if (CurDAG->isBaseWithConstantOffset(Addr)) {
842 SDValue N0 = Addr.getOperand(0);
843 SDValue N1 = Addr.getOperand(1);
844 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
845 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) {
848 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
851 } else if (Addr.getOpcode() == ISD::SUB) {
852 // sub C, x -> add (sub 0, x), C
853 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
854 int64_t ByteOffset = C->getSExtValue();
855 if (isUInt<16>(ByteOffset)) {
856 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
858 // XXX - This is kind of hacky. Create a dummy sub node so we can check
859 // the known bits in isDSOffsetLegal. We need to emit the selected node
860 // here, so this is thrown away.
861 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
862 Zero, Addr.getOperand(1));
864 if (isDSOffsetLegal(Sub, ByteOffset, 16)) {
865 // FIXME: Select to VOP3 version for with-carry.
866 unsigned SubOp = Subtarget->hasAddNoCarry() ?
867 AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32;
869 MachineSDNode *MachineSub
870 = CurDAG->getMachineNode(SubOp, DL, MVT::i32,
871 Zero, Addr.getOperand(1));
873 Base = SDValue(MachineSub, 0);
874 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16);
879 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
880 // If we have a constant address, prefer to put the constant into the
881 // offset. This can save moves to load the constant address since multiple
882 // operations can share the zero base address register, and enables merging
883 // into read2 / write2 instructions.
887 if (isUInt<16>(CAddr->getZExtValue())) {
888 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
889 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
891 Base = SDValue(MovZero, 0);
892 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
899 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16);
903 // TODO: If offset is too big, put low 16-bit into offset.
904 bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base,
906 SDValue &Offset1) const {
909 if (CurDAG->isBaseWithConstantOffset(Addr)) {
910 SDValue N0 = Addr.getOperand(0);
911 SDValue N1 = Addr.getOperand(1);
912 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
913 unsigned DWordOffset0 = C1->getZExtValue() / 4;
914 unsigned DWordOffset1 = DWordOffset0 + 1;
916 if (isDSOffsetLegal(N0, DWordOffset1, 8)) {
918 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
919 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
922 } else if (Addr.getOpcode() == ISD::SUB) {
923 // sub C, x -> add (sub 0, x), C
924 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) {
925 unsigned DWordOffset0 = C->getZExtValue() / 4;
926 unsigned DWordOffset1 = DWordOffset0 + 1;
928 if (isUInt<8>(DWordOffset0)) {
930 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
932 // XXX - This is kind of hacky. Create a dummy sub node so we can check
933 // the known bits in isDSOffsetLegal. We need to emit the selected node
934 // here, so this is thrown away.
935 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32,
936 Zero, Addr.getOperand(1));
938 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) {
939 unsigned SubOp = Subtarget->hasAddNoCarry() ?
940 AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32;
942 MachineSDNode *MachineSub
943 = CurDAG->getMachineNode(SubOp, DL, MVT::i32,
944 Zero, Addr.getOperand(1));
946 Base = SDValue(MachineSub, 0);
947 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
948 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
953 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
954 unsigned DWordOffset0 = CAddr->getZExtValue() / 4;
955 unsigned DWordOffset1 = DWordOffset0 + 1;
956 assert(4 * DWordOffset0 == CAddr->getZExtValue());
958 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) {
959 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32);
960 MachineSDNode *MovZero
961 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
963 Base = SDValue(MovZero, 0);
964 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8);
965 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8);
972 // FIXME: This is broken on SI where we still need to check if the base
973 // pointer is positive here.
975 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8);
976 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8);
980 bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr,
981 SDValue &VAddr, SDValue &SOffset,
982 SDValue &Offset, SDValue &Offen,
983 SDValue &Idxen, SDValue &Addr64,
984 SDValue &GLC, SDValue &SLC,
985 SDValue &TFE) const {
986 // Subtarget prefers to use flat instruction
987 if (Subtarget->useFlatForGlobal())
993 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
995 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1);
996 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1);
998 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1);
999 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1);
1000 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1);
1001 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1003 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1004 SDValue N0 = Addr.getOperand(0);
1005 SDValue N1 = Addr.getOperand(1);
1006 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1008 if (N0.getOpcode() == ISD::ADD) {
1009 // (add (add N2, N3), C1) -> addr64
1010 SDValue N2 = N0.getOperand(0);
1011 SDValue N3 = N0.getOperand(1);
1012 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
1016 // (add N0, C1) -> offset
1017 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
1021 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) {
1022 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1026 if (isUInt<32>(C1->getZExtValue())) {
1027 // Illegal offset, store it in soffset.
1028 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1029 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
1030 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)),
1036 if (Addr.getOpcode() == ISD::ADD) {
1037 // (add N0, N1) -> addr64
1038 SDValue N0 = Addr.getOperand(0);
1039 SDValue N1 = Addr.getOperand(1);
1040 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1);
1043 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1047 // default case -> offset
1048 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32);
1050 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1055 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
1056 SDValue &VAddr, SDValue &SOffset,
1057 SDValue &Offset, SDValue &GLC,
1058 SDValue &SLC, SDValue &TFE) const {
1059 SDValue Ptr, Offen, Idxen, Addr64;
1061 // addr64 bit was removed for volcanic islands.
1062 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1065 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
1069 ConstantSDNode *C = cast<ConstantSDNode>(Addr64);
1070 if (C->getSExtValue()) {
1073 const SITargetLowering& Lowering =
1074 *static_cast<const SITargetLowering*>(getTargetLowering());
1076 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0);
1083 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc,
1084 SDValue &VAddr, SDValue &SOffset,
1086 SDValue &SLC) const {
1087 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1);
1090 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE);
1093 static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) {
1094 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>();
1095 return PSV && PSV->isStack();
1098 std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const {
1099 const MachineFunction &MF = CurDAG->getMachineFunction();
1100 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1102 if (auto FI = dyn_cast<FrameIndexSDNode>(N)) {
1103 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(),
1104 FI->getValueType(0));
1106 // If we can resolve this to a frame index access, this is relative to the
1107 // frame pointer SGPR.
1108 return std::make_pair(TFI, CurDAG->getRegister(Info->getFrameOffsetReg(),
1112 // If we don't know this private access is a local stack object, it needs to
1113 // be relative to the entry point's scratch wave offset register.
1114 return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(),
1118 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent,
1119 SDValue Addr, SDValue &Rsrc,
1120 SDValue &VAddr, SDValue &SOffset,
1121 SDValue &ImmOffset) const {
1124 MachineFunction &MF = CurDAG->getMachineFunction();
1125 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1127 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
1129 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) {
1130 unsigned Imm = CAddr->getZExtValue();
1132 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32);
1133 MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
1134 DL, MVT::i32, HighBits);
1135 VAddr = SDValue(MovHighBits, 0);
1137 // In a call sequence, stores to the argument stack area are relative to the
1139 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
1140 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1141 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1143 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
1144 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16);
1148 if (CurDAG->isBaseWithConstantOffset(Addr)) {
1151 SDValue N0 = Addr.getOperand(0);
1152 SDValue N1 = Addr.getOperand(1);
1154 // Offsets in vaddr must be positive if range checking is enabled.
1156 // The total computation of vaddr + soffset + offset must not overflow. If
1157 // vaddr is negative, even if offset is 0 the sgpr offset add will end up
1160 // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would
1161 // always perform a range check. If a negative vaddr base index was used,
1162 // this would fail the range check. The overall address computation would
1163 // compute a valid address, but this doesn't happen due to the range
1164 // check. For out-of-bounds MUBUF loads, a 0 is returned.
1166 // Therefore it should be safe to fold any VGPR offset on gfx9 into the
1167 // MUBUF vaddr, but not on older subtargets which can only do this if the
1168 // sign bit is known 0.
1169 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1170 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) &&
1171 (!Subtarget->privateMemoryResourceIsRangeChecked() ||
1172 CurDAG->SignBitIsZero(N0))) {
1173 std::tie(VAddr, SOffset) = foldFrameIndex(N0);
1174 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16);
1180 std::tie(VAddr, SOffset) = foldFrameIndex(Addr);
1181 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1185 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent,
1189 SDValue &Offset) const {
1190 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr);
1191 if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue()))
1195 MachineFunction &MF = CurDAG->getMachineFunction();
1196 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1198 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32);
1200 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo();
1201 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ?
1202 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg();
1204 // FIXME: Get from MachinePointerInfo? We should only be using the frame
1205 // offset if we know this is in a call sequence.
1206 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32);
1208 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16);
1212 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1213 SDValue &SOffset, SDValue &Offset,
1214 SDValue &GLC, SDValue &SLC,
1215 SDValue &TFE) const {
1216 SDValue Ptr, VAddr, Offen, Idxen, Addr64;
1217 const SIInstrInfo *TII =
1218 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1220 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64,
1224 if (!cast<ConstantSDNode>(Offen)->getSExtValue() &&
1225 !cast<ConstantSDNode>(Idxen)->getSExtValue() &&
1226 !cast<ConstantSDNode>(Addr64)->getSExtValue()) {
1227 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() |
1228 APInt::getAllOnesValue(32).getZExtValue(); // Size
1231 const SITargetLowering& Lowering =
1232 *static_cast<const SITargetLowering*>(getTargetLowering());
1234 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0);
1240 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1241 SDValue &Soffset, SDValue &Offset
1243 SDValue GLC, SLC, TFE;
1245 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE);
1247 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc,
1248 SDValue &Soffset, SDValue &Offset,
1249 SDValue &SLC) const {
1252 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE);
1255 bool AMDGPUDAGToDAGISel::SelectMUBUFConstant(SDValue Constant,
1257 SDValue &ImmOffset) const {
1259 const uint32_t Align = 4;
1260 const uint32_t MaxImm = alignDown(4095, Align);
1261 uint32_t Imm = cast<ConstantSDNode>(Constant)->getZExtValue();
1262 uint32_t Overflow = 0;
1265 if (Imm <= MaxImm + 64) {
1266 // Use an SOffset inline constant for 4..64
1267 Overflow = Imm - MaxImm;
1270 // Try to keep the same value in SOffset for adjacent loads, so that
1271 // the corresponding register contents can be re-used.
1273 // Load values with all low-bits (except for alignment bits) set into
1274 // SOffset, so that a larger range of values can be covered using
1277 // Atomic operations fail to work correctly when individual address
1278 // components are unaligned, even if their sum is aligned.
1279 uint32_t High = (Imm + Align) & ~4095;
1280 uint32_t Low = (Imm + Align) & 4095;
1282 Overflow = High - Align;
1286 // There is a hardware bug in SI and CI which prevents address clamping in
1287 // MUBUF instructions from working correctly with SOffsets. The immediate
1288 // offset is unaffected.
1290 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
1293 ImmOffset = CurDAG->getTargetConstant(Imm, DL, MVT::i16);
1296 SOffset = CurDAG->getTargetConstant(Overflow, DL, MVT::i32);
1298 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32,
1299 CurDAG->getTargetConstant(Overflow, DL, MVT::i32)),
1305 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicOffset(SDValue Offset,
1307 SDValue &ImmOffset) const {
1310 if (!isa<ConstantSDNode>(Offset))
1313 return SelectMUBUFConstant(Offset, SOffset, ImmOffset);
1316 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicVOffset(SDValue Offset,
1319 SDValue &VOffset) const {
1322 // Don't generate an unnecessary voffset for constant offsets.
1323 if (isa<ConstantSDNode>(Offset)) {
1326 // When necessary, use a voffset in <= CI anyway to work around a hardware
1328 if (Subtarget->getGeneration() > AMDGPUSubtarget::SEA_ISLANDS ||
1329 SelectMUBUFConstant(Offset, Tmp1, Tmp2))
1333 if (CurDAG->isBaseWithConstantOffset(Offset)) {
1334 SDValue N0 = Offset.getOperand(0);
1335 SDValue N1 = Offset.getOperand(1);
1336 if (cast<ConstantSDNode>(N1)->getSExtValue() >= 0 &&
1337 SelectMUBUFConstant(N1, SOffset, ImmOffset)) {
1343 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1344 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16);
1350 template <bool IsSigned>
1351 bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDValue Addr,
1354 SDValue &SLC) const {
1355 int64_t OffsetVal = 0;
1357 if (Subtarget->hasFlatInstOffsets() &&
1358 CurDAG->isBaseWithConstantOffset(Addr)) {
1359 SDValue N0 = Addr.getOperand(0);
1360 SDValue N1 = Addr.getOperand(1);
1361 int64_t COffsetVal = cast<ConstantSDNode>(N1)->getSExtValue();
1363 if ((IsSigned && isInt<13>(COffsetVal)) ||
1364 (!IsSigned && isUInt<12>(COffsetVal))) {
1366 OffsetVal = COffsetVal;
1371 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i16);
1372 SLC = CurDAG->getTargetConstant(0, SDLoc(), MVT::i1);
1377 bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDValue Addr,
1380 SDValue &SLC) const {
1381 return SelectFlatOffset<false>(Addr, VAddr, Offset, SLC);
1384 bool AMDGPUDAGToDAGISel::SelectFlatAtomicSigned(SDValue Addr,
1387 SDValue &SLC) const {
1388 return SelectFlatOffset<true>(Addr, VAddr, Offset, SLC);
1391 bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode,
1392 SDValue &Offset, bool &Imm) const {
1394 // FIXME: Handle non-constant offsets.
1395 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode);
1399 SDLoc SL(ByteOffsetNode);
1400 GCNSubtarget::Generation Gen = Subtarget->getGeneration();
1401 int64_t ByteOffset = C->getSExtValue();
1402 int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset);
1404 if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) {
1405 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1410 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset))
1413 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) {
1414 // 32-bit Immediates are supported on Sea Islands.
1415 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32);
1417 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32);
1418 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32,
1425 SDValue AMDGPUDAGToDAGISel::Expand32BitAddress(SDValue Addr) const {
1426 if (Addr.getValueType() != MVT::i32)
1429 // Zero-extend a 32-bit address.
1432 const MachineFunction &MF = CurDAG->getMachineFunction();
1433 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1434 unsigned AddrHiVal = Info->get32BitAddressHighBits();
1435 SDValue AddrHi = CurDAG->getTargetConstant(AddrHiVal, SL, MVT::i32);
1437 const SDValue Ops[] = {
1438 CurDAG->getTargetConstant(AMDGPU::SReg_64_XEXECRegClassID, SL, MVT::i32),
1440 CurDAG->getTargetConstant(AMDGPU::sub0, SL, MVT::i32),
1441 SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, AddrHi),
1443 CurDAG->getTargetConstant(AMDGPU::sub1, SL, MVT::i32),
1446 return SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, SL, MVT::i64,
1450 bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase,
1451 SDValue &Offset, bool &Imm) const {
1454 // A 32-bit (address + offset) should not cause unsigned 32-bit integer
1455 // wraparound, because s_load instructions perform the addition in 64 bits.
1456 if ((Addr.getValueType() != MVT::i32 ||
1457 Addr->getFlags().hasNoUnsignedWrap()) &&
1458 CurDAG->isBaseWithConstantOffset(Addr)) {
1459 SDValue N0 = Addr.getOperand(0);
1460 SDValue N1 = Addr.getOperand(1);
1462 if (SelectSMRDOffset(N1, Offset, Imm)) {
1463 SBase = Expand32BitAddress(N0);
1467 SBase = Expand32BitAddress(Addr);
1468 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32);
1473 bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase,
1474 SDValue &Offset) const {
1476 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm;
1479 bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase,
1480 SDValue &Offset) const {
1482 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1486 if (!SelectSMRD(Addr, SBase, Offset, Imm))
1489 return !Imm && isa<ConstantSDNode>(Offset);
1492 bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase,
1493 SDValue &Offset) const {
1495 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm &&
1496 !isa<ConstantSDNode>(Offset);
1499 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr,
1500 SDValue &Offset) const {
1502 return SelectSMRDOffset(Addr, Offset, Imm) && Imm;
1505 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr,
1506 SDValue &Offset) const {
1507 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS)
1511 if (!SelectSMRDOffset(Addr, Offset, Imm))
1514 return !Imm && isa<ConstantSDNode>(Offset);
1517 bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index,
1519 SDValue &Offset) const {
1522 if (CurDAG->isBaseWithConstantOffset(Index)) {
1523 SDValue N0 = Index.getOperand(0);
1524 SDValue N1 = Index.getOperand(1);
1525 ConstantSDNode *C1 = cast<ConstantSDNode>(N1);
1529 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32);
1533 if (isa<ConstantSDNode>(Index))
1537 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
1541 SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL,
1542 SDValue Val, uint32_t Offset,
1544 // Transformation function, pack the offset and width of a BFE into
1545 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
1546 // source, bits [5:0] contain the offset and bits [22:16] the width.
1547 uint32_t PackedVal = Offset | (Width << 16);
1548 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32);
1550 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst);
1553 void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) {
1554 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c)
1555 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c)
1556 // Predicate: 0 < b <= c < 32
1558 const SDValue &Shl = N->getOperand(0);
1559 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1));
1560 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
1563 uint32_t BVal = B->getZExtValue();
1564 uint32_t CVal = C->getZExtValue();
1566 if (0 < BVal && BVal <= CVal && CVal < 32) {
1567 bool Signed = N->getOpcode() == ISD::SRA;
1568 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32;
1570 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal,
1578 void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) {
1579 switch (N->getOpcode()) {
1581 if (N->getOperand(0).getOpcode() == ISD::SRL) {
1582 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)"
1583 // Predicate: isMask(mask)
1584 const SDValue &Srl = N->getOperand(0);
1585 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1));
1586 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1));
1588 if (Shift && Mask) {
1589 uint32_t ShiftVal = Shift->getZExtValue();
1590 uint32_t MaskVal = Mask->getZExtValue();
1592 if (isMask_32(MaskVal)) {
1593 uint32_t WidthVal = countPopulation(MaskVal);
1595 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1596 Srl.getOperand(0), ShiftVal, WidthVal));
1603 if (N->getOperand(0).getOpcode() == ISD::AND) {
1604 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)"
1605 // Predicate: isMask(mask >> b)
1606 const SDValue &And = N->getOperand(0);
1607 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1));
1608 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1));
1610 if (Shift && Mask) {
1611 uint32_t ShiftVal = Shift->getZExtValue();
1612 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal;
1614 if (isMask_32(MaskVal)) {
1615 uint32_t WidthVal = countPopulation(MaskVal);
1617 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N),
1618 And.getOperand(0), ShiftVal, WidthVal));
1622 } else if (N->getOperand(0).getOpcode() == ISD::SHL) {
1623 SelectS_BFEFromShifts(N);
1628 if (N->getOperand(0).getOpcode() == ISD::SHL) {
1629 SelectS_BFEFromShifts(N);
1634 case ISD::SIGN_EXTEND_INREG: {
1635 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8
1636 SDValue Src = N->getOperand(0);
1637 if (Src.getOpcode() != ISD::SRL)
1640 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1));
1644 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
1645 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0),
1646 Amt->getZExtValue(), Width));
1654 bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const {
1655 assert(N->getOpcode() == ISD::BRCOND);
1656 if (!N->hasOneUse())
1659 SDValue Cond = N->getOperand(1);
1660 if (Cond.getOpcode() == ISD::CopyToReg)
1661 Cond = Cond.getOperand(2);
1663 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse())
1666 MVT VT = Cond.getOperand(0).getSimpleValueType();
1670 if (VT == MVT::i64) {
1671 auto ST = static_cast<const GCNSubtarget *>(Subtarget);
1673 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1674 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64();
1680 void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) {
1681 SDValue Cond = N->getOperand(1);
1683 if (Cond.isUndef()) {
1684 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other,
1685 N->getOperand(2), N->getOperand(0));
1689 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N);
1690 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ;
1691 unsigned CondReg = UseSCCBr ? AMDGPU::SCC : AMDGPU::VCC;
1695 // This is the case that we are selecting to S_CBRANCH_VCCNZ. We have not
1696 // analyzed what generates the vcc value, so we do not know whether vcc
1697 // bits for disabled lanes are 0. Thus we need to mask out bits for
1700 // For the case that we select S_CBRANCH_SCC1 and it gets
1701 // changed to S_CBRANCH_VCCNZ in SIFixSGPRCopies, SIFixSGPRCopies calls
1702 // SIInstrInfo::moveToVALU which inserts the S_AND).
1704 // We could add an analysis of what generates the vcc value here and omit
1705 // the S_AND when is unnecessary. But it would be better to add a separate
1706 // pass after SIFixSGPRCopies to do the unnecessary S_AND removal, so it
1707 // catches both cases.
1708 Cond = SDValue(CurDAG->getMachineNode(AMDGPU::S_AND_B64, SL, MVT::i1,
1709 CurDAG->getRegister(AMDGPU::EXEC, MVT::i1),
1714 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond);
1715 CurDAG->SelectNodeTo(N, BrOp, MVT::Other,
1716 N->getOperand(2), // Basic Block
1720 void AMDGPUDAGToDAGISel::SelectFMAD_FMA(SDNode *N) {
1721 MVT VT = N->getSimpleValueType(0);
1722 bool IsFMA = N->getOpcode() == ISD::FMA;
1723 if (VT != MVT::f32 || (!Subtarget->hasMadMixInsts() &&
1724 !Subtarget->hasFmaMixInsts()) ||
1725 ((IsFMA && Subtarget->hasMadMixInsts()) ||
1726 (!IsFMA && Subtarget->hasFmaMixInsts()))) {
1731 SDValue Src0 = N->getOperand(0);
1732 SDValue Src1 = N->getOperand(1);
1733 SDValue Src2 = N->getOperand(2);
1734 unsigned Src0Mods, Src1Mods, Src2Mods;
1736 // Avoid using v_mad_mix_f32/v_fma_mix_f32 unless there is actually an operand
1737 // using the conversion from f16.
1738 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods);
1739 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods);
1740 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods);
1742 assert((IsFMA || !Subtarget->hasFP32Denormals()) &&
1743 "fmad selected with denormals enabled");
1744 // TODO: We can select this with f32 denormals enabled if all the sources are
1745 // converted from f16 (in which case fmad isn't legal).
1747 if (Sel0 || Sel1 || Sel2) {
1748 // For dummy operands.
1749 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32);
1751 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0,
1752 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1,
1753 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2,
1754 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1),
1758 CurDAG->SelectNodeTo(N,
1759 IsFMA ? AMDGPU::V_FMA_MIX_F32 : AMDGPU::V_MAD_MIX_F32,
1766 // This is here because there isn't a way to use the generated sub0_sub1 as the
1767 // subreg index to EXTRACT_SUBREG in tablegen.
1768 void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) {
1769 MemSDNode *Mem = cast<MemSDNode>(N);
1770 unsigned AS = Mem->getAddressSpace();
1771 if (AS == AMDGPUASI.FLAT_ADDRESS) {
1776 MVT VT = N->getSimpleValueType(0);
1777 bool Is32 = (VT == MVT::i32);
1780 MachineSDNode *CmpSwap = nullptr;
1781 if (Subtarget->hasAddr64()) {
1782 SDValue SRsrc, VAddr, SOffset, Offset, SLC;
1784 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) {
1785 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN :
1786 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN;
1787 SDValue CmpVal = Mem->getOperand(2);
1789 // XXX - Do we care about glue operands?
1792 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain()
1795 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
1800 SDValue SRsrc, SOffset, Offset, SLC;
1801 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) {
1802 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN :
1803 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN;
1805 SDValue CmpVal = Mem->getOperand(2);
1807 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain()
1810 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops);
1819 MachineSDNode::mmo_iterator MMOs = MF->allocateMemRefsArray(1);
1820 *MMOs = Mem->getMemOperand();
1821 CmpSwap->setMemRefs(MMOs, MMOs + 1);
1823 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
1825 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0));
1827 ReplaceUses(SDValue(N, 0), Extract);
1828 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1));
1829 CurDAG->RemoveDeadNode(N);
1832 bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src,
1833 unsigned &Mods) const {
1837 if (Src.getOpcode() == ISD::FNEG) {
1838 Mods |= SISrcMods::NEG;
1839 Src = Src.getOperand(0);
1842 if (Src.getOpcode() == ISD::FABS) {
1843 Mods |= SISrcMods::ABS;
1844 Src = Src.getOperand(0);
1850 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src,
1851 SDValue &SrcMods) const {
1853 if (SelectVOP3ModsImpl(In, Src, Mods)) {
1854 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
1861 bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src,
1862 SDValue &SrcMods) const {
1863 SelectVOP3Mods(In, Src, SrcMods);
1864 return isNoNanSrc(Src);
1867 bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const {
1868 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG)
1875 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src,
1876 SDValue &SrcMods, SDValue &Clamp,
1877 SDValue &Omod) const {
1879 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
1880 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
1882 return SelectVOP3Mods(In, Src, SrcMods);
1885 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src,
1888 SDValue &Omod) const {
1889 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
1890 return SelectVOP3Mods(In, Src, SrcMods);
1893 bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src,
1894 SDValue &Clamp, SDValue &Omod) const {
1898 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1);
1899 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1);
1904 static SDValue stripBitcast(SDValue Val) {
1905 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
1908 // Figure out if this is really an extract of the high 16-bits of a dword.
1909 static bool isExtractHiElt(SDValue In, SDValue &Out) {
1910 In = stripBitcast(In);
1911 if (In.getOpcode() != ISD::TRUNCATE)
1914 SDValue Srl = In.getOperand(0);
1915 if (Srl.getOpcode() == ISD::SRL) {
1916 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
1917 if (ShiftAmt->getZExtValue() == 16) {
1918 Out = stripBitcast(Srl.getOperand(0));
1927 // Look through operations that obscure just looking at the low 16-bits of the
1929 static SDValue stripExtractLoElt(SDValue In) {
1930 if (In.getOpcode() == ISD::TRUNCATE) {
1931 SDValue Src = In.getOperand(0);
1932 if (Src.getValueType().getSizeInBits() == 32)
1933 return stripBitcast(Src);
1939 bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src,
1940 SDValue &SrcMods) const {
1944 if (Src.getOpcode() == ISD::FNEG) {
1945 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI);
1946 Src = Src.getOperand(0);
1949 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
1950 unsigned VecMods = Mods;
1952 SDValue Lo = stripBitcast(Src.getOperand(0));
1953 SDValue Hi = stripBitcast(Src.getOperand(1));
1955 if (Lo.getOpcode() == ISD::FNEG) {
1956 Lo = stripBitcast(Lo.getOperand(0));
1957 Mods ^= SISrcMods::NEG;
1960 if (Hi.getOpcode() == ISD::FNEG) {
1961 Hi = stripBitcast(Hi.getOperand(0));
1962 Mods ^= SISrcMods::NEG_HI;
1965 if (isExtractHiElt(Lo, Lo))
1966 Mods |= SISrcMods::OP_SEL_0;
1968 if (isExtractHiElt(Hi, Hi))
1969 Mods |= SISrcMods::OP_SEL_1;
1971 Lo = stripExtractLoElt(Lo);
1972 Hi = stripExtractLoElt(Hi);
1974 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) {
1975 // Really a scalar input. Just select from the low half of the register to
1979 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
1986 // Packed instructions do not have abs modifiers.
1987 Mods |= SISrcMods::OP_SEL_1;
1989 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
1993 bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src,
1995 SDValue &Clamp) const {
1998 // FIXME: Handle clamp and op_sel
1999 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2001 return SelectVOP3PMods(In, Src, SrcMods);
2004 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src,
2005 SDValue &SrcMods) const {
2007 // FIXME: Handle op_sel
2008 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32);
2012 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel0(SDValue In, SDValue &Src,
2014 SDValue &Clamp) const {
2017 // FIXME: Handle clamp
2018 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2020 return SelectVOP3OpSel(In, Src, SrcMods);
2023 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src,
2024 SDValue &SrcMods) const {
2025 // FIXME: Handle op_sel
2026 return SelectVOP3Mods(In, Src, SrcMods);
2029 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods0(SDValue In, SDValue &Src,
2031 SDValue &Clamp) const {
2034 // FIXME: Handle clamp
2035 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32);
2037 return SelectVOP3OpSelMods(In, Src, SrcMods);
2040 // The return value is not whether the match is possible (which it always is),
2041 // but whether or not it a conversion is really used.
2042 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src,
2043 unsigned &Mods) const {
2045 SelectVOP3ModsImpl(In, Src, Mods);
2047 if (Src.getOpcode() == ISD::FP_EXTEND) {
2048 Src = Src.getOperand(0);
2049 assert(Src.getValueType() == MVT::f16);
2050 Src = stripBitcast(Src);
2052 // Be careful about folding modifiers if we already have an abs. fneg is
2053 // applied last, so we don't want to apply an earlier fneg.
2054 if ((Mods & SISrcMods::ABS) == 0) {
2056 SelectVOP3ModsImpl(Src, Src, ModsTmp);
2058 if ((ModsTmp & SISrcMods::NEG) != 0)
2059 Mods ^= SISrcMods::NEG;
2061 if ((ModsTmp & SISrcMods::ABS) != 0)
2062 Mods |= SISrcMods::ABS;
2065 // op_sel/op_sel_hi decide the source type and source.
2066 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16.
2067 // If the sources's op_sel is set, it picks the high half of the source
2070 Mods |= SISrcMods::OP_SEL_1;
2071 if (isExtractHiElt(Src, Src)) {
2072 Mods |= SISrcMods::OP_SEL_0;
2074 // TODO: Should we try to look for neg/abs here?
2083 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src,
2084 SDValue &SrcMods) const {
2086 SelectVOP3PMadMixModsImpl(In, Src, Mods);
2087 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32);
2091 // TODO: Can we identify things like v_mad_mixhi_f16?
2092 bool AMDGPUDAGToDAGISel::SelectHi16Elt(SDValue In, SDValue &Src) const {
2098 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) {
2100 SDValue K = CurDAG->getTargetConstant(C->getZExtValue() << 16, SL, MVT::i32);
2101 MachineSDNode *MovK = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
2103 Src = SDValue(MovK, 0);
2107 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) {
2109 SDValue K = CurDAG->getTargetConstant(
2110 C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32);
2111 MachineSDNode *MovK = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32,
2113 Src = SDValue(MovK, 0);
2117 return isExtractHiElt(In, Src);
2120 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
2121 const AMDGPUTargetLowering& Lowering =
2122 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
2123 bool IsModified = false;
2127 // Go over all selected nodes and try to fold them a bit more
2128 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_begin();
2129 while (Position != CurDAG->allnodes_end()) {
2130 SDNode *Node = &*Position++;
2131 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(Node);
2135 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
2136 if (ResNode != Node) {
2138 ReplaceUses(Node, ResNode);
2142 CurDAG->RemoveDeadNodes();
2143 } while (IsModified);
2146 bool R600DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
2147 Subtarget = &MF.getSubtarget<R600Subtarget>();
2148 return SelectionDAGISel::runOnMachineFunction(MF);
2151 bool R600DAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const {
2155 return N->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS ||
2156 N->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS_32BIT;
2158 return N->getAddressSpace() == AMDGPUASI.CONSTANT_BUFFER_0 + CbId;
2161 bool R600DAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
2163 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
2164 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr),
2171 bool R600DAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
2172 SDValue& BaseReg, SDValue &Offset) {
2173 if (!isa<ConstantSDNode>(Addr)) {
2175 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true);
2181 void R600DAGToDAGISel::Select(SDNode *N) {
2182 unsigned int Opc = N->getOpcode();
2183 if (N->isMachineOpcode()) {
2185 return; // Already selected.
2190 case AMDGPUISD::BUILD_VERTICAL_VECTOR:
2191 case ISD::SCALAR_TO_VECTOR:
2192 case ISD::BUILD_VECTOR: {
2193 EVT VT = N->getValueType(0);
2194 unsigned NumVectorElts = VT.getVectorNumElements();
2195 unsigned RegClassID;
2196 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
2197 // that adds a 128 bits reg copy when going through TwoAddressInstructions
2198 // pass. We want to avoid 128 bits copies as much as possible because they
2199 // can't be bundled by our scheduler.
2200 switch(NumVectorElts) {
2201 case 2: RegClassID = R600::R600_Reg64RegClassID; break;
2203 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
2204 RegClassID = R600::R600_Reg128VerticalRegClassID;
2206 RegClassID = R600::R600_Reg128RegClassID;
2208 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
2210 SelectBuildVector(N, RegClassID);
2218 bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
2223 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
2224 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
2225 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2226 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) &&
2227 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) {
2228 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32);
2229 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2230 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
2231 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
2232 Base = Addr.getOperand(0);
2233 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32);
2236 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32);
2242 bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
2244 ConstantSDNode *IMMOffset;
2246 if (Addr.getOpcode() == ISD::ADD
2247 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
2248 && isInt<16>(IMMOffset->getZExtValue())) {
2250 Base = Addr.getOperand(0);
2251 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2254 // If the pointer address is constant, we can move it to the offset field.
2255 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
2256 && isInt<16>(IMMOffset->getZExtValue())) {
2257 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
2258 SDLoc(CurDAG->getEntryNode()),
2259 R600::ZERO, MVT::i32);
2260 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr),
2265 // Default case, no offset
2267 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);