This is an archive of the discontinued LLVM Phabricator instance.

[X86] Teach "(select (and (x , 0x1) == 0), y, (z | y) ) -> (-(and (x , 0x1)) & z ) | y" to also handle the case where there is no setcc and the and is used directly with the args swapped
AbandonedPublic

Authored by craig.topper on Jun 21 2018, 5:15 PM.

Details

Summary

It seems SimplifySetcc likes to turns (i1 (setcc ne (and x, 1), 1)) into (i1 (setcc eq (i1 (trunc x)), 0) which becomes (i1 (xor (i1 (trunc x)), 1)). If its used by a select, the xor gets removed by swapping the true/false values. Then type legalization comes along and turns the trunc into (i8 (and x, 1)). So in the end we have no setcc.

I attempted to make (i1 (setcc eq (and x, 1), 0)) also produce (i1 (xor (i1 (trunc x)), 1) so they would be the same, but discovered that broke this code.

This patch teachs the code in LowerSELECT to handle this alternate possibility. I moved the code above the call to LowerSETCC because I don't think we need to do that before doing this check.

Ultimately, this is step towards improving this code. Which currently produces a BT and SETAE instruction. But produces arithmetic/logic operations if you use "icmp ne i64 %a17, 1" or if you use "icmp ne i64 %a17, 0" and swap the select operands.

define void @foo(i64 %x, i32 %c.0282.in, i32 %d.0280, i32* %ptr0, i32* %ptr1) {
    %c.0282 = and i32 %c.0282.in, 268435455
    %a16 = lshr i64 32508, %x
    %a17 = and i64 %a16, 1
    %tobool = icmp eq i64 %a17, 0
    %. = select i1 %tobool, i32 1, i32 2
    %.286 = select i1 %tobool, i32 27, i32 26
    %shr97 = lshr i32 %c.0282, %.
    %shl98 = shl i32 %c.0282.in, %.286
    %or99 = or i32 %shr97, %shl98
    %shr100 = lshr i32 %d.0280, %.
    %shl101 = shl i32 %d.0280, %.286
    %or102 = or i32 %shr100, %shl101
    store i32 %or99, i32* %ptr0
    store i32 %or102, i32* %ptr1
    ret void
}

Diff Detail

Event Timeline

craig.topper created this revision.Jun 21 2018, 5:15 PM

This is the complete patch. The previous one was relative to an intermediate step.

I was working on a generic DAGCombiner patch for what I think are the early patterns in the motivating case:
https://rise4fun.com/Alive/Gsyp

...but we might need multiple patches to fix this up. I'll post what I have and see what you think

This revision was not accepted when it landed; it landed in state Needs Review.Jun 24 2018, 7:43 AM
This revision was automatically updated to reflect the committed changes.
spatel reopened this revision.Jun 24 2018, 7:46 AM

Oops - I included the wrong link in the commit message for rL335433; that was meant to correspond to D48508.

Not sure how to make Phab select the previous version of the patch.
It's showing the patch from rL335433 now, which is not what we want of course.
Did D48508 affect any of the tests or motivation for this one?

craig.topper abandoned this revision.Jun 24 2018, 11:12 PM

D48508 fixed part my motivating case and I think there are other things we can do to fix the remaining issue. Abandoning this.