<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 with unrolled loops which exit in the first iteration."
   href="https://llvm.org/bugs/show_bug.cgi?id=26344">26344</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>failure with unrolled loops which exit in the first iteration.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.7
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>Loria@phantasia.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>System: OSX 10.10.5
Xcode 7.2
gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

Having a loop, which might exit after the first iteration, the optimization
fails to produce correct code.

<---
#define FAIL 0xFFFFFFFF
#define rotate_left(v, n) (v << n | v >> (32 - n))

unsigned int
encode_arm_immediate (unsigned int val)
{
  unsigned int a, i;

  for (i = 0; i < 32 ; i += 2)
    if((a = rotate_left (val, i)) <= 0xFF)
      return a | (i << 7) ; /* 12-bit pack: [shift-cnt,const].  */
   return FAIL;
}
<---
calling this with val = 0xFF the loop should exit within the first iteration
with 0xFF
if this code fragment gets compiled with 
clang -Wall -m32 -O2  test.c
or
clang -Wall -m64 -O2  test.c
the function will incorrectly return FAIL
looking at the assembler text generated on 
clang -Wall -S -O2  test.c
shows the following:
<---
_encode_arm_immediate:                  ## @encode_arm_immediate
        .cfi_startproc
## BB#0:
        pushq   %rbp
Ltmp4:
        .cfi_def_cfa_offset 16
Ltmp5:
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
Ltmp6:
        .cfi_def_cfa_register %rbp
        movl    %edi, %ecx
        roll    $2, %ecx
        movl    $256, %edx              ## imm = 0x100
        cmpl    $255, %ecx
        jbe     LBB1_1
....
LBB1_1:
        orl     %edx, %ecx
        movl    %ecx, %eax
LBB1_2:                                 ## %.loopexit5
        popq    %rbp
        retq
        .cfi_endproc
<---
it seems the first iteration with i=0 has been omited which causes the loop to
begin with i=1.

I attached the testcase source and the 2 (32bit, 64bit) generated files</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>