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