[cfe-dev] [RFC] Introduce overflow builtins

Dave Zarzycki zarzycki at apple.com
Mon Apr 2 10:04:20 PDT 2012

On Apr 2, 2012, at 6:27 AM, Xi Wang <xi.wang at gmail.com> wrote:

>> In practice? Well maybe not your code, but for many others the lack of 8-bit support makes using the __builtin_*_with_overflow() intrinsics in generic code much harder and much uglier.
> It would be nice to have llvm.*.with.overflow.i8 in LLVM for completeness.


A colleague pointed out that i8 works. See the example at the end of this email and please consider adding support for 8-bit integers for the sake of completeness. Thanks! :-)

>> With the overflow intrinsics that you are proposing, you've solved half of the work required to writing a straightforward, efficient, and somewhat portable "Big Number" library. :-)
>> For example, 128-bit or larger integers could have been solved in a library instead of directly in the compiler if these intrinsics were available.
> That sounds interesting, though I would rather focus on integer overflows for now and leave the joy/pain of implementing bignum to the GMP folks. ;-)

Fair enough! :-)


The example:

/tmp/llvm/b $ cat of.bc
; ModuleID = 'of.c'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.7.0"

define signext i8 @example2(i8 signext %x, i8 signext %y, i8 signext %z) nounwind uwtable optsize ssp {
  %0 = tail call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %y, i8 %z)
  %1 = extractvalue { i8, i1 } %0, 0
  %2 = extractvalue { i8, i1 } %0, 1
  br i1 %2, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  tail call void @llvm.trap()
  br label %if.end

if.end:                                           ; preds = %if.then, %entry
  ret i8 %1

declare { i8, i1 } @llvm.sadd.with.overflow.i8(i8, i8) nounwind readnone

declare void @llvm.trap() nounwind

/tmp/llvm/b $ clang -Os -momit-leaf-frame-pointer -c of.bc && lldb of.o
Current executable set to 'of.o' (x86_64).
(lldb) disassemble -bn example2
of.o[0x0]:  40 00 d6     addb   %dl, %sil
of.o[0x3]:  71 02        jno    0x0000000000000007       ; example2 + 7
of.o[0x5]:  0f 0b        ud2    
of.o[0x7]:  40 0f be c6  movsbl %sil, %eax
of.o[0xb]:  c3           ret    
of.o[0xc]:  00 00        addb   %al, (%rax)
of.o[0xe]:  00 00        addb   %al, (%rax)


More information about the cfe-dev mailing list