Bug ID: 41110
           Summary: Avoid generating branch in branchless code
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations

struct edge {
    int from;
    int to;

int edge_to_rank(struct edge e, size_t n) {
    return e.from < ? - 1 : e.from - 1 + n / 2;

Clang -O3:
edge_to_rank(edge, unsigned long): # @edge_to_rank(edge, unsigned long)
  mov rax, rdi
  movabs rcx, -4294967296
  mov rdx, rdi
  shr rdx, 32
  cmp eax, edx
  jge .LBB0_2
  add rax, rcx
  sar rax, 32
  shl rax, 32
  add rax, rcx
  sar rax, 32
  shr rsi
  add rax, rsi

GCC -O3:
edge_to_rank(edge, unsigned long):
  mov rax, rdi
  sar rax, 32
  cmp edi, eax
  jge .L2
  dec eax
  shr rsi
  lea eax, [rdi-1+rsi]

edge_to_rank(edge, unsigned long):
        mov       rax, rdi                                      #9.21
        movsxd    rdx, edi                                      #9.48
        shr       rsi, 1                                        #9.56
        shr       rax, 32                                       #9.21
        movsxd    rcx, eax                                      #9.35
        dec       rcx                                           #9.35
        cmp       edi, eax                                      #9.21
        lea       r8, QWORD PTR [-1+rdx+rsi]                    #9.56
        cmovl     r8, rcx                                       #9.21
        mov       eax, r8d                                      #9.21

I think Clang trunk generates supoptimal code. GCC's output is small and nice
(maybe three op LEA should not be used), but with a branch...

ICC generates the code I would expect - branchless using cmov.

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

Reply via email to