Skip to content

Commit b3a1500

Browse files
committed
[SCEV][NFC] API for predicate evaluation
Provides API that allows to check predicate for being true or false with one call. Current implementation is naive and just calls isKnownPredicate twice, but further we can rework this logic trying to use one check to prove both facts.
1 parent b790443 commit b3a1500

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,11 +938,24 @@ class ScalarEvolution {
938938
bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
939939
const SCEV *RHS);
940940

941+
/// Check whether the condition described by Pred, LHS, and RHS is true or
942+
/// false. If we know it, return the evaluation of this condition. If neither
943+
/// is proved, return None.
944+
Optional<bool> evaluatePredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
945+
const SCEV *RHS);
946+
941947
/// Test if the given expression is known to satisfy the condition described
942948
/// by Pred, LHS, and RHS in the given Context.
943949
bool isKnownPredicateAt(ICmpInst::Predicate Pred, const SCEV *LHS,
944950
const SCEV *RHS, const Instruction *Context);
945951

952+
/// Check whether the condition described by Pred, LHS, and RHS is true or
953+
/// false in the given \p Context. If we know it, return the evaluation of
954+
/// this condition. If neither is proved, return None.
955+
Optional<bool> evaluatePredicateAt(ICmpInst::Predicate Pred, const SCEV *LHS,
956+
const SCEV *RHS,
957+
const Instruction *Context);
958+
946959
/// Test if the condition described by Pred, LHS, RHS is known to be true on
947960
/// every iteration of the loop of the recurrency LHS.
948961
bool isKnownOnEveryIteration(ICmpInst::Predicate Pred,

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9534,6 +9534,16 @@ bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
95349534
return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS);
95359535
}
95369536

9537+
Optional<bool> ScalarEvolution::evaluatePredicate(ICmpInst::Predicate Pred,
9538+
const SCEV *LHS,
9539+
const SCEV *RHS) {
9540+
if (isKnownPredicate(Pred, LHS, RHS))
9541+
return true;
9542+
else if (isKnownPredicate(ICmpInst::getInversePredicate(Pred), LHS, RHS))
9543+
return false;
9544+
return None;
9545+
}
9546+
95379547
bool ScalarEvolution::isKnownPredicateAt(ICmpInst::Predicate Pred,
95389548
const SCEV *LHS, const SCEV *RHS,
95399549
const Instruction *Context) {
@@ -9542,6 +9552,23 @@ bool ScalarEvolution::isKnownPredicateAt(ICmpInst::Predicate Pred,
95429552
isBasicBlockEntryGuardedByCond(Context->getParent(), Pred, LHS, RHS);
95439553
}
95449554

9555+
Optional<bool>
9556+
ScalarEvolution::evaluatePredicateAt(ICmpInst::Predicate Pred, const SCEV *LHS,
9557+
const SCEV *RHS,
9558+
const Instruction *Context) {
9559+
Optional<bool> KnownWithoutContext = evaluatePredicate(Pred, LHS, RHS);
9560+
if (KnownWithoutContext)
9561+
return KnownWithoutContext;
9562+
9563+
if (isBasicBlockEntryGuardedByCond(Context->getParent(), Pred, LHS, RHS))
9564+
return true;
9565+
else if (isBasicBlockEntryGuardedByCond(Context->getParent(),
9566+
ICmpInst::getInversePredicate(Pred),
9567+
LHS, RHS))
9568+
return false;
9569+
return None;
9570+
}
9571+
95459572
bool ScalarEvolution::isKnownOnEveryIteration(ICmpInst::Predicate Pred,
95469573
const SCEVAddRecExpr *LHS,
95479574
const SCEV *RHS) {

0 commit comments

Comments
 (0)