<div dir="ltr">LGTM<div><br></div><div>FYI, this testcase should also be canonicalized by instcombine, which makes sense to add now that instsimplify isn't catch it ;-)<br><div class="gmail_extra"><br><br><div class="gmail_quote">

On 14 July 2014 12:48, David Majnemer <span dir="ltr"><<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi nicholas,<br>
<br>
When calculating the upper bound of X / -<a href="tel:8589934592" value="+18589934592">8589934592</a>, we would perform<br>
the following calculation: Floor[INT_MAX / <a href="tel:8589934592" value="+18589934592">8589934592</a>]<br>
<br>
However, flooring the result would make us wrongly come to the<br>
conclusion that 1073741824 was not in the set of possible values.<br>
Instead, use the ceiling of the result.<br>
<br>
<a href="http://reviews.llvm.org/D4502" target="_blank">http://reviews.llvm.org/D4502</a><br>
<br>
Files:<br>
  lib/Analysis/InstructionSimplify.cpp<br>
  test/Transforms/InstSimplify/compare.ll<br>
<br>
Index: lib/Analysis/InstructionSimplify.cpp<br>
===================================================================<br>
--- lib/Analysis/InstructionSimplify.cpp<br>
+++ lib/Analysis/InstructionSimplify.cpp<br>
@@ -1964,7 +1964,15 @@<br>
       APInt Val = CI2->getValue().abs();<br>
       if (!Val.isMinValue()) {<br>
         Lower = IntMin.sdiv(Val);<br>
-        Upper = IntMax.sdiv(Val) + 1;<br>
+        APInt Rem;<br>
+        APInt::sdivrem(IntMax, Val, Upper, Rem);<br>
+        // We may need to round the result of the INT_MAX / CI2 calculation up<br>
+        // if we see that the division was not exact.<br>
+        if (Rem.isMinValue())<br>
+          Upper = Upper + 1;<br>
+        else<br>
+          Upper = Upper + 2;<br>
+        assert(Upper != Lower && "Upper part of range has wrapped!");<br>
       }<br>
     } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) {<br>
       // 'lshr x, CI2' produces [0, UINT_MAX >> CI2].<br>
Index: test/Transforms/InstSimplify/compare.ll<br>
===================================================================<br>
--- test/Transforms/InstSimplify/compare.ll<br>
+++ test/Transforms/InstSimplify/compare.ll<br>
@@ -913,3 +913,14 @@<br>
 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[DIV]], -1073741824<br>
 ; CHECK-NEXT: ret i1 [[CMP]]<br>
 }<br>
+<br>
+define i1 @icmp_sdiv_pr20288(i64 %a) {<br>
+   %div = sdiv i64 %a, -8589934592<br>
+   %cmp = icmp ne i64 %div, 1073741824<br>
+   ret i1 %cmp<br>
+<br>
+; CHECK-LABEL: @icmp_sdiv_pr20288<br>
+; CHECK-NEXT: [[DIV:%.*]] = sdiv i64 %a, -8589934592<br>
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[DIV]], 1073741824<br>
+; CHECK-NEXT: ret i1 [[CMP]]<br>
+}<br>
<br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div></div></div>