[llvm] caf92a8 - [ValueTracking] Handle non-zero shl recurrence

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 26 10:43:03 PDT 2021


Author: Nikita Popov
Date: 2021-03-26T18:39:06+01:00
New Revision: caf92a8a92abcd09051e036521cdc89d16e8866d

URL: https://github.com/llvm/llvm-project/commit/caf92a8a92abcd09051e036521cdc89d16e8866d
DIFF: https://github.com/llvm/llvm-project/commit/caf92a8a92abcd09051e036521cdc89d16e8866d.diff

LOG: [ValueTracking] Handle non-zero shl recurrence

In this case we don't care about the step at all, and only require
that the starting value is non-zero.

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Analysis/ValueTracking/monotonic-phi.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 8e7858db38fd..1c3d5df21907 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2220,19 +2220,23 @@ static bool isNonZeroRecurrence(const PHINode *PN) {
   Value *Start = nullptr, *Step = nullptr;
   const APInt *StartC, *StepC;
   if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
-      !match(Start, m_APInt(StartC)) || !match(Step, m_APInt(StepC)))
+      !match(Start, m_APInt(StartC)))
     return false;
 
   switch (BO->getOpcode()) {
   case Instruction::Add:
-    return (BO->hasNoUnsignedWrap() && !StartC->isNullValue() &&
-            !StepC->isNullValue()) ||
-           (BO->hasNoSignedWrap() && StartC->isStrictlyPositive() &&
-            StepC->isNonNegative());
+    return match(Step, m_APInt(StepC)) &&
+           ((BO->hasNoUnsignedWrap() && !StartC->isNullValue() &&
+             !StepC->isNullValue()) ||
+            (BO->hasNoSignedWrap() && StartC->isStrictlyPositive() &&
+             StepC->isNonNegative()));
   case Instruction::Mul:
-    return !StartC->isNullValue() &&
+    return !StartC->isNullValue() && match(Step, m_APInt(StepC)) &&
            ((BO->hasNoUnsignedWrap() && !StepC->isNullValue()) ||
             (BO->hasNoSignedWrap() && StepC->isStrictlyPositive()));
+  case Instruction::Shl:
+    return !StartC->isNullValue() &&
+           (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap());
   default:
     return false;
   }

diff  --git a/llvm/test/Analysis/ValueTracking/monotonic-phi.ll b/llvm/test/Analysis/ValueTracking/monotonic-phi.ll
index 43d39f50d3b0..e79e949401dd 100644
--- a/llvm/test/Analysis/ValueTracking/monotonic-phi.ll
+++ b/llvm/test/Analysis/ValueTracking/monotonic-phi.ll
@@ -363,8 +363,7 @@ define i1 @test_shl_nuw(i8 %p, i8* %pq, i8 %n) {
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
 ; CHECK-NEXT:    br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
 ; CHECK:       exit:
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], 0
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   br label %loop
@@ -388,8 +387,7 @@ define i1 @test_shl_nsw(i8 %p, i8* %pq, i8 %n) {
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
 ; CHECK-NEXT:    br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
 ; CHECK:       exit:
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], 0
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   br label %loop
@@ -413,8 +411,7 @@ define i1 @test_shl_dynamic_shift(i8 %p, i8* %pq, i8 %n, i8 %shift) {
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
 ; CHECK-NEXT:    br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
 ; CHECK:       exit:
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], 0
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 false
 ;
 entry:
   br label %loop


        


More information about the llvm-commits mailing list