This file is indexed.

/usr/share/doc/llvm-6.0-doc/html/BranchWeightMetadata.html is in llvm-6.0-doc 1:6.0-1ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>LLVM Branch Weight Metadata &#8212; LLVM 6 documentation</title>
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '6',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="LLVM bugpoint tool: design and usage" href="Bugpoint.html" />
    <link rel="prev" title="LLVM Block Frequency Terminology" href="BlockFrequencyTerminology.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head>
  <body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="Bugpoint.html" title="LLVM bugpoint tool: design and usage"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="BlockFrequencyTerminology.html" title="LLVM Block Frequency Terminology"
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>
 
      </ul>
    </div>


    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <div class="section" id="llvm-branch-weight-metadata">
<h1>LLVM Branch Weight Metadata<a class="headerlink" href="#llvm-branch-weight-metadata" title="Permalink to this headline"></a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#introduction" id="id1">Introduction</a></li>
<li><a class="reference internal" href="#supported-instructions" id="id2">Supported Instructions</a><ul>
<li><a class="reference internal" href="#branchinst" id="id3"><code class="docutils literal"><span class="pre">BranchInst</span></code></a></li>
<li><a class="reference internal" href="#switchinst" id="id4"><code class="docutils literal"><span class="pre">SwitchInst</span></code></a></li>
<li><a class="reference internal" href="#indirectbrinst" id="id5"><code class="docutils literal"><span class="pre">IndirectBrInst</span></code></a></li>
<li><a class="reference internal" href="#callinst" id="id6"><code class="docutils literal"><span class="pre">CallInst</span></code></a></li>
<li><a class="reference internal" href="#other" id="id7">Other</a></li>
</ul>
</li>
<li><a class="reference internal" href="#built-in-expect-instructions" id="id8">Built-in <code class="docutils literal"><span class="pre">expect</span></code> Instructions</a><ul>
<li><a class="reference internal" href="#if-statement" id="id9"><code class="docutils literal"><span class="pre">if</span></code> statement</a></li>
<li><a class="reference internal" href="#switch-statement" id="id10"><code class="docutils literal"><span class="pre">switch</span></code> statement</a></li>
</ul>
</li>
<li><a class="reference internal" href="#cfg-modifications" id="id11">CFG Modifications</a></li>
<li><a class="reference internal" href="#function-entry-counts" id="id12">Function Entry Counts</a></li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id1">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline"></a></h2>
<p>Branch Weight Metadata represents branch weights as its likeliness to be taken
(see <a class="reference internal" href="BlockFrequencyTerminology.html"><span class="doc">LLVM Block Frequency Terminology</span></a>). Metadata is assigned to the
<code class="docutils literal"><span class="pre">TerminatorInst</span></code> as a <code class="docutils literal"><span class="pre">MDNode</span></code> of the <code class="docutils literal"><span class="pre">MD_prof</span></code> kind. The first operator
is always a <code class="docutils literal"><span class="pre">MDString</span></code> node with the string “branch_weights”.  Number of
operators depends on the terminator type.</p>
<p>Branch weights might be fetch from the profiling file, or generated based on
<a class="reference internal" href="#builtin-expect">__builtin_expect</a> instruction.</p>
<p>All weights are represented as an unsigned 32-bit values, where higher value
indicates greater chance to be taken.</p>
</div>
<div class="section" id="supported-instructions">
<h2><a class="toc-backref" href="#id2">Supported Instructions</a><a class="headerlink" href="#supported-instructions" title="Permalink to this headline"></a></h2>
<div class="section" id="branchinst">
<h3><a class="toc-backref" href="#id3"><code class="docutils literal"><span class="pre">BranchInst</span></code></a><a class="headerlink" href="#branchinst" title="Permalink to this headline"></a></h3>
<p>Metadata is only assigned to the conditional branches. There are two extra
operands for the true and the false branch.</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;TRUE_BRANCH_WEIGHT&gt;,
  i32 &lt;FALSE_BRANCH_WEIGHT&gt;
}
</pre></div>
</div>
</div>
<div class="section" id="switchinst">
<h3><a class="toc-backref" href="#id4"><code class="docutils literal"><span class="pre">SwitchInst</span></code></a><a class="headerlink" href="#switchinst" title="Permalink to this headline"></a></h3>
<p>Branch weights are assigned to every case (including the <code class="docutils literal"><span class="pre">default</span></code> case which
is always case #0).</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;DEFAULT_BRANCH_WEIGHT&gt;
  [ , i32 &lt;CASE_BRANCH_WEIGHT&gt; ... ]
}
</pre></div>
</div>
</div>
<div class="section" id="indirectbrinst">
<h3><a class="toc-backref" href="#id5"><code class="docutils literal"><span class="pre">IndirectBrInst</span></code></a><a class="headerlink" href="#indirectbrinst" title="Permalink to this headline"></a></h3>
<p>Branch weights are assigned to every destination.</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;LABEL_BRANCH_WEIGHT&gt;
  [ , i32 &lt;LABEL_BRANCH_WEIGHT&gt; ... ]
}
</pre></div>
</div>
</div>
<div class="section" id="callinst">
<h3><a class="toc-backref" href="#id6"><code class="docutils literal"><span class="pre">CallInst</span></code></a><a class="headerlink" href="#callinst" title="Permalink to this headline"></a></h3>
<p>Calls may have branch weight metadata, containing the execution count of
the call. It is currently used in SamplePGO mode only, to augment the
block and entry counts which may not be accurate with sampling.</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;CALL_BRANCH_WEIGHT&gt;
}
</pre></div>
</div>
</div>
<div class="section" id="other">
<h3><a class="toc-backref" href="#id7">Other</a><a class="headerlink" href="#other" title="Permalink to this headline"></a></h3>
<p>Other terminator instructions are not allowed to contain Branch Weight Metadata.</p>
</div>
</div>
<div class="section" id="built-in-expect-instructions">
<span id="builtin-expect"></span><h2><a class="toc-backref" href="#id8">Built-in <code class="docutils literal"><span class="pre">expect</span></code> Instructions</a><a class="headerlink" href="#built-in-expect-instructions" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal"><span class="pre">__builtin_expect(long</span> <span class="pre">exp,</span> <span class="pre">long</span> <span class="pre">c)</span></code> instruction provides branch prediction
information. The return value is the value of <code class="docutils literal"><span class="pre">exp</span></code>.</p>
<p>It is especially useful in conditional statements. Currently Clang supports two
conditional statements:</p>
<div class="section" id="if-statement">
<h3><a class="toc-backref" href="#id9"><code class="docutils literal"><span class="pre">if</span></code> statement</a><a class="headerlink" href="#if-statement" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal"><span class="pre">exp</span></code> parameter is the condition. The <code class="docutils literal"><span class="pre">c</span></code> parameter is the expected
comparison value. If it is equal to 1 (true), the condition is likely to be
true, in other case condition is likely to be false. For example:</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">__builtin_expect</span><span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="p">{</span>
  <span class="c1">// This block is likely to be taken.</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="switch-statement">
