Skip to content

Commit 5ae5d25

Browse files
committed
[ValueTracking] match negative-stepping non-zero recurrence
This is pulled out of D100408. This avoids a regression that would be exposed by making the calling code from InstSimplify more efficient.
1 parent 856c49d commit 5ae5d25

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,8 +2213,8 @@ static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value)
22132213
return true;
22142214
}
22152215

2216-
/// Try to detect a recurrence that monotonically increases from a non-zero
2217-
/// starting value. These are common as induction variables.
2216+
/// Try to detect a recurrence that monotonically increases/decreases from a
2217+
/// non-zero starting value. These are common as induction variables.
22182218
static bool isNonZeroRecurrence(const PHINode *PN) {
22192219
BinaryOperator *BO = nullptr;
22202220
Value *Start = nullptr, *Step = nullptr;
@@ -2225,10 +2225,13 @@ static bool isNonZeroRecurrence(const PHINode *PN) {
22252225

22262226
switch (BO->getOpcode()) {
22272227
case Instruction::Add:
2228+
// Starting from non-zero and stepping away from zero can never wrap back
2229+
// to zero.
2230+
// TODO: The constant step requirement is not needed with NUW.
22282231
return match(Step, m_APInt(StepC)) &&
22292232
((BO->hasNoUnsignedWrap() && !StepC->isNullValue()) ||
2230-
(BO->hasNoSignedWrap() && StartC->isStrictlyPositive() &&
2231-
StepC->isNonNegative()));
2233+
(BO->hasNoSignedWrap() &&
2234+
StartC->isNegative() == StepC->isNegative()));
22322235
case Instruction::Mul:
22332236
return match(Step, m_APInt(StepC)) &&
22342237
((BO->hasNoUnsignedWrap() && !StepC->isNullValue()) ||

llvm/unittests/Analysis/ValueTrackingTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ TEST_F(ValueTrackingTest, isNonZeroRecurrence) {
11911191
)");
11921192
DataLayout DL = M->getDataLayout();
11931193
AssumptionCache AC(*F);
1194-
EXPECT_FALSE(isKnownNonZero(A, DL, 0, &AC, CxtI));
1194+
EXPECT_TRUE(isKnownNonZero(A, DL, 0, &AC, CxtI));
11951195
}
11961196

11971197
TEST_F(ValueTrackingTest, KnownNonZeroFromDomCond) {

0 commit comments

Comments
 (0)