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

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 29 14:59:37 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
> 
> LLVM would NEVER put nuw for x + C if x ugt -C was true.
> 
> Let C1 be the positive interpretation of -C in two's compliment.
> 
> x ugt C1 being true means x is at (unsigned least) C1 + 1. But, C + C1 would wrap. meaning X + C MUST wrap if X ugt -C.

Ignoring the undef input possibility, alive2 says its valid to apply nuw. https://alive2.llvm.org/ce/z/sPStwP

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


More information about the llvm-commits mailing list