[llvm] [InstCombine] Use the select condition to try to constant fold binops into select (PR #84696)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 11 08:13:01 PDT 2024


goldsteinn wrote:

> > ```
> > define i32 @before(i1 %cond) {
> >   %sel = select i1 %cond, i32 32, i32 0
> >   call void @use(i32 %sel)
> >   %ret = or disjoint i32 %sel, 16
> >   ret i32 %ret
> > }
> > 
> > define i32 @after(i1 %cond) {
> >   %sel = select i1 %cond, i32 32, i32 0
> >   call void @use(i32 %sel)
> >   %ret = select i1 %cond, i32 48, i32 16
> >   ret i32 %ret
> > }
> > ```
> > 
> > 
> >     
> >       
> >     
> > 
> >       
> >     
> > 
> >     
> >   
> > I don't think the latter form is better than the former. Do we need the one-use check?
> 
> Yeah, Although I would would think this is somewhat instruction dependent. I.e `div`/`mul`, I would probably prefer the constant folded select, `or`/`and`, the logic op.
> 
> Ill update with a flag for if we can accept more than 1 use.

I updated to add a oneuse check, but thinking a bit more on it, I think we generally benefit
from this fold even for multi-use.
We don't really lose any information, so would think the right approach would be teach the
backend to undo this and make the the canonical form in the middle-end.

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


More information about the llvm-commits mailing list