[llvm-bugs] [Bug 33270] New: tail duplication failed to happen when BB's pred (within the same loop) has >1 succ

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jun 1 12:47:27 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=33270

            Bug ID: 33270
           Summary: tail duplication failed to happen when BB's pred
                    (within the same loop) has >1 succ
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: danielcdh at gmail.com
                CC: llvm-bugs at lists.llvm.org

For the following testcase, gcc managed to tail-duplicate the loop header to
make the loop more efficient (when the call pattern is foo-bar-foo-bar, the
gcc-generated-code saves 2 taken branches every foo-bar call pair.

The issue is that llvm will not duplicate a BB if any of BB's predecessor
(within the same loop) has multiple successors. As a result, the loop header
will not be duplicated if it contains a "continue" in the loop body.

$cat test.cc
void foo();
void bar();
bool test1();
bool test2();
void func() {
  while (1) {
    if (__builtin_expect(test1(), 0)) {
      foo();
    } else {
      if (test2())
        continue;
      bar();
    }
  }
}


$clang -O2 -fno-unroll-loops -S test.cc  -o - -mllvm
-tail-dup-placement-threshold=50

        .text
        .file   "test.cc"
        .globl  _Z4funcv                # -- Begin function _Z4funcv
        .p2align        4, 0x90
        .type   _Z4funcv, at function
_Z4funcv:                               # @_Z4funcv
        .cfi_startproc
# BB#0:
        pushq   %rax
.Lcfi0:
        .cfi_def_cfa_offset 16
        jmp     .LBB0_1
        .p2align        4, 0x90
.LBB0_4:                                #   in Loop: Header=BB0_1 Depth=1
        callq   _Z3barv
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        callq   _Z5test1v
        testb   %al, %al
        jne     .LBB0_2
# BB#3:                                 #   in Loop: Header=BB0_1 Depth=1
        callq   _Z5test2v
        testb   %al, %al
        jne     .LBB0_1
        jmp     .LBB0_4
.LBB0_2:                                #   in Loop: Header=BB0_1 Depth=1
        callq   _Z3foov
        jmp     .LBB0_1
.Lfunc_end0:
        .size   _Z4funcv, .Lfunc_end0-_Z4funcv
        .cfi_endproc
                                        # -- End function

        .ident  "clang version 5.0.0 (trunk 304371)"
        .section        ".note.GNU-stack","", at progbits


$g++ -O2 -fno-unroll-loops -S test.cc -o -
        .file   "test.cc"
        .text
        .p2align 4,,15
        .globl  _Z4funcv
        .type   _Z4funcv, @function
_Z4funcv:
.LFB0:
        .cfi_startproc
        subq    $8, %rsp
        .cfi_def_cfa_offset 16
        .p2align 5,,20
        .p2align 3
.L2:
        call    _Z5test1v
        testb   %al, %al
        jne     .L7
.L3:
        call    _Z5test2v
        testb   %al, %al
        jne     .L2
        call    _Z3barv
        call    _Z5test1v
        testb   %al, %al
        je      .L3
.L7:
        call    _Z3foov
        jmp     .L2
        .cfi_endproc
.LFE0:
        .size   _Z4funcv, .-_Z4funcv
        .ident  "GCC: (GNU) 4.9.x-google 20150123 (prerelease)"
        .section        .note.GNU-stack,"", at progbits

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170601/8ea66370/attachment.html>


More information about the llvm-bugs mailing list