xref: /aosp_15_r20/external/compiler-rt/lib/profile/InstrProfilingValue.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /*===- InstrProfilingValue.c - Support library for PGO instrumentation ----===*\
2*7c3d14c8STreehugger Robot |*
3*7c3d14c8STreehugger Robot |*                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot |*
5*7c3d14c8STreehugger Robot |* This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot |* License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot |*
8*7c3d14c8STreehugger Robot \*===----------------------------------------------------------------------===*/
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot #include "InstrProfiling.h"
11*7c3d14c8STreehugger Robot #include "InstrProfilingInternal.h"
12*7c3d14c8STreehugger Robot #include "InstrProfilingUtil.h" /* For PS4 getenv shim. */
13*7c3d14c8STreehugger Robot #include <limits.h>
14*7c3d14c8STreehugger Robot #include <stdio.h>
15*7c3d14c8STreehugger Robot #include <stdlib.h>
16*7c3d14c8STreehugger Robot #include <string.h>
17*7c3d14c8STreehugger Robot #define INSTR_PROF_VALUE_PROF_DATA
18*7c3d14c8STreehugger Robot #define INSTR_PROF_COMMON_API_IMPL
19*7c3d14c8STreehugger Robot #include "InstrProfData.inc"
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot static int hasStaticCounters = 1;
22*7c3d14c8STreehugger Robot static int OutOfNodesWarnings = 0;
23*7c3d14c8STreehugger Robot static int hasNonDefaultValsPerSite = 0;
24*7c3d14c8STreehugger Robot #define INSTR_PROF_MAX_VP_WARNS 10
25*7c3d14c8STreehugger Robot #define INSTR_PROF_DEFAULT_NUM_VAL_PER_SITE 8
26*7c3d14c8STreehugger Robot #define INSTR_PROF_VNODE_POOL_SIZE 1024
27*7c3d14c8STreehugger Robot 
28*7c3d14c8STreehugger Robot #ifndef _MSC_VER
29*7c3d14c8STreehugger Robot /* A shared static pool in addition to the vnodes statically
30*7c3d14c8STreehugger Robot  * allocated by the compiler.  */
31*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY ValueProfNode
32*7c3d14c8STreehugger Robot     lprofValueProfNodes[INSTR_PROF_VNODE_POOL_SIZE] COMPILER_RT_SECTION(
33*7c3d14c8STreehugger Robot        COMPILER_RT_SEG INSTR_PROF_VNODES_SECT_NAME_STR);
34*7c3d14c8STreehugger Robot #endif
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY uint32_t VPMaxNumValsPerSite =
37*7c3d14c8STreehugger Robot     INSTR_PROF_DEFAULT_NUM_VAL_PER_SITE;
38*7c3d14c8STreehugger Robot 
lprofSetupValueProfiler()39*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY void lprofSetupValueProfiler() {
40*7c3d14c8STreehugger Robot   const char *Str = 0;
41*7c3d14c8STreehugger Robot   Str = getenv("LLVM_VP_MAX_NUM_VALS_PER_SITE");
42*7c3d14c8STreehugger Robot   if (Str && Str[0]) {
43*7c3d14c8STreehugger Robot     VPMaxNumValsPerSite = atoi(Str);
44*7c3d14c8STreehugger Robot     hasNonDefaultValsPerSite = 1;
45*7c3d14c8STreehugger Robot   }
46*7c3d14c8STreehugger Robot   if (VPMaxNumValsPerSite > INSTR_PROF_MAX_NUM_VAL_PER_SITE)
47*7c3d14c8STreehugger Robot     VPMaxNumValsPerSite = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
48*7c3d14c8STreehugger Robot }
49*7c3d14c8STreehugger Robot 
lprofSetMaxValsPerSite(uint32_t MaxVals)50*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY void lprofSetMaxValsPerSite(uint32_t MaxVals) {
51*7c3d14c8STreehugger Robot   VPMaxNumValsPerSite = MaxVals;
52*7c3d14c8STreehugger Robot   hasNonDefaultValsPerSite = 1;
53*7c3d14c8STreehugger Robot }
54*7c3d14c8STreehugger Robot 
55*7c3d14c8STreehugger Robot /* This method is only used in value profiler mock testing.  */
56*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY void
__llvm_profile_set_num_value_sites(__llvm_profile_data * Data,uint32_t ValueKind,uint16_t NumValueSites)57*7c3d14c8STreehugger Robot __llvm_profile_set_num_value_sites(__llvm_profile_data *Data,
58*7c3d14c8STreehugger Robot                                    uint32_t ValueKind, uint16_t NumValueSites) {
59*7c3d14c8STreehugger Robot   *((uint16_t *)&Data->NumValueSites[ValueKind]) = NumValueSites;
60*7c3d14c8STreehugger Robot }
61*7c3d14c8STreehugger Robot 
62*7c3d14c8STreehugger Robot /* This method is only used in value profiler mock testing.  */
63*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY const __llvm_profile_data *
__llvm_profile_iterate_data(const __llvm_profile_data * Data)64*7c3d14c8STreehugger Robot __llvm_profile_iterate_data(const __llvm_profile_data *Data) {
65*7c3d14c8STreehugger Robot   return Data + 1;
66*7c3d14c8STreehugger Robot }
67*7c3d14c8STreehugger Robot 
68*7c3d14c8STreehugger Robot /* This method is only used in value profiler mock testing.  */
69*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY void *
__llvm_get_function_addr(const __llvm_profile_data * Data)70*7c3d14c8STreehugger Robot __llvm_get_function_addr(const __llvm_profile_data *Data) {
71*7c3d14c8STreehugger Robot   return Data->FunctionPointer;
72*7c3d14c8STreehugger Robot }
73*7c3d14c8STreehugger Robot 
74*7c3d14c8STreehugger Robot /* Allocate an array that holds the pointers to the linked lists of
75*7c3d14c8STreehugger Robot  * value profile counter nodes. The number of element of the array
76*7c3d14c8STreehugger Robot  * is the total number of value profile sites instrumented. Returns
77*7c3d14c8STreehugger Robot  * 0 if allocation fails.
78*7c3d14c8STreehugger Robot  */
79*7c3d14c8STreehugger Robot 
allocateValueProfileCounters(__llvm_profile_data * Data)80*7c3d14c8STreehugger Robot static int allocateValueProfileCounters(__llvm_profile_data *Data) {
81*7c3d14c8STreehugger Robot   uint64_t NumVSites = 0;
82*7c3d14c8STreehugger Robot   uint32_t VKI;
83*7c3d14c8STreehugger Robot 
84*7c3d14c8STreehugger Robot   /* This function will never be called when value site array is allocated
85*7c3d14c8STreehugger Robot      statically at compile time.  */
86*7c3d14c8STreehugger Robot   hasStaticCounters = 0;
87*7c3d14c8STreehugger Robot   /* When dynamic allocation is enabled, allow tracking the max number of
88*7c3d14c8STreehugger Robot    * values allowd.  */
89*7c3d14c8STreehugger Robot   if (!hasNonDefaultValsPerSite)
90*7c3d14c8STreehugger Robot     VPMaxNumValsPerSite = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
91*7c3d14c8STreehugger Robot 
92*7c3d14c8STreehugger Robot   for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)
93*7c3d14c8STreehugger Robot     NumVSites += Data->NumValueSites[VKI];
94*7c3d14c8STreehugger Robot 
95*7c3d14c8STreehugger Robot   ValueProfNode **Mem =
96*7c3d14c8STreehugger Robot       (ValueProfNode **)calloc(NumVSites, sizeof(ValueProfNode *));
97*7c3d14c8STreehugger Robot   if (!Mem)
98*7c3d14c8STreehugger Robot     return 0;
99*7c3d14c8STreehugger Robot   if (!COMPILER_RT_BOOL_CMPXCHG(&Data->Values, 0, Mem)) {
100*7c3d14c8STreehugger Robot     free(Mem);
101*7c3d14c8STreehugger Robot     return 0;
102*7c3d14c8STreehugger Robot   }
103*7c3d14c8STreehugger Robot   return 1;
104*7c3d14c8STreehugger Robot }
105*7c3d14c8STreehugger Robot 
allocateOneNode(__llvm_profile_data * Data,uint32_t Index,uint64_t Value)106*7c3d14c8STreehugger Robot static ValueProfNode *allocateOneNode(__llvm_profile_data *Data, uint32_t Index,
107*7c3d14c8STreehugger Robot                                       uint64_t Value) {
108*7c3d14c8STreehugger Robot   ValueProfNode *Node;
109*7c3d14c8STreehugger Robot 
110*7c3d14c8STreehugger Robot   if (!hasStaticCounters)
111*7c3d14c8STreehugger Robot     return (ValueProfNode *)calloc(1, sizeof(ValueProfNode));
112*7c3d14c8STreehugger Robot 
113*7c3d14c8STreehugger Robot   /* Early check to avoid value wrapping around.  */
114*7c3d14c8STreehugger Robot   if (CurrentVNode + 1 > EndVNode) {
115*7c3d14c8STreehugger Robot     if (OutOfNodesWarnings++ < INSTR_PROF_MAX_VP_WARNS) {
116*7c3d14c8STreehugger Robot       PROF_WARN("Unable to track new values: %s. "
117*7c3d14c8STreehugger Robot                 " Consider using option -mllvm -vp-counters-per-site=<n> to "
118*7c3d14c8STreehugger Robot                 "allocate more"
119*7c3d14c8STreehugger Robot                 " value profile counters at compile time. \n",
120*7c3d14c8STreehugger Robot                 "Running out of static counters");
121*7c3d14c8STreehugger Robot     }
122*7c3d14c8STreehugger Robot     return 0;
123*7c3d14c8STreehugger Robot   }
124*7c3d14c8STreehugger Robot   Node = COMPILER_RT_PTR_FETCH_ADD(ValueProfNode, CurrentVNode, 1);
125*7c3d14c8STreehugger Robot   /* Due to section padding, EndVNode point to a byte which is one pass
126*7c3d14c8STreehugger Robot    * an incomplete VNode, so we need to skip the last incomplete node. */
127*7c3d14c8STreehugger Robot   if (Node + 1 > EndVNode)
128*7c3d14c8STreehugger Robot     return 0;
129*7c3d14c8STreehugger Robot 
130*7c3d14c8STreehugger Robot   return Node;
131*7c3d14c8STreehugger Robot }
132*7c3d14c8STreehugger Robot 
133*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY void
__llvm_profile_instrument_target(uint64_t TargetValue,void * Data,uint32_t CounterIndex)134*7c3d14c8STreehugger Robot __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
135*7c3d14c8STreehugger Robot                                  uint32_t CounterIndex) {
136*7c3d14c8STreehugger Robot   __llvm_profile_data *PData = (__llvm_profile_data *)Data;
137*7c3d14c8STreehugger Robot   if (!PData)
138*7c3d14c8STreehugger Robot     return;
139*7c3d14c8STreehugger Robot 
140*7c3d14c8STreehugger Robot   if (!PData->Values) {
141*7c3d14c8STreehugger Robot     if (!allocateValueProfileCounters(PData))
142*7c3d14c8STreehugger Robot       return;
143*7c3d14c8STreehugger Robot   }
144*7c3d14c8STreehugger Robot 
145*7c3d14c8STreehugger Robot   ValueProfNode **ValueCounters = (ValueProfNode **)PData->Values;
146*7c3d14c8STreehugger Robot   ValueProfNode *PrevVNode = NULL;
147*7c3d14c8STreehugger Robot   ValueProfNode *MinCountVNode = NULL;
148*7c3d14c8STreehugger Robot   ValueProfNode *CurVNode = ValueCounters[CounterIndex];
149*7c3d14c8STreehugger Robot   uint64_t MinCount = UINT64_MAX;
150*7c3d14c8STreehugger Robot 
151*7c3d14c8STreehugger Robot   uint8_t VDataCount = 0;
152*7c3d14c8STreehugger Robot   while (CurVNode) {
153*7c3d14c8STreehugger Robot     if (TargetValue == CurVNode->Value) {
154*7c3d14c8STreehugger Robot       CurVNode->Count++;
155*7c3d14c8STreehugger Robot       return;
156*7c3d14c8STreehugger Robot     }
157*7c3d14c8STreehugger Robot     if (CurVNode->Count < MinCount) {
158*7c3d14c8STreehugger Robot       MinCount = CurVNode->Count;
159*7c3d14c8STreehugger Robot       MinCountVNode = CurVNode;
160*7c3d14c8STreehugger Robot     }
161*7c3d14c8STreehugger Robot     PrevVNode = CurVNode;
162*7c3d14c8STreehugger Robot     CurVNode = CurVNode->Next;
163*7c3d14c8STreehugger Robot     ++VDataCount;
164*7c3d14c8STreehugger Robot   }
165*7c3d14c8STreehugger Robot 
166*7c3d14c8STreehugger Robot   if (VDataCount >= VPMaxNumValsPerSite) {
167*7c3d14c8STreehugger Robot     /* Bump down the min count node's count. If it reaches 0,
168*7c3d14c8STreehugger Robot      * evict it. This eviction/replacement policy makes hot
169*7c3d14c8STreehugger Robot      * targets more sticky while cold targets less so. In other
170*7c3d14c8STreehugger Robot      * words, it makes it less likely for the hot targets to be
171*7c3d14c8STreehugger Robot      * prematurally evicted during warmup/establishment period,
172*7c3d14c8STreehugger Robot      * when their counts are still low. In a special case when
173*7c3d14c8STreehugger Robot      * the number of values tracked is reduced to only one, this
174*7c3d14c8STreehugger Robot      * policy will guarantee that the dominating target with >50%
175*7c3d14c8STreehugger Robot      * total count will survive in the end. Note that this scheme
176*7c3d14c8STreehugger Robot      * allows the runtime to track the min count node in an adaptive
177*7c3d14c8STreehugger Robot      * manner. It can correct previous mistakes and eventually
178*7c3d14c8STreehugger Robot      * lock on a cold target that is alread in stable state.
179*7c3d14c8STreehugger Robot      *
180*7c3d14c8STreehugger Robot      * In very rare cases,  this replacement scheme may still lead
181*7c3d14c8STreehugger Robot      * to target loss. For instance, out of \c N value slots, \c N-1
182*7c3d14c8STreehugger Robot      * slots are occupied by luke warm targets during the warmup
183*7c3d14c8STreehugger Robot      * period and the remaining one slot is competed by two or more
184*7c3d14c8STreehugger Robot      * very hot targets. If those hot targets occur in an interleaved
185*7c3d14c8STreehugger Robot      * way, none of them will survive (gain enough weight to throw out
186*7c3d14c8STreehugger Robot      * other established entries) due to the ping-pong effect.
187*7c3d14c8STreehugger Robot      * To handle this situation, user can choose to increase the max
188*7c3d14c8STreehugger Robot      * number of tracked values per value site. Alternatively, a more
189*7c3d14c8STreehugger Robot      * expensive eviction mechanism can be implemented. It requires
190*7c3d14c8STreehugger Robot      * the runtime to track the total number of evictions per-site.
191*7c3d14c8STreehugger Robot      * When the total number of evictions reaches certain threshold,
192*7c3d14c8STreehugger Robot      * the runtime can wipe out more than one lowest count entries
193*7c3d14c8STreehugger Robot      * to give space for hot targets.
194*7c3d14c8STreehugger Robot      */
195*7c3d14c8STreehugger Robot     if (!(--MinCountVNode->Count)) {
196*7c3d14c8STreehugger Robot       CurVNode = MinCountVNode;
197*7c3d14c8STreehugger Robot       CurVNode->Value = TargetValue;
198*7c3d14c8STreehugger Robot       CurVNode->Count++;
199*7c3d14c8STreehugger Robot     }
200*7c3d14c8STreehugger Robot     return;
201*7c3d14c8STreehugger Robot   }
202*7c3d14c8STreehugger Robot 
203*7c3d14c8STreehugger Robot   CurVNode = allocateOneNode(PData, CounterIndex, TargetValue);
204*7c3d14c8STreehugger Robot   if (!CurVNode)
205*7c3d14c8STreehugger Robot     return;
206*7c3d14c8STreehugger Robot   CurVNode->Value = TargetValue;
207*7c3d14c8STreehugger Robot   CurVNode->Count++;
208*7c3d14c8STreehugger Robot 
209*7c3d14c8STreehugger Robot   uint32_t Success = 0;
210*7c3d14c8STreehugger Robot   if (!ValueCounters[CounterIndex])
211*7c3d14c8STreehugger Robot     Success =
212*7c3d14c8STreehugger Robot         COMPILER_RT_BOOL_CMPXCHG(&ValueCounters[CounterIndex], 0, CurVNode);
213*7c3d14c8STreehugger Robot   else if (PrevVNode && !PrevVNode->Next)
214*7c3d14c8STreehugger Robot     Success = COMPILER_RT_BOOL_CMPXCHG(&(PrevVNode->Next), 0, CurVNode);
215*7c3d14c8STreehugger Robot 
216*7c3d14c8STreehugger Robot   if (!Success && !hasStaticCounters) {
217*7c3d14c8STreehugger Robot     free(CurVNode);
218*7c3d14c8STreehugger Robot     return;
219*7c3d14c8STreehugger Robot   }
220*7c3d14c8STreehugger Robot }
221*7c3d14c8STreehugger Robot 
222*7c3d14c8STreehugger Robot /*
223*7c3d14c8STreehugger Robot  * A wrapper struct that represents value profile runtime data.
224*7c3d14c8STreehugger Robot  * Like InstrProfRecord class which is used by profiling host tools,
225*7c3d14c8STreehugger Robot  * ValueProfRuntimeRecord also implements the abstract intefaces defined in
226*7c3d14c8STreehugger Robot  * ValueProfRecordClosure so that the runtime data can be serialized using
227*7c3d14c8STreehugger Robot  * shared C implementation.
228*7c3d14c8STreehugger Robot  */
229*7c3d14c8STreehugger Robot typedef struct ValueProfRuntimeRecord {
230*7c3d14c8STreehugger Robot   const __llvm_profile_data *Data;
231*7c3d14c8STreehugger Robot   ValueProfNode **NodesKind[IPVK_Last + 1];
232*7c3d14c8STreehugger Robot   uint8_t **SiteCountArray;
233*7c3d14c8STreehugger Robot } ValueProfRuntimeRecord;
234*7c3d14c8STreehugger Robot 
235*7c3d14c8STreehugger Robot /* ValueProfRecordClosure Interface implementation. */
236*7c3d14c8STreehugger Robot 
getNumValueSitesRT(const void * R,uint32_t VK)237*7c3d14c8STreehugger Robot static uint32_t getNumValueSitesRT(const void *R, uint32_t VK) {
238*7c3d14c8STreehugger Robot   return ((const ValueProfRuntimeRecord *)R)->Data->NumValueSites[VK];
239*7c3d14c8STreehugger Robot }
240*7c3d14c8STreehugger Robot 
getNumValueDataRT(const void * R,uint32_t VK)241*7c3d14c8STreehugger Robot static uint32_t getNumValueDataRT(const void *R, uint32_t VK) {
242*7c3d14c8STreehugger Robot   uint32_t S = 0, I;
243*7c3d14c8STreehugger Robot   const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R;
244*7c3d14c8STreehugger Robot   if (Record->SiteCountArray[VK] == INSTR_PROF_NULLPTR)
245*7c3d14c8STreehugger Robot     return 0;
246*7c3d14c8STreehugger Robot   for (I = 0; I < Record->Data->NumValueSites[VK]; I++)
247*7c3d14c8STreehugger Robot     S += Record->SiteCountArray[VK][I];
248*7c3d14c8STreehugger Robot   return S;
249*7c3d14c8STreehugger Robot }
250*7c3d14c8STreehugger Robot 
getNumValueDataForSiteRT(const void * R,uint32_t VK,uint32_t S)251*7c3d14c8STreehugger Robot static uint32_t getNumValueDataForSiteRT(const void *R, uint32_t VK,
252*7c3d14c8STreehugger Robot                                          uint32_t S) {
253*7c3d14c8STreehugger Robot   const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R;
254*7c3d14c8STreehugger Robot   return Record->SiteCountArray[VK][S];
255*7c3d14c8STreehugger Robot }
256*7c3d14c8STreehugger Robot 
257*7c3d14c8STreehugger Robot static ValueProfRuntimeRecord RTRecord;
258*7c3d14c8STreehugger Robot static ValueProfRecordClosure RTRecordClosure = {
259*7c3d14c8STreehugger Robot     &RTRecord,          INSTR_PROF_NULLPTR, /* GetNumValueKinds */
260*7c3d14c8STreehugger Robot     getNumValueSitesRT, getNumValueDataRT,  getNumValueDataForSiteRT,
261*7c3d14c8STreehugger Robot     INSTR_PROF_NULLPTR, /* RemapValueData */
262*7c3d14c8STreehugger Robot     INSTR_PROF_NULLPTR, /* GetValueForSite, */
263*7c3d14c8STreehugger Robot     INSTR_PROF_NULLPTR  /* AllocValueProfData */
264*7c3d14c8STreehugger Robot };
265*7c3d14c8STreehugger Robot 
266*7c3d14c8STreehugger Robot static uint32_t
initializeValueProfRuntimeRecord(const __llvm_profile_data * Data,uint8_t * SiteCountArray[])267*7c3d14c8STreehugger Robot initializeValueProfRuntimeRecord(const __llvm_profile_data *Data,
268*7c3d14c8STreehugger Robot                                  uint8_t *SiteCountArray[]) {
269*7c3d14c8STreehugger Robot   unsigned I, J, S = 0, NumValueKinds = 0;
270*7c3d14c8STreehugger Robot   ValueProfNode **Nodes = (ValueProfNode **)Data->Values;
271*7c3d14c8STreehugger Robot   RTRecord.Data = Data;
272*7c3d14c8STreehugger Robot   RTRecord.SiteCountArray = SiteCountArray;
273*7c3d14c8STreehugger Robot   for (I = 0; I <= IPVK_Last; I++) {
274*7c3d14c8STreehugger Robot     uint16_t N = Data->NumValueSites[I];
275*7c3d14c8STreehugger Robot     if (!N)
276*7c3d14c8STreehugger Robot       continue;
277*7c3d14c8STreehugger Robot 
278*7c3d14c8STreehugger Robot     NumValueKinds++;
279*7c3d14c8STreehugger Robot 
280*7c3d14c8STreehugger Robot     RTRecord.NodesKind[I] = Nodes ? &Nodes[S] : INSTR_PROF_NULLPTR;
281*7c3d14c8STreehugger Robot     for (J = 0; J < N; J++) {
282*7c3d14c8STreehugger Robot       /* Compute value count for each site. */
283*7c3d14c8STreehugger Robot       uint32_t C = 0;
284*7c3d14c8STreehugger Robot       ValueProfNode *Site =
285*7c3d14c8STreehugger Robot           Nodes ? RTRecord.NodesKind[I][J] : INSTR_PROF_NULLPTR;
286*7c3d14c8STreehugger Robot       while (Site) {
287*7c3d14c8STreehugger Robot         C++;
288*7c3d14c8STreehugger Robot         Site = Site->Next;
289*7c3d14c8STreehugger Robot       }
290*7c3d14c8STreehugger Robot       if (C > UCHAR_MAX)
291*7c3d14c8STreehugger Robot         C = UCHAR_MAX;
292*7c3d14c8STreehugger Robot       RTRecord.SiteCountArray[I][J] = C;
293*7c3d14c8STreehugger Robot     }
294*7c3d14c8STreehugger Robot     S += N;
295*7c3d14c8STreehugger Robot   }
296*7c3d14c8STreehugger Robot   return NumValueKinds;
297*7c3d14c8STreehugger Robot }
298*7c3d14c8STreehugger Robot 
getNextNValueData(uint32_t VK,uint32_t Site,InstrProfValueData * Dst,ValueProfNode * StartNode,uint32_t N)299*7c3d14c8STreehugger Robot static ValueProfNode *getNextNValueData(uint32_t VK, uint32_t Site,
300*7c3d14c8STreehugger Robot                                         InstrProfValueData *Dst,
301*7c3d14c8STreehugger Robot                                         ValueProfNode *StartNode, uint32_t N) {
302*7c3d14c8STreehugger Robot   unsigned I;
303*7c3d14c8STreehugger Robot   ValueProfNode *VNode = StartNode ? StartNode : RTRecord.NodesKind[VK][Site];
304*7c3d14c8STreehugger Robot   for (I = 0; I < N; I++) {
305*7c3d14c8STreehugger Robot     Dst[I].Value = VNode->Value;
306*7c3d14c8STreehugger Robot     Dst[I].Count = VNode->Count;
307*7c3d14c8STreehugger Robot     VNode = VNode->Next;
308*7c3d14c8STreehugger Robot   }
309*7c3d14c8STreehugger Robot   return VNode;
310*7c3d14c8STreehugger Robot }
311*7c3d14c8STreehugger Robot 
getValueProfDataSizeWrapper(void)312*7c3d14c8STreehugger Robot static uint32_t getValueProfDataSizeWrapper(void) {
313*7c3d14c8STreehugger Robot   return getValueProfDataSize(&RTRecordClosure);
314*7c3d14c8STreehugger Robot }
315*7c3d14c8STreehugger Robot 
getNumValueDataForSiteWrapper(uint32_t VK,uint32_t S)316*7c3d14c8STreehugger Robot static uint32_t getNumValueDataForSiteWrapper(uint32_t VK, uint32_t S) {
317*7c3d14c8STreehugger Robot   return getNumValueDataForSiteRT(&RTRecord, VK, S);
318*7c3d14c8STreehugger Robot }
319*7c3d14c8STreehugger Robot 
320*7c3d14c8STreehugger Robot static VPDataReaderType TheVPDataReader = {
321*7c3d14c8STreehugger Robot     initializeValueProfRuntimeRecord, getValueProfRecordHeaderSize,
322*7c3d14c8STreehugger Robot     getFirstValueProfRecord,          getNumValueDataForSiteWrapper,
323*7c3d14c8STreehugger Robot     getValueProfDataSizeWrapper,      getNextNValueData};
324*7c3d14c8STreehugger Robot 
lprofGetVPDataReader()325*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY VPDataReaderType *lprofGetVPDataReader() {
326*7c3d14c8STreehugger Robot   return &TheVPDataReader;
327*7c3d14c8STreehugger Robot }
328