diff options
Diffstat (limited to 'compiler-rt/lib/profile/InstrProfilingValue.c')
-rw-r--r-- | compiler-rt/lib/profile/InstrProfilingValue.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/compiler-rt/lib/profile/InstrProfilingValue.c b/compiler-rt/lib/profile/InstrProfilingValue.c index d4991470038..1253a6b2da6 100644 --- a/compiler-rt/lib/profile/InstrProfilingValue.c +++ b/compiler-rt/lib/profile/InstrProfilingValue.c @@ -117,6 +117,118 @@ __llvm_profile_instrument_target(uint64_t TargetValue, void *Data, } } +ValueProfData *allocValueProfDataRT(size_t TotalSizeInBytes) { + return (ValueProfData *)calloc(TotalSizeInBytes, 1); +} + +/* + * The value profiler runtime library stores the value profile data + * for a given function in \c NumValueSites and \c Nodes structures. + * \c ValueProfRuntimeRecord class is used to encapsulate the runtime + * profile data and provides fast interfaces to retrieve the profile + * information. This interface is used to initialize the runtime record + * and pre-compute the information needed for efficient implementation + * of callbacks required by ValueProfRecordClosure class. + */ +static int +initializeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord, + const uint16_t *NumValueSites, + ValueProfNode **Nodes) { + unsigned I, S = 0, NumValueKinds = 0; + RuntimeRecord->NumValueSites = NumValueSites; + RuntimeRecord->Nodes = Nodes; + for (I = 0; I <= IPVK_Last; I++) { + uint16_t N = NumValueSites[I]; + if (!N) + continue; + NumValueKinds++; + + RuntimeRecord->NodesKind[I] = Nodes ? &Nodes[S] : INSTR_PROF_NULLPTR; + S += N; + } + RuntimeRecord->NumValueKinds = NumValueKinds; + return 0; +} + +static void +finalizeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord) {} + +/* ValueProfRecordClosure Interface implementation for + * ValueProfDataRuntimeRecord. */ +static uint32_t getNumValueKindsRT(const void *R) { + return ((const ValueProfRuntimeRecord *)R)->NumValueKinds; +} + +static uint32_t getNumValueSitesRT(const void *R, uint32_t VK) { + return ((const ValueProfRuntimeRecord *)R)->NumValueSites[VK]; +} + +static uint32_t getNumValueDataForSiteRT(const void *R, uint32_t VK, + uint32_t S) { + uint32_t C = 0; + const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R; + ValueProfNode *Site = + Record->NodesKind[VK] ? Record->NodesKind[VK][S] : INSTR_PROF_NULLPTR; + while (Site) { + C++; + Site = Site->Next; + } + if (C > UCHAR_MAX) + C = UCHAR_MAX; + + return C; +} + +static uint32_t getNumValueDataRT(const void *R, uint32_t VK) { + unsigned I, S = 0; + const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R; + for (I = 0; I < Record->NumValueSites[VK]; I++) + S += getNumValueDataForSiteRT(Record, VK, I); + return S; +} + +static void getValueForSiteRT(const void *R, InstrProfValueData *Dst, + uint32_t VK, uint32_t S) { + unsigned I, N = 0; + const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R; + N = getNumValueDataForSiteRT(R, VK, S); + if (N == 0) + return; + ValueProfNode *VNode = Record->NodesKind[VK][S]; + for (I = 0; I < N; I++) { + Dst[I] = VNode->VData; + VNode = VNode->Next; + } +} + +static ValueProfRecordClosure RTRecordClosure = { + INSTR_PROF_NULLPTR, getNumValueKindsRT, getNumValueSitesRT, + getNumValueDataRT, getNumValueDataForSiteRT, INSTR_PROF_NULLPTR, + getValueForSiteRT, allocValueProfDataRT}; + +/* + * Return a ValueProfData instance that stores the data collected + * from runtime. If \c DstData is provided by the caller, the value + * profile data will be store in *DstData and DstData is returned, + * otherwise the method will allocate space for the value data and + * return pointer to the newly allocated space. + */ +static ValueProfData * +serializeValueProfDataFromRT(const ValueProfRuntimeRecord *Record, + ValueProfData *DstData) { + RTRecordClosure.Record = Record; + return serializeValueProfDataFrom(&RTRecordClosure, DstData); +} + +/* + * Return the size of ValueProfData structure to store data + * recorded in the runtime record. + */ +static uint32_t getValueProfDataSizeRT(const ValueProfRuntimeRecord *Record) { + RTRecordClosure.Record = Record; + return getValueProfDataSize(&RTRecordClosure); +} + COMPILER_RT_VISIBILITY struct ValueProfData * lprofGatherValueProfData(const __llvm_profile_data *Data) { ValueProfData *VD = NULL; |