Skip to content

Commit aa10137

Browse files
committed
[SCEV] Factor out utility for proving same sign of two SCEVs [nfc]
This is a slightly different API than ConstantRange's areInsensitiveToSignednessOfICmpPredicate. The only actual difference (beyond naming) is the handling of empty ranges (i.e. unreachable code). I wanted to keep the existing SCEV behavior for the unreachable code as we should be folding that to poison, not reasoning about samesign. The new API will be reused in llvm#170363. I'll rebase whichever lands second over the first to land.
1 parent 49a9787 commit aa10137

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,9 @@ class ScalarEvolution {
10781078
isKnownMultipleOf(const SCEV *S, uint64_t M,
10791079
SmallVectorImpl<const SCEVPredicate *> &Assumptions);
10801080

1081+
/// Return true if we know that S1 and S2 must have the same sign.
1082+
LLVM_ABI bool haveSameSign(const SCEV *S1, const SCEV *S2);
1083+
10811084
/// Splits SCEV expression \p S into two SCEVs. One of them is obtained from
10821085
/// \p S by substitution of all AddRec sub-expression related to loop \p L
10831086
/// with initial value of that SCEV. The second is obtained from \p S by

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11115,6 +11115,11 @@ bool ScalarEvolution::isKnownMultipleOf(
1111511115
return true;
1111611116
}
1111711117

11118+
bool ScalarEvolution::haveSameSign(const SCEV *S1, const SCEV *S2) {
11119+
return ((isKnownNonNegative(S1) && isKnownNonNegative(S2)) ||
11120+
(isKnownNegative(S1) && isKnownNegative(S2)));
11121+
}
11122+
1111811123
std::pair<const SCEV *, const SCEV *>
1111911124
ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) {
1112011125
// Compute SCEV on entry of loop L.
@@ -12034,8 +12039,7 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
1203412039
if (IsSignFlippedPredicate(Pred, FoundPred)) {
1203512040
// Unsigned comparison is the same as signed comparison when both the
1203612041
// operands are non-negative or negative.
12037-
if ((isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) ||
12038-
(isKnownNegative(FoundLHS) && isKnownNegative(FoundRHS)))
12042+
if (haveSameSign(FoundLHS, FoundRHS))
1203912043
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI);
1204012044
// Create local copies that we can freely swap and canonicalize our
1204112045
// conditions to "le/lt".

0 commit comments

Comments
 (0)