[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 07:03:46 PST 2022


bcl5980 created this revision.
bcl5980 added reviewers: spatel, liaolucy.
Herald added a subscriber: hiraditya.
Herald added a project: All.
bcl5980 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

(X || Y) ? X : Y --> X
https://alive2.llvm.org/ce/z/oRQJee


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
@@ -250,9 +250,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
@@ -262,9 +260,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
@@ -274,9 +270,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
@@ -286,9 +280,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
@@ -298,9 +290,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
@@ -310,9 +300,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) && X --> false (commuted 2 ways)
       if (match(TrueVal, m_ZeroInt()) && (X == FalseVal || Y == FalseVal))
         return TrueVal;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138815.478233.patch
Type: text/x-patch
Size: 3362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221128/2c5e918f/attachment.bin>


More information about the llvm-commits mailing list