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