xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformOther.c (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric /*===- InstrProfilingPlatformOther.c - Profile data default platform ------===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__) &&     \
100b57cec5SDimitry Andric     !(defined(__sun__) && defined(__svr4__)) && !defined(__NetBSD__) &&        \
110b57cec5SDimitry Andric     !defined(_WIN32)
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include <stdlib.h>
140b57cec5SDimitry Andric #include <stdio.h>
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "InstrProfiling.h"
17fe6060f1SDimitry Andric #include "InstrProfilingInternal.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric static const __llvm_profile_data *DataFirst = NULL;
200b57cec5SDimitry Andric static const __llvm_profile_data *DataLast = NULL;
210b57cec5SDimitry Andric static const char *NamesFirst = NULL;
220b57cec5SDimitry Andric static const char *NamesLast = NULL;
230b57cec5SDimitry Andric static uint64_t *CountersFirst = NULL;
240b57cec5SDimitry Andric static uint64_t *CountersLast = NULL;
250b57cec5SDimitry Andric static uint32_t *OrderFileFirst = NULL;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric static const void *getMinAddr(const void *A1, const void *A2) {
280b57cec5SDimitry Andric   return A1 < A2 ? A1 : A2;
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric static const void *getMaxAddr(const void *A1, const void *A2) {
320b57cec5SDimitry Andric   return A1 > A2 ? A1 : A2;
330b57cec5SDimitry Andric }
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric /*!
360b57cec5SDimitry Andric  * \brief Register an instrumented function.
370b57cec5SDimitry Andric  *
380b57cec5SDimitry Andric  * Calls to this are emitted by clang with -fprofile-instr-generate.  Such
390b57cec5SDimitry Andric  * calls are only required (and only emitted) on targets where we haven't
400b57cec5SDimitry Andric  * implemented linker magic to find the bounds of the sections.
410b57cec5SDimitry Andric  */
420b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
430b57cec5SDimitry Andric void __llvm_profile_register_function(void *Data_) {
440b57cec5SDimitry Andric   /* TODO: Only emit this function if we can't use linker magic. */
450b57cec5SDimitry Andric   const __llvm_profile_data *Data = (__llvm_profile_data *)Data_;
460b57cec5SDimitry Andric   if (!DataFirst) {
470b57cec5SDimitry Andric     DataFirst = Data;
480b57cec5SDimitry Andric     DataLast = Data + 1;
49*349cc55cSDimitry Andric     CountersFirst = (uint64_t *)((uintptr_t)Data_ + Data->CounterPtr);
50*349cc55cSDimitry Andric     CountersLast = CountersFirst + Data->NumCounters;
510b57cec5SDimitry Andric     return;
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   DataFirst = (const __llvm_profile_data *)getMinAddr(DataFirst, Data);
55*349cc55cSDimitry Andric   CountersFirst = (uint64_t *)getMinAddr(
56*349cc55cSDimitry Andric       CountersFirst, (uint64_t *)((uintptr_t)Data_ + Data->CounterPtr));
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   DataLast = (const __llvm_profile_data *)getMaxAddr(DataLast, Data + 1);
590b57cec5SDimitry Andric   CountersLast = (uint64_t *)getMaxAddr(
60*349cc55cSDimitry Andric       CountersLast,
61*349cc55cSDimitry Andric       (uint64_t *)((uintptr_t)Data_ + Data->CounterPtr) + Data->NumCounters);
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
650b57cec5SDimitry Andric void __llvm_profile_register_names_function(void *NamesStart,
660b57cec5SDimitry Andric                                             uint64_t NamesSize) {
670b57cec5SDimitry Andric   if (!NamesFirst) {
680b57cec5SDimitry Andric     NamesFirst = (const char *)NamesStart;
690b57cec5SDimitry Andric     NamesLast = (const char *)NamesStart + NamesSize;
700b57cec5SDimitry Andric     return;
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric   NamesFirst = (const char *)getMinAddr(NamesFirst, NamesStart);
730b57cec5SDimitry Andric   NamesLast =
740b57cec5SDimitry Andric       (const char *)getMaxAddr(NamesLast, (const char *)NamesStart + NamesSize);
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
780b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_begin_data(void) { return DataFirst; }
790b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
800b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_end_data(void) { return DataLast; }
810b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
820b57cec5SDimitry Andric const char *__llvm_profile_begin_names(void) { return NamesFirst; }
830b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
840b57cec5SDimitry Andric const char *__llvm_profile_end_names(void) { return NamesLast; }
850b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
860b57cec5SDimitry Andric uint64_t *__llvm_profile_begin_counters(void) { return CountersFirst; }
870b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
880b57cec5SDimitry Andric uint64_t *__llvm_profile_end_counters(void) { return CountersLast; }
890b57cec5SDimitry Andric /* TODO: correctly set up OrderFileFirst. */
900b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
910b57cec5SDimitry Andric uint32_t *__llvm_profile_begin_orderfile(void) { return OrderFileFirst; }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
940b57cec5SDimitry Andric ValueProfNode *__llvm_profile_begin_vnodes(void) {
950b57cec5SDimitry Andric   return 0;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
980b57cec5SDimitry Andric ValueProfNode *__llvm_profile_end_vnodes(void) { return 0; }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = 0;
1010b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = 0;
1020b57cec5SDimitry Andric 
103fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
104fe6060f1SDimitry Andric   return 0;
105fe6060f1SDimitry Andric }
106fe6060f1SDimitry Andric 
1070b57cec5SDimitry Andric #endif
108