xref: /aosp_15_r20/external/llvm/docs/BranchWeightMetadata.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker===========================
2*9880d681SAndroid Build Coastguard WorkerLLVM Branch Weight Metadata
3*9880d681SAndroid Build Coastguard Worker===========================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerIntroduction
9*9880d681SAndroid Build Coastguard Worker============
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerBranch Weight Metadata represents branch weights as its likeliness to be taken
12*9880d681SAndroid Build Coastguard Worker(see :doc:`BlockFrequencyTerminology`). Metadata is assigned to the
13*9880d681SAndroid Build Coastguard Worker``TerminatorInst`` as a ``MDNode`` of the ``MD_prof`` kind. The first operator
14*9880d681SAndroid Build Coastguard Workeris always a ``MDString`` node with the string "branch_weights".  Number of
15*9880d681SAndroid Build Coastguard Workeroperators depends on the terminator type.
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard WorkerBranch weights might be fetch from the profiling file, or generated based on
18*9880d681SAndroid Build Coastguard Worker`__builtin_expect`_ instruction.
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard WorkerAll weights are represented as an unsigned 32-bit values, where higher value
21*9880d681SAndroid Build Coastguard Workerindicates greater chance to be taken.
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard WorkerSupported Instructions
24*9880d681SAndroid Build Coastguard Worker======================
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker``BranchInst``
27*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard WorkerMetadata is only assigned to the conditional branches. There are two extra
30*9880d681SAndroid Build Coastguard Workeroperands for the true and the false branch.
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker  !0 = metadata !{
35*9880d681SAndroid Build Coastguard Worker    metadata !"branch_weights",
36*9880d681SAndroid Build Coastguard Worker    i32 <TRUE_BRANCH_WEIGHT>,
37*9880d681SAndroid Build Coastguard Worker    i32 <FALSE_BRANCH_WEIGHT>
38*9880d681SAndroid Build Coastguard Worker  }
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker``SwitchInst``
41*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard WorkerBranch weights are assigned to every case (including the ``default`` case which
44*9880d681SAndroid Build Coastguard Workeris always case #0).
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker  !0 = metadata !{
49*9880d681SAndroid Build Coastguard Worker    metadata !"branch_weights",
50*9880d681SAndroid Build Coastguard Worker    i32 <DEFAULT_BRANCH_WEIGHT>
51*9880d681SAndroid Build Coastguard Worker    [ , i32 <CASE_BRANCH_WEIGHT> ... ]
52*9880d681SAndroid Build Coastguard Worker  }
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker``IndirectBrInst``
55*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard WorkerBranch weights are assigned to every destination.
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker  !0 = metadata !{
62*9880d681SAndroid Build Coastguard Worker    metadata !"branch_weights",
63*9880d681SAndroid Build Coastguard Worker    i32 <LABEL_BRANCH_WEIGHT>
64*9880d681SAndroid Build Coastguard Worker    [ , i32 <LABEL_BRANCH_WEIGHT> ... ]
65*9880d681SAndroid Build Coastguard Worker  }
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard WorkerOther
68*9880d681SAndroid Build Coastguard Worker^^^^^
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard WorkerOther terminator instructions are not allowed to contain Branch Weight Metadata.
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker.. _\__builtin_expect:
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard WorkerBuilt-in ``expect`` Instructions
75*9880d681SAndroid Build Coastguard Worker================================
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker``__builtin_expect(long exp, long c)`` instruction provides branch prediction
78*9880d681SAndroid Build Coastguard Workerinformation. The return value is the value of ``exp``.
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard WorkerIt is especially useful in conditional statements. Currently Clang supports two
81*9880d681SAndroid Build Coastguard Workerconditional statements:
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker``if`` statement
84*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard WorkerThe ``exp`` parameter is the condition. The ``c`` parameter is the expected
87*9880d681SAndroid Build Coastguard Workercomparison value. If it is equal to 1 (true), the condition is likely to be
88*9880d681SAndroid Build Coastguard Workertrue, in other case condition is likely to be false. For example:
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker  if (__builtin_expect(x > 0, 1)) {
93*9880d681SAndroid Build Coastguard Worker    // This block is likely to be taken.
94*9880d681SAndroid Build Coastguard Worker  }
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker``switch`` statement
97*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^
98*9880d681SAndroid Build Coastguard Worker
99*9880d681SAndroid Build Coastguard WorkerThe ``exp`` parameter is the value. The ``c`` parameter is the expected
100*9880d681SAndroid Build Coastguard Workervalue. If the expected value doesn't show on the cases list, the ``default``
101*9880d681SAndroid Build Coastguard Workercase is assumed to be likely taken.
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker  switch (__builtin_expect(x, 5)) {
106*9880d681SAndroid Build Coastguard Worker  default: break;
107*9880d681SAndroid Build Coastguard Worker  case 0:  // ...
108*9880d681SAndroid Build Coastguard Worker  case 3:  // ...
109*9880d681SAndroid Build Coastguard Worker  case 5:  // This case is likely to be taken.
110*9880d681SAndroid Build Coastguard Worker  }
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard WorkerCFG Modifications
113*9880d681SAndroid Build Coastguard Worker=================
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard WorkerBranch Weight Metatada is not proof against CFG changes. If terminator operands'
116*9880d681SAndroid Build Coastguard Workerare changed some action should be taken. In other case some misoptimizations may
117*9880d681SAndroid Build Coastguard Workeroccur due to incorrect branch prediction information.
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard WorkerFunction Entry Counts
120*9880d681SAndroid Build Coastguard Worker=====================
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard WorkerTo allow comparing different functions during inter-procedural analysis and
123*9880d681SAndroid Build Coastguard Workeroptimization, ``MD_prof`` nodes can also be assigned to a function definition.
124*9880d681SAndroid Build Coastguard WorkerThe first operand is a string indicating the name of the associated counter.
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard WorkerCurrently, one counter is supported: "function_entry_count". This is a 64-bit
127*9880d681SAndroid Build Coastguard Workercounter that indicates the number of times that this function was invoked (in
128*9880d681SAndroid Build Coastguard Workerthe case of instrumentation-based profiles). In the case of sampling-based
129*9880d681SAndroid Build Coastguard Workerprofiles, this counter is an approximation of how many times the function was
130*9880d681SAndroid Build Coastguard Workerinvoked.
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard WorkerFor example, in the code below, the instrumentation for function foo()
133*9880d681SAndroid Build Coastguard Workerindicates that it was called 2,590 times at runtime.
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker  define i32 @foo() !prof !1 {
138*9880d681SAndroid Build Coastguard Worker    ret i32 0
139*9880d681SAndroid Build Coastguard Worker  }
140*9880d681SAndroid Build Coastguard Worker  !1 = !{!"function_entry_count", i64 2590}
141