[llvm-bugs] [Bug 30496] New: Clang unrolls infinite loops incorrectly to finite number of instructions with -O2/-O3 flag

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Sep 22 12:47:10 PDT 2016


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

            Bug ID: 30496
           Summary: Clang unrolls infinite loops incorrectly to finite
                    number of instructions with -O2/-O3 flag
           Product: clang
           Version: 3.8
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: umair at iitk.ac.in
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

Clang compiles some infinite loop programs incorrectly with O2/O3 level
optimization. Example inf.c given below. 

Based on the assembly code of inf.c below, it seems LLVM (and not clang) is
unrolling the loop (see the constants in movl instructions), but only a finite
number of times! 

It seems to terminates before loop counter hits 0, even with other variants
(initializing n=7, or decrementing by n-=3, etc)

--------------------------

$cat inf.c
#include <stdio.h>

int main(){
    int n;
    for(n=9; n!=-2; n-=2){
        printf("n=%d\n", n);
    }
    return 0;
}


$clang --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu

$clang -O3 inf.c
$./a.out
n=9
n=7
n=5
n=3
n=1

$gdb a.out
(gdb) disassemble main
Dump of assembler code for function main:
   0x0000000000400530 <+0>:    push   %rax
   0x0000000000400531 <+1>:    mov    $0x400614,%edi
   0x0000000000400536 <+6>:    mov    $0x9,%esi
   0x000000000040053b <+11>:    xor    %eax,%eax
   0x000000000040053d <+13>:    callq  0x400400 <printf at plt>
   0x0000000000400542 <+18>:    mov    $0x400614,%edi
   0x0000000000400547 <+23>:    mov    $0x7,%esi
   0x000000000040054c <+28>:    xor    %eax,%eax
   0x000000000040054e <+30>:    callq  0x400400 <printf at plt>
   0x0000000000400553 <+35>:    mov    $0x400614,%edi
   0x0000000000400558 <+40>:    mov    $0x5,%esi
   0x000000000040055d <+45>:    xor    %eax,%eax
   0x000000000040055f <+47>:    callq  0x400400 <printf at plt>
   0x0000000000400564 <+52>:    mov    $0x400614,%edi
   0x0000000000400569 <+57>:    mov    $0x3,%esi
   0x000000000040056e <+62>:    xor    %eax,%eax
   0x0000000000400570 <+64>:    callq  0x400400 <printf at plt>
   0x0000000000400575 <+69>:    mov    $0x400614,%edi
   0x000000000040057a <+74>:    mov    $0x1,%esi
   0x000000000040057f <+79>:    xor    %eax,%eax
   0x0000000000400581 <+81>:    callq  0x400400 <printf at plt>
   0x0000000000400586 <+86>:    xor    %eax,%eax
   0x0000000000400588 <+88>:    pop    %rcx
   0x0000000000400589 <+89>:    retq   
End of assembler dump.

-- 
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/20160922/cd24345b/attachment.html>


More information about the llvm-bugs mailing list