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

chenglin.bi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 29 18:11:26 PST 2022


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

add TODO for test


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
@@ -382,9 +382,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
@@ -394,9 +392,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
@@ -406,9 +402,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
@@ -418,9 +412,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
@@ -430,9 +422,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
@@ -442,15 +432,15 @@
 
 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
   ret <2 x i1> %r
 }
 
+; TODO: this could transform to X
+; (X || Y) ? X : Y --> X
 
 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(
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4555,6 +4555,10 @@
     if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
       return Cond;
 
+    // (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()))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138815.478775.patch
Type: text/x-patch
Size: 3615 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221130/97d7e599/attachment.bin>


More information about the llvm-commits mailing list