LLVM  mainline
Compiler.h
Go to the documentation of this file.
00001 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines several macros, based on the current compiler.  This allows
00011 // use of compiler-specific features in a way that remains portable.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_COMPILER_H
00016 #define LLVM_SUPPORT_COMPILER_H
00017 
00018 #include "llvm/Config/llvm-config.h"
00019 
00020 #ifndef __has_feature
00021 # define __has_feature(x) 0
00022 #endif
00023 
00024 #ifndef __has_extension
00025 # define __has_extension(x) 0
00026 #endif
00027 
00028 #ifndef __has_attribute
00029 # define __has_attribute(x) 0
00030 #endif
00031 
00032 #ifndef __has_builtin
00033 # define __has_builtin(x) 0
00034 #endif
00035 
00036 /// \macro LLVM_GNUC_PREREQ
00037 /// \brief Extend the default __GNUC_PREREQ even if glibc's features.h isn't
00038 /// available.
00039 #ifndef LLVM_GNUC_PREREQ
00040 # if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
00041 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
00042     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
00043      ((maj) << 20) + ((min) << 10) + (patch))
00044 # elif defined(__GNUC__) && defined(__GNUC_MINOR__)
00045 #  define LLVM_GNUC_PREREQ(maj, min, patch) \
00046     ((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
00047 # else
00048 #  define LLVM_GNUC_PREREQ(maj, min, patch) 0
00049 # endif
00050 #endif
00051 
00052 /// \macro LLVM_MSC_PREREQ
00053 /// \brief Is the compiler MSVC of at least the specified version?
00054 /// The common \param version values to check for are:
00055 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
00056 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
00057 #ifdef _MSC_VER
00058 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
00059 
00060 // We require at least MSVC 2013.
00061 #if !LLVM_MSC_PREREQ(1800)
00062 #error LLVM requires at least MSVC 2013.
00063 #endif
00064 
00065 #else
00066 #define LLVM_MSC_PREREQ(version) 0
00067 #endif
00068 
00069 #if !defined(_MSC_VER) || defined(__clang__) || LLVM_MSC_PREREQ(1900)
00070 #define LLVM_NOEXCEPT noexcept
00071 #else
00072 #define LLVM_NOEXCEPT
00073 #endif
00074 
00075 /// \brief Does the compiler support ref-qualifiers for *this?
00076 ///
00077 /// Sadly, this is separate from just rvalue reference support because GCC
00078 /// and MSVC implemented this later than everything else.
00079 #if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
00080 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
00081 #else
00082 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
00083 #endif
00084 
00085 /// Expands to '&' if ref-qualifiers for *this are supported.
00086 ///
00087 /// This can be used to provide lvalue/rvalue overrides of member functions.
00088 /// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
00089 #if LLVM_HAS_RVALUE_REFERENCE_THIS
00090 #define LLVM_LVALUE_FUNCTION &
00091 #else
00092 #define LLVM_LVALUE_FUNCTION
00093 #endif
00094 
00095 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
00096 # define LLVM_CONSTEXPR constexpr
00097 #else
00098 # define LLVM_CONSTEXPR
00099 #endif
00100 
00101 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
00102 /// into a shared library, then the class should be private to the library and
00103 /// not accessible from outside it.  Can also be used to mark variables and
00104 /// functions, making them private to any shared library they are linked into.
00105 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
00106 #if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) &&              \
00107     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
00108 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
00109 #else
00110 #define LLVM_LIBRARY_VISIBILITY
00111 #endif
00112 
00113 #if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
00114 #define LLVM_END_WITH_NULL __attribute__((sentinel))
00115 #else
00116 #define LLVM_END_WITH_NULL
00117 #endif
00118 
00119 #if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
00120 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
00121 #else
00122 #define LLVM_ATTRIBUTE_USED
00123 #endif
00124 
00125 #if __has_attribute(warn_unused_result) || LLVM_GNUC_PREREQ(3, 4, 0)
00126 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
00127 #else
00128 #define LLVM_ATTRIBUTE_UNUSED_RESULT
00129 #endif
00130 
00131 // Some compilers warn about unused functions. When a function is sometimes
00132 // used or not depending on build settings (e.g. a function only called from
00133 // within "assert"), this attribute can be used to suppress such warnings.
00134 //
00135 // However, it shouldn't be used for unused *variables*, as those have a much
00136 // more portable solution:
00137 //   (void)unused_var_name;
00138 // Prefer cast-to-void wherever it is sufficient.
00139 #if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
00140 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
00141 #else
00142 #define LLVM_ATTRIBUTE_UNUSED
00143 #endif
00144 
00145 // FIXME: Provide this for PE/COFF targets.
00146 #if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) &&                    \
00147     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
00148 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
00149 #else
00150 #define LLVM_ATTRIBUTE_WEAK
00151 #endif
00152 
00153 // Prior to clang 3.2, clang did not accept any spelling of
00154 // __has_attribute(const), so assume it is supported.
00155 #if defined(__clang__) || defined(__GNUC__)
00156 // aka 'CONST' but following LLVM Conventions.
00157 #define LLVM_READNONE __attribute__((__const__))
00158 #else
00159 #define LLVM_READNONE
00160 #endif
00161 
00162 #if __has_attribute(pure) || defined(__GNUC__)
00163 // aka 'PURE' but following LLVM Conventions.
00164 #define LLVM_READONLY __attribute__((__pure__))
00165 #else
00166 #define LLVM_READONLY
00167 #endif
00168 
00169 #if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
00170 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
00171 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
00172 #else
00173 #define LLVM_LIKELY(EXPR) (EXPR)
00174 #define LLVM_UNLIKELY(EXPR) (EXPR)
00175 #endif
00176 
00177 // C++ doesn't support 'extern template' of template specializations.  GCC does,
00178 // but requires __extension__ before it.  In the header, use this:
00179 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
00180 // in the .cpp file, use this:
00181 //   TEMPLATE_INSTANTIATION(class foo<bar>);
00182 #ifdef __GNUC__
00183 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
00184 #define TEMPLATE_INSTANTIATION(X) template X
00185 #else
00186 #define EXTERN_TEMPLATE_INSTANTIATION(X)
00187 #define TEMPLATE_INSTANTIATION(X)
00188 #endif
00189 
00190 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
00191 /// mark a method "not for inlining".
00192 #if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
00193 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
00194 #elif defined(_MSC_VER)
00195 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
00196 #else
00197 #define LLVM_ATTRIBUTE_NOINLINE
00198 #endif
00199 
00200 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
00201 /// so, mark a method "always inline" because it is performance sensitive. GCC
00202 /// 3.4 supported this but is buggy in various cases and produces unimplemented
00203 /// errors, just use it in GCC 4.0 and later.
00204 #if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
00205 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
00206 #elif defined(_MSC_VER)
00207 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
00208 #else
00209 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
00210 #endif
00211 
00212 #ifdef __GNUC__
00213 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
00214 #elif defined(_MSC_VER)
00215 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
00216 #else
00217 #define LLVM_ATTRIBUTE_NORETURN
00218 #endif
00219 
00220 #if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
00221 #define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
00222 #else
00223 #define LLVM_ATTRIBUTE_RETURNS_NONNULL
00224 #endif
00225 
00226 /// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
00227 /// pointer that does not alias any other valid pointer.
00228 #ifdef __GNUC__
00229 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
00230 #elif defined(_MSC_VER)
00231 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
00232 #else
00233 #define LLVM_ATTRIBUTE_RETURNS_NOALIAS
00234 #endif
00235 
00236 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
00237 /// pedantic diagnostics.
00238 #ifdef __GNUC__
00239 #define LLVM_EXTENSION __extension__
00240 #else
00241 #define LLVM_EXTENSION
00242 #endif
00243 
00244 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
00245 #if __has_feature(attribute_deprecated_with_message)
00246 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00247   decl __attribute__((deprecated(message)))
00248 #elif defined(__GNUC__)
00249 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00250   decl __attribute__((deprecated))
00251 #elif defined(_MSC_VER)
00252 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00253   __declspec(deprecated(message)) decl
00254 #else
00255 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
00256   decl
00257 #endif
00258 
00259 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
00260 /// to an expression which states that it is undefined behavior for the
00261 /// compiler to reach this point.  Otherwise is not defined.
00262 #if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
00263 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
00264 #elif defined(_MSC_VER)
00265 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
00266 #endif
00267 
00268 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
00269 /// which causes the program to exit abnormally.
00270 #if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
00271 # define LLVM_BUILTIN_TRAP __builtin_trap()
00272 #elif defined(_MSC_VER)
00273 // The __debugbreak intrinsic is supported by MSVC, does not require forward
00274 // declarations involving platform-specific typedefs (unlike RaiseException),
00275 // results in a call to vectored exception handlers, and encodes to a short
00276 // instruction that still causes the trapping behavior we want.
00277 # define LLVM_BUILTIN_TRAP __debugbreak()
00278 #else
00279 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
00280 #endif
00281 
00282 /// \macro LLVM_ASSUME_ALIGNED
00283 /// \brief Returns a pointer with an assumed alignment.
00284 #if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
00285 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
00286 #elif defined(LLVM_BUILTIN_UNREACHABLE)
00287 // As of today, clang does not support __builtin_assume_aligned.
00288 # define LLVM_ASSUME_ALIGNED(p, a) \
00289            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
00290 #else
00291 # define LLVM_ASSUME_ALIGNED(p, a) (p)
00292 #endif
00293 
00294 /// \macro LLVM_ALIGNAS
00295 /// \brief Used to specify a minimum alignment for a structure or variable. The
00296 /// alignment must be a constant integer. Use LLVM_PTR_SIZE to compute
00297 /// alignments in terms of the size of a pointer.
00298 ///
00299 /// Note that __declspec(align) has special quirks, it's not legal to pass a
00300 /// structure with __declspec(align) as a formal parameter.
00301 #ifdef _MSC_VER
00302 # define LLVM_ALIGNAS(x) __declspec(align(x))
00303 #elif __GNUC__ && !__has_feature(cxx_alignas) && !LLVM_GNUC_PREREQ(4, 8, 0)
00304 # define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
00305 #else
00306 # define LLVM_ALIGNAS(x) alignas(x)
00307 #endif
00308 
00309 /// \macro LLVM_PTR_SIZE
00310 /// \brief A constant integer equivalent to the value of sizeof(void*).
00311 /// Generally used in combination with LLVM_ALIGNAS or when doing computation in
00312 /// the preprocessor.
00313 #ifdef __SIZEOF_POINTER__
00314 # define LLVM_PTR_SIZE __SIZEOF_POINTER__
00315 #elif defined(_WIN64)
00316 # define LLVM_PTR_SIZE 8
00317 #elif defined(_WIN32)
00318 # define LLVM_PTR_SIZE 4
00319 #elif defined(_MSC_VER)
00320 # error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
00321 #else
00322 # define LLVM_PTR_SIZE sizeof(void *)
00323 #endif
00324 
00325 /// \macro LLVM_FUNCTION_NAME
00326 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
00327 /// expands to a compiler-dependent replacement.
00328 #if defined(_MSC_VER)
00329 # define LLVM_FUNCTION_NAME __FUNCTION__
00330 #else
00331 # define LLVM_FUNCTION_NAME __func__
00332 #endif
00333 
00334 /// \macro LLVM_MEMORY_SANITIZER_BUILD
00335 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
00336 #if __has_feature(memory_sanitizer)
00337 # define LLVM_MEMORY_SANITIZER_BUILD 1
00338 # include <sanitizer/msan_interface.h>
00339 #else
00340 # define LLVM_MEMORY_SANITIZER_BUILD 0
00341 # define __msan_allocated_memory(p, size)
00342 # define __msan_unpoison(p, size)
00343 #endif
00344 
00345 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
00346 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
00347 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
00348 # define LLVM_ADDRESS_SANITIZER_BUILD 1
00349 #else
00350 # define LLVM_ADDRESS_SANITIZER_BUILD 0
00351 #endif
00352 
00353 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
00354 /// \brief Is unaligned memory access fast on the host machine.
00355 ///
00356 /// Don't specialize on alignment for platforms where unaligned memory accesses
00357 /// generates the same code as aligned memory accesses for common types.
00358 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
00359     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
00360     defined(_X86_) || defined(__i386) || defined(__i386__)
00361 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
00362 #else
00363 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
00364 #endif
00365 
00366 /// \brief Mark debug helper function definitions like dump() that should not be
00367 /// stripped from debug builds.
00368 // FIXME: Move this to a private config.h as it's not usable in public headers.
00369 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00370 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
00371 #else
00372 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
00373 #endif
00374 
00375 /// \macro LLVM_THREAD_LOCAL
00376 /// \brief A thread-local storage specifier which can be used with globals,
00377 /// extern globals, and static globals.
00378 ///
00379 /// This is essentially an extremely restricted analog to C++11's thread_local
00380 /// support, and uses that when available. However, it falls back on
00381 /// platform-specific or vendor-provided extensions when necessary. These
00382 /// extensions don't support many of the C++11 thread_local's features. You
00383 /// should only use this for PODs that you can statically initialize to
00384 /// some constant value. In almost all circumstances this is most appropriate
00385 /// for use with a pointer, integer, or small aggregation of pointers and
00386 /// integers.
00387 #if LLVM_ENABLE_THREADS
00388 #if __has_feature(cxx_thread_local)
00389 #define LLVM_THREAD_LOCAL thread_local
00390 #elif defined(_MSC_VER)
00391 // MSVC supports this with a __declspec.
00392 #define LLVM_THREAD_LOCAL __declspec(thread)
00393 #else
00394 // Clang, GCC, and other compatible compilers used __thread prior to C++11 and
00395 // we only need the restricted functionality that provides.
00396 #define LLVM_THREAD_LOCAL __thread
00397 #endif
00398 #else // !LLVM_ENABLE_THREADS
00399 // If threading is disabled entirely, this compiles to nothing and you get
00400 // a normal global variable.
00401 #define LLVM_THREAD_LOCAL
00402 #endif
00403 
00404 #endif