# [PATCH] InstSimplify: Optimize using dividend in sdiv

Fri May 16 00:45:47 PDT 2014

```On 16 May 2014 00:21, David Majnemer <david.majnemer at gmail.com> wrote:

> Hi nicholas,
>
> The dividend in an sdiv tells us the largest and smallest possible
> results.  Use this fact to optimize comparisons against an sdiv with a
> constant dividend.
>
> http://reviews.llvm.org/D3795
>
> Files:
>   lib/Analysis/InstructionSimplify.cpp
>   test/Transforms/InstSimplify/compare.ll
>
> Index: lib/Analysis/InstructionSimplify.cpp
> ===================================================================
> --- lib/Analysis/InstructionSimplify.cpp
> +++ lib/Analysis/InstructionSimplify.cpp
> @@ -2020,6 +2020,10 @@
>        APInt NegOne = APInt::getAllOnesValue(Width);
>        if (!CI2->isZero())
>          Upper = NegOne.udiv(CI2->getValue()) + 1;
> +    } else if (match(LHS, m_SDiv(m_ConstantInt(CI2), m_Value()))) {
> +      // 'sdiv CI2, x' produces (-|CI2|, |CI2|).
>

Doesn't it produce [-|CI2|, |CI2|] , by which I mean, the closed range
including CI2 and -CI2?

> +      Upper = CI2->getValue().abs();
>

Upper is a one-past-the-end value, so this needs a +1 on it. The ranges are
half-open, [Lower, Upper).

Nick

+      Lower = (-Upper) + 1;
>      } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) {
>        // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2].
>        APInt IntMin = APInt::getSignedMinValue(Width);
> Index: test/Transforms/InstSimplify/compare.ll
> ===================================================================
> --- test/Transforms/InstSimplify/compare.ll
> +++ test/Transforms/InstSimplify/compare.ll
> @@ -817,3 +817,12 @@
>  ; CHECK-LABEL: @compare_always_false_ne
>  ; CHECK-NEXT: ret i1 true
>  }
> +
> +define i1 @compare_dividend(i32 %a) {
> +  %div = sdiv i32 2, %a
> +  %cmp = icmp eq i32 %div, 3
> +  ret i1 %cmp
> +
> +; CHECK-LABEL: @compare_dividend
> +; CHECK-NEXT: ret i1 false
> +}
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140516/ac673fc2/attachment.html>
```