blob: 38b87baa3b708d82947d4e5156ebdae650a687e3 [file] [log] [blame]
Jakub Staszak29057762011-07-06 18:31:02 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
NAKAMURA Takumi5c6e4df2011-10-31 11:21:59 +00005 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Jakub Staszak29057762011-07-06 18:31:02 +00006 <title>LLVM Branch Weight Metadata</title>
7 <link rel="stylesheet" href="llvm.css" type="text/css">
8</head>
9<body>
10
11<h1>
12 LLVM Branch Weight Metadata
13</h1>
14
15<ol>
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>
20</ol>
21
22<div class="doc_author">
23 <p>Written by <a href="mailto:jstaszak@apple.com">Jakub Staszak</a></p>
24</div>
25
26<h2>
27 <a name="introduction">Introduction</a>
28</h2>
29<div>
30<p>Branch Weight Metadata represents branch weights as its likeliness to
31be 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
34depends on the terminator type.</p>
35
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.
38</p>
39
40<p>All weights are represented as an unsigned 32-bit values, where higher value
41indicates greater chance to be taken.</p>
42</div>
43
44<h2>
45 <a name="supported_instructions">Supported Instructions</a>
46</h2>
47
48<div>
49 <h4>BranchInst</h4>
50 <div>
51 <p>Metadata is only assign to the conditional branches. There are two extra
52 operarands, for the true and the false branch.</p>
53 </div>
54 <div class="doc_code">
55 <pre>
56!0 = metadata !{
57 metadata !"branch_weights",
58 i32 &lt;TRUE_BRANCH_WEIGHT&gt;,
59 i32 &lt;FALSE_BRANCH_WEIGHT&gt;
60}
61 </pre>
62 </div>
63
64 <h4>SwitchInst</h4>
65 <div>
66 <p>Branch weights are assign to every case (including <tt>default</tt> case
67 which is always case #0).</p>
68 </div>
69 <div class="doc_code">
70 <pre>
71!0 = metadata !{
72 metadata !"branch_weights",
73 i32 &lt;DEFAULT_BRANCH_WEIGHT&gt;
74 [ , i32 &lt;CASE_BRANCH_WEIGHT&gt; ... ]
75}
76 </pre>
77 </div>
78
79 <h4>IndirectBrInst</h4>
80 <div>
81 <p>Branch weights are assign to every destination.</p>
82 </div>
83 <div class="doc_code">
84 <pre>
85!0 = metadata !{
86 metadata !"branch_weights",
87 i32 &lt;LABEL_BRANCH_WEIGHT&gt;
88 [ , i32 &lt;LABEL_BRANCH_WEIGHT&gt; ... ]
89}
90 </pre>
91 </div>
92
93 <h4>Other</h4>
94 <div>
95 <p>Other terminator instructions are not allowed to contain Branch Weight
96 Metadata.</p>
97 </div>
98</div>
99
100<h2>
101 <a name="builtin_expect">Built-in "expect" Instructions</a>
102</h2>
103<div>
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>
106
107 <p>It is especially useful in conditional statements. Currently Clang supports
108 two conditional statements:
109 </p>
110 <h4><tt>if</tt> statement</h4>
111 <div>
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:
115 </p>
116 </div>
117 <div class="doc_code">
118 <pre>
119 if (__builtin_expect(x &gt; 0, 1)) {
120 // This block is likely to be taken.
121 }
122 </pre>
123 </div>
124
125 <h4><tt>switch</tt> statement</h4>
126 <div>
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>
130 </div>
131 <div class="doc_code">
132 <pre>
133 switch (__builtin_expect(x, 5)) {
134 default: break;
135 case 0: // ...
136 case 3: // ...
137 case 5: // This case is likely to be taken.
138 }
139 </pre>
140 </div>
141</div>
142
143<h2>
144 <a name="cfg_modifications">CFG Modifications</a>
145</h2>
146<div>
147<p>Branch Weight Metatada is not proof against CFG changes. If terminator
148operands' are changed some action should be taken. In other case some
149misoptimizations may occur due to incorrent branch prediction information.</p>
150</div>
151
152<hr>
153<address>
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>
158
159 <a href="mailto:jstaszak@apple.com">Jakub Staszak</a><br>
160 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
161</address>
162
163</body>
164</html>