[cfe-dev] [RFC] Introduce overflow builtins

Joerg Sonnenberger joerg at britannica.bec.de
Wed Mar 28 19:02:29 PDT 2012


On Wed, Mar 28, 2012 at 02:56:23PM -0400, Xi Wang wrote:
> On Mar 28, 2012, at 9:26 AM, Joerg Sonnenberger wrote:
> 
> > On Tue, Mar 27, 2012 at 11:13:11PM -0400, Xi Wang wrote:
> >> As Eli said, there's no such thing as llvm.div.with.overflow ---
> > 
> > Just because it hasn't been done...
> > 
> >> I am not sure if there's any performance gain from compiler support.
> > 
> > At least for x86, the same argument applies here as for the other cases:
> > you can detect it with a trivial flag compare. An additional branch, if
> > you also want to handle divisor of 0.
> 
> Which case did you mean?  The same argument holds for signed division,
> but probably not for unsigned division and shifts.
> 
> If the LLVM IR adds llvm.sdiv.with.overflow, we can definitely add
> a corresponding builtin.  I feel like it is better to leave the
> rest cases to an overflow library, and have the compiler focus on
> providing minimum necessary support for overflow detection (rather
> than implementing a complete library itself).

As originally said, a large point of doing it in the compiler is making
sure it is low overhead. unsigned divisions can't overflow in the
traditional sense. There is still the point of divide by zero and
depending on where you come from, it is a form of overflow. I don't
believe merging division and remainder computation forcefully provides
any value -- modulo can't overflow and if you checked the division for
divide-by-0, the modulo works. The cost of the
overflow-or-divide-by-zero checking builtins on x86 is one post-branch
and in case of divide/remainder one pre-branch per instruction.

Joerg



More information about the cfe-dev mailing list