1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <title>LLVM Branch Weight Metadata</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
12 LLVM Branch Weight Metadata
16 <li><a href="#introduction">Introduction</a></li>
17 <li><a href="#supported_instructions">Supported Instructions</a></li>
18 <li><a href="#builtin_expect">Built-in "expect" Instruction </a></li>
19 <li><a href="#cfg_modifications">CFG Modifications</a></li>
22 <div class="doc_author">
23 <p>Written by <a href="mailto:jstaszak@apple.com">Jakub Staszak</a></p>
27 <a name="introduction">Introduction</a>
30 <p>Branch Weight Metadata represents branch weights as its likeliness to
31 be taken. Metadata is assigned to the <tt>TerminatorInst</tt> as a
32 <tt>MDNode</tt> of the <tt>MD_prof</tt> kind. The first operator is always a
33 <tt>MDString</tt> node with the string "branch_weights". Number of operators
34 depends on the terminator type.</p>
36 <p>Branch weights might be fetch from the profiling file, or generated based on
37 <a href="#builtin_expect"><tt>__builtin_expect</tt></a> instruction.
40 <p>All weights are represented as an unsigned 32-bit values, where higher value
41 indicates greater chance to be taken.</p>
45 <a name="supported_instructions">Supported Instructions</a>
51 <p>Metadata is only assign to the conditional branches. There are two extra
52 operarands, for the true and the false branch.</p>
54 <div class="doc_code">
57 metadata !"branch_weights",
58 i32 <TRUE_BRANCH_WEIGHT>,
59 i32 <FALSE_BRANCH_WEIGHT>
66 <p>Branch weights are assign to every case (including <tt>default</tt> case
67 which is always case #0).</p>
69 <div class="doc_code">
72 metadata !"branch_weights",
73 i32 <DEFAULT_BRANCH_WEIGHT>
74 [ , i32 <CASE_BRANCH_WEIGHT> ... ]
79 <h4>IndirectBrInst</h4>
81 <p>Branch weights are assign to every destination.</p>
83 <div class="doc_code">
86 metadata !"branch_weights",
87 i32 <LABEL_BRANCH_WEIGHT>
88 [ , i32 <LABEL_BRANCH_WEIGHT> ... ]
95 <p>Other terminator instructions are not allowed to contain Branch Weight
101 <a name="builtin_expect">Built-in "expect" Instructions</a>
104 <p><tt>__builtin_expect(long exp, long c)</tt> instruction provides branch
105 prediction information. The return value is the value of <tt>exp</tt>.</p>
107 <p>It is especially useful in conditional statements. Currently Clang supports
108 two conditional statements:
110 <h4><tt>if</tt> statement</h4>
112 <p>The <tt>exp</tt> parameter is the condition. The <tt>c</tt> parameter is
113 the expected comparision value. If it is equal to 1 (true), the condition is
114 likely to be true, in other case condition is likely to be false. For example:
117 <div class="doc_code">
119 if (__builtin_expect(x > 0, 1)) {
120 // This block is likely to be taken.
125 <h4><tt>switch</tt> statement</h4>
127 <p>The <tt>exp</tt> parameter is the value. The <tt>c</tt> parameter is the
128 expected value. If the expected value doesn't show on the cases list, the
129 <tt>default</tt> case is assumed to be likely taken.</p>
131 <div class="doc_code">
133 switch (__builtin_expect(x, 5)) {
137 case 5: // This case is likely to be taken.
144 <a name="cfg_modifications">CFG Modifications</a>
147 <p>Branch Weight Metatada is not proof against CFG changes. If terminator
148 operands' are changed some action should be taken. In other case some
149 misoptimizations may occur due to incorrent branch prediction information.</p>
154 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
155 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
156 <a href="http://validator.w3.org/check/referer"><img
157 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
159 <a href="mailto:jstaszak@apple.com">Jakub Staszak</a><br>
160 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>