[PATCH] D149157: [InstSimplify] with logical ops: (0 == (X | Y)) ? X : 0 --> 0

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 07:17:32 PDT 2023


Allen created this revision.
Allen added reviewers: nikic, spatel, RKSimon.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
Allen requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This code try to internally match the EQ predicate when their
operands swapped.


https://reviews.llvm.org/D149157

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/select_or_and.ll


Index: llvm/test/Transforms/InstSimplify/select_or_and.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/select_or_and.ll
+++ llvm/test/Transforms/InstSimplify/select_or_and.ll
@@ -211,6 +211,16 @@
   ret i32 %cond
 }
 
+define i32 @select_icmp_or_eq_swap(i32 %a, i32 %b) {
+; CHECK-LABEL: @select_icmp_or_eq_swap(
+; CHECK-NEXT:    ret i32 0
+;
+  %or = or i32 %a, %b
+  %tobool = icmp eq i32 0, %or  ; swap the icmp operands
+  %cond = select i1 %tobool, i32 %a, i32 0
+  ret i32 %cond
+}
+
 define i32 @select_icmp_or_eq_commuted(i32 %a, i32 %b) {
 ; CHECK-LABEL: @select_icmp_or_eq_commuted(
 ; CHECK-NEXT:    ret i32 0
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4594,14 +4594,18 @@
     // select(X | Y == 0 ?  X : 0) --> 0 (commuted 2 ways)
     Value *X;
     Value *Y;
-    if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) &&
-        match(CmpRHS, m_Zero())) {
+    if ((match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) &&
+         match(CmpRHS, m_Zero())) ||
+        (match(CmpRHS, m_Or(m_Value(X), m_Value(Y))) &&
+         match(CmpLHS, m_Zero()))) {
       // X | Y == 0 implies X == 0 and Y == 0.
-      if (Value *V = simplifySelectWithICmpEq(X, CmpRHS, TrueVal, FalseVal, Q,
-                                              MaxRecurse))
+      if (Value *V =
+              simplifySelectWithICmpEq(X, ConstantInt::get(X->getType(), 0),
+                                       TrueVal, FalseVal, Q, MaxRecurse))
         return V;
-      if (Value *V = simplifySelectWithICmpEq(Y, CmpRHS, TrueVal, FalseVal, Q,
-                                              MaxRecurse))
+      if (Value *V =
+              simplifySelectWithICmpEq(Y, ConstantInt::get(Y->getType(), 0),
+                                       TrueVal, FalseVal, Q, MaxRecurse))
         return V;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149157.516797.patch
Type: text/x-patch
Size: 2016 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/c7d68c37/attachment-0001.bin>


More information about the llvm-commits mailing list