1 //===--- AlignOf.h - Portable calculation of type alignment -----*- 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 the AlignOf function that computes alignments for
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_ALIGNOF_H
16 #define LLVM_SUPPORT_ALIGNOF_H
18 #include "llvm/Support/Compiler.h"
20 #include <type_traits>
26 // For everything other than an abstract class we can calulate alignment by
27 // building a class with a single character and a member of the given type.
28 template <typename T, bool = std::is_abstract<T>::value>
29 struct AlignmentCalcImpl {
32 // Disables "structure was padded due to __declspec(align())" warnings that are
33 // generated by any class using AlignOf<T> with a manually specified alignment.
34 // Although the warning is disabled in the LLVM project we need this pragma
35 // as AlignOf.h is a published support header that's available for use
36 // out-of-tree, and we would like that to compile cleanly at /W4.
37 #pragma warning(suppress : 4324)
41 AlignmentCalcImpl() {} // Never instantiate.
44 // Abstract base class helper, this will have the minimal alignment and size
45 // for any abstract class. We don't even define its destructor because this
46 // type should never be used in a way that requires it.
47 struct AlignmentCalcImplBase {
48 virtual ~AlignmentCalcImplBase() = 0;
51 // When we have an abstract class type, specialize the alignment computation
52 // engine to create another abstract class that derives from both an empty
53 // abstract base class and the provided type. This has the same effect as the
54 // above except that it handles the fact that we can't actually create a member
57 struct AlignmentCalcImpl<T, true> : AlignmentCalcImplBase, T {
58 virtual ~AlignmentCalcImpl() = 0;
61 } // End detail namespace.
63 /// AlignOf - A templated class that contains an enum value representing
64 /// the alignment of the template argument. For example,
65 /// AlignOf<int>::Alignment represents the alignment of type "int". The
66 /// alignment calculated is the minimum alignment, and not necessarily
67 /// the "desired" alignment returned by GCC's __alignof__ (for example). Note
68 /// that because the alignment is an enum value, it can be used as a
69 /// compile-time constant (e.g., for template instantiation).
73 // Avoid warnings from GCC like:
74 // comparison between 'enum llvm::AlignOf<X>::<anonymous>' and 'enum
75 // llvm::AlignOf<Y>::<anonymous>' [-Wenum-compare]
76 // by using constexpr instead of enum.
77 // (except on MSVC, since it doesn't support constexpr yet).
78 static constexpr unsigned Alignment = static_cast<unsigned int>(
79 sizeof(detail::AlignmentCalcImpl<T>) - sizeof(T));
82 Alignment = static_cast<unsigned int>(sizeof(detail::AlignmentCalcImpl<T>) -
86 enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
87 enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
88 enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
89 enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
91 enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
92 enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
93 enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
94 enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
98 template <typename T> constexpr unsigned AlignOf<T>::Alignment;
101 /// alignOf - A templated function that returns the minimum alignment of
102 /// of a type. This provides no extra functionality beyond the AlignOf
103 /// class besides some cosmetic cleanliness. Example usage:
104 /// alignOf<int>() returns the alignment of an int.
105 template <typename T>
106 inline unsigned alignOf() { return AlignOf<T>::Alignment; }
108 /// \struct AlignedCharArray
109 /// \brief Helper for building an aligned character array type.
111 /// This template is used to explicitly build up a collection of aligned
112 /// character array types. We have to build these up using a macro and explicit
113 /// specialization to cope with old versions of MSVC and GCC where only an
114 /// integer literal can be used to specify an alignment constraint. Once built
115 /// up here, we can then begin to indirect between these using normal C++
116 /// template parameters.
118 // MSVC requires special handling here.
121 #if __has_feature(cxx_alignas)
122 template<std::size_t Alignment, std::size_t Size>
123 struct AlignedCharArray {
124 alignas(Alignment) char buffer[Size];
127 #elif defined(__GNUC__) || defined(__IBM_ATTRIBUTES)
128 /// \brief Create a type with an aligned char buffer.
129 template<std::size_t Alignment, std::size_t Size>
130 struct AlignedCharArray;
132 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
133 template<std::size_t Size> \
134 struct AlignedCharArray<x, Size> { \
135 __attribute__((aligned(x))) char buffer[Size]; \
138 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1)
139 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2)
140 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4)
141 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8)
142 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
143 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
144 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
145 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
147 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
150 # error No supported align as directive.
155 /// \brief Create a type with an aligned char buffer.
156 template<std::size_t Alignment, std::size_t Size>
157 struct AlignedCharArray;
159 // We provide special variations of this template for the most common
160 // alignments because __declspec(align(...)) doesn't actually work when it is
161 // a member of a by-value function argument in MSVC, even if the alignment
162 // request is something reasonably like 8-byte or 16-byte. Note that we can't
163 // even include the declspec with the union that forces the alignment because
164 // MSVC warns on the existence of the declspec despite the union member forcing
167 template<std::size_t Size>
168 struct AlignedCharArray<1, Size> {
175 template<std::size_t Size>
176 struct AlignedCharArray<2, Size> {
183 template<std::size_t Size>
184 struct AlignedCharArray<4, Size> {
191 template<std::size_t Size>
192 struct AlignedCharArray<8, Size> {
200 // The rest of these are provided with a __declspec(align(...)) and we simply
201 // can't pass them by-value as function arguments on MSVC.
203 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
204 template<std::size_t Size> \
205 struct AlignedCharArray<x, Size> { \
206 __declspec(align(x)) char buffer[Size]; \
209 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
210 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
211 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
212 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
214 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
219 template <typename T1,
220 typename T2 = char, typename T3 = char, typename T4 = char,
221 typename T5 = char, typename T6 = char, typename T7 = char,
222 typename T8 = char, typename T9 = char, typename T10 = char>
224 T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
226 AlignerImpl(); // Never defined or instantiated.
229 template <typename T1,
230 typename T2 = char, typename T3 = char, typename T4 = char,
231 typename T5 = char, typename T6 = char, typename T7 = char,
232 typename T8 = char, typename T9 = char, typename T10 = char>
234 char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
235 arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
236 arr9[sizeof(T9)], arr10[sizeof(T10)];
238 } // end namespace detail
240 /// \brief This union template exposes a suitably aligned and sized character
241 /// array member which can hold elements of any of up to ten types.
243 /// These types may be arrays, structs, or any other types. The goal is to
244 /// expose a char array buffer member which can be used as suitable storage for
245 /// a placement new of any of these types. Support for more than ten types can
246 /// be added at the cost of more boilerplate.
247 template <typename T1,
248 typename T2 = char, typename T3 = char, typename T4 = char,
249 typename T5 = char, typename T6 = char, typename T7 = char,
250 typename T8 = char, typename T9 = char, typename T10 = char>
251 struct AlignedCharArrayUnion : llvm::AlignedCharArray<
252 AlignOf<detail::AlignerImpl<T1, T2, T3, T4, T5,
253 T6, T7, T8, T9, T10> >::Alignment,
254 sizeof(detail::SizerImpl<T1, T2, T3, T4, T5,
255 T6, T7, T8, T9, T10>)> {
257 } // end namespace llvm