[llvm] [InstCombine] Fold `select Cond, not X, X` into `Cond ^ X` (PR #93591)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed May 29 05:04:48 PDT 2024


dtcxzyw wrote:

> Missed optimization from cvc5: https://alive2.llvm.org/ce/z/s3a6rd
> 
> ```
> define i1 @src (i1 %cmp23, i1 %pol) {
>    %lnot = xor i1 %pol, true
>    call void @use(i1 %lnot)
>    %lnot.pol = select i1 %cmp23, i1 %lnot, i1 %pol
>    ret i1 %lnot.pol
> }
> 
> define i1 @tgt(i1 %cmp23, i1 %pol) {
>    %lnot = xor i1 %pol, true
>    call void @use(i1 %lnot)
>    %lnot.pol = xor i1 %cmp23, %pol
>    ret i1 %lnot.pol
> }
> 
> declare void @use(i1)
> ```

Without multi-use on xor: https://godbolt.org/z/TYxz1njab
```
INSTCOMBINE ITERATION #1 on src
ADD:   ret i1 %lnot.pol
ADD:   %lnot.pol = select i1 %cmp23, i1 %lnot, i1 %pol
ADD:   %lnot = xor i1 %pol, true
IC: Visiting:   %lnot = xor i1 %pol, true
IC: Visiting:   %lnot.pol = select i1 %cmp23, i1 %lnot, i1 %pol
ADD DEFERRED:   %1 = select i1 %cmp23, i1 true, i1 false
IC: Old =   %lnot.pol = select i1 %cmp23, i1 %1, i1 %pol
    New =   <badref> = xor i1 %pol, %lnot
ADD:   %lnot.pol = xor i1 %pol, %lnot
IC: ERASE   %2 = select i1 %cmp23, i1 %1, i1 %pol
ADD DEFERRED:   %1 = xor i1 %pol, true
IC: ERASE   %1 = xor i1 %pol, true
ADD:   %lnot = select i1 %cmp23, i1 true, i1 false
IC: Visiting:   %lnot = select i1 %cmp23, i1 true, i1 false
IC: Replacing   %lnot = select i1 %cmp23, i1 true, i1 false
    with i1 %cmp23
IC: Mod =   %lnot = select i1 %cmp23, i1 true, i1 false
    New =   %lnot = select i1 %cmp23, i1 true, i1 false
IC: ERASE   %lnot = select i1 %cmp23, i1 true, i1 false
IC: Visiting:   %lnot.pol = xor i1 %pol, %cmp23
IC: Visiting:   ret i1 %lnot.pol
```

As there is no way to remove one-use constraint in `InstCombinerImpl::foldSelectIntoOp`, should we handle this pattern in this PR?


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


More information about the llvm-commits mailing list