1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>LLVM Branch Weight Metadata
</title>
6 <link rel=
"stylesheet" href=
"llvm.css" type=
"text/css">
11 LLVM Branch Weight Metadata
15 <li><a href=
"#introduction">Introduction
</a></li>
16 <li><a href=
"#supported_instructions">Supported Instructions
</a></li>
17 <li><a href=
"#builtin_expect">Built-in
"expect" Instruction
</a></li>
18 <li><a href=
"#cfg_modifications">CFG Modifications
</a></li>
21 <div class=
"doc_author">
22 <p>Written by
<a href=
"mailto:jstaszak@apple.com">Jakub Staszak
</a></p>
26 <a name=
"introduction">Introduction
</a>
29 <p>Branch Weight Metadata represents branch weights as its likeliness to
30 be taken. Metadata is assigned to the
<tt>TerminatorInst
</tt> as a
31 <tt>MDNode
</tt> of the
<tt>MD_prof
</tt> kind. The first operator is always a
32 <tt>MDString
</tt> node with the string
"branch_weights". Number of operators
33 depends on the terminator type.
</p>
35 <p>Branch weights might be fetch from the profiling file, or generated based on
36 <a href=
"#builtin_expect"><tt>__builtin_expect
</tt></a> instruction.
39 <p>All weights are represented as an unsigned
32-bit values, where higher value
40 indicates greater chance to be taken.
</p>
44 <a name=
"supported_instructions">Supported Instructions
</a>
50 <p>Metadata is only assign to the conditional branches. There are two extra
51 operarands, for the true and the false branch.
</p>
53 <div class=
"doc_code">
56 metadata !
"branch_weights",
57 i32
<TRUE_BRANCH_WEIGHT
>,
58 i32
<FALSE_BRANCH_WEIGHT
>
65 <p>Branch weights are assign to every case (including
<tt>default
</tt> case
66 which is always case #
0).
</p>
68 <div class=
"doc_code">
71 metadata !
"branch_weights",
72 i32
<DEFAULT_BRANCH_WEIGHT
>
73 [ , i32
<CASE_BRANCH_WEIGHT
> ... ]
78 <h4>IndirectBrInst
</h4>
80 <p>Branch weights are assign to every destination.
</p>
82 <div class=
"doc_code">
85 metadata !
"branch_weights",
86 i32
<LABEL_BRANCH_WEIGHT
>
87 [ , i32
<LABEL_BRANCH_WEIGHT
> ... ]
94 <p>Other terminator instructions are not allowed to contain Branch Weight
100 <a name=
"builtin_expect">Built-in
"expect" Instructions
</a>
103 <p><tt>__builtin_expect(long exp, long c)
</tt> instruction provides branch
104 prediction information. The return value is the value of
<tt>exp
</tt>.
</p>
106 <p>It is especially useful in conditional statements. Currently Clang supports
107 two conditional statements:
109 <h4><tt>if
</tt> statement
</h4>
111 <p>The
<tt>exp
</tt> parameter is the condition. The
<tt>c
</tt> parameter is
112 the expected comparision value. If it is equal to
1 (true), the condition is
113 likely to be true, in other case condition is likely to be false. For example:
116 <div class=
"doc_code">
118 if (__builtin_expect(x
> 0,
1)) {
119 // This block is likely to be taken.
124 <h4><tt>switch
</tt> statement
</h4>
126 <p>The
<tt>exp
</tt> parameter is the value. The
<tt>c
</tt> parameter is the
127 expected value. If the expected value doesn't show on the cases list, the
128 <tt>default
</tt> case is assumed to be likely taken.
</p>
130 <div class=
"doc_code">
132 switch (__builtin_expect(x,
5)) {
136 case
5: // This case is likely to be taken.
143 <a name=
"cfg_modifications">CFG Modifications
</a>
146 <p>Branch Weight Metatada is not proof against CFG changes. If terminator
147 operands' are changed some action should be taken. In other case some
148 misoptimizations may occur due to incorrent branch prediction information.
</p>
153 <a href=
"http://jigsaw.w3.org/css-validator/check/referer"><img
154 src=
"http://jigsaw.w3.org/css-validator/images/vcss-blue" alt=
"Valid CSS"></a>
155 <a href=
"http://validator.w3.org/check/referer"><img
156 src=
"http://www.w3.org/Icons/valid-html401-blue" alt=
"Valid HTML 4.01"></a>
158 <a href=
"mailto:jstaszak@apple.com">Jakub Staszak
</a><br>
159 <a href=
"http://llvm.org/">LLVM Compiler Infrastructure
</a><br>