<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - combine multiple checks of __builtin_*_with_overflow()"
   href="http://llvm.org/bugs/show_bug.cgi?id=16424">16424</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>combine multiple checks of __builtin_*_with_overflow()
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>nunoplopes@sapo.pt
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>