[PATCH] D153963: [InstCombine] Fold binop of select and cast of select condition

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 10 09:04:32 PDT 2023


goldstein.w.n added a comment.

In D153963#4485400 <https://reviews.llvm.org/D153963#4485400>, @antoniofrighetto wrote:

> In D153963#4482931 <https://reviews.llvm.org/D153963#4482931>, @goldstein.w.n wrote:
>
>> So I'm going to propose something slightly different for accomplishing this patch.
>> Instead of making a combine for binops, I think you can just arbitrarily
>> constant propagate any usage of `cond` to the true/false arms.
>
> I guess with `TrueVal->Operands()` we actually mean the (unique) user of `TrueVal`? I'm not exactly following how we would benefit from this, where the operands of the select are constants in most of our use cases, here's just an example:

No I mean the actual operands. The point is the select condition can be constant propagated to instruction on the true/false arm (usually). You can't do this for instructions that are not speculatable (although same is true for binops in your current patch).

>   define i64 @src(i1 %c) {
>     %sel = select i1 %c, i64 64, i64 1
>     %ext = zext i1 %c to i64
>     %add = add i64 %sel, %ext
>     ret i64 %add
>   }
>
> Conversely, wouldn't the proposed change be trying to catch the following orthogonal test case?
>
>   define i64 @src(i1 %c, i64 %a, i64 %b, i64 %d) {
>     %ext = zext i1 %c to i64
>     %add = add i64 %a, %ext
>     %sub = sub i64 %b, %ext
>     %sel = select i1 %c, i64 %add, i64 %sub
>     ret i64 %sel
>   }
>
> Where `%add` and `%sub` may be optimized respectively with `%1 = add i64 %a, 1` and  `%2 = sub i64 %b, 0`.

Yes, what I'm proposing covers this case, it just doesn't stop at binops.
Basically for any instruction that we may speculate that uses the condition (or `(ext %condition)`)
we can just arbitrarily replace `%condition` with `1`/`0` depending if its from the true/false
arm.

So instead of matching BinOps, just take any speculatable instruction, iterate through its
operands and replace all references to `%condition` with the constant. Its just a more
general implementation.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153963/new/

https://reviews.llvm.org/D153963



More information about the llvm-commits mailing list