llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[SCEV] Missed fold for descending loop IV range check `n - 1 - i >= 0`

Open 134ARG opened this issue 6 months ago • 2 comments

This is a reduced probe for a source pattern seen in SPEC CPU2017 538.imagick_r, magick/quantize.c:FloydSteinbergDither. The original code scans a row with 0 <= x < image->columns; on odd serpentine rows it computes u = image->columns - 1 - x and uses u as a row-local index. The reduced test below turns the scalar range fact into an explicit check so that the missed optimization is observable.

int descending_iv_bound(long n, long *out) {
  int bad = 0;
  for (long i = 0; i < n; i++) {
    long u = n - 1 - i;
    if (u < 0 || u >= n)
      bad = 1;
    // preserved for side effect
    out[i] = u;
  }
  return bad;
}

For every executed iteration, 0 <= i < n, so u = n - 1 - i is in [0, n). The function should therefore return 0.

Current behavior (https://godbolt.org/z/GMsb7GfaP): opt -passes=indvars keeps the lower-bound comparison and bad-flag select, which could be optimized away:

%cmp = icmp sgt i64 %u, -1
%bad.next = select i1 %cmp, i32 %bad, i32 1
ret i32 %bad.lcssa

But SCEV already has enough structural information to derive the endpoint range:

# opt -disable-output -passes='print<scalar-evolution>'

# variable i
  %i.012 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
  -->  {0,+,1}<nuw><nsw><%for.body> U: [0,9223372036854775807) S: [0,9223372036854775807)		Exits: (-1 + %n)		LoopDispositions: { %for.body: Computable }
  
# variable u
  %sub1 = add nsw i64 %n, %0
  -->  {(-1 + %n),+,-1}<nw><%for.body> U: full-set S: full-set		Exits: 0		LoopDispositions: { %for.body: Computable }

Loop %for.body: backedge-taken count is (-1 + %n)

It also explicitly reports the exit value of %u as 0. This should be enough for optimizing the u < 0 check away and making the return a constant 0.

134ARG avatar Jun 02 '26 09:06 134ARG

opt -disable-output -scalar-evolution-use-expensive-range-sharpening -passes='print<scalar-evolution>' /tmp/llvm-201064.ll
Printing analysis 'Scalar Evolution Analysis' for function 'descending_iv_bound':
Classifying expressions for: @descending_iv_bound
  %i = phi i64 [ 0, %entry ], [ %inc, %loop ]
  -->  {0,+,1}<nuw><nsw><%loop> U: [0,9223372036854775807) S: [0,9223372036854775807)           Exits: (-1 + %n)          LoopDispositions: { %loop: Computable }
  %bad = phi i32 [ 0, %entry ], [ %bad.next, %loop ]
  -->  %bad U: [0,2) S: [0,2)           Exits: <<Unknown>>              LoopDispositions: { %loop: Variant }
  %u0 = add nsw i64 %n, -1
  -->  (-1 + %n) U: full-set S: full-set                Exits: (-1 + %n)                LoopDispositions: { %loop: Invariant }
  %u = sub nsw i64 %u0, %i
  -->  {(-1 + %n),+,-1}<nw><%loop> U: [0,9223372036854775807) S: [0,9223372036854775807)                Exits: 0          LoopDispositions: { %loop: Computable }
  %bad.next = select i1 %nonneg, i32 %bad, i32 1
  -->  %bad.next U: [0,2) S: [0,2)              Exits: <<Unknown>>              LoopDispositions: { %loop: Variant }
  %p = getelementptr i64, ptr %out, i64 %i
  -->  {%out,+,8}<%loop> U: full-set S: full-set                Exits: (-8 + (8 * %n) + %out)           LoopDispositions: { %loop: Computable }
  %inc = add nuw nsw i64 %i, 1
  -->  {1,+,1}<nuw><nsw><%loop> U: [1,-9223372036854775808) S: [1,-9223372036854775808)         Exits: %nLoopDispositions: { %loop: Computable }
  %r = phi i32 [ 0, %entry ], [ %bad.next, %loop ]
  -->  %r U: [0,2) S: [0,2)
Determining loop execution counts for: @descending_iv_bound
Loop %loop: backedge-taken count is (-1 + %n)
Loop %loop: constant max backedge-taken count is i64 9223372036854775806
Loop %loop: symbolic max backedge-taken count is (-1 + %n)
Loop %loop: Trip multiple is 1

This appears to be handled in SCEV's optional expensive range sharpening

robertvirany avatar Jun 02 '26 20:06 robertvirany

I just realized I may overlook that. Since we already have the needed analysis results for the optimization in the SCEV without expensive-range-sharpening, it might be a missed pattern that deserves a fix?

134ARG avatar Jun 04 '26 06:06 134ARG