1 //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to emit Constant Expr nodes as LLVM code.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenFunction.h"
16 #include "CGObjCRuntime.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenModule.h"
19 #include "ConstantEmitter.h"
20 #include "TargetInfo.h"
21 #include "clang/AST/APValue.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/RecordLayout.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/Basic/Builtins.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/GlobalVariable.h"
30 using namespace clang;
31 using namespace CodeGen;
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
38 class ConstExprEmitter;
39 class ConstStructBuilder {
41 ConstantEmitter &Emitter;
44 CharUnits NextFieldOffsetInChars;
45 CharUnits LLVMStructAlignment;
46 SmallVector<llvm::Constant *, 32> Elements;
48 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
49 ConstExprEmitter *ExprEmitter,
50 llvm::ConstantStruct *Base,
51 InitListExpr *Updater,
53 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
54 InitListExpr *ILE, QualType StructTy);
55 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
56 const APValue &Value, QualType ValTy);
59 ConstStructBuilder(ConstantEmitter &emitter)
60 : CGM(emitter.CGM), Emitter(emitter), Packed(false),
61 NextFieldOffsetInChars(CharUnits::Zero()),
62 LLVMStructAlignment(CharUnits::One()) { }
64 void AppendField(const FieldDecl *Field, uint64_t FieldOffset,
65 llvm::Constant *InitExpr);
67 void AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst);
69 void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
70 llvm::ConstantInt *InitExpr);
72 void AppendPadding(CharUnits PadSize);
74 void AppendTailPadding(CharUnits RecordSize);
76 void ConvertStructToPacked();
78 bool Build(InitListExpr *ILE);
79 bool Build(ConstExprEmitter *Emitter, llvm::ConstantStruct *Base,
80 InitListExpr *Updater);
81 bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
82 const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
83 llvm::Constant *Finalize(QualType Ty);
85 CharUnits getAlignment(const llvm::Constant *C) const {
86 if (Packed) return CharUnits::One();
87 return CharUnits::fromQuantity(
88 CGM.getDataLayout().getABITypeAlignment(C->getType()));
91 CharUnits getSizeInChars(const llvm::Constant *C) const {
92 return CharUnits::fromQuantity(
93 CGM.getDataLayout().getTypeAllocSize(C->getType()));
97 void ConstStructBuilder::
98 AppendField(const FieldDecl *Field, uint64_t FieldOffset,
99 llvm::Constant *InitCst) {
100 const ASTContext &Context = CGM.getContext();
102 CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
104 AppendBytes(FieldOffsetInChars, InitCst);
107 void ConstStructBuilder::
108 AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst) {
110 assert(NextFieldOffsetInChars <= FieldOffsetInChars
111 && "Field offset mismatch!");
113 CharUnits FieldAlignment = getAlignment(InitCst);
115 // Round up the field offset to the alignment of the field type.
116 CharUnits AlignedNextFieldOffsetInChars =
117 NextFieldOffsetInChars.alignTo(FieldAlignment);
119 if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) {
120 // We need to append padding.
121 AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
123 assert(NextFieldOffsetInChars == FieldOffsetInChars &&
124 "Did not add enough padding!");
126 AlignedNextFieldOffsetInChars =
127 NextFieldOffsetInChars.alignTo(FieldAlignment);
130 if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
131 assert(!Packed && "Alignment is wrong even with a packed struct!");
133 // Convert the struct to a packed struct.
134 ConvertStructToPacked();
136 // After we pack the struct, we may need to insert padding.
137 if (NextFieldOffsetInChars < FieldOffsetInChars) {
138 // We need to append padding.
139 AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
141 assert(NextFieldOffsetInChars == FieldOffsetInChars &&
142 "Did not add enough padding!");
144 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
148 Elements.push_back(InitCst);
149 NextFieldOffsetInChars = AlignedNextFieldOffsetInChars +
150 getSizeInChars(InitCst);
153 assert(LLVMStructAlignment == CharUnits::One() &&
154 "Packed struct not byte-aligned!");
156 LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
159 void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
160 uint64_t FieldOffset,
161 llvm::ConstantInt *CI) {
162 const ASTContext &Context = CGM.getContext();
163 const uint64_t CharWidth = Context.getCharWidth();
164 uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
165 if (FieldOffset > NextFieldOffsetInBits) {
166 // We need to add padding.
167 CharUnits PadSize = Context.toCharUnitsFromBits(
168 llvm::alignTo(FieldOffset - NextFieldOffsetInBits,
169 Context.getTargetInfo().getCharAlign()));
171 AppendPadding(PadSize);
174 uint64_t FieldSize = Field->getBitWidthValue(Context);
176 llvm::APInt FieldValue = CI->getValue();
178 // Promote the size of FieldValue if necessary
179 // FIXME: This should never occur, but currently it can because initializer
180 // constants are cast to bool, and because clang is not enforcing bitfield
182 if (FieldSize > FieldValue.getBitWidth())
183 FieldValue = FieldValue.zext(FieldSize);
185 // Truncate the size of FieldValue to the bit field size.
186 if (FieldSize < FieldValue.getBitWidth())
187 FieldValue = FieldValue.trunc(FieldSize);
189 NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
190 if (FieldOffset < NextFieldOffsetInBits) {
191 // Either part of the field or the entire field can go into the previous
193 assert(!Elements.empty() && "Elements can't be empty!");
195 unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset;
197 bool FitsCompletelyInPreviousByte =
198 BitsInPreviousByte >= FieldValue.getBitWidth();
200 llvm::APInt Tmp = FieldValue;
202 if (!FitsCompletelyInPreviousByte) {
203 unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
205 if (CGM.getDataLayout().isBigEndian()) {
206 Tmp.lshrInPlace(NewFieldWidth);
207 Tmp = Tmp.trunc(BitsInPreviousByte);
209 // We want the remaining high bits.
210 FieldValue = FieldValue.trunc(NewFieldWidth);
212 Tmp = Tmp.trunc(BitsInPreviousByte);
214 // We want the remaining low bits.
215 FieldValue.lshrInPlace(BitsInPreviousByte);
216 FieldValue = FieldValue.trunc(NewFieldWidth);
220 Tmp = Tmp.zext(CharWidth);
221 if (CGM.getDataLayout().isBigEndian()) {
222 if (FitsCompletelyInPreviousByte)
223 Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
225 Tmp = Tmp.shl(CharWidth - BitsInPreviousByte);
228 // 'or' in the bits that go into the previous byte.
229 llvm::Value *LastElt = Elements.back();
230 if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt))
231 Tmp |= Val->getValue();
233 assert(isa<llvm::UndefValue>(LastElt));
234 // If there is an undef field that we're adding to, it can either be a
235 // scalar undef (in which case, we just replace it with our field) or it
236 // is an array. If it is an array, we have to pull one byte off the
237 // array so that the other undef bytes stay around.
238 if (!isa<llvm::IntegerType>(LastElt->getType())) {
239 // The undef padding will be a multibyte array, create a new smaller
240 // padding and then an hole for our i8 to get plopped into.
241 assert(isa<llvm::ArrayType>(LastElt->getType()) &&
242 "Expected array padding of undefs");
243 llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
244 assert(AT->getElementType()->isIntegerTy(CharWidth) &&
245 AT->getNumElements() != 0 &&
246 "Expected non-empty array padding of undefs");
248 // Remove the padding array.
249 NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements());
252 // Add the padding back in two chunks.
253 AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1));
254 AppendPadding(CharUnits::One());
255 assert(isa<llvm::UndefValue>(Elements.back()) &&
256 Elements.back()->getType()->isIntegerTy(CharWidth) &&
257 "Padding addition didn't work right");
261 Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
263 if (FitsCompletelyInPreviousByte)
267 while (FieldValue.getBitWidth() > CharWidth) {
270 if (CGM.getDataLayout().isBigEndian()) {
271 // We want the high bits.
273 FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth);
275 // We want the low bits.
276 Tmp = FieldValue.trunc(CharWidth);
278 FieldValue.lshrInPlace(CharWidth);
281 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
282 ++NextFieldOffsetInChars;
284 FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth);
287 assert(FieldValue.getBitWidth() > 0 &&
288 "Should have at least one bit left!");
289 assert(FieldValue.getBitWidth() <= CharWidth &&
290 "Should not have more than a byte left!");
292 if (FieldValue.getBitWidth() < CharWidth) {
293 if (CGM.getDataLayout().isBigEndian()) {
294 unsigned BitWidth = FieldValue.getBitWidth();
296 FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth);
298 FieldValue = FieldValue.zext(CharWidth);
301 // Append the last element.
302 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
304 ++NextFieldOffsetInChars;
307 void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
308 if (PadSize.isZero())
311 llvm::Type *Ty = CGM.Int8Ty;
312 if (PadSize > CharUnits::One())
313 Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
315 llvm::Constant *C = llvm::UndefValue::get(Ty);
316 Elements.push_back(C);
317 assert(getAlignment(C) == CharUnits::One() &&
318 "Padding must have 1 byte alignment!");
320 NextFieldOffsetInChars += getSizeInChars(C);
323 void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
324 assert(NextFieldOffsetInChars <= RecordSize &&
327 AppendPadding(RecordSize - NextFieldOffsetInChars);
330 void ConstStructBuilder::ConvertStructToPacked() {
331 SmallVector<llvm::Constant *, 16> PackedElements;
332 CharUnits ElementOffsetInChars = CharUnits::Zero();
334 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
335 llvm::Constant *C = Elements[i];
337 CharUnits ElementAlign = CharUnits::fromQuantity(
338 CGM.getDataLayout().getABITypeAlignment(C->getType()));
339 CharUnits AlignedElementOffsetInChars =
340 ElementOffsetInChars.alignTo(ElementAlign);
342 if (AlignedElementOffsetInChars > ElementOffsetInChars) {
343 // We need some padding.
345 AlignedElementOffsetInChars - ElementOffsetInChars;
347 llvm::Type *Ty = CGM.Int8Ty;
348 if (NumChars > CharUnits::One())
349 Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity());
351 llvm::Constant *Padding = llvm::UndefValue::get(Ty);
352 PackedElements.push_back(Padding);
353 ElementOffsetInChars += getSizeInChars(Padding);
356 PackedElements.push_back(C);
357 ElementOffsetInChars += getSizeInChars(C);
360 assert(ElementOffsetInChars == NextFieldOffsetInChars &&
361 "Packing the struct changed its size!");
363 Elements.swap(PackedElements);
364 LLVMStructAlignment = CharUnits::One();
368 bool ConstStructBuilder::Build(InitListExpr *ILE) {
369 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
370 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
372 unsigned FieldNo = 0;
373 unsigned ElementNo = 0;
375 // Bail out if we have base classes. We could support these, but they only
376 // arise in C++1z where we will have already constant folded most interesting
377 // cases. FIXME: There are still a few more cases we can handle this way.
378 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
379 if (CXXRD->getNumBases())
382 for (RecordDecl::field_iterator Field = RD->field_begin(),
383 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
384 // If this is a union, skip all the fields that aren't being initialized.
385 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
388 // Don't emit anonymous bitfields, they just affect layout.
389 if (Field->isUnnamedBitfield())
392 // Get the initializer. A struct can include fields without initializers,
393 // we just use explicit null values for them.
394 llvm::Constant *EltInit;
395 if (ElementNo < ILE->getNumInits())
396 EltInit = Emitter.tryEmitPrivateForMemory(ILE->getInit(ElementNo++),
399 EltInit = Emitter.emitNullForMemory(Field->getType());
404 if (!Field->isBitField()) {
405 // Handle non-bitfield members.
406 AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit);
408 // Otherwise we have a bitfield.
409 if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) {
410 AppendBitField(*Field, Layout.getFieldOffset(FieldNo), CI);
412 // We are trying to initialize a bitfield with a non-trivial constant,
413 // this must require run-time code.
424 BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
425 : Decl(Decl), Offset(Offset), Index(Index) {
428 const CXXRecordDecl *Decl;
432 bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
436 bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
438 const CXXRecordDecl *VTableClass,
440 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
442 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
443 // Add a vtable pointer, if we need one and it hasn't already been added.
444 if (CD->isDynamicClass() && !IsPrimaryBase) {
445 llvm::Constant *VTableAddressPoint =
446 CGM.getCXXABI().getVTableAddressPointForConstExpr(
447 BaseSubobject(CD, Offset), VTableClass);
448 AppendBytes(Offset, VTableAddressPoint);
451 // Accumulate and sort bases, in order to visit them in address order, which
452 // may not be the same as declaration order.
453 SmallVector<BaseInfo, 8> Bases;
454 Bases.reserve(CD->getNumBases());
456 for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
457 BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
458 assert(!Base->isVirtual() && "should not have virtual bases here");
459 const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
460 CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
461 Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
463 std::stable_sort(Bases.begin(), Bases.end());
465 for (unsigned I = 0, N = Bases.size(); I != N; ++I) {
466 BaseInfo &Base = Bases[I];
468 bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
469 Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
470 VTableClass, Offset + Base.Offset);
474 unsigned FieldNo = 0;
475 uint64_t OffsetBits = CGM.getContext().toBits(Offset);
477 for (RecordDecl::field_iterator Field = RD->field_begin(),
478 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
479 // If this is a union, skip all the fields that aren't being initialized.
480 if (RD->isUnion() && Val.getUnionField() != *Field)
483 // Don't emit anonymous bitfields, they just affect layout.
484 if (Field->isUnnamedBitfield())
487 // Emit the value of the initializer.
488 const APValue &FieldValue =
489 RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
490 llvm::Constant *EltInit =
491 Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType());
495 if (!Field->isBitField()) {
496 // Handle non-bitfield members.
497 AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, EltInit);
499 // Otherwise we have a bitfield.
500 AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
501 cast<llvm::ConstantInt>(EltInit));
508 llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) {
509 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
510 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
512 CharUnits LayoutSizeInChars = Layout.getSize();
514 if (NextFieldOffsetInChars > LayoutSizeInChars) {
515 // If the struct is bigger than the size of the record type,
516 // we must have a flexible array member at the end.
517 assert(RD->hasFlexibleArrayMember() &&
518 "Must have flexible array member if struct is bigger than type!");
520 // No tail padding is necessary.
522 // Append tail padding if necessary.
523 CharUnits LLVMSizeInChars =
524 NextFieldOffsetInChars.alignTo(LLVMStructAlignment);
526 if (LLVMSizeInChars != LayoutSizeInChars)
527 AppendTailPadding(LayoutSizeInChars);
529 LLVMSizeInChars = NextFieldOffsetInChars.alignTo(LLVMStructAlignment);
531 // Check if we need to convert the struct to a packed struct.
532 if (NextFieldOffsetInChars <= LayoutSizeInChars &&
533 LLVMSizeInChars > LayoutSizeInChars) {
534 assert(!Packed && "Size mismatch!");
536 ConvertStructToPacked();
537 assert(NextFieldOffsetInChars <= LayoutSizeInChars &&
538 "Converting to packed did not help!");
541 LLVMSizeInChars = NextFieldOffsetInChars.alignTo(LLVMStructAlignment);
543 assert(LayoutSizeInChars == LLVMSizeInChars &&
544 "Tail padding mismatch!");
547 // Pick the type to use. If the type is layout identical to the ConvertType
548 // type then use it, otherwise use whatever the builder produced for us.
549 llvm::StructType *STy =
550 llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(),
552 llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty);
553 if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) {
554 if (ValSTy->isLayoutIdentical(STy))
558 llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements);
560 assert(NextFieldOffsetInChars.alignTo(getAlignment(Result)) ==
561 getSizeInChars(Result) &&
567 llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
568 ConstExprEmitter *ExprEmitter,
569 llvm::ConstantStruct *Base,
570 InitListExpr *Updater,
572 ConstStructBuilder Builder(Emitter);
573 if (!Builder.Build(ExprEmitter, Base, Updater))
575 return Builder.Finalize(ValTy);
578 llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
581 ConstStructBuilder Builder(Emitter);
583 if (!Builder.Build(ILE))
586 return Builder.Finalize(ValTy);
589 llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
592 ConstStructBuilder Builder(Emitter);
594 const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
595 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
596 if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero()))
599 return Builder.Finalize(ValTy);
603 //===----------------------------------------------------------------------===//
605 //===----------------------------------------------------------------------===//
607 static ConstantAddress tryEmitGlobalCompoundLiteral(CodeGenModule &CGM,
608 CodeGenFunction *CGF,
609 const CompoundLiteralExpr *E) {
610 CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType());
611 if (llvm::GlobalVariable *Addr =
612 CGM.getAddrOfConstantCompoundLiteralIfEmitted(E))
613 return ConstantAddress(Addr, Align);
615 LangAS addressSpace = E->getType().getAddressSpace();
617 ConstantEmitter emitter(CGM, CGF);
618 llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(),
619 addressSpace, E->getType());
621 assert(!E->isFileScope() &&
622 "file-scope compound literal did not have constant initializer!");
623 return ConstantAddress::invalid();
626 auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
627 CGM.isTypeConstant(E->getType(), true),
628 llvm::GlobalValue::InternalLinkage,
629 C, ".compoundliteral", nullptr,
630 llvm::GlobalVariable::NotThreadLocal,
631 CGM.getContext().getTargetAddressSpace(addressSpace));
632 emitter.finalize(GV);
633 GV->setAlignment(Align.getQuantity());
634 CGM.setAddrOfConstantCompoundLiteral(E, GV);
635 return ConstantAddress(GV, Align);
638 static llvm::Constant *
639 EmitArrayConstant(CodeGenModule &CGM, const ConstantArrayType *DestType,
640 llvm::Type *CommonElementType, unsigned ArrayBound,
641 SmallVectorImpl<llvm::Constant *> &Elements,
642 llvm::Constant *Filler) {
643 // Figure out how long the initial prefix of non-zero elements is.
644 unsigned NonzeroLength = ArrayBound;
645 if (Elements.size() < NonzeroLength && Filler->isNullValue())
646 NonzeroLength = Elements.size();
647 if (NonzeroLength == Elements.size()) {
648 while (NonzeroLength > 0 && Elements[NonzeroLength - 1]->isNullValue())
652 if (NonzeroLength == 0) {
653 return llvm::ConstantAggregateZero::get(
654 CGM.getTypes().ConvertType(QualType(DestType, 0)));
657 // Add a zeroinitializer array filler if we have lots of trailing zeroes.
658 unsigned TrailingZeroes = ArrayBound - NonzeroLength;
659 if (TrailingZeroes >= 8) {
660 assert(Elements.size() >= NonzeroLength &&
661 "missing initializer for non-zero element");
663 // If all the elements had the same type up to the trailing zeroes, emit a
664 // struct of two arrays (the nonzero data and the zeroinitializer).
665 if (CommonElementType && NonzeroLength >= 8) {
666 llvm::Constant *Initial = llvm::ConstantArray::get(
667 llvm::ArrayType::get(CommonElementType, NonzeroLength),
668 makeArrayRef(Elements).take_front(NonzeroLength));
670 Elements[0] = Initial;
672 Elements.resize(NonzeroLength + 1);
678 : CGM.getTypes().ConvertType(DestType->getElementType());
679 FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
680 Elements.back() = llvm::ConstantAggregateZero::get(FillerType);
681 CommonElementType = nullptr;
682 } else if (Elements.size() != ArrayBound) {
683 // Otherwise pad to the right size with the filler if necessary.
684 Elements.resize(ArrayBound, Filler);
685 if (Filler->getType() != CommonElementType)
686 CommonElementType = nullptr;
689 // If all elements have the same type, just emit an array constant.
690 if (CommonElementType)
691 return llvm::ConstantArray::get(
692 llvm::ArrayType::get(CommonElementType, ArrayBound), Elements);
694 // We have mixed types. Use a packed struct.
695 llvm::SmallVector<llvm::Type *, 16> Types;
696 Types.reserve(Elements.size());
697 for (llvm::Constant *Elt : Elements)
698 Types.push_back(Elt->getType());
699 llvm::StructType *SType =
700 llvm::StructType::get(CGM.getLLVMContext(), Types, true);
701 return llvm::ConstantStruct::get(SType, Elements);
704 /// This class only needs to handle two cases:
705 /// 1) Literals (this is used by APValue emission to emit literals).
706 /// 2) Arrays, structs and unions (outside C++11 mode, we don't currently
707 /// constant fold these types).
708 class ConstExprEmitter :
709 public StmtVisitor<ConstExprEmitter, llvm::Constant*, QualType> {
711 ConstantEmitter &Emitter;
712 llvm::LLVMContext &VMContext;
714 ConstExprEmitter(ConstantEmitter &emitter)
715 : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) {
718 //===--------------------------------------------------------------------===//
720 //===--------------------------------------------------------------------===//
722 llvm::Constant *VisitStmt(Stmt *S, QualType T) {
726 llvm::Constant *VisitParenExpr(ParenExpr *PE, QualType T) {
727 return Visit(PE->getSubExpr(), T);
731 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE,
733 return Visit(PE->getReplacement(), T);
736 llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE,
738 return Visit(GE->getResultExpr(), T);
741 llvm::Constant *VisitChooseExpr(ChooseExpr *CE, QualType T) {
742 return Visit(CE->getChosenSubExpr(), T);
745 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) {
746 return Visit(E->getInitializer(), T);
749 llvm::Constant *VisitCastExpr(CastExpr *E, QualType destType) {
750 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
751 CGM.EmitExplicitCastExprType(ECE, Emitter.CGF);
752 Expr *subExpr = E->getSubExpr();
754 switch (E->getCastKind()) {
756 // GCC cast to union extension
757 assert(E->getType()->isUnionType() &&
758 "Destination type is not union type!");
760 auto field = E->getTargetUnionField();
762 auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType());
763 if (!C) return nullptr;
765 auto destTy = ConvertType(destType);
766 if (C->getType() == destTy) return C;
768 // Build a struct with the union sub-element as the first member,
769 // and padded to the appropriate size.
770 SmallVector<llvm::Constant*, 2> Elts;
771 SmallVector<llvm::Type*, 2> Types;
773 Types.push_back(C->getType());
774 unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
775 unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy);
777 assert(CurSize <= TotalSize && "Union size mismatch!");
778 if (unsigned NumPadBytes = TotalSize - CurSize) {
779 llvm::Type *Ty = CGM.Int8Ty;
781 Ty = llvm::ArrayType::get(Ty, NumPadBytes);
783 Elts.push_back(llvm::UndefValue::get(Ty));
787 llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false);
788 return llvm::ConstantStruct::get(STy, Elts);
791 case CK_AddressSpaceConversion: {
792 auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
793 if (!C) return nullptr;
794 LangAS destAS = E->getType()->getPointeeType().getAddressSpace();
795 LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace();
796 llvm::Type *destTy = ConvertType(E->getType());
797 return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS,
801 case CK_LValueToRValue:
802 case CK_AtomicToNonAtomic:
803 case CK_NonAtomicToAtomic:
805 case CK_ConstructorConversion:
806 return Visit(subExpr, destType);
808 case CK_IntToOCLSampler:
809 llvm_unreachable("global sampler variables are not generated");
811 case CK_Dependent: llvm_unreachable("saw dependent cast!");
813 case CK_BuiltinFnToFnPtr:
814 llvm_unreachable("builtin functions are handled elsewhere");
816 case CK_ReinterpretMemberPointer:
817 case CK_DerivedToBaseMemberPointer:
818 case CK_BaseToDerivedMemberPointer: {
819 auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
820 if (!C) return nullptr;
821 return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
824 // These will never be supported.
825 case CK_ObjCObjectLValueCast:
826 case CK_ARCProduceObject:
827 case CK_ARCConsumeObject:
828 case CK_ARCReclaimReturnedObject:
829 case CK_ARCExtendBlockObject:
830 case CK_CopyAndAutoreleaseBlockObject:
833 // These don't need to be handled here because Evaluate knows how to
834 // evaluate them in the cases where they can be folded.
838 case CK_LValueBitCast:
839 case CK_NullToMemberPointer:
840 case CK_UserDefinedConversion:
841 case CK_CPointerToObjCPointerCast:
842 case CK_BlockPointerToObjCPointerCast:
843 case CK_AnyPointerToBlockPointerCast:
844 case CK_ArrayToPointerDecay:
845 case CK_FunctionToPointerDecay:
846 case CK_BaseToDerived:
847 case CK_DerivedToBase:
848 case CK_UncheckedDerivedToBase:
849 case CK_MemberPointerToBoolean:
851 case CK_FloatingRealToComplex:
852 case CK_FloatingComplexToReal:
853 case CK_FloatingComplexToBoolean:
854 case CK_FloatingComplexCast:
855 case CK_FloatingComplexToIntegralComplex:
856 case CK_IntegralRealToComplex:
857 case CK_IntegralComplexToReal:
858 case CK_IntegralComplexToBoolean:
859 case CK_IntegralComplexCast:
860 case CK_IntegralComplexToFloatingComplex:
861 case CK_PointerToIntegral:
862 case CK_PointerToBoolean:
863 case CK_NullToPointer:
864 case CK_IntegralCast:
865 case CK_BooleanToSignedIntegral:
866 case CK_IntegralToPointer:
867 case CK_IntegralToBoolean:
868 case CK_IntegralToFloating:
869 case CK_FloatingToIntegral:
870 case CK_FloatingToBoolean:
871 case CK_FloatingCast:
872 case CK_ZeroToOCLEvent:
873 case CK_ZeroToOCLQueue:
876 llvm_unreachable("Invalid CastKind");
879 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE, QualType T) {
880 return Visit(DAE->getExpr(), T);
883 llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE, QualType T) {
884 // No need for a DefaultInitExprScope: we don't handle 'this' in a
885 // constant expression.
886 return Visit(DIE->getExpr(), T);
889 llvm::Constant *VisitExprWithCleanups(ExprWithCleanups *E, QualType T) {
890 if (!E->cleanupsHaveSideEffects())
891 return Visit(E->getSubExpr(), T);
895 llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E,
897 return Visit(E->GetTemporaryExpr(), T);
900 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) {
901 auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
902 assert(CAT && "can't emit array init for non-constant-bound array");
903 unsigned NumInitElements = ILE->getNumInits();
904 unsigned NumElements = CAT->getSize().getZExtValue();
906 // Initialising an array requires us to automatically
907 // initialise any elements that have not been initialised explicitly
908 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
910 QualType EltType = CAT->getElementType();
912 // Initialize remaining array elements.
913 llvm::Constant *fillC = nullptr;
914 if (Expr *filler = ILE->getArrayFiller()) {
915 fillC = Emitter.tryEmitAbstractForMemory(filler, EltType);
920 // Copy initializer elements.
921 SmallVector<llvm::Constant*, 16> Elts;
922 if (fillC && fillC->isNullValue())
923 Elts.reserve(NumInitableElts + 1);
925 Elts.reserve(NumElements);
927 llvm::Type *CommonElementType = nullptr;
928 for (unsigned i = 0; i < NumInitableElts; ++i) {
929 Expr *Init = ILE->getInit(i);
930 llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType);
934 CommonElementType = C->getType();
935 else if (C->getType() != CommonElementType)
936 CommonElementType = nullptr;
940 return EmitArrayConstant(CGM, CAT, CommonElementType, NumElements, Elts,
944 llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) {
945 return ConstStructBuilder::BuildStruct(Emitter, ILE, T);
948 llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E,
950 return CGM.EmitNullConstant(T);
953 llvm::Constant *VisitInitListExpr(InitListExpr *ILE, QualType T) {
954 if (ILE->isTransparent())
955 return Visit(ILE->getInit(0), T);
957 if (ILE->getType()->isArrayType())
958 return EmitArrayInitialization(ILE, T);
960 if (ILE->getType()->isRecordType())
961 return EmitRecordInitialization(ILE, T);
966 llvm::Constant *EmitDesignatedInitUpdater(llvm::Constant *Base,
967 InitListExpr *Updater,
969 if (auto destAT = CGM.getContext().getAsArrayType(destType)) {
970 llvm::ArrayType *AType = cast<llvm::ArrayType>(ConvertType(destType));
971 llvm::Type *ElemType = AType->getElementType();
973 unsigned NumInitElements = Updater->getNumInits();
974 unsigned NumElements = AType->getNumElements();
976 std::vector<llvm::Constant *> Elts;
977 Elts.reserve(NumElements);
979 QualType destElemType = destAT->getElementType();
981 if (auto DataArray = dyn_cast<llvm::ConstantDataArray>(Base))
982 for (unsigned i = 0; i != NumElements; ++i)
983 Elts.push_back(DataArray->getElementAsConstant(i));
984 else if (auto Array = dyn_cast<llvm::ConstantArray>(Base))
985 for (unsigned i = 0; i != NumElements; ++i)
986 Elts.push_back(Array->getOperand(i));
988 return nullptr; // FIXME: other array types not implemented
990 llvm::Constant *fillC = nullptr;
991 if (Expr *filler = Updater->getArrayFiller())
992 if (!isa<NoInitExpr>(filler))
993 fillC = Emitter.tryEmitAbstractForMemory(filler, destElemType);
994 bool RewriteType = (fillC && fillC->getType() != ElemType);
996 for (unsigned i = 0; i != NumElements; ++i) {
997 Expr *Init = nullptr;
998 if (i < NumInitElements)
999 Init = Updater->getInit(i);
1003 else if (!Init || isa<NoInitExpr>(Init))
1005 else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
1006 Elts[i] = EmitDesignatedInitUpdater(Elts[i], ChildILE, destElemType);
1008 Elts[i] = Emitter.tryEmitPrivateForMemory(Init, destElemType);
1012 RewriteType |= (Elts[i]->getType() != ElemType);
1016 std::vector<llvm::Type *> Types;
1017 Types.reserve(NumElements);
1018 for (unsigned i = 0; i != NumElements; ++i)
1019 Types.push_back(Elts[i]->getType());
1020 llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
1022 return llvm::ConstantStruct::get(SType, Elts);
1025 return llvm::ConstantArray::get(AType, Elts);
1028 if (destType->isRecordType())
1029 return ConstStructBuilder::BuildStruct(Emitter, this,
1030 dyn_cast<llvm::ConstantStruct>(Base), Updater, destType);
1035 llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E,
1036 QualType destType) {
1037 auto C = Visit(E->getBase(), destType);
1038 if (!C) return nullptr;
1039 return EmitDesignatedInitUpdater(C, E->getUpdater(), destType);
1042 llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E, QualType Ty) {
1043 if (!E->getConstructor()->isTrivial())
1046 // FIXME: We should not have to call getBaseElementType here.
1047 const RecordType *RT =
1048 CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>();
1049 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1051 // If the class doesn't have a trivial destructor, we can't emit it as a
1053 if (!RD->hasTrivialDestructor())
1056 // Only copy and default constructors can be trivial.
1059 if (E->getNumArgs()) {
1060 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
1061 assert(E->getConstructor()->isCopyOrMoveConstructor() &&
1062 "trivial ctor has argument but isn't a copy/move ctor");
1064 Expr *Arg = E->getArg(0);
1065 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
1066 "argument to copy ctor is of wrong type");
1068 return Visit(Arg, Ty);
1071 return CGM.EmitNullConstant(Ty);
1074 llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) {
1075 return CGM.GetConstantArrayFromStringLiteral(E);
1078 llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E, QualType T) {
1079 // This must be an @encode initializing an array in a static initializer.
1080 // Don't emit it as the address of the string, emit the string data itself
1081 // as an inline array.
1083 CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1084 const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
1086 // Resize the string to the right size, adding zeros at the end, or
1087 // truncating as needed.
1088 Str.resize(CAT->getSize().getZExtValue(), '\0');
1089 return llvm::ConstantDataArray::getString(VMContext, Str, false);
1092 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) {
1093 return Visit(E->getSubExpr(), T);
1097 llvm::Type *ConvertType(QualType T) {
1098 return CGM.getTypes().ConvertType(T);
1102 } // end anonymous namespace.
1104 bool ConstStructBuilder::Build(ConstExprEmitter *ExprEmitter,
1105 llvm::ConstantStruct *Base,
1106 InitListExpr *Updater) {
1107 assert(Base && "base expression should not be empty");
1109 QualType ExprType = Updater->getType();
1110 RecordDecl *RD = ExprType->getAs<RecordType>()->getDecl();
1111 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1112 const llvm::StructLayout *BaseLayout = CGM.getDataLayout().getStructLayout(
1114 unsigned FieldNo = -1;
1115 unsigned ElementNo = 0;
1117 // Bail out if we have base classes. We could support these, but they only
1118 // arise in C++1z where we will have already constant folded most interesting
1119 // cases. FIXME: There are still a few more cases we can handle this way.
1120 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1121 if (CXXRD->getNumBases())
1124 for (FieldDecl *Field : RD->fields()) {
1127 if (RD->isUnion() && Updater->getInitializedFieldInUnion() != Field)
1130 // Skip anonymous bitfields.
1131 if (Field->isUnnamedBitfield())
1134 llvm::Constant *EltInit = Base->getOperand(ElementNo);
1136 // Bail out if the type of the ConstantStruct does not have the same layout
1137 // as the type of the InitListExpr.
1138 if (CGM.getTypes().ConvertType(Field->getType()) != EltInit->getType() ||
1139 Layout.getFieldOffset(ElementNo) !=
1140 BaseLayout->getElementOffsetInBits(ElementNo))
1143 // Get the initializer. If we encounter an empty field or a NoInitExpr,
1144 // we use values from the base expression.
1145 Expr *Init = nullptr;
1146 if (ElementNo < Updater->getNumInits())
1147 Init = Updater->getInit(ElementNo);
1149 if (!Init || isa<NoInitExpr>(Init))
1151 else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init))
1152 EltInit = ExprEmitter->EmitDesignatedInitUpdater(EltInit, ChildILE,
1155 EltInit = Emitter.tryEmitPrivateForMemory(Init, Field->getType());
1162 if (!Field->isBitField())
1163 AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit);
1164 else if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(EltInit))
1165 AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI);
1167 // Initializing a bitfield with a non-trivial constant?
1174 llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C,
1175 AbstractState saved) {
1176 Abstract = saved.OldValue;
1178 assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() &&
1179 "created a placeholder while doing an abstract emission?");
1181 // No validation necessary for now.
1182 // No cleanup to do for now.
1187 ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) {
1188 auto state = pushAbstract();
1189 auto C = tryEmitPrivateForVarInit(D);
1190 return validateAndPopAbstract(C, state);
1194 ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) {
1195 auto state = pushAbstract();
1196 auto C = tryEmitPrivate(E, destType);
1197 return validateAndPopAbstract(C, state);
1201 ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) {
1202 auto state = pushAbstract();
1203 auto C = tryEmitPrivate(value, destType);
1204 return validateAndPopAbstract(C, state);
1208 ConstantEmitter::emitAbstract(const Expr *E, QualType destType) {
1209 auto state = pushAbstract();
1210 auto C = tryEmitPrivate(E, destType);
1211 C = validateAndPopAbstract(C, state);
1213 CGM.Error(E->getExprLoc(),
1214 "internal error: could not emit constant value \"abstractly\"");
1215 C = CGM.EmitNullConstant(destType);
1221 ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value,
1222 QualType destType) {
1223 auto state = pushAbstract();
1224 auto C = tryEmitPrivate(value, destType);
1225 C = validateAndPopAbstract(C, state);
1228 "internal error: could not emit constant value \"abstractly\"");
1229 C = CGM.EmitNullConstant(destType);
1234 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) {
1235 initializeNonAbstract(D.getType().getAddressSpace());
1236 return markIfFailed(tryEmitPrivateForVarInit(D));
1239 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E,
1240 LangAS destAddrSpace,
1241 QualType destType) {
1242 initializeNonAbstract(destAddrSpace);
1243 return markIfFailed(tryEmitPrivateForMemory(E, destType));
1246 llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value,
1247 LangAS destAddrSpace,
1248 QualType destType) {
1249 initializeNonAbstract(destAddrSpace);
1250 auto C = tryEmitPrivateForMemory(value, destType);
1251 assert(C && "couldn't emit constant value non-abstractly?");
1255 llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() {
1256 assert(!Abstract && "cannot get current address for abstract constant");
1260 // Make an obviously ill-formed global that should blow up compilation
1262 auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true,
1263 llvm::GlobalValue::PrivateLinkage,
1267 llvm::GlobalVariable::NotThreadLocal,
1268 CGM.getContext().getTargetAddressSpace(DestAddressSpace));
1270 PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
1275 void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal,
1276 llvm::GlobalValue *placeholder) {
1277 assert(!PlaceholderAddresses.empty());
1278 assert(PlaceholderAddresses.back().first == nullptr);
1279 assert(PlaceholderAddresses.back().second == placeholder);
1280 PlaceholderAddresses.back().first = signal;
1284 struct ReplacePlaceholders {
1287 /// The base address of the global.
1288 llvm::Constant *Base;
1289 llvm::Type *BaseValueTy = nullptr;
1291 /// The placeholder addresses that were registered during emission.
1292 llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses;
1294 /// The locations of the placeholder signals.
1295 llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations;
1297 /// The current index stack. We use a simple unsigned stack because
1298 /// we assume that placeholders will be relatively sparse in the
1299 /// initializer, but we cache the index values we find just in case.
1300 llvm::SmallVector<unsigned, 8> Indices;
1301 llvm::SmallVector<llvm::Constant*, 8> IndexValues;
1303 ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base,
1304 ArrayRef<std::pair<llvm::Constant*,
1305 llvm::GlobalVariable*>> addresses)
1306 : CGM(CGM), Base(base),
1307 PlaceholderAddresses(addresses.begin(), addresses.end()) {
1310 void replaceInInitializer(llvm::Constant *init) {
1311 // Remember the type of the top-most initializer.
1312 BaseValueTy = init->getType();
1314 // Initialize the stack.
1315 Indices.push_back(0);
1316 IndexValues.push_back(nullptr);
1318 // Recurse into the initializer.
1319 findLocations(init);
1321 // Check invariants.
1322 assert(IndexValues.size() == Indices.size() && "mismatch");
1323 assert(Indices.size() == 1 && "didn't pop all indices");
1325 // Do the replacement; this basically invalidates 'init'.
1326 assert(Locations.size() == PlaceholderAddresses.size() &&
1327 "missed a placeholder?");
1329 // We're iterating over a hashtable, so this would be a source of
1330 // non-determinism in compiler output *except* that we're just
1331 // messing around with llvm::Constant structures, which never itself
1332 // does anything that should be visible in compiler output.
1333 for (auto &entry : Locations) {
1334 assert(entry.first->getParent() == nullptr && "not a placeholder!");
1335 entry.first->replaceAllUsesWith(entry.second);
1336 entry.first->eraseFromParent();
1341 void findLocations(llvm::Constant *init) {
1342 // Recurse into aggregates.
1343 if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) {
1344 for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) {
1345 Indices.push_back(i);
1346 IndexValues.push_back(nullptr);
1348 findLocations(agg->getOperand(i));
1350 IndexValues.pop_back();
1356 // Otherwise, check for registered constants.
1358 auto it = PlaceholderAddresses.find(init);
1359 if (it != PlaceholderAddresses.end()) {
1360 setLocation(it->second);
1364 // Look through bitcasts or other expressions.
1365 if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) {
1366 init = expr->getOperand(0);
1373 void setLocation(llvm::GlobalVariable *placeholder) {
1374 assert(Locations.find(placeholder) == Locations.end() &&
1375 "already found location for placeholder!");
1377 // Lazily fill in IndexValues with the values from Indices.
1378 // We do this in reverse because we should always have a strict
1379 // prefix of indices from the start.
1380 assert(Indices.size() == IndexValues.size());
1381 for (size_t i = Indices.size() - 1; i != size_t(-1); --i) {
1382 if (IndexValues[i]) {
1384 for (size_t j = 0; j != i + 1; ++j) {
1385 assert(IndexValues[j] &&
1386 isa<llvm::ConstantInt>(IndexValues[j]) &&
1387 cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue()
1394 IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]);
1397 // Form a GEP and then bitcast to the placeholder type so that the
1398 // replacement will succeed.
1399 llvm::Constant *location =
1400 llvm::ConstantExpr::getInBoundsGetElementPtr(BaseValueTy,
1402 location = llvm::ConstantExpr::getBitCast(location,
1403 placeholder->getType());
1405 Locations.insert({placeholder, location});
1410 void ConstantEmitter::finalize(llvm::GlobalVariable *global) {
1411 assert(InitializedNonAbstract &&
1412 "finalizing emitter that was used for abstract emission?");
1413 assert(!Finalized && "finalizing emitter multiple times");
1414 assert(global->getInitializer());
1416 // Note that we might also be Failed.
1419 if (!PlaceholderAddresses.empty()) {
1420 ReplacePlaceholders(CGM, global, PlaceholderAddresses)
1421 .replaceInInitializer(global->getInitializer());
1422 PlaceholderAddresses.clear(); // satisfy
1426 ConstantEmitter::~ConstantEmitter() {
1427 assert((!InitializedNonAbstract || Finalized || Failed) &&
1428 "not finalized after being initialized for non-abstract emission");
1429 assert(PlaceholderAddresses.empty() && "unhandled placeholders");
1432 static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) {
1433 if (auto AT = type->getAs<AtomicType>()) {
1434 return CGM.getContext().getQualifiedType(AT->getValueType(),
1435 type.getQualifiers());
1440 llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
1441 // Make a quick check if variable can be default NULL initialized
1442 // and avoid going through rest of code which may do, for c++11,
1443 // initialization of memory to all NULLs.
1444 if (!D.hasLocalStorage()) {
1445 QualType Ty = CGM.getContext().getBaseElementType(D.getType());
1446 if (Ty->isRecordType())
1447 if (const CXXConstructExpr *E =
1448 dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1449 const CXXConstructorDecl *CD = E->getConstructor();
1450 if (CD->isTrivial() && CD->isDefaultConstructor())
1451 return CGM.EmitNullConstant(D.getType());
1455 QualType destType = D.getType();
1457 // Try to emit the initializer. Note that this can allow some things that
1458 // are not allowed by tryEmitPrivateForMemory alone.
1459 if (auto value = D.evaluateValue()) {
1460 return tryEmitPrivateForMemory(*value, destType);
1463 // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1464 // reference is a constant expression, and the reference binds to a temporary,
1465 // then constant initialization is performed. ConstExprEmitter will
1466 // incorrectly emit a prvalue constant in this case, and the calling code
1467 // interprets that as the (pointer) value of the reference, rather than the
1468 // desired value of the referee.
1469 if (destType->isReferenceType())
1472 const Expr *E = D.getInit();
1473 assert(E && "No initializer to emit");
1475 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1477 ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType);
1478 return (C ? emitForMemory(C, destType) : nullptr);
1482 ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) {
1483 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1484 auto C = tryEmitAbstract(E, nonMemoryDestType);
1485 return (C ? emitForMemory(C, destType) : nullptr);
1489 ConstantEmitter::tryEmitAbstractForMemory(const APValue &value,
1490 QualType destType) {
1491 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1492 auto C = tryEmitAbstract(value, nonMemoryDestType);
1493 return (C ? emitForMemory(C, destType) : nullptr);
1496 llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E,
1497 QualType destType) {
1498 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1499 llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType);
1500 return (C ? emitForMemory(C, destType) : nullptr);
1503 llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value,
1504 QualType destType) {
1505 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1506 auto C = tryEmitPrivate(value, nonMemoryDestType);
1507 return (C ? emitForMemory(C, destType) : nullptr);
1510 llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM,
1512 QualType destType) {
1513 // For an _Atomic-qualified constant, we may need to add tail padding.
1514 if (auto AT = destType->getAs<AtomicType>()) {
1515 QualType destValueType = AT->getValueType();
1516 C = emitForMemory(CGM, C, destValueType);
1518 uint64_t innerSize = CGM.getContext().getTypeSize(destValueType);
1519 uint64_t outerSize = CGM.getContext().getTypeSize(destType);
1520 if (innerSize == outerSize)
1523 assert(innerSize < outerSize && "emitted over-large constant for atomic");
1524 llvm::Constant *elts[] = {
1526 llvm::ConstantAggregateZero::get(
1527 llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8))
1529 return llvm::ConstantStruct::getAnon(elts);
1532 // Zero-extend bool.
1533 if (C->getType()->isIntegerTy(1)) {
1534 llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType);
1535 return llvm::ConstantExpr::getZExt(C, boolTy);
1541 llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
1542 QualType destType) {
1543 Expr::EvalResult Result;
1545 bool Success = false;
1547 if (destType->isReferenceType())
1548 Success = E->EvaluateAsLValue(Result, CGM.getContext());
1550 Success = E->EvaluateAsRValue(Result, CGM.getContext());
1553 if (Success && !Result.HasSideEffects)
1554 C = tryEmitPrivate(Result.Val, destType);
1556 C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType);
1561 llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {
1562 return getTargetCodeGenInfo().getNullPointer(*this, T, QT);
1566 /// A struct which can be used to peephole certain kinds of finalization
1567 /// that normally happen during l-value emission.
1568 struct ConstantLValue {
1569 llvm::Constant *Value;
1570 bool HasOffsetApplied;
1572 /*implicit*/ ConstantLValue(llvm::Constant *value,
1573 bool hasOffsetApplied = false)
1574 : Value(value), HasOffsetApplied(false) {}
1576 /*implicit*/ ConstantLValue(ConstantAddress address)
1577 : ConstantLValue(address.getPointer()) {}
1580 /// A helper class for emitting constant l-values.
1581 class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
1584 ConstantEmitter &Emitter;
1585 const APValue &Value;
1588 // Befriend StmtVisitorBase so that we don't have to expose Visit*.
1589 friend StmtVisitorBase;
1592 ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
1594 : CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType) {}
1596 llvm::Constant *tryEmit();
1599 llvm::Constant *tryEmitAbsolute(llvm::Type *destTy);
1600 ConstantLValue tryEmitBase(const APValue::LValueBase &base);
1602 ConstantLValue VisitStmt(const Stmt *S) { return nullptr; }
1603 ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
1604 ConstantLValue VisitStringLiteral(const StringLiteral *E);
1605 ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
1606 ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E);
1607 ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E);
1608 ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E);
1609 ConstantLValue VisitCallExpr(const CallExpr *E);
1610 ConstantLValue VisitBlockExpr(const BlockExpr *E);
1611 ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E);
1612 ConstantLValue VisitCXXUuidofExpr(const CXXUuidofExpr *E);
1613 ConstantLValue VisitMaterializeTemporaryExpr(
1614 const MaterializeTemporaryExpr *E);
1616 bool hasNonZeroOffset() const {
1617 return !Value.getLValueOffset().isZero();
1620 /// Return the value offset.
1621 llvm::Constant *getOffset() {
1622 return llvm::ConstantInt::get(CGM.Int64Ty,
1623 Value.getLValueOffset().getQuantity());
1626 /// Apply the value offset to the given constant.
1627 llvm::Constant *applyOffset(llvm::Constant *C) {
1628 if (!hasNonZeroOffset())
1631 llvm::Type *origPtrTy = C->getType();
1632 unsigned AS = origPtrTy->getPointerAddressSpace();
1633 llvm::Type *charPtrTy = CGM.Int8Ty->getPointerTo(AS);
1634 C = llvm::ConstantExpr::getBitCast(C, charPtrTy);
1635 C = llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset());
1636 C = llvm::ConstantExpr::getPointerCast(C, origPtrTy);
1643 llvm::Constant *ConstantLValueEmitter::tryEmit() {
1644 const APValue::LValueBase &base = Value.getLValueBase();
1646 // Certain special array initializers are represented in APValue
1647 // as l-values referring to the base expression which generates the
1648 // array. This happens with e.g. string literals. These should
1649 // probably just get their own representation kind in APValue.
1650 if (DestType->isArrayType()) {
1651 assert(!hasNonZeroOffset() && "offset on array initializer");
1652 auto expr = const_cast<Expr*>(base.get<const Expr*>());
1653 return ConstExprEmitter(Emitter).Visit(expr, DestType);
1656 // Otherwise, the destination type should be a pointer or reference
1657 // type, but it might also be a cast thereof.
1659 // FIXME: the chain of casts required should be reflected in the APValue.
1660 // We need this in order to correctly handle things like a ptrtoint of a
1661 // non-zero null pointer and addrspace casts that aren't trivially
1662 // represented in LLVM IR.
1663 auto destTy = CGM.getTypes().ConvertTypeForMem(DestType);
1664 assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy));
1666 // If there's no base at all, this is a null or absolute pointer,
1667 // possibly cast back to an integer type.
1669 return tryEmitAbsolute(destTy);
1672 // Otherwise, try to emit the base.
1673 ConstantLValue result = tryEmitBase(base);
1675 // If that failed, we're done.
1676 llvm::Constant *value = result.Value;
1677 if (!value) return nullptr;
1679 // Apply the offset if necessary and not already done.
1680 if (!result.HasOffsetApplied) {
1681 value = applyOffset(value);
1684 // Convert to the appropriate type; this could be an lvalue for
1685 // an integer. FIXME: performAddrSpaceCast
1686 if (isa<llvm::PointerType>(destTy))
1687 return llvm::ConstantExpr::getPointerCast(value, destTy);
1689 return llvm::ConstantExpr::getPtrToInt(value, destTy);
1692 /// Try to emit an absolute l-value, such as a null pointer or an integer
1693 /// bitcast to pointer type.
1695 ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
1696 auto offset = getOffset();
1698 // If we're producing a pointer, this is easy.
1699 if (auto destPtrTy = cast<llvm::PointerType>(destTy)) {
1700 if (Value.isNullPointer()) {
1701 // FIXME: integer offsets from non-zero null pointers.
1702 return CGM.getNullPointer(destPtrTy, DestType);
1705 // Convert the integer to a pointer-sized integer before converting it
1707 // FIXME: signedness depends on the original integer type.
1708 auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy);
1709 llvm::Constant *C = offset;
1710 C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy,
1711 /*isSigned*/ false);
1712 C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy);
1716 // Otherwise, we're basically returning an integer constant.
1718 // FIXME: this does the wrong thing with ptrtoint of a null pointer,
1719 // but since we don't know the original pointer type, there's not much
1720 // we can do about it.
1722 auto C = getOffset();
1723 C = llvm::ConstantExpr::getIntegerCast(C, destTy, /*isSigned*/ false);
1728 ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
1730 if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) {
1731 if (D->hasAttr<WeakRefAttr>())
1732 return CGM.GetWeakRefReference(D).getPointer();
1734 if (auto FD = dyn_cast<FunctionDecl>(D))
1735 return CGM.GetAddrOfFunction(FD);
1737 if (auto VD = dyn_cast<VarDecl>(D)) {
1738 // We can never refer to a variable with local storage.
1739 if (!VD->hasLocalStorage()) {
1740 if (VD->isFileVarDecl() || VD->hasExternalStorage())
1741 return CGM.GetAddrOfGlobalVar(VD);
1743 if (VD->isLocalVarDecl()) {
1744 return CGM.getOrCreateStaticVarDecl(
1745 *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false));
1753 // Otherwise, it must be an expression.
1754 return Visit(base.get<const Expr*>());
1758 ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
1759 return tryEmitGlobalCompoundLiteral(CGM, Emitter.CGF, E);
1763 ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) {
1764 return CGM.GetAddrOfConstantStringFromLiteral(E);
1768 ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
1769 return CGM.GetAddrOfConstantStringFromObjCEncode(E);
1773 ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
1774 auto C = CGM.getObjCRuntime().GenerateConstantString(E->getString());
1775 return C.getElementBitCast(CGM.getTypes().ConvertTypeForMem(E->getType()));
1779 ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) {
1780 if (auto CGF = Emitter.CGF) {
1781 LValue Res = CGF->EmitPredefinedLValue(E);
1782 return cast<ConstantAddress>(Res.getAddress());
1785 auto kind = E->getIdentType();
1786 if (kind == PredefinedExpr::PrettyFunction) {
1787 return CGM.GetAddrOfConstantCString("top level", ".tmp");
1790 return CGM.GetAddrOfConstantCString("", ".tmp");
1794 ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) {
1795 assert(Emitter.CGF && "Invalid address of label expression outside function");
1796 llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel());
1797 Ptr = llvm::ConstantExpr::getBitCast(Ptr,
1798 CGM.getTypes().ConvertType(E->getType()));
1803 ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
1804 unsigned builtin = E->getBuiltinCallee();
1805 if (builtin != Builtin::BI__builtin___CFStringMakeConstantString &&
1806 builtin != Builtin::BI__builtin___NSStringMakeConstantString)
1809 auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts());
1810 if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) {
1811 return CGM.getObjCRuntime().GenerateConstantString(literal);
1813 // FIXME: need to deal with UCN conversion issues.
1814 return CGM.GetAddrOfConstantCFString(literal);
1819 ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) {
1820 StringRef functionName;
1821 if (auto CGF = Emitter.CGF)
1822 functionName = CGF->CurFn->getName();
1824 functionName = "global";
1826 return CGM.GetAddrOfGlobalBlock(E, functionName);
1830 ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
1832 if (E->isTypeOperand())
1833 T = E->getTypeOperand(CGM.getContext());
1835 T = E->getExprOperand()->getType();
1836 return CGM.GetAddrOfRTTIDescriptor(T);
1840 ConstantLValueEmitter::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
1841 return CGM.GetAddrOfUuidDescriptor(E);
1845 ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
1846 const MaterializeTemporaryExpr *E) {
1847 assert(E->getStorageDuration() == SD_Static);
1848 SmallVector<const Expr *, 2> CommaLHSs;
1849 SmallVector<SubobjectAdjustment, 2> Adjustments;
1850 const Expr *Inner = E->GetTemporaryExpr()
1851 ->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
1852 return CGM.GetAddrOfGlobalTemporary(E, Inner);
1855 llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
1856 QualType DestType) {
1857 switch (Value.getKind()) {
1858 case APValue::Uninitialized:
1859 llvm_unreachable("Constant expressions should be initialized.");
1860 case APValue::LValue:
1861 return ConstantLValueEmitter(*this, Value, DestType).tryEmit();
1863 return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt());
1864 case APValue::ComplexInt: {
1865 llvm::Constant *Complex[2];
1867 Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(),
1868 Value.getComplexIntReal());
1869 Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(),
1870 Value.getComplexIntImag());
1872 // FIXME: the target may want to specify that this is packed.
1873 llvm::StructType *STy =
1874 llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
1875 return llvm::ConstantStruct::get(STy, Complex);
1877 case APValue::Float: {
1878 const llvm::APFloat &Init = Value.getFloat();
1879 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() &&
1880 !CGM.getContext().getLangOpts().NativeHalfType &&
1881 CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics())
1882 return llvm::ConstantInt::get(CGM.getLLVMContext(),
1883 Init.bitcastToAPInt());
1885 return llvm::ConstantFP::get(CGM.getLLVMContext(), Init);
1887 case APValue::ComplexFloat: {
1888 llvm::Constant *Complex[2];
1890 Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(),
1891 Value.getComplexFloatReal());
1892 Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(),
1893 Value.getComplexFloatImag());
1895 // FIXME: the target may want to specify that this is packed.
1896 llvm::StructType *STy =
1897 llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
1898 return llvm::ConstantStruct::get(STy, Complex);
1900 case APValue::Vector: {
1901 unsigned NumElts = Value.getVectorLength();
1902 SmallVector<llvm::Constant *, 4> Inits(NumElts);
1904 for (unsigned I = 0; I != NumElts; ++I) {
1905 const APValue &Elt = Value.getVectorElt(I);
1907 Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt());
1908 else if (Elt.isFloat())
1909 Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
1911 llvm_unreachable("unsupported vector element type");
1913 return llvm::ConstantVector::get(Inits);
1915 case APValue::AddrLabelDiff: {
1916 const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
1917 const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
1918 llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType());
1919 llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType());
1920 if (!LHS || !RHS) return nullptr;
1922 // Compute difference
1923 llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType);
1924 LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy);
1925 RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy);
1926 llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
1928 // LLVM is a bit sensitive about the exact format of the
1929 // address-of-label difference; make sure to truncate after
1931 return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
1933 case APValue::Struct:
1934 case APValue::Union:
1935 return ConstStructBuilder::BuildStruct(*this, Value, DestType);
1936 case APValue::Array: {
1937 const ConstantArrayType *CAT =
1938 CGM.getContext().getAsConstantArrayType(DestType);
1939 unsigned NumElements = Value.getArraySize();
1940 unsigned NumInitElts = Value.getArrayInitializedElts();
1942 // Emit array filler, if there is one.
1943 llvm::Constant *Filler = nullptr;
1944 if (Value.hasArrayFiller()) {
1945 Filler = tryEmitAbstractForMemory(Value.getArrayFiller(),
1946 CAT->getElementType());
1951 // Emit initializer elements.
1952 SmallVector<llvm::Constant*, 16> Elts;
1953 if (Filler && Filler->isNullValue())
1954 Elts.reserve(NumInitElts + 1);
1956 Elts.reserve(NumElements);
1958 llvm::Type *CommonElementType = nullptr;
1959 for (unsigned I = 0; I < NumInitElts; ++I) {
1960 llvm::Constant *C = tryEmitPrivateForMemory(
1961 Value.getArrayInitializedElt(I), CAT->getElementType());
1962 if (!C) return nullptr;
1965 CommonElementType = C->getType();
1966 else if (C->getType() != CommonElementType)
1967 CommonElementType = nullptr;
1971 return EmitArrayConstant(CGM, CAT, CommonElementType, NumElements, Elts,
1974 case APValue::MemberPointer:
1975 return CGM.getCXXABI().EmitMemberPointer(Value, DestType);
1977 llvm_unreachable("Unknown APValue kind");
1980 llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted(
1981 const CompoundLiteralExpr *E) {
1982 return EmittedCompoundLiterals.lookup(E);
1985 void CodeGenModule::setAddrOfConstantCompoundLiteral(
1986 const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) {
1987 bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second;
1989 assert(Ok && "CLE has already been emitted!");
1993 CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
1994 assert(E->isFileScope() && "not a file-scope compound literal expr");
1995 return tryEmitGlobalCompoundLiteral(*this, nullptr, E);
1999 CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
2000 // Member pointer constants always have a very particular form.
2001 const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
2002 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
2004 // A member function pointer.
2005 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
2006 return getCXXABI().EmitMemberFunctionPointer(method);
2008 // Otherwise, a member data pointer.
2009 uint64_t fieldOffset = getContext().getFieldOffset(decl);
2010 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
2011 return getCXXABI().EmitMemberDataPointer(type, chars);
2014 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2015 llvm::Type *baseType,
2016 const CXXRecordDecl *base);
2018 static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
2019 const RecordDecl *record,
2020 bool asCompleteObject) {
2021 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
2022 llvm::StructType *structure =
2023 (asCompleteObject ? layout.getLLVMType()
2024 : layout.getBaseSubobjectLLVMType());
2026 unsigned numElements = structure->getNumElements();
2027 std::vector<llvm::Constant *> elements(numElements);
2029 auto CXXR = dyn_cast<CXXRecordDecl>(record);
2030 // Fill in all the bases.
2032 for (const auto &I : CXXR->bases()) {
2033 if (I.isVirtual()) {
2034 // Ignore virtual bases; if we're laying out for a complete
2035 // object, we'll lay these out later.
2039 const CXXRecordDecl *base =
2040 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2042 // Ignore empty bases.
2043 if (base->isEmpty() ||
2044 CGM.getContext().getASTRecordLayout(base).getNonVirtualSize()
2048 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
2049 llvm::Type *baseType = structure->getElementType(fieldIndex);
2050 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2054 // Fill in all the fields.
2055 for (const auto *Field : record->fields()) {
2056 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
2057 // will fill in later.)
2058 if (!Field->isBitField()) {
2059 unsigned fieldIndex = layout.getLLVMFieldNo(Field);
2060 elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
2063 // For unions, stop after the first named field.
2064 if (record->isUnion()) {
2065 if (Field->getIdentifier())
2067 if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
2068 if (FieldRD->findFirstNamedDataMember())
2073 // Fill in the virtual bases, if we're working with the complete object.
2074 if (CXXR && asCompleteObject) {
2075 for (const auto &I : CXXR->vbases()) {
2076 const CXXRecordDecl *base =
2077 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2079 // Ignore empty bases.
2080 if (base->isEmpty())
2083 unsigned fieldIndex = layout.getVirtualBaseIndex(base);
2085 // We might have already laid this field out.
2086 if (elements[fieldIndex]) continue;
2088 llvm::Type *baseType = structure->getElementType(fieldIndex);
2089 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2093 // Now go through all other fields and zero them out.
2094 for (unsigned i = 0; i != numElements; ++i) {
2096 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
2099 return llvm::ConstantStruct::get(structure, elements);
2102 /// Emit the null constant for a base subobject.
2103 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2104 llvm::Type *baseType,
2105 const CXXRecordDecl *base) {
2106 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
2108 // Just zero out bases that don't have any pointer to data members.
2109 if (baseLayout.isZeroInitializableAsBase())
2110 return llvm::Constant::getNullValue(baseType);
2112 // Otherwise, we can just use its null constant.
2113 return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
2116 llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM,
2118 return emitForMemory(CGM, CGM.EmitNullConstant(T), T);
2121 llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
2122 if (T->getAs<PointerType>())
2123 return getNullPointer(
2124 cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T);
2126 if (getTypes().isZeroInitializable(T))
2127 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
2129 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
2130 llvm::ArrayType *ATy =
2131 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
2133 QualType ElementTy = CAT->getElementType();
2135 llvm::Constant *Element =
2136 ConstantEmitter::emitNullForMemory(*this, ElementTy);
2137 unsigned NumElements = CAT->getSize().getZExtValue();
2138 SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
2139 return llvm::ConstantArray::get(ATy, Array);
2142 if (const RecordType *RT = T->getAs<RecordType>())
2143 return ::EmitNullConstant(*this, RT->getDecl(), /*complete object*/ true);
2145 assert(T->isMemberDataPointerType() &&
2146 "Should only see pointers to data members here!");
2148 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
2152 CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
2153 return ::EmitNullConstant(*this, Record, false);