<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 --- - Honor __builtin_expect through inlining"
   href="http://llvm.org/bugs/show_bug.cgi?id=19230">19230</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Honor __builtin_expect through inlining
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Transformation Utilities
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>wielkiegie@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=12269" name="attach_12269" title="Test case">attachment 12269</a> <a href="attachment.cgi?id=12269&action=edit" title="Test case">[details]</a></span>
Test case

A common usage model of __builtin_expect is to create helper macros called
'likely' and 'unlikely':

#if __has_builtin(__builtin_expect)
#   define likely(x)   __builtin_expect(!!(x), 1)
#   define unlikely(x) __builtin_expect(!!(x), 0)
#else
#   define likely(x)   (x)
#   define unlikely(x) (x)
#endif

However using macros (especially in C++) has a major drawback: short names
pollute namespace and long ones are harder to use.

I wanted to fix this by using inline functions (which can reside in a
namespace) instead of macros:

#if __has_builtin(__builtin_expect)
inline bool likely  (const bool x) { return __builtin_expect(x, 1); }
inline bool unlikely(const bool x) { return __builtin_expect(x, 0); }
#else
inline bool likely  (const bool x) { return x; }
inline bool unlikely(const bool x) { return x; }
#endif

These inline functions above work as expected in GCC but unfortunately not in
LLVM/Clang (tested both with -O3). Apparently, the branch weight metadata from
llvm.expect intrinsic are dropped before the inlining occurs. Doing the
inlining explicitly before any other optimizations fixes this problem.

$ clang++ llvm-expect.cpp -o - -emit-llvm -c -O3 -DUSE_MACROS | llvm-dis | grep
branch_weights
!1 = metadata !{metadata !"branch_weights", i32 64, i32 4}
$ clang++ llvm-expect.cpp -o - -emit-llvm -c -O3 | llvm-dis | grep
branch_weights
$ clang++ llvm-expect.cpp -o - -emit-llvm -c | opt -inline | opt -O3 | llvm-dis
| grep branch_weights
!1 = metadata !{metadata !"branch_weights", i32 64, i32 4}</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>