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

Davide Italiano via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 11:12:35 PDT 2017


davide added a comment.

In https://reviews.llvm.org/D33172#755146, @spatel wrote:

> 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
>   }
>  
>


If you really want canonicalize (and that needs to be evaluated yet), I think you might consider instead canonicalizing to:

  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 slt i32 %a, %b
    %sel2 = select i1 %cmpnot, i32 0, i32 %d
    %call = tail call i32 @bar(i32 %sel1, i32 %sel2)
    ret i32 %call
  }

and then let NewGVN discover that `%cmpnot` and `%cmp` are actually in the same congruence class (GVN will also get to the same conclusion, but it doesn't have a real analysis/notion of congruence class).


https://reviews.llvm.org/D33172





More information about the llvm-commits mailing list