<div dir="ltr"><div>Are you sure this is an alias problem?<br></div>What is happening is LLVM is leaving the code looking like this:<br><div><div>int foo(int *p, volatile int *q, int n) {<br>
  int i, s = 0;<br>
  for (i = 0; i < n; ++i)<br>
    s += *p + *q;<br>
  return s;<br>
}<br><br></div><div>but GCC is changing to code to look like this:<br><br>int foo(int *p, volatile int *q, int n) {<br>
  int i, s = 0;<br></div><div>  int t;<br></div><div>  t = *p;<br></div><div>
  for (i = 0; i < n; ++i)<br>
    s += t + *q;<br>
  return s;<br>
}<br><br></div><div>GCC is raising the *p out of the loop, recognizing the fact that memory access is more expensive than reg access.<br></div><div>What is preventing LLVM from doing the same?<br></div><div><br>It could have been even faster if it wished and changed the code to this:<br>
<div><br>int foo(int *p, volatile int *q, int n) {<br>
  int i, s = 0;<br></div><div>  int t;<br></div><div>  t = *p;<br></div><div>  s += t * n;<br></div>
  for (i = 0; i < n; ++i)<br>
    s += *q;<br>
  return s;<br>
}<br><br></div><div><br><br></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 4 September 2013 22:33, Krzysztof Parzyszek <span dir="ltr"><<a href="mailto:kparzysz@codeaurora.org" target="_blank">kparzysz@codeaurora.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">A customer has reported a performance problem, which I have eventually tracked down to the following situation:<br>
<br>
Consider this program:<br>
<br>
int foo(int *p, volatile int *q, int n) {<br>
  int i, s = 0;<br>
  for (i = 0; i < n; ++i)<br>
    s += *p + *q;<br>
  return s;<br>
}<br>
<br>
<br>
LLVM's analysis indicates that *p and *q can alias, even though *p is non-volatile whereas *q is volatile.  I don't have the exact section from the C standard, but if I remember correctly, accessing volatile memory via a non-volatile object results in an undefined behavior.  This would suggest that volatiles and non-volatiles may be considered not to alias automatically, even if TBAA would not be able to prove it.<br>

<br>
The LLVM's code (on x86) at -O2 looks like this:<br>
<br>
        .text<br>
        .globl  foo<br>
        .align  16, 0x90<br>
        .type   foo,@function<br>
foo:                                    # @foo<br>
        .cfi_startproc<br>
# BB#0:                                 # %entry<br>
        xorl    %eax, %eax<br>
        testl   %edx, %edx<br>
        jle     .LBB0_2<br>
        .align  16, 0x90<br>
.LBB0_1:                                # %for.body<br>
        addl    (%rdi), %eax<br>
        addl    (%rsi), %eax<br>
        decl    %edx<br>
        jne     .LBB0_1<br>
.LBB0_2:                                # %for.end<br>
        ret<br>
.Ltmp0:<br>
        .size   foo, .Ltmp0-foo<br>
        .cfi_endproc<br>
        .section        ".note.GNU-stack","",@progbits<br>
<br>
<br>
For comparison, GCC has only one load in the loop:<br>
<br>
        .text<br>
        .p2align 4,,15<br>
.globl foo<br>
        .type   foo, @function<br>
foo:<br>
.LFB0:<br>
        .cfi_startproc<br>
        xorl    %eax, %eax<br>
        testl   %edx, %edx<br>
        jle     .L3<br>
        movl    (%rdi), %r8d<br>
        xorl    %ecx, %ecx<br>
        .p2align 4,,10<br>
        .p2align 3<br>
.L4:<br>
        movl    (%rsi), %edi<br>
        addl    $1, %ecx<br>
        addl    %r8d, %edi<br>
        addl    %edi, %eax<br>
        cmpl    %edx, %ecx<br>
        jne     .L4<br>
.L3:<br>
        rep<br>
        ret<br>
        .cfi_endproc<br>
.LFE0:<br>
        .size   foo, .-foo<br>
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"<br>
        .section        .note.GNU-stack,"",@progbits<br>
<br>
<br>
The specifics in our case were that the AliasSetTracker indicated mod/ref for all pointers in the loop, where one of them was used in a volatile load, and all others were used in non-volatile loads.  As a result, the loop had more loads than necessary.<br>

<br>
Any thoughts?  Has there been any consideration for this type of a situation?<span class="HOEnZb"><font color="#888888"><br>
<br>
-K<br>
<br>
<br>
-- <br>
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation<br>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvmdev</a><br>
</font></span></blockquote></div><br></div>