Issue 234550
Summary [LoopBoundSplit] Exit compare with the IV on the right-hand side gets the IV replaced by the new bound
Labels miscompilation, loopoptim, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/h38aaaYz4
alive: https://alive2.llvm.org/ce/z/WddbMT

```llvm
define i32 @main() {
entry:
  br label %loop

loop:
  %i = phi i8 [ 0, %entry ], [ %inc, %latch ]
  %count = phi i32 [ 0, %entry ], [ %count.next, %latch ]
  %cond = icmp ult i8 %i, 20
  br i1 %cond, label %then, label %else

then:
 %add = add i32 %count, 1
  br label %latch

else:
  br label %latch

latch:
  %count.next = phi i32 [ %add, %then ], [ %count, %else ]
  %inc = add i8 %i, 1
  %exit = icmp ugt i8 10, %i
  br i1 %exit, label %loop, label %done

done:
  ret i32 %count.next
}
```

```console
$ opt -S -passes=loop-bound-split repro.ll
...
loop:
  %i = phi i8 [ 0, %entry.split ], [ %inc, %latch ]
  %count = phi i32 [ 0, %entry.split ], [ %count.next, %latch ]
  %cond = icmp ult i8 %i, 20
  br i1 true, label %then, label %else
...
latch:
  %count.next = phi i32 [ %add, %then ], [ %count, %else ]
  %inc = add i8 %i, 1
  %exit = icmp ugt i8 10, 10
  br i1 %exit, label %loop, label %entry.split.split
...

$ llubi repro.ll; echo $?
11
$ opt -S -passes=loop-bound-split repro.ll | llubi -; echo $?
1

$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=16 --tgt-unroll=16 -passes=loop-bound-split repro.ll
...
Transformation doesn't verify!

ERROR: Value mismatch
...
Source value: #x0000000b (11)
Target value: #x00000001 (1)
```

### suggested fix
When the recurrence is the RHS of the icmp, `hasProcessableCondition` swaps `Cond.AddRecValue`/`Cond.BoundValue` and the predicate, but `splitLoopBound` then always does `ExitingCond.ICmp->setOperand(1, NewBoundValue)`, which for `icmp ugt i8 10, %i` overwrites the IV.
Replace the operand that actually holds `ExitingCond.BoundValue`, e.g. record its index in `ConditionInfo` when swapping, or, when the operands were swapped, call `ExitingCond.ICmp->swapOperands()` before the `setOperand(1, ...)`.

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

Reply via email to