<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 --- - LLVM pessimizes simple array comparison loop"
   href="http://llvm.org/bugs/show_bug.cgi?id=19710">19710</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLVM pessimizes simple array comparison loop
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </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>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>st@quanttec.com
          </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>The current trunk version of clang/LLVM pessimizes the loop in the following
example (by unnecessarily introducing a new loop induction variable):

bool arrayLess(const int* p1, long length1,
               const int* p2, long length2)
{
  const long minLength = length1 <= length2 ? length1 : length2;
  if (minLength > 0) {
    const int* const end = p1 + minLength;
    do {
      if (*p1 != *p2) {
        return *p1 < *p2;
      }
      ++p1;
      ++p2;
    } while (p1 != end);
  }
  return length1 < length2;
}

clang++ -O3 -S test.cpp yields the following assembly:

_Z9arrayLessPKilS0_l:                   # @_Z9arrayLessPKilS0_l
    .cfi_startproc
# BB#0:                                 # %entry
    cmpq    %rcx, %rsi
    movq    %rcx, %rax
    cmovleq    %rsi, %rax
    testq    %rax, %rax
    jle    .LBB0_4
# BB#1:                                 # %if.then
    movq    %rcx, %rax
    notq    %rax
    movq    %rsi, %r8
    notq    %r8
    cmpq    %r8, %rax
    cmovgeq    %rax, %r8
    shlq    $2, %r8
    movq    $-4, %r9
    subq    %r8, %r9
    .align    16, 0x90
.LBB0_2:                                # %do.body
                                        # =>This Inner Loop Header: Depth=1
    movl    (%rdx), %eax
    cmpl    %eax, (%rdi)
    jne    .LBB0_5
# BB#3:                                 # %if.end
                                        #   in Loop: Header=BB0_2 Depth=1
    addq    $4, %rdi
    addq    $4, %rdx
    addq    $-4, %r9
    jne    .LBB0_2
.LBB0_4:                                # %if.end7
    cmpq    %rcx, %rsi
.LBB0_5:                                # %return
    setl    %al
    retq
.Ltmp0:
    .size    _Z9arrayLessPKilS0_l, .Ltmp0-_Z9arrayLessPKilS0_l
    .cfi_endproc

For comparison, GCC generates the following assembly:

_Z9arrayLessPKilS0_l:
.LFB0:
    .cfi_startproc
    cmpq    %rcx, %rsi
    movq    %rcx, %rax
    cmovle    %rsi, %rax
    testq    %rax, %rax
    jle    .L2
    leaq    (%rdi,%rax,4), %r9
    jmp    .L5
    .p2align 4,,10
    .p2align 3
.L3:
    addq    $4, %rdi
    addq    $4, %rdx
    cmpq    %r9, %rdi
    je    .L2
.L5:
    movl    (%rdi), %eax
    movl    (%rdx), %r8d
    cmpl    %r8d, %eax
    je    .L3
    cmpl    %eax, %r8d
    setg    %al
    ret
    .p2align 4,,10
    .p2align 3
.L2:
    cmpq    %rcx, %rsi
    setl    %al
    ret
    .cfi_endproc</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>