<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Avoid generating branch in branchless code"
   href="https://bugs.llvm.org/show_bug.cgi?id=41110">41110</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Avoid generating branch in branchless code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>struct edge {
    int from;
    int to;
};

int edge_to_rank(struct edge e, size_t n) {
    return e.from < e.to ? e.to - 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
  ret
.LBB0_2:
  shl rax, 32
  add rax, rcx
  sar rax, 32
  shr rsi
  add rax, rsi
  ret

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

ICC -O3
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
        ret     


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.

<a href="https://godbolt.org/z/z6uzsn">https://godbolt.org/z/z6uzsn</a></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>