[llvm] [InstSimplify] Simplify select if it combinated `and/or/xor` (PR #73362)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 10 01:29:05 PST 2024


ParkHanbum wrote:

> > @dtcxzyw I commented on the comment above, can you see it? It says (pending) on my screen, but I don't know why, but it may mean that it hasn't been uploaded. So I'll reply here again.
> > I think it is necessary. because Select ICmp is must be "X (comp) Y". here is example:
> > ```
> > define i32 @src_select_xor_max_positive_int(i32 %x, i32 %y) {
> > ; CHECK-LABEL: @src_select_xor_max_positive_int(
> > ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
> > ; CHECK-NEXT:    [[XOR0:%.*]] = icmp eq i32 [[XOR]], 2147483647
> > ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], [[Y]]
> > ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[X]], [[Y]]
> > ; CHECK-NEXT:    [[COND:%.*]] = select i1 [[XOR0]], i32 [[AND]], i32 [[OR]]
> > ; CHECK-NEXT:    ret i32 [[COND]]
> > ;
> >   %xor = xor i32 %x, %y
> >   %xor0 = icmp eq i32 %xor, 2147483647
> >   %and = and i32 %x, %y
> >   %or = or i32 %x, %y
> >   %cond = select i1 %xor0, i32 %and, i32 %or
> >   ret i32 %cond
> > }
> > ```
> > 
> > 
> >     
> >       
> >     
> > 
> >       
> >     
> > 
> >     
> >   
> > without `if (match(CmpLHS, m_Value(X)) && match(CmpRHS, m_Value(Y))) {` condition then `%and = and i32 %x, %y %or = or i32 %x, %y` will match case.
> > am I right?
> 
> `match(CmpLHS, m_Value(X))` is equivalent to the following code :(
> 
> ```
> bool match(...) {
>   X = CmpLHS;
>   return true;
> }

Required to specify X and Y.

In the example above, CmpLHS is '%xor' and CmpRHS is '2147483647'. The operands of 'and' and 'or' are '%x, %y'.

`X==Y ? X&Y : I thought I needed `match(CmpLHS, m_Value(X)) && match(CmpRHS, m_Value(Y))` to check if X|Y` matches X|Y`.

then can check whether the operands of `icmp eq`, `and`, and `or` are all the same as `%x, %y`.


Yes, it is. 

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


More information about the llvm-commits mailing list