137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//===- SampleProfReader.h - Read LLVM sample profile data -----------------===//
237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//
337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//                      The LLVM Compiler Infrastructure
437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//
537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// This file is distributed under the University of Illinois Open Source
637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// License. See LICENSE.TXT for details.
737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//
837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//===----------------------------------------------------------------------===//
937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//
1037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines// This file contains definitions needed for reading sample profiles.
1137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//
12f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// NOTE: If you are making changes to this file format, please remember
13f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//       to document them in the Clang documentation at
14f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//       tools/clang/docs/UsersManual.rst.
15f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
16f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Text format
17f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// -----------
18f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
19f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Sample profiles are written as ASCII text. The file is divided into
20f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// sections, which correspond to each of the functions executed at runtime.
21f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Each section has the following format
22f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
23f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//     function1:total_samples:total_head_samples
24f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      offset1[.discriminator]: number_of_samples [fn1:num fn2:num ... ]
25f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      offset2[.discriminator]: number_of_samples [fn3:num fn4:num ... ]
26f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      ...
27f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      offsetN[.discriminator]: number_of_samples [fn5:num fn6:num ... ]
28f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      offsetA[.discriminator]: fnA:num_of_total_samples
29f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//       offsetA1[.discriminator]: number_of_samples [fn7:num fn8:num ... ]
30f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//       ...
31f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
32f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// This is a nested tree in which the identation represents the nesting level
33f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// of the inline stack. There are no blank lines in the file. And the spacing
34f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// within a single line is fixed. Additional spaces will result in an error
35f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// while reading the file.
36f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
37f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Any line starting with the '#' character is completely ignored.
38f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
39f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Inlined calls are represented with indentation. The Inline stack is a
40f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// stack of source locations in which the top of the stack represents the
41f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// leaf function, and the bottom of the stack represents the actual
42f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// symbol to which the instruction belongs.
43f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
44f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Function names must be mangled in order for the profile loader to
45f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// match them in the current translation unit. The two numbers in the
46f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// function header specify how many total samples were accumulated in the
47f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// function (first number), and the total number of samples accumulated
48f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// in the prologue of the function (second number). This head sample
49f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// count provides an indicator of how frequently the function is invoked.
50f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
51f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// There are two types of lines in the function body.
52f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
53f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// * Sampled line represents the profile information of a source location.
54f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// * Callsite line represents the profile information of a callsite.
55f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
56f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Each sampled line may contain several items. Some are optional (marked
57f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// below):
58f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
59f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// a. Source line offset. This number represents the line number
60f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    in the function where the sample was collected. The line number is
61f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    always relative to the line where symbol of the function is
62f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    defined. So, if the function has its header at line 280, the offset
63f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    13 is at line 293 in the file.
64f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
65f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    Note that this offset should never be a negative number. This could
66f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    happen in cases like macros. The debug machinery will register the
67f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    line number at the point of macro expansion. So, if the macro was
68f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    expanded in a line before the start of the function, the profile
69f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    converter should emit a 0 as the offset (this means that the optimizers
70f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    will not be able to associate a meaningful weight to the instructions
71f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    in the macro).
72f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
73f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// b. [OPTIONAL] Discriminator. This is used if the sampled program
74f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    was compiled with DWARF discriminator support
75f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    (http://wiki.dwarfstd.org/index.php?title=Path_Discriminators).
76f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    DWARF discriminators are unsigned integer values that allow the
77f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    compiler to distinguish between multiple execution paths on the
78f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    same source line location.
79f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
80f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    For example, consider the line of code ``if (cond) foo(); else bar();``.
81f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    If the predicate ``cond`` is true 80% of the time, then the edge
82f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    into function ``foo`` should be considered to be taken most of the
83f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    time. But both calls to ``foo`` and ``bar`` are at the same source
84f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    line, so a sample count at that line is not sufficient. The
85f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    compiler needs to know which part of that line is taken more
86f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    frequently.
87f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
88f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    This is what discriminators provide. In this case, the calls to
89f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    ``foo`` and ``bar`` will be at the same line, but will have
90f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    different discriminator values. This allows the compiler to correctly
91f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    set edge weights into ``foo`` and ``bar``.
92f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
93f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// c. Number of samples. This is an integer quantity representing the
94f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    number of samples collected by the profiler at this source
95f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    location.
96f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
97f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// d. [OPTIONAL] Potential call targets and samples. If present, this
98f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    line contains a call instruction. This models both direct and
99f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    number of samples. For example,
100f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
101f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      130: 7  foo:3  bar:2  baz:7
102f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
103f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    The above means that at relative line offset 130 there is a call
104f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    instruction that calls one of ``foo()``, ``bar()`` and ``baz()``,
105f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    with ``baz()`` being the relatively more frequently called target.
106f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
107f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Each callsite line may contain several items. Some are optional.
108f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
109f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// a. Source line offset. This number represents the line number of the
110f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    callsite that is inlined in the profiled binary.
111f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
112f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// b. [OPTIONAL] Discriminator. Same as the discriminator for sampled line.
113f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
114f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// c. Number of samples. This is an integer quantity representing the
115f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    total number of samples collected for the inlined instance at this
116f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    callsite
117f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
118f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
119f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Binary format
120f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// -------------
121f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
122f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// This is a more compact encoding. Numbers are encoded as ULEB128 values
123f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// and all strings are encoded in a name table. The file is organized in
124f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// the following sections:
125f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
126f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// MAGIC (uint64_t)
127f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    File identifier computed by function SPMagic() (0x5350524f463432ff)
128f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
129f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// VERSION (uint32_t)
130f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    File format version number computed by SPVersion()
131f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
132de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar// SUMMARY
133de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//    TOTAL_COUNT (uint64_t)
134de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        Total number of samples in the profile.
135de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//    MAX_COUNT (uint64_t)
136de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        Maximum value of samples on a line.
137de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//    MAX_FUNCTION_COUNT (uint64_t)
138de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        Maximum number of samples at function entry (head samples).
139de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//    NUM_COUNTS (uint64_t)
140de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        Number of lines with samples.
141de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//    NUM_FUNCTIONS (uint64_t)
142de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        Number of functions with samples.
143de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//    NUM_DETAILED_SUMMARY_ENTRIES (size_t)
144de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        Number of entries in detailed summary
145de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//    DETAILED_SUMMARY
146de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        A list of detailed summary entry. Each entry consists of
147de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        CUTOFF (uint32_t)
148de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//            Required percentile of total sample count expressed as a fraction
149de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//            multiplied by 1000000.
150de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        MIN_COUNT (uint64_t)
151de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//            The minimum number of samples required to reach the target
152de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//            CUTOFF.
153de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//        NUM_COUNTS (uint64_t)
154de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//            Number of samples to get to the desrired percentile.
155de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar//
156f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// NAME TABLE
157f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    SIZE (uint32_t)
158f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        Number of entries in the name table.
159f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    NAMES
160f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        A NUL-separated list of SIZE strings.
161f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//
162f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// FUNCTION BODY (one for each uninlined function body present in the profile)
163f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    HEAD_SAMPLES (uint64_t) [only for top-level functions]
164f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        Total number of samples collected at the head (prologue) of the
165f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        function.
166f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        NOTE: This field should only be present for top-level functions
167f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//              (i.e., not inlined into any caller). Inlined function calls
168f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//              have no prologue, so they don't need this.
169f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    NAME_IDX (uint32_t)
170f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        Index into the name table indicating the function name.
171f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    SAMPLES (uint64_t)
172f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        Total number of samples collected in this function.
173f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    NRECS (uint32_t)
174f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        Total number of sampling records this function's profile.
175f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    BODY RECORDS
176f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        A list of NRECS entries. Each entry contains:
177f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          OFFSET (uint32_t)
178f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            Line offset from the start of the function.
179f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          DISCRIMINATOR (uint32_t)
180f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            Discriminator value (see description of discriminators
181f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            in the text format documentation above).
182f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          SAMPLES (uint64_t)
183f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            Number of samples collected at this location.
184f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          NUM_CALLS (uint32_t)
185f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            Number of non-inlined function calls made at this location. In the
186f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            case of direct calls, this number will always be 1. For indirect
187f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            calls (virtual functions and function pointers) this will
188f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            represent all the actual functions called at runtime.
189f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          CALL_TARGETS
190f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//            A list of NUM_CALLS entries for each called function:
191f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//               NAME_IDX (uint32_t)
192f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//                  Index into the name table with the callee name.
193f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//               SAMPLES (uint64_t)
194f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//                  Number of samples collected at the call site.
195f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    NUM_INLINED_FUNCTIONS (uint32_t)
196f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      Number of callees inlined into this function.
197f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//    INLINED FUNCTION RECORDS
198f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      A list of NUM_INLINED_FUNCTIONS entries describing each of the inlined
199f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//      callees.
200f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        OFFSET (uint32_t)
201f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          Line offset from the start of the function.
202f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        DISCRIMINATOR (uint32_t)
203f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          Discriminator value (see description of discriminators
204f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          in the text format documentation above).
205f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//        FUNCTION BODY
206f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar//          A FUNCTION BODY entry describing the inlined function.
20737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines//===----------------------------------------------------------------------===//
20837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#ifndef LLVM_PROFILEDATA_SAMPLEPROFREADER_H
20937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#define LLVM_PROFILEDATA_SAMPLEPROFREADER_H
21037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
21137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/ADT/StringMap.h"
21237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/ADT/StringRef.h"
21337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/ADT/Twine.h"
214ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/IR/DiagnosticInfo.h"
215ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/IR/Function.h"
216ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/IR/LLVMContext.h"
217de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include "llvm/ProfileData/ProfileCommon.h"
21837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/ProfileData/SampleProf.h"
21937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/Support/Debug.h"
22037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/Support/ErrorHandling.h"
22137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/Support/ErrorOr.h"
222f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include "llvm/Support/GCOV.h"
22337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/Support/MemoryBuffer.h"
22437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#include "llvm/Support/raw_ostream.h"
22537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
22637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesnamespace llvm {
22737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
22837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesnamespace sampleprof {
22937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
23037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// \brief Sample-based profile reader.
23137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
23237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// Each profile contains sample counts for all the functions
23337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// executed. Inside each function, statements are annotated with the
23437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// collected samples on all the instructions associated with that
23537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// statement.
23637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
23737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// For this to produce meaningful data, the program needs to be
23837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// compiled with some debug information (at minimum, line numbers:
23937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// -gline-tables-only). Otherwise, it will be impossible to match IR
24037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// instructions to the line numbers collected by the profiler.
24137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
24237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// From the profile file, we are interested in collecting the
24337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// following information:
24437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
24537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// * A list of functions included in the profile (mangled names).
24637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
24737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// * For each function F:
24837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///   1. The total number of samples collected in F.
24937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
25037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///   2. The samples collected at each line in F. To provide some
25137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///      protection against source code shuffling, line numbers should
25237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///      be relative to the start of the function.
25337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines///
25437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// The reader supports two file formats: text and binary. The text format
25537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// is useful for debugging and testing, while the binary format is more
256f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar/// compact and I/O efficient. They can both be used interchangeably.
25737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesclass SampleProfileReader {
25837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinespublic:
25937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  SampleProfileReader(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
26037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      : Profiles(0), Ctx(C), Buffer(std::move(B)) {}
26137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
26237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  virtual ~SampleProfileReader() {}
26337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
26437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read and validate the file header.
26537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  virtual std::error_code readHeader() = 0;
26637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
26737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read sample profiles from the associated file.
26837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  virtual std::error_code read() = 0;
26937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
27037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Print the profile for \p FName on stream \p OS.
27137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void dumpFunctionProfile(StringRef FName, raw_ostream &OS = dbgs());
27237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
27337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Print all the profiles on stream \p OS.
27437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  void dump(raw_ostream &OS = dbgs());
27537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
27637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return the samples collected for function \p F.
27737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  FunctionSamples *getSamplesFor(const Function &F) {
27837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return &Profiles[F.getName()];
27937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
28037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
28137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return all the profiles.
28237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  StringMap<FunctionSamples> &getProfiles() { return Profiles; }
28337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
28437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Report a parse error message.
285f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  void reportError(int64_t LineNumber, Twine Msg) const {
28637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    Ctx.diagnose(DiagnosticInfoSampleProfile(Buffer->getBufferIdentifier(),
28737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                             LineNumber, Msg));
28837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  }
28937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
29037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Create a sample profile reader appropriate to the file format.
29137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  static ErrorOr<std::unique_ptr<SampleProfileReader>>
292de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  create(const Twine &Filename, LLVMContext &C);
29337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
294f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Create a sample profile reader from the supplied memory buffer.
295f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  static ErrorOr<std::unique_ptr<SampleProfileReader>>
296f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C);
297f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
298de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// \brief Return the profile summary.
299de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ProfileSummary &getSummary() { return *(Summary.get()); }
300de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
30137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesprotected:
30237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Map every function to its associated profile.
30337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
30437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// The profile of every function executed at runtime is collected
30537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// in the structure FunctionSamples. This maps function objects
30637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// to their corresponding profiles.
30737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  StringMap<FunctionSamples> Profiles;
30837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
30937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief LLVM context used to emit diagnostics.
31037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMContext &Ctx;
31137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
31237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Memory buffer holding the profile file.
31337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<MemoryBuffer> Buffer;
314de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
315de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// \brief Profile summary information.
316de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ProfileSummary> Summary;
317de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
318de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// \brief Compute summary for this profile.
319de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void computeSummary();
32037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines};
32137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesclass SampleProfileReaderText : public SampleProfileReader {
32337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinespublic:
32437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  SampleProfileReaderText(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
32537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      : SampleProfileReader(std::move(B), C) {}
32637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read and validate the file header.
32837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::error_code readHeader() override { return sampleprof_error::success; }
32937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
33037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read sample profiles from the associated file.
33137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::error_code read() override;
332f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
333f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Return true if \p Buffer is in the format supported by this class.
334f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  static bool hasFormat(const MemoryBuffer &Buffer);
33537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines};
33637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
33737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesclass SampleProfileReaderBinary : public SampleProfileReader {
33837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinespublic:
33937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  SampleProfileReaderBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
34037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      : SampleProfileReader(std::move(B), C), Data(nullptr), End(nullptr) {}
34137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read and validate the file header.
34337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::error_code readHeader() override;
34437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read sample profiles from the associated file.
34637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::error_code read() override;
34737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return true if \p Buffer is in the format supported by this class.
34937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  static bool hasFormat(const MemoryBuffer &Buffer);
35037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
35137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesprotected:
35237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read a numeric value of type T from the profile.
35337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
35437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// If an error occurs during decoding, a diagnostic message is emitted and
35537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// EC is set.
35637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
35737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \returns the read value.
35837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  template <typename T> ErrorOr<T> readNumber();
35937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
36037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Read a string from the profile.
36137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
36237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// If an error occurs during decoding, a diagnostic message is emitted and
36337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// EC is set.
36437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ///
36537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \returns the read value.
36637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ErrorOr<StringRef> readString();
36737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
368f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Read a string indirectly via the name table.
369f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  ErrorOr<StringRef> readStringFromTable();
370f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
37137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Return true if we've reached the end of file.
37237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  bool at_eof() const { return Data >= End; }
37337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
374f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Read the contents of the given profile instance.
375f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code readProfile(FunctionSamples &FProfile);
376f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
37737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Points to the current location in the buffer.
37837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const uint8_t *Data;
37937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
38037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  /// \brief Points to the end of the buffer.
38137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  const uint8_t *End;
382f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
383f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Function name table.
384f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::vector<StringRef> NameTable;
385de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
386de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarprivate:
387de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::error_code readSummaryEntry(std::vector<ProfileSummaryEntry> &Entries);
388de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
389de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// \brief Read profile summary.
390de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::error_code readSummary();
391f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar};
392f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
393f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainartypedef SmallVector<FunctionSamples *, 10> InlineCallStack;
394f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
395f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// Supported histogram types in GCC.  Currently, we only need support for
396f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar// call target histograms.
397f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarenum HistType {
398f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_INTERVAL,
399f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_POW2,
400f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_SINGLE_VALUE,
401f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_CONST_DELTA,
402f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_INDIR_CALL,
403f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_AVERAGE,
404f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_IOR,
405f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  HIST_TYPE_INDIR_CALL_TOPN
406f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar};
407f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
408f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarclass SampleProfileReaderGCC : public SampleProfileReader {
409f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarpublic:
410f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  SampleProfileReaderGCC(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
411f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      : SampleProfileReader(std::move(B), C), GcovBuffer(Buffer.get()) {}
412f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
413f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Read and validate the file header.
414f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code readHeader() override;
415f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
416f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Read sample profiles from the associated file.
417f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code read() override;
418f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
419f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Return true if \p Buffer is in the format supported by this class.
420f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  static bool hasFormat(const MemoryBuffer &Buffer);
421f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
422f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarprotected:
423f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code readNameTable();
424f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code readOneFunctionProfile(const InlineCallStack &InlineStack,
425f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                                         bool Update, uint32_t Offset);
426f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code readFunctionProfiles();
427f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code skipNextWord();
428f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  template <typename T> ErrorOr<T> readNumber();
429f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  ErrorOr<StringRef> readString();
430f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
431f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// \brief Read the section tag and check that it's the same as \p Expected.
432f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::error_code readSectionTag(uint32_t Expected);
433f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
434f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// GCOV buffer containing the profile.
435f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  GCOVBuffer GcovBuffer;
436f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
437f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// Function names in this profile.
438f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::vector<std::string> Names;
439f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
440f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  /// GCOV tags used to separate sections in the profile file.
441f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  static const uint32_t GCOVTagAFDOFileNames = 0xaa000000;
442f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  static const uint32_t GCOVTagAFDOFunction = 0xac000000;
44337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines};
44437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
44537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines} // End namespace sampleprof
44637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
44737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines} // End namespace llvm
44837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
44937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines#endif // LLVM_PROFILEDATA_SAMPLEPROFREADER_H
450