Skip to content

Commit 1b60236

Browse files
authored
[SimplifyCFG] Avoid redundant calls in gather. (NFC) (#154133)
Split out from #154007 as it showed compile time improvements NFC as there needs to be at least two icmps that is part of the chain.
1 parent 4a9d038 commit 1b60236

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ struct ConstantComparesGatherer {
565565
/// Number of comparisons matched in the and/or chain
566566
unsigned UsedICmps = 0;
567567

568+
/// If the elements in Vals matches the comparisons
569+
bool IsEq = false;
570+
568571
/// Construct and compute the result for the comparison instruction Cond
569572
ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
570573
gather(Cond);
@@ -736,23 +739,23 @@ struct ConstantComparesGatherer {
736739
/// vector.
737740
/// One "Extra" case is allowed to differ from the other.
738741
void gather(Value *V) {
739-
bool isEQ = match(V, m_LogicalOr(m_Value(), m_Value()));
740-
742+
Value *Op0, *Op1;
743+
if (match(V, m_LogicalOr(m_Value(Op0), m_Value(Op1))))
744+
IsEq = true;
745+
else if (match(V, m_LogicalAnd(m_Value(Op0), m_Value(Op1))))
746+
IsEq = false;
747+
else
748+
return;
741749
// Keep a stack (SmallVector for efficiency) for depth-first traversal
742-
SmallVector<Value *, 8> DFT;
743-
SmallPtrSet<Value *, 8> Visited;
744-
745-
// Initialize
746-
Visited.insert(V);
747-
DFT.push_back(V);
750+
SmallVector<Value *, 8> DFT{Op0, Op1};
751+
SmallPtrSet<Value *, 8> Visited{V, Op0, Op1};
748752

749753
while (!DFT.empty()) {
750754
V = DFT.pop_back_val();
751755

752756
if (Instruction *I = dyn_cast<Instruction>(V)) {
753757
// If it is a || (or && depending on isEQ), process the operands.
754-
Value *Op0, *Op1;
755-
if (isEQ ? match(I, m_LogicalOr(m_Value(Op0), m_Value(Op1)))
758+
if (IsEq ? match(I, m_LogicalOr(m_Value(Op0), m_Value(Op1)))
756759
: match(I, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
757760
if (Visited.insert(Op1).second)
758761
DFT.push_back(Op1);
@@ -763,7 +766,7 @@ struct ConstantComparesGatherer {
763766
}
764767

765768
// Try to match the current instruction
766-
if (matchInstruction(I, isEQ))
769+
if (matchInstruction(I, IsEq))
767770
// Match succeed, continue the loop
768771
continue;
769772
}
@@ -5103,6 +5106,7 @@ bool SimplifyCFGOpt::simplifyBranchOnICmpChain(BranchInst *BI,
51035106
Value *CompVal = ConstantCompare.CompValue;
51045107
unsigned UsedICmps = ConstantCompare.UsedICmps;
51055108
Value *ExtraCase = ConstantCompare.Extra;
5109+
bool TrueWhenEqual = ConstantCompare.IsEq;
51065110

51075111
// If we didn't have a multiply compared value, fail.
51085112
if (!CompVal)
@@ -5112,8 +5116,6 @@ bool SimplifyCFGOpt::simplifyBranchOnICmpChain(BranchInst *BI,
51125116
if (UsedICmps <= 1)
51135117
return false;
51145118

5115-
bool TrueWhenEqual = match(Cond, m_LogicalOr(m_Value(), m_Value()));
5116-
51175119
// There might be duplicate constants in the list, which the switch
51185120
// instruction can't handle, remove them now.
51195121
array_pod_sort(Values.begin(), Values.end(), constantIntSortPredicate);

0 commit comments

Comments
 (0)