[Mlir-commits] [mlir] [mlir][arith] Fold addi(x, not(x)) -> -1 (PR #212272)
Victor Perez
llvmlistbot at llvm.org
Tue Jul 28 06:41:11 PDT 2026
victor-eds wrote:
> How about generalize these and use `^ -1` hoisting:
>
> ```ocaml
> addi(not(x), y) -> not(subi(x, y))
> addi(x, not(y)) -> not(subi(y, x))
> subi(not(x), y) -> not(addi(x, y))
> ```
>
> and for special case when `x == y` next iteration will fold it to `-1`:
>
> ```ocaml
> not(subi(x, x)) -> not(0) -> -1
> not(subi(x, x)) -> not(0) -> -1
> ```
>
> Using hoisting unlocks more optimization DRRs like:
>
> ```ocaml
> addi(subi(not(x), c0), c1) ->
> addi(not(addi(x, c0)), c1) ->
> not(subi(addi(x, c0), c1)) -> not(addi(x, c0 - c1))
> ```
Thanks for the suggestion! I like it: hoisting the `^ -1` outward unlocks the further optimizations like the `addi(subi(not(x), c0), c1)` chain.
The one caveat is that those hoisting rewrites create new operations: `addi(not(x), y) -> not(subi(x, y))` has to materialize a `subi` and a `xori`. So they belong as canonicalization patterns, not in the folder. What this PR adds is a pure fold, which will be applied in more contexts.
I'd suggest we land this fold as-is and do the `^ -1` hoisting as a follow-up canonicalization set.
Sound good?
https://github.com/llvm/llvm-project/pull/212272
More information about the Mlir-commits
mailing list