xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPort.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*===- InstrProfilingPort.h- Support library for PGO instrumentation ------===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 \*===----------------------------------------------------------------------===*/
8 
9 /* This header must be included after all others so it can provide fallback
10    definitions for stuff missing in system headers. */
11 
12 #ifndef PROFILE_INSTRPROFILING_PORT_H_
13 #define PROFILE_INSTRPROFILING_PORT_H_
14 
15 #ifdef _MSC_VER
16 #define COMPILER_RT_ALIGNAS(x) __declspec(align(x))
17 #define COMPILER_RT_VISIBILITY
18 /* FIXME: selectany does not have the same semantics as weak. */
19 #define COMPILER_RT_WEAK __declspec(selectany)
20 /* Need to include <windows.h> */
21 #define COMPILER_RT_ALLOCA _alloca
22 /* Need to include <stdio.h> and <io.h> */
23 #define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)
24 #define COMPILER_RT_ALWAYS_INLINE __forceinline
25 #define COMPILER_RT_CLEANUP(x)
26 #elif __GNUC__
27 #define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))
28 #define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
29 #define COMPILER_RT_WEAK __attribute__((weak))
30 #define COMPILER_RT_ALLOCA __builtin_alloca
31 #define COMPILER_RT_FTRUNCATE(f,l) ftruncate(fileno(f),l)
32 #define COMPILER_RT_ALWAYS_INLINE inline __attribute((always_inline))
33 #define COMPILER_RT_CLEANUP(x) __attribute__((cleanup(x)))
34 #endif
35 
36 #if defined(__APPLE__)
37 #define COMPILER_RT_SEG "__DATA,"
38 #else
39 #define COMPILER_RT_SEG ""
40 #endif
41 
42 #ifdef _MSC_VER
43 #define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))
44 #else
45 #define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))
46 #endif
47 
48 #define COMPILER_RT_MAX_HOSTLEN 128
49 #ifdef __ORBIS__
50 #define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
51 #else
52 #define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
53 #endif
54 
55 #if COMPILER_RT_HAS_ATOMICS == 1
56 #ifdef _WIN32
57 #include <windows.h>
58 #if defined(_MSC_VER) && _MSC_VER < 1900
59 #define snprintf _snprintf
60 #endif
61 #if defined(_WIN64)
62 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
63   (InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV,      \
64                                 (LONGLONG)OldV) == (LONGLONG)OldV)
65 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
66   (DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar,            \
67                                       (LONGLONG)sizeof(DomType) * PtrIncr)
68 #else /* !defined(_WIN64) */
69 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
70   (InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \
71    (LONG)OldV)
72 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
73   (DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar,                  \
74                                     (LONG)sizeof(DomType) * PtrIncr)
75 #endif
76 #else /* !defined(_WIN32) */
77 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
78   __sync_bool_compare_and_swap(Ptr, OldV, NewV)
79 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
80   (DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr)
81 #endif
82 #else /* COMPILER_RT_HAS_ATOMICS != 1 */
83 #include "InstrProfilingUtil.h"
84 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
85   lprofBoolCmpXchg((void **)Ptr, OldV, NewV)
86 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
87   (DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr)
88 #endif
89 
90 #if defined(_WIN32)
91 #define DIR_SEPARATOR '\\'
92 #define DIR_SEPARATOR_2 '/'
93 #else
94 #define DIR_SEPARATOR '/'
95 #endif
96 
97 #ifndef DIR_SEPARATOR_2
98 #define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
99 #else /* DIR_SEPARATOR_2 */
100 #define IS_DIR_SEPARATOR(ch)                                                   \
101   (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
102 #endif /* DIR_SEPARATOR_2 */
103 
104 #if defined(_WIN32)
105 #include <windows.h>
106 static inline size_t getpagesize() {
107   SYSTEM_INFO S;
108   GetNativeSystemInfo(&S);
109   return S.dwPageSize;
110 }
111 #else /* defined(_WIN32) */
112 #include <unistd.h>
113 #endif /* defined(_WIN32) */
114 
115 #define PROF_ERR(Format, ...)                                                  \
116   fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);
117 
118 #define PROF_WARN(Format, ...)                                                 \
119   fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);
120 
121 #define PROF_NOTE(Format, ...)                                                 \
122   fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
123 
124 #ifndef MAP_FILE
125 #define MAP_FILE 0
126 #endif
127 
128 #ifndef O_BINARY
129 #define O_BINARY 0
130 #endif
131 
132 #if defined(__FreeBSD__)
133 
134 #include <inttypes.h>
135 #include <sys/types.h>
136 
137 #else /* defined(__FreeBSD__) */
138 
139 #include <inttypes.h>
140 #include <stdint.h>
141 
142 #endif /* defined(__FreeBSD__) && defined(__i386__) */
143 
144 #endif /* PROFILE_INSTRPROFILING_PORT_H_ */
145