Bug ID: 30692
           Summary: Failing to hoist all loads out of a loop
           Product: tools
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: opt
    Classification: Unclassified

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)

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

Here is the assembly.
$ cat t.s

        .file   "t.cpp"
        .globl  _Z3fooPKi
        .p2align        4, 0x90
        .type   _Z3fooPKi,@function
_Z3fooPKi:                              # @_Z3fooPKi
# 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
        .size   _Z3fooPKi, .Lfunc_end0-_Z3fooPKi

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

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

        .type   ccc,@object             # @ccc
        .globl  ccc
        .p2align        2
        .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.

You are receiving this mail because:
You are on the CC list for the bug.
llvm-bugs mailing list

Reply via email to