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