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