[llvm-bugs] [Bug 47906] New: Suboptimal code for branch on ctpop(x) == 1

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Oct 19 13:44:50 PDT 2020


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

            Bug ID: 47906
           Summary: Suboptimal code for branch on ctpop(x) == 1
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: craig.topper at gmail.com
                CC: craig.topper at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, pengfei.wang at intel.com,
                    spatel+llvm at rotateright.com

This code produces suboptimal code with clang

void bar();

void foo(int x) {
  if (x && (x & (x - 1)) == 0)
    bar();
}

clang:
foo(int):                                # @foo(int)
        leal    -1(%rdi), %eax
        testl   %eax, %edi
        setne   %al
        testl   %edi, %edi
        sete    %cl
        orb     %al, %cl
        jne     .LBB0_1
        jmp     bar()                         # TAILCALL
.LBB0_1:
        retq

gcc:
foo(int):
        test    edi, edi
        je      .L1
        lea     eax, [rdi-1]
        test    eax, edi
        je      .L7
.L1:
        ret
.L7:
        jmp     bar()


The condition is canonicalized to ctpop(x) == 1 in InstCombine. Then expanded
back into two conditionals during SelectionDAG. SelectionDAG doesn't have any
optimizations for changing a branch on 2 setccs ored together into separate
branches. I'm not sure if we could add that or if we should expand the ctpop in
CodeGenPrepare.

-- 
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/20201019/dbb8355b/attachment.html>


More information about the llvm-bugs mailing list