[llvm] r251597 - [InstSimplify] sgt on i1s also encodes implication

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 28 20:19:11 PDT 2015


Author: reames
Date: Wed Oct 28 22:19:10 2015
New Revision: 251597

URL: http://llvm.org/viewvc/llvm-project?rev=251597&view=rev
Log:
[InstSimplify] sgt on i1s also encodes implication

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".

Differential Revision: http://reviews.llvm.org/D13756


Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/test/Transforms/InstSimplify/implies.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=251597&r1=251596&r2=251597&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Wed Oct 28 22:19:10 2015
@@ -2179,6 +2179,17 @@ static Value *SimplifyICmpInst(unsigned
       if (isImpliedCondition(RHS, LHS))
         return getTrue(ITy);
       break;
+    case ICmpInst::ICMP_SGE:
+      /// 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
+      if (isImpliedCondition(LHS, RHS))
+        return getTrue(ITy);
+      break;
     case ICmpInst::ICMP_SLT:
       // X <s 0 -> X
       if (match(RHS, m_Zero()))

Modified: llvm/trunk/test/Transforms/InstSimplify/implies.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/implies.ll?rev=251597&r1=251596&r2=251597&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/implies.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/implies.ll Wed Oct 28 22:19:10 2015
@@ -91,3 +91,14 @@ define <4 x i1> @test6(<4 x i1> %a, <4 x
   %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
+}




More information about the llvm-commits mailing list