Issue 237702
Summary [IndVarSimplify] Congruent IV elimination keeps `nsw` on the wide increment when the narrow IV only matches its truncation
Labels miscompilation, loopoptim, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/fTG8M84fY
alive: https://alive2.llvm.org/ce/z/EKGSpr

```llvm
target triple = "x86_64-unknown-linux-gnu"

@g = global i32 0

define void @f() {
entry:
  br label %loop

loop:
  %w = phi i16 [ 32767, %entry ], [ %w.next, %latch ]
  %i = phi i8 [ -1, %entry ], [ %i.next, %latch ]
  %n = phi i32 [ 0, %entry ], [ %n.next, %latch ]
  %z = icmp eq i8 %i, 0
  br i1 %z, label %hit, label %latch

hit:
  store i32 42, ptr @g
  br label %latch

latch:
  %w.next = add nsw i16 %w, 1
  %i.next = add nsw i8 %i, 1
  %n.next = add i32 %n, 1
  %c = icmp eq i32 %n.next, 2
  br i1 %c, label %exit, label %loop

exit:
  ret void
}

define i32 @main() {
 call void @f()
  %r = load i32, ptr @g
  ret i32 %r
}
```

```console
$ opt -S -passes=indvars issue-repro.ll
...
define void @f() {
entry:
  br label %loop

loop: ; preds = %latch, %entry
  %w = phi i16 [ 32767, %entry ], [ %w.next, %latch ]
  %n = phi i32 [ 0, %entry ], [ %n.next, %latch ]
  %indvars1 = trunc i16 %w to i8
  %z = icmp eq i8 %indvars1, 0
  br i1 %z, label %hit, label %latch

hit: ; preds = %loop
  store i32 42, ptr @g, align 4
 br label %latch

latch:                                            ; preds = %hit, %loop
  %w.next = add nuw nsw i16 %w, 1
  %n.next = add nuw nsw i32 %n, 1
  %c = icmp eq i32 %n.next, 2
  br i1 %c, label %exit, label %loop

exit:                                             ; preds = %latch
 ret void
}
...

$ alive-tv --disable-undef-input --disable-poison-input -passes=indvars --src-unroll=3 --tgt-unroll=3 issue-repro.ll
...
Transformation doesn't verify!

ERROR: Source is more defined than target

Example:

Source:
  >> Jump to %loop
i16 %w = #x7fff (32767)
i8 %i = #xff (255, -1)
i32 %n = #x00000000 (0)
i1 %z = #x0 (0)
  >> Jump to %latch
i16 %w.next = poison
i8 %i.next = #x00 (0)
i32 %n.next = #x00000001 (1)
i1 %c = #x0 (0)
  >> Jump to %loop#2
i16 %w#2 = poison
i8 %i#2 = #x00 (0)
i32 %n#2 = #x00000001 (1)
i1 %z#2 = #x1 (1)
 >> Jump to %hit#2
  >> Jump to %latch#2
i16 %w.next#2 = poison
i8 %i.next#2 = #x01 (1)
i32 %n.next#2 = #x00000002 (2)
i1 %c#2 = #x1 (1)
  >> Jump to %exit

...

Target:
  >> Jump to %loop
i16 %w = #x7fff (32767)
i32 %n = #x00000000 (0)
i8 %indvars1 = #xff (255, -1)
i1 %z = #x0 (0)
  >> Jump to %latch
i16 %w.next = poison
i32 %n.next = #x00000001 (1)
i1 %c = #x0 (0)
  >> Jump to %loop#2
i16 %w#2 = poison
i32 %n#2 = #x00000001 (1)
i8 %indvars1#2 = poison
i1 %z#2 = poison
UB triggered on br

...
Summary:
  1 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors

$ cat issue-repro-control.ll
target triple = "x86_64-unknown-linux-gnu"

@g = global i32 0

define void @f() {
entry:
  br label %loop

loop:
  %w = phi i16 [ 32767, %entry ], [ %w.next, %latch ]
  %i = phi i8 [ -1, %entry ], [ %i.next, %latch ]
  %n = phi i32 [ 0, %entry ], [ %n.next, %latch ]
  %z = icmp eq i8 %i, 0
  br i1 %z, label %hit, label %latch

hit:
  store i32 42, ptr @g
  br label %latch

latch:
  %w.next = add i16 %w, 1
  %i.next = add nsw i8 %i, 1
 %n.next = add i32 %n, 1
  %c = icmp eq i32 %n.next, 2
  br i1 %c, label %exit, label %loop

exit:
  ret void
}

define i32 @main() {
  call void @f()
  %r = load i32, ptr @g
  ret i32 %r
}

$ opt -S -passes=indvars issue-repro-control.ll
...
define void @f() {
entry:
  br label %loop

loop:                                             ; preds = %latch, %entry
  %w = phi i16 [ 32767, %entry ], [ %w.next, %latch ]
  %n = phi i32 [ 0, %entry ], [ %n.next, %latch ]
  %indvars1 = trunc i16 %w to i8
  %z = icmp eq i8 %indvars1, 0
  br i1 %z, label %hit, label %latch

hit:                                              ; preds = %loop
 store i32 42, ptr @g, align 4
  br label %latch

latch: ; preds = %hit, %loop
  %w.next = add nuw i16 %w, 1
  %n.next = add nuw nsw i32 %n, 1
  %c = icmp eq i32 %n.next, 2
  br i1 %c, label %exit, label %loop

exit: ; preds = %latch
  ret void
}
...
```

### suggested fix
`SCEVExpander::replaceCongruentIVInc` restores `nsw`/`nuw` through `BothHaveNSW`/`BothHaveNUW` after `hoistIVInc(..., RecomputePoisonFlags=true)` recomputes them.
Truncation congruence does not establish that the wide increment is no more poisonous: here the i16 increment overflows before the i8 increment.
Remove the `BothHaveNSW`/`BothHaveNUW` restoration on this successful-hoist path and retain the flags recomputed by `hoistIVInc`; any selective preservation needs to prove that the actual wide increment cannot become poison earlier.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to