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 ref-qualifiers for *this?
77 /// Sadly, this is separate from just rvalue reference support because GCC
78 /// and MSVC 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 ref-qualifiers for *this are supported.
87 /// This can be used to provide lvalue/rvalue overrides of member functions.
88 /// The rvalue 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 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
227 /// pointer that does not alias any other valid pointer.
229 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
230 #elif defined(_MSC_VER)
231 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
233 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
236 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
237 /// pedantic diagnostics.
239 #define LLVM_EXTENSION __extension__
241 #define LLVM_EXTENSION
244 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
245 #if __has_feature(attribute_deprecated_with_message)
246 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
247 decl __attribute__((deprecated(message)))
248 #elif defined(__GNUC__)
249 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
250 decl __attribute__((deprecated))
251 #elif defined(_MSC_VER)
252 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
253 __declspec(deprecated(message)) decl
255 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
259 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
260 /// to an expression which states that it is undefined behavior for the
261 /// compiler to reach this point. Otherwise is not defined.
262 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
263 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
264 #elif defined(_MSC_VER)
265 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
268 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
269 /// which causes the program to exit abnormally.
270 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
271 # define LLVM_BUILTIN_TRAP __builtin_trap()
272 #elif defined(_MSC_VER)
273 // The __debugbreak intrinsic is supported by MSVC, does not require forward
274 // declarations involving platform-specific typedefs (unlike RaiseException),
275 // results in a call to vectored exception handlers, and encodes to a short
276 // instruction that still causes the trapping behavior we want.
277 # define LLVM_BUILTIN_TRAP __debugbreak()
279 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
282 /// \macro LLVM_ASSUME_ALIGNED
283 /// \brief Returns a pointer with an assumed alignment.
284 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
285 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
286 #elif defined(LLVM_BUILTIN_UNREACHABLE)
287 // As of today, clang does not support __builtin_assume_aligned.
288 # define LLVM_ASSUME_ALIGNED(p, a) \
289 (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
291 # define LLVM_ASSUME_ALIGNED(p, a) (p)
294 /// \macro LLVM_ALIGNAS
295 /// \brief Used to specify a minimum alignment for a structure or variable. The
296 /// alignment must be a constant integer. Use LLVM_PTR_SIZE to compute
297 /// alignments in terms of the size of a pointer.
299 /// Note that __declspec(align) has special quirks, it's not legal to pass a
300 /// structure with __declspec(align) as a formal parameter.
302 # define LLVM_ALIGNAS(x) __declspec(align(x))
303 #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0)
304 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
306 # define LLVM_ALIGNAS(x) alignas(x)
309 /// \macro LLVM_PTR_SIZE
310 /// \brief A constant integer equivalent to the value of sizeof(void*).
311 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
312 /// the preprocessor.
313 #ifdef __SIZEOF_POINTER__
314 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
315 #elif defined(_WIN64)
316 # define LLVM_PTR_SIZE 8
317 #elif defined(_WIN32)
318 # define LLVM_PTR_SIZE 4
319 #elif defined(_MSC_VER)
320 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
322 # define LLVM_PTR_SIZE sizeof(void *)
325 /// \macro LLVM_FUNCTION_NAME
326 /// \brief Expands to __func__ on compilers which support it. Otherwise,
327 /// expands to a compiler-dependent replacement.
328 #if defined(_MSC_VER)
329 # define LLVM_FUNCTION_NAME __FUNCTION__
331 # define LLVM_FUNCTION_NAME __func__
334 /// \macro LLVM_MEMORY_SANITIZER_BUILD
335 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
336 #if __has_feature(memory_sanitizer)
337 # define LLVM_MEMORY_SANITIZER_BUILD 1
338 # include <sanitizer/msan_interface.h>
340 # define LLVM_MEMORY_SANITIZER_BUILD 0
341 # define __msan_allocated_memory(p, size)
342 # define __msan_unpoison(p, size)
345 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
346 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
347 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
348 # define LLVM_ADDRESS_SANITIZER_BUILD 1
350 # define LLVM_ADDRESS_SANITIZER_BUILD 0
353 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
354 /// \brief Is unaligned memory access fast on the host machine.
356 /// Don't specialize on alignment for platforms where unaligned memory accesses
357 /// generates the same code as aligned memory accesses for common types.
358 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
359 defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
360 defined(_X86_) || defined(__i386) || defined(__i386__)
361 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
363 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
366 /// \brief Mark debug helper function definitions like dump() that should not be
367 /// stripped from debug builds.
368 // FIXME: Move this to a private config.h as it's not usable in public headers.
369 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
370 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
372 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
375 /// \macro LLVM_THREAD_LOCAL
376 /// \brief A thread-local storage specifier which can be used with globals,
377 /// extern globals, and static globals.
379 /// This is essentially an extremely restricted analog to C++11's thread_local
380 /// support, and uses that when available. However, it falls back on
381 /// platform-specific or vendor-provided extensions when necessary. These
382 /// extensions don't support many of the C++11 thread_local's features. You
383 /// should only use this for PODs that you can statically initialize to
384 /// some constant value. In almost all circumstances this is most appropriate
385 /// for use with a pointer, integer, or small aggregation of pointers and
387 #if LLVM_ENABLE_THREADS
388 #if __has_feature(cxx_thread_local)
389 #define LLVM_THREAD_LOCAL thread_local
390 #elif defined(_MSC_VER)
391 // MSVC supports this with a __declspec.
392 #define LLVM_THREAD_LOCAL __declspec(thread)
394 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
395 // we only need the restricted functionality that provides.
396 #define LLVM_THREAD_LOCAL __thread
398 #else // !LLVM_ENABLE_THREADS
399 // If threading is disabled entirely, this compiles to nothing and you get
400 // a normal global variable.
401 #define LLVM_THREAD_LOCAL