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