BranchWeightMetadata.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ===========================
  2. LLVM Branch Weight Metadata
  3. ===========================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. Branch Weight Metadata represents branch weights as its likeliness to be taken
  9. (see :doc:`BlockFrequencyTerminology`). Metadata is assigned to the
  10. ``TerminatorInst`` as a ``MDNode`` of the ``MD_prof`` kind. The first operator
  11. is always a ``MDString`` node with the string "branch_weights". Number of
  12. operators depends on the terminator type.
  13. Branch weights might be fetch from the profiling file, or generated based on
  14. `__builtin_expect`_ instruction.
  15. All weights are represented as an unsigned 32-bit values, where higher value
  16. indicates greater chance to be taken.
  17. Supported Instructions
  18. ======================
  19. ``BranchInst``
  20. ^^^^^^^^^^^^^^
  21. Metadata is only assigned to the conditional branches. There are two extra
  22. operarands for the true and the false branch.
  23. .. code-block:: llvm
  24. !0 = metadata !{
  25. metadata !"branch_weights",
  26. i32 <TRUE_BRANCH_WEIGHT>,
  27. i32 <FALSE_BRANCH_WEIGHT>
  28. }
  29. ``SwitchInst``
  30. ^^^^^^^^^^^^^^
  31. Branch weights are assigned to every case (including the ``default`` case which
  32. is always case #0).
  33. .. code-block:: llvm
  34. !0 = metadata !{
  35. metadata !"branch_weights",
  36. i32 <DEFAULT_BRANCH_WEIGHT>
  37. [ , i32 <CASE_BRANCH_WEIGHT> ... ]
  38. }
  39. ``IndirectBrInst``
  40. ^^^^^^^^^^^^^^^^^^
  41. Branch weights are assigned to every destination.
  42. .. code-block:: llvm
  43. !0 = metadata !{
  44. metadata !"branch_weights",
  45. i32 <LABEL_BRANCH_WEIGHT>
  46. [ , i32 <LABEL_BRANCH_WEIGHT> ... ]
  47. }
  48. Other
  49. ^^^^^
  50. Other terminator instructions are not allowed to contain Branch Weight Metadata.
  51. .. _\__builtin_expect:
  52. Built-in ``expect`` Instructions
  53. ================================
  54. ``__builtin_expect(long exp, long c)`` instruction provides branch prediction
  55. information. The return value is the value of ``exp``.
  56. It is especially useful in conditional statements. Currently Clang supports two
  57. conditional statements:
  58. ``if`` statement
  59. ^^^^^^^^^^^^^^^^
  60. The ``exp`` parameter is the condition. The ``c`` parameter is the expected
  61. comparison value. If it is equal to 1 (true), the condition is likely to be
  62. true, in other case condition is likely to be false. For example:
  63. .. code-block:: c++
  64. if (__builtin_expect(x > 0, 1)) {
  65. // This block is likely to be taken.
  66. }
  67. ``switch`` statement
  68. ^^^^^^^^^^^^^^^^^^^^
  69. The ``exp`` parameter is the value. The ``c`` parameter is the expected
  70. value. If the expected value doesn't show on the cases list, the ``default``
  71. case is assumed to be likely taken.
  72. .. code-block:: c++
  73. switch (__builtin_expect(x, 5)) {
  74. default: break;
  75. case 0: // ...
  76. case 3: // ...
  77. case 5: // This case is likely to be taken.
  78. }
  79. CFG Modifications
  80. =================
  81. Branch Weight Metatada is not proof against CFG changes. If terminator operands'
  82. are changed some action should be taken. In other case some misoptimizations may
  83. occur due to incorrent branch prediction information.
  84. Function Entry Counts
  85. =====================
  86. To allow comparing different functions durint inter-procedural analysis and
  87. optimization, ``MD_prof`` nodes can also be assigned to a function definition.
  88. The first operand is a string indicating the name of the associated counter.
  89. Currently, one counter is supported: "function_entry_count". This is a 64-bit
  90. counter that indicates the number of times that this function was invoked (in
  91. the case of instrumentation-based profiles). In the case of sampling-based
  92. profiles, this counter is an approximation of how many times the function was
  93. invoked.
  94. For example, in the code below, the instrumentation for function foo()
  95. indicates that it was called 2,590 times at runtime.
  96. .. code-block:: llvm
  97. define i32 @foo() !prof !1 {
  98. ret i32 0
  99. }
  100. !1 = !{!"function_entry_count", i64 2590}