<html>
    <head>
      <base href="https://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 --- - Failure to hoist constant out of loop"
   href="https://llvm.org/bugs/show_bug.cgi?id=27136">27136</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failure to hoist constant out of loop
          </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>llvm-dev@redking.me.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>chisophugis@gmail.com, llvm-bugs@lists.llvm.org, spatel+llvm@rotateright.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Poor code gen identified in Andreas Fredriksson's GDC 2016 Talk 'Taming the
Jaguar: x86 Optimization at Insomniac Games': 

<a href="http://schedule.gdconf.com/session/taming-the-jaguar-x86-optimization-at-insomniac-games">http://schedule.gdconf.com/session/taming-the-jaguar-x86-optimization-at-insomniac-games</a>

search.c:

#include <stdint.h>
#include <stdbool.h>

bool search(uint32_t needle, const uint32_t *haystack, int count) {
  for (int i = 0; i < count; ++i)
    if (needle == haystack[i])
      return true;
  return false;
}

clang -O3 -S -march=btver2 search.c -o -

_search:
    .cfi_startproc
## BB#0:
    pushq    %rbp
Ltmp0:
    .cfi_def_cfa_offset 16
Ltmp1:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp2:
    .cfi_def_cfa_register %rbp
    testl    %edx, %edx
    jle    LBB0_1
## BB#4:
    movslq    %edx, %rcx
    xorl    %edx, %edx
    .p2align    4, 0x90
LBB0_5:
    cmpl    %edi, (%rsi,%rdx,4)
    movb    $1, %al
    je    LBB0_6
## BB#2:
    incq    %rdx
    cmpq    %rcx, %rdx
    jl    LBB0_5
## BB#3:
    xorl    %eax, %eax
    popq    %rbp
    retq
LBB0_1:
    xorl    %eax, %eax
    popq    %rbp
    retq
LBB0_6:
    popq    %rbp
    retq
    .cfi_endproc

As well as failing to pull the "movb $1, %al" out of the loop (and avoid the
partial register move issue), it also fails to merge the 3 epilogs into
something like:

## BB#3:
LBB0_1:
    xorl    %eax, %eax
LBB0_6:
    popq    %rbp
    retq</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>