[PATCH] D101722: [SCEV] Don't require ControlsExit for gt/lt NoWrap

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 9 20:58:51 PDT 2021


reames added a subscriber: test.
reames added a comment.

Here is what I think an example of a bug in the current computeBECount.  Overflow is complicated, so I might be wrong here.  Sanity check me?

define void @test(i8 %n) {
entry:

  br label %loop


loop:

  %iv = phi i8 [0, %entry], [%iv.next, %loop]
  %iv.next = add nuw i8 %iv, 127
  %cmp = icmp ult i8 %iv, 254
  br i1 %cmp, label %loop, label %exit


exit:

  ret void

}

SCEV computes a backedge taken count for this loop of zero.  I believe this is incorrect, and that this loop actually exits on the third iteration without executing UB.

On iteration #1:
%iv = 0
%iv.next = 127
%cmp = true

On iteration #2:
%iv = 127
%iv.next = 254
%cmp = true

On iteration #3:
%iv = 254
%iv.next = poison
%cmp = false

If we replace the exit test of 254 with an unknown %n, we can see why the result is wrong.  We get:
((126 + %n) /u 127)

I believe this is the simplified form of:
((%n - 0 + 127 - 1) /u 127)

Which is what howManyLessThans + computeBECount should produce on this example if I'm reading it correctly.

Anything I'm missing here?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101722/new/

https://reviews.llvm.org/D101722



More information about the llvm-commits mailing list