[llvm] [InstCombine] Canonicalize more saturated-add variants (PR #100008)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 29 14:33:09 PDT 2024


topperc wrote:

> > Your alive proof has
> > 
> > 
> > 
> > ```
> > 
> > define dso_local i8 @src3(i8 %x, i8  %c) {
> > 
> > entry:
> > 
> >   %noZero = icmp ne i8 %c, 0
> > 
> >   call void @llvm.assume(i1 %noZero)
> > 
> >   %add = add nuw i8 %x, %c
> > 
> >   %c.not = xor i8 %c, -1
> > 
> >   %d = add i8 %c.not, 1
> > 
> >   %cmp.not = icmp ugt i8 %x, %d
> > 
> >   %cond = select i1 %cmp.not, i8 -1, i8 %add
> > 
> >   ret i8 %cond
> > 
> > }
> > 
> > ```
> > 
> > 
> > 
> > The `nuw` flag there says that the add never overflows and thus never saturates. The transform fails without the nuw. There's no check for the nuw in your transform.
> 
> This shouldn't be in my proof. I apologize: it's actually malformed IR.
> 
> nuw is impossible for x ugt -C ? x + C : -1;

Why would it be malformed IR? The select would pick -1 in the case of overflow and the add would produce poison. The selects blocks the poison from propagating by picking -1. That's completely valid IR

https://github.com/llvm/llvm-project/pull/100008


More information about the llvm-commits mailing list