[LLVMdev] Proposal: add intrinsics for safe division

Reid Kleckner rnk at google.com
Fri Apr 25 10:48:16 PDT 2014


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.

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140425/4dbca872/attachment.html>


More information about the llvm-dev mailing list