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"
23 struct AlignmentCalcImpl {
26 // Disables "structure was padded due to __declspec(align())" warnings that are
27 // generated by any class using AlignOf<T> with a manually specified alignment.
28 // Although the warning is disabled in the LLVM project we need this pragma
29 // as AlignOf.h is a published support header that's available for use
30 // out-of-tree, and we would like that to compile cleanly at /W4.
31 #pragma warning(suppress : 4324)
35 AlignmentCalcImpl() {} // Never instantiate.
38 /// AlignOf - A templated class that contains an enum value representing
39 /// the alignment of the template argument. For example,
40 /// AlignOf<int>::Alignment represents the alignment of type "int". The
41 /// alignment calculated is the minimum alignment, and not necessarily
42 /// the "desired" alignment returned by GCC's __alignof__ (for example). Note
43 /// that because the alignment is an enum value, it can be used as a
44 /// compile-time constant (e.g., for template instantiation).
48 // Avoid warnings from GCC like:
49 // comparison between 'enum llvm::AlignOf<X>::<anonymous>' and 'enum
50 // llvm::AlignOf<Y>::<anonymous>' [-Wenum-compare]
51 // by using constexpr instead of enum.
52 // (except on MSVC, since it doesn't support constexpr yet).
53 static constexpr unsigned Alignment =
54 static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T));
57 static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
59 enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
60 enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
61 enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
62 enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
64 enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
65 enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
66 enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
67 enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
71 template <typename T> constexpr unsigned AlignOf<T>::Alignment;
74 /// alignOf - A templated function that returns the minimum alignment of
75 /// of a type. This provides no extra functionality beyond the AlignOf
76 /// class besides some cosmetic cleanliness. Example usage:
77 /// alignOf<int>() returns the alignment of an int.
79 inline unsigned alignOf() { return AlignOf<T>::Alignment; }
81 /// \struct AlignedCharArray
82 /// \brief Helper for building an aligned character array type.
84 /// This template is used to explicitly build up a collection of aligned
85 /// character array types. We have to build these up using a macro and explicit
86 /// specialization to cope with old versions of MSVC and GCC where only an
87 /// integer literal can be used to specify an alignment constraint. Once built
88 /// up here, we can then begin to indirect between these using normal C++
89 /// template parameters.
91 // MSVC requires special handling here.
94 #if __has_feature(cxx_alignas)
95 template<std::size_t Alignment, std::size_t Size>
96 struct AlignedCharArray {
97 alignas(Alignment) char buffer[Size];
100 #elif defined(__GNUC__) || defined(__IBM_ATTRIBUTES)
101 /// \brief Create a type with an aligned char buffer.
102 template<std::size_t Alignment, std::size_t Size>
103 struct AlignedCharArray;
105 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
106 template<std::size_t Size> \
107 struct AlignedCharArray<x, Size> { \
108 __attribute__((aligned(x))) char buffer[Size]; \
111 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1)
112 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2)
113 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4)
114 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8)
115 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
116 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
117 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
118 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
120 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
123 # error No supported align as directive.
128 /// \brief Create a type with an aligned char buffer.
129 template<std::size_t Alignment, std::size_t Size>
130 struct AlignedCharArray;
132 // We provide special variations of this template for the most common
133 // alignments because __declspec(align(...)) doesn't actually work when it is
134 // a member of a by-value function argument in MSVC, even if the alignment
135 // request is something reasonably like 8-byte or 16-byte. Note that we can't
136 // even include the declspec with the union that forces the alignment because
137 // MSVC warns on the existence of the declspec despite the union member forcing
140 template<std::size_t Size>
141 struct AlignedCharArray<1, Size> {
148 template<std::size_t Size>
149 struct AlignedCharArray<2, Size> {
156 template<std::size_t Size>
157 struct AlignedCharArray<4, Size> {
164 template<std::size_t Size>
165 struct AlignedCharArray<8, Size> {
173 // The rest of these are provided with a __declspec(align(...)) and we simply
174 // can't pass them by-value as function arguments on MSVC.
176 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
177 template<std::size_t Size> \
178 struct AlignedCharArray<x, Size> { \
179 __declspec(align(x)) char buffer[Size]; \
182 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
183 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
184 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
185 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
187 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
192 template <typename T1,
193 typename T2 = char, typename T3 = char, typename T4 = char,
194 typename T5 = char, typename T6 = char, typename T7 = char,
195 typename T8 = char, typename T9 = char, typename T10 = char>
197 T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
199 AlignerImpl(); // Never defined or instantiated.
202 template <typename T1,
203 typename T2 = char, typename T3 = char, typename T4 = char,
204 typename T5 = char, typename T6 = char, typename T7 = char,
205 typename T8 = char, typename T9 = char, typename T10 = char>
207 char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
208 arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
209 arr9[sizeof(T9)], arr10[sizeof(T10)];
211 } // end namespace detail
213 /// \brief This union template exposes a suitably aligned and sized character
214 /// array member which can hold elements of any of up to ten types.
216 /// These types may be arrays, structs, or any other types. The goal is to
217 /// expose a char array buffer member which can be used as suitable storage for
218 /// a placement new of any of these types. Support for more than ten types can
219 /// be added at the cost of more boilerplate.
220 template <typename T1,
221 typename T2 = char, typename T3 = char, typename T4 = char,
222 typename T5 = char, typename T6 = char, typename T7 = char,
223 typename T8 = char, typename T9 = char, typename T10 = char>
224 struct AlignedCharArrayUnion : llvm::AlignedCharArray<
225 AlignOf<detail::AlignerImpl<T1, T2, T3, T4, T5,
226 T6, T7, T8, T9, T10> >::Alignment,
227 sizeof(detail::SizerImpl<T1, T2, T3, T4, T5,
228 T6, T7, T8, T9, T10>)> {
230 } // end namespace llvm