Skip to content

Commit 05b4f76

Browse files
committed
[InstCombine] Eliminate icmp+zext pairs over phis more aggressively
1 parent c630e13 commit 05b4f76

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,12 +1822,29 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN,
18221822
continue;
18231823
}
18241824

1825-
// If the only use of phi is comparing it with a constant then we can
1826-
// put this comparison in the incoming BB directly after a ucmp/scmp call
1827-
// because we know that it will simplify to a single icmp.
1828-
const APInt *Ignored;
1829-
if (isa<CmpIntrinsic>(InVal) && InVal->hasOneUser() &&
1830-
match(&I, m_ICmp(m_Specific(PN), m_APInt(Ignored)))) {
1825+
// Handle some cases that can't be fully simplified, but where we know that
1826+
// the two instructions will fold into one.
1827+
auto WillFold = [&]() {
1828+
if (!InVal->hasOneUser())
1829+
return false;
1830+
1831+
// icmp of ucmp/scmp with constant will fold to icmp.
1832+
const APInt *Ignored;
1833+
if (isa<CmpIntrinsic>(InVal) &&
1834+
match(&I, m_ICmp(m_Specific(PN), m_APInt(Ignored))))
1835+
return true;
1836+
1837+
// icmp eq zext(bool), 0 will fold to !bool.
1838+
if (isa<ZExtInst>(InVal) &&
1839+
cast<ZExtInst>(InVal)->getSrcTy()->isIntOrIntVectorTy(1) &&
1840+
match(&I,
1841+
m_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(PN), m_Zero())))
1842+
return true;
1843+
1844+
return false;
1845+
};
1846+
1847+
if (WillFold()) {
18311848
OpsToMoveUseToIncomingBB.push_back(i);
18321849
NewPhiValues.push_back(nullptr);
18331850
continue;

0 commit comments

Comments
 (0)