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

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 5 13:28:08 PDT 2021


nikic added a comment.

In D101722#2739927 <https://reviews.llvm.org/D101722#2739927>, @reames wrote:

> I think this code may simply be too strict in general.  Let me explain my reasoning.
>
> For each exit, we are only computing the exit count for the loop *if and only if* that exit is taken.  The fact that the loop might exit through another exit previously is handled by the fact we take the min of all exit counts.

This is correct at a high level, but the details are a bit subtle. In particular, exactly because we take the min of all exit counts, we cannot under-estimate the exit count of an exit that is never taken.

As I didn't do a good job of explaining this, let's take the example from PR28012:

  loop:
    %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
    %iv.inc = add nsw i32 %iv, 3
    call void @may_exit()
    %becond = icmp ne i32 %iv.inc, 46
    br i1 %becond, label %loop, label %leave

Reporting `46 u/ 3` (which does not divide evenly) as the exit count would be incorrect. `@may_exit()` could bail out somewhere after iteration `46 /u 3`, but before the iteration where `nsw` causes poison. To perform poison -> UB reasoning in this specific case, we can't have any exits (normal or abnormal) before the branch.

The same issue doesn't apply to a `ult` predicate, because we can't end up under-estimating the exit count. We definitely can't do more iterations than that exit count (if latch dominating), though we could do less (through a different exit).

> We have an invariant in this code that all cached exit counts are for exits which dominate the backedge (and thus are guaranteed not to be bypassed).

I think we do compute all exit counts, but don't use the non-dominating ones when calculating the loop trip count (and also exclude them in various indvars optimizations).


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