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: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected],
                    [email protected]

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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to