[PATCH] D33172: [InstCombine] Simpify inverted predicates in 'or'

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 09:21:19 PDT 2017


spatel added a comment.

In https://reviews.llvm.org/D33172#755138, @davide wrote:

> > Swapping select operands to eliminate an inverted compare seems like a generally good fold for some other pass too. Would that be GVN?
>
> Not entirely sure I follow this last one.
>  NewGVN finds whether two expressions e1 and e2 are equivalent (actually, not general equivalence as checking equivalence of program expressions is undecidable, so we approximate with Herbrand equivalence). If you have an example (IR), that will help (and I can take a look).




  declare i32 @bar(i32, i32)
  
  define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) {
    %cmp = icmp slt i32 %a, %b
    %sel1 = select i1 %cmp, i32 %c, i32 0
    %cmpnot = icmp sge i32 %a, %b
    %sel2 = select i1 %cmpnot, i32 %d, i32 0
    %call = tail call i32 @bar(i32 %sel1, i32 %sel2)
    ret i32 %call
  }

I think this should be canonicalized to:

  define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) {
    %cmp = icmp slt i32 %a, %b
    %sel1 = select i1 %cmp, i32 %c, i32 0
    %sel2 = select i1 %cmp, i32 0,  i32 %d
    %call = tail call i32 @bar(i32 %sel1, i32 %sel2)
    ret i32 %call
  }

The general problem is that we should recognize when a compare value used by a select already exists in inverted form. If it does, swap the select operands and eliminate the inverted usage.


https://reviews.llvm.org/D33172





More information about the llvm-commits mailing list