[PATCH] D13756: sgt on i1s also encodes implication

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 14 16:33:31 PDT 2015


reames created this revision.
reames added a reviewer: sanjoy.
reames added a subscriber: llvm-commits.

Follow on to http://reviews.llvm.org/D13074, implementing something pointed out by Sanjoy.  His truth table from his comment on that bug summarizes things well:
LHS | RHS | LHS >=s RHS   | LHS implies RHS
 0  |  0  |  1 (0 >= 0)   |  1
 0  |  1  |  1 (0 >= -1)  |  1
 1  |  0  |  0 (-1 >= 0)  |  0
 1  |  1  |  1 (-1 >= -1) |  1

The key point is that an "i1 1" is the value "-1", not "1".  

http://reviews.llvm.org/D13756

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

Index: test/Transforms/InstSimplify/implies.ll
===================================================================
--- test/Transforms/InstSimplify/implies.ll
+++ test/Transforms/InstSimplify/implies.ll
@@ -91,3 +91,14 @@
   %res = icmp ule <4 x i1> %a, %b
   ret <4 x i1> %res
 }
+
+; X >=(s) Y == X ==> Y (i1 1 becomes -1 for reasoning)
+define i1 @test_sge(i32 %length.i, i32 %i) {
+; CHECK-LABEL: @test_sge
+; CHECK: ret i1 true
+  %iplus1 = add nsw nuw i32 %i, 1
+  %var29 = icmp ult i32 %i, %length.i
+  %var30 = icmp ult i32 %iplus1, %length.i
+  %res = icmp sge i1 %var30, %var29
+  ret i1 %res
+}
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -2130,12 +2130,19 @@
 
 /// Return true if B is known to be implied by A.  A & B must be i1 (boolean)
 /// values or a vector of such values. Note that the truth table for
-/// implication is the same as <=u on i1 values (but not <=s!).  The truth
-/// table for both is: 
+/// implication is the same as <=u on i1 values (but not <=s, see below!).  The
+/// truth table for both is: 
 ///    | T | F (B)
 ///  T | T | F
 ///  F | T | T
 /// (A)
+/// For signed comparison, the values for an i1 are 0 and -1 respectively.
+/// This maps into a truth table of:
+/// LHS | RHS | LHS >=s RHS   | LHS implies RHS
+///  0  |  0  |  1 (0 >= 0)   |  1
+///  0  |  1  |  1 (0 >= -1)  |  1
+///  1  |  0  |  0 (-1 >= 0)  |  0
+///  1  |  1  |  1 (-1 >= -1) |  1
 static bool implies(Value *A, Value *B) {
   assert(A->getType() == B->getType() && "mismatched type");
   Type *OpTy = A->getType();
@@ -2250,6 +2257,10 @@
       if (implies(RHS, LHS))
         return getTrue(ITy);
       break;
+    case ICmpInst::ICMP_SGE:
+      if (implies(LHS, RHS))
+        return getTrue(ITy);
+      break;
     case ICmpInst::ICMP_SLT:
       // X <s 0 -> X
       if (match(RHS, m_Zero()))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13756.37423.patch
Type: text/x-patch
Size: 1977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151014/cfaab331/attachment.bin>


More information about the llvm-commits mailing list