[llvm] [InstCombine] Canonicalize (a + 1 == 0) ? -1 : a + 1 -> uadd.sat(a, 1) (PR #144566)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 17 12:32:08 PDT 2025


topperc wrote:

> > > > I don't think this is the right place to fix this. We need to rewrite
> > > > ```
> > > >   %add = add i32 %num, 1
> > > >   %cmp = icmp eq i32 %add, 0
> > > > ```
> > > > 
> > > > 
> > > >     
> > > >       
> > > >     
> > > > 
> > > >       
> > > >     
> > > > 
> > > >     
> > > >   
> > > > to
> > > > ```
> > > >   %cmp = icmp eq i32 %num, -1
> > > > ```
> > > > 
> > > > 
> > > >     
> > > >       
> > > >     
> > > > 
> > > >       
> > > >     
> > > > 
> > > >     
> > > >   
> > > > Then the rest just works.
> > > > unsigned square2eer(unsigned a) {
> > > > return a + 7 == 7;
> > > > }
> > > 
> > > 
> > > folds fine, but when we have multiple uses, it is not simplifying because it is reusing the a + 7
> > 
> > 
> > I don't understand why we're willing to fold an `ugt` compare when the add has multiple uses, but not an `eq` compare.
> 
> Beats me, this is just my observation.

There's a FIXME for it in `InstCombinerImpl::foldICmpBinOpEqualityWithConstant`

```
  case Instruction::Add: {                                                       
    // (A + C2) == C --> A == (C - C2)                                           
    // (A + C2) != C --> A != (C - C2)                                           
    // TODO: Remove the one-use limitation? See discussion in D58633
```

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


More information about the llvm-commits mailing list