/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGExprConstant.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This contains code to emit Constant Expr nodes as LLVM code. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "CGCXXABI.h" |
14 | | #include "CGObjCRuntime.h" |
15 | | #include "CGRecordLayout.h" |
16 | | #include "CodeGenFunction.h" |
17 | | #include "CodeGenModule.h" |
18 | | #include "ConstantEmitter.h" |
19 | | #include "TargetInfo.h" |
20 | | #include "clang/AST/APValue.h" |
21 | | #include "clang/AST/ASTContext.h" |
22 | | #include "clang/AST/Attr.h" |
23 | | #include "clang/AST/RecordLayout.h" |
24 | | #include "clang/AST/StmtVisitor.h" |
25 | | #include "clang/Basic/Builtins.h" |
26 | | #include "llvm/ADT/STLExtras.h" |
27 | | #include "llvm/ADT/Sequence.h" |
28 | | #include "llvm/IR/Constants.h" |
29 | | #include "llvm/IR/DataLayout.h" |
30 | | #include "llvm/IR/Function.h" |
31 | | #include "llvm/IR/GlobalVariable.h" |
32 | | using namespace clang; |
33 | | using namespace CodeGen; |
34 | | |
35 | | //===----------------------------------------------------------------------===// |
36 | | // ConstantAggregateBuilder |
37 | | //===----------------------------------------------------------------------===// |
38 | | |
39 | | namespace { |
40 | | class ConstExprEmitter; |
41 | | |
42 | | struct ConstantAggregateBuilderUtils { |
43 | | CodeGenModule &CGM; |
44 | | |
45 | 11.5k | ConstantAggregateBuilderUtils(CodeGenModule &CGM) : CGM(CGM) {} |
46 | | |
47 | 23.1k | CharUnits getAlignment(const llvm::Constant *C) const { |
48 | 23.1k | return CharUnits::fromQuantity( |
49 | 23.1k | CGM.getDataLayout().getABITypeAlignment(C->getType())); |
50 | 23.1k | } |
51 | | |
52 | 17.5k | CharUnits getSize(llvm::Type *Ty) const { |
53 | 17.5k | return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(Ty)); |
54 | 17.5k | } |
55 | | |
56 | 11.8k | CharUnits getSize(const llvm::Constant *C) const { |
57 | 11.8k | return getSize(C->getType()); |
58 | 11.8k | } |
59 | | |
60 | 434 | llvm::Constant *getPadding(CharUnits PadSize) const { |
61 | 434 | llvm::Type *Ty = CGM.Int8Ty; |
62 | 434 | if (PadSize > CharUnits::One()) |
63 | 397 | Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity()); |
64 | 434 | return llvm::UndefValue::get(Ty); |
65 | 434 | } |
66 | | |
67 | 8 | llvm::Constant *getZeroes(CharUnits ZeroSize) const { |
68 | 8 | llvm::Type *Ty = llvm::ArrayType::get(CGM.Int8Ty, ZeroSize.getQuantity()); |
69 | 8 | return llvm::ConstantAggregateZero::get(Ty); |
70 | 8 | } |
71 | | }; |
72 | | |
73 | | /// Incremental builder for an llvm::Constant* holding a struct or array |
74 | | /// constant. |
75 | | class ConstantAggregateBuilder : private ConstantAggregateBuilderUtils { |
76 | | /// The elements of the constant. These two arrays must have the same size; |
77 | | /// Offsets[i] describes the offset of Elems[i] within the constant. The |
78 | | /// elements are kept in increasing offset order, and we ensure that there |
79 | | /// is no overlap: Offsets[i+1] >= Offsets[i] + getSize(Elemes[i]). |
80 | | /// |
81 | | /// This may contain explicit padding elements (in order to create a |
82 | | /// natural layout), but need not. Gaps between elements are implicitly |
83 | | /// considered to be filled with undef. |
84 | | llvm::SmallVector<llvm::Constant*, 32> Elems; |
85 | | llvm::SmallVector<CharUnits, 32> Offsets; |
86 | | |
87 | | /// The size of the constant (the maximum end offset of any added element). |
88 | | /// May be larger than the end of Elems.back() if we split the last element |
89 | | /// and removed some trailing undefs. |
90 | | CharUnits Size = CharUnits::Zero(); |
91 | | |
92 | | /// This is true only if laying out Elems in order as the elements of a |
93 | | /// non-packed LLVM struct will give the correct layout. |
94 | | bool NaturalLayout = true; |
95 | | |
96 | | bool split(size_t Index, CharUnits Hint); |
97 | | Optional<size_t> splitAt(CharUnits Pos); |
98 | | |
99 | | static llvm::Constant *buildFrom(CodeGenModule &CGM, |
100 | | ArrayRef<llvm::Constant *> Elems, |
101 | | ArrayRef<CharUnits> Offsets, |
102 | | CharUnits StartOffset, CharUnits Size, |
103 | | bool NaturalLayout, llvm::Type *DesiredTy, |
104 | | bool AllowOversized); |
105 | | |
106 | | public: |
107 | | ConstantAggregateBuilder(CodeGenModule &CGM) |
108 | 5.80k | : ConstantAggregateBuilderUtils(CGM) {} |
109 | | |
110 | | /// Update or overwrite the value starting at \p Offset with \c C. |
111 | | /// |
112 | | /// \param AllowOverwrite If \c true, this constant might overwrite (part of) |
113 | | /// a constant that has already been added. This flag is only used to |
114 | | /// detect bugs. |
115 | | bool add(llvm::Constant *C, CharUnits Offset, bool AllowOverwrite); |
116 | | |
117 | | /// Update or overwrite the bits starting at \p OffsetInBits with \p Bits. |
118 | | bool addBits(llvm::APInt Bits, uint64_t OffsetInBits, bool AllowOverwrite); |
119 | | |
120 | | /// Attempt to condense the value starting at \p Offset to a constant of type |
121 | | /// \p DesiredTy. |
122 | | void condense(CharUnits Offset, llvm::Type *DesiredTy); |
123 | | |
124 | | /// Produce a constant representing the entire accumulated value, ideally of |
125 | | /// the specified type. If \p AllowOversized, the constant might be larger |
126 | | /// than implied by \p DesiredTy (eg, if there is a flexible array member). |
127 | | /// Otherwise, the constant will be of exactly the same size as \p DesiredTy |
128 | | /// even if we can't represent it as that type. |
129 | 5.76k | llvm::Constant *build(llvm::Type *DesiredTy, bool AllowOversized) const { |
130 | 5.76k | return buildFrom(CGM, Elems, Offsets, CharUnits::Zero(), Size, |
131 | 5.76k | NaturalLayout, DesiredTy, AllowOversized); |
132 | 5.76k | } |
133 | | }; |
134 | | |
135 | | template<typename Container, typename Range = std::initializer_list< |
136 | | typename Container::value_type>> |
137 | 274 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { |
138 | 274 | assert(BeginOff <= EndOff && "invalid replacement range"); |
139 | 274 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); |
140 | 274 | } CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<llvm::Constant*, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_0, llvm::Constant*> > >(llvm::SmallVector<llvm::Constant*, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_0, llvm::Constant*> >) Line | Count | Source | 137 | 34 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { | 138 | 34 | assert(BeginOff <= EndOff && "invalid replacement range"); | 139 | 34 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); | 140 | 34 | } |
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_1, clang::CharUnits> > >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_1, clang::CharUnits> >) Line | Count | Source | 137 | 2 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { | 138 | 2 | assert(BeginOff <= EndOff && "invalid replacement range"); | 139 | 2 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); | 140 | 2 | } |
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_2, clang::CharUnits> > >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_2, clang::CharUnits> >) Line | Count | Source | 137 | 32 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { | 138 | 32 | assert(BeginOff <= EndOff && "invalid replacement range"); | 139 | 32 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); | 140 | 32 | } |
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<llvm::Constant*, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_3, llvm::Constant*> > >(llvm::SmallVector<llvm::Constant*, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_3, llvm::Constant*> >) Line | Count | Source | 137 | 15 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { | 138 | 15 | assert(BeginOff <= EndOff && "invalid replacement range"); | 139 | 15 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); | 140 | 15 | } |
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_4, clang::CharUnits> > >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, llvm::iterator_range<llvm::mapped_iterator<llvm::detail::value_sequence_iterator<unsigned int>, (anonymous namespace)::ConstantAggregateBuilder::split(unsigned long, clang::CharUnits)::$_4, clang::CharUnits> >) Line | Count | Source | 137 | 15 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { | 138 | 15 | assert(BeginOff <= EndOff && "invalid replacement range"); | 139 | 15 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); | 140 | 15 | } |
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<llvm::Constant*, 32u>, std::initializer_list<llvm::Constant*> >(llvm::SmallVector<llvm::Constant*, 32u>&, unsigned long, unsigned long, std::initializer_list<llvm::Constant*>) Line | Count | Source | 137 | 88 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { | 138 | 88 | assert(BeginOff <= EndOff && "invalid replacement range"); | 139 | 88 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); | 140 | 88 | } |
CGExprConstant.cpp:void (anonymous namespace)::replace<llvm::SmallVector<clang::CharUnits, 32u>, std::initializer_list<clang::CharUnits> >(llvm::SmallVector<clang::CharUnits, 32u>&, unsigned long, unsigned long, std::initializer_list<clang::CharUnits>) Line | Count | Source | 137 | 88 | static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) { | 138 | 88 | assert(BeginOff <= EndOff && "invalid replacement range"); | 139 | 88 | llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals); | 140 | 88 | } |
|
141 | | |
142 | | bool ConstantAggregateBuilder::add(llvm::Constant *C, CharUnits Offset, |
143 | 11.4k | bool AllowOverwrite) { |
144 | | // Common case: appending to a layout. |
145 | 11.4k | if (Offset >= Size) { |
146 | 11.3k | CharUnits Align = getAlignment(C); |
147 | 11.3k | CharUnits AlignedSize = Size.alignTo(Align); |
148 | 11.3k | if (AlignedSize > Offset || Offset.alignTo(Align) != Offset11.2k ) |
149 | 104 | NaturalLayout = false; |
150 | 11.2k | else if (AlignedSize < Offset) { |
151 | 56 | Elems.push_back(getPadding(Offset - Size)); |
152 | 56 | Offsets.push_back(Size); |
153 | 56 | } |
154 | 11.3k | Elems.push_back(C); |
155 | 11.3k | Offsets.push_back(Offset); |
156 | 11.3k | Size = Offset + getSize(C); |
157 | 11.3k | return true; |
158 | 11.3k | } |
159 | | |
160 | | // Uncommon case: constant overlaps what we've already created. |
161 | 70 | llvm::Optional<size_t> FirstElemToReplace = splitAt(Offset); |
162 | 70 | if (!FirstElemToReplace) |
163 | 0 | return false; |
164 | | |
165 | 70 | CharUnits CSize = getSize(C); |
166 | 70 | llvm::Optional<size_t> LastElemToReplace = splitAt(Offset + CSize); |
167 | 70 | if (!LastElemToReplace) |
168 | 0 | return false; |
169 | | |
170 | 70 | assert((FirstElemToReplace == LastElemToReplace || AllowOverwrite) && |
171 | 70 | "unexpectedly overwriting field"); |
172 | | |
173 | 70 | replace(Elems, *FirstElemToReplace, *LastElemToReplace, {C}); |
174 | 70 | replace(Offsets, *FirstElemToReplace, *LastElemToReplace, {Offset}); |
175 | 70 | Size = std::max(Size, Offset + CSize); |
176 | 70 | NaturalLayout = false; |
177 | 70 | return true; |
178 | 70 | } |
179 | | |
180 | | bool ConstantAggregateBuilder::addBits(llvm::APInt Bits, uint64_t OffsetInBits, |
181 | 216 | bool AllowOverwrite) { |
182 | 216 | const ASTContext &Context = CGM.getContext(); |
183 | 216 | const uint64_t CharWidth = CGM.getContext().getCharWidth(); |
184 | | |
185 | | // Offset of where we want the first bit to go within the bits of the |
186 | | // current char. |
187 | 216 | unsigned OffsetWithinChar = OffsetInBits % CharWidth; |
188 | | |
189 | | // We split bit-fields up into individual bytes. Walk over the bytes and |
190 | | // update them. |
191 | 216 | for (CharUnits OffsetInChars = |
192 | 216 | Context.toCharUnitsFromBits(OffsetInBits - OffsetWithinChar); |
193 | 422 | /**/; ++OffsetInChars206 ) { |
194 | | // Number of bits we want to fill in this char. |
195 | 422 | unsigned WantedBits = |
196 | 422 | std::min((uint64_t)Bits.getBitWidth(), CharWidth - OffsetWithinChar); |
197 | | |
198 | | // Get a char containing the bits we want in the right places. The other |
199 | | // bits have unspecified values. |
200 | 422 | llvm::APInt BitsThisChar = Bits; |
201 | 422 | if (BitsThisChar.getBitWidth() < CharWidth) |
202 | 189 | BitsThisChar = BitsThisChar.zext(CharWidth); |
203 | 422 | if (CGM.getDataLayout().isBigEndian()) { |
204 | | // Figure out how much to shift by. We may need to left-shift if we have |
205 | | // less than one byte of Bits left. |
206 | 10 | int Shift = Bits.getBitWidth() - CharWidth + OffsetWithinChar; |
207 | 10 | if (Shift > 0) |
208 | 8 | BitsThisChar.lshrInPlace(Shift); |
209 | 2 | else if (Shift < 0) |
210 | 1 | BitsThisChar = BitsThisChar.shl(-Shift); |
211 | 412 | } else { |
212 | 412 | BitsThisChar = BitsThisChar.shl(OffsetWithinChar); |
213 | 412 | } |
214 | 422 | if (BitsThisChar.getBitWidth() > CharWidth) |
215 | 203 | BitsThisChar = BitsThisChar.trunc(CharWidth); |
216 | | |
217 | 422 | if (WantedBits == CharWidth) { |
218 | | // Got a full byte: just add it directly. |
219 | 219 | add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar), |
220 | 219 | OffsetInChars, AllowOverwrite); |
221 | 203 | } else { |
222 | | // Partial byte: update the existing integer if there is one. If we |
223 | | // can't split out a 1-CharUnit range to update, then we can't add |
224 | | // these bits and fail the entire constant emission. |
225 | 203 | llvm::Optional<size_t> FirstElemToUpdate = splitAt(OffsetInChars); |
226 | 203 | if (!FirstElemToUpdate) |
227 | 0 | return false; |
228 | 203 | llvm::Optional<size_t> LastElemToUpdate = |
229 | 203 | splitAt(OffsetInChars + CharUnits::One()); |
230 | 203 | if (!LastElemToUpdate) |
231 | 0 | return false; |
232 | 203 | assert(*LastElemToUpdate - *FirstElemToUpdate < 2 && |
233 | 203 | "should have at most one element covering one byte"); |
234 | | |
235 | | // Figure out which bits we want and discard the rest. |
236 | 203 | llvm::APInt UpdateMask(CharWidth, 0); |
237 | 203 | if (CGM.getDataLayout().isBigEndian()) |
238 | 1 | UpdateMask.setBits(CharWidth - OffsetWithinChar - WantedBits, |
239 | 1 | CharWidth - OffsetWithinChar); |
240 | 202 | else |
241 | 202 | UpdateMask.setBits(OffsetWithinChar, OffsetWithinChar + WantedBits); |
242 | 203 | BitsThisChar &= UpdateMask; |
243 | | |
244 | 203 | if (*FirstElemToUpdate == *LastElemToUpdate || |
245 | 58 | Elems[*FirstElemToUpdate]->isNullValue() || |
246 | 166 | isa<llvm::UndefValue>(Elems[*FirstElemToUpdate])39 ) { |
247 | | // All existing bits are either zero or undef. |
248 | 166 | add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar), |
249 | 166 | OffsetInChars, /*AllowOverwrite*/ true); |
250 | 37 | } else { |
251 | 37 | llvm::Constant *&ToUpdate = Elems[*FirstElemToUpdate]; |
252 | | // In order to perform a partial update, we need the existing bitwise |
253 | | // value, which we can only extract for a constant int. |
254 | 37 | auto *CI = dyn_cast<llvm::ConstantInt>(ToUpdate); |
255 | 37 | if (!CI) |
256 | 0 | return false; |
257 | | // Because this is a 1-CharUnit range, the constant occupying it must |
258 | | // be exactly one CharUnit wide. |
259 | 37 | assert(CI->getBitWidth() == CharWidth && "splitAt failed"); |
260 | 37 | assert((!(CI->getValue() & UpdateMask) || AllowOverwrite) && |
261 | 37 | "unexpectedly overwriting bitfield"); |
262 | 37 | BitsThisChar |= (CI->getValue() & ~UpdateMask); |
263 | 37 | ToUpdate = llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar); |
264 | 37 | } |
265 | 203 | } |
266 | | |
267 | | // Stop if we've added all the bits. |
268 | 422 | if (WantedBits == Bits.getBitWidth()) |
269 | 216 | break; |
270 | | |
271 | | // Remove the consumed bits from Bits. |
272 | 206 | if (!CGM.getDataLayout().isBigEndian()) |
273 | 198 | Bits.lshrInPlace(WantedBits); |
274 | 206 | Bits = Bits.trunc(Bits.getBitWidth() - WantedBits); |
275 | | |
276 | | // The remanining bits go at the start of the following bytes. |
277 | 206 | OffsetWithinChar = 0; |
278 | 206 | } |
279 | | |
280 | 216 | return true; |
281 | 216 | } |
282 | | |
283 | | /// Returns a position within Elems and Offsets such that all elements |
284 | | /// before the returned index end before Pos and all elements at or after |
285 | | /// the returned index begin at or after Pos. Splits elements as necessary |
286 | | /// to ensure this. Returns None if we find something we can't split. |
287 | 582 | Optional<size_t> ConstantAggregateBuilder::splitAt(CharUnits Pos) { |
288 | 582 | if (Pos >= Size) |
289 | 391 | return Offsets.size(); |
290 | | |
291 | 244 | while (191 true) { |
292 | 244 | auto FirstAfterPos = llvm::upper_bound(Offsets, Pos); |
293 | 244 | if (FirstAfterPos == Offsets.begin()) |
294 | 0 | return 0; |
295 | | |
296 | | // If we already have an element starting at Pos, we're done. |
297 | 244 | size_t LastAtOrBeforePosIndex = FirstAfterPos - Offsets.begin() - 1; |
298 | 244 | if (Offsets[LastAtOrBeforePosIndex] == Pos) |
299 | 187 | return LastAtOrBeforePosIndex; |
300 | | |
301 | | // We found an element starting before Pos. Check for overlap. |
302 | 57 | if (Offsets[LastAtOrBeforePosIndex] + |
303 | 57 | getSize(Elems[LastAtOrBeforePosIndex]) <= Pos) |
304 | 4 | return LastAtOrBeforePosIndex + 1; |
305 | | |
306 | | // Try to decompose it into smaller constants. |
307 | 53 | if (!split(LastAtOrBeforePosIndex, Pos)) |
308 | 0 | return None; |
309 | 53 | } |
310 | 191 | } |
311 | | |
312 | | /// Split the constant at index Index, if possible. Return true if we did. |
313 | | /// Hint indicates the location at which we'd like to split, but may be |
314 | | /// ignored. |
315 | 53 | bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) { |
316 | 53 | NaturalLayout = false; |
317 | 53 | llvm::Constant *C = Elems[Index]; |
318 | 53 | CharUnits Offset = Offsets[Index]; |
319 | | |
320 | 53 | if (auto *CA = dyn_cast<llvm::ConstantAggregate>(C)) { |
321 | | // Expand the sequence into its contained elements. |
322 | | // FIXME: This assumes vector elements are byte-sized. |
323 | 34 | replace(Elems, Index, Index + 1, |
324 | 34 | llvm::map_range(llvm::seq(0u, CA->getNumOperands()), |
325 | 75 | [&](unsigned Op) { return CA->getOperand(Op); })); |
326 | 34 | if (isa<llvm::ArrayType>(CA->getType()) || |
327 | 32 | isa<llvm::VectorType>(CA->getType())) { |
328 | | // Array or vector. |
329 | 2 | llvm::Type *ElemTy = |
330 | 2 | llvm::GetElementPtrInst::getTypeAtIndex(CA->getType(), (uint64_t)0); |
331 | 2 | CharUnits ElemSize = getSize(ElemTy); |
332 | 2 | replace( |
333 | 2 | Offsets, Index, Index + 1, |
334 | 2 | llvm::map_range(llvm::seq(0u, CA->getNumOperands()), |
335 | 6 | [&](unsigned Op) { return Offset + Op * ElemSize; })); |
336 | 32 | } else { |
337 | | // Must be a struct. |
338 | 32 | auto *ST = cast<llvm::StructType>(CA->getType()); |
339 | 32 | const llvm::StructLayout *Layout = |
340 | 32 | CGM.getDataLayout().getStructLayout(ST); |
341 | 32 | replace(Offsets, Index, Index + 1, |
342 | 32 | llvm::map_range( |
343 | 69 | llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) { |
344 | 69 | return Offset + CharUnits::fromQuantity( |
345 | 69 | Layout->getElementOffset(Op)); |
346 | 69 | })); |
347 | 32 | } |
348 | 34 | return true; |
349 | 34 | } |
350 | | |
351 | 19 | if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(C)) { |
352 | | // Expand the sequence into its contained elements. |
353 | | // FIXME: This assumes vector elements are byte-sized. |
354 | | // FIXME: If possible, split into two ConstantDataSequentials at Hint. |
355 | 15 | CharUnits ElemSize = getSize(CDS->getElementType()); |
356 | 15 | replace(Elems, Index, Index + 1, |
357 | 15 | llvm::map_range(llvm::seq(0u, CDS->getNumElements()), |
358 | 87 | [&](unsigned Elem) { |
359 | 87 | return CDS->getElementAsConstant(Elem); |
360 | 87 | })); |
361 | 15 | replace(Offsets, Index, Index + 1, |
362 | 15 | llvm::map_range( |
363 | 15 | llvm::seq(0u, CDS->getNumElements()), |
364 | 87 | [&](unsigned Elem) { return Offset + Elem * ElemSize; })); |
365 | 15 | return true; |
366 | 15 | } |
367 | | |
368 | 4 | if (isa<llvm::ConstantAggregateZero>(C)) { |
369 | | // Split into two zeros at the hinted offset. |
370 | 4 | CharUnits ElemSize = getSize(C); |
371 | 4 | assert(Hint > Offset && Hint < Offset + ElemSize && "nothing to split"); |
372 | 4 | replace(Elems, Index, Index + 1, |
373 | 4 | {getZeroes(Hint - Offset), getZeroes(Offset + ElemSize - Hint)}); |
374 | 4 | replace(Offsets, Index, Index + 1, {Offset, Hint}); |
375 | 4 | return true; |
376 | 4 | } |
377 | | |
378 | 0 | if (isa<llvm::UndefValue>(C)) { |
379 | | // Drop undef; it doesn't contribute to the final layout. |
380 | 0 | replace(Elems, Index, Index + 1, {}); |
381 | 0 | replace(Offsets, Index, Index + 1, {}); |
382 | 0 | return true; |
383 | 0 | } |
384 | | |
385 | | // FIXME: We could split a ConstantInt if the need ever arose. |
386 | | // We don't need to do this to handle bit-fields because we always eagerly |
387 | | // split them into 1-byte chunks. |
388 | | |
389 | 0 | return false; |
390 | 0 | } |
391 | | |
392 | | static llvm::Constant * |
393 | | EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType, |
394 | | llvm::Type *CommonElementType, unsigned ArrayBound, |
395 | | SmallVectorImpl<llvm::Constant *> &Elements, |
396 | | llvm::Constant *Filler); |
397 | | |
398 | | llvm::Constant *ConstantAggregateBuilder::buildFrom( |
399 | | CodeGenModule &CGM, ArrayRef<llvm::Constant *> Elems, |
400 | | ArrayRef<CharUnits> Offsets, CharUnits StartOffset, CharUnits Size, |
401 | 5.77k | bool NaturalLayout, llvm::Type *DesiredTy, bool AllowOversized) { |
402 | 5.77k | ConstantAggregateBuilderUtils Utils(CGM); |
403 | | |
404 | 5.77k | if (Elems.empty()) |
405 | 90 | return llvm::UndefValue::get(DesiredTy); |
406 | | |
407 | 5.68k | auto Offset = [&](size_t I) { return Offsets[I] - StartOffset; }465 ; |
408 | | |
409 | | // If we want an array type, see if all the elements are the same type and |
410 | | // appropriately spaced. |
411 | 5.68k | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(DesiredTy)) { |
412 | 21 | assert(!AllowOversized && "oversized array emission not supported"); |
413 | | |
414 | 21 | bool CanEmitArray = true; |
415 | 21 | llvm::Type *CommonType = Elems[0]->getType(); |
416 | 21 | llvm::Constant *Filler = llvm::Constant::getNullValue(CommonType); |
417 | 21 | CharUnits ElemSize = Utils.getSize(ATy->getElementType()); |
418 | 21 | SmallVector<llvm::Constant*, 32> ArrayElements; |
419 | 128 | for (size_t I = 0; I != Elems.size(); ++I107 ) { |
420 | | // Skip zeroes; we'll use a zero value as our array filler. |
421 | 109 | if (Elems[I]->isNullValue()) |
422 | 35 | continue; |
423 | | |
424 | | // All remaining elements must be the same type. |
425 | 74 | if (Elems[I]->getType() != CommonType || |
426 | 72 | Offset(I) % ElemSize != 0) { |
427 | 2 | CanEmitArray = false; |
428 | 2 | break; |
429 | 2 | } |
430 | 72 | ArrayElements.resize(Offset(I) / ElemSize + 1, Filler); |
431 | 72 | ArrayElements.back() = Elems[I]; |
432 | 72 | } |
433 | | |
434 | 21 | if (CanEmitArray) { |
435 | 19 | return EmitArrayConstant(CGM, ATy, CommonType, ATy->getNumElements(), |
436 | 19 | ArrayElements, Filler); |
437 | 19 | } |
438 | | |
439 | | // Can't emit as an array, carry on to emit as a struct. |
440 | 21 | } |
441 | | |
442 | 5.67k | CharUnits DesiredSize = Utils.getSize(DesiredTy); |
443 | 5.67k | CharUnits Align = CharUnits::One(); |
444 | 5.67k | for (llvm::Constant *C : Elems) |
445 | 11.4k | Align = std::max(Align, Utils.getAlignment(C)); |
446 | 5.67k | CharUnits AlignedSize = Size.alignTo(Align); |
447 | | |
448 | 5.67k | bool Packed = false; |
449 | 5.67k | ArrayRef<llvm::Constant*> UnpackedElems = Elems; |
450 | 5.67k | llvm::SmallVector<llvm::Constant*, 32> UnpackedElemStorage; |
451 | 5.67k | if ((DesiredSize < AlignedSize && !AllowOversized116 ) || |
452 | 5.55k | DesiredSize.alignTo(Align) != DesiredSize) { |
453 | | // The natural layout would be the wrong size; force use of a packed layout. |
454 | 113 | NaturalLayout = false; |
455 | 113 | Packed = true; |
456 | 5.55k | } else if (DesiredSize > AlignedSize) { |
457 | | // The constant would be too small. Add padding to fix it. |
458 | 366 | UnpackedElemStorage.assign(Elems.begin(), Elems.end()); |
459 | 366 | UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size)); |
460 | 366 | UnpackedElems = UnpackedElemStorage; |
461 | 366 | } |
462 | | |
463 | | // If we don't have a natural layout, insert padding as necessary. |
464 | | // As we go, double-check to see if we can actually just emit Elems |
465 | | // as a non-packed struct and do so opportunistically if possible. |
466 | 5.67k | llvm::SmallVector<llvm::Constant*, 32> PackedElems; |
467 | 5.67k | if (!NaturalLayout) { |
468 | 156 | CharUnits SizeSoFar = CharUnits::Zero(); |
469 | 477 | for (size_t I = 0; I != Elems.size(); ++I321 ) { |
470 | 321 | CharUnits Align = Utils.getAlignment(Elems[I]); |
471 | 321 | CharUnits NaturalOffset = SizeSoFar.alignTo(Align); |
472 | 321 | CharUnits DesiredOffset = Offset(I); |
473 | 321 | assert(DesiredOffset >= SizeSoFar && "elements out of order"); |
474 | | |
475 | 321 | if (DesiredOffset != NaturalOffset) |
476 | 106 | Packed = true; |
477 | 321 | if (DesiredOffset != SizeSoFar) |
478 | 3 | PackedElems.push_back(Utils.getPadding(DesiredOffset - SizeSoFar)); |
479 | 321 | PackedElems.push_back(Elems[I]); |
480 | 321 | SizeSoFar = DesiredOffset + Utils.getSize(Elems[I]); |
481 | 321 | } |
482 | | // If we're using the packed layout, pad it out to the desired size if |
483 | | // necessary. |
484 | 156 | if (Packed) { |
485 | 116 | assert((SizeSoFar <= DesiredSize || AllowOversized) && |
486 | 116 | "requested size is too small for contents"); |
487 | 116 | if (SizeSoFar < DesiredSize) |
488 | 9 | PackedElems.push_back(Utils.getPadding(DesiredSize - SizeSoFar)); |
489 | 116 | } |
490 | 156 | } |
491 | | |
492 | 5.67k | llvm::StructType *STy = llvm::ConstantStruct::getTypeForElements( |
493 | 5.55k | CGM.getLLVMContext(), Packed ? PackedElems116 : UnpackedElems, Packed); |
494 | | |
495 | | // Pick the type to use. If the type is layout identical to the desired |
496 | | // type then use it, otherwise use whatever the builder produced for us. |
497 | 5.67k | if (llvm::StructType *DesiredSTy = dyn_cast<llvm::StructType>(DesiredTy)) { |
498 | 5.66k | if (DesiredSTy->isLayoutIdentical(STy)) |
499 | 4.73k | STy = DesiredSTy; |
500 | 5.66k | } |
501 | | |
502 | 5.55k | return llvm::ConstantStruct::get(STy, Packed ? PackedElems116 : UnpackedElems); |
503 | 5.67k | } |
504 | | |
505 | | void ConstantAggregateBuilder::condense(CharUnits Offset, |
506 | 18 | llvm::Type *DesiredTy) { |
507 | 18 | CharUnits Size = getSize(DesiredTy); |
508 | | |
509 | 18 | llvm::Optional<size_t> FirstElemToReplace = splitAt(Offset); |
510 | 18 | if (!FirstElemToReplace) |
511 | 0 | return; |
512 | 18 | size_t First = *FirstElemToReplace; |
513 | | |
514 | 18 | llvm::Optional<size_t> LastElemToReplace = splitAt(Offset + Size); |
515 | 18 | if (!LastElemToReplace) |
516 | 0 | return; |
517 | 18 | size_t Last = *LastElemToReplace; |
518 | | |
519 | 18 | size_t Length = Last - First; |
520 | 18 | if (Length == 0) |
521 | 0 | return; |
522 | | |
523 | 18 | if (Length == 1 && Offsets[First] == Offset4 && |
524 | 4 | getSize(Elems[First]) == Size) { |
525 | | // Re-wrap single element structs if necessary. Otherwise, leave any single |
526 | | // element constant of the right size alone even if it has the wrong type. |
527 | 4 | auto *STy = dyn_cast<llvm::StructType>(DesiredTy); |
528 | 4 | if (STy && STy->getNumElements() == 1 && |
529 | 4 | STy->getElementType(0) == Elems[First]->getType()) |
530 | 2 | Elems[First] = llvm::ConstantStruct::get(STy, Elems[First]); |
531 | 4 | return; |
532 | 4 | } |
533 | | |
534 | 14 | llvm::Constant *Replacement = buildFrom( |
535 | 14 | CGM, makeArrayRef(Elems).slice(First, Length), |
536 | 14 | makeArrayRef(Offsets).slice(First, Length), Offset, getSize(DesiredTy), |
537 | 14 | /*known to have natural layout=*/false, DesiredTy, false); |
538 | 14 | replace(Elems, First, Last, {Replacement}); |
539 | 14 | replace(Offsets, First, Last, {Offset}); |
540 | 14 | } |
541 | | |
542 | | //===----------------------------------------------------------------------===// |
543 | | // ConstStructBuilder |
544 | | //===----------------------------------------------------------------------===// |
545 | | |
546 | | class ConstStructBuilder { |
547 | | CodeGenModule &CGM; |
548 | | ConstantEmitter &Emitter; |
549 | | ConstantAggregateBuilder &Builder; |
550 | | CharUnits StartOffset; |
551 | | |
552 | | public: |
553 | | static llvm::Constant *BuildStruct(ConstantEmitter &Emitter, |
554 | | InitListExpr *ILE, QualType StructTy); |
555 | | static llvm::Constant *BuildStruct(ConstantEmitter &Emitter, |
556 | | const APValue &Value, QualType ValTy); |
557 | | static bool UpdateStruct(ConstantEmitter &Emitter, |
558 | | ConstantAggregateBuilder &Const, CharUnits Offset, |
559 | | InitListExpr *Updater); |
560 | | |
561 | | private: |
562 | | ConstStructBuilder(ConstantEmitter &Emitter, |
563 | | ConstantAggregateBuilder &Builder, CharUnits StartOffset) |
564 | | : CGM(Emitter.CGM), Emitter(Emitter), Builder(Builder), |
565 | 5.80k | StartOffset(StartOffset) {} |
566 | | |
567 | | bool AppendField(const FieldDecl *Field, uint64_t FieldOffset, |
568 | | llvm::Constant *InitExpr, bool AllowOverwrite = false); |
569 | | |
570 | | bool AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst, |
571 | | bool AllowOverwrite = false); |
572 | | |
573 | | bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset, |
574 | | llvm::ConstantInt *InitExpr, bool AllowOverwrite = false); |
575 | | |
576 | | bool Build(InitListExpr *ILE, bool AllowOverwrite); |
577 | | bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase, |
578 | | const CXXRecordDecl *VTableClass, CharUnits BaseOffset); |
579 | | llvm::Constant *Finalize(QualType Ty); |
580 | | }; |
581 | | |
582 | | bool ConstStructBuilder::AppendField( |
583 | | const FieldDecl *Field, uint64_t FieldOffset, llvm::Constant *InitCst, |
584 | 10.6k | bool AllowOverwrite) { |
585 | 10.6k | const ASTContext &Context = CGM.getContext(); |
586 | | |
587 | 10.6k | CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset); |
588 | | |
589 | 10.6k | return AppendBytes(FieldOffsetInChars, InitCst, AllowOverwrite); |
590 | 10.6k | } |
591 | | |
592 | | bool ConstStructBuilder::AppendBytes(CharUnits FieldOffsetInChars, |
593 | | llvm::Constant *InitCst, |
594 | 10.9k | bool AllowOverwrite) { |
595 | 10.9k | return Builder.add(InitCst, StartOffset + FieldOffsetInChars, AllowOverwrite); |
596 | 10.9k | } |
597 | | |
598 | | bool ConstStructBuilder::AppendBitField( |
599 | | const FieldDecl *Field, uint64_t FieldOffset, llvm::ConstantInt *CI, |
600 | 216 | bool AllowOverwrite) { |
601 | 216 | const CGRecordLayout &RL = |
602 | 216 | CGM.getTypes().getCGRecordLayout(Field->getParent()); |
603 | 216 | const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field); |
604 | 216 | llvm::APInt FieldValue = CI->getValue(); |
605 | | |
606 | | // Promote the size of FieldValue if necessary |
607 | | // FIXME: This should never occur, but currently it can because initializer |
608 | | // constants are cast to bool, and because clang is not enforcing bitfield |
609 | | // width limits. |
610 | 216 | if (Info.Size > FieldValue.getBitWidth()) |
611 | 4 | FieldValue = FieldValue.zext(Info.Size); |
612 | | |
613 | | // Truncate the size of FieldValue to the bit field size. |
614 | 216 | if (Info.Size < FieldValue.getBitWidth()) |
615 | 201 | FieldValue = FieldValue.trunc(Info.Size); |
616 | | |
617 | 216 | return Builder.addBits(FieldValue, |
618 | 216 | CGM.getContext().toBits(StartOffset) + FieldOffset, |
619 | 216 | AllowOverwrite); |
620 | 216 | } |
621 | | |
622 | | static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter, |
623 | | ConstantAggregateBuilder &Const, |
624 | | CharUnits Offset, QualType Type, |
625 | 46 | InitListExpr *Updater) { |
626 | 46 | if (Type->isRecordType()) |
627 | 25 | return ConstStructBuilder::UpdateStruct(Emitter, Const, Offset, Updater); |
628 | | |
629 | 21 | auto CAT = Emitter.CGM.getContext().getAsConstantArrayType(Type); |
630 | 21 | if (!CAT) |
631 | 0 | return false; |
632 | 21 | QualType ElemType = CAT->getElementType(); |
633 | 21 | CharUnits ElemSize = Emitter.CGM.getContext().getTypeSizeInChars(ElemType); |
634 | 21 | llvm::Type *ElemTy = Emitter.CGM.getTypes().ConvertTypeForMem(ElemType); |
635 | | |
636 | 21 | llvm::Constant *FillC = nullptr; |
637 | 21 | if (Expr *Filler = Updater->getArrayFiller()) { |
638 | 21 | if (!isa<NoInitExpr>(Filler)) { |
639 | 3 | FillC = Emitter.tryEmitAbstractForMemory(Filler, ElemType); |
640 | 3 | if (!FillC) |
641 | 0 | return false; |
642 | 21 | } |
643 | 21 | } |
644 | | |
645 | 21 | unsigned NumElementsToUpdate = |
646 | 18 | FillC ? CAT->getSize().getZExtValue()3 : Updater->getNumInits(); |
647 | 111 | for (unsigned I = 0; I != NumElementsToUpdate; ++I, Offset += ElemSize90 ) { |
648 | 90 | Expr *Init = nullptr; |
649 | 90 | if (I < Updater->getNumInits()) |
650 | 78 | Init = Updater->getInit(I); |
651 | | |
652 | 90 | if (!Init && FillC12 ) { |
653 | 12 | if (!Const.add(FillC, Offset, true)) |
654 | 0 | return false; |
655 | 78 | } else if (!Init || isa<NoInitExpr>(Init)) { |
656 | 52 | continue; |
657 | 26 | } else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init)) { |
658 | 4 | if (!EmitDesignatedInitUpdater(Emitter, Const, Offset, ElemType, |
659 | 4 | ChildILE)) |
660 | 0 | return false; |
661 | | // Attempt to reduce the array element to a single constant if necessary. |
662 | 4 | Const.condense(Offset, ElemTy); |
663 | 22 | } else { |
664 | 22 | llvm::Constant *Val = Emitter.tryEmitPrivateForMemory(Init, ElemType); |
665 | 22 | if (!Const.add(Val, Offset, true)) |
666 | 0 | return false; |
667 | 22 | } |
668 | 90 | } |
669 | | |
670 | 21 | return true; |
671 | 21 | } |
672 | | |
673 | 2.44k | bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) { |
674 | 2.44k | RecordDecl *RD = ILE->getType()->castAs<RecordType>()->getDecl(); |
675 | 2.44k | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
676 | | |
677 | 2.44k | unsigned FieldNo = -1; |
678 | 2.44k | unsigned ElementNo = 0; |
679 | | |
680 | | // Bail out if we have base classes. We could support these, but they only |
681 | | // arise in C++1z where we will have already constant folded most interesting |
682 | | // cases. FIXME: There are still a few more cases we can handle this way. |
683 | 2.44k | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
684 | 125 | if (CXXRD->getNumBases()) |
685 | 6 | return false; |
686 | | |
687 | 5.43k | for (FieldDecl *Field : RD->fields())2.44k { |
688 | 5.43k | ++FieldNo; |
689 | | |
690 | | // If this is a union, skip all the fields that aren't being initialized. |
691 | 5.43k | if (RD->isUnion() && |
692 | 254 | !declaresSameEntity(ILE->getInitializedFieldInUnion(), Field)) |
693 | 125 | continue; |
694 | | |
695 | | // Don't emit anonymous bitfields or zero-sized fields. |
696 | 5.30k | if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext())5.28k ) |
697 | 24 | continue; |
698 | | |
699 | | // Get the initializer. A struct can include fields without initializers, |
700 | | // we just use explicit null values for them. |
701 | 5.28k | Expr *Init = nullptr; |
702 | 5.28k | if (ElementNo < ILE->getNumInits()) |
703 | 5.23k | Init = ILE->getInit(ElementNo++); |
704 | 5.28k | if (Init && isa<NoInitExpr>(Init)5.23k ) |
705 | 15 | continue; |
706 | | |
707 | | // When emitting a DesignatedInitUpdateExpr, a nested InitListExpr |
708 | | // represents additional overwriting of our current constant value, and not |
709 | | // a new constant to emit independently. |
710 | 5.27k | if (AllowOverwrite && |
711 | 27 | (Field->getType()->isArrayType() || Field->getType()->isRecordType()13 )) { |
712 | 15 | if (auto *SubILE = dyn_cast<InitListExpr>(Init)) { |
713 | 14 | CharUnits Offset = CGM.getContext().toCharUnitsFromBits( |
714 | 14 | Layout.getFieldOffset(FieldNo)); |
715 | 14 | if (!EmitDesignatedInitUpdater(Emitter, Builder, StartOffset + Offset, |
716 | 14 | Field->getType(), SubILE)) |
717 | 0 | return false; |
718 | | // If we split apart the field's value, try to collapse it down to a |
719 | | // single value now. |
720 | 14 | Builder.condense(StartOffset + Offset, |
721 | 14 | CGM.getTypes().ConvertTypeForMem(Field->getType())); |
722 | 14 | continue; |
723 | 14 | } |
724 | 15 | } |
725 | | |
726 | 5.25k | llvm::Constant *EltInit = |
727 | 5.21k | Init ? Emitter.tryEmitPrivateForMemory(Init, Field->getType()) |
728 | 46 | : Emitter.emitNullForMemory(Field->getType()); |
729 | 5.25k | if (!EltInit) |
730 | 29 | return false; |
731 | | |
732 | 5.22k | if (!Field->isBitField()) { |
733 | | // Handle non-bitfield members. |
734 | 5.09k | if (!AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit, |
735 | 5.09k | AllowOverwrite)) |
736 | 0 | return false; |
737 | | // After emitting a non-empty field with [[no_unique_address]], we may |
738 | | // need to overwrite its tail padding. |
739 | 5.09k | if (Field->hasAttr<NoUniqueAddressAttr>()) |
740 | 0 | AllowOverwrite = true; |
741 | 129 | } else { |
742 | | // Otherwise we have a bitfield. |
743 | 129 | if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) { |
744 | 126 | if (!AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI, |
745 | 126 | AllowOverwrite)) |
746 | 0 | return false; |
747 | 3 | } else { |
748 | | // We are trying to initialize a bitfield with a non-trivial constant, |
749 | | // this must require run-time code. |
750 | 3 | return false; |
751 | 3 | } |
752 | 129 | } |
753 | 5.22k | } |
754 | | |
755 | 2.41k | return true; |
756 | 2.44k | } |
757 | | |
758 | | namespace { |
759 | | struct BaseInfo { |
760 | | BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index) |
761 | 409 | : Decl(Decl), Offset(Offset), Index(Index) { |
762 | 409 | } |
763 | | |
764 | | const CXXRecordDecl *Decl; |
765 | | CharUnits Offset; |
766 | | unsigned Index; |
767 | | |
768 | 63 | bool operator<(const BaseInfo &O) const { return Offset < O.Offset; } |
769 | | }; |
770 | | } |
771 | | |
772 | | bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD, |
773 | | bool IsPrimaryBase, |
774 | | const CXXRecordDecl *VTableClass, |
775 | 3.76k | CharUnits Offset) { |
776 | 3.76k | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
777 | | |
778 | 3.76k | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
779 | | // Add a vtable pointer, if we need one and it hasn't already been added. |
780 | 3.75k | if (Layout.hasOwnVFPtr()) { |
781 | 346 | llvm::Constant *VTableAddressPoint = |
782 | 346 | CGM.getCXXABI().getVTableAddressPointForConstExpr( |
783 | 346 | BaseSubobject(CD, Offset), VTableClass); |
784 | 346 | if (!AppendBytes(Offset, VTableAddressPoint)) |
785 | 0 | return false; |
786 | 3.75k | } |
787 | | |
788 | | // Accumulate and sort bases, in order to visit them in address order, which |
789 | | // may not be the same as declaration order. |
790 | 3.75k | SmallVector<BaseInfo, 8> Bases; |
791 | 3.75k | Bases.reserve(CD->getNumBases()); |
792 | 3.75k | unsigned BaseNo = 0; |
793 | 3.75k | for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(), |
794 | 4.16k | BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo409 ) { |
795 | 409 | assert(!Base->isVirtual() && "should not have virtual bases here"); |
796 | 409 | const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl(); |
797 | 409 | CharUnits BaseOffset = Layout.getBaseClassOffset(BD); |
798 | 409 | Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo)); |
799 | 409 | } |
800 | 3.75k | llvm::stable_sort(Bases); |
801 | | |
802 | 4.16k | for (unsigned I = 0, N = Bases.size(); I != N; ++I409 ) { |
803 | 409 | BaseInfo &Base = Bases[I]; |
804 | | |
805 | 409 | bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl; |
806 | 409 | Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase, |
807 | 409 | VTableClass, Offset + Base.Offset); |
808 | 409 | } |
809 | 3.75k | } |
810 | | |
811 | 3.76k | unsigned FieldNo = 0; |
812 | 3.76k | uint64_t OffsetBits = CGM.getContext().toBits(Offset); |
813 | | |
814 | 3.76k | bool AllowOverwrite = false; |
815 | 3.76k | for (RecordDecl::field_iterator Field = RD->field_begin(), |
816 | 9.88k | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo6.12k ) { |
817 | | // If this is a union, skip all the fields that aren't being initialized. |
818 | 6.12k | if (RD->isUnion() && !declaresSameEntity(Val.getUnionField(), *Field)913 ) |
819 | 452 | continue; |
820 | | |
821 | | // Don't emit anonymous bitfields or zero-sized fields. |
822 | 5.66k | if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext())5.64k ) |
823 | 43 | continue; |
824 | | |
825 | | // Emit the value of the initializer. |
826 | 5.62k | const APValue &FieldValue = |
827 | 5.16k | RD->isUnion() ? Val.getUnionValue()461 : Val.getStructField(FieldNo); |
828 | 5.62k | llvm::Constant *EltInit = |
829 | 5.62k | Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType()); |
830 | 5.62k | if (!EltInit) |
831 | 0 | return false; |
832 | | |
833 | 5.62k | if (!Field->isBitField()) { |
834 | | // Handle non-bitfield members. |
835 | 5.53k | if (!AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, |
836 | 5.53k | EltInit, AllowOverwrite)) |
837 | 0 | return false; |
838 | | // After emitting a non-empty field with [[no_unique_address]], we may |
839 | | // need to overwrite its tail padding. |
840 | 5.53k | if (Field->hasAttr<NoUniqueAddressAttr>()) |
841 | 6 | AllowOverwrite = true; |
842 | 90 | } else { |
843 | | // Otherwise we have a bitfield. |
844 | 90 | if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, |
845 | 90 | cast<llvm::ConstantInt>(EltInit), AllowOverwrite)) |
846 | 0 | return false; |
847 | 90 | } |
848 | 5.62k | } |
849 | | |
850 | 3.76k | return true; |
851 | 3.76k | } |
852 | | |
853 | 5.73k | llvm::Constant *ConstStructBuilder::Finalize(QualType Type) { |
854 | 5.73k | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
855 | 5.73k | llvm::Type *ValTy = CGM.getTypes().ConvertType(Type); |
856 | 5.73k | return Builder.build(ValTy, RD->hasFlexibleArrayMember()); |
857 | 5.73k | } |
858 | | |
859 | | llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter, |
860 | | InitListExpr *ILE, |
861 | 2.42k | QualType ValTy) { |
862 | 2.42k | ConstantAggregateBuilder Const(Emitter.CGM); |
863 | 2.42k | ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero()); |
864 | | |
865 | 2.42k | if (!Builder.Build(ILE, /*AllowOverwrite*/false)) |
866 | 38 | return nullptr; |
867 | | |
868 | 2.38k | return Builder.Finalize(ValTy); |
869 | 2.38k | } |
870 | | |
871 | | llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter, |
872 | | const APValue &Val, |
873 | 3.35k | QualType ValTy) { |
874 | 3.35k | ConstantAggregateBuilder Const(Emitter.CGM); |
875 | 3.35k | ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero()); |
876 | | |
877 | 3.35k | const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl(); |
878 | 3.35k | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
879 | 3.35k | if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero())) |
880 | 0 | return nullptr; |
881 | | |
882 | 3.35k | return Builder.Finalize(ValTy); |
883 | 3.35k | } |
884 | | |
885 | | bool ConstStructBuilder::UpdateStruct(ConstantEmitter &Emitter, |
886 | | ConstantAggregateBuilder &Const, |
887 | 25 | CharUnits Offset, InitListExpr *Updater) { |
888 | 25 | return ConstStructBuilder(Emitter, Const, Offset) |
889 | 25 | .Build(Updater, /*AllowOverwrite*/ true); |
890 | 25 | } |
891 | | |
892 | | //===----------------------------------------------------------------------===// |
893 | | // ConstExprEmitter |
894 | | //===----------------------------------------------------------------------===// |
895 | | |
896 | | static ConstantAddress tryEmitGlobalCompoundLiteral(CodeGenModule &CGM, |
897 | | CodeGenFunction *CGF, |
898 | 17 | const CompoundLiteralExpr *E) { |
899 | 17 | CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType()); |
900 | 17 | if (llvm::GlobalVariable *Addr = |
901 | 2 | CGM.getAddrOfConstantCompoundLiteralIfEmitted(E)) |
902 | 2 | return ConstantAddress(Addr, Align); |
903 | | |
904 | 15 | LangAS addressSpace = E->getType().getAddressSpace(); |
905 | | |
906 | 15 | ConstantEmitter emitter(CGM, CGF); |
907 | 15 | llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(), |
908 | 15 | addressSpace, E->getType()); |
909 | 15 | if (!C) { |
910 | 0 | assert(!E->isFileScope() && |
911 | 0 | "file-scope compound literal did not have constant initializer!"); |
912 | 0 | return ConstantAddress::invalid(); |
913 | 0 | } |
914 | | |
915 | 15 | auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), |
916 | 15 | CGM.isTypeConstant(E->getType(), true), |
917 | 15 | llvm::GlobalValue::InternalLinkage, |
918 | 15 | C, ".compoundliteral", nullptr, |
919 | 15 | llvm::GlobalVariable::NotThreadLocal, |
920 | 15 | CGM.getContext().getTargetAddressSpace(addressSpace)); |
921 | 15 | emitter.finalize(GV); |
922 | 15 | GV->setAlignment(Align.getAsAlign()); |
923 | 15 | CGM.setAddrOfConstantCompoundLiteral(E, GV); |
924 | 15 | return ConstantAddress(GV, Align); |
925 | 15 | } |
926 | | |
927 | | static llvm::Constant * |
928 | | EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType, |
929 | | llvm::Type *CommonElementType, unsigned ArrayBound, |
930 | | SmallVectorImpl<llvm::Constant *> &Elements, |
931 | 3.76k | llvm::Constant *Filler) { |
932 | | // Figure out how long the initial prefix of non-zero elements is. |
933 | 3.76k | unsigned NonzeroLength = ArrayBound; |
934 | 3.76k | if (Elements.size() < NonzeroLength && Filler->isNullValue()738 ) |
935 | 730 | NonzeroLength = Elements.size(); |
936 | 3.76k | if (NonzeroLength == Elements.size()) { |
937 | 4.27k | while (NonzeroLength > 0 && Elements[NonzeroLength - 1]->isNullValue()3.65k ) |
938 | 516 | --NonzeroLength; |
939 | 3.75k | } |
940 | | |
941 | 3.76k | if (NonzeroLength == 0) |
942 | 621 | return llvm::ConstantAggregateZero::get(DesiredType); |
943 | | |
944 | | // Add a zeroinitializer array filler if we have lots of trailing zeroes. |
945 | 3.14k | unsigned TrailingZeroes = ArrayBound - NonzeroLength; |
946 | 3.14k | if (TrailingZeroes >= 8) { |
947 | 58 | assert(Elements.size() >= NonzeroLength && |
948 | 58 | "missing initializer for non-zero element"); |
949 | | |
950 | | // If all the elements had the same type up to the trailing zeroes, emit a |
951 | | // struct of two arrays (the nonzero data and the zeroinitializer). |
952 | 58 | if (CommonElementType && NonzeroLength >= 856 ) { |
953 | 17 | llvm::Constant *Initial = llvm::ConstantArray::get( |
954 | 17 | llvm::ArrayType::get(CommonElementType, NonzeroLength), |
955 | 17 | makeArrayRef(Elements).take_front(NonzeroLength)); |
956 | 17 | Elements.resize(2); |
957 | 17 | Elements[0] = Initial; |
958 | 41 | } else { |
959 | 41 | Elements.resize(NonzeroLength + 1); |
960 | 41 | } |
961 | | |
962 | 58 | auto *FillerType = |
963 | 56 | CommonElementType ? CommonElementType : DesiredType->getElementType()2 ; |
964 | 58 | FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes); |
965 | 58 | Elements.back() = llvm::ConstantAggregateZero::get(FillerType); |
966 | 58 | CommonElementType = nullptr; |
967 | 3.08k | } else if (Elements.size() != ArrayBound) { |
968 | | // Otherwise pad to the right size with the filler if necessary. |
969 | 165 | Elements.resize(ArrayBound, Filler); |
970 | 165 | if (Filler->getType() != CommonElementType) |
971 | 8 | CommonElementType = nullptr; |
972 | 165 | } |
973 | | |
974 | | // If all elements have the same type, just emit an array constant. |
975 | 3.14k | if (CommonElementType) |
976 | 3.07k | return llvm::ConstantArray::get( |
977 | 3.07k | llvm::ArrayType::get(CommonElementType, ArrayBound), Elements); |
978 | | |
979 | | // We have mixed types. Use a packed struct. |
980 | 71 | llvm::SmallVector<llvm::Type *, 16> Types; |
981 | 71 | Types.reserve(Elements.size()); |
982 | 71 | for (llvm::Constant *Elt : Elements) |
983 | 233 | Types.push_back(Elt->getType()); |
984 | 71 | llvm::StructType *SType = |
985 | 71 | llvm::StructType::get(CGM.getLLVMContext(), Types, true); |
986 | 71 | return llvm::ConstantStruct::get(SType, Elements); |
987 | 71 | } |
988 | | |
989 | | // This class only needs to handle arrays, structs and unions. Outside C++11 |
990 | | // mode, we don't currently constant fold those types. All other types are |
991 | | // handled by constant folding. |
992 | | // |
993 | | // Constant folding is currently missing support for a few features supported |
994 | | // here: CK_ToUnion, CK_ReinterpretMemberPointer, and DesignatedInitUpdateExpr. |
995 | | class ConstExprEmitter : |
996 | | public StmtVisitor<ConstExprEmitter, llvm::Constant*, QualType> { |
997 | | CodeGenModule &CGM; |
998 | | ConstantEmitter &Emitter; |
999 | | llvm::LLVMContext &VMContext; |
1000 | | public: |
1001 | | ConstExprEmitter(ConstantEmitter &emitter) |
1002 | 23.9k | : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) { |
1003 | 23.9k | } |
1004 | | |
1005 | | //===--------------------------------------------------------------------===// |
1006 | | // Visitor Methods |
1007 | | //===--------------------------------------------------------------------===// |
1008 | | |
1009 | 14.9k | llvm::Constant *VisitStmt(Stmt *S, QualType T) { |
1010 | 14.9k | return nullptr; |
1011 | 14.9k | } |
1012 | | |
1013 | 16 | llvm::Constant *VisitConstantExpr(ConstantExpr *CE, QualType T) { |
1014 | 16 | if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(CE)) |
1015 | 0 | return Result; |
1016 | 16 | return Visit(CE->getSubExpr(), T); |
1017 | 16 | } |
1018 | | |
1019 | 87 | llvm::Constant *VisitParenExpr(ParenExpr *PE, QualType T) { |
1020 | 87 | return Visit(PE->getSubExpr(), T); |
1021 | 87 | } |
1022 | | |
1023 | | llvm::Constant * |
1024 | | VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE, |
1025 | 0 | QualType T) { |
1026 | 0 | return Visit(PE->getReplacement(), T); |
1027 | 0 | } |
1028 | | |
1029 | | llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE, |
1030 | 1 | QualType T) { |
1031 | 1 | return Visit(GE->getResultExpr(), T); |
1032 | 1 | } |
1033 | | |
1034 | 1 | llvm::Constant *VisitChooseExpr(ChooseExpr *CE, QualType T) { |
1035 | 1 | return Visit(CE->getChosenSubExpr(), T); |
1036 | 1 | } |
1037 | | |
1038 | 38 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) { |
1039 | 38 | return Visit(E->getInitializer(), T); |
1040 | 38 | } |
1041 | | |
1042 | 4.90k | llvm::Constant *VisitCastExpr(CastExpr *E, QualType destType) { |
1043 | 4.90k | if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E)) |
1044 | 391 | CGM.EmitExplicitCastExprType(ECE, Emitter.CGF); |
1045 | 4.90k | Expr *subExpr = E->getSubExpr(); |
1046 | | |
1047 | 4.90k | switch (E->getCastKind()) { |
1048 | 4 | case CK_ToUnion: { |
1049 | | // GCC cast to union extension |
1050 | 4 | assert(E->getType()->isUnionType() && |
1051 | 4 | "Destination type is not union type!"); |
1052 | | |
1053 | 4 | auto field = E->getTargetUnionField(); |
1054 | | |
1055 | 4 | auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType()); |
1056 | 4 | if (!C) return nullptr0 ; |
1057 | | |
1058 | 4 | auto destTy = ConvertType(destType); |
1059 | 4 | if (C->getType() == destTy) return C0 ; |
1060 | | |
1061 | | // Build a struct with the union sub-element as the first member, |
1062 | | // and padded to the appropriate size. |
1063 | 4 | SmallVector<llvm::Constant*, 2> Elts; |
1064 | 4 | SmallVector<llvm::Type*, 2> Types; |
1065 | 4 | Elts.push_back(C); |
1066 | 4 | Types.push_back(C->getType()); |
1067 | 4 | unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType()); |
1068 | 4 | unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy); |
1069 | | |
1070 | 4 | assert(CurSize <= TotalSize && "Union size mismatch!"); |
1071 | 4 | if (unsigned NumPadBytes = TotalSize - CurSize) { |
1072 | 2 | llvm::Type *Ty = CGM.Int8Ty; |
1073 | 2 | if (NumPadBytes > 1) |
1074 | 2 | Ty = llvm::ArrayType::get(Ty, NumPadBytes); |
1075 | | |
1076 | 2 | Elts.push_back(llvm::UndefValue::get(Ty)); |
1077 | 2 | Types.push_back(Ty); |
1078 | 2 | } |
1079 | | |
1080 | 4 | llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false); |
1081 | 4 | return llvm::ConstantStruct::get(STy, Elts); |
1082 | 4 | } |
1083 | | |
1084 | 0 | case CK_AddressSpaceConversion: { |
1085 | 0 | auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType()); |
1086 | 0 | if (!C) return nullptr; |
1087 | 0 | LangAS destAS = E->getType()->getPointeeType().getAddressSpace(); |
1088 | 0 | LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace(); |
1089 | 0 | llvm::Type *destTy = ConvertType(E->getType()); |
1090 | 0 | return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS, |
1091 | 0 | destAS, destTy); |
1092 | 0 | } |
1093 | |
|
1094 | 4.03k | case CK_LValueToRValue: |
1095 | 4.03k | case CK_AtomicToNonAtomic: |
1096 | 4.03k | case CK_NonAtomicToAtomic: |
1097 | 4.43k | case CK_NoOp: |
1098 | 4.45k | case CK_ConstructorConversion: |
1099 | 4.45k | return Visit(subExpr, destType); |
1100 | | |
1101 | 0 | case CK_IntToOCLSampler: |
1102 | 0 | llvm_unreachable("global sampler variables are not generated"); |
1103 | | |
1104 | 0 | case CK_Dependent: llvm_unreachable("saw dependent cast!"); |
1105 | | |
1106 | 0 | case CK_BuiltinFnToFnPtr: |
1107 | 0 | llvm_unreachable("builtin functions are handled elsewhere"); |
1108 | | |
1109 | 43 | case CK_ReinterpretMemberPointer: |
1110 | 45 | case CK_DerivedToBaseMemberPointer: |
1111 | 57 | case CK_BaseToDerivedMemberPointer: { |
1112 | 57 | auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType()); |
1113 | 57 | if (!C) return nullptr0 ; |
1114 | 57 | return CGM.getCXXABI().EmitMemberPointerConversion(E, C); |
1115 | 57 | } |
1116 | | |
1117 | | // These will never be supported. |
1118 | 0 | case CK_ObjCObjectLValueCast: |
1119 | 0 | case CK_ARCProduceObject: |
1120 | 3 | case CK_ARCConsumeObject: |
1121 | 34 | case CK_ARCReclaimReturnedObject: |
1122 | 34 | case CK_ARCExtendBlockObject: |
1123 | 34 | case CK_CopyAndAutoreleaseBlockObject: |
1124 | 34 | return nullptr; |
1125 | | |
1126 | | // These don't need to be handled here because Evaluate knows how to |
1127 | | // evaluate them in the cases where they can be folded. |
1128 | 70 | case CK_BitCast: |
1129 | 70 | case CK_ToVoid: |
1130 | 70 | case CK_Dynamic: |
1131 | 73 | case CK_LValueBitCast: |
1132 | 73 | case CK_LValueToRValueBitCast: |
1133 | 74 | case CK_NullToMemberPointer: |
1134 | 121 | case CK_UserDefinedConversion: |
1135 | 124 | case CK_CPointerToObjCPointerCast: |
1136 | 124 | case CK_BlockPointerToObjCPointerCast: |
1137 | 124 | case CK_AnyPointerToBlockPointerCast: |
1138 | 126 | case CK_ArrayToPointerDecay: |
1139 | 126 | case CK_FunctionToPointerDecay: |
1140 | 126 | case CK_BaseToDerived: |
1141 | 138 | case CK_DerivedToBase: |
1142 | 138 | case CK_UncheckedDerivedToBase: |
1143 | 138 | case CK_MemberPointerToBoolean: |
1144 | 138 | case CK_VectorSplat: |
1145 | 138 | case CK_FloatingRealToComplex: |
1146 | 138 | case CK_FloatingComplexToReal: |
1147 | 138 | case CK_FloatingComplexToBoolean: |
1148 | 138 | case CK_FloatingComplexCast: |
1149 | 138 | case CK_FloatingComplexToIntegralComplex: |
1150 | 138 | case CK_IntegralRealToComplex: |
1151 | 138 | case CK_IntegralComplexToReal: |
1152 | 138 | case CK_IntegralComplexToBoolean: |
1153 | 138 | case CK_IntegralComplexCast: |
1154 | 138 | case CK_IntegralComplexToFloatingComplex: |
1155 | 143 | case CK_PointerToIntegral: |
1156 | 143 | case CK_PointerToBoolean: |
1157 | 144 | case CK_NullToPointer: |
1158 | 344 | case CK_IntegralCast: |
1159 | 344 | case CK_BooleanToSignedIntegral: |
1160 | 347 | case CK_IntegralToPointer: |
1161 | 347 | case CK_IntegralToBoolean: |
1162 | 352 | case CK_IntegralToFloating: |
1163 | 354 | case CK_FloatingToIntegral: |
1164 | 354 | case CK_FloatingToBoolean: |
1165 | 354 | case CK_FloatingCast: |
1166 | 354 | case CK_FloatingToFixedPoint: |
1167 | 354 | case CK_FixedPointToFloating: |
1168 | 354 | case CK_FixedPointCast: |
1169 | 354 | case CK_FixedPointToBoolean: |
1170 | 354 | case CK_FixedPointToIntegral: |
1171 | 354 | case CK_IntegralToFixedPoint: |
1172 | 354 | case CK_ZeroToOCLOpaqueType: |
1173 | 354 | return nullptr; |
1174 | 0 | } |
1175 | 0 | llvm_unreachable("Invalid CastKind"); |
1176 | 0 | } |
1177 | | |
1178 | 3 | llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE, QualType T) { |
1179 | | // No need for a DefaultInitExprScope: we don't handle 'this' in a |
1180 | | // constant expression. |
1181 | 3 | return Visit(DIE->getExpr(), T); |
1182 | 3 | } |
1183 | | |
1184 | 712 | llvm::Constant *VisitExprWithCleanups(ExprWithCleanups *E, QualType T) { |
1185 | 712 | return Visit(E->getSubExpr(), T); |
1186 | 712 | } |
1187 | | |
1188 | | llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E, |
1189 | 168 | QualType T) { |
1190 | 168 | return Visit(E->getSubExpr(), T); |
1191 | 168 | } |
1192 | | |
1193 | 1.15k | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) { |
1194 | 1.15k | auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType()); |
1195 | 1.15k | assert(CAT && "can't emit array init for non-constant-bound array"); |
1196 | 1.15k | unsigned NumInitElements = ILE->getNumInits(); |
1197 | 1.15k | unsigned NumElements = CAT->getSize().getZExtValue(); |
1198 | | |
1199 | | // Initialising an array requires us to automatically |
1200 | | // initialise any elements that have not been initialised explicitly |
1201 | 1.15k | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
1202 | | |
1203 | 1.15k | QualType EltType = CAT->getElementType(); |
1204 | | |
1205 | | // Initialize remaining array elements. |
1206 | 1.15k | llvm::Constant *fillC = nullptr; |
1207 | 1.15k | if (Expr *filler = ILE->getArrayFiller()) { |
1208 | 141 | fillC = Emitter.tryEmitAbstractForMemory(filler, EltType); |
1209 | 141 | if (!fillC) |
1210 | 11 | return nullptr; |
1211 | 1.14k | } |
1212 | | |
1213 | | // Copy initializer elements. |
1214 | 1.14k | SmallVector<llvm::Constant*, 16> Elts; |
1215 | 1.14k | if (fillC && fillC->isNullValue()130 ) |
1216 | 130 | Elts.reserve(NumInitableElts + 1); |
1217 | 1.01k | else |
1218 | 1.01k | Elts.reserve(NumElements); |
1219 | | |
1220 | 1.14k | llvm::Type *CommonElementType = nullptr; |
1221 | 6.97k | for (unsigned i = 0; i < NumInitableElts; ++i5.83k ) { |
1222 | 6.21k | Expr *Init = ILE->getInit(i); |
1223 | 6.21k | llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType); |
1224 | 6.21k | if (!C) |
1225 | 389 | return nullptr; |
1226 | 5.83k | if (i == 0) |
1227 | 739 | CommonElementType = C->getType(); |
1228 | 5.09k | else if (C->getType() != CommonElementType) |
1229 | 4 | CommonElementType = nullptr; |
1230 | 5.83k | Elts.push_back(C); |
1231 | 5.83k | } |
1232 | | |
1233 | 758 | llvm::ArrayType *Desired = |
1234 | 758 | cast<llvm::ArrayType>(CGM.getTypes().ConvertType(ILE->getType())); |
1235 | 758 | return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts, |
1236 | 758 | fillC); |
1237 | 1.14k | } |
1238 | | |
1239 | 2.42k | llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) { |
1240 | 2.42k | return ConstStructBuilder::BuildStruct(Emitter, ILE, T); |
1241 | 2.42k | } |
1242 | | |
1243 | | llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E, |
1244 | 58 | QualType T) { |
1245 | 58 | return CGM.EmitNullConstant(T); |
1246 | 58 | } |
1247 | | |
1248 | 3.59k | llvm::Constant *VisitInitListExpr(InitListExpr *ILE, QualType T) { |
1249 | 3.59k | if (ILE->isTransparent()) |
1250 | 15 | return Visit(ILE->getInit(0), T); |
1251 | | |
1252 | 3.58k | if (ILE->getType()->isArrayType()) |
1253 | 1.15k | return EmitArrayInitialization(ILE, T); |
1254 | | |
1255 | 2.42k | if (ILE->getType()->isRecordType()) |
1256 | 2.42k | return EmitRecordInitialization(ILE, T); |
1257 | | |
1258 | 0 | return nullptr; |
1259 | 0 | } |
1260 | | |
1261 | | llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E, |
1262 | 28 | QualType destType) { |
1263 | 28 | auto C = Visit(E->getBase(), destType); |
1264 | 28 | if (!C) |
1265 | 0 | return nullptr; |
1266 | | |
1267 | 28 | ConstantAggregateBuilder Const(CGM); |
1268 | 28 | Const.add(C, CharUnits::Zero(), false); |
1269 | | |
1270 | 28 | if (!EmitDesignatedInitUpdater(Emitter, Const, CharUnits::Zero(), destType, |
1271 | 28 | E->getUpdater())) |
1272 | 0 | return nullptr; |
1273 | | |
1274 | 28 | llvm::Type *ValTy = CGM.getTypes().ConvertType(destType); |
1275 | 28 | bool HasFlexibleArray = false; |
1276 | 28 | if (auto *RT = destType->getAs<RecordType>()) |
1277 | 20 | HasFlexibleArray = RT->getDecl()->hasFlexibleArrayMember(); |
1278 | 28 | return Const.build(ValTy, HasFlexibleArray); |
1279 | 28 | } |
1280 | | |
1281 | 5.00k | llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E, QualType Ty) { |
1282 | 5.00k | if (!E->getConstructor()->isTrivial()) |
1283 | 4.79k | return nullptr; |
1284 | | |
1285 | | // Only default and copy/move constructors can be trivial. |
1286 | 212 | if (E->getNumArgs()) { |
1287 | 210 | assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); |
1288 | 210 | assert(E->getConstructor()->isCopyOrMoveConstructor() && |
1289 | 210 | "trivial ctor has argument but isn't a copy/move ctor"); |
1290 | | |
1291 | 210 | Expr *Arg = E->getArg(0); |
1292 | 210 | assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && |
1293 | 210 | "argument to copy ctor is of wrong type"); |
1294 | | |
1295 | 210 | return Visit(Arg, Ty); |
1296 | 210 | } |
1297 | | |
1298 | 2 | return CGM.EmitNullConstant(Ty); |
1299 | 2 | } |
1300 | | |
1301 | 93 | llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) { |
1302 | | // This is a string literal initializing an array in an initializer. |
1303 | 93 | return CGM.GetConstantArrayFromStringLiteral(E); |
1304 | 93 | } |
1305 | | |
1306 | 50 | llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E, QualType T) { |
1307 | | // This must be an @encode initializing an array in a static initializer. |
1308 | | // Don't emit it as the address of the string, emit the string data itself |
1309 | | // as an inline array. |
1310 | 50 | std::string Str; |
1311 | 50 | CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
1312 | 50 | const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T); |
1313 | | |
1314 | | // Resize the string to the right size, adding zeros at the end, or |
1315 | | // truncating as needed. |
1316 | 50 | Str.resize(CAT->getSize().getZExtValue(), '\0'); |
1317 | 50 | return llvm::ConstantDataArray::getString(VMContext, Str, false); |
1318 | 50 | } |
1319 | | |
1320 | 1 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) { |
1321 | 1 | return Visit(E->getSubExpr(), T); |
1322 | 1 | } |
1323 | | |
1324 | | // Utility methods |
1325 | 4 | llvm::Type *ConvertType(QualType T) { |
1326 | 4 | return CGM.getTypes().ConvertType(T); |
1327 | 4 | } |
1328 | | }; |
1329 | | |
1330 | | } // end anonymous namespace. |
1331 | | |
1332 | | llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C, |
1333 | 25.0k | AbstractState saved) { |
1334 | 25.0k | Abstract = saved.OldValue; |
1335 | | |
1336 | 25.0k | assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() && |
1337 | 25.0k | "created a placeholder while doing an abstract emission?"); |
1338 | | |
1339 | | // No validation necessary for now. |
1340 | | // No cleanup to do for now. |
1341 | 25.0k | return C; |
1342 | 25.0k | } |
1343 | | |
1344 | | llvm::Constant * |
1345 | 15.1k | ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) { |
1346 | 15.1k | auto state = pushAbstract(); |
1347 | 15.1k | auto C = tryEmitPrivateForVarInit(D); |
1348 | 15.1k | return validateAndPopAbstract(C, state); |
1349 | 15.1k | } |
1350 | | |
1351 | | llvm::Constant * |
1352 | 556 | ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) { |
1353 | 556 | auto state = pushAbstract(); |
1354 | 556 | auto C = tryEmitPrivate(E, destType); |
1355 | 556 | return validateAndPopAbstract(C, state); |
1356 | 556 | } |
1357 | | |
1358 | | llvm::Constant * |
1359 | 601 | ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) { |
1360 | 601 | auto state = pushAbstract(); |
1361 | 601 | auto C = tryEmitPrivate(value, destType); |
1362 | 601 | return validateAndPopAbstract(C, state); |
1363 | 601 | } |
1364 | | |
1365 | 595 | llvm::Constant *ConstantEmitter::tryEmitConstantExpr(const ConstantExpr *CE) { |
1366 | 595 | if (!CE->hasAPValueResult()) |
1367 | 113 | return nullptr; |
1368 | 482 | const Expr *Inner = CE->getSubExpr()->IgnoreImplicit(); |
1369 | 482 | QualType RetType = Inner->getType(); |
1370 | 482 | if (Inner->isLValue()) |
1371 | 4 | RetType = CGF->getContext().getLValueReferenceType(RetType); |
1372 | 478 | else if (Inner->isXValue()) |
1373 | 4 | RetType = CGF->getContext().getRValueReferenceType(RetType); |
1374 | 482 | llvm::Constant *Res = |
1375 | 482 | emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(), RetType); |
1376 | 482 | return Res; |
1377 | 482 | } |
1378 | | |
1379 | | llvm::Constant * |
1380 | 308 | ConstantEmitter::emitAbstract(const Expr *E, QualType destType) { |
1381 | 308 | auto state = pushAbstract(); |
1382 | 308 | auto C = tryEmitPrivate(E, destType); |
1383 | 308 | C = validateAndPopAbstract(C, state); |
1384 | 308 | if (!C) { |
1385 | 0 | CGM.Error(E->getExprLoc(), |
1386 | 0 | "internal error: could not emit constant value \"abstractly\""); |
1387 | 0 | C = CGM.EmitNullConstant(destType); |
1388 | 0 | } |
1389 | 308 | return C; |
1390 | 308 | } |
1391 | | |
1392 | | llvm::Constant * |
1393 | | ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value, |
1394 | 8.40k | QualType destType) { |
1395 | 8.40k | auto state = pushAbstract(); |
1396 | 8.40k | auto C = tryEmitPrivate(value, destType); |
1397 | 8.40k | C = validateAndPopAbstract(C, state); |
1398 | 8.40k | if (!C) { |
1399 | 0 | CGM.Error(loc, |
1400 | 0 | "internal error: could not emit constant value \"abstractly\""); |
1401 | 0 | C = CGM.EmitNullConstant(destType); |
1402 | 0 | } |
1403 | 8.40k | return C; |
1404 | 8.40k | } |
1405 | | |
1406 | 25.0k | llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) { |
1407 | 25.0k | initializeNonAbstract(D.getType().getAddressSpace()); |
1408 | 25.0k | return markIfFailed(tryEmitPrivateForVarInit(D)); |
1409 | 25.0k | } |
1410 | | |
1411 | | llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E, |
1412 | | LangAS destAddrSpace, |
1413 | 103 | QualType destType) { |
1414 | 103 | initializeNonAbstract(destAddrSpace); |
1415 | 103 | return markIfFailed(tryEmitPrivateForMemory(E, destType)); |
1416 | 103 | } |
1417 | | |
1418 | | llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value, |
1419 | | LangAS destAddrSpace, |
1420 | 156 | QualType destType) { |
1421 | 156 | initializeNonAbstract(destAddrSpace); |
1422 | 156 | auto C = tryEmitPrivateForMemory(value, destType); |
1423 | 156 | assert(C && "couldn't emit constant value non-abstractly?"); |
1424 | 156 | return C; |
1425 | 156 | } |
1426 | | |
1427 | 0 | llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() { |
1428 | 0 | assert(!Abstract && "cannot get current address for abstract constant"); |
1429 | | |
1430 | | |
1431 | | |
1432 | | // Make an obviously ill-formed global that should blow up compilation |
1433 | | // if it survives. |
1434 | 0 | auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true, |
1435 | 0 | llvm::GlobalValue::PrivateLinkage, |
1436 | 0 | /*init*/ nullptr, |
1437 | 0 | /*name*/ "", |
1438 | 0 | /*before*/ nullptr, |
1439 | 0 | llvm::GlobalVariable::NotThreadLocal, |
1440 | 0 | CGM.getContext().getTargetAddressSpace(DestAddressSpace)); |
1441 | |
|
1442 | 0 | PlaceholderAddresses.push_back(std::make_pair(nullptr, global)); |
1443 | |
|
1444 | 0 | return global; |
1445 | 0 | } |
1446 | | |
1447 | | void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal, |
1448 | 0 | llvm::GlobalValue *placeholder) { |
1449 | 0 | assert(!PlaceholderAddresses.empty()); |
1450 | 0 | assert(PlaceholderAddresses.back().first == nullptr); |
1451 | 0 | assert(PlaceholderAddresses.back().second == placeholder); |
1452 | 0 | PlaceholderAddresses.back().first = signal; |
1453 | 0 | } |
1454 | | |
1455 | | namespace { |
1456 | | struct ReplacePlaceholders { |
1457 | | CodeGenModule &CGM; |
1458 | | |
1459 | | /// The base address of the global. |
1460 | | llvm::Constant *Base; |
1461 | | llvm::Type *BaseValueTy = nullptr; |
1462 | | |
1463 | | /// The placeholder addresses that were registered during emission. |
1464 | | llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses; |
1465 | | |
1466 | | /// The locations of the placeholder signals. |
1467 | | llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations; |
1468 | | |
1469 | | /// The current index stack. We use a simple unsigned stack because |
1470 | | /// we assume that placeholders will be relatively sparse in the |
1471 | | /// initializer, but we cache the index values we find just in case. |
1472 | | llvm::SmallVector<unsigned, 8> Indices; |
1473 | | llvm::SmallVector<llvm::Constant*, 8> IndexValues; |
1474 | | |
1475 | | ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base, |
1476 | | ArrayRef<std::pair<llvm::Constant*, |
1477 | | llvm::GlobalVariable*>> addresses) |
1478 | | : CGM(CGM), Base(base), |
1479 | 0 | PlaceholderAddresses(addresses.begin(), addresses.end()) { |
1480 | 0 | } |
1481 | | |
1482 | | void replaceInInitializer(llvm::Constant *init) { |
1483 | | // Remember the type of the top-most initializer. |
1484 | | BaseValueTy = init->getType(); |
1485 | | |
1486 | | // Initialize the stack. |
1487 | | Indices.push_back(0); |
1488 | | IndexValues.push_back(nullptr); |
1489 | | |
1490 | | // Recurse into the initializer. |
1491 | | findLocations(init); |
1492 | | |
1493 | | // Check invariants. |
1494 | | assert(IndexValues.size() == Indices.size() && "mismatch"); |
1495 | | assert(Indices.size() == 1 && "didn't pop all indices"); |
1496 | | |
1497 | | // Do the replacement; this basically invalidates 'init'. |
1498 | | assert(Locations.size() == PlaceholderAddresses.size() && |
1499 | | "missed a placeholder?"); |
1500 | | |
1501 | | // We're iterating over a hashtable, so this would be a source of |
1502 | | // non-determinism in compiler output *except* that we're just |
1503 | | // messing around with llvm::Constant structures, which never itself |
1504 | | // does anything that should be visible in compiler output. |
1505 | | for (auto &entry : Locations) { |
1506 | | assert(entry.first->getParent() == nullptr && "not a placeholder!"); |
1507 | | entry.first->replaceAllUsesWith(entry.second); |
1508 | | entry.first->eraseFromParent(); |
1509 | | } |
1510 | | } |
1511 | | |
1512 | | private: |
1513 | 0 | void findLocations(llvm::Constant *init) { |
1514 | | // Recurse into aggregates. |
1515 | 0 | if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) { |
1516 | 0 | for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) { |
1517 | 0 | Indices.push_back(i); |
1518 | 0 | IndexValues.push_back(nullptr); |
1519 | |
|
1520 | 0 | findLocations(agg->getOperand(i)); |
1521 | |
|
1522 | 0 | IndexValues.pop_back(); |
1523 | 0 | Indices.pop_back(); |
1524 | 0 | } |
1525 | 0 | return; |
1526 | 0 | } |
1527 | | |
1528 | | // Otherwise, check for registered constants. |
1529 | 0 | while (true) { |
1530 | 0 | auto it = PlaceholderAddresses.find(init); |
1531 | 0 | if (it != PlaceholderAddresses.end()) { |
1532 | 0 | setLocation(it->second); |
1533 | 0 | break; |
1534 | 0 | } |
1535 | | |
1536 | | // Look through bitcasts or other expressions. |
1537 | 0 | if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) { |
1538 | 0 | init = expr->getOperand(0); |
1539 | 0 | } else { |
1540 | 0 | break; |
1541 | 0 | } |
1542 | 0 | } |
1543 | 0 | } |
1544 | | |
1545 | 0 | void setLocation(llvm::GlobalVariable *placeholder) { |
1546 | 0 | assert(Locations.find(placeholder) == Locations.end() && |
1547 | 0 | "already found location for placeholder!"); |
1548 | | |
1549 | | // Lazily fill in IndexValues with the values from Indices. |
1550 | | // We do this in reverse because we should always have a strict |
1551 | | // prefix of indices from the start. |
1552 | 0 | assert(Indices.size() == IndexValues.size()); |
1553 | 0 | for (size_t i = Indices.size() - 1; i != size_t(-1); --i) { |
1554 | 0 | if (IndexValues[i]) { |
1555 | 0 | #ifndef NDEBUG |
1556 | 0 | for (size_t j = 0; j != i + 1; ++j) { |
1557 | 0 | assert(IndexValues[j] && |
1558 | 0 | isa<llvm::ConstantInt>(IndexValues[j]) && |
1559 | 0 | cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue() |
1560 | 0 | == Indices[j]); |
1561 | 0 | } |
1562 | 0 | #endif |
1563 | 0 | break; |
1564 | 0 | } |
1565 | | |
1566 | 0 | IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]); |
1567 | 0 | } |
1568 | | |
1569 | | // Form a GEP and then bitcast to the placeholder type so that the |
1570 | | // replacement will succeed. |
1571 | 0 | llvm::Constant *location = |
1572 | 0 | llvm::ConstantExpr::getInBoundsGetElementPtr(BaseValueTy, |
1573 | 0 | Base, IndexValues); |
1574 | 0 | location = llvm::ConstantExpr::getBitCast(location, |
1575 | 0 | placeholder->getType()); |
1576 | |
|
1577 | 0 | Locations.insert({placeholder, location}); |
1578 | 0 | } |
1579 | | }; |
1580 | | } |
1581 | | |
1582 | 17.5k | void ConstantEmitter::finalize(llvm::GlobalVariable *global) { |
1583 | 17.5k | assert(InitializedNonAbstract && |
1584 | 17.5k | "finalizing emitter that was used for abstract emission?"); |
1585 | 17.5k | assert(!Finalized && "finalizing emitter multiple times"); |
1586 | 17.5k | assert(global->getInitializer()); |
1587 | | |
1588 | | // Note that we might also be Failed. |
1589 | 17.5k | Finalized = true; |
1590 | | |
1591 | 17.5k | if (!PlaceholderAddresses.empty()) { |
1592 | 0 | ReplacePlaceholders(CGM, global, PlaceholderAddresses) |
1593 | 0 | .replaceInInitializer(global->getInitializer()); |
1594 | 0 | PlaceholderAddresses.clear(); // satisfy |
1595 | 0 | } |
1596 | 17.5k | } |
1597 | | |
1598 | 49.7k | ConstantEmitter::~ConstantEmitter() { |
1599 | 49.7k | assert((!InitializedNonAbstract || Finalized || Failed) && |
1600 | 49.7k | "not finalized after being initialized for non-abstract emission"); |
1601 | 49.7k | assert(PlaceholderAddresses.empty() && "unhandled placeholders"); |
1602 | 49.7k | } |
1603 | | |
1604 | 63.2k | static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) { |
1605 | 63.2k | if (auto AT = type->getAs<AtomicType>()) { |
1606 | 98 | return CGM.getContext().getQualifiedType(AT->getValueType(), |
1607 | 98 | type.getQualifiers()); |
1608 | 98 | } |
1609 | 63.1k | return type; |
1610 | 63.1k | } |
1611 | | |
1612 | 40.2k | llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) { |
1613 | | // Make a quick check if variable can be default NULL initialized |
1614 | | // and avoid going through rest of code which may do, for c++11, |
1615 | | // initialization of memory to all NULLs. |
1616 | 40.2k | if (!D.hasLocalStorage()) { |
1617 | 25.0k | QualType Ty = CGM.getContext().getBaseElementType(D.getType()); |
1618 | 25.0k | if (Ty->isRecordType()) |
1619 | 7.19k | if (const CXXConstructExpr *E = |
1620 | 6.12k | dyn_cast_or_null<CXXConstructExpr>(D.getInit())) { |
1621 | 6.12k | const CXXConstructorDecl *CD = E->getConstructor(); |
1622 | 6.12k | if (CD->isTrivial() && CD->isDefaultConstructor()1.19k ) |
1623 | 1.16k | return CGM.EmitNullConstant(D.getType()); |
1624 | 39.1k | } |
1625 | 25.0k | } |
1626 | 39.1k | InConstantContext = D.hasConstantInitialization(); |
1627 | | |
1628 | 39.1k | QualType destType = D.getType(); |
1629 | | |
1630 | | // Try to emit the initializer. Note that this can allow some things that |
1631 | | // are not allowed by tryEmitPrivateForMemory alone. |
1632 | 39.1k | if (auto value = D.evaluateValue()) { |
1633 | 13.5k | return tryEmitPrivateForMemory(*value, destType); |
1634 | 13.5k | } |
1635 | | |
1636 | | // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a |
1637 | | // reference is a constant expression, and the reference binds to a temporary, |
1638 | | // then constant initialization is performed. ConstExprEmitter will |
1639 | | // incorrectly emit a prvalue constant in this case, and the calling code |
1640 | | // interprets that as the (pointer) value of the reference, rather than the |
1641 | | // desired value of the referee. |
1642 | 25.6k | if (destType->isReferenceType()) |
1643 | 3.38k | return nullptr; |
1644 | | |
1645 | 22.2k | const Expr *E = D.getInit(); |
1646 | 22.2k | assert(E && "No initializer to emit"); |
1647 | | |
1648 | 22.2k | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1649 | 22.2k | auto C = |
1650 | 22.2k | ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType); |
1651 | 19.8k | return (C ? emitForMemory(C, destType)2.31k : nullptr); |
1652 | 22.2k | } |
1653 | | |
1654 | | llvm::Constant * |
1655 | 144 | ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) { |
1656 | 144 | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1657 | 144 | auto C = tryEmitAbstract(E, nonMemoryDestType); |
1658 | 133 | return (C ? emitForMemory(C, destType) : nullptr11 ); |
1659 | 144 | } |
1660 | | |
1661 | | llvm::Constant * |
1662 | | ConstantEmitter::tryEmitAbstractForMemory(const APValue &value, |
1663 | 601 | QualType destType) { |
1664 | 601 | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1665 | 601 | auto C = tryEmitAbstract(value, nonMemoryDestType); |
1666 | 601 | return (C ? emitForMemory(C, destType) : nullptr0 ); |
1667 | 601 | } |
1668 | | |
1669 | | llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E, |
1670 | 11.5k | QualType destType) { |
1671 | 11.5k | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1672 | 11.5k | llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType); |
1673 | 11.0k | return (C ? emitForMemory(C, destType) : nullptr474 ); |
1674 | 11.5k | } |
1675 | | |
1676 | | llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value, |
1677 | 28.6k | QualType destType) { |
1678 | 28.6k | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1679 | 28.6k | auto C = tryEmitPrivate(value, nonMemoryDestType); |
1680 | 28.6k | return (C ? emitForMemory(C, destType) : nullptr0 ); |
1681 | 28.6k | } |
1682 | | |
1683 | | llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM, |
1684 | | llvm::Constant *C, |
1685 | 42.9k | QualType destType) { |
1686 | | // For an _Atomic-qualified constant, we may need to add tail padding. |
1687 | 42.9k | if (auto AT = destType->getAs<AtomicType>()) { |
1688 | 94 | QualType destValueType = AT->getValueType(); |
1689 | 94 | C = emitForMemory(CGM, C, destValueType); |
1690 | | |
1691 | 94 | uint64_t innerSize = CGM.getContext().getTypeSize(destValueType); |
1692 | 94 | uint64_t outerSize = CGM.getContext().getTypeSize(destType); |
1693 | 94 | if (innerSize == outerSize) |
1694 | 93 | return C; |
1695 | | |
1696 | 1 | assert(innerSize < outerSize && "emitted over-large constant for atomic"); |
1697 | 1 | llvm::Constant *elts[] = { |
1698 | 1 | C, |
1699 | 1 | llvm::ConstantAggregateZero::get( |
1700 | 1 | llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8)) |
1701 | 1 | }; |
1702 | 1 | return llvm::ConstantStruct::getAnon(elts); |
1703 | 1 | } |
1704 | | |
1705 | | // Zero-extend bool. |
1706 | 42.8k | if (C->getType()->isIntegerTy(1)) { |
1707 | 384 | llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType); |
1708 | 384 | return llvm::ConstantExpr::getZExt(C, boolTy); |
1709 | 384 | } |
1710 | | |
1711 | 42.4k | return C; |
1712 | 42.4k | } |
1713 | | |
1714 | | llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E, |
1715 | 12.4k | QualType destType) { |
1716 | 12.4k | Expr::EvalResult Result; |
1717 | | |
1718 | 12.4k | bool Success = false; |
1719 | | |
1720 | 12.4k | if (destType->isReferenceType()) |
1721 | 10 | Success = E->EvaluateAsLValue(Result, CGM.getContext()); |
1722 | 12.4k | else |
1723 | 12.4k | Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext); |
1724 | | |
1725 | 12.4k | llvm::Constant *C; |
1726 | 12.4k | if (Success && !Result.HasSideEffects10.7k ) |
1727 | 10.7k | C = tryEmitPrivate(Result.Val, destType); |
1728 | 1.72k | else |
1729 | 1.72k | C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType); |
1730 | | |
1731 | 12.4k | return C; |
1732 | 12.4k | } |
1733 | | |
1734 | 30.6k | llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) { |
1735 | 30.6k | return getTargetCodeGenInfo().getNullPointer(*this, T, QT); |
1736 | 30.6k | } |
1737 | | |
1738 | | namespace { |
1739 | | /// A struct which can be used to peephole certain kinds of finalization |
1740 | | /// that normally happen during l-value emission. |
1741 | | struct ConstantLValue { |
1742 | | llvm::Constant *Value; |
1743 | | bool HasOffsetApplied; |
1744 | | |
1745 | | /*implicit*/ ConstantLValue(llvm::Constant *value, |
1746 | | bool hasOffsetApplied = false) |
1747 | 7.27k | : Value(value), HasOffsetApplied(hasOffsetApplied) {} |
1748 | | |
1749 | | /*implicit*/ ConstantLValue(ConstantAddress address) |
1750 | 3.95k | : ConstantLValue(address.getPointer()) {} |
1751 | | }; |
1752 | | |
1753 | | /// A helper class for emitting constant l-values. |
1754 | | class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter, |
1755 | | ConstantLValue> { |
1756 | | CodeGenModule &CGM; |
1757 | | ConstantEmitter &Emitter; |
1758 | | const APValue &Value; |
1759 | | QualType DestType; |
1760 | | |
1761 | | // Befriend StmtVisitorBase so that we don't have to expose Visit*. |
1762 | | friend StmtVisitorBase; |
1763 | | |
1764 | | public: |
1765 | | ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value, |
1766 | | QualType destType) |
1767 | 8.00k | : CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType) {} |
1768 | | |
1769 | | llvm::Constant *tryEmit(); |
1770 | | |
1771 | | private: |
1772 | | llvm::Constant *tryEmitAbsolute(llvm::Type *destTy); |
1773 | | ConstantLValue tryEmitBase(const APValue::LValueBase &base); |
1774 | | |
1775 | 0 | ConstantLValue VisitStmt(const Stmt *S) { return nullptr; } |
1776 | | ConstantLValue VisitConstantExpr(const ConstantExpr *E); |
1777 | | ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
1778 | | ConstantLValue VisitStringLiteral(const StringLiteral *E); |
1779 | | ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *E); |
1780 | | ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
1781 | | ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E); |
1782 | | ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E); |
1783 | | ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E); |
1784 | | ConstantLValue VisitCallExpr(const CallExpr *E); |
1785 | | ConstantLValue VisitBlockExpr(const BlockExpr *E); |
1786 | | ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
1787 | | ConstantLValue VisitMaterializeTemporaryExpr( |
1788 | | const MaterializeTemporaryExpr *E); |
1789 | | |
1790 | 7.27k | bool hasNonZeroOffset() const { |
1791 | 7.27k | return !Value.getLValueOffset().isZero(); |
1792 | 7.27k | } |
1793 | | |
1794 | | /// Return the value offset. |
1795 | 269 | llvm::Constant *getOffset() { |
1796 | 269 | return llvm::ConstantInt::get(CGM.Int64Ty, |
1797 | 269 | Value.getLValueOffset().getQuantity()); |
1798 | 269 | } |
1799 | | |
1800 | | /// Apply the value offset to the given constant. |
1801 | 7.27k | llvm::Constant *applyOffset(llvm::Constant *C) { |
1802 | 7.27k | if (!hasNonZeroOffset()) |
1803 | 7.21k | return C; |
1804 | | |
1805 | 59 | llvm::Type *origPtrTy = C->getType(); |
1806 | 59 | unsigned AS = origPtrTy->getPointerAddressSpace(); |
1807 | 59 | llvm::Type *charPtrTy = CGM.Int8Ty->getPointerTo(AS); |
1808 | 59 | C = llvm::ConstantExpr::getBitCast(C, charPtrTy); |
1809 | 59 | C = llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset()); |
1810 | 59 | C = llvm::ConstantExpr::getPointerCast(C, origPtrTy); |
1811 | 59 | return C; |
1812 | 59 | } |
1813 | | }; |
1814 | | |
1815 | | } |
1816 | | |
1817 | 8.00k | llvm::Constant *ConstantLValueEmitter::tryEmit() { |
1818 | 8.00k | const APValue::LValueBase &base = Value.getLValueBase(); |
1819 | | |
1820 | | // The destination type should be a pointer or reference |
1821 | | // type, but it might also be a cast thereof. |
1822 | | // |
1823 | | // FIXME: the chain of casts required should be reflected in the APValue. |
1824 | | // We need this in order to correctly handle things like a ptrtoint of a |
1825 | | // non-zero null pointer and addrspace casts that aren't trivially |
1826 | | // represented in LLVM IR. |
1827 | 8.00k | auto destTy = CGM.getTypes().ConvertTypeForMem(DestType); |
1828 | 8.00k | assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy)); |
1829 | | |
1830 | | // If there's no base at all, this is a null or absolute pointer, |
1831 | | // possibly cast back to an integer type. |
1832 | 8.00k | if (!base) { |
1833 | 735 | return tryEmitAbsolute(destTy); |
1834 | 735 | } |
1835 | | |
1836 | | // Otherwise, try to emit the base. |
1837 | 7.27k | ConstantLValue result = tryEmitBase(base); |
1838 | | |
1839 | | // If that failed, we're done. |
1840 | 7.27k | llvm::Constant *value = result.Value; |
1841 | 7.27k | if (!value) return nullptr0 ; |
1842 | | |
1843 | | // Apply the offset if necessary and not already done. |
1844 | 7.27k | if (!result.HasOffsetApplied) { |
1845 | 7.27k | value = applyOffset(value); |
1846 | 7.27k | } |
1847 | | |
1848 | | // Convert to the appropriate type; this could be an lvalue for |
1849 | | // an integer. FIXME: performAddrSpaceCast |
1850 | 7.27k | if (isa<llvm::PointerType>(destTy)) |
1851 | 7.25k | return llvm::ConstantExpr::getPointerCast(value, destTy); |
1852 | | |
1853 | 19 | return llvm::ConstantExpr::getPtrToInt(value, destTy); |
1854 | 19 | } |
1855 | | |
1856 | | /// Try to emit an absolute l-value, such as a null pointer or an integer |
1857 | | /// bitcast to pointer type. |
1858 | | llvm::Constant * |
1859 | 735 | ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) { |
1860 | | // If we're producing a pointer, this is easy. |
1861 | 735 | auto destPtrTy = cast<llvm::PointerType>(destTy); |
1862 | 735 | if (Value.isNullPointer()) { |
1863 | | // FIXME: integer offsets from non-zero null pointers. |
1864 | 525 | return CGM.getNullPointer(destPtrTy, DestType); |
1865 | 525 | } |
1866 | | |
1867 | | // Convert the integer to a pointer-sized integer before converting it |
1868 | | // to a pointer. |
1869 | | // FIXME: signedness depends on the original integer type. |
1870 | 210 | auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy); |
1871 | 210 | llvm::Constant *C; |
1872 | 210 | C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy, |
1873 | 210 | /*isSigned*/ false); |
1874 | 210 | C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy); |
1875 | 210 | return C; |
1876 | 210 | } |
1877 | | |
1878 | | ConstantLValue |
1879 | 7.27k | ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) { |
1880 | | // Handle values. |
1881 | 7.27k | if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) { |
1882 | | // The constant always points to the canonical declaration. We want to look |
1883 | | // at properties of the most recent declaration at the point of emission. |
1884 | 3.10k | D = cast<ValueDecl>(D->getMostRecentDecl()); |
1885 | | |
1886 | 3.10k | if (D->hasAttr<WeakRefAttr>()) |
1887 | 5 | return CGM.GetWeakRefReference(D).getPointer(); |
1888 | | |
1889 | 3.09k | if (auto FD = dyn_cast<FunctionDecl>(D)) |
1890 | 2.26k | return CGM.GetAddrOfFunction(FD); |
1891 | | |
1892 | 833 | if (auto VD = dyn_cast<VarDecl>(D)) { |
1893 | | // We can never refer to a variable with local storage. |
1894 | 793 | if (!VD->hasLocalStorage()) { |
1895 | 793 | if (VD->isFileVarDecl() || VD->hasExternalStorage()16 ) |
1896 | 778 | return CGM.GetAddrOfGlobalVar(VD); |
1897 | | |
1898 | 15 | if (VD->isLocalVarDecl()) { |
1899 | 15 | return CGM.getOrCreateStaticVarDecl( |
1900 | 15 | *VD, CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false)); |
1901 | 15 | } |
1902 | 40 | } |
1903 | 793 | } |
1904 | | |
1905 | 40 | if (auto *GD = dyn_cast<MSGuidDecl>(D)) |
1906 | 36 | return CGM.GetAddrOfMSGuidDecl(GD); |
1907 | | |
1908 | 4 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) |
1909 | 4 | return CGM.GetAddrOfTemplateParamObject(TPO); |
1910 | | |
1911 | 0 | return nullptr; |
1912 | 0 | } |
1913 | | |
1914 | | // Handle typeid(T). |
1915 | 4.16k | if (TypeInfoLValue TI = base.dyn_cast<TypeInfoLValue>()) { |
1916 | 142 | llvm::Type *StdTypeInfoPtrTy = |
1917 | 142 | CGM.getTypes().ConvertType(base.getTypeInfoType())->getPointerTo(); |
1918 | 142 | llvm::Constant *TypeInfo = |
1919 | 142 | CGM.GetAddrOfRTTIDescriptor(QualType(TI.getType(), 0)); |
1920 | 142 | if (TypeInfo->getType() != StdTypeInfoPtrTy) |
1921 | 142 | TypeInfo = llvm::ConstantExpr::getBitCast(TypeInfo, StdTypeInfoPtrTy); |
1922 | 142 | return TypeInfo; |
1923 | 142 | } |
1924 | | |
1925 | | // Otherwise, it must be an expression. |
1926 | 4.02k | return Visit(base.get<const Expr*>()); |
1927 | 4.02k | } |
1928 | | |
1929 | | ConstantLValue |
1930 | 0 | ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) { |
1931 | 0 | if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(E)) |
1932 | 0 | return Result; |
1933 | 0 | return Visit(E->getSubExpr()); |
1934 | 0 | } |
1935 | | |
1936 | | ConstantLValue |
1937 | 14 | ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
1938 | 14 | return tryEmitGlobalCompoundLiteral(CGM, Emitter.CGF, E); |
1939 | 14 | } |
1940 | | |
1941 | | ConstantLValue |
1942 | 3.06k | ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) { |
1943 | 3.06k | return CGM.GetAddrOfConstantStringFromLiteral(E); |
1944 | 3.06k | } |
1945 | | |
1946 | | ConstantLValue |
1947 | 9 | ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
1948 | 9 | return CGM.GetAddrOfConstantStringFromObjCEncode(E); |
1949 | 9 | } |
1950 | | |
1951 | | static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S, |
1952 | | QualType T, |
1953 | 316 | CodeGenModule &CGM) { |
1954 | 316 | auto C = CGM.getObjCRuntime().GenerateConstantString(S); |
1955 | 316 | return C.getElementBitCast(CGM.getTypes().ConvertTypeForMem(T)); |
1956 | 316 | } |
1957 | | |
1958 | | ConstantLValue |
1959 | 313 | ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
1960 | 313 | return emitConstantObjCStringLiteral(E->getString(), E->getType(), CGM); |
1961 | 313 | } |
1962 | | |
1963 | | ConstantLValue |
1964 | 3 | ConstantLValueEmitter::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
1965 | 3 | assert(E->isExpressibleAsConstantInitializer() && |
1966 | 3 | "this boxed expression can't be emitted as a compile-time constant"); |
1967 | 3 | auto *SL = cast<StringLiteral>(E->getSubExpr()->IgnoreParenCasts()); |
1968 | 3 | return emitConstantObjCStringLiteral(SL, E->getType(), CGM); |
1969 | 3 | } |
1970 | | |
1971 | | ConstantLValue |
1972 | 3 | ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) { |
1973 | 3 | return CGM.GetAddrOfConstantStringFromLiteral(E->getFunctionName()); |
1974 | 3 | } |
1975 | | |
1976 | | ConstantLValue |
1977 | 36 | ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
1978 | 36 | assert(Emitter.CGF && "Invalid address of label expression outside function"); |
1979 | 36 | llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel()); |
1980 | 36 | Ptr = llvm::ConstantExpr::getBitCast(Ptr, |
1981 | 36 | CGM.getTypes().ConvertType(E->getType())); |
1982 | 36 | return Ptr; |
1983 | 36 | } |
1984 | | |
1985 | | ConstantLValue |
1986 | 365 | ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) { |
1987 | 365 | unsigned builtin = E->getBuiltinCallee(); |
1988 | 365 | if (builtin != Builtin::BI__builtin___CFStringMakeConstantString && |
1989 | 0 | builtin != Builtin::BI__builtin___NSStringMakeConstantString) |
1990 | 0 | return nullptr; |
1991 | | |
1992 | 365 | auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts()); |
1993 | 365 | if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) { |
1994 | 0 | return CGM.getObjCRuntime().GenerateConstantString(literal); |
1995 | 365 | } else { |
1996 | | // FIXME: need to deal with UCN conversion issues. |
1997 | 365 | return CGM.GetAddrOfConstantCFString(literal); |
1998 | 365 | } |
1999 | 365 | } |
2000 | | |
2001 | | ConstantLValue |
2002 | 80 | ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) { |
2003 | 80 | StringRef functionName; |
2004 | 80 | if (auto CGF = Emitter.CGF) |
2005 | 38 | functionName = CGF->CurFn->getName(); |
2006 | 42 | else |
2007 | 42 | functionName = "global"; |
2008 | | |
2009 | 80 | return CGM.GetAddrOfGlobalBlock(E, functionName); |
2010 | 80 | } |
2011 | | |
2012 | | ConstantLValue |
2013 | 0 | ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
2014 | 0 | QualType T; |
2015 | 0 | if (E->isTypeOperand()) |
2016 | 0 | T = E->getTypeOperand(CGM.getContext()); |
2017 | 0 | else |
2018 | 0 | T = E->getExprOperand()->getType(); |
2019 | 0 | return CGM.GetAddrOfRTTIDescriptor(T); |
2020 | 0 | } |
2021 | | |
2022 | | ConstantLValue |
2023 | | ConstantLValueEmitter::VisitMaterializeTemporaryExpr( |
2024 | 141 | const MaterializeTemporaryExpr *E) { |
2025 | 141 | assert(E->getStorageDuration() == SD_Static); |
2026 | 141 | SmallVector<const Expr *, 2> CommaLHSs; |
2027 | 141 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
2028 | 141 | const Expr *Inner = |
2029 | 141 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
2030 | 141 | return CGM.GetAddrOfGlobalTemporary(E, Inner); |
2031 | 141 | } |
2032 | | |
2033 | | llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value, |
2034 | 48.4k | QualType DestType) { |
2035 | 48.4k | switch (Value.getKind()) { |
2036 | 0 | case APValue::None: |
2037 | 0 | case APValue::Indeterminate: |
2038 | | // Out-of-lifetime and indeterminate values can be modeled as 'undef'. |
2039 | 0 | return llvm::UndefValue::get(CGM.getTypes().ConvertType(DestType)); |
2040 | 8.00k | case APValue::LValue: |
2041 | 8.00k | return ConstantLValueEmitter(*this, Value, DestType).tryEmit(); |
2042 | 29.3k | case APValue::Int: |
2043 | 29.3k | return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt()); |
2044 | 344 | case APValue::FixedPoint: |
2045 | 344 | return llvm::ConstantInt::get(CGM.getLLVMContext(), |
2046 | 344 | Value.getFixedPoint().getValue()); |
2047 | 8 | case APValue::ComplexInt: { |
2048 | 8 | llvm::Constant *Complex[2]; |
2049 | | |
2050 | 8 | Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(), |
2051 | 8 | Value.getComplexIntReal()); |
2052 | 8 | Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(), |
2053 | 8 | Value.getComplexIntImag()); |
2054 | | |
2055 | | // FIXME: the target may want to specify that this is packed. |
2056 | 8 | llvm::StructType *STy = |
2057 | 8 | llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType()); |
2058 | 8 | return llvm::ConstantStruct::get(STy, Complex); |
2059 | 0 | } |
2060 | 2.79k | case APValue::Float: { |
2061 | 2.79k | const llvm::APFloat &Init = Value.getFloat(); |
2062 | 2.79k | if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() && |
2063 | 87 | !CGM.getContext().getLangOpts().NativeHalfType && |
2064 | 75 | CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics()) |
2065 | 1 | return llvm::ConstantInt::get(CGM.getLLVMContext(), |
2066 | 1 | Init.bitcastToAPInt()); |
2067 | 2.79k | else |
2068 | 2.79k | return llvm::ConstantFP::get(CGM.getLLVMContext(), Init); |
2069 | 0 | } |
2070 | 41 | case APValue::ComplexFloat: { |
2071 | 41 | llvm::Constant *Complex[2]; |
2072 | | |
2073 | 41 | Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(), |
2074 | 41 | Value.getComplexFloatReal()); |
2075 | 41 | Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(), |
2076 | 41 | Value.getComplexFloatImag()); |
2077 | | |
2078 | | // FIXME: the target may want to specify that this is packed. |
2079 | 41 | llvm::StructType *STy = |
2080 | 41 | llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType()); |
2081 | 41 | return llvm::ConstantStruct::get(STy, Complex); |
2082 | 0 | } |
2083 | 1.34k | case APValue::Vector: { |
2084 | 1.34k | unsigned NumElts = Value.getVectorLength(); |
2085 | 1.34k | SmallVector<llvm::Constant *, 4> Inits(NumElts); |
2086 | | |
2087 | 10.0k | for (unsigned I = 0; I != NumElts; ++I8.70k ) { |
2088 | 8.70k | const APValue &Elt = Value.getVectorElt(I); |
2089 | 8.70k | if (Elt.isInt()) |
2090 | 7.60k | Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt()); |
2091 | 1.10k | else if (Elt.isFloat()) |
2092 | 1.10k | Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat()); |
2093 | 1.10k | else |
2094 | 0 | llvm_unreachable("unsupported vector element type"); |
2095 | 8.70k | } |
2096 | 1.34k | return llvm::ConstantVector::get(Inits); |
2097 | 1.34k | } |
2098 | 5 | case APValue::AddrLabelDiff: { |
2099 | 5 | const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS(); |
2100 | 5 | const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS(); |
2101 | 5 | llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType()); |
2102 | 5 | llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType()); |
2103 | 5 | if (!LHS || !RHS) return nullptr0 ; |
2104 | | |
2105 | | // Compute difference |
2106 | 5 | llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType); |
2107 | 5 | LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy); |
2108 | 5 | RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy); |
2109 | 5 | llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS); |
2110 | | |
2111 | | // LLVM is a bit sensitive about the exact format of the |
2112 | | // address-of-label difference; make sure to truncate after |
2113 | | // the subtraction. |
2114 | 5 | return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType); |
2115 | 5 | } |
2116 | 2.89k | case APValue::Struct: |
2117 | 3.35k | case APValue::Union: |
2118 | 3.35k | return ConstStructBuilder::BuildStruct(*this, Value, DestType); |
2119 | 2.98k | case APValue::Array: { |
2120 | 2.98k | const ArrayType *ArrayTy = CGM.getContext().getAsArrayType(DestType); |
2121 | 2.98k | unsigned NumElements = Value.getArraySize(); |
2122 | 2.98k | unsigned NumInitElts = Value.getArrayInitializedElts(); |
2123 | | |
2124 | | // Emit array filler, if there is one. |
2125 | 2.98k | llvm::Constant *Filler = nullptr; |
2126 | 2.98k | if (Value.hasArrayFiller()) { |
2127 | 601 | Filler = tryEmitAbstractForMemory(Value.getArrayFiller(), |
2128 | 601 | ArrayTy->getElementType()); |
2129 | 601 | if (!Filler) |
2130 | 0 | return nullptr; |
2131 | 2.98k | } |
2132 | | |
2133 | | // Emit initializer elements. |
2134 | 2.98k | SmallVector<llvm::Constant*, 16> Elts; |
2135 | 2.98k | if (Filler && Filler->isNullValue()601 ) |
2136 | 593 | Elts.reserve(NumInitElts + 1); |
2137 | 2.39k | else |
2138 | 2.39k | Elts.reserve(NumElements); |
2139 | | |
2140 | 2.98k | llvm::Type *CommonElementType = nullptr; |
2141 | 12.3k | for (unsigned I = 0; I < NumInitElts; ++I9.40k ) { |
2142 | 9.40k | llvm::Constant *C = tryEmitPrivateForMemory( |
2143 | 9.40k | Value.getArrayInitializedElt(I), ArrayTy->getElementType()); |
2144 | 9.40k | if (!C) return nullptr0 ; |
2145 | | |
2146 | 9.40k | if (I == 0) |
2147 | 2.75k | CommonElementType = C->getType(); |
2148 | 6.65k | else if (C->getType() != CommonElementType) |
2149 | 11 | CommonElementType = nullptr; |
2150 | 9.40k | Elts.push_back(C); |
2151 | 9.40k | } |
2152 | | |
2153 | 2.98k | llvm::ArrayType *Desired = |
2154 | 2.98k | cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType)); |
2155 | 2.98k | return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts, |
2156 | 2.98k | Filler); |
2157 | 2.98k | } |
2158 | 272 | case APValue::MemberPointer: |
2159 | 272 | return CGM.getCXXABI().EmitMemberPointer(Value, DestType); |
2160 | 0 | } |
2161 | 0 | llvm_unreachable("Unknown APValue kind"); |
2162 | 0 | } |
2163 | | |
2164 | | llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted( |
2165 | 17 | const CompoundLiteralExpr *E) { |
2166 | 17 | return EmittedCompoundLiterals.lookup(E); |
2167 | 17 | } |
2168 | | |
2169 | | void CodeGenModule::setAddrOfConstantCompoundLiteral( |
2170 | 15 | const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) { |
2171 | 15 | bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second; |
2172 | 15 | (void)Ok; |
2173 | 15 | assert(Ok && "CLE has already been emitted!"); |
2174 | 15 | } |
2175 | | |
2176 | | ConstantAddress |
2177 | 3 | CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) { |
2178 | 3 | assert(E->isFileScope() && "not a file-scope compound literal expr"); |
2179 | 3 | return tryEmitGlobalCompoundLiteral(*this, nullptr, E); |
2180 | 3 | } |
2181 | | |
2182 | | llvm::Constant * |
2183 | 615 | CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) { |
2184 | | // Member pointer constants always have a very particular form. |
2185 | 615 | const MemberPointerType *type = cast<MemberPointerType>(uo->getType()); |
2186 | 615 | const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl(); |
2187 | | |
2188 | | // A member function pointer. |
2189 | 615 | if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl)) |
2190 | 558 | return getCXXABI().EmitMemberFunctionPointer(method); |
2191 | | |
2192 | | // Otherwise, a member data pointer. |
2193 | 57 | uint64_t fieldOffset = getContext().getFieldOffset(decl); |
2194 | 57 | CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset); |
2195 | 57 | return getCXXABI().EmitMemberDataPointer(type, chars); |
2196 | 57 | } |
2197 | | |
2198 | | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
2199 | | llvm::Type *baseType, |
2200 | | const CXXRecordDecl *base); |
2201 | | |
2202 | | static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, |
2203 | | const RecordDecl *record, |
2204 | 69 | bool asCompleteObject) { |
2205 | 69 | const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record); |
2206 | 69 | llvm::StructType *structure = |
2207 | 42 | (asCompleteObject ? layout.getLLVMType() |
2208 | 27 | : layout.getBaseSubobjectLLVMType()); |
2209 | | |
2210 | 69 | unsigned numElements = structure->getNumElements(); |
2211 | 69 | std::vector<llvm::Constant *> elements(numElements); |
2212 | | |
2213 | 69 | auto CXXR = dyn_cast<CXXRecordDecl>(record); |
2214 | | // Fill in all the bases. |
2215 | 69 | if (CXXR) { |
2216 | 25 | for (const auto &I : CXXR->bases()) { |
2217 | 25 | if (I.isVirtual()) { |
2218 | | // Ignore virtual bases; if we're laying out for a complete |
2219 | | // object, we'll lay these out later. |
2220 | 17 | continue; |
2221 | 17 | } |
2222 | | |
2223 | 8 | const CXXRecordDecl *base = |
2224 | 8 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
2225 | | |
2226 | | // Ignore empty bases. |
2227 | 8 | if (base->isEmpty() || |
2228 | 8 | CGM.getContext().getASTRecordLayout(base).getNonVirtualSize() |
2229 | 8 | .isZero()) |
2230 | 1 | continue; |
2231 | | |
2232 | 7 | unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base); |
2233 | 7 | llvm::Type *baseType = structure->getElementType(fieldIndex); |
2234 | 7 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
2235 | 7 | } |
2236 | 53 | } |
2237 | | |
2238 | | // Fill in all the fields. |
2239 | 139 | for (const auto *Field : record->fields()) { |
2240 | | // Fill in non-bitfields. (Bitfields always use a zero pattern, which we |
2241 | | // will fill in later.) |
2242 | 139 | if (!Field->isBitField() && !Field->isZeroSize(CGM.getContext())137 ) { |
2243 | 137 | unsigned fieldIndex = layout.getLLVMFieldNo(Field); |
2244 | 137 | elements[fieldIndex] = CGM.EmitNullConstant(Field->getType()); |
2245 | 137 | } |
2246 | | |
2247 | | // For unions, stop after the first named field. |
2248 | 139 | if (record->isUnion()) { |
2249 | 4 | if (Field->getIdentifier()) |
2250 | 3 | break; |
2251 | 1 | if (const auto *FieldRD = Field->getType()->getAsRecordDecl()) |
2252 | 1 | if (FieldRD->findFirstNamedDataMember()) |
2253 | 1 | break; |
2254 | 1 | } |
2255 | 139 | } |
2256 | | |
2257 | | // Fill in the virtual bases, if we're working with the complete object. |
2258 | 69 | if (CXXR && asCompleteObject53 ) { |
2259 | 5 | for (const auto &I : CXXR->vbases()) { |
2260 | 5 | const CXXRecordDecl *base = |
2261 | 5 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
2262 | | |
2263 | | // Ignore empty bases. |
2264 | 5 | if (base->isEmpty()) |
2265 | 0 | continue; |
2266 | | |
2267 | 5 | unsigned fieldIndex = layout.getVirtualBaseIndex(base); |
2268 | | |
2269 | | // We might have already laid this field out. |
2270 | 5 | if (elements[fieldIndex]) continue0 ; |
2271 | | |
2272 | 5 | llvm::Type *baseType = structure->getElementType(fieldIndex); |
2273 | 5 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
2274 | 5 | } |
2275 | 26 | } |
2276 | | |
2277 | | // Now go through all other fields and zero them out. |
2278 | 247 | for (unsigned i = 0; i != numElements; ++i178 ) { |
2279 | 178 | if (!elements[i]) |
2280 | 29 | elements[i] = llvm::Constant::getNullValue(structure->getElementType(i)); |
2281 | 178 | } |
2282 | | |
2283 | 69 | return llvm::ConstantStruct::get(structure, elements); |
2284 | 69 | } |
2285 | | |
2286 | | /// Emit the null constant for a base subobject. |
2287 | | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
2288 | | llvm::Type *baseType, |
2289 | 12 | const CXXRecordDecl *base) { |
2290 | 12 | const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base); |
2291 | | |
2292 | | // Just zero out bases that don't have any pointer to data members. |
2293 | 12 | if (baseLayout.isZeroInitializableAsBase()) |
2294 | 4 | return llvm::Constant::getNullValue(baseType); |
2295 | | |
2296 | | // Otherwise, we can just use its null constant. |
2297 | 8 | return EmitNullConstant(CGM, base, /*asCompleteObject=*/false); |
2298 | 8 | } |
2299 | | |
2300 | | llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM, |
2301 | 57 | QualType T) { |
2302 | 57 | return emitForMemory(CGM, CGM.EmitNullConstant(T), T); |
2303 | 57 | } |
2304 | | |
2305 | 35.5k | llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { |
2306 | 35.5k | if (T->getAs<PointerType>()) |
2307 | 8.60k | return getNullPointer( |
2308 | 8.60k | cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T); |
2309 | | |
2310 | 26.9k | if (getTypes().isZeroInitializable(T)) |
2311 | 26.8k | return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); |
2312 | | |
2313 | 146 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { |
2314 | 11 | llvm::ArrayType *ATy = |
2315 | 11 | cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); |
2316 | | |
2317 | 11 | QualType ElementTy = CAT->getElementType(); |
2318 | | |
2319 | 11 | llvm::Constant *Element = |
2320 | 11 | ConstantEmitter::emitNullForMemory(*this, ElementTy); |
2321 | 11 | unsigned NumElements = CAT->getSize().getZExtValue(); |
2322 | 11 | SmallVector<llvm::Constant *, 8> Array(NumElements, Element); |
2323 | 11 | return llvm::ConstantArray::get(ATy, Array); |
2324 | 11 | } |
2325 | | |
2326 | 135 | if (const RecordType *RT = T->getAs<RecordType>()) |
2327 | 42 | return ::EmitNullConstant(*this, RT->getDecl(), /*complete object*/ true); |
2328 | | |
2329 | 93 | assert(T->isMemberDataPointerType() && |
2330 | 93 | "Should only see pointers to data members here!"); |
2331 | | |
2332 | 93 | return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>()); |
2333 | 93 | } |
2334 | | |
2335 | | llvm::Constant * |
2336 | 19 | CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) { |
2337 | 19 | return ::EmitNullConstant(*this, Record, false); |
2338 | 19 | } |