[llvm] 6c232db - [InstSimplify] fold selects where true/false arm is the same as condition

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 30 06:17:36 PST 2022


Author: Sanjay Patel
Date: 2022-12-30T08:54:09-05:00
New Revision: 6c232db2ae933ad8f53cc39c8387fdceef7e8542

URL: https://github.com/llvm/llvm-project/commit/6c232db2ae933ad8f53cc39c8387fdceef7e8542
DIFF: https://github.com/llvm/llvm-project/commit/6c232db2ae933ad8f53cc39c8387fdceef7e8542.diff

LOG: [InstSimplify] fold selects where true/false arm is the same as condition

We managed to fold related patterns in issue #59704,
but we were missing these more basic folds:
https://alive2.llvm.org/ce/z/y6d7SN

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 78b493d478823..48abf50b1117d 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4605,6 +4605,23 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
   if (TrueVal == FalseVal)
     return TrueVal;
 
+  if (Cond == TrueVal) {
+    // select i1 X, i1 X, i1 false --> X (logical-and)
+    if (match(FalseVal, m_ZeroInt()))
+      return Cond;
+    // select i1 X, i1 X, i1 true --> true
+    if (match(FalseVal, m_One()))
+      return ConstantInt::getTrue(Cond->getType());
+  }
+  if (Cond == FalseVal) {
+    // select i1 X, i1 true, i1 X --> X (logical-or)
+    if (match(TrueVal, m_One()))
+      return Cond;
+    // select i1 X, i1 false, i1 X --> false
+    if (match(TrueVal, m_ZeroInt()))
+      return ConstantInt::getFalse(Cond->getType());
+  }
+
   // If the true or false value is poison, we can fold to the other value.
   // If the true or false value is undef, we can fold to the other value as
   // long as the other value isn't poison.

diff  --git a/llvm/test/Transforms/InstSimplify/select-logical.ll b/llvm/test/Transforms/InstSimplify/select-logical.ll
index 1d1ee58eb939c..09109eb1c4fe5 100644
--- a/llvm/test/Transforms/InstSimplify/select-logical.ll
+++ b/llvm/test/Transforms/InstSimplify/select-logical.ll
@@ -556,8 +556,7 @@ define i1 @select_and_same_op_negative(i1 %x, i1 %y, i1 %z) {
 
 define i1 @and_same_op(i1 %x) {
 ; CHECK-LABEL: @and_same_op(
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[X:%.*]], i1 [[X]], i1 false
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 [[X:%.*]]
 ;
   %r = select i1 %x, i1 %x, i1 false
   ret i1 %r
@@ -565,8 +564,7 @@ define i1 @and_same_op(i1 %x) {
 
 define <2 x i1> @or_same_op(<2 x i1> %x) {
 ; CHECK-LABEL: @or_same_op(
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[X]]
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> [[X:%.*]]
 ;
   %r = select <2 x i1> %x, <2 x i1> <i1 true, i1 true>, <2 x i1> %x
   ret <2 x i1> %r
@@ -574,8 +572,7 @@ define <2 x i1> @or_same_op(<2 x i1> %x) {
 
 define <2 x i1> @always_true_same_op(<2 x i1> %x) {
 ; CHECK-LABEL: @always_true_same_op(
-; CHECK-NEXT:    [[R:%.*]] = select <2 x i1> [[X:%.*]], <2 x i1> [[X]], <2 x i1> <i1 poison, i1 true>
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %r = select <2 x i1> %x, <2 x i1> %x, <2 x i1> <i1 poison, i1 true>
   ret <2 x i1> %r
@@ -583,8 +580,7 @@ define <2 x i1> @always_true_same_op(<2 x i1> %x) {
 
 define i1 @always_false_same_op(i1 %x) {
 ; CHECK-LABEL: @always_false_same_op(
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[X:%.*]], i1 false, i1 [[X]]
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %r = select i1 %x, i1 false, i1 %x
   ret i1 %r


        


More information about the llvm-commits mailing list