[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.
This commit is contained in:
Philip Reames 2021-08-30 09:47:45 -07:00
parent 1724a16437
commit 301fbf9b81
2 changed files with 34 additions and 4 deletions

View File

@ -2043,8 +2043,11 @@ private:
/// 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);

View File

@ -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