25#define DEBUG_TYPE "si-fold-operands"
46 unsigned DefSubReg = AMDGPU::NoSubRegister;
51 FoldableDef() =
delete;
53 unsigned DefSubReg = AMDGPU::NoSubRegister)
54 : DefRC(DefRC), DefSubReg(DefSubReg), Kind(FoldOp.
getType()) {
57 ImmToFold = FoldOp.
getImm();
58 }
else if (FoldOp.
isFI()) {
59 FrameIndexToFold = FoldOp.
getIndex();
69 unsigned DefSubReg = AMDGPU::NoSubRegister)
70 : ImmToFold(FoldImm), DefRC(DefRC), DefSubReg(DefSubReg),
75 FoldableDef Copy(*
this);
76 Copy.DefSubReg =
TRI.composeSubRegIndices(DefSubReg, SubReg);
84 return OpToFold->getReg();
87 unsigned getSubReg()
const {
89 return OpToFold->getSubReg();
100 return FrameIndexToFold;
108 std::optional<int64_t> getEffectiveImmVal()
const {
116 unsigned OpIdx)
const {
119 std::optional<int64_t> ImmToFold = getEffectiveImmVal();
123 if (!
TII.isImmEquivalentToReg(
MI, OpIdx, *ImmToFold))
129 return TII.isOperandLegal(
MI, OpIdx, &TmpOp);
132 if (DefSubReg != AMDGPU::NoSubRegister)
135 return TII.isOperandLegal(
MI, OpIdx, &TmpOp);
140 if (DefSubReg != AMDGPU::NoSubRegister)
142 return TII.isOperandLegal(
MI, OpIdx, OpToFold);
149struct FoldCandidate {
157 bool Commuted =
false,
int ShrinkOp = -1)
158 :
UseMI(
MI), Def(Def), ShrinkOpcode(ShrinkOp), UseOpNo(OpNo),
159 Commuted(Commuted) {}
161 bool isFI()
const {
return Def.isFI(); }
165 return Def.FrameIndexToFold;
168 bool isImm()
const {
return Def.isImm(); }
170 bool isReg()
const {
return Def.isReg(); }
174 bool isGlobal()
const {
return Def.isGlobal(); }
176 bool needsShrink()
const {
return ShrinkOpcode != -1; }
179class SIFoldOperandsImpl {
190 const FoldableDef &OpToFold)
const;
193 unsigned convertToVALUOp(
unsigned Opc,
bool UseVOP3 =
false)
const {
195 case AMDGPU::S_ADD_I32: {
196 if (ST->hasAddNoCarryInsts())
197 return UseVOP3 ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_U32_e32;
198 return UseVOP3 ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_ADD_CO_U32_e32;
200 case AMDGPU::S_OR_B32:
201 return UseVOP3 ? AMDGPU::V_OR_B32_e64 : AMDGPU::V_OR_B32_e32;
202 case AMDGPU::S_AND_B32:
203 return UseVOP3 ? AMDGPU::V_AND_B32_e64 : AMDGPU::V_AND_B32_e32;
204 case AMDGPU::S_MUL_I32:
205 return AMDGPU::V_MUL_LO_U32_e64;
207 return AMDGPU::INSTRUCTION_LIST_END;
211 bool foldCopyToVGPROfScalarAddOfFrameIndex(
Register DstReg,
Register SrcReg,
217 int64_t ImmVal)
const;
220 bool tryFoldInlineWithOpSel(
MachineInstr &
MI,
unsigned OpNo,
unsigned ModIdx,
221 int64_t NewImm, int64_t
Value)
const;
225 bool tryFoldShuffledHalves(
MachineInstr &
MI,
unsigned OpNo,
unsigned ModIdx,
229 bool tryFoldImmWithOpSel(
MachineInstr *
MI,
int OpNo, int64_t ImmVal)
const;
233 const FoldableDef &OpToFold)
const;
236 bool isTemporallyDivergentUse(
const FoldableDef &OpToFold,
244 getRegSeqInit(
SmallVectorImpl<std::pair<MachineOperand *, unsigned>> &Defs,
247 std::pair<int64_t, const TargetRegisterClass *>
261 struct ANDMaskResult {
266 std::optional<ANDMaskResult> getANDMaskRegOperand(
MachineInstr &AndMI)
const;
273 bool foldInstOperand(
MachineInstr &
MI,
const FoldableDef &OpToFold)
const;
275 bool foldCopyToAGPRRegSequence(
MachineInstr *CopyMI)
const;
282 std::pair<const MachineOperand *, int> isOMod(
const MachineInstr &
MI)
const;
292 SIFoldOperandsImpl() =
default;
307 &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
308 return SIFoldOperandsImpl().run(MF, MLI);
311 StringRef getPassName()
const override {
return "SI Fold Operands"; }
333char SIFoldOperandsLegacy::ID = 0;
342 TRI.getSubRegisterClass(RC, MO.getSubReg()))
350 case AMDGPU::V_MAC_F32_e64:
351 return AMDGPU::V_MAD_F32_e64;
352 case AMDGPU::V_MAC_F16_e64:
353 return AMDGPU::V_MAD_F16_e64;
354 case AMDGPU::V_FMAC_F32_e64:
355 return AMDGPU::V_FMA_F32_e64;
356 case AMDGPU::V_FMAC_F16_e64:
357 return AMDGPU::V_FMA_F16_gfx9_e64;
358 case AMDGPU::V_FMAC_F16_t16_e64:
359 return AMDGPU::V_FMA_F16_gfx9_t16_e64;
360 case AMDGPU::V_FMAC_F16_fake16_e64:
361 return AMDGPU::V_FMA_F16_gfx9_fake16_e64;
362 case AMDGPU::V_FMAC_LEGACY_F32_e64:
363 return AMDGPU::V_FMA_LEGACY_F32_e64;
364 case AMDGPU::V_FMAC_F64_e64:
365 return AMDGPU::V_FMA_F64_e64;
367 return AMDGPU::INSTRUCTION_LIST_END;
373 const FoldableDef &OpToFold)
const {
374 if (!OpToFold.isFI())
377 const unsigned Opc =
UseMI.getOpcode();
379 case AMDGPU::S_ADD_I32:
380 case AMDGPU::S_ADD_U32:
381 case AMDGPU::V_ADD_U32_e32:
382 case AMDGPU::V_ADD_CO_U32_e32:
386 return UseMI.getOperand(OpNo == 1 ? 2 : 1).isImm() &&
388 case AMDGPU::V_ADD_U32_e64:
389 case AMDGPU::V_ADD_CO_U32_e64:
390 return UseMI.getOperand(OpNo == 2 ? 3 : 2).isImm() &&
397 return OpNo == AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::vaddr);
401 int SIdx = AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::saddr);
405 int VIdx = AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::vaddr);
406 return OpNo == VIdx && SIdx == -1;
412bool SIFoldOperandsImpl::foldCopyToVGPROfScalarAddOfFrameIndex(
417 if (
TRI->isVGPR(*
MRI, DstReg) &&
TRI->isSGPRReg(*
MRI, SrcReg) &&
420 if (!Def ||
Def->getNumOperands() != 4)
423 MachineOperand *
Src0 = &
Def->getOperand(1);
424 MachineOperand *
Src1 = &
Def->getOperand(2);
435 const bool UseVOP3 = !
Src0->isImm() ||
TII->isInlineConstant(*Src0);
436 unsigned NewOp = convertToVALUOp(
Def->getOpcode(), UseVOP3);
437 if (NewOp == AMDGPU::INSTRUCTION_LIST_END ||
438 !
Def->getOperand(3).isDead())
441 MachineBasicBlock *
MBB =
Def->getParent();
443 if (NewOp != AMDGPU::V_ADD_CO_U32_e32) {
444 MachineInstrBuilder
Add =
447 if (
Add->getDesc().getNumDefs() == 2) {
449 Add.addDef(CarryOutReg, RegState::Dead);
453 Add.add(*Src0).add(*Src1).setMIFlags(
Def->getFlags());
457 Def->eraseFromParent();
458 MI.eraseFromParent();
462 assert(NewOp == AMDGPU::V_ADD_CO_U32_e32);
473 Def->eraseFromParent();
474 MI.eraseFromParent();
482bool SIFoldOperandsImpl::canUseImmWithOpSel(
const MachineInstr *
MI,
484 int64_t ImmVal)
const {
490 if (!
TII->getNumOpSelBits(*
MI, UseOpNo))
493 unsigned Opcode =
MI->getOpcode();
494 uint8_t OpType =
TII->get(Opcode).operands()[UseOpNo].OperandType;
498 int16_t Half =
TII->getSrcOperandValue(*
MI, UseOpNo, ImmVal);
518 static_cast<uint16_t
>(ImmVal) !=
static_cast<uint16_t
>(ImmVal >> 16))
526bool SIFoldOperandsImpl::tryFoldInlineWithOpSel(MachineInstr &
MI,
unsigned OpNo,
527 unsigned ModIdx, int64_t NewImm,
528 int64_t
Value)
const {
529 uint8_t OpType =
MI.getDesc().operands()[OpNo].OperandType;
530 if (!
TII->isInlineConstant(NewImm, OpType))
533 MachineOperand &
Mod =
MI.getOperand(ModIdx);
534 std::optional<unsigned> NewModVal =
535 TII->getImmOpSelForValue(
MI, OpNo, NewImm,
Value,
Mod.getImm());
537 !
TII->isImmOperandLegalWithMods(
MI, OpNo, NewImm, *NewModVal))
540 Mod.setImm(*NewModVal);
541 MI.getOperand(OpNo).ChangeToImmediate(NewImm);
545bool SIFoldOperandsImpl::tryFoldShuffledHalves(MachineInstr &
MI,
unsigned OpNo,
548 uint16_t
Lo =
static_cast<uint16_t
>(
Imm);
549 uint16_t
Hi =
static_cast<uint16_t
>(
Imm >> 16);
550 if (tryFoldInlineWithOpSel(
MI, OpNo, ModIdx,
Imm,
Imm))
555 return tryFoldInlineWithOpSel(
MI, OpNo, ModIdx, Swapped,
Imm);
558 if (tryFoldInlineWithOpSel(
MI, OpNo, ModIdx,
Lo,
Imm))
561 if (
static_cast<int16_t
>(
Lo) < 0 &&
562 tryFoldInlineWithOpSel(
MI, OpNo, ModIdx,
static_cast<int16_t
>(
Lo),
Imm))
566 return tryFoldInlineWithOpSel(
MI, OpNo, ModIdx, HiOnly,
Imm);
569bool SIFoldOperandsImpl::tryFoldImmWithOpSel(MachineInstr *
MI,
int OpNo,
570 int64_t ImmVal)
const {
571 unsigned Opcode =
MI->getOpcode();
575 AMDGPU::OpName ModName = AMDGPU::OpName::NUM_OPERAND_NAMES;
576 unsigned SrcIdx = ~0;
577 if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0)) {
578 ModName = AMDGPU::OpName::src0_modifiers;
580 }
else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1)) {
581 ModName = AMDGPU::OpName::src1_modifiers;
583 }
else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2)) {
584 ModName = AMDGPU::OpName::src2_modifiers;
587 assert(ModName != AMDGPU::OpName::NUM_OPERAND_NAMES);
588 int ModIdx = AMDGPU::getNamedOperandIdx(Opcode, ModName);
592 unsigned ModVal =
MI->getOperand(ModIdx).getImm();
593 int64_t
Imm =
TII->getSrcOperandValue(*
MI, OpNo, ImmVal, ModVal);
597 if (tryFoldInlineWithOpSel(*
MI, OpNo, ModIdx, ImmVal,
Imm))
600 if (tryFoldShuffledHalves(*
MI, OpNo, ModIdx,
Imm))
609 bool IsUAdd = Opcode == AMDGPU::V_PK_ADD_U16;
610 bool IsUSub = Opcode == AMDGPU::V_PK_SUB_U16;
611 if (SrcIdx == 1 && (IsUAdd || IsUSub)) {
613 AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::clamp);
614 bool Clamp =
MI->getOperand(ClampIdx).getImm() != 0;
617 uint16_t NegLo = -
static_cast<uint16_t
>(
Imm);
618 uint16_t NegHi = -
static_cast<uint16_t
>(
Imm >> 16);
622 if (tryFoldShuffledHalves(*
MI, OpNo, ModIdx, NegImm)) {
624 IsUAdd ? AMDGPU::V_PK_SUB_U16 : AMDGPU::V_PK_ADD_U16;
625 MI->setDesc(
TII->get(NegOpcode));
634bool SIFoldOperandsImpl::updateOperand(FoldCandidate &Fold)
const {
635 MachineInstr *
MI = Fold.UseMI;
639 std::optional<int64_t> ImmVal;
641 ImmVal = Fold.Def.getEffectiveImmVal();
643 if (ImmVal && canUseImmWithOpSel(Fold.UseMI, Fold.UseOpNo, *ImmVal)) {
644 if (tryFoldImmWithOpSel(Fold.UseMI, Fold.UseOpNo, *ImmVal))
649 if (!Fold.Def.isOperandLegal(*
TII, *
MI, Fold.UseOpNo))
656 if ((Fold.isImm() || Fold.isFI() || Fold.isGlobal()) && Fold.needsShrink()) {
664 int Op32 = Fold.ShrinkOpcode;
665 MachineOperand &Dst0 =
MI->getOperand(0);
666 MachineOperand &Dst1 =
MI->getOperand(1);
674 MachineInstr *Inst32 =
TII->buildShrunkInst(*
MI, Op32);
676 if (HaveNonDbgCarryUse) {
679 .
addReg(AMDGPU::VCC, RegState::Kill);
693 for (
unsigned I =
MI->getNumOperands() - 1;
I > 0; --
I)
694 MI->removeOperand(
I);
695 MI->setDesc(
TII->get(AMDGPU::IMPLICIT_DEF));
698 TII->commuteInstruction(*Inst32,
false);
702 assert(!Fold.needsShrink() &&
"not handled");
707 if (NewMFMAOpc == -1)
709 MI->setDesc(
TII->get(NewMFMAOpc));
710 MI->untieRegOperand(0);
711 const MCInstrDesc &MCID =
MI->getDesc();
712 for (
unsigned I = 0;
I <
MI->getNumDefs(); ++
I)
714 MI->getOperand(
I).setIsEarlyClobber(
true);
719 int OpNo =
MI->getOperandNo(&Old);
720 if (!
TII->isOperandLegal(*
MI, OpNo, &New))
727 if (Fold.isGlobal()) {
728 Old.
ChangeToGA(Fold.Def.OpToFold->getGlobal(),
729 Fold.Def.OpToFold->getOffset(),
730 Fold.Def.OpToFold->getTargetFlags());
739 MachineOperand *
New = Fold.Def.OpToFold;
743 TII->getRegClass(
MI->getDesc(), Fold.UseOpNo)) {
745 TRI->getRegClassForReg(*
MRI,
New->getReg());
748 if (
New->getSubReg()) {
750 TRI->getMatchingSuperRegClass(NewRC, OpRC,
New->getSubReg());
756 if (
New->getReg().isVirtual() &&
759 <<
TRI->getRegClassName(ConstrainRC) <<
'\n');
768 if (
New->getReg().isPhysical()) {
776 if (
MI->isBundledWithPred()) {
778 for (MachineOperand &MO : Header.operands()) {
779 if (MO.getReg() == OldReg) {
780 MO.setReg(
New->getReg());
781 MO.setSubReg(
New->getSubReg());
790 FoldCandidate &&Entry) {
792 for (FoldCandidate &Fold : FoldList)
793 if (Fold.UseMI == Entry.UseMI && Fold.UseOpNo == Entry.UseOpNo)
795 LLVM_DEBUG(
dbgs() <<
"Append " << (Entry.Commuted ?
"commuted" :
"normal")
796 <<
" operand " << Entry.UseOpNo <<
"\n " << *Entry.UseMI);
802 const FoldableDef &FoldOp,
803 bool Commuted =
false,
int ShrinkOp = -1) {
805 FoldCandidate(
MI, OpNo, FoldOp, Commuted, ShrinkOp));
808bool SIFoldOperandsImpl::tryAddToFoldList(
809 SmallVectorImpl<FoldCandidate> &FoldList, MachineInstr *
MI,
unsigned OpNo,
810 const FoldableDef &OpToFold)
const {
811 const unsigned Opc =
MI->getOpcode();
813 auto tryToFoldAsFMAAKorMK = [&]() {
814 if (!OpToFold.isImm())
817 const bool TryAK = OpNo == 3;
818 const unsigned NewOpc = TryAK ? AMDGPU::S_FMAAK_F32 : AMDGPU::S_FMAMK_F32;
819 MI->setDesc(
TII->get(NewOpc));
822 bool FoldAsFMAAKorMK =
823 tryAddToFoldList(FoldList,
MI, TryAK ? 3 : 2, OpToFold);
824 if (FoldAsFMAAKorMK) {
826 MI->untieRegOperand(3);
829 MachineOperand &Op1 =
MI->getOperand(1);
830 MachineOperand &Op2 =
MI->getOperand(2);
847 bool IsLegal = OpToFold.isOperandLegal(*
TII, *
MI, OpNo);
848 if (!IsLegal && OpToFold.isImm()) {
849 if (std::optional<int64_t> ImmVal = OpToFold.getEffectiveImmVal())
850 IsLegal = canUseImmWithOpSel(
MI, OpNo, *ImmVal);
856 if (NewOpc != AMDGPU::INSTRUCTION_LIST_END) {
859 MI->setDesc(
TII->get(NewOpc));
864 bool FoldAsMAD = tryAddToFoldList(FoldList,
MI, OpNo, OpToFold);
866 MI->untieRegOperand(OpNo);
870 MI->removeOperand(
MI->getNumExplicitOperands() - 1);
876 if (
Opc == AMDGPU::S_FMAC_F32 && OpNo == 3) {
877 if (tryToFoldAsFMAAKorMK())
883 if ((
Opc == AMDGPU::S_FMAAK_F32 ||
Opc == AMDGPU::S_FMAMK_F32) &&
885 std::optional<int64_t> ImmVal = OpToFold.getEffectiveImmVal();
886 if (ImmVal && !
TII->isInlineConstant(*
MI, OpNo, *ImmVal)) {
887 unsigned ImmIdx =
Opc == AMDGPU::S_FMAAK_F32 ? 3 : 2;
888 MachineOperand &OpImm =
MI->getOperand(ImmIdx);
889 if (!OpImm.
isReg() &&
890 TII->isInlineConstant(*
MI,
MI->getOperand(OpNo), OpImm))
891 return tryToFoldAsFMAAKorMK();
896 if (OpToFold.isImm()) {
898 if (
Opc == AMDGPU::S_SETREG_B32)
899 ImmOpc = AMDGPU::S_SETREG_IMM32_B32;
900 else if (
Opc == AMDGPU::S_SETREG_B32_mode)
901 ImmOpc = AMDGPU::S_SETREG_IMM32_B32_mode;
903 MI->setDesc(
TII->get(ImmOpc));
912 bool CanCommute =
TII->findCommutedOpIndices(*
MI, OpNo, CommuteOpNo);
916 MachineOperand &
Op =
MI->getOperand(OpNo);
917 MachineOperand &CommutedOp =
MI->getOperand(CommuteOpNo);
923 if (!
Op.isReg() || !CommutedOp.
isReg())
928 if (
Op.isReg() && CommutedOp.
isReg() &&
929 (
Op.getReg() == CommutedOp.
getReg() &&
933 if (!
TII->commuteInstruction(*
MI,
false, OpNo, CommuteOpNo))
937 if (!OpToFold.isOperandLegal(*
TII, *
MI, CommuteOpNo)) {
938 if ((
Opc != AMDGPU::V_ADD_CO_U32_e64 &&
Opc != AMDGPU::V_SUB_CO_U32_e64 &&
939 Opc != AMDGPU::V_SUBREV_CO_U32_e64) ||
940 (!OpToFold.isImm() && !OpToFold.isFI() && !OpToFold.isGlobal())) {
941 TII->commuteInstruction(*
MI,
false, OpNo, CommuteOpNo);
947 MachineOperand &OtherOp =
MI->getOperand(OpNo);
948 if (!OtherOp.
isReg() ||
955 unsigned MaybeCommutedOpc =
MI->getOpcode();
969 if (
Opc == AMDGPU::S_FMAC_F32 &&
970 (OpNo != 1 || !
MI->getOperand(1).isIdenticalTo(
MI->getOperand(2)))) {
971 if (tryToFoldAsFMAAKorMK())
979bool SIFoldOperandsImpl::isUseSafeToFold(
const MachineInstr &
MI,
980 const MachineOperand &UseMO)
const {
982 return !
TII->isSDWA(
MI);
989 if (
MI.modifiesRegister(
TRI.getExec(), &
TRI))
997bool SIFoldOperandsImpl::isTemporallyDivergentUse(
998 const FoldableDef &OpToFold,
const MachineInstr &
UseMI)
const {
999 if (!OpToFold.isReg())
1001 const MachineInstr *
DefMI = OpToFold.DefMI;
1004 !
TRI->isSGPRReg(*
MRI, OpToFold.getReg()))
1016 SubDef &&
TII.isFoldableCopy(*SubDef);
1017 SubDef =
MRI.getVRegDef(
Sub->getReg())) {
1018 unsigned SrcIdx =
TII.getFoldableCopySrcIdx(*SubDef);
1027 if (
SrcOp.getSubReg())
1035 MachineInstr &RegSeq,
1036 SmallVectorImpl<std::pair<MachineOperand *, unsigned>> &Defs)
const {
1052 else if (!
TRI->getCommonSubClass(RC, OpRC))
1057 Defs.emplace_back(&SrcOp, SubRegIdx);
1062 if (DefSrc && (DefSrc->
isReg() || DefSrc->
isImm())) {
1063 Defs.emplace_back(DefSrc, SubRegIdx);
1067 Defs.emplace_back(&SrcOp, SubRegIdx);
1077 SmallVectorImpl<std::pair<MachineOperand *, unsigned>> &Defs,
1080 if (!Def || !
Def->isRegSequence())
1083 return getRegSeqInit(*Def, Defs);
1086std::pair<int64_t, const TargetRegisterClass *>
1087SIFoldOperandsImpl::isRegSeqSplat(MachineInstr &RegSeq)
const {
1093 bool TryToMatchSplat64 =
false;
1095 std::optional<int64_t>
Imm;
1096 for (
unsigned I = 0,
E = Defs.
size();
I !=
E; ++
I) {
1097 const MachineOperand *
Op = Defs[
I].first;
1101 if (!Def ||
Def->isImplicitDef())
1107 int64_t SubImm =
Op->getImm();
1113 if (
Imm != SubImm) {
1114 if (
I == 1 && (
E & 1) == 0) {
1117 TryToMatchSplat64 =
true;
1125 if (!TryToMatchSplat64) {
1127 return {*
Imm, SrcRC};
1134 for (
unsigned I = 0,
E = Defs.
size();
I !=
E;
I += 2) {
1135 const MachineOperand *Op0 = Defs[
I].first;
1136 const MachineOperand *Op1 = Defs[
I + 1].first;
1141 unsigned SubReg0 = Defs[
I].second;
1142 unsigned SubReg1 = Defs[
I + 1].second;
1146 if (
TRI->getChannelFromSubReg(SubReg0) + 1 !=
1147 TRI->getChannelFromSubReg(SubReg1))
1150 if (
TRI->getSubRegIdxSize(SubReg0) != 32)
1155 SplatVal64 = MergedVal;
1156 else if (SplatVal64 != MergedVal)
1163 return {SplatVal64, RC64};
1166bool SIFoldOperandsImpl::tryFoldRegSeqSplat(
1167 MachineInstr *
UseMI,
unsigned UseOpIdx, int64_t SplatVal,
1170 if (UseOpIdx >=
Desc.getNumOperands())
1177 int16_t RCID =
TII->getOpRegClassID(
Desc.operands()[UseOpIdx]);
1186 if (SplatVal != 0 && SplatVal != -1) {
1190 uint8_t OpTy =
Desc.operands()[UseOpIdx].OperandType;
1197 OpRC =
TRI->getSubRegisterClass(OpRC, AMDGPU::sub0);
1204 OpRC =
TRI->getSubRegisterClass(OpRC, AMDGPU::sub0_sub1);
1210 if (!
TRI->getCommonSubClass(OpRC, SplatRC))
1215 if (!
TII->isOperandLegal(*
UseMI, UseOpIdx, &TmpOp))
1221bool SIFoldOperandsImpl::tryToFoldACImm(
1222 const FoldableDef &OpToFold, MachineInstr *
UseMI,
unsigned UseOpIdx,
1223 SmallVectorImpl<FoldCandidate> &FoldList)
const {
1225 if (UseOpIdx >=
Desc.getNumOperands())
1232 if (OpToFold.isImm() && OpToFold.isOperandLegal(*
TII, *
UseMI, UseOpIdx)) {
1240bool SIFoldOperandsImpl::foldOperand(
1241 FoldableDef OpToFold, MachineInstr *
UseMI,
int UseOpIdx,
1242 SmallVectorImpl<FoldCandidate> &FoldList,
1243 SmallVectorImpl<MachineInstr *> &CopiesToReplace)
const {
1247 if (!isUseSafeToFold(*
UseMI, *UseOp))
1250 if (isTemporallyDivergentUse(OpToFold, *
UseMI))
1254 if (UseOp->
isReg() && OpToFold.isReg()) {
1258 if (UseOp->
getSubReg() != AMDGPU::NoSubRegister &&
1260 !
TRI->isSGPRReg(*
MRI, OpToFold.getReg())))
1273 std::tie(SplatVal, SplatRC) = isRegSeqSplat(*
UseMI);
1278 for (
unsigned I = 0;
I != UsesToProcess.size(); ++
I) {
1279 MachineOperand *RSUse = UsesToProcess[
I];
1280 MachineInstr *RSUseMI = RSUse->
getParent();
1290 if (tryFoldRegSeqSplat(RSUseMI, OpNo, SplatVal, SplatRC)) {
1291 FoldableDef SplatDef(SplatVal, SplatRC);
1299 if (RSUse->
getSubReg() != RegSeqDstSubReg)
1305 FoldList, CopiesToReplace);
1311 if (tryToFoldACImm(OpToFold,
UseMI, UseOpIdx, FoldList))
1314 if (frameIndexMayFold(*
UseMI, UseOpIdx, OpToFold)) {
1319 if (
TII->getNamedOperand(*
UseMI, AMDGPU::OpName::srsrc)->getReg() !=
1325 MachineOperand &SOff =
1326 *
TII->getNamedOperand(*
UseMI, AMDGPU::OpName::soffset);
1337 TII->getNamedOperand(*
UseMI, AMDGPU::OpName::cpol)->getImm();
1352 bool FoldingImmLike =
1353 OpToFold.isImm() || OpToFold.isFI() || OpToFold.isGlobal();
1372 for (
unsigned MovOp :
1373 {AMDGPU::S_MOV_B32, AMDGPU::V_MOV_B32_e32, AMDGPU::S_MOV_B64,
1374 AMDGPU::V_MOV_B64_PSEUDO, AMDGPU::V_MOV_B16_t16_e64,
1375 AMDGPU::V_ACCVGPR_WRITE_B32_e64, AMDGPU::AV_MOV_B32_IMM_PSEUDO,
1376 AMDGPU::AV_MOV_B64_IMM_PSEUDO}) {
1377 const MCInstrDesc &MovDesc =
TII->get(MovOp);
1387 const int SrcIdx = MovOp == AMDGPU::V_MOV_B16_t16_e64 ? 2 : 1;
1389 int16_t RegClassID =
TII->getOpRegClassID(MovDesc.
operands()[SrcIdx]);
1390 if (RegClassID != -1) {
1394 MovSrcRC =
TRI->getMatchingSuperRegClass(SrcRC, MovSrcRC, UseSubReg);
1398 if (MovOp == AMDGPU::AV_MOV_B32_IMM_PSEUDO &&
1399 (!OpToFold.isImm() ||
1400 !
TII->isImmOperandLegal(MovDesc, SrcIdx,
1401 *OpToFold.getEffectiveImmVal())))
1414 if (!OpToFold.isImm() ||
1415 !
TII->isImmOperandLegal(MovDesc, 1, *OpToFold.getEffectiveImmVal()))
1421 while (ImpOpI != ImpOpE) {
1428 if (MovOp == AMDGPU::V_MOV_B16_t16_e64) {
1430 MachineOperand NewSrcOp(SrcOp);
1452 LLVM_DEBUG(
dbgs() <<
"Folding " << *OpToFold.OpToFold <<
"\n into "
1457 unsigned SubRegIdx = OpToFold.getSubReg();
1462 TRI->isSGPRReg(*
MRI,
UseReg) && SubRegIdx != AMDGPU::NoSubRegister) {
1465 unsigned Channel =
TRI->getChannelFromSubReg(SubRegIdx);
1467 SubRegIdx =
TRI->getRegSizeInBits(*UseRC) == 32
1468 ? AMDGPU::NoSubRegister
1474 OpToFold.OpToFold->setIsKill(
false);
1477 if (foldCopyToAGPRRegSequence(
UseMI))
1482 if (UseOpc == AMDGPU::V_READFIRSTLANE_B32 ||
1483 (UseOpc == AMDGPU::V_READLANE_B32 &&
1485 AMDGPU::getNamedOperandIdx(UseOpc, AMDGPU::OpName::src0))) {
1490 if (FoldingImmLike) {
1493 *OpToFold.DefMI, *
UseMI))
1499 if (OpToFold.isImm()) {
1501 *OpToFold.getEffectiveImmVal());
1502 }
else if (OpToFold.isFI())
1505 assert(OpToFold.isGlobal());
1507 OpToFold.OpToFold->getOffset(),
1508 OpToFold.OpToFold->getTargetFlags());
1514 if (OpToFold.isReg() &&
TRI->isSGPRReg(*
MRI, OpToFold.getReg())) {
1517 *OpToFold.DefMI, *
UseMI))
1539 UseDesc.
operands()[UseOpIdx].RegClass == -1)
1547 Changed |= tryAddToFoldList(FoldList,
UseMI, UseOpIdx, OpToFold);
1554 case AMDGPU::S_ADD_I32:
1555 case AMDGPU::S_ADD_U32:
1558 case AMDGPU::S_SUB_I32:
1559 case AMDGPU::S_SUB_U32:
1562 case AMDGPU::V_AND_B32_e64:
1563 case AMDGPU::V_AND_B32_e32:
1564 case AMDGPU::S_AND_B32:
1567 case AMDGPU::V_OR_B32_e64:
1568 case AMDGPU::V_OR_B32_e32:
1569 case AMDGPU::S_OR_B32:
1572 case AMDGPU::V_XOR_B32_e64:
1573 case AMDGPU::V_XOR_B32_e32:
1574 case AMDGPU::S_XOR_B32:
1577 case AMDGPU::S_XNOR_B32:
1580 case AMDGPU::S_NAND_B32:
1583 case AMDGPU::S_NOR_B32:
1586 case AMDGPU::S_ANDN2_B32:
1589 case AMDGPU::S_ORN2_B32:
1592 case AMDGPU::V_LSHL_B32_e64:
1593 case AMDGPU::V_LSHL_B32_e32:
1594 case AMDGPU::S_LSHL_B32:
1596 Result =
LHS << (
RHS & 31);
1598 case AMDGPU::V_LSHLREV_B32_e64:
1599 case AMDGPU::V_LSHLREV_B32_e32:
1600 Result =
RHS << (
LHS & 31);
1602 case AMDGPU::V_LSHR_B32_e64:
1603 case AMDGPU::V_LSHR_B32_e32:
1604 case AMDGPU::S_LSHR_B32:
1605 Result =
LHS >> (
RHS & 31);
1607 case AMDGPU::V_LSHRREV_B32_e64:
1608 case AMDGPU::V_LSHRREV_B32_e32:
1609 Result =
RHS >> (
LHS & 31);
1611 case AMDGPU::V_ASHR_I32_e64:
1612 case AMDGPU::V_ASHR_I32_e32:
1613 case AMDGPU::S_ASHR_I32:
1614 Result =
static_cast<int32_t
>(
LHS) >> (
RHS & 31);
1616 case AMDGPU::V_ASHRREV_I32_e64:
1617 case AMDGPU::V_ASHRREV_I32_e32:
1618 Result =
static_cast<int32_t
>(
RHS) >> (
LHS & 31);
1626 return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
1632bool SIFoldOperandsImpl::tryConstantFoldOp(MachineInstr *
MI)
const {
1633 if (!
MI->allImplicitDefsAreDead())
1636 unsigned Opc =
MI->getOpcode();
1639 if (
Opc == AMDGPU::V_LSHL_ADD_U32_e64) {
1640 int DataIdx = AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src0);
1641 int AddIdx = AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src2);
1642 std::optional<int64_t> Addend =
1643 TII->getImmOrMaterializedImm(*
MRI,
MI->getOperand(AddIdx));
1645 if (Addend &&
static_cast<uint32_t
>(*Addend) == 0) {
1646 MachineOperand
Data =
MI->getOperand(DataIdx);
1647 MI->removeOperand(AddIdx);
1648 MI->removeOperand(DataIdx);
1649 MI->addOperand(*MF,
Data);
1650 TII->mutateAndCleanupImplicit(*
MI,
TII->get(AMDGPU::V_LSHLREV_B32_e64));
1655 int Src0Idx = AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src0);
1659 MachineOperand *
Src0 = &
MI->getOperand(Src0Idx);
1660 std::optional<int64_t> Src0Imm =
TII->getImmOrMaterializedImm(*
MRI, *Src0);
1662 if ((
Opc == AMDGPU::V_NOT_B32_e64 ||
Opc == AMDGPU::V_NOT_B32_e32 ||
1663 Opc == AMDGPU::S_NOT_B32) &&
1665 MI->getOperand(1).ChangeToImmediate(~*Src0Imm);
1666 TII->mutateAndCleanupImplicit(
1671 int Src1Idx = AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src1);
1675 MachineOperand *
Src1 = &
MI->getOperand(Src1Idx);
1676 std::optional<int64_t> Src1Imm =
TII->getImmOrMaterializedImm(*
MRI, *Src1);
1678 if (!Src0Imm && !Src1Imm)
1684 if (Src0Imm && Src1Imm) {
1689 bool IsSGPR =
TRI->isSGPRReg(*
MRI,
MI->getOperand(0).getReg());
1693 MI->getOperand(Src0Idx).ChangeToImmediate(NewImm);
1694 MI->removeOperand(Src1Idx);
1701 if (
Opc == AMDGPU::S_SUB_I32 ||
Opc == AMDGPU::S_SUB_U32) {
1702 if (Src1Imm &&
static_cast<int32_t
>(*Src1Imm) == 0) {
1704 MI->removeOperand(Src1Idx);
1705 TII->mutateAndCleanupImplicit(*
MI,
TII->get(AMDGPU::COPY));
1711 if (!
MI->isCommutable())
1714 if (Src0Imm && !Src1Imm) {
1720 int32_t Src1Val =
static_cast<int32_t
>(*Src1Imm);
1721 if (
Opc == AMDGPU::S_ADD_I32 ||
Opc == AMDGPU::S_ADD_U32) {
1724 MI->removeOperand(Src1Idx);
1725 TII->mutateAndCleanupImplicit(*
MI,
TII->get(AMDGPU::COPY));
1731 if (
Opc == AMDGPU::V_OR_B32_e64 ||
1732 Opc == AMDGPU::V_OR_B32_e32 ||
1733 Opc == AMDGPU::S_OR_B32) {
1736 MI->removeOperand(Src1Idx);
1737 TII->mutateAndCleanupImplicit(*
MI,
TII->get(AMDGPU::COPY));
1738 }
else if (Src1Val == -1) {
1740 MI->removeOperand(Src0Idx);
1741 TII->mutateAndCleanupImplicit(
1749 if (
Opc == AMDGPU::V_AND_B32_e64 ||
Opc == AMDGPU::V_AND_B32_e32 ||
1750 Opc == AMDGPU::S_AND_B32 ||
Opc == AMDGPU::S_AND_B64) {
1751 int64_t
C =
Opc == AMDGPU::S_AND_B64 ? *Src1Imm : Src1Val;
1754 MI->removeOperand(Src0Idx);
1755 TII->mutateAndCleanupImplicit(
1756 *
MI,
TII->get(
TII->getMovOpcode(
TII->getOpRegClass(*
MI, 0))));
1757 }
else if (
C == -1) {
1759 MI->removeOperand(Src1Idx);
1760 TII->mutateAndCleanupImplicit(*
MI,
TII->get(AMDGPU::COPY));
1767 if (
Opc == AMDGPU::V_XOR_B32_e64 ||
Opc == AMDGPU::V_XOR_B32_e32 ||
1768 Opc == AMDGPU::S_XOR_B32) {
1771 MI->removeOperand(Src1Idx);
1772 TII->mutateAndCleanupImplicit(*
MI,
TII->get(AMDGPU::COPY));
1781bool SIFoldOperandsImpl::tryFoldCndMask(MachineInstr &
MI)
const {
1782 unsigned Opc =
MI.getOpcode();
1783 if (
Opc != AMDGPU::V_CNDMASK_B32_e32 &&
Opc != AMDGPU::V_CNDMASK_B32_e64 &&
1784 Opc != AMDGPU::S_CSELECT_B32 &&
Opc != AMDGPU::S_CSELECT_B64)
1787 MachineOperand *
Src0 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src0);
1788 MachineOperand *
Src1 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src1);
1789 if (!
Src1->isIdenticalTo(*Src0)) {
1790 std::optional<int64_t> Src1Imm =
TII->getImmOrMaterializedImm(*
MRI, *Src1);
1794 std::optional<int64_t> Src0Imm =
TII->getImmOrMaterializedImm(*
MRI, *Src0);
1795 if (!Src0Imm || *Src0Imm != *Src1Imm)
1800 AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src1_modifiers);
1802 AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src0_modifiers);
1803 if ((Src1ModIdx != -1 &&
MI.getOperand(Src1ModIdx).getImm() != 0) ||
1804 (Src0ModIdx != -1 &&
MI.getOperand(Src0ModIdx).getImm() != 0))
1808 if (
Src0->isReg()) {
1809 NewOpc = AMDGPU::COPY;
1812 case AMDGPU::S_CSELECT_B32:
1813 NewOpc = AMDGPU::S_MOV_B32;
1815 case AMDGPU::S_CSELECT_B64:
1816 NewOpc = AMDGPU::S_MOV_B64;
1819 NewOpc = AMDGPU::V_MOV_B32_e32;
1825 int Src2Idx = AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src2);
1827 MI.removeOperand(Src2Idx);
1828 MI.removeOperand(AMDGPU::getNamedOperandIdx(
Opc, AMDGPU::OpName::src1));
1829 if (Src1ModIdx != -1)
1830 MI.removeOperand(Src1ModIdx);
1831 if (Src0ModIdx != -1)
1832 MI.removeOperand(Src0ModIdx);
1833 TII->mutateAndCleanupImplicit(
MI,
TII->get(NewOpc));
1840std::optional<SIFoldOperandsImpl::ANDMaskResult>
1841SIFoldOperandsImpl::getANDMaskRegOperand(MachineInstr &AndMI)
const {
1843 if (
Opc != AMDGPU::V_AND_B32_e64 &&
Opc != AMDGPU::V_AND_B32_e32 &&
1844 Opc != AMDGPU::S_AND_B32)
1845 return std::nullopt;
1847 std::optional<int64_t> MaskImm =
1856 return std::nullopt;
1862 AMDGPU::OpName::src0_modifiers, AMDGPU::OpName::src1_modifiers,
1863 AMDGPU::OpName::src2_modifiers, AMDGPU::OpName::op_sel};
1874 switch (
MI.getOpcode()) {
1875 case AMDGPU::V_MAD_U16_gfx9_e64:
1876 LegacyOpc = AMDGPU::V_MAD_U16_e64;
1878 case AMDGPU::V_MAD_I16_gfx9_e64:
1879 LegacyOpc = AMDGPU::V_MAD_I16_e64;
1886 if (
MI.getOperand(AMDGPU::getNamedOperandIdx(
MI.getOpcode(),
OpName))
1905bool SIFoldOperandsImpl::tryFoldRedundantAND(MachineInstr &ChildMI)
const {
1910 std::optional<ANDMaskResult> ChildResult = getANDMaskRegOperand(ChildMI);
1914 if (!ChildResult->Reg.isVirtual())
1921 int64_t ParentMask = 0;
1922 unsigned LegacyOpc = 0;
1923 std::optional<ANDMaskResult> ParentResult = getANDMaskRegOperand(*ParentMI);
1926 ParentMask = ParentResult->Mask;
1929 ParentMask = 0xffff;
1933 ParentMask = 0xffff;
1939 if ((ParentMask & ChildResult->Mask) != ParentMask)
1955 TII->mutateAndCleanupImplicit(*ParentMI,
TII->get(LegacyOpc));
1969bool SIFoldOperandsImpl::tryFoldAndExec(MachineInstr &
MI)
const {
1975 if (!
MI.allImplicitDefsAreDead())
1979 unsigned ExecIdx = 0;
1980 for (
unsigned I : {1u, 2u}) {
1981 const MachineOperand &MO =
MI.getOperand(
I);
1987 MachineOperand &Src =
MI.getOperand(3 - ExecIdx);
1988 if (!Src.isReg() || !Src.getReg().isVirtual() || Src.getSubReg())
1992 if (!
TII->isMaskedByExec(SrcReg,
MI, *
MRI))
2010 MI.eraseFromParent();
2017bool SIFoldOperandsImpl::tryFoldAndCSelectExec(MachineInstr &
MI)
const {
2020 if (
MI.getOpcode() != LMC.
AndOpc || !
MI.allImplicitDefsAreDead())
2023 for (
unsigned SelIdx : {1u, 2u}) {
2024 const MachineOperand &SelMO =
MI.getOperand(SelIdx);
2025 const MachineOperand &MaskMO =
MI.getOperand(3 - SelIdx);
2050 const bool IsAllOnesArm =
Arm.isImm() &&
Arm.getImm() == -1;
2051 const bool IsExecArm =
Arm.isReg() &&
Arm.getReg() == LMC.
ExecReg;
2052 if (!IsAllOnesArm &&
2053 (!IsExecArm || !
TII->isMaskedByExec(MaskReg,
MI, *
MRI)))
2063 constexpr unsigned MaxInstScan = 20;
2064 unsigned NumInst = 0;
2067 [&](
const MachineInstr &
I) {
2068 return ++NumInst > MaxInstScan || &I == MaskDef;
2084 Arm.ChangeToRegister(MaskReg,
false);
2094 MI.eraseFromParent();
2101bool SIFoldOperandsImpl::foldInstOperand(MachineInstr &
MI,
2102 const FoldableDef &OpToFold)
const {
2106 SmallVector<MachineInstr *, 4> CopiesToReplace;
2108 MachineOperand &Dst =
MI.getOperand(0);
2113 for (
auto *U : UsesToProcess) {
2114 MachineInstr *
UseMI =
U->getParent();
2116 FoldableDef SubOpToFold = OpToFold.getWithSubReg(*
TRI,
U->getSubReg());
2121 if (CopiesToReplace.
empty() && FoldList.
empty())
2125 for (MachineInstr *Copy : CopiesToReplace)
2126 Copy->addImplicitDefUseOperands(*MF);
2128 SetVector<MachineInstr *> ConstantFoldCandidates;
2129 for (FoldCandidate &Fold : FoldList) {
2130 assert(!Fold.isReg() || Fold.Def.OpToFold);
2131 if (Fold.isReg() && Fold.getReg().isVirtual()) {
2133 const MachineInstr *
DefMI = Fold.Def.DefMI;
2140 <<
static_cast<int>(Fold.UseOpNo) <<
" of "
2144 ConstantFoldCandidates.
insert(Fold.UseMI);
2146 }
else if (Fold.Commuted) {
2148 TII->commuteInstruction(*Fold.UseMI,
false);
2152 for (MachineInstr *
MI : ConstantFoldCandidates) {
2153 if (tryConstantFoldOp(
MI)) {
2163bool SIFoldOperandsImpl::foldCopyToAGPRRegSequence(MachineInstr *CopyMI)
const {
2170 if (!
TRI->isAGPRClass(DefRC))
2182 DenseMap<TargetInstrInfo::RegSubRegPair, Register> VGPRCopies;
2191 unsigned NumFoldable = 0;
2193 for (
unsigned I = 1;
I != NumRegSeqOperands;
I += 2) {
2210 DefRC, &AMDGPU::AGPR_32RegClass, SubRegIdx);
2230 TRI->getMatchingSuperRegClass(DefRC, InputRC, SubRegIdx);
2241 if (NumFoldable == 0)
2244 CopyMI->
setDesc(
TII->get(AMDGPU::REG_SEQUENCE));
2248 for (
auto [Def, DestSubIdx] : NewDefs) {
2249 if (!
Def->isReg()) {
2253 BuildMI(
MBB, CopyMI,
DL,
TII->get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), Tmp)
2258 Def->setIsKill(
false);
2260 Register &VGPRCopy = VGPRCopies[Src];
2263 TRI->getSubRegisterClass(UseRC, DestSubIdx);
2288 B.addImm(DestSubIdx);
2295bool SIFoldOperandsImpl::tryFoldFoldableCopy(
2296 MachineInstr &
MI, MachineOperand *&CurrentKnownM0Val)
const {
2300 if (DstReg == AMDGPU::M0) {
2301 MachineOperand &NewM0Val =
MI.getOperand(1);
2302 if (CurrentKnownM0Val && CurrentKnownM0Val->
isIdenticalTo(NewM0Val)) {
2303 MI.eraseFromParent();
2314 MachineOperand *OpToFoldPtr;
2315 if (
MI.getOpcode() == AMDGPU::V_MOV_B16_t16_e64) {
2317 if (
TII->hasAnyModifiersSet(
MI))
2319 OpToFoldPtr = &
MI.getOperand(2);
2321 OpToFoldPtr = &
MI.getOperand(1);
2322 MachineOperand &OpToFold = *OpToFoldPtr;
2326 if (!FoldingImm && !OpToFold.
isReg())
2331 !
TRI->isConstantPhysReg(OpToFold.
getReg()))
2360 if (
MI.getOpcode() == AMDGPU::COPY && OpToFold.
isReg() &&
2362 if (DstRC == &AMDGPU::SReg_32RegClass &&
2364 if (!
TRI->getMatchingSuperRegClass(DstRC, &AMDGPU::SGPR_LO16RegClass,
2373 if (OpToFold.
isReg() &&
MI.isCopy() && !
MI.getOperand(1).getSubReg()) {
2374 if (foldCopyToAGPRRegSequence(&
MI))
2378 FoldableDef
Def(OpToFold, DstRC);
2379 bool Changed = foldInstOperand(
MI, Def);
2386 auto *InstToErase = &
MI;
2388 auto &SrcOp = InstToErase->getOperand(1);
2390 InstToErase->eraseFromParent();
2392 InstToErase =
nullptr;
2396 if (!InstToErase || !
TII->isFoldableCopy(*InstToErase))
2400 if (InstToErase && InstToErase->isRegSequence() &&
2402 InstToErase->eraseFromParent();
2412 return OpToFold.
isReg() &&
2413 foldCopyToVGPROfScalarAddOfFrameIndex(DstReg, OpToFold.
getReg(),
MI);
2418const MachineOperand *
2419SIFoldOperandsImpl::isClamp(
const MachineInstr &
MI)
const {
2420 unsigned Op =
MI.getOpcode();
2422 case AMDGPU::V_MAX_F32_e64:
2423 case AMDGPU::V_MAX_F16_e64:
2424 case AMDGPU::V_MAX_F16_t16_e64:
2425 case AMDGPU::V_MAX_F16_fake16_e64:
2426 case AMDGPU::V_MAX_F64_e64:
2427 case AMDGPU::V_MAX_NUM_F64_e64:
2428 case AMDGPU::V_PK_MAX_F16:
2429 case AMDGPU::V_MAX_BF16_PSEUDO_e64:
2430 case AMDGPU::V_PK_MAX_NUM_BF16: {
2431 if (
MI.mayRaiseFPException())
2434 if (!
TII->getNamedOperand(
MI, AMDGPU::OpName::clamp)->getImm())
2438 const MachineOperand *
Src0 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src0);
2439 const MachineOperand *
Src1 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src1);
2440 if (!
Src0->isReg() || !
Src1->isReg() ||
2441 Src0->getReg() !=
Src1->getReg() ||
2442 Src0->getSubReg() !=
Src1->getSubReg() ||
2443 Src0->getSubReg() != AMDGPU::NoSubRegister)
2447 if (
TII->hasModifiersSet(
MI, AMDGPU::OpName::omod))
2451 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src0_modifiers)->getImm();
2453 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src1_modifiers)->getImm();
2457 unsigned UnsetMods =
2458 (
Op == AMDGPU::V_PK_MAX_F16 ||
Op == AMDGPU::V_PK_MAX_NUM_BF16)
2461 if (Src0Mods != UnsetMods || Src1Mods != UnsetMods)
2471bool SIFoldOperandsImpl::tryFoldClamp(MachineInstr &
MI) {
2472 const MachineOperand *ClampSrc = isClamp(
MI);
2483 while (Def &&
Def->isCopy()) {
2484 Register CopySrcReg =
Def->getOperand(1).getReg();
2496 if (
Def->mayRaiseFPException())
2499 MachineOperand *DefClamp =
TII->getNamedOperand(*Def, AMDGPU::OpName::clamp);
2503 LLVM_DEBUG(
dbgs() <<
"Folding clamp " << *DefClamp <<
" into " << *Def);
2509 Register MIDstReg =
MI.getOperand(0).getReg();
2510 if (
TRI->isSGPRReg(*
MRI, DefReg)) {
2519 MI.eraseFromParent();
2524 if (
TII->convertToThreeAddress(*Def,
nullptr))
2525 Def->eraseFromParent();
2532 case AMDGPU::V_MUL_F64_e64:
2533 case AMDGPU::V_MUL_F64_pseudo_e64: {
2535 case 0x3fe0000000000000:
2537 case 0x4000000000000000:
2539 case 0x4010000000000000:
2545 case AMDGPU::V_MUL_F32_e64: {
2546 switch (
static_cast<uint32_t>(Val)) {
2557 case AMDGPU::V_MUL_F16_e64:
2558 case AMDGPU::V_MUL_F16_t16_e64:
2559 case AMDGPU::V_MUL_F16_fake16_e64: {
2560 switch (
static_cast<uint16_t>(Val)) {
2571 case AMDGPU::V_PK_MUL_BF16: {
2572 switch (
static_cast<uint16_t>(Val)) {
2591std::pair<const MachineOperand *, int>
2592SIFoldOperandsImpl::isOMod(
const MachineInstr &
MI)
const {
2593 unsigned Op =
MI.getOpcode();
2595 case AMDGPU::V_MUL_F64_e64:
2596 case AMDGPU::V_MUL_F64_pseudo_e64:
2597 case AMDGPU::V_MUL_F32_e64:
2598 case AMDGPU::V_MUL_F16_t16_e64:
2599 case AMDGPU::V_MUL_F16_fake16_e64:
2600 case AMDGPU::V_MUL_F16_e64: {
2602 if ((
Op == AMDGPU::V_MUL_F32_e64 &&
2604 ((
Op == AMDGPU::V_MUL_F64_e64 ||
Op == AMDGPU::V_MUL_F64_pseudo_e64 ||
2605 Op == AMDGPU::V_MUL_F16_e64 ||
Op == AMDGPU::V_MUL_F16_t16_e64 ||
2606 Op == AMDGPU::V_MUL_F16_fake16_e64) &&
2609 MI.mayRaiseFPException())
2612 const MachineOperand *
Src0 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src0);
2613 const MachineOperand *
Src1 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src1);
2616 std::optional<int64_t> Src1Imm =
TII->getImmOrMaterializedImm(*
MRI, *Src1);
2622 TII->hasModifiersSet(
MI, AMDGPU::OpName::src0_modifiers) ||
2623 TII->hasModifiersSet(
MI, AMDGPU::OpName::src1_modifiers) ||
2624 TII->hasModifiersSet(
MI, AMDGPU::OpName::omod) ||
2625 TII->hasModifiersSet(
MI, AMDGPU::OpName::clamp))
2628 return {
Src0, OMod};
2630 case AMDGPU::V_ADD_F64_e64:
2631 case AMDGPU::V_ADD_F64_pseudo_e64:
2632 case AMDGPU::V_ADD_F32_e64:
2633 case AMDGPU::V_ADD_F16_e64:
2634 case AMDGPU::V_ADD_F16_t16_e64:
2635 case AMDGPU::V_ADD_F16_fake16_e64: {
2637 if ((
Op == AMDGPU::V_ADD_F32_e64 &&
2639 ((
Op == AMDGPU::V_ADD_F64_e64 ||
Op == AMDGPU::V_ADD_F64_pseudo_e64 ||
2640 Op == AMDGPU::V_ADD_F16_e64 ||
Op == AMDGPU::V_ADD_F16_t16_e64 ||
2641 Op == AMDGPU::V_ADD_F16_fake16_e64) &&
2646 const MachineOperand *
Src0 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src0);
2647 const MachineOperand *
Src1 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src1);
2650 Src0->getSubReg() ==
Src1->getSubReg() &&
2651 !
TII->hasModifiersSet(
MI, AMDGPU::OpName::src0_modifiers) &&
2652 !
TII->hasModifiersSet(
MI, AMDGPU::OpName::src1_modifiers) &&
2653 !
TII->hasModifiersSet(
MI, AMDGPU::OpName::clamp) &&
2654 !
TII->hasModifiersSet(
MI, AMDGPU::OpName::omod))
2659 case AMDGPU::V_PK_MUL_BF16: {
2664 MI.mayRaiseFPException())
2667 const MachineOperand *
Src0 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src0);
2668 const MachineOperand *
Src1 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src1);
2671 std::optional<int64_t> Src1Imm =
TII->getImmOrMaterializedImm(*
MRI, *Src1);
2675 int OMod =
getOModValue(AMDGPU::V_PK_MUL_BF16, *Src1Imm);
2682 const MachineOperand *Src0Mods =
2683 TII->getNamedOperand(
MI, AMDGPU::OpName::src0_modifiers);
2684 const MachineOperand *Src1Mods =
2685 TII->getNamedOperand(
MI, AMDGPU::OpName::src1_modifiers);
2688 TII->hasModifiersSet(
MI, AMDGPU::OpName::omod) ||
2689 TII->hasModifiersSet(
MI, AMDGPU::OpName::clamp))
2692 return {
Src0, OMod};
2694 case AMDGPU::V_PK_ADD_BF16: {
2700 const MachineOperand *
Src0 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src0);
2701 const MachineOperand *
Src1 =
TII->getNamedOperand(
MI, AMDGPU::OpName::src1);
2703 if (!
Src0->isReg() || !
Src1->isReg() ||
Src0->getReg() !=
Src1->getReg() ||
2704 Src0->getSubReg() !=
Src1->getSubReg())
2708 const MachineOperand *Src0Mods =
2709 TII->getNamedOperand(
MI, AMDGPU::OpName::src0_modifiers);
2710 const MachineOperand *Src1Mods =
2711 TII->getNamedOperand(
MI, AMDGPU::OpName::src1_modifiers);
2714 TII->hasModifiersSet(
MI, AMDGPU::OpName::omod) ||
2715 TII->hasModifiersSet(
MI, AMDGPU::OpName::clamp))
2726bool SIFoldOperandsImpl::tryFoldOMod(MachineInstr &
MI) {
2727 const MachineOperand *RegOp;
2729 std::tie(RegOp, OMod) = isOMod(
MI);
2731 RegOp->
getSubReg() != AMDGPU::NoSubRegister ||
2736 Register OModSrcReg =
Def->getOperand(0).getReg();
2740 if (
Def->isRegSequence() &&
Def->getNumOperands() == 5 &&
2741 Def->getOperand(2).getImm() == AMDGPU::lo16) {
2743 bool CanLookThrough =
true;
2746 CanLookThrough =
false;
2748 if (CanLookThrough) {
2759 MachineOperand *DefOMod =
TII->getNamedOperand(*Def, AMDGPU::OpName::omod);
2763 if (
Def->mayRaiseFPException())
2768 if (
TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
2775 MI.eraseFromParent();
2780 if (
TII->convertToThreeAddress(*Def,
nullptr))
2781 Def->eraseFromParent();
2788bool SIFoldOperandsImpl::tryFoldSGPRSplatRegSequence(MachineInstr &
MI) {
2791 if (!ST->hasPackedFP64SingleSGPROps() && !ST->hasPackedU64SingleSGPROps())
2798 if (!
TRI->isSGPRClass(RegClass) ||
TRI->getRegSizeInBits(*RegClass) != 128)
2802 if (!getRegSeqInit(Defs,
Reg))
2806 if (Defs.
size() <= 1)
2809 const auto &[FirstOp,
_] = Defs.
front();
2810 if (!FirstOp->isReg())
2813 Register FirstReg = FirstOp->getReg();
2814 unsigned FirstSubReg = FirstOp->getSubReg();
2817 if (!
TRI->isSGPRClass(FirstRegClass))
2822 const auto &[
Op,
_] =
Def;
2823 return Op->isReg() &&
Op->getReg() == FirstReg &&
2824 Op->getSubReg() == FirstSubReg;
2836 MachineInstrBuilder
RS =
BuildMI(*
MI.getParent(),
MI,
MI.getDebugLoc(),
2837 TII->get(AMDGPU::REG_SEQUENCE), NewDst);
2840 FirstOp->setIsKill(
false);
2842 RS.addImm(Defs[0].second);
2847 for (
unsigned i = 1; i < Defs.
size(); ++i) {
2848 RS.addReg(UndefReg, RegState::Undef);
2849 RS.addImm(Defs[i].second);
2858 MI.eraseFromParent();
2864bool SIFoldOperandsImpl::tryFoldRegSequence(MachineInstr &
MI) {
2868 if (tryFoldSGPRSplatRegSequence(
MI))
2871 auto Reg =
MI.getOperand(0).getReg();
2873 if (!ST->hasGFX90AInsts() || !
TRI->isVGPR(*
MRI,
Reg) ||
2878 if (!getRegSeqInit(Defs,
Reg))
2881 for (
auto &[
Op, SubIdx] : Defs) {
2884 if (
TRI->isAGPR(*
MRI,
Op->getReg()))
2895 MachineInstr *
UseMI =
Op->getParent();
2904 if (
Op->getSubReg())
2910 if (!OpRC || !
TRI->isVectorSuperClass(OpRC))
2916 TII->get(AMDGPU::REG_SEQUENCE), Dst);
2918 for (
auto &[Def, SubIdx] : Defs) {
2919 Def->setIsKill(
false);
2931 if (!
TII->isOperandLegal(*
UseMI, OpIdx,
Op)) {
2933 RS->eraseFromParent();
2942 MI.eraseFromParent();
2950 Register &OutReg,
unsigned &OutSubReg) {
2960 if (
TRI.isAGPR(
MRI, CopySrcReg)) {
2961 OutReg = CopySrcReg;
2970 if (!CopySrcDef || !CopySrcDef->
isCopy())
2977 OtherCopySrc.
getSubReg() != AMDGPU::NoSubRegister ||
2978 !
TRI.isAGPR(
MRI, OtherCopySrcReg))
2981 OutReg = OtherCopySrcReg;
3015bool SIFoldOperandsImpl::tryFoldPhiAGPR(MachineInstr &
PHI) {
3019 if (!
TRI->isVGPR(*
MRI, PhiOut))
3025 for (
unsigned K = 1;
K <
PHI.getNumExplicitOperands();
K += 2) {
3026 MachineOperand &MO =
PHI.getOperand(K);
3028 if (!Copy || !
Copy->isCopy())
3032 unsigned AGPRRegMask = AMDGPU::NoSubRegister;
3037 if (
const auto *SubRC =
TRI->getSubRegisterClass(CopyInRC, AGPRRegMask))
3048 bool IsAGPR32 = (ARC == &AMDGPU::AGPR_32RegClass);
3052 for (
unsigned K = 1;
K <
PHI.getNumExplicitOperands();
K += 2) {
3053 MachineOperand &MO =
PHI.getOperand(K);
3057 MachineBasicBlock *InsertMBB =
nullptr;
3060 unsigned CopyOpc = AMDGPU::COPY;
3065 if (
Def->isCopy()) {
3067 unsigned AGPRSubReg = AMDGPU::NoSubRegister;
3080 MachineOperand &CopyIn =
Def->getOperand(1);
3083 CopyOpc = AMDGPU::V_ACCVGPR_WRITE_B32_e64;
3086 InsertMBB =
Def->getParent();
3095 TII->get(CopyOpc), NewReg)
3105 PHI.getOperand(0).setReg(NewReg);
3111 TII->get(AMDGPU::COPY), PhiOut)
3119bool SIFoldOperandsImpl::tryFoldLoad(MachineInstr &
MI) {
3121 if (!ST->hasGFX90AInsts() ||
MI.getNumExplicitDefs() != 1)
3124 MachineOperand &
Def =
MI.getOperand(0);
3141 while (!
Users.empty()) {
3142 const MachineInstr *
I =
Users.pop_back_val();
3143 if (!
I->isCopy() && !
I->isRegSequence())
3145 Register DstReg =
I->getOperand(0).getReg();
3149 if (
TRI->isAGPR(*
MRI, DstReg))
3153 Users.push_back(&U);
3158 if (!
TII->isOperandLegal(
MI, 0, &Def)) {
3163 while (!MoveRegs.
empty()) {
3205bool SIFoldOperandsImpl::tryOptimizeAGPRPhis(MachineBasicBlock &
MBB) {
3208 if (ST->hasGFX90AInsts())
3212 DenseMap<std::pair<Register, unsigned>, std::vector<MachineOperand *>>
3215 for (
auto &
MI :
MBB) {
3219 if (!
TRI->isAGPR(*
MRI,
MI.getOperand(0).getReg()))
3222 for (
unsigned K = 1;
K <
MI.getNumOperands();
K += 2) {
3223 MachineOperand &PhiMO =
MI.getOperand(K);
3233 for (
const auto &[Entry, MOs] : RegToMO) {
3234 if (MOs.size() == 1)
3239 MachineBasicBlock *DefMBB =
Def->getParent();
3246 MachineInstr *VGPRCopy =
3248 TII->get(AMDGPU::V_ACCVGPR_READ_B32_e64), TempVGPR)
3254 TII->get(AMDGPU::COPY), TempAGPR)
3258 for (MachineOperand *MO : MOs) {
3270bool SIFoldOperandsImpl::run(
MachineFunction &MF,
const MachineLoopInfo *MLI) {
3276 MFI = MF.
getInfo<SIMachineFunctionInfo>();
3287 MachineOperand *CurrentKnownM0Val =
nullptr;
3295 if (tryConstantFoldOp(&
MI)) {
3300 if (tryFoldRedundantAND(
MI)) {
3305 if (tryFoldAndExec(
MI)) {
3310 if (
MI.isRegSequence() && tryFoldRegSequence(
MI)) {
3315 if (
MI.isPHI() && tryFoldPhiAGPR(
MI)) {
3320 if (
MI.mayLoad() && tryFoldLoad(
MI)) {
3325 if (tryFoldAndCSelectExec(
MI)) {
3330 if (
TII->isFoldableCopy(
MI)) {
3331 Changed |= tryFoldFoldableCopy(
MI, CurrentKnownM0Val);
3336 if (CurrentKnownM0Val &&
MI.modifiesRegister(AMDGPU::M0,
TRI))
3337 CurrentKnownM0Val =
nullptr;
3357 bool Changed = SIFoldOperandsImpl().run(MF, MLI);
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo MachineBasicBlock::iterator InsertPt
const AArch64InstrInfo & TII
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool updateOperand(Instruction *Inst, unsigned Idx, Instruction *Mat)
Updates the operand at Idx in instruction Inst with the result of instruction Mat.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
AMD GCN specific subclass of TargetSubtarget.
static Register UseReg(const MachineOperand &MO)
iv Induction Variable Users
Register const TargetRegisterInfo * TRI
Promote Memory to Register
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
static bool loopModifiesExec(const MachineLoop &L, const SIRegisterInfo &TRI)
static unsigned macToMad(unsigned Opc)
static bool isAGPRCopy(const SIRegisterInfo &TRI, const MachineRegisterInfo &MRI, const MachineInstr &Copy, Register &OutReg, unsigned &OutSubReg)
Checks whether Copy is a AGPR -> VGPR copy.
static void appendFoldCandidate(SmallVectorImpl< FoldCandidate > &FoldList, FoldCandidate &&Entry)
static unsigned getZeroHi16LegacyOpcode(const GCNSubtarget &ST, const MachineInstr &MI)
static const TargetRegisterClass * getRegOpRC(const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const MachineOperand &MO)
static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result, uint32_t LHS, uint32_t RHS)
static int getOModValue(unsigned Opc, int64_t Val)
static unsigned getMovOpc(bool IsScalar)
static MachineOperand * lookUpCopyChain(const SIInstrInfo &TII, const MachineRegisterInfo &MRI, Register SrcReg)
static constexpr AMDGPU::OpName OpSelOnlyOpNames[]
Interface definition for SIInstrInfo.
Interface definition for SIRegisterInfo.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
const AArch64RegisterInfo & getRegisterInfo() const
getRegisterInfo - TargetInstrInfo is a superset of MRegister info.
const unsigned CSelectOpc
static const LaneMaskConstants & get(const GCNSubtarget &ST)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
const SIInstrInfo * getInstrInfo() const override
bool hasDOTOpSelHazard() const
bool zeroesHigh16BitsOfDest(unsigned Opcode) const
Returns if the result of this instruction with a 16-bit result returned in a 32-bit register implicit...
bool contains(const LoopT *L) const
Return true if the specified loop is contained within this loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
ArrayRef< MCOperandInfo > operands() const
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specified operand constraint if it is present.
bool isVariadic() const
Return true if this instruction can have a variable number of operands.
bool hasSuperClassEq(const MCRegisterClass *RC) const
Returns true if RC is a super-class of or equal to this class.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
bool hasSubClassEq(const MCRegisterClass *RC) const
Returns true if RC is a sub-class of or equal to this class.
An RAII based helper class to modify MachineFunctionProperties when running pass.
LLVM_ABI iterator SkipPHIsLabelsAndDebug(iterator I, Register Reg=Register(), bool SkipPseudoOp=true)
Return the first instruction in MBB after I that is not a PHI, label or debug.
LLVM_ABI LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI, MCRegister Reg, const_iterator Before, unsigned Neighborhood=10) const
Return whether (physical) register Reg has been defined and not killed as of just before Before.
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
LivenessQueryResult
Possible outcome of a register liveness query to computeRegisterLiveness()
@ LQR_Dead
Register is known to be fully dead.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & setOperandDead(unsigned OpIdx) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isImplicitDef() const
const MachineBasicBlock * getParent() const
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr reads the specified register.
LLVM_ABI bool allImplicitDefsAreDead() const
Return true if all the implicit defs of this instruction are dead.
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
LLVM_ABI unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
mop_range implicit_operands()
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
void clearFlag(MIFlag Flag)
clearFlag - Clear a MI flag.
bool isRegSequence() const
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
MachineOperand * mop_iterator
iterator/begin/end - Iterate over all operands of a machine instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Analysis pass that exposes the MachineLoopInfo for a machine function.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
LLVM_ABI unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
LLVM_ABI void substVirtReg(Register Reg, unsigned SubIdx, const TargetRegisterInfo &)
substVirtReg - Substitute the current register with the virtual subregister Reg:SubReg.
LLVM_ABI void ChangeToFrameIndex(int Idx, unsigned TargetFlags=0)
Replace this operand with a frame index.
void setImm(int64_t immVal)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
LLVM_ABI void ChangeToGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
ChangeToGA - Replace this operand with a new global address operand.
void setIsKill(bool Val=true)
LLVM_ABI void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
LLVM_ABI void substPhysReg(MCRegister Reg, const TargetRegisterInfo &)
substPhysReg - Substitute the current register with the physical register Reg, taking any existing Su...
static MachineOperand CreateImm(int64_t Val)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
void setIsUndef(bool Val=true)
Register getReg() const
getReg - Returns the register number.
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
@ MO_Immediate
Immediate operand.
@ MO_GlobalAddress
Address of a global value.
@ MO_FrameIndex
Abstract Stack Frame Index.
@ MO_Register
Register operand.
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
use_nodbg_iterator use_nodbg_begin(Register RegNo) const
LLVM_ABI void markUsesInDebugValueAsUndef(Register Reg) const
markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the specified register as undefined wh...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI bool hasOneNonDBGUser(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug instruction using the specified regis...
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg)
setRegAllocationHint - Specify a register allocation hint for the specified virtual register.
LLVM_ABI void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static bool hasSameClamp(const MachineInstr &A, const MachineInstr &B)
static std::optional< int64_t > extractSubregFromImm(int64_t ImmVal, unsigned SubRegIndex)
Return the extracted immediate value in a subregister use from a constant materialized in a super reg...
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Register getScratchRSrcReg() const
Returns the physical register reserved for use as the resource descriptor for scratch accesses.
SIModeRegisterDefaults getMode() const
static unsigned getSubRegFromChannel(uint64_t Channel, uint64_t NumRegs=1)
bool insert(const value_type &X)
Insert a new element into the SetVector.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
static const unsigned CommuteAnyOperandIndex
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM Value Representation.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_READONLY int32_t getMFMAEarlyClobberOp(uint32_t Opcode)
bool isPackedSingleSGPR64BitInst(unsigned Opc)
The opcode is a packed 64-bit instruction which only reads low 64 bits of a scalar operand and propag...
LLVM_READONLY bool hasNamedOperand(uint32_t Opcode, OpName NamedIdx)
LLVM_READONLY int32_t getVOPe32(uint32_t Opcode)
constexpr bool isSISrcOperand(const MCOperandInfo &OpInfo)
Is this an AMDGPU specific source operand?
bool isInlinableLiteralBF16(int16_t Literal, bool HasInv2Pi, bool *IsBF16)
@ OPERAND_REG_INLINE_C_FP64
@ OPERAND_REG_INLINE_C_BF16
@ OPERAND_REG_INLINE_C_V2BF16
@ OPERAND_REG_IMM_V2INT64
@ OPERAND_REG_IMM_V2INT16
@ OPERAND_REG_INLINE_C_INT64
@ OPERAND_REG_IMM_NOINLINE_V2FP16
@ OPERAND_REG_INLINE_C_V2FP16
@ OPERAND_REG_INLINE_AC_INT32
Operands with an AccVGPR register or inline constant.
@ OPERAND_REG_INLINE_AC_FP32
@ OPERAND_REG_INLINE_C_FP32
@ OPERAND_REG_INLINE_C_INT32
@ OPERAND_REG_INLINE_C_V2INT16
@ OPERAND_REG_INLINE_AC_FP64
LLVM_READONLY int32_t getFlatScratchInstSSfromSV(uint32_t Opcode)
bool supportsScaleOffset(const MCInstrInfo &MII, unsigned Opcode)
constexpr bool isVOP3(const T &...O)
constexpr bool isMAI(const T &...O)
constexpr bool isSWMMAC(const T &...O)
constexpr bool isVOP3P(const T &...O)
constexpr bool isWMMA(const T &...O)
constexpr bool isDOT(const T &...O)
constexpr bool isPacked(const T &...O)
NodeAddr< DefNode * > Def
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
TargetInstrInfo::RegSubRegPair getRegSubRegPair(const MachineOperand &O)
Create RegSubRegPair from a register MachineOperand.
MachineBasicBlock::instr_iterator getBundleStart(MachineBasicBlock::instr_iterator I)
Returns an iterator to the first instruction in the bundle containing I.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI, Register VReg, const MachineInstr &DefMI, const MachineInstr &UseMI)
Return false if EXEC is not changed between the def of VReg at DefMI and the use at UseMI.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
char & SIFoldOperandsLegacyID
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
iterator_range< df_iterator< T > > depth_first(const T &G)
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
constexpr uint64_t Make_64(uint32_t High, uint32_t Low)
Make a 64-bit integer from a high / low pair of 32-bit integers.
MCRegisterClass TargetRegisterClass
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
DenormalMode FP64FP16Denormals
If this is set, neither input or output denormals are flushed for both f64 and f16/v2f16 instructions...
bool IEEE
Floating point opcodes that support exception flag gathering quiet and propagate signaling NaN inputs...
DenormalMode FP32Denormals
If this is set, neither input or output denormals are flushed for most f32 instructions.