1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines several macros, based on the current compiler. This allows
11 // use of compiler-specific features in a way that remains portable.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
18 #include "llvm/Config/llvm-config.h"
21 # define __has_feature(x) 0
24 #ifndef __has_extension
25 # define __has_extension(x) 0
28 #ifndef __has_attribute
29 # define __has_attribute(x) 0
33 # define __has_builtin(x) 0
36 /// \macro LLVM_GNUC_PREREQ
37 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
39 #ifndef LLVM_GNUC_PREREQ
40 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
41 # define LLVM_GNUC_PREREQ(maj, min, patch) \
42 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
43 ((maj) << 20) + ((min) << 10) + (patch))
44 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
45 # define LLVM_GNUC_PREREQ(maj, min, patch) \
46 ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
48 # define LLVM_GNUC_PREREQ(maj, min, patch) 0
52 /// \macro LLVM_MSC_PREREQ
53 /// \brief Is the compiler MSVC of at least the specified version?
54 /// The common \param version values to check for are:
55 /// * 1800: Microsoft Visual Studio 2013 / 12.0
56 /// * 1900: Microsoft Visual Studio 2015 / 14.0
58 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
60 // We require at least MSVC 2013.
61 #if !LLVM_MSC_PREREQ(1800)
62 #error LLVM requires at least MSVC 2013.
66 #define LLVM_MSC_PREREQ(version) 0
69 #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900)
70 #define LLVM_NOEXCEPT noexcept
75 /// \brief Does the compiler support r-value reference *this?
77 /// Sadly, this is separate from just r-value reference support because GCC
78 /// implemented this later than everything else.
79 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
80 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
82 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
85 /// Expands to '&' if r-value references are supported.
87 /// This can be used to provide l-value/r-value overrides of member functions.
88 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
89 #if LLVM_HAS_RVALUE_REFERENCE_THIS
90 #define LLVM_LVALUE_FUNCTION &
92 #define LLVM_LVALUE_FUNCTION
95 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
96 # define LLVM_CONSTEXPR constexpr
98 # define LLVM_CONSTEXPR
101 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
102 /// into a shared library, then the class should be private to the library and
103 /// not accessible from outside it. Can also be used to mark variables and
104 /// functions, making them private to any shared library they are linked into.
105 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
106 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
107 !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
108 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
110 #define LLVM_LIBRARY_VISIBILITY
113 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
114 #define LLVM_END_WITH_NULL __attribute__((sentinel))
116 #define LLVM_END_WITH_NULL
119 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
120 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
122 #define LLVM_ATTRIBUTE_USED
125 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
126 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
128 #define LLVM_ATTRIBUTE_UNUSED_RESULT
131 // Some compilers warn about unused functions. When a function is sometimes
132 // used or not depending on build settings (e.g. a function only called from
133 // within "assert"), this attribute can be used to suppress such warnings.
135 // However, it shouldn't be used for unused *variables*, as those have a much
136 // more portable solution:
137 // (void)unused_var_name;
138 // Prefer cast-to-void wherever it is sufficient.
139 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
140 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
142 #define LLVM_ATTRIBUTE_UNUSED
145 // FIXME: Provide this for PE/COFF targets.
146 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
147 (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
148 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
150 #define LLVM_ATTRIBUTE_WEAK
153 // Prior to clang 3.2, clang did not accept any spelling of
154 // __has_attribute(const), so assume it is supported.
155 #if defined(__clang__) || defined(__GNUC__)
156 // aka 'CONST' but following LLVM Conventions.
157 #define LLVM_READNONE __attribute__((__const__))
159 #define LLVM_READNONE
162 #if __has_attribute(pure) || defined(__GNUC__)
163 // aka 'PURE' but following LLVM Conventions.
164 #define LLVM_READONLY __attribute__((__pure__))
166 #define LLVM_READONLY
169 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
170 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
171 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
173 #define LLVM_LIKELY(EXPR) (EXPR)
174 #define LLVM_UNLIKELY(EXPR) (EXPR)
177 // C++ doesn't support 'extern template' of template specializations. GCC does,
178 // but requires __extension__ before it. In the header, use this:
179 // EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
180 // in the .cpp file, use this:
181 // TEMPLATE_INSTANTIATION(class foo<bar>);
183 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
184 #define TEMPLATE_INSTANTIATION(X) template X
186 #define EXTERN_TEMPLATE_INSTANTIATION(X)
187 #define TEMPLATE_INSTANTIATION(X)
190 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
191 /// mark a method "not for inlining".
192 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
193 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
194 #elif defined(_MSC_VER)
195 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
197 #define LLVM_ATTRIBUTE_NOINLINE
200 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
201 /// so, mark a method "always inline" because it is performance sensitive. GCC
202 /// 3.4 supported this but is buggy in various cases and produces unimplemented
203 /// errors, just use it in GCC 4.0 and later.
204 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
205 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
206 #elif defined(_MSC_VER)
207 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
209 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
213 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
214 #elif defined(_MSC_VER)
215 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
217 #define LLVM_ATTRIBUTE_NORETURN
220 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
221 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
223 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
226 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
227 /// pedantic diagnostics.
229 #define LLVM_EXTENSION __extension__
231 #define LLVM_EXTENSION
234 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
235 #if __has_feature(attribute_deprecated_with_message)
236 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
237 decl __attribute__((deprecated(message)))
238 #elif defined(__GNUC__)
239 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
240 decl __attribute__((deprecated))
241 #elif defined(_MSC_VER)
242 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
243 __declspec(deprecated(message)) decl
245 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
249 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
250 /// to an expression which states that it is undefined behavior for the
251 /// compiler to reach this point. Otherwise is not defined.
252 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
253 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
254 #elif defined(_MSC_VER)
255 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
258 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
259 /// which causes the program to exit abnormally.
260 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
261 # define LLVM_BUILTIN_TRAP __builtin_trap()
262 #elif defined(_MSC_VER)
263 // The __debugbreak intrinsic is supported by MSVC, does not require forward
264 // declarations involving platform-specific typedefs (unlike RaiseException),
265 // results in a call to vectored exception handlers, and encodes to a short
266 // instruction that still causes the trapping behavior we want.
267 # define LLVM_BUILTIN_TRAP __debugbreak()
269 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
272 /// \macro LLVM_ASSUME_ALIGNED
273 /// \brief Returns a pointer with an assumed alignment.
274 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
275 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
276 #elif defined(LLVM_BUILTIN_UNREACHABLE)
277 // As of today, clang does not support __builtin_assume_aligned.
278 # define LLVM_ASSUME_ALIGNED(p, a) \
279 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
281 # define LLVM_ASSUME_ALIGNED(p, a) (p)
284 /// \macro LLVM_ALIGNAS
285 /// \brief Used to specify a minimum alignment for a structure or variable. The
286 /// alignment must be a constant integer. Use LLVM_PTR_SIZE to compute
287 /// alignments in terms of the size of a pointer.
289 /// Note that __declspec(align) has special quirks, it's not legal to pass a
290 /// structure with __declspec(align) as a formal parameter.
292 # define LLVM_ALIGNAS(x) __declspec(align(x))
293 #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0)
294 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
296 # define LLVM_ALIGNAS(x) alignas(x)
299 /// \macro LLVM_PTR_SIZE
300 /// \brief A constant integer equivalent to the value of sizeof(void*).
301 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
302 /// the preprocessor.
303 #ifdef __SIZEOF_POINTER__
304 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
305 #elif defined(_WIN64)
306 # define LLVM_PTR_SIZE 8
307 #elif defined(_WIN32)
308 # define LLVM_PTR_SIZE 4
309 #elif defined(_MSC_VER)
310 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
312 # define LLVM_PTR_SIZE sizeof(void *)
315 /// \macro LLVM_FUNCTION_NAME
316 /// \brief Expands to __func__ on compilers which support it. Otherwise,
317 /// expands to a compiler-dependent replacement.
318 #if defined(_MSC_VER)
319 # define LLVM_FUNCTION_NAME __FUNCTION__
321 # define LLVM_FUNCTION_NAME __func__
324 /// \macro LLVM_MEMORY_SANITIZER_BUILD
325 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
326 #if __has_feature(memory_sanitizer)
327 # define LLVM_MEMORY_SANITIZER_BUILD 1
328 # include <sanitizer/msan_interface.h>
330 # define LLVM_MEMORY_SANITIZER_BUILD 0
331 # define __msan_allocated_memory(p, size)
332 # define __msan_unpoison(p, size)
335 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
336 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
337 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
338 # define LLVM_ADDRESS_SANITIZER_BUILD 1
340 # define LLVM_ADDRESS_SANITIZER_BUILD 0
343 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
344 /// \brief Is unaligned memory access fast on the host machine.
346 /// Don't specialize on alignment for platforms where unaligned memory accesses
347 /// generates the same code as aligned memory accesses for common types.
348 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
349 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
350 defined(_X86_) || defined(__i386) || defined(__i386__)
351 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
353 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
356 /// \brief Mark debug helper function definitions like dump() that should not be
357 /// stripped from debug builds.
358 // FIXME: Move this to a private config.h as it's not usable in public headers.
359 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
360 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
362 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
365 /// \macro LLVM_THREAD_LOCAL
366 /// \brief A thread-local storage specifier which can be used with globals,
367 /// extern globals, and static globals.
369 /// This is essentially an extremely restricted analog to C++11's thread_local
370 /// support, and uses that when available. However, it falls back on
371 /// platform-specific or vendor-provided extensions when necessary. These
372 /// extensions don't support many of the C++11 thread_local's features. You
373 /// should only use this for PODs that you can statically initialize to
374 /// some constant value. In almost all circumstances this is most appropriate
375 /// for use with a pointer, integer, or small aggregation of pointers and
377 #if LLVM_ENABLE_THREADS
378 #if __has_feature(cxx_thread_local)
379 #define LLVM_THREAD_LOCAL thread_local
380 #elif defined(_MSC_VER)
381 // MSVC supports this with a __declspec.
382 #define LLVM_THREAD_LOCAL __declspec(thread)
384 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
385 // we only need the restricted functionality that provides.
386 #define LLVM_THREAD_LOCAL __thread
388 #else // !LLVM_ENABLE_THREADS
389 // If threading is disabled entirely, this compiles to nothing and you get
390 // a normal global variable.
391 #define LLVM_THREAD_LOCAL