[LLVMdev] Proposal: add intrinsics for safe division

Eric Christopher echristo at gmail.com
Fri Apr 25 11:15:34 PDT 2014


On Fri, Apr 25, 2014 at 10:48 AM, Reid Kleckner <rnk at google.com> wrote:
> On Fri, Apr 25, 2014 at 10:19 AM, Filip Pizlo <fpizlo at apple.com> wrote:
>>
>> The sdiv operation in LLVM IR only makes sense for C and its very direct
>> relatives.  The amount of control flow necessary to represent a safe
>> division for any other language is ghastly.  (a/b) becomes something like (b
>> != 0 ? ((a != INT_MIN || b != -1) ? a / b : INT_MIN) : 0).  It's more useful
>> to the optimizer to see this as the single operation that it really is.
>
> This argument makes sense to me. It would be hard for the arm64 backend to
> pattern match all this control flow back into a single instruction after
> optimizations.

No objections on this point here either - I agree that this makes
things more verbose for generating IR and that pattern matching it
back might be difficult (CVP should be able to do it, however...)

>
> Are there ISAs out there that produce different results without trapping for
> these boundary conditions? Should we leave the quotient result unspecified
> in the b == 0 || (a == INT_MIN && B == -1) cases? If we do that, we require
> frontends to check the "overflow" bit if they want deterministic
> cross-platform behavior.

Agreed, perhaps we want to say "undefined" or "returns the same value"
or are there other semantics?

-eric



More information about the llvm-dev mailing list