f30467d5440ded5ebbdd98c1cb805fed471c1d14
[oota-llvm.git] / include / llvm / Support / AlignOf.h
1 //===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the AlignOf function that computes alignments for
11 // arbitrary types.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_ALIGNOF_H
16 #define LLVM_SUPPORT_ALIGNOF_H
17
18 #include "llvm/Support/Compiler.h"
19 #include <cstddef>
20
21 namespace llvm {
22 template <typename T>
23 struct AlignmentCalcImpl {
24   char x;
25   T t;
26 private:
27   AlignmentCalcImpl() {} // Never instantiate.
28 };
29
30 /// AlignOf - A templated class that contains an enum value representing
31 ///  the alignment of the template argument.  For example,
32 ///  AlignOf<int>::Alignment represents the alignment of type "int".  The
33 ///  alignment calculated is the minimum alignment, and not necessarily
34 ///  the "desired" alignment returned by GCC's __alignof__ (for example).  Note
35 ///  that because the alignment is an enum value, it can be used as a
36 ///  compile-time constant (e.g., for template instantiation).
37 template <typename T>
38 struct AlignOf {
39   enum { Alignment =
40          static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
41
42   enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
43   enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
44   enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
45   enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
46
47   enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
48   enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
49   enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
50   enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
51 };
52
53 /// alignOf - A templated function that returns the minimum alignment of
54 ///  of a type.  This provides no extra functionality beyond the AlignOf
55 ///  class besides some cosmetic cleanliness.  Example usage:
56 ///  alignOf<int>() returns the alignment of an int.
57 template <typename T>
58 inline unsigned alignOf() { return AlignOf<T>::Alignment; }
59
60 /// \struct AlignedCharArray
61 /// \brief Helper for building an aligned character array type.
62 ///
63 /// This template is used to explicitly build up a collection of aligned
64 /// character array types. We have to build these up using a macro and explicit
65 /// specialization to cope with old versions of MSVC and GCC where only an
66 /// integer literal can be used to specify an alignment constraint. Once built
67 /// up here, we can then begin to indirect between these using normal C++
68 /// template parameters.
69
70 // MSVC requires special handling here.
71 #ifndef _MSC_VER
72
73 template<std::size_t Alignment, std::size_t Size>
74 struct AlignedCharArray {
75   LLVM_ALIGNAS(Alignment) char buffer[Size];
76 };
77
78 #else // _MSC_VER
79
80 /// \brief Create a type with an aligned char buffer.
81 template<std::size_t Alignment, std::size_t Size>
82 struct AlignedCharArray;
83
84 // We provide special variations of this template for the most common
85 // alignments because __declspec(align(...)) doesn't actually work when it is
86 // a member of a by-value function argument in MSVC, even if the alignment
87 // request is something reasonably like 8-byte or 16-byte. Note that we can't
88 // even include the declspec with the union that forces the alignment because
89 // MSVC warns on the existence of the declspec despite the union member forcing
90 // proper alignment.
91
92 template<std::size_t Size>
93 struct AlignedCharArray<1, Size> {
94   union {
95     char aligned;
96     char buffer[Size];
97   };
98 };
99
100 template<std::size_t Size>
101 struct AlignedCharArray<2, Size> {
102   union {
103     short aligned;
104     char buffer[Size];
105   };
106 };
107
108 template<std::size_t Size>
109 struct AlignedCharArray<4, Size> {
110   union {
111     int aligned;
112     char buffer[Size];
113   };
114 };
115
116 template<std::size_t Size>
117 struct AlignedCharArray<8, Size> {
118   union {
119     double aligned;
120     char buffer[Size];
121   };
122 };
123
124
125 // The rest of these are provided with a __declspec(align(...)) and we simply
126 // can't pass them by-value as function arguments on MSVC.
127
128 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
129   template<std::size_t Size> \
130   struct AlignedCharArray<x, Size> { \
131     __declspec(align(x)) char buffer[Size]; \
132   };
133
134 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
135 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
136 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
137 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
138
139 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
140
141 #endif // _MSC_VER
142
143 namespace detail {
144 template <typename T1,
145           typename T2 = char, typename T3 = char, typename T4 = char,
146           typename T5 = char, typename T6 = char, typename T7 = char,
147           typename T8 = char, typename T9 = char, typename T10 = char>
148 class AlignerImpl {
149   T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
150
151   AlignerImpl(); // Never defined or instantiated.
152 };
153
154 template <typename T1,
155           typename T2 = char, typename T3 = char, typename T4 = char,
156           typename T5 = char, typename T6 = char, typename T7 = char,
157           typename T8 = char, typename T9 = char, typename T10 = char>
158 union SizerImpl {
159   char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
160        arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
161        arr9[sizeof(T9)], arr10[sizeof(T10)];
162 };
163 } // end namespace detail
164
165 /// \brief This union template exposes a suitably aligned and sized character
166 /// array member which can hold elements of any of up to four types.
167 ///
168 /// These types may be arrays, structs, or any other types. The goal is to
169 /// expose a char array buffer member which can be used as suitable storage for
170 /// a placement new of any of these types. Support for more than seven types can
171 /// be added at the cost of more boiler plate.
172 template <typename T1,
173           typename T2 = char, typename T3 = char, typename T4 = char,
174           typename T5 = char, typename T6 = char, typename T7 = char,
175           typename T8 = char, typename T9 = char, typename T10 = char>
176 struct AlignedCharArrayUnion : llvm::AlignedCharArray<
177     AlignOf<detail::AlignerImpl<T1, T2, T3, T4, T5,
178                                 T6, T7, T8, T9, T10> >::Alignment,
179     sizeof(detail::SizerImpl<T1, T2, T3, T4, T5,
180                              T6, T7, T8, T9, T10>)> {
181 };
182 } // end namespace llvm
183 #endif