[llvm] [InstSimplify] Fold `X < Y ? (X + zext(X < Y)) <= Y : false` to `X < Y` (PR #118579)

via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 14 18:07:50 PST 2024


veera-sivarajan wrote:

> Make sure that `isImpliedPoison((X + zext(X < Y)) <= Y, X < Y)` returns true

The motivating case for this optimization is:
```
  %c1 = icmp ult i8 %x, %y
  %z.neg = zext i1 %c1 to i8
  %add = add nuw i8 %x, %z.neg
  %c2 = icmp ule i8 %add, %y
  %and = select i1 %c1, i1 %c2, i1 false
  ret i1 %and
```

In this case, the call to `impliesPoison(y, x)` will look like `impliesPoison(%c2, %c1)`. This will return `false` as `%c2` can be `poison` (due to `nuw` in %add) when `%c1` is `true`.

This is because `impliesPoison(y, x)`, by definition, cannot prove poison safety when `y = poison` and `x = true` even though transforming the `select` to `and` is correct[^1].

So I don't know how to make `impliesPoison(y, x)` return `true` for the optimizing case. Can I hard code a generic version of the motivating case in `impliesPoisonOrCond()`?

[^1]: https://aqjune.github.io/posts/2021-10-4.the-select-story.html


https://github.com/llvm/llvm-project/pull/118579


More information about the llvm-commits mailing list