Skip to content

Commit 5419b67

Browse files
committed
[SimplifyCFG] Update FoldTwoEntryPHINode to handle and/or of select and binop equally
This is a minor change that fixes FoldTwoEntryPHINode to handle phis with and/ors of select form and binop form equally.
1 parent 06c5119 commit 5419b67

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,9 @@ m_LogicalAnd(const LHS &L, const RHS &R) {
24462446
return LogicalOp_match<LHS, RHS, Instruction::And>(L, R);
24472447
}
24482448

2449+
/// Matches L && R where L and R are arbitrary values.
2450+
inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
2451+
24492452
/// Matches L || R either in the form of L | R or L ? true : R.
24502453
/// Note that the latter form is poison-blocking.
24512454
template <typename LHS, typename RHS>

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,13 +2598,17 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
25982598
return match(V0, m_Not(m_Value())) && match(V1, Invertible);
25992599
};
26002600

2601-
// Don't fold i1 branches on PHIs which contain binary operators, unless one
2602-
// of the incoming values is an 'not' and another one is freely invertible.
2601+
// Don't fold i1 branches on PHIs which contain binary operators or
2602+
// select form of or/ands, unless one of the incoming values is an 'not' and
2603+
// another one is freely invertible.
26032604
// These can often be turned into switches and other things.
2605+
auto IsBinOpOrAnd = [](Value *V) {
2606+
return match(
2607+
V, m_CombineOr(m_BinOp(), m_CombineOr(m_LogicalAnd(), m_LogicalOr())));
2608+
};
26042609
if (PN->getType()->isIntegerTy(1) &&
2605-
(isa<BinaryOperator>(PN->getIncomingValue(0)) ||
2606-
isa<BinaryOperator>(PN->getIncomingValue(1)) ||
2607-
isa<BinaryOperator>(IfCond)) &&
2610+
(IsBinOpOrAnd(PN->getIncomingValue(0)) ||
2611+
IsBinOpOrAnd(PN->getIncomingValue(1)) || IsBinOpOrAnd(IfCond)) &&
26082612
!CanHoistNotFromBothValues(PN->getIncomingValue(0),
26092613
PN->getIncomingValue(1)))
26102614
return Changed;

0 commit comments

Comments
 (0)