xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGExprConstant.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
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>
35 using namespace clang;
36 using namespace CodeGen;
37 
38 //===----------------------------------------------------------------------===//
39 //                            ConstantAggregateBuilder
40 //===----------------------------------------------------------------------===//
41 
42 namespace {
43 class ConstExprEmitter;
44 
getPadding(const CodeGenModule & CGM,CharUnits PadSize)45 llvm::Constant *getPadding(const CodeGenModule &CGM, CharUnits PadSize) {
46   llvm::Type *Ty = CGM.CharTy;
47   if (PadSize > CharUnits::One())
48     Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
49   if (CGM.shouldZeroInitPadding()) {
50     return llvm::Constant::getNullValue(Ty);
51   }
52   return llvm::UndefValue::get(Ty);
53 }
54 
55 struct ConstantAggregateBuilderUtils {
56   CodeGenModule &CGM;
57 
ConstantAggregateBuilderUtils__anon9d6f04f10111::ConstantAggregateBuilderUtils58   ConstantAggregateBuilderUtils(CodeGenModule &CGM) : CGM(CGM) {}
59 
getAlignment__anon9d6f04f10111::ConstantAggregateBuilderUtils60   CharUnits getAlignment(const llvm::Constant *C) const {
61     return CharUnits::fromQuantity(
62         CGM.getDataLayout().getABITypeAlign(C->getType()));
63   }
64 
getSize__anon9d6f04f10111::ConstantAggregateBuilderUtils65   CharUnits getSize(llvm::Type *Ty) const {
66     return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(Ty));
67   }
68 
getSize__anon9d6f04f10111::ConstantAggregateBuilderUtils69   CharUnits getSize(const llvm::Constant *C) const {
70     return getSize(C->getType());
71   }
72 
getPadding__anon9d6f04f10111::ConstantAggregateBuilderUtils73   llvm::Constant *getPadding(CharUnits PadSize) const {
74     return ::getPadding(CGM, PadSize);
75   }
76 
getZeroes__anon9d6f04f10111::ConstantAggregateBuilderUtils77   llvm::Constant *getZeroes(CharUnits ZeroSize) const {
78     llvm::Type *Ty = llvm::ArrayType::get(CGM.CharTy, 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.
85 class 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 
116 public:
ConstantAggregateBuilder(CodeGenModule & CGM)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.
build(llvm::Type * DesiredTy,bool AllowOversized) const139   llvm::Constant *build(llvm::Type *DesiredTy, bool AllowOversized) const {
140     return buildFrom(CGM, Elems, Offsets, CharUnits::Zero(), Size,
141                      NaturalLayout, DesiredTy, AllowOversized);
142   }
143 };
144 
145 template<typename Container, typename Range = std::initializer_list<
146                                  typename Container::value_type>>
replace(Container & C,size_t BeginOff,size_t EndOff,Range Vals)147 static 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 
add(llvm::Constant * C,CharUnits Offset,bool AllowOverwrite)152 bool 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(getPadding(Offset - Size));
162       Offsets.push_back(Size);
163     }
164     Elems.push_back(C);
165     Offsets.push_back(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(Offset);
172   if (!FirstElemToReplace)
173     return false;
174 
175   CharUnits CSize = getSize(C);
176   std::optional<size_t> LastElemToReplace = splitAt(Offset + CSize);
177   if (!LastElemToReplace)
178     return false;
179 
180   assert((FirstElemToReplace == LastElemToReplace || AllowOverwrite) &&
181          "unexpectedly overwriting field");
182 
183   replace(Elems, *FirstElemToReplace, *LastElemToReplace, {C});
184   replace(Offsets, *FirstElemToReplace, *LastElemToReplace, {Offset});
185   Size = std::max(Size, Offset + CSize);
186   NaturalLayout = false;
187   return true;
188 }
189 
addBits(llvm::APInt Bits,uint64_t OffsetInBits,bool AllowOverwrite)190 bool 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(OffsetInBits - OffsetWithinChar);
203        /**/; ++OffsetInChars) {
204     // Number of bits we want to fill in this char.
205     unsigned WantedBits =
206         std::min((uint64_t)Bits.getBitWidth(), 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(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(Shift);
219       else if (Shift < 0)
220         BitsThisChar = BitsThisChar.shl(-Shift);
221     } else {
222       BitsThisChar = BitsThisChar.shl(OffsetWithinChar);
223     }
224     if (BitsThisChar.getBitWidth() > CharWidth)
225       BitsThisChar = BitsThisChar.trunc(CharWidth);
226 
227     if (WantedBits == CharWidth) {
228       // Got a full byte: just add it directly.
229       add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
230           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(OffsetInChars);
236       if (!FirstElemToUpdate)
237         return false;
238       std::optional<size_t> LastElemToUpdate =
239           splitAt(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(CharWidth - OffsetWithinChar - WantedBits,
249                            CharWidth - OffsetWithinChar);
250       else
251         UpdateMask.setBits(OffsetWithinChar, OffsetWithinChar + WantedBits);
252       BitsThisChar &= UpdateMask;
253 
254       if (*FirstElemToUpdate == *LastElemToUpdate ||
255           Elems[*FirstElemToUpdate]->isNullValue() ||
256           isa<llvm::UndefValue>(Elems[*FirstElemToUpdate])) {
257         // All existing bits are either zero or undef.
258         add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
259             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>(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(CGM.getLLVMContext(), 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(WantedBits);
284     Bits = Bits.trunc(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.
splitAt(CharUnits Pos)297 std::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(Offsets, 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(Elems[LastAtOrBeforePosIndex]) <= Pos)
314       return LastAtOrBeforePosIndex + 1;
315 
316     // Try to decompose it into smaller constants.
317     if (!split(LastAtOrBeforePosIndex, 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.
split(size_t Index,CharUnits Hint)325 bool 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>(C)) {
331     // Expand the sequence into its contained elements.
332     // FIXME: This assumes vector elements are byte-sized.
333     replace(Elems, Index, Index + 1,
334             llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
335                             [&](unsigned Op) { return CA->getOperand(Op); }));
336     if (isa<llvm::ArrayType>(CA->getType()) ||
337         isa<llvm::VectorType>(CA->getType())) {
338       // Array or vector.
339       llvm::Type *ElemTy =
340           llvm::GetElementPtrInst::getTypeAtIndex(CA->getType(), (uint64_t)0);
341       CharUnits ElemSize = getSize(ElemTy);
342       replace(
343           Offsets, Index, Index + 1,
344           llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
345                           [&](unsigned Op) { return Offset + Op * ElemSize; }));
346     } else {
347       // Must be a struct.
348       auto *ST = cast<llvm::StructType>(CA->getType());
349       const llvm::StructLayout *Layout =
350           CGM.getDataLayout().getStructLayout(ST);
351       replace(Offsets, Index, Index + 1,
352               llvm::map_range(
353                   llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) {
354                     return Offset + CharUnits::fromQuantity(
355                                         Layout->getElementOffset(Op));
356                   }));
357     }
358     return true;
359   }
360 
361   if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(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(CDS->getElementType());
366     replace(Elems, Index, Index + 1,
367             llvm::map_range(llvm::seq(uint64_t(0u), CDS->getNumElements()),
368                             [&](uint64_t Elem) {
369                               return CDS->getElementAsConstant(Elem);
370                             }));
371     replace(Offsets, Index, Index + 1,
372             llvm::map_range(
373                 llvm::seq(uint64_t(0u), CDS->getNumElements()),
374                 [&](uint64_t Elem) { return Offset + Elem * ElemSize; }));
375     return true;
376   }
377 
378   if (isa<llvm::ConstantAggregateZero>(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(Elems, Index, Index + 1,
383             {getZeroes(Hint - Offset), getZeroes(Offset + ElemSize - Hint)});
384     replace(Offsets, Index, Index + 1, {Offset, Hint});
385     return true;
386   }
387 
388   if (isa<llvm::UndefValue>(C)) {
389     // Drop undef; it doesn't contribute to the final layout.
390     replace(Elems, Index, Index + 1, {});
391     replace(Offsets, Index, Index + 1, {});
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 
402 static llvm::Constant *
403 EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
404                   llvm::Type *CommonElementType, uint64_t ArrayBound,
405                   SmallVectorImpl<llvm::Constant *> &Elements,
406                   llvm::Constant *Filler);
407 
buildFrom(CodeGenModule & CGM,ArrayRef<llvm::Constant * > Elems,ArrayRef<CharUnits> Offsets,CharUnits StartOffset,CharUnits Size,bool NaturalLayout,llvm::Type * DesiredTy,bool AllowOversized)408 llvm::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(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>(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(CommonType);
427     CharUnits ElemSize = Utils.getSize(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(Offset(I) / ElemSize + 1, Filler);
441       ArrayElements.back() = Elems[I];
442     }
443 
444     if (CanEmitArray) {
445       return EmitArrayConstant(CGM, ATy, CommonType, ATy->getNumElements(),
446                                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(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(Align, 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(Elems.begin(), Elems.end());
480     UnpackedElemStorage.push_back(Utils.getPadding(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(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(Utils.getPadding(DesiredOffset - SizeSoFar));
500       PackedElems.push_back(Elems[I]);
501       SizeSoFar = DesiredOffset + Utils.getSize(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(Utils.getPadding(DesiredSize - SizeSoFar));
510     }
511   }
512 
513   llvm::StructType *STy = llvm::ConstantStruct::getTypeForElements(
514       CGM.getLLVMContext(), 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>(DesiredTy)) {
519     if (DesiredSTy->isLayoutIdentical(STy))
520       STy = DesiredSTy;
521   }
522 
523   return llvm::ConstantStruct::get(STy, Packed ? PackedElems : UnpackedElems);
524 }
525 
condense(CharUnits Offset,llvm::Type * DesiredTy)526 void ConstantAggregateBuilder::condense(CharUnits Offset,
527                                         llvm::Type *DesiredTy) {
528   CharUnits Size = getSize(DesiredTy);
529 
530   std::optional<size_t> FirstElemToReplace = splitAt(Offset);
531   if (!FirstElemToReplace)
532     return;
533   size_t First = *FirstElemToReplace;
534 
535   std::optional<size_t> LastElemToReplace = splitAt(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(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>(DesiredTy);
549     if (STy && STy->getNumElements() == 1 &&
550         STy->getElementType(0) == Elems[First]->getType())
551       Elems[First] = llvm::ConstantStruct::get(STy, Elems[First]);
552     return;
553   }
554 
555   llvm::Constant *Replacement = buildFrom(
556       CGM, ArrayRef(Elems).slice(First, Length),
557       ArrayRef(Offsets).slice(First, Length), Offset, getSize(DesiredTy),
558       /*known to have natural layout=*/false, DesiredTy, false);
559   replace(Elems, First, Last, {Replacement});
560   replace(Offsets, First, Last, {Offset});
561 }
562 
563 //===----------------------------------------------------------------------===//
564 //                            ConstStructBuilder
565 //===----------------------------------------------------------------------===//
566 
567 class ConstStructBuilder {
568   CodeGenModule &CGM;
569   ConstantEmitter &Emitter;
570   ConstantAggregateBuilder &Builder;
571   CharUnits StartOffset;
572 
573 public:
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 
583 private:
ConstStructBuilder(ConstantEmitter & Emitter,ConstantAggregateBuilder & Builder,CharUnits StartOffset)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 
AppendField(const FieldDecl * Field,uint64_t FieldOffset,llvm::Constant * InitCst,bool AllowOverwrite)609 bool 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(FieldOffset);
615 
616   return AppendBytes(FieldOffsetInChars, InitCst, AllowOverwrite);
617 }
618 
AppendBytes(CharUnits FieldOffsetInChars,llvm::Constant * InitCst,bool AllowOverwrite)619 bool ConstStructBuilder::AppendBytes(CharUnits FieldOffsetInChars,
620                                      llvm::Constant *InitCst,
621                                      bool AllowOverwrite) {
622   return Builder.add(InitCst, StartOffset + FieldOffsetInChars, AllowOverwrite);
623 }
624 
AppendBitField(const FieldDecl * Field,uint64_t FieldOffset,llvm::Constant * C,bool AllowOverwrite)625 bool ConstStructBuilder::AppendBitField(const FieldDecl *Field,
626                                         uint64_t FieldOffset, llvm::Constant *C,
627                                         bool AllowOverwrite) {
628 
629   llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(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(Field->getType(), C->getType());
637     llvm::Constant *FoldedConstant = llvm::ConstantFoldLoadFromConst(
638         C, LoadType, llvm::APInt::getZero(32), CGM.getDataLayout());
639     CI = dyn_cast_if_present<llvm::ConstantInt>(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(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(Info.Size);
655 
656   // Truncate the size of FieldValue to the bit field size.
657   if (Info.Size < FieldValue.getBitWidth())
658     FieldValue = FieldValue.trunc(Info.Size);
659 
660   return Builder.addBits(FieldValue,
661                          CGM.getContext().toBits(StartOffset) + FieldOffset,
662                          AllowOverwrite);
663 }
664 
EmitDesignatedInitUpdater(ConstantEmitter & Emitter,ConstantAggregateBuilder & Const,CharUnits Offset,QualType Type,const InitListExpr * Updater)665 static 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(Type);
673   if (!CAT)
674     return false;
675   QualType ElemType = CAT->getElementType();
676   CharUnits ElemSize = Emitter.CGM.getContext().getTypeSizeInChars(ElemType);
677   llvm::Type *ElemTy = Emitter.CGM.getTypes().ConvertTypeForMem(ElemType);
678 
679   llvm::Constant *FillC = nullptr;
680   if (const Expr *Filler = Updater->getArrayFiller()) {
681     if (!isa<NoInitExpr>(Filler)) {
682       FillC = Emitter.tryEmitAbstractForMemory(Filler, 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(I);
694 
695     if (!Init && FillC) {
696       if (!Const.add(FillC, Offset, true))
697         return false;
698     } else if (!Init || isa<NoInitExpr>(Init)) {
699       continue;
700     } else if (const auto *ChildILE = dyn_cast<InitListExpr>(Init)) {
701       if (!EmitDesignatedInitUpdater(Emitter, Const, Offset, ElemType,
702                                      ChildILE))
703         return false;
704       // Attempt to reduce the array element to a single constant if necessary.
705       Const.condense(Offset, ElemTy);
706     } else {
707       llvm::Constant *Val = Emitter.tryEmitPrivateForMemory(Init, ElemType);
708       if (!Const.add(Val, Offset, true))
709         return false;
710     }
711   }
712 
713   return true;
714 }
715 
Build(const InitListExpr * ILE,bool AllowOverwrite)716 bool ConstStructBuilder::Build(const InitListExpr *ILE, bool AllowOverwrite) {
717   RecordDecl *RD = ILE->getType()->castAs<RecordType>()->getDecl();
718   const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(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>(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(ILE->getInitializedFieldInUnion(), 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(ElementNo++);
751     if (isa_and_nonnull<NoInitExpr>(Init)) {
752       if (ZeroInitPadding &&
753           !DoZeroInitPadding(Layout, FieldNo, *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(CGM.getContext(), Field)) {
762       if (Init && Init->HasSideEffects(CGM.getContext()))
763         return false;
764       continue;
765     }
766 
767     if (ZeroInitPadding &&
768         !DoZeroInitPadding(Layout, FieldNo, *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>(Init)) {
778         CharUnits Offset = CGM.getContext().toCharUnitsFromBits(
779             Layout.getFieldOffset(FieldNo));
780         if (!EmitDesignatedInitUpdater(Emitter, Builder, StartOffset + Offset,
781                                        Field->getType(), 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(StartOffset + Offset,
786                          CGM.getTypes().ConvertTypeForMem(Field->getType()));
787         continue;
788       }
789     }
790 
791     llvm::Constant *EltInit =
792         Init ? Emitter.tryEmitPrivateForMemory(Init, Field->getType())
793              : Emitter.emitNullForMemory(Field->getType());
794     if (!EltInit)
795       return false;
796 
797     if (ZeroInitPadding && ZeroFieldSize)
798       SizeSoFar += CharUnits::fromQuantity(
799           CGM.getDataLayout().getTypeAllocSize(EltInit->getType()));
800 
801     if (!Field->isBitField()) {
802       // Handle non-bitfield members.
803       if (!AppendField(Field, Layout.getFieldOffset(FieldNo), 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, Layout.getFieldOffset(FieldNo), 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 
824 namespace {
825 struct BaseInfo {
BaseInfo__anon9d6f04f10111::__anon9d6f04f10811::BaseInfo826   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 
operator <__anon9d6f04f10111::__anon9d6f04f10811::BaseInfo834   bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
835 };
836 }
837 
Build(const APValue & Val,const RecordDecl * RD,bool IsPrimaryBase,const CXXRecordDecl * VTableClass,CharUnits Offset)838 bool 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(RD);
843 
844   if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(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(BaseSubobject(CD, Offset),
849                                                 VTableClass);
850       if (auto Authentication = CGM.getVTablePointerAuthentication(CD)) {
851         VTableAddressPoint = Emitter.tryEmitConstantSignedPointer(
852             VTableAddressPoint, *Authentication);
853         if (!VTableAddressPoint)
854           return false;
855       }
856       if (!AppendBytes(Offset, 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(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(BD);
870       Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
871     }
872     llvm::stable_sort(Bases);
873 
874     for (const BaseInfo &Base : Bases) {
875       bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
876       if (!Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
877                  VTableClass, Offset + Base.Offset))
878         return false;
879     }
880   }
881 
882   unsigned FieldNo = 0;
883   uint64_t OffsetBits = CGM.getContext().toBits(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(Val.getUnionField(), *Field))
893       continue;
894 
895     // Don't emit anonymous bitfields or zero-sized fields.
896     if (Field->isUnnamedBitField() ||
897         isEmptyFieldForLayout(CGM.getContext(), *Field))
898       continue;
899 
900     // Emit the value of the initializer.
901     const APValue &FieldValue =
902       RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
903     llvm::Constant *EltInit =
904       Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType());
905     if (!EltInit)
906       return false;
907 
908     if (ZeroInitPadding) {
909       if (!DoZeroInitPadding(Layout, FieldNo, **Field, AllowOverwrite,
910                              SizeSoFar, ZeroFieldSize))
911         return false;
912       if (ZeroFieldSize)
913         SizeSoFar += CharUnits::fromQuantity(
914             CGM.getDataLayout().getTypeAllocSize(EltInit->getType()));
915     }
916 
917     if (!Field->isBitField()) {
918       // Handle non-bitfield members.
919       if (!AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
920                        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, Layout.getFieldOffset(FieldNo) + OffsetBits,
929                           EltInit, AllowOverwrite))
930         return false;
931     }
932   }
933   if (ZeroInitPadding && !DoZeroInitPadding(Layout, AllowOverwrite, SizeSoFar))
934     return false;
935 
936   return true;
937 }
938 
DoZeroInitPadding(const ASTRecordLayout & Layout,unsigned FieldNo,const FieldDecl & Field,bool AllowOverwrite,CharUnits & SizeSoFar,bool & ZeroFieldSize)939 bool 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(StartBitOffset);
944   if (SizeSoFar < StartOffset)
945     if (!AppendBytes(SizeSoFar, getPadding(CGM, StartOffset - SizeSoFar),
946                      AllowOverwrite))
947       return false;
948 
949   if (!Field.isBitField()) {
950     CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(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(&Field);
957     uint64_t EndBitOffset = StartBitOffset + Info.Size;
958     SizeSoFar = CGM.getContext().toCharUnitsFromBits(EndBitOffset);
959     if (EndBitOffset % CGM.getContext().getCharWidth() != 0) {
960       SizeSoFar++;
961     }
962     ZeroFieldSize = Info.Size == 0;
963   }
964   return true;
965 }
966 
DoZeroInitPadding(const ASTRecordLayout & Layout,bool AllowOverwrite,CharUnits SizeSoFar)967 bool ConstStructBuilder::DoZeroInitPadding(const ASTRecordLayout &Layout,
968                                            bool AllowOverwrite,
969                                            CharUnits SizeSoFar) {
970   CharUnits TotalSize = Layout.getSize();
971   if (SizeSoFar < TotalSize)
972     if (!AppendBytes(SizeSoFar, getPadding(CGM, TotalSize - SizeSoFar),
973                      AllowOverwrite))
974       return false;
975   SizeSoFar = TotalSize;
976   return true;
977 }
978 
Finalize(QualType Type)979 llvm::Constant *ConstStructBuilder::Finalize(QualType Type) {
980   Type = Type.getNonReferenceType();
981   RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
982   llvm::Type *ValTy = CGM.getTypes().ConvertType(Type);
983   return Builder.build(ValTy, RD->hasFlexibleArrayMember());
984 }
985 
BuildStruct(ConstantEmitter & Emitter,const InitListExpr * ILE,QualType ValTy)986 llvm::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(ValTy);
996 }
997 
BuildStruct(ConstantEmitter & Emitter,const APValue & Val,QualType ValTy)998 llvm::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>(RD);
1006   if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero()))
1007     return nullptr;
1008 
1009   return Builder.Finalize(ValTy);
1010 }
1011 
UpdateStruct(ConstantEmitter & Emitter,ConstantAggregateBuilder & Const,CharUnits Offset,const InitListExpr * Updater)1012 bool ConstStructBuilder::UpdateStruct(ConstantEmitter &Emitter,
1013                                       ConstantAggregateBuilder &Const,
1014                                       CharUnits Offset,
1015                                       const InitListExpr *Updater) {
1016   return ConstStructBuilder(Emitter, Const, Offset)
1017       .Build(Updater, /*AllowOverwrite*/ true);
1018 }
1019 
1020 //===----------------------------------------------------------------------===//
1021 //                             ConstExprEmitter
1022 //===----------------------------------------------------------------------===//
1023 
1024 static ConstantAddress
tryEmitGlobalCompoundLiteral(ConstantEmitter & emitter,const CompoundLiteralExpr * E)1025 tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
1026                              const CompoundLiteralExpr *E) {
1027   CodeGenModule &CGM = emitter.CGM;
1028   CharUnits Align = CGM.getContext().getTypeAlignInChars(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->getInitializer(),
1035                                                     addressSpace, 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(CGM.getContext(), true, false),
1045       llvm::GlobalValue::InternalLinkage, C, ".compoundliteral", nullptr,
1046       llvm::GlobalVariable::NotThreadLocal,
1047       CGM.getContext().getTargetAddressSpace(addressSpace));
1048   emitter.finalize(GV);
1049   GV->setAlignment(Align.getAsAlign());
1050   CGM.setAddrOfConstantCompoundLiteral(E, GV);
1051   return ConstantAddress(GV, GV->getValueType(), Align);
1052 }
1053 
1054 static llvm::Constant *
EmitArrayConstant(CodeGenModule & CGM,llvm::ArrayType * DesiredType,llvm::Type * CommonElementType,uint64_t ArrayBound,SmallVectorImpl<llvm::Constant * > & Elements,llvm::Constant * Filler)1055 EmitArrayConstant(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(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           llvm::ArrayType::get(CommonElementType, NonzeroLength),
1082           ArrayRef(Elements).take_front(NonzeroLength));
1083       Elements.resize(2);
1084       Elements[0] = Initial;
1085     } else {
1086       Elements.resize(NonzeroLength + 1);
1087     }
1088 
1089     auto *FillerType =
1090         CommonElementType ? CommonElementType : DesiredType->getElementType();
1091     FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
1092     Elements.back() = llvm::ConstantAggregateZero::get(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(ArrayBound, 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         llvm::ArrayType::get(CommonElementType, ArrayBound), Elements);
1105 
1106   // We have mixed types. Use a packed struct.
1107   llvm::SmallVector<llvm::Type *, 16> Types;
1108   Types.reserve(Elements.size());
1109   for (llvm::Constant *Elt : Elements)
1110     Types.push_back(Elt->getType());
1111   llvm::StructType *SType =
1112       llvm::StructType::get(CGM.getLLVMContext(), Types, true);
1113   return llvm::ConstantStruct::get(SType, 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.
1122 class ConstExprEmitter
1123     : public ConstStmtVisitor<ConstExprEmitter, llvm::Constant *, QualType> {
1124   CodeGenModule &CGM;
1125   ConstantEmitter &Emitter;
1126   llvm::LLVMContext &VMContext;
1127 public:
ConstExprEmitter(ConstantEmitter & emitter)1128   ConstExprEmitter(ConstantEmitter &emitter)
1129     : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) {
1130   }
1131 
1132   //===--------------------------------------------------------------------===//
1133   //                            Visitor Methods
1134   //===--------------------------------------------------------------------===//
1135 
VisitStmt(const Stmt * S,QualType T)1136   llvm::Constant *VisitStmt(const Stmt *S, QualType T) { return nullptr; }
1137 
VisitConstantExpr(const ConstantExpr * CE,QualType T)1138   llvm::Constant *VisitConstantExpr(const ConstantExpr *CE, QualType T) {
1139     if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(CE))
1140       return Result;
1141     return Visit(CE->getSubExpr(), T);
1142   }
1143 
VisitParenExpr(const ParenExpr * PE,QualType T)1144   llvm::Constant *VisitParenExpr(const ParenExpr *PE, QualType T) {
1145     return Visit(PE->getSubExpr(), T);
1146   }
1147 
1148   llvm::Constant *
VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr * PE,QualType T)1149   VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *PE,
1150                                     QualType T) {
1151     return Visit(PE->getReplacement(), T);
1152   }
1153 
VisitGenericSelectionExpr(const GenericSelectionExpr * GE,QualType T)1154   llvm::Constant *VisitGenericSelectionExpr(const GenericSelectionExpr *GE,
1155                                             QualType T) {
1156     return Visit(GE->getResultExpr(), T);
1157   }
1158 
VisitChooseExpr(const ChooseExpr * CE,QualType T)1159   llvm::Constant *VisitChooseExpr(const ChooseExpr *CE, QualType T) {
1160     return Visit(CE->getChosenSubExpr(), T);
1161   }
1162 
VisitCompoundLiteralExpr(const CompoundLiteralExpr * E,QualType T)1163   llvm::Constant *VisitCompoundLiteralExpr(const CompoundLiteralExpr *E,
1164                                            QualType T) {
1165     return Visit(E->getInitializer(), T);
1166   }
1167 
ProduceIntToIntCast(const Expr * E,QualType DestType)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(E, FromType))
1173         if (auto *CI = dyn_cast<llvm::ConstantInt>(C)) {
1174           unsigned SrcWidth = CGM.getContext().getIntWidth(FromType);
1175           unsigned DstWidth = CGM.getContext().getIntWidth(DestType);
1176           if (DstWidth == SrcWidth)
1177             return CI;
1178           llvm::APInt A = FromType->isSignedIntegerType()
1179                               ? CI->getValue().sextOrTrunc(DstWidth)
1180                               : CI->getValue().zextOrTrunc(DstWidth);
1181           return llvm::ConstantInt::get(CGM.getLLVMContext(), A);
1182         }
1183     return nullptr;
1184   }
1185 
VisitCastExpr(const CastExpr * E,QualType destType)1186   llvm::Constant *VisitCastExpr(const CastExpr *E, QualType destType) {
1187     if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
1188       CGM.EmitExplicitCastExprType(ECE, 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(subExpr, field->getType());
1200       if (!C) return nullptr;
1201 
1202       auto destTy = ConvertType(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(C);
1210       Types.push_back(C->getType());
1211       unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
1212       unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy);
1213 
1214       assert(CurSize <= TotalSize && "Union size mismatch!");
1215       if (unsigned NumPadBytes = TotalSize - CurSize) {
1216         llvm::Constant *Padding =
1217             getPadding(CGM, CharUnits::fromQuantity(NumPadBytes));
1218         Elts.push_back(Padding);
1219         Types.push_back(Padding->getType());
1220       }
1221 
1222       llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false);
1223       return llvm::ConstantStruct::get(STy, Elts);
1224     }
1225 
1226     case CK_AddressSpaceConversion: {
1227       auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
1228       if (!C)
1229         return nullptr;
1230       LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace();
1231       llvm::Type *destTy = ConvertType(E->getType());
1232       return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS,
1233                                                              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>(subExpr->IgnoreParens()))
1243         return Visit(E->getInitializer(), 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(subExpr, destType);
1252 
1253     case CK_ArrayToPointerDecay:
1254       if (const auto *S = dyn_cast<StringLiteral>(subExpr))
1255         return CGM.GetAddrOfConstantStringFromLiteral(S).getPointer();
1256       return nullptr;
1257     case CK_NullToPointer:
1258       if (Visit(subExpr, destType))
1259         return CGM.EmitNullConstant(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(subExpr, 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(subExpr, subExpr->getType());
1277       if (!C) return nullptr;
1278       return CGM.getCXXABI().EmitMemberPointerConversion(E, 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 
VisitCXXDefaultInitExpr(const CXXDefaultInitExpr * DIE,QualType T)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(DIE->getExpr(), T);
1349   }
1350 
VisitExprWithCleanups(const ExprWithCleanups * E,QualType T)1351   llvm::Constant *VisitExprWithCleanups(const ExprWithCleanups *E, QualType T) {
1352     return Visit(E->getSubExpr(), T);
1353   }
1354 
VisitIntegerLiteral(const IntegerLiteral * I,QualType T)1355   llvm::Constant *VisitIntegerLiteral(const IntegerLiteral *I, QualType T) {
1356     return llvm::ConstantInt::get(CGM.getLLVMContext(), I->getValue());
1357   }
1358 
withDestType(ASTContext & Ctx,const Expr * E,QualType SrcType,QualType DestType,const llvm::APSInt & Value)1359   static APValue withDestType(ASTContext &Ctx, const Expr *E, QualType SrcType,
1360                               QualType DestType, const llvm::APSInt &Value) {
1361     if (!Ctx.hasSameType(SrcType, DestType)) {
1362       if (DestType->isFloatingType()) {
1363         llvm::APFloat Result =
1364             llvm::APFloat(Ctx.getFloatTypeSemantics(DestType), 1);
1365         llvm::RoundingMode RM =
1366             E->getFPFeaturesInEffect(Ctx.getLangOpts()).getRoundingMode();
1367         if (RM == llvm::RoundingMode::Dynamic)
1368           RM = llvm::RoundingMode::NearestTiesToEven;
1369         Result.convertFromAPInt(Value, Value.isSigned(), RM);
1370         return APValue(Result);
1371       }
1372     }
1373     return APValue(Value);
1374   }
1375 
EmitArrayInitialization(const InitListExpr * ILE,QualType T)1376   llvm::Constant *EmitArrayInitialization(const InitListExpr *ILE, QualType T) {
1377     auto *CAT = CGM.getContext().getAsConstantArrayType(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>(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>(NumInitElements, 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(filler, 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(NumInitableElts + 1);
1410     else
1411       Elts.reserve(NumElements);
1412 
1413     llvm::Type *CommonElementType = nullptr;
1414     auto Emit = [&](const Expr *Init, unsigned ArrayIndex) {
1415       llvm::Constant *C = nullptr;
1416       C = Emitter.tryEmitPrivateForMemory(Init, 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(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(i);
1431       if (auto *EmbedS = dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
1432         StringLiteral *SL = EmbedS->getDataStringLiteral();
1433         llvm::APSInt Value(CGM.getContext().getTypeSize(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);
1440           if (DestTy->isIntegerType()) {
1441             C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value);
1442           } else {
1443             C = Emitter.tryEmitPrivateForMemory(
1444                 withDestType(CGM.getContext(), Init, EmbedS->getType(), DestTy,
1445                              Value),
1446                 EltType);
1447           }
1448           if (!C)
1449             return nullptr;
1450           Elts.push_back(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>(CGM.getTypes().ConvertType(ILE->getType()));
1466     return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
1467                              fillC);
1468   }
1469 
EmitRecordInitialization(const InitListExpr * ILE,QualType T)1470   llvm::Constant *EmitRecordInitialization(const InitListExpr *ILE,
1471                                            QualType T) {
1472     return ConstStructBuilder::BuildStruct(Emitter, ILE, T);
1473   }
1474 
VisitImplicitValueInitExpr(const ImplicitValueInitExpr * E,QualType T)1475   llvm::Constant *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E,
1476                                              QualType T) {
1477     return CGM.EmitNullConstant(T);
1478   }
1479 
VisitInitListExpr(const InitListExpr * ILE,QualType T)1480   llvm::Constant *VisitInitListExpr(const InitListExpr *ILE, QualType T) {
1481     if (ILE->isTransparent())
1482       return Visit(ILE->getInit(0), 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 *
VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr * E,QualType destType)1494   VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E,
1495                                 QualType destType) {
1496     auto C = Visit(E->getBase(), destType);
1497     if (!C)
1498       return nullptr;
1499 
1500     ConstantAggregateBuilder Const(CGM);
1501     Const.add(C, CharUnits::Zero(), false);
1502 
1503     if (!EmitDesignatedInitUpdater(Emitter, Const, CharUnits::Zero(), destType,
1504                                    E->getUpdater()))
1505       return nullptr;
1506 
1507     llvm::Type *ValTy = CGM.getTypes().ConvertType(destType);
1508     bool HasFlexibleArray = false;
1509     if (const auto *RT = destType->getAs<RecordType>())
1510       HasFlexibleArray = RT->getDecl()->hasFlexibleArrayMember();
1511     return Const.build(ValTy, HasFlexibleArray);
1512   }
1513 
VisitCXXConstructExpr(const CXXConstructExpr * E,QualType Ty)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(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>(Arg))
1532         return Visit(MTE->getSubExpr(), Ty);
1533       // Don't try to support arbitrary lvalue-to-rvalue conversions for now.
1534       return nullptr;
1535     }
1536 
1537     return CGM.EmitNullConstant(Ty);
1538   }
1539 
VisitStringLiteral(const StringLiteral * E,QualType T)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 
VisitObjCEncodeExpr(const ObjCEncodeExpr * E,QualType T)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(E->getEncodedType(), 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(CAT->getZExtSize(), '\0');
1557     return llvm::ConstantDataArray::getString(VMContext, Str, false);
1558   }
1559 
VisitUnaryExtension(const UnaryOperator * E,QualType T)1560   llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) {
1561     return Visit(E->getSubExpr(), T);
1562   }
1563 
VisitUnaryMinus(const UnaryOperator * U,QualType T)1564   llvm::Constant *VisitUnaryMinus(const UnaryOperator *U, QualType T) {
1565     if (llvm::Constant *C = Visit(U->getSubExpr(), T))
1566       if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
1567         return llvm::ConstantInt::get(CGM.getLLVMContext(), -CI->getValue());
1568     return nullptr;
1569   }
1570 
VisitPackIndexingExpr(const PackIndexingExpr * E,QualType T)1571   llvm::Constant *VisitPackIndexingExpr(const PackIndexingExpr *E, QualType T) {
1572     return Visit(E->getSelectedExpr(), T);
1573   }
1574 
1575   // Utility methods
ConvertType(QualType T)1576   llvm::Type *ConvertType(QualType T) {
1577     return CGM.getTypes().ConvertType(T);
1578   }
1579 };
1580 
1581 }  // end anonymous namespace.
1582 
validateAndPopAbstract(llvm::Constant * C,AbstractState saved)1583 llvm::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 
1595 llvm::Constant *
tryEmitAbstractForInitializer(const VarDecl & D)1596 ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) {
1597   auto state = pushAbstract();
1598   auto C = tryEmitPrivateForVarInit(D);
1599   return validateAndPopAbstract(C, state);
1600 }
1601 
1602 llvm::Constant *
tryEmitAbstract(const Expr * E,QualType destType)1603 ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) {
1604   auto state = pushAbstract();
1605   auto C = tryEmitPrivate(E, destType);
1606   return validateAndPopAbstract(C, state);
1607 }
1608 
1609 llvm::Constant *
tryEmitAbstract(const APValue & value,QualType destType)1610 ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) {
1611   auto state = pushAbstract();
1612   auto C = tryEmitPrivate(value, destType);
1613   return validateAndPopAbstract(C, state);
1614 }
1615 
tryEmitConstantExpr(const ConstantExpr * CE)1616 llvm::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(RetType);
1623 
1624   return tryEmitAbstract(CE->getAPValueResult(), RetType);
1625 }
1626 
1627 llvm::Constant *
emitAbstract(const Expr * E,QualType destType)1628 ConstantEmitter::emitAbstract(const Expr *E, QualType destType) {
1629   auto state = pushAbstract();
1630   auto C = tryEmitPrivate(E, destType);
1631   C = validateAndPopAbstract(C, state);
1632   if (!C) {
1633     CGM.Error(E->getExprLoc(),
1634               "internal error: could not emit constant value \"abstractly\"");
1635     C = CGM.EmitNullConstant(destType);
1636   }
1637   return C;
1638 }
1639 
1640 llvm::Constant *
emitAbstract(SourceLocation loc,const APValue & value,QualType destType,bool EnablePtrAuthFunctionTypeDiscrimination)1641 ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value,
1642                               QualType destType,
1643                               bool EnablePtrAuthFunctionTypeDiscrimination) {
1644   auto state = pushAbstract();
1645   auto C =
1646       tryEmitPrivate(value, destType, EnablePtrAuthFunctionTypeDiscrimination);
1647   C = validateAndPopAbstract(C, state);
1648   if (!C) {
1649     CGM.Error(loc,
1650               "internal error: could not emit constant value \"abstractly\"");
1651     C = CGM.EmitNullConstant(destType);
1652   }
1653   return C;
1654 }
1655 
tryEmitForInitializer(const VarDecl & D)1656 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) {
1657   initializeNonAbstract(D.getType().getAddressSpace());
1658   return markIfFailed(tryEmitPrivateForVarInit(D));
1659 }
1660 
tryEmitForInitializer(const Expr * E,LangAS destAddrSpace,QualType destType)1661 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E,
1662                                                        LangAS destAddrSpace,
1663                                                        QualType destType) {
1664   initializeNonAbstract(destAddrSpace);
1665   return markIfFailed(tryEmitPrivateForMemory(E, destType));
1666 }
1667 
emitForInitializer(const APValue & value,LangAS destAddrSpace,QualType destType)1668 llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value,
1669                                                     LangAS destAddrSpace,
1670                                                     QualType destType) {
1671   initializeNonAbstract(destAddrSpace);
1672   auto C = tryEmitPrivateForMemory(value, destType);
1673   assert(C && "couldn't emit constant value non-abstractly?");
1674   return C;
1675 }
1676 
getCurrentAddrPrivate()1677 llvm::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(DestAddressSpace));
1691 
1692   PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
1693 
1694   return global;
1695 }
1696 
registerCurrentAddrPrivate(llvm::Constant * signal,llvm::GlobalValue * placeholder)1697 void 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 
1705 namespace {
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 
ReplacePlaceholders__anon9d6f04f10a11::ReplacePlaceholders1725     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 
replaceInInitializer__anon9d6f04f10a11::ReplacePlaceholders1732     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(0);
1738       IndexValues.push_back(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(entry.second);
1758         entry.first->eraseFromParent();
1759       }
1760     }
1761 
1762   private:
findLocations__anon9d6f04f10a11::ReplacePlaceholders1763     void findLocations(llvm::Constant *init) {
1764       // Recurse into aggregates.
1765       if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) {
1766         for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) {
1767           Indices.push_back(i);
1768           IndexValues.push_back(nullptr);
1769 
1770           findLocations(agg->getOperand(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(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>(init)) {
1788           init = expr->getOperand(0);
1789         } else {
1790           break;
1791         }
1792       }
1793     }
1794 
setLocation__anon9d6f04f10a11::ReplacePlaceholders1795     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(CGM.Int32Ty, Indices[i]);
1817       }
1818 
1819       llvm::Constant *location = llvm::ConstantExpr::getInBoundsGetElementPtr(
1820           BaseValueTy, Base, IndexValues);
1821 
1822       Locations.insert({placeholder, location});
1823     }
1824   };
1825 }
1826 
finalize(llvm::GlobalVariable * global)1827 void 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(global->getInitializer());
1839     PlaceholderAddresses.clear(); // satisfy
1840   }
1841 }
1842 
~ConstantEmitter()1843 ConstantEmitter::~ConstantEmitter() {
1844   assert((!InitializedNonAbstract || Finalized || Failed) &&
1845          "not finalized after being initialized for non-abstract emission");
1846   assert(PlaceholderAddresses.empty() && "unhandled placeholders");
1847 }
1848 
getNonMemoryType(CodeGenModule & CGM,QualType type)1849 static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) {
1850   if (auto AT = type->getAs<AtomicType>()) {
1851     return CGM.getContext().getQualifiedType(AT->getValueType(),
1852                                              type.getQualifiers());
1853   }
1854   return type;
1855 }
1856 
tryEmitPrivateForVarInit(const VarDecl & D)1857 llvm::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(D.getType());
1863     if (Ty->isRecordType())
1864       if (const CXXConstructExpr *E =
1865           dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1866         const CXXConstructorDecl *CD = E->getConstructor();
1867         if (CD->isTrivial() && CD->isDefaultConstructor())
1868           return CGM.EmitNullConstant(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, destType);
1879     if (llvm::Constant *C = ConstExprEmitter(*this).Visit(E, nonMemoryDestType))
1880       return emitForMemory(C, 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, destType);
1889   }
1890 
1891   return nullptr;
1892 }
1893 
1894 llvm::Constant *
tryEmitAbstractForMemory(const Expr * E,QualType destType)1895 ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) {
1896   auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1897   auto C = tryEmitAbstract(E, nonMemoryDestType);
1898   return (C ? emitForMemory(C, destType) : nullptr);
1899 }
1900 
1901 llvm::Constant *
tryEmitAbstractForMemory(const APValue & value,QualType destType)1902 ConstantEmitter::tryEmitAbstractForMemory(const APValue &value,
1903                                           QualType destType) {
1904   auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1905   auto C = tryEmitAbstract(value, nonMemoryDestType);
1906   return (C ? emitForMemory(C, destType) : nullptr);
1907 }
1908 
tryEmitPrivateForMemory(const Expr * E,QualType destType)1909 llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E,
1910                                                          QualType destType) {
1911   auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1912   llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType);
1913   return (C ? emitForMemory(C, destType) : nullptr);
1914 }
1915 
tryEmitPrivateForMemory(const APValue & value,QualType destType)1916 llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value,
1917                                                          QualType destType) {
1918   auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1919   auto C = tryEmitPrivate(value, nonMemoryDestType);
1920   return (C ? emitForMemory(C, 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.
1928 llvm::Constant *
tryEmitConstantSignedPointer(llvm::Constant * UnsignedPointer,PointerAuthQualifier Schema)1929 ConstantEmitter::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(CGM.IntPtrTy, Schema.getExtraDiscriminator());
1950 
1951   llvm::Constant *SignedPointer = CGM.getConstantSignedPointer(
1952       UnsignedPointer, Key, StorageAddress, Discriminator);
1953 
1954   if (Schema.isAddressDiscriminated())
1955     registerCurrentAddrPrivate(SignedPointer, StorageAddress);
1956 
1957   return SignedPointer;
1958 }
1959 
emitForMemory(CodeGenModule & CGM,llvm::Constant * C,QualType destType)1960 llvm::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, destValueType);
1967 
1968     uint64_t innerSize = CGM.getContext().getTypeSize(destValueType);
1969     uint64_t outerSize = CGM.getContext().getTypeSize(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           llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8))
1978     };
1979     return llvm::ConstantStruct::getAnon(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(1) && !destType->isBitIntType()) ||
1985       (destType->isExtVectorBoolType() &&
1986        !destType->isPackedVectorBoolType(CGM.getContext()))) {
1987     llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType);
1988     llvm::Constant *Res = llvm::ConstantFoldCastOperand(
1989         llvm::Instruction::ZExt, C, boolTy, 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(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>(C);
2000     llvm::Constant *Res = llvm::ConstantFoldCastOperand(
2001         destType->isSignedIntegerOrEnumerationType() ? llvm::Instruction::SExt
2002                                                      : llvm::Instruction::ZExt,
2003         CI, LoadStoreTy, CGM.getDataLayout());
2004     if (CGM.getTypes().typeRequiresSplitIntoByteArray(destType, 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(destType);
2008       llvm::APInt Value = cast<llvm::ConstantInt>(Res)->getValue();
2009       Builder.addBits(Value, /*OffsetInBits=*/0, /*AllowOverwrite=*/false);
2010       return Builder.build(DesiredTy, /*AllowOversized*/ false);
2011     }
2012     return Res;
2013   }
2014 
2015   return C;
2016 }
2017 
tryEmitPrivate(const Expr * E,QualType destType)2018 llvm::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(E, destType))
2024       return C;
2025 
2026   Expr::EvalResult Result;
2027 
2028   bool Success = false;
2029 
2030   if (destType->isReferenceType())
2031     Success = E->EvaluateAsLValue(Result, CGM.getContext());
2032   else
2033     Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext);
2034 
2035   if (Success && !Result.HasSideEffects)
2036     return tryEmitPrivate(Result.Val, destType);
2037 
2038   return nullptr;
2039 }
2040 
getNullPointer(llvm::PointerType * T,QualType QT)2041 llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {
2042   return getTargetCodeGenInfo().getNullPointer(*this, T, QT);
2043 }
2044 
2045 namespace {
2046 /// A struct which can be used to peephole certain kinds of finalization
2047 /// that normally happen during l-value emission.
2048 struct ConstantLValue {
2049   llvm::Constant *Value;
2050   bool HasOffsetApplied;
2051   bool HasDestPointerAuth;
2052 
ConstantLValue__anon9d6f04f10b11::ConstantLValue2053   /*implicit*/ ConstantLValue(llvm::Constant *value,
2054                               bool hasOffsetApplied = false,
2055                               bool hasDestPointerAuth = false)
2056       : Value(value), HasOffsetApplied(hasOffsetApplied),
2057         HasDestPointerAuth(hasDestPointerAuth) {}
2058 
ConstantLValue__anon9d6f04f10b11::ConstantLValue2059   /*implicit*/ ConstantLValue(ConstantAddress address)
2060     : ConstantLValue(address.getPointer()) {}
2061 };
2062 
2063 /// A helper class for emitting constant l-values.
2064 class 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 
2075 public:
ConstantLValueEmitter(ConstantEmitter & emitter,const APValue & value,QualType destType,bool EnablePtrAuthFunctionTypeDiscrimination=true)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 
2085 private:
2086   llvm::Constant *tryEmitAbsolute(llvm::Type *destTy);
2087   ConstantLValue tryEmitBase(const APValue::LValueBase &base);
2088 
VisitStmt(const Stmt * S)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 
hasNonZeroOffset() const2110   bool hasNonZeroOffset() const {
2111     return !Value.getLValueOffset().isZero();
2112   }
2113 
2114   /// Return the value offset.
getOffset()2115   llvm::Constant *getOffset() {
2116     return llvm::ConstantInt::get(CGM.Int64Ty,
2117                                   Value.getLValueOffset().getQuantity());
2118   }
2119 
2120   /// Apply the value offset to the given constant.
applyOffset(llvm::Constant * C)2121   llvm::Constant *applyOffset(llvm::Constant *C) {
2122     if (!hasNonZeroOffset())
2123       return C;
2124 
2125     return llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset());
2126   }
2127 };
2128 
2129 }
2130 
tryEmit()2131 llvm::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(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(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(value, 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>(destTy))
2173     return llvm::ConstantExpr::getPointerCast(value, destTy);
2174 
2175   return llvm::ConstantExpr::getPtrToInt(value, destTy);
2176 }
2177 
2178 /// Try to emit an absolute l-value, such as a null pointer or an integer
2179 /// bitcast to pointer type.
2180 llvm::Constant *
tryEmitAbsolute(llvm::Type * destTy)2181 ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
2182   // If we're producing a pointer, this is easy.
2183   auto destPtrTy = cast<llvm::PointerType>(destTy);
2184   if (Value.isNullPointer()) {
2185     // FIXME: integer offsets from non-zero null pointers.
2186     return CGM.getNullPointer(destPtrTy, 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(getOffset(), intptrTy, /*isSigned*/ false,
2195                                     CGM.getDataLayout());
2196   assert(C && "Must have folded, as Offset is a ConstantInt");
2197   C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy);
2198   return C;
2199 }
2200 
2201 ConstantLValue
tryEmitBase(const APValue::LValueBase & base)2202 ConstantLValueEmitter::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>(D->getMostRecentDecl());
2208 
2209     if (D->hasAttr<WeakRefAttr>())
2210       return CGM.GetWeakRefReference(D).getPointer();
2211 
2212     auto PtrAuthSign = [&](llvm::Constant *C) {
2213       if (PointerAuthQualifier PointerAuth = DestType.getPointerAuth()) {
2214         C = applyOffset(C);
2215         C = Emitter.tryEmitConstantSignedPointer(C, PointerAuth);
2216         return ConstantLValue(C, /*applied offset*/ true, /*signed*/ true);
2217       }
2218 
2219       CGPointerAuthInfo AuthInfo;
2220 
2221       if (EnablePtrAuthFunctionTypeDiscrimination)
2222         AuthInfo = CGM.getFunctionPointerAuthInfo(DestType);
2223 
2224       if (AuthInfo) {
2225         if (hasNonZeroOffset())
2226           return ConstantLValue(nullptr);
2227 
2228         C = applyOffset(C);
2229         C = CGM.getConstantSignedPointer(
2230             C, AuthInfo.getKey(), nullptr,
2231             cast_or_null<llvm::ConstantInt>(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>(D)) {
2239       llvm::Constant *C = CGM.getRawFunctionPointer(FD);
2240       if (FD->getType()->isCFIUncheckedCalleeFunctionType())
2241         C = llvm::NoCFIValue::get(cast<llvm::GlobalValue>(C));
2242       return PtrAuthSign(C);
2243     }
2244 
2245     if (const auto *VD = dyn_cast<VarDecl>(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(VD);
2250 
2251         if (VD->isLocalVarDecl()) {
2252           return CGM.getOrCreateStaticVarDecl(
2253               *VD, CGM.getLLVMLinkageVarDefinition(VD));
2254         }
2255       }
2256     }
2257 
2258     if (const auto *GD = dyn_cast<MSGuidDecl>(D))
2259       return CGM.GetAddrOfMSGuidDecl(GD);
2260 
2261     if (const auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(D))
2262       return CGM.GetAddrOfUnnamedGlobalConstantDecl(GCD);
2263 
2264     if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(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(QualType(TI.getType(), 0));
2273 
2274   // Otherwise, it must be an expression.
2275   return Visit(base.get<const Expr*>());
2276 }
2277 
2278 ConstantLValue
VisitConstantExpr(const ConstantExpr * E)2279 ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) {
2280   if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(E))
2281     return Result;
2282   return Visit(E->getSubExpr());
2283 }
2284 
2285 ConstantLValue
VisitCompoundLiteralExpr(const CompoundLiteralExpr * E)2286 ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2287   ConstantEmitter CompoundLiteralEmitter(CGM, Emitter.CGF);
2288   CompoundLiteralEmitter.setInConstantContext(Emitter.isInConstantContext());
2289   return tryEmitGlobalCompoundLiteral(CompoundLiteralEmitter, E);
2290 }
2291 
2292 ConstantLValue
VisitStringLiteral(const StringLiteral * E)2293 ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) {
2294   return CGM.GetAddrOfConstantStringFromLiteral(E);
2295 }
2296 
2297 ConstantLValue
VisitObjCEncodeExpr(const ObjCEncodeExpr * E)2298 ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2299   return CGM.GetAddrOfConstantStringFromObjCEncode(E);
2300 }
2301 
emitConstantObjCStringLiteral(const StringLiteral * S,QualType T,CodeGenModule & CGM)2302 static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S,
2303                                                     QualType T,
2304                                                     CodeGenModule &CGM) {
2305   auto C = CGM.getObjCRuntime().GenerateConstantString(S);
2306   return C.withElementType(CGM.getTypes().ConvertTypeForMem(T));
2307 }
2308 
2309 ConstantLValue
VisitObjCStringLiteral(const ObjCStringLiteral * E)2310 ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
2311   return emitConstantObjCStringLiteral(E->getString(), E->getType(), CGM);
2312 }
2313 
2314 ConstantLValue
VisitObjCBoxedExpr(const ObjCBoxedExpr * E)2315 ConstantLValueEmitter::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>(E->getSubExpr()->IgnoreParenCasts());
2319   return emitConstantObjCStringLiteral(SL, E->getType(), CGM);
2320 }
2321 
2322 ConstantLValue
VisitPredefinedExpr(const PredefinedExpr * E)2323 ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) {
2324   return CGM.GetAddrOfConstantStringFromLiteral(E->getFunctionName());
2325 }
2326 
2327 ConstantLValue
VisitAddrLabelExpr(const AddrLabelExpr * E)2328 ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) {
2329   assert(Emitter.CGF && "Invalid address of label expression outside function");
2330   llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel());
2331   return Ptr;
2332 }
2333 
2334 ConstantLValue
VisitCallExpr(const CallExpr * E)2335 ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
2336   unsigned builtin = E->getBuiltinCallee();
2337   if (builtin == Builtin::BI__builtin_function_start)
2338     return CGM.GetFunctionStart(
2339         E->getArg(0)->getAsBuiltinConstantDeclRef(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>(E->getArg(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 
2357 ConstantLValue
emitPointerAuthSignConstant(const CallExpr * E)2358 ConstantLValueEmitter::emitPointerAuthSignConstant(const CallExpr *E) {
2359   llvm::Constant *UnsignedPointer = emitPointerAuthPointer(E->getArg(0));
2360   unsigned Key = emitPointerAuthKey(E->getArg(1));
2361   auto [StorageAddress, OtherDiscriminator] =
2362       emitPointerAuthDiscriminator(E->getArg(2));
2363 
2364   llvm::Constant *SignedPointer = CGM.getConstantSignedPointer(
2365       UnsignedPointer, Key, StorageAddress, OtherDiscriminator);
2366   return SignedPointer;
2367 }
2368 
emitPointerAuthPointer(const Expr * E)2369 llvm::Constant *ConstantLValueEmitter::emitPointerAuthPointer(const Expr *E) {
2370   Expr::EvalResult Result;
2371   bool Succeeded = E->EvaluateAsRValue(Result, 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>(Result.Val.getLValueBase().get<const ValueDecl *>()))
2378     assert(Result.Val.getLValueOffset().isZero());
2379   return ConstantEmitter(CGM, Emitter.CGF)
2380       .emitAbstract(E->getExprLoc(), Result.Val, E->getType(), false);
2381 }
2382 
emitPointerAuthKey(const Expr * E)2383 unsigned ConstantLValueEmitter::emitPointerAuthKey(const Expr *E) {
2384   return E->EvaluateKnownConstInt(CGM.getContext()).getZExtValue();
2385 }
2386 
2387 std::pair<llvm::Constant *, llvm::ConstantInt *>
emitPointerAuthDiscriminator(const Expr * E)2388 ConstantLValueEmitter::emitPointerAuthDiscriminator(const Expr *E) {
2389   E = E->IgnoreParens();
2390 
2391   if (const auto *Call = dyn_cast<CallExpr>(E)) {
2392     if (Call->getBuiltinCallee() ==
2393         Builtin::BI__builtin_ptrauth_blend_discriminator) {
2394       llvm::Constant *Pointer = ConstantEmitter(CGM).emitAbstract(
2395           Call->getArg(0), Call->getArg(0)->getType());
2396       auto *Extra = cast<llvm::ConstantInt>(ConstantEmitter(CGM).emitAbstract(
2397           Call->getArg(1), Call->getArg(1)->getType()));
2398       return {Pointer, Extra};
2399     }
2400   }
2401 
2402   llvm::Constant *Result = ConstantEmitter(CGM).emitAbstract(E, E->getType());
2403   if (Result->getType()->isPointerTy())
2404     return {Result, nullptr};
2405   return {nullptr, cast<llvm::ConstantInt>(Result)};
2406 }
2407 
2408 ConstantLValue
VisitBlockExpr(const BlockExpr * E)2409 ConstantLValueEmitter::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(E, functionName);
2417 }
2418 
2419 ConstantLValue
VisitCXXTypeidExpr(const CXXTypeidExpr * E)2420 ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
2421   QualType T;
2422   if (E->isTypeOperand())
2423     T = E->getTypeOperand(CGM.getContext());
2424   else
2425     T = E->getExprOperand()->getType();
2426   return CGM.GetAddrOfRTTIDescriptor(T);
2427 }
2428 
2429 ConstantLValue
VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr * E)2430 ConstantLValueEmitter::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 
2437 llvm::Constant *
tryEmitPrivate(const APValue & Value,QualType DestType,bool EnablePtrAuthFunctionTypeDiscrimination)2438 ConstantEmitter::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(CGM.getTypes().ConvertType(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(CGM.getLLVMContext(), Value.getInt());
2455   case APValue::FixedPoint:
2456     return llvm::ConstantInt::get(CGM.getLLVMContext(),
2457                                   Value.getFixedPoint().getValue());
2458   case APValue::ComplexInt: {
2459     llvm::Constant *Complex[2];
2460 
2461     Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2462                                         Value.getComplexIntReal());
2463     Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2464                                         Value.getComplexIntImag());
2465 
2466     // FIXME: the target may want to specify that this is packed.
2467     llvm::StructType *STy =
2468         llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2469     return llvm::ConstantStruct::get(STy, 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(CGM.getLLVMContext(),
2477                                     Init.bitcastToAPInt());
2478     else
2479       return llvm::ConstantFP::get(CGM.getLLVMContext(), Init);
2480   }
2481   case APValue::ComplexFloat: {
2482     llvm::Constant *Complex[2];
2483 
2484     Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2485                                        Value.getComplexFloatReal());
2486     Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2487                                        Value.getComplexFloatImag());
2488 
2489     // FIXME: the target may want to specify that this is packed.
2490     llvm::StructType *STy =
2491         llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2492     return llvm::ConstantStruct::get(STy, 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(CGM.getLLVMContext(), Elt.getInt());
2502       else if (Elt.isFloat())
2503         Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
2504       else if (Elt.isIndeterminate())
2505         Inits[I] = llvm::UndefValue::get(CGM.getTypes().ConvertType(
2506             DestType->castAs<VectorType>()->getElementType()));
2507       else
2508         llvm_unreachable("unsupported vector element type");
2509     }
2510     return llvm::ConstantVector::get(Inits);
2511   }
2512   case APValue::AddrLabelDiff: {
2513     const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
2514     const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
2515     llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType());
2516     llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType());
2517     if (!LHS || !RHS) return nullptr;
2518 
2519     // Compute difference
2520     llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType);
2521     LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy);
2522     RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy);
2523     llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, 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(AddrLabelDiff, ResultType);
2529   }
2530   case APValue::Struct:
2531   case APValue::Union:
2532     return ConstStructBuilder::BuildStruct(*this, Value, DestType);
2533   case APValue::Array: {
2534     const ArrayType *ArrayTy = CGM.getContext().getAsArrayType(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.getArrayFiller(),
2542                                         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(NumInitElts + 1);
2551     else
2552       Elts.reserve(NumElements);
2553 
2554     llvm::Type *CommonElementType = nullptr;
2555     for (unsigned I = 0; I < NumInitElts; ++I) {
2556       llvm::Constant *C = tryEmitPrivateForMemory(
2557           Value.getArrayInitializedElt(I), 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(C);
2565     }
2566 
2567     llvm::ArrayType *Desired =
2568         cast<llvm::ArrayType>(CGM.getTypes().ConvertType(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(Desired->getElementType(), Elts.size());
2573 
2574     return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
2575                              Filler);
2576   }
2577   case APValue::MemberPointer:
2578     return CGM.getCXXABI().EmitMemberPointer(Value, DestType);
2579   }
2580   llvm_unreachable("Unknown APValue kind");
2581 }
2582 
getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr * E)2583 llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted(
2584     const CompoundLiteralExpr *E) {
2585   return EmittedCompoundLiterals.lookup(E);
2586 }
2587 
setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr * CLE,llvm::GlobalVariable * GV)2588 void CodeGenModule::setAddrOfConstantCompoundLiteral(
2589     const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) {
2590   bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second;
2591   (void)Ok;
2592   assert(Ok && "CLE has already been emitted!");
2593 }
2594 
2595 ConstantAddress
GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr * E)2596 CodeGenModule::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 
2602 llvm::Constant *
getMemberPointerConstant(const UnaryOperator * uo)2603 CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
2604   // Member pointer constants always have a very particular form.
2605   const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
2606   const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
2607 
2608   // A member function pointer.
2609   if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
2610     return getCXXABI().EmitMemberFunctionPointer(method);
2611 
2612   // Otherwise, a member data pointer.
2613   uint64_t fieldOffset = getContext().getFieldOffset(decl);
2614   CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
2615   return getCXXABI().EmitMemberDataPointer(type, chars);
2616 }
2617 
2618 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2619                                                llvm::Type *baseType,
2620                                                const CXXRecordDecl *base);
2621 
EmitNullConstant(CodeGenModule & CGM,const RecordDecl * record,bool asCompleteObject)2622 static 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>(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>(I.getType()->castAs<RecordType>()->getDecl());
2645 
2646       // Ignore empty bases.
2647       if (isEmptyRecordForLayout(CGM.getContext(), I.getType()) ||
2648           CGM.getContext()
2649               .getASTRecordLayout(base)
2650               .getNonVirtualSize()
2651               .isZero())
2652         continue;
2653 
2654       unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
2655       llvm::Type *baseType = structure->getElementType(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(CGM.getContext(), Field)) {
2666       unsigned fieldIndex = layout.getLLVMFieldNo(Field);
2667       elements[fieldIndex] = CGM.EmitNullConstant(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>(I.getType()->castAs<RecordType>()->getDecl());
2685 
2686       // Ignore empty bases.
2687       if (isEmptyRecordForLayout(CGM.getContext(), 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(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(structure->getElementType(i));
2704   }
2705 
2706   return llvm::ConstantStruct::get(structure, elements);
2707 }
2708 
2709 /// Emit the null constant for a base subobject.
EmitNullConstantForBase(CodeGenModule & CGM,llvm::Type * baseType,const CXXRecordDecl * base)2710 static 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(baseType);
2718 
2719   // Otherwise, we can just use its null constant.
2720   return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
2721 }
2722 
emitNullForMemory(CodeGenModule & CGM,QualType T)2723 llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM,
2724                                                    QualType T) {
2725   return emitForMemory(CGM, CGM.EmitNullConstant(T), T);
2726 }
2727 
EmitNullConstant(QualType T)2728 llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
2729   if (T->getAs<PointerType>())
2730     return getNullPointer(
2731         cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T);
2732 
2733   if (getTypes().isZeroInitializable(T))
2734     return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
2735 
2736   if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
2737     llvm::ArrayType *ATy =
2738       cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
2739 
2740     QualType ElementTy = CAT->getElementType();
2741 
2742     llvm::Constant *Element =
2743       ConstantEmitter::emitNullForMemory(*this, ElementTy);
2744     unsigned NumElements = CAT->getZExtSize();
2745     SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
2746     return llvm::ConstantArray::get(ATy, Array);
2747   }
2748 
2749   if (const RecordType *RT = T->getAs<RecordType>())
2750     return ::EmitNullConstant(*this, RT->getDecl(), /*complete object*/ true);
2751 
2752   assert(T->isMemberDataPointerType() &&
2753          "Should only see pointers to data members here!");
2754 
2755   return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
2756 }
2757 
2758 llvm::Constant *
EmitNullConstantForBase(const CXXRecordDecl * Record)2759 CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
2760   return ::EmitNullConstant(*this, Record, false);
2761 }
2762