[llvm] 301fbf9 - [SCEV] Clarify the overflow precondition of computeMaxBECountForLT [NFC]

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 30 09:49:22 PDT 2021


Author: Philip Reames
Date: 2021-08-30T09:49:17-07:00
New Revision: 301fbf9b8115620480db851d091cbbb516cd983d

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

LOG: [SCEV] Clarify the overflow precondition of computeMaxBECountForLT [NFC]

And add a test case to illustrate that we do in fact produce the right result for the multiple exit case.  I have gotten myself confused at least three times when reading this code, so clarify to prevent future confusion.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/ScalarEvolution.h
    llvm/test/Analysis/ScalarEvolution/max-trip-count.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index a2b9b59ec2247..6ae865e011cf0 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -2043,8 +2043,11 @@ class ScalarEvolution {
   /// permitted by Start, End, and Stride. This is for loops of the form
   /// {Start, +, Stride} LT End.
   ///
-  /// Precondition: the induction variable is known to be positive.  We *don't*
-  /// assert these preconditions so please be careful.
+  /// Preconditions:
+  /// * the induction variable is known to be positive.
+  /// * the induction variable is assumed not to overflow (i.e. either it
+  ///   actually doesn't, or we'd have to immediately execute UB)
+  /// We *don't* assert these preconditions so please be careful.
   const SCEV *computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride,
                                      const SCEV *End, unsigned BitWidth,
                                      bool IsSigned);

diff  --git a/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll b/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
index 123543aee4ae6..75fd30c3f952a 100644
--- a/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
+++ b/llvm/test/Analysis/ScalarEvolution/max-trip-count.ll
@@ -456,8 +456,8 @@ loop.exit:
   ret void
 }
 
-define void @max_overflow(i8 %n) mustprogress {
-; CHECK-LABEL: Determining loop execution counts for: @max_overflow
+define void @max_overflow_se(i8 %n) mustprogress {
+; CHECK-LABEL: Determining loop execution counts for: @max_overflow_se
 ; CHECK: Loop %loop: backedge-taken count is (-126 + (126 smax %n))<nsw>
 ; CHECK: Loop %loop: max backedge-taken count is 0
 entry:
@@ -473,6 +473,33 @@ exit:
   ret void
 }
 
+; Show that we correctly realize that %i can overflow here as long as
+; the early exit is taken before we branch on poison.
+define void @max_overflow_me(i8 %n) mustprogress {
+; CHECK-LABEL: Determining loop execution counts for: @max_overflow_me
+; CHECK: Loop %loop: <multiple exits> Unpredictable backedge-taken count.
+; CHECK:   exit count for loop: 1
+; CHECK:   exit count for latch: ***COULDNOTCOMPUTE***
+; CHECK: Loop %loop: max backedge-taken count is 1
+entry:
+  br label %loop
+
+loop:
+  %i = phi i8 [ 63, %entry ], [ %i.next, %latch ]
+  %j = phi i8 [  0, %entry ], [ %j.next, %latch ]
+  %early.exit = icmp ne i8 %j, 1
+  br i1 %early.exit, label %latch, label %exit
+latch:
+  %i.next = add nsw i8 %i, 63
+  %j.next = add nsw nuw i8 %j, 1
+  %t = icmp slt i8 %i.next, %n
+  br i1 %t, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+
 ; Max backedge-taken count is zero.
 define void @bool_stride(i1 %s, i1 %n) mustprogress {
 ; CHECK-LABEL: Determining loop execution counts for: @bool_stride


        


More information about the llvm-commits mailing list