[LLVMbugs] [Bug 16424] New: combine multiple checks of __builtin_*_with_overflow()

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun Jun 23 09:21:06 PDT 2013


http://llvm.org/bugs/show_bug.cgi?id=16424

            Bug ID: 16424
           Summary: combine multiple checks of __builtin_*_with_overflow()
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: nunoplopes at sapo.pt
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

At least the x86 backend could be improved to combine checks from multiple
__builtin_*_with_overflow() calls.

E.g.:

target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128"
target triple = "i386-pc-linux-gnu"

; Function Attrs: nounwind readnone
define i32 @safe_address(i32 %nmemb, i32 %size, i32 %offset) {
entry:
  %0 = tail call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %nmemb, i32
%size)
  %1 = extractvalue { i32, i1 } %0, 1
  %2 = extractvalue { i32, i1 } %0, 0
  %3 = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %2, i32 %offset)
  %4 = extractvalue { i32, i1 } %3, 1
  %or3 = or i1 %1, %4
  br i1 %or3, label %return, label %if.end, !prof !0

if.end:                                           ; preds = %entry
  %5 = extractvalue { i32, i1 } %3, 0
  br label %return

return:                                           ; preds = %entry, %if.end
  %retval.0 = phi i32 [ %5, %if.end ], [ 0, %entry ]
  ret i32 %retval.0
}

!0 = metadata !{metadata !"branch_weights", i32 4, i32 64}
declare { i32, i1 } @llvm.umul.with.overflow.i32(i32, i32) #1
declare { i32, i1 } @llvm.uadd.with.overflow.i32(i32, i32) #1


compiles to:
safe_address:                           # @safe_address
# BB#0:                                 # %entry
        pushl   %esi
        movl    8(%esp), %eax
        mull    12(%esp)
        pushfl
        popl    %esi
        addl    16(%esp), %eax
        setb    %dl
        xorl    %ecx, %ecx
        pushl   %esi
        popfl
        jo      .LBB0_3
# BB#1:                                 # %entry
        testb   %dl, %dl
        jne     .LBB0_3
# BB#2:                                 # %if.end
        movl    %eax, %ecx
.LBB0_3:                                # %return
        movl    %ecx, %eax
        popl    %esi
        ret


Although a simpler sequence (w/ just 1 branch) like the following could be used
instead:
        movl    8(%esp), %eax
        mull    12(%esp)         # overflow in %edx
        addl    16(%esp), %eax
        adcl    %edx, %edx
        testl   %edx, %edx
        jne     .LBB0_1          # jump if %edx + %edx + carry != 0
.LBB0_2:
        # result already in EAX
        ret
.LBB0_1:
        xorl    %eax, %eax
        jmp     .LBB0_2

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20130623/8316a40b/attachment.html>


More information about the llvm-bugs mailing list