[polly] r238905 - Translate power-of-two floor-division into ashr

Tobias Grosser tobias at grosser.es
Wed Jun 3 23:14:55 PDT 2015


On 06/03/2015 07:35 PM, David Majnemer wrote:
>
>
> On Wed, Jun 3, 2015 at 7:56 AM, Tobias Grosser <tobias at grosser.es
> <mailto:tobias at grosser.es>> wrote:
>
>     On 06/03/2015 09:58 AM, David Majnemer wrote:
>
>
>
>         On Tue, Jun 2, 2015 at 11:31 PM, Tobias Grosser
>         <tobias at grosser.es <mailto:tobias at grosser.es>
>         <mailto:tobias at grosser.es <mailto:tobias at grosser.es>>> wrote:
>
>              Author: grosser
>              Date: Wed Jun  3 01:31:30 2015
>              New Revision: 238905
>
>              URL: http://llvm.org/viewvc/llvm-project?rev=238905&view=rev
>              Log:
>              Translate power-of-two floor-division into ashr
>
>              Power-of-two floor divisions can be translated into an
>         arithmetic shift
>              operation. This allows us to replace a complex lowering that
>              requires division
>              operations:
>
>
>         This only holds if the numerator is non-negative.  -1/INT_MIN should
>         give 0 but calculating it with a right-shift would give -1.
>
>
>     Your example -1/INT_MIN is uses negative operands in both the
>     numerator and the denominator. This it indeed failed with my code.
>     However, the case of negative numerators with positive denominators
>     should be fine, as we are translating here a division with rounding
>     towards -inf, not rounding towards zero as the sdiv instruction in
>     LLVM uses. Or did I miss something here.
>
>
> Here is an example using a negative numerator and a positive denominator:
> %r = sdiv i32 2550202368, 1073741824 => -1
>
> Here is what shifting produces:
> %r = ashr i32 2550202368, 30 => -2

Thanks David for looking into this. Maybe I was not clear above, but 
what we are doing is translating from the isl AST to LLVM-IR. The isl 
ast has an operation called floor-division, which is defined as:

   floor((double) n / double (e))

This operations always rounds the result of the division toward -inf. 
LLVM's sdiv however rounds (truncates) towards zero.

Hence, it is expected that there is a mismatch between sdiv and ashr. 
However, the ashr lowering to my understanding implements a 
floor-division that corresponds to the one we have in the isl AST.

Best,
Tobias



More information about the llvm-commits mailing list