Hi,<div><br></div><div>I have a question on why gcc and llvm-gcc compile the following simple code snippet differently:</div><div><br></div><div><font class="Apple-style-span" face="'courier new', monospace">extern int a;</font></div>
<div><div><font class="Apple-style-span" face="'courier new', monospace">extern int *b;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">void foo() {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> int i;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> for (i = 1; i < 100; ++i) </font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> a += b[i];</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div><div><br></div></div><div>gcc compiles this function hoisting the load of the global variable "b" outside of the loop, while llvm-gcc keeps it inside the loop. This results in slower code on the part of llvm-gcc, and I'm wondering why this choice is made? Is it because of the memory consistency model? With respect to memory consistency, does the C standard say whether a global variable used inside a function is loaded at the point of the use(s), or whether it can be loaded by the compiler earlier in the function? I had always thought that it was legal to hoist the load of a global variable outside of the loop as long as it was not declared volatile....</div>
<div><br></div><div>Here is the x86 assembly code generated by gcc 4.5.2. The load of "b" is highlighted:</div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div>
<div>
<font class="Apple-style-span" face="'courier new', monospace"> .file "foo.c"</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .text</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> .p2align 4,,15</font></div><div><font class="Apple-style-span" face="'courier new', monospace">.globl foo</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .type foo, @function</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">foo:</font></div><div><font class="Apple-style-span" face="'courier new', monospace" color="#CC0000"><b> movl b, %ecx</b></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> movl $1, %eax</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> movl a, %edx</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> pushl %ebp</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> movl %esp, %ebp</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> .p2align 4,,7</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .p2align 3</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace">.L2:</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> addl (%ecx,%eax,4), %edx</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> addl $1, %eax</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> cmpl $100, %eax</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> movl %edx, a</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> jne .L2</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> popl %ebp</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> ret</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .size foo, .-foo</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .ident "GCC: (GNU) 4.5.2"</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> .section .note.GNU-stack,"",@progbits</font></div></div><div><br></div><div>And here is the code produced by llvm-gcc 4.2.1:</div>
<div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace"> .file "foo.c"</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .text</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> .globl foo</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .align 16, 0x90</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> .type foo,@function</font></div><div><font class="Apple-style-span" face="'courier new', monospace">foo:</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> pushl %ebp</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> movl %esp, %ebp</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> movl $1, %eax</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> movl a, %ecx</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .align 16, 0x90</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">.LBB0_1:</font></div><div><font class="Apple-style-span" face="'courier new', monospace" color="#CC0000"><b> movl b, %edx</b></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> addl (%edx,%eax,4), %ecx</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> movl %ecx, a</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> incl %eax</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> cmpl $100, %eax</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> jne .LBB0_1</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> popl %ebp</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> ret</font></div><div><font class="Apple-style-span" face="'courier new', monospace">.Ltmp0:</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .size foo, .Ltmp0-foo</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> .section .note.GNU-stack,"",@progbits</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> .ident "GCC: (GNU) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build)"</font></div>
</div><div><br></div><div><br></div>