[InstCombine] Optimize compares with multiple selects as operands
authorTejas Joshi <TejasSanjay.Joshi@amd.com>
Fri, 26 May 2023 14:02:22 +0000 (16:02 +0200)
committerNikita Popov <npopov@redhat.com>
Fri, 26 May 2023 14:05:32 +0000 (16:05 +0200)
commitc75a0f5a9a368b6ca3ec0a696f2a934e8dd0e5bb
tree0732860ca137127b5632c5d649021f7f5fb785bf
parent75cd8cdbde45d07800d5cefa6f7a213271245e4a
[InstCombine] Optimize compares with multiple selects as operands

In case of a comparison with two select instructions having the same
condition, check whether one of the resulting branches can be simplified.
If so, just compare the other branch and select the appropriate result.
For example:

    %tmp1 = select i1 %cmp, i32 %y, i32 %x
    %tmp2 = select i1 %cmp, i32 %z, i32 %x
    %cmp2 = icmp slt i32 %tmp2, %tmp1

The icmp will result false for the false value of selects and the result
will depend upon the comparison of true values of selects if %cmp is
true. Thus, transform this into:

    %cmp = icmp slt i32 %y, %z
    %sel = select i1 %cond, i1 %cmp, i1 false

Differential Revision: https://reviews.llvm.org/D150360
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-with-selects.ll