[LLVMdev] Checked arithmetic

Duncan Sands baldrick at free.fr
Wed Mar 26 13:04:07 PDT 2008


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

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.



More information about the llvm-dev mailing list