Skip to content

Commit 07f0dde

Browse files
committed
generalize dom cond helpers
1 parent 9ac82f0 commit 07f0dde

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -706,45 +706,57 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
706706
}
707707
}
708708

709-
static void computeKnownBitsFromCond(const Value *V, Value *Cond,
710-
KnownBits &Known, unsigned Depth,
711-
const SimplifyQuery &SQ, bool Invert) {
709+
using InformationFn = function_ref<void(const Value *, bool)>;
710+
711+
static void computeInformationFromCond(const Value *V, Value *Cond,
712+
unsigned Depth, const SimplifyQuery &SQ,
713+
InformationFn Fn, bool Invert) {
712714
Value *A, *B;
713715
if (Depth < MaxAnalysisRecursionDepth &&
714716
(Invert ? match(Cond, m_LogicalOr(m_Value(A), m_Value(B)))
715717
: match(Cond, m_LogicalAnd(m_Value(A), m_Value(B))))) {
716-
computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, Invert);
717-
computeKnownBitsFromCond(V, B, Known, Depth + 1, SQ, Invert);
718+
computeInformationFromCond(V, A, Depth + 1, SQ, Fn, Invert);
719+
computeInformationFromCond(V, B, Depth + 1, SQ, Fn, Invert);
718720
}
719721

720-
if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
721-
computeKnownBitsFromCmp(
722-
V, Invert ? Cmp->getInversePredicate() : Cmp->getPredicate(),
723-
Cmp->getOperand(0), Cmp->getOperand(1), Known, SQ);
722+
Fn(Cond, Invert);
724723
}
725724

726-
void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
727-
unsigned Depth, const SimplifyQuery &Q) {
728-
if (!Q.CxtI)
729-
return;
730-
725+
static void computeInformationFromDominatingConditions(const Value *V,
726+
unsigned Depth,
727+
const SimplifyQuery &Q,
728+
InformationFn Fn) {
731729
if (Q.DC && Q.DT) {
732730
// Handle dominating conditions.
733731
for (BranchInst *BI : Q.DC->conditionsFor(V)) {
734732
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
735733
if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
736-
computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
737-
/*Invert*/ false);
734+
computeInformationFromCond(V, BI->getCondition(), Depth, Q, Fn,
735+
/*Invert*/ false);
738736

739737
BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
740738
if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
741-
computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
742-
/*Invert*/ true);
739+
computeInformationFromCond(V, BI->getCondition(), Depth, Q, Fn,
740+
/*Invert*/ true);
743741
}
744-
745-
if (Known.hasConflict())
746-
Known.resetAll();
747742
}
743+
}
744+
745+
void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
746+
unsigned Depth, const SimplifyQuery &Q) {
747+
if (!Q.CxtI)
748+
return;
749+
750+
computeInformationFromDominatingConditions(
751+
V, Depth, Q, [&Known, &Q, V](const Value *Cond, bool Invert) {
752+
if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
753+
computeKnownBitsFromCmp(
754+
V, Invert ? Cmp->getInversePredicate() : Cmp->getPredicate(),
755+
Cmp->getOperand(0), Cmp->getOperand(1), Known, Q);
756+
});
757+
758+
if (Known.hasConflict())
759+
Known.resetAll();
748760

749761
if (!Q.AC)
750762
return;

0 commit comments

Comments
 (0)