[PATCH] D138815: [InstSimplify] Fold (X || Y) ? X : Y --> X

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 29 09:03:11 PST 2022


spatel accepted this revision.
spatel added a comment.
This revision is now accepted and ready to land.

LGTM



================
Comment at: llvm/lib/Analysis/InstructionSimplify.cpp:4558
 
+    // (X || Y) ? X : Y --> X (commuted 2 ways)
+    if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
----------------
bcl5980 wrote:
> Hi @spatel , which way do you think is better here?
> 
> 
> ```
>     // (X || Y) ? X : Y --> X (commuted 2 ways)
>     if (match(Cond, m_c_LogicalOr(m_Specific(TrueVal), m_Specific(FalseVal))))
>       return TrueVal;
> 
>     // (X || Y) ? false : X --> false (commuted 2 ways)
>     if (match(Cond, m_c_LogicalOr(m_Specific(FalseVal), m_Value())) &&
>         match(TrueVal, m_ZeroInt()))
>       return ConstantInt::getFalse(Cond->getType());
> ```
> or
> 
> ```
>     if (match(Cond, m_LogicalOr(m_Value(X), m_Value(Y)))) {
>       // (X || Y) ? X : Y --> X
>       if ((X == TrueVal && Y == FalseVal) || (X == FalseVal && Y == TrueVal))
>         return TrueVal;
> 
>       // (X || Y) ? false : X --> false (commuted 2 ways)
>       if (match(TrueVal, m_ZeroInt()) && (X == FalseVal || Y == FalseVal))
>         return ConstantInt::getFalse(Cond->getType());
>     }
> 
> 
> ```
The current code (using m_Specific) is a little easier to read to me.


================
Comment at: llvm/test/Transforms/InstSimplify/select-logical.ll:443
 
 define <2 x i1> @select_or_same_op_vector2_poison(<2 x i1> %x, <2 x i1> %y) {
 ; CHECK-LABEL: @select_or_same_op_vector2_poison(
----------------
This could fold, but we don't match it currently, right? Add a TODO comment to be consistent with the previous patches.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138815/new/

https://reviews.llvm.org/D138815



More information about the llvm-commits mailing list