Issue 235798
Summary [CVP] Flags derived from an assume let InstCombine weaken that assume
Labels missed-optimization, llvm:transforms
Assignees
Reporter chfast
    ```c
void f(unsigned long *r, unsigned long n)
{
    if (n == 0 || n > 3)
 __builtin_unreachable();
    for (unsigned long i = 0; i < n - 1; ++i)
 r[i] += 1;
}
```

https://godbolt.org/z/xdfG4MzGK

The loop runs at most twice, but `clang -O2` vectorizes it. With `__builtin_assume(n != 0); __builtin_assume(n <= 3);` instead, it becomes two conditional increments. Here the assume `n - 1 u< 3` ends up as `n s< 4`, so `n - 1` is no longer known to be small. Reduced:

```llvm
declare void @use(i64)

define void @f(i64 %n) {
  %a = add i64 %n, -1
  %c = icmp ult i64 %a, 3
  call void @llvm.assume(i1 %c)
  call void @use(i64 %a)
  ret void
}
```

`opt -passes=correlated-propagation,instcombine` gives:

```llvm
  %a = add nsw i64 %n, -1
  %c = icmp slt i64 %n, 4
  call void @llvm.assume(i1 %c)
```

CVP takes the range [0, 3) of `%a` from the assume and adds `nsw` to `%a` and `samesign` to `%c`, the assume's own condition. InstCombine then folds `icmp samesign ult (add nsw %n, -1), 3` to `icmp slt %n, 4`, which drops `n >= 1`. Without the other use of `%a`, CVP leaves `%c` unchanged.

Found while reducing code from intx (multi-precision integers).

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

Reply via email to