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

source code of clang/lib/CodeGen/CGExprConstant.cpp