1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This coordinates the per-module state used while generating code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeGenModule.h"
14 #include "CGBlocks.h"
15 #include "CGCUDARuntime.h"
16 #include "CGCXXABI.h"
17 #include "CGCall.h"
18 #include "CGDebugInfo.h"
19 #include "CGObjCRuntime.h"
20 #include "CGOpenCLRuntime.h"
21 #include "CGOpenMPRuntime.h"
22 #include "CGOpenMPRuntimeNVPTX.h"
23 #include "CodeGenFunction.h"
24 #include "CodeGenPGO.h"
25 #include "ConstantEmitter.h"
26 #include "CoverageMappingGen.h"
27 #include "TargetInfo.h"
28 #include "clang/AST/ASTContext.h"
29 #include "clang/AST/CharUnits.h"
30 #include "clang/AST/DeclCXX.h"
31 #include "clang/AST/DeclObjC.h"
32 #include "clang/AST/DeclTemplate.h"
33 #include "clang/AST/Mangle.h"
34 #include "clang/AST/RecordLayout.h"
35 #include "clang/AST/RecursiveASTVisitor.h"
36 #include "clang/AST/StmtVisitor.h"
37 #include "clang/Basic/Builtins.h"
38 #include "clang/Basic/CharInfo.h"
39 #include "clang/Basic/CodeGenOptions.h"
40 #include "clang/Basic/Diagnostic.h"
41 #include "clang/Basic/FileManager.h"
42 #include "clang/Basic/Module.h"
43 #include "clang/Basic/SourceManager.h"
44 #include "clang/Basic/TargetInfo.h"
45 #include "clang/Basic/Version.h"
46 #include "clang/CodeGen/ConstantInitBuilder.h"
47 #include "clang/Frontend/FrontendDiagnostic.h"
48 #include "llvm/ADT/StringSwitch.h"
49 #include "llvm/ADT/Triple.h"
50 #include "llvm/Analysis/TargetLibraryInfo.h"
51 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
52 #include "llvm/IR/CallingConv.h"
53 #include "llvm/IR/DataLayout.h"
54 #include "llvm/IR/Intrinsics.h"
55 #include "llvm/IR/LLVMContext.h"
56 #include "llvm/IR/Module.h"
57 #include "llvm/IR/ProfileSummary.h"
58 #include "llvm/ProfileData/InstrProfReader.h"
59 #include "llvm/Support/CodeGen.h"
60 #include "llvm/Support/CommandLine.h"
61 #include "llvm/Support/ConvertUTF.h"
62 #include "llvm/Support/ErrorHandling.h"
63 #include "llvm/Support/MD5.h"
64 #include "llvm/Support/TimeProfiler.h"
65 
66 using namespace clang;
67 using namespace CodeGen;
68 
69 static llvm::cl::opt<bool> LimitedCoverage(
70     "limited-coverage-experimental", llvm::cl::ZeroOrMore, llvm::cl::Hidden,
71     llvm::cl::desc("Emit limited coverage mapping information (experimental)"),
72     llvm::cl::init(false));
73 
74 static const char AnnotationSection[] = "llvm.metadata";
75 
76 static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
77   switch (CGM.getTarget().getCXXABI().getKind()) {
78   case TargetCXXABI::Fuchsia:
79   case TargetCXXABI::GenericAArch64:
80   case TargetCXXABI::GenericARM:
81   case TargetCXXABI::iOS:
82   case TargetCXXABI::iOS64:
83   case TargetCXXABI::WatchOS:
84   case TargetCXXABI::GenericMIPS:
85   case TargetCXXABI::GenericItanium:
86   case TargetCXXABI::WebAssembly:
87   case TargetCXXABI::XL:
88     return CreateItaniumCXXABI(CGM);
89   case TargetCXXABI::Microsoft:
90     return CreateMicrosoftCXXABI(CGM);
91   }
92 
93   llvm_unreachable("invalid C++ ABI kind");
94 }
95 
96 CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
97                              const PreprocessorOptions &PPO,
98                              const CodeGenOptions &CGO, llvm::Module &M,
99                              DiagnosticsEngine &diags,
100                              CoverageSourceInfo *CoverageInfo)
101     : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO),
102       PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
103       Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
104       VMContext(M.getContext()), Types(*this), VTables(*this),
105       SanitizerMD(new SanitizerMetadata(*this)) {
106 
107   // Initialize the type cache.
108   llvm::LLVMContext &LLVMContext = M.getContext();
109   VoidTy = llvm::Type::getVoidTy(LLVMContext);
110   Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
111   Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
112   Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
113   Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
114   HalfTy = llvm::Type::getHalfTy(LLVMContext);
115   BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
116   FloatTy = llvm::Type::getFloatTy(LLVMContext);
117   DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
118   PointerWidthInBits = C.getTargetInfo().getPointerWidth(0);
119   PointerAlignInBytes =
120     C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
121   SizeSizeInBytes =
122     C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
123   IntAlignInBytes =
124     C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
125   IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
126   IntPtrTy = llvm::IntegerType::get(LLVMContext,
127     C.getTargetInfo().getMaxPointerWidth());
128   Int8PtrTy = Int8Ty->getPointerTo(0);
129   Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
130   AllocaInt8PtrTy = Int8Ty->getPointerTo(
131       M.getDataLayout().getAllocaAddrSpace());
132   ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();
133 
134   RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
135 
136   if (LangOpts.ObjC)
137     createObjCRuntime();
138   if (LangOpts.OpenCL)
139     createOpenCLRuntime();
140   if (LangOpts.OpenMP)
141     createOpenMPRuntime();
142   if (LangOpts.CUDA)
143     createCUDARuntime();
144 
145   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
146   if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
147       (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
148     TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
149                                getCXXABI().getMangleContext()));
150 
151   // If debug info or coverage generation is enabled, create the CGDebugInfo
152   // object.
153   if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo ||
154       CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)
155     DebugInfo.reset(new CGDebugInfo(*this));
156 
157   Block.GlobalUniqueCount = 0;
158 
159   if (C.getLangOpts().ObjC)
160     ObjCData.reset(new ObjCEntrypoints());
161 
162   if (CodeGenOpts.hasProfileClangUse()) {
163     auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
164         CodeGenOpts.ProfileInstrumentUsePath, CodeGenOpts.ProfileRemappingFile);
165     if (auto E = ReaderOrErr.takeError()) {
166       unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
167                                               "Could not read profile %0: %1");
168       llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) {
169         getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath
170                                   << EI.message();
171       });
172     } else
173       PGOReader = std::move(ReaderOrErr.get());
174   }
175 
176   // If coverage mapping generation is enabled, create the
177   // CoverageMappingModuleGen object.
178   if (CodeGenOpts.CoverageMapping)
179     CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
180 }
181 
182 CodeGenModule::~CodeGenModule() {}
183 
184 void CodeGenModule::createObjCRuntime() {
185   // This is just isGNUFamily(), but we want to force implementors of
186   // new ABIs to decide how best to do this.
187   switch (LangOpts.ObjCRuntime.getKind()) {
188   case ObjCRuntime::GNUstep:
189   case ObjCRuntime::GCC:
190   case ObjCRuntime::ObjFW:
191     ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
192     return;
193 
194   case ObjCRuntime::FragileMacOSX:
195   case ObjCRuntime::MacOSX:
196   case ObjCRuntime::iOS:
197   case ObjCRuntime::WatchOS:
198     ObjCRuntime.reset(CreateMacObjCRuntime(*this));
199     return;
200   }
201   llvm_unreachable("bad runtime kind");
202 }
203 
204 void CodeGenModule::createOpenCLRuntime() {
205   OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
206 }
207 
208 void CodeGenModule::createOpenMPRuntime() {
209   // Select a specialized code generation class based on the target, if any.
210   // If it does not exist use the default implementation.
211   switch (getTriple().getArch()) {
212   case llvm::Triple::nvptx:
213   case llvm::Triple::nvptx64:
214     assert(getLangOpts().OpenMPIsDevice &&
215            "OpenMP NVPTX is only prepared to deal with device code.");
216     OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this));
217     break;
218   default:
219     if (LangOpts.OpenMPSimd)
220       OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this));
221     else
222       OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
223     break;
224   }
225 }
226 
227 void CodeGenModule::createCUDARuntime() {
228   CUDARuntime.reset(CreateNVCUDARuntime(*this));
229 }
230 
231 void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
232   Replacements[Name] = C;
233 }
234 
235 void CodeGenModule::applyReplacements() {
236   for (auto &I : Replacements) {
237     StringRef MangledName = I.first();
238     llvm::Constant *Replacement = I.second;
239     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
240     if (!Entry)
241       continue;
242     auto *OldF = cast<llvm::Function>(Entry);
243     auto *NewF = dyn_cast<llvm::Function>(Replacement);
244     if (!NewF) {
245       if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
246         NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
247       } else {
248         auto *CE = cast<llvm::ConstantExpr>(Replacement);
249         assert(CE->getOpcode() == llvm::Instruction::BitCast ||
250                CE->getOpcode() == llvm::Instruction::GetElementPtr);
251         NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
252       }
253     }
254 
255     // Replace old with new, but keep the old order.
256     OldF->replaceAllUsesWith(Replacement);
257     if (NewF) {
258       NewF->removeFromParent();
259       OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
260                                                        NewF);
261     }
262     OldF->eraseFromParent();
263   }
264 }
265 
266 void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
267   GlobalValReplacements.push_back(std::make_pair(GV, C));
268 }
269 
270 void CodeGenModule::applyGlobalValReplacements() {
271   for (auto &I : GlobalValReplacements) {
272     llvm::GlobalValue *GV = I.first;
273     llvm::Constant *C = I.second;
274 
275     GV->replaceAllUsesWith(C);
276     GV->eraseFromParent();
277   }
278 }
279 
280 // This is only used in aliases that we created and we know they have a
281 // linear structure.
282 static const llvm::GlobalObject *getAliasedGlobal(
283     const llvm::GlobalIndirectSymbol &GIS) {
284   llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited;
285   const llvm::Constant *C = &GIS;
286   for (;;) {
287     C = C->stripPointerCasts();
288     if (auto *GO = dyn_cast<llvm::GlobalObject>(C))
289       return GO;
290     // stripPointerCasts will not walk over weak aliases.
291     auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C);
292     if (!GIS2)
293       return nullptr;
294     if (!Visited.insert(GIS2).second)
295       return nullptr;
296     C = GIS2->getIndirectSymbol();
297   }
298 }
299 
300 void CodeGenModule::checkAliases() {
301   // Check if the constructed aliases are well formed. It is really unfortunate
302   // that we have to do this in CodeGen, but we only construct mangled names
303   // and aliases during codegen.
304   bool Error = false;
305   DiagnosticsEngine &Diags = getDiags();
306   for (const GlobalDecl &GD : Aliases) {
307     const auto *D = cast<ValueDecl>(GD.getDecl());
308     SourceLocation Location;
309     bool IsIFunc = D->hasAttr<IFuncAttr>();
310     if (const Attr *A = D->getDefiningAttr())
311       Location = A->getLocation();
312     else
313       llvm_unreachable("Not an alias or ifunc?");
314     StringRef MangledName = getMangledName(GD);
315     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
316     auto *Alias  = cast<llvm::GlobalIndirectSymbol>(Entry);
317     const llvm::GlobalValue *GV = getAliasedGlobal(*Alias);
318     if (!GV) {
319       Error = true;
320       Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
321     } else if (GV->isDeclaration()) {
322       Error = true;
323       Diags.Report(Location, diag::err_alias_to_undefined)
324           << IsIFunc << IsIFunc;
325     } else if (IsIFunc) {
326       // Check resolver function type.
327       llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>(
328           GV->getType()->getPointerElementType());
329       assert(FTy);
330       if (!FTy->getReturnType()->isPointerTy())
331         Diags.Report(Location, diag::err_ifunc_resolver_return);
332     }
333 
334     llvm::Constant *Aliasee = Alias->getIndirectSymbol();
335     llvm::GlobalValue *AliaseeGV;
336     if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
337       AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
338     else
339       AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
340 
341     if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
342       StringRef AliasSection = SA->getName();
343       if (AliasSection != AliaseeGV->getSection())
344         Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
345             << AliasSection << IsIFunc << IsIFunc;
346     }
347 
348     // We have to handle alias to weak aliases in here. LLVM itself disallows
349     // this since the object semantics would not match the IL one. For
350     // compatibility with gcc we implement it by just pointing the alias
351     // to its aliasee's aliasee. We also warn, since the user is probably
352     // expecting the link to be weak.
353     if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) {
354       if (GA->isInterposable()) {
355         Diags.Report(Location, diag::warn_alias_to_weak_alias)
356             << GV->getName() << GA->getName() << IsIFunc;
357         Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
358             GA->getIndirectSymbol(), Alias->getType());
359         Alias->setIndirectSymbol(Aliasee);
360       }
361     }
362   }
363   if (!Error)
364     return;
365 
366   for (const GlobalDecl &GD : Aliases) {
367     StringRef MangledName = getMangledName(GD);
368     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
369     auto *Alias = dyn_cast<llvm::GlobalIndirectSymbol>(Entry);
370     Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
371     Alias->eraseFromParent();
372   }
373 }
374 
375 void CodeGenModule::clear() {
376   DeferredDeclsToEmit.clear();
377   if (OpenMPRuntime)
378     OpenMPRuntime->clear();
379 }
380 
381 void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags,
382                                        StringRef MainFile) {
383   if (!hasDiagnostics())
384     return;
385   if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
386     if (MainFile.empty())
387       MainFile = "<stdin>";
388     Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
389   } else {
390     if (Mismatched > 0)
391       Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
392 
393     if (Missing > 0)
394       Diags.Report(diag::warn_profile_data_missing) << Visited << Missing;
395   }
396 }
397 
398 void CodeGenModule::Release() {
399   EmitDeferred();
400   EmitVTablesOpportunistically();
401   applyGlobalValReplacements();
402   applyReplacements();
403   checkAliases();
404   emitMultiVersionFunctions();
405   EmitCXXGlobalInitFunc();
406   EmitCXXGlobalCleanUpFunc();
407   registerGlobalDtorsWithAtExit();
408   EmitCXXThreadLocalInitFunc();
409   if (ObjCRuntime)
410     if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
411       AddGlobalCtor(ObjCInitFunction);
412   if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice &&
413       CUDARuntime) {
414     if (llvm::Function *CudaCtorFunction =
415             CUDARuntime->makeModuleCtorFunction())
416       AddGlobalCtor(CudaCtorFunction);
417   }
418   if (OpenMPRuntime) {
419     if (llvm::Function *OpenMPRequiresDirectiveRegFun =
420             OpenMPRuntime->emitRequiresDirectiveRegFun()) {
421       AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0);
422     }
423     OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
424     OpenMPRuntime->clear();
425   }
426   if (PGOReader) {
427     getModule().setProfileSummary(
428         PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
429         llvm::ProfileSummary::PSK_Instr);
430     if (PGOStats.hasDiagnostics())
431       PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
432   }
433   EmitCtorList(GlobalCtors, "llvm.global_ctors");
434   EmitCtorList(GlobalDtors, "llvm.global_dtors");
435   EmitGlobalAnnotations();
436   EmitStaticExternCAliases();
437   EmitDeferredUnusedCoverageMappings();
438   if (CoverageMapping)
439     CoverageMapping->emit();
440   if (CodeGenOpts.SanitizeCfiCrossDso) {
441     CodeGenFunction(*this).EmitCfiCheckFail();
442     CodeGenFunction(*this).EmitCfiCheckStub();
443   }
444   emitAtAvailableLinkGuard();
445   if (Context.getTargetInfo().getTriple().isWasm() &&
446       !Context.getTargetInfo().getTriple().isOSEmscripten()) {
447     EmitMainVoidAlias();
448   }
449   emitLLVMUsed();
450   if (SanStats)
451     SanStats->finish();
452 
453   if (CodeGenOpts.Autolink &&
454       (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
455     EmitModuleLinkOptions();
456   }
457 
458   // On ELF we pass the dependent library specifiers directly to the linker
459   // without manipulating them. This is in contrast to other platforms where
460   // they are mapped to a specific linker option by the compiler. This
461   // difference is a result of the greater variety of ELF linkers and the fact
462   // that ELF linkers tend to handle libraries in a more complicated fashion
463   // than on other platforms. This forces us to defer handling the dependent
464   // libs to the linker.
465   //
466   // CUDA/HIP device and host libraries are different. Currently there is no
467   // way to differentiate dependent libraries for host or device. Existing
468   // usage of #pragma comment(lib, *) is intended for host libraries on
469   // Windows. Therefore emit llvm.dependent-libraries only for host.
470   if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) {
471     auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
472     for (auto *MD : ELFDependentLibraries)
473       NMD->addOperand(MD);
474   }
475 
476   // Record mregparm value now so it is visible through rest of codegen.
477   if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
478     getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
479                               CodeGenOpts.NumRegisterParameters);
480 
481   if (CodeGenOpts.DwarfVersion) {
482     getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version",
483                               CodeGenOpts.DwarfVersion);
484   }
485 
486   if (Context.getLangOpts().SemanticInterposition)
487     // Require various optimization to respect semantic interposition.
488     getModule().setSemanticInterposition(1);
489   else if (Context.getLangOpts().ExplicitNoSemanticInterposition)
490     // Allow dso_local on applicable targets.
491     getModule().setSemanticInterposition(0);
492 
493   if (CodeGenOpts.EmitCodeView) {
494     // Indicate that we want CodeView in the metadata.
495     getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
496   }
497   if (CodeGenOpts.CodeViewGHash) {
498     getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1);
499   }
500   if (CodeGenOpts.ControlFlowGuard) {
501     // Function ID tables and checks for Control Flow Guard (cfguard=2).
502     getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 2);
503   } else if (CodeGenOpts.ControlFlowGuardNoChecks) {
504     // Function ID tables for Control Flow Guard (cfguard=1).
505     getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1);
506   }
507   if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
508     // We don't support LTO with 2 with different StrictVTablePointers
509     // FIXME: we could support it by stripping all the information introduced
510     // by StrictVTablePointers.
511 
512     getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
513 
514     llvm::Metadata *Ops[2] = {
515               llvm::MDString::get(VMContext, "StrictVTablePointers"),
516               llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
517                   llvm::Type::getInt32Ty(VMContext), 1))};
518 
519     getModule().addModuleFlag(llvm::Module::Require,
520                               "StrictVTablePointersRequirement",
521                               llvm::MDNode::get(VMContext, Ops));
522   }
523   if (getModuleDebugInfo())
524     // We support a single version in the linked module. The LLVM
525     // parser will drop debug info with a different version number
526     // (and warn about it, too).
527     getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
528                               llvm::DEBUG_METADATA_VERSION);
529 
530   // We need to record the widths of enums and wchar_t, so that we can generate
531   // the correct build attributes in the ARM backend. wchar_size is also used by
532   // TargetLibraryInfo.
533   uint64_t WCharWidth =
534       Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
535   getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
536 
537   llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
538   if (   Arch == llvm::Triple::arm
539       || Arch == llvm::Triple::armeb
540       || Arch == llvm::Triple::thumb
541       || Arch == llvm::Triple::thumbeb) {
542     // The minimum width of an enum in bytes
543     uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
544     getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
545   }
546 
547   if (Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64) {
548     StringRef ABIStr = Target.getABI();
549     llvm::LLVMContext &Ctx = TheModule.getContext();
550     getModule().addModuleFlag(llvm::Module::Error, "target-abi",
551                               llvm::MDString::get(Ctx, ABIStr));
552   }
553 
554   if (CodeGenOpts.SanitizeCfiCrossDso) {
555     // Indicate that we want cross-DSO control flow integrity checks.
556     getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
557   }
558 
559   if (CodeGenOpts.WholeProgramVTables) {
560     // Indicate whether VFE was enabled for this module, so that the
561     // vcall_visibility metadata added under whole program vtables is handled
562     // appropriately in the optimizer.
563     getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
564                               CodeGenOpts.VirtualFunctionElimination);
565   }
566 
567   if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
568     getModule().addModuleFlag(llvm::Module::Override,
569                               "CFI Canonical Jump Tables",
570                               CodeGenOpts.SanitizeCfiCanonicalJumpTables);
571   }
572 
573   if (CodeGenOpts.CFProtectionReturn &&
574       Target.checkCFProtectionReturnSupported(getDiags())) {
575     // Indicate that we want to instrument return control flow protection.
576     getModule().addModuleFlag(llvm::Module::Override, "cf-protection-return",
577                               1);
578   }
579 
580   if (CodeGenOpts.CFProtectionBranch &&
581       Target.checkCFProtectionBranchSupported(getDiags())) {
582     // Indicate that we want to instrument branch control flow protection.
583     getModule().addModuleFlag(llvm::Module::Override, "cf-protection-branch",
584                               1);
585   }
586 
587   if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
588     // Indicate whether __nvvm_reflect should be configured to flush denormal
589     // floating point values to 0.  (This corresponds to its "__CUDA_FTZ"
590     // property.)
591     getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
592                               CodeGenOpts.FP32DenormalMode.Output !=
593                                   llvm::DenormalMode::IEEE);
594   }
595 
596   // Emit OpenCL specific module metadata: OpenCL/SPIR version.
597   if (LangOpts.OpenCL) {
598     EmitOpenCLMetadata();
599     // Emit SPIR version.
600     if (getTriple().isSPIR()) {
601       // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
602       // opencl.spir.version named metadata.
603       // C++ is backwards compatible with OpenCL v2.0.
604       auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
605       llvm::Metadata *SPIRVerElts[] = {
606           llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
607               Int32Ty, Version / 100)),
608           llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
609               Int32Ty, (Version / 100 > 1) ? 0 : 2))};
610       llvm::NamedMDNode *SPIRVerMD =
611           TheModule.getOrInsertNamedMetadata("opencl.spir.version");
612       llvm::LLVMContext &Ctx = TheModule.getContext();
613       SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
614     }
615   }
616 
617   if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
618     assert(PLevel < 3 && "Invalid PIC Level");
619     getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
620     if (Context.getLangOpts().PIE)
621       getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
622   }
623 
624   if (getCodeGenOpts().CodeModel.size() > 0) {
625     unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel)
626                   .Case("tiny", llvm::CodeModel::Tiny)
627                   .Case("small", llvm::CodeModel::Small)
628                   .Case("kernel", llvm::CodeModel::Kernel)
629                   .Case("medium", llvm::CodeModel::Medium)
630                   .Case("large", llvm::CodeModel::Large)
631                   .Default(~0u);
632     if (CM != ~0u) {
633       llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
634       getModule().setCodeModel(codeModel);
635     }
636   }
637 
638   if (CodeGenOpts.NoPLT)
639     getModule().setRtLibUseGOT();
640 
641   SimplifyPersonality();
642 
643   if (getCodeGenOpts().EmitDeclMetadata)
644     EmitDeclMetadata();
645 
646   if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
647     EmitCoverageFile();
648 
649   if (CGDebugInfo *DI = getModuleDebugInfo())
650     DI->finalize();
651 
652   if (getCodeGenOpts().EmitVersionIdentMetadata)
653     EmitVersionIdentMetadata();
654 
655   if (!getCodeGenOpts().RecordCommandLine.empty())
656     EmitCommandLineMetadata();
657 
658   getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames);
659 
660   EmitBackendOptionsMetadata(getCodeGenOpts());
661 }
662 
663 void CodeGenModule::EmitOpenCLMetadata() {
664   // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
665   // opencl.ocl.version named metadata node.
666   // C++ is backwards compatible with OpenCL v2.0.
667   // FIXME: We might need to add CXX version at some point too?
668   auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
669   llvm::Metadata *OCLVerElts[] = {
670       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
671           Int32Ty, Version / 100)),
672       llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
673           Int32Ty, (Version % 100) / 10))};
674   llvm::NamedMDNode *OCLVerMD =
675       TheModule.getOrInsertNamedMetadata("opencl.ocl.version");
676   llvm::LLVMContext &Ctx = TheModule.getContext();
677   OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
678 }
679 
680 void CodeGenModule::EmitBackendOptionsMetadata(
681     const CodeGenOptions CodeGenOpts) {
682   switch (getTriple().getArch()) {
683   default:
684     break;
685   case llvm::Triple::riscv32:
686   case llvm::Triple::riscv64:
687     getModule().addModuleFlag(llvm::Module::Error, "SmallDataLimit",
688                               CodeGenOpts.SmallDataLimit);
689     break;
690   }
691 }
692 
693 void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
694   // Make sure that this type is translated.
695   Types.UpdateCompletedType(TD);
696 }
697 
698 void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
699   // Make sure that this type is translated.
700   Types.RefreshTypeCacheForClass(RD);
701 }
702 
703 llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
704   if (!TBAA)
705     return nullptr;
706   return TBAA->getTypeInfo(QTy);
707 }
708 
709 TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) {
710   if (!TBAA)
711     return TBAAAccessInfo();
712   if (getLangOpts().CUDAIsDevice) {
713     // As CUDA builtin surface/texture types are replaced, skip generating TBAA
714     // access info.
715     if (AccessType->isCUDADeviceBuiltinSurfaceType()) {
716       if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() !=
717           nullptr)
718         return TBAAAccessInfo();
719     } else if (AccessType->isCUDADeviceBuiltinTextureType()) {
720       if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() !=
721           nullptr)
722         return TBAAAccessInfo();
723     }
724   }
725   return TBAA->getAccessInfo(AccessType);
726 }
727 
728 TBAAAccessInfo
729 CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
730   if (!TBAA)
731     return TBAAAccessInfo();
732   return TBAA->getVTablePtrAccessInfo(VTablePtrType);
733 }
734 
735 llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
736   if (!TBAA)
737     return nullptr;
738   return TBAA->getTBAAStructInfo(QTy);
739 }
740 
741 llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) {
742   if (!TBAA)
743     return nullptr;
744   return TBAA->getBaseTypeInfo(QTy);
745 }
746 
747 llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) {
748   if (!TBAA)
749     return nullptr;
750   return TBAA->getAccessTagInfo(Info);
751 }
752 
753 TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
754                                                    TBAAAccessInfo TargetInfo) {
755   if (!TBAA)
756     return TBAAAccessInfo();
757   return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
758 }
759 
760 TBAAAccessInfo
761 CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
762                                                    TBAAAccessInfo InfoB) {
763   if (!TBAA)
764     return TBAAAccessInfo();
765   return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
766 }
767 
768 TBAAAccessInfo
769 CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
770                                               TBAAAccessInfo SrcInfo) {
771   if (!TBAA)
772     return TBAAAccessInfo();
773   return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
774 }
775 
776 void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
777                                                 TBAAAccessInfo TBAAInfo) {
778   if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
779     Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
780 }
781 
782 void CodeGenModule::DecorateInstructionWithInvariantGroup(
783     llvm::Instruction *I, const CXXRecordDecl *RD) {
784   I->setMetadata(llvm::LLVMContext::MD_invariant_group,
785                  llvm::MDNode::get(getLLVMContext(), {}));
786 }
787 
788 void CodeGenModule::Error(SourceLocation loc, StringRef message) {
789   unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
790   getDiags().Report(Context.getFullLoc(loc), diagID) << message;
791 }
792 
793 /// ErrorUnsupported - Print out an error that codegen doesn't support the
794 /// specified stmt yet.
795 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
796   unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
797                                                "cannot compile this %0 yet");
798   std::string Msg = Type;
799   getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
800       << Msg << S->getSourceRange();
801 }
802 
803 /// ErrorUnsupported - Print out an error that codegen doesn't support the
804 /// specified decl yet.
805 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
806   unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
807                                                "cannot compile this %0 yet");
808   std::string Msg = Type;
809   getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
810 }
811 
812 llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
813   return llvm::ConstantInt::get(SizeTy, size.getQuantity());
814 }
815 
816 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
817                                         const NamedDecl *D) const {
818   if (GV->hasDLLImportStorageClass())
819     return;
820   // Internal definitions always have default visibility.
821   if (GV->hasLocalLinkage()) {
822     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
823     return;
824   }
825   if (!D)
826     return;
827   // Set visibility for definitions, and for declarations if requested globally
828   // or set explicitly.
829   LinkageInfo LV = D->getLinkageAndVisibility();
830   if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
831       !GV->isDeclarationForLinker())
832     GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
833 }
834 
835 static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
836                                  llvm::GlobalValue *GV) {
837   if (GV->hasLocalLinkage())
838     return true;
839 
840   if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
841     return true;
842 
843   // DLLImport explicitly marks the GV as external.
844   if (GV->hasDLLImportStorageClass())
845     return false;
846 
847   const llvm::Triple &TT = CGM.getTriple();
848   if (TT.isWindowsGNUEnvironment()) {
849     // In MinGW, variables without DLLImport can still be automatically
850     // imported from a DLL by the linker; don't mark variables that
851     // potentially could come from another DLL as DSO local.
852     if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
853         !GV->isThreadLocal())
854       return false;
855   }
856 
857   // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
858   // remain unresolved in the link, they can be resolved to zero, which is
859   // outside the current DSO.
860   if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
861     return false;
862 
863   // Every other GV is local on COFF.
864   // Make an exception for windows OS in the triple: Some firmware builds use
865   // *-win32-macho triples. This (accidentally?) produced windows relocations
866   // without GOT tables in older clang versions; Keep this behaviour.
867   // FIXME: even thread local variables?
868   if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
869     return true;
870 
871   // Only handle COFF and ELF for now.
872   if (!TT.isOSBinFormatELF())
873     return false;
874 
875   // If this is not an executable, don't assume anything is local.
876   const auto &CGOpts = CGM.getCodeGenOpts();
877   llvm::Reloc::Model RM = CGOpts.RelocationModel;
878   const auto &LOpts = CGM.getLangOpts();
879   if (RM != llvm::Reloc::Static && !LOpts.PIE)
880     return false;
881 
882   // A definition cannot be preempted from an executable.
883   if (!GV->isDeclarationForLinker())
884     return true;
885 
886   // Most PIC code sequences that assume that a symbol is local cannot produce a
887   // 0 if it turns out the symbol is undefined. While this is ABI and relocation
888   // depended, it seems worth it to handle it here.
889   if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
890     return false;
891 
892   // PPC has no copy relocations and cannot use a plt entry as a symbol address.
893   llvm::Triple::ArchType Arch = TT.getArch();
894   if (Arch == llvm::Triple::ppc || Arch == llvm::Triple::ppc64 ||
895       Arch == llvm::Triple::ppc64le)
896     return false;
897 
898   // If we can use copy relocations we can assume it is local.
899   if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
900     if (!Var->isThreadLocal() &&
901         (RM == llvm::Reloc::Static || CGOpts.PIECopyRelocations))
902       return true;
903 
904   // If we can use a plt entry as the symbol address we can assume it
905   // is local.
906   // FIXME: This should work for PIE, but the gold linker doesn't support it.
907   if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static)
908     return true;
909 
910   // Otherwise don't assume it is local.
911   return false;
912 }
913 
914 void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const {
915   GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
916 }
917 
918 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
919                                           GlobalDecl GD) const {
920   const auto *D = dyn_cast<NamedDecl>(GD.getDecl());
921   // C++ destructors have a few C++ ABI specific special cases.
922   if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
923     getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType());
924     return;
925   }
926   setDLLImportDLLExport(GV, D);
927 }
928 
929 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
930                                           const NamedDecl *D) const {
931   if (D && D->isExternallyVisible()) {
932     if (D->hasAttr<DLLImportAttr>())
933       GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
934     else if (D->hasAttr<DLLExportAttr>() && !GV->isDeclarationForLinker())
935       GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
936   }
937 }
938 
939 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
940                                     GlobalDecl GD) const {
941   setDLLImportDLLExport(GV, GD);
942   setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl()));
943 }
944 
945 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
946                                     const NamedDecl *D) const {
947   setDLLImportDLLExport(GV, D);
948   setGVPropertiesAux(GV, D);
949 }
950 
951 void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV,
952                                        const NamedDecl *D) const {
953   setGlobalVisibility(GV, D);
954   setDSOLocal(GV);
955   GV->setPartition(CodeGenOpts.SymbolPartition);
956 }
957 
958 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
959   return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
960       .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
961       .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
962       .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
963       .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
964 }
965 
966 llvm::GlobalVariable::ThreadLocalMode
967 CodeGenModule::GetDefaultLLVMTLSModel() const {
968   switch (CodeGenOpts.getDefaultTLSModel()) {
969   case CodeGenOptions::GeneralDynamicTLSModel:
970     return llvm::GlobalVariable::GeneralDynamicTLSModel;
971   case CodeGenOptions::LocalDynamicTLSModel:
972     return llvm::GlobalVariable::LocalDynamicTLSModel;
973   case CodeGenOptions::InitialExecTLSModel:
974     return llvm::GlobalVariable::InitialExecTLSModel;
975   case CodeGenOptions::LocalExecTLSModel:
976     return llvm::GlobalVariable::LocalExecTLSModel;
977   }
978   llvm_unreachable("Invalid TLS model!");
979 }
980 
981 void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
982   assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
983 
984   llvm::GlobalValue::ThreadLocalMode TLM;
985   TLM = GetDefaultLLVMTLSModel();
986 
987   // Override the TLS model if it is explicitly specified.
988   if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
989     TLM = GetLLVMTLSModel(Attr->getModel());
990   }
991 
992   GV->setThreadLocalMode(TLM);
993 }
994 
995 static std::string getCPUSpecificMangling(const CodeGenModule &CGM,
996                                           StringRef Name) {
997   const TargetInfo &Target = CGM.getTarget();
998   return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str();
999 }
1000 
1001 static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM,
1002                                                  const CPUSpecificAttr *Attr,
1003                                                  unsigned CPUIndex,
1004                                                  raw_ostream &Out) {
1005   // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
1006   // supported.
1007   if (Attr)
1008     Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
1009   else if (CGM.getTarget().supportsIFunc())
1010     Out << ".resolver";
1011 }
1012 
1013 static void AppendTargetMangling(const CodeGenModule &CGM,
1014                                  const TargetAttr *Attr, raw_ostream &Out) {
1015   if (Attr->isDefaultVersion())
1016     return;
1017 
1018   Out << '.';
1019   const TargetInfo &Target = CGM.getTarget();
1020   ParsedTargetAttr Info =
1021       Attr->parse([&Target](StringRef LHS, StringRef RHS) {
1022         // Multiversioning doesn't allow "no-${feature}", so we can
1023         // only have "+" prefixes here.
1024         assert(LHS.startswith("+") && RHS.startswith("+") &&
1025                "Features should always have a prefix.");
1026         return Target.multiVersionSortPriority(LHS.substr(1)) >
1027                Target.multiVersionSortPriority(RHS.substr(1));
1028       });
1029 
1030   bool IsFirst = true;
1031 
1032   if (!Info.Architecture.empty()) {
1033     IsFirst = false;
1034     Out << "arch_" << Info.Architecture;
1035   }
1036 
1037   for (StringRef Feat : Info.Features) {
1038     if (!IsFirst)
1039       Out << '_';
1040     IsFirst = false;
1041     Out << Feat.substr(1);
1042   }
1043 }
1044 
1045 static std::string getMangledNameImpl(const CodeGenModule &CGM, GlobalDecl GD,
1046                                       const NamedDecl *ND,
1047                                       bool OmitMultiVersionMangling = false) {
1048   SmallString<256> Buffer;
1049   llvm::raw_svector_ostream Out(Buffer);
1050   MangleContext &MC = CGM.getCXXABI().getMangleContext();
1051   if (MC.shouldMangleDeclName(ND))
1052     MC.mangleName(GD.getWithDecl(ND), Out);
1053   else {
1054     IdentifierInfo *II = ND->getIdentifier();
1055     assert(II && "Attempt to mangle unnamed decl.");
1056     const auto *FD = dyn_cast<FunctionDecl>(ND);
1057 
1058     if (FD &&
1059         FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
1060       Out << "__regcall3__" << II->getName();
1061     } else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
1062                GD.getKernelReferenceKind() == KernelReferenceKind::Stub) {
1063       Out << "__device_stub__" << II->getName();
1064     } else {
1065       Out << II->getName();
1066     }
1067   }
1068 
1069   if (const auto *FD = dyn_cast<FunctionDecl>(ND))
1070     if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
1071       switch (FD->getMultiVersionKind()) {
1072       case MultiVersionKind::CPUDispatch:
1073       case MultiVersionKind::CPUSpecific:
1074         AppendCPUSpecificCPUDispatchMangling(CGM,
1075                                              FD->getAttr<CPUSpecificAttr>(),
1076                                              GD.getMultiVersionIndex(), Out);
1077         break;
1078       case MultiVersionKind::Target:
1079         AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out);
1080         break;
1081       case MultiVersionKind::None:
1082         llvm_unreachable("None multiversion type isn't valid here");
1083       }
1084     }
1085 
1086   return std::string(Out.str());
1087 }
1088 
1089 void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD,
1090                                             const FunctionDecl *FD) {
1091   if (!FD->isMultiVersion())
1092     return;
1093 
1094   // Get the name of what this would be without the 'target' attribute.  This
1095   // allows us to lookup the version that was emitted when this wasn't a
1096   // multiversion function.
1097   std::string NonTargetName =
1098       getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
1099   GlobalDecl OtherGD;
1100   if (lookupRepresentativeDecl(NonTargetName, OtherGD)) {
1101     assert(OtherGD.getCanonicalDecl()
1102                .getDecl()
1103                ->getAsFunction()
1104                ->isMultiVersion() &&
1105            "Other GD should now be a multiversioned function");
1106     // OtherFD is the version of this function that was mangled BEFORE
1107     // becoming a MultiVersion function.  It potentially needs to be updated.
1108     const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl()
1109                                       .getDecl()
1110                                       ->getAsFunction()
1111                                       ->getMostRecentDecl();
1112     std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD);
1113     // This is so that if the initial version was already the 'default'
1114     // version, we don't try to update it.
1115     if (OtherName != NonTargetName) {
1116       // Remove instead of erase, since others may have stored the StringRef
1117       // to this.
1118       const auto ExistingRecord = Manglings.find(NonTargetName);
1119       if (ExistingRecord != std::end(Manglings))
1120         Manglings.remove(&(*ExistingRecord));
1121       auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
1122       MangledDeclNames[OtherGD.getCanonicalDecl()] = Result.first->first();
1123       if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName))
1124         Entry->setName(OtherName);
1125     }
1126   }
1127 }
1128 
1129 StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
1130   GlobalDecl CanonicalGD = GD.getCanonicalDecl();
1131 
1132   // Some ABIs don't have constructor variants.  Make sure that base and
1133   // complete constructors get mangled the same.
1134   if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
1135     if (!getTarget().getCXXABI().hasConstructorVariants()) {
1136       CXXCtorType OrigCtorType = GD.getCtorType();
1137       assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete);
1138       if (OrigCtorType == Ctor_Base)
1139         CanonicalGD = GlobalDecl(CD, Ctor_Complete);
1140     }
1141   }
1142 
1143   auto FoundName = MangledDeclNames.find(CanonicalGD);
1144   if (FoundName != MangledDeclNames.end())
1145     return FoundName->second;
1146 
1147   // Keep the first result in the case of a mangling collision.
1148   const auto *ND = cast<NamedDecl>(GD.getDecl());
1149   std::string MangledName = getMangledNameImpl(*this, GD, ND);
1150 
1151   // Ensure either we have different ABIs between host and device compilations,
1152   // says host compilation following MSVC ABI but device compilation follows
1153   // Itanium C++ ABI or, if they follow the same ABI, kernel names after
1154   // mangling should be the same after name stubbing. The later checking is
1155   // very important as the device kernel name being mangled in host-compilation
1156   // is used to resolve the device binaries to be executed. Inconsistent naming
1157   // result in undefined behavior. Even though we cannot check that naming
1158   // directly between host- and device-compilations, the host- and
1159   // device-mangling in host compilation could help catching certain ones.
1160   assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||
1161          getLangOpts().CUDAIsDevice ||
1162          (getContext().getAuxTargetInfo() &&
1163           (getContext().getAuxTargetInfo()->getCXXABI() !=
1164            getContext().getTargetInfo().getCXXABI())) ||
1165          getCUDARuntime().getDeviceSideName(ND) ==
1166              getMangledNameImpl(
1167                  *this,
1168                  GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel),
1169                  ND));
1170 
1171   auto Result = Manglings.insert(std::make_pair(MangledName, GD));
1172   return MangledDeclNames[CanonicalGD] = Result.first->first();
1173 }
1174 
1175 StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD,
1176                                              const BlockDecl *BD) {
1177   MangleContext &MangleCtx = getCXXABI().getMangleContext();
1178   const Decl *D = GD.getDecl();
1179 
1180   SmallString<256> Buffer;
1181   llvm::raw_svector_ostream Out(Buffer);
1182   if (!D)
1183     MangleCtx.mangleGlobalBlock(BD,
1184       dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
1185   else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
1186     MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
1187   else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
1188     MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
1189   else
1190     MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
1191 
1192   auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
1193   return Result.first->first();
1194 }
1195 
1196 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
1197   return getModule().getNamedValue(Name);
1198 }
1199 
1200 /// AddGlobalCtor - Add a function to the list that will be called before
1201 /// main() runs.
1202 void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
1203                                   llvm::Constant *AssociatedData) {
1204   // FIXME: Type coercion of void()* types.
1205   GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData));
1206 }
1207 
1208 /// AddGlobalDtor - Add a function to the list that will be called
1209 /// when the module is unloaded.
1210 void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) {
1211   if (CodeGenOpts.RegisterGlobalDtorsWithAtExit) {
1212     DtorsUsingAtExit[Priority].push_back(Dtor);
1213     return;
1214   }
1215 
1216   // FIXME: Type coercion of void()* types.
1217   GlobalDtors.push_back(Structor(Priority, Dtor, nullptr));
1218 }
1219 
1220 void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
1221   if (Fns.empty()) return;
1222 
1223   // Ctor function type is void()*.
1224   llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
1225   llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
1226       TheModule.getDataLayout().getProgramAddressSpace());
1227 
1228   // Get the type of a ctor entry, { i32, void ()*, i8* }.
1229   llvm::StructType *CtorStructTy = llvm::StructType::get(
1230       Int32Ty, CtorPFTy, VoidPtrTy);
1231 
1232   // Construct the constructor and destructor arrays.
1233   ConstantInitBuilder builder(*this);
1234   auto ctors = builder.beginArray(CtorStructTy);
1235   for (const auto &I : Fns) {
1236     auto ctor = ctors.beginStruct(CtorStructTy);
1237     ctor.addInt(Int32Ty, I.Priority);
1238     ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
1239     if (I.AssociatedData)
1240       ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
1241     else
1242       ctor.addNullPointer(VoidPtrTy);
1243     ctor.finishAndAddTo(ctors);
1244   }
1245 
1246   auto list =
1247     ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
1248                                 /*constant*/ false,
1249                                 llvm::GlobalValue::AppendingLinkage);
1250 
1251   // The LTO linker doesn't seem to like it when we set an alignment
1252   // on appending variables.  Take it off as a workaround.
1253   list->setAlignment(llvm::None);
1254 
1255   Fns.clear();
1256 }
1257 
1258 llvm::GlobalValue::LinkageTypes
1259 CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
1260   const auto *D = cast<FunctionDecl>(GD.getDecl());
1261 
1262   GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
1263 
1264   if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
1265     return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType());
1266 
1267   if (isa<CXXConstructorDecl>(D) &&
1268       cast<CXXConstructorDecl>(D)->isInheritingConstructor() &&
1269       Context.getTargetInfo().getCXXABI().isMicrosoft()) {
1270     // Our approach to inheriting constructors is fundamentally different from
1271     // that used by the MS ABI, so keep our inheriting constructor thunks
1272     // internal rather than trying to pick an unambiguous mangling for them.
1273     return llvm::GlobalValue::InternalLinkage;
1274   }
1275 
1276   return getLLVMLinkageForDeclarator(D, Linkage, /*IsConstantVariable=*/false);
1277 }
1278 
1279 llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
1280   llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
1281   if (!MDS) return nullptr;
1282 
1283   return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
1284 }
1285 
1286 void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD,
1287                                               const CGFunctionInfo &Info,
1288                                               llvm::Function *F) {
1289   unsigned CallingConv;
1290   llvm::AttributeList PAL;
1291   ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv, false);
1292   F->setAttributes(PAL);
1293   F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1294 }
1295 
1296 static void removeImageAccessQualifier(std::string& TyName) {
1297   std::string ReadOnlyQual("__read_only");
1298   std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
1299   if (ReadOnlyPos != std::string::npos)
1300     // "+ 1" for the space after access qualifier.
1301     TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
1302   else {
1303     std::string WriteOnlyQual("__write_only");
1304     std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
1305     if (WriteOnlyPos != std::string::npos)
1306       TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
1307     else {
1308       std::string ReadWriteQual("__read_write");
1309       std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
1310       if (ReadWritePos != std::string::npos)
1311         TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
1312     }
1313   }
1314 }
1315 
1316 // Returns the address space id that should be produced to the
1317 // kernel_arg_addr_space metadata. This is always fixed to the ids
1318 // as specified in the SPIR 2.0 specification in order to differentiate
1319 // for example in clGetKernelArgInfo() implementation between the address
1320 // spaces with targets without unique mapping to the OpenCL address spaces
1321 // (basically all single AS CPUs).
1322 static unsigned ArgInfoAddressSpace(LangAS AS) {
1323   switch (AS) {
1324   case LangAS::opencl_global:   return 1;
1325   case LangAS::opencl_constant: return 2;
1326   case LangAS::opencl_local:    return 3;
1327   case LangAS::opencl_generic:  return 4; // Not in SPIR 2.0 specs.
1328   default:
1329     return 0; // Assume private.
1330   }
1331 }
1332 
1333 void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn,
1334                                          const FunctionDecl *FD,
1335                                          CodeGenFunction *CGF) {
1336   assert(((FD && CGF) || (!FD && !CGF)) &&
1337          "Incorrect use - FD and CGF should either be both null or not!");
1338   // Create MDNodes that represent the kernel arg metadata.
1339   // Each MDNode is a list in the form of "key", N number of values which is
1340   // the same number of values as their are kernel arguments.
1341 
1342   const PrintingPolicy &Policy = Context.getPrintingPolicy();
1343 
1344   // MDNode for the kernel argument address space qualifiers.
1345   SmallVector<llvm::Metadata *, 8> addressQuals;
1346 
1347   // MDNode for the kernel argument access qualifiers (images only).
1348   SmallVector<llvm::Metadata *, 8> accessQuals;
1349 
1350   // MDNode for the kernel argument type names.
1351   SmallVector<llvm::Metadata *, 8> argTypeNames;
1352 
1353   // MDNode for the kernel argument base type names.
1354   SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
1355 
1356   // MDNode for the kernel argument type qualifiers.
1357   SmallVector<llvm::Metadata *, 8> argTypeQuals;
1358 
1359   // MDNode for the kernel argument names.
1360   SmallVector<llvm::Metadata *, 8> argNames;
1361 
1362   if (FD && CGF)
1363     for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
1364       const ParmVarDecl *parm = FD->getParamDecl(i);
1365       QualType ty = parm->getType();
1366       std::string typeQuals;
1367 
1368       if (ty->isPointerType()) {
1369         QualType pointeeTy = ty->getPointeeType();
1370 
1371         // Get address qualifier.
1372         addressQuals.push_back(
1373             llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
1374                 ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
1375 
1376         // Get argument type name.
1377         std::string typeName =
1378             pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
1379 
1380         // Turn "unsigned type" to "utype"
1381         std::string::size_type pos = typeName.find("unsigned");
1382         if (pointeeTy.isCanonical() && pos != std::string::npos)
1383           typeName.erase(pos + 1, 8);
1384 
1385         argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1386 
1387         std::string baseTypeName =
1388             pointeeTy.getUnqualifiedType().getCanonicalType().getAsString(
1389                 Policy) +
1390             "*";
1391 
1392         // Turn "unsigned type" to "utype"
1393         pos = baseTypeName.find("unsigned");
1394         if (pos != std::string::npos)
1395           baseTypeName.erase(pos + 1, 8);
1396 
1397         argBaseTypeNames.push_back(
1398             llvm::MDString::get(VMContext, baseTypeName));
1399 
1400         // Get argument type qualifiers:
1401         if (ty.isRestrictQualified())
1402           typeQuals = "restrict";
1403         if (pointeeTy.isConstQualified() ||
1404             (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
1405           typeQuals += typeQuals.empty() ? "const" : " const";
1406         if (pointeeTy.isVolatileQualified())
1407           typeQuals += typeQuals.empty() ? "volatile" : " volatile";
1408       } else {
1409         uint32_t AddrSpc = 0;
1410         bool isPipe = ty->isPipeType();
1411         if (ty->isImageType() || isPipe)
1412           AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global);
1413 
1414         addressQuals.push_back(
1415             llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
1416 
1417         // Get argument type name.
1418         std::string typeName;
1419         if (isPipe)
1420           typeName = ty.getCanonicalType()
1421                          ->castAs<PipeType>()
1422                          ->getElementType()
1423                          .getAsString(Policy);
1424         else
1425           typeName = ty.getUnqualifiedType().getAsString(Policy);
1426 
1427         // Turn "unsigned type" to "utype"
1428         std::string::size_type pos = typeName.find("unsigned");
1429         if (ty.isCanonical() && pos != std::string::npos)
1430           typeName.erase(pos + 1, 8);
1431 
1432         std::string baseTypeName;
1433         if (isPipe)
1434           baseTypeName = ty.getCanonicalType()
1435                              ->castAs<PipeType>()
1436                              ->getElementType()
1437                              .getCanonicalType()
1438                              .getAsString(Policy);
1439         else
1440           baseTypeName =
1441               ty.getUnqualifiedType().getCanonicalType().getAsString(Policy);
1442 
1443         // Remove access qualifiers on images
1444         // (as they are inseparable from type in clang implementation,
1445         // but OpenCL spec provides a special query to get access qualifier
1446         // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
1447         if (ty->isImageType()) {
1448           removeImageAccessQualifier(typeName);
1449           removeImageAccessQualifier(baseTypeName);
1450         }
1451 
1452         argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1453 
1454         // Turn "unsigned type" to "utype"
1455         pos = baseTypeName.find("unsigned");
1456         if (pos != std::string::npos)
1457           baseTypeName.erase(pos + 1, 8);
1458 
1459         argBaseTypeNames.push_back(
1460             llvm::MDString::get(VMContext, baseTypeName));
1461 
1462         if (isPipe)
1463           typeQuals = "pipe";
1464       }
1465 
1466       argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
1467 
1468       // Get image and pipe access qualifier:
1469       if (ty->isImageType() || ty->isPipeType()) {
1470         const Decl *PDecl = parm;
1471         if (auto *TD = dyn_cast<TypedefType>(ty))
1472           PDecl = TD->getDecl();
1473         const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
1474         if (A && A->isWriteOnly())
1475           accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
1476         else if (A && A->isReadWrite())
1477           accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
1478         else
1479           accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
1480       } else
1481         accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
1482 
1483       // Get argument name.
1484       argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
1485     }
1486 
1487   Fn->setMetadata("kernel_arg_addr_space",
1488                   llvm::MDNode::get(VMContext, addressQuals));
1489   Fn->setMetadata("kernel_arg_access_qual",
1490                   llvm::MDNode::get(VMContext, accessQuals));
1491   Fn->setMetadata("kernel_arg_type",
1492                   llvm::MDNode::get(VMContext, argTypeNames));
1493   Fn->setMetadata("kernel_arg_base_type",
1494                   llvm::MDNode::get(VMContext, argBaseTypeNames));
1495   Fn->setMetadata("kernel_arg_type_qual",
1496                   llvm::MDNode::get(VMContext, argTypeQuals));
1497   if (getCodeGenOpts().EmitOpenCLArgMetadata)
1498     Fn->setMetadata("kernel_arg_name",
1499                     llvm::MDNode::get(VMContext, argNames));
1500 }
1501 
1502 /// Determines whether the language options require us to model
1503 /// unwind exceptions.  We treat -fexceptions as mandating this
1504 /// except under the fragile ObjC ABI with only ObjC exceptions
1505 /// enabled.  This means, for example, that C with -fexceptions
1506 /// enables this.
1507 static bool hasUnwindExceptions(const LangOptions &LangOpts) {
1508   // If exceptions are completely disabled, obviously this is false.
1509   if (!LangOpts.Exceptions) return false;
1510 
1511   // If C++ exceptions are enabled, this is true.
1512   if (LangOpts.CXXExceptions) return true;
1513 
1514   // If ObjC exceptions are enabled, this depends on the ABI.
1515   if (LangOpts.ObjCExceptions) {
1516     return LangOpts.ObjCRuntime.hasUnwindExceptions();
1517   }
1518 
1519   return true;
1520 }
1521 
1522 static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM,
1523                                                       const CXXMethodDecl *MD) {
1524   // Check that the type metadata can ever actually be used by a call.
1525   if (!CGM.getCodeGenOpts().LTOUnit ||
1526       !CGM.HasHiddenLTOVisibility(MD->getParent()))
1527     return false;
1528 
1529   // Only functions whose address can be taken with a member function pointer
1530   // need this sort of type metadata.
1531   return !MD->isStatic() && !MD->isVirtual() && !isa<CXXConstructorDecl>(MD) &&
1532          !isa<CXXDestructorDecl>(MD);
1533 }
1534 
1535 std::vector<const CXXRecordDecl *>
1536 CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) {
1537   llvm::SetVector<const CXXRecordDecl *> MostBases;
1538 
1539   std::function<void (const CXXRecordDecl *)> CollectMostBases;
1540   CollectMostBases = [&](const CXXRecordDecl *RD) {
1541     if (RD->getNumBases() == 0)
1542       MostBases.insert(RD);
1543     for (const CXXBaseSpecifier &B : RD->bases())
1544       CollectMostBases(B.getType()->getAsCXXRecordDecl());
1545   };
1546   CollectMostBases(RD);
1547   return MostBases.takeVector();
1548 }
1549 
1550 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
1551                                                            llvm::Function *F) {
1552   llvm::AttrBuilder B;
1553 
1554   if (CodeGenOpts.UnwindTables)
1555     B.addAttribute(llvm::Attribute::UWTable);
1556 
1557   if (CodeGenOpts.StackClashProtector)
1558     B.addAttribute("probe-stack", "inline-asm");
1559 
1560   if (!hasUnwindExceptions(LangOpts))
1561     B.addAttribute(llvm::Attribute::NoUnwind);
1562 
1563   if (!D || !D->hasAttr<NoStackProtectorAttr>()) {
1564     if (LangOpts.getStackProtector() == LangOptions::SSPOn)
1565       B.addAttribute(llvm::Attribute::StackProtect);
1566     else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
1567       B.addAttribute(llvm::Attribute::StackProtectStrong);
1568     else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
1569       B.addAttribute(llvm::Attribute::StackProtectReq);
1570   }
1571 
1572   if (!D) {
1573     // If we don't have a declaration to control inlining, the function isn't
1574     // explicitly marked as alwaysinline for semantic reasons, and inlining is
1575     // disabled, mark the function as noinline.
1576     if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
1577         CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
1578       B.addAttribute(llvm::Attribute::NoInline);
1579 
1580     F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1581     return;
1582   }
1583 
1584   // Track whether we need to add the optnone LLVM attribute,
1585   // starting with the default for this optimization level.
1586   bool ShouldAddOptNone =
1587       !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
1588   // We can't add optnone in the following cases, it won't pass the verifier.
1589   ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
1590   ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
1591 
1592   // Add optnone, but do so only if the function isn't always_inline.
1593   if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) &&
1594       !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
1595     B.addAttribute(llvm::Attribute::OptimizeNone);
1596 
1597     // OptimizeNone implies noinline; we should not be inlining such functions.
1598     B.addAttribute(llvm::Attribute::NoInline);
1599 
1600     // We still need to handle naked functions even though optnone subsumes
1601     // much of their semantics.
1602     if (D->hasAttr<NakedAttr>())
1603       B.addAttribute(llvm::Attribute::Naked);
1604 
1605     // OptimizeNone wins over OptimizeForSize and MinSize.
1606     F->removeFnAttr(llvm::Attribute::OptimizeForSize);
1607     F->removeFnAttr(llvm::Attribute::MinSize);
1608   } else if (D->hasAttr<NakedAttr>()) {
1609     // Naked implies noinline: we should not be inlining such functions.
1610     B.addAttribute(llvm::Attribute::Naked);
1611     B.addAttribute(llvm::Attribute::NoInline);
1612   } else if (D->hasAttr<NoDuplicateAttr>()) {
1613     B.addAttribute(llvm::Attribute::NoDuplicate);
1614   } else if (D->hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
1615     // Add noinline if the function isn't always_inline.
1616     B.addAttribute(llvm::Attribute::NoInline);
1617   } else if (D->hasAttr<AlwaysInlineAttr>() &&
1618              !F->hasFnAttribute(llvm::Attribute::NoInline)) {
1619     // (noinline wins over always_inline, and we can't specify both in IR)
1620     B.addAttribute(llvm::Attribute::AlwaysInline);
1621   } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
1622     // If we're not inlining, then force everything that isn't always_inline to
1623     // carry an explicit noinline attribute.
1624     if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
1625       B.addAttribute(llvm::Attribute::NoInline);
1626   } else {
1627     // Otherwise, propagate the inline hint attribute and potentially use its
1628     // absence to mark things as noinline.
1629     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1630       // Search function and template pattern redeclarations for inline.
1631       auto CheckForInline = [](const FunctionDecl *FD) {
1632         auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
1633           return Redecl->isInlineSpecified();
1634         };
1635         if (any_of(FD->redecls(), CheckRedeclForInline))
1636           return true;
1637         const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
1638         if (!Pattern)
1639           return false;
1640         return any_of(Pattern->redecls(), CheckRedeclForInline);
1641       };
1642       if (CheckForInline(FD)) {
1643         B.addAttribute(llvm::Attribute::InlineHint);
1644       } else if (CodeGenOpts.getInlining() ==
1645                      CodeGenOptions::OnlyHintInlining &&
1646                  !FD->isInlined() &&
1647                  !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
1648         B.addAttribute(llvm::Attribute::NoInline);
1649       }
1650     }
1651   }
1652 
1653   // Add other optimization related attributes if we are optimizing this
1654   // function.
1655   if (!D->hasAttr<OptimizeNoneAttr>()) {
1656     if (D->hasAttr<ColdAttr>()) {
1657       if (!ShouldAddOptNone)
1658         B.addAttribute(llvm::Attribute::OptimizeForSize);
1659       B.addAttribute(llvm::Attribute::Cold);
1660     }
1661 
1662     if (D->hasAttr<MinSizeAttr>())
1663       B.addAttribute(llvm::Attribute::MinSize);
1664   }
1665 
1666   F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1667 
1668   unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
1669   if (alignment)
1670     F->setAlignment(llvm::Align(alignment));
1671 
1672   if (!D->hasAttr<AlignedAttr>())
1673     if (LangOpts.FunctionAlignment)
1674       F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
1675 
1676   // Some C++ ABIs require 2-byte alignment for member functions, in order to
1677   // reserve a bit for differentiating between virtual and non-virtual member
1678   // functions. If the current target's C++ ABI requires this and this is a
1679   // member function, set its alignment accordingly.
1680   if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
1681     if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
1682       F->setAlignment(llvm::Align(2));
1683   }
1684 
1685   // In the cross-dso CFI mode with canonical jump tables, we want !type
1686   // attributes on definitions only.
1687   if (CodeGenOpts.SanitizeCfiCrossDso &&
1688       CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
1689     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1690       // Skip available_externally functions. They won't be codegen'ed in the
1691       // current module anyway.
1692       if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
1693         CreateFunctionTypeMetadataForIcall(FD, F);
1694     }
1695   }
1696 
1697   // Emit type metadata on member functions for member function pointer checks.
1698   // These are only ever necessary on definitions; we're guaranteed that the
1699   // definition will be present in the LTO unit as a result of LTO visibility.
1700   auto *MD = dyn_cast<CXXMethodDecl>(D);
1701   if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) {
1702     for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
1703       llvm::Metadata *Id =
1704           CreateMetadataIdentifierForType(Context.getMemberPointerType(
1705               MD->getType(), Context.getRecordType(Base).getTypePtr()));
1706       F->addTypeMetadata(0, Id);
1707     }
1708   }
1709 }
1710 
1711 void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
1712   const Decl *D = GD.getDecl();
1713   if (dyn_cast_or_null<NamedDecl>(D))
1714     setGVProperties(GV, GD);
1715   else
1716     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1717 
1718   if (D && D->hasAttr<UsedAttr>())
1719     addUsedGlobal(GV);
1720 
1721   if (CodeGenOpts.KeepStaticConsts && D && isa<VarDecl>(D)) {
1722     const auto *VD = cast<VarDecl>(D);
1723     if (VD->getType().isConstQualified() &&
1724         VD->getStorageDuration() == SD_Static)
1725       addUsedGlobal(GV);
1726   }
1727 }
1728 
1729 bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
1730                                                 llvm::AttrBuilder &Attrs) {
1731   // Add target-cpu and target-features attributes to functions. If
1732   // we have a decl for the function and it has a target attribute then
1733   // parse that and add it to the feature set.
1734   StringRef TargetCPU = getTarget().getTargetOpts().CPU;
1735   std::vector<std::string> Features;
1736   const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
1737   FD = FD ? FD->getMostRecentDecl() : FD;
1738   const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
1739   const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
1740   bool AddedAttr = false;
1741   if (TD || SD) {
1742     llvm::StringMap<bool> FeatureMap;
1743     getContext().getFunctionFeatureMap(FeatureMap, GD);
1744 
1745     // Produce the canonical string for this set of features.
1746     for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
1747       Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
1748 
1749     // Now add the target-cpu and target-features to the function.
1750     // While we populated the feature map above, we still need to
1751     // get and parse the target attribute so we can get the cpu for
1752     // the function.
1753     if (TD) {
1754       ParsedTargetAttr ParsedAttr = TD->parse();
1755       if (ParsedAttr.Architecture != "" &&
1756           getTarget().isValidCPUName(ParsedAttr.Architecture))
1757         TargetCPU = ParsedAttr.Architecture;
1758     }
1759   } else {
1760     // Otherwise just add the existing target cpu and target features to the
1761     // function.
1762     Features = getTarget().getTargetOpts().Features;
1763   }
1764 
1765   if (TargetCPU != "") {
1766     Attrs.addAttribute("target-cpu", TargetCPU);
1767     AddedAttr = true;
1768   }
1769   if (!Features.empty()) {
1770     llvm::sort(Features);
1771     Attrs.addAttribute("target-features", llvm::join(Features, ","));
1772     AddedAttr = true;
1773   }
1774 
1775   return AddedAttr;
1776 }
1777 
1778 void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
1779                                           llvm::GlobalObject *GO) {
1780   const Decl *D = GD.getDecl();
1781   SetCommonAttributes(GD, GO);
1782 
1783   if (D) {
1784     if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
1785       if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
1786         GV->addAttribute("bss-section", SA->getName());
1787       if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
1788         GV->addAttribute("data-section", SA->getName());
1789       if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
1790         GV->addAttribute("rodata-section", SA->getName());
1791       if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
1792         GV->addAttribute("relro-section", SA->getName());
1793     }
1794 
1795     if (auto *F = dyn_cast<llvm::Function>(GO)) {
1796       if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
1797         if (!D->getAttr<SectionAttr>())
1798           F->addFnAttr("implicit-section-name", SA->getName());
1799 
1800       llvm::AttrBuilder Attrs;
1801       if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
1802         // We know that GetCPUAndFeaturesAttributes will always have the
1803         // newest set, since it has the newest possible FunctionDecl, so the
1804         // new ones should replace the old.
1805         F->removeFnAttr("target-cpu");
1806         F->removeFnAttr("target-features");
1807         F->addAttributes(llvm::AttributeList::FunctionIndex, Attrs);
1808       }
1809     }
1810 
1811     if (const auto *CSA = D->getAttr<CodeSegAttr>())
1812       GO->setSection(CSA->getName());
1813     else if (const auto *SA = D->getAttr<SectionAttr>())
1814       GO->setSection(SA->getName());
1815   }
1816 
1817   getTargetCodeGenInfo().setTargetAttributes(D, GO, *this);
1818 }
1819 
1820 void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
1821                                                   llvm::Function *F,
1822                                                   const CGFunctionInfo &FI) {
1823   const Decl *D = GD.getDecl();
1824   SetLLVMFunctionAttributes(GD, FI, F);
1825   SetLLVMFunctionAttributesForDefinition(D, F);
1826 
1827   F->setLinkage(llvm::Function::InternalLinkage);
1828 
1829   setNonAliasAttributes(GD, F);
1830 }
1831 
1832 static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
1833   // Set linkage and visibility in case we never see a definition.
1834   LinkageInfo LV = ND->getLinkageAndVisibility();
1835   // Don't set internal linkage on declarations.
1836   // "extern_weak" is overloaded in LLVM; we probably should have
1837   // separate linkage types for this.
1838   if (isExternallyVisible(LV.getLinkage()) &&
1839       (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
1840     GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
1841 }
1842 
1843 void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1844                                                        llvm::Function *F) {
1845   // Only if we are checking indirect calls.
1846   if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
1847     return;
1848 
1849   // Non-static class methods are handled via vtable or member function pointer
1850   // checks elsewhere.
1851   if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
1852     return;
1853 
1854   llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
1855   F->addTypeMetadata(0, MD);
1856   F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
1857 
1858   // Emit a hash-based bit set entry for cross-DSO calls.
1859   if (CodeGenOpts.SanitizeCfiCrossDso)
1860     if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
1861       F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
1862 }
1863 
1864 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1865                                           bool IsIncompleteFunction,
1866                                           bool IsThunk) {
1867 
1868   if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
1869     // If this is an intrinsic function, set the function's attributes
1870     // to the intrinsic's attributes.
1871     F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
1872     return;
1873   }
1874 
1875   const auto *FD = cast<FunctionDecl>(GD.getDecl());
1876 
1877   if (!IsIncompleteFunction)
1878     SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F);
1879 
1880   // Add the Returned attribute for "this", except for iOS 5 and earlier
1881   // where substantial code, including the libstdc++ dylib, was compiled with
1882   // GCC and does not actually return "this".
1883   if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
1884       !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
1885     assert(!F->arg_empty() &&
1886            F->arg_begin()->getType()
1887              ->canLosslesslyBitCastTo(F->getReturnType()) &&
1888            "unexpected this return");
1889     F->addAttribute(1, llvm::Attribute::Returned);
1890   }
1891 
1892   // Only a few attributes are set on declarations; these may later be
1893   // overridden by a definition.
1894 
1895   setLinkageForGV(F, FD);
1896   setGVProperties(F, FD);
1897 
1898   // Setup target-specific attributes.
1899   if (!IsIncompleteFunction && F->isDeclaration())
1900     getTargetCodeGenInfo().setTargetAttributes(FD, F, *this);
1901 
1902   if (const auto *CSA = FD->getAttr<CodeSegAttr>())
1903     F->setSection(CSA->getName());
1904   else if (const auto *SA = FD->getAttr<SectionAttr>())
1905      F->setSection(SA->getName());
1906 
1907   // If we plan on emitting this inline builtin, we can't treat it as a builtin.
1908   if (FD->isInlineBuiltinDeclaration()) {
1909     const FunctionDecl *FDBody;
1910     bool HasBody = FD->hasBody(FDBody);
1911     (void)HasBody;
1912     assert(HasBody && "Inline builtin declarations should always have an "
1913                       "available body!");
1914     if (shouldEmitFunction(FDBody))
1915       F->addAttribute(llvm::AttributeList::FunctionIndex,
1916                       llvm::Attribute::NoBuiltin);
1917   }
1918 
1919   if (FD->isReplaceableGlobalAllocationFunction()) {
1920     // A replaceable global allocation function does not act like a builtin by
1921     // default, only if it is invoked by a new-expression or delete-expression.
1922     F->addAttribute(llvm::AttributeList::FunctionIndex,
1923                     llvm::Attribute::NoBuiltin);
1924   }
1925 
1926   if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
1927     F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1928   else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1929     if (MD->isVirtual())
1930       F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1931 
1932   // Don't emit entries for function declarations in the cross-DSO mode. This
1933   // is handled with better precision by the receiving DSO. But if jump tables
1934   // are non-canonical then we need type metadata in order to produce the local
1935   // jump table.
1936   if (!CodeGenOpts.SanitizeCfiCrossDso ||
1937       !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
1938     CreateFunctionTypeMetadataForIcall(FD, F);
1939 
1940   if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
1941     getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
1942 
1943   if (const auto *CB = FD->getAttr<CallbackAttr>()) {
1944     // Annotate the callback behavior as metadata:
1945     //  - The callback callee (as argument number).
1946     //  - The callback payloads (as argument numbers).
1947     llvm::LLVMContext &Ctx = F->getContext();
1948     llvm::MDBuilder MDB(Ctx);
1949 
1950     // The payload indices are all but the first one in the encoding. The first
1951     // identifies the callback callee.
1952     int CalleeIdx = *CB->encoding_begin();
1953     ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
1954     F->addMetadata(llvm::LLVMContext::MD_callback,
1955                    *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
1956                                                CalleeIdx, PayloadIndices,
1957                                                /* VarArgsArePassed */ false)}));
1958   }
1959 }
1960 
1961 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
1962   assert(!GV->isDeclaration() &&
1963          "Only globals with definition can force usage.");
1964   LLVMUsed.emplace_back(GV);
1965 }
1966 
1967 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
1968   assert(!GV->isDeclaration() &&
1969          "Only globals with definition can force usage.");
1970   LLVMCompilerUsed.emplace_back(GV);
1971 }
1972 
1973 static void emitUsed(CodeGenModule &CGM, StringRef Name,
1974                      std::vector<llvm::WeakTrackingVH> &List) {
1975   // Don't create llvm.used if there is no need.
1976   if (List.empty())
1977     return;
1978 
1979   // Convert List to what ConstantArray needs.
1980   SmallVector<llvm::Constant*, 8> UsedArray;
1981   UsedArray.resize(List.size());
1982   for (unsigned i = 0, e = List.size(); i != e; ++i) {
1983     UsedArray[i] =
1984         llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1985             cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
1986   }
1987 
1988   if (UsedArray.empty())
1989     return;
1990   llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
1991 
1992   auto *GV = new llvm::GlobalVariable(
1993       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
1994       llvm::ConstantArray::get(ATy, UsedArray), Name);
1995 
1996   GV->setSection("llvm.metadata");
1997 }
1998 
1999 void CodeGenModule::emitLLVMUsed() {
2000   emitUsed(*this, "llvm.used", LLVMUsed);
2001   emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
2002 }
2003 
2004 void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
2005   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
2006   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2007 }
2008 
2009 void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
2010   llvm::SmallString<32> Opt;
2011   getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
2012   if (Opt.empty())
2013     return;
2014   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2015   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2016 }
2017 
2018 void CodeGenModule::AddDependentLib(StringRef Lib) {
2019   auto &C = getLLVMContext();
2020   if (getTarget().getTriple().isOSBinFormatELF()) {
2021       ELFDependentLibraries.push_back(
2022         llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
2023     return;
2024   }
2025 
2026   llvm::SmallString<24> Opt;
2027   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
2028   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2029   LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
2030 }
2031 
2032 /// Add link options implied by the given module, including modules
2033 /// it depends on, using a postorder walk.
2034 static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
2035                                     SmallVectorImpl<llvm::MDNode *> &Metadata,
2036                                     llvm::SmallPtrSet<Module *, 16> &Visited) {
2037   // Import this module's parent.
2038   if (Mod->Parent && Visited.insert(Mod->Parent).second) {
2039     addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
2040   }
2041 
2042   // Import this module's dependencies.
2043   for (unsigned I = Mod->Imports.size(); I > 0; --I) {
2044     if (Visited.insert(Mod->Imports[I - 1]).second)
2045       addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited);
2046   }
2047 
2048   // Add linker options to link against the libraries/frameworks
2049   // described by this module.
2050   llvm::LLVMContext &Context = CGM.getLLVMContext();
2051   bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
2052 
2053   // For modules that use export_as for linking, use that module
2054   // name instead.
2055   if (Mod->UseExportAsModuleLinkName)
2056     return;
2057 
2058   for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
2059     // Link against a framework.  Frameworks are currently Darwin only, so we
2060     // don't to ask TargetCodeGenInfo for the spelling of the linker option.
2061     if (Mod->LinkLibraries[I-1].IsFramework) {
2062       llvm::Metadata *Args[2] = {
2063           llvm::MDString::get(Context, "-framework"),
2064           llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)};
2065 
2066       Metadata.push_back(llvm::MDNode::get(Context, Args));
2067       continue;
2068     }
2069 
2070     // Link against a library.
2071     if (IsELF) {
2072       llvm::Metadata *Args[2] = {
2073           llvm::MDString::get(Context, "lib"),
2074           llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library),
2075       };
2076       Metadata.push_back(llvm::MDNode::get(Context, Args));
2077     } else {
2078       llvm::SmallString<24> Opt;
2079       CGM.getTargetCodeGenInfo().getDependentLibraryOption(
2080           Mod->LinkLibraries[I - 1].Library, Opt);
2081       auto *OptString = llvm::MDString::get(Context, Opt);
2082       Metadata.push_back(llvm::MDNode::get(Context, OptString));
2083     }
2084   }
2085 }
2086 
2087 void CodeGenModule::EmitModuleLinkOptions() {
2088   // Collect the set of all of the modules we want to visit to emit link
2089   // options, which is essentially the imported modules and all of their
2090   // non-explicit child modules.
2091   llvm::SetVector<clang::Module *> LinkModules;
2092   llvm::SmallPtrSet<clang::Module *, 16> Visited;
2093   SmallVector<clang::Module *, 16> Stack;
2094 
2095   // Seed the stack with imported modules.
2096   for (Module *M : ImportedModules) {
2097     // Do not add any link flags when an implementation TU of a module imports
2098     // a header of that same module.
2099     if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
2100         !getLangOpts().isCompilingModule())
2101       continue;
2102     if (Visited.insert(M).second)
2103       Stack.push_back(M);
2104   }
2105 
2106   // Find all of the modules to import, making a little effort to prune
2107   // non-leaf modules.
2108   while (!Stack.empty()) {
2109     clang::Module *Mod = Stack.pop_back_val();
2110 
2111     bool AnyChildren = false;
2112 
2113     // Visit the submodules of this module.
2114     for (const auto &SM : Mod->submodules()) {
2115       // Skip explicit children; they need to be explicitly imported to be
2116       // linked against.
2117       if (SM->IsExplicit)
2118         continue;
2119 
2120       if (Visited.insert(SM).second) {
2121         Stack.push_back(SM);
2122         AnyChildren = true;
2123       }
2124     }
2125 
2126     // We didn't find any children, so add this module to the list of
2127     // modules to link against.
2128     if (!AnyChildren) {
2129       LinkModules.insert(Mod);
2130     }
2131   }
2132 
2133   // Add link options for all of the imported modules in reverse topological
2134   // order.  We don't do anything to try to order import link flags with respect
2135   // to linker options inserted by things like #pragma comment().
2136   SmallVector<llvm::MDNode *, 16> MetadataArgs;
2137   Visited.clear();
2138   for (Module *M : LinkModules)
2139     if (Visited.insert(M).second)
2140       addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
2141   std::reverse(MetadataArgs.begin(), MetadataArgs.end());
2142   LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
2143 
2144   // Add the linker options metadata flag.
2145   auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
2146   for (auto *MD : LinkerOptionsMetadata)
2147     NMD->addOperand(MD);
2148 }
2149 
2150 void CodeGenModule::EmitDeferred() {
2151   // Emit deferred declare target declarations.
2152   if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
2153     getOpenMPRuntime().emitDeferredTargetDecls();
2154 
2155   // Emit code for any potentially referenced deferred decls.  Since a
2156   // previously unused static decl may become used during the generation of code
2157   // for a static function, iterate until no changes are made.
2158 
2159   if (!DeferredVTables.empty()) {
2160     EmitDeferredVTables();
2161 
2162     // Emitting a vtable doesn't directly cause more vtables to
2163     // become deferred, although it can cause functions to be
2164     // emitted that then need those vtables.
2165     assert(DeferredVTables.empty());
2166   }
2167 
2168   // Stop if we're out of both deferred vtables and deferred declarations.
2169   if (DeferredDeclsToEmit.empty())
2170     return;
2171 
2172   // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
2173   // work, it will not interfere with this.
2174   std::vector<GlobalDecl> CurDeclsToEmit;
2175   CurDeclsToEmit.swap(DeferredDeclsToEmit);
2176 
2177   for (GlobalDecl &D : CurDeclsToEmit) {
2178     // We should call GetAddrOfGlobal with IsForDefinition set to true in order
2179     // to get GlobalValue with exactly the type we need, not something that
2180     // might had been created for another decl with the same mangled name but
2181     // different type.
2182     llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
2183         GetAddrOfGlobal(D, ForDefinition));
2184 
2185     // In case of different address spaces, we may still get a cast, even with
2186     // IsForDefinition equal to true. Query mangled names table to get
2187     // GlobalValue.
2188     if (!GV)
2189       GV = GetGlobalValue(getMangledName(D));
2190 
2191     // Make sure GetGlobalValue returned non-null.
2192     assert(GV);
2193 
2194     // Check to see if we've already emitted this.  This is necessary
2195     // for a couple of reasons: first, decls can end up in the
2196     // deferred-decls queue multiple times, and second, decls can end
2197     // up with definitions in unusual ways (e.g. by an extern inline
2198     // function acquiring a strong function redefinition).  Just
2199     // ignore these cases.
2200     if (!GV->isDeclaration())
2201       continue;
2202 
2203     // If this is OpenMP, check if it is legal to emit this global normally.
2204     if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
2205       continue;
2206 
2207     // Otherwise, emit the definition and move on to the next one.
2208     EmitGlobalDefinition(D, GV);
2209 
2210     // If we found out that we need to emit more decls, do that recursively.
2211     // This has the advantage that the decls are emitted in a DFS and related
2212     // ones are close together, which is convenient for testing.
2213     if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
2214       EmitDeferred();
2215       assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
2216     }
2217   }
2218 }
2219 
2220 void CodeGenModule::EmitVTablesOpportunistically() {
2221   // Try to emit external vtables as available_externally if they have emitted
2222   // all inlined virtual functions.  It runs after EmitDeferred() and therefore
2223   // is not allowed to create new references to things that need to be emitted
2224   // lazily. Note that it also uses fact that we eagerly emitting RTTI.
2225 
2226   assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
2227          && "Only emit opportunistic vtables with optimizations");
2228 
2229   for (const CXXRecordDecl *RD : OpportunisticVTables) {
2230     assert(getVTables().isVTableExternal(RD) &&
2231            "This queue should only contain external vtables");
2232     if (getCXXABI().canSpeculativelyEmitVTable(RD))
2233       VTables.GenerateClassData(RD);
2234   }
2235   OpportunisticVTables.clear();
2236 }
2237 
2238 void CodeGenModule::EmitGlobalAnnotations() {
2239   if (Annotations.empty())
2240     return;
2241 
2242   // Create a new global variable for the ConstantStruct in the Module.
2243   llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
2244     Annotations[0]->getType(), Annotations.size()), Annotations);
2245   auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
2246                                       llvm::GlobalValue::AppendingLinkage,
2247                                       Array, "llvm.global.annotations");
2248   gv->setSection(AnnotationSection);
2249 }
2250 
2251 llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
2252   llvm::Constant *&AStr = AnnotationStrings[Str];
2253   if (AStr)
2254     return AStr;
2255 
2256   // Not found yet, create a new global.
2257   llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
2258   auto *gv =
2259       new llvm::GlobalVariable(getModule(), s->getType(), true,
2260                                llvm::GlobalValue::PrivateLinkage, s, ".str");
2261   gv->setSection(AnnotationSection);
2262   gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2263   AStr = gv;
2264   return gv;
2265 }
2266 
2267 llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
2268   SourceManager &SM = getContext().getSourceManager();
2269   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
2270   if (PLoc.isValid())
2271     return EmitAnnotationString(PLoc.getFilename());
2272   return EmitAnnotationString(SM.getBufferName(Loc));
2273 }
2274 
2275 llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
2276   SourceManager &SM = getContext().getSourceManager();
2277   PresumedLoc PLoc = SM.getPresumedLoc(L);
2278   unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
2279     SM.getExpansionLineNumber(L);
2280   return llvm::ConstantInt::get(Int32Ty, LineNo);
2281 }
2282 
2283 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
2284                                                 const AnnotateAttr *AA,
2285                                                 SourceLocation L) {
2286   // Get the globals for file name, annotation, and the line number.
2287   llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
2288                  *UnitGV = EmitAnnotationUnit(L),
2289                  *LineNoCst = EmitAnnotationLineNo(L);
2290 
2291   llvm::Constant *ASZeroGV = GV;
2292   if (GV->getAddressSpace() != 0) {
2293     ASZeroGV = llvm::ConstantExpr::getAddrSpaceCast(
2294                    GV, GV->getValueType()->getPointerTo(0));
2295   }
2296 
2297   // Create the ConstantStruct for the global annotation.
2298   llvm::Constant *Fields[4] = {
2299     llvm::ConstantExpr::getBitCast(ASZeroGV, Int8PtrTy),
2300     llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
2301     llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
2302     LineNoCst
2303   };
2304   return llvm::ConstantStruct::getAnon(Fields);
2305 }
2306 
2307 void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
2308                                          llvm::GlobalValue *GV) {
2309   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2310   // Get the struct elements for these annotations.
2311   for (const auto *I : D->specific_attrs<AnnotateAttr>())
2312     Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
2313 }
2314 
2315 bool CodeGenModule::isInSanitizerBlacklist(SanitizerMask Kind,
2316                                            llvm::Function *Fn,
2317                                            SourceLocation Loc) const {
2318   const auto &SanitizerBL = getContext().getSanitizerBlacklist();
2319   // Blacklist by function name.
2320   if (SanitizerBL.isBlacklistedFunction(Kind, Fn->getName()))
2321     return true;
2322   // Blacklist by location.
2323   if (Loc.isValid())
2324     return SanitizerBL.isBlacklistedLocation(Kind, Loc);
2325   // If location is unknown, this may be a compiler-generated function. Assume
2326   // it's located in the main file.
2327   auto &SM = Context.getSourceManager();
2328   if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
2329     return SanitizerBL.isBlacklistedFile(Kind, MainFile->getName());
2330   }
2331   return false;
2332 }
2333 
2334 bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV,
2335                                            SourceLocation Loc, QualType Ty,
2336                                            StringRef Category) const {
2337   // For now globals can be blacklisted only in ASan and KASan.
2338   const SanitizerMask EnabledAsanMask =
2339       LangOpts.Sanitize.Mask &
2340       (SanitizerKind::Address | SanitizerKind::KernelAddress |
2341        SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
2342        SanitizerKind::MemTag);
2343   if (!EnabledAsanMask)
2344     return false;
2345   const auto &SanitizerBL = getContext().getSanitizerBlacklist();
2346   if (SanitizerBL.isBlacklistedGlobal(EnabledAsanMask, GV->getName(), Category))
2347     return true;
2348   if (SanitizerBL.isBlacklistedLocation(EnabledAsanMask, Loc, Category))
2349     return true;
2350   // Check global type.
2351   if (!Ty.isNull()) {
2352     // Drill down the array types: if global variable of a fixed type is
2353     // blacklisted, we also don't instrument arrays of them.
2354     while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
2355       Ty = AT->getElementType();
2356     Ty = Ty.getCanonicalType().getUnqualifiedType();
2357     // We allow to blacklist only record types (classes, structs etc.)
2358     if (Ty->isRecordType()) {
2359       std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
2360       if (SanitizerBL.isBlacklistedType(EnabledAsanMask, TypeStr, Category))
2361         return true;
2362     }
2363   }
2364   return false;
2365 }
2366 
2367 bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
2368                                    StringRef Category) const {
2369   const auto &XRayFilter = getContext().getXRayFilter();
2370   using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
2371   auto Attr = ImbueAttr::NONE;
2372   if (Loc.isValid())
2373     Attr = XRayFilter.shouldImbueLocation(Loc, Category);
2374   if (Attr == ImbueAttr::NONE)
2375     Attr = XRayFilter.shouldImbueFunction(Fn->getName());
2376   switch (Attr) {
2377   case ImbueAttr::NONE:
2378     return false;
2379   case ImbueAttr::ALWAYS:
2380     Fn->addFnAttr("function-instrument", "xray-always");
2381     break;
2382   case ImbueAttr::ALWAYS_ARG1:
2383     Fn->addFnAttr("function-instrument", "xray-always");
2384     Fn->addFnAttr("xray-log-args", "1");
2385     break;
2386   case ImbueAttr::NEVER:
2387     Fn->addFnAttr("function-instrument", "xray-never");
2388     break;
2389   }
2390   return true;
2391 }
2392 
2393 bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
2394   // Never defer when EmitAllDecls is specified.
2395   if (LangOpts.EmitAllDecls)
2396     return true;
2397 
2398   if (CodeGenOpts.KeepStaticConsts) {
2399     const auto *VD = dyn_cast<VarDecl>(Global);
2400     if (VD && VD->getType().isConstQualified() &&
2401         VD->getStorageDuration() == SD_Static)
2402       return true;
2403   }
2404 
2405   return getContext().DeclMustBeEmitted(Global);
2406 }
2407 
2408 bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
2409   if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
2410     if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
2411       // Implicit template instantiations may change linkage if they are later
2412       // explicitly instantiated, so they should not be emitted eagerly.
2413       return false;
2414     // In OpenMP 5.0 function may be marked as device_type(nohost) and we should
2415     // not emit them eagerly unless we sure that the function must be emitted on
2416     // the host.
2417     if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd &&
2418         !LangOpts.OpenMPIsDevice &&
2419         !OMPDeclareTargetDeclAttr::getDeviceType(FD) &&
2420         !FD->isUsed(/*CheckUsedAttr=*/false) && !FD->isReferenced())
2421       return false;
2422   }
2423   if (const auto *VD = dyn_cast<VarDecl>(Global))
2424     if (Context.getInlineVariableDefinitionKind(VD) ==
2425         ASTContext::InlineVariableDefinitionKind::WeakUnknown)
2426       // A definition of an inline constexpr static data member may change
2427       // linkage later if it's redeclared outside the class.
2428       return false;
2429   // If OpenMP is enabled and threadprivates must be generated like TLS, delay
2430   // codegen for global variables, because they may be marked as threadprivate.
2431   if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
2432       getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
2433       !isTypeConstant(Global->getType(), false) &&
2434       !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
2435     return false;
2436 
2437   return true;
2438 }
2439 
2440 ConstantAddress CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl *GD) {
2441   StringRef Name = getMangledName(GD);
2442 
2443   // The UUID descriptor should be pointer aligned.
2444   CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes);
2445 
2446   // Look for an existing global.
2447   if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
2448     return ConstantAddress(GV, Alignment);
2449 
2450   ConstantEmitter Emitter(*this);
2451   llvm::Constant *Init;
2452 
2453   APValue &V = GD->getAsAPValue();
2454   if (!V.isAbsent()) {
2455     // If possible, emit the APValue version of the initializer. In particular,
2456     // this gets the type of the constant right.
2457     Init = Emitter.emitForInitializer(
2458         GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
2459   } else {
2460     // As a fallback, directly construct the constant.
2461     // FIXME: This may get padding wrong under esoteric struct layout rules.
2462     // MSVC appears to create a complete type 'struct __s_GUID' that it
2463     // presumably uses to represent these constants.
2464     MSGuidDecl::Parts Parts = GD->getParts();
2465     llvm::Constant *Fields[4] = {
2466         llvm::ConstantInt::get(Int32Ty, Parts.Part1),
2467         llvm::ConstantInt::get(Int16Ty, Parts.Part2),
2468         llvm::ConstantInt::get(Int16Ty, Parts.Part3),
2469         llvm::ConstantDataArray::getRaw(
2470             StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8,
2471             Int8Ty)};
2472     Init = llvm::ConstantStruct::getAnon(Fields);
2473   }
2474 
2475   auto *GV = new llvm::GlobalVariable(
2476       getModule(), Init->getType(),
2477       /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
2478   if (supportsCOMDAT())
2479     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
2480   setDSOLocal(GV);
2481 
2482   llvm::Constant *Addr = GV;
2483   if (!V.isAbsent()) {
2484     Emitter.finalize(GV);
2485   } else {
2486     llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
2487     Addr = llvm::ConstantExpr::getBitCast(
2488         GV, Ty->getPointerTo(GV->getAddressSpace()));
2489   }
2490   return ConstantAddress(Addr, Alignment);
2491 }
2492 
2493 ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
2494   const AliasAttr *AA = VD->getAttr<AliasAttr>();
2495   assert(AA && "No alias?");
2496 
2497   CharUnits Alignment = getContext().getDeclAlign(VD);
2498   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
2499 
2500   // See if there is already something with the target's name in the module.
2501   llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
2502   if (Entry) {
2503     unsigned AS = getContext().getTargetAddressSpace(VD->getType());
2504     auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
2505     return ConstantAddress(Ptr, Alignment);
2506   }
2507 
2508   llvm::Constant *Aliasee;
2509   if (isa<llvm::FunctionType>(DeclTy))
2510     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
2511                                       GlobalDecl(cast<FunctionDecl>(VD)),
2512                                       /*ForVTable=*/false);
2513   else
2514     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
2515                                     llvm::PointerType::getUnqual(DeclTy),
2516                                     nullptr);
2517 
2518   auto *F = cast<llvm::GlobalValue>(Aliasee);
2519   F->setLinkage(llvm::Function::ExternalWeakLinkage);
2520   WeakRefReferences.insert(F);
2521 
2522   return ConstantAddress(Aliasee, Alignment);
2523 }
2524 
2525 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
2526   const auto *Global = cast<ValueDecl>(GD.getDecl());
2527 
2528   // Weak references don't produce any output by themselves.
2529   if (Global->hasAttr<WeakRefAttr>())
2530     return;
2531 
2532   // If this is an alias definition (which otherwise looks like a declaration)
2533   // emit it now.
2534   if (Global->hasAttr<AliasAttr>())
2535     return EmitAliasDefinition(GD);
2536 
2537   // IFunc like an alias whose value is resolved at runtime by calling resolver.
2538   if (Global->hasAttr<IFuncAttr>())
2539     return emitIFuncDefinition(GD);
2540 
2541   // If this is a cpu_dispatch multiversion function, emit the resolver.
2542   if (Global->hasAttr<CPUDispatchAttr>())
2543     return emitCPUDispatchDefinition(GD);
2544 
2545   // If this is CUDA, be selective about which declarations we emit.
2546   if (LangOpts.CUDA) {
2547     if (LangOpts.CUDAIsDevice) {
2548       if (!Global->hasAttr<CUDADeviceAttr>() &&
2549           !Global->hasAttr<CUDAGlobalAttr>() &&
2550           !Global->hasAttr<CUDAConstantAttr>() &&
2551           !Global->hasAttr<CUDASharedAttr>() &&
2552           !Global->getType()->isCUDADeviceBuiltinSurfaceType() &&
2553           !Global->getType()->isCUDADeviceBuiltinTextureType())
2554         return;
2555     } else {
2556       // We need to emit host-side 'shadows' for all global
2557       // device-side variables because the CUDA runtime needs their
2558       // size and host-side address in order to provide access to
2559       // their device-side incarnations.
2560 
2561       // So device-only functions are the only things we skip.
2562       if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() &&
2563           Global->hasAttr<CUDADeviceAttr>())
2564         return;
2565 
2566       assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
2567              "Expected Variable or Function");
2568     }
2569   }
2570 
2571   if (LangOpts.OpenMP) {
2572     // If this is OpenMP, check if it is legal to emit this global normally.
2573     if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
2574       return;
2575     if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
2576       if (MustBeEmitted(Global))
2577         EmitOMPDeclareReduction(DRD);
2578       return;
2579     } else if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
2580       if (MustBeEmitted(Global))
2581         EmitOMPDeclareMapper(DMD);
2582       return;
2583     }
2584   }
2585 
2586   // Ignore declarations, they will be emitted on their first use.
2587   if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
2588     // Forward declarations are emitted lazily on first use.
2589     if (!FD->doesThisDeclarationHaveABody()) {
2590       if (!FD->doesDeclarationForceExternallyVisibleDefinition())
2591         return;
2592 
2593       StringRef MangledName = getMangledName(GD);
2594 
2595       // Compute the function info and LLVM type.
2596       const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
2597       llvm::Type *Ty = getTypes().GetFunctionType(FI);
2598 
2599       GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
2600                               /*DontDefer=*/false);
2601       return;
2602     }
2603   } else {
2604     const auto *VD = cast<VarDecl>(Global);
2605     assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
2606     if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
2607         !Context.isMSStaticDataMemberInlineDefinition(VD)) {
2608       if (LangOpts.OpenMP) {
2609         // Emit declaration of the must-be-emitted declare target variable.
2610         if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2611                 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
2612           bool UnifiedMemoryEnabled =
2613               getOpenMPRuntime().hasRequiresUnifiedSharedMemory();
2614           if (*Res == OMPDeclareTargetDeclAttr::MT_To &&
2615               !UnifiedMemoryEnabled) {
2616             (void)GetAddrOfGlobalVar(VD);
2617           } else {
2618             assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
2619                     (*Res == OMPDeclareTargetDeclAttr::MT_To &&
2620                      UnifiedMemoryEnabled)) &&
2621                    "Link clause or to clause with unified memory expected.");
2622             (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
2623           }
2624 
2625           return;
2626         }
2627       }
2628       // If this declaration may have caused an inline variable definition to
2629       // change linkage, make sure that it's emitted.
2630       if (Context.getInlineVariableDefinitionKind(VD) ==
2631           ASTContext::InlineVariableDefinitionKind::Strong)
2632         GetAddrOfGlobalVar(VD);
2633       return;
2634     }
2635   }
2636 
2637   // Defer code generation to first use when possible, e.g. if this is an inline
2638   // function. If the global must always be emitted, do it eagerly if possible
2639   // to benefit from cache locality.
2640   if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
2641     // Emit the definition if it can't be deferred.
2642     EmitGlobalDefinition(GD);
2643     return;
2644   }
2645 
2646   // If we're deferring emission of a C++ variable with an
2647   // initializer, remember the order in which it appeared in the file.
2648   if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
2649       cast<VarDecl>(Global)->hasInit()) {
2650     DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
2651     CXXGlobalInits.push_back(nullptr);
2652   }
2653 
2654   StringRef MangledName = getMangledName(GD);
2655   if (GetGlobalValue(MangledName) != nullptr) {
2656     // The value has already been used and should therefore be emitted.
2657     addDeferredDeclToEmit(GD);
2658   } else if (MustBeEmitted(Global)) {
2659     // The value must be emitted, but cannot be emitted eagerly.
2660     assert(!MayBeEmittedEagerly(Global));
2661     addDeferredDeclToEmit(GD);
2662   } else {
2663     // Otherwise, remember that we saw a deferred decl with this name.  The
2664     // first use of the mangled name will cause it to move into
2665     // DeferredDeclsToEmit.
2666     DeferredDecls[MangledName] = GD;
2667   }
2668 }
2669 
2670 // Check if T is a class type with a destructor that's not dllimport.
2671 static bool HasNonDllImportDtor(QualType T) {
2672   if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
2673     if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
2674       if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
2675         return true;
2676 
2677   return false;
2678 }
2679 
2680 namespace {
2681   struct FunctionIsDirectlyRecursive
2682       : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
2683     const StringRef Name;
2684     const Builtin::Context &BI;
2685     FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
2686         : Name(N), BI(C) {}
2687 
2688     bool VisitCallExpr(const CallExpr *E) {
2689       const FunctionDecl *FD = E->getDirectCallee();
2690       if (!FD)
2691         return false;
2692       AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
2693       if (Attr && Name == Attr->getLabel())
2694         return true;
2695       unsigned BuiltinID = FD->getBuiltinID();
2696       if (!BuiltinID || !BI.isLibFunction(BuiltinID))
2697         return false;
2698       StringRef BuiltinName = BI.getName(BuiltinID);
2699       if (BuiltinName.startswith("__builtin_") &&
2700           Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
2701         return true;
2702       }
2703       return false;
2704     }
2705 
2706     bool VisitStmt(const Stmt *S) {
2707       for (const Stmt *Child : S->children())
2708         if (Child && this->Visit(Child))
2709           return true;
2710       return false;
2711     }
2712   };
2713 
2714   // Make sure we're not referencing non-imported vars or functions.
2715   struct DLLImportFunctionVisitor
2716       : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
2717     bool SafeToInline = true;
2718 
2719     bool shouldVisitImplicitCode() const { return true; }
2720 
2721     bool VisitVarDecl(VarDecl *VD) {
2722       if (VD->getTLSKind()) {
2723         // A thread-local variable cannot be imported.
2724         SafeToInline = false;
2725         return SafeToInline;
2726       }
2727 
2728       // A variable definition might imply a destructor call.
2729       if (VD->isThisDeclarationADefinition())
2730         SafeToInline = !HasNonDllImportDtor(VD->getType());
2731 
2732       return SafeToInline;
2733     }
2734 
2735     bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2736       if (const auto *D = E->getTemporary()->getDestructor())
2737         SafeToInline = D->hasAttr<DLLImportAttr>();
2738       return SafeToInline;
2739     }
2740 
2741     bool VisitDeclRefExpr(DeclRefExpr *E) {
2742       ValueDecl *VD = E->getDecl();
2743       if (isa<FunctionDecl>(VD))
2744         SafeToInline = VD->hasAttr<DLLImportAttr>();
2745       else if (VarDecl *V = dyn_cast<VarDecl>(VD))
2746         SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
2747       return SafeToInline;
2748     }
2749 
2750     bool VisitCXXConstructExpr(CXXConstructExpr *E) {
2751       SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
2752       return SafeToInline;
2753     }
2754 
2755     bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
2756       CXXMethodDecl *M = E->getMethodDecl();
2757       if (!M) {
2758         // Call through a pointer to member function. This is safe to inline.
2759         SafeToInline = true;
2760       } else {
2761         SafeToInline = M->hasAttr<DLLImportAttr>();
2762       }
2763       return SafeToInline;
2764     }
2765 
2766     bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2767       SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
2768       return SafeToInline;
2769     }
2770 
2771     bool VisitCXXNewExpr(CXXNewExpr *E) {
2772       SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
2773       return SafeToInline;
2774     }
2775   };
2776 }
2777 
2778 // isTriviallyRecursive - Check if this function calls another
2779 // decl that, because of the asm attribute or the other decl being a builtin,
2780 // ends up pointing to itself.
2781 bool
2782 CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
2783   StringRef Name;
2784   if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
2785     // asm labels are a special kind of mangling we have to support.
2786     AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
2787     if (!Attr)
2788       return false;
2789     Name = Attr->getLabel();
2790   } else {
2791     Name = FD->getName();
2792   }
2793 
2794   FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
2795   const Stmt *Body = FD->getBody();
2796   return Body ? Walker.Visit(Body) : false;
2797 }
2798 
2799 bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
2800   if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
2801     return true;
2802   const auto *F = cast<FunctionDecl>(GD.getDecl());
2803   if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
2804     return false;
2805 
2806   if (F->hasAttr<DLLImportAttr>()) {
2807     // Check whether it would be safe to inline this dllimport function.
2808     DLLImportFunctionVisitor Visitor;
2809     Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
2810     if (!Visitor.SafeToInline)
2811       return false;
2812 
2813     if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
2814       // Implicit destructor invocations aren't captured in the AST, so the
2815       // check above can't see them. Check for them manually here.
2816       for (const Decl *Member : Dtor->getParent()->decls())
2817         if (isa<FieldDecl>(Member))
2818           if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
2819             return false;
2820       for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
2821         if (HasNonDllImportDtor(B.getType()))
2822           return false;
2823     }
2824   }
2825 
2826   // PR9614. Avoid cases where the source code is lying to us. An available
2827   // externally function should have an equivalent function somewhere else,
2828   // but a function that calls itself through asm label/`__builtin_` trickery is
2829   // clearly not equivalent to the real implementation.
2830   // This happens in glibc's btowc and in some configure checks.
2831   return !isTriviallyRecursive(F);
2832 }
2833 
2834 bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
2835   return CodeGenOpts.OptimizationLevel > 0;
2836 }
2837 
2838 void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
2839                                                        llvm::GlobalValue *GV) {
2840   const auto *FD = cast<FunctionDecl>(GD.getDecl());
2841 
2842   if (FD->isCPUSpecificMultiVersion()) {
2843     auto *Spec = FD->getAttr<CPUSpecificAttr>();
2844     for (unsigned I = 0; I < Spec->cpus_size(); ++I)
2845       EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
2846     // Requires multiple emits.
2847   } else
2848     EmitGlobalFunctionDefinition(GD, GV);
2849 }
2850 
2851 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
2852   const auto *D = cast<ValueDecl>(GD.getDecl());
2853 
2854   PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
2855                                  Context.getSourceManager(),
2856                                  "Generating code for declaration");
2857 
2858   if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2859     // At -O0, don't generate IR for functions with available_externally
2860     // linkage.
2861     if (!shouldEmitFunction(GD))
2862       return;
2863 
2864     llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
2865       std::string Name;
2866       llvm::raw_string_ostream OS(Name);
2867       FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
2868                                /*Qualified=*/true);
2869       return Name;
2870     });
2871 
2872     if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
2873       // Make sure to emit the definition(s) before we emit the thunks.
2874       // This is necessary for the generation of certain thunks.
2875       if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
2876         ABI->emitCXXStructor(GD);
2877       else if (FD->isMultiVersion())
2878         EmitMultiVersionFunctionDefinition(GD, GV);
2879       else
2880         EmitGlobalFunctionDefinition(GD, GV);
2881 
2882       if (Method->isVirtual())
2883         getVTables().EmitThunks(GD);
2884 
2885       return;
2886     }
2887 
2888     if (FD->isMultiVersion())
2889       return EmitMultiVersionFunctionDefinition(GD, GV);
2890     return EmitGlobalFunctionDefinition(GD, GV);
2891   }
2892 
2893   if (const auto *VD = dyn_cast<VarDecl>(D))
2894     return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
2895 
2896   llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
2897 }
2898 
2899 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
2900                                                       llvm::Function *NewFn);
2901 
2902 static unsigned
2903 TargetMVPriority(const TargetInfo &TI,
2904                  const CodeGenFunction::MultiVersionResolverOption &RO) {
2905   unsigned Priority = 0;
2906   for (StringRef Feat : RO.Conditions.Features)
2907     Priority = std::max(Priority, TI.multiVersionSortPriority(Feat));
2908 
2909   if (!RO.Conditions.Architecture.empty())
2910     Priority = std::max(
2911         Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture));
2912   return Priority;
2913 }
2914 
2915 void CodeGenModule::emitMultiVersionFunctions() {
2916   for (GlobalDecl GD : MultiVersionFuncs) {
2917     SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
2918     const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2919     getContext().forEachMultiversionedFunctionVersion(
2920         FD, [this, &GD, &Options](const FunctionDecl *CurFD) {
2921           GlobalDecl CurGD{
2922               (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)};
2923           StringRef MangledName = getMangledName(CurGD);
2924           llvm::Constant *Func = GetGlobalValue(MangledName);
2925           if (!Func) {
2926             if (CurFD->isDefined()) {
2927               EmitGlobalFunctionDefinition(CurGD, nullptr);
2928               Func = GetGlobalValue(MangledName);
2929             } else {
2930               const CGFunctionInfo &FI =
2931                   getTypes().arrangeGlobalDeclaration(GD);
2932               llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
2933               Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
2934                                        /*DontDefer=*/false, ForDefinition);
2935             }
2936             assert(Func && "This should have just been created");
2937           }
2938 
2939           const auto *TA = CurFD->getAttr<TargetAttr>();
2940           llvm::SmallVector<StringRef, 8> Feats;
2941           TA->getAddedFeatures(Feats);
2942 
2943           Options.emplace_back(cast<llvm::Function>(Func),
2944                                TA->getArchitecture(), Feats);
2945         });
2946 
2947     llvm::Function *ResolverFunc;
2948     const TargetInfo &TI = getTarget();
2949 
2950     if (TI.supportsIFunc() || FD->isTargetMultiVersion()) {
2951       ResolverFunc = cast<llvm::Function>(
2952           GetGlobalValue((getMangledName(GD) + ".resolver").str()));
2953       ResolverFunc->setLinkage(llvm::Function::WeakODRLinkage);
2954     } else {
2955       ResolverFunc = cast<llvm::Function>(GetGlobalValue(getMangledName(GD)));
2956     }
2957 
2958     if (supportsCOMDAT())
2959       ResolverFunc->setComdat(
2960           getModule().getOrInsertComdat(ResolverFunc->getName()));
2961 
2962     llvm::stable_sort(
2963         Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS,
2964                        const CodeGenFunction::MultiVersionResolverOption &RHS) {
2965           return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS);
2966         });
2967     CodeGenFunction CGF(*this);
2968     CGF.EmitMultiVersionResolver(ResolverFunc, Options);
2969   }
2970 }
2971 
2972 void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
2973   const auto *FD = cast<FunctionDecl>(GD.getDecl());
2974   assert(FD && "Not a FunctionDecl?");
2975   const auto *DD = FD->getAttr<CPUDispatchAttr>();
2976   assert(DD && "Not a cpu_dispatch Function?");
2977   llvm::Type *DeclTy = getTypes().ConvertType(FD->getType());
2978 
2979   if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
2980     const CGFunctionInfo &FInfo = getTypes().arrangeCXXMethodDeclaration(CXXFD);
2981     DeclTy = getTypes().GetFunctionType(FInfo);
2982   }
2983 
2984   StringRef ResolverName = getMangledName(GD);
2985 
2986   llvm::Type *ResolverType;
2987   GlobalDecl ResolverGD;
2988   if (getTarget().supportsIFunc())
2989     ResolverType = llvm::FunctionType::get(
2990         llvm::PointerType::get(DeclTy,
2991                                Context.getTargetAddressSpace(FD->getType())),
2992         false);
2993   else {
2994     ResolverType = DeclTy;
2995     ResolverGD = GD;
2996   }
2997 
2998   auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
2999       ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
3000   ResolverFunc->setLinkage(llvm::Function::WeakODRLinkage);
3001   if (supportsCOMDAT())
3002     ResolverFunc->setComdat(
3003         getModule().getOrInsertComdat(ResolverFunc->getName()));
3004 
3005   SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
3006   const TargetInfo &Target = getTarget();
3007   unsigned Index = 0;
3008   for (const IdentifierInfo *II : DD->cpus()) {
3009     // Get the name of the target function so we can look it up/create it.
3010     std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
3011                               getCPUSpecificMangling(*this, II->getName());
3012 
3013     llvm::Constant *Func = GetGlobalValue(MangledName);
3014 
3015     if (!Func) {
3016       GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
3017       if (ExistingDecl.getDecl() &&
3018           ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
3019         EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
3020         Func = GetGlobalValue(MangledName);
3021       } else {
3022         if (!ExistingDecl.getDecl())
3023           ExistingDecl = GD.getWithMultiVersionIndex(Index);
3024 
3025       Func = GetOrCreateLLVMFunction(
3026           MangledName, DeclTy, ExistingDecl,
3027           /*ForVTable=*/false, /*DontDefer=*/true,
3028           /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
3029       }
3030     }
3031 
3032     llvm::SmallVector<StringRef, 32> Features;
3033     Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
3034     llvm::transform(Features, Features.begin(),
3035                     [](StringRef Str) { return Str.substr(1); });
3036     Features.erase(std::remove_if(
3037         Features.begin(), Features.end(), [&Target](StringRef Feat) {
3038           return !Target.validateCpuSupports(Feat);
3039         }), Features.end());
3040     Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
3041     ++Index;
3042   }
3043 
3044   llvm::sort(
3045       Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS,
3046                   const CodeGenFunction::MultiVersionResolverOption &RHS) {
3047         return CodeGenFunction::GetX86CpuSupportsMask(LHS.Conditions.Features) >
3048                CodeGenFunction::GetX86CpuSupportsMask(RHS.Conditions.Features);
3049       });
3050 
3051   // If the list contains multiple 'default' versions, such as when it contains
3052   // 'pentium' and 'generic', don't emit the call to the generic one (since we
3053   // always run on at least a 'pentium'). We do this by deleting the 'least
3054   // advanced' (read, lowest mangling letter).
3055   while (Options.size() > 1 &&
3056          CodeGenFunction::GetX86CpuSupportsMask(
3057              (Options.end() - 2)->Conditions.Features) == 0) {
3058     StringRef LHSName = (Options.end() - 2)->Function->getName();
3059     StringRef RHSName = (Options.end() - 1)->Function->getName();
3060     if (LHSName.compare(RHSName) < 0)
3061       Options.erase(Options.end() - 2);
3062     else
3063       Options.erase(Options.end() - 1);
3064   }
3065 
3066   CodeGenFunction CGF(*this);
3067   CGF.EmitMultiVersionResolver(ResolverFunc, Options);
3068 
3069   if (getTarget().supportsIFunc()) {
3070     std::string AliasName = getMangledNameImpl(
3071         *this, GD, FD, /*OmitMultiVersionMangling=*/true);
3072     llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
3073     if (!AliasFunc) {
3074       auto *IFunc = cast<llvm::GlobalIFunc>(GetOrCreateLLVMFunction(
3075           AliasName, DeclTy, GD, /*ForVTable=*/false, /*DontDefer=*/true,
3076           /*IsThunk=*/false, llvm::AttributeList(), NotForDefinition));
3077       auto *GA = llvm::GlobalAlias::create(
3078          DeclTy, 0, getFunctionLinkage(GD), AliasName, IFunc, &getModule());
3079       GA->setLinkage(llvm::Function::WeakODRLinkage);
3080       SetCommonAttributes(GD, GA);
3081     }
3082   }
3083 }
3084 
3085 /// If a dispatcher for the specified mangled name is not in the module, create
3086 /// and return an llvm Function with the specified type.
3087 llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(
3088     GlobalDecl GD, llvm::Type *DeclTy, const FunctionDecl *FD) {
3089   std::string MangledName =
3090       getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
3091 
3092   // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
3093   // a separate resolver).
3094   std::string ResolverName = MangledName;
3095   if (getTarget().supportsIFunc())
3096     ResolverName += ".ifunc";
3097   else if (FD->isTargetMultiVersion())
3098     ResolverName += ".resolver";
3099 
3100   // If this already exists, just return that one.
3101   if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName))
3102     return ResolverGV;
3103 
3104   // Since this is the first time we've created this IFunc, make sure
3105   // that we put this multiversioned function into the list to be
3106   // replaced later if necessary (target multiversioning only).
3107   if (!FD->isCPUDispatchMultiVersion() && !FD->isCPUSpecificMultiVersion())
3108     MultiVersionFuncs.push_back(GD);
3109 
3110   if (getTarget().supportsIFunc()) {
3111     llvm::Type *ResolverType = llvm::FunctionType::get(
3112         llvm::PointerType::get(
3113             DeclTy, getContext().getTargetAddressSpace(FD->getType())),
3114         false);
3115     llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3116         MangledName + ".resolver", ResolverType, GlobalDecl{},
3117         /*ForVTable=*/false);
3118     llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create(
3119         DeclTy, 0, llvm::Function::WeakODRLinkage, "", Resolver, &getModule());
3120     GIF->setName(ResolverName);
3121     SetCommonAttributes(FD, GIF);
3122 
3123     return GIF;
3124   }
3125 
3126   llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3127       ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
3128   assert(isa<llvm::GlobalValue>(Resolver) &&
3129          "Resolver should be created for the first time");
3130   SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
3131   return Resolver;
3132 }
3133 
3134 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
3135 /// module, create and return an llvm Function with the specified type. If there
3136 /// is something in the module with the specified name, return it potentially
3137 /// bitcasted to the right type.
3138 ///
3139 /// If D is non-null, it specifies a decl that correspond to this.  This is used
3140 /// to set the attributes on the function when it is first created.
3141 llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
3142     StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
3143     bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
3144     ForDefinition_t IsForDefinition) {
3145   const Decl *D = GD.getDecl();
3146 
3147   // Any attempts to use a MultiVersion function should result in retrieving
3148   // the iFunc instead. Name Mangling will handle the rest of the changes.
3149   if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
3150     // For the device mark the function as one that should be emitted.
3151     if (getLangOpts().OpenMPIsDevice && OpenMPRuntime &&
3152         !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
3153         !DontDefer && !IsForDefinition) {
3154       if (const FunctionDecl *FDDef = FD->getDefinition()) {
3155         GlobalDecl GDDef;
3156         if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
3157           GDDef = GlobalDecl(CD, GD.getCtorType());
3158         else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
3159           GDDef = GlobalDecl(DD, GD.getDtorType());
3160         else
3161           GDDef = GlobalDecl(FDDef);
3162         EmitGlobal(GDDef);
3163       }
3164     }
3165 
3166     if (FD->isMultiVersion()) {
3167       if (FD->hasAttr<TargetAttr>())
3168         UpdateMultiVersionNames(GD, FD);
3169       if (!IsForDefinition)
3170         return GetOrCreateMultiVersionResolver(GD, Ty, FD);
3171     }
3172   }
3173 
3174   // Lookup the entry, lazily creating it if necessary.
3175   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3176   if (Entry) {
3177     if (WeakRefReferences.erase(Entry)) {
3178       const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
3179       if (FD && !FD->hasAttr<WeakAttr>())
3180         Entry->setLinkage(llvm::Function::ExternalLinkage);
3181     }
3182 
3183     // Handle dropped DLL attributes.
3184     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) {
3185       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
3186       setDSOLocal(Entry);
3187     }
3188 
3189     // If there are two attempts to define the same mangled name, issue an
3190     // error.
3191     if (IsForDefinition && !Entry->isDeclaration()) {
3192       GlobalDecl OtherGD;
3193       // Check that GD is not yet in DiagnosedConflictingDefinitions is required
3194       // to make sure that we issue an error only once.
3195       if (lookupRepresentativeDecl(MangledName, OtherGD) &&
3196           (GD.getCanonicalDecl().getDecl() !=
3197            OtherGD.getCanonicalDecl().getDecl()) &&
3198           DiagnosedConflictingDefinitions.insert(GD).second) {
3199         getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
3200             << MangledName;
3201         getDiags().Report(OtherGD.getDecl()->getLocation(),
3202                           diag::note_previous_definition);
3203       }
3204     }
3205 
3206     if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
3207         (Entry->getValueType() == Ty)) {
3208       return Entry;
3209     }
3210 
3211     // Make sure the result is of the correct type.
3212     // (If function is requested for a definition, we always need to create a new
3213     // function, not just return a bitcast.)
3214     if (!IsForDefinition)
3215       return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
3216   }
3217 
3218   // This function doesn't have a complete type (for example, the return
3219   // type is an incomplete struct). Use a fake type instead, and make
3220   // sure not to try to set attributes.
3221   bool IsIncompleteFunction = false;
3222 
3223   llvm::FunctionType *FTy;
3224   if (isa<llvm::FunctionType>(Ty)) {
3225     FTy = cast<llvm::FunctionType>(Ty);
3226   } else {
3227     FTy = llvm::FunctionType::get(VoidTy, false);
3228     IsIncompleteFunction = true;
3229   }
3230 
3231   llvm::Function *F =
3232       llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
3233                              Entry ? StringRef() : MangledName, &getModule());
3234 
3235   // If we already created a function with the same mangled name (but different
3236   // type) before, take its name and add it to the list of functions to be
3237   // replaced with F at the end of CodeGen.
3238   //
3239   // This happens if there is a prototype for a function (e.g. "int f()") and
3240   // then a definition of a different type (e.g. "int f(int x)").
3241   if (Entry) {
3242     F->takeName(Entry);
3243 
3244     // This might be an implementation of a function without a prototype, in
3245     // which case, try to do special replacement of calls which match the new
3246     // prototype.  The really key thing here is that we also potentially drop
3247     // arguments from the call site so as to make a direct call, which makes the
3248     // inliner happier and suppresses a number of optimizer warnings (!) about
3249     // dropping arguments.
3250     if (!Entry->use_empty()) {
3251       ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F);
3252       Entry->removeDeadConstantUsers();
3253     }
3254 
3255     llvm::Constant *BC = llvm::ConstantExpr::getBitCast(
3256         F, Entry->getValueType()->getPointerTo());
3257     addGlobalValReplacement(Entry, BC);
3258   }
3259 
3260   assert(F->getName() == MangledName && "name was uniqued!");
3261   if (D)
3262     SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
3263   if (ExtraAttrs.hasAttributes(llvm::AttributeList::FunctionIndex)) {
3264     llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex);
3265     F->addAttributes(llvm::AttributeList::FunctionIndex, B);
3266   }
3267 
3268   if (!DontDefer) {
3269     // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
3270     // each other bottoming out with the base dtor.  Therefore we emit non-base
3271     // dtors on usage, even if there is no dtor definition in the TU.
3272     if (D && isa<CXXDestructorDecl>(D) &&
3273         getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
3274                                            GD.getDtorType()))
3275       addDeferredDeclToEmit(GD);
3276 
3277     // This is the first use or definition of a mangled name.  If there is a
3278     // deferred decl with this name, remember that we need to emit it at the end
3279     // of the file.
3280     auto DDI = DeferredDecls.find(MangledName);
3281     if (DDI != DeferredDecls.end()) {
3282       // Move the potentially referenced deferred decl to the
3283       // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
3284       // don't need it anymore).
3285       addDeferredDeclToEmit(DDI->second);
3286       DeferredDecls.erase(DDI);
3287 
3288       // Otherwise, there are cases we have to worry about where we're
3289       // using a declaration for which we must emit a definition but where
3290       // we might not find a top-level definition:
3291       //   - member functions defined inline in their classes
3292       //   - friend functions defined inline in some class
3293       //   - special member functions with implicit definitions
3294       // If we ever change our AST traversal to walk into class methods,
3295       // this will be unnecessary.
3296       //
3297       // We also don't emit a definition for a function if it's going to be an
3298       // entry in a vtable, unless it's already marked as used.
3299     } else if (getLangOpts().CPlusPlus && D) {
3300       // Look for a declaration that's lexically in a record.
3301       for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
3302            FD = FD->getPreviousDecl()) {
3303         if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
3304           if (FD->doesThisDeclarationHaveABody()) {
3305             addDeferredDeclToEmit(GD.getWithDecl(FD));
3306             break;
3307           }
3308         }
3309       }
3310     }
3311   }
3312 
3313   // Make sure the result is of the requested type.
3314   if (!IsIncompleteFunction) {
3315     assert(F->getFunctionType() == Ty);
3316     return F;
3317   }
3318 
3319   llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
3320   return llvm::ConstantExpr::getBitCast(F, PTy);
3321 }
3322 
3323 /// GetAddrOfFunction - Return the address of the given function.  If Ty is
3324 /// non-null, then this function will use the specified type if it has to
3325 /// create it (this occurs when we see a definition of the function).
3326 llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
3327                                                  llvm::Type *Ty,
3328                                                  bool ForVTable,
3329                                                  bool DontDefer,
3330                                               ForDefinition_t IsForDefinition) {
3331   assert(!cast<FunctionDecl>(GD.getDecl())->isConsteval() &&
3332          "consteval function should never be emitted");
3333   // If there was no specific requested type, just convert it now.
3334   if (!Ty) {
3335     const auto *FD = cast<FunctionDecl>(GD.getDecl());
3336     Ty = getTypes().ConvertType(FD->getType());
3337   }
3338 
3339   // Devirtualized destructor calls may come through here instead of via
3340   // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
3341   // of the complete destructor when necessary.
3342   if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
3343     if (getTarget().getCXXABI().isMicrosoft() &&
3344         GD.getDtorType() == Dtor_Complete &&
3345         DD->getParent()->getNumVBases() == 0)
3346       GD = GlobalDecl(DD, Dtor_Base);
3347   }
3348 
3349   StringRef MangledName = getMangledName(GD);
3350   return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
3351                                  /*IsThunk=*/false, llvm::AttributeList(),
3352                                  IsForDefinition);
3353 }
3354 
3355 static const FunctionDecl *
3356 GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
3357   TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
3358   DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
3359 
3360   IdentifierInfo &CII = C.Idents.get(Name);
3361   for (const auto &Result : DC->lookup(&CII))
3362     if (const auto FD = dyn_cast<FunctionDecl>(Result))
3363       return FD;
3364 
3365   if (!C.getLangOpts().CPlusPlus)
3366     return nullptr;
3367 
3368   // Demangle the premangled name from getTerminateFn()
3369   IdentifierInfo &CXXII =
3370       (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ")
3371           ? C.Idents.get("terminate")
3372           : C.Idents.get(Name);
3373 
3374   for (const auto &N : {"__cxxabiv1", "std"}) {
3375     IdentifierInfo &NS = C.Idents.get(N);
3376     for (const auto &Result : DC->lookup(&NS)) {
3377       NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
3378       if (auto LSD = dyn_cast<LinkageSpecDecl>(Result))
3379         for (const auto &Result : LSD->lookup(&NS))
3380           if ((ND = dyn_cast<NamespaceDecl>(Result)))
3381             break;
3382 
3383       if (ND)
3384         for (const auto &Result : ND->lookup(&CXXII))
3385           if (const auto *FD = dyn_cast<FunctionDecl>(Result))
3386             return FD;
3387     }
3388   }
3389 
3390   return nullptr;
3391 }
3392 
3393 /// CreateRuntimeFunction - Create a new runtime function with the specified
3394 /// type and name.
3395 llvm::FunctionCallee
3396 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
3397                                      llvm::AttributeList ExtraAttrs, bool Local,
3398                                      bool AssumeConvergent) {
3399   if (AssumeConvergent) {
3400     ExtraAttrs =
3401         ExtraAttrs.addAttribute(VMContext, llvm::AttributeList::FunctionIndex,
3402                                 llvm::Attribute::Convergent);
3403   }
3404 
3405   llvm::Constant *C =
3406       GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
3407                               /*DontDefer=*/false, /*IsThunk=*/false,
3408                               ExtraAttrs);
3409 
3410   if (auto *F = dyn_cast<llvm::Function>(C)) {
3411     if (F->empty()) {
3412       F->setCallingConv(getRuntimeCC());
3413 
3414       // In Windows Itanium environments, try to mark runtime functions
3415       // dllimport. For Mingw and MSVC, don't. We don't really know if the user
3416       // will link their standard library statically or dynamically. Marking
3417       // functions imported when they are not imported can cause linker errors
3418       // and warnings.
3419       if (!Local && getTriple().isWindowsItaniumEnvironment() &&
3420           !getCodeGenOpts().LTOVisibilityPublicStd) {
3421         const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
3422         if (!FD || FD->hasAttr<DLLImportAttr>()) {
3423           F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
3424           F->setLinkage(llvm::GlobalValue::ExternalLinkage);
3425         }
3426       }
3427       setDSOLocal(F);
3428     }
3429   }
3430 
3431   return {FTy, C};
3432 }
3433 
3434 /// isTypeConstant - Determine whether an object of this type can be emitted
3435 /// as a constant.
3436 ///
3437 /// If ExcludeCtor is true, the duration when the object's constructor runs
3438 /// will not be considered. The caller will need to verify that the object is
3439 /// not written to during its construction.
3440 bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
3441   if (!Ty.isConstant(Context) && !Ty->isReferenceType())
3442     return false;
3443 
3444   if (Context.getLangOpts().CPlusPlus) {
3445     if (const CXXRecordDecl *Record
3446           = Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
3447       return ExcludeCtor && !Record->hasMutableFields() &&
3448              Record->hasTrivialDestructor();
3449   }
3450 
3451   return true;
3452 }
3453 
3454 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
3455 /// create and return an llvm GlobalVariable with the specified type.  If there
3456 /// is something in the module with the specified name, return it potentially
3457 /// bitcasted to the right type.
3458 ///
3459 /// If D is non-null, it specifies a decl that correspond to this.  This is used
3460 /// to set the attributes on the global when it is first created.
3461 ///
3462 /// If IsForDefinition is true, it is guaranteed that an actual global with
3463 /// type Ty will be returned, not conversion of a variable with the same
3464 /// mangled name but some other type.
3465 llvm::Constant *
3466 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
3467                                      llvm::PointerType *Ty,
3468                                      const VarDecl *D,
3469                                      ForDefinition_t IsForDefinition) {
3470   // Lookup the entry, lazily creating it if necessary.
3471   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3472   if (Entry) {
3473     if (WeakRefReferences.erase(Entry)) {
3474       if (D && !D->hasAttr<WeakAttr>())
3475         Entry->setLinkage(llvm::Function::ExternalLinkage);
3476     }
3477 
3478     // Handle dropped DLL attributes.
3479     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
3480       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
3481 
3482     if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
3483       getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
3484 
3485     if (Entry->getType() == Ty)
3486       return Entry;
3487 
3488     // If there are two attempts to define the same mangled name, issue an
3489     // error.
3490     if (IsForDefinition && !Entry->isDeclaration()) {
3491       GlobalDecl OtherGD;
3492       const VarDecl *OtherD;
3493 
3494       // Check that D is not yet in DiagnosedConflictingDefinitions is required
3495       // to make sure that we issue an error only once.
3496       if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
3497           (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
3498           (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
3499           OtherD->hasInit() &&
3500           DiagnosedConflictingDefinitions.insert(D).second) {
3501         getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
3502             << MangledName;
3503         getDiags().Report(OtherGD.getDecl()->getLocation(),
3504                           diag::note_previous_definition);
3505       }
3506     }
3507 
3508     // Make sure the result is of the correct type.
3509     if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace())
3510       return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty);
3511 
3512     // (If global is requested for a definition, we always need to create a new
3513     // global, not just return a bitcast.)
3514     if (!IsForDefinition)
3515       return llvm::ConstantExpr::getBitCast(Entry, Ty);
3516   }
3517 
3518   auto AddrSpace = GetGlobalVarAddressSpace(D);
3519   auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace);
3520 
3521   auto *GV = new llvm::GlobalVariable(
3522       getModule(), Ty->getElementType(), false,
3523       llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr,
3524       llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace);
3525 
3526   // If we already created a global with the same mangled name (but different
3527   // type) before, take its name and remove it from its parent.
3528   if (Entry) {
3529     GV->takeName(Entry);
3530 
3531     if (!Entry->use_empty()) {
3532       llvm::Constant *NewPtrForOldDecl =
3533           llvm::ConstantExpr::getBitCast(GV, Entry->getType());
3534       Entry->replaceAllUsesWith(NewPtrForOldDecl);
3535     }
3536 
3537     Entry->eraseFromParent();
3538   }
3539 
3540   // This is the first use or definition of a mangled name.  If there is a
3541   // deferred decl with this name, remember that we need to emit it at the end
3542   // of the file.
3543   auto DDI = DeferredDecls.find(MangledName);
3544   if (DDI != DeferredDecls.end()) {
3545     // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
3546     // list, and remove it from DeferredDecls (since we don't need it anymore).
3547     addDeferredDeclToEmit(DDI->second);
3548     DeferredDecls.erase(DDI);
3549   }
3550 
3551   // Handle things which are present even on external declarations.
3552   if (D) {
3553     if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
3554       getOpenMPRuntime().registerTargetGlobalVariable(D, GV);
3555 
3556     // FIXME: This code is overly simple and should be merged with other global
3557     // handling.
3558     GV->setConstant(isTypeConstant(D->getType(), false));
3559 
3560     GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
3561 
3562     setLinkageForGV(GV, D);
3563 
3564     if (D->getTLSKind()) {
3565       if (D->getTLSKind() == VarDecl::TLS_Dynamic)
3566         CXXThreadLocals.push_back(D);
3567       setTLSMode(GV, *D);
3568     }
3569 
3570     setGVProperties(GV, D);
3571 
3572     // If required by the ABI, treat declarations of static data members with
3573     // inline initializers as definitions.
3574     if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
3575       EmitGlobalVarDefinition(D);
3576     }
3577 
3578     // Emit section information for extern variables.
3579     if (D->hasExternalStorage()) {
3580       if (const SectionAttr *SA = D->getAttr<SectionAttr>())
3581         GV->setSection(SA->getName());
3582     }
3583 
3584     // Handle XCore specific ABI requirements.
3585     if (getTriple().getArch() == llvm::Triple::xcore &&
3586         D->getLanguageLinkage() == CLanguageLinkage &&
3587         D->getType().isConstant(Context) &&
3588         isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
3589       GV->setSection(".cp.rodata");
3590 
3591     // Check if we a have a const declaration with an initializer, we may be
3592     // able to emit it as available_externally to expose it's value to the
3593     // optimizer.
3594     if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
3595         D->getType().isConstQualified() && !GV->hasInitializer() &&
3596         !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
3597       const auto *Record =
3598           Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
3599       bool HasMutableFields = Record && Record->hasMutableFields();
3600       if (!HasMutableFields) {
3601         const VarDecl *InitDecl;
3602         const Expr *InitExpr = D->getAnyInitializer(InitDecl);
3603         if (InitExpr) {
3604           ConstantEmitter emitter(*this);
3605           llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
3606           if (Init) {
3607             auto *InitType = Init->getType();
3608             if (GV->getValueType() != InitType) {
3609               // The type of the initializer does not match the definition.
3610               // This happens when an initializer has a different type from
3611               // the type of the global (because of padding at the end of a
3612               // structure for instance).
3613               GV->setName(StringRef());
3614               // Make a new global with the correct type, this is now guaranteed
3615               // to work.
3616               auto *NewGV = cast<llvm::GlobalVariable>(
3617                   GetAddrOfGlobalVar(D, InitType, IsForDefinition)
3618                       ->stripPointerCasts());
3619 
3620               // Erase the old global, since it is no longer used.
3621               GV->eraseFromParent();
3622               GV = NewGV;
3623             } else {
3624               GV->setInitializer(Init);
3625               GV->setConstant(true);
3626               GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
3627             }
3628             emitter.finalize(GV);
3629           }
3630         }
3631       }
3632     }
3633   }
3634 
3635   if (GV->isDeclaration())
3636     getTargetCodeGenInfo().setTargetAttributes(D, GV, *this);
3637 
3638   LangAS ExpectedAS =
3639       D ? D->getType().getAddressSpace()
3640         : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
3641   assert(getContext().getTargetAddressSpace(ExpectedAS) ==
3642          Ty->getPointerAddressSpace());
3643   if (AddrSpace != ExpectedAS)
3644     return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace,
3645                                                        ExpectedAS, Ty);
3646 
3647   return GV;
3648 }
3649 
3650 llvm::Constant *
3651 CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition) {
3652   const Decl *D = GD.getDecl();
3653 
3654   if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
3655     return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
3656                                 /*DontDefer=*/false, IsForDefinition);
3657 
3658   if (isa<CXXMethodDecl>(D)) {
3659     auto FInfo =
3660         &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D));
3661     auto Ty = getTypes().GetFunctionType(*FInfo);
3662     return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
3663                              IsForDefinition);
3664   }
3665 
3666   if (isa<FunctionDecl>(D)) {
3667     const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
3668     llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
3669     return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
3670                              IsForDefinition);
3671   }
3672 
3673   return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition);
3674 }
3675 
3676 llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable(
3677     StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
3678     unsigned Alignment) {
3679   llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
3680   llvm::GlobalVariable *OldGV = nullptr;
3681 
3682   if (GV) {
3683     // Check if the variable has the right type.
3684     if (GV->getValueType() == Ty)
3685       return GV;
3686 
3687     // Because C++ name mangling, the only way we can end up with an already
3688     // existing global with the same name is if it has been declared extern "C".
3689     assert(GV->isDeclaration() && "Declaration has wrong type!");
3690     OldGV = GV;
3691   }
3692 
3693   // Create a new variable.
3694   GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
3695                                 Linkage, nullptr, Name);
3696 
3697   if (OldGV) {
3698     // Replace occurrences of the old variable if needed.
3699     GV->takeName(OldGV);
3700 
3701     if (!OldGV->use_empty()) {
3702       llvm::Constant *NewPtrForOldDecl =
3703       llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3704       OldGV->replaceAllUsesWith(NewPtrForOldDecl);
3705     }
3706 
3707     OldGV->eraseFromParent();
3708   }
3709 
3710   if (supportsCOMDAT() && GV->isWeakForLinker() &&
3711       !GV->hasAvailableExternallyLinkage())
3712     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3713 
3714   GV->setAlignment(llvm::MaybeAlign(Alignment));
3715 
3716   return GV;
3717 }
3718 
3719 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
3720 /// given global variable.  If Ty is non-null and if the global doesn't exist,
3721 /// then it will be created with the specified type instead of whatever the
3722 /// normal requested type would be. If IsForDefinition is true, it is guaranteed
3723 /// that an actual global with type Ty will be returned, not conversion of a
3724 /// variable with the same mangled name but some other type.
3725 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
3726                                                   llvm::Type *Ty,
3727                                            ForDefinition_t IsForDefinition) {
3728   assert(D->hasGlobalStorage() && "Not a global variable");
3729   QualType ASTTy = D->getType();
3730   if (!Ty)
3731     Ty = getTypes().ConvertTypeForMem(ASTTy);
3732 
3733   llvm::PointerType *PTy =
3734     llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
3735 
3736   StringRef MangledName = getMangledName(D);
3737   return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition);
3738 }
3739 
3740 /// CreateRuntimeVariable - Create a new runtime global variable with the
3741 /// specified type and name.
3742 llvm::Constant *
3743 CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
3744                                      StringRef Name) {
3745   auto PtrTy =
3746       getContext().getLangOpts().OpenCL
3747           ? llvm::PointerType::get(
3748                 Ty, getContext().getTargetAddressSpace(LangAS::opencl_global))
3749           : llvm::PointerType::getUnqual(Ty);
3750   auto *Ret = GetOrCreateLLVMGlobal(Name, PtrTy, nullptr);
3751   setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
3752   return Ret;
3753 }
3754 
3755 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
3756   assert(!D->getInit() && "Cannot emit definite definitions here!");
3757 
3758   StringRef MangledName = getMangledName(D);
3759   llvm::GlobalValue *GV = GetGlobalValue(MangledName);
3760 
3761   // We already have a definition, not declaration, with the same mangled name.
3762   // Emitting of declaration is not required (and actually overwrites emitted
3763   // definition).
3764   if (GV && !GV->isDeclaration())
3765     return;
3766 
3767   // If we have not seen a reference to this variable yet, place it into the
3768   // deferred declarations table to be emitted if needed later.
3769   if (!MustBeEmitted(D) && !GV) {
3770       DeferredDecls[MangledName] = D;
3771       return;
3772   }
3773 
3774   // The tentative definition is the only definition.
3775   EmitGlobalVarDefinition(D);
3776 }
3777 
3778 void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) {
3779   EmitExternalVarDeclaration(D);
3780 }
3781 
3782 CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
3783   return Context.toCharUnitsFromBits(
3784       getDataLayout().getTypeStoreSizeInBits(Ty));
3785 }
3786 
3787 LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
3788   LangAS AddrSpace = LangAS::Default;
3789   if (LangOpts.OpenCL) {
3790     AddrSpace = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
3791     assert(AddrSpace == LangAS::opencl_global ||
3792            AddrSpace == LangAS::opencl_constant ||
3793            AddrSpace == LangAS::opencl_local ||
3794            AddrSpace >= LangAS::FirstTargetAddressSpace);
3795     return AddrSpace;
3796   }
3797 
3798   if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
3799     if (D && D->hasAttr<CUDAConstantAttr>())
3800       return LangAS::cuda_constant;
3801     else if (D && D->hasAttr<CUDASharedAttr>())
3802       return LangAS::cuda_shared;
3803     else if (D && D->hasAttr<CUDADeviceAttr>())
3804       return LangAS::cuda_device;
3805     else if (D && D->getType().isConstQualified())
3806       return LangAS::cuda_constant;
3807     else
3808       return LangAS::cuda_device;
3809   }
3810 
3811   if (LangOpts.OpenMP) {
3812     LangAS AS;
3813     if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
3814       return AS;
3815   }
3816   return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
3817 }
3818 
3819 LangAS CodeGenModule::getStringLiteralAddressSpace() const {
3820   // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
3821   if (LangOpts.OpenCL)
3822     return LangAS::opencl_constant;
3823   if (auto AS = getTarget().getConstantAddressSpace())
3824     return AS.getValue();
3825   return LangAS::Default;
3826 }
3827 
3828 // In address space agnostic languages, string literals are in default address
3829 // space in AST. However, certain targets (e.g. amdgcn) request them to be
3830 // emitted in constant address space in LLVM IR. To be consistent with other
3831 // parts of AST, string literal global variables in constant address space
3832 // need to be casted to default address space before being put into address
3833 // map and referenced by other part of CodeGen.
3834 // In OpenCL, string literals are in constant address space in AST, therefore
3835 // they should not be casted to default address space.
3836 static llvm::Constant *
3837 castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM,
3838                                        llvm::GlobalVariable *GV) {
3839   llvm::Constant *Cast = GV;
3840   if (!CGM.getLangOpts().OpenCL) {
3841     if (auto AS = CGM.getTarget().getConstantAddressSpace()) {
3842       if (AS != LangAS::Default)
3843         Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast(
3844             CGM, GV, AS.getValue(), LangAS::Default,
3845             GV->getValueType()->getPointerTo(
3846                 CGM.getContext().getTargetAddressSpace(LangAS::Default)));
3847     }
3848   }
3849   return Cast;
3850 }
3851 
3852 template<typename SomeDecl>
3853 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
3854                                                llvm::GlobalValue *GV) {
3855   if (!getLangOpts().CPlusPlus)
3856     return;
3857 
3858   // Must have 'used' attribute, or else inline assembly can't rely on
3859   // the name existing.
3860   if (!D->template hasAttr<UsedAttr>())
3861     return;
3862 
3863   // Must have internal linkage and an ordinary name.
3864   if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
3865     return;
3866 
3867   // Must be in an extern "C" context. Entities declared directly within
3868   // a record are not extern "C" even if the record is in such a context.
3869   const SomeDecl *First = D->getFirstDecl();
3870   if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
3871     return;
3872 
3873   // OK, this is an internal linkage entity inside an extern "C" linkage
3874   // specification. Make a note of that so we can give it the "expected"
3875   // mangled name if nothing else is using that name.
3876   std::pair<StaticExternCMap::iterator, bool> R =
3877       StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
3878 
3879   // If we have multiple internal linkage entities with the same name
3880   // in extern "C" regions, none of them gets that name.
3881   if (!R.second)
3882     R.first->second = nullptr;
3883 }
3884 
3885 static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
3886   if (!CGM.supportsCOMDAT())
3887     return false;
3888 
3889   // Do not set COMDAT attribute for CUDA/HIP stub functions to prevent
3890   // them being "merged" by the COMDAT Folding linker optimization.
3891   if (D.hasAttr<CUDAGlobalAttr>())
3892     return false;
3893 
3894   if (D.hasAttr<SelectAnyAttr>())
3895     return true;
3896 
3897   GVALinkage Linkage;
3898   if (auto *VD = dyn_cast<VarDecl>(&D))
3899     Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
3900   else
3901     Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
3902 
3903   switch (Linkage) {
3904   case GVA_Internal:
3905   case GVA_AvailableExternally:
3906   case GVA_StrongExternal:
3907     return false;
3908   case GVA_DiscardableODR:
3909   case GVA_StrongODR:
3910     return true;
3911   }
3912   llvm_unreachable("No such linkage");
3913 }
3914 
3915 void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
3916                                           llvm::GlobalObject &GO) {
3917   if (!shouldBeInCOMDAT(*this, D))
3918     return;
3919   GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
3920 }
3921 
3922 /// Pass IsTentative as true if you want to create a tentative definition.
3923 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
3924                                             bool IsTentative) {
3925   // OpenCL global variables of sampler type are translated to function calls,
3926   // therefore no need to be translated.
3927   QualType ASTTy = D->getType();
3928   if (getLangOpts().OpenCL && ASTTy->isSamplerT())
3929     return;
3930 
3931   // If this is OpenMP device, check if it is legal to emit this global
3932   // normally.
3933   if (LangOpts.OpenMPIsDevice && OpenMPRuntime &&
3934       OpenMPRuntime->emitTargetGlobalVariable(D))
3935     return;
3936 
3937   llvm::Constant *Init = nullptr;
3938   bool NeedsGlobalCtor = false;
3939   bool NeedsGlobalDtor =
3940       D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
3941 
3942   const VarDecl *InitDecl;
3943   const Expr *InitExpr = D->getAnyInitializer(InitDecl);
3944 
3945   Optional<ConstantEmitter> emitter;
3946 
3947   // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
3948   // as part of their declaration."  Sema has already checked for
3949   // error cases, so we just need to set Init to UndefValue.
3950   bool IsCUDASharedVar =
3951       getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
3952   // Shadows of initialized device-side global variables are also left
3953   // undefined.
3954   bool IsCUDAShadowVar =
3955       !getLangOpts().CUDAIsDevice &&
3956       (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
3957        D->hasAttr<CUDASharedAttr>());
3958   bool IsCUDADeviceShadowVar =
3959       getLangOpts().CUDAIsDevice &&
3960       (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
3961        D->getType()->isCUDADeviceBuiltinTextureType());
3962   // HIP pinned shadow of initialized host-side global variables are also
3963   // left undefined.
3964   if (getLangOpts().CUDA &&
3965       (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
3966     Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
3967   else if (D->hasAttr<LoaderUninitializedAttr>())
3968     Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
3969   else if (!InitExpr) {
3970     // This is a tentative definition; tentative definitions are
3971     // implicitly initialized with { 0 }.
3972     //
3973     // Note that tentative definitions are only emitted at the end of
3974     // a translation unit, so they should never have incomplete
3975     // type. In addition, EmitTentativeDefinition makes sure that we
3976     // never attempt to emit a tentative definition if a real one
3977     // exists. A use may still exists, however, so we still may need
3978     // to do a RAUW.
3979     assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
3980     Init = EmitNullConstant(D->getType());
3981   } else {
3982     initializedGlobalDecl = GlobalDecl(D);
3983     emitter.emplace(*this);
3984     Init = emitter->tryEmitForInitializer(*InitDecl);
3985 
3986     if (!Init) {
3987       QualType T = InitExpr->getType();
3988       if (D->getType()->isReferenceType())
3989         T = D->getType();
3990 
3991       if (getLangOpts().CPlusPlus) {
3992         Init = EmitNullConstant(T);
3993         NeedsGlobalCtor = true;
3994       } else {
3995         ErrorUnsupported(D, "static initializer");
3996         Init = llvm::UndefValue::get(getTypes().ConvertType(T));
3997       }
3998     } else {
3999       // We don't need an initializer, so remove the entry for the delayed
4000       // initializer position (just in case this entry was delayed) if we
4001       // also don't need to register a destructor.
4002       if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
4003         DelayedCXXInitPosition.erase(D);
4004     }
4005   }
4006 
4007   llvm::Type* InitType = Init->getType();
4008   llvm::Constant *Entry =
4009       GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
4010 
4011   // Strip off pointer casts if we got them.
4012   Entry = Entry->stripPointerCasts();
4013 
4014   // Entry is now either a Function or GlobalVariable.
4015   auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
4016 
4017   // We have a definition after a declaration with the wrong type.
4018   // We must make a new GlobalVariable* and update everything that used OldGV
4019   // (a declaration or tentative definition) with the new GlobalVariable*
4020   // (which will be a definition).
4021   //
4022   // This happens if there is a prototype for a global (e.g.
4023   // "extern int x[];") and then a definition of a different type (e.g.
4024   // "int x[10];"). This also happens when an initializer has a different type
4025   // from the type of the global (this happens with unions).
4026   if (!GV || GV->getValueType() != InitType ||
4027       GV->getType()->getAddressSpace() !=
4028           getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) {
4029 
4030     // Move the old entry aside so that we'll create a new one.
4031     Entry->setName(StringRef());
4032 
4033     // Make a new global with the correct type, this is now guaranteed to work.
4034     GV = cast<llvm::GlobalVariable>(
4035         GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))
4036             ->stripPointerCasts());
4037 
4038     // Replace all uses of the old global with the new global
4039     llvm::Constant *NewPtrForOldDecl =
4040         llvm::ConstantExpr::getBitCast(GV, Entry->getType());
4041     Entry->replaceAllUsesWith(NewPtrForOldDecl);
4042 
4043     // Erase the old global, since it is no longer used.
4044     cast<llvm::GlobalValue>(Entry)->eraseFromParent();
4045   }
4046 
4047   MaybeHandleStaticInExternC(D, GV);
4048 
4049   if (D->hasAttr<AnnotateAttr>())
4050     AddGlobalAnnotations(D, GV);
4051 
4052   // Set the llvm linkage type as appropriate.
4053   llvm::GlobalValue::LinkageTypes Linkage =
4054       getLLVMLinkageVarDefinition(D, GV->isConstant());
4055 
4056   // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
4057   // the device. [...]"
4058   // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
4059   // __device__, declares a variable that: [...]
4060   // Is accessible from all the threads within the grid and from the host
4061   // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
4062   // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
4063   if (GV && LangOpts.CUDA) {
4064     if (LangOpts.CUDAIsDevice) {
4065       if (Linkage != llvm::GlobalValue::InternalLinkage &&
4066           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()))
4067         GV->setExternallyInitialized(true);
4068     } else {
4069       // Host-side shadows of external declarations of device-side
4070       // global variables become internal definitions. These have to
4071       // be internal in order to prevent name conflicts with global
4072       // host variables with the same name in a different TUs.
4073       if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) {
4074         Linkage = llvm::GlobalValue::InternalLinkage;
4075         // Shadow variables and their properties must be registered with CUDA
4076         // runtime. Skip Extern global variables, which will be registered in
4077         // the TU where they are defined.
4078         if (!D->hasExternalStorage())
4079           getCUDARuntime().registerDeviceVar(D, *GV, !D->hasDefinition(),
4080                                              D->hasAttr<CUDAConstantAttr>());
4081       } else if (D->hasAttr<CUDASharedAttr>()) {
4082         // __shared__ variables are odd. Shadows do get created, but
4083         // they are not registered with the CUDA runtime, so they
4084         // can't really be used to access their device-side
4085         // counterparts. It's not clear yet whether it's nvcc's bug or
4086         // a feature, but we've got to do the same for compatibility.
4087         Linkage = llvm::GlobalValue::InternalLinkage;
4088       } else if (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
4089                  D->getType()->isCUDADeviceBuiltinTextureType()) {
4090         // Builtin surfaces and textures and their template arguments are
4091         // also registered with CUDA runtime.
4092         Linkage = llvm::GlobalValue::InternalLinkage;
4093         const ClassTemplateSpecializationDecl *TD =
4094             cast<ClassTemplateSpecializationDecl>(
4095                 D->getType()->getAs<RecordType>()->getDecl());
4096         const TemplateArgumentList &Args = TD->getTemplateArgs();
4097         if (TD->hasAttr<CUDADeviceBuiltinSurfaceTypeAttr>()) {
4098           assert(Args.size() == 2 &&
4099                  "Unexpected number of template arguments of CUDA device "
4100                  "builtin surface type.");
4101           auto SurfType = Args[1].getAsIntegral();
4102           if (!D->hasExternalStorage())
4103             getCUDARuntime().registerDeviceSurf(D, *GV, !D->hasDefinition(),
4104                                                 SurfType.getSExtValue());
4105         } else {
4106           assert(Args.size() == 3 &&
4107                  "Unexpected number of template arguments of CUDA device "
4108                  "builtin texture type.");
4109           auto TexType = Args[1].getAsIntegral();
4110           auto Normalized = Args[2].getAsIntegral();
4111           if (!D->hasExternalStorage())
4112             getCUDARuntime().registerDeviceTex(D, *GV, !D->hasDefinition(),
4113                                                TexType.getSExtValue(),
4114                                                Normalized.getZExtValue());
4115         }
4116       }
4117     }
4118   }
4119 
4120   GV->setInitializer(Init);
4121   if (emitter)
4122     emitter->finalize(GV);
4123 
4124   // If it is safe to mark the global 'constant', do so now.
4125   GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
4126                   isTypeConstant(D->getType(), true));
4127 
4128   // If it is in a read-only section, mark it 'constant'.
4129   if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
4130     const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
4131     if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
4132       GV->setConstant(true);
4133   }
4134 
4135   GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
4136 
4137   // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
4138   // function is only defined alongside the variable, not also alongside
4139   // callers. Normally, all accesses to a thread_local go through the
4140   // thread-wrapper in order to ensure initialization has occurred, underlying
4141   // variable will never be used other than the thread-wrapper, so it can be
4142   // converted to internal linkage.
4143   //
4144   // However, if the variable has the 'constinit' attribute, it _can_ be
4145   // referenced directly, without calling the thread-wrapper, so the linkage
4146   // must not be changed.
4147   //
4148   // Additionally, if the variable isn't plain external linkage, e.g. if it's
4149   // weak or linkonce, the de-duplication semantics are important to preserve,
4150   // so we don't change the linkage.
4151   if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
4152       Linkage == llvm::GlobalValue::ExternalLinkage &&
4153       Context.getTargetInfo().getTriple().isOSDarwin() &&
4154       !D->hasAttr<ConstInitAttr>())
4155     Linkage = llvm::GlobalValue::InternalLinkage;
4156 
4157   GV->setLinkage(Linkage);
4158   if (D->hasAttr<DLLImportAttr>())
4159     GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
4160   else if (D->hasAttr<DLLExportAttr>())
4161     GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
4162   else
4163     GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
4164 
4165   if (Linkage == llvm::GlobalVariable::CommonLinkage) {
4166     // common vars aren't constant even if declared const.
4167     GV->setConstant(false);
4168     // Tentative definition of global variables may be initialized with
4169     // non-zero null pointers. In this case they should have weak linkage
4170     // since common linkage must have zero initializer and must not have
4171     // explicit section therefore cannot have non-zero initial value.
4172     if (!GV->getInitializer()->isNullValue())
4173       GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
4174   }
4175 
4176   setNonAliasAttributes(D, GV);
4177 
4178   if (D->getTLSKind() && !GV->isThreadLocal()) {
4179     if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4180       CXXThreadLocals.push_back(D);
4181     setTLSMode(GV, *D);
4182   }
4183 
4184   maybeSetTrivialComdat(*D, *GV);
4185 
4186   // Emit the initializer function if necessary.
4187   if (NeedsGlobalCtor || NeedsGlobalDtor)
4188     EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
4189 
4190   SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
4191 
4192   // Emit global variable debug information.
4193   if (CGDebugInfo *DI = getModuleDebugInfo())
4194     if (getCodeGenOpts().hasReducedDebugInfo())
4195       DI->EmitGlobalVariable(GV, D);
4196 }
4197 
4198 void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
4199   if (CGDebugInfo *DI = getModuleDebugInfo())
4200     if (getCodeGenOpts().hasReducedDebugInfo()) {
4201       QualType ASTTy = D->getType();
4202       llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
4203       llvm::PointerType *PTy =
4204           llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
4205       llvm::Constant *GV = GetOrCreateLLVMGlobal(D->getName(), PTy, D);
4206       DI->EmitExternalVariable(
4207           cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
4208     }
4209 }
4210 
4211 static bool isVarDeclStrongDefinition(const ASTContext &Context,
4212                                       CodeGenModule &CGM, const VarDecl *D,
4213                                       bool NoCommon) {
4214   // Don't give variables common linkage if -fno-common was specified unless it
4215   // was overridden by a NoCommon attribute.
4216   if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
4217     return true;
4218 
4219   // C11 6.9.2/2:
4220   //   A declaration of an identifier for an object that has file scope without
4221   //   an initializer, and without a storage-class specifier or with the
4222   //   storage-class specifier static, constitutes a tentative definition.
4223   if (D->getInit() || D->hasExternalStorage())
4224     return true;
4225 
4226   // A variable cannot be both common and exist in a section.
4227   if (D->hasAttr<SectionAttr>())
4228     return true;
4229 
4230   // A variable cannot be both common and exist in a section.
4231   // We don't try to determine which is the right section in the front-end.
4232   // If no specialized section name is applicable, it will resort to default.
4233   if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
4234       D->hasAttr<PragmaClangDataSectionAttr>() ||
4235       D->hasAttr<PragmaClangRelroSectionAttr>() ||
4236       D->hasAttr<PragmaClangRodataSectionAttr>())
4237     return true;
4238 
4239   // Thread local vars aren't considered common linkage.
4240   if (D->getTLSKind())
4241     return true;
4242 
4243   // Tentative definitions marked with WeakImportAttr are true definitions.
4244   if (D->hasAttr<WeakImportAttr>())
4245     return true;
4246 
4247   // A variable cannot be both common and exist in a comdat.
4248   if (shouldBeInCOMDAT(CGM, *D))
4249     return true;
4250 
4251   // Declarations with a required alignment do not have common linkage in MSVC
4252   // mode.
4253   if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4254     if (D->hasAttr<AlignedAttr>())
4255       return true;
4256     QualType VarType = D->getType();
4257     if (Context.isAlignmentRequired(VarType))
4258       return true;
4259 
4260     if (const auto *RT = VarType->getAs<RecordType>()) {
4261       const RecordDecl *RD = RT->getDecl();
4262       for (const FieldDecl *FD : RD->fields()) {
4263         if (FD->isBitField())
4264           continue;
4265         if (FD->hasAttr<AlignedAttr>())
4266           return true;
4267         if (Context.isAlignmentRequired(FD->getType()))
4268           return true;
4269       }
4270     }
4271   }
4272 
4273   // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
4274   // common symbols, so symbols with greater alignment requirements cannot be
4275   // common.
4276   // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
4277   // alignments for common symbols via the aligncomm directive, so this
4278   // restriction only applies to MSVC environments.
4279   if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
4280       Context.getTypeAlignIfKnown(D->getType()) >
4281           Context.toBits(CharUnits::fromQuantity(32)))
4282     return true;
4283 
4284   return false;
4285 }
4286 
4287 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator(
4288     const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) {
4289   if (Linkage == GVA_Internal)
4290     return llvm::Function::InternalLinkage;
4291 
4292   if (D->hasAttr<WeakAttr>()) {
4293     if (IsConstantVariable)
4294       return llvm::GlobalVariable::WeakODRLinkage;
4295     else
4296       return llvm::GlobalVariable::WeakAnyLinkage;
4297   }
4298 
4299   if (const auto *FD = D->getAsFunction())
4300     if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally)
4301       return llvm::GlobalVariable::LinkOnceAnyLinkage;
4302 
4303   // We are guaranteed to have a strong definition somewhere else,
4304   // so we can use available_externally linkage.
4305   if (Linkage == GVA_AvailableExternally)
4306     return llvm::GlobalValue::AvailableExternallyLinkage;
4307 
4308   // Note that Apple's kernel linker doesn't support symbol
4309   // coalescing, so we need to avoid linkonce and weak linkages there.
4310   // Normally, this means we just map to internal, but for explicit
4311   // instantiations we'll map to external.
4312 
4313   // In C++, the compiler has to emit a definition in every translation unit
4314   // that references the function.  We should use linkonce_odr because
4315   // a) if all references in this translation unit are optimized away, we
4316   // don't need to codegen it.  b) if the function persists, it needs to be
4317   // merged with other definitions. c) C++ has the ODR, so we know the
4318   // definition is dependable.
4319   if (Linkage == GVA_DiscardableODR)
4320     return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
4321                                             : llvm::Function::InternalLinkage;
4322 
4323   // An explicit instantiation of a template has weak linkage, since
4324   // explicit instantiations can occur in multiple translation units
4325   // and must all be equivalent. However, we are not allowed to
4326   // throw away these explicit instantiations.
4327   //
4328   // We don't currently support CUDA device code spread out across multiple TUs,
4329   // so say that CUDA templates are either external (for kernels) or internal.
4330   // This lets llvm perform aggressive inter-procedural optimizations.
4331   if (Linkage == GVA_StrongODR) {
4332     if (Context.getLangOpts().AppleKext)
4333       return llvm::Function::ExternalLinkage;
4334     if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice)
4335       return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
4336                                           : llvm::Function::InternalLinkage;
4337     return llvm::Function::WeakODRLinkage;
4338   }
4339 
4340   // C++ doesn't have tentative definitions and thus cannot have common
4341   // linkage.
4342   if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
4343       !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
4344                                  CodeGenOpts.NoCommon))
4345     return llvm::GlobalVariable::CommonLinkage;
4346 
4347   // selectany symbols are externally visible, so use weak instead of
4348   // linkonce.  MSVC optimizes away references to const selectany globals, so
4349   // all definitions should be the same and ODR linkage should be used.
4350   // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
4351   if (D->hasAttr<SelectAnyAttr>())
4352     return llvm::GlobalVariable::WeakODRLinkage;
4353 
4354   // Otherwise, we have strong external linkage.
4355   assert(Linkage == GVA_StrongExternal);
4356   return llvm::GlobalVariable::ExternalLinkage;
4357 }
4358 
4359 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition(
4360     const VarDecl *VD, bool IsConstant) {
4361   GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
4362   return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant);
4363 }
4364 
4365 /// Replace the uses of a function that was declared with a non-proto type.
4366 /// We want to silently drop extra arguments from call sites
4367 static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
4368                                           llvm::Function *newFn) {
4369   // Fast path.
4370   if (old->use_empty()) return;
4371 
4372   llvm::Type *newRetTy = newFn->getReturnType();
4373   SmallVector<llvm::Value*, 4> newArgs;
4374   SmallVector<llvm::OperandBundleDef, 1> newBundles;
4375 
4376   for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
4377          ui != ue; ) {
4378     llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
4379     llvm::User *user = use->getUser();
4380 
4381     // Recognize and replace uses of bitcasts.  Most calls to
4382     // unprototyped functions will use bitcasts.
4383     if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
4384       if (bitcast->getOpcode() == llvm::Instruction::BitCast)
4385         replaceUsesOfNonProtoConstant(bitcast, newFn);
4386       continue;
4387     }
4388 
4389     // Recognize calls to the function.
4390     llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
4391     if (!callSite) continue;
4392     if (!callSite->isCallee(&*use))
4393       continue;
4394 
4395     // If the return types don't match exactly, then we can't
4396     // transform this call unless it's dead.
4397     if (callSite->getType() != newRetTy && !callSite->use_empty())
4398       continue;
4399 
4400     // Get the call site's attribute list.
4401     SmallVector<llvm::AttributeSet, 8> newArgAttrs;
4402     llvm::AttributeList oldAttrs = callSite->getAttributes();
4403 
4404     // If the function was passed too few arguments, don't transform.
4405     unsigned newNumArgs = newFn->arg_size();
4406     if (callSite->arg_size() < newNumArgs)
4407       continue;
4408 
4409     // If extra arguments were passed, we silently drop them.
4410     // If any of the types mismatch, we don't transform.
4411     unsigned argNo = 0;
4412     bool dontTransform = false;
4413     for (llvm::Argument &A : newFn->args()) {
4414       if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
4415         dontTransform = true;
4416         break;
4417       }
4418 
4419       // Add any parameter attributes.
4420       newArgAttrs.push_back(oldAttrs.getParamAttributes(argNo));
4421       argNo++;
4422     }
4423     if (dontTransform)
4424       continue;
4425 
4426     // Okay, we can transform this.  Create the new call instruction and copy
4427     // over the required information.
4428     newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
4429 
4430     // Copy over any operand bundles.
4431     callSite->getOperandBundlesAsDefs(newBundles);
4432 
4433     llvm::CallBase *newCall;
4434     if (dyn_cast<llvm::CallInst>(callSite)) {
4435       newCall =
4436           llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite);
4437     } else {
4438       auto *oldInvoke = cast<llvm::InvokeInst>(callSite);
4439       newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(),
4440                                          oldInvoke->getUnwindDest(), newArgs,
4441                                          newBundles, "", callSite);
4442     }
4443     newArgs.clear(); // for the next iteration
4444 
4445     if (!newCall->getType()->isVoidTy())
4446       newCall->takeName(callSite);
4447     newCall->setAttributes(llvm::AttributeList::get(
4448         newFn->getContext(), oldAttrs.getFnAttributes(),
4449         oldAttrs.getRetAttributes(), newArgAttrs));
4450     newCall->setCallingConv(callSite->getCallingConv());
4451 
4452     // Finally, remove the old call, replacing any uses with the new one.
4453     if (!callSite->use_empty())
4454       callSite->replaceAllUsesWith(newCall);
4455 
4456     // Copy debug location attached to CI.
4457     if (callSite->getDebugLoc())
4458       newCall->setDebugLoc(callSite->getDebugLoc());
4459 
4460     callSite->eraseFromParent();
4461   }
4462 }
4463 
4464 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
4465 /// implement a function with no prototype, e.g. "int foo() {}".  If there are
4466 /// existing call uses of the old function in the module, this adjusts them to
4467 /// call the new function directly.
4468 ///
4469 /// This is not just a cleanup: the always_inline pass requires direct calls to
4470 /// functions to be able to inline them.  If there is a bitcast in the way, it
4471 /// won't inline them.  Instcombine normally deletes these calls, but it isn't
4472 /// run at -O0.
4473 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
4474                                                       llvm::Function *NewFn) {
4475   // If we're redefining a global as a function, don't transform it.
4476   if (!isa<llvm::Function>(Old)) return;
4477 
4478   replaceUsesOfNonProtoConstant(Old, NewFn);
4479 }
4480 
4481 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
4482   auto DK = VD->isThisDeclarationADefinition();
4483   if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>())
4484     return;
4485 
4486   TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
4487   // If we have a definition, this might be a deferred decl. If the
4488   // instantiation is explicit, make sure we emit it at the end.
4489   if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
4490     GetAddrOfGlobalVar(VD);
4491 
4492   EmitTopLevelDecl(VD);
4493 }
4494 
4495 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
4496                                                  llvm::GlobalValue *GV) {
4497   const auto *D = cast<FunctionDecl>(GD.getDecl());
4498 
4499   // Compute the function info and LLVM type.
4500   const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4501   llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4502 
4503   // Get or create the prototype for the function.
4504   if (!GV || (GV->getValueType() != Ty))
4505     GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
4506                                                    /*DontDefer=*/true,
4507                                                    ForDefinition));
4508 
4509   // Already emitted.
4510   if (!GV->isDeclaration())
4511     return;
4512 
4513   // We need to set linkage and visibility on the function before
4514   // generating code for it because various parts of IR generation
4515   // want to propagate this information down (e.g. to local static
4516   // declarations).
4517   auto *Fn = cast<llvm::Function>(GV);
4518   setFunctionLinkage(GD, Fn);
4519 
4520   // FIXME: this is redundant with part of setFunctionDefinitionAttributes
4521   setGVProperties(Fn, GD);
4522 
4523   MaybeHandleStaticInExternC(D, Fn);
4524 
4525 
4526   maybeSetTrivialComdat(*D, *Fn);
4527 
4528   CodeGenFunction(*this).GenerateCode(GD, Fn, FI);
4529 
4530   setNonAliasAttributes(GD, Fn);
4531   SetLLVMFunctionAttributesForDefinition(D, Fn);
4532 
4533   if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
4534     AddGlobalCtor(Fn, CA->getPriority());
4535   if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
4536     AddGlobalDtor(Fn, DA->getPriority());
4537   if (D->hasAttr<AnnotateAttr>())
4538     AddGlobalAnnotations(D, Fn);
4539 }
4540 
4541 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
4542   const auto *D = cast<ValueDecl>(GD.getDecl());
4543   const AliasAttr *AA = D->getAttr<AliasAttr>();
4544   assert(AA && "Not an alias?");
4545 
4546   StringRef MangledName = getMangledName(GD);
4547 
4548   if (AA->getAliasee() == MangledName) {
4549     Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
4550     return;
4551   }
4552 
4553   // If there is a definition in the module, then it wins over the alias.
4554   // This is dubious, but allow it to be safe.  Just ignore the alias.
4555   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4556   if (Entry && !Entry->isDeclaration())
4557     return;
4558 
4559   Aliases.push_back(GD);
4560 
4561   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
4562 
4563   // Create a reference to the named value.  This ensures that it is emitted
4564   // if a deferred decl.
4565   llvm::Constant *Aliasee;
4566   llvm::GlobalValue::LinkageTypes LT;
4567   if (isa<llvm::FunctionType>(DeclTy)) {
4568     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
4569                                       /*ForVTable=*/false);
4570     LT = getFunctionLinkage(GD);
4571   } else {
4572     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
4573                                     llvm::PointerType::getUnqual(DeclTy),
4574                                     /*D=*/nullptr);
4575     LT = getLLVMLinkageVarDefinition(cast<VarDecl>(GD.getDecl()),
4576                                      D->getType().isConstQualified());
4577   }
4578 
4579   // Create the new alias itself, but don't set a name yet.
4580   unsigned AS = Aliasee->getType()->getPointerAddressSpace();
4581   auto *GA =
4582       llvm::GlobalAlias::create(DeclTy, AS, LT, "", Aliasee, &getModule());
4583 
4584   if (Entry) {
4585     if (GA->getAliasee() == Entry) {
4586       Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
4587       return;
4588     }
4589 
4590     assert(Entry->isDeclaration());
4591 
4592     // If there is a declaration in the module, then we had an extern followed
4593     // by the alias, as in:
4594     //   extern int test6();
4595     //   ...
4596     //   int test6() __attribute__((alias("test7")));
4597     //
4598     // Remove it and replace uses of it with the alias.
4599     GA->takeName(Entry);
4600 
4601     Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
4602                                                           Entry->getType()));
4603     Entry->eraseFromParent();
4604   } else {
4605     GA->setName(MangledName);
4606   }
4607 
4608   // Set attributes which are particular to an alias; this is a
4609   // specialization of the attributes which may be set on a global
4610   // variable/function.
4611   if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
4612       D->isWeakImported()) {
4613     GA->setLinkage(llvm::Function::WeakAnyLinkage);
4614   }
4615 
4616   if (const auto *VD = dyn_cast<VarDecl>(D))
4617     if (VD->getTLSKind())
4618       setTLSMode(GA, *VD);
4619 
4620   SetCommonAttributes(GD, GA);
4621 }
4622 
4623 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
4624   const auto *D = cast<ValueDecl>(GD.getDecl());
4625   const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
4626   assert(IFA && "Not an ifunc?");
4627 
4628   StringRef MangledName = getMangledName(GD);
4629 
4630   if (IFA->getResolver() == MangledName) {
4631     Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
4632     return;
4633   }
4634 
4635   // Report an error if some definition overrides ifunc.
4636   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4637   if (Entry && !Entry->isDeclaration()) {
4638     GlobalDecl OtherGD;
4639     if (lookupRepresentativeDecl(MangledName, OtherGD) &&
4640         DiagnosedConflictingDefinitions.insert(GD).second) {
4641       Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name)
4642           << MangledName;
4643       Diags.Report(OtherGD.getDecl()->getLocation(),
4644                    diag::note_previous_definition);
4645     }
4646     return;
4647   }
4648 
4649   Aliases.push_back(GD);
4650 
4651   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
4652   llvm::Constant *Resolver =
4653       GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD,
4654                               /*ForVTable=*/false);
4655   llvm::GlobalIFunc *GIF =
4656       llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage,
4657                                 "", Resolver, &getModule());
4658   if (Entry) {
4659     if (GIF->getResolver() == Entry) {
4660       Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
4661       return;
4662     }
4663     assert(Entry->isDeclaration());
4664 
4665     // If there is a declaration in the module, then we had an extern followed
4666     // by the ifunc, as in:
4667     //   extern int test();
4668     //   ...
4669     //   int test() __attribute__((ifunc("resolver")));
4670     //
4671     // Remove it and replace uses of it with the ifunc.
4672     GIF->takeName(Entry);
4673 
4674     Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF,
4675                                                           Entry->getType()));
4676     Entry->eraseFromParent();
4677   } else
4678     GIF->setName(MangledName);
4679 
4680   SetCommonAttributes(GD, GIF);
4681 }
4682 
4683 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
4684                                             ArrayRef<llvm::Type*> Tys) {
4685   return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
4686                                          Tys);
4687 }
4688 
4689 static llvm::StringMapEntry<llvm::GlobalVariable *> &
4690 GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
4691                          const StringLiteral *Literal, bool TargetIsLSB,
4692                          bool &IsUTF16, unsigned &StringLength) {
4693   StringRef String = Literal->getString();
4694   unsigned NumBytes = String.size();
4695 
4696   // Check for simple case.
4697   if (!Literal->containsNonAsciiOrNull()) {
4698     StringLength = NumBytes;
4699     return *Map.insert(std::make_pair(String, nullptr)).first;
4700   }
4701 
4702   // Otherwise, convert the UTF8 literals into a string of shorts.
4703   IsUTF16 = true;
4704 
4705   SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
4706   const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
4707   llvm::UTF16 *ToPtr = &ToBuf[0];
4708 
4709   (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
4710                                  ToPtr + NumBytes, llvm::strictConversion);
4711 
4712   // ConvertUTF8toUTF16 returns the length in ToPtr.
4713   StringLength = ToPtr - &ToBuf[0];
4714 
4715   // Add an explicit null.
4716   *ToPtr = 0;
4717   return *Map.insert(std::make_pair(
4718                          StringRef(reinterpret_cast<const char *>(ToBuf.data()),
4719                                    (StringLength + 1) * 2),
4720                          nullptr)).first;
4721 }
4722 
4723 ConstantAddress
4724 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
4725   unsigned StringLength = 0;
4726   bool isUTF16 = false;
4727   llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
4728       GetConstantCFStringEntry(CFConstantStringMap, Literal,
4729                                getDataLayout().isLittleEndian(), isUTF16,
4730                                StringLength);
4731 
4732   if (auto *C = Entry.second)
4733     return ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment()));
4734 
4735   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
4736   llvm::Constant *Zeros[] = { Zero, Zero };
4737 
4738   const ASTContext &Context = getContext();
4739   const llvm::Triple &Triple = getTriple();
4740 
4741   const auto CFRuntime = getLangOpts().CFRuntime;
4742   const bool IsSwiftABI =
4743       static_cast<unsigned>(CFRuntime) >=
4744       static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift);
4745   const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1;
4746 
4747   // If we don't already have it, get __CFConstantStringClassReference.
4748   if (!CFConstantStringClassRef) {
4749     const char *CFConstantStringClassName = "__CFConstantStringClassReference";
4750     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
4751     Ty = llvm::ArrayType::get(Ty, 0);
4752 
4753     switch (CFRuntime) {
4754     default: break;
4755     case LangOptions::CoreFoundationABI::Swift: LLVM_FALLTHROUGH;
4756     case LangOptions::CoreFoundationABI::Swift5_0:
4757       CFConstantStringClassName =
4758           Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN"
4759                               : "$s10Foundation19_NSCFConstantStringCN";
4760       Ty = IntPtrTy;
4761       break;
4762     case LangOptions::CoreFoundationABI::Swift4_2:
4763       CFConstantStringClassName =
4764           Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN"
4765                               : "$S10Foundation19_NSCFConstantStringCN";
4766       Ty = IntPtrTy;
4767       break;
4768     case LangOptions::CoreFoundationABI::Swift4_1:
4769       CFConstantStringClassName =
4770           Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN"
4771                               : "__T010Foundation19_NSCFConstantStringCN";
4772       Ty = IntPtrTy;
4773       break;
4774     }
4775 
4776     llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName);
4777 
4778     if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) {
4779       llvm::GlobalValue *GV = nullptr;
4780 
4781       if ((GV = dyn_cast<llvm::GlobalValue>(C))) {
4782         IdentifierInfo &II = Context.Idents.get(GV->getName());
4783         TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
4784         DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
4785 
4786         const VarDecl *VD = nullptr;
4787         for (const auto &Result : DC->lookup(&II))
4788           if ((VD = dyn_cast<VarDecl>(Result)))
4789             break;
4790 
4791         if (Triple.isOSBinFormatELF()) {
4792           if (!VD)
4793             GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4794         } else {
4795           GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4796           if (!VD || !VD->hasAttr<DLLExportAttr>())
4797             GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4798           else
4799             GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
4800         }
4801 
4802         setDSOLocal(GV);
4803       }
4804     }
4805 
4806     // Decay array -> ptr
4807     CFConstantStringClassRef =
4808         IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty)
4809                    : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros);
4810   }
4811 
4812   QualType CFTy = Context.getCFConstantStringType();
4813 
4814   auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
4815 
4816   ConstantInitBuilder Builder(*this);
4817   auto Fields = Builder.beginStruct(STy);
4818 
4819   // Class pointer.
4820   Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef));
4821 
4822   // Flags.
4823   if (IsSwiftABI) {
4824     Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01);
4825     Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8);
4826   } else {
4827     Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8);
4828   }
4829 
4830   // String pointer.
4831   llvm::Constant *C = nullptr;
4832   if (isUTF16) {
4833     auto Arr = llvm::makeArrayRef(
4834         reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
4835         Entry.first().size() / 2);
4836     C = llvm::ConstantDataArray::get(VMContext, Arr);
4837   } else {
4838     C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
4839   }
4840 
4841   // Note: -fwritable-strings doesn't make the backing store strings of
4842   // CFStrings writable. (See <rdar://problem/10657500>)
4843   auto *GV =
4844       new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
4845                                llvm::GlobalValue::PrivateLinkage, C, ".str");
4846   GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4847   // Don't enforce the target's minimum global alignment, since the only use
4848   // of the string is via this class initializer.
4849   CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy)
4850                             : Context.getTypeAlignInChars(Context.CharTy);
4851   GV->setAlignment(Align.getAsAlign());
4852 
4853   // FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
4854   // Without it LLVM can merge the string with a non unnamed_addr one during
4855   // LTO.  Doing that changes the section it ends in, which surprises ld64.
4856   if (Triple.isOSBinFormatMachO())
4857     GV->setSection(isUTF16 ? "__TEXT,__ustring"
4858                            : "__TEXT,__cstring,cstring_literals");
4859   // Make sure the literal ends up in .rodata to allow for safe ICF and for
4860   // the static linker to adjust permissions to read-only later on.
4861   else if (Triple.isOSBinFormatELF())
4862     GV->setSection(".rodata");
4863 
4864   // String.
4865   llvm::Constant *Str =
4866       llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
4867 
4868   if (isUTF16)
4869     // Cast the UTF16 string to the correct type.
4870     Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy);
4871   Fields.add(Str);
4872 
4873   // String length.
4874   llvm::IntegerType *LengthTy =
4875       llvm::IntegerType::get(getModule().getContext(),
4876                              Context.getTargetInfo().getLongWidth());
4877   if (IsSwiftABI) {
4878     if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 ||
4879         CFRuntime == LangOptions::CoreFoundationABI::Swift4_2)
4880       LengthTy = Int32Ty;
4881     else
4882       LengthTy = IntPtrTy;
4883   }
4884   Fields.addInt(LengthTy, StringLength);
4885 
4886   // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
4887   // properly aligned on 32-bit platforms.
4888   CharUnits Alignment =
4889       IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign();
4890 
4891   // The struct.
4892   GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
4893                                     /*isConstant=*/false,
4894                                     llvm::GlobalVariable::PrivateLinkage);
4895   GV->addAttribute("objc_arc_inert");
4896   switch (Triple.getObjectFormat()) {
4897   case llvm::Triple::UnknownObjectFormat:
4898     llvm_unreachable("unknown file format");
4899   case llvm::Triple::XCOFF:
4900     llvm_unreachable("XCOFF is not yet implemented");
4901   case llvm::Triple::COFF:
4902   case llvm::Triple::ELF:
4903   case llvm::Triple::Wasm:
4904     GV->setSection("cfstring");
4905     break;
4906   case llvm::Triple::MachO:
4907     GV->setSection("__DATA,__cfstring");
4908     break;
4909   }
4910   Entry.second = GV;
4911 
4912   return ConstantAddress(GV, Alignment);
4913 }
4914 
4915 bool CodeGenModule::getExpressionLocationsEnabled() const {
4916   return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo;
4917 }
4918 
4919 QualType CodeGenModule::getObjCFastEnumerationStateType() {
4920   if (ObjCFastEnumerationStateType.isNull()) {
4921     RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
4922     D->startDefinition();
4923 
4924     QualType FieldTypes[] = {
4925       Context.UnsignedLongTy,
4926       Context.getPointerType(Context.getObjCIdType()),
4927       Context.getPointerType(Context.UnsignedLongTy),
4928       Context.getConstantArrayType(Context.UnsignedLongTy,
4929                            llvm::APInt(32, 5), nullptr, ArrayType::Normal, 0)
4930     };
4931 
4932     for (size_t i = 0; i < 4; ++i) {
4933       FieldDecl *Field = FieldDecl::Create(Context,
4934                                            D,
4935                                            SourceLocation(),
4936                                            SourceLocation(), nullptr,
4937                                            FieldTypes[i], /*TInfo=*/nullptr,
4938                                            /*BitWidth=*/nullptr,
4939                                            /*Mutable=*/false,
4940                                            ICIS_NoInit);
4941       Field->setAccess(AS_public);
4942       D->addDecl(Field);
4943     }
4944 
4945     D->completeDefinition();
4946     ObjCFastEnumerationStateType = Context.getTagDeclType(D);
4947   }
4948 
4949   return ObjCFastEnumerationStateType;
4950 }
4951 
4952 llvm::Constant *
4953 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
4954   assert(!E->getType()->isPointerType() && "Strings are always arrays");
4955 
4956   // Don't emit it as the address of the string, emit the string data itself
4957   // as an inline array.
4958   if (E->getCharByteWidth() == 1) {
4959     SmallString<64> Str(E->getString());
4960 
4961     // Resize the string to the right size, which is indicated by its type.
4962     const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
4963     Str.resize(CAT->getSize().getZExtValue());
4964     return llvm::ConstantDataArray::getString(VMContext, Str, false);
4965   }
4966 
4967   auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
4968   llvm::Type *ElemTy = AType->getElementType();
4969   unsigned NumElements = AType->getNumElements();
4970 
4971   // Wide strings have either 2-byte or 4-byte elements.
4972   if (ElemTy->getPrimitiveSizeInBits() == 16) {
4973     SmallVector<uint16_t, 32> Elements;
4974     Elements.reserve(NumElements);
4975 
4976     for(unsigned i = 0, e = E->getLength(); i != e; ++i)
4977       Elements.push_back(E->getCodeUnit(i));
4978     Elements.resize(NumElements);
4979     return llvm::ConstantDataArray::get(VMContext, Elements);
4980   }
4981 
4982   assert(ElemTy->getPrimitiveSizeInBits() == 32);
4983   SmallVector<uint32_t, 32> Elements;
4984   Elements.reserve(NumElements);
4985 
4986   for(unsigned i = 0, e = E->getLength(); i != e; ++i)
4987     Elements.push_back(E->getCodeUnit(i));
4988   Elements.resize(NumElements);
4989   return llvm::ConstantDataArray::get(VMContext, Elements);
4990 }
4991 
4992 static llvm::GlobalVariable *
4993 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
4994                       CodeGenModule &CGM, StringRef GlobalName,
4995                       CharUnits Alignment) {
4996   unsigned AddrSpace = CGM.getContext().getTargetAddressSpace(
4997       CGM.getStringLiteralAddressSpace());
4998 
4999   llvm::Module &M = CGM.getModule();
5000   // Create a global variable for this string
5001   auto *GV = new llvm::GlobalVariable(
5002       M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
5003       nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
5004   GV->setAlignment(Alignment.getAsAlign());
5005   GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
5006   if (GV->isWeakForLinker()) {
5007     assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals");
5008     GV->setComdat(M.getOrInsertComdat(GV->getName()));
5009   }
5010   CGM.setDSOLocal(GV);
5011 
5012   return GV;
5013 }
5014 
5015 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
5016 /// constant array for the given string literal.
5017 ConstantAddress
5018 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
5019                                                   StringRef Name) {
5020   CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType());
5021 
5022   llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
5023   llvm::GlobalVariable **Entry = nullptr;
5024   if (!LangOpts.WritableStrings) {
5025     Entry = &ConstantStringMap[C];
5026     if (auto GV = *Entry) {
5027       if (Alignment.getQuantity() > GV->getAlignment())
5028         GV->setAlignment(Alignment.getAsAlign());
5029       return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5030                              Alignment);
5031     }
5032   }
5033 
5034   SmallString<256> MangledNameBuffer;
5035   StringRef GlobalVariableName;
5036   llvm::GlobalValue::LinkageTypes LT;
5037 
5038   // Mangle the string literal if that's how the ABI merges duplicate strings.
5039   // Don't do it if they are writable, since we don't want writes in one TU to
5040   // affect strings in another.
5041   if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
5042       !LangOpts.WritableStrings) {
5043     llvm::raw_svector_ostream Out(MangledNameBuffer);
5044     getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
5045     LT = llvm::GlobalValue::LinkOnceODRLinkage;
5046     GlobalVariableName = MangledNameBuffer;
5047   } else {
5048     LT = llvm::GlobalValue::PrivateLinkage;
5049     GlobalVariableName = Name;
5050   }
5051 
5052   auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
5053   if (Entry)
5054     *Entry = GV;
5055 
5056   SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>",
5057                                   QualType());
5058 
5059   return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5060                          Alignment);
5061 }
5062 
5063 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
5064 /// array for the given ObjCEncodeExpr node.
5065 ConstantAddress
5066 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
5067   std::string Str;
5068   getContext().getObjCEncodingForType(E->getEncodedType(), Str);
5069 
5070   return GetAddrOfConstantCString(Str);
5071 }
5072 
5073 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
5074 /// the literal and a terminating '\0' character.
5075 /// The result has pointer to array type.
5076 ConstantAddress CodeGenModule::GetAddrOfConstantCString(
5077     const std::string &Str, const char *GlobalName) {
5078   StringRef StrWithNull(Str.c_str(), Str.size() + 1);
5079   CharUnits Alignment =
5080     getContext().getAlignOfGlobalVarInChars(getContext().CharTy);
5081 
5082   llvm::Constant *C =
5083       llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
5084 
5085   // Don't share any string literals if strings aren't constant.
5086   llvm::GlobalVariable **Entry = nullptr;
5087   if (!LangOpts.WritableStrings) {
5088     Entry = &ConstantStringMap[C];
5089     if (auto GV = *Entry) {
5090       if (Alignment.getQuantity() > GV->getAlignment())
5091         GV->setAlignment(Alignment.getAsAlign());
5092       return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5093                              Alignment);
5094     }
5095   }
5096 
5097   // Get the default prefix if a name wasn't specified.
5098   if (!GlobalName)
5099     GlobalName = ".str";
5100   // Create a global variable for this.
5101   auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
5102                                   GlobalName, Alignment);
5103   if (Entry)
5104     *Entry = GV;
5105 
5106   return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
5107                          Alignment);
5108 }
5109 
5110 ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
5111     const MaterializeTemporaryExpr *E, const Expr *Init) {
5112   assert((E->getStorageDuration() == SD_Static ||
5113           E->getStorageDuration() == SD_Thread) && "not a global temporary");
5114   const auto *VD = cast<VarDecl>(E->getExtendingDecl());
5115 
5116   // If we're not materializing a subobject of the temporary, keep the
5117   // cv-qualifiers from the type of the MaterializeTemporaryExpr.
5118   QualType MaterializedType = Init->getType();
5119   if (Init == E->getSubExpr())
5120     MaterializedType = E->getType();
5121 
5122   CharUnits Align = getContext().getTypeAlignInChars(MaterializedType);
5123 
5124   if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E])
5125     return ConstantAddress(Slot, Align);
5126 
5127   // FIXME: If an externally-visible declaration extends multiple temporaries,
5128   // we need to give each temporary the same name in every translation unit (and
5129   // we also need to make the temporaries externally-visible).
5130   SmallString<256> Name;
5131   llvm::raw_svector_ostream Out(Name);
5132   getCXXABI().getMangleContext().mangleReferenceTemporary(
5133       VD, E->getManglingNumber(), Out);
5134 
5135   APValue *Value = nullptr;
5136   if (E->getStorageDuration() == SD_Static && VD && VD->evaluateValue()) {
5137     // If the initializer of the extending declaration is a constant
5138     // initializer, we should have a cached constant initializer for this
5139     // temporary. Note that this might have a different value from the value
5140     // computed by evaluating the initializer if the surrounding constant
5141     // expression modifies the temporary.
5142     Value = E->getOrCreateValue(false);
5143   }
5144 
5145   // Try evaluating it now, it might have a constant initializer.
5146   Expr::EvalResult EvalResult;
5147   if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
5148       !EvalResult.hasSideEffects())
5149     Value = &EvalResult.Val;
5150 
5151   LangAS AddrSpace =
5152       VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace();
5153 
5154   Optional<ConstantEmitter> emitter;
5155   llvm::Constant *InitialValue = nullptr;
5156   bool Constant = false;
5157   llvm::Type *Type;
5158   if (Value) {
5159     // The temporary has a constant initializer, use it.
5160     emitter.emplace(*this);
5161     InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
5162                                                MaterializedType);
5163     Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
5164     Type = InitialValue->getType();
5165   } else {
5166     // No initializer, the initialization will be provided when we
5167     // initialize the declaration which performed lifetime extension.
5168     Type = getTypes().ConvertTypeForMem(MaterializedType);
5169   }
5170 
5171   // Create a global variable for this lifetime-extended temporary.
5172   llvm::GlobalValue::LinkageTypes Linkage =
5173       getLLVMLinkageVarDefinition(VD, Constant);
5174   if (Linkage == llvm::GlobalVariable::ExternalLinkage) {
5175     const VarDecl *InitVD;
5176     if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
5177         isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
5178       // Temporaries defined inside a class get linkonce_odr linkage because the
5179       // class can be defined in multiple translation units.
5180       Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
5181     } else {
5182       // There is no need for this temporary to have external linkage if the
5183       // VarDecl has external linkage.
5184       Linkage = llvm::GlobalVariable::InternalLinkage;
5185     }
5186   }
5187   auto TargetAS = getContext().getTargetAddressSpace(AddrSpace);
5188   auto *GV = new llvm::GlobalVariable(
5189       getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
5190       /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
5191   if (emitter) emitter->finalize(GV);
5192   setGVProperties(GV, VD);
5193   GV->setAlignment(Align.getAsAlign());
5194   if (supportsCOMDAT() && GV->isWeakForLinker())
5195     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
5196   if (VD->getTLSKind())
5197     setTLSMode(GV, *VD);
5198   llvm::Constant *CV = GV;
5199   if (AddrSpace != LangAS::Default)
5200     CV = getTargetCodeGenInfo().performAddrSpaceCast(
5201         *this, GV, AddrSpace, LangAS::Default,
5202         Type->getPointerTo(
5203             getContext().getTargetAddressSpace(LangAS::Default)));
5204   MaterializedGlobalTemporaryMap[E] = CV;
5205   return ConstantAddress(CV, Align);
5206 }
5207 
5208 /// EmitObjCPropertyImplementations - Emit information for synthesized
5209 /// properties for an implementation.
5210 void CodeGenModule::EmitObjCPropertyImplementations(const
5211                                                     ObjCImplementationDecl *D) {
5212   for (const auto *PID : D->property_impls()) {
5213     // Dynamic is just for type-checking.
5214     if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
5215       ObjCPropertyDecl *PD = PID->getPropertyDecl();
5216 
5217       // Determine which methods need to be implemented, some may have
5218       // been overridden. Note that ::isPropertyAccessor is not the method
5219       // we want, that just indicates if the decl came from a
5220       // property. What we want to know is if the method is defined in
5221       // this implementation.
5222       auto *Getter = PID->getGetterMethodDecl();
5223       if (!Getter || Getter->isSynthesizedAccessorStub())
5224         CodeGenFunction(*this).GenerateObjCGetter(
5225             const_cast<ObjCImplementationDecl *>(D), PID);
5226       auto *Setter = PID->getSetterMethodDecl();
5227       if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub()))
5228         CodeGenFunction(*this).GenerateObjCSetter(
5229                                  const_cast<ObjCImplementationDecl *>(D), PID);
5230     }
5231   }
5232 }
5233 
5234 static bool needsDestructMethod(ObjCImplementationDecl *impl) {
5235   const ObjCInterfaceDecl *iface = impl->getClassInterface();
5236   for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
5237        ivar; ivar = ivar->getNextIvar())
5238     if (ivar->getType().isDestructedType())
5239       return true;
5240 
5241   return false;
5242 }
5243 
5244 static bool AllTrivialInitializers(CodeGenModule &CGM,
5245                                    ObjCImplementationDecl *D) {
5246   CodeGenFunction CGF(CGM);
5247   for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
5248        E = D->init_end(); B != E; ++B) {
5249     CXXCtorInitializer *CtorInitExp = *B;
5250     Expr *Init = CtorInitExp->getInit();
5251     if (!CGF.isTrivialInitializer(Init))
5252       return false;
5253   }
5254   return true;
5255 }
5256 
5257 /// EmitObjCIvarInitializations - Emit information for ivar initialization
5258 /// for an implementation.
5259 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
5260   // We might need a .cxx_destruct even if we don't have any ivar initializers.
5261   if (needsDestructMethod(D)) {
5262     IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
5263     Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
5264     ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(
5265         getContext(), D->getLocation(), D->getLocation(), cxxSelector,
5266         getContext().VoidTy, nullptr, D,
5267         /*isInstance=*/true, /*isVariadic=*/false,
5268         /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
5269         /*isImplicitlyDeclared=*/true,
5270         /*isDefined=*/false, ObjCMethodDecl::Required);
5271     D->addInstanceMethod(DTORMethod);
5272     CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
5273     D->setHasDestructors(true);
5274   }
5275 
5276   // If the implementation doesn't have any ivar initializers, we don't need
5277   // a .cxx_construct.
5278   if (D->getNumIvarInitializers() == 0 ||
5279       AllTrivialInitializers(*this, D))
5280     return;
5281 
5282   IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
5283   Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
5284   // The constructor returns 'self'.
5285   ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(
5286       getContext(), D->getLocation(), D->getLocation(), cxxSelector,
5287       getContext().getObjCIdType(), nullptr, D, /*isInstance=*/true,
5288       /*isVariadic=*/false,
5289       /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
5290       /*isImplicitlyDeclared=*/true,
5291       /*isDefined=*/false, ObjCMethodDecl::Required);
5292   D->addInstanceMethod(CTORMethod);
5293   CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
5294   D->setHasNonZeroConstructors(true);
5295 }
5296 
5297 // EmitLinkageSpec - Emit all declarations in a linkage spec.
5298 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
5299   if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
5300       LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
5301     ErrorUnsupported(LSD, "linkage spec");
5302     return;
5303   }
5304 
5305   EmitDeclContext(LSD);
5306 }
5307 
5308 void CodeGenModule::EmitDeclContext(const DeclContext *DC) {
5309   for (auto *I : DC->decls()) {
5310     // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
5311     // are themselves considered "top-level", so EmitTopLevelDecl on an
5312     // ObjCImplDecl does not recursively visit them. We need to do that in
5313     // case they're nested inside another construct (LinkageSpecDecl /
5314     // ExportDecl) that does stop them from being considered "top-level".
5315     if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
5316       for (auto *M : OID->methods())
5317         EmitTopLevelDecl(M);
5318     }
5319 
5320     EmitTopLevelDecl(I);
5321   }
5322 }
5323 
5324 /// EmitTopLevelDecl - Emit code for a single top level declaration.
5325 void CodeGenModule::EmitTopLevelDecl(Decl *D) {
5326   // Ignore dependent declarations.
5327   if (D->isTemplated())
5328     return;
5329 
5330   // Consteval function shouldn't be emitted.
5331   if (auto *FD = dyn_cast<FunctionDecl>(D))
5332     if (FD->isConsteval())
5333       return;
5334 
5335   switch (D->getKind()) {
5336   case Decl::CXXConversion:
5337   case Decl::CXXMethod:
5338   case Decl::Function:
5339     EmitGlobal(cast<FunctionDecl>(D));
5340     // Always provide some coverage mapping
5341     // even for the functions that aren't emitted.
5342     AddDeferredUnusedCoverageMapping(D);
5343     break;
5344 
5345   case Decl::CXXDeductionGuide:
5346     // Function-like, but does not result in code emission.
5347     break;
5348 
5349   case Decl::Var:
5350   case Decl::Decomposition:
5351   case Decl::VarTemplateSpecialization:
5352     EmitGlobal(cast<VarDecl>(D));
5353     if (auto *DD = dyn_cast<DecompositionDecl>(D))
5354       for (auto *B : DD->bindings())
5355         if (auto *HD = B->getHoldingVar())
5356           EmitGlobal(HD);
5357     break;
5358 
5359   // Indirect fields from global anonymous structs and unions can be
5360   // ignored; only the actual variable requires IR gen support.
5361   case Decl::IndirectField:
5362     break;
5363 
5364   // C++ Decls
5365   case Decl::Namespace:
5366     EmitDeclContext(cast<NamespaceDecl>(D));
5367     break;
5368   case Decl::ClassTemplateSpecialization: {
5369     const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
5370     if (CGDebugInfo *DI = getModuleDebugInfo())
5371       if (Spec->getSpecializationKind() ==
5372               TSK_ExplicitInstantiationDefinition &&
5373           Spec->hasDefinition())
5374         DI->completeTemplateDefinition(*Spec);
5375   } LLVM_FALLTHROUGH;
5376   case Decl::CXXRecord:
5377     if (CGDebugInfo *DI = getModuleDebugInfo())
5378       if (auto *ES = D->getASTContext().getExternalSource())
5379         if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
5380           DI->completeUnusedClass(cast<CXXRecordDecl>(*D));
5381     // Emit any static data members, they may be definitions.
5382     for (auto *I : cast<CXXRecordDecl>(D)->decls())
5383       if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
5384         EmitTopLevelDecl(I);
5385     break;
5386     // No code generation needed.
5387   case Decl::UsingShadow:
5388   case Decl::ClassTemplate:
5389   case Decl::VarTemplate:
5390   case Decl::Concept:
5391   case Decl::VarTemplatePartialSpecialization:
5392   case Decl::FunctionTemplate:
5393   case Decl::TypeAliasTemplate:
5394   case Decl::Block:
5395   case Decl::Empty:
5396   case Decl::Binding:
5397     break;
5398   case Decl::Using:          // using X; [C++]
5399     if (CGDebugInfo *DI = getModuleDebugInfo())
5400         DI->EmitUsingDecl(cast<UsingDecl>(*D));
5401     break;
5402   case Decl::NamespaceAlias:
5403     if (CGDebugInfo *DI = getModuleDebugInfo())
5404         DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
5405     break;
5406   case Decl::UsingDirective: // using namespace X; [C++]
5407     if (CGDebugInfo *DI = getModuleDebugInfo())
5408       DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
5409     break;
5410   case Decl::CXXConstructor:
5411     getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
5412     break;
5413   case Decl::CXXDestructor:
5414     getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
5415     break;
5416 
5417   case Decl::StaticAssert:
5418     // Nothing to do.
5419     break;
5420 
5421   // Objective-C Decls
5422 
5423   // Forward declarations, no (immediate) code generation.
5424   case Decl::ObjCInterface:
5425   case Decl::ObjCCategory:
5426     break;
5427 
5428   case Decl::ObjCProtocol: {
5429     auto *Proto = cast<ObjCProtocolDecl>(D);
5430     if (Proto->isThisDeclarationADefinition())
5431       ObjCRuntime->GenerateProtocol(Proto);
5432     break;
5433   }
5434 
5435   case Decl::ObjCCategoryImpl:
5436     // Categories have properties but don't support synthesize so we
5437     // can ignore them here.
5438     ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
5439     break;
5440 
5441   case Decl::ObjCImplementation: {
5442     auto *OMD = cast<ObjCImplementationDecl>(D);
5443     EmitObjCPropertyImplementations(OMD);
5444     EmitObjCIvarInitializations(OMD);
5445     ObjCRuntime->GenerateClass(OMD);
5446     // Emit global variable debug information.
5447     if (CGDebugInfo *DI = getModuleDebugInfo())
5448       if (getCodeGenOpts().hasReducedDebugInfo())
5449         DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
5450             OMD->getClassInterface()), OMD->getLocation());
5451     break;
5452   }
5453   case Decl::ObjCMethod: {
5454     auto *OMD = cast<ObjCMethodDecl>(D);
5455     // If this is not a prototype, emit the body.
5456     if (OMD->getBody())
5457       CodeGenFunction(*this).GenerateObjCMethod(OMD);
5458     break;
5459   }
5460   case Decl::ObjCCompatibleAlias:
5461     ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
5462     break;
5463 
5464   case Decl::PragmaComment: {
5465     const auto *PCD = cast<PragmaCommentDecl>(D);
5466     switch (PCD->getCommentKind()) {
5467     case PCK_Unknown:
5468       llvm_unreachable("unexpected pragma comment kind");
5469     case PCK_Linker:
5470       AppendLinkerOptions(PCD->getArg());
5471       break;
5472     case PCK_Lib:
5473         AddDependentLib(PCD->getArg());
5474       break;
5475     case PCK_Compiler:
5476     case PCK_ExeStr:
5477     case PCK_User:
5478       break; // We ignore all of these.
5479     }
5480     break;
5481   }
5482 
5483   case Decl::PragmaDetectMismatch: {
5484     const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
5485     AddDetectMismatch(PDMD->getName(), PDMD->getValue());
5486     break;
5487   }
5488 
5489   case Decl::LinkageSpec:
5490     EmitLinkageSpec(cast<LinkageSpecDecl>(D));
5491     break;
5492 
5493   case Decl::FileScopeAsm: {
5494     // File-scope asm is ignored during device-side CUDA compilation.
5495     if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
5496       break;
5497     // File-scope asm is ignored during device-side OpenMP compilation.
5498     if (LangOpts.OpenMPIsDevice)
5499       break;
5500     auto *AD = cast<FileScopeAsmDecl>(D);
5501     getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
5502     break;
5503   }
5504 
5505   case Decl::Import: {
5506     auto *Import = cast<ImportDecl>(D);
5507 
5508     // If we've already imported this module, we're done.
5509     if (!ImportedModules.insert(Import->getImportedModule()))
5510       break;
5511 
5512     // Emit debug information for direct imports.
5513     if (!Import->getImportedOwningModule()) {
5514       if (CGDebugInfo *DI = getModuleDebugInfo())
5515         DI->EmitImportDecl(*Import);
5516     }
5517 
5518     // Find all of the submodules and emit the module initializers.
5519     llvm::SmallPtrSet<clang::Module *, 16> Visited;
5520     SmallVector<clang::Module *, 16> Stack;
5521     Visited.insert(Import->getImportedModule());
5522     Stack.push_back(Import->getImportedModule());
5523 
5524     while (!Stack.empty()) {
5525       clang::Module *Mod = Stack.pop_back_val();
5526       if (!EmittedModuleInitializers.insert(Mod).second)
5527         continue;
5528 
5529       for (auto *D : Context.getModuleInitializers(Mod))
5530         EmitTopLevelDecl(D);
5531 
5532       // Visit the submodules of this module.
5533       for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
5534                                              SubEnd = Mod->submodule_end();
5535            Sub != SubEnd; ++Sub) {
5536         // Skip explicit children; they need to be explicitly imported to emit
5537         // the initializers.
5538         if ((*Sub)->IsExplicit)
5539           continue;
5540 
5541         if (Visited.insert(*Sub).second)
5542           Stack.push_back(*Sub);
5543       }
5544     }
5545     break;
5546   }
5547 
5548   case Decl::Export:
5549     EmitDeclContext(cast<ExportDecl>(D));
5550     break;
5551 
5552   case Decl::OMPThreadPrivate:
5553     EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
5554     break;
5555 
5556   case Decl::OMPAllocate:
5557     break;
5558 
5559   case Decl::OMPDeclareReduction:
5560     EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D));
5561     break;
5562 
5563   case Decl::OMPDeclareMapper:
5564     EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D));
5565     break;
5566 
5567   case Decl::OMPRequires:
5568     EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D));
5569     break;
5570 
5571   default:
5572     // Make sure we handled everything we should, every other kind is a
5573     // non-top-level decl.  FIXME: Would be nice to have an isTopLevelDeclKind
5574     // function. Need to recode Decl::Kind to do that easily.
5575     assert(isa<TypeDecl>(D) && "Unsupported decl kind");
5576     break;
5577   }
5578 }
5579 
5580 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
5581   // Do we need to generate coverage mapping?
5582   if (!CodeGenOpts.CoverageMapping)
5583     return;
5584   switch (D->getKind()) {
5585   case Decl::CXXConversion:
5586   case Decl::CXXMethod:
5587   case Decl::Function:
5588   case Decl::ObjCMethod:
5589   case Decl::CXXConstructor:
5590   case Decl::CXXDestructor: {
5591     if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
5592       break;
5593     SourceManager &SM = getContext().getSourceManager();
5594     if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
5595       break;
5596     auto I = DeferredEmptyCoverageMappingDecls.find(D);
5597     if (I == DeferredEmptyCoverageMappingDecls.end())
5598       DeferredEmptyCoverageMappingDecls[D] = true;
5599     break;
5600   }
5601   default:
5602     break;
5603   };
5604 }
5605 
5606 void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) {
5607   // Do we need to generate coverage mapping?
5608   if (!CodeGenOpts.CoverageMapping)
5609     return;
5610   if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
5611     if (Fn->isTemplateInstantiation())
5612       ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
5613   }
5614   auto I = DeferredEmptyCoverageMappingDecls.find(D);
5615   if (I == DeferredEmptyCoverageMappingDecls.end())
5616     DeferredEmptyCoverageMappingDecls[D] = false;
5617   else
5618     I->second = false;
5619 }
5620 
5621 void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
5622   // We call takeVector() here to avoid use-after-free.
5623   // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because
5624   // we deserialize function bodies to emit coverage info for them, and that
5625   // deserializes more declarations. How should we handle that case?
5626   for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) {
5627     if (!Entry.second)
5628       continue;
5629     const Decl *D = Entry.first;
5630     switch (D->getKind()) {
5631     case Decl::CXXConversion:
5632     case Decl::CXXMethod:
5633     case Decl::Function:
5634     case Decl::ObjCMethod: {
5635       CodeGenPGO PGO(*this);
5636       GlobalDecl GD(cast<FunctionDecl>(D));
5637       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
5638                                   getFunctionLinkage(GD));
5639       break;
5640     }
5641     case Decl::CXXConstructor: {
5642       CodeGenPGO PGO(*this);
5643       GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
5644       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
5645                                   getFunctionLinkage(GD));
5646       break;
5647     }
5648     case Decl::CXXDestructor: {
5649       CodeGenPGO PGO(*this);
5650       GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
5651       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
5652                                   getFunctionLinkage(GD));
5653       break;
5654     }
5655     default:
5656       break;
5657     };
5658   }
5659 }
5660 
5661 void CodeGenModule::EmitMainVoidAlias() {
5662   // In order to transition away from "__original_main" gracefully, emit an
5663   // alias for "main" in the no-argument case so that libc can detect when
5664   // new-style no-argument main is in used.
5665   if (llvm::Function *F = getModule().getFunction("main")) {
5666     if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
5667         F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth()))
5668       addUsedGlobal(llvm::GlobalAlias::create("__main_void", F));
5669   }
5670 }
5671 
5672 /// Turns the given pointer into a constant.
5673 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
5674                                           const void *Ptr) {
5675   uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
5676   llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
5677   return llvm::ConstantInt::get(i64, PtrInt);
5678 }
5679 
5680 static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
5681                                    llvm::NamedMDNode *&GlobalMetadata,
5682                                    GlobalDecl D,
5683                                    llvm::GlobalValue *Addr) {
5684   if (!GlobalMetadata)
5685     GlobalMetadata =
5686       CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
5687 
5688   // TODO: should we report variant information for ctors/dtors?
5689   llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
5690                            llvm::ConstantAsMetadata::get(GetPointerConstant(
5691                                CGM.getLLVMContext(), D.getDecl()))};
5692   GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
5693 }
5694 
5695 /// For each function which is declared within an extern "C" region and marked
5696 /// as 'used', but has internal linkage, create an alias from the unmangled
5697 /// name to the mangled name if possible. People expect to be able to refer
5698 /// to such functions with an unmangled name from inline assembly within the
5699 /// same translation unit.
5700 void CodeGenModule::EmitStaticExternCAliases() {
5701   if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases())
5702     return;
5703   for (auto &I : StaticExternCValues) {
5704     IdentifierInfo *Name = I.first;
5705     llvm::GlobalValue *Val = I.second;
5706     if (Val && !getModule().getNamedValue(Name->getName()))
5707       addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
5708   }
5709 }
5710 
5711 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
5712                                              GlobalDecl &Result) const {
5713   auto Res = Manglings.find(MangledName);
5714   if (Res == Manglings.end())
5715     return false;
5716   Result = Res->getValue();
5717   return true;
5718 }
5719 
5720 /// Emits metadata nodes associating all the global values in the
5721 /// current module with the Decls they came from.  This is useful for
5722 /// projects using IR gen as a subroutine.
5723 ///
5724 /// Since there's currently no way to associate an MDNode directly
5725 /// with an llvm::GlobalValue, we create a global named metadata
5726 /// with the name 'clang.global.decl.ptrs'.
5727 void CodeGenModule::EmitDeclMetadata() {
5728   llvm::NamedMDNode *GlobalMetadata = nullptr;
5729 
5730   for (auto &I : MangledDeclNames) {
5731     llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
5732     // Some mangled names don't necessarily have an associated GlobalValue
5733     // in this module, e.g. if we mangled it for DebugInfo.
5734     if (Addr)
5735       EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
5736   }
5737 }
5738 
5739 /// Emits metadata nodes for all the local variables in the current
5740 /// function.
5741 void CodeGenFunction::EmitDeclMetadata() {
5742   if (LocalDeclMap.empty()) return;
5743 
5744   llvm::LLVMContext &Context = getLLVMContext();
5745 
5746   // Find the unique metadata ID for this name.
5747   unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
5748 
5749   llvm::NamedMDNode *GlobalMetadata = nullptr;
5750 
5751   for (auto &I : LocalDeclMap) {
5752     const Decl *D = I.first;
5753     llvm::Value *Addr = I.second.getPointer();
5754     if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
5755       llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
5756       Alloca->setMetadata(
5757           DeclPtrKind, llvm::MDNode::get(
5758                            Context, llvm::ValueAsMetadata::getConstant(DAddr)));
5759     } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
5760       GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
5761       EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
5762     }
5763   }
5764 }
5765 
5766 void CodeGenModule::EmitVersionIdentMetadata() {
5767   llvm::NamedMDNode *IdentMetadata =
5768     TheModule.getOrInsertNamedMetadata("llvm.ident");
5769   std::string Version = getClangFullVersion();
5770   llvm::LLVMContext &Ctx = TheModule.getContext();
5771 
5772   llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
5773   IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
5774 }
5775 
5776 void CodeGenModule::EmitCommandLineMetadata() {
5777   llvm::NamedMDNode *CommandLineMetadata =
5778     TheModule.getOrInsertNamedMetadata("llvm.commandline");
5779   std::string CommandLine = getCodeGenOpts().RecordCommandLine;
5780   llvm::LLVMContext &Ctx = TheModule.getContext();
5781 
5782   llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)};
5783   CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
5784 }
5785 
5786 void CodeGenModule::EmitCoverageFile() {
5787   if (getCodeGenOpts().CoverageDataFile.empty() &&
5788       getCodeGenOpts().CoverageNotesFile.empty())
5789     return;
5790 
5791   llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
5792   if (!CUNode)
5793     return;
5794 
5795   llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
5796   llvm::LLVMContext &Ctx = TheModule.getContext();
5797   auto *CoverageDataFile =
5798       llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile);
5799   auto *CoverageNotesFile =
5800       llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile);
5801   for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
5802     llvm::MDNode *CU = CUNode->getOperand(i);
5803     llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
5804     GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
5805   }
5806 }
5807 
5808 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
5809                                                        bool ForEH) {
5810   // Return a bogus pointer if RTTI is disabled, unless it's for EH.
5811   // FIXME: should we even be calling this method if RTTI is disabled
5812   // and it's not for EH?
5813   if ((!ForEH && !getLangOpts().RTTI) || getLangOpts().CUDAIsDevice ||
5814       (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
5815        getTriple().isNVPTX()))
5816     return llvm::Constant::getNullValue(Int8PtrTy);
5817 
5818   if (ForEH && Ty->isObjCObjectPointerType() &&
5819       LangOpts.ObjCRuntime.isGNUFamily())
5820     return ObjCRuntime->GetEHType(Ty);
5821 
5822   return getCXXABI().getAddrOfRTTIDescriptor(Ty);
5823 }
5824 
5825 void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
5826   // Do not emit threadprivates in simd-only mode.
5827   if (LangOpts.OpenMP && LangOpts.OpenMPSimd)
5828     return;
5829   for (auto RefExpr : D->varlists()) {
5830     auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
5831     bool PerformInit =
5832         VD->getAnyInitializer() &&
5833         !VD->getAnyInitializer()->isConstantInitializer(getContext(),
5834                                                         /*ForRef=*/false);
5835 
5836     Address Addr(GetAddrOfGlobalVar(VD), getContext().getDeclAlign(VD));
5837     if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
5838             VD, Addr, RefExpr->getBeginLoc(), PerformInit))
5839       CXXGlobalInits.push_back(InitFunction);
5840   }
5841 }
5842 
5843 llvm::Metadata *
5844 CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
5845                                             StringRef Suffix) {
5846   llvm::Metadata *&InternalId = Map[T.getCanonicalType()];
5847   if (InternalId)
5848     return InternalId;
5849 
5850   if (isExternallyVisible(T->getLinkage())) {
5851     std::string OutName;
5852     llvm::raw_string_ostream Out(OutName);
5853     getCXXABI().getMangleContext().mangleTypeName(T, Out);
5854     Out << Suffix;
5855 
5856     InternalId = llvm::MDString::get(getLLVMContext(), Out.str());
5857   } else {
5858     InternalId = llvm::MDNode::getDistinct(getLLVMContext(),
5859                                            llvm::ArrayRef<llvm::Metadata *>());
5860   }
5861 
5862   return InternalId;
5863 }
5864 
5865 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) {
5866   return CreateMetadataIdentifierImpl(T, MetadataIdMap, "");
5867 }
5868 
5869 llvm::Metadata *
5870 CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
5871   return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
5872 }
5873 
5874 // Generalize pointer types to a void pointer with the qualifiers of the
5875 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
5876 // generalize to 'const void *' while 'char *' and 'const char **' generalize to
5877 // 'void *'.
5878 static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
5879   if (!Ty->isPointerType())
5880     return Ty;
5881 
5882   return Ctx.getPointerType(
5883       QualType(Ctx.VoidTy).withCVRQualifiers(
5884           Ty->getPointeeType().getCVRQualifiers()));
5885 }
5886 
5887 // Apply type generalization to a FunctionType's return and argument types
5888 static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) {
5889   if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
5890     SmallVector<QualType, 8> GeneralizedParams;
5891     for (auto &Param : FnType->param_types())
5892       GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
5893 
5894     return Ctx.getFunctionType(
5895         GeneralizeType(Ctx, FnType->getReturnType()),
5896         GeneralizedParams, FnType->getExtProtoInfo());
5897   }
5898 
5899   if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
5900     return Ctx.getFunctionNoProtoType(
5901         GeneralizeType(Ctx, FnType->getReturnType()));
5902 
5903   llvm_unreachable("Encountered unknown FunctionType");
5904 }
5905 
5906 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
5907   return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
5908                                       GeneralizedMetadataIdMap, ".generalized");
5909 }
5910 
5911 /// Returns whether this module needs the "all-vtables" type identifier.
5912 bool CodeGenModule::NeedAllVtablesTypeId() const {
5913   // Returns true if at least one of vtable-based CFI checkers is enabled and
5914   // is not in the trapping mode.
5915   return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
5916            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) ||
5917           (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
5918            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) ||
5919           (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
5920            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) ||
5921           (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) &&
5922            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast)));
5923 }
5924 
5925 void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable,
5926                                           CharUnits Offset,
5927                                           const CXXRecordDecl *RD) {
5928   llvm::Metadata *MD =
5929       CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
5930   VTable->addTypeMetadata(Offset.getQuantity(), MD);
5931 
5932   if (CodeGenOpts.SanitizeCfiCrossDso)
5933     if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
5934       VTable->addTypeMetadata(Offset.getQuantity(),
5935                               llvm::ConstantAsMetadata::get(CrossDsoTypeId));
5936 
5937   if (NeedAllVtablesTypeId()) {
5938     llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables");
5939     VTable->addTypeMetadata(Offset.getQuantity(), MD);
5940   }
5941 }
5942 
5943 llvm::SanitizerStatReport &CodeGenModule::getSanStats() {
5944   if (!SanStats)
5945     SanStats = std::make_unique<llvm::SanitizerStatReport>(&getModule());
5946 
5947   return *SanStats;
5948 }
5949 llvm::Value *
5950 CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E,
5951                                                   CodeGenFunction &CGF) {
5952   llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType());
5953   auto SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
5954   auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
5955   return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy,
5956                                 "__translate_sampler_initializer"),
5957                                 {C});
5958 }
5959 
5960 CharUnits CodeGenModule::getNaturalPointeeTypeAlignment(
5961     QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) {
5962   return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
5963                                  /* forPointeeType= */ true);
5964 }
5965 
5966 CharUnits CodeGenModule::getNaturalTypeAlignment(QualType T,
5967                                                  LValueBaseInfo *BaseInfo,
5968                                                  TBAAAccessInfo *TBAAInfo,
5969                                                  bool forPointeeType) {
5970   if (TBAAInfo)
5971     *TBAAInfo = getTBAAAccessInfo(T);
5972 
5973   // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But
5974   // that doesn't return the information we need to compute BaseInfo.
5975 
5976   // Honor alignment typedef attributes even on incomplete types.
5977   // We also honor them straight for C++ class types, even as pointees;
5978   // there's an expressivity gap here.
5979   if (auto TT = T->getAs<TypedefType>()) {
5980     if (auto Align = TT->getDecl()->getMaxAlignment()) {
5981       if (BaseInfo)
5982         *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType);
5983       return getContext().toCharUnitsFromBits(Align);
5984     }
5985   }
5986 
5987   bool AlignForArray = T->isArrayType();
5988 
5989   // Analyze the base element type, so we don't get confused by incomplete
5990   // array types.
5991   T = getContext().getBaseElementType(T);
5992 
5993   if (T->isIncompleteType()) {
5994     // We could try to replicate the logic from
5995     // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
5996     // type is incomplete, so it's impossible to test. We could try to reuse
5997     // getTypeAlignIfKnown, but that doesn't return the information we need
5998     // to set BaseInfo.  So just ignore the possibility that the alignment is
5999     // greater than one.
6000     if (BaseInfo)
6001       *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
6002     return CharUnits::One();
6003   }
6004 
6005   if (BaseInfo)
6006     *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
6007 
6008   CharUnits Alignment;
6009   // For C++ class pointees, we don't know whether we're pointing at a
6010   // base or a complete object, so we generally need to use the
6011   // non-virtual alignment.
6012   const CXXRecordDecl *RD;
6013   if (forPointeeType && !AlignForArray && (RD = T->getAsCXXRecordDecl())) {
6014     Alignment = getClassPointerAlignment(RD);
6015   } else {
6016     Alignment = getContext().getTypeAlignInChars(T);
6017     if (T.getQualifiers().hasUnaligned())
6018       Alignment = CharUnits::One();
6019   }
6020 
6021   // Cap to the global maximum type alignment unless the alignment
6022   // was somehow explicit on the type.
6023   if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) {
6024     if (Alignment.getQuantity() > MaxAlign &&
6025         !getContext().isAlignmentRequired(T))
6026       Alignment = CharUnits::fromQuantity(MaxAlign);
6027   }
6028   return Alignment;
6029 }
6030 
6031 bool CodeGenModule::stopAutoInit() {
6032   unsigned StopAfter = getContext().getLangOpts().TrivialAutoVarInitStopAfter;
6033   if (StopAfter) {
6034     // This number is positive only when -ftrivial-auto-var-init-stop-after=* is
6035     // used
6036     if (NumAutoVarInit >= StopAfter) {
6037       return true;
6038     }
6039     if (!NumAutoVarInit) {
6040       unsigned DiagID = getDiags().getCustomDiagID(
6041           DiagnosticsEngine::Warning,
6042           "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
6043           "number of times ftrivial-auto-var-init=%1 gets applied.");
6044       getDiags().Report(DiagID)
6045           << StopAfter
6046           << (getContext().getLangOpts().getTrivialAutoVarInit() ==
6047                       LangOptions::TrivialAutoVarInitKind::Zero
6048                   ? "zero"
6049                   : "pattern");
6050     }
6051     ++NumAutoVarInit;
6052   }
6053   return false;
6054 }
6055