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