<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 --- - Clang unrolls infinite loops incorrectly to finite number of instructions with -O2/-O3 flag"
href="https://llvm.org/bugs/show_bug.cgi?id=30496">30496</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Clang unrolls infinite loops incorrectly to finite number of instructions with -O2/-O3 flag
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.8
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</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>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>umair@iitk.ac.in
</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>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@plt>
0x0000000000400542 <+18>: mov $0x400614,%edi
0x0000000000400547 <+23>: mov $0x7,%esi
0x000000000040054c <+28>: xor %eax,%eax
0x000000000040054e <+30>: callq 0x400400 <printf@plt>
0x0000000000400553 <+35>: mov $0x400614,%edi
0x0000000000400558 <+40>: mov $0x5,%esi
0x000000000040055d <+45>: xor %eax,%eax
0x000000000040055f <+47>: callq 0x400400 <printf@plt>
0x0000000000400564 <+52>: mov $0x400614,%edi
0x0000000000400569 <+57>: mov $0x3,%esi
0x000000000040056e <+62>: xor %eax,%eax
0x0000000000400570 <+64>: callq 0x400400 <printf@plt>
0x0000000000400575 <+69>: mov $0x400614,%edi
0x000000000040057a <+74>: mov $0x1,%esi
0x000000000040057f <+79>: xor %eax,%eax
0x0000000000400581 <+81>: callq 0x400400 <printf@plt>
0x0000000000400586 <+86>: xor %eax,%eax
0x0000000000400588 <+88>: pop %rcx
0x0000000000400589 <+89>: retq
End of assembler dump.</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>