[LLVMdev] Checked arithmetic

Chris Lattner sabre at nondot.org
Wed Mar 26 13:37:53 PDT 2008


On Wed, 26 Mar 2008, Duncan Sands wrote:

> Hi Chris,
>
>> Why not define an "add with overflow" intrinsic that returns its value and
>> overflow bit as an i1?
>
> what's the point?  We have this today with apint codegen (if you turn on
> LegalizeTypes).  For example, this function

The desired code is something like:

foo:
    addl %eax, %ecx
    jo overflow_happened
    use(%ecx)

etc.

-Chris

> define i1 @cc(i32 %x, i32 %y) {
>  %xx = zext i32 %x to i33
>  %yy = zext i32 %y to i33
>  %s = add i33 %xx, %yy
>  %tmp = lshr i33 %s, 32
>  %b = trunc i33 %tmp to i1
>  ret i1 %b
> }
>
> codegens (on x86-32) to
>
> cc:
>        xorl    %eax, %eax
>        movl    4(%esp), %ecx
>        addl    8(%esp), %ecx
>        adcl    $0, %eax
>        andl    $1, %eax
>        ret
>
> which uses the condition code as desired.  Pity about the
> redundant andl $1, %eax!
>
> Ciao,
>
> Duncan.
>

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/



More information about the llvm-dev mailing list