[PATCH] D51929: [DAGCombiner] use UADDO to optimize saturated unsigned add

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 21 10:09:23 PDT 2018


spatel added a subscriber: nlopes.
spatel added a comment.

In https://reviews.llvm.org/D51929#1241507, @lebedev.ri wrote:

> Do we somehow enforce that in `%r = select i1 %c, i32 -1, i32 %a`, `-1` is in the middle?
>  If not, we miss at least one case i think:


That's correct. I think there are also 4 swapped variants for the pattern with a variable and a 'not' op (online version of Alive looks dead? cc'ing @nlopes):

  Optimization: swap1
  Precondition: true
    %noty = xor i32 %y, -1
    %a = add %x, %y
    %c = icmp ugt i32 %x, %noty
    %r = select i1 %c, i32 -1, i32 %a
  =>
    %c2 = icmp ugt i32 %noty, %x
    %r = select i1 %c2, i32 %a, i32 -1
  
  Done: 1
  Optimization is correct!
  
  ----------------------------------------
  Optimization: swap2
  Precondition: true
    %noty = xor i32 %y, -1
    %a = add %x, %y
    %c = icmp ugt i32 %x, %noty
    %r = select i1 %c, i32 -1, i32 %a
  =>
    %c2 = icmp ult i32 %x, %noty
    %r = select i1 %c2, i32 %a, i32 -1
  
  Done: 1
  Optimization is correct!
  
  ----------------------------------------
  Optimization: swap3
  Precondition: true
    %noty = xor i32 %y, -1
    %a = add %x, %y
    %c = icmp ugt i32 %x, %noty
    %r = select i1 %c, i32 -1, i32 %a
  =>
    %c2 = icmp ult i32 %noty, %x
    %r = select i1 %c2, i32 -1, i32 %a
  
  Done: 1
  Optimization is correct!

My plan is to canonicalize all of the patterns in IR. If I'm seeing it correctly, there shouldn't be anything blocking those canonicalizations because we only try to form the uaddo here when the cmp (setcc) has one use (the select). That should let us get by with just the basic matching here in the backend. For example, the pattern with a variable should never reach the backend with a 'not' op because we can always shrink it in IR:

  %noty = xor i8 %y, -1
  %a = add i8 %x, %y
  %c = icmp ugt i8 %x, %noty
  %r = select i1 %c, i8 -1, i8 %a

>
=

  %a = add i8 %x, %y
  %c = icmp ugt i8 %x, %a
  %r = select i1 %c, i8 -1, i8 %a


https://reviews.llvm.org/D51929





More information about the llvm-commits mailing list