xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPort.h (revision 02e9120893770924227138ba49df1edb3896112a)
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 #define COMPILER_RT_USED
27 #elif __GNUC__
28 #ifdef _WIN32
29 #define COMPILER_RT_FTRUNCATE(f, l) _chsize(fileno(f), l)
30 #define COMPILER_RT_VISIBILITY
31 #define COMPILER_RT_WEAK __attribute__((selectany))
32 #else
33 #define COMPILER_RT_FTRUNCATE(f, l) ftruncate(fileno(f), l)
34 #define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
35 #define COMPILER_RT_WEAK __attribute__((weak))
36 #endif
37 #define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))
38 #define COMPILER_RT_ALLOCA __builtin_alloca
39 #define COMPILER_RT_ALWAYS_INLINE inline __attribute((always_inline))
40 #define COMPILER_RT_CLEANUP(x) __attribute__((cleanup(x)))
41 #define COMPILER_RT_USED __attribute__((used))
42 #endif
43 
44 #if defined(__APPLE__)
45 #define COMPILER_RT_SEG "__DATA,"
46 #else
47 #define COMPILER_RT_SEG ""
48 #endif
49 
50 #ifdef _MSC_VER
51 #define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))
52 #else
53 #define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))
54 #endif
55 
56 #define COMPILER_RT_MAX_HOSTLEN 128
57 #ifdef __ORBIS__
58 #define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
59 #else
60 #define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
61 #endif
62 
63 #if COMPILER_RT_HAS_ATOMICS == 1
64 #ifdef _WIN32
65 #include <windows.h>
66 #if defined(_MSC_VER) && _MSC_VER < 1900
67 #define snprintf _snprintf
68 #endif
69 #if defined(_WIN64)
70 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
71   (InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV,      \
72                                 (LONGLONG)OldV) == (LONGLONG)OldV)
73 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
74   (DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar,            \
75                                       (LONGLONG)sizeof(DomType) * PtrIncr)
76 #else /* !defined(_WIN64) */
77 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
78   (InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \
79    (LONG)OldV)
80 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
81   (DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar,                  \
82                                     (LONG)sizeof(DomType) * PtrIncr)
83 #endif
84 #else /* !defined(_WIN32) */
85 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
86   __sync_bool_compare_and_swap(Ptr, OldV, NewV)
87 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
88   (DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr)
89 #endif
90 #else /* COMPILER_RT_HAS_ATOMICS != 1 */
91 #include "InstrProfilingUtil.h"
92 #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
93   lprofBoolCmpXchg((void **)Ptr, OldV, NewV)
94 #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
95   (DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr)
96 #endif
97 
98 #if defined(_WIN32)
99 #define DIR_SEPARATOR '\\'
100 #define DIR_SEPARATOR_2 '/'
101 #else
102 #define DIR_SEPARATOR '/'
103 #endif
104 
105 #ifndef DIR_SEPARATOR_2
106 #define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
107 #else /* DIR_SEPARATOR_2 */
108 #define IS_DIR_SEPARATOR(ch)                                                   \
109   (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
110 #endif /* DIR_SEPARATOR_2 */
111 
112 #if defined(_WIN32)
113 #include <windows.h>
114 static inline size_t getpagesize() {
115   SYSTEM_INFO S;
116   GetNativeSystemInfo(&S);
117   return S.dwPageSize;
118 }
119 #else /* defined(_WIN32) */
120 #include <unistd.h>
121 #endif /* defined(_WIN32) */
122 
123 #define PROF_ERR(Format, ...)                                                  \
124   fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);
125 
126 #define PROF_WARN(Format, ...)                                                 \
127   fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);
128 
129 #define PROF_NOTE(Format, ...)                                                 \
130   fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
131 
132 #ifndef MAP_FILE
133 #define MAP_FILE 0
134 #endif
135 
136 #ifndef O_BINARY
137 #define O_BINARY 0
138 #endif
139 
140 #if defined(__FreeBSD__)
141 
142 #include <inttypes.h>
143 #include <sys/types.h>
144 
145 #else /* defined(__FreeBSD__) */
146 
147 #include <inttypes.h>
148 #include <stdint.h>
149 
150 #endif /* defined(__FreeBSD__) && defined(__i386__) */
151 
152 #endif /* PROFILE_INSTRPROFILING_PORT_H_ */
153