[llvm-dev] [RFC] Canonicalization of unsigned subtraction with saturation

Koval, Julia via llvm-dev llvm-dev at lists.llvm.org
Tue May 16 04:30:55 PDT 2017


Hi,

This message is a result of a discussion of backend optimization for sub(max) pattern(https://reviews.llvm.org/D25987), which can be either converted to unsigned min-max or unsigned saturation instruction(if the target supports it).
Currently these versions of the code produce different IR(and we need to manage both types in backend):

(1.16)
void foo(unsigned short *p, unsigned short max, int n) {
  int i;
unsigned short m;
for (i = 0; i < n; i++) {
    m = *--p;
    *p =(m >= max ? m-max : 0);
  }
}

(2.16)
void goo(unsigned short *p, unsigned short max, int n) {
  int i;
  unsigned short m;
  for (i = 0; i < n; i++) {
    m = *--p;
    unsigned short umax = m > max ? m : max;
    *p =umax - max;
  }
}

(1.16)
%cmp = icmp ugt i16 %x, %y
%sub2 = sub i16 %y, %x
%res = select i1 %cmp, i16 0, i16 %sub2
 
or 

(2.16)
%cmp = icmp ugt i16 %x, %y
%sel = select i1 %cmp, i16 %x, i16 %y
%sub = sub i16 %sel, %x

Which of these versions is canonical? I think first version is better, because it can be converted to unsigned saturation instruction(i.e. PSUBUS), using existing backend code. 


More information about the llvm-dev mailing list