[PATCH] D114666: Simplify icmp for bool value

Hasyimi Bahrudin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 27 08:25:03 PST 2021


hasyimibhar created this revision.
Herald added a subscriber: hiraditya.
hasyimibhar requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Simplifies X ^ 1 == 0 to X. Refer to https://bugs.llvm.org/show_bug.cgi?id=52546.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114666

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


Index: llvm/test/Transforms/InstSimplify/icmp.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/icmp.ll
+++ llvm/test/Transforms/InstSimplify/icmp.ll
@@ -186,6 +186,16 @@
   ret i1 %cmp
 }
 
+define i1 @not_cmp_equal(i1 %x) {
+; CHECK-LABEL: @not_cmp_equal(
+; CHECK-NEXT:    ret i1 %x
+;
+  %not = xor i1 %x, true
+  %t0 = sext i1 %not to i32
+  %cmp = icmp eq i32 %t0, 0
+  ret i1 %cmp
+}
+
 ; Don't crash matching recurrences/invertible ops.
 
 define void @PR50191(i32 %x) {
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2690,6 +2690,9 @@
     return nullptr;
 
   // A boolean compared to true/false can be simplified in 14 out of the 20
+
+  Value *X;
+
   // (10 predicates * 2 constants) possible combinations. Cases not handled here
   // require a 'not' of the LHS, so those must be transformed in InstCombine.
   if (match(RHS, m_Zero())) {
@@ -2699,6 +2702,13 @@
     case CmpInst::ICMP_SLT: // X <s  0 -> X
       return LHS;
 
+    case CmpInst::ICMP_EQ:
+      // (X ^ 1) == 0 -> X
+      if (match(LHS, m_Xor(m_Value(X), m_One()))) {
+        return X;
+      }
+      break;
+
     case CmpInst::ICMP_ULT: // X <u  0 -> false
     case CmpInst::ICMP_SGT: // X >s  0 -> false
       return getFalse(ITy);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114666.390163.patch
Type: text/x-patch
Size: 1447 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211127/024183c8/attachment.bin>


More information about the llvm-commits mailing list