1 //===-- SIFoldOperands.cpp - Fold operands --- ----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
12 #include "GCNSubtarget.h"
13 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
14 #include "SIMachineFunctionInfo.h"
15 #include "llvm/ADT/DepthFirstIterator.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineOperand.h"
19 #define DEBUG_TYPE "si-fold-operands"
24 struct FoldCandidate {
27 MachineOperand *OpToFold;
33 MachineOperand::MachineOperandType Kind;
36 FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp,
37 bool Commuted_ = false,
39 UseMI(MI), OpToFold(nullptr), ShrinkOpcode(ShrinkOp), UseOpNo(OpNo),
40 Kind(FoldOp->getType()),
42 if (FoldOp->isImm()) {
43 ImmToFold = FoldOp->getImm();
44 } else if (FoldOp->isFI()) {
45 FrameIndexToFold = FoldOp->getIndex();
47 assert(FoldOp->isReg() || FoldOp->isGlobal());
53 return Kind == MachineOperand::MO_FrameIndex;
57 return Kind == MachineOperand::MO_Immediate;
61 return Kind == MachineOperand::MO_Register;
64 bool isGlobal() const { return Kind == MachineOperand::MO_GlobalAddress; }
66 bool needsShrink() const { return ShrinkOpcode != -1; }
69 class SIFoldOperands : public MachineFunctionPass {
72 MachineRegisterInfo *MRI;
73 const SIInstrInfo *TII;
74 const SIRegisterInfo *TRI;
75 const GCNSubtarget *ST;
76 const SIMachineFunctionInfo *MFI;
78 bool frameIndexMayFold(const MachineInstr &UseMI, int OpNo,
79 const MachineOperand &OpToFold) const;
81 bool updateOperand(FoldCandidate &Fold) const;
83 bool tryAddToFoldList(SmallVectorImpl<FoldCandidate> &FoldList,
84 MachineInstr *MI, unsigned OpNo,
85 MachineOperand *OpToFold) const;
86 bool isUseSafeToFold(const MachineInstr &MI,
87 const MachineOperand &UseMO) const;
89 getRegSeqInit(SmallVectorImpl<std::pair<MachineOperand *, unsigned>> &Defs,
90 Register UseReg, uint8_t OpTy) const;
91 bool tryToFoldACImm(const MachineOperand &OpToFold, MachineInstr *UseMI,
93 SmallVectorImpl<FoldCandidate> &FoldList) const;
94 void foldOperand(MachineOperand &OpToFold,
97 SmallVectorImpl<FoldCandidate> &FoldList,
98 SmallVectorImpl<MachineInstr *> &CopiesToReplace) const;
100 MachineOperand *getImmOrMaterializedImm(MachineOperand &Op) const;
101 bool tryConstantFoldOp(MachineInstr *MI) const;
102 bool tryFoldCndMask(MachineInstr &MI) const;
103 bool tryFoldZeroHighBits(MachineInstr &MI) const;
104 bool foldInstOperand(MachineInstr &MI, MachineOperand &OpToFold) const;
105 bool tryFoldFoldableCopy(MachineInstr &MI,
106 MachineOperand *&CurrentKnownM0Val) const;
108 const MachineOperand *isClamp(const MachineInstr &MI) const;
109 bool tryFoldClamp(MachineInstr &MI);
111 std::pair<const MachineOperand *, int> isOMod(const MachineInstr &MI) const;
112 bool tryFoldOMod(MachineInstr &MI);
113 bool tryFoldRegSequence(MachineInstr &MI);
114 bool tryFoldLCSSAPhi(MachineInstr &MI);
115 bool tryFoldLoad(MachineInstr &MI);
118 SIFoldOperands() : MachineFunctionPass(ID) {
119 initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
122 bool runOnMachineFunction(MachineFunction &MF) override;
124 StringRef getPassName() const override { return "SI Fold Operands"; }
126 void getAnalysisUsage(AnalysisUsage &AU) const override {
127 AU.setPreservesCFG();
128 MachineFunctionPass::getAnalysisUsage(AU);
132 } // End anonymous namespace.
134 INITIALIZE_PASS(SIFoldOperands, DEBUG_TYPE,
135 "SI Fold Operands", false, false)
137 char SIFoldOperands::ID = 0;
139 char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
141 // Map multiply-accumulate opcode to corresponding multiply-add opcode if any.
142 static unsigned macToMad(unsigned Opc) {
144 case AMDGPU::V_MAC_F32_e64:
145 return AMDGPU::V_MAD_F32_e64;
146 case AMDGPU::V_MAC_F16_e64:
147 return AMDGPU::V_MAD_F16_e64;
148 case AMDGPU::V_FMAC_F32_e64:
149 return AMDGPU::V_FMA_F32_e64;
150 case AMDGPU::V_FMAC_F16_e64:
151 return AMDGPU::V_FMA_F16_gfx9_e64;
152 case AMDGPU::V_FMAC_F16_t16_e64:
153 return AMDGPU::V_FMA_F16_gfx9_e64;
154 case AMDGPU::V_FMAC_LEGACY_F32_e64:
155 return AMDGPU::V_FMA_LEGACY_F32_e64;
156 case AMDGPU::V_FMAC_F64_e64:
157 return AMDGPU::V_FMA_F64_e64;
159 return AMDGPU::INSTRUCTION_LIST_END;
162 // TODO: Add heuristic that the frame index might not fit in the addressing mode
163 // immediate offset to avoid materializing in loops.
164 bool SIFoldOperands::frameIndexMayFold(const MachineInstr &UseMI, int OpNo,
165 const MachineOperand &OpToFold) const {
166 if (!OpToFold.isFI())
169 const unsigned Opc = UseMI.getOpcode();
170 if (TII->isMUBUF(UseMI))
171 return OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr);
172 if (!TII->isFLATScratch(UseMI))
175 int SIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::saddr);
179 int VIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr);
180 return OpNo == VIdx && SIdx == -1;
183 FunctionPass *llvm::createSIFoldOperandsPass() {
184 return new SIFoldOperands();
187 bool SIFoldOperands::updateOperand(FoldCandidate &Fold) const {
188 MachineInstr *MI = Fold.UseMI;
189 MachineOperand &Old = MI->getOperand(Fold.UseOpNo);
193 const uint64_t TSFlags = MI->getDesc().TSFlags;
195 if (TSFlags & SIInstrFlags::IsPacked && !(TSFlags & SIInstrFlags::IsMAI) &&
196 (!ST->hasDOTOpSelHazard() || !(TSFlags & SIInstrFlags::IsDOT)) &&
197 AMDGPU::isFoldableLiteralV216(Fold.ImmToFold,
198 ST->hasInv2PiInlineImm())) {
199 // Set op_sel/op_sel_hi on this operand or bail out if op_sel is
201 unsigned Opcode = MI->getOpcode();
202 int OpNo = MI->getOperandNo(&Old);
204 if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0))
205 ModIdx = AMDGPU::OpName::src0_modifiers;
206 else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1))
207 ModIdx = AMDGPU::OpName::src1_modifiers;
208 else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2))
209 ModIdx = AMDGPU::OpName::src2_modifiers;
210 assert(ModIdx != -1);
211 ModIdx = AMDGPU::getNamedOperandIdx(Opcode, ModIdx);
212 MachineOperand &Mod = MI->getOperand(ModIdx);
213 unsigned Val = Mod.getImm();
214 if (!(Val & SISrcMods::OP_SEL_0) && (Val & SISrcMods::OP_SEL_1)) {
215 // Only apply the following transformation if that operand requires
216 // a packed immediate.
217 switch (TII->get(Opcode).operands()[OpNo].OperandType) {
218 case AMDGPU::OPERAND_REG_IMM_V2FP16:
219 case AMDGPU::OPERAND_REG_IMM_V2INT16:
220 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
221 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
222 // If upper part is all zero we do not need op_sel_hi.
223 if (!isUInt<16>(Fold.ImmToFold)) {
224 if (!(Fold.ImmToFold & 0xffff)) {
225 Mod.setImm(Mod.getImm() | SISrcMods::OP_SEL_0);
226 Mod.setImm(Mod.getImm() & ~SISrcMods::OP_SEL_1);
227 Old.ChangeToImmediate((Fold.ImmToFold >> 16) & 0xffff);
230 Mod.setImm(Mod.getImm() & ~SISrcMods::OP_SEL_1);
231 Old.ChangeToImmediate(Fold.ImmToFold & 0xffff);
242 if ((Fold.isImm() || Fold.isFI() || Fold.isGlobal()) && Fold.needsShrink()) {
243 MachineBasicBlock *MBB = MI->getParent();
244 auto Liveness = MBB->computeRegisterLiveness(TRI, AMDGPU::VCC, MI, 16);
245 if (Liveness != MachineBasicBlock::LQR_Dead) {
246 LLVM_DEBUG(dbgs() << "Not shrinking " << MI << " due to vcc liveness\n");
250 int Op32 = Fold.ShrinkOpcode;
251 MachineOperand &Dst0 = MI->getOperand(0);
252 MachineOperand &Dst1 = MI->getOperand(1);
253 assert(Dst0.isDef() && Dst1.isDef());
255 bool HaveNonDbgCarryUse = !MRI->use_nodbg_empty(Dst1.getReg());
257 const TargetRegisterClass *Dst0RC = MRI->getRegClass(Dst0.getReg());
258 Register NewReg0 = MRI->createVirtualRegister(Dst0RC);
260 MachineInstr *Inst32 = TII->buildShrunkInst(*MI, Op32);
262 if (HaveNonDbgCarryUse) {
263 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(AMDGPU::COPY),
265 .addReg(AMDGPU::VCC, RegState::Kill);
268 // Keep the old instruction around to avoid breaking iterators, but
269 // replace it with a dummy instruction to remove uses.
271 // FIXME: We should not invert how this pass looks at operands to avoid
272 // this. Should track set of foldable movs instead of looking for uses
273 // when looking at a use.
274 Dst0.setReg(NewReg0);
275 for (unsigned I = MI->getNumOperands() - 1; I > 0; --I)
276 MI->removeOperand(I);
277 MI->setDesc(TII->get(AMDGPU::IMPLICIT_DEF));
280 TII->commuteInstruction(*Inst32, false);
284 assert(!Fold.needsShrink() && "not handled");
288 int NewMFMAOpc = AMDGPU::getMFMAEarlyClobberOp(MI->getOpcode());
289 if (NewMFMAOpc == -1)
291 MI->setDesc(TII->get(NewMFMAOpc));
292 MI->untieRegOperand(0);
294 Old.ChangeToImmediate(Fold.ImmToFold);
298 if (Fold.isGlobal()) {
299 Old.ChangeToGA(Fold.OpToFold->getGlobal(), Fold.OpToFold->getOffset(),
300 Fold.OpToFold->getTargetFlags());
305 Old.ChangeToFrameIndex(Fold.FrameIndexToFold);
309 MachineOperand *New = Fold.OpToFold;
310 Old.substVirtReg(New->getReg(), New->getSubReg(), *TRI);
311 Old.setIsUndef(New->isUndef());
315 static bool isUseMIInFoldList(ArrayRef<FoldCandidate> FoldList,
316 const MachineInstr *MI) {
317 return any_of(FoldList, [&](const auto &C) { return C.UseMI == MI; });
320 static void appendFoldCandidate(SmallVectorImpl<FoldCandidate> &FoldList,
321 MachineInstr *MI, unsigned OpNo,
322 MachineOperand *FoldOp, bool Commuted = false,
324 // Skip additional folding on the same operand.
325 for (FoldCandidate &Fold : FoldList)
326 if (Fold.UseMI == MI && Fold.UseOpNo == OpNo)
328 LLVM_DEBUG(dbgs() << "Append " << (Commuted ? "commuted" : "normal")
329 << " operand " << OpNo << "\n " << *MI);
330 FoldList.emplace_back(MI, OpNo, FoldOp, Commuted, ShrinkOp);
333 bool SIFoldOperands::tryAddToFoldList(SmallVectorImpl<FoldCandidate> &FoldList,
334 MachineInstr *MI, unsigned OpNo,
335 MachineOperand *OpToFold) const {
336 if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
337 // Special case for v_mac_{f16, f32}_e64 if we are trying to fold into src2
338 unsigned Opc = MI->getOpcode();
339 unsigned NewOpc = macToMad(Opc);
340 if (NewOpc != AMDGPU::INSTRUCTION_LIST_END) {
341 // Check if changing this to a v_mad_{f16, f32} instruction will allow us
342 // to fold the operand.
343 MI->setDesc(TII->get(NewOpc));
344 if (!AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::op_sel) &&
345 AMDGPU::hasNamedOperand(NewOpc, AMDGPU::OpName::op_sel))
346 MI->addOperand(MachineOperand::CreateImm(0));
347 bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold);
349 MI->untieRegOperand(OpNo);
352 MI->setDesc(TII->get(Opc));
355 // Special case for s_setreg_b32
356 if (OpToFold->isImm()) {
358 if (Opc == AMDGPU::S_SETREG_B32)
359 ImmOpc = AMDGPU::S_SETREG_IMM32_B32;
360 else if (Opc == AMDGPU::S_SETREG_B32_mode)
361 ImmOpc = AMDGPU::S_SETREG_IMM32_B32_mode;
363 MI->setDesc(TII->get(ImmOpc));
364 appendFoldCandidate(FoldList, MI, OpNo, OpToFold);
369 // If we are already folding into another operand of MI, then
370 // we can't commute the instruction, otherwise we risk making the
371 // other fold illegal.
372 if (isUseMIInFoldList(FoldList, MI))
375 unsigned CommuteOpNo = OpNo;
377 // Operand is not legal, so try to commute the instruction to
378 // see if this makes it possible to fold.
379 unsigned CommuteIdx0 = TargetInstrInfo::CommuteAnyOperandIndex;
380 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
381 bool CanCommute = TII->findCommutedOpIndices(*MI, CommuteIdx0, CommuteIdx1);
384 if (CommuteIdx0 == OpNo)
385 CommuteOpNo = CommuteIdx1;
386 else if (CommuteIdx1 == OpNo)
387 CommuteOpNo = CommuteIdx0;
391 // One of operands might be an Imm operand, and OpNo may refer to it after
392 // the call of commuteInstruction() below. Such situations are avoided
393 // here explicitly as OpNo must be a register operand to be a candidate
394 // for memory folding.
395 if (CanCommute && (!MI->getOperand(CommuteIdx0).isReg() ||
396 !MI->getOperand(CommuteIdx1).isReg()))
400 !TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1))
403 if (!TII->isOperandLegal(*MI, CommuteOpNo, OpToFold)) {
404 if ((Opc == AMDGPU::V_ADD_CO_U32_e64 ||
405 Opc == AMDGPU::V_SUB_CO_U32_e64 ||
406 Opc == AMDGPU::V_SUBREV_CO_U32_e64) && // FIXME
407 (OpToFold->isImm() || OpToFold->isFI() || OpToFold->isGlobal())) {
409 // Verify the other operand is a VGPR, otherwise we would violate the
410 // constant bus restriction.
411 unsigned OtherIdx = CommuteOpNo == CommuteIdx0 ? CommuteIdx1 : CommuteIdx0;
412 MachineOperand &OtherOp = MI->getOperand(OtherIdx);
413 if (!OtherOp.isReg() ||
414 !TII->getRegisterInfo().isVGPR(*MRI, OtherOp.getReg()))
417 assert(MI->getOperand(1).isDef());
419 // Make sure to get the 32-bit version of the commuted opcode.
420 unsigned MaybeCommutedOpc = MI->getOpcode();
421 int Op32 = AMDGPU::getVOPe32(MaybeCommutedOpc);
423 appendFoldCandidate(FoldList, MI, CommuteOpNo, OpToFold, true, Op32);
427 TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1);
431 appendFoldCandidate(FoldList, MI, CommuteOpNo, OpToFold, true);
435 // Check the case where we might introduce a second constant operand to a
436 // scalar instruction
437 if (TII->isSALU(MI->getOpcode())) {
438 const MCInstrDesc &InstDesc = MI->getDesc();
439 const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo];
441 // Fine if the operand can be encoded as an inline constant
442 if (!OpToFold->isReg() && !TII->isInlineConstant(*OpToFold, OpInfo)) {
443 // Otherwise check for another constant
444 for (unsigned i = 0, e = InstDesc.getNumOperands(); i != e; ++i) {
445 auto &Op = MI->getOperand(i);
446 if (OpNo != i && !Op.isReg() && !TII->isInlineConstant(Op, OpInfo))
452 appendFoldCandidate(FoldList, MI, OpNo, OpToFold);
456 bool SIFoldOperands::isUseSafeToFold(const MachineInstr &MI,
457 const MachineOperand &UseMO) const {
458 // Operands of SDWA instructions must be registers.
459 return !TII->isSDWA(MI);
462 // Find a def of the UseReg, check if it is a reg_sequence and find initializers
463 // for each subreg, tracking it to foldable inline immediate if possible.
464 // Returns true on success.
465 bool SIFoldOperands::getRegSeqInit(
466 SmallVectorImpl<std::pair<MachineOperand *, unsigned>> &Defs,
467 Register UseReg, uint8_t OpTy) const {
468 MachineInstr *Def = MRI->getVRegDef(UseReg);
469 if (!Def || !Def->isRegSequence())
472 for (unsigned I = 1, E = Def->getNumExplicitOperands(); I < E; I += 2) {
473 MachineOperand *Sub = &Def->getOperand(I);
474 assert(Sub->isReg());
476 for (MachineInstr *SubDef = MRI->getVRegDef(Sub->getReg());
477 SubDef && Sub->isReg() && Sub->getReg().isVirtual() &&
478 !Sub->getSubReg() && TII->isFoldableCopy(*SubDef);
479 SubDef = MRI->getVRegDef(Sub->getReg())) {
480 MachineOperand *Op = &SubDef->getOperand(1);
482 if (TII->isInlineConstant(*Op, OpTy))
486 if (!Op->isReg() || Op->getReg().isPhysical())
491 Defs.emplace_back(Sub, Def->getOperand(I + 1).getImm());
497 bool SIFoldOperands::tryToFoldACImm(
498 const MachineOperand &OpToFold, MachineInstr *UseMI, unsigned UseOpIdx,
499 SmallVectorImpl<FoldCandidate> &FoldList) const {
500 const MCInstrDesc &Desc = UseMI->getDesc();
501 if (UseOpIdx >= Desc.getNumOperands())
504 uint8_t OpTy = Desc.operands()[UseOpIdx].OperandType;
505 if ((OpTy < AMDGPU::OPERAND_REG_INLINE_AC_FIRST ||
506 OpTy > AMDGPU::OPERAND_REG_INLINE_AC_LAST) &&
507 (OpTy < AMDGPU::OPERAND_REG_INLINE_C_FIRST ||
508 OpTy > AMDGPU::OPERAND_REG_INLINE_C_LAST))
511 if (OpToFold.isImm() && TII->isInlineConstant(OpToFold, OpTy) &&
512 TII->isOperandLegal(*UseMI, UseOpIdx, &OpToFold)) {
513 UseMI->getOperand(UseOpIdx).ChangeToImmediate(OpToFold.getImm());
517 if (!OpToFold.isReg())
520 Register UseReg = OpToFold.getReg();
521 if (!UseReg.isVirtual())
524 if (isUseMIInFoldList(FoldList, UseMI))
527 // Maybe it is just a COPY of an immediate itself.
528 MachineInstr *Def = MRI->getVRegDef(UseReg);
529 MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
530 if (!UseOp.getSubReg() && Def && TII->isFoldableCopy(*Def)) {
531 MachineOperand &DefOp = Def->getOperand(1);
532 if (DefOp.isImm() && TII->isInlineConstant(DefOp, OpTy) &&
533 TII->isOperandLegal(*UseMI, UseOpIdx, &DefOp)) {
534 UseMI->getOperand(UseOpIdx).ChangeToImmediate(DefOp.getImm());
539 SmallVector<std::pair<MachineOperand*, unsigned>, 32> Defs;
540 if (!getRegSeqInit(Defs, UseReg, OpTy))
544 for (unsigned I = 0, E = Defs.size(); I != E; ++I) {
545 const MachineOperand *Op = Defs[I].first;
549 auto SubImm = Op->getImm();
552 if (!TII->isInlineConstant(*Op, OpTy) ||
553 !TII->isOperandLegal(*UseMI, UseOpIdx, Op))
559 return false; // Can only fold splat constants
562 appendFoldCandidate(FoldList, UseMI, UseOpIdx, Defs[0].first);
566 void SIFoldOperands::foldOperand(
567 MachineOperand &OpToFold,
570 SmallVectorImpl<FoldCandidate> &FoldList,
571 SmallVectorImpl<MachineInstr *> &CopiesToReplace) const {
572 const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
574 if (!isUseSafeToFold(*UseMI, UseOp))
577 // FIXME: Fold operands with subregs.
578 if (UseOp.isReg() && OpToFold.isReg() &&
579 (UseOp.isImplicit() || UseOp.getSubReg() != AMDGPU::NoSubRegister))
582 // Special case for REG_SEQUENCE: We can't fold literals into
583 // REG_SEQUENCE instructions, so we have to fold them into the
584 // uses of REG_SEQUENCE.
585 if (UseMI->isRegSequence()) {
586 Register RegSeqDstReg = UseMI->getOperand(0).getReg();
587 unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
589 for (auto &RSUse : make_early_inc_range(MRI->use_nodbg_operands(RegSeqDstReg))) {
590 MachineInstr *RSUseMI = RSUse.getParent();
592 if (tryToFoldACImm(UseMI->getOperand(0), RSUseMI,
593 RSUseMI->getOperandNo(&RSUse), FoldList))
596 if (RSUse.getSubReg() != RegSeqDstSubReg)
599 foldOperand(OpToFold, RSUseMI, RSUseMI->getOperandNo(&RSUse), FoldList,
606 if (tryToFoldACImm(OpToFold, UseMI, UseOpIdx, FoldList))
609 if (frameIndexMayFold(*UseMI, UseOpIdx, OpToFold)) {
610 // Verify that this is a stack access.
611 // FIXME: Should probably use stack pseudos before frame lowering.
613 if (TII->isMUBUF(*UseMI)) {
614 if (TII->getNamedOperand(*UseMI, AMDGPU::OpName::srsrc)->getReg() !=
615 MFI->getScratchRSrcReg())
618 // Ensure this is either relative to the current frame or the current
620 MachineOperand &SOff =
621 *TII->getNamedOperand(*UseMI, AMDGPU::OpName::soffset);
622 if (!SOff.isImm() || SOff.getImm() != 0)
626 // A frame index will resolve to a positive constant, so it should always be
627 // safe to fold the addressing mode, even pre-GFX9.
628 UseMI->getOperand(UseOpIdx).ChangeToFrameIndex(OpToFold.getIndex());
630 const unsigned Opc = UseMI->getOpcode();
631 if (TII->isFLATScratch(*UseMI) &&
632 AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::vaddr) &&
633 !AMDGPU::hasNamedOperand(Opc, AMDGPU::OpName::saddr)) {
634 unsigned NewOpc = AMDGPU::getFlatScratchInstSSfromSV(Opc);
635 UseMI->setDesc(TII->get(NewOpc));
641 bool FoldingImmLike =
642 OpToFold.isImm() || OpToFold.isFI() || OpToFold.isGlobal();
644 if (FoldingImmLike && UseMI->isCopy()) {
645 Register DestReg = UseMI->getOperand(0).getReg();
646 Register SrcReg = UseMI->getOperand(1).getReg();
647 assert(SrcReg.isVirtual());
649 const TargetRegisterClass *SrcRC = MRI->getRegClass(SrcReg);
651 // Don't fold into a copy to a physical register with the same class. Doing
652 // so would interfere with the register coalescer's logic which would avoid
653 // redundant initializations.
654 if (DestReg.isPhysical() && SrcRC->contains(DestReg))
657 const TargetRegisterClass *DestRC = TRI->getRegClassForReg(*MRI, DestReg);
658 if (!DestReg.isPhysical()) {
659 if (TRI->isSGPRClass(SrcRC) && TRI->hasVectorRegisters(DestRC)) {
660 SmallVector<FoldCandidate, 4> CopyUses;
661 for (auto &Use : MRI->use_nodbg_operands(DestReg)) {
662 // There's no point trying to fold into an implicit operand.
663 if (Use.isImplicit())
666 CopyUses.emplace_back(Use.getParent(),
667 Use.getParent()->getOperandNo(&Use),
668 &UseMI->getOperand(1));
671 for (auto &F : CopyUses) {
672 foldOperand(*F.OpToFold, F.UseMI, F.UseOpNo, FoldList,
677 if (DestRC == &AMDGPU::AGPR_32RegClass &&
678 TII->isInlineConstant(OpToFold, AMDGPU::OPERAND_REG_INLINE_C_INT32)) {
679 UseMI->setDesc(TII->get(AMDGPU::V_ACCVGPR_WRITE_B32_e64));
680 UseMI->getOperand(1).ChangeToImmediate(OpToFold.getImm());
681 CopiesToReplace.push_back(UseMI);
686 // In order to fold immediates into copies, we need to change the
689 unsigned MovOp = TII->getMovOpcode(DestRC);
690 if (MovOp == AMDGPU::COPY)
693 UseMI->setDesc(TII->get(MovOp));
694 MachineInstr::mop_iterator ImpOpI = UseMI->implicit_operands().begin();
695 MachineInstr::mop_iterator ImpOpE = UseMI->implicit_operands().end();
696 while (ImpOpI != ImpOpE) {
697 MachineInstr::mop_iterator Tmp = ImpOpI;
699 UseMI->removeOperand(UseMI->getOperandNo(Tmp));
701 CopiesToReplace.push_back(UseMI);
703 if (UseMI->isCopy() && OpToFold.isReg() &&
704 UseMI->getOperand(0).getReg().isVirtual() &&
705 !UseMI->getOperand(1).getSubReg()) {
706 LLVM_DEBUG(dbgs() << "Folding " << OpToFold << "\n into " << *UseMI);
707 unsigned Size = TII->getOpSize(*UseMI, 1);
708 Register UseReg = OpToFold.getReg();
709 UseMI->getOperand(1).setReg(UseReg);
710 UseMI->getOperand(1).setSubReg(OpToFold.getSubReg());
711 UseMI->getOperand(1).setIsKill(false);
712 CopiesToReplace.push_back(UseMI);
713 OpToFold.setIsKill(false);
715 // Remove kill flags as kills may now be out of order with uses.
716 MRI->clearKillFlags(OpToFold.getReg());
718 // That is very tricky to store a value into an AGPR. v_accvgpr_write_b32
719 // can only accept VGPR or inline immediate. Recreate a reg_sequence with
720 // its initializers right here, so we will rematerialize immediates and
721 // avoid copies via different reg classes.
722 SmallVector<std::pair<MachineOperand*, unsigned>, 32> Defs;
723 if (Size > 4 && TRI->isAGPR(*MRI, UseMI->getOperand(0).getReg()) &&
724 getRegSeqInit(Defs, UseReg, AMDGPU::OPERAND_REG_INLINE_C_INT32)) {
725 const DebugLoc &DL = UseMI->getDebugLoc();
726 MachineBasicBlock &MBB = *UseMI->getParent();
728 UseMI->setDesc(TII->get(AMDGPU::REG_SEQUENCE));
729 for (unsigned I = UseMI->getNumOperands() - 1; I > 0; --I)
730 UseMI->removeOperand(I);
732 MachineInstrBuilder B(*MBB.getParent(), UseMI);
733 DenseMap<TargetInstrInfo::RegSubRegPair, Register> VGPRCopies;
734 SmallSetVector<TargetInstrInfo::RegSubRegPair, 32> SeenAGPRs;
735 for (unsigned I = 0; I < Size / 4; ++I) {
736 MachineOperand *Def = Defs[I].first;
737 TargetInstrInfo::RegSubRegPair CopyToVGPR;
739 TII->isInlineConstant(*Def, AMDGPU::OPERAND_REG_INLINE_C_INT32)) {
740 int64_t Imm = Def->getImm();
742 auto Tmp = MRI->createVirtualRegister(&AMDGPU::AGPR_32RegClass);
743 BuildMI(MBB, UseMI, DL,
744 TII->get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), Tmp).addImm(Imm);
746 } else if (Def->isReg() && TRI->isAGPR(*MRI, Def->getReg())) {
747 auto Src = getRegSubRegPair(*Def);
748 Def->setIsKill(false);
749 if (!SeenAGPRs.insert(Src)) {
750 // We cannot build a reg_sequence out of the same registers, they
751 // must be copied. Better do it here before copyPhysReg() created
752 // several reads to do the AGPR->VGPR->AGPR copy.
755 B.addReg(Src.Reg, Def->isUndef() ? RegState::Undef : 0,
759 assert(Def->isReg());
760 Def->setIsKill(false);
761 auto Src = getRegSubRegPair(*Def);
763 // Direct copy from SGPR to AGPR is not possible. To avoid creation
764 // of exploded copies SGPR->VGPR->AGPR in the copyPhysReg() later,
765 // create a copy here and track if we already have such a copy.
766 if (TRI->isSGPRReg(*MRI, Src.Reg)) {
769 auto Tmp = MRI->createVirtualRegister(&AMDGPU::AGPR_32RegClass);
770 BuildMI(MBB, UseMI, DL, TII->get(AMDGPU::COPY), Tmp).add(*Def);
775 if (CopyToVGPR.Reg) {
777 if (VGPRCopies.count(CopyToVGPR)) {
778 Vgpr = VGPRCopies[CopyToVGPR];
780 Vgpr = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
781 BuildMI(MBB, UseMI, DL, TII->get(AMDGPU::COPY), Vgpr).add(*Def);
782 VGPRCopies[CopyToVGPR] = Vgpr;
784 auto Tmp = MRI->createVirtualRegister(&AMDGPU::AGPR_32RegClass);
785 BuildMI(MBB, UseMI, DL,
786 TII->get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), Tmp).addReg(Vgpr);
790 B.addImm(Defs[I].second);
792 LLVM_DEBUG(dbgs() << "Folded " << *UseMI);
799 Register Reg0 = UseMI->getOperand(0).getReg();
800 Register Reg1 = UseMI->getOperand(1).getReg();
801 if (TRI->isAGPR(*MRI, Reg0) && TRI->isVGPR(*MRI, Reg1))
802 UseMI->setDesc(TII->get(AMDGPU::V_ACCVGPR_WRITE_B32_e64));
803 else if (TRI->isVGPR(*MRI, Reg0) && TRI->isAGPR(*MRI, Reg1))
804 UseMI->setDesc(TII->get(AMDGPU::V_ACCVGPR_READ_B32_e64));
805 else if (ST->hasGFX90AInsts() && TRI->isAGPR(*MRI, Reg0) &&
806 TRI->isAGPR(*MRI, Reg1))
807 UseMI->setDesc(TII->get(AMDGPU::V_ACCVGPR_MOV_B32));
811 unsigned UseOpc = UseMI->getOpcode();
812 if (UseOpc == AMDGPU::V_READFIRSTLANE_B32 ||
813 (UseOpc == AMDGPU::V_READLANE_B32 &&
815 AMDGPU::getNamedOperandIdx(UseOpc, AMDGPU::OpName::src0))) {
816 // %vgpr = V_MOV_B32 imm
817 // %sgpr = V_READFIRSTLANE_B32 %vgpr
819 // %sgpr = S_MOV_B32 imm
820 if (FoldingImmLike) {
821 if (execMayBeModifiedBeforeUse(*MRI,
822 UseMI->getOperand(UseOpIdx).getReg(),
823 *OpToFold.getParent(),
827 UseMI->setDesc(TII->get(AMDGPU::S_MOV_B32));
829 if (OpToFold.isImm())
830 UseMI->getOperand(1).ChangeToImmediate(OpToFold.getImm());
832 UseMI->getOperand(1).ChangeToFrameIndex(OpToFold.getIndex());
833 UseMI->removeOperand(2); // Remove exec read (or src1 for readlane)
837 if (OpToFold.isReg() && TRI->isSGPRReg(*MRI, OpToFold.getReg())) {
838 if (execMayBeModifiedBeforeUse(*MRI,
839 UseMI->getOperand(UseOpIdx).getReg(),
840 *OpToFold.getParent(),
844 // %vgpr = COPY %sgpr0
845 // %sgpr1 = V_READFIRSTLANE_B32 %vgpr
847 // %sgpr1 = COPY %sgpr0
848 UseMI->setDesc(TII->get(AMDGPU::COPY));
849 UseMI->getOperand(1).setReg(OpToFold.getReg());
850 UseMI->getOperand(1).setSubReg(OpToFold.getSubReg());
851 UseMI->getOperand(1).setIsKill(false);
852 UseMI->removeOperand(2); // Remove exec read (or src1 for readlane)
857 const MCInstrDesc &UseDesc = UseMI->getDesc();
859 // Don't fold into target independent nodes. Target independent opcodes
860 // don't have defined register classes.
861 if (UseDesc.isVariadic() || UseOp.isImplicit() ||
862 UseDesc.operands()[UseOpIdx].RegClass == -1)
866 if (!FoldingImmLike) {
867 if (OpToFold.isReg() && ST->needsAlignedVGPRs()) {
868 // Don't fold if OpToFold doesn't hold an aligned register.
869 const TargetRegisterClass *RC =
870 TRI->getRegClassForReg(*MRI, OpToFold.getReg());
871 if (TRI->hasVectorRegisters(RC) && OpToFold.getSubReg()) {
872 unsigned SubReg = OpToFold.getSubReg();
873 if (const TargetRegisterClass *SubRC =
874 TRI->getSubRegisterClass(RC, SubReg))
878 if (!RC || !TRI->isProperlyAlignedRC(*RC))
882 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold);
884 // FIXME: We could try to change the instruction from 64-bit to 32-bit
885 // to enable more folding opportunities. The shrink operands pass
886 // already does this.
891 const MCInstrDesc &FoldDesc = OpToFold.getParent()->getDesc();
892 const TargetRegisterClass *FoldRC =
893 TRI->getRegClass(FoldDesc.operands()[0].RegClass);
895 // Split 64-bit constants into 32-bits for folding.
896 if (UseOp.getSubReg() && AMDGPU::getRegBitWidth(FoldRC->getID()) == 64) {
897 Register UseReg = UseOp.getReg();
898 const TargetRegisterClass *UseRC = MRI->getRegClass(UseReg);
900 if (AMDGPU::getRegBitWidth(UseRC->getID()) != 64)
903 APInt Imm(64, OpToFold.getImm());
904 if (UseOp.getSubReg() == AMDGPU::sub0) {
905 Imm = Imm.getLoBits(32);
907 assert(UseOp.getSubReg() == AMDGPU::sub1);
908 Imm = Imm.getHiBits(32);
911 MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
912 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp);
916 tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold);
919 static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result,
920 uint32_t LHS, uint32_t RHS) {
922 case AMDGPU::V_AND_B32_e64:
923 case AMDGPU::V_AND_B32_e32:
924 case AMDGPU::S_AND_B32:
927 case AMDGPU::V_OR_B32_e64:
928 case AMDGPU::V_OR_B32_e32:
929 case AMDGPU::S_OR_B32:
932 case AMDGPU::V_XOR_B32_e64:
933 case AMDGPU::V_XOR_B32_e32:
934 case AMDGPU::S_XOR_B32:
937 case AMDGPU::S_XNOR_B32:
938 Result = ~(LHS ^ RHS);
940 case AMDGPU::S_NAND_B32:
941 Result = ~(LHS & RHS);
943 case AMDGPU::S_NOR_B32:
944 Result = ~(LHS | RHS);
946 case AMDGPU::S_ANDN2_B32:
949 case AMDGPU::S_ORN2_B32:
952 case AMDGPU::V_LSHL_B32_e64:
953 case AMDGPU::V_LSHL_B32_e32:
954 case AMDGPU::S_LSHL_B32:
955 // The instruction ignores the high bits for out of bounds shifts.
956 Result = LHS << (RHS & 31);
958 case AMDGPU::V_LSHLREV_B32_e64:
959 case AMDGPU::V_LSHLREV_B32_e32:
960 Result = RHS << (LHS & 31);
962 case AMDGPU::V_LSHR_B32_e64:
963 case AMDGPU::V_LSHR_B32_e32:
964 case AMDGPU::S_LSHR_B32:
965 Result = LHS >> (RHS & 31);
967 case AMDGPU::V_LSHRREV_B32_e64:
968 case AMDGPU::V_LSHRREV_B32_e32:
969 Result = RHS >> (LHS & 31);
971 case AMDGPU::V_ASHR_I32_e64:
972 case AMDGPU::V_ASHR_I32_e32:
973 case AMDGPU::S_ASHR_I32:
974 Result = static_cast<int32_t>(LHS) >> (RHS & 31);
976 case AMDGPU::V_ASHRREV_I32_e64:
977 case AMDGPU::V_ASHRREV_I32_e32:
978 Result = static_cast<int32_t>(RHS) >> (LHS & 31);
985 static unsigned getMovOpc(bool IsScalar) {
986 return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
989 static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc) {
992 // Remove any leftover implicit operands from mutating the instruction. e.g.
993 // if we replace an s_and_b32 with a copy, we don't need the implicit scc def
995 const MCInstrDesc &Desc = MI.getDesc();
996 unsigned NumOps = Desc.getNumOperands() + Desc.implicit_uses().size() +
997 Desc.implicit_defs().size();
999 for (unsigned I = MI.getNumOperands() - 1; I >= NumOps; --I)
1000 MI.removeOperand(I);
1004 SIFoldOperands::getImmOrMaterializedImm(MachineOperand &Op) const {
1005 // If this has a subregister, it obviously is a register source.
1006 if (!Op.isReg() || Op.getSubReg() != AMDGPU::NoSubRegister ||
1007 !Op.getReg().isVirtual())
1010 MachineInstr *Def = MRI->getVRegDef(Op.getReg());
1011 if (Def && Def->isMoveImmediate()) {
1012 MachineOperand &ImmSrc = Def->getOperand(1);
1020 // Try to simplify operations with a constant that may appear after instruction
1022 // TODO: See if a frame index with a fixed offset can fold.
1023 bool SIFoldOperands::tryConstantFoldOp(MachineInstr *MI) const {
1024 unsigned Opc = MI->getOpcode();
1026 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
1029 MachineOperand *Src0 = getImmOrMaterializedImm(MI->getOperand(Src0Idx));
1031 if ((Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
1032 Opc == AMDGPU::S_NOT_B32) &&
1034 MI->getOperand(1).ChangeToImmediate(~Src0->getImm());
1035 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_NOT_B32)));
1039 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
1042 MachineOperand *Src1 = getImmOrMaterializedImm(MI->getOperand(Src1Idx));
1044 if (!Src0->isImm() && !Src1->isImm())
1047 // and k0, k1 -> v_mov_b32 (k0 & k1)
1048 // or k0, k1 -> v_mov_b32 (k0 | k1)
1049 // xor k0, k1 -> v_mov_b32 (k0 ^ k1)
1050 if (Src0->isImm() && Src1->isImm()) {
1052 if (!evalBinaryInstruction(Opc, NewImm, Src0->getImm(), Src1->getImm()))
1055 bool IsSGPR = TRI->isSGPRReg(*MRI, MI->getOperand(0).getReg());
1057 // Be careful to change the right operand, src0 may belong to a different
1059 MI->getOperand(Src0Idx).ChangeToImmediate(NewImm);
1060 MI->removeOperand(Src1Idx);
1061 mutateCopyOp(*MI, TII->get(getMovOpc(IsSGPR)));
1065 if (!MI->isCommutable())
1068 if (Src0->isImm() && !Src1->isImm()) {
1069 std::swap(Src0, Src1);
1070 std::swap(Src0Idx, Src1Idx);
1073 int32_t Src1Val = static_cast<int32_t>(Src1->getImm());
1074 if (Opc == AMDGPU::V_OR_B32_e64 ||
1075 Opc == AMDGPU::V_OR_B32_e32 ||
1076 Opc == AMDGPU::S_OR_B32) {
1078 // y = or x, 0 => y = copy x
1079 MI->removeOperand(Src1Idx);
1080 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
1081 } else if (Src1Val == -1) {
1082 // y = or x, -1 => y = v_mov_b32 -1
1083 MI->removeOperand(Src1Idx);
1084 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_OR_B32)));
1091 if (Opc == AMDGPU::V_AND_B32_e64 || Opc == AMDGPU::V_AND_B32_e32 ||
1092 Opc == AMDGPU::S_AND_B32) {
1094 // y = and x, 0 => y = v_mov_b32 0
1095 MI->removeOperand(Src0Idx);
1096 mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_AND_B32)));
1097 } else if (Src1Val == -1) {
1098 // y = and x, -1 => y = copy x
1099 MI->removeOperand(Src1Idx);
1100 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
1107 if (Opc == AMDGPU::V_XOR_B32_e64 || Opc == AMDGPU::V_XOR_B32_e32 ||
1108 Opc == AMDGPU::S_XOR_B32) {
1110 // y = xor x, 0 => y = copy x
1111 MI->removeOperand(Src1Idx);
1112 mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
1120 // Try to fold an instruction into a simpler one
1121 bool SIFoldOperands::tryFoldCndMask(MachineInstr &MI) const {
1122 unsigned Opc = MI.getOpcode();
1123 if (Opc != AMDGPU::V_CNDMASK_B32_e32 && Opc != AMDGPU::V_CNDMASK_B32_e64 &&
1124 Opc != AMDGPU::V_CNDMASK_B64_PSEUDO)
1127 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
1128 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
1129 if (!Src1->isIdenticalTo(*Src0)) {
1130 auto *Src0Imm = getImmOrMaterializedImm(*Src0);
1131 auto *Src1Imm = getImmOrMaterializedImm(*Src1);
1132 if (!Src1Imm->isIdenticalTo(*Src0Imm))
1137 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1_modifiers);
1139 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0_modifiers);
1140 if ((Src1ModIdx != -1 && MI.getOperand(Src1ModIdx).getImm() != 0) ||
1141 (Src0ModIdx != -1 && MI.getOperand(Src0ModIdx).getImm() != 0))
1144 LLVM_DEBUG(dbgs() << "Folded " << MI << " into ");
1146 TII->get(Src0->isReg() ? (unsigned)AMDGPU::COPY : getMovOpc(false));
1147 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
1149 MI.removeOperand(Src2Idx);
1150 MI.removeOperand(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1));
1151 if (Src1ModIdx != -1)
1152 MI.removeOperand(Src1ModIdx);
1153 if (Src0ModIdx != -1)
1154 MI.removeOperand(Src0ModIdx);
1155 mutateCopyOp(MI, NewDesc);
1156 LLVM_DEBUG(dbgs() << MI);
1160 bool SIFoldOperands::tryFoldZeroHighBits(MachineInstr &MI) const {
1161 if (MI.getOpcode() != AMDGPU::V_AND_B32_e64 &&
1162 MI.getOpcode() != AMDGPU::V_AND_B32_e32)
1165 MachineOperand *Src0 = getImmOrMaterializedImm(MI.getOperand(1));
1166 if (!Src0->isImm() || Src0->getImm() != 0xffff)
1169 Register Src1 = MI.getOperand(2).getReg();
1170 MachineInstr *SrcDef = MRI->getVRegDef(Src1);
1171 if (!ST->zeroesHigh16BitsOfDest(SrcDef->getOpcode()))
1174 Register Dst = MI.getOperand(0).getReg();
1175 MRI->replaceRegWith(Dst, SrcDef->getOperand(0).getReg());
1176 MI.eraseFromParent();
1180 bool SIFoldOperands::foldInstOperand(MachineInstr &MI,
1181 MachineOperand &OpToFold) const {
1182 // We need mutate the operands of new mov instructions to add implicit
1183 // uses of EXEC, but adding them invalidates the use_iterator, so defer
1185 SmallVector<MachineInstr *, 4> CopiesToReplace;
1186 SmallVector<FoldCandidate, 4> FoldList;
1187 MachineOperand &Dst = MI.getOperand(0);
1188 bool Changed = false;
1190 if (OpToFold.isImm()) {
1192 make_early_inc_range(MRI->use_nodbg_instructions(Dst.getReg()))) {
1193 // Folding the immediate may reveal operations that can be constant
1194 // folded or replaced with a copy. This can happen for example after
1195 // frame indices are lowered to constants or from splitting 64-bit
1198 // We may also encounter cases where one or both operands are
1199 // immediates materialized into a register, which would ordinarily not
1200 // be folded due to multiple uses or operand constraints.
1201 if (tryConstantFoldOp(&UseMI)) {
1202 LLVM_DEBUG(dbgs() << "Constant folded " << UseMI);
1208 SmallVector<MachineOperand *, 4> UsesToProcess;
1209 for (auto &Use : MRI->use_nodbg_operands(Dst.getReg()))
1210 UsesToProcess.push_back(&Use);
1211 for (auto *U : UsesToProcess) {
1212 MachineInstr *UseMI = U->getParent();
1213 foldOperand(OpToFold, UseMI, UseMI->getOperandNo(U), FoldList,
1217 if (CopiesToReplace.empty() && FoldList.empty())
1220 MachineFunction *MF = MI.getParent()->getParent();
1221 // Make sure we add EXEC uses to any new v_mov instructions created.
1222 for (MachineInstr *Copy : CopiesToReplace)
1223 Copy->addImplicitDefUseOperands(*MF);
1225 for (FoldCandidate &Fold : FoldList) {
1226 assert(!Fold.isReg() || Fold.OpToFold);
1227 if (Fold.isReg() && Fold.OpToFold->getReg().isVirtual()) {
1228 Register Reg = Fold.OpToFold->getReg();
1229 MachineInstr *DefMI = Fold.OpToFold->getParent();
1230 if (DefMI->readsRegister(AMDGPU::EXEC, TRI) &&
1231 execMayBeModifiedBeforeUse(*MRI, Reg, *DefMI, *Fold.UseMI))
1234 if (updateOperand(Fold)) {
1235 // Clear kill flags.
1237 assert(Fold.OpToFold && Fold.OpToFold->isReg());
1238 // FIXME: Probably shouldn't bother trying to fold if not an
1239 // SGPR. PeepholeOptimizer can eliminate redundant VGPR->VGPR
1241 MRI->clearKillFlags(Fold.OpToFold->getReg());
1243 LLVM_DEBUG(dbgs() << "Folded source from " << MI << " into OpNo "
1244 << static_cast<int>(Fold.UseOpNo) << " of "
1246 } else if (Fold.Commuted) {
1247 // Restoring instruction's original operand order if fold has failed.
1248 TII->commuteInstruction(*Fold.UseMI, false);
1254 bool SIFoldOperands::tryFoldFoldableCopy(
1255 MachineInstr &MI, MachineOperand *&CurrentKnownM0Val) const {
1256 // Specially track simple redefs of m0 to the same value in a block, so we
1257 // can erase the later ones.
1258 if (MI.getOperand(0).getReg() == AMDGPU::M0) {
1259 MachineOperand &NewM0Val = MI.getOperand(1);
1260 if (CurrentKnownM0Val && CurrentKnownM0Val->isIdenticalTo(NewM0Val)) {
1261 MI.eraseFromParent();
1265 // We aren't tracking other physical registers
1266 CurrentKnownM0Val = (NewM0Val.isReg() && NewM0Val.getReg().isPhysical())
1272 MachineOperand &OpToFold = MI.getOperand(1);
1273 bool FoldingImm = OpToFold.isImm() || OpToFold.isFI() || OpToFold.isGlobal();
1275 // FIXME: We could also be folding things like TargetIndexes.
1276 if (!FoldingImm && !OpToFold.isReg())
1279 if (OpToFold.isReg() && !OpToFold.getReg().isVirtual())
1282 // Prevent folding operands backwards in the function. For example,
1283 // the COPY opcode must not be replaced by 1 in this example:
1285 // %3 = COPY %vgpr0; VGPR_32:%3
1287 // %vgpr0 = V_MOV_B32_e32 1, implicit %exec
1288 if (!MI.getOperand(0).getReg().isVirtual())
1291 bool Changed = foldInstOperand(MI, OpToFold);
1293 // If we managed to fold all uses of this copy then we might as well
1295 // The only reason we need to follow chains of copies here is that
1296 // tryFoldRegSequence looks forward through copies before folding a
1297 // REG_SEQUENCE into its eventual users.
1298 auto *InstToErase = &MI;
1299 while (MRI->use_nodbg_empty(InstToErase->getOperand(0).getReg())) {
1300 auto &SrcOp = InstToErase->getOperand(1);
1301 auto SrcReg = SrcOp.isReg() ? SrcOp.getReg() : Register();
1302 InstToErase->eraseFromParent();
1304 InstToErase = nullptr;
1305 if (!SrcReg || SrcReg.isPhysical())
1307 InstToErase = MRI->getVRegDef(SrcReg);
1308 if (!InstToErase || !TII->isFoldableCopy(*InstToErase))
1312 if (InstToErase && InstToErase->isRegSequence() &&
1313 MRI->use_nodbg_empty(InstToErase->getOperand(0).getReg())) {
1314 InstToErase->eraseFromParent();
1321 // Clamp patterns are canonically selected to v_max_* instructions, so only
1323 const MachineOperand *SIFoldOperands::isClamp(const MachineInstr &MI) const {
1324 unsigned Op = MI.getOpcode();
1326 case AMDGPU::V_MAX_F32_e64:
1327 case AMDGPU::V_MAX_F16_e64:
1328 case AMDGPU::V_MAX_F16_t16_e64:
1329 case AMDGPU::V_MAX_F64_e64:
1330 case AMDGPU::V_PK_MAX_F16: {
1331 if (!TII->getNamedOperand(MI, AMDGPU::OpName::clamp)->getImm())
1334 // Make sure sources are identical.
1335 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
1336 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
1337 if (!Src0->isReg() || !Src1->isReg() ||
1338 Src0->getReg() != Src1->getReg() ||
1339 Src0->getSubReg() != Src1->getSubReg() ||
1340 Src0->getSubReg() != AMDGPU::NoSubRegister)
1343 // Can't fold up if we have modifiers.
1344 if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
1348 = TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm();
1350 = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm();
1352 // Having a 0 op_sel_hi would require swizzling the output in the source
1353 // instruction, which we can't do.
1354 unsigned UnsetMods = (Op == AMDGPU::V_PK_MAX_F16) ? SISrcMods::OP_SEL_1
1356 if (Src0Mods != UnsetMods && Src1Mods != UnsetMods)
1365 // FIXME: Clamp for v_mad_mixhi_f16 handled during isel.
1366 bool SIFoldOperands::tryFoldClamp(MachineInstr &MI) {
1367 const MachineOperand *ClampSrc = isClamp(MI);
1368 if (!ClampSrc || !MRI->hasOneNonDBGUser(ClampSrc->getReg()))
1371 MachineInstr *Def = MRI->getVRegDef(ClampSrc->getReg());
1373 // The type of clamp must be compatible.
1374 if (TII->getClampMask(*Def) != TII->getClampMask(MI))
1377 MachineOperand *DefClamp = TII->getNamedOperand(*Def, AMDGPU::OpName::clamp);
1381 LLVM_DEBUG(dbgs() << "Folding clamp " << *DefClamp << " into " << *Def);
1383 // Clamp is applied after omod, so it is OK if omod is set.
1384 DefClamp->setImm(1);
1385 MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
1386 MI.eraseFromParent();
1388 // Use of output modifiers forces VOP3 encoding for a VOP2 mac/fmac
1389 // instruction, so we might as well convert it to the more flexible VOP3-only
1391 if (TII->convertToThreeAddress(*Def, nullptr, nullptr))
1392 Def->eraseFromParent();
1397 static int getOModValue(unsigned Opc, int64_t Val) {
1399 case AMDGPU::V_MUL_F64_e64: {
1401 case 0x3fe0000000000000: // 0.5
1402 return SIOutMods::DIV2;
1403 case 0x4000000000000000: // 2.0
1404 return SIOutMods::MUL2;
1405 case 0x4010000000000000: // 4.0
1406 return SIOutMods::MUL4;
1408 return SIOutMods::NONE;
1411 case AMDGPU::V_MUL_F32_e64: {
1412 switch (static_cast<uint32_t>(Val)) {
1413 case 0x3f000000: // 0.5
1414 return SIOutMods::DIV2;
1415 case 0x40000000: // 2.0
1416 return SIOutMods::MUL2;
1417 case 0x40800000: // 4.0
1418 return SIOutMods::MUL4;
1420 return SIOutMods::NONE;
1423 case AMDGPU::V_MUL_F16_e64:
1424 case AMDGPU::V_MUL_F16_t16_e64: {
1425 switch (static_cast<uint16_t>(Val)) {
1427 return SIOutMods::DIV2;
1429 return SIOutMods::MUL2;
1431 return SIOutMods::MUL4;
1433 return SIOutMods::NONE;
1437 llvm_unreachable("invalid mul opcode");
1441 // FIXME: Does this really not support denormals with f16?
1442 // FIXME: Does this need to check IEEE mode bit? SNaNs are generally not
1443 // handled, so will anything other than that break?
1444 std::pair<const MachineOperand *, int>
1445 SIFoldOperands::isOMod(const MachineInstr &MI) const {
1446 unsigned Op = MI.getOpcode();
1448 case AMDGPU::V_MUL_F64_e64:
1449 case AMDGPU::V_MUL_F32_e64:
1450 case AMDGPU::V_MUL_F16_t16_e64:
1451 case AMDGPU::V_MUL_F16_e64: {
1452 // If output denormals are enabled, omod is ignored.
1453 if ((Op == AMDGPU::V_MUL_F32_e64 &&
1454 MFI->getMode().FP32Denormals.Output != DenormalMode::PreserveSign) ||
1455 ((Op == AMDGPU::V_MUL_F64_e64 || Op == AMDGPU::V_MUL_F16_e64 ||
1456 Op == AMDGPU::V_MUL_F16_t16_e64) &&
1457 MFI->getMode().FP64FP16Denormals.Output != DenormalMode::PreserveSign))
1458 return std::pair(nullptr, SIOutMods::NONE);
1460 const MachineOperand *RegOp = nullptr;
1461 const MachineOperand *ImmOp = nullptr;
1462 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
1463 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
1464 if (Src0->isImm()) {
1467 } else if (Src1->isImm()) {
1471 return std::pair(nullptr, SIOutMods::NONE);
1473 int OMod = getOModValue(Op, ImmOp->getImm());
1474 if (OMod == SIOutMods::NONE ||
1475 TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
1476 TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
1477 TII->hasModifiersSet(MI, AMDGPU::OpName::omod) ||
1478 TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
1479 return std::pair(nullptr, SIOutMods::NONE);
1481 return std::pair(RegOp, OMod);
1483 case AMDGPU::V_ADD_F64_e64:
1484 case AMDGPU::V_ADD_F32_e64:
1485 case AMDGPU::V_ADD_F16_e64:
1486 case AMDGPU::V_ADD_F16_t16_e64: {
1487 // If output denormals are enabled, omod is ignored.
1488 if ((Op == AMDGPU::V_ADD_F32_e64 &&
1489 MFI->getMode().FP32Denormals.Output != DenormalMode::PreserveSign) ||
1490 ((Op == AMDGPU::V_ADD_F64_e64 || Op == AMDGPU::V_ADD_F16_e64 ||
1491 Op == AMDGPU::V_ADD_F16_t16_e64) &&
1492 MFI->getMode().FP64FP16Denormals.Output != DenormalMode::PreserveSign))
1493 return std::pair(nullptr, SIOutMods::NONE);
1495 // Look through the DAGCombiner canonicalization fmul x, 2 -> fadd x, x
1496 const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
1497 const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
1499 if (Src0->isReg() && Src1->isReg() && Src0->getReg() == Src1->getReg() &&
1500 Src0->getSubReg() == Src1->getSubReg() &&
1501 !TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) &&
1502 !TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) &&
1503 !TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
1504 !TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
1505 return std::pair(Src0, SIOutMods::MUL2);
1507 return std::pair(nullptr, SIOutMods::NONE);
1510 return std::pair(nullptr, SIOutMods::NONE);
1514 // FIXME: Does this need to check IEEE bit on function?
1515 bool SIFoldOperands::tryFoldOMod(MachineInstr &MI) {
1516 const MachineOperand *RegOp;
1518 std::tie(RegOp, OMod) = isOMod(MI);
1519 if (OMod == SIOutMods::NONE || !RegOp->isReg() ||
1520 RegOp->getSubReg() != AMDGPU::NoSubRegister ||
1521 !MRI->hasOneNonDBGUser(RegOp->getReg()))
1524 MachineInstr *Def = MRI->getVRegDef(RegOp->getReg());
1525 MachineOperand *DefOMod = TII->getNamedOperand(*Def, AMDGPU::OpName::omod);
1526 if (!DefOMod || DefOMod->getImm() != SIOutMods::NONE)
1529 // Clamp is applied after omod. If the source already has clamp set, don't
1531 if (TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
1534 LLVM_DEBUG(dbgs() << "Folding omod " << MI << " into " << *Def);
1536 DefOMod->setImm(OMod);
1537 MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
1538 MI.eraseFromParent();
1540 // Use of output modifiers forces VOP3 encoding for a VOP2 mac/fmac
1541 // instruction, so we might as well convert it to the more flexible VOP3-only
1543 if (TII->convertToThreeAddress(*Def, nullptr, nullptr))
1544 Def->eraseFromParent();
1549 // Try to fold a reg_sequence with vgpr output and agpr inputs into an
1550 // instruction which can take an agpr. So far that means a store.
1551 bool SIFoldOperands::tryFoldRegSequence(MachineInstr &MI) {
1552 assert(MI.isRegSequence());
1553 auto Reg = MI.getOperand(0).getReg();
1555 if (!ST->hasGFX90AInsts() || !TRI->isVGPR(*MRI, Reg) ||
1556 !MRI->hasOneNonDBGUse(Reg))
1559 SmallVector<std::pair<MachineOperand*, unsigned>, 32> Defs;
1560 if (!getRegSeqInit(Defs, Reg, MCOI::OPERAND_REGISTER))
1563 for (auto &Def : Defs) {
1564 const auto *Op = Def.first;
1567 if (TRI->isAGPR(*MRI, Op->getReg()))
1569 // Maybe this is a COPY from AREG
1570 const MachineInstr *SubDef = MRI->getVRegDef(Op->getReg());
1571 if (!SubDef || !SubDef->isCopy() || SubDef->getOperand(1).getSubReg())
1573 if (!TRI->isAGPR(*MRI, SubDef->getOperand(1).getReg()))
1577 MachineOperand *Op = &*MRI->use_nodbg_begin(Reg);
1578 MachineInstr *UseMI = Op->getParent();
1579 while (UseMI->isCopy() && !Op->getSubReg()) {
1580 Reg = UseMI->getOperand(0).getReg();
1581 if (!TRI->isVGPR(*MRI, Reg) || !MRI->hasOneNonDBGUse(Reg))
1583 Op = &*MRI->use_nodbg_begin(Reg);
1584 UseMI = Op->getParent();
1587 if (Op->getSubReg())
1590 unsigned OpIdx = Op - &UseMI->getOperand(0);
1591 const MCInstrDesc &InstDesc = UseMI->getDesc();
1592 const TargetRegisterClass *OpRC =
1593 TII->getRegClass(InstDesc, OpIdx, TRI, *MI.getMF());
1594 if (!OpRC || !TRI->isVectorSuperClass(OpRC))
1597 const auto *NewDstRC = TRI->getEquivalentAGPRClass(MRI->getRegClass(Reg));
1598 auto Dst = MRI->createVirtualRegister(NewDstRC);
1599 auto RS = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
1600 TII->get(AMDGPU::REG_SEQUENCE), Dst);
1602 for (unsigned I = 0; I < Defs.size(); ++I) {
1603 MachineOperand *Def = Defs[I].first;
1604 Def->setIsKill(false);
1605 if (TRI->isAGPR(*MRI, Def->getReg())) {
1607 } else { // This is a copy
1608 MachineInstr *SubDef = MRI->getVRegDef(Def->getReg());
1609 SubDef->getOperand(1).setIsKill(false);
1610 RS.addReg(SubDef->getOperand(1).getReg(), 0, Def->getSubReg());
1612 RS.addImm(Defs[I].second);
1616 if (!TII->isOperandLegal(*UseMI, OpIdx, Op)) {
1618 RS->eraseFromParent();
1622 LLVM_DEBUG(dbgs() << "Folded " << *RS << " into " << *UseMI);
1624 // Erase the REG_SEQUENCE eagerly, unless we followed a chain of COPY users,
1625 // in which case we can erase them all later in runOnMachineFunction.
1626 if (MRI->use_nodbg_empty(MI.getOperand(0).getReg()))
1627 MI.eraseFromParent();
1631 // Try to hoist an AGPR to VGPR copy out of the loop across a LCSSA PHI.
1632 // This should allow folding of an AGPR into a consumer which may support it.
1636 // %1:vreg = COPY %0:areg // exit:
1637 // exit: => // %1:areg = PHI %0:areg, %loop
1638 // %2:vreg = PHI %1:vreg, %loop // %2:vreg = COPY %1:areg
1639 bool SIFoldOperands::tryFoldLCSSAPhi(MachineInstr &PHI) {
1640 assert(PHI.isPHI());
1642 if (PHI.getNumExplicitOperands() != 3) // Single input LCSSA PHI
1645 Register PhiIn = PHI.getOperand(1).getReg();
1646 Register PhiOut = PHI.getOperand(0).getReg();
1647 if (PHI.getOperand(1).getSubReg() ||
1648 !TRI->isVGPR(*MRI, PhiIn) || !TRI->isVGPR(*MRI, PhiOut))
1651 // A single use should not matter for correctness, but if it has another use
1652 // inside the loop we may perform copy twice in a worst case.
1653 if (!MRI->hasOneNonDBGUse(PhiIn))
1656 MachineInstr *Copy = MRI->getVRegDef(PhiIn);
1657 if (!Copy || !Copy->isCopy())
1660 Register CopyIn = Copy->getOperand(1).getReg();
1661 if (!TRI->isAGPR(*MRI, CopyIn) || Copy->getOperand(1).getSubReg())
1664 const TargetRegisterClass *ARC = MRI->getRegClass(CopyIn);
1665 Register NewReg = MRI->createVirtualRegister(ARC);
1666 PHI.getOperand(1).setReg(CopyIn);
1667 PHI.getOperand(0).setReg(NewReg);
1669 MachineBasicBlock *MBB = PHI.getParent();
1670 BuildMI(*MBB, MBB->getFirstNonPHI(), Copy->getDebugLoc(),
1671 TII->get(AMDGPU::COPY), PhiOut)
1672 .addReg(NewReg, RegState::Kill);
1673 Copy->eraseFromParent(); // We know this copy had a single use.
1675 LLVM_DEBUG(dbgs() << "Folded " << PHI);
1680 // Attempt to convert VGPR load to an AGPR load.
1681 bool SIFoldOperands::tryFoldLoad(MachineInstr &MI) {
1682 assert(MI.mayLoad());
1683 if (!ST->hasGFX90AInsts() || MI.getNumExplicitDefs() != 1)
1686 MachineOperand &Def = MI.getOperand(0);
1690 Register DefReg = Def.getReg();
1692 if (DefReg.isPhysical() || !TRI->isVGPR(*MRI, DefReg))
1695 SmallVector<const MachineInstr*, 8> Users;
1696 SmallVector<Register, 8> MoveRegs;
1697 for (const MachineInstr &I : MRI->use_nodbg_instructions(DefReg))
1698 Users.push_back(&I);
1703 // Check that all uses a copy to an agpr or a reg_sequence producing an agpr.
1704 while (!Users.empty()) {
1705 const MachineInstr *I = Users.pop_back_val();
1706 if (!I->isCopy() && !I->isRegSequence())
1708 Register DstReg = I->getOperand(0).getReg();
1709 // Physical registers may have more than one instruction definitions
1710 if (DstReg.isPhysical())
1712 if (TRI->isAGPR(*MRI, DstReg))
1714 MoveRegs.push_back(DstReg);
1715 for (const MachineInstr &U : MRI->use_nodbg_instructions(DstReg))
1716 Users.push_back(&U);
1719 const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
1720 MRI->setRegClass(DefReg, TRI->getEquivalentAGPRClass(RC));
1721 if (!TII->isOperandLegal(MI, 0, &Def)) {
1722 MRI->setRegClass(DefReg, RC);
1726 while (!MoveRegs.empty()) {
1727 Register Reg = MoveRegs.pop_back_val();
1728 MRI->setRegClass(Reg, TRI->getEquivalentAGPRClass(MRI->getRegClass(Reg)));
1731 LLVM_DEBUG(dbgs() << "Folded " << MI);
1736 bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
1737 if (skipFunction(MF.getFunction()))
1740 MRI = &MF.getRegInfo();
1741 ST = &MF.getSubtarget<GCNSubtarget>();
1742 TII = ST->getInstrInfo();
1743 TRI = &TII->getRegisterInfo();
1744 MFI = MF.getInfo<SIMachineFunctionInfo>();
1746 // omod is ignored by hardware if IEEE bit is enabled. omod also does not
1747 // correctly handle signed zeros.
1749 // FIXME: Also need to check strictfp
1750 bool IsIEEEMode = MFI->getMode().IEEE;
1751 bool HasNSZ = MFI->hasNoSignedZerosFPMath();
1753 bool Changed = false;
1754 for (MachineBasicBlock *MBB : depth_first(&MF)) {
1755 MachineOperand *CurrentKnownM0Val = nullptr;
1756 for (auto &MI : make_early_inc_range(*MBB)) {
1757 Changed |= tryFoldCndMask(MI);
1759 if (tryFoldZeroHighBits(MI)) {
1764 if (MI.isRegSequence() && tryFoldRegSequence(MI)) {
1769 if (MI.isPHI() && tryFoldLCSSAPhi(MI)) {
1774 if (MI.mayLoad() && tryFoldLoad(MI)) {
1779 if (TII->isFoldableCopy(MI)) {
1780 Changed |= tryFoldFoldableCopy(MI, CurrentKnownM0Val);
1784 // Saw an unknown clobber of m0, so we no longer know what it is.
1785 if (CurrentKnownM0Val && MI.modifiesRegister(AMDGPU::M0, TRI))
1786 CurrentKnownM0Val = nullptr;
1788 // TODO: Omod might be OK if there is NSZ only on the source
1789 // instruction, and not the omod multiply.
1790 if (IsIEEEMode || (!HasNSZ && !MI.getFlag(MachineInstr::FmNsz)) ||
1792 Changed |= tryFoldClamp(MI);