[llvm] GlobalISel: Migrate misc. CombinerHelper def checks to MIPatternMatch (PR #216671)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 17 02:45:32 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-globalisel
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
Replace getVRegDef + cast/opcode-check idioms across CombinerHelper
with mi_match, adding named instruction binders and operand-form matchers
as needed.
Co-authored-by: Claude (Opus 4.8) <noreply@<!-- -->anthropic.com>
---
Patch is 41.51 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/216671.diff
4 Files Affected:
- (modified) llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h (+47-7)
- (modified) llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp (+208-157)
- (modified) llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp (+6-4)
- (modified) llvm/lib/CodeGen/GlobalISel/CombinerHelperVectorOps.cpp (+24-18)
``````````diff
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h b/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
index e6c6f69d3e0a1..a4312d48ae5cf 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
@@ -480,13 +480,12 @@ struct ImplicitDefMatch {
inline ImplicitDefMatch m_GImplicitDef() { return ImplicitDefMatch(); }
-/// Matches a G_CONSTANT and binds the defining instruction. Unlike m_ICst, this
-/// returns the instruction (not the value) and does not look through vector
-/// splats.
-template <typename Class> struct GConstantMatch {
+/// Binds the defining instruction of \p Reg if it is a \p Class. Prefer the
+/// named helpers below so the opcode is spelled out at the call site.
+template <typename Class> struct GInstrBind {
Class *&Inst;
- GConstantMatch(Class *&Inst) : Inst(Inst) {}
+ GInstrBind(Class *&Inst) : Inst(Inst) {}
bool match(const MachineRegisterInfo &MRI, Register Reg) {
MachineInstr *TmpMI;
if (mi_match(Reg, MRI, m_MInstr(TmpMI))) {
@@ -499,8 +498,43 @@ template <typename Class> struct GConstantMatch {
}
};
-inline GConstantMatch<GConstant> m_GConstant(GConstant *&Inst) { return Inst; }
-inline GConstantMatch<const GConstant> m_GConstant(const GConstant *&Inst) {
+/// Match a literal G_CONSTANT instruction (no look-through of splats or
+/// copies).
+inline GInstrBind<GConstant> m_GConstant(GConstant *&Inst) { return Inst; }
+inline GInstrBind<const GConstant> m_GConstant(const GConstant *&Inst) {
+ return Inst;
+}
+
+/// Match a literal G_CONSTANT or G_FCONSTANT, binding its raw bits to \p Bits
+/// (the integer value, or the float reinterpreted as an integer).
+struct GConstantBitsMatch {
+ APInt &Bits;
+ bool match(const MachineRegisterInfo &MRI, Register Reg) {
+ MachineInstr *MI = MRI.getVRegDef(Reg);
+ if (MI->getOpcode() == TargetOpcode::G_CONSTANT) {
+ Bits = MI->getOperand(1).getCImm()->getValue();
+ return true;
+ }
+ if (MI->getOpcode() == TargetOpcode::G_FCONSTANT) {
+ Bits = MI->getOperand(1).getFPImm()->getValueAPF().bitcastToAPInt();
+ return true;
+ }
+ return false;
+ }
+};
+
+inline GConstantBitsMatch m_GConstantOrFConstantBits(APInt &Bits) {
+ return {Bits};
+}
+
+/// Instruction binders for ops with no operand-form matcher (constant-immediate
+/// or variadic-source ops).
+inline GInstrBind<GUnmerge> m_GUnmerge(GUnmerge *&Inst) { return Inst; }
+inline GInstrBind<GVScale> m_GVScale(GVScale *&Inst) { return Inst; }
+inline GInstrBind<GBuildVector> m_GBuildVector(GBuildVector *&Inst) {
+ return Inst;
+}
+inline GInstrBind<GConcatVectors> m_GConcatVectors(GConcatVectors *&Inst) {
return Inst;
}
@@ -624,6 +658,12 @@ inline BinaryOp_match<LHS, RHS, TargetOpcode::G_SUB> m_GSub(const LHS &L,
return BinaryOp_match<LHS, RHS, TargetOpcode::G_SUB>(L, R);
}
+template <typename LHS, typename RHS>
+inline BinaryOp_match<LHS, RHS, TargetOpcode::G_SUB>
+m_GSub(const LHS &L, const RHS &R, MIFlagsRef Flags) {
+ return BinaryOp_match<LHS, RHS, TargetOpcode::G_SUB>(L, R, Flags);
+}
+
template <typename LHS, typename RHS>
inline BinaryOp_match<LHS, RHS, TargetOpcode::G_MUL, true>
m_GMul(const LHS &L, const RHS &R) {
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index b8f5f840d5cff..92e3e8e4c1d5b 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -261,7 +261,9 @@ bool CombinerHelper::matchFreezeOfSingleMaybePoisonOperand(
if (!MRI.hasOneNonDBGUse(OrigOp))
return false;
- MachineInstr *OrigDef = MRI.getUniqueVRegDef(OrigOp);
+ MachineInstr *OrigDef;
+ if (!mi_match(OrigOp, MRI, m_MInstr(OrigDef)))
+ return false;
// Even if only a single operand of the PHI is not guaranteed non-poison,
// moving freeze() backwards across a PHI can cause optimization issues for
// other users of that operand.
@@ -338,8 +340,9 @@ bool CombinerHelper::matchCombineConcatVectors(
// Then collect their operands in Ops.
for (const MachineOperand &MO : MI.uses()) {
Register Reg = MO.getReg();
- MachineInstr *Def = MRI.getVRegDef(Reg);
- assert(Def && "Operand not defined");
+ MachineInstr *Def;
+ if (!mi_match(Reg, MRI, m_MInstr(Def)))
+ return false;
if (!MRI.hasOneNonDBGUse(Reg))
return false;
switch (Def->getOpcode()) {
@@ -414,10 +417,10 @@ bool CombinerHelper::matchCombineBuildVectorOfBitcast(
GUnmerge *Unmerge = getOpcodeDef<GUnmerge>(BV.getSourceReg(0), MRI);
if (!Unmerge || Unmerge->getReg(0) != BV.getSourceReg(0))
return false;
- MachineInstr *BC = MRI.getVRegDef(Unmerge->getSourceReg());
- if (BC->getOpcode() != TargetOpcode::G_BITCAST)
+ Register BCSrc;
+ if (!mi_match(Unmerge->getSourceReg(), MRI, m_GBitcast(m_Reg(BCSrc))))
return false;
- LLT InputTy = MRI.getType(BC->getOperand(1).getReg());
+ LLT InputTy = MRI.getType(BCSrc);
unsigned Factor = Unmerge->getNumDefs();
if (!InputTy.isScalar() || BV.getNumSources() % Factor != 0)
return false;
@@ -431,8 +434,7 @@ bool CombinerHelper::matchCombineBuildVectorOfBitcast(
for (unsigned Idx = 0; Idx < BV.getNumSources(); Idx += Factor) {
GUnmerge *Unmerge = getOpcodeDef<GUnmerge>(BV.getSourceReg(Idx), MRI);
if (!all_of(iota_range<unsigned>(0, Factor, false), [&](unsigned J) {
- MachineInstr *Src = MRI.getVRegDef(BV.getSourceReg(Idx + J));
- if (Src->getOpcode() == TargetOpcode::G_IMPLICIT_DEF)
+ if (mi_match(BV.getSourceReg(Idx + J), MRI, m_GImplicitDef()))
return true;
return Unmerge && BV.getSourceReg(Idx + J) == Unmerge->getReg(J);
}))
@@ -440,11 +442,12 @@ bool CombinerHelper::matchCombineBuildVectorOfBitcast(
if (!Unmerge)
Ops.push_back(0);
else {
- MachineInstr *BC = MRI.getVRegDef(Unmerge->getSourceReg());
- if (BC->getOpcode() != TargetOpcode::G_BITCAST ||
- MRI.getType(BC->getOperand(1).getReg()) != InputTy)
+ Register BCSrc;
+ if (!mi_match(
+ Unmerge->getSourceReg(), MRI,
+ m_GBitcast(m_all_of(m_Reg(BCSrc), m_SpecificType(InputTy)))))
return false;
- Ops.push_back(BC->getOperand(1).getReg());
+ Ops.push_back(BCSrc);
}
}
@@ -502,11 +505,9 @@ void CombinerHelper::applyCombineShuffleToBuildVector(MachineInstr &MI) const {
bool CombinerHelper::matchCombineShuffleConcat(
MachineInstr &MI, SmallVector<Register> &Ops) const {
ArrayRef<int> Mask = MI.getOperand(3).getShuffleMask();
- auto ConcatMI1 =
- dyn_cast<GConcatVectors>(MRI.getVRegDef(MI.getOperand(1).getReg()));
- auto ConcatMI2 =
- dyn_cast<GConcatVectors>(MRI.getVRegDef(MI.getOperand(2).getReg()));
- if (!ConcatMI1 || !ConcatMI2)
+ GConcatVectors *ConcatMI1, *ConcatMI2;
+ if (!mi_match(MI.getOperand(1).getReg(), MRI, m_GConcatVectors(ConcatMI1)) ||
+ !mi_match(MI.getOperand(2).getReg(), MRI, m_GConcatVectors(ConcatMI2)))
return false;
// Check that the sources of the Concat instructions have the same type
@@ -1285,7 +1286,9 @@ bool CombinerHelper::findPostIndexCandidate(GLoadStore &LdSt, Register &Addr,
return false;
MachineInstr *StoredValDef = getDefIgnoringCopies(LdSt.getReg(0), MRI);
- auto *PtrDef = MRI.getVRegDef(Ptr);
+ MachineInstr *PtrDef;
+ if (!mi_match(Ptr, MRI, m_MInstr(PtrDef)))
+ return false;
unsigned NumUsesChecked = 0;
for (auto &Use : MRI.use_nodbg_instructions(Ptr)) {
@@ -1310,7 +1313,9 @@ bool CombinerHelper::findPostIndexCandidate(GLoadStore &LdSt, Register &Addr,
continue;
// Make sure the offset calculation is before the potentially indexed op.
- MachineInstr *OffsetDef = MRI.getVRegDef(Offset);
+ MachineInstr *OffsetDef;
+ if (!mi_match(Offset, MRI, m_MInstr(OffsetDef)))
+ continue;
RematOffset = false;
if (!dominates(*OffsetDef, LdSt)) {
// If the offset however is just a G_CONSTANT, we can always just
@@ -1540,7 +1545,7 @@ bool CombinerHelper::matchCombineIndexedLoadStore(
void CombinerHelper::applyCombineIndexedLoadStore(
MachineInstr &MI, IndexedLoadStoreMatchInfo &MatchInfo) const {
- MachineInstr &AddrDef = *MRI.getUniqueVRegDef(MatchInfo.Addr);
+ MachineInstr &AddrDef = *MRI.getVRegDef(MatchInfo.Addr);
unsigned Opcode = MI.getOpcode();
bool IsStore = Opcode == TargetOpcode::G_STORE;
unsigned NewOpcode = getIndexedOpc(Opcode);
@@ -1946,8 +1951,8 @@ bool CombinerHelper::matchShiftImmedChain(MachineInstr &MI,
if (!MaybeImmVal)
return false;
- MachineInstr *Shl2Def = MRI.getUniqueVRegDef(Shl2);
- if (Shl2Def->getOpcode() != Opcode)
+ MachineInstr *Shl2Def;
+ if (!mi_match(Shl2, MRI, m_MInstr(Shl2Def)) || Shl2Def->getOpcode() != Opcode)
return false;
Register Base = Shl2Def->getOperand(1).getReg();
@@ -2027,7 +2032,9 @@ bool CombinerHelper::matchShiftOfShiftedLogic(
if (!MRI.hasOneNonDBGUse(LogicDest))
return false;
- MachineInstr *LogicMI = MRI.getUniqueVRegDef(LogicDest);
+ MachineInstr *LogicMI;
+ if (!mi_match(LogicDest, MRI, m_MInstr(LogicMI)))
+ return false;
unsigned LogicOpcode = LogicMI->getOpcode();
if (LogicOpcode != TargetOpcode::G_AND && LogicOpcode != TargetOpcode::G_OR &&
LogicOpcode != TargetOpcode::G_XOR)
@@ -2059,9 +2066,12 @@ bool CombinerHelper::matchShiftOfShiftedLogic(
// Logic ops are commutative, so check each operand for a match.
Register LogicMIReg1 = LogicMI->getOperand(1).getReg();
- MachineInstr *LogicMIOp1 = MRI.getUniqueVRegDef(LogicMIReg1);
+ MachineInstr *LogicMIOp1;
Register LogicMIReg2 = LogicMI->getOperand(2).getReg();
- MachineInstr *LogicMIOp2 = MRI.getUniqueVRegDef(LogicMIReg2);
+ MachineInstr *LogicMIOp2;
+ if (!mi_match(LogicMIReg1, MRI, m_MInstr(LogicMIOp1)) ||
+ !mi_match(LogicMIReg2, MRI, m_MInstr(LogicMIOp2)))
+ return false;
uint64_t C0Val;
if (matchFirstShift(LogicMIOp1, C0Val)) {
@@ -2136,9 +2146,11 @@ bool CombinerHelper::matchCommuteShift(MachineInstr &MI,
if (!getTargetLowering().isDesirableToCommuteWithShift(MI, !isPreLegalize()))
return false;
+ MachineInstr *SrcDef;
if (!mi_match(SrcReg, MRI,
m_OneNonDBGUse(m_any_of(m_GAdd(m_Reg(X), m_Reg(C1)),
- m_GOr(m_Reg(X), m_Reg(C1))))))
+ m_GOr(m_Reg(X), m_Reg(C1))))) ||
+ !mi_match(SrcReg, MRI, m_MInstr(SrcDef)))
return false;
APInt C1Val, C2Val;
@@ -2146,14 +2158,12 @@ bool CombinerHelper::matchCommuteShift(MachineInstr &MI,
!mi_match(ShiftReg, MRI, m_ICstOrSplat(C2Val)))
return false;
- auto *SrcDef = MRI.getVRegDef(SrcReg);
- assert((SrcDef->getOpcode() == TargetOpcode::G_ADD ||
- SrcDef->getOpcode() == TargetOpcode::G_OR) && "Unexpected op");
+ unsigned SrcOpc = SrcDef->getOpcode();
LLT SrcTy = MRI.getType(SrcReg);
MatchInfo = [=](MachineIRBuilder &B) {
auto S1 = B.buildShl(SrcTy, X, ShiftReg);
auto S2 = B.buildShl(SrcTy, C1, ShiftReg);
- B.buildInstr(SrcDef->getOpcode(), {DstReg}, {S1, S2});
+ B.buildInstr(SrcOpc, {DstReg}, {S1, S2});
};
return true;
}
@@ -2412,15 +2422,10 @@ bool CombinerHelper::matchCombineUnmergeConstant(
MachineInstr &MI, SmallVectorImpl<APInt> &Csts) const {
unsigned SrcIdx = MI.getNumOperands() - 1;
Register SrcReg = MI.getOperand(SrcIdx).getReg();
- MachineInstr *SrcInstr = MRI.getVRegDef(SrcReg);
- if (SrcInstr->getOpcode() != TargetOpcode::G_CONSTANT &&
- SrcInstr->getOpcode() != TargetOpcode::G_FCONSTANT)
- return false;
// Break down the big constant in smaller ones.
- const MachineOperand &CstVal = SrcInstr->getOperand(1);
- APInt Val = SrcInstr->getOpcode() == TargetOpcode::G_CONSTANT
- ? CstVal.getCImm()->getValue()
- : CstVal.getFPImm()->getValueAPF().bitcastToAPInt();
+ APInt Val;
+ if (!mi_match(SrcReg, MRI, m_GConstantOrFConstantBits(Val)))
+ return false;
LLT Dst0Ty = MRI.getType(MI.getOperand(0).getReg());
unsigned ShiftAmt = Dst0Ty.getSizeInBits();
@@ -2518,12 +2523,9 @@ void CombinerHelper::applyCombineUnmergeZExtToZExt(MachineInstr &MI) const {
Register Dst0Reg = MI.getOperand(0).getReg();
- MachineInstr *ZExtInstr =
- MRI.getVRegDef(MI.getOperand(MI.getNumDefs()).getReg());
- assert(ZExtInstr && ZExtInstr->getOpcode() == TargetOpcode::G_ZEXT &&
- "Expecting a G_ZEXT");
-
- Register ZExtSrcReg = ZExtInstr->getOperand(1).getReg();
+ GZext *ZExtInstr =
+ cast<GZext>(MRI.getVRegDef(MI.getOperand(MI.getNumDefs()).getReg()));
+ Register ZExtSrcReg = ZExtInstr->getSrcReg();
LLT Dst0Ty = MRI.getType(Dst0Reg);
LLT ZExtSrcTy = MRI.getType(ZExtSrcReg);
@@ -3282,8 +3284,8 @@ bool CombinerHelper::matchBinopWithNegInner(Register MInner, Register Other,
BuildFnTy &MatchInfo) const {
/// Helper function for matchBinopWithNeg: tries to match one commuted form
/// of `a bitwiseop (~b +/- c)` -> `a bitwiseop ~(b -/+ c)`.
- MachineInstr *InnerDef = MRI.getVRegDef(MInner);
- if (!InnerDef)
+ MachineInstr *InnerDef;
+ if (!mi_match(MInner, MRI, m_MInstr(InnerDef)))
return false;
unsigned InnerOpc = InnerDef->getOpcode();
@@ -3750,49 +3752,41 @@ bool CombinerHelper::matchUseVectorTruncate(MachineInstr &MI,
// Check the G_BUILD_VECTOR sources
unsigned I;
- MachineInstr *UnmergeMI = nullptr;
+ GUnmerge *UnmergeMI = nullptr;
// Check all source TRUNCs come from the same UNMERGE instruction
// and that the element order matches (BUILD_VECTOR position I
// corresponds to UNMERGE result I)
for (I = 0; I < NumOperands; ++I) {
- auto SrcMI = MRI.getVRegDef(BuildMI->getSourceReg(I));
- auto SrcMIOpc = SrcMI->getOpcode();
-
// Check if the G_TRUNC instructions all come from the same MI
- if (SrcMIOpc == TargetOpcode::G_TRUNC) {
- Register TruncSrcReg = SrcMI->getOperand(1).getReg();
- if (!UnmergeMI) {
- UnmergeMI = MRI.getVRegDef(TruncSrcReg);
- if (UnmergeMI->getOpcode() != TargetOpcode::G_UNMERGE_VALUES)
- return false;
- } else {
- auto UnmergeSrcMI = MRI.getVRegDef(TruncSrcReg);
- if (UnmergeMI != UnmergeSrcMI)
- return false;
- }
- // Verify element ordering: BUILD_VECTOR position I must use
- // UNMERGE result I, otherwise the fold would lose element reordering
- if (UnmergeMI->getOperand(I).getReg() != TruncSrcReg)
+ Register TruncSrcReg;
+ if (!mi_match(BuildMI->getSourceReg(I), MRI, m_GTrunc(m_Reg(TruncSrcReg))))
+ break;
+
+ if (!UnmergeMI) {
+ if (!mi_match(TruncSrcReg, MRI, m_GUnmerge(UnmergeMI)))
return false;
} else {
- break;
+ MachineInstr *UnmergeSrcMI;
+ if (!mi_match(TruncSrcReg, MRI, m_MInstr(UnmergeSrcMI)) ||
+ UnmergeMI != UnmergeSrcMI)
+ return false;
}
+ // Element order must match: position I must use UNMERGE result I.
+ if (UnmergeMI->getOperand(I).getReg() != TruncSrcReg)
+ return false;
}
if (I < 2)
return false;
// Check the remaining source elements are only G_IMPLICIT_DEF
for (; I < NumOperands; ++I) {
- auto SrcMI = MRI.getVRegDef(BuildMI->getSourceReg(I));
- auto SrcMIOpc = SrcMI->getOpcode();
-
- if (SrcMIOpc != TargetOpcode::G_IMPLICIT_DEF)
+ if (!mi_match(BuildMI->getSourceReg(I), MRI, m_GImplicitDef()))
return false;
}
// Check the size of unmerge source
- MatchInfo = cast<GUnmerge>(UnmergeMI)->getSourceReg();
+ MatchInfo = UnmergeMI->getSourceReg();
LLT UnmergeSrcTy = MRI.getType(MatchInfo);
if (!DstTy.getElementCount().isKnownMultipleOf(UnmergeSrcTy.getNumElements()))
return false;
@@ -3872,7 +3866,9 @@ bool CombinerHelper::matchNotCmp(
Register Reg = RegsToNegate[I];
if (!MRI.hasOneNonDBGUse(Reg))
return false;
- MachineInstr *Def = MRI.getVRegDef(Reg);
+ MachineInstr *Def;
+ if (!mi_match(Reg, MRI, m_MInstr(Def)))
+ return false;
switch (Def->getOpcode()) {
default:
// Don't match if the tree contains anything other than ANDs, ORs and
@@ -3907,11 +3903,10 @@ bool CombinerHelper::matchNotCmp(
// the constant in the xor.
int64_t Cst;
if (Ty.isVector()) {
- MachineInstr *CstDef = MRI.getVRegDef(CstReg);
- auto MaybeCst = getIConstantSplatSExtVal(*CstDef, MRI);
- if (!MaybeCst)
+ int64_t SplatCst;
+ if (!mi_match(CstReg, MRI, m_ICstOrSplat(SplatCst)))
return false;
- if (!isConstValidTrue(TLI, Ty.getScalarSizeInBits(), *MaybeCst, true, IsFP))
+ if (!isConstValidTrue(TLI, Ty.getScalarSizeInBits(), SplatCst, true, IsFP))
return false;
} else {
if (!mi_match(CstReg, MRI, m_ICst(Cst)))
@@ -4014,7 +4009,9 @@ bool CombinerHelper::matchPtrAddZero(MachineInstr &MI) const {
}
assert(Ty.isVector() && "Expecting a vector type");
- const MachineInstr *VecMI = MRI.getVRegDef(PtrAdd.getBaseReg());
+ const MachineInstr *VecMI;
+ if (!mi_match(PtrAdd.getBaseReg(), MRI, m_MInstr(VecMI)))
+ return false;
return isBuildVectorAllZeros(*VecMI, MRI);
}
@@ -4045,22 +4042,25 @@ bool CombinerHelper::matchFoldBinOpIntoSelect(MachineInstr &MI,
Register OtherOperandReg = RHS;
SelectOpNo = 1;
- MachineInstr *Select = MRI.getVRegDef(LHS);
+ Register SelectTrue, SelectFalse;
// Don't do this unless the old select is going away. We want to eliminate the
// binary operator, not replace a binop with a select.
- if (Select->getOpcode() != TargetOpcode::G_SELECT ||
+ if (!mi_match(LHS, MRI,
+ m_GISelect(m_Reg(), m_Reg(SelectTrue), m_Reg(SelectFalse))) ||
!MRI.hasOneNonDBGUse(LHS)) {
OtherOperandReg = LHS;
SelectOpNo = 2;
- Select = MRI.getVRegDef(RHS);
- if (Select->getOpcode() != TargetOpcode::G_SELECT ||
+ if (!mi_match(RHS, MRI,
+ m_GISelect(m_Reg(), m_Reg(SelectTrue), m_Reg(SelectFalse))) ||
!MRI.hasOneNonDBGUse(RHS))
return false;
}
- MachineInstr *SelectLHS = MRI.getVRegDef(Select->getOperand(2).getReg());
- MachineInstr *SelectRHS = MRI.getVRegDef(Select->getOperand(3).getReg());
+ MachineInstr *SelectLHS, *SelectRHS;
+ if (!mi_match(SelectTrue, MRI, m_MInstr(SelectLHS)) ||
+ !mi_match(SelectFalse, MRI, m_MInstr(SelectRHS)))
+ return false;
if (!isConstantOrConstantVector(*SelectLHS, MRI,
/*AllowFP*/ true,
@@ -4085,7 +4085,10 @@ bool CombinerHelper::matchFoldBinOpIntoSelect(MachineInstr &MI,
if (CanFoldNonConst)
return true;
- return isConstantOrConstantVector(*MRI.getVRegDef(OtherOperandReg), MRI,
+ MachineInstr *OtherOperandDef;
+ if (!mi_match(OtherOperandReg, MRI, m_MInstr(OtherOperandDef)))
+ return false;
+ return isConstantOrConstantVector(*OtherOperandDef, MRI,
/*AllowFP*/ true,
/*AllowOpaqueConstants*/ false);
}
@@ -4097,11 +4100,12 @@ void CombinerHelper::applyFoldBinOpIntoSelect(
Register Dst = MI.getOperand(0).getReg();
Register LHS = MI.getOperand(1).getReg();
Register RHS = MI.getOperand(2).getReg();
- MachineInstr *Select = MRI.getVRegDef(MI.getOperand(SelectOperand).getReg());
+ GSelect *Select =
+ cast<GSelect>(MRI.getVRegDef(MI.getOperand(SelectOperand).getReg()));
- Register SelectCond = Select->getOperand(1).getReg();
- Register SelectTrue = Select->getOperand(2).getReg();
- Register SelectFalse = Select->getOperand(3).getReg();
+ Register SelectCond = Select->getCondReg();
+ Register SelectTrue = Select->getTrueReg();
+ Register SelectFalse = Select->getFalseReg();
LLT Ty = MRI.getType(Dst);
unsigned BinOpcode = MI.getOpcode();
@@ -4542,7 +4546,9 @@ void CombinerHelper::applyExtendThroughPhis(MachineInstr &MI,
SmallDenseMap<MachineInstr *, MachineInstr *, 8> OldToNewSrcMap;
for (unsigned I = 0; I < PHI.getNumIncomingValues(); ++I) {
auto SrcReg = PHI.getIncomingValue(I);
- auto *SrcMI = MRI.getVRegDef(SrcReg);
+ MachineInstr *SrcMI;
+ if (!mi_match(SrcReg, MRI, m_MInstr(SrcMI)))
+ continue;
if (!SrcMIs.insert(SrcMI))
continue;
@@ -4592,10 +4598,13 @@ bool CombinerHelper::matchExtractVecEltBuildVec(MachineInstr &MI,
// Check if we have a build_vector or build_vector_trunc with an optional
// trunc i...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/216671
More information about the llvm-commits
mailing list