<h3><a class="toc-backref" href="#id10"><code class="docutils literal"><span class="pre">switch</span></code> statement</a><a class="headerlink" href="#switch-statement" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal"><span class="pre">exp</span></code> parameter is the value. The <code class="docutils literal"><span class="pre">c</span></code> parameter is the expected
value. If the expected value doesn’t show on the cases list, the <code class="docutils literal"><span class="pre">default</span></code>
case is assumed to be likely taken.</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">switch</span> <span class="p">(</span><span class="n">__builtin_expect</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span> <span class="p">{</span>
<span class="k">default</span><span class="o">:</span> <span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="mi">0</span><span class="o">:</span>  <span class="c1">// ...</span>
<span class="k">case</span> <span class="mi">3</span><span class="o">:</span>  <span class="c1">// ...</span>
<span class="k">case</span> <span class="mi">5</span><span class="o">:</span>  <span class="c1">// This case is likely to be taken.</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="cfg-modifications">
<h2><a class="toc-backref" href="#id11">CFG Modifications</a><a class="headerlink" href="#cfg-modifications" title="Permalink to this headline"></a></h2>
<p>Branch Weight Metatada is not proof against CFG changes. If terminator operands’
are changed some action should be taken. In other case some misoptimizations may
occur due to incorrect branch prediction information.</p>
</div>
<div class="section" id="function-entry-counts">
<h2><a class="toc-backref" href="#id12">Function Entry Counts</a><a class="headerlink" href="#function-entry-counts" title="Permalink to this headline"></a></h2>
<p>To allow comparing different functions during inter-procedural analysis and
optimization, <code class="docutils literal"><span class="pre">MD_prof</span></code> nodes can also be assigned to a function definition.
The first operand is a string indicating the name of the associated counter.</p>
<p>Currently, one counter is supported: “function_entry_count”. The second operand
is a 64-bit counter that indicates the number of times that this function was
invoked (in the case of instrumentation-based profiles). In the case of
sampling-based profiles, this operand is an approximation of how many times
the function was invoked.</p>
<p>For example, in the code below, the instrumentation for function foo()
indicates that it was called 2,590 times at runtime.</p>
<div class="highlight-llvm"><div class="highlight"><pre><span></span><span class="k">define</span> <span class="k">i32</span> <span class="vg">@foo</span><span class="p">()</span> <span class="nv">!prof</span> <span class="nv nv-Anonymous">!1</span> <span class="p">{</span>
  <span class="k">ret</span> <span class="k">i32</span> <span class="m">0</span>
<span class="p">}</span>
<span class="nv nv-Anonymous">!1</span> <span class="p">=</span> <span class="p">!{</span><span class="nv">!&quot;function_entry_count&quot;</span><span class="p">,</span> <span class="k">i64</span> <span class="m">2590</span><span class="p">}</span>
</pre></div>
</div>
<p>If “function_entry_count” has more than 2 operands, the later operands are
the GUID of the functions that needs to be imported by ThinLTO. This is only
set by sampling based profile. It is needed because the sampling based profile
was collected on a binary that had already imported and inlined these functions,
and we need to ensure the IR matches in the ThinLTO backends for profile
annotation. The reason why we cannot annotate this on the callsite is that it
can only goes down 1 level in the call chain. For the cases where
foo_in_a_cc()-&gt;bar_in_b_cc()-&gt;baz_in_c_cc(), we will need to go down 2 levels
in the call chain to import both bar_in_b_cc and baz_in_c_cc.</p>
</div>
</div>


          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="Bugpoint.html" title="LLVM bugpoint tool: design and usage"
             >next</a> |</li>
        <li class="right" >
          <a href="BlockFrequencyTerminology.html" title="LLVM Block Frequency Terminology"
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>
 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2003-2018, LLVM Project.
      Last updated on 2018-04-05.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
    </div>
  </body>
</html>