Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 3f84050

Browse files
author
Max Kazantsev
committed
[SCEV][NFC] Factor out common logic into a separate method
SCEV has multiple occurences of code when we need to prove some predicate on every iteration of a loop and do it with invocations of couple `isLoopEntryGuardedByCond`, `isLoopBackedgeGuardedByCond`. This patch factors out these two calls into a separate method. It is a preparation step to extend this logic: it is not the only way how we can prove such conditions. Differential Revision: https://reviews.llvm.org/D43373 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325745 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 634abdc commit 3f84050

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

include/llvm/Analysis/ScalarEvolution.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,11 @@ class ScalarEvolution {
834834
bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
835835
const SCEV *RHS);
836836

837+
/// Test if the condition described by Pred, LHS, RHS is known to be true on
838+
/// every iteration of the loop of the recurrency LHS.
839+
bool isKnownOnEveryIteration(ICmpInst::Predicate Pred,
840+
const SCEVAddRecExpr *LHS, const SCEV *RHS);
841+
837842
/// Return true if, for all loop invariant X, the predicate "LHS `Pred` X"
838843
/// is monotonically increasing or decreasing. In the former case set
839844
/// `Increasing` to true and in the latter case set `Increasing` to false.

lib/Analysis/ScalarEvolution.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,9 +1732,7 @@ ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
17321732
const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
17331733
getUnsignedRangeMax(Step));
17341734
if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
1735-
(isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
1736-
isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
1737-
AR->getPostIncExpr(*this), N))) {
1735+
isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) {
17381736
// Cache knowledge of AR NUW, which is propagated to this
17391737
// AddRec.
17401738
const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
@@ -1749,9 +1747,7 @@ ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
17491747
const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
17501748
getSignedRangeMin(Step));
17511749
if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
1752-
(isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) &&
1753-
isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
1754-
AR->getPostIncExpr(*this), N))) {
1750+
isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR, N)) {
17551751
// Cache knowledge of AR NW, which is propagated to this
17561752
// AddRec. Negative step causes unsigned wrap, but it
17571753
// still can't self-wrap.
@@ -1994,9 +1990,7 @@ ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
19941990
getSignedOverflowLimitForStep(Step, &Pred, this);
19951991
if (OverflowLimit &&
19961992
(isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
1997-
(isLoopEntryGuardedByCond(L, Pred, Start, OverflowLimit) &&
1998-
isLoopBackedgeGuardedByCond(L, Pred, AR->getPostIncExpr(*this),
1999-
OverflowLimit)))) {
1993+
isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
20001994
// Cache knowledge of AR NSW, then propagate NSW to the wide AddRec.
20011995
const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
20021996
return getAddRecExpr(
@@ -8677,17 +8671,16 @@ bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
86778671
if (LAR) {
86788672
const Loop *L = LAR->getLoop();
86798673
if (isAvailableAtLoopEntry(RHS, L) &&
8680-
isLoopEntryGuardedByCond(L, Pred, LAR->getStart(), RHS) &&
8681-
isLoopBackedgeGuardedByCond(L, Pred, LAR->getPostIncExpr(*this), RHS)) {
8674+
isKnownOnEveryIteration(Pred, LAR, RHS)) {
86828675
if (!RAR) return true;
86838676
LeftGuarded = true;
86848677
}
86858678
}
86868679
if (RAR) {
86878680
const Loop *L = RAR->getLoop();
8681+
auto SwappedPred = ICmpInst::getSwappedPredicate(Pred);
86888682
if (isAvailableAtLoopEntry(LHS, L) &&
8689-
isLoopEntryGuardedByCond(L, Pred, LHS, RAR->getStart()) &&
8690-
isLoopBackedgeGuardedByCond(L, Pred, LHS, RAR->getPostIncExpr(*this))) {
8683+
isKnownOnEveryIteration(SwappedPred, RAR, LHS)) {
86918684
if (!LAR) return true;
86928685
RightGuarded = true;
86938686
}
@@ -8702,6 +8695,14 @@ bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
87028695
return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS);
87038696
}
87048697

8698+
bool ScalarEvolution::isKnownOnEveryIteration(ICmpInst::Predicate Pred,
8699+
const SCEVAddRecExpr *LHS,
8700+
const SCEV *RHS) {
8701+
const Loop *L = LHS->getLoop();
8702+
return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) &&
8703+
isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS);
8704+
}
8705+
87058706
bool ScalarEvolution::isMonotonicPredicate(const SCEVAddRecExpr *LHS,
87068707
ICmpInst::Predicate Pred,
87078708
bool &Increasing) {

0 commit comments

Comments
 (0)