[llvm] r212976 - InstSimplify: The upper bound of X / C was missing a rounding step

David Majnemer david.majnemer at gmail.com
Mon Jul 14 12:49:57 PDT 2014


Author: majnemer
Date: Mon Jul 14 14:49:57 2014
New Revision: 212976

URL: http://llvm.org/viewvc/llvm-project?rev=212976&view=rev
Log:
InstSimplify: The upper bound of X / C was missing a rounding step

Summary:
When calculating the upper bound of X / -8589934592, we would perform
the following calculation: Floor[INT_MAX / 8589934592]

However, flooring the result would make us wrongly come to the
conclusion that 1073741824 was not in the set of possible values.
Instead, use the ceiling of the result.

Reviewers: nicholas

Subscribers: llvm-commits

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

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

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=212976&r1=212975&r2=212976&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon Jul 14 14:49:57 2014
@@ -1964,7 +1964,15 @@ static Value *SimplifyICmpInst(unsigned
       APInt Val = CI2->getValue().abs();
       if (!Val.isMinValue()) {
         Lower = IntMin.sdiv(Val);
-        Upper = IntMax.sdiv(Val) + 1;
+        APInt Rem;
+        APInt::sdivrem(IntMax, Val, Upper, Rem);
+        // We may need to round the result of the INT_MAX / CI2 calculation up
+        // if we see that the division was not exact.
+        if (Rem.isMinValue())
+          Upper = Upper + 1;
+        else
+          Upper = Upper + 2;
+        assert(Upper != Lower && "Upper part of range has wrapped!");
       }
     } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) {
       // 'lshr x, CI2' produces [0, UINT_MAX >> CI2].

Modified: llvm/trunk/test/Transforms/InstSimplify/compare.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/compare.ll?rev=212976&r1=212975&r2=212976&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/compare.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/compare.ll Mon Jul 14 14:49:57 2014
@@ -913,3 +913,14 @@ define i1 @icmp_sdiv_int_min(i32 %a) {
 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[DIV]], -1073741824
 ; CHECK-NEXT: ret i1 [[CMP]]
 }
+
+define i1 @icmp_sdiv_pr20288(i64 %a) {
+   %div = sdiv i64 %a, -8589934592
+   %cmp = icmp ne i64 %div, 1073741824
+   ret i1 %cmp
+
+; CHECK-LABEL: @icmp_sdiv_pr20288
+; CHECK-NEXT: [[DIV:%.*]] = sdiv i64 %a, -8589934592
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[DIV]], 1073741824
+; CHECK-NEXT: ret i1 [[CMP]]
+}





More information about the llvm-commits mailing list