xref: /aosp_15_r20/prebuilts/clang-tools/linux-x86/clang-headers/profile/InstrProfData.inc (revision bed243d3d9cd544cfb038bfa7be843dedc6e6bf7)
1*bed243d3SAndroid Build Coastguard Worker/*===-- InstrProfData.inc - instr profiling runtime structures -*- C++ -*-=== *\
2*bed243d3SAndroid Build Coastguard Worker|*
3*bed243d3SAndroid Build Coastguard Worker|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bed243d3SAndroid Build Coastguard Worker|* See https://llvm.org/LICENSE.txt for license information.
5*bed243d3SAndroid Build Coastguard Worker|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bed243d3SAndroid Build Coastguard Worker|*
7*bed243d3SAndroid Build Coastguard Worker\*===----------------------------------------------------------------------===*/
8*bed243d3SAndroid Build Coastguard Worker/*
9*bed243d3SAndroid Build Coastguard Worker * This is the main file that defines all the data structure, signature,
10*bed243d3SAndroid Build Coastguard Worker * constant literals that are shared across profiling runtime library,
11*bed243d3SAndroid Build Coastguard Worker * compiler (instrumentation), and host tools (reader/writer). The entities
12*bed243d3SAndroid Build Coastguard Worker * defined in this file affect the profile runtime ABI, the raw profile format,
13*bed243d3SAndroid Build Coastguard Worker * or both.
14*bed243d3SAndroid Build Coastguard Worker *
15*bed243d3SAndroid Build Coastguard Worker * The file has two identical copies. The primary copy lives in LLVM and
16*bed243d3SAndroid Build Coastguard Worker * the other one  sits in compiler-rt/lib/profile directory. To make changes
17*bed243d3SAndroid Build Coastguard Worker * in this file, first modify the primary copy and copy it over to compiler-rt.
18*bed243d3SAndroid Build Coastguard Worker * Testing of any change in this file can start only after the two copies are
19*bed243d3SAndroid Build Coastguard Worker * synced up.
20*bed243d3SAndroid Build Coastguard Worker *
21*bed243d3SAndroid Build Coastguard Worker * The first part of the file includes macros that defines types, names, and
22*bed243d3SAndroid Build Coastguard Worker * initializers for the member fields of the core data structures. The field
23*bed243d3SAndroid Build Coastguard Worker * declarations for one structure is enabled by defining the field activation
24*bed243d3SAndroid Build Coastguard Worker * macro associated with that structure. Only one field activation record
25*bed243d3SAndroid Build Coastguard Worker * can be defined at one time and the rest definitions will be filtered out by
26*bed243d3SAndroid Build Coastguard Worker * the preprocessor.
27*bed243d3SAndroid Build Coastguard Worker *
28*bed243d3SAndroid Build Coastguard Worker * Examples of how the template is used to instantiate structure definition:
29*bed243d3SAndroid Build Coastguard Worker * 1. To declare a structure:
30*bed243d3SAndroid Build Coastguard Worker *
31*bed243d3SAndroid Build Coastguard Worker * struct ProfData {
32*bed243d3SAndroid Build Coastguard Worker * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
33*bed243d3SAndroid Build Coastguard Worker *    Type Name;
34*bed243d3SAndroid Build Coastguard Worker * #include "llvm/ProfileData/InstrProfData.inc"
35*bed243d3SAndroid Build Coastguard Worker * };
36*bed243d3SAndroid Build Coastguard Worker *
37*bed243d3SAndroid Build Coastguard Worker * 2. To construct LLVM type arrays for the struct type:
38*bed243d3SAndroid Build Coastguard Worker *
39*bed243d3SAndroid Build Coastguard Worker * Type *DataTypes[] = {
40*bed243d3SAndroid Build Coastguard Worker * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
41*bed243d3SAndroid Build Coastguard Worker *   LLVMType,
42*bed243d3SAndroid Build Coastguard Worker * #include "llvm/ProfileData/InstrProfData.inc"
43*bed243d3SAndroid Build Coastguard Worker * };
44*bed243d3SAndroid Build Coastguard Worker *
45*bed243d3SAndroid Build Coastguard Worker * 4. To construct constant array for the initializers:
46*bed243d3SAndroid Build Coastguard Worker * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
47*bed243d3SAndroid Build Coastguard Worker *   Initializer,
48*bed243d3SAndroid Build Coastguard Worker * Constant *ConstantVals[] = {
49*bed243d3SAndroid Build Coastguard Worker * #include "llvm/ProfileData/InstrProfData.inc"
50*bed243d3SAndroid Build Coastguard Worker * };
51*bed243d3SAndroid Build Coastguard Worker *
52*bed243d3SAndroid Build Coastguard Worker *
53*bed243d3SAndroid Build Coastguard Worker * The second part of the file includes definitions all other entities that
54*bed243d3SAndroid Build Coastguard Worker * are related to runtime ABI and format. When no field activation macro is
55*bed243d3SAndroid Build Coastguard Worker * defined, this file can be included to introduce the definitions.
56*bed243d3SAndroid Build Coastguard Worker *
57*bed243d3SAndroid Build Coastguard Worker\*===----------------------------------------------------------------------===*/
58*bed243d3SAndroid Build Coastguard Worker
59*bed243d3SAndroid Build Coastguard Worker/* Functions marked with INSTR_PROF_VISIBILITY must have hidden visibility in
60*bed243d3SAndroid Build Coastguard Worker * the compiler runtime. */
61*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_PROF_VISIBILITY
62*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VISIBILITY
63*bed243d3SAndroid Build Coastguard Worker#endif
64*bed243d3SAndroid Build Coastguard Worker
65*bed243d3SAndroid Build Coastguard Worker// clang-format off:consider re-enabling clang-format if auto-formatted C macros
66*bed243d3SAndroid Build Coastguard Worker// are readable (e.g., after `issue #82426` is fixed)
67*bed243d3SAndroid Build Coastguard Worker/* INSTR_PROF_DATA start. */
68*bed243d3SAndroid Build Coastguard Worker/* Definition of member fields of the per-function control structure. */
69*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_PROF_DATA
70*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer)
71*bed243d3SAndroid Build Coastguard Worker#else
72*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
73*bed243d3SAndroid Build Coastguard Worker#endif
74*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
75*bed243d3SAndroid Build Coastguard Worker                ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
76*bed243d3SAndroid Build Coastguard Worker                IndexedInstrProf::ComputeHash(getPGOFuncNameVarInitializer(Inc->getName()))))
77*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
78*bed243d3SAndroid Build Coastguard Worker                ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
79*bed243d3SAndroid Build Coastguard Worker                Inc->getHash()->getZExtValue()))
80*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const IntPtrT, IntPtrTy, CounterPtr, RelativeCounterPtr)
81*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const IntPtrT, IntPtrTy, BitmapPtr, RelativeBitmapPtr)
82*bed243d3SAndroid Build Coastguard Worker/* This is used to map function pointers for the indirect call targets to
83*bed243d3SAndroid Build Coastguard Worker * function name hashes during the conversion from raw to merged profile
84*bed243d3SAndroid Build Coastguard Worker * data.
85*bed243d3SAndroid Build Coastguard Worker */
86*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const IntPtrT, llvm::PointerType::getUnqual(Ctx), FunctionPointer, \
87*bed243d3SAndroid Build Coastguard Worker                FunctionAddr)
88*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(IntPtrT, llvm::PointerType::getUnqual(Ctx), Values, \
89*bed243d3SAndroid Build Coastguard Worker                ValuesPtrExpr)
90*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumCounters, \
91*bed243d3SAndroid Build Coastguard Worker                ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumCounters))
92*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const uint16_t, Int16ArrayTy, NumValueSites[IPVK_Last+1], \
93*bed243d3SAndroid Build Coastguard Worker                ConstantArray::get(Int16ArrayTy, Int16ArrayVals)) \
94*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumBitmapBytes, \
95*bed243d3SAndroid Build Coastguard Worker                ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumBitmapBytes))
96*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_DATA
97*bed243d3SAndroid Build Coastguard Worker/* INSTR_PROF_DATA end. */
98*bed243d3SAndroid Build Coastguard Worker
99*bed243d3SAndroid Build Coastguard Worker/* For a virtual table object, record the name hash to associate profiled
100*bed243d3SAndroid Build Coastguard Worker * addresses with global variables, and record {starting address, size in bytes}
101*bed243d3SAndroid Build Coastguard Worker * to map the profiled virtual table (which usually have an offset from the
102*bed243d3SAndroid Build Coastguard Worker * starting address) back to a virtual table object. */
103*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_PROF_VTABLE_DATA
104*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Initializer)
105*bed243d3SAndroid Build Coastguard Worker#else
106*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VTABLE_DATA_DEFINED
107*bed243d3SAndroid Build Coastguard Worker#endif
108*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VTABLE_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), \
109*bed243d3SAndroid Build Coastguard Worker                       VTableNameHash, ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
110*bed243d3SAndroid Build Coastguard Worker                       IndexedInstrProf::ComputeHash(PGOVTableName)))
111*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VTABLE_DATA(const IntPtrT, llvm::PointerType::getUnqual(Ctx), \
112*bed243d3SAndroid Build Coastguard Worker                       VTablePointer, VTableAddr)
113*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VTABLE_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), VTableSize, \
114*bed243d3SAndroid Build Coastguard Worker                       ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \
115*bed243d3SAndroid Build Coastguard Worker                                        VTableSizeVal))
116*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_VTABLE_DATA
117*bed243d3SAndroid Build Coastguard Worker/* INSTR_PROF_VTABLE_DATA end. */
118*bed243d3SAndroid Build Coastguard Worker
119*bed243d3SAndroid Build Coastguard Worker/* This is an internal data structure used by value profiler. It
120*bed243d3SAndroid Build Coastguard Worker * is defined here to allow serialization code sharing by LLVM
121*bed243d3SAndroid Build Coastguard Worker * to be used in unit test.
122*bed243d3SAndroid Build Coastguard Worker *
123*bed243d3SAndroid Build Coastguard Worker * typedef struct ValueProfNode {
124*bed243d3SAndroid Build Coastguard Worker *   // InstrProfValueData VData;
125*bed243d3SAndroid Build Coastguard Worker *   uint64_t Value;
126*bed243d3SAndroid Build Coastguard Worker *   uint64_t Count;
127*bed243d3SAndroid Build Coastguard Worker *   struct ValueProfNode *Next;
128*bed243d3SAndroid Build Coastguard Worker * } ValueProfNode;
129*bed243d3SAndroid Build Coastguard Worker */
130*bed243d3SAndroid Build Coastguard Worker/* INSTR_PROF_VALUE_NODE start. */
131*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_PROF_VALUE_NODE
132*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer)
133*bed243d3SAndroid Build Coastguard Worker#else
134*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
135*bed243d3SAndroid Build Coastguard Worker#endif
136*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Value, \
137*bed243d3SAndroid Build Coastguard Worker                      ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
138*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Count, \
139*bed243d3SAndroid Build Coastguard Worker                      ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
140*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::PointerType::getUnqual(Ctx), Next, \
141*bed243d3SAndroid Build Coastguard Worker                      ConstantInt::get(llvm::PointerType::getUnqual(Ctx), 0))
142*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_VALUE_NODE
143*bed243d3SAndroid Build Coastguard Worker/* INSTR_PROF_VALUE_NODE end. */
144*bed243d3SAndroid Build Coastguard Worker
145*bed243d3SAndroid Build Coastguard Worker/* INSTR_PROF_RAW_HEADER  start */
146*bed243d3SAndroid Build Coastguard Worker/* Definition of member fields of the raw profile header data structure. */
147*bed243d3SAndroid Build Coastguard Worker/* Please update llvm/docs/InstrProfileFormat.rst as appropriate when updating
148*bed243d3SAndroid Build Coastguard Worker   raw profile format. */
149*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_PROF_RAW_HEADER
150*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer)
151*bed243d3SAndroid Build Coastguard Worker#else
152*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
153*bed243d3SAndroid Build Coastguard Worker#endif
154*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
155*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
156*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
157*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, NumData, NumData)
158*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesBeforeCounters, PaddingBytesBeforeCounters)
159*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, NumCounters, NumCounters)
160*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterCounters, PaddingBytesAfterCounters)
161*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, NumBitmapBytes, NumBitmapBytes)
162*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterBitmapBytes, PaddingBytesAfterBitmapBytes)
163*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, NamesSize,  NamesSize)
164*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, CountersDelta,
165*bed243d3SAndroid Build Coastguard Worker                      (uintptr_t)CountersBegin - (uintptr_t)DataBegin)
166*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, BitmapDelta,
167*bed243d3SAndroid Build Coastguard Worker                      (uintptr_t)BitmapBegin - (uintptr_t)DataBegin)
168*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
169*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, NumVTables, NumVTables)
170*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, VNamesSize, VNamesSize)
171*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
172*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_RAW_HEADER
173*bed243d3SAndroid Build Coastguard Worker/* INSTR_PROF_RAW_HEADER  end */
174*bed243d3SAndroid Build Coastguard Worker
175*bed243d3SAndroid Build Coastguard Worker/* VALUE_PROF_FUNC_PARAM start */
176*bed243d3SAndroid Build Coastguard Worker/* Definition of parameter types of the runtime API used to do value profiling
177*bed243d3SAndroid Build Coastguard Worker * for a given value site.
178*bed243d3SAndroid Build Coastguard Worker */
179*bed243d3SAndroid Build Coastguard Worker#ifndef VALUE_PROF_FUNC_PARAM
180*bed243d3SAndroid Build Coastguard Worker#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType)
181*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COMMA
182*bed243d3SAndroid Build Coastguard Worker#else
183*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
184*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COMMA ,
185*bed243d3SAndroid Build Coastguard Worker#endif
186*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_FUNC_PARAM(uint64_t, TargetValue, Type::getInt64Ty(Ctx)) \
187*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_COMMA
188*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_FUNC_PARAM(void *, Data, PointerType::getUnqual(Ctx)) INSTR_PROF_COMMA
189*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx))
190*bed243d3SAndroid Build Coastguard Worker#undef VALUE_PROF_FUNC_PARAM
191*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_COMMA
192*bed243d3SAndroid Build Coastguard Worker/* VALUE_PROF_FUNC_PARAM end */
193*bed243d3SAndroid Build Coastguard Worker
194*bed243d3SAndroid Build Coastguard Worker/* VALUE_PROF_KIND start */
195*bed243d3SAndroid Build Coastguard Worker#ifndef VALUE_PROF_KIND
196*bed243d3SAndroid Build Coastguard Worker#define VALUE_PROF_KIND(Enumerator, Value, Descr)
197*bed243d3SAndroid Build Coastguard Worker#else
198*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
199*bed243d3SAndroid Build Coastguard Worker#endif
200*bed243d3SAndroid Build Coastguard Worker/* For indirect function call value profiling, the addresses of the target
201*bed243d3SAndroid Build Coastguard Worker * functions are profiled by the instrumented code. The target addresses are
202*bed243d3SAndroid Build Coastguard Worker * written in the raw profile data and converted to target function name's MD5
203*bed243d3SAndroid Build Coastguard Worker * hash by the profile reader during deserialization.  Typically, this happens
204*bed243d3SAndroid Build Coastguard Worker * when the raw profile data is read during profile merging.
205*bed243d3SAndroid Build Coastguard Worker *
206*bed243d3SAndroid Build Coastguard Worker * For this remapping the ProfData is used.  ProfData contains both the function
207*bed243d3SAndroid Build Coastguard Worker * name hash and the function address.
208*bed243d3SAndroid Build Coastguard Worker */
209*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_KIND(IPVK_IndirectCallTarget, 0, "indirect call target")
210*bed243d3SAndroid Build Coastguard Worker/* For memory intrinsic functions size profiling. */
211*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_KIND(IPVK_MemOPSize, 1, "memory intrinsic functions size")
212*bed243d3SAndroid Build Coastguard Worker/* For virtual table address profiling, the address point of the virtual table
213*bed243d3SAndroid Build Coastguard Worker * (i.e., the address contained in objects pointing to a virtual table) are
214*bed243d3SAndroid Build Coastguard Worker * profiled. Note this may not be the address of the per C++ class virtual table
215*bed243d3SAndroid Build Coastguard Worker *  object (e.g., there might be an offset).
216*bed243d3SAndroid Build Coastguard Worker *
217*bed243d3SAndroid Build Coastguard Worker * The profiled addresses are stored in raw profile, together with the following
218*bed243d3SAndroid Build Coastguard Worker * two types of information.
219*bed243d3SAndroid Build Coastguard Worker * 1. The (starting and ending) addresses of per C++ class virtual table objects.
220*bed243d3SAndroid Build Coastguard Worker * 2. The (compressed) virtual table object names.
221*bed243d3SAndroid Build Coastguard Worker * RawInstrProfReader converts profiled virtual table addresses to virtual table
222*bed243d3SAndroid Build Coastguard Worker *  objects' MD5 hash.
223*bed243d3SAndroid Build Coastguard Worker */
224*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_KIND(IPVK_VTableTarget, 2, "The profiled address point of the vtable")
225*bed243d3SAndroid Build Coastguard Worker/* These two kinds must be the last to be
226*bed243d3SAndroid Build Coastguard Worker * declared. This is to make sure the string
227*bed243d3SAndroid Build Coastguard Worker * array created with the template can be
228*bed243d3SAndroid Build Coastguard Worker * indexed with the kind value.
229*bed243d3SAndroid Build Coastguard Worker */
230*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_KIND(IPVK_First, IPVK_IndirectCallTarget, "first")
231*bed243d3SAndroid Build Coastguard WorkerVALUE_PROF_KIND(IPVK_Last, IPVK_VTableTarget, "last")
232*bed243d3SAndroid Build Coastguard Worker
233*bed243d3SAndroid Build Coastguard Worker#undef VALUE_PROF_KIND
234*bed243d3SAndroid Build Coastguard Worker/* VALUE_PROF_KIND end */
235*bed243d3SAndroid Build Coastguard Worker
236*bed243d3SAndroid Build Coastguard Worker#undef COVMAP_V2_OR_V3
237*bed243d3SAndroid Build Coastguard Worker#ifdef COVMAP_V2
238*bed243d3SAndroid Build Coastguard Worker#define COVMAP_V2_OR_V3
239*bed243d3SAndroid Build Coastguard Worker#endif
240*bed243d3SAndroid Build Coastguard Worker#ifdef COVMAP_V3
241*bed243d3SAndroid Build Coastguard Worker#define COVMAP_V2_OR_V3
242*bed243d3SAndroid Build Coastguard Worker#endif
243*bed243d3SAndroid Build Coastguard Worker
244*bed243d3SAndroid Build Coastguard Worker/* COVMAP_FUNC_RECORD start */
245*bed243d3SAndroid Build Coastguard Worker/* Definition of member fields of the function record structure in coverage
246*bed243d3SAndroid Build Coastguard Worker * map.
247*bed243d3SAndroid Build Coastguard Worker */
248*bed243d3SAndroid Build Coastguard Worker#ifndef COVMAP_FUNC_RECORD
249*bed243d3SAndroid Build Coastguard Worker#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer)
250*bed243d3SAndroid Build Coastguard Worker#else
251*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
252*bed243d3SAndroid Build Coastguard Worker#endif
253*bed243d3SAndroid Build Coastguard Worker#ifdef COVMAP_V1
254*bed243d3SAndroid Build Coastguard WorkerCOVMAP_FUNC_RECORD(const IntPtrT, llvm::PointerType::getUnqual(Ctx), \
255*bed243d3SAndroid Build Coastguard Worker                   NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \
256*bed243d3SAndroid Build Coastguard Worker                   llvm::PointerType::getUnqual(Ctx)))
257*bed243d3SAndroid Build Coastguard WorkerCOVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
258*bed243d3SAndroid Build Coastguard Worker                   llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \
259*bed243d3SAndroid Build Coastguard Worker                   NameValue.size()))
260*bed243d3SAndroid Build Coastguard Worker#endif
261*bed243d3SAndroid Build Coastguard Worker#ifdef COVMAP_V2_OR_V3
262*bed243d3SAndroid Build Coastguard WorkerCOVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
263*bed243d3SAndroid Build Coastguard Worker                   llvm::ConstantInt::get( \
264*bed243d3SAndroid Build Coastguard Worker                     llvm::Type::getInt64Ty(Ctx), NameHash))
265*bed243d3SAndroid Build Coastguard Worker#endif
266*bed243d3SAndroid Build Coastguard WorkerCOVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), DataSize, \
267*bed243d3SAndroid Build Coastguard Worker                   llvm::ConstantInt::get( \
268*bed243d3SAndroid Build Coastguard Worker                     llvm::Type::getInt32Ty(Ctx), CoverageMapping.size()))
269*bed243d3SAndroid Build Coastguard WorkerCOVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
270*bed243d3SAndroid Build Coastguard Worker                   llvm::ConstantInt::get( \
271*bed243d3SAndroid Build Coastguard Worker                     llvm::Type::getInt64Ty(Ctx), FuncHash))
272*bed243d3SAndroid Build Coastguard Worker#ifdef COVMAP_V3
273*bed243d3SAndroid Build Coastguard WorkerCOVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FilenamesRef, \
274*bed243d3SAndroid Build Coastguard Worker                   llvm::ConstantInt::get( \
275*bed243d3SAndroid Build Coastguard Worker                     llvm::Type::getInt64Ty(Ctx), FilenamesRef))
276*bed243d3SAndroid Build Coastguard WorkerCOVMAP_FUNC_RECORD(const char, \
277*bed243d3SAndroid Build Coastguard Worker                   llvm::ArrayType::get(llvm::Type::getInt8Ty(Ctx), \
278*bed243d3SAndroid Build Coastguard Worker                                        CoverageMapping.size()), \
279*bed243d3SAndroid Build Coastguard Worker                   CoverageMapping,
280*bed243d3SAndroid Build Coastguard Worker                   llvm::ConstantDataArray::getRaw( \
281*bed243d3SAndroid Build Coastguard Worker                     CoverageMapping, CoverageMapping.size(), \
282*bed243d3SAndroid Build Coastguard Worker                     llvm::Type::getInt8Ty(Ctx)))
283*bed243d3SAndroid Build Coastguard Worker#endif
284*bed243d3SAndroid Build Coastguard Worker#undef COVMAP_FUNC_RECORD
285*bed243d3SAndroid Build Coastguard Worker/* COVMAP_FUNC_RECORD end.  */
286*bed243d3SAndroid Build Coastguard Worker
287*bed243d3SAndroid Build Coastguard Worker/* COVMAP_HEADER start */
288*bed243d3SAndroid Build Coastguard Worker/* Definition of member fields of coverage map header.
289*bed243d3SAndroid Build Coastguard Worker */
290*bed243d3SAndroid Build Coastguard Worker#ifndef COVMAP_HEADER
291*bed243d3SAndroid Build Coastguard Worker#define COVMAP_HEADER(Type, LLVMType, Name, Initializer)
292*bed243d3SAndroid Build Coastguard Worker#else
293*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
294*bed243d3SAndroid Build Coastguard Worker#endif
295*bed243d3SAndroid Build Coastguard WorkerCOVMAP_HEADER(uint32_t, Int32Ty, NRecords, \
296*bed243d3SAndroid Build Coastguard Worker              llvm::ConstantInt::get(Int32Ty, NRecords))
297*bed243d3SAndroid Build Coastguard WorkerCOVMAP_HEADER(uint32_t, Int32Ty, FilenamesSize, \
298*bed243d3SAndroid Build Coastguard Worker              llvm::ConstantInt::get(Int32Ty, FilenamesSize))
299*bed243d3SAndroid Build Coastguard WorkerCOVMAP_HEADER(uint32_t, Int32Ty, CoverageSize, \
300*bed243d3SAndroid Build Coastguard Worker              llvm::ConstantInt::get(Int32Ty, CoverageMappingSize))
301*bed243d3SAndroid Build Coastguard WorkerCOVMAP_HEADER(uint32_t, Int32Ty, Version, \
302*bed243d3SAndroid Build Coastguard Worker              llvm::ConstantInt::get(Int32Ty, CovMapVersion::CurrentVersion))
303*bed243d3SAndroid Build Coastguard Worker#undef COVMAP_HEADER
304*bed243d3SAndroid Build Coastguard Worker/* COVMAP_HEADER end.  */
305*bed243d3SAndroid Build Coastguard Worker
306*bed243d3SAndroid Build Coastguard Worker
307*bed243d3SAndroid Build Coastguard Worker#ifdef INSTR_PROF_SECT_ENTRY
308*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
309*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_data, \
310*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON), \
311*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_DATA_COFF, "__DATA,")
312*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_cnts, \
313*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON), \
314*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_CNTS_COFF, "__DATA,")
315*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_bitmap, \
316*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_BITS_COMMON), \
317*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_BITS_COFF, "__DATA,")
318*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_name, \
319*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON), \
320*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_NAME_COFF, "__DATA,")
321*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_vname, \
322*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_VNAME_COMMON), \
323*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_VNAME_COFF, "__DATA,")
324*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_vals, \
325*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON), \
326*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_VALS_COFF, "__DATA,")
327*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_vnodes, \
328*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON), \
329*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_VNODES_COFF, "__DATA,")
330*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_vtab, \
331*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_VTAB_COMMON), \
332*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_VTAB_COFF, "__DATA,")
333*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_covmap, \
334*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON), \
335*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_COVMAP_COFF, "__LLVM_COV,")
336*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_covfun, \
337*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_COVFUN_COMMON), \
338*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_COVFUN_COFF, "__LLVM_COV,")
339*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_orderfile, \
340*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COMMON), \
341*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COFF), "__DATA,")
342*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_covdata, \
343*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_COVDATA_COMMON), \
344*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_COVDATA_COFF, "__LLVM_COV,")
345*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_SECT_ENTRY(IPSK_covname, \
346*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_QUOTE(INSTR_PROF_COVNAME_COMMON), \
347*bed243d3SAndroid Build Coastguard Worker                      INSTR_PROF_COVNAME_COFF, "__LLVM_COV,")
348*bed243d3SAndroid Build Coastguard Worker
349*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_SECT_ENTRY
350*bed243d3SAndroid Build Coastguard Worker#endif
351*bed243d3SAndroid Build Coastguard Worker
352*bed243d3SAndroid Build Coastguard Worker
353*bed243d3SAndroid Build Coastguard Worker#ifdef INSTR_PROF_VALUE_PROF_DATA
354*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
355*bed243d3SAndroid Build Coastguard Worker
356*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_MAX_NUM_VAL_PER_SITE 255
357*bed243d3SAndroid Build Coastguard Worker/*!
358*bed243d3SAndroid Build Coastguard Worker * This is the header of the data structure that defines the on-disk
359*bed243d3SAndroid Build Coastguard Worker * layout of the value profile data of a particular kind for one function.
360*bed243d3SAndroid Build Coastguard Worker */
361*bed243d3SAndroid Build Coastguard Workertypedef struct ValueProfRecord {
362*bed243d3SAndroid Build Coastguard Worker  /* The kind of the value profile record. */
363*bed243d3SAndroid Build Coastguard Worker  uint32_t Kind;
364*bed243d3SAndroid Build Coastguard Worker  /*
365*bed243d3SAndroid Build Coastguard Worker   * The number of value profile sites. It is guaranteed to be non-zero;
366*bed243d3SAndroid Build Coastguard Worker   * otherwise the record for this kind won't be emitted.
367*bed243d3SAndroid Build Coastguard Worker   */
368*bed243d3SAndroid Build Coastguard Worker  uint32_t NumValueSites;
369*bed243d3SAndroid Build Coastguard Worker  /*
370*bed243d3SAndroid Build Coastguard Worker   * The first element of the array that stores the number of profiled
371*bed243d3SAndroid Build Coastguard Worker   * values for each value site. The size of the array is NumValueSites.
372*bed243d3SAndroid Build Coastguard Worker   * Since NumValueSites is greater than zero, there is at least one
373*bed243d3SAndroid Build Coastguard Worker   * element in the array.
374*bed243d3SAndroid Build Coastguard Worker   */
375*bed243d3SAndroid Build Coastguard Worker  uint8_t SiteCountArray[1];
376*bed243d3SAndroid Build Coastguard Worker
377*bed243d3SAndroid Build Coastguard Worker  /*
378*bed243d3SAndroid Build Coastguard Worker   * The fake declaration is for documentation purpose only.
379*bed243d3SAndroid Build Coastguard Worker   * Align the start of next field to be on 8 byte boundaries.
380*bed243d3SAndroid Build Coastguard Worker  uint8_t Padding[X];
381*bed243d3SAndroid Build Coastguard Worker   */
382*bed243d3SAndroid Build Coastguard Worker
383*bed243d3SAndroid Build Coastguard Worker  /* The array of value profile data. The size of the array is the sum
384*bed243d3SAndroid Build Coastguard Worker   * of all elements in SiteCountArray[].
385*bed243d3SAndroid Build Coastguard Worker  InstrProfValueData ValueData[];
386*bed243d3SAndroid Build Coastguard Worker   */
387*bed243d3SAndroid Build Coastguard Worker
388*bed243d3SAndroid Build Coastguard Worker#ifdef __cplusplus
389*bed243d3SAndroid Build Coastguard Worker  /*!
390*bed243d3SAndroid Build Coastguard Worker   * Return the number of value sites.
391*bed243d3SAndroid Build Coastguard Worker   */
392*bed243d3SAndroid Build Coastguard Worker  uint32_t getNumValueSites() const { return NumValueSites; }
393*bed243d3SAndroid Build Coastguard Worker  /*!
394*bed243d3SAndroid Build Coastguard Worker   * Read data from this record and save it to Record.
395*bed243d3SAndroid Build Coastguard Worker   */
396*bed243d3SAndroid Build Coastguard Worker  void deserializeTo(InstrProfRecord &Record,
397*bed243d3SAndroid Build Coastguard Worker                     InstrProfSymtab *SymTab);
398*bed243d3SAndroid Build Coastguard Worker  /*
399*bed243d3SAndroid Build Coastguard Worker   * In-place byte swap:
400*bed243d3SAndroid Build Coastguard Worker   * Do byte swap for this instance. \c Old is the original order before
401*bed243d3SAndroid Build Coastguard Worker   * the swap, and \c New is the New byte order.
402*bed243d3SAndroid Build Coastguard Worker   */
403*bed243d3SAndroid Build Coastguard Worker  void swapBytes(llvm::endianness Old, llvm::endianness New);
404*bed243d3SAndroid Build Coastguard Worker#endif
405*bed243d3SAndroid Build Coastguard Worker} ValueProfRecord;
406*bed243d3SAndroid Build Coastguard Worker
407*bed243d3SAndroid Build Coastguard Worker/*!
408*bed243d3SAndroid Build Coastguard Worker * Per-function header/control data structure for value profiling
409*bed243d3SAndroid Build Coastguard Worker * data in indexed format.
410*bed243d3SAndroid Build Coastguard Worker */
411*bed243d3SAndroid Build Coastguard Workertypedef struct ValueProfData {
412*bed243d3SAndroid Build Coastguard Worker  /*
413*bed243d3SAndroid Build Coastguard Worker   * Total size in bytes including this field. It must be a multiple
414*bed243d3SAndroid Build Coastguard Worker   * of sizeof(uint64_t).
415*bed243d3SAndroid Build Coastguard Worker   */
416*bed243d3SAndroid Build Coastguard Worker  uint32_t TotalSize;
417*bed243d3SAndroid Build Coastguard Worker  /*
418*bed243d3SAndroid Build Coastguard Worker   *The number of value profile kinds that has value profile data.
419*bed243d3SAndroid Build Coastguard Worker   * In this implementation, a value profile kind is considered to
420*bed243d3SAndroid Build Coastguard Worker   * have profile data if the number of value profile sites for the
421*bed243d3SAndroid Build Coastguard Worker   * kind is not zero. More aggressively, the implementation can
422*bed243d3SAndroid Build Coastguard Worker   * choose to check the actual data value: if none of the value sites
423*bed243d3SAndroid Build Coastguard Worker   * has any profiled values, the kind can be skipped.
424*bed243d3SAndroid Build Coastguard Worker   */
425*bed243d3SAndroid Build Coastguard Worker  uint32_t NumValueKinds;
426*bed243d3SAndroid Build Coastguard Worker
427*bed243d3SAndroid Build Coastguard Worker  /*
428*bed243d3SAndroid Build Coastguard Worker   * Following are a sequence of variable length records. The prefix/header
429*bed243d3SAndroid Build Coastguard Worker   * of each record is defined by ValueProfRecord type. The number of
430*bed243d3SAndroid Build Coastguard Worker   * records is NumValueKinds.
431*bed243d3SAndroid Build Coastguard Worker   * ValueProfRecord Record_1;
432*bed243d3SAndroid Build Coastguard Worker   * ValueProfRecord Record_N;
433*bed243d3SAndroid Build Coastguard Worker   */
434*bed243d3SAndroid Build Coastguard Worker
435*bed243d3SAndroid Build Coastguard Worker#if __cplusplus
436*bed243d3SAndroid Build Coastguard Worker  /*!
437*bed243d3SAndroid Build Coastguard Worker   * Return the total size in bytes of the on-disk value profile data
438*bed243d3SAndroid Build Coastguard Worker   * given the data stored in Record.
439*bed243d3SAndroid Build Coastguard Worker   */
440*bed243d3SAndroid Build Coastguard Worker  static uint32_t getSize(const InstrProfRecord &Record);
441*bed243d3SAndroid Build Coastguard Worker  /*!
442*bed243d3SAndroid Build Coastguard Worker   * Return a pointer to \c ValueProfData instance ready to be streamed.
443*bed243d3SAndroid Build Coastguard Worker   */
444*bed243d3SAndroid Build Coastguard Worker  static std::unique_ptr<ValueProfData>
445*bed243d3SAndroid Build Coastguard Worker  serializeFrom(const InstrProfRecord &Record);
446*bed243d3SAndroid Build Coastguard Worker  /*!
447*bed243d3SAndroid Build Coastguard Worker   * Check the integrity of the record.
448*bed243d3SAndroid Build Coastguard Worker   */
449*bed243d3SAndroid Build Coastguard Worker  Error checkIntegrity();
450*bed243d3SAndroid Build Coastguard Worker  /*!
451*bed243d3SAndroid Build Coastguard Worker   * Return a pointer to \c ValueProfileData instance ready to be read.
452*bed243d3SAndroid Build Coastguard Worker   * All data in the instance are properly byte swapped. The input
453*bed243d3SAndroid Build Coastguard Worker   * data is assumed to be in little endian order.
454*bed243d3SAndroid Build Coastguard Worker   */
455*bed243d3SAndroid Build Coastguard Worker  static Expected<std::unique_ptr<ValueProfData>>
456*bed243d3SAndroid Build Coastguard Worker  getValueProfData(const unsigned char *SrcBuffer,
457*bed243d3SAndroid Build Coastguard Worker                   const unsigned char *const SrcBufferEnd,
458*bed243d3SAndroid Build Coastguard Worker                   llvm::endianness SrcDataEndianness);
459*bed243d3SAndroid Build Coastguard Worker  /*!
460*bed243d3SAndroid Build Coastguard Worker   * Swap byte order from \c Endianness order to host byte order.
461*bed243d3SAndroid Build Coastguard Worker   */
462*bed243d3SAndroid Build Coastguard Worker  void swapBytesToHost(llvm::endianness Endianness);
463*bed243d3SAndroid Build Coastguard Worker  /*!
464*bed243d3SAndroid Build Coastguard Worker   * Swap byte order from host byte order to \c Endianness order.
465*bed243d3SAndroid Build Coastguard Worker   */
466*bed243d3SAndroid Build Coastguard Worker  void swapBytesFromHost(llvm::endianness Endianness);
467*bed243d3SAndroid Build Coastguard Worker  /*!
468*bed243d3SAndroid Build Coastguard Worker   * Return the total size of \c ValueProfileData.
469*bed243d3SAndroid Build Coastguard Worker   */
470*bed243d3SAndroid Build Coastguard Worker  uint32_t getSize() const { return TotalSize; }
471*bed243d3SAndroid Build Coastguard Worker  /*!
472*bed243d3SAndroid Build Coastguard Worker   * Read data from this data and save it to \c Record.
473*bed243d3SAndroid Build Coastguard Worker   */
474*bed243d3SAndroid Build Coastguard Worker  void deserializeTo(InstrProfRecord &Record,
475*bed243d3SAndroid Build Coastguard Worker                     InstrProfSymtab *SymTab);
476*bed243d3SAndroid Build Coastguard Worker  void operator delete(void *ptr) { ::operator delete(ptr); }
477*bed243d3SAndroid Build Coastguard Worker#endif
478*bed243d3SAndroid Build Coastguard Worker} ValueProfData;
479*bed243d3SAndroid Build Coastguard Worker
480*bed243d3SAndroid Build Coastguard Worker/*
481*bed243d3SAndroid Build Coastguard Worker * The closure is designed to abstact away two types of value profile data:
482*bed243d3SAndroid Build Coastguard Worker * - InstrProfRecord which is the primary data structure used to
483*bed243d3SAndroid Build Coastguard Worker *   represent profile data in host tools (reader, writer, and profile-use)
484*bed243d3SAndroid Build Coastguard Worker * - value profile runtime data structure suitable to be used by C
485*bed243d3SAndroid Build Coastguard Worker *   runtime library.
486*bed243d3SAndroid Build Coastguard Worker *
487*bed243d3SAndroid Build Coastguard Worker * Both sources of data need to serialize to disk/memory-buffer in common
488*bed243d3SAndroid Build Coastguard Worker * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
489*bed243d3SAndroid Build Coastguard Worker * writer to share the same format and code with indexed profile writer.
490*bed243d3SAndroid Build Coastguard Worker *
491*bed243d3SAndroid Build Coastguard Worker * For documentation of the member methods below, refer to corresponding methods
492*bed243d3SAndroid Build Coastguard Worker * in class InstrProfRecord.
493*bed243d3SAndroid Build Coastguard Worker */
494*bed243d3SAndroid Build Coastguard Workertypedef struct ValueProfRecordClosure {
495*bed243d3SAndroid Build Coastguard Worker  const void *Record;
496*bed243d3SAndroid Build Coastguard Worker  uint32_t (*GetNumValueKinds)(const void *Record);
497*bed243d3SAndroid Build Coastguard Worker  uint32_t (*GetNumValueSites)(const void *Record, uint32_t VKind);
498*bed243d3SAndroid Build Coastguard Worker  uint32_t (*GetNumValueData)(const void *Record, uint32_t VKind);
499*bed243d3SAndroid Build Coastguard Worker  uint32_t (*GetNumValueDataForSite)(const void *R, uint32_t VK, uint32_t S);
500*bed243d3SAndroid Build Coastguard Worker
501*bed243d3SAndroid Build Coastguard Worker  /*
502*bed243d3SAndroid Build Coastguard Worker   * After extracting the value profile data from the value profile record,
503*bed243d3SAndroid Build Coastguard Worker   * this method is used to map the in-memory value to on-disk value. If
504*bed243d3SAndroid Build Coastguard Worker   * the method is null, value will be written out untranslated.
505*bed243d3SAndroid Build Coastguard Worker   */
506*bed243d3SAndroid Build Coastguard Worker  uint64_t (*RemapValueData)(uint32_t, uint64_t Value);
507*bed243d3SAndroid Build Coastguard Worker  void (*GetValueForSite)(const void *R, InstrProfValueData *Dst, uint32_t K,
508*bed243d3SAndroid Build Coastguard Worker                          uint32_t S);
509*bed243d3SAndroid Build Coastguard Worker  ValueProfData *(*AllocValueProfData)(size_t TotalSizeInBytes);
510*bed243d3SAndroid Build Coastguard Worker} ValueProfRecordClosure;
511*bed243d3SAndroid Build Coastguard Worker
512*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY ValueProfRecord *
513*bed243d3SAndroid Build Coastguard WorkergetFirstValueProfRecord(ValueProfData *VPD);
514*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY ValueProfRecord *
515*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordNext(ValueProfRecord *VPR);
516*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY InstrProfValueData *
517*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordValueData(ValueProfRecord *VPR);
518*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY uint32_t
519*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordHeaderSize(uint32_t NumValueSites);
520*bed243d3SAndroid Build Coastguard Worker
521*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_VALUE_PROF_DATA
522*bed243d3SAndroid Build Coastguard Worker#endif  /* INSTR_PROF_VALUE_PROF_DATA */
523*bed243d3SAndroid Build Coastguard Worker
524*bed243d3SAndroid Build Coastguard Worker
525*bed243d3SAndroid Build Coastguard Worker#ifdef INSTR_PROF_COMMON_API_IMPL
526*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_DEFINED
527*bed243d3SAndroid Build Coastguard Worker#ifdef __cplusplus
528*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_INLINE inline
529*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_NULLPTR nullptr
530*bed243d3SAndroid Build Coastguard Worker#else
531*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_INLINE
532*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_NULLPTR NULL
533*bed243d3SAndroid Build Coastguard Worker#endif
534*bed243d3SAndroid Build Coastguard Worker
535*bed243d3SAndroid Build Coastguard Worker#ifndef offsetof
536*bed243d3SAndroid Build Coastguard Worker#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
537*bed243d3SAndroid Build Coastguard Worker#endif
538*bed243d3SAndroid Build Coastguard Worker
539*bed243d3SAndroid Build Coastguard Worker// clang-format on
540*bed243d3SAndroid Build Coastguard Worker
541*bed243d3SAndroid Build Coastguard Worker/*!
542*bed243d3SAndroid Build Coastguard Worker * Return the \c ValueProfRecord header size including the
543*bed243d3SAndroid Build Coastguard Worker * padding bytes.
544*bed243d3SAndroid Build Coastguard Worker */
545*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE uint32_t
546*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordHeaderSize(uint32_t NumValueSites) {
547*bed243d3SAndroid Build Coastguard Worker  uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
548*bed243d3SAndroid Build Coastguard Worker                  sizeof(uint8_t) * NumValueSites;
549*bed243d3SAndroid Build Coastguard Worker  /* Round the size to multiple of 8 bytes. */
550*bed243d3SAndroid Build Coastguard Worker  Size = (Size + 7) & ~7;
551*bed243d3SAndroid Build Coastguard Worker  return Size;
552*bed243d3SAndroid Build Coastguard Worker}
553*bed243d3SAndroid Build Coastguard Worker
554*bed243d3SAndroid Build Coastguard Worker/*!
555*bed243d3SAndroid Build Coastguard Worker * Return the total size of the value profile record including the
556*bed243d3SAndroid Build Coastguard Worker * header and the value data.
557*bed243d3SAndroid Build Coastguard Worker */
558*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE uint32_t
559*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordSize(uint32_t NumValueSites, uint32_t NumValueData) {
560*bed243d3SAndroid Build Coastguard Worker  return getValueProfRecordHeaderSize(NumValueSites) +
561*bed243d3SAndroid Build Coastguard Worker         sizeof(InstrProfValueData) * NumValueData;
562*bed243d3SAndroid Build Coastguard Worker}
563*bed243d3SAndroid Build Coastguard Worker
564*bed243d3SAndroid Build Coastguard Worker/*!
565*bed243d3SAndroid Build Coastguard Worker * Return the pointer to the start of value data array.
566*bed243d3SAndroid Build Coastguard Worker */
567*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE InstrProfValueData *
568*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordValueData(ValueProfRecord *This) {
569*bed243d3SAndroid Build Coastguard Worker  return (InstrProfValueData *)((char *)This + getValueProfRecordHeaderSize(
570*bed243d3SAndroid Build Coastguard Worker                                                   This->NumValueSites));
571*bed243d3SAndroid Build Coastguard Worker}
572*bed243d3SAndroid Build Coastguard Worker
573*bed243d3SAndroid Build Coastguard Worker/*!
574*bed243d3SAndroid Build Coastguard Worker * Return the total number of value data for \c This record.
575*bed243d3SAndroid Build Coastguard Worker */
576*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE uint32_t
577*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordNumValueData(ValueProfRecord *This) {
578*bed243d3SAndroid Build Coastguard Worker  uint32_t NumValueData = 0;
579*bed243d3SAndroid Build Coastguard Worker  uint32_t I;
580*bed243d3SAndroid Build Coastguard Worker  for (I = 0; I < This->NumValueSites; I++)
581*bed243d3SAndroid Build Coastguard Worker    NumValueData += This->SiteCountArray[I];
582*bed243d3SAndroid Build Coastguard Worker  return NumValueData;
583*bed243d3SAndroid Build Coastguard Worker}
584*bed243d3SAndroid Build Coastguard Worker
585*bed243d3SAndroid Build Coastguard Worker/*!
586*bed243d3SAndroid Build Coastguard Worker * Use this method to advance to the next \c This \c ValueProfRecord.
587*bed243d3SAndroid Build Coastguard Worker */
588*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE ValueProfRecord *
589*bed243d3SAndroid Build Coastguard WorkergetValueProfRecordNext(ValueProfRecord *This) {
590*bed243d3SAndroid Build Coastguard Worker  uint32_t NumValueData = getValueProfRecordNumValueData(This);
591*bed243d3SAndroid Build Coastguard Worker  return (ValueProfRecord *)((char *)This +
592*bed243d3SAndroid Build Coastguard Worker                             getValueProfRecordSize(This->NumValueSites,
593*bed243d3SAndroid Build Coastguard Worker                                                    NumValueData));
594*bed243d3SAndroid Build Coastguard Worker}
595*bed243d3SAndroid Build Coastguard Worker
596*bed243d3SAndroid Build Coastguard Worker/*!
597*bed243d3SAndroid Build Coastguard Worker * Return the first \c ValueProfRecord instance.
598*bed243d3SAndroid Build Coastguard Worker */
599*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE ValueProfRecord *
600*bed243d3SAndroid Build Coastguard WorkergetFirstValueProfRecord(ValueProfData *This) {
601*bed243d3SAndroid Build Coastguard Worker  return (ValueProfRecord *)((char *)This + sizeof(ValueProfData));
602*bed243d3SAndroid Build Coastguard Worker}
603*bed243d3SAndroid Build Coastguard Worker
604*bed243d3SAndroid Build Coastguard Worker/* Closure based interfaces.  */
605*bed243d3SAndroid Build Coastguard Worker
606*bed243d3SAndroid Build Coastguard Worker/*!
607*bed243d3SAndroid Build Coastguard Worker * Return the total size in bytes of the on-disk value profile data
608*bed243d3SAndroid Build Coastguard Worker * given the data stored in Record.
609*bed243d3SAndroid Build Coastguard Worker */
610*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY uint32_t
611*bed243d3SAndroid Build Coastguard WorkergetValueProfDataSize(ValueProfRecordClosure *Closure) {
612*bed243d3SAndroid Build Coastguard Worker  uint32_t Kind;
613*bed243d3SAndroid Build Coastguard Worker  uint32_t TotalSize = sizeof(ValueProfData);
614*bed243d3SAndroid Build Coastguard Worker  const void *Record = Closure->Record;
615*bed243d3SAndroid Build Coastguard Worker
616*bed243d3SAndroid Build Coastguard Worker  for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
617*bed243d3SAndroid Build Coastguard Worker    uint32_t NumValueSites = Closure->GetNumValueSites(Record, Kind);
618*bed243d3SAndroid Build Coastguard Worker    if (!NumValueSites)
619*bed243d3SAndroid Build Coastguard Worker      continue;
620*bed243d3SAndroid Build Coastguard Worker    TotalSize += getValueProfRecordSize(NumValueSites,
621*bed243d3SAndroid Build Coastguard Worker                                        Closure->GetNumValueData(Record, Kind));
622*bed243d3SAndroid Build Coastguard Worker  }
623*bed243d3SAndroid Build Coastguard Worker  return TotalSize;
624*bed243d3SAndroid Build Coastguard Worker}
625*bed243d3SAndroid Build Coastguard Worker
626*bed243d3SAndroid Build Coastguard Worker/*!
627*bed243d3SAndroid Build Coastguard Worker * Extract value profile data of a function for the profile kind \c ValueKind
628*bed243d3SAndroid Build Coastguard Worker * from the \c Closure and serialize the data into \c This record instance.
629*bed243d3SAndroid Build Coastguard Worker */
630*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY void
631*bed243d3SAndroid Build Coastguard WorkerserializeValueProfRecordFrom(ValueProfRecord *This,
632*bed243d3SAndroid Build Coastguard Worker                             ValueProfRecordClosure *Closure,
633*bed243d3SAndroid Build Coastguard Worker                             uint32_t ValueKind, uint32_t NumValueSites) {
634*bed243d3SAndroid Build Coastguard Worker  uint32_t S;
635*bed243d3SAndroid Build Coastguard Worker  const void *Record = Closure->Record;
636*bed243d3SAndroid Build Coastguard Worker  This->Kind = ValueKind;
637*bed243d3SAndroid Build Coastguard Worker  This->NumValueSites = NumValueSites;
638*bed243d3SAndroid Build Coastguard Worker  InstrProfValueData *DstVD = getValueProfRecordValueData(This);
639*bed243d3SAndroid Build Coastguard Worker
640*bed243d3SAndroid Build Coastguard Worker  for (S = 0; S < NumValueSites; S++) {
641*bed243d3SAndroid Build Coastguard Worker    uint32_t ND = Closure->GetNumValueDataForSite(Record, ValueKind, S);
642*bed243d3SAndroid Build Coastguard Worker    This->SiteCountArray[S] = ND;
643*bed243d3SAndroid Build Coastguard Worker    Closure->GetValueForSite(Record, DstVD, ValueKind, S);
644*bed243d3SAndroid Build Coastguard Worker    DstVD += ND;
645*bed243d3SAndroid Build Coastguard Worker  }
646*bed243d3SAndroid Build Coastguard Worker}
647*bed243d3SAndroid Build Coastguard Worker
648*bed243d3SAndroid Build Coastguard Worker/*!
649*bed243d3SAndroid Build Coastguard Worker * Extract value profile data of a function  from the \c Closure
650*bed243d3SAndroid Build Coastguard Worker * and serialize the data into \c DstData if it is not NULL or heap
651*bed243d3SAndroid Build Coastguard Worker * memory allocated by the \c Closure's allocator method. If \c
652*bed243d3SAndroid Build Coastguard Worker * DstData is not null, the caller is expected to set the TotalSize
653*bed243d3SAndroid Build Coastguard Worker * in DstData.
654*bed243d3SAndroid Build Coastguard Worker */
655*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY ValueProfData *
656*bed243d3SAndroid Build Coastguard WorkerserializeValueProfDataFrom(ValueProfRecordClosure *Closure,
657*bed243d3SAndroid Build Coastguard Worker                           ValueProfData *DstData) {
658*bed243d3SAndroid Build Coastguard Worker  uint32_t Kind;
659*bed243d3SAndroid Build Coastguard Worker  uint32_t TotalSize =
660*bed243d3SAndroid Build Coastguard Worker      DstData ? DstData->TotalSize : getValueProfDataSize(Closure);
661*bed243d3SAndroid Build Coastguard Worker
662*bed243d3SAndroid Build Coastguard Worker  ValueProfData *VPD =
663*bed243d3SAndroid Build Coastguard Worker      DstData ? DstData : Closure->AllocValueProfData(TotalSize);
664*bed243d3SAndroid Build Coastguard Worker
665*bed243d3SAndroid Build Coastguard Worker  VPD->TotalSize = TotalSize;
666*bed243d3SAndroid Build Coastguard Worker  VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
667*bed243d3SAndroid Build Coastguard Worker  ValueProfRecord *VR = getFirstValueProfRecord(VPD);
668*bed243d3SAndroid Build Coastguard Worker  for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
669*bed243d3SAndroid Build Coastguard Worker    uint32_t NumValueSites = Closure->GetNumValueSites(Closure->Record, Kind);
670*bed243d3SAndroid Build Coastguard Worker    if (!NumValueSites)
671*bed243d3SAndroid Build Coastguard Worker      continue;
672*bed243d3SAndroid Build Coastguard Worker    serializeValueProfRecordFrom(VR, Closure, Kind, NumValueSites);
673*bed243d3SAndroid Build Coastguard Worker    VR = getValueProfRecordNext(VR);
674*bed243d3SAndroid Build Coastguard Worker  }
675*bed243d3SAndroid Build Coastguard Worker  return VPD;
676*bed243d3SAndroid Build Coastguard Worker}
677*bed243d3SAndroid Build Coastguard Worker
678*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_COMMON_API_IMPL
679*bed243d3SAndroid Build Coastguard Worker#endif /* INSTR_PROF_COMMON_API_IMPL */
680*bed243d3SAndroid Build Coastguard Worker
681*bed243d3SAndroid Build Coastguard Worker/*============================================================================*/
682*bed243d3SAndroid Build Coastguard Worker
683*bed243d3SAndroid Build Coastguard Worker// clang-format off:consider re-enabling clang-format if auto-formatted C macros
684*bed243d3SAndroid Build Coastguard Worker// are readable (e.g., after `issue #82426` is fixed)
685*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_PROF_DATA_DEFINED
686*bed243d3SAndroid Build Coastguard Worker
687*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_PROF_DATA_INC
688*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_INC
689*bed243d3SAndroid Build Coastguard Worker
690*bed243d3SAndroid Build Coastguard Worker/* Helper macros.  */
691*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_SIMPLE_QUOTE(x) #x
692*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_QUOTE(x) INSTR_PROF_SIMPLE_QUOTE(x)
693*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_SIMPLE_CONCAT(x,y) x ## y
694*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_CONCAT(x,y) INSTR_PROF_SIMPLE_CONCAT(x,y)
695*bed243d3SAndroid Build Coastguard Worker
696*bed243d3SAndroid Build Coastguard Worker/* Magic number to detect file format and endianness.
697*bed243d3SAndroid Build Coastguard Worker * Use 255 at one end, since no UTF-8 file can use that character.  Avoid 0,
698*bed243d3SAndroid Build Coastguard Worker * so that utilities, like strings, don't grab it as a string.  129 is also
699*bed243d3SAndroid Build Coastguard Worker * invalid UTF-8, and high enough to be interesting.
700*bed243d3SAndroid Build Coastguard Worker * Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
701*bed243d3SAndroid Build Coastguard Worker * for 32-bit platforms.
702*bed243d3SAndroid Build Coastguard Worker */
703*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_RAW_MAGIC_64 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
704*bed243d3SAndroid Build Coastguard Worker       (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 |  \
705*bed243d3SAndroid Build Coastguard Worker        (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129
706*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_RAW_MAGIC_32 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
707*bed243d3SAndroid Build Coastguard Worker       (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 |  \
708*bed243d3SAndroid Build Coastguard Worker        (uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
709*bed243d3SAndroid Build Coastguard Worker
710*bed243d3SAndroid Build Coastguard Worker/* Raw profile format version (start from 1). */
711*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_RAW_VERSION 10
712*bed243d3SAndroid Build Coastguard Worker/* Indexed profile format version (start from 1). */
713*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_INDEX_VERSION 12
714*bed243d3SAndroid Build Coastguard Worker/* Coverage mapping format version (start from 0). */
715*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVMAP_VERSION 6
716*bed243d3SAndroid Build Coastguard Worker
717*bed243d3SAndroid Build Coastguard Worker/* Profile version is always of type uint64_t. Reserve the upper 32 bits in the
718*bed243d3SAndroid Build Coastguard Worker * version for other variants of profile. We set the 8th most significant bit
719*bed243d3SAndroid Build Coastguard Worker * (i.e. bit 56) to 1 to indicate if this is an IR-level instrumentation
720*bed243d3SAndroid Build Coastguard Worker * generated profile, and 0 if this is a Clang FE generated profile.
721*bed243d3SAndroid Build Coastguard Worker * 1 in bit 57 indicates there are context-sensitive records in the profile.
722*bed243d3SAndroid Build Coastguard Worker * The 59th bit indicates whether to use debug info to correlate profiles.
723*bed243d3SAndroid Build Coastguard Worker * The 60th bit indicates single byte coverage instrumentation.
724*bed243d3SAndroid Build Coastguard Worker * The 61st bit indicates function entry instrumentation only.
725*bed243d3SAndroid Build Coastguard Worker * The 62nd bit indicates whether memory profile information is present.
726*bed243d3SAndroid Build Coastguard Worker * The 63rd bit indicates if this is a temporal profile.
727*bed243d3SAndroid Build Coastguard Worker */
728*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASKS_ALL 0xffffffff00000000ULL
729*bed243d3SAndroid Build Coastguard Worker#define GET_VERSION(V) ((V) & ~VARIANT_MASKS_ALL)
730*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_IR_PROF (0x1ULL << 56)
731*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_CSIR_PROF (0x1ULL << 57)
732*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_INSTR_ENTRY (0x1ULL << 58)
733*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_DBG_CORRELATE (0x1ULL << 59)
734*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_BYTE_COVERAGE (0x1ULL << 60)
735*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_FUNCTION_ENTRY_ONLY (0x1ULL << 61)
736*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_MEMPROF (0x1ULL << 62)
737*bed243d3SAndroid Build Coastguard Worker#define VARIANT_MASK_TEMPORAL_PROF (0x1ULL << 63)
738*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_RAW_VERSION_VAR __llvm_profile_raw_version
739*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_PROFILE_RUNTIME_VAR __llvm_profile_runtime
740*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_PROFILE_COUNTER_BIAS_VAR __llvm_profile_counter_bias
741*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_PROFILE_SET_TIMESTAMP __llvm_profile_set_timestamp
742*bed243d3SAndroid Build Coastguard Worker
743*bed243d3SAndroid Build Coastguard Worker/* The variable that holds the name of the profile data
744*bed243d3SAndroid Build Coastguard Worker * specified via command line. */
745*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_PROFILE_NAME_VAR __llvm_profile_filename
746*bed243d3SAndroid Build Coastguard Worker
747*bed243d3SAndroid Build Coastguard Worker/* section name strings common to all targets other
748*bed243d3SAndroid Build Coastguard Worker   than WIN32 */
749*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_COMMON __llvm_prf_data
750*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_NAME_COMMON __llvm_prf_names
751*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNAME_COMMON __llvm_prf_vns
752*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_CNTS_COMMON __llvm_prf_cnts
753*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_BITS_COMMON __llvm_prf_bits
754*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALS_COMMON __llvm_prf_vals
755*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNODES_COMMON __llvm_prf_vnds
756*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VTAB_COMMON __llvm_prf_vtab
757*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVMAP_COMMON __llvm_covmap
758*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVFUN_COMMON __llvm_covfun
759*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVDATA_COMMON __llvm_covdata
760*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVNAME_COMMON __llvm_covnames
761*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_COMMON __llvm_orderfile
762*bed243d3SAndroid Build Coastguard Worker/* Windows section names. Because these section names contain dollar characters,
763*bed243d3SAndroid Build Coastguard Worker * they must be quoted.
764*bed243d3SAndroid Build Coastguard Worker */
765*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_COFF ".lprfd$M"
766*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_NAME_COFF ".lprfn$M"
767*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNAME_COFF ".lprfvn$M"
768*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_CNTS_COFF ".lprfc$M"
769*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_BITS_COFF ".lprfb$M"
770*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALS_COFF ".lprfv$M"
771*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNODES_COFF ".lprfnd$M"
772*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VTAB_COFF ".lprfvt$M"
773*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVMAP_COFF ".lcovmap$M"
774*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVFUN_COFF ".lcovfun$M"
775*bed243d3SAndroid Build Coastguard Worker/* Since cov data and cov names sections are not allocated, we don't need to
776*bed243d3SAndroid Build Coastguard Worker * access them at runtime.
777*bed243d3SAndroid Build Coastguard Worker */
778*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVDATA_COFF ".lcovd"
779*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVNAME_COFF ".lcovn"
780*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_COFF ".lorderfile$M"
781*bed243d3SAndroid Build Coastguard Worker
782*bed243d3SAndroid Build Coastguard Worker#ifdef _WIN32
783*bed243d3SAndroid Build Coastguard Worker/* Runtime section names and name strings.  */
784*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COFF
785*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COFF
786*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COFF
787*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_BITS_SECT_NAME INSTR_PROF_BITS_COFF
788*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VTAB_SECT_NAME INSTR_PROF_VTAB_COFF
789*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNAME_SECT_NAME INSTR_PROF_VNAME_COFF
790*bed243d3SAndroid Build Coastguard Worker/* Array of pointers. Each pointer points to a list
791*bed243d3SAndroid Build Coastguard Worker * of value nodes associated with one value site.
792*bed243d3SAndroid Build Coastguard Worker */
793*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COFF
794*bed243d3SAndroid Build Coastguard Worker/* Value profile nodes section. */
795*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COFF
796*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COFF
797*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVFUN_SECT_NAME INSTR_PROF_COVFUN_COFF
798*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVDATA_SECT_NAME INSTR_PROF_COVDATA_COFF
799*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVNAME_SECT_NAME INSTR_PROF_COVNAME_COFF
800*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_SECT_NAME INSTR_PROF_ORDERFILE_COFF
801*bed243d3SAndroid Build Coastguard Worker#else
802*bed243d3SAndroid Build Coastguard Worker/* Runtime section names and name strings.  */
803*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON)
804*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON)
805*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON)
806*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_BITS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_BITS_COMMON)
807*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VTAB_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VTAB_COMMON)
808*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNAME_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VNAME_COMMON)
809*bed243d3SAndroid Build Coastguard Worker/* Array of pointers. Each pointer points to a list
810*bed243d3SAndroid Build Coastguard Worker * of value nodes associated with one value site.
811*bed243d3SAndroid Build Coastguard Worker */
812*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON)
813*bed243d3SAndroid Build Coastguard Worker/* Value profile nodes section. */
814*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON)
815*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON)
816*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVFUN_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVFUN_COMMON)
817*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVDATA_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVDATA_COMMON)
818*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_COVNAME_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVNAME_COMMON)
819*bed243d3SAndroid Build Coastguard Worker/* Order file instrumentation. */
820*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_SECT_NAME                                         \
821*bed243d3SAndroid Build Coastguard Worker  INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COMMON)
822*bed243d3SAndroid Build Coastguard Worker#endif
823*bed243d3SAndroid Build Coastguard Worker
824*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_BUFFER_NAME _llvm_order_file_buffer
825*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_BUFFER_NAME_STR                                   \
826*bed243d3SAndroid Build Coastguard Worker  INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_BUFFER_NAME)
827*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME _llvm_order_file_buffer_idx
828*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME_STR                               \
829*bed243d3SAndroid Build Coastguard Worker  INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME)
830*bed243d3SAndroid Build Coastguard Worker
831*bed243d3SAndroid Build Coastguard Worker/* Macros to define start/stop section symbol for a given
832*bed243d3SAndroid Build Coastguard Worker * section on Linux. For instance
833*bed243d3SAndroid Build Coastguard Worker * INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME) will
834*bed243d3SAndroid Build Coastguard Worker * expand to __start___llvm_prof_data
835*bed243d3SAndroid Build Coastguard Worker */
836*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_SECT_START(Sect) \
837*bed243d3SAndroid Build Coastguard Worker        INSTR_PROF_CONCAT(__start_,Sect)
838*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_SECT_STOP(Sect) \
839*bed243d3SAndroid Build Coastguard Worker        INSTR_PROF_CONCAT(__stop_,Sect)
840*bed243d3SAndroid Build Coastguard Worker
841*bed243d3SAndroid Build Coastguard Worker/* Value Profiling API linkage name.  */
842*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALUE_PROF_FUNC __llvm_profile_instrument_target
843*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALUE_PROF_FUNC_STR \
844*bed243d3SAndroid Build Coastguard Worker        INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_FUNC)
845*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALUE_PROF_MEMOP_FUNC __llvm_profile_instrument_memop
846*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_VALUE_PROF_MEMOP_FUNC_STR                                   \
847*bed243d3SAndroid Build Coastguard Worker  INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_MEMOP_FUNC)
848*bed243d3SAndroid Build Coastguard Worker
849*bed243d3SAndroid Build Coastguard Worker/* InstrProfile per-function control data alignment.  */
850*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_DATA_ALIGNMENT 8
851*bed243d3SAndroid Build Coastguard Worker
852*bed243d3SAndroid Build Coastguard Worker/* The data structure that represents a tracked value by the
853*bed243d3SAndroid Build Coastguard Worker * value profiler.
854*bed243d3SAndroid Build Coastguard Worker */
855*bed243d3SAndroid Build Coastguard Workertypedef struct InstrProfValueData {
856*bed243d3SAndroid Build Coastguard Worker  /* Profiled value. */
857*bed243d3SAndroid Build Coastguard Worker  uint64_t Value;
858*bed243d3SAndroid Build Coastguard Worker  /* Number of times the value appears in the training run. */
859*bed243d3SAndroid Build Coastguard Worker  uint64_t Count;
860*bed243d3SAndroid Build Coastguard Worker} InstrProfValueData;
861*bed243d3SAndroid Build Coastguard Worker
862*bed243d3SAndroid Build Coastguard Worker#endif /* INSTR_PROF_DATA_INC */
863*bed243d3SAndroid Build Coastguard Worker
864*bed243d3SAndroid Build Coastguard Worker#ifndef INSTR_ORDER_FILE_INC
865*bed243d3SAndroid Build Coastguard Worker/* The maximal # of functions: 128*1024 (the buffer size will be 128*4 KB). */
866*bed243d3SAndroid Build Coastguard Worker#define INSTR_ORDER_FILE_BUFFER_SIZE 131072
867*bed243d3SAndroid Build Coastguard Worker#define INSTR_ORDER_FILE_BUFFER_BITS 17
868*bed243d3SAndroid Build Coastguard Worker#define INSTR_ORDER_FILE_BUFFER_MASK 0x1ffff
869*bed243d3SAndroid Build Coastguard Worker#endif /* INSTR_ORDER_FILE_INC */
870*bed243d3SAndroid Build Coastguard Worker#else
871*bed243d3SAndroid Build Coastguard Worker#undef INSTR_PROF_DATA_DEFINED
872*bed243d3SAndroid Build Coastguard Worker#endif
873*bed243d3SAndroid Build Coastguard Worker
874*bed243d3SAndroid Build Coastguard Worker#undef COVMAP_V2_OR_V3
875*bed243d3SAndroid Build Coastguard Worker
876*bed243d3SAndroid Build Coastguard Worker#ifdef INSTR_PROF_VALUE_PROF_MEMOP_API
877*bed243d3SAndroid Build Coastguard Worker
878*bed243d3SAndroid Build Coastguard Worker#ifdef __cplusplus
879*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_INLINE inline
880*bed243d3SAndroid Build Coastguard Worker#else
881*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_INLINE
882*bed243d3SAndroid Build Coastguard Worker#endif
883*bed243d3SAndroid Build Coastguard Worker
884*bed243d3SAndroid Build Coastguard Worker/* The value range buckets (22 buckets) for the memop size value profiling looks
885*bed243d3SAndroid Build Coastguard Worker * like:
886*bed243d3SAndroid Build Coastguard Worker *
887*bed243d3SAndroid Build Coastguard Worker *   [0, 0]
888*bed243d3SAndroid Build Coastguard Worker *   [1, 1]
889*bed243d3SAndroid Build Coastguard Worker *   [2, 2]
890*bed243d3SAndroid Build Coastguard Worker *   [3, 3]
891*bed243d3SAndroid Build Coastguard Worker *   [4, 4]
892*bed243d3SAndroid Build Coastguard Worker *   [5, 5]
893*bed243d3SAndroid Build Coastguard Worker *   [6, 6]
894*bed243d3SAndroid Build Coastguard Worker *   [7, 7]
895*bed243d3SAndroid Build Coastguard Worker *   [8, 8]
896*bed243d3SAndroid Build Coastguard Worker *   [9, 15]
897*bed243d3SAndroid Build Coastguard Worker *   [16, 16]
898*bed243d3SAndroid Build Coastguard Worker *   [17, 31]
899*bed243d3SAndroid Build Coastguard Worker *   [32, 32]
900*bed243d3SAndroid Build Coastguard Worker *   [33, 63]
901*bed243d3SAndroid Build Coastguard Worker *   [64, 64]
902*bed243d3SAndroid Build Coastguard Worker *   [65, 127]
903*bed243d3SAndroid Build Coastguard Worker *   [128, 128]
904*bed243d3SAndroid Build Coastguard Worker *   [129, 255]
905*bed243d3SAndroid Build Coastguard Worker *   [256, 256]
906*bed243d3SAndroid Build Coastguard Worker *   [257, 511]
907*bed243d3SAndroid Build Coastguard Worker *   [512, 512]
908*bed243d3SAndroid Build Coastguard Worker *   [513, UINT64_MAX]
909*bed243d3SAndroid Build Coastguard Worker *
910*bed243d3SAndroid Build Coastguard Worker * Each range has a 'representative value' which is the lower end value of the
911*bed243d3SAndroid Build Coastguard Worker * range and used to store in the runtime profile data records and the VP
912*bed243d3SAndroid Build Coastguard Worker * metadata. For example, it's 2 for [2, 2] and 64 for [65, 127].
913*bed243d3SAndroid Build Coastguard Worker */
914*bed243d3SAndroid Build Coastguard Worker#define INSTR_PROF_NUM_BUCKETS 22
915*bed243d3SAndroid Build Coastguard Worker
916*bed243d3SAndroid Build Coastguard Worker/*
917*bed243d3SAndroid Build Coastguard Worker * Clz and Popcount. This code was copied from
918*bed243d3SAndroid Build Coastguard Worker * compiler-rt/lib/fuzzer/{FuzzerBuiltins.h,FuzzerBuiltinsMsvc.h} and
919*bed243d3SAndroid Build Coastguard Worker * llvm/include/llvm/Support/MathExtras.h.
920*bed243d3SAndroid Build Coastguard Worker */
921*bed243d3SAndroid Build Coastguard Worker#if defined(_MSC_VER) && !defined(__clang__)
922*bed243d3SAndroid Build Coastguard Worker
923*bed243d3SAndroid Build Coastguard Worker#include <intrin.h>
924*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE
925*bed243d3SAndroid Build Coastguard Workerint InstProfClzll(unsigned long long X) {
926*bed243d3SAndroid Build Coastguard Worker  unsigned long LeadZeroIdx = 0;
927*bed243d3SAndroid Build Coastguard Worker#if !defined(_M_ARM64) && !defined(_M_X64)
928*bed243d3SAndroid Build Coastguard Worker  // Scan the high 32 bits.
929*bed243d3SAndroid Build Coastguard Worker  if (_BitScanReverse(&LeadZeroIdx, (unsigned long)(X >> 32)))
930*bed243d3SAndroid Build Coastguard Worker    return (int)(63 - (LeadZeroIdx + 32)); // Create a bit offset
931*bed243d3SAndroid Build Coastguard Worker                                                      // from the MSB.
932*bed243d3SAndroid Build Coastguard Worker  // Scan the low 32 bits.
933*bed243d3SAndroid Build Coastguard Worker  if (_BitScanReverse(&LeadZeroIdx, (unsigned long)(X)))
934*bed243d3SAndroid Build Coastguard Worker    return (int)(63 - LeadZeroIdx);
935*bed243d3SAndroid Build Coastguard Worker#else
936*bed243d3SAndroid Build Coastguard Worker  if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
937*bed243d3SAndroid Build Coastguard Worker#endif
938*bed243d3SAndroid Build Coastguard Worker  return 64;
939*bed243d3SAndroid Build Coastguard Worker}
940*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE
941*bed243d3SAndroid Build Coastguard Workerint InstProfPopcountll(unsigned long long X) {
942*bed243d3SAndroid Build Coastguard Worker  // This code originates from https://reviews.llvm.org/rG30626254510f.
943*bed243d3SAndroid Build Coastguard Worker  unsigned long long v = X;
944*bed243d3SAndroid Build Coastguard Worker  v = v - ((v >> 1) & 0x5555555555555555ULL);
945*bed243d3SAndroid Build Coastguard Worker  v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
946*bed243d3SAndroid Build Coastguard Worker  v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
947*bed243d3SAndroid Build Coastguard Worker  return (int)((unsigned long long)(v * 0x0101010101010101ULL) >> 56);
948*bed243d3SAndroid Build Coastguard Worker}
949*bed243d3SAndroid Build Coastguard Worker
950*bed243d3SAndroid Build Coastguard Worker#else
951*bed243d3SAndroid Build Coastguard Worker
952*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE
953*bed243d3SAndroid Build Coastguard Workerint InstProfClzll(unsigned long long X) { return __builtin_clzll(X); }
954*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE
955*bed243d3SAndroid Build Coastguard Workerint InstProfPopcountll(unsigned long long X) { return __builtin_popcountll(X); }
956*bed243d3SAndroid Build Coastguard Worker
957*bed243d3SAndroid Build Coastguard Worker#endif  /* defined(_MSC_VER) && !defined(__clang__) */
958*bed243d3SAndroid Build Coastguard Worker
959*bed243d3SAndroid Build Coastguard Worker// clang-format on
960*bed243d3SAndroid Build Coastguard Worker
961*bed243d3SAndroid Build Coastguard Worker/* Map an (observed) memop size value to the representative value of its range.
962*bed243d3SAndroid Build Coastguard Worker * For example, 5 -> 5, 22 -> 17, 99 -> 65, 256 -> 256, 1001 -> 513. */
963*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE uint64_t
964*bed243d3SAndroid Build Coastguard WorkerInstrProfGetRangeRepValue(uint64_t Value) {
965*bed243d3SAndroid Build Coastguard Worker  if (Value <= 8)
966*bed243d3SAndroid Build Coastguard Worker    // The first ranges are individually tracked. Use the value as is.
967*bed243d3SAndroid Build Coastguard Worker    return Value;
968*bed243d3SAndroid Build Coastguard Worker  else if (Value >= 513)
969*bed243d3SAndroid Build Coastguard Worker    // The last range is mapped to its lowest value.
970*bed243d3SAndroid Build Coastguard Worker    return 513;
971*bed243d3SAndroid Build Coastguard Worker  else if (InstProfPopcountll(Value) == 1)
972*bed243d3SAndroid Build Coastguard Worker    // If it's a power of two, use it as is.
973*bed243d3SAndroid Build Coastguard Worker    return Value;
974*bed243d3SAndroid Build Coastguard Worker  else
975*bed243d3SAndroid Build Coastguard Worker    // Otherwise, take to the previous power of two + 1.
976*bed243d3SAndroid Build Coastguard Worker    return (UINT64_C(1) << (64 - InstProfClzll(Value) - 1)) + 1;
977*bed243d3SAndroid Build Coastguard Worker}
978*bed243d3SAndroid Build Coastguard Worker
979*bed243d3SAndroid Build Coastguard Worker/* Return true if the range that an (observed) memop size value belongs to has
980*bed243d3SAndroid Build Coastguard Worker * only a single value in the range.  For example, 0 -> true, 8 -> true, 10 ->
981*bed243d3SAndroid Build Coastguard Worker * false, 64 -> true, 100 -> false, 513 -> false. */
982*bed243d3SAndroid Build Coastguard WorkerINSTR_PROF_VISIBILITY INSTR_PROF_INLINE unsigned
983*bed243d3SAndroid Build Coastguard WorkerInstrProfIsSingleValRange(uint64_t Value) {
984*bed243d3SAndroid Build Coastguard Worker  if (Value <= 8)
985*bed243d3SAndroid Build Coastguard Worker    // The first ranges are individually tracked.
986*bed243d3SAndroid Build Coastguard Worker    return 1;
987*bed243d3SAndroid Build Coastguard Worker  else if (InstProfPopcountll(Value) == 1)
988*bed243d3SAndroid Build Coastguard Worker    // If it's a power of two, there's only one value.
989*bed243d3SAndroid Build Coastguard Worker    return 1;
990*bed243d3SAndroid Build Coastguard Worker  else
991*bed243d3SAndroid Build Coastguard Worker    // Otherwise, there's more than one value in the range.
992*bed243d3SAndroid Build Coastguard Worker    return 0;
993*bed243d3SAndroid Build Coastguard Worker}
994*bed243d3SAndroid Build Coastguard Worker
995*bed243d3SAndroid Build Coastguard Worker#endif /* INSTR_PROF_VALUE_PROF_MEMOP_API */
996