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

chenglin.bi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 28 17:42:48 PST 2022


bcl5980 updated this revision to Diff 478428.
bcl5980 added a comment.

rebase code


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

https://reviews.llvm.org/D138815

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/select-logical.ll


Index: llvm/test/Transforms/InstSimplify/select-logical.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/select-logical.ll
+++ llvm/test/Transforms/InstSimplify/select-logical.ll
@@ -381,9 +381,7 @@
 
 define i1 @select_or_same_op(i1 %x, i1 %y) {
 ; CHECK-LABEL: @select_or_same_op(
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]]
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 [[X:%.*]]
 ;
   %or = or i1 %x, %y
   %r = select i1 %or, i1 %x, i1 %y
@@ -393,9 +391,7 @@
 
 define i1 @select_or_same_op_commute(i1 %x, i1 %y) {
 ; CHECK-LABEL: @select_or_same_op_commute(
-; CHECK-NEXT:    [[OR:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[OR]], i1 [[Y]], i1 [[X]]
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 [[Y:%.*]]
 ;
   %or = or i1 %x, %y
   %r = select i1 %or, i1 %y, i1 %x
@@ -405,9 +401,7 @@
 
 define <2 x i1> @select_or_same_op_vector1(<2 x i1> %x, <2 x i1> %y) {
 ; CHECK-LABEL: @select_or_same_op_vector1(
-; CHECK-NEXT:    [[OR:%.*]] = or <2 x i1> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> [[X]], <2 x i1> [[Y]]
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> [[X:%.*]]
 ;
   %or = or <2 x i1> %x, %y
   %r = select <2 x i1> %or, <2 x i1> %x, <2 x i1> %y
@@ -417,9 +411,7 @@
 
 define i1 @select_logic_or1_same_op(i1 %x, i1 %y) {
 ; CHECK-LABEL: @select_logic_or1_same_op(
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]]
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 [[X:%.*]]
 ;
   %or = select i1 %x, i1 true, i1 %y
   %r = select i1 %or, i1 %x, i1 %y
@@ -429,9 +421,7 @@
 
 define i1 @select_logic_or2_same_op(i1 %x, i1 %y) {
 ; CHECK-LABEL: @select_logic_or2_same_op(
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[Y:%.*]], i1 true, i1 [[X:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[OR]], i1 [[X]], i1 [[Y]]
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 [[X:%.*]]
 ;
   %or = select i1 %y, i1 true, i1 %x
   %r = select i1 %or, i1 %x, i1 %y
@@ -441,9 +431,7 @@
 
 define <2 x i1> @select_or_same_op_vector2(<2 x i1> %x, <2 x i1> %y) {
 ; CHECK-LABEL: @select_or_same_op_vector2(
-; CHECK-NEXT:    [[OR:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[OR]], <2 x i1> [[X]], <2 x i1> [[Y]]
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> [[X:%.*]]
 ;
   %or = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %y
   %r = select <2 x i1> %or, <2 x i1> %x, <2 x i1> %y
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4557,6 +4557,10 @@
 
     Value *X, *Y;
     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());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138815.478428.patch
Type: text/x-patch
Size: 3399 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221129/5f440e2c/attachment.bin>


More information about the llvm-commits mailing list