[llvm-bugs] [Bug 26344] New: failure with unrolled loops which exit in the first iteration.

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jan 27 10:00:51 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=26344

            Bug ID: 26344
           Summary: failure with unrolled loops which exit in the first
                    iteration.
           Product: clang
           Version: 3.7
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: Loria at phantasia.org
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

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

-- 
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/20160127/cf29b669/attachment.html>


More information about the llvm-bugs mailing list