[PATCH] InstCombineCompare with constant return false if we know it is never going to be equal

suyog suyog.sarda at samsung.com
Wed Jun 4 14:32:52 PDT 2014


Hi Duncan, David, all,

Thanks for the review.

I modified the comment as per Duncan's suggestion.
Please suggest if the comment is OK or suggest an appropriate comment.

Also re-factored code as per suggestion. Trimmed test cases to eliminate unnecessary code.

Please help in reviewing the patch. Suggestions/Comments/Modification are most awaited.

Thanks !

http://reviews.llvm.org/D3868

Files:
  lib/Analysis/InstructionSimplify.cpp
  test/Transforms/InstCombine/align-2d-gep.ll
  test/Transforms/InstSimplify/compare.ll

Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -1990,6 +1990,30 @@
     }
   }
 
+  // If a bit is known to be zero for A and known to be one for B,
+  // then A and B cannot be equal.
+  if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
+    uint32_t BitWidth = CI->getBitWidth();
+    APInt LHSKnownZero(BitWidth, 0);
+    APInt LHSKnownOne(BitWidth, 0);
+    computeKnownBits(LHS, LHSKnownZero, LHSKnownOne);
+    APInt RHSKnownZero(BitWidth, 0);
+    APInt RHSKnownOne(BitWidth, 0);
+    computeKnownBits(RHS, RHSKnownZero, RHSKnownOne);
+    switch (Pred) {
+    default:
+      break;
+    case ICmpInst::ICMP_EQ:
+    case ICmpInst::ICMP_NE:
+      if (((LHSKnownOne & RHSKnownZero) != 0) ||
+          ((LHSKnownZero & RHSKnownOne) != 0))
+        return (Pred == ICmpInst::ICMP_EQ) ?
+                   ConstantInt::getFalse(CI->getContext()) :
+                   ConstantInt::getTrue(CI->getContext());
+      break;
+    }
+  }
+
   // See if we are doing a comparison with a constant integer.
   if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
     // Rule out tautological comparisons (eg., ult 0 or uge 0).
Index: test/Transforms/InstCombine/align-2d-gep.ll
===================================================================
--- test/Transforms/InstCombine/align-2d-gep.ll
+++ test/Transforms/InstCombine/align-2d-gep.ll
@@ -31,7 +31,7 @@
   store <2 x double><double 0.0, double 0.0>, <2 x double>* %r, align 8
 
   %indvar.next = add i64 %j, 2
-  %exitcond = icmp eq i64 %indvar.next, 557
+  %exitcond = icmp eq i64 %indvar.next, 556
   br i1 %exitcond, label %bb11, label %bb1
 
 bb11:
Index: test/Transforms/InstSimplify/compare.ll
===================================================================
--- test/Transforms/InstSimplify/compare.ll
+++ test/Transforms/InstSimplify/compare.ll
@@ -883,3 +883,22 @@
 ; CHECK: ret i1 false
 }
 
+; If a bit is known to be zero for A and known to be one for B,
+; then A and B cannot be equal.
+define i1 @icmp_eq_const(i32 %a) nounwind {
+  %b = mul nsw i32 %a, -2
+  %c = icmp eq i32 %b, 1
+  ret i1 %c
+
+; CHECK-LABEL: @icmp_eq_const
+; CHECK-NEXT: ret i1 false 
+}
+
+define i1 @icmp_ne_const(i32 %a) nounwind {
+  %b = mul nsw i32 %a, -2
+  %c = icmp ne i32 %b, 1
+  ret i1 %c
+
+; CHECK-LABEL: @icmp_ne_const
+; CHECK-NEXT: ret i1 true
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3868.10109.patch
Type: text/x-patch
Size: 2464 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140604/b11642d8/attachment.bin>


More information about the llvm-commits mailing list