[LLVMdev] Integer divide by zero

James Courtier-Dutton james.dutton at gmail.com
Mon Apr 8 02:31:28 PDT 2013


On Apr 8, 2013 8:42 AM, "Duncan Sands" <baldrick at free.fr> wrote:
>
> Hi Cameron,
>
>
> On 07/04/13 19:42, Cameron McInally wrote:
>>
>> On Sun, Apr 7, 2013 at 1:23 PM, Duncan Sands <baldrick at free.fr
>> <mailto:baldrick at free.fr>> wrote:
>> ...
>>
>>     If you want a trap you are going to have to (IMO) output IR
>>     instructions rather than a constant expression.  For example you can
output the
>>     equivalent of
>>        if (x == 0)
>>           llvm.trap() // or maybe some special trap routine
>>        y = whatever/x
>>        ... use y ...
>>     rather than the constant expression "whatever/x".  If the optimizers
can prove
>>     that x is never zero then this will be sunk back into a constant
expression (if
>>     "whatever" is a constant expression).
>>
>>
>> Ah, that's super interesting. I'll have to give it some more thought.
Thanks for
>> this, Duncan.
>>
>> My knee-jerk reaction is that emulating hardware behaviour in software
should be
>> avoided. At least in this specific case.
>
>
> I reckon it shouldn't be too hard to teach the code generators to turn
this IR
> sequence into "y = target-divide whatever/x" on targets for which
dividing by 0
> traps in a satisfactory way, so it turns into something efficient.
>
If the application is safety related, then the if (x == 0) should be there
in the source code anyway.
If it happens to be able to optimize it into the div instruction at target
lowering, then it could do it then.
If a target cpu does not do the div trap then the target lowering would at
least maintain the safety at the expense of an extra branch.

James.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130408/69314498/attachment.html>


More information about the llvm-dev mailing list