<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 --- - Failing to hoist all loads out of a loop"
   href="https://llvm.org/bugs/show_bug.cgi?id=30692">30692</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failing to hoist all loads out of a loop
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>tools
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>opt
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>charles_li@playstation.sony.com
          </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>Hi Everyone,

Found a optimization issue.


Here is the test case.
$ cat t.cpp
/*************************/
int aaa = 10;
int bbb = 20;
int ccc = 30;

void foo(const int *pData)
{
    while (1)
    {
        int x = *pData++;

        if (x > ccc)
            bbb += x;

        if (x > bbb)
            aaa += x;

        if (x > aaa)
            ccc += x;

        if (x < 0)
            break;
    }
}
/*************************/


Compile this test with optimization.
$ clang t.cpp -S -O2


Here is the assembly.
$ cat t.s

        .text
        .file   "t.cpp"
        .globl  _Z3fooPKi
        .p2align        4, 0x90
        .type   _Z3fooPKi,@function
_Z3fooPKi:                              # @_Z3fooPKi
        .cfi_startproc
# BB#0:                                 # %entry
        movl    ccc(%rip), %eax
        .p2align        4, 0x90
.LBB0_1:                                # %while.body
                                        # =>This Inner Loop Header: Depth=1
        movl    (%rdi), %ecx
        movl    bbb(%rip), %edx
        cmpl    %eax, %ecx
        jle     .LBB0_3
# BB#2:                                 # %if.then
                                        #   in Loop: Header=BB0_1 Depth=1
        addl    %ecx, %edx
        movl    %edx, bbb(%rip)
.LBB0_3:                                # %if.end
                                        #   in Loop: Header=BB0_1 Depth=1
        movl    aaa(%rip), %esi
        cmpl    %edx, %ecx
        jle     .LBB0_5
# BB#4:                                 # %if.then2
                                        #   in Loop: Header=BB0_1 Depth=1
        addl    %ecx, %esi
        movl    %esi, aaa(%rip)
.LBB0_5:                                # %if.end4
                                        #   in Loop: Header=BB0_1 Depth=1
        cmpl    %esi, %ecx
        jle     .LBB0_7
# BB#6:                                 # %if.then6
                                        #   in Loop: Header=BB0_1 Depth=1
        addl    %ecx, %eax
        movl    %eax, ccc(%rip)
.LBB0_7:                                # %if.end8
                                        #   in Loop: Header=BB0_1 Depth=1
        addq    $4, %rdi
        testl   %ecx, %ecx
        jns     .LBB0_1
# BB#8:                                 # %while.end
        retq
.Lfunc_end0:
        .size   _Z3fooPKi, .Lfunc_end0-_Z3fooPKi
        .cfi_endproc

        .type   aaa,@object             # @aaa
        .data
        .globl  aaa
        .p2align        2
aaa:
        .long   10                      # 0xa
        .size   aaa, 4

        .type   bbb,@object             # @bbb
        .globl  bbb
        .p2align        2
bbb:
        .long   20                      # 0x14
        .size   bbb, 4

        .type   ccc,@object             # @ccc
        .globl  ccc
        .p2align        2
ccc:
        .long   30                      # 0x1e
        .size   ccc, 4



Looking at the location of these 3 instructions:
  movl    ccc(%rip), %eax
  movl    bbb(%rip), %edx
  movl    aaa(%rip), %esi
we can see that, 
only the load of “ccc” got hoisted into the %entry block,
while the load of “bbb” and “aaa” are still inside the %while.body.</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>