/3rd_party/llvm/docs/BranchWeightMetadata.html

https://code.google.com/p/softart/ · HTML · 164 lines · 146 code · 18 blank · 0 comment · 0 complexity · aa145c6dde0adcc88e65b49eeb829abd MD5 · raw file

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  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">
  8. </head>
  9. <body>
  10. <h1>
  11. LLVM Branch Weight Metadata
  12. </h1>
  13. <ol>
  14. <li><a href="#introduction">Introduction</a></li>
  15. <li><a href="#supported_instructions">Supported Instructions</a></li>
  16. <li><a href="#builtin_expect">Built-in "expect" Instruction </a></li>
  17. <li><a href="#cfg_modifications">CFG Modifications</a></li>
  18. </ol>
  19. <div class="doc_author">
  20. <p>Written by <a href="mailto:jstaszak@apple.com">Jakub Staszak</a></p>
  21. </div>
  22. <h2>
  23. <a name="introduction">Introduction</a>
  24. </h2>
  25. <div>
  26. <p>Branch Weight Metadata represents branch weights as its likeliness to
  27. be taken. Metadata is assigned to the <tt>TerminatorInst</tt> as a
  28. <tt>MDNode</tt> of the <tt>MD_prof</tt> kind. The first operator is always a
  29. <tt>MDString</tt> node with the string "branch_weights". Number of operators
  30. depends on the terminator type.</p>
  31. <p>Branch weights might be fetch from the profiling file, or generated based on
  32. <a href="#builtin_expect"><tt>__builtin_expect</tt></a> instruction.
  33. </p>
  34. <p>All weights are represented as an unsigned 32-bit values, where higher value
  35. indicates greater chance to be taken.</p>
  36. </div>
  37. <h2>
  38. <a name="supported_instructions">Supported Instructions</a>
  39. </h2>
  40. <div>
  41. <h4>BranchInst</h4>
  42. <div>
  43. <p>Metadata is only assign to the conditional branches. There are two extra
  44. operarands, for the true and the false branch.</p>
  45. </div>
  46. <div class="doc_code">
  47. <pre>
  48. !0 = metadata !{
  49. metadata !"branch_weights",
  50. i32 &lt;TRUE_BRANCH_WEIGHT&gt;,
  51. i32 &lt;FALSE_BRANCH_WEIGHT&gt;
  52. }
  53. </pre>
  54. </div>
  55. <h4>SwitchInst</h4>
  56. <div>
  57. <p>Branch weights are assign to every case (including <tt>default</tt> case
  58. which is always case #0).</p>
  59. </div>
  60. <div class="doc_code">
  61. <pre>
  62. !0 = metadata !{
  63. metadata !"branch_weights",
  64. i32 &lt;DEFAULT_BRANCH_WEIGHT&gt;
  65. [ , i32 &lt;CASE_BRANCH_WEIGHT&gt; ... ]
  66. }
  67. </pre>
  68. </div>
  69. <h4>IndirectBrInst</h4>
  70. <div>
  71. <p>Branch weights are assign to every destination.</p>
  72. </div>
  73. <div class="doc_code">
  74. <pre>
  75. !0 = metadata !{
  76. metadata !"branch_weights",
  77. i32 &lt;LABEL_BRANCH_WEIGHT&gt;
  78. [ , i32 &lt;LABEL_BRANCH_WEIGHT&gt; ... ]
  79. }
  80. </pre>
  81. </div>
  82. <h4>Other</h4>
  83. <div>
  84. <p>Other terminator instructions are not allowed to contain Branch Weight
  85. Metadata.</p>
  86. </div>
  87. </div>
  88. <h2>
  89. <a name="builtin_expect">Built-in "expect" Instructions</a>
  90. </h2>
  91. <div>
  92. <p><tt>__builtin_expect(long exp, long c)</tt> instruction provides branch
  93. prediction information. The return value is the value of <tt>exp</tt>.</p>
  94. <p>It is especially useful in conditional statements. Currently Clang supports
  95. two conditional statements:
  96. </p>
  97. <h4><tt>if</tt> statement</h4>
  98. <div>
  99. <p>The <tt>exp</tt> parameter is the condition. The <tt>c</tt> parameter is
  100. the expected comparision value. If it is equal to 1 (true), the condition is
  101. likely to be true, in other case condition is likely to be false. For example:
  102. </p>
  103. </div>
  104. <div class="doc_code">
  105. <pre>
  106. if (__builtin_expect(x &gt; 0, 1)) {
  107. // This block is likely to be taken.
  108. }
  109. </pre>
  110. </div>
  111. <h4><tt>switch</tt> statement</h4>
  112. <div>
  113. <p>The <tt>exp</tt> parameter is the value. The <tt>c</tt> parameter is the
  114. expected value. If the expected value doesn't show on the cases list, the
  115. <tt>default</tt> case is assumed to be likely taken.</p>
  116. </div>
  117. <div class="doc_code">
  118. <pre>
  119. switch (__builtin_expect(x, 5)) {
  120. default: break;
  121. case 0: // ...
  122. case 3: // ...
  123. case 5: // This case is likely to be taken.
  124. }
  125. </pre>
  126. </div>
  127. </div>
  128. <h2>
  129. <a name="cfg_modifications">CFG Modifications</a>
  130. </h2>
  131. <div>
  132. <p>Branch Weight Metatada is not proof against CFG changes. If terminator
  133. operands' are changed some action should be taken. In other case some
  134. misoptimizations may occur due to incorrent branch prediction information.</p>
  135. </div>
  136. <hr>
  137. <address>
  138. <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
  139. src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
  140. <a href="http://validator.w3.org/check/referer"><img
  141. src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
  142. <a href="mailto:jstaszak@apple.com">Jakub Staszak</a><br>
  143. <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
  144. </address>
  145. </body>
  146. </html>