xref: /llvm-project/clang/lib/CodeGen/CodeGenModule.cpp (revision e1a0b2e2af7af1ae4b481eea838837d440825c3e)
1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This coordinates the per-module state used while generating code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenModule.h"
15 #include "CGCUDARuntime.h"
16 #include "CGCXXABI.h"
17 #include "CGCall.h"
18 #include "CGDebugInfo.h"
19 #include "CGObjCRuntime.h"
20 #include "CGOpenCLRuntime.h"
21 #include "CGOpenMPRuntime.h"
22 #include "CodeGenFunction.h"
23 #include "CodeGenPGO.h"
24 #include "CodeGenTBAA.h"
25 #include "CoverageMappingGen.h"
26 #include "TargetInfo.h"
27 #include "clang/AST/ASTContext.h"
28 #include "clang/AST/CharUnits.h"
29 #include "clang/AST/DeclCXX.h"
30 #include "clang/AST/DeclObjC.h"
31 #include "clang/AST/DeclTemplate.h"
32 #include "clang/AST/Mangle.h"
33 #include "clang/AST/RecordLayout.h"
34 #include "clang/AST/RecursiveASTVisitor.h"
35 #include "clang/Basic/Builtins.h"
36 #include "clang/Basic/CharInfo.h"
37 #include "clang/Basic/Diagnostic.h"
38 #include "clang/Basic/Module.h"
39 #include "clang/Basic/SourceManager.h"
40 #include "clang/Basic/TargetInfo.h"
41 #include "clang/Basic/Version.h"
42 #include "clang/Frontend/CodeGenOptions.h"
43 #include "clang/Sema/SemaDiagnostic.h"
44 #include "llvm/ADT/APSInt.h"
45 #include "llvm/ADT/Triple.h"
46 #include "llvm/IR/CallSite.h"
47 #include "llvm/IR/CallingConv.h"
48 #include "llvm/IR/DataLayout.h"
49 #include "llvm/IR/Intrinsics.h"
50 #include "llvm/IR/LLVMContext.h"
51 #include "llvm/IR/Module.h"
52 #include "llvm/ProfileData/InstrProfReader.h"
53 #include "llvm/Support/ConvertUTF.h"
54 #include "llvm/Support/ErrorHandling.h"
55 
56 using namespace clang;
57 using namespace CodeGen;
58 
59 static const char AnnotationSection[] = "llvm.metadata";
60 
61 static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
62   switch (CGM.getTarget().getCXXABI().getKind()) {
63   case TargetCXXABI::GenericAArch64:
64   case TargetCXXABI::GenericARM:
65   case TargetCXXABI::iOS:
66   case TargetCXXABI::iOS64:
67   case TargetCXXABI::GenericItanium:
68     return CreateItaniumCXXABI(CGM);
69   case TargetCXXABI::Microsoft:
70     return CreateMicrosoftCXXABI(CGM);
71   }
72 
73   llvm_unreachable("invalid C++ ABI kind");
74 }
75 
76 CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
77                              llvm::Module &M, const llvm::DataLayout &TD,
78                              DiagnosticsEngine &diags,
79                              CoverageSourceInfo *CoverageInfo)
80     : Context(C), LangOpts(C.getLangOpts()), CodeGenOpts(CGO), TheModule(M),
81       Diags(diags), TheDataLayout(TD), Target(C.getTargetInfo()),
82       ABI(createCXXABI(*this)), VMContext(M.getContext()), TBAA(nullptr),
83       TheTargetCodeGenInfo(nullptr), Types(*this), VTables(*this),
84       ObjCRuntime(nullptr), OpenCLRuntime(nullptr), OpenMPRuntime(nullptr),
85       CUDARuntime(nullptr), DebugInfo(nullptr), ARCData(nullptr),
86       NoObjCARCExceptionsMetadata(nullptr), RRData(nullptr), PGOReader(nullptr),
87       CFConstantStringClassRef(nullptr), ConstantStringClassRef(nullptr),
88       NSConstantStringType(nullptr), NSConcreteGlobalBlock(nullptr),
89       NSConcreteStackBlock(nullptr), BlockObjectAssign(nullptr),
90       BlockObjectDispose(nullptr), BlockDescriptorType(nullptr),
91       GenericBlockLiteralType(nullptr), LifetimeStartFn(nullptr),
92       LifetimeEndFn(nullptr), SanitizerMD(new SanitizerMetadata(*this)) {
93 
94   // Initialize the type cache.
95   llvm::LLVMContext &LLVMContext = M.getContext();
96   VoidTy = llvm::Type::getVoidTy(LLVMContext);
97   Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
98   Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
99   Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
100   Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
101   FloatTy = llvm::Type::getFloatTy(LLVMContext);
102   DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
103   PointerWidthInBits = C.getTargetInfo().getPointerWidth(0);
104   PointerAlignInBytes =
105   C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
106   IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
107   IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits);
108   Int8PtrTy = Int8Ty->getPointerTo(0);
109   Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
110 
111   RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
112   BuiltinCC = getTargetCodeGenInfo().getABIInfo().getBuiltinCC();
113 
114   if (LangOpts.ObjC1)
115     createObjCRuntime();
116   if (LangOpts.OpenCL)
117     createOpenCLRuntime();
118   if (LangOpts.OpenMP)
119     createOpenMPRuntime();
120   if (LangOpts.CUDA)
121     createCUDARuntime();
122 
123   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
124   if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
125       (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
126     TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(),
127                            getCXXABI().getMangleContext());
128 
129   // If debug info or coverage generation is enabled, create the CGDebugInfo
130   // object.
131   if (CodeGenOpts.getDebugInfo() != CodeGenOptions::NoDebugInfo ||
132       CodeGenOpts.EmitGcovArcs ||
133       CodeGenOpts.EmitGcovNotes)
134     DebugInfo = new CGDebugInfo(*this);
135 
136   Block.GlobalUniqueCount = 0;
137 
138   if (C.getLangOpts().ObjCAutoRefCount)
139     ARCData = new ARCEntrypoints();
140   RRData = new RREntrypoints();
141 
142   if (!CodeGenOpts.InstrProfileInput.empty()) {
143     if (std::error_code EC = llvm::IndexedInstrProfReader::create(
144             CodeGenOpts.InstrProfileInput, PGOReader)) {
145       unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
146                                               "Could not read profile: %0");
147       getDiags().Report(DiagID) << EC.message();
148     }
149   }
150 
151   // If coverage mapping generation is enabled, create the
152   // CoverageMappingModuleGen object.
153   if (CodeGenOpts.CoverageMapping)
154     CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
155 }
156 
157 CodeGenModule::~CodeGenModule() {
158   delete ObjCRuntime;
159   delete OpenCLRuntime;
160   delete OpenMPRuntime;
161   delete CUDARuntime;
162   delete TheTargetCodeGenInfo;
163   delete TBAA;
164   delete DebugInfo;
165   delete ARCData;
166   delete RRData;
167 }
168 
169 void CodeGenModule::createObjCRuntime() {
170   // This is just isGNUFamily(), but we want to force implementors of
171   // new ABIs to decide how best to do this.
172   switch (LangOpts.ObjCRuntime.getKind()) {
173   case ObjCRuntime::GNUstep:
174   case ObjCRuntime::GCC:
175   case ObjCRuntime::ObjFW:
176     ObjCRuntime = CreateGNUObjCRuntime(*this);
177     return;
178 
179   case ObjCRuntime::FragileMacOSX:
180   case ObjCRuntime::MacOSX:
181   case ObjCRuntime::iOS:
182     ObjCRuntime = CreateMacObjCRuntime(*this);
183     return;
184   }
185   llvm_unreachable("bad runtime kind");
186 }
187 
188 void CodeGenModule::createOpenCLRuntime() {
189   OpenCLRuntime = new CGOpenCLRuntime(*this);
190 }
191 
192 void CodeGenModule::createOpenMPRuntime() {
193   OpenMPRuntime = new CGOpenMPRuntime(*this);
194 }
195 
196 void CodeGenModule::createCUDARuntime() {
197   CUDARuntime = CreateNVCUDARuntime(*this);
198 }
199 
200 void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
201   Replacements[Name] = C;
202 }
203 
204 void CodeGenModule::applyReplacements() {
205   for (ReplacementsTy::iterator I = Replacements.begin(),
206                                 E = Replacements.end();
207        I != E; ++I) {
208     StringRef MangledName = I->first();
209     llvm::Constant *Replacement = I->second;
210     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
211     if (!Entry)
212       continue;
213     auto *OldF = cast<llvm::Function>(Entry);
214     auto *NewF = dyn_cast<llvm::Function>(Replacement);
215     if (!NewF) {
216       if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
217         NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
218       } else {
219         auto *CE = cast<llvm::ConstantExpr>(Replacement);
220         assert(CE->getOpcode() == llvm::Instruction::BitCast ||
221                CE->getOpcode() == llvm::Instruction::GetElementPtr);
222         NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
223       }
224     }
225 
226     // Replace old with new, but keep the old order.
227     OldF->replaceAllUsesWith(Replacement);
228     if (NewF) {
229       NewF->removeFromParent();
230       OldF->getParent()->getFunctionList().insertAfter(OldF, NewF);
231     }
232     OldF->eraseFromParent();
233   }
234 }
235 
236 // This is only used in aliases that we created and we know they have a
237 // linear structure.
238 static const llvm::GlobalObject *getAliasedGlobal(const llvm::GlobalAlias &GA) {
239   llvm::SmallPtrSet<const llvm::GlobalAlias*, 4> Visited;
240   const llvm::Constant *C = &GA;
241   for (;;) {
242     C = C->stripPointerCasts();
243     if (auto *GO = dyn_cast<llvm::GlobalObject>(C))
244       return GO;
245     // stripPointerCasts will not walk over weak aliases.
246     auto *GA2 = dyn_cast<llvm::GlobalAlias>(C);
247     if (!GA2)
248       return nullptr;
249     if (!Visited.insert(GA2).second)
250       return nullptr;
251     C = GA2->getAliasee();
252   }
253 }
254 
255 void CodeGenModule::checkAliases() {
256   // Check if the constructed aliases are well formed. It is really unfortunate
257   // that we have to do this in CodeGen, but we only construct mangled names
258   // and aliases during codegen.
259   bool Error = false;
260   DiagnosticsEngine &Diags = getDiags();
261   for (std::vector<GlobalDecl>::iterator I = Aliases.begin(),
262          E = Aliases.end(); I != E; ++I) {
263     const GlobalDecl &GD = *I;
264     const auto *D = cast<ValueDecl>(GD.getDecl());
265     const AliasAttr *AA = D->getAttr<AliasAttr>();
266     StringRef MangledName = getMangledName(GD);
267     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
268     auto *Alias = cast<llvm::GlobalAlias>(Entry);
269     const llvm::GlobalValue *GV = getAliasedGlobal(*Alias);
270     if (!GV) {
271       Error = true;
272       Diags.Report(AA->getLocation(), diag::err_cyclic_alias);
273     } else if (GV->isDeclaration()) {
274       Error = true;
275       Diags.Report(AA->getLocation(), diag::err_alias_to_undefined);
276     }
277 
278     llvm::Constant *Aliasee = Alias->getAliasee();
279     llvm::GlobalValue *AliaseeGV;
280     if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
281       AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
282     else
283       AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
284 
285     if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
286       StringRef AliasSection = SA->getName();
287       if (AliasSection != AliaseeGV->getSection())
288         Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
289             << AliasSection;
290     }
291 
292     // We have to handle alias to weak aliases in here. LLVM itself disallows
293     // this since the object semantics would not match the IL one. For
294     // compatibility with gcc we implement it by just pointing the alias
295     // to its aliasee's aliasee. We also warn, since the user is probably
296     // expecting the link to be weak.
297     if (auto GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
298       if (GA->mayBeOverridden()) {
299         Diags.Report(AA->getLocation(), diag::warn_alias_to_weak_alias)
300             << GV->getName() << GA->getName();
301         Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
302             GA->getAliasee(), Alias->getType());
303         Alias->setAliasee(Aliasee);
304       }
305     }
306   }
307   if (!Error)
308     return;
309 
310   for (std::vector<GlobalDecl>::iterator I = Aliases.begin(),
311          E = Aliases.end(); I != E; ++I) {
312     const GlobalDecl &GD = *I;
313     StringRef MangledName = getMangledName(GD);
314     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
315     auto *Alias = cast<llvm::GlobalAlias>(Entry);
316     Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
317     Alias->eraseFromParent();
318   }
319 }
320 
321 void CodeGenModule::clear() {
322   DeferredDeclsToEmit.clear();
323 }
324 
325 void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags,
326                                        StringRef MainFile) {
327   if (!hasDiagnostics())
328     return;
329   if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
330     if (MainFile.empty())
331       MainFile = "<stdin>";
332     Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
333   } else
334     Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Missing
335                                                       << Mismatched;
336 }
337 
338 void CodeGenModule::Release() {
339   EmitDeferred();
340   applyReplacements();
341   checkAliases();
342   EmitCXXGlobalInitFunc();
343   EmitCXXGlobalDtorFunc();
344   EmitCXXThreadLocalInitFunc();
345   if (ObjCRuntime)
346     if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
347       AddGlobalCtor(ObjCInitFunction);
348   if (PGOReader && PGOStats.hasDiagnostics())
349     PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
350   EmitCtorList(GlobalCtors, "llvm.global_ctors");
351   EmitCtorList(GlobalDtors, "llvm.global_dtors");
352   EmitGlobalAnnotations();
353   EmitStaticExternCAliases();
354   EmitDeferredUnusedCoverageMappings();
355   if (CoverageMapping)
356     CoverageMapping->emit();
357   emitLLVMUsed();
358 
359   if (CodeGenOpts.Autolink &&
360       (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
361     EmitModuleLinkOptions();
362   }
363   if (CodeGenOpts.DwarfVersion)
364     // We actually want the latest version when there are conflicts.
365     // We can change from Warning to Latest if such mode is supported.
366     getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version",
367                               CodeGenOpts.DwarfVersion);
368   if (DebugInfo)
369     // We support a single version in the linked module. The LLVM
370     // parser will drop debug info with a different version number
371     // (and warn about it, too).
372     getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
373                               llvm::DEBUG_METADATA_VERSION);
374 
375   // We need to record the widths of enums and wchar_t, so that we can generate
376   // the correct build attributes in the ARM backend.
377   llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
378   if (   Arch == llvm::Triple::arm
379       || Arch == llvm::Triple::armeb
380       || Arch == llvm::Triple::thumb
381       || Arch == llvm::Triple::thumbeb) {
382     // Width of wchar_t in bytes
383     uint64_t WCharWidth =
384         Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
385     getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
386 
387     // The minimum width of an enum in bytes
388     uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
389     getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
390   }
391 
392   if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
393     llvm::PICLevel::Level PL = llvm::PICLevel::Default;
394     switch (PLevel) {
395     case 0: break;
396     case 1: PL = llvm::PICLevel::Small; break;
397     case 2: PL = llvm::PICLevel::Large; break;
398     default: llvm_unreachable("Invalid PIC Level");
399     }
400 
401     getModule().setPICLevel(PL);
402   }
403 
404   SimplifyPersonality();
405 
406   if (getCodeGenOpts().EmitDeclMetadata)
407     EmitDeclMetadata();
408 
409   if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
410     EmitCoverageFile();
411 
412   if (DebugInfo)
413     DebugInfo->finalize();
414 
415   EmitVersionIdentMetadata();
416 
417   EmitTargetMetadata();
418 }
419 
420 void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
421   // Make sure that this type is translated.
422   Types.UpdateCompletedType(TD);
423 }
424 
425 llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
426   if (!TBAA)
427     return nullptr;
428   return TBAA->getTBAAInfo(QTy);
429 }
430 
431 llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() {
432   if (!TBAA)
433     return nullptr;
434   return TBAA->getTBAAInfoForVTablePtr();
435 }
436 
437 llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
438   if (!TBAA)
439     return nullptr;
440   return TBAA->getTBAAStructInfo(QTy);
441 }
442 
443 llvm::MDNode *CodeGenModule::getTBAAStructTypeInfo(QualType QTy) {
444   if (!TBAA)
445     return nullptr;
446   return TBAA->getTBAAStructTypeInfo(QTy);
447 }
448 
449 llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(QualType BaseTy,
450                                                   llvm::MDNode *AccessN,
451                                                   uint64_t O) {
452   if (!TBAA)
453     return nullptr;
454   return TBAA->getTBAAStructTagInfo(BaseTy, AccessN, O);
455 }
456 
457 /// Decorate the instruction with a TBAA tag. For both scalar TBAA
458 /// and struct-path aware TBAA, the tag has the same format:
459 /// base type, access type and offset.
460 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
461 void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
462                                         llvm::MDNode *TBAAInfo,
463                                         bool ConvertTypeToTag) {
464   if (ConvertTypeToTag && TBAA)
465     Inst->setMetadata(llvm::LLVMContext::MD_tbaa,
466                       TBAA->getTBAAScalarTagInfo(TBAAInfo));
467   else
468     Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
469 }
470 
471 void CodeGenModule::Error(SourceLocation loc, StringRef message) {
472   unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
473   getDiags().Report(Context.getFullLoc(loc), diagID) << message;
474 }
475 
476 /// ErrorUnsupported - Print out an error that codegen doesn't support the
477 /// specified stmt yet.
478 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
479   unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
480                                                "cannot compile this %0 yet");
481   std::string Msg = Type;
482   getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
483     << Msg << S->getSourceRange();
484 }
485 
486 /// ErrorUnsupported - Print out an error that codegen doesn't support the
487 /// specified decl yet.
488 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
489   unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
490                                                "cannot compile this %0 yet");
491   std::string Msg = Type;
492   getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
493 }
494 
495 llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
496   return llvm::ConstantInt::get(SizeTy, size.getQuantity());
497 }
498 
499 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
500                                         const NamedDecl *D) const {
501   // Internal definitions always have default visibility.
502   if (GV->hasLocalLinkage()) {
503     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
504     return;
505   }
506 
507   // Set visibility for definitions.
508   LinkageInfo LV = D->getLinkageAndVisibility();
509   if (LV.isVisibilityExplicit() || !GV->hasAvailableExternallyLinkage())
510     GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
511 }
512 
513 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
514   return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
515       .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
516       .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
517       .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
518       .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
519 }
520 
521 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(
522     CodeGenOptions::TLSModel M) {
523   switch (M) {
524   case CodeGenOptions::GeneralDynamicTLSModel:
525     return llvm::GlobalVariable::GeneralDynamicTLSModel;
526   case CodeGenOptions::LocalDynamicTLSModel:
527     return llvm::GlobalVariable::LocalDynamicTLSModel;
528   case CodeGenOptions::InitialExecTLSModel:
529     return llvm::GlobalVariable::InitialExecTLSModel;
530   case CodeGenOptions::LocalExecTLSModel:
531     return llvm::GlobalVariable::LocalExecTLSModel;
532   }
533   llvm_unreachable("Invalid TLS model!");
534 }
535 
536 void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
537   assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
538 
539   llvm::GlobalValue::ThreadLocalMode TLM;
540   TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel());
541 
542   // Override the TLS model if it is explicitly specified.
543   if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
544     TLM = GetLLVMTLSModel(Attr->getModel());
545   }
546 
547   GV->setThreadLocalMode(TLM);
548 }
549 
550 StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
551   StringRef &FoundStr = MangledDeclNames[GD.getCanonicalDecl()];
552   if (!FoundStr.empty())
553     return FoundStr;
554 
555   const auto *ND = cast<NamedDecl>(GD.getDecl());
556   SmallString<256> Buffer;
557   StringRef Str;
558   if (getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
559     llvm::raw_svector_ostream Out(Buffer);
560     if (const auto *D = dyn_cast<CXXConstructorDecl>(ND))
561       getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out);
562     else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND))
563       getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out);
564     else
565       getCXXABI().getMangleContext().mangleName(ND, Out);
566     Str = Out.str();
567   } else {
568     IdentifierInfo *II = ND->getIdentifier();
569     assert(II && "Attempt to mangle unnamed decl.");
570     Str = II->getName();
571   }
572 
573   // Keep the first result in the case of a mangling collision.
574   auto Result = Manglings.insert(std::make_pair(Str, GD));
575   return FoundStr = Result.first->first();
576 }
577 
578 StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD,
579                                              const BlockDecl *BD) {
580   MangleContext &MangleCtx = getCXXABI().getMangleContext();
581   const Decl *D = GD.getDecl();
582 
583   SmallString<256> Buffer;
584   llvm::raw_svector_ostream Out(Buffer);
585   if (!D)
586     MangleCtx.mangleGlobalBlock(BD,
587       dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
588   else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
589     MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
590   else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
591     MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
592   else
593     MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
594 
595   auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
596   return Result.first->first();
597 }
598 
599 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
600   return getModule().getNamedValue(Name);
601 }
602 
603 /// AddGlobalCtor - Add a function to the list that will be called before
604 /// main() runs.
605 void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
606                                   llvm::Constant *AssociatedData) {
607   // FIXME: Type coercion of void()* types.
608   GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData));
609 }
610 
611 /// AddGlobalDtor - Add a function to the list that will be called
612 /// when the module is unloaded.
613 void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) {
614   // FIXME: Type coercion of void()* types.
615   GlobalDtors.push_back(Structor(Priority, Dtor, nullptr));
616 }
617 
618 void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
619   // Ctor function type is void()*.
620   llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
621   llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
622 
623   // Get the type of a ctor entry, { i32, void ()*, i8* }.
624   llvm::StructType *CtorStructTy = llvm::StructType::get(
625       Int32Ty, llvm::PointerType::getUnqual(CtorFTy), VoidPtrTy, nullptr);
626 
627   // Construct the constructor and destructor arrays.
628   SmallVector<llvm::Constant*, 8> Ctors;
629   for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
630     llvm::Constant *S[] = {
631       llvm::ConstantInt::get(Int32Ty, I->Priority, false),
632       llvm::ConstantExpr::getBitCast(I->Initializer, CtorPFTy),
633       (I->AssociatedData
634            ? llvm::ConstantExpr::getBitCast(I->AssociatedData, VoidPtrTy)
635            : llvm::Constant::getNullValue(VoidPtrTy))
636     };
637     Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
638   }
639 
640   if (!Ctors.empty()) {
641     llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
642     new llvm::GlobalVariable(TheModule, AT, false,
643                              llvm::GlobalValue::AppendingLinkage,
644                              llvm::ConstantArray::get(AT, Ctors),
645                              GlobalName);
646   }
647 }
648 
649 llvm::GlobalValue::LinkageTypes
650 CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
651   const auto *D = cast<FunctionDecl>(GD.getDecl());
652 
653   GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
654 
655   if (isa<CXXDestructorDecl>(D) &&
656       getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
657                                          GD.getDtorType())) {
658     // Destructor variants in the Microsoft C++ ABI are always internal or
659     // linkonce_odr thunks emitted on an as-needed basis.
660     return Linkage == GVA_Internal ? llvm::GlobalValue::InternalLinkage
661                                    : llvm::GlobalValue::LinkOnceODRLinkage;
662   }
663 
664   return getLLVMLinkageForDeclarator(D, Linkage, /*isConstantVariable=*/false);
665 }
666 
667 void CodeGenModule::setFunctionDefinitionAttributes(const FunctionDecl *D,
668                                                     llvm::Function *F) {
669   setNonAliasAttributes(D, F);
670 }
671 
672 void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
673                                               const CGFunctionInfo &Info,
674                                               llvm::Function *F) {
675   unsigned CallingConv;
676   AttributeListType AttributeList;
677   ConstructAttributeList(Info, D, AttributeList, CallingConv, false);
678   F->setAttributes(llvm::AttributeSet::get(getLLVMContext(), AttributeList));
679   F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
680 }
681 
682 /// Determines whether the language options require us to model
683 /// unwind exceptions.  We treat -fexceptions as mandating this
684 /// except under the fragile ObjC ABI with only ObjC exceptions
685 /// enabled.  This means, for example, that C with -fexceptions
686 /// enables this.
687 static bool hasUnwindExceptions(const LangOptions &LangOpts) {
688   // If exceptions are completely disabled, obviously this is false.
689   if (!LangOpts.Exceptions) return false;
690 
691   // If C++ exceptions are enabled, this is true.
692   if (LangOpts.CXXExceptions) return true;
693 
694   // If ObjC exceptions are enabled, this depends on the ABI.
695   if (LangOpts.ObjCExceptions) {
696     return LangOpts.ObjCRuntime.hasUnwindExceptions();
697   }
698 
699   return true;
700 }
701 
702 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
703                                                            llvm::Function *F) {
704   llvm::AttrBuilder B;
705 
706   if (CodeGenOpts.UnwindTables)
707     B.addAttribute(llvm::Attribute::UWTable);
708 
709   if (!hasUnwindExceptions(LangOpts))
710     B.addAttribute(llvm::Attribute::NoUnwind);
711 
712   if (D->hasAttr<NakedAttr>()) {
713     // Naked implies noinline: we should not be inlining such functions.
714     B.addAttribute(llvm::Attribute::Naked);
715     B.addAttribute(llvm::Attribute::NoInline);
716   } else if (D->hasAttr<NoDuplicateAttr>()) {
717     B.addAttribute(llvm::Attribute::NoDuplicate);
718   } else if (D->hasAttr<NoInlineAttr>()) {
719     B.addAttribute(llvm::Attribute::NoInline);
720   } else if (D->hasAttr<AlwaysInlineAttr>() &&
721              !F->getAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex,
722                                               llvm::Attribute::NoInline)) {
723     // (noinline wins over always_inline, and we can't specify both in IR)
724     B.addAttribute(llvm::Attribute::AlwaysInline);
725   }
726 
727   if (D->hasAttr<ColdAttr>()) {
728     if (!D->hasAttr<OptimizeNoneAttr>())
729       B.addAttribute(llvm::Attribute::OptimizeForSize);
730     B.addAttribute(llvm::Attribute::Cold);
731   }
732 
733   if (D->hasAttr<MinSizeAttr>())
734     B.addAttribute(llvm::Attribute::MinSize);
735 
736   if (LangOpts.getStackProtector() == LangOptions::SSPOn)
737     B.addAttribute(llvm::Attribute::StackProtect);
738   else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
739     B.addAttribute(llvm::Attribute::StackProtectStrong);
740   else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
741     B.addAttribute(llvm::Attribute::StackProtectReq);
742 
743   // Add sanitizer attributes if function is not blacklisted.
744   if (!isInSanitizerBlacklist(F, D->getLocation())) {
745     // When AddressSanitizer is enabled, set SanitizeAddress attribute
746     // unless __attribute__((no_sanitize_address)) is used.
747     if (LangOpts.Sanitize.has(SanitizerKind::Address) &&
748         !D->hasAttr<NoSanitizeAddressAttr>())
749       B.addAttribute(llvm::Attribute::SanitizeAddress);
750     // Same for ThreadSanitizer and __attribute__((no_sanitize_thread))
751     if (LangOpts.Sanitize.has(SanitizerKind::Thread) &&
752         !D->hasAttr<NoSanitizeThreadAttr>())
753       B.addAttribute(llvm::Attribute::SanitizeThread);
754     // Same for MemorySanitizer and __attribute__((no_sanitize_memory))
755     if (LangOpts.Sanitize.has(SanitizerKind::Memory) &&
756         !D->hasAttr<NoSanitizeMemoryAttr>())
757       B.addAttribute(llvm::Attribute::SanitizeMemory);
758   }
759 
760   F->addAttributes(llvm::AttributeSet::FunctionIndex,
761                    llvm::AttributeSet::get(
762                        F->getContext(), llvm::AttributeSet::FunctionIndex, B));
763 
764   if (D->hasAttr<OptimizeNoneAttr>()) {
765     // OptimizeNone implies noinline; we should not be inlining such functions.
766     F->addFnAttr(llvm::Attribute::OptimizeNone);
767     F->addFnAttr(llvm::Attribute::NoInline);
768 
769     // OptimizeNone wins over OptimizeForSize, MinSize, AlwaysInline.
770     assert(!F->hasFnAttribute(llvm::Attribute::OptimizeForSize) &&
771            "OptimizeNone and OptimizeForSize on same function!");
772     assert(!F->hasFnAttribute(llvm::Attribute::MinSize) &&
773            "OptimizeNone and MinSize on same function!");
774     assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
775            "OptimizeNone and AlwaysInline on same function!");
776 
777     // Attribute 'inlinehint' has no effect on 'optnone' functions.
778     // Explicitly remove it from the set of function attributes.
779     F->removeFnAttr(llvm::Attribute::InlineHint);
780   }
781 
782   if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
783     F->setUnnamedAddr(true);
784   else if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
785     if (MD->isVirtual())
786       F->setUnnamedAddr(true);
787 
788   unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
789   if (alignment)
790     F->setAlignment(alignment);
791 
792   // C++ ABI requires 2-byte alignment for member functions.
793   if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
794     F->setAlignment(2);
795 }
796 
797 void CodeGenModule::SetCommonAttributes(const Decl *D,
798                                         llvm::GlobalValue *GV) {
799   if (const auto *ND = dyn_cast<NamedDecl>(D))
800     setGlobalVisibility(GV, ND);
801   else
802     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
803 
804   if (D->hasAttr<UsedAttr>())
805     addUsedGlobal(GV);
806 }
807 
808 void CodeGenModule::setAliasAttributes(const Decl *D,
809                                        llvm::GlobalValue *GV) {
810   SetCommonAttributes(D, GV);
811 
812   // Process the dllexport attribute based on whether the original definition
813   // (not necessarily the aliasee) was exported.
814   if (D->hasAttr<DLLExportAttr>())
815     GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
816 }
817 
818 void CodeGenModule::setNonAliasAttributes(const Decl *D,
819                                           llvm::GlobalObject *GO) {
820   SetCommonAttributes(D, GO);
821 
822   if (const SectionAttr *SA = D->getAttr<SectionAttr>())
823     GO->setSection(SA->getName());
824 
825   getTargetCodeGenInfo().SetTargetAttributes(D, GO, *this);
826 }
827 
828 void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
829                                                   llvm::Function *F,
830                                                   const CGFunctionInfo &FI) {
831   SetLLVMFunctionAttributes(D, FI, F);
832   SetLLVMFunctionAttributesForDefinition(D, F);
833 
834   F->setLinkage(llvm::Function::InternalLinkage);
835 
836   setNonAliasAttributes(D, F);
837 }
838 
839 static void setLinkageAndVisibilityForGV(llvm::GlobalValue *GV,
840                                          const NamedDecl *ND) {
841   // Set linkage and visibility in case we never see a definition.
842   LinkageInfo LV = ND->getLinkageAndVisibility();
843   if (LV.getLinkage() != ExternalLinkage) {
844     // Don't set internal linkage on declarations.
845   } else {
846     if (ND->hasAttr<DLLImportAttr>()) {
847       GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
848       GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
849     } else if (ND->hasAttr<DLLExportAttr>()) {
850       GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
851       GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
852     } else if (ND->hasAttr<WeakAttr>() || ND->isWeakImported()) {
853       // "extern_weak" is overloaded in LLVM; we probably should have
854       // separate linkage types for this.
855       GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
856     }
857 
858     // Set visibility on a declaration only if it's explicit.
859     if (LV.isVisibilityExplicit())
860       GV->setVisibility(CodeGenModule::GetLLVMVisibility(LV.getVisibility()));
861   }
862 }
863 
864 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
865                                           bool IsIncompleteFunction,
866                                           bool IsThunk) {
867   if (unsigned IID = F->getIntrinsicID()) {
868     // If this is an intrinsic function, set the function's attributes
869     // to the intrinsic's attributes.
870     F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(),
871                                                     (llvm::Intrinsic::ID)IID));
872     return;
873   }
874 
875   const auto *FD = cast<FunctionDecl>(GD.getDecl());
876 
877   if (!IsIncompleteFunction)
878     SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F);
879 
880   // Add the Returned attribute for "this", except for iOS 5 and earlier
881   // where substantial code, including the libstdc++ dylib, was compiled with
882   // GCC and does not actually return "this".
883   if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
884       !(getTarget().getTriple().isiOS() &&
885         getTarget().getTriple().isOSVersionLT(6))) {
886     assert(!F->arg_empty() &&
887            F->arg_begin()->getType()
888              ->canLosslesslyBitCastTo(F->getReturnType()) &&
889            "unexpected this return");
890     F->addAttribute(1, llvm::Attribute::Returned);
891   }
892 
893   // Only a few attributes are set on declarations; these may later be
894   // overridden by a definition.
895 
896   setLinkageAndVisibilityForGV(F, FD);
897 
898   if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(FD)) {
899     if (getCXXABI().useThunkForDtorVariant(Dtor, GD.getDtorType())) {
900       // Don't dllexport/import destructor thunks.
901       F->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
902     }
903   }
904 
905   if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
906     F->setSection(SA->getName());
907 
908   // A replaceable global allocation function does not act like a builtin by
909   // default, only if it is invoked by a new-expression or delete-expression.
910   if (FD->isReplaceableGlobalAllocationFunction())
911     F->addAttribute(llvm::AttributeSet::FunctionIndex,
912                     llvm::Attribute::NoBuiltin);
913 }
914 
915 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
916   assert(!GV->isDeclaration() &&
917          "Only globals with definition can force usage.");
918   LLVMUsed.push_back(GV);
919 }
920 
921 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
922   assert(!GV->isDeclaration() &&
923          "Only globals with definition can force usage.");
924   LLVMCompilerUsed.push_back(GV);
925 }
926 
927 static void emitUsed(CodeGenModule &CGM, StringRef Name,
928                      std::vector<llvm::WeakVH> &List) {
929   // Don't create llvm.used if there is no need.
930   if (List.empty())
931     return;
932 
933   // Convert List to what ConstantArray needs.
934   SmallVector<llvm::Constant*, 8> UsedArray;
935   UsedArray.resize(List.size());
936   for (unsigned i = 0, e = List.size(); i != e; ++i) {
937     UsedArray[i] =
938         llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
939             cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
940   }
941 
942   if (UsedArray.empty())
943     return;
944   llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
945 
946   auto *GV = new llvm::GlobalVariable(
947       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
948       llvm::ConstantArray::get(ATy, UsedArray), Name);
949 
950   GV->setSection("llvm.metadata");
951 }
952 
953 void CodeGenModule::emitLLVMUsed() {
954   emitUsed(*this, "llvm.used", LLVMUsed);
955   emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
956 }
957 
958 void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
959   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
960   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
961 }
962 
963 void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
964   llvm::SmallString<32> Opt;
965   getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
966   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
967   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
968 }
969 
970 void CodeGenModule::AddDependentLib(StringRef Lib) {
971   llvm::SmallString<24> Opt;
972   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
973   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
974   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
975 }
976 
977 /// \brief Add link options implied by the given module, including modules
978 /// it depends on, using a postorder walk.
979 static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
980                                     SmallVectorImpl<llvm::Metadata *> &Metadata,
981                                     llvm::SmallPtrSet<Module *, 16> &Visited) {
982   // Import this module's parent.
983   if (Mod->Parent && Visited.insert(Mod->Parent).second) {
984     addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
985   }
986 
987   // Import this module's dependencies.
988   for (unsigned I = Mod->Imports.size(); I > 0; --I) {
989     if (Visited.insert(Mod->Imports[I - 1]).second)
990       addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited);
991   }
992 
993   // Add linker options to link against the libraries/frameworks
994   // described by this module.
995   llvm::LLVMContext &Context = CGM.getLLVMContext();
996   for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
997     // Link against a framework.  Frameworks are currently Darwin only, so we
998     // don't to ask TargetCodeGenInfo for the spelling of the linker option.
999     if (Mod->LinkLibraries[I-1].IsFramework) {
1000       llvm::Metadata *Args[2] = {
1001           llvm::MDString::get(Context, "-framework"),
1002           llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)};
1003 
1004       Metadata.push_back(llvm::MDNode::get(Context, Args));
1005       continue;
1006     }
1007 
1008     // Link against a library.
1009     llvm::SmallString<24> Opt;
1010     CGM.getTargetCodeGenInfo().getDependentLibraryOption(
1011       Mod->LinkLibraries[I-1].Library, Opt);
1012     auto *OptString = llvm::MDString::get(Context, Opt);
1013     Metadata.push_back(llvm::MDNode::get(Context, OptString));
1014   }
1015 }
1016 
1017 void CodeGenModule::EmitModuleLinkOptions() {
1018   // Collect the set of all of the modules we want to visit to emit link
1019   // options, which is essentially the imported modules and all of their
1020   // non-explicit child modules.
1021   llvm::SetVector<clang::Module *> LinkModules;
1022   llvm::SmallPtrSet<clang::Module *, 16> Visited;
1023   SmallVector<clang::Module *, 16> Stack;
1024 
1025   // Seed the stack with imported modules.
1026   for (llvm::SetVector<clang::Module *>::iterator M = ImportedModules.begin(),
1027                                                MEnd = ImportedModules.end();
1028        M != MEnd; ++M) {
1029     if (Visited.insert(*M).second)
1030       Stack.push_back(*M);
1031   }
1032 
1033   // Find all of the modules to import, making a little effort to prune
1034   // non-leaf modules.
1035   while (!Stack.empty()) {
1036     clang::Module *Mod = Stack.pop_back_val();
1037 
1038     bool AnyChildren = false;
1039 
1040     // Visit the submodules of this module.
1041     for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
1042                                         SubEnd = Mod->submodule_end();
1043          Sub != SubEnd; ++Sub) {
1044       // Skip explicit children; they need to be explicitly imported to be
1045       // linked against.
1046       if ((*Sub)->IsExplicit)
1047         continue;
1048 
1049       if (Visited.insert(*Sub).second) {
1050         Stack.push_back(*Sub);
1051         AnyChildren = true;
1052       }
1053     }
1054 
1055     // We didn't find any children, so add this module to the list of
1056     // modules to link against.
1057     if (!AnyChildren) {
1058       LinkModules.insert(Mod);
1059     }
1060   }
1061 
1062   // Add link options for all of the imported modules in reverse topological
1063   // order.  We don't do anything to try to order import link flags with respect
1064   // to linker options inserted by things like #pragma comment().
1065   SmallVector<llvm::Metadata *, 16> MetadataArgs;
1066   Visited.clear();
1067   for (llvm::SetVector<clang::Module *>::iterator M = LinkModules.begin(),
1068                                                MEnd = LinkModules.end();
1069        M != MEnd; ++M) {
1070     if (Visited.insert(*M).second)
1071       addLinkOptionsPostorder(*this, *M, MetadataArgs, Visited);
1072   }
1073   std::reverse(MetadataArgs.begin(), MetadataArgs.end());
1074   LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
1075 
1076   // Add the linker options metadata flag.
1077   getModule().addModuleFlag(llvm::Module::AppendUnique, "Linker Options",
1078                             llvm::MDNode::get(getLLVMContext(),
1079                                               LinkerOptionsMetadata));
1080 }
1081 
1082 void CodeGenModule::EmitDeferred() {
1083   // Emit code for any potentially referenced deferred decls.  Since a
1084   // previously unused static decl may become used during the generation of code
1085   // for a static function, iterate until no changes are made.
1086 
1087   if (!DeferredVTables.empty()) {
1088     EmitDeferredVTables();
1089 
1090     // Emitting a v-table doesn't directly cause more v-tables to
1091     // become deferred, although it can cause functions to be
1092     // emitted that then need those v-tables.
1093     assert(DeferredVTables.empty());
1094   }
1095 
1096   // Stop if we're out of both deferred v-tables and deferred declarations.
1097   if (DeferredDeclsToEmit.empty())
1098     return;
1099 
1100   // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
1101   // work, it will not interfere with this.
1102   std::vector<DeferredGlobal> CurDeclsToEmit;
1103   CurDeclsToEmit.swap(DeferredDeclsToEmit);
1104 
1105   for (DeferredGlobal &G : CurDeclsToEmit) {
1106     GlobalDecl D = G.GD;
1107     llvm::GlobalValue *GV = G.GV;
1108     G.GV = nullptr;
1109 
1110     assert(!GV || GV == GetGlobalValue(getMangledName(D)));
1111     if (!GV)
1112       GV = GetGlobalValue(getMangledName(D));
1113 
1114     // Check to see if we've already emitted this.  This is necessary
1115     // for a couple of reasons: first, decls can end up in the
1116     // deferred-decls queue multiple times, and second, decls can end
1117     // up with definitions in unusual ways (e.g. by an extern inline
1118     // function acquiring a strong function redefinition).  Just
1119     // ignore these cases.
1120     if (GV && !GV->isDeclaration())
1121       continue;
1122 
1123     // Otherwise, emit the definition and move on to the next one.
1124     EmitGlobalDefinition(D, GV);
1125 
1126     // If we found out that we need to emit more decls, do that recursively.
1127     // This has the advantage that the decls are emitted in a DFS and related
1128     // ones are close together, which is convenient for testing.
1129     if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
1130       EmitDeferred();
1131       assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
1132     }
1133   }
1134 }
1135 
1136 void CodeGenModule::EmitGlobalAnnotations() {
1137   if (Annotations.empty())
1138     return;
1139 
1140   // Create a new global variable for the ConstantStruct in the Module.
1141   llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
1142     Annotations[0]->getType(), Annotations.size()), Annotations);
1143   auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
1144                                       llvm::GlobalValue::AppendingLinkage,
1145                                       Array, "llvm.global.annotations");
1146   gv->setSection(AnnotationSection);
1147 }
1148 
1149 llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
1150   llvm::Constant *&AStr = AnnotationStrings[Str];
1151   if (AStr)
1152     return AStr;
1153 
1154   // Not found yet, create a new global.
1155   llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
1156   auto *gv =
1157       new llvm::GlobalVariable(getModule(), s->getType(), true,
1158                                llvm::GlobalValue::PrivateLinkage, s, ".str");
1159   gv->setSection(AnnotationSection);
1160   gv->setUnnamedAddr(true);
1161   AStr = gv;
1162   return gv;
1163 }
1164 
1165 llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
1166   SourceManager &SM = getContext().getSourceManager();
1167   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
1168   if (PLoc.isValid())
1169     return EmitAnnotationString(PLoc.getFilename());
1170   return EmitAnnotationString(SM.getBufferName(Loc));
1171 }
1172 
1173 llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
1174   SourceManager &SM = getContext().getSourceManager();
1175   PresumedLoc PLoc = SM.getPresumedLoc(L);
1176   unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
1177     SM.getExpansionLineNumber(L);
1178   return llvm::ConstantInt::get(Int32Ty, LineNo);
1179 }
1180 
1181 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
1182                                                 const AnnotateAttr *AA,
1183                                                 SourceLocation L) {
1184   // Get the globals for file name, annotation, and the line number.
1185   llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
1186                  *UnitGV = EmitAnnotationUnit(L),
1187                  *LineNoCst = EmitAnnotationLineNo(L);
1188 
1189   // Create the ConstantStruct for the global annotation.
1190   llvm::Constant *Fields[4] = {
1191     llvm::ConstantExpr::getBitCast(GV, Int8PtrTy),
1192     llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
1193     llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
1194     LineNoCst
1195   };
1196   return llvm::ConstantStruct::getAnon(Fields);
1197 }
1198 
1199 void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
1200                                          llvm::GlobalValue *GV) {
1201   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
1202   // Get the struct elements for these annotations.
1203   for (const auto *I : D->specific_attrs<AnnotateAttr>())
1204     Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
1205 }
1206 
1207 bool CodeGenModule::isInSanitizerBlacklist(llvm::Function *Fn,
1208                                            SourceLocation Loc) const {
1209   const auto &SanitizerBL = getContext().getSanitizerBlacklist();
1210   // Blacklist by function name.
1211   if (SanitizerBL.isBlacklistedFunction(Fn->getName()))
1212     return true;
1213   // Blacklist by location.
1214   if (!Loc.isInvalid())
1215     return SanitizerBL.isBlacklistedLocation(Loc);
1216   // If location is unknown, this may be a compiler-generated function. Assume
1217   // it's located in the main file.
1218   auto &SM = Context.getSourceManager();
1219   if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1220     return SanitizerBL.isBlacklistedFile(MainFile->getName());
1221   }
1222   return false;
1223 }
1224 
1225 bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV,
1226                                            SourceLocation Loc, QualType Ty,
1227                                            StringRef Category) const {
1228   // For now globals can be blacklisted only in ASan.
1229   if (!LangOpts.Sanitize.has(SanitizerKind::Address))
1230     return false;
1231   const auto &SanitizerBL = getContext().getSanitizerBlacklist();
1232   if (SanitizerBL.isBlacklistedGlobal(GV->getName(), Category))
1233     return true;
1234   if (SanitizerBL.isBlacklistedLocation(Loc, Category))
1235     return true;
1236   // Check global type.
1237   if (!Ty.isNull()) {
1238     // Drill down the array types: if global variable of a fixed type is
1239     // blacklisted, we also don't instrument arrays of them.
1240     while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
1241       Ty = AT->getElementType();
1242     Ty = Ty.getCanonicalType().getUnqualifiedType();
1243     // We allow to blacklist only record types (classes, structs etc.)
1244     if (Ty->isRecordType()) {
1245       std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
1246       if (SanitizerBL.isBlacklistedType(TypeStr, Category))
1247         return true;
1248     }
1249   }
1250   return false;
1251 }
1252 
1253 bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
1254   // Never defer when EmitAllDecls is specified.
1255   if (LangOpts.EmitAllDecls)
1256     return true;
1257 
1258   return getContext().DeclMustBeEmitted(Global);
1259 }
1260 
1261 bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
1262   if (const auto *FD = dyn_cast<FunctionDecl>(Global))
1263     if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
1264       // Implicit template instantiations may change linkage if they are later
1265       // explicitly instantiated, so they should not be emitted eagerly.
1266       return false;
1267 
1268   return true;
1269 }
1270 
1271 llvm::Constant *CodeGenModule::GetAddrOfUuidDescriptor(
1272     const CXXUuidofExpr* E) {
1273   // Sema has verified that IIDSource has a __declspec(uuid()), and that its
1274   // well-formed.
1275   StringRef Uuid = E->getUuidAsStringRef(Context);
1276   std::string Name = "_GUID_" + Uuid.lower();
1277   std::replace(Name.begin(), Name.end(), '-', '_');
1278 
1279   // Look for an existing global.
1280   if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
1281     return GV;
1282 
1283   llvm::Constant *Init = EmitUuidofInitializer(Uuid);
1284   assert(Init && "failed to initialize as constant");
1285 
1286   auto *GV = new llvm::GlobalVariable(
1287       getModule(), Init->getType(),
1288       /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
1289   if (supportsCOMDAT())
1290     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
1291   return GV;
1292 }
1293 
1294 llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
1295   const AliasAttr *AA = VD->getAttr<AliasAttr>();
1296   assert(AA && "No alias?");
1297 
1298   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
1299 
1300   // See if there is already something with the target's name in the module.
1301   llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
1302   if (Entry) {
1303     unsigned AS = getContext().getTargetAddressSpace(VD->getType());
1304     return llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
1305   }
1306 
1307   llvm::Constant *Aliasee;
1308   if (isa<llvm::FunctionType>(DeclTy))
1309     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
1310                                       GlobalDecl(cast<FunctionDecl>(VD)),
1311                                       /*ForVTable=*/false);
1312   else
1313     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
1314                                     llvm::PointerType::getUnqual(DeclTy),
1315                                     nullptr);
1316 
1317   auto *F = cast<llvm::GlobalValue>(Aliasee);
1318   F->setLinkage(llvm::Function::ExternalWeakLinkage);
1319   WeakRefReferences.insert(F);
1320 
1321   return Aliasee;
1322 }
1323 
1324 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
1325   const auto *Global = cast<ValueDecl>(GD.getDecl());
1326 
1327   // Weak references don't produce any output by themselves.
1328   if (Global->hasAttr<WeakRefAttr>())
1329     return;
1330 
1331   // If this is an alias definition (which otherwise looks like a declaration)
1332   // emit it now.
1333   if (Global->hasAttr<AliasAttr>())
1334     return EmitAliasDefinition(GD);
1335 
1336   // If this is CUDA, be selective about which declarations we emit.
1337   if (LangOpts.CUDA) {
1338     if (CodeGenOpts.CUDAIsDevice) {
1339       if (!Global->hasAttr<CUDADeviceAttr>() &&
1340           !Global->hasAttr<CUDAGlobalAttr>() &&
1341           !Global->hasAttr<CUDAConstantAttr>() &&
1342           !Global->hasAttr<CUDASharedAttr>())
1343         return;
1344     } else {
1345       if (!Global->hasAttr<CUDAHostAttr>() && (
1346             Global->hasAttr<CUDADeviceAttr>() ||
1347             Global->hasAttr<CUDAConstantAttr>() ||
1348             Global->hasAttr<CUDASharedAttr>()))
1349         return;
1350     }
1351   }
1352 
1353   // Ignore declarations, they will be emitted on their first use.
1354   if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
1355     // Forward declarations are emitted lazily on first use.
1356     if (!FD->doesThisDeclarationHaveABody()) {
1357       if (!FD->doesDeclarationForceExternallyVisibleDefinition())
1358         return;
1359 
1360       StringRef MangledName = getMangledName(GD);
1361 
1362       // Compute the function info and LLVM type.
1363       const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
1364       llvm::Type *Ty = getTypes().GetFunctionType(FI);
1365 
1366       GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
1367                               /*DontDefer=*/false);
1368       return;
1369     }
1370   } else {
1371     const auto *VD = cast<VarDecl>(Global);
1372     assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
1373 
1374     if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
1375         !Context.isMSStaticDataMemberInlineDefinition(VD))
1376       return;
1377   }
1378 
1379   // Defer code generation to first use when possible, e.g. if this is an inline
1380   // function. If the global must always be emitted, do it eagerly if possible
1381   // to benefit from cache locality.
1382   if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
1383     // Emit the definition if it can't be deferred.
1384     EmitGlobalDefinition(GD);
1385     return;
1386   }
1387 
1388   // If we're deferring emission of a C++ variable with an
1389   // initializer, remember the order in which it appeared in the file.
1390   if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
1391       cast<VarDecl>(Global)->hasInit()) {
1392     DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
1393     CXXGlobalInits.push_back(nullptr);
1394   }
1395 
1396   StringRef MangledName = getMangledName(GD);
1397   if (llvm::GlobalValue *GV = GetGlobalValue(MangledName)) {
1398     // The value has already been used and should therefore be emitted.
1399     addDeferredDeclToEmit(GV, GD);
1400   } else if (MustBeEmitted(Global)) {
1401     // The value must be emitted, but cannot be emitted eagerly.
1402     assert(!MayBeEmittedEagerly(Global));
1403     addDeferredDeclToEmit(/*GV=*/nullptr, GD);
1404   } else {
1405     // Otherwise, remember that we saw a deferred decl with this name.  The
1406     // first use of the mangled name will cause it to move into
1407     // DeferredDeclsToEmit.
1408     DeferredDecls[MangledName] = GD;
1409   }
1410 }
1411 
1412 namespace {
1413   struct FunctionIsDirectlyRecursive :
1414     public RecursiveASTVisitor<FunctionIsDirectlyRecursive> {
1415     const StringRef Name;
1416     const Builtin::Context &BI;
1417     bool Result;
1418     FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) :
1419       Name(N), BI(C), Result(false) {
1420     }
1421     typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base;
1422 
1423     bool TraverseCallExpr(CallExpr *E) {
1424       const FunctionDecl *FD = E->getDirectCallee();
1425       if (!FD)
1426         return true;
1427       AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
1428       if (Attr && Name == Attr->getLabel()) {
1429         Result = true;
1430         return false;
1431       }
1432       unsigned BuiltinID = FD->getBuiltinID();
1433       if (!BuiltinID)
1434         return true;
1435       StringRef BuiltinName = BI.GetName(BuiltinID);
1436       if (BuiltinName.startswith("__builtin_") &&
1437           Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
1438         Result = true;
1439         return false;
1440       }
1441       return true;
1442     }
1443   };
1444 }
1445 
1446 // isTriviallyRecursive - Check if this function calls another
1447 // decl that, because of the asm attribute or the other decl being a builtin,
1448 // ends up pointing to itself.
1449 bool
1450 CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
1451   StringRef Name;
1452   if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
1453     // asm labels are a special kind of mangling we have to support.
1454     AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
1455     if (!Attr)
1456       return false;
1457     Name = Attr->getLabel();
1458   } else {
1459     Name = FD->getName();
1460   }
1461 
1462   FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
1463   Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD));
1464   return Walker.Result;
1465 }
1466 
1467 bool
1468 CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
1469   if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
1470     return true;
1471   const auto *F = cast<FunctionDecl>(GD.getDecl());
1472   if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
1473     return false;
1474   // PR9614. Avoid cases where the source code is lying to us. An available
1475   // externally function should have an equivalent function somewhere else,
1476   // but a function that calls itself is clearly not equivalent to the real
1477   // implementation.
1478   // This happens in glibc's btowc and in some configure checks.
1479   return !isTriviallyRecursive(F);
1480 }
1481 
1482 /// If the type for the method's class was generated by
1483 /// CGDebugInfo::createContextChain(), the cache contains only a
1484 /// limited DIType without any declarations. Since EmitFunctionStart()
1485 /// needs to find the canonical declaration for each method, we need
1486 /// to construct the complete type prior to emitting the method.
1487 void CodeGenModule::CompleteDIClassType(const CXXMethodDecl* D) {
1488   if (!D->isInstance())
1489     return;
1490 
1491   if (CGDebugInfo *DI = getModuleDebugInfo())
1492     if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
1493       const auto *ThisPtr = cast<PointerType>(D->getThisType(getContext()));
1494       DI->getOrCreateRecordType(ThisPtr->getPointeeType(), D->getLocation());
1495     }
1496 }
1497 
1498 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
1499   const auto *D = cast<ValueDecl>(GD.getDecl());
1500 
1501   PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
1502                                  Context.getSourceManager(),
1503                                  "Generating code for declaration");
1504 
1505   if (isa<FunctionDecl>(D)) {
1506     // At -O0, don't generate IR for functions with available_externally
1507     // linkage.
1508     if (!shouldEmitFunction(GD))
1509       return;
1510 
1511     if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
1512       CompleteDIClassType(Method);
1513       // Make sure to emit the definition(s) before we emit the thunks.
1514       // This is necessary for the generation of certain thunks.
1515       if (const auto *CD = dyn_cast<CXXConstructorDecl>(Method))
1516         ABI->emitCXXStructor(CD, getFromCtorType(GD.getCtorType()));
1517       else if (const auto *DD = dyn_cast<CXXDestructorDecl>(Method))
1518         ABI->emitCXXStructor(DD, getFromDtorType(GD.getDtorType()));
1519       else
1520         EmitGlobalFunctionDefinition(GD, GV);
1521 
1522       if (Method->isVirtual())
1523         getVTables().EmitThunks(GD);
1524 
1525       return;
1526     }
1527 
1528     return EmitGlobalFunctionDefinition(GD, GV);
1529   }
1530 
1531   if (const auto *VD = dyn_cast<VarDecl>(D))
1532     return EmitGlobalVarDefinition(VD);
1533 
1534   llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
1535 }
1536 
1537 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
1538 /// module, create and return an llvm Function with the specified type. If there
1539 /// is something in the module with the specified name, return it potentially
1540 /// bitcasted to the right type.
1541 ///
1542 /// If D is non-null, it specifies a decl that correspond to this.  This is used
1543 /// to set the attributes on the function when it is first created.
1544 llvm::Constant *
1545 CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName,
1546                                        llvm::Type *Ty,
1547                                        GlobalDecl GD, bool ForVTable,
1548                                        bool DontDefer, bool IsThunk,
1549                                        llvm::AttributeSet ExtraAttrs) {
1550   const Decl *D = GD.getDecl();
1551 
1552   // Lookup the entry, lazily creating it if necessary.
1553   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
1554   if (Entry) {
1555     if (WeakRefReferences.erase(Entry)) {
1556       const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
1557       if (FD && !FD->hasAttr<WeakAttr>())
1558         Entry->setLinkage(llvm::Function::ExternalLinkage);
1559     }
1560 
1561     // Handle dropped DLL attributes.
1562     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
1563       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
1564 
1565     if (Entry->getType()->getElementType() == Ty)
1566       return Entry;
1567 
1568     // Make sure the result is of the correct type.
1569     return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
1570   }
1571 
1572   // This function doesn't have a complete type (for example, the return
1573   // type is an incomplete struct). Use a fake type instead, and make
1574   // sure not to try to set attributes.
1575   bool IsIncompleteFunction = false;
1576 
1577   llvm::FunctionType *FTy;
1578   if (isa<llvm::FunctionType>(Ty)) {
1579     FTy = cast<llvm::FunctionType>(Ty);
1580   } else {
1581     FTy = llvm::FunctionType::get(VoidTy, false);
1582     IsIncompleteFunction = true;
1583   }
1584 
1585   llvm::Function *F = llvm::Function::Create(FTy,
1586                                              llvm::Function::ExternalLinkage,
1587                                              MangledName, &getModule());
1588   assert(F->getName() == MangledName && "name was uniqued!");
1589   if (D)
1590     SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
1591   if (ExtraAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex)) {
1592     llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeSet::FunctionIndex);
1593     F->addAttributes(llvm::AttributeSet::FunctionIndex,
1594                      llvm::AttributeSet::get(VMContext,
1595                                              llvm::AttributeSet::FunctionIndex,
1596                                              B));
1597   }
1598 
1599   if (!DontDefer) {
1600     // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
1601     // each other bottoming out with the base dtor.  Therefore we emit non-base
1602     // dtors on usage, even if there is no dtor definition in the TU.
1603     if (D && isa<CXXDestructorDecl>(D) &&
1604         getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
1605                                            GD.getDtorType()))
1606       addDeferredDeclToEmit(F, GD);
1607 
1608     // This is the first use or definition of a mangled name.  If there is a
1609     // deferred decl with this name, remember that we need to emit it at the end
1610     // of the file.
1611     auto DDI = DeferredDecls.find(MangledName);
1612     if (DDI != DeferredDecls.end()) {
1613       // Move the potentially referenced deferred decl to the
1614       // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
1615       // don't need it anymore).
1616       addDeferredDeclToEmit(F, DDI->second);
1617       DeferredDecls.erase(DDI);
1618 
1619       // Otherwise, if this is a sized deallocation function, emit a weak
1620       // definition
1621       // for it at the end of the translation unit.
1622     } else if (D && cast<FunctionDecl>(D)
1623                         ->getCorrespondingUnsizedGlobalDeallocationFunction()) {
1624       addDeferredDeclToEmit(F, GD);
1625 
1626       // Otherwise, there are cases we have to worry about where we're
1627       // using a declaration for which we must emit a definition but where
1628       // we might not find a top-level definition:
1629       //   - member functions defined inline in their classes
1630       //   - friend functions defined inline in some class
1631       //   - special member functions with implicit definitions
1632       // If we ever change our AST traversal to walk into class methods,
1633       // this will be unnecessary.
1634       //
1635       // We also don't emit a definition for a function if it's going to be an
1636       // entry in a vtable, unless it's already marked as used.
1637     } else if (getLangOpts().CPlusPlus && D) {
1638       // Look for a declaration that's lexically in a record.
1639       for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
1640            FD = FD->getPreviousDecl()) {
1641         if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
1642           if (FD->doesThisDeclarationHaveABody()) {
1643             addDeferredDeclToEmit(F, GD.getWithDecl(FD));
1644             break;
1645           }
1646         }
1647       }
1648     }
1649   }
1650 
1651   // Make sure the result is of the requested type.
1652   if (!IsIncompleteFunction) {
1653     assert(F->getType()->getElementType() == Ty);
1654     return F;
1655   }
1656 
1657   llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
1658   return llvm::ConstantExpr::getBitCast(F, PTy);
1659 }
1660 
1661 /// GetAddrOfFunction - Return the address of the given function.  If Ty is
1662 /// non-null, then this function will use the specified type if it has to
1663 /// create it (this occurs when we see a definition of the function).
1664 llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
1665                                                  llvm::Type *Ty,
1666                                                  bool ForVTable,
1667                                                  bool DontDefer) {
1668   // If there was no specific requested type, just convert it now.
1669   if (!Ty)
1670     Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
1671 
1672   StringRef MangledName = getMangledName(GD);
1673   return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer);
1674 }
1675 
1676 /// CreateRuntimeFunction - Create a new runtime function with the specified
1677 /// type and name.
1678 llvm::Constant *
1679 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy,
1680                                      StringRef Name,
1681                                      llvm::AttributeSet ExtraAttrs) {
1682   llvm::Constant *C =
1683       GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
1684                               /*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs);
1685   if (auto *F = dyn_cast<llvm::Function>(C))
1686     if (F->empty())
1687       F->setCallingConv(getRuntimeCC());
1688   return C;
1689 }
1690 
1691 /// CreateBuiltinFunction - Create a new builtin function with the specified
1692 /// type and name.
1693 llvm::Constant *
1694 CodeGenModule::CreateBuiltinFunction(llvm::FunctionType *FTy,
1695                                      StringRef Name,
1696                                      llvm::AttributeSet ExtraAttrs) {
1697   llvm::Constant *C =
1698       GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
1699                               /*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs);
1700   if (auto *F = dyn_cast<llvm::Function>(C))
1701     if (F->empty())
1702       F->setCallingConv(getBuiltinCC());
1703   return C;
1704 }
1705 
1706 /// isTypeConstant - Determine whether an object of this type can be emitted
1707 /// as a constant.
1708 ///
1709 /// If ExcludeCtor is true, the duration when the object's constructor runs
1710 /// will not be considered. The caller will need to verify that the object is
1711 /// not written to during its construction.
1712 bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
1713   if (!Ty.isConstant(Context) && !Ty->isReferenceType())
1714     return false;
1715 
1716   if (Context.getLangOpts().CPlusPlus) {
1717     if (const CXXRecordDecl *Record
1718           = Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
1719       return ExcludeCtor && !Record->hasMutableFields() &&
1720              Record->hasTrivialDestructor();
1721   }
1722 
1723   return true;
1724 }
1725 
1726 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
1727 /// create and return an llvm GlobalVariable with the specified type.  If there
1728 /// is something in the module with the specified name, return it potentially
1729 /// bitcasted to the right type.
1730 ///
1731 /// If D is non-null, it specifies a decl that correspond to this.  This is used
1732 /// to set the attributes on the global when it is first created.
1733 llvm::Constant *
1734 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
1735                                      llvm::PointerType *Ty,
1736                                      const VarDecl *D) {
1737   // Lookup the entry, lazily creating it if necessary.
1738   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
1739   if (Entry) {
1740     if (WeakRefReferences.erase(Entry)) {
1741       if (D && !D->hasAttr<WeakAttr>())
1742         Entry->setLinkage(llvm::Function::ExternalLinkage);
1743     }
1744 
1745     // Handle dropped DLL attributes.
1746     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
1747       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
1748 
1749     if (Entry->getType() == Ty)
1750       return Entry;
1751 
1752     // Make sure the result is of the correct type.
1753     if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace())
1754       return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty);
1755 
1756     return llvm::ConstantExpr::getBitCast(Entry, Ty);
1757   }
1758 
1759   unsigned AddrSpace = GetGlobalVarAddressSpace(D, Ty->getAddressSpace());
1760   auto *GV = new llvm::GlobalVariable(
1761       getModule(), Ty->getElementType(), false,
1762       llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr,
1763       llvm::GlobalVariable::NotThreadLocal, AddrSpace);
1764 
1765   // This is the first use or definition of a mangled name.  If there is a
1766   // deferred decl with this name, remember that we need to emit it at the end
1767   // of the file.
1768   auto DDI = DeferredDecls.find(MangledName);
1769   if (DDI != DeferredDecls.end()) {
1770     // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
1771     // list, and remove it from DeferredDecls (since we don't need it anymore).
1772     addDeferredDeclToEmit(GV, DDI->second);
1773     DeferredDecls.erase(DDI);
1774   }
1775 
1776   // Handle things which are present even on external declarations.
1777   if (D) {
1778     // FIXME: This code is overly simple and should be merged with other global
1779     // handling.
1780     GV->setConstant(isTypeConstant(D->getType(), false));
1781 
1782     setLinkageAndVisibilityForGV(GV, D);
1783 
1784     if (D->getTLSKind()) {
1785       if (D->getTLSKind() == VarDecl::TLS_Dynamic)
1786         CXXThreadLocals.push_back(std::make_pair(D, GV));
1787       setTLSMode(GV, *D);
1788     }
1789 
1790     // If required by the ABI, treat declarations of static data members with
1791     // inline initializers as definitions.
1792     if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
1793       EmitGlobalVarDefinition(D);
1794     }
1795 
1796     // Handle XCore specific ABI requirements.
1797     if (getTarget().getTriple().getArch() == llvm::Triple::xcore &&
1798         D->getLanguageLinkage() == CLanguageLinkage &&
1799         D->getType().isConstant(Context) &&
1800         isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
1801       GV->setSection(".cp.rodata");
1802   }
1803 
1804   if (AddrSpace != Ty->getAddressSpace())
1805     return llvm::ConstantExpr::getAddrSpaceCast(GV, Ty);
1806 
1807   return GV;
1808 }
1809 
1810 
1811 llvm::GlobalVariable *
1812 CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name,
1813                                       llvm::Type *Ty,
1814                                       llvm::GlobalValue::LinkageTypes Linkage) {
1815   llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
1816   llvm::GlobalVariable *OldGV = nullptr;
1817 
1818   if (GV) {
1819     // Check if the variable has the right type.
1820     if (GV->getType()->getElementType() == Ty)
1821       return GV;
1822 
1823     // Because C++ name mangling, the only way we can end up with an already
1824     // existing global with the same name is if it has been declared extern "C".
1825     assert(GV->isDeclaration() && "Declaration has wrong type!");
1826     OldGV = GV;
1827   }
1828 
1829   // Create a new variable.
1830   GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
1831                                 Linkage, nullptr, Name);
1832 
1833   if (OldGV) {
1834     // Replace occurrences of the old variable if needed.
1835     GV->takeName(OldGV);
1836 
1837     if (!OldGV->use_empty()) {
1838       llvm::Constant *NewPtrForOldDecl =
1839       llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
1840       OldGV->replaceAllUsesWith(NewPtrForOldDecl);
1841     }
1842 
1843     OldGV->eraseFromParent();
1844   }
1845 
1846   if (supportsCOMDAT() && GV->isWeakForLinker())
1847     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
1848 
1849   return GV;
1850 }
1851 
1852 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
1853 /// given global variable.  If Ty is non-null and if the global doesn't exist,
1854 /// then it will be created with the specified type instead of whatever the
1855 /// normal requested type would be.
1856 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
1857                                                   llvm::Type *Ty) {
1858   assert(D->hasGlobalStorage() && "Not a global variable");
1859   QualType ASTTy = D->getType();
1860   if (!Ty)
1861     Ty = getTypes().ConvertTypeForMem(ASTTy);
1862 
1863   llvm::PointerType *PTy =
1864     llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
1865 
1866   StringRef MangledName = getMangledName(D);
1867   return GetOrCreateLLVMGlobal(MangledName, PTy, D);
1868 }
1869 
1870 /// CreateRuntimeVariable - Create a new runtime global variable with the
1871 /// specified type and name.
1872 llvm::Constant *
1873 CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
1874                                      StringRef Name) {
1875   return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), nullptr);
1876 }
1877 
1878 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
1879   assert(!D->getInit() && "Cannot emit definite definitions here!");
1880 
1881   if (!MustBeEmitted(D)) {
1882     // If we have not seen a reference to this variable yet, place it
1883     // into the deferred declarations table to be emitted if needed
1884     // later.
1885     StringRef MangledName = getMangledName(D);
1886     if (!GetGlobalValue(MangledName)) {
1887       DeferredDecls[MangledName] = D;
1888       return;
1889     }
1890   }
1891 
1892   // The tentative definition is the only definition.
1893   EmitGlobalVarDefinition(D);
1894 }
1895 
1896 CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
1897     return Context.toCharUnitsFromBits(
1898       TheDataLayout.getTypeStoreSizeInBits(Ty));
1899 }
1900 
1901 unsigned CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D,
1902                                                  unsigned AddrSpace) {
1903   if (LangOpts.CUDA && CodeGenOpts.CUDAIsDevice) {
1904     if (D->hasAttr<CUDAConstantAttr>())
1905       AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_constant);
1906     else if (D->hasAttr<CUDASharedAttr>())
1907       AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_shared);
1908     else
1909       AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_device);
1910   }
1911 
1912   return AddrSpace;
1913 }
1914 
1915 template<typename SomeDecl>
1916 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
1917                                                llvm::GlobalValue *GV) {
1918   if (!getLangOpts().CPlusPlus)
1919     return;
1920 
1921   // Must have 'used' attribute, or else inline assembly can't rely on
1922   // the name existing.
1923   if (!D->template hasAttr<UsedAttr>())
1924     return;
1925 
1926   // Must have internal linkage and an ordinary name.
1927   if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
1928     return;
1929 
1930   // Must be in an extern "C" context. Entities declared directly within
1931   // a record are not extern "C" even if the record is in such a context.
1932   const SomeDecl *First = D->getFirstDecl();
1933   if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
1934     return;
1935 
1936   // OK, this is an internal linkage entity inside an extern "C" linkage
1937   // specification. Make a note of that so we can give it the "expected"
1938   // mangled name if nothing else is using that name.
1939   std::pair<StaticExternCMap::iterator, bool> R =
1940       StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
1941 
1942   // If we have multiple internal linkage entities with the same name
1943   // in extern "C" regions, none of them gets that name.
1944   if (!R.second)
1945     R.first->second = nullptr;
1946 }
1947 
1948 static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
1949   if (!CGM.supportsCOMDAT())
1950     return false;
1951 
1952   if (D.hasAttr<SelectAnyAttr>())
1953     return true;
1954 
1955   GVALinkage Linkage;
1956   if (auto *VD = dyn_cast<VarDecl>(&D))
1957     Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
1958   else
1959     Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
1960 
1961   switch (Linkage) {
1962   case GVA_Internal:
1963   case GVA_AvailableExternally:
1964   case GVA_StrongExternal:
1965     return false;
1966   case GVA_DiscardableODR:
1967   case GVA_StrongODR:
1968     return true;
1969   }
1970   llvm_unreachable("No such linkage");
1971 }
1972 
1973 void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
1974                                           llvm::GlobalObject &GO) {
1975   if (!shouldBeInCOMDAT(*this, D))
1976     return;
1977   GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
1978 }
1979 
1980 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
1981   llvm::Constant *Init = nullptr;
1982   QualType ASTTy = D->getType();
1983   CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
1984   bool NeedsGlobalCtor = false;
1985   bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor();
1986 
1987   const VarDecl *InitDecl;
1988   const Expr *InitExpr = D->getAnyInitializer(InitDecl);
1989 
1990   if (!InitExpr) {
1991     // This is a tentative definition; tentative definitions are
1992     // implicitly initialized with { 0 }.
1993     //
1994     // Note that tentative definitions are only emitted at the end of
1995     // a translation unit, so they should never have incomplete
1996     // type. In addition, EmitTentativeDefinition makes sure that we
1997     // never attempt to emit a tentative definition if a real one
1998     // exists. A use may still exists, however, so we still may need
1999     // to do a RAUW.
2000     assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
2001     Init = EmitNullConstant(D->getType());
2002   } else {
2003     initializedGlobalDecl = GlobalDecl(D);
2004     Init = EmitConstantInit(*InitDecl);
2005 
2006     if (!Init) {
2007       QualType T = InitExpr->getType();
2008       if (D->getType()->isReferenceType())
2009         T = D->getType();
2010 
2011       if (getLangOpts().CPlusPlus) {
2012         Init = EmitNullConstant(T);
2013         NeedsGlobalCtor = true;
2014       } else {
2015         ErrorUnsupported(D, "static initializer");
2016         Init = llvm::UndefValue::get(getTypes().ConvertType(T));
2017       }
2018     } else {
2019       // We don't need an initializer, so remove the entry for the delayed
2020       // initializer position (just in case this entry was delayed) if we
2021       // also don't need to register a destructor.
2022       if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
2023         DelayedCXXInitPosition.erase(D);
2024     }
2025   }
2026 
2027   llvm::Type* InitType = Init->getType();
2028   llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
2029 
2030   // Strip off a bitcast if we got one back.
2031   if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
2032     assert(CE->getOpcode() == llvm::Instruction::BitCast ||
2033            CE->getOpcode() == llvm::Instruction::AddrSpaceCast ||
2034            // All zero index gep.
2035            CE->getOpcode() == llvm::Instruction::GetElementPtr);
2036     Entry = CE->getOperand(0);
2037   }
2038 
2039   // Entry is now either a Function or GlobalVariable.
2040   auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
2041 
2042   // We have a definition after a declaration with the wrong type.
2043   // We must make a new GlobalVariable* and update everything that used OldGV
2044   // (a declaration or tentative definition) with the new GlobalVariable*
2045   // (which will be a definition).
2046   //
2047   // This happens if there is a prototype for a global (e.g.
2048   // "extern int x[];") and then a definition of a different type (e.g.
2049   // "int x[10];"). This also happens when an initializer has a different type
2050   // from the type of the global (this happens with unions).
2051   if (!GV ||
2052       GV->getType()->getElementType() != InitType ||
2053       GV->getType()->getAddressSpace() !=
2054        GetGlobalVarAddressSpace(D, getContext().getTargetAddressSpace(ASTTy))) {
2055 
2056     // Move the old entry aside so that we'll create a new one.
2057     Entry->setName(StringRef());
2058 
2059     // Make a new global with the correct type, this is now guaranteed to work.
2060     GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType));
2061 
2062     // Replace all uses of the old global with the new global
2063     llvm::Constant *NewPtrForOldDecl =
2064         llvm::ConstantExpr::getBitCast(GV, Entry->getType());
2065     Entry->replaceAllUsesWith(NewPtrForOldDecl);
2066 
2067     // Erase the old global, since it is no longer used.
2068     cast<llvm::GlobalValue>(Entry)->eraseFromParent();
2069   }
2070 
2071   MaybeHandleStaticInExternC(D, GV);
2072 
2073   if (D->hasAttr<AnnotateAttr>())
2074     AddGlobalAnnotations(D, GV);
2075 
2076   GV->setInitializer(Init);
2077 
2078   // If it is safe to mark the global 'constant', do so now.
2079   GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
2080                   isTypeConstant(D->getType(), true));
2081 
2082   // If it is in a read-only section, mark it 'constant'.
2083   if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
2084     const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
2085     if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
2086       GV->setConstant(true);
2087   }
2088 
2089   GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
2090 
2091   // Set the llvm linkage type as appropriate.
2092   llvm::GlobalValue::LinkageTypes Linkage =
2093       getLLVMLinkageVarDefinition(D, GV->isConstant());
2094 
2095   // On Darwin, the backing variable for a C++11 thread_local variable always
2096   // has internal linkage; all accesses should just be calls to the
2097   // Itanium-specified entry point, which has the normal linkage of the
2098   // variable.
2099   if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
2100       Context.getTargetInfo().getTriple().isMacOSX())
2101     Linkage = llvm::GlobalValue::InternalLinkage;
2102 
2103   GV->setLinkage(Linkage);
2104   if (D->hasAttr<DLLImportAttr>())
2105     GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
2106   else if (D->hasAttr<DLLExportAttr>())
2107     GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
2108   else
2109     GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
2110 
2111   if (Linkage == llvm::GlobalVariable::CommonLinkage)
2112     // common vars aren't constant even if declared const.
2113     GV->setConstant(false);
2114 
2115   setNonAliasAttributes(D, GV);
2116 
2117   if (D->getTLSKind() && !GV->isThreadLocal()) {
2118     if (D->getTLSKind() == VarDecl::TLS_Dynamic)
2119       CXXThreadLocals.push_back(std::make_pair(D, GV));
2120     setTLSMode(GV, *D);
2121   }
2122 
2123   maybeSetTrivialComdat(*D, *GV);
2124 
2125   // Emit the initializer function if necessary.
2126   if (NeedsGlobalCtor || NeedsGlobalDtor)
2127     EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
2128 
2129   SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
2130 
2131   // Emit global variable debug information.
2132   if (CGDebugInfo *DI = getModuleDebugInfo())
2133     if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
2134       DI->EmitGlobalVariable(GV, D);
2135 }
2136 
2137 static bool isVarDeclStrongDefinition(const ASTContext &Context,
2138                                       const VarDecl *D, bool NoCommon) {
2139   // Don't give variables common linkage if -fno-common was specified unless it
2140   // was overridden by a NoCommon attribute.
2141   if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
2142     return true;
2143 
2144   // C11 6.9.2/2:
2145   //   A declaration of an identifier for an object that has file scope without
2146   //   an initializer, and without a storage-class specifier or with the
2147   //   storage-class specifier static, constitutes a tentative definition.
2148   if (D->getInit() || D->hasExternalStorage())
2149     return true;
2150 
2151   // A variable cannot be both common and exist in a section.
2152   if (D->hasAttr<SectionAttr>())
2153     return true;
2154 
2155   // Thread local vars aren't considered common linkage.
2156   if (D->getTLSKind())
2157     return true;
2158 
2159   // Tentative definitions marked with WeakImportAttr are true definitions.
2160   if (D->hasAttr<WeakImportAttr>())
2161     return true;
2162 
2163   // Declarations with a required alignment do not have common linakge in MSVC
2164   // mode.
2165   if (Context.getLangOpts().MSVCCompat) {
2166     if (D->hasAttr<AlignedAttr>())
2167       return true;
2168     QualType VarType = D->getType();
2169     if (Context.isAlignmentRequired(VarType))
2170       return true;
2171 
2172     if (const auto *RT = VarType->getAs<RecordType>()) {
2173       const RecordDecl *RD = RT->getDecl();
2174       for (const FieldDecl *FD : RD->fields()) {
2175         if (FD->isBitField())
2176           continue;
2177         if (FD->hasAttr<AlignedAttr>())
2178           return true;
2179         if (Context.isAlignmentRequired(FD->getType()))
2180           return true;
2181       }
2182     }
2183   }
2184 
2185   return false;
2186 }
2187 
2188 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator(
2189     const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) {
2190   if (Linkage == GVA_Internal)
2191     return llvm::Function::InternalLinkage;
2192 
2193   if (D->hasAttr<WeakAttr>()) {
2194     if (IsConstantVariable)
2195       return llvm::GlobalVariable::WeakODRLinkage;
2196     else
2197       return llvm::GlobalVariable::WeakAnyLinkage;
2198   }
2199 
2200   // We are guaranteed to have a strong definition somewhere else,
2201   // so we can use available_externally linkage.
2202   if (Linkage == GVA_AvailableExternally)
2203     return llvm::Function::AvailableExternallyLinkage;
2204 
2205   // Note that Apple's kernel linker doesn't support symbol
2206   // coalescing, so we need to avoid linkonce and weak linkages there.
2207   // Normally, this means we just map to internal, but for explicit
2208   // instantiations we'll map to external.
2209 
2210   // In C++, the compiler has to emit a definition in every translation unit
2211   // that references the function.  We should use linkonce_odr because
2212   // a) if all references in this translation unit are optimized away, we
2213   // don't need to codegen it.  b) if the function persists, it needs to be
2214   // merged with other definitions. c) C++ has the ODR, so we know the
2215   // definition is dependable.
2216   if (Linkage == GVA_DiscardableODR)
2217     return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
2218                                             : llvm::Function::InternalLinkage;
2219 
2220   // An explicit instantiation of a template has weak linkage, since
2221   // explicit instantiations can occur in multiple translation units
2222   // and must all be equivalent. However, we are not allowed to
2223   // throw away these explicit instantiations.
2224   if (Linkage == GVA_StrongODR)
2225     return !Context.getLangOpts().AppleKext ? llvm::Function::WeakODRLinkage
2226                                             : llvm::Function::ExternalLinkage;
2227 
2228   // C++ doesn't have tentative definitions and thus cannot have common
2229   // linkage.
2230   if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
2231       !isVarDeclStrongDefinition(Context, cast<VarDecl>(D),
2232                                  CodeGenOpts.NoCommon))
2233     return llvm::GlobalVariable::CommonLinkage;
2234 
2235   // selectany symbols are externally visible, so use weak instead of
2236   // linkonce.  MSVC optimizes away references to const selectany globals, so
2237   // all definitions should be the same and ODR linkage should be used.
2238   // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
2239   if (D->hasAttr<SelectAnyAttr>())
2240     return llvm::GlobalVariable::WeakODRLinkage;
2241 
2242   // Otherwise, we have strong external linkage.
2243   assert(Linkage == GVA_StrongExternal);
2244   return llvm::GlobalVariable::ExternalLinkage;
2245 }
2246 
2247 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition(
2248     const VarDecl *VD, bool IsConstant) {
2249   GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
2250   return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant);
2251 }
2252 
2253 /// Replace the uses of a function that was declared with a non-proto type.
2254 /// We want to silently drop extra arguments from call sites
2255 static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
2256                                           llvm::Function *newFn) {
2257   // Fast path.
2258   if (old->use_empty()) return;
2259 
2260   llvm::Type *newRetTy = newFn->getReturnType();
2261   SmallVector<llvm::Value*, 4> newArgs;
2262 
2263   for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
2264          ui != ue; ) {
2265     llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
2266     llvm::User *user = use->getUser();
2267 
2268     // Recognize and replace uses of bitcasts.  Most calls to
2269     // unprototyped functions will use bitcasts.
2270     if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
2271       if (bitcast->getOpcode() == llvm::Instruction::BitCast)
2272         replaceUsesOfNonProtoConstant(bitcast, newFn);
2273       continue;
2274     }
2275 
2276     // Recognize calls to the function.
2277     llvm::CallSite callSite(user);
2278     if (!callSite) continue;
2279     if (!callSite.isCallee(&*use)) continue;
2280 
2281     // If the return types don't match exactly, then we can't
2282     // transform this call unless it's dead.
2283     if (callSite->getType() != newRetTy && !callSite->use_empty())
2284       continue;
2285 
2286     // Get the call site's attribute list.
2287     SmallVector<llvm::AttributeSet, 8> newAttrs;
2288     llvm::AttributeSet oldAttrs = callSite.getAttributes();
2289 
2290     // Collect any return attributes from the call.
2291     if (oldAttrs.hasAttributes(llvm::AttributeSet::ReturnIndex))
2292       newAttrs.push_back(
2293         llvm::AttributeSet::get(newFn->getContext(),
2294                                 oldAttrs.getRetAttributes()));
2295 
2296     // If the function was passed too few arguments, don't transform.
2297     unsigned newNumArgs = newFn->arg_size();
2298     if (callSite.arg_size() < newNumArgs) continue;
2299 
2300     // If extra arguments were passed, we silently drop them.
2301     // If any of the types mismatch, we don't transform.
2302     unsigned argNo = 0;
2303     bool dontTransform = false;
2304     for (llvm::Function::arg_iterator ai = newFn->arg_begin(),
2305            ae = newFn->arg_end(); ai != ae; ++ai, ++argNo) {
2306       if (callSite.getArgument(argNo)->getType() != ai->getType()) {
2307         dontTransform = true;
2308         break;
2309       }
2310 
2311       // Add any parameter attributes.
2312       if (oldAttrs.hasAttributes(argNo + 1))
2313         newAttrs.
2314           push_back(llvm::
2315                     AttributeSet::get(newFn->getContext(),
2316                                       oldAttrs.getParamAttributes(argNo + 1)));
2317     }
2318     if (dontTransform)
2319       continue;
2320 
2321     if (oldAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex))
2322       newAttrs.push_back(llvm::AttributeSet::get(newFn->getContext(),
2323                                                  oldAttrs.getFnAttributes()));
2324 
2325     // Okay, we can transform this.  Create the new call instruction and copy
2326     // over the required information.
2327     newArgs.append(callSite.arg_begin(), callSite.arg_begin() + argNo);
2328 
2329     llvm::CallSite newCall;
2330     if (callSite.isCall()) {
2331       newCall = llvm::CallInst::Create(newFn, newArgs, "",
2332                                        callSite.getInstruction());
2333     } else {
2334       auto *oldInvoke = cast<llvm::InvokeInst>(callSite.getInstruction());
2335       newCall = llvm::InvokeInst::Create(newFn,
2336                                          oldInvoke->getNormalDest(),
2337                                          oldInvoke->getUnwindDest(),
2338                                          newArgs, "",
2339                                          callSite.getInstruction());
2340     }
2341     newArgs.clear(); // for the next iteration
2342 
2343     if (!newCall->getType()->isVoidTy())
2344       newCall->takeName(callSite.getInstruction());
2345     newCall.setAttributes(
2346                      llvm::AttributeSet::get(newFn->getContext(), newAttrs));
2347     newCall.setCallingConv(callSite.getCallingConv());
2348 
2349     // Finally, remove the old call, replacing any uses with the new one.
2350     if (!callSite->use_empty())
2351       callSite->replaceAllUsesWith(newCall.getInstruction());
2352 
2353     // Copy debug location attached to CI.
2354     if (!callSite->getDebugLoc().isUnknown())
2355       newCall->setDebugLoc(callSite->getDebugLoc());
2356     callSite->eraseFromParent();
2357   }
2358 }
2359 
2360 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
2361 /// implement a function with no prototype, e.g. "int foo() {}".  If there are
2362 /// existing call uses of the old function in the module, this adjusts them to
2363 /// call the new function directly.
2364 ///
2365 /// This is not just a cleanup: the always_inline pass requires direct calls to
2366 /// functions to be able to inline them.  If there is a bitcast in the way, it
2367 /// won't inline them.  Instcombine normally deletes these calls, but it isn't
2368 /// run at -O0.
2369 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
2370                                                       llvm::Function *NewFn) {
2371   // If we're redefining a global as a function, don't transform it.
2372   if (!isa<llvm::Function>(Old)) return;
2373 
2374   replaceUsesOfNonProtoConstant(Old, NewFn);
2375 }
2376 
2377 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
2378   TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
2379   // If we have a definition, this might be a deferred decl. If the
2380   // instantiation is explicit, make sure we emit it at the end.
2381   if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
2382     GetAddrOfGlobalVar(VD);
2383 
2384   EmitTopLevelDecl(VD);
2385 }
2386 
2387 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
2388                                                  llvm::GlobalValue *GV) {
2389   const auto *D = cast<FunctionDecl>(GD.getDecl());
2390 
2391   // Compute the function info and LLVM type.
2392   const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
2393   llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
2394 
2395   // Get or create the prototype for the function.
2396   if (!GV) {
2397     llvm::Constant *C =
2398         GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer*/ true);
2399 
2400     // Strip off a bitcast if we got one back.
2401     if (auto *CE = dyn_cast<llvm::ConstantExpr>(C)) {
2402       assert(CE->getOpcode() == llvm::Instruction::BitCast);
2403       GV = cast<llvm::GlobalValue>(CE->getOperand(0));
2404     } else {
2405       GV = cast<llvm::GlobalValue>(C);
2406     }
2407   }
2408 
2409   if (!GV->isDeclaration()) {
2410     getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name);
2411     GlobalDecl OldGD = Manglings.lookup(GV->getName());
2412     if (auto *Prev = OldGD.getDecl())
2413       getDiags().Report(Prev->getLocation(), diag::note_previous_definition);
2414     return;
2415   }
2416 
2417   if (GV->getType()->getElementType() != Ty) {
2418     // If the types mismatch then we have to rewrite the definition.
2419     assert(GV->isDeclaration() && "Shouldn't replace non-declaration");
2420 
2421     // F is the Function* for the one with the wrong type, we must make a new
2422     // Function* and update everything that used F (a declaration) with the new
2423     // Function* (which will be a definition).
2424     //
2425     // This happens if there is a prototype for a function
2426     // (e.g. "int f()") and then a definition of a different type
2427     // (e.g. "int f(int x)").  Move the old function aside so that it
2428     // doesn't interfere with GetAddrOfFunction.
2429     GV->setName(StringRef());
2430     auto *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
2431 
2432     // This might be an implementation of a function without a
2433     // prototype, in which case, try to do special replacement of
2434     // calls which match the new prototype.  The really key thing here
2435     // is that we also potentially drop arguments from the call site
2436     // so as to make a direct call, which makes the inliner happier
2437     // and suppresses a number of optimizer warnings (!) about
2438     // dropping arguments.
2439     if (!GV->use_empty()) {
2440       ReplaceUsesOfNonProtoTypeWithRealFunction(GV, NewFn);
2441       GV->removeDeadConstantUsers();
2442     }
2443 
2444     // Replace uses of F with the Function we will endow with a body.
2445     if (!GV->use_empty()) {
2446       llvm::Constant *NewPtrForOldDecl =
2447           llvm::ConstantExpr::getBitCast(NewFn, GV->getType());
2448       GV->replaceAllUsesWith(NewPtrForOldDecl);
2449     }
2450 
2451     // Ok, delete the old function now, which is dead.
2452     GV->eraseFromParent();
2453 
2454     GV = NewFn;
2455   }
2456 
2457   // We need to set linkage and visibility on the function before
2458   // generating code for it because various parts of IR generation
2459   // want to propagate this information down (e.g. to local static
2460   // declarations).
2461   auto *Fn = cast<llvm::Function>(GV);
2462   setFunctionLinkage(GD, Fn);
2463   if (D->hasAttr<DLLImportAttr>())
2464     GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
2465   else if (D->hasAttr<DLLExportAttr>())
2466     GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
2467   else
2468     GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
2469 
2470   // FIXME: this is redundant with part of setFunctionDefinitionAttributes
2471   setGlobalVisibility(Fn, D);
2472 
2473   MaybeHandleStaticInExternC(D, Fn);
2474 
2475   maybeSetTrivialComdat(*D, *Fn);
2476 
2477   CodeGenFunction(*this).GenerateCode(D, Fn, FI);
2478 
2479   setFunctionDefinitionAttributes(D, Fn);
2480   SetLLVMFunctionAttributesForDefinition(D, Fn);
2481 
2482   if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
2483     AddGlobalCtor(Fn, CA->getPriority());
2484   if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
2485     AddGlobalDtor(Fn, DA->getPriority());
2486   if (D->hasAttr<AnnotateAttr>())
2487     AddGlobalAnnotations(D, Fn);
2488 }
2489 
2490 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
2491   const auto *D = cast<ValueDecl>(GD.getDecl());
2492   const AliasAttr *AA = D->getAttr<AliasAttr>();
2493   assert(AA && "Not an alias?");
2494 
2495   StringRef MangledName = getMangledName(GD);
2496 
2497   // If there is a definition in the module, then it wins over the alias.
2498   // This is dubious, but allow it to be safe.  Just ignore the alias.
2499   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
2500   if (Entry && !Entry->isDeclaration())
2501     return;
2502 
2503   Aliases.push_back(GD);
2504 
2505   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
2506 
2507   // Create a reference to the named value.  This ensures that it is emitted
2508   // if a deferred decl.
2509   llvm::Constant *Aliasee;
2510   if (isa<llvm::FunctionType>(DeclTy))
2511     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
2512                                       /*ForVTable=*/false);
2513   else
2514     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
2515                                     llvm::PointerType::getUnqual(DeclTy),
2516                                     /*D=*/nullptr);
2517 
2518   // Create the new alias itself, but don't set a name yet.
2519   auto *GA = llvm::GlobalAlias::create(
2520       cast<llvm::PointerType>(Aliasee->getType())->getElementType(), 0,
2521       llvm::Function::ExternalLinkage, "", Aliasee, &getModule());
2522 
2523   if (Entry) {
2524     if (GA->getAliasee() == Entry) {
2525       Diags.Report(AA->getLocation(), diag::err_cyclic_alias);
2526       return;
2527     }
2528 
2529     assert(Entry->isDeclaration());
2530 
2531     // If there is a declaration in the module, then we had an extern followed
2532     // by the alias, as in:
2533     //   extern int test6();
2534     //   ...
2535     //   int test6() __attribute__((alias("test7")));
2536     //
2537     // Remove it and replace uses of it with the alias.
2538     GA->takeName(Entry);
2539 
2540     Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
2541                                                           Entry->getType()));
2542     Entry->eraseFromParent();
2543   } else {
2544     GA->setName(MangledName);
2545   }
2546 
2547   // Set attributes which are particular to an alias; this is a
2548   // specialization of the attributes which may be set on a global
2549   // variable/function.
2550   if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
2551       D->isWeakImported()) {
2552     GA->setLinkage(llvm::Function::WeakAnyLinkage);
2553   }
2554 
2555   if (const auto *VD = dyn_cast<VarDecl>(D))
2556     if (VD->getTLSKind())
2557       setTLSMode(GA, *VD);
2558 
2559   setAliasAttributes(D, GA);
2560 }
2561 
2562 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
2563                                             ArrayRef<llvm::Type*> Tys) {
2564   return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
2565                                          Tys);
2566 }
2567 
2568 static llvm::StringMapEntry<llvm::Constant*> &
2569 GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map,
2570                          const StringLiteral *Literal,
2571                          bool TargetIsLSB,
2572                          bool &IsUTF16,
2573                          unsigned &StringLength) {
2574   StringRef String = Literal->getString();
2575   unsigned NumBytes = String.size();
2576 
2577   // Check for simple case.
2578   if (!Literal->containsNonAsciiOrNull()) {
2579     StringLength = NumBytes;
2580     return *Map.insert(std::make_pair(String, nullptr)).first;
2581   }
2582 
2583   // Otherwise, convert the UTF8 literals into a string of shorts.
2584   IsUTF16 = true;
2585 
2586   SmallVector<UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
2587   const UTF8 *FromPtr = (const UTF8 *)String.data();
2588   UTF16 *ToPtr = &ToBuf[0];
2589 
2590   (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
2591                            &ToPtr, ToPtr + NumBytes,
2592                            strictConversion);
2593 
2594   // ConvertUTF8toUTF16 returns the length in ToPtr.
2595   StringLength = ToPtr - &ToBuf[0];
2596 
2597   // Add an explicit null.
2598   *ToPtr = 0;
2599   return *Map.insert(std::make_pair(
2600                          StringRef(reinterpret_cast<const char *>(ToBuf.data()),
2601                                    (StringLength + 1) * 2),
2602                          nullptr)).first;
2603 }
2604 
2605 static llvm::StringMapEntry<llvm::Constant*> &
2606 GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map,
2607                        const StringLiteral *Literal,
2608                        unsigned &StringLength) {
2609   StringRef String = Literal->getString();
2610   StringLength = String.size();
2611   return *Map.insert(std::make_pair(String, nullptr)).first;
2612 }
2613 
2614 llvm::Constant *
2615 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
2616   unsigned StringLength = 0;
2617   bool isUTF16 = false;
2618   llvm::StringMapEntry<llvm::Constant*> &Entry =
2619     GetConstantCFStringEntry(CFConstantStringMap, Literal,
2620                              getDataLayout().isLittleEndian(),
2621                              isUTF16, StringLength);
2622 
2623   if (auto *C = Entry.second)
2624     return C;
2625 
2626   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
2627   llvm::Constant *Zeros[] = { Zero, Zero };
2628   llvm::Value *V;
2629 
2630   // If we don't already have it, get __CFConstantStringClassReference.
2631   if (!CFConstantStringClassRef) {
2632     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
2633     Ty = llvm::ArrayType::get(Ty, 0);
2634     llvm::Constant *GV = CreateRuntimeVariable(Ty,
2635                                            "__CFConstantStringClassReference");
2636     // Decay array -> ptr
2637     V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2638     CFConstantStringClassRef = V;
2639   }
2640   else
2641     V = CFConstantStringClassRef;
2642 
2643   QualType CFTy = getContext().getCFConstantStringType();
2644 
2645   auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
2646 
2647   llvm::Constant *Fields[4];
2648 
2649   // Class pointer.
2650   Fields[0] = cast<llvm::ConstantExpr>(V);
2651 
2652   // Flags.
2653   llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
2654   Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) :
2655     llvm::ConstantInt::get(Ty, 0x07C8);
2656 
2657   // String pointer.
2658   llvm::Constant *C = nullptr;
2659   if (isUTF16) {
2660     ArrayRef<uint16_t> Arr = llvm::makeArrayRef<uint16_t>(
2661         reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
2662         Entry.first().size() / 2);
2663     C = llvm::ConstantDataArray::get(VMContext, Arr);
2664   } else {
2665     C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
2666   }
2667 
2668   // Note: -fwritable-strings doesn't make the backing store strings of
2669   // CFStrings writable. (See <rdar://problem/10657500>)
2670   auto *GV =
2671       new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
2672                                llvm::GlobalValue::PrivateLinkage, C, ".str");
2673   GV->setUnnamedAddr(true);
2674   // Don't enforce the target's minimum global alignment, since the only use
2675   // of the string is via this class initializer.
2676   // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. Without
2677   // it LLVM can merge the string with a non unnamed_addr one during LTO. Doing
2678   // that changes the section it ends in, which surprises ld64.
2679   if (isUTF16) {
2680     CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
2681     GV->setAlignment(Align.getQuantity());
2682     GV->setSection("__TEXT,__ustring");
2683   } else {
2684     CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
2685     GV->setAlignment(Align.getQuantity());
2686     GV->setSection("__TEXT,__cstring,cstring_literals");
2687   }
2688 
2689   // String.
2690   Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2691 
2692   if (isUTF16)
2693     // Cast the UTF16 string to the correct type.
2694     Fields[2] = llvm::ConstantExpr::getBitCast(Fields[2], Int8PtrTy);
2695 
2696   // String length.
2697   Ty = getTypes().ConvertType(getContext().LongTy);
2698   Fields[3] = llvm::ConstantInt::get(Ty, StringLength);
2699 
2700   // The struct.
2701   C = llvm::ConstantStruct::get(STy, Fields);
2702   GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
2703                                 llvm::GlobalVariable::PrivateLinkage, C,
2704                                 "_unnamed_cfstring_");
2705   GV->setSection("__DATA,__cfstring");
2706   Entry.second = GV;
2707 
2708   return GV;
2709 }
2710 
2711 llvm::Constant *
2712 CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) {
2713   unsigned StringLength = 0;
2714   llvm::StringMapEntry<llvm::Constant*> &Entry =
2715     GetConstantStringEntry(CFConstantStringMap, Literal, StringLength);
2716 
2717   if (auto *C = Entry.second)
2718     return C;
2719 
2720   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
2721   llvm::Constant *Zeros[] = { Zero, Zero };
2722   llvm::Value *V;
2723   // If we don't already have it, get _NSConstantStringClassReference.
2724   if (!ConstantStringClassRef) {
2725     std::string StringClass(getLangOpts().ObjCConstantStringClass);
2726     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
2727     llvm::Constant *GV;
2728     if (LangOpts.ObjCRuntime.isNonFragile()) {
2729       std::string str =
2730         StringClass.empty() ? "OBJC_CLASS_$_NSConstantString"
2731                             : "OBJC_CLASS_$_" + StringClass;
2732       GV = getObjCRuntime().GetClassGlobal(str);
2733       // Make sure the result is of the correct type.
2734       llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
2735       V = llvm::ConstantExpr::getBitCast(GV, PTy);
2736       ConstantStringClassRef = V;
2737     } else {
2738       std::string str =
2739         StringClass.empty() ? "_NSConstantStringClassReference"
2740                             : "_" + StringClass + "ClassReference";
2741       llvm::Type *PTy = llvm::ArrayType::get(Ty, 0);
2742       GV = CreateRuntimeVariable(PTy, str);
2743       // Decay array -> ptr
2744       V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2745       ConstantStringClassRef = V;
2746     }
2747   }
2748   else
2749     V = ConstantStringClassRef;
2750 
2751   if (!NSConstantStringType) {
2752     // Construct the type for a constant NSString.
2753     RecordDecl *D = Context.buildImplicitRecord("__builtin_NSString");
2754     D->startDefinition();
2755 
2756     QualType FieldTypes[3];
2757 
2758     // const int *isa;
2759     FieldTypes[0] = Context.getPointerType(Context.IntTy.withConst());
2760     // const char *str;
2761     FieldTypes[1] = Context.getPointerType(Context.CharTy.withConst());
2762     // unsigned int length;
2763     FieldTypes[2] = Context.UnsignedIntTy;
2764 
2765     // Create fields
2766     for (unsigned i = 0; i < 3; ++i) {
2767       FieldDecl *Field = FieldDecl::Create(Context, D,
2768                                            SourceLocation(),
2769                                            SourceLocation(), nullptr,
2770                                            FieldTypes[i], /*TInfo=*/nullptr,
2771                                            /*BitWidth=*/nullptr,
2772                                            /*Mutable=*/false,
2773                                            ICIS_NoInit);
2774       Field->setAccess(AS_public);
2775       D->addDecl(Field);
2776     }
2777 
2778     D->completeDefinition();
2779     QualType NSTy = Context.getTagDeclType(D);
2780     NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy));
2781   }
2782 
2783   llvm::Constant *Fields[3];
2784 
2785   // Class pointer.
2786   Fields[0] = cast<llvm::ConstantExpr>(V);
2787 
2788   // String pointer.
2789   llvm::Constant *C =
2790       llvm::ConstantDataArray::getString(VMContext, Entry.first());
2791 
2792   llvm::GlobalValue::LinkageTypes Linkage;
2793   bool isConstant;
2794   Linkage = llvm::GlobalValue::PrivateLinkage;
2795   isConstant = !LangOpts.WritableStrings;
2796 
2797   auto *GV = new llvm::GlobalVariable(getModule(), C->getType(), isConstant,
2798                                       Linkage, C, ".str");
2799   GV->setUnnamedAddr(true);
2800   // Don't enforce the target's minimum global alignment, since the only use
2801   // of the string is via this class initializer.
2802   CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
2803   GV->setAlignment(Align.getQuantity());
2804   Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2805 
2806   // String length.
2807   llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
2808   Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
2809 
2810   // The struct.
2811   C = llvm::ConstantStruct::get(NSConstantStringType, Fields);
2812   GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
2813                                 llvm::GlobalVariable::PrivateLinkage, C,
2814                                 "_unnamed_nsstring_");
2815   const char *NSStringSection = "__OBJC,__cstring_object,regular,no_dead_strip";
2816   const char *NSStringNonFragileABISection =
2817       "__DATA,__objc_stringobj,regular,no_dead_strip";
2818   // FIXME. Fix section.
2819   GV->setSection(LangOpts.ObjCRuntime.isNonFragile()
2820                      ? NSStringNonFragileABISection
2821                      : NSStringSection);
2822   Entry.second = GV;
2823 
2824   return GV;
2825 }
2826 
2827 QualType CodeGenModule::getObjCFastEnumerationStateType() {
2828   if (ObjCFastEnumerationStateType.isNull()) {
2829     RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
2830     D->startDefinition();
2831 
2832     QualType FieldTypes[] = {
2833       Context.UnsignedLongTy,
2834       Context.getPointerType(Context.getObjCIdType()),
2835       Context.getPointerType(Context.UnsignedLongTy),
2836       Context.getConstantArrayType(Context.UnsignedLongTy,
2837                            llvm::APInt(32, 5), ArrayType::Normal, 0)
2838     };
2839 
2840     for (size_t i = 0; i < 4; ++i) {
2841       FieldDecl *Field = FieldDecl::Create(Context,
2842                                            D,
2843                                            SourceLocation(),
2844                                            SourceLocation(), nullptr,
2845                                            FieldTypes[i], /*TInfo=*/nullptr,
2846                                            /*BitWidth=*/nullptr,
2847                                            /*Mutable=*/false,
2848                                            ICIS_NoInit);
2849       Field->setAccess(AS_public);
2850       D->addDecl(Field);
2851     }
2852 
2853     D->completeDefinition();
2854     ObjCFastEnumerationStateType = Context.getTagDeclType(D);
2855   }
2856 
2857   return ObjCFastEnumerationStateType;
2858 }
2859 
2860 llvm::Constant *
2861 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
2862   assert(!E->getType()->isPointerType() && "Strings are always arrays");
2863 
2864   // Don't emit it as the address of the string, emit the string data itself
2865   // as an inline array.
2866   if (E->getCharByteWidth() == 1) {
2867     SmallString<64> Str(E->getString());
2868 
2869     // Resize the string to the right size, which is indicated by its type.
2870     const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
2871     Str.resize(CAT->getSize().getZExtValue());
2872     return llvm::ConstantDataArray::getString(VMContext, Str, false);
2873   }
2874 
2875   auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
2876   llvm::Type *ElemTy = AType->getElementType();
2877   unsigned NumElements = AType->getNumElements();
2878 
2879   // Wide strings have either 2-byte or 4-byte elements.
2880   if (ElemTy->getPrimitiveSizeInBits() == 16) {
2881     SmallVector<uint16_t, 32> Elements;
2882     Elements.reserve(NumElements);
2883 
2884     for(unsigned i = 0, e = E->getLength(); i != e; ++i)
2885       Elements.push_back(E->getCodeUnit(i));
2886     Elements.resize(NumElements);
2887     return llvm::ConstantDataArray::get(VMContext, Elements);
2888   }
2889 
2890   assert(ElemTy->getPrimitiveSizeInBits() == 32);
2891   SmallVector<uint32_t, 32> Elements;
2892   Elements.reserve(NumElements);
2893 
2894   for(unsigned i = 0, e = E->getLength(); i != e; ++i)
2895     Elements.push_back(E->getCodeUnit(i));
2896   Elements.resize(NumElements);
2897   return llvm::ConstantDataArray::get(VMContext, Elements);
2898 }
2899 
2900 static llvm::GlobalVariable *
2901 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
2902                       CodeGenModule &CGM, StringRef GlobalName,
2903                       unsigned Alignment) {
2904   // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
2905   unsigned AddrSpace = 0;
2906   if (CGM.getLangOpts().OpenCL)
2907     AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant);
2908 
2909   llvm::Module &M = CGM.getModule();
2910   // Create a global variable for this string
2911   auto *GV = new llvm::GlobalVariable(
2912       M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
2913       nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
2914   GV->setAlignment(Alignment);
2915   GV->setUnnamedAddr(true);
2916   if (GV->isWeakForLinker()) {
2917     assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals");
2918     GV->setComdat(M.getOrInsertComdat(GV->getName()));
2919   }
2920 
2921   return GV;
2922 }
2923 
2924 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
2925 /// constant array for the given string literal.
2926 llvm::GlobalVariable *
2927 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
2928                                                   StringRef Name) {
2929   auto Alignment =
2930       getContext().getAlignOfGlobalVarInChars(S->getType()).getQuantity();
2931 
2932   llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
2933   llvm::GlobalVariable **Entry = nullptr;
2934   if (!LangOpts.WritableStrings) {
2935     Entry = &ConstantStringMap[C];
2936     if (auto GV = *Entry) {
2937       if (Alignment > GV->getAlignment())
2938         GV->setAlignment(Alignment);
2939       return GV;
2940     }
2941   }
2942 
2943   SmallString<256> MangledNameBuffer;
2944   StringRef GlobalVariableName;
2945   llvm::GlobalValue::LinkageTypes LT;
2946 
2947   // Mangle the string literal if the ABI allows for it.  However, we cannot
2948   // do this if  we are compiling with ASan or -fwritable-strings because they
2949   // rely on strings having normal linkage.
2950   if (!LangOpts.WritableStrings &&
2951       !LangOpts.Sanitize.has(SanitizerKind::Address) &&
2952       getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) {
2953     llvm::raw_svector_ostream Out(MangledNameBuffer);
2954     getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
2955     Out.flush();
2956 
2957     LT = llvm::GlobalValue::LinkOnceODRLinkage;
2958     GlobalVariableName = MangledNameBuffer;
2959   } else {
2960     LT = llvm::GlobalValue::PrivateLinkage;
2961     GlobalVariableName = Name;
2962   }
2963 
2964   auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
2965   if (Entry)
2966     *Entry = GV;
2967 
2968   SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>",
2969                                   QualType());
2970   return GV;
2971 }
2972 
2973 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
2974 /// array for the given ObjCEncodeExpr node.
2975 llvm::GlobalVariable *
2976 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
2977   std::string Str;
2978   getContext().getObjCEncodingForType(E->getEncodedType(), Str);
2979 
2980   return GetAddrOfConstantCString(Str);
2981 }
2982 
2983 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
2984 /// the literal and a terminating '\0' character.
2985 /// The result has pointer to array type.
2986 llvm::GlobalVariable *CodeGenModule::GetAddrOfConstantCString(
2987     const std::string &Str, const char *GlobalName, unsigned Alignment) {
2988   StringRef StrWithNull(Str.c_str(), Str.size() + 1);
2989   if (Alignment == 0) {
2990     Alignment = getContext()
2991                     .getAlignOfGlobalVarInChars(getContext().CharTy)
2992                     .getQuantity();
2993   }
2994 
2995   llvm::Constant *C =
2996       llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
2997 
2998   // Don't share any string literals if strings aren't constant.
2999   llvm::GlobalVariable **Entry = nullptr;
3000   if (!LangOpts.WritableStrings) {
3001     Entry = &ConstantStringMap[C];
3002     if (auto GV = *Entry) {
3003       if (Alignment > GV->getAlignment())
3004         GV->setAlignment(Alignment);
3005       return GV;
3006     }
3007   }
3008 
3009   // Get the default prefix if a name wasn't specified.
3010   if (!GlobalName)
3011     GlobalName = ".str";
3012   // Create a global variable for this.
3013   auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
3014                                   GlobalName, Alignment);
3015   if (Entry)
3016     *Entry = GV;
3017   return GV;
3018 }
3019 
3020 llvm::Constant *CodeGenModule::GetAddrOfGlobalTemporary(
3021     const MaterializeTemporaryExpr *E, const Expr *Init) {
3022   assert((E->getStorageDuration() == SD_Static ||
3023           E->getStorageDuration() == SD_Thread) && "not a global temporary");
3024   const auto *VD = cast<VarDecl>(E->getExtendingDecl());
3025 
3026   // If we're not materializing a subobject of the temporary, keep the
3027   // cv-qualifiers from the type of the MaterializeTemporaryExpr.
3028   QualType MaterializedType = Init->getType();
3029   if (Init == E->GetTemporaryExpr())
3030     MaterializedType = E->getType();
3031 
3032   llvm::Constant *&Slot = MaterializedGlobalTemporaryMap[E];
3033   if (Slot)
3034     return Slot;
3035 
3036   // FIXME: If an externally-visible declaration extends multiple temporaries,
3037   // we need to give each temporary the same name in every translation unit (and
3038   // we also need to make the temporaries externally-visible).
3039   SmallString<256> Name;
3040   llvm::raw_svector_ostream Out(Name);
3041   getCXXABI().getMangleContext().mangleReferenceTemporary(
3042       VD, E->getManglingNumber(), Out);
3043   Out.flush();
3044 
3045   APValue *Value = nullptr;
3046   if (E->getStorageDuration() == SD_Static) {
3047     // We might have a cached constant initializer for this temporary. Note
3048     // that this might have a different value from the value computed by
3049     // evaluating the initializer if the surrounding constant expression
3050     // modifies the temporary.
3051     Value = getContext().getMaterializedTemporaryValue(E, false);
3052     if (Value && Value->isUninit())
3053       Value = nullptr;
3054   }
3055 
3056   // Try evaluating it now, it might have a constant initializer.
3057   Expr::EvalResult EvalResult;
3058   if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
3059       !EvalResult.hasSideEffects())
3060     Value = &EvalResult.Val;
3061 
3062   llvm::Constant *InitialValue = nullptr;
3063   bool Constant = false;
3064   llvm::Type *Type;
3065   if (Value) {
3066     // The temporary has a constant initializer, use it.
3067     InitialValue = EmitConstantValue(*Value, MaterializedType, nullptr);
3068     Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
3069     Type = InitialValue->getType();
3070   } else {
3071     // No initializer, the initialization will be provided when we
3072     // initialize the declaration which performed lifetime extension.
3073     Type = getTypes().ConvertTypeForMem(MaterializedType);
3074   }
3075 
3076   // Create a global variable for this lifetime-extended temporary.
3077   llvm::GlobalValue::LinkageTypes Linkage =
3078       getLLVMLinkageVarDefinition(VD, Constant);
3079   // There is no need for this temporary to have global linkage if the global
3080   // variable has external linkage.
3081   if (Linkage == llvm::GlobalVariable::ExternalLinkage)
3082     Linkage = llvm::GlobalVariable::PrivateLinkage;
3083   unsigned AddrSpace = GetGlobalVarAddressSpace(
3084       VD, getContext().getTargetAddressSpace(MaterializedType));
3085   auto *GV = new llvm::GlobalVariable(
3086       getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
3087       /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal,
3088       AddrSpace);
3089   setGlobalVisibility(GV, VD);
3090   GV->setAlignment(
3091       getContext().getTypeAlignInChars(MaterializedType).getQuantity());
3092   if (VD->getTLSKind())
3093     setTLSMode(GV, *VD);
3094   Slot = GV;
3095   return GV;
3096 }
3097 
3098 /// EmitObjCPropertyImplementations - Emit information for synthesized
3099 /// properties for an implementation.
3100 void CodeGenModule::EmitObjCPropertyImplementations(const
3101                                                     ObjCImplementationDecl *D) {
3102   for (const auto *PID : D->property_impls()) {
3103     // Dynamic is just for type-checking.
3104     if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
3105       ObjCPropertyDecl *PD = PID->getPropertyDecl();
3106 
3107       // Determine which methods need to be implemented, some may have
3108       // been overridden. Note that ::isPropertyAccessor is not the method
3109       // we want, that just indicates if the decl came from a
3110       // property. What we want to know is if the method is defined in
3111       // this implementation.
3112       if (!D->getInstanceMethod(PD->getGetterName()))
3113         CodeGenFunction(*this).GenerateObjCGetter(
3114                                  const_cast<ObjCImplementationDecl *>(D), PID);
3115       if (!PD->isReadOnly() &&
3116           !D->getInstanceMethod(PD->getSetterName()))
3117         CodeGenFunction(*this).GenerateObjCSetter(
3118                                  const_cast<ObjCImplementationDecl *>(D), PID);
3119     }
3120   }
3121 }
3122 
3123 static bool needsDestructMethod(ObjCImplementationDecl *impl) {
3124   const ObjCInterfaceDecl *iface = impl->getClassInterface();
3125   for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
3126        ivar; ivar = ivar->getNextIvar())
3127     if (ivar->getType().isDestructedType())
3128       return true;
3129 
3130   return false;
3131 }
3132 
3133 static bool AllTrivialInitializers(CodeGenModule &CGM,
3134                                    ObjCImplementationDecl *D) {
3135   CodeGenFunction CGF(CGM);
3136   for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
3137        E = D->init_end(); B != E; ++B) {
3138     CXXCtorInitializer *CtorInitExp = *B;
3139     Expr *Init = CtorInitExp->getInit();
3140     if (!CGF.isTrivialInitializer(Init))
3141       return false;
3142   }
3143   return true;
3144 }
3145 
3146 /// EmitObjCIvarInitializations - Emit information for ivar initialization
3147 /// for an implementation.
3148 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
3149   // We might need a .cxx_destruct even if we don't have any ivar initializers.
3150   if (needsDestructMethod(D)) {
3151     IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
3152     Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
3153     ObjCMethodDecl *DTORMethod =
3154       ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(),
3155                              cxxSelector, getContext().VoidTy, nullptr, D,
3156                              /*isInstance=*/true, /*isVariadic=*/false,
3157                           /*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true,
3158                              /*isDefined=*/false, ObjCMethodDecl::Required);
3159     D->addInstanceMethod(DTORMethod);
3160     CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
3161     D->setHasDestructors(true);
3162   }
3163 
3164   // If the implementation doesn't have any ivar initializers, we don't need
3165   // a .cxx_construct.
3166   if (D->getNumIvarInitializers() == 0 ||
3167       AllTrivialInitializers(*this, D))
3168     return;
3169 
3170   IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
3171   Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
3172   // The constructor returns 'self'.
3173   ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
3174                                                 D->getLocation(),
3175                                                 D->getLocation(),
3176                                                 cxxSelector,
3177                                                 getContext().getObjCIdType(),
3178                                                 nullptr, D, /*isInstance=*/true,
3179                                                 /*isVariadic=*/false,
3180                                                 /*isPropertyAccessor=*/true,
3181                                                 /*isImplicitlyDeclared=*/true,
3182                                                 /*isDefined=*/false,
3183                                                 ObjCMethodDecl::Required);
3184   D->addInstanceMethod(CTORMethod);
3185   CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
3186   D->setHasNonZeroConstructors(true);
3187 }
3188 
3189 /// EmitNamespace - Emit all declarations in a namespace.
3190 void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
3191   for (auto *I : ND->decls()) {
3192     if (const auto *VD = dyn_cast<VarDecl>(I))
3193       if (VD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
3194           VD->getTemplateSpecializationKind() != TSK_Undeclared)
3195         continue;
3196     EmitTopLevelDecl(I);
3197   }
3198 }
3199 
3200 // EmitLinkageSpec - Emit all declarations in a linkage spec.
3201 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
3202   if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
3203       LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
3204     ErrorUnsupported(LSD, "linkage spec");
3205     return;
3206   }
3207 
3208   for (auto *I : LSD->decls()) {
3209     // Meta-data for ObjC class includes references to implemented methods.
3210     // Generate class's method definitions first.
3211     if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
3212       for (auto *M : OID->methods())
3213         EmitTopLevelDecl(M);
3214     }
3215     EmitTopLevelDecl(I);
3216   }
3217 }
3218 
3219 /// EmitTopLevelDecl - Emit code for a single top level declaration.
3220 void CodeGenModule::EmitTopLevelDecl(Decl *D) {
3221   // Ignore dependent declarations.
3222   if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
3223     return;
3224 
3225   switch (D->getKind()) {
3226   case Decl::CXXConversion:
3227   case Decl::CXXMethod:
3228   case Decl::Function:
3229     // Skip function templates
3230     if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
3231         cast<FunctionDecl>(D)->isLateTemplateParsed())
3232       return;
3233 
3234     EmitGlobal(cast<FunctionDecl>(D));
3235     // Always provide some coverage mapping
3236     // even for the functions that aren't emitted.
3237     AddDeferredUnusedCoverageMapping(D);
3238     break;
3239 
3240   case Decl::Var:
3241     // Skip variable templates
3242     if (cast<VarDecl>(D)->getDescribedVarTemplate())
3243       return;
3244   case Decl::VarTemplateSpecialization:
3245     EmitGlobal(cast<VarDecl>(D));
3246     break;
3247 
3248   // Indirect fields from global anonymous structs and unions can be
3249   // ignored; only the actual variable requires IR gen support.
3250   case Decl::IndirectField:
3251     break;
3252 
3253   // C++ Decls
3254   case Decl::Namespace:
3255     EmitNamespace(cast<NamespaceDecl>(D));
3256     break;
3257     // No code generation needed.
3258   case Decl::UsingShadow:
3259   case Decl::ClassTemplate:
3260   case Decl::VarTemplate:
3261   case Decl::VarTemplatePartialSpecialization:
3262   case Decl::FunctionTemplate:
3263   case Decl::TypeAliasTemplate:
3264   case Decl::Block:
3265   case Decl::Empty:
3266     break;
3267   case Decl::Using:          // using X; [C++]
3268     if (CGDebugInfo *DI = getModuleDebugInfo())
3269         DI->EmitUsingDecl(cast<UsingDecl>(*D));
3270     return;
3271   case Decl::NamespaceAlias:
3272     if (CGDebugInfo *DI = getModuleDebugInfo())
3273         DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
3274     return;
3275   case Decl::UsingDirective: // using namespace X; [C++]
3276     if (CGDebugInfo *DI = getModuleDebugInfo())
3277       DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
3278     return;
3279   case Decl::CXXConstructor:
3280     // Skip function templates
3281     if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
3282         cast<FunctionDecl>(D)->isLateTemplateParsed())
3283       return;
3284 
3285     getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
3286     break;
3287   case Decl::CXXDestructor:
3288     if (cast<FunctionDecl>(D)->isLateTemplateParsed())
3289       return;
3290     getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
3291     break;
3292 
3293   case Decl::StaticAssert:
3294     // Nothing to do.
3295     break;
3296 
3297   // Objective-C Decls
3298 
3299   // Forward declarations, no (immediate) code generation.
3300   case Decl::ObjCInterface:
3301   case Decl::ObjCCategory:
3302     break;
3303 
3304   case Decl::ObjCProtocol: {
3305     auto *Proto = cast<ObjCProtocolDecl>(D);
3306     if (Proto->isThisDeclarationADefinition())
3307       ObjCRuntime->GenerateProtocol(Proto);
3308     break;
3309   }
3310 
3311   case Decl::ObjCCategoryImpl:
3312     // Categories have properties but don't support synthesize so we
3313     // can ignore them here.
3314     ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
3315     break;
3316 
3317   case Decl::ObjCImplementation: {
3318     auto *OMD = cast<ObjCImplementationDecl>(D);
3319     EmitObjCPropertyImplementations(OMD);
3320     EmitObjCIvarInitializations(OMD);
3321     ObjCRuntime->GenerateClass(OMD);
3322     // Emit global variable debug information.
3323     if (CGDebugInfo *DI = getModuleDebugInfo())
3324       if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
3325         DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
3326             OMD->getClassInterface()), OMD->getLocation());
3327     break;
3328   }
3329   case Decl::ObjCMethod: {
3330     auto *OMD = cast<ObjCMethodDecl>(D);
3331     // If this is not a prototype, emit the body.
3332     if (OMD->getBody())
3333       CodeGenFunction(*this).GenerateObjCMethod(OMD);
3334     break;
3335   }
3336   case Decl::ObjCCompatibleAlias:
3337     ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
3338     break;
3339 
3340   case Decl::LinkageSpec:
3341     EmitLinkageSpec(cast<LinkageSpecDecl>(D));
3342     break;
3343 
3344   case Decl::FileScopeAsm: {
3345     auto *AD = cast<FileScopeAsmDecl>(D);
3346     StringRef AsmString = AD->getAsmString()->getString();
3347 
3348     const std::string &S = getModule().getModuleInlineAsm();
3349     if (S.empty())
3350       getModule().setModuleInlineAsm(AsmString);
3351     else if (S.end()[-1] == '\n')
3352       getModule().setModuleInlineAsm(S + AsmString.str());
3353     else
3354       getModule().setModuleInlineAsm(S + '\n' + AsmString.str());
3355     break;
3356   }
3357 
3358   case Decl::Import: {
3359     auto *Import = cast<ImportDecl>(D);
3360 
3361     // Ignore import declarations that come from imported modules.
3362     if (clang::Module *Owner = Import->getOwningModule()) {
3363       if (getLangOpts().CurrentModule.empty() ||
3364           Owner->getTopLevelModule()->Name == getLangOpts().CurrentModule)
3365         break;
3366     }
3367 
3368     ImportedModules.insert(Import->getImportedModule());
3369     break;
3370   }
3371 
3372   case Decl::OMPThreadPrivate:
3373     EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
3374     break;
3375 
3376   case Decl::ClassTemplateSpecialization: {
3377     const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
3378     if (DebugInfo &&
3379         Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition &&
3380         Spec->hasDefinition())
3381       DebugInfo->completeTemplateDefinition(*Spec);
3382     break;
3383   }
3384 
3385   default:
3386     // Make sure we handled everything we should, every other kind is a
3387     // non-top-level decl.  FIXME: Would be nice to have an isTopLevelDeclKind
3388     // function. Need to recode Decl::Kind to do that easily.
3389     assert(isa<TypeDecl>(D) && "Unsupported decl kind");
3390     break;
3391   }
3392 }
3393 
3394 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
3395   // Do we need to generate coverage mapping?
3396   if (!CodeGenOpts.CoverageMapping)
3397     return;
3398   switch (D->getKind()) {
3399   case Decl::CXXConversion:
3400   case Decl::CXXMethod:
3401   case Decl::Function:
3402   case Decl::ObjCMethod:
3403   case Decl::CXXConstructor:
3404   case Decl::CXXDestructor: {
3405     if (!cast<FunctionDecl>(D)->hasBody())
3406       return;
3407     auto I = DeferredEmptyCoverageMappingDecls.find(D);
3408     if (I == DeferredEmptyCoverageMappingDecls.end())
3409       DeferredEmptyCoverageMappingDecls[D] = true;
3410     break;
3411   }
3412   default:
3413     break;
3414   };
3415 }
3416 
3417 void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) {
3418   // Do we need to generate coverage mapping?
3419   if (!CodeGenOpts.CoverageMapping)
3420     return;
3421   if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
3422     if (Fn->isTemplateInstantiation())
3423       ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
3424   }
3425   auto I = DeferredEmptyCoverageMappingDecls.find(D);
3426   if (I == DeferredEmptyCoverageMappingDecls.end())
3427     DeferredEmptyCoverageMappingDecls[D] = false;
3428   else
3429     I->second = false;
3430 }
3431 
3432 void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
3433   std::vector<const Decl *> DeferredDecls;
3434   for (const auto I : DeferredEmptyCoverageMappingDecls) {
3435     if (!I.second)
3436       continue;
3437     DeferredDecls.push_back(I.first);
3438   }
3439   // Sort the declarations by their location to make sure that the tests get a
3440   // predictable order for the coverage mapping for the unused declarations.
3441   if (CodeGenOpts.DumpCoverageMapping)
3442     std::sort(DeferredDecls.begin(), DeferredDecls.end(),
3443               [] (const Decl *LHS, const Decl *RHS) {
3444       return LHS->getLocStart() < RHS->getLocStart();
3445     });
3446   for (const auto *D : DeferredDecls) {
3447     switch (D->getKind()) {
3448     case Decl::CXXConversion:
3449     case Decl::CXXMethod:
3450     case Decl::Function:
3451     case Decl::ObjCMethod: {
3452       CodeGenPGO PGO(*this);
3453       GlobalDecl GD(cast<FunctionDecl>(D));
3454       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
3455                                   getFunctionLinkage(GD));
3456       break;
3457     }
3458     case Decl::CXXConstructor: {
3459       CodeGenPGO PGO(*this);
3460       GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
3461       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
3462                                   getFunctionLinkage(GD));
3463       break;
3464     }
3465     case Decl::CXXDestructor: {
3466       CodeGenPGO PGO(*this);
3467       GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
3468       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
3469                                   getFunctionLinkage(GD));
3470       break;
3471     }
3472     default:
3473       break;
3474     };
3475   }
3476 }
3477 
3478 /// Turns the given pointer into a constant.
3479 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
3480                                           const void *Ptr) {
3481   uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
3482   llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
3483   return llvm::ConstantInt::get(i64, PtrInt);
3484 }
3485 
3486 static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
3487                                    llvm::NamedMDNode *&GlobalMetadata,
3488                                    GlobalDecl D,
3489                                    llvm::GlobalValue *Addr) {
3490   if (!GlobalMetadata)
3491     GlobalMetadata =
3492       CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
3493 
3494   // TODO: should we report variant information for ctors/dtors?
3495   llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
3496                            llvm::ConstantAsMetadata::get(GetPointerConstant(
3497                                CGM.getLLVMContext(), D.getDecl()))};
3498   GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
3499 }
3500 
3501 /// For each function which is declared within an extern "C" region and marked
3502 /// as 'used', but has internal linkage, create an alias from the unmangled
3503 /// name to the mangled name if possible. People expect to be able to refer
3504 /// to such functions with an unmangled name from inline assembly within the
3505 /// same translation unit.
3506 void CodeGenModule::EmitStaticExternCAliases() {
3507   for (StaticExternCMap::iterator I = StaticExternCValues.begin(),
3508                                   E = StaticExternCValues.end();
3509        I != E; ++I) {
3510     IdentifierInfo *Name = I->first;
3511     llvm::GlobalValue *Val = I->second;
3512     if (Val && !getModule().getNamedValue(Name->getName()))
3513       addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
3514   }
3515 }
3516 
3517 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
3518                                              GlobalDecl &Result) const {
3519   auto Res = Manglings.find(MangledName);
3520   if (Res == Manglings.end())
3521     return false;
3522   Result = Res->getValue();
3523   return true;
3524 }
3525 
3526 /// Emits metadata nodes associating all the global values in the
3527 /// current module with the Decls they came from.  This is useful for
3528 /// projects using IR gen as a subroutine.
3529 ///
3530 /// Since there's currently no way to associate an MDNode directly
3531 /// with an llvm::GlobalValue, we create a global named metadata
3532 /// with the name 'clang.global.decl.ptrs'.
3533 void CodeGenModule::EmitDeclMetadata() {
3534   llvm::NamedMDNode *GlobalMetadata = nullptr;
3535 
3536   // StaticLocalDeclMap
3537   for (auto &I : MangledDeclNames) {
3538     llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
3539     EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
3540   }
3541 }
3542 
3543 /// Emits metadata nodes for all the local variables in the current
3544 /// function.
3545 void CodeGenFunction::EmitDeclMetadata() {
3546   if (LocalDeclMap.empty()) return;
3547 
3548   llvm::LLVMContext &Context = getLLVMContext();
3549 
3550   // Find the unique metadata ID for this name.
3551   unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
3552 
3553   llvm::NamedMDNode *GlobalMetadata = nullptr;
3554 
3555   for (auto &I : LocalDeclMap) {
3556     const Decl *D = I.first;
3557     llvm::Value *Addr = I.second;
3558     if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
3559       llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
3560       Alloca->setMetadata(
3561           DeclPtrKind, llvm::MDNode::get(
3562                            Context, llvm::ValueAsMetadata::getConstant(DAddr)));
3563     } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
3564       GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
3565       EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
3566     }
3567   }
3568 }
3569 
3570 void CodeGenModule::EmitVersionIdentMetadata() {
3571   llvm::NamedMDNode *IdentMetadata =
3572     TheModule.getOrInsertNamedMetadata("llvm.ident");
3573   std::string Version = getClangFullVersion();
3574   llvm::LLVMContext &Ctx = TheModule.getContext();
3575 
3576   llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
3577   IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
3578 }
3579 
3580 void CodeGenModule::EmitTargetMetadata() {
3581   // Warning, new MangledDeclNames may be appended within this loop.
3582   // We rely on MapVector insertions adding new elements to the end
3583   // of the container.
3584   // FIXME: Move this loop into the one target that needs it, and only
3585   // loop over those declarations for which we couldn't emit the target
3586   // metadata when we emitted the declaration.
3587   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
3588     auto Val = *(MangledDeclNames.begin() + I);
3589     const Decl *D = Val.first.getDecl()->getMostRecentDecl();
3590     llvm::GlobalValue *GV = GetGlobalValue(Val.second);
3591     getTargetCodeGenInfo().emitTargetMD(D, GV, *this);
3592   }
3593 }
3594 
3595 void CodeGenModule::EmitCoverageFile() {
3596   if (!getCodeGenOpts().CoverageFile.empty()) {
3597     if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) {
3598       llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
3599       llvm::LLVMContext &Ctx = TheModule.getContext();
3600       llvm::MDString *CoverageFile =
3601           llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile);
3602       for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
3603         llvm::MDNode *CU = CUNode->getOperand(i);
3604         llvm::Metadata *Elts[] = {CoverageFile, CU};
3605         GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
3606       }
3607     }
3608   }
3609 }
3610 
3611 llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) {
3612   // Sema has checked that all uuid strings are of the form
3613   // "12345678-1234-1234-1234-1234567890ab".
3614   assert(Uuid.size() == 36);
3615   for (unsigned i = 0; i < 36; ++i) {
3616     if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-');
3617     else                                         assert(isHexDigit(Uuid[i]));
3618   }
3619 
3620   // The starts of all bytes of Field3 in Uuid. Field 3 is "1234-1234567890ab".
3621   const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 };
3622 
3623   llvm::Constant *Field3[8];
3624   for (unsigned Idx = 0; Idx < 8; ++Idx)
3625     Field3[Idx] = llvm::ConstantInt::get(
3626         Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16);
3627 
3628   llvm::Constant *Fields[4] = {
3629     llvm::ConstantInt::get(Int32Ty, Uuid.substr(0,  8), 16),
3630     llvm::ConstantInt::get(Int16Ty, Uuid.substr(9,  4), 16),
3631     llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16),
3632     llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3)
3633   };
3634 
3635   return llvm::ConstantStruct::getAnon(Fields);
3636 }
3637 
3638 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
3639                                                        bool ForEH) {
3640   // Return a bogus pointer if RTTI is disabled, unless it's for EH.
3641   // FIXME: should we even be calling this method if RTTI is disabled
3642   // and it's not for EH?
3643   if (!ForEH && !getLangOpts().RTTI)
3644     return llvm::Constant::getNullValue(Int8PtrTy);
3645 
3646   if (ForEH && Ty->isObjCObjectPointerType() &&
3647       LangOpts.ObjCRuntime.isGNUFamily())
3648     return ObjCRuntime->GetEHType(Ty);
3649 
3650   return getCXXABI().getAddrOfRTTIDescriptor(Ty);
3651 }
3652 
3653 void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
3654   for (auto RefExpr : D->varlists()) {
3655     auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
3656     bool PerformInit =
3657         VD->getAnyInitializer() &&
3658         !VD->getAnyInitializer()->isConstantInitializer(getContext(),
3659                                                         /*ForRef=*/false);
3660     if (auto InitFunction =
3661             getOpenMPRuntime().EmitOMPThreadPrivateVarDefinition(
3662                 VD, GetAddrOfGlobalVar(VD), RefExpr->getLocStart(),
3663                 PerformInit))
3664       CXXGlobalInits.push_back(InitFunction);
3665   }
3666 }
3667 
3668