Path: blob/main/contrib/llvm-project/compiler-rt/lib/profile/InstrProfiling.c
35233 views
/*===- InstrProfiling.c - Support library for PGO instrumentation ---------===*\1|*2|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3|* See https://llvm.org/LICENSE.txt for license information.4|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5|*6\*===----------------------------------------------------------------------===*/78// Note: This is linked into the Darwin kernel, and must remain compatible9// with freestanding compilation. See `darwin_add_builtin_libraries`.1011#include <limits.h>12#include <stdio.h>13#include <stdlib.h>14#include <string.h>1516#include "InstrProfiling.h"17#include "InstrProfilingInternal.h"1819#define INSTR_PROF_VALUE_PROF_DATA20#include "profile/InstrProfData.inc"2122static uint32_t __llvm_profile_global_timestamp = 1;2324COMPILER_RT_VISIBILITY25void INSTR_PROF_PROFILE_SET_TIMESTAMP(uint64_t *Probe) {26if (*Probe == 0 || *Probe == (uint64_t)-1)27*Probe = __llvm_profile_global_timestamp++;28}2930COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_magic(void) {31return sizeof(void *) == sizeof(uint64_t) ? (INSTR_PROF_RAW_MAGIC_64)32: (INSTR_PROF_RAW_MAGIC_32);33}3435COMPILER_RT_VISIBILITY void __llvm_profile_set_dumped(void) {36lprofSetProfileDumped(1);37}3839/* Return the number of bytes needed to add to SizeInBytes to make it40* the result a multiple of 8.41*/42COMPILER_RT_VISIBILITY uint8_t43__llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes) {44return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));45}4647COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {48return INSTR_PROF_RAW_VERSION_VAR;49}5051COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {52if (__llvm_profile_get_version() & VARIANT_MASK_TEMPORAL_PROF)53__llvm_profile_global_timestamp = 1;5455char *I = __llvm_profile_begin_counters();56char *E = __llvm_profile_end_counters();5758char ResetValue =59(__llvm_profile_get_version() & VARIANT_MASK_BYTE_COVERAGE) ? 0xFF : 0;60memset(I, ResetValue, E - I);6162I = __llvm_profile_begin_bitmap();63E = __llvm_profile_end_bitmap();64memset(I, 0x0, E - I);6566const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();67const __llvm_profile_data *DataEnd = __llvm_profile_end_data();68const __llvm_profile_data *DI;69for (DI = DataBegin; DI < DataEnd; ++DI) {70uint64_t CurrentVSiteCount = 0;71uint32_t VKI, i;72if (!DI->Values)73continue;7475ValueProfNode **ValueCounters = (ValueProfNode **)DI->Values;7677for (VKI = IPVK_First; VKI <= IPVK_Last; ++VKI)78CurrentVSiteCount += DI->NumValueSites[VKI];7980for (i = 0; i < CurrentVSiteCount; ++i) {81ValueProfNode *CurrVNode = ValueCounters[i];8283while (CurrVNode) {84CurrVNode->Count = 0;85CurrVNode = CurrVNode->Next;86}87}88}89lprofSetProfileDumped(0);90}919293