xref: /llvm-project/clang/lib/CodeGen/CodeGenModule.cpp (revision 8e7f073eb42c92aa7a2b651ca314d7fcebf296e3)
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 = getClangVendor() + "clang";
999     getModule().addModuleFlag(llvm::Module::Error, "zos_product_id",
1000                               llvm::MDString::get(VMContext, ProductId));
1001 
1002     // Record the language because we need it for the PPA2.
1003     StringRef lang_str = languageToString(
1004         LangStandard::getLangStandardForKind(LangOpts.LangStd).Language);
1005     getModule().addModuleFlag(llvm::Module::Error, "zos_cu_language",
1006                               llvm::MDString::get(VMContext, lang_str));
1007 
1008     time_t TT = PreprocessorOpts.SourceDateEpoch
1009                     ? *PreprocessorOpts.SourceDateEpoch
1010                     : std::time(nullptr);
1011     getModule().addModuleFlag(llvm::Module::Max, "zos_translation_time",
1012                               static_cast<uint64_t>(TT));
1013 
1014     // Multiple modes will be supported here.
1015     getModule().addModuleFlag(llvm::Module::Error, "zos_le_char_mode",
1016                               llvm::MDString::get(VMContext, "ascii"));
1017   }
1018 
1019   llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
1020   if (   Arch == llvm::Triple::arm
1021       || Arch == llvm::Triple::armeb
1022       || Arch == llvm::Triple::thumb
1023       || Arch == llvm::Triple::thumbeb) {
1024     // The minimum width of an enum in bytes
1025     uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
1026     getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
1027   }
1028 
1029   if (Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64) {
1030     StringRef ABIStr = Target.getABI();
1031     llvm::LLVMContext &Ctx = TheModule.getContext();
1032     getModule().addModuleFlag(llvm::Module::Error, "target-abi",
1033                               llvm::MDString::get(Ctx, ABIStr));
1034   }
1035 
1036   if (CodeGenOpts.SanitizeCfiCrossDso) {
1037     // Indicate that we want cross-DSO control flow integrity checks.
1038     getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
1039   }
1040 
1041   if (CodeGenOpts.WholeProgramVTables) {
1042     // Indicate whether VFE was enabled for this module, so that the
1043     // vcall_visibility metadata added under whole program vtables is handled
1044     // appropriately in the optimizer.
1045     getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
1046                               CodeGenOpts.VirtualFunctionElimination);
1047   }
1048 
1049   if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
1050     getModule().addModuleFlag(llvm::Module::Override,
1051                               "CFI Canonical Jump Tables",
1052                               CodeGenOpts.SanitizeCfiCanonicalJumpTables);
1053   }
1054 
1055   if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) {
1056     getModule().addModuleFlag(llvm::Module::Override, "kcfi", 1);
1057     // KCFI assumes patchable-function-prefix is the same for all indirectly
1058     // called functions. Store the expected offset for code generation.
1059     if (CodeGenOpts.PatchableFunctionEntryOffset)
1060       getModule().addModuleFlag(llvm::Module::Override, "kcfi-offset",
1061                                 CodeGenOpts.PatchableFunctionEntryOffset);
1062   }
1063 
1064   if (CodeGenOpts.CFProtectionReturn &&
1065       Target.checkCFProtectionReturnSupported(getDiags())) {
1066     // Indicate that we want to instrument return control flow protection.
1067     getModule().addModuleFlag(llvm::Module::Min, "cf-protection-return",
1068                               1);
1069   }
1070 
1071   if (CodeGenOpts.CFProtectionBranch &&
1072       Target.checkCFProtectionBranchSupported(getDiags())) {
1073     // Indicate that we want to instrument branch control flow protection.
1074     getModule().addModuleFlag(llvm::Module::Min, "cf-protection-branch",
1075                               1);
1076   }
1077 
1078   if (CodeGenOpts.FunctionReturnThunks)
1079     getModule().addModuleFlag(llvm::Module::Override, "function_return_thunk_extern", 1);
1080 
1081   if (CodeGenOpts.IndirectBranchCSPrefix)
1082     getModule().addModuleFlag(llvm::Module::Override, "indirect_branch_cs_prefix", 1);
1083 
1084   // Add module metadata for return address signing (ignoring
1085   // non-leaf/all) and stack tagging. These are actually turned on by function
1086   // attributes, but we use module metadata to emit build attributes. This is
1087   // needed for LTO, where the function attributes are inside bitcode
1088   // serialised into a global variable by the time build attributes are
1089   // emitted, so we can't access them. LTO objects could be compiled with
1090   // different flags therefore module flags are set to "Min" behavior to achieve
1091   // the same end result of the normal build where e.g BTI is off if any object
1092   // doesn't support it.
1093   if (Context.getTargetInfo().hasFeature("ptrauth") &&
1094       LangOpts.getSignReturnAddressScope() !=
1095           LangOptions::SignReturnAddressScopeKind::None)
1096     getModule().addModuleFlag(llvm::Module::Override,
1097                               "sign-return-address-buildattr", 1);
1098   if (LangOpts.Sanitize.has(SanitizerKind::MemtagStack))
1099     getModule().addModuleFlag(llvm::Module::Override,
1100                               "tag-stack-memory-buildattr", 1);
1101 
1102   if (Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb ||
1103       Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||
1104       Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_32 ||
1105       Arch == llvm::Triple::aarch64_be) {
1106     if (LangOpts.BranchTargetEnforcement)
1107       getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
1108                                 1);
1109     if (LangOpts.BranchProtectionPAuthLR)
1110       getModule().addModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr",
1111                                 1);
1112     if (LangOpts.GuardedControlStack)
1113       getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 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 || CM == llvm::CodeModel::Large) &&
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 (auto *Attr = D->getAttr<ArmNewAttr>()) {
2384     if (Attr->isNewZA())
2385       B.addAttribute("aarch64_pstate_za_new");
2386   }
2387 
2388   // Track whether we need to add the optnone LLVM attribute,
2389   // starting with the default for this optimization level.
2390   bool ShouldAddOptNone =
2391       !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
2392   // We can't add optnone in the following cases, it won't pass the verifier.
2393   ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
2394   ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
2395 
2396   // Add optnone, but do so only if the function isn't always_inline.
2397   if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) &&
2398       !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2399     B.addAttribute(llvm::Attribute::OptimizeNone);
2400 
2401     // OptimizeNone implies noinline; we should not be inlining such functions.
2402     B.addAttribute(llvm::Attribute::NoInline);
2403 
2404     // We still need to handle naked functions even though optnone subsumes
2405     // much of their semantics.
2406     if (D->hasAttr<NakedAttr>())
2407       B.addAttribute(llvm::Attribute::Naked);
2408 
2409     // OptimizeNone wins over OptimizeForSize and MinSize.
2410     F->removeFnAttr(llvm::Attribute::OptimizeForSize);
2411     F->removeFnAttr(llvm::Attribute::MinSize);
2412   } else if (D->hasAttr<NakedAttr>()) {
2413     // Naked implies noinline: we should not be inlining such functions.
2414     B.addAttribute(llvm::Attribute::Naked);
2415     B.addAttribute(llvm::Attribute::NoInline);
2416   } else if (D->hasAttr<NoDuplicateAttr>()) {
2417     B.addAttribute(llvm::Attribute::NoDuplicate);
2418   } else if (D->hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2419     // Add noinline if the function isn't always_inline.
2420     B.addAttribute(llvm::Attribute::NoInline);
2421   } else if (D->hasAttr<AlwaysInlineAttr>() &&
2422              !F->hasFnAttribute(llvm::Attribute::NoInline)) {
2423     // (noinline wins over always_inline, and we can't specify both in IR)
2424     B.addAttribute(llvm::Attribute::AlwaysInline);
2425   } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2426     // If we're not inlining, then force everything that isn't always_inline to
2427     // carry an explicit noinline attribute.
2428     if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
2429       B.addAttribute(llvm::Attribute::NoInline);
2430   } else {
2431     // Otherwise, propagate the inline hint attribute and potentially use its
2432     // absence to mark things as noinline.
2433     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2434       // Search function and template pattern redeclarations for inline.
2435       auto CheckForInline = [](const FunctionDecl *FD) {
2436         auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
2437           return Redecl->isInlineSpecified();
2438         };
2439         if (any_of(FD->redecls(), CheckRedeclForInline))
2440           return true;
2441         const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2442         if (!Pattern)
2443           return false;
2444         return any_of(Pattern->redecls(), CheckRedeclForInline);
2445       };
2446       if (CheckForInline(FD)) {
2447         B.addAttribute(llvm::Attribute::InlineHint);
2448       } else if (CodeGenOpts.getInlining() ==
2449                      CodeGenOptions::OnlyHintInlining &&
2450                  !FD->isInlined() &&
2451                  !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2452         B.addAttribute(llvm::Attribute::NoInline);
2453       }
2454     }
2455   }
2456 
2457   // Add other optimization related attributes if we are optimizing this
2458   // function.
2459   if (!D->hasAttr<OptimizeNoneAttr>()) {
2460     if (D->hasAttr<ColdAttr>()) {
2461       if (!ShouldAddOptNone)
2462         B.addAttribute(llvm::Attribute::OptimizeForSize);
2463       B.addAttribute(llvm::Attribute::Cold);
2464     }
2465     if (D->hasAttr<HotAttr>())
2466       B.addAttribute(llvm::Attribute::Hot);
2467     if (D->hasAttr<MinSizeAttr>())
2468       B.addAttribute(llvm::Attribute::MinSize);
2469   }
2470 
2471   F->addFnAttrs(B);
2472 
2473   unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
2474   if (alignment)
2475     F->setAlignment(llvm::Align(alignment));
2476 
2477   if (!D->hasAttr<AlignedAttr>())
2478     if (LangOpts.FunctionAlignment)
2479       F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2480 
2481   // Some C++ ABIs require 2-byte alignment for member functions, in order to
2482   // reserve a bit for differentiating between virtual and non-virtual member
2483   // functions. If the current target's C++ ABI requires this and this is a
2484   // member function, set its alignment accordingly.
2485   if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
2486     if (isa<CXXMethodDecl>(D) && F->getPointerAlignment(getDataLayout()) < 2)
2487       F->setAlignment(std::max(llvm::Align(2), F->getAlign().valueOrOne()));
2488   }
2489 
2490   // In the cross-dso CFI mode with canonical jump tables, we want !type
2491   // attributes on definitions only.
2492   if (CodeGenOpts.SanitizeCfiCrossDso &&
2493       CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
2494     if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2495       // Skip available_externally functions. They won't be codegen'ed in the
2496       // current module anyway.
2497       if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
2498         CreateFunctionTypeMetadataForIcall(FD, F);
2499     }
2500   }
2501 
2502   // Emit type metadata on member functions for member function pointer checks.
2503   // These are only ever necessary on definitions; we're guaranteed that the
2504   // definition will be present in the LTO unit as a result of LTO visibility.
2505   auto *MD = dyn_cast<CXXMethodDecl>(D);
2506   if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) {
2507     for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
2508       llvm::Metadata *Id =
2509           CreateMetadataIdentifierForType(Context.getMemberPointerType(
2510               MD->getType(), Context.getRecordType(Base).getTypePtr()));
2511       F->addTypeMetadata(0, Id);
2512     }
2513   }
2514 }
2515 
2516 void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
2517   const Decl *D = GD.getDecl();
2518   if (isa_and_nonnull<NamedDecl>(D))
2519     setGVProperties(GV, GD);
2520   else
2521     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
2522 
2523   if (D && D->hasAttr<UsedAttr>())
2524     addUsedOrCompilerUsedGlobal(GV);
2525 
2526   if (const auto *VD = dyn_cast_if_present<VarDecl>(D);
2527       VD &&
2528       ((CodeGenOpts.KeepPersistentStorageVariables &&
2529         (VD->getStorageDuration() == SD_Static ||
2530          VD->getStorageDuration() == SD_Thread)) ||
2531        (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
2532         VD->getType().isConstQualified())))
2533     addUsedOrCompilerUsedGlobal(GV);
2534 }
2535 
2536 bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
2537                                                 llvm::AttrBuilder &Attrs,
2538                                                 bool SetTargetFeatures) {
2539   // Add target-cpu and target-features attributes to functions. If
2540   // we have a decl for the function and it has a target attribute then
2541   // parse that and add it to the feature set.
2542   StringRef TargetCPU = getTarget().getTargetOpts().CPU;
2543   StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU;
2544   std::vector<std::string> Features;
2545   const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
2546   FD = FD ? FD->getMostRecentDecl() : FD;
2547   const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
2548   const auto *TV = FD ? FD->getAttr<TargetVersionAttr>() : nullptr;
2549   assert((!TD || !TV) && "both target_version and target specified");
2550   const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
2551   const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr;
2552   bool AddedAttr = false;
2553   if (TD || TV || SD || TC) {
2554     llvm::StringMap<bool> FeatureMap;
2555     getContext().getFunctionFeatureMap(FeatureMap, GD);
2556 
2557     // Produce the canonical string for this set of features.
2558     for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
2559       Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
2560 
2561     // Now add the target-cpu and target-features to the function.
2562     // While we populated the feature map above, we still need to
2563     // get and parse the target attribute so we can get the cpu for
2564     // the function.
2565     if (TD) {
2566       ParsedTargetAttr ParsedAttr =
2567           Target.parseTargetAttr(TD->getFeaturesStr());
2568       if (!ParsedAttr.CPU.empty() &&
2569           getTarget().isValidCPUName(ParsedAttr.CPU)) {
2570         TargetCPU = ParsedAttr.CPU;
2571         TuneCPU = ""; // Clear the tune CPU.
2572       }
2573       if (!ParsedAttr.Tune.empty() &&
2574           getTarget().isValidCPUName(ParsedAttr.Tune))
2575         TuneCPU = ParsedAttr.Tune;
2576     }
2577 
2578     if (SD) {
2579       // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
2580       // favor this processor.
2581       TuneCPU = SD->getCPUName(GD.getMultiVersionIndex())->getName();
2582     }
2583   } else {
2584     // Otherwise just add the existing target cpu and target features to the
2585     // function.
2586     Features = getTarget().getTargetOpts().Features;
2587   }
2588 
2589   if (!TargetCPU.empty()) {
2590     Attrs.addAttribute("target-cpu", TargetCPU);
2591     AddedAttr = true;
2592   }
2593   if (!TuneCPU.empty()) {
2594     Attrs.addAttribute("tune-cpu", TuneCPU);
2595     AddedAttr = true;
2596   }
2597   if (!Features.empty() && SetTargetFeatures) {
2598     llvm::erase_if(Features, [&](const std::string& F) {
2599        return getTarget().isReadOnlyFeature(F.substr(1));
2600     });
2601     llvm::sort(Features);
2602     Attrs.addAttribute("target-features", llvm::join(Features, ","));
2603     AddedAttr = true;
2604   }
2605 
2606   return AddedAttr;
2607 }
2608 
2609 void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
2610                                           llvm::GlobalObject *GO) {
2611   const Decl *D = GD.getDecl();
2612   SetCommonAttributes(GD, GO);
2613 
2614   if (D) {
2615     if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
2616       if (D->hasAttr<RetainAttr>())
2617         addUsedGlobal(GV);
2618       if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
2619         GV->addAttribute("bss-section", SA->getName());
2620       if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
2621         GV->addAttribute("data-section", SA->getName());
2622       if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
2623         GV->addAttribute("rodata-section", SA->getName());
2624       if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
2625         GV->addAttribute("relro-section", SA->getName());
2626     }
2627 
2628     if (auto *F = dyn_cast<llvm::Function>(GO)) {
2629       if (D->hasAttr<RetainAttr>())
2630         addUsedGlobal(F);
2631       if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
2632         if (!D->getAttr<SectionAttr>())
2633           F->addFnAttr("implicit-section-name", SA->getName());
2634 
2635       llvm::AttrBuilder Attrs(F->getContext());
2636       if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
2637         // We know that GetCPUAndFeaturesAttributes will always have the
2638         // newest set, since it has the newest possible FunctionDecl, so the
2639         // new ones should replace the old.
2640         llvm::AttributeMask RemoveAttrs;
2641         RemoveAttrs.addAttribute("target-cpu");
2642         RemoveAttrs.addAttribute("target-features");
2643         RemoveAttrs.addAttribute("tune-cpu");
2644         F->removeFnAttrs(RemoveAttrs);
2645         F->addFnAttrs(Attrs);
2646       }
2647     }
2648 
2649     if (const auto *CSA = D->getAttr<CodeSegAttr>())
2650       GO->setSection(CSA->getName());
2651     else if (const auto *SA = D->getAttr<SectionAttr>())
2652       GO->setSection(SA->getName());
2653   }
2654 
2655   getTargetCodeGenInfo().setTargetAttributes(D, GO, *this);
2656 }
2657 
2658 void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD,
2659                                                   llvm::Function *F,
2660                                                   const CGFunctionInfo &FI) {
2661   const Decl *D = GD.getDecl();
2662   SetLLVMFunctionAttributes(GD, FI, F, /*IsThunk=*/false);
2663   SetLLVMFunctionAttributesForDefinition(D, F);
2664 
2665   F->setLinkage(llvm::Function::InternalLinkage);
2666 
2667   setNonAliasAttributes(GD, F);
2668 }
2669 
2670 static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
2671   // Set linkage and visibility in case we never see a definition.
2672   LinkageInfo LV = ND->getLinkageAndVisibility();
2673   // Don't set internal linkage on declarations.
2674   // "extern_weak" is overloaded in LLVM; we probably should have
2675   // separate linkage types for this.
2676   if (isExternallyVisible(LV.getLinkage()) &&
2677       (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
2678     GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2679 }
2680 
2681 void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
2682                                                        llvm::Function *F) {
2683   // Only if we are checking indirect calls.
2684   if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
2685     return;
2686 
2687   // Non-static class methods are handled via vtable or member function pointer
2688   // checks elsewhere.
2689   if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
2690     return;
2691 
2692   llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
2693   F->addTypeMetadata(0, MD);
2694   F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
2695 
2696   // Emit a hash-based bit set entry for cross-DSO calls.
2697   if (CodeGenOpts.SanitizeCfiCrossDso)
2698     if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
2699       F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
2700 }
2701 
2702 void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) {
2703   llvm::LLVMContext &Ctx = F->getContext();
2704   llvm::MDBuilder MDB(Ctx);
2705   F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
2706                  llvm::MDNode::get(
2707                      Ctx, MDB.createConstant(CreateKCFITypeId(FD->getType()))));
2708 }
2709 
2710 static bool allowKCFIIdentifier(StringRef Name) {
2711   // KCFI type identifier constants are only necessary for external assembly
2712   // functions, which means it's safe to skip unusual names. Subset of
2713   // MCAsmInfo::isAcceptableChar() and MCAsmInfoXCOFF::isAcceptableChar().
2714   return llvm::all_of(Name, [](const char &C) {
2715     return llvm::isAlnum(C) || C == '_' || C == '.';
2716   });
2717 }
2718 
2719 void CodeGenModule::finalizeKCFITypes() {
2720   llvm::Module &M = getModule();
2721   for (auto &F : M.functions()) {
2722     // Remove KCFI type metadata from non-address-taken local functions.
2723     bool AddressTaken = F.hasAddressTaken();
2724     if (!AddressTaken && F.hasLocalLinkage())
2725       F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type);
2726 
2727     // Generate a constant with the expected KCFI type identifier for all
2728     // address-taken function declarations to support annotating indirectly
2729     // called assembly functions.
2730     if (!AddressTaken || !F.isDeclaration())
2731       continue;
2732 
2733     const llvm::ConstantInt *Type;
2734     if (const llvm::MDNode *MD = F.getMetadata(llvm::LLVMContext::MD_kcfi_type))
2735       Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0));
2736     else
2737       continue;
2738 
2739     StringRef Name = F.getName();
2740     if (!allowKCFIIdentifier(Name))
2741       continue;
2742 
2743     std::string Asm = (".weak __kcfi_typeid_" + Name + "\n.set __kcfi_typeid_" +
2744                        Name + ", " + Twine(Type->getZExtValue()) + "\n")
2745                           .str();
2746     M.appendModuleInlineAsm(Asm);
2747   }
2748 }
2749 
2750 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
2751                                           bool IsIncompleteFunction,
2752                                           bool IsThunk) {
2753 
2754   if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
2755     // If this is an intrinsic function, set the function's attributes
2756     // to the intrinsic's attributes.
2757     F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
2758     return;
2759   }
2760 
2761   const auto *FD = cast<FunctionDecl>(GD.getDecl());
2762 
2763   if (!IsIncompleteFunction)
2764     SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F,
2765                               IsThunk);
2766 
2767   // Add the Returned attribute for "this", except for iOS 5 and earlier
2768   // where substantial code, including the libstdc++ dylib, was compiled with
2769   // GCC and does not actually return "this".
2770   if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
2771       !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
2772     assert(!F->arg_empty() &&
2773            F->arg_begin()->getType()
2774              ->canLosslesslyBitCastTo(F->getReturnType()) &&
2775            "unexpected this return");
2776     F->addParamAttr(0, llvm::Attribute::Returned);
2777   }
2778 
2779   // Only a few attributes are set on declarations; these may later be
2780   // overridden by a definition.
2781 
2782   setLinkageForGV(F, FD);
2783   setGVProperties(F, FD);
2784 
2785   // Setup target-specific attributes.
2786   if (!IsIncompleteFunction && F->isDeclaration())
2787     getTargetCodeGenInfo().setTargetAttributes(FD, F, *this);
2788 
2789   if (const auto *CSA = FD->getAttr<CodeSegAttr>())
2790     F->setSection(CSA->getName());
2791   else if (const auto *SA = FD->getAttr<SectionAttr>())
2792      F->setSection(SA->getName());
2793 
2794   if (const auto *EA = FD->getAttr<ErrorAttr>()) {
2795     if (EA->isError())
2796       F->addFnAttr("dontcall-error", EA->getUserDiagnostic());
2797     else if (EA->isWarning())
2798       F->addFnAttr("dontcall-warn", EA->getUserDiagnostic());
2799   }
2800 
2801   // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2802   if (FD->isInlineBuiltinDeclaration()) {
2803     const FunctionDecl *FDBody;
2804     bool HasBody = FD->hasBody(FDBody);
2805     (void)HasBody;
2806     assert(HasBody && "Inline builtin declarations should always have an "
2807                       "available body!");
2808     if (shouldEmitFunction(FDBody))
2809       F->addFnAttr(llvm::Attribute::NoBuiltin);
2810   }
2811 
2812   if (FD->isReplaceableGlobalAllocationFunction()) {
2813     // A replaceable global allocation function does not act like a builtin by
2814     // default, only if it is invoked by a new-expression or delete-expression.
2815     F->addFnAttr(llvm::Attribute::NoBuiltin);
2816   }
2817 
2818   if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
2819     F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2820   else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
2821     if (MD->isVirtual())
2822       F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2823 
2824   // Don't emit entries for function declarations in the cross-DSO mode. This
2825   // is handled with better precision by the receiving DSO. But if jump tables
2826   // are non-canonical then we need type metadata in order to produce the local
2827   // jump table.
2828   if (!CodeGenOpts.SanitizeCfiCrossDso ||
2829       !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
2830     CreateFunctionTypeMetadataForIcall(FD, F);
2831 
2832   if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
2833     setKCFIType(FD, F);
2834 
2835   if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
2836     getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
2837 
2838   if (CodeGenOpts.InlineMaxStackSize != UINT_MAX)
2839     F->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize));
2840 
2841   if (const auto *CB = FD->getAttr<CallbackAttr>()) {
2842     // Annotate the callback behavior as metadata:
2843     //  - The callback callee (as argument number).
2844     //  - The callback payloads (as argument numbers).
2845     llvm::LLVMContext &Ctx = F->getContext();
2846     llvm::MDBuilder MDB(Ctx);
2847 
2848     // The payload indices are all but the first one in the encoding. The first
2849     // identifies the callback callee.
2850     int CalleeIdx = *CB->encoding_begin();
2851     ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
2852     F->addMetadata(llvm::LLVMContext::MD_callback,
2853                    *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
2854                                                CalleeIdx, PayloadIndices,
2855                                                /* VarArgsArePassed */ false)}));
2856   }
2857 }
2858 
2859 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
2860   assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2861          "Only globals with definition can force usage.");
2862   LLVMUsed.emplace_back(GV);
2863 }
2864 
2865 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
2866   assert(!GV->isDeclaration() &&
2867          "Only globals with definition can force usage.");
2868   LLVMCompilerUsed.emplace_back(GV);
2869 }
2870 
2871 void CodeGenModule::addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV) {
2872   assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2873          "Only globals with definition can force usage.");
2874   if (getTriple().isOSBinFormatELF())
2875     LLVMCompilerUsed.emplace_back(GV);
2876   else
2877     LLVMUsed.emplace_back(GV);
2878 }
2879 
2880 static void emitUsed(CodeGenModule &CGM, StringRef Name,
2881                      std::vector<llvm::WeakTrackingVH> &List) {
2882   // Don't create llvm.used if there is no need.
2883   if (List.empty())
2884     return;
2885 
2886   // Convert List to what ConstantArray needs.
2887   SmallVector<llvm::Constant*, 8> UsedArray;
2888   UsedArray.resize(List.size());
2889   for (unsigned i = 0, e = List.size(); i != e; ++i) {
2890     UsedArray[i] =
2891         llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2892             cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
2893   }
2894 
2895   if (UsedArray.empty())
2896     return;
2897   llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
2898 
2899   auto *GV = new llvm::GlobalVariable(
2900       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
2901       llvm::ConstantArray::get(ATy, UsedArray), Name);
2902 
2903   GV->setSection("llvm.metadata");
2904 }
2905 
2906 void CodeGenModule::emitLLVMUsed() {
2907   emitUsed(*this, "llvm.used", LLVMUsed);
2908   emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
2909 }
2910 
2911 void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
2912   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
2913   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2914 }
2915 
2916 void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
2917   llvm::SmallString<32> Opt;
2918   getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
2919   if (Opt.empty())
2920     return;
2921   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2922   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2923 }
2924 
2925 void CodeGenModule::AddDependentLib(StringRef Lib) {
2926   auto &C = getLLVMContext();
2927   if (getTarget().getTriple().isOSBinFormatELF()) {
2928       ELFDependentLibraries.push_back(
2929         llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
2930     return;
2931   }
2932 
2933   llvm::SmallString<24> Opt;
2934   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
2935   auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2936   LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
2937 }
2938 
2939 /// Add link options implied by the given module, including modules
2940 /// it depends on, using a postorder walk.
2941 static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
2942                                     SmallVectorImpl<llvm::MDNode *> &Metadata,
2943                                     llvm::SmallPtrSet<Module *, 16> &Visited) {
2944   // Import this module's parent.
2945   if (Mod->Parent && Visited.insert(Mod->Parent).second) {
2946     addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
2947   }
2948 
2949   // Import this module's dependencies.
2950   for (Module *Import : llvm::reverse(Mod->Imports)) {
2951     if (Visited.insert(Import).second)
2952       addLinkOptionsPostorder(CGM, Import, Metadata, Visited);
2953   }
2954 
2955   // Add linker options to link against the libraries/frameworks
2956   // described by this module.
2957   llvm::LLVMContext &Context = CGM.getLLVMContext();
2958   bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
2959 
2960   // For modules that use export_as for linking, use that module
2961   // name instead.
2962   if (Mod->UseExportAsModuleLinkName)
2963     return;
2964 
2965   for (const Module::LinkLibrary &LL : llvm::reverse(Mod->LinkLibraries)) {
2966     // Link against a framework.  Frameworks are currently Darwin only, so we
2967     // don't to ask TargetCodeGenInfo for the spelling of the linker option.
2968     if (LL.IsFramework) {
2969       llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
2970                                  llvm::MDString::get(Context, LL.Library)};
2971 
2972       Metadata.push_back(llvm::MDNode::get(Context, Args));
2973       continue;
2974     }
2975 
2976     // Link against a library.
2977     if (IsELF) {
2978       llvm::Metadata *Args[2] = {
2979           llvm::MDString::get(Context, "lib"),
2980           llvm::MDString::get(Context, LL.Library),
2981       };
2982       Metadata.push_back(llvm::MDNode::get(Context, Args));
2983     } else {
2984       llvm::SmallString<24> Opt;
2985       CGM.getTargetCodeGenInfo().getDependentLibraryOption(LL.Library, Opt);
2986       auto *OptString = llvm::MDString::get(Context, Opt);
2987       Metadata.push_back(llvm::MDNode::get(Context, OptString));
2988     }
2989   }
2990 }
2991 
2992 void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
2993   assert(Primary->isNamedModuleUnit() &&
2994          "We should only emit module initializers for named modules.");
2995 
2996   // Emit the initializers in the order that sub-modules appear in the
2997   // source, first Global Module Fragments, if present.
2998   if (auto GMF = Primary->getGlobalModuleFragment()) {
2999     for (Decl *D : getContext().getModuleInitializers(GMF)) {
3000       if (isa<ImportDecl>(D))
3001         continue;
3002       assert(isa<VarDecl>(D) && "GMF initializer decl is not a var?");
3003       EmitTopLevelDecl(D);
3004     }
3005   }
3006   // Second any associated with the module, itself.
3007   for (Decl *D : getContext().getModuleInitializers(Primary)) {
3008     // Skip import decls, the inits for those are called explicitly.
3009     if (isa<ImportDecl>(D))
3010       continue;
3011     EmitTopLevelDecl(D);
3012   }
3013   // Third any associated with the Privat eMOdule Fragment, if present.
3014   if (auto PMF = Primary->getPrivateModuleFragment()) {
3015     for (Decl *D : getContext().getModuleInitializers(PMF)) {
3016       // Skip import decls, the inits for those are called explicitly.
3017       if (isa<ImportDecl>(D))
3018         continue;
3019       assert(isa<VarDecl>(D) && "PMF initializer decl is not a var?");
3020       EmitTopLevelDecl(D);
3021     }
3022   }
3023 }
3024 
3025 void CodeGenModule::EmitModuleLinkOptions() {
3026   // Collect the set of all of the modules we want to visit to emit link
3027   // options, which is essentially the imported modules and all of their
3028   // non-explicit child modules.
3029   llvm::SetVector<clang::Module *> LinkModules;
3030   llvm::SmallPtrSet<clang::Module *, 16> Visited;
3031   SmallVector<clang::Module *, 16> Stack;
3032 
3033   // Seed the stack with imported modules.
3034   for (Module *M : ImportedModules) {
3035     // Do not add any link flags when an implementation TU of a module imports
3036     // a header of that same module.
3037     if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
3038         !getLangOpts().isCompilingModule())
3039       continue;
3040     if (Visited.insert(M).second)
3041       Stack.push_back(M);
3042   }
3043 
3044   // Find all of the modules to import, making a little effort to prune
3045   // non-leaf modules.
3046   while (!Stack.empty()) {
3047     clang::Module *Mod = Stack.pop_back_val();
3048 
3049     bool AnyChildren = false;
3050 
3051     // Visit the submodules of this module.
3052     for (const auto &SM : Mod->submodules()) {
3053       // Skip explicit children; they need to be explicitly imported to be
3054       // linked against.
3055       if (SM->IsExplicit)
3056         continue;
3057 
3058       if (Visited.insert(SM).second) {
3059         Stack.push_back(SM);
3060         AnyChildren = true;
3061       }
3062     }
3063 
3064     // We didn't find any children, so add this module to the list of
3065     // modules to link against.
3066     if (!AnyChildren) {
3067       LinkModules.insert(Mod);
3068     }
3069   }
3070 
3071   // Add link options for all of the imported modules in reverse topological
3072   // order.  We don't do anything to try to order import link flags with respect
3073   // to linker options inserted by things like #pragma comment().
3074   SmallVector<llvm::MDNode *, 16> MetadataArgs;
3075   Visited.clear();
3076   for (Module *M : LinkModules)
3077     if (Visited.insert(M).second)
3078       addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
3079   std::reverse(MetadataArgs.begin(), MetadataArgs.end());
3080   LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
3081 
3082   // Add the linker options metadata flag.
3083   auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
3084   for (auto *MD : LinkerOptionsMetadata)
3085     NMD->addOperand(MD);
3086 }
3087 
3088 void CodeGenModule::EmitDeferred() {
3089   // Emit deferred declare target declarations.
3090   if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
3091     getOpenMPRuntime().emitDeferredTargetDecls();
3092 
3093   // Emit code for any potentially referenced deferred decls.  Since a
3094   // previously unused static decl may become used during the generation of code
3095   // for a static function, iterate until no changes are made.
3096 
3097   if (!DeferredVTables.empty()) {
3098     EmitDeferredVTables();
3099 
3100     // Emitting a vtable doesn't directly cause more vtables to
3101     // become deferred, although it can cause functions to be
3102     // emitted that then need those vtables.
3103     assert(DeferredVTables.empty());
3104   }
3105 
3106   // Emit CUDA/HIP static device variables referenced by host code only.
3107   // Note we should not clear CUDADeviceVarODRUsedByHost since it is still
3108   // needed for further handling.
3109   if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice)
3110     llvm::append_range(DeferredDeclsToEmit,
3111                        getContext().CUDADeviceVarODRUsedByHost);
3112 
3113   // Stop if we're out of both deferred vtables and deferred declarations.
3114   if (DeferredDeclsToEmit.empty())
3115     return;
3116 
3117   // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
3118   // work, it will not interfere with this.
3119   std::vector<GlobalDecl> CurDeclsToEmit;
3120   CurDeclsToEmit.swap(DeferredDeclsToEmit);
3121 
3122   for (GlobalDecl &D : CurDeclsToEmit) {
3123     // We should call GetAddrOfGlobal with IsForDefinition set to true in order
3124     // to get GlobalValue with exactly the type we need, not something that
3125     // might had been created for another decl with the same mangled name but
3126     // different type.
3127     llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
3128         GetAddrOfGlobal(D, ForDefinition));
3129 
3130     // In case of different address spaces, we may still get a cast, even with
3131     // IsForDefinition equal to true. Query mangled names table to get
3132     // GlobalValue.
3133     if (!GV)
3134       GV = GetGlobalValue(getMangledName(D));
3135 
3136     // Make sure GetGlobalValue returned non-null.
3137     assert(GV);
3138 
3139     // Check to see if we've already emitted this.  This is necessary
3140     // for a couple of reasons: first, decls can end up in the
3141     // deferred-decls queue multiple times, and second, decls can end
3142     // up with definitions in unusual ways (e.g. by an extern inline
3143     // function acquiring a strong function redefinition).  Just
3144     // ignore these cases.
3145     if (!GV->isDeclaration())
3146       continue;
3147 
3148     // If this is OpenMP, check if it is legal to emit this global normally.
3149     if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
3150       continue;
3151 
3152     // Otherwise, emit the definition and move on to the next one.
3153     EmitGlobalDefinition(D, GV);
3154 
3155     // If we found out that we need to emit more decls, do that recursively.
3156     // This has the advantage that the decls are emitted in a DFS and related
3157     // ones are close together, which is convenient for testing.
3158     if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
3159       EmitDeferred();
3160       assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
3161     }
3162   }
3163 }
3164 
3165 void CodeGenModule::EmitVTablesOpportunistically() {
3166   // Try to emit external vtables as available_externally if they have emitted
3167   // all inlined virtual functions.  It runs after EmitDeferred() and therefore
3168   // is not allowed to create new references to things that need to be emitted
3169   // lazily. Note that it also uses fact that we eagerly emitting RTTI.
3170 
3171   assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
3172          && "Only emit opportunistic vtables with optimizations");
3173 
3174   for (const CXXRecordDecl *RD : OpportunisticVTables) {
3175     assert(getVTables().isVTableExternal(RD) &&
3176            "This queue should only contain external vtables");
3177     if (getCXXABI().canSpeculativelyEmitVTable(RD))
3178       VTables.GenerateClassData(RD);
3179   }
3180   OpportunisticVTables.clear();
3181 }
3182 
3183 void CodeGenModule::EmitGlobalAnnotations() {
3184   for (const auto& [MangledName, VD] : DeferredAnnotations) {
3185     llvm::GlobalValue *GV = GetGlobalValue(MangledName);
3186     if (GV)
3187       AddGlobalAnnotations(VD, GV);
3188   }
3189   DeferredAnnotations.clear();
3190 
3191   if (Annotations.empty())
3192     return;
3193 
3194   // Create a new global variable for the ConstantStruct in the Module.
3195   llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
3196     Annotations[0]->getType(), Annotations.size()), Annotations);
3197   auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
3198                                       llvm::GlobalValue::AppendingLinkage,
3199                                       Array, "llvm.global.annotations");
3200   gv->setSection(AnnotationSection);
3201 }
3202 
3203 llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
3204   llvm::Constant *&AStr = AnnotationStrings[Str];
3205   if (AStr)
3206     return AStr;
3207 
3208   // Not found yet, create a new global.
3209   llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
3210   auto *gv = new llvm::GlobalVariable(
3211       getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s,
3212       ".str", nullptr, llvm::GlobalValue::NotThreadLocal,
3213       ConstGlobalsPtrTy->getAddressSpace());
3214   gv->setSection(AnnotationSection);
3215   gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3216   AStr = gv;
3217   return gv;
3218 }
3219 
3220 llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
3221   SourceManager &SM = getContext().getSourceManager();
3222   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
3223   if (PLoc.isValid())
3224     return EmitAnnotationString(PLoc.getFilename());
3225   return EmitAnnotationString(SM.getBufferName(Loc));
3226 }
3227 
3228 llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
3229   SourceManager &SM = getContext().getSourceManager();
3230   PresumedLoc PLoc = SM.getPresumedLoc(L);
3231   unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
3232     SM.getExpansionLineNumber(L);
3233   return llvm::ConstantInt::get(Int32Ty, LineNo);
3234 }
3235 
3236 llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
3237   ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
3238   if (Exprs.empty())
3239     return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy);
3240 
3241   llvm::FoldingSetNodeID ID;
3242   for (Expr *E : Exprs) {
3243     ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
3244   }
3245   llvm::Constant *&Lookup = AnnotationArgs[ID.ComputeHash()];
3246   if (Lookup)
3247     return Lookup;
3248 
3249   llvm::SmallVector<llvm::Constant *, 4> LLVMArgs;
3250   LLVMArgs.reserve(Exprs.size());
3251   ConstantEmitter ConstEmiter(*this);
3252   llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](const Expr *E) {
3253     const auto *CE = cast<clang::ConstantExpr>(E);
3254     return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
3255                                     CE->getType());
3256   });
3257   auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
3258   auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true,
3259                                       llvm::GlobalValue::PrivateLinkage, Struct,
3260                                       ".args");
3261   GV->setSection(AnnotationSection);
3262   GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3263 
3264   Lookup = GV;
3265   return GV;
3266 }
3267 
3268 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
3269                                                 const AnnotateAttr *AA,
3270                                                 SourceLocation L) {
3271   // Get the globals for file name, annotation, and the line number.
3272   llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
3273                  *UnitGV = EmitAnnotationUnit(L),
3274                  *LineNoCst = EmitAnnotationLineNo(L),
3275                  *Args = EmitAnnotationArgs(AA);
3276 
3277   llvm::Constant *GVInGlobalsAS = GV;
3278   if (GV->getAddressSpace() !=
3279       getDataLayout().getDefaultGlobalsAddressSpace()) {
3280     GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast(
3281         GV,
3282         llvm::PointerType::get(
3283             GV->getContext(), getDataLayout().getDefaultGlobalsAddressSpace()));
3284   }
3285 
3286   // Create the ConstantStruct for the global annotation.
3287   llvm::Constant *Fields[] = {
3288       GVInGlobalsAS, AnnoGV, UnitGV, LineNoCst, Args,
3289   };
3290   return llvm::ConstantStruct::getAnon(Fields);
3291 }
3292 
3293 void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
3294                                          llvm::GlobalValue *GV) {
3295   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
3296   // Get the struct elements for these annotations.
3297   for (const auto *I : D->specific_attrs<AnnotateAttr>())
3298     Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
3299 }
3300 
3301 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
3302                                        SourceLocation Loc) const {
3303   const auto &NoSanitizeL = getContext().getNoSanitizeList();
3304   // NoSanitize by function name.
3305   if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
3306     return true;
3307   // NoSanitize by location. Check "mainfile" prefix.
3308   auto &SM = Context.getSourceManager();
3309   FileEntryRef MainFile = *SM.getFileEntryRefForID(SM.getMainFileID());
3310   if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
3311     return true;
3312 
3313   // Check "src" prefix.
3314   if (Loc.isValid())
3315     return NoSanitizeL.containsLocation(Kind, Loc);
3316   // If location is unknown, this may be a compiler-generated function. Assume
3317   // it's located in the main file.
3318   return NoSanitizeL.containsFile(Kind, MainFile.getName());
3319 }
3320 
3321 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind,
3322                                        llvm::GlobalVariable *GV,
3323                                        SourceLocation Loc, QualType Ty,
3324                                        StringRef Category) const {
3325   const auto &NoSanitizeL = getContext().getNoSanitizeList();
3326   if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
3327     return true;
3328   auto &SM = Context.getSourceManager();
3329   if (NoSanitizeL.containsMainFile(
3330           Kind, SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
3331           Category))
3332     return true;
3333   if (NoSanitizeL.containsLocation(Kind, Loc, Category))
3334     return true;
3335 
3336   // Check global type.
3337   if (!Ty.isNull()) {
3338     // Drill down the array types: if global variable of a fixed type is
3339     // not sanitized, we also don't instrument arrays of them.
3340     while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
3341       Ty = AT->getElementType();
3342     Ty = Ty.getCanonicalType().getUnqualifiedType();
3343     // Only record types (classes, structs etc.) are ignored.
3344     if (Ty->isRecordType()) {
3345       std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
3346       if (NoSanitizeL.containsType(Kind, TypeStr, Category))
3347         return true;
3348     }
3349   }
3350   return false;
3351 }
3352 
3353 bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
3354                                    StringRef Category) const {
3355   const auto &XRayFilter = getContext().getXRayFilter();
3356   using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
3357   auto Attr = ImbueAttr::NONE;
3358   if (Loc.isValid())
3359     Attr = XRayFilter.shouldImbueLocation(Loc, Category);
3360   if (Attr == ImbueAttr::NONE)
3361     Attr = XRayFilter.shouldImbueFunction(Fn->getName());
3362   switch (Attr) {
3363   case ImbueAttr::NONE:
3364     return false;
3365   case ImbueAttr::ALWAYS:
3366     Fn->addFnAttr("function-instrument", "xray-always");
3367     break;
3368   case ImbueAttr::ALWAYS_ARG1:
3369     Fn->addFnAttr("function-instrument", "xray-always");
3370     Fn->addFnAttr("xray-log-args", "1");
3371     break;
3372   case ImbueAttr::NEVER:
3373     Fn->addFnAttr("function-instrument", "xray-never");
3374     break;
3375   }
3376   return true;
3377 }
3378 
3379 ProfileList::ExclusionType
3380 CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
3381                                               SourceLocation Loc) const {
3382   const auto &ProfileList = getContext().getProfileList();
3383   // If the profile list is empty, then instrument everything.
3384   if (ProfileList.isEmpty())
3385     return ProfileList::Allow;
3386   CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
3387   // First, check the function name.
3388   if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
3389     return *V;
3390   // Next, check the source location.
3391   if (Loc.isValid())
3392     if (auto V = ProfileList.isLocationExcluded(Loc, Kind))
3393       return *V;
3394   // If location is unknown, this may be a compiler-generated function. Assume
3395   // it's located in the main file.
3396   auto &SM = Context.getSourceManager();
3397   if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID()))
3398     if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind))
3399       return *V;
3400   return ProfileList.getDefault(Kind);
3401 }
3402 
3403 ProfileList::ExclusionType
3404 CodeGenModule::isFunctionBlockedFromProfileInstr(llvm::Function *Fn,
3405                                                  SourceLocation Loc) const {
3406   auto V = isFunctionBlockedByProfileList(Fn, Loc);
3407   if (V != ProfileList::Allow)
3408     return V;
3409 
3410   auto NumGroups = getCodeGenOpts().ProfileTotalFunctionGroups;
3411   if (NumGroups > 1) {
3412     auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups;
3413     if (Group != getCodeGenOpts().ProfileSelectedFunctionGroup)
3414       return ProfileList::Skip;
3415   }
3416   return ProfileList::Allow;
3417 }
3418 
3419 bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
3420   // Never defer when EmitAllDecls is specified.
3421   if (LangOpts.EmitAllDecls)
3422     return true;
3423 
3424   const auto *VD = dyn_cast<VarDecl>(Global);
3425   if (VD &&
3426       ((CodeGenOpts.KeepPersistentStorageVariables &&
3427         (VD->getStorageDuration() == SD_Static ||
3428          VD->getStorageDuration() == SD_Thread)) ||
3429        (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
3430         VD->getType().isConstQualified())))
3431     return true;
3432 
3433   return getContext().DeclMustBeEmitted(Global);
3434 }
3435 
3436 bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
3437   // In OpenMP 5.0 variables and function may be marked as
3438   // device_type(host/nohost) and we should not emit them eagerly unless we sure
3439   // that they must be emitted on the host/device. To be sure we need to have
3440   // seen a declare target with an explicit mentioning of the function, we know
3441   // we have if the level of the declare target attribute is -1. Note that we
3442   // check somewhere else if we should emit this at all.
3443   if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd) {
3444     std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
3445         OMPDeclareTargetDeclAttr::getActiveAttr(Global);
3446     if (!ActiveAttr || (*ActiveAttr)->getLevel() != (unsigned)-1)
3447       return false;
3448   }
3449 
3450   if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3451     if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
3452       // Implicit template instantiations may change linkage if they are later
3453       // explicitly instantiated, so they should not be emitted eagerly.
3454       return false;
3455   }
3456   if (const auto *VD = dyn_cast<VarDecl>(Global)) {
3457     if (Context.getInlineVariableDefinitionKind(VD) ==
3458         ASTContext::InlineVariableDefinitionKind::WeakUnknown)
3459       // A definition of an inline constexpr static data member may change
3460       // linkage later if it's redeclared outside the class.
3461       return false;
3462     if (CXX20ModuleInits && VD->getOwningModule() &&
3463         !VD->getOwningModule()->isModuleMapModule()) {
3464       // For CXX20, module-owned initializers need to be deferred, since it is
3465       // not known at this point if they will be run for the current module or
3466       // as part of the initializer for an imported one.
3467       return false;
3468     }
3469   }
3470   // If OpenMP is enabled and threadprivates must be generated like TLS, delay
3471   // codegen for global variables, because they may be marked as threadprivate.
3472   if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
3473       getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
3474       !Global->getType().isConstantStorage(getContext(), false, false) &&
3475       !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
3476     return false;
3477 
3478   return true;
3479 }
3480 
3481 ConstantAddress CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl *GD) {
3482   StringRef Name = getMangledName(GD);
3483 
3484   // The UUID descriptor should be pointer aligned.
3485   CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes);
3486 
3487   // Look for an existing global.
3488   if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3489     return ConstantAddress(GV, GV->getValueType(), Alignment);
3490 
3491   ConstantEmitter Emitter(*this);
3492   llvm::Constant *Init;
3493 
3494   APValue &V = GD->getAsAPValue();
3495   if (!V.isAbsent()) {
3496     // If possible, emit the APValue version of the initializer. In particular,
3497     // this gets the type of the constant right.
3498     Init = Emitter.emitForInitializer(
3499         GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
3500   } else {
3501     // As a fallback, directly construct the constant.
3502     // FIXME: This may get padding wrong under esoteric struct layout rules.
3503     // MSVC appears to create a complete type 'struct __s_GUID' that it
3504     // presumably uses to represent these constants.
3505     MSGuidDecl::Parts Parts = GD->getParts();
3506     llvm::Constant *Fields[4] = {
3507         llvm::ConstantInt::get(Int32Ty, Parts.Part1),
3508         llvm::ConstantInt::get(Int16Ty, Parts.Part2),
3509         llvm::ConstantInt::get(Int16Ty, Parts.Part3),
3510         llvm::ConstantDataArray::getRaw(
3511             StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8,
3512             Int8Ty)};
3513     Init = llvm::ConstantStruct::getAnon(Fields);
3514   }
3515 
3516   auto *GV = new llvm::GlobalVariable(
3517       getModule(), Init->getType(),
3518       /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
3519   if (supportsCOMDAT())
3520     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3521   setDSOLocal(GV);
3522 
3523   if (!V.isAbsent()) {
3524     Emitter.finalize(GV);
3525     return ConstantAddress(GV, GV->getValueType(), Alignment);
3526   }
3527 
3528   llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
3529   return ConstantAddress(GV, Ty, Alignment);
3530 }
3531 
3532 ConstantAddress CodeGenModule::GetAddrOfUnnamedGlobalConstantDecl(
3533     const UnnamedGlobalConstantDecl *GCD) {
3534   CharUnits Alignment = getContext().getTypeAlignInChars(GCD->getType());
3535 
3536   llvm::GlobalVariable **Entry = nullptr;
3537   Entry = &UnnamedGlobalConstantDeclMap[GCD];
3538   if (*Entry)
3539     return ConstantAddress(*Entry, (*Entry)->getValueType(), Alignment);
3540 
3541   ConstantEmitter Emitter(*this);
3542   llvm::Constant *Init;
3543 
3544   const APValue &V = GCD->getValue();
3545 
3546   assert(!V.isAbsent());
3547   Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(),
3548                                     GCD->getType());
3549 
3550   auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3551                                       /*isConstant=*/true,
3552                                       llvm::GlobalValue::PrivateLinkage, Init,
3553                                       ".constant");
3554   GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3555   GV->setAlignment(Alignment.getAsAlign());
3556 
3557   Emitter.finalize(GV);
3558 
3559   *Entry = GV;
3560   return ConstantAddress(GV, GV->getValueType(), Alignment);
3561 }
3562 
3563 ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
3564     const TemplateParamObjectDecl *TPO) {
3565   StringRef Name = getMangledName(TPO);
3566   CharUnits Alignment = getNaturalTypeAlignment(TPO->getType());
3567 
3568   if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3569     return ConstantAddress(GV, GV->getValueType(), Alignment);
3570 
3571   ConstantEmitter Emitter(*this);
3572   llvm::Constant *Init = Emitter.emitForInitializer(
3573         TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
3574 
3575   if (!Init) {
3576     ErrorUnsupported(TPO, "template parameter object");
3577     return ConstantAddress::invalid();
3578   }
3579 
3580   llvm::GlobalValue::LinkageTypes Linkage =
3581       isExternallyVisible(TPO->getLinkageAndVisibility().getLinkage())
3582           ? llvm::GlobalValue::LinkOnceODRLinkage
3583           : llvm::GlobalValue::InternalLinkage;
3584   auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3585                                       /*isConstant=*/true, Linkage, Init, Name);
3586   setGVProperties(GV, TPO);
3587   if (supportsCOMDAT())
3588     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3589   Emitter.finalize(GV);
3590 
3591     return ConstantAddress(GV, GV->getValueType(), Alignment);
3592 }
3593 
3594 ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
3595   const AliasAttr *AA = VD->getAttr<AliasAttr>();
3596   assert(AA && "No alias?");
3597 
3598   CharUnits Alignment = getContext().getDeclAlign(VD);
3599   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
3600 
3601   // See if there is already something with the target's name in the module.
3602   llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
3603   if (Entry)
3604     return ConstantAddress(Entry, DeclTy, Alignment);
3605 
3606   llvm::Constant *Aliasee;
3607   if (isa<llvm::FunctionType>(DeclTy))
3608     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
3609                                       GlobalDecl(cast<FunctionDecl>(VD)),
3610                                       /*ForVTable=*/false);
3611   else
3612     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
3613                                     nullptr);
3614 
3615   auto *F = cast<llvm::GlobalValue>(Aliasee);
3616   F->setLinkage(llvm::Function::ExternalWeakLinkage);
3617   WeakRefReferences.insert(F);
3618 
3619   return ConstantAddress(Aliasee, DeclTy, Alignment);
3620 }
3621 
3622 template <typename AttrT> static bool hasImplicitAttr(const ValueDecl *D) {
3623   if (!D)
3624     return false;
3625   if (auto *A = D->getAttr<AttrT>())
3626     return A->isImplicit();
3627   return D->isImplicit();
3628 }
3629 
3630 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
3631   const auto *Global = cast<ValueDecl>(GD.getDecl());
3632 
3633   // Weak references don't produce any output by themselves.
3634   if (Global->hasAttr<WeakRefAttr>())
3635     return;
3636 
3637   // If this is an alias definition (which otherwise looks like a declaration)
3638   // emit it now.
3639   if (Global->hasAttr<AliasAttr>())
3640     return EmitAliasDefinition(GD);
3641 
3642   // IFunc like an alias whose value is resolved at runtime by calling resolver.
3643   if (Global->hasAttr<IFuncAttr>())
3644     return emitIFuncDefinition(GD);
3645 
3646   // If this is a cpu_dispatch multiversion function, emit the resolver.
3647   if (Global->hasAttr<CPUDispatchAttr>())
3648     return emitCPUDispatchDefinition(GD);
3649 
3650   // If this is CUDA, be selective about which declarations we emit.
3651   // Non-constexpr non-lambda implicit host device functions are not emitted
3652   // unless they are used on device side.
3653   if (LangOpts.CUDA) {
3654     if (LangOpts.CUDAIsDevice) {
3655       const auto *FD = dyn_cast<FunctionDecl>(Global);
3656       if ((!Global->hasAttr<CUDADeviceAttr>() ||
3657            (LangOpts.OffloadImplicitHostDeviceTemplates && FD &&
3658             hasImplicitAttr<CUDAHostAttr>(FD) &&
3659             hasImplicitAttr<CUDADeviceAttr>(FD) && !FD->isConstexpr() &&
3660             !isLambdaCallOperator(FD) &&
3661             !getContext().CUDAImplicitHostDeviceFunUsedByDevice.count(FD))) &&
3662           !Global->hasAttr<CUDAGlobalAttr>() &&
3663           !Global->hasAttr<CUDAConstantAttr>() &&
3664           !Global->hasAttr<CUDASharedAttr>() &&
3665           !Global->getType()->isCUDADeviceBuiltinSurfaceType() &&
3666           !Global->getType()->isCUDADeviceBuiltinTextureType() &&
3667           !(LangOpts.HIPStdPar && isa<FunctionDecl>(Global) &&
3668             !Global->hasAttr<CUDAHostAttr>()))
3669         return;
3670     } else {
3671       // We need to emit host-side 'shadows' for all global
3672       // device-side variables because the CUDA runtime needs their
3673       // size and host-side address in order to provide access to
3674       // their device-side incarnations.
3675 
3676       // So device-only functions are the only things we skip.
3677       if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() &&
3678           Global->hasAttr<CUDADeviceAttr>())
3679         return;
3680 
3681       assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
3682              "Expected Variable or Function");
3683     }
3684   }
3685 
3686   if (LangOpts.OpenMP) {
3687     // If this is OpenMP, check if it is legal to emit this global normally.
3688     if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
3689       return;
3690     if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
3691       if (MustBeEmitted(Global))
3692         EmitOMPDeclareReduction(DRD);
3693       return;
3694     }
3695     if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
3696       if (MustBeEmitted(Global))
3697         EmitOMPDeclareMapper(DMD);
3698       return;
3699     }
3700   }
3701 
3702   // Ignore declarations, they will be emitted on their first use.
3703   if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3704     // Update deferred annotations with the latest declaration if the function
3705     // function was already used or defined.
3706     if (FD->hasAttr<AnnotateAttr>()) {
3707       StringRef MangledName = getMangledName(GD);
3708       if (GetGlobalValue(MangledName))
3709         DeferredAnnotations[MangledName] = FD;
3710     }
3711 
3712     // Forward declarations are emitted lazily on first use.
3713     if (!FD->doesThisDeclarationHaveABody()) {
3714       if (!FD->doesDeclarationForceExternallyVisibleDefinition())
3715         return;
3716 
3717       StringRef MangledName = getMangledName(GD);
3718 
3719       // Compute the function info and LLVM type.
3720       const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
3721       llvm::Type *Ty = getTypes().GetFunctionType(FI);
3722 
3723       GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
3724                               /*DontDefer=*/false);
3725       return;
3726     }
3727   } else {
3728     const auto *VD = cast<VarDecl>(Global);
3729     assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
3730     if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
3731         !Context.isMSStaticDataMemberInlineDefinition(VD)) {
3732       if (LangOpts.OpenMP) {
3733         // Emit declaration of the must-be-emitted declare target variable.
3734         if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3735                 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
3736 
3737           // If this variable has external storage and doesn't require special
3738           // link handling we defer to its canonical definition.
3739           if (VD->hasExternalStorage() &&
3740               Res != OMPDeclareTargetDeclAttr::MT_Link)
3741             return;
3742 
3743           bool UnifiedMemoryEnabled =
3744               getOpenMPRuntime().hasRequiresUnifiedSharedMemory();
3745           if ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3746                *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3747               !UnifiedMemoryEnabled) {
3748             (void)GetAddrOfGlobalVar(VD);
3749           } else {
3750             assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
3751                     ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3752                       *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3753                      UnifiedMemoryEnabled)) &&
3754                    "Link clause or to clause with unified memory expected.");
3755             (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
3756           }
3757 
3758           return;
3759         }
3760       }
3761       // If this declaration may have caused an inline variable definition to
3762       // change linkage, make sure that it's emitted.
3763       if (Context.getInlineVariableDefinitionKind(VD) ==
3764           ASTContext::InlineVariableDefinitionKind::Strong)
3765         GetAddrOfGlobalVar(VD);
3766       return;
3767     }
3768   }
3769 
3770   // Defer code generation to first use when possible, e.g. if this is an inline
3771   // function. If the global must always be emitted, do it eagerly if possible
3772   // to benefit from cache locality.
3773   if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
3774     // Emit the definition if it can't be deferred.
3775     EmitGlobalDefinition(GD);
3776     addEmittedDeferredDecl(GD);
3777     return;
3778   }
3779 
3780   // If we're deferring emission of a C++ variable with an
3781   // initializer, remember the order in which it appeared in the file.
3782   if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
3783       cast<VarDecl>(Global)->hasInit()) {
3784     DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
3785     CXXGlobalInits.push_back(nullptr);
3786   }
3787 
3788   StringRef MangledName = getMangledName(GD);
3789   if (GetGlobalValue(MangledName) != nullptr) {
3790     // The value has already been used and should therefore be emitted.
3791     addDeferredDeclToEmit(GD);
3792   } else if (MustBeEmitted(Global)) {
3793     // The value must be emitted, but cannot be emitted eagerly.
3794     assert(!MayBeEmittedEagerly(Global));
3795     addDeferredDeclToEmit(GD);
3796   } else {
3797     // Otherwise, remember that we saw a deferred decl with this name.  The
3798     // first use of the mangled name will cause it to move into
3799     // DeferredDeclsToEmit.
3800     DeferredDecls[MangledName] = GD;
3801   }
3802 }
3803 
3804 // Check if T is a class type with a destructor that's not dllimport.
3805 static bool HasNonDllImportDtor(QualType T) {
3806   if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
3807     if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
3808       if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
3809         return true;
3810 
3811   return false;
3812 }
3813 
3814 namespace {
3815   struct FunctionIsDirectlyRecursive
3816       : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
3817     const StringRef Name;
3818     const Builtin::Context &BI;
3819     FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
3820         : Name(N), BI(C) {}
3821 
3822     bool VisitCallExpr(const CallExpr *E) {
3823       const FunctionDecl *FD = E->getDirectCallee();
3824       if (!FD)
3825         return false;
3826       AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3827       if (Attr && Name == Attr->getLabel())
3828         return true;
3829       unsigned BuiltinID = FD->getBuiltinID();
3830       if (!BuiltinID || !BI.isLibFunction(BuiltinID))
3831         return false;
3832       StringRef BuiltinName = BI.getName(BuiltinID);
3833       if (BuiltinName.starts_with("__builtin_") &&
3834           Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
3835         return true;
3836       }
3837       return false;
3838     }
3839 
3840     bool VisitStmt(const Stmt *S) {
3841       for (const Stmt *Child : S->children())
3842         if (Child && this->Visit(Child))
3843           return true;
3844       return false;
3845     }
3846   };
3847 
3848   // Make sure we're not referencing non-imported vars or functions.
3849   struct DLLImportFunctionVisitor
3850       : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
3851     bool SafeToInline = true;
3852 
3853     bool shouldVisitImplicitCode() const { return true; }
3854 
3855     bool VisitVarDecl(VarDecl *VD) {
3856       if (VD->getTLSKind()) {
3857         // A thread-local variable cannot be imported.
3858         SafeToInline = false;
3859         return SafeToInline;
3860       }
3861 
3862       // A variable definition might imply a destructor call.
3863       if (VD->isThisDeclarationADefinition())
3864         SafeToInline = !HasNonDllImportDtor(VD->getType());
3865 
3866       return SafeToInline;
3867     }
3868 
3869     bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3870       if (const auto *D = E->getTemporary()->getDestructor())
3871         SafeToInline = D->hasAttr<DLLImportAttr>();
3872       return SafeToInline;
3873     }
3874 
3875     bool VisitDeclRefExpr(DeclRefExpr *E) {
3876       ValueDecl *VD = E->getDecl();
3877       if (isa<FunctionDecl>(VD))
3878         SafeToInline = VD->hasAttr<DLLImportAttr>();
3879       else if (VarDecl *V = dyn_cast<VarDecl>(VD))
3880         SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
3881       return SafeToInline;
3882     }
3883 
3884     bool VisitCXXConstructExpr(CXXConstructExpr *E) {
3885       SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
3886       return SafeToInline;
3887     }
3888 
3889     bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
3890       CXXMethodDecl *M = E->getMethodDecl();
3891       if (!M) {
3892         // Call through a pointer to member function. This is safe to inline.
3893         SafeToInline = true;
3894       } else {
3895         SafeToInline = M->hasAttr<DLLImportAttr>();
3896       }
3897       return SafeToInline;
3898     }
3899 
3900     bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
3901       SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
3902       return SafeToInline;
3903     }
3904 
3905     bool VisitCXXNewExpr(CXXNewExpr *E) {
3906       SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
3907       return SafeToInline;
3908     }
3909   };
3910 }
3911 
3912 // isTriviallyRecursive - Check if this function calls another
3913 // decl that, because of the asm attribute or the other decl being a builtin,
3914 // ends up pointing to itself.
3915 bool
3916 CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
3917   StringRef Name;
3918   if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
3919     // asm labels are a special kind of mangling we have to support.
3920     AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3921     if (!Attr)
3922       return false;
3923     Name = Attr->getLabel();
3924   } else {
3925     Name = FD->getName();
3926   }
3927 
3928   FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
3929   const Stmt *Body = FD->getBody();
3930   return Body ? Walker.Visit(Body) : false;
3931 }
3932 
3933 bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
3934   if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
3935     return true;
3936 
3937   const auto *F = cast<FunctionDecl>(GD.getDecl());
3938   if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
3939     return false;
3940 
3941   // We don't import function bodies from other named module units since that
3942   // behavior may break ABI compatibility of the current unit.
3943   if (const Module *M = F->getOwningModule();
3944       M && M->getTopLevelModule()->isNamedModule() &&
3945       getContext().getCurrentNamedModule() != M->getTopLevelModule() &&
3946       !F->hasAttr<AlwaysInlineAttr>())
3947     return false;
3948 
3949   if (F->hasAttr<NoInlineAttr>())
3950     return false;
3951 
3952   if (F->hasAttr<DLLImportAttr>() && !F->hasAttr<AlwaysInlineAttr>()) {
3953     // Check whether it would be safe to inline this dllimport function.
3954     DLLImportFunctionVisitor Visitor;
3955     Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
3956     if (!Visitor.SafeToInline)
3957       return false;
3958 
3959     if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
3960       // Implicit destructor invocations aren't captured in the AST, so the
3961       // check above can't see them. Check for them manually here.
3962       for (const Decl *Member : Dtor->getParent()->decls())
3963         if (isa<FieldDecl>(Member))
3964           if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
3965             return false;
3966       for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
3967         if (HasNonDllImportDtor(B.getType()))
3968           return false;
3969     }
3970   }
3971 
3972   // Inline builtins declaration must be emitted. They often are fortified
3973   // functions.
3974   if (F->isInlineBuiltinDeclaration())
3975     return true;
3976 
3977   // PR9614. Avoid cases where the source code is lying to us. An available
3978   // externally function should have an equivalent function somewhere else,
3979   // but a function that calls itself through asm label/`__builtin_` trickery is
3980   // clearly not equivalent to the real implementation.
3981   // This happens in glibc's btowc and in some configure checks.
3982   return !isTriviallyRecursive(F);
3983 }
3984 
3985 bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
3986   return CodeGenOpts.OptimizationLevel > 0;
3987 }
3988 
3989 void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
3990                                                        llvm::GlobalValue *GV) {
3991   const auto *FD = cast<FunctionDecl>(GD.getDecl());
3992 
3993   if (FD->isCPUSpecificMultiVersion()) {
3994     auto *Spec = FD->getAttr<CPUSpecificAttr>();
3995     for (unsigned I = 0; I < Spec->cpus_size(); ++I)
3996       EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3997   } else if (FD->isTargetClonesMultiVersion()) {
3998     auto *Clone = FD->getAttr<TargetClonesAttr>();
3999     for (unsigned I = 0; I < Clone->featuresStrs_size(); ++I)
4000       if (Clone->isFirstOfVersion(I))
4001         EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
4002     // Ensure that the resolver function is also emitted.
4003     GetOrCreateMultiVersionResolver(GD);
4004   } else
4005     EmitGlobalFunctionDefinition(GD, GV);
4006 }
4007 
4008 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
4009   const auto *D = cast<ValueDecl>(GD.getDecl());
4010 
4011   PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
4012                                  Context.getSourceManager(),
4013                                  "Generating code for declaration");
4014 
4015   if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
4016     // At -O0, don't generate IR for functions with available_externally
4017     // linkage.
4018     if (!shouldEmitFunction(GD))
4019       return;
4020 
4021     llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
4022       std::string Name;
4023       llvm::raw_string_ostream OS(Name);
4024       FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
4025                                /*Qualified=*/true);
4026       return Name;
4027     });
4028 
4029     if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
4030       // Make sure to emit the definition(s) before we emit the thunks.
4031       // This is necessary for the generation of certain thunks.
4032       if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
4033         ABI->emitCXXStructor(GD);
4034       else if (FD->isMultiVersion())
4035         EmitMultiVersionFunctionDefinition(GD, GV);
4036       else
4037         EmitGlobalFunctionDefinition(GD, GV);
4038 
4039       if (Method->isVirtual())
4040         getVTables().EmitThunks(GD);
4041 
4042       return;
4043     }
4044 
4045     if (FD->isMultiVersion())
4046       return EmitMultiVersionFunctionDefinition(GD, GV);
4047     return EmitGlobalFunctionDefinition(GD, GV);
4048   }
4049 
4050   if (const auto *VD = dyn_cast<VarDecl>(D))
4051     return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
4052 
4053   llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
4054 }
4055 
4056 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
4057                                                       llvm::Function *NewFn);
4058 
4059 static unsigned
4060 TargetMVPriority(const TargetInfo &TI,
4061                  const CodeGenFunction::MultiVersionResolverOption &RO) {
4062   unsigned Priority = 0;
4063   unsigned NumFeatures = 0;
4064   for (StringRef Feat : RO.Conditions.Features) {
4065     Priority = std::max(Priority, TI.multiVersionSortPriority(Feat));
4066     NumFeatures++;
4067   }
4068 
4069   if (!RO.Conditions.Architecture.empty())
4070     Priority = std::max(
4071         Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture));
4072 
4073   Priority += TI.multiVersionFeatureCost() * NumFeatures;
4074 
4075   return Priority;
4076 }
4077 
4078 // Multiversion functions should be at most 'WeakODRLinkage' so that a different
4079 // TU can forward declare the function without causing problems.  Particularly
4080 // in the cases of CPUDispatch, this causes issues. This also makes sure we
4081 // work with internal linkage functions, so that the same function name can be
4082 // used with internal linkage in multiple TUs.
4083 llvm::GlobalValue::LinkageTypes getMultiversionLinkage(CodeGenModule &CGM,
4084                                                        GlobalDecl GD) {
4085   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
4086   if (FD->getFormalLinkage() == Linkage::Internal)
4087     return llvm::GlobalValue::InternalLinkage;
4088   return llvm::GlobalValue::WeakODRLinkage;
4089 }
4090 
4091 void CodeGenModule::emitMultiVersionFunctions() {
4092   std::vector<GlobalDecl> MVFuncsToEmit;
4093   MultiVersionFuncs.swap(MVFuncsToEmit);
4094   for (GlobalDecl GD : MVFuncsToEmit) {
4095     const auto *FD = cast<FunctionDecl>(GD.getDecl());
4096     assert(FD && "Expected a FunctionDecl");
4097 
4098     SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
4099     if (FD->isTargetMultiVersion()) {
4100       getContext().forEachMultiversionedFunctionVersion(
4101           FD, [this, &GD, &Options](const FunctionDecl *CurFD) {
4102             GlobalDecl CurGD{
4103                 (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)};
4104             StringRef MangledName = getMangledName(CurGD);
4105             llvm::Constant *Func = GetGlobalValue(MangledName);
4106             if (!Func) {
4107               if (CurFD->isDefined()) {
4108                 EmitGlobalFunctionDefinition(CurGD, nullptr);
4109                 Func = GetGlobalValue(MangledName);
4110               } else {
4111                 const CGFunctionInfo &FI =
4112                     getTypes().arrangeGlobalDeclaration(GD);
4113                 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4114                 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
4115                                          /*DontDefer=*/false, ForDefinition);
4116               }
4117               assert(Func && "This should have just been created");
4118             }
4119             if (CurFD->getMultiVersionKind() == MultiVersionKind::Target) {
4120               const auto *TA = CurFD->getAttr<TargetAttr>();
4121               llvm::SmallVector<StringRef, 8> Feats;
4122               TA->getAddedFeatures(Feats);
4123               Options.emplace_back(cast<llvm::Function>(Func),
4124                                    TA->getArchitecture(), Feats);
4125             } else {
4126               const auto *TVA = CurFD->getAttr<TargetVersionAttr>();
4127               llvm::SmallVector<StringRef, 8> Feats;
4128               TVA->getFeatures(Feats);
4129               Options.emplace_back(cast<llvm::Function>(Func),
4130                                    /*Architecture*/ "", Feats);
4131             }
4132           });
4133     } else if (FD->isTargetClonesMultiVersion()) {
4134       const auto *TC = FD->getAttr<TargetClonesAttr>();
4135       for (unsigned VersionIndex = 0; VersionIndex < TC->featuresStrs_size();
4136            ++VersionIndex) {
4137         if (!TC->isFirstOfVersion(VersionIndex))
4138           continue;
4139         GlobalDecl CurGD{(FD->isDefined() ? FD->getDefinition() : FD),
4140                          VersionIndex};
4141         StringRef Version = TC->getFeatureStr(VersionIndex);
4142         StringRef MangledName = getMangledName(CurGD);
4143         llvm::Constant *Func = GetGlobalValue(MangledName);
4144         if (!Func) {
4145           if (FD->isDefined()) {
4146             EmitGlobalFunctionDefinition(CurGD, nullptr);
4147             Func = GetGlobalValue(MangledName);
4148           } else {
4149             const CGFunctionInfo &FI =
4150                 getTypes().arrangeGlobalDeclaration(CurGD);
4151             llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4152             Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
4153                                      /*DontDefer=*/false, ForDefinition);
4154           }
4155           assert(Func && "This should have just been created");
4156         }
4157 
4158         StringRef Architecture;
4159         llvm::SmallVector<StringRef, 1> Feature;
4160 
4161         if (getTarget().getTriple().isAArch64()) {
4162           if (Version != "default") {
4163             llvm::SmallVector<StringRef, 8> VerFeats;
4164             Version.split(VerFeats, "+");
4165             for (auto &CurFeat : VerFeats)
4166               Feature.push_back(CurFeat.trim());
4167           }
4168         } else {
4169           if (Version.starts_with("arch="))
4170             Architecture = Version.drop_front(sizeof("arch=") - 1);
4171           else if (Version != "default")
4172             Feature.push_back(Version);
4173         }
4174 
4175         Options.emplace_back(cast<llvm::Function>(Func), Architecture, Feature);
4176       }
4177     } else {
4178       assert(0 && "Expected a target or target_clones multiversion function");
4179       continue;
4180     }
4181 
4182     llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD);
4183     if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant)) {
4184       ResolverConstant = IFunc->getResolver();
4185       // In Aarch64, default versions of multiversioned functions are mangled to
4186       // their 'normal' assembly name. This deviates from other targets which
4187       // append a '.default' string. As a result we need to continue appending
4188       // .ifunc in Aarch64.
4189       // FIXME: Should Aarch64 mangling for 'default' multiversion function and
4190       // in turn ifunc function match that of other targets?
4191       if (FD->isTargetClonesMultiVersion() &&
4192           !getTarget().getTriple().isAArch64()) {
4193         const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4194         llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4195         std::string MangledName = getMangledNameImpl(
4196             *this, GD, FD, /*OmitMultiVersionMangling=*/true);
4197         // In prior versions of Clang, the mangling for ifuncs incorrectly
4198         // included an .ifunc suffix. This alias is generated for backward
4199         // compatibility. It is deprecated, and may be removed in the future.
4200         auto *Alias = llvm::GlobalAlias::create(
4201             DeclTy, 0, getMultiversionLinkage(*this, GD),
4202             MangledName + ".ifunc", IFunc, &getModule());
4203         SetCommonAttributes(FD, Alias);
4204       }
4205     }
4206     llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant);
4207 
4208     ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4209 
4210     if (!ResolverFunc->hasLocalLinkage() && supportsCOMDAT())
4211       ResolverFunc->setComdat(
4212           getModule().getOrInsertComdat(ResolverFunc->getName()));
4213 
4214     const TargetInfo &TI = getTarget();
4215     llvm::stable_sort(
4216         Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS,
4217                        const CodeGenFunction::MultiVersionResolverOption &RHS) {
4218           return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS);
4219         });
4220     CodeGenFunction CGF(*this);
4221     CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4222   }
4223 
4224   // Ensure that any additions to the deferred decls list caused by emitting a
4225   // variant are emitted.  This can happen when the variant itself is inline and
4226   // calls a function without linkage.
4227   if (!MVFuncsToEmit.empty())
4228     EmitDeferred();
4229 
4230   // Ensure that any additions to the multiversion funcs list from either the
4231   // deferred decls or the multiversion functions themselves are emitted.
4232   if (!MultiVersionFuncs.empty())
4233     emitMultiVersionFunctions();
4234 }
4235 
4236 void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
4237   const auto *FD = cast<FunctionDecl>(GD.getDecl());
4238   assert(FD && "Not a FunctionDecl?");
4239   assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?");
4240   const auto *DD = FD->getAttr<CPUDispatchAttr>();
4241   assert(DD && "Not a cpu_dispatch Function?");
4242 
4243   const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4244   llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4245 
4246   StringRef ResolverName = getMangledName(GD);
4247   UpdateMultiVersionNames(GD, FD, ResolverName);
4248 
4249   llvm::Type *ResolverType;
4250   GlobalDecl ResolverGD;
4251   if (getTarget().supportsIFunc()) {
4252     ResolverType = llvm::FunctionType::get(
4253         llvm::PointerType::get(DeclTy,
4254                                getTypes().getTargetAddressSpace(FD->getType())),
4255         false);
4256   }
4257   else {
4258     ResolverType = DeclTy;
4259     ResolverGD = GD;
4260   }
4261 
4262   auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
4263       ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
4264   ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4265   if (supportsCOMDAT())
4266     ResolverFunc->setComdat(
4267         getModule().getOrInsertComdat(ResolverFunc->getName()));
4268 
4269   SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options;
4270   const TargetInfo &Target = getTarget();
4271   unsigned Index = 0;
4272   for (const IdentifierInfo *II : DD->cpus()) {
4273     // Get the name of the target function so we can look it up/create it.
4274     std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
4275                               getCPUSpecificMangling(*this, II->getName());
4276 
4277     llvm::Constant *Func = GetGlobalValue(MangledName);
4278 
4279     if (!Func) {
4280       GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
4281       if (ExistingDecl.getDecl() &&
4282           ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
4283         EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
4284         Func = GetGlobalValue(MangledName);
4285       } else {
4286         if (!ExistingDecl.getDecl())
4287           ExistingDecl = GD.getWithMultiVersionIndex(Index);
4288 
4289       Func = GetOrCreateLLVMFunction(
4290           MangledName, DeclTy, ExistingDecl,
4291           /*ForVTable=*/false, /*DontDefer=*/true,
4292           /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
4293       }
4294     }
4295 
4296     llvm::SmallVector<StringRef, 32> Features;
4297     Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
4298     llvm::transform(Features, Features.begin(),
4299                     [](StringRef Str) { return Str.substr(1); });
4300     llvm::erase_if(Features, [&Target](StringRef Feat) {
4301       return !Target.validateCpuSupports(Feat);
4302     });
4303     Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
4304     ++Index;
4305   }
4306 
4307   llvm::stable_sort(
4308       Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS,
4309                   const CodeGenFunction::MultiVersionResolverOption &RHS) {
4310         return llvm::X86::getCpuSupportsMask(LHS.Conditions.Features) >
4311                llvm::X86::getCpuSupportsMask(RHS.Conditions.Features);
4312       });
4313 
4314   // If the list contains multiple 'default' versions, such as when it contains
4315   // 'pentium' and 'generic', don't emit the call to the generic one (since we
4316   // always run on at least a 'pentium'). We do this by deleting the 'least
4317   // advanced' (read, lowest mangling letter).
4318   while (Options.size() > 1 &&
4319          llvm::all_of(llvm::X86::getCpuSupportsMask(
4320                           (Options.end() - 2)->Conditions.Features),
4321                       [](auto X) { return X == 0; })) {
4322     StringRef LHSName = (Options.end() - 2)->Function->getName();
4323     StringRef RHSName = (Options.end() - 1)->Function->getName();
4324     if (LHSName.compare(RHSName) < 0)
4325       Options.erase(Options.end() - 2);
4326     else
4327       Options.erase(Options.end() - 1);
4328   }
4329 
4330   CodeGenFunction CGF(*this);
4331   CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4332 
4333   if (getTarget().supportsIFunc()) {
4334     llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage(*this, GD);
4335     auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD));
4336 
4337     // Fix up function declarations that were created for cpu_specific before
4338     // cpu_dispatch was known
4339     if (!isa<llvm::GlobalIFunc>(IFunc)) {
4340       assert(cast<llvm::Function>(IFunc)->isDeclaration());
4341       auto *GI = llvm::GlobalIFunc::create(DeclTy, 0, Linkage, "", ResolverFunc,
4342                                            &getModule());
4343       GI->takeName(IFunc);
4344       IFunc->replaceAllUsesWith(GI);
4345       IFunc->eraseFromParent();
4346       IFunc = GI;
4347     }
4348 
4349     std::string AliasName = getMangledNameImpl(
4350         *this, GD, FD, /*OmitMultiVersionMangling=*/true);
4351     llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
4352     if (!AliasFunc) {
4353       auto *GA = llvm::GlobalAlias::create(DeclTy, 0, Linkage, AliasName, IFunc,
4354                                            &getModule());
4355       SetCommonAttributes(GD, GA);
4356     }
4357   }
4358 }
4359 
4360 /// If a dispatcher for the specified mangled name is not in the module, create
4361 /// and return an llvm Function with the specified type.
4362 llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
4363   const auto *FD = cast<FunctionDecl>(GD.getDecl());
4364   assert(FD && "Not a FunctionDecl?");
4365 
4366   std::string MangledName =
4367       getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
4368 
4369   // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
4370   // a separate resolver).
4371   std::string ResolverName = MangledName;
4372   if (getTarget().supportsIFunc()) {
4373     // In Aarch64, default versions of multiversioned functions are mangled to
4374     // their 'normal' assembly name. This deviates from other targets which
4375     // append a '.default' string. As a result we need to continue appending
4376     // .ifunc in Aarch64.
4377     // FIXME: Should Aarch64 mangling for 'default' multiversion function and
4378     // in turn ifunc function match that of other targets?
4379     if (!FD->isTargetClonesMultiVersion() ||
4380         getTarget().getTriple().isAArch64())
4381       ResolverName += ".ifunc";
4382   } else if (FD->isTargetMultiVersion()) {
4383     ResolverName += ".resolver";
4384   }
4385 
4386   // If the resolver has already been created, just return it.
4387   if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName))
4388     return ResolverGV;
4389 
4390   const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4391   llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4392 
4393   // The resolver needs to be created. For target and target_clones, defer
4394   // creation until the end of the TU.
4395   if (FD->isTargetMultiVersion() || FD->isTargetClonesMultiVersion())
4396     MultiVersionFuncs.push_back(GD);
4397 
4398   // For cpu_specific, don't create an ifunc yet because we don't know if the
4399   // cpu_dispatch will be emitted in this translation unit.
4400   if (getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion()) {
4401     llvm::Type *ResolverType = llvm::FunctionType::get(
4402         llvm::PointerType::get(DeclTy,
4403                                getTypes().getTargetAddressSpace(FD->getType())),
4404         false);
4405     llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4406         MangledName + ".resolver", ResolverType, GlobalDecl{},
4407         /*ForVTable=*/false);
4408     llvm::GlobalIFunc *GIF =
4409         llvm::GlobalIFunc::create(DeclTy, 0, getMultiversionLinkage(*this, GD),
4410                                   "", Resolver, &getModule());
4411     GIF->setName(ResolverName);
4412     SetCommonAttributes(FD, GIF);
4413 
4414     return GIF;
4415   }
4416 
4417   llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4418       ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
4419   assert(isa<llvm::GlobalValue>(Resolver) &&
4420          "Resolver should be created for the first time");
4421   SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
4422   return Resolver;
4423 }
4424 
4425 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
4426 /// module, create and return an llvm Function with the specified type. If there
4427 /// is something in the module with the specified name, return it potentially
4428 /// bitcasted to the right type.
4429 ///
4430 /// If D is non-null, it specifies a decl that correspond to this.  This is used
4431 /// to set the attributes on the function when it is first created.
4432 llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
4433     StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
4434     bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
4435     ForDefinition_t IsForDefinition) {
4436   const Decl *D = GD.getDecl();
4437 
4438   // Any attempts to use a MultiVersion function should result in retrieving
4439   // the iFunc instead. Name Mangling will handle the rest of the changes.
4440   if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
4441     // For the device mark the function as one that should be emitted.
4442     if (getLangOpts().OpenMPIsTargetDevice && OpenMPRuntime &&
4443         !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
4444         !DontDefer && !IsForDefinition) {
4445       if (const FunctionDecl *FDDef = FD->getDefinition()) {
4446         GlobalDecl GDDef;
4447         if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
4448           GDDef = GlobalDecl(CD, GD.getCtorType());
4449         else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
4450           GDDef = GlobalDecl(DD, GD.getDtorType());
4451         else
4452           GDDef = GlobalDecl(FDDef);
4453         EmitGlobal(GDDef);
4454       }
4455     }
4456 
4457     if (FD->isMultiVersion()) {
4458       UpdateMultiVersionNames(GD, FD, MangledName);
4459       if (!IsForDefinition)
4460         return GetOrCreateMultiVersionResolver(GD);
4461     }
4462   }
4463 
4464   // Lookup the entry, lazily creating it if necessary.
4465   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4466   if (Entry) {
4467     if (WeakRefReferences.erase(Entry)) {
4468       const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
4469       if (FD && !FD->hasAttr<WeakAttr>())
4470         Entry->setLinkage(llvm::Function::ExternalLinkage);
4471     }
4472 
4473     // Handle dropped DLL attributes.
4474     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
4475         !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) {
4476       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4477       setDSOLocal(Entry);
4478     }
4479 
4480     // If there are two attempts to define the same mangled name, issue an
4481     // error.
4482     if (IsForDefinition && !Entry->isDeclaration()) {
4483       GlobalDecl OtherGD;
4484       // Check that GD is not yet in DiagnosedConflictingDefinitions is required
4485       // to make sure that we issue an error only once.
4486       if (lookupRepresentativeDecl(MangledName, OtherGD) &&
4487           (GD.getCanonicalDecl().getDecl() !=
4488            OtherGD.getCanonicalDecl().getDecl()) &&
4489           DiagnosedConflictingDefinitions.insert(GD).second) {
4490         getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4491             << MangledName;
4492         getDiags().Report(OtherGD.getDecl()->getLocation(),
4493                           diag::note_previous_definition);
4494       }
4495     }
4496 
4497     if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
4498         (Entry->getValueType() == Ty)) {
4499       return Entry;
4500     }
4501 
4502     // Make sure the result is of the correct type.
4503     // (If function is requested for a definition, we always need to create a new
4504     // function, not just return a bitcast.)
4505     if (!IsForDefinition)
4506       return Entry;
4507   }
4508 
4509   // This function doesn't have a complete type (for example, the return
4510   // type is an incomplete struct). Use a fake type instead, and make
4511   // sure not to try to set attributes.
4512   bool IsIncompleteFunction = false;
4513 
4514   llvm::FunctionType *FTy;
4515   if (isa<llvm::FunctionType>(Ty)) {
4516     FTy = cast<llvm::FunctionType>(Ty);
4517   } else {
4518     FTy = llvm::FunctionType::get(VoidTy, false);
4519     IsIncompleteFunction = true;
4520   }
4521 
4522   llvm::Function *F =
4523       llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
4524                              Entry ? StringRef() : MangledName, &getModule());
4525 
4526   // Store the declaration associated with this function so it is potentially
4527   // updated by further declarations or definitions and emitted at the end.
4528   if (D && D->hasAttr<AnnotateAttr>())
4529     DeferredAnnotations[MangledName] = cast<ValueDecl>(D);
4530 
4531   // If we already created a function with the same mangled name (but different
4532   // type) before, take its name and add it to the list of functions to be
4533   // replaced with F at the end of CodeGen.
4534   //
4535   // This happens if there is a prototype for a function (e.g. "int f()") and
4536   // then a definition of a different type (e.g. "int f(int x)").
4537   if (Entry) {
4538     F->takeName(Entry);
4539 
4540     // This might be an implementation of a function without a prototype, in
4541     // which case, try to do special replacement of calls which match the new
4542     // prototype.  The really key thing here is that we also potentially drop
4543     // arguments from the call site so as to make a direct call, which makes the
4544     // inliner happier and suppresses a number of optimizer warnings (!) about
4545     // dropping arguments.
4546     if (!Entry->use_empty()) {
4547       ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F);
4548       Entry->removeDeadConstantUsers();
4549     }
4550 
4551     addGlobalValReplacement(Entry, F);
4552   }
4553 
4554   assert(F->getName() == MangledName && "name was uniqued!");
4555   if (D)
4556     SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
4557   if (ExtraAttrs.hasFnAttrs()) {
4558     llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
4559     F->addFnAttrs(B);
4560   }
4561 
4562   if (!DontDefer) {
4563     // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
4564     // each other bottoming out with the base dtor.  Therefore we emit non-base
4565     // dtors on usage, even if there is no dtor definition in the TU.
4566     if (isa_and_nonnull<CXXDestructorDecl>(D) &&
4567         getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
4568                                            GD.getDtorType()))
4569       addDeferredDeclToEmit(GD);
4570 
4571     // This is the first use or definition of a mangled name.  If there is a
4572     // deferred decl with this name, remember that we need to emit it at the end
4573     // of the file.
4574     auto DDI = DeferredDecls.find(MangledName);
4575     if (DDI != DeferredDecls.end()) {
4576       // Move the potentially referenced deferred decl to the
4577       // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
4578       // don't need it anymore).
4579       addDeferredDeclToEmit(DDI->second);
4580       DeferredDecls.erase(DDI);
4581 
4582       // Otherwise, there are cases we have to worry about where we're
4583       // using a declaration for which we must emit a definition but where
4584       // we might not find a top-level definition:
4585       //   - member functions defined inline in their classes
4586       //   - friend functions defined inline in some class
4587       //   - special member functions with implicit definitions
4588       // If we ever change our AST traversal to walk into class methods,
4589       // this will be unnecessary.
4590       //
4591       // We also don't emit a definition for a function if it's going to be an
4592       // entry in a vtable, unless it's already marked as used.
4593     } else if (getLangOpts().CPlusPlus && D) {
4594       // Look for a declaration that's lexically in a record.
4595       for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
4596            FD = FD->getPreviousDecl()) {
4597         if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
4598           if (FD->doesThisDeclarationHaveABody()) {
4599             addDeferredDeclToEmit(GD.getWithDecl(FD));
4600             break;
4601           }
4602         }
4603       }
4604     }
4605   }
4606 
4607   // Make sure the result is of the requested type.
4608   if (!IsIncompleteFunction) {
4609     assert(F->getFunctionType() == Ty);
4610     return F;
4611   }
4612 
4613   return F;
4614 }
4615 
4616 /// GetAddrOfFunction - Return the address of the given function.  If Ty is
4617 /// non-null, then this function will use the specified type if it has to
4618 /// create it (this occurs when we see a definition of the function).
4619 llvm::Constant *
4620 CodeGenModule::GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty, bool ForVTable,
4621                                  bool DontDefer,
4622                                  ForDefinition_t IsForDefinition) {
4623   // If there was no specific requested type, just convert it now.
4624   if (!Ty) {
4625     const auto *FD = cast<FunctionDecl>(GD.getDecl());
4626     Ty = getTypes().ConvertType(FD->getType());
4627   }
4628 
4629   // Devirtualized destructor calls may come through here instead of via
4630   // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
4631   // of the complete destructor when necessary.
4632   if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
4633     if (getTarget().getCXXABI().isMicrosoft() &&
4634         GD.getDtorType() == Dtor_Complete &&
4635         DD->getParent()->getNumVBases() == 0)
4636       GD = GlobalDecl(DD, Dtor_Base);
4637   }
4638 
4639   StringRef MangledName = getMangledName(GD);
4640   auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
4641                                     /*IsThunk=*/false, llvm::AttributeList(),
4642                                     IsForDefinition);
4643   // Returns kernel handle for HIP kernel stub function.
4644   if (LangOpts.CUDA && !LangOpts.CUDAIsDevice &&
4645       cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) {
4646     auto *Handle = getCUDARuntime().getKernelHandle(
4647         cast<llvm::Function>(F->stripPointerCasts()), GD);
4648     if (IsForDefinition)
4649       return F;
4650     return Handle;
4651   }
4652   return F;
4653 }
4654 
4655 llvm::Constant *CodeGenModule::GetFunctionStart(const ValueDecl *Decl) {
4656   llvm::GlobalValue *F =
4657       cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts());
4658 
4659   return llvm::NoCFIValue::get(F);
4660 }
4661 
4662 static const FunctionDecl *
4663 GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
4664   TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
4665   DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
4666 
4667   IdentifierInfo &CII = C.Idents.get(Name);
4668   for (const auto *Result : DC->lookup(&CII))
4669     if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4670       return FD;
4671 
4672   if (!C.getLangOpts().CPlusPlus)
4673     return nullptr;
4674 
4675   // Demangle the premangled name from getTerminateFn()
4676   IdentifierInfo &CXXII =
4677       (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ")
4678           ? C.Idents.get("terminate")
4679           : C.Idents.get(Name);
4680 
4681   for (const auto &N : {"__cxxabiv1", "std"}) {
4682     IdentifierInfo &NS = C.Idents.get(N);
4683     for (const auto *Result : DC->lookup(&NS)) {
4684       const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
4685       if (auto *LSD = dyn_cast<LinkageSpecDecl>(Result))
4686         for (const auto *Result : LSD->lookup(&NS))
4687           if ((ND = dyn_cast<NamespaceDecl>(Result)))
4688             break;
4689 
4690       if (ND)
4691         for (const auto *Result : ND->lookup(&CXXII))
4692           if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4693             return FD;
4694     }
4695   }
4696 
4697   return nullptr;
4698 }
4699 
4700 /// CreateRuntimeFunction - Create a new runtime function with the specified
4701 /// type and name.
4702 llvm::FunctionCallee
4703 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
4704                                      llvm::AttributeList ExtraAttrs, bool Local,
4705                                      bool AssumeConvergent) {
4706   if (AssumeConvergent) {
4707     ExtraAttrs =
4708         ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
4709   }
4710 
4711   llvm::Constant *C =
4712       GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
4713                               /*DontDefer=*/false, /*IsThunk=*/false,
4714                               ExtraAttrs);
4715 
4716   if (auto *F = dyn_cast<llvm::Function>(C)) {
4717     if (F->empty()) {
4718       F->setCallingConv(getRuntimeCC());
4719 
4720       // In Windows Itanium environments, try to mark runtime functions
4721       // dllimport. For Mingw and MSVC, don't. We don't really know if the user
4722       // will link their standard library statically or dynamically. Marking
4723       // functions imported when they are not imported can cause linker errors
4724       // and warnings.
4725       if (!Local && getTriple().isWindowsItaniumEnvironment() &&
4726           !getCodeGenOpts().LTOVisibilityPublicStd) {
4727         const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
4728         if (!FD || FD->hasAttr<DLLImportAttr>()) {
4729           F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4730           F->setLinkage(llvm::GlobalValue::ExternalLinkage);
4731         }
4732       }
4733       setDSOLocal(F);
4734     }
4735   }
4736 
4737   return {FTy, C};
4738 }
4739 
4740 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
4741 /// create and return an llvm GlobalVariable with the specified type and address
4742 /// space. If there is something in the module with the specified name, return
4743 /// it potentially bitcasted to the right type.
4744 ///
4745 /// If D is non-null, it specifies a decl that correspond to this.  This is used
4746 /// to set the attributes on the global when it is first created.
4747 ///
4748 /// If IsForDefinition is true, it is guaranteed that an actual global with
4749 /// type Ty will be returned, not conversion of a variable with the same
4750 /// mangled name but some other type.
4751 llvm::Constant *
4752 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
4753                                      LangAS AddrSpace, const VarDecl *D,
4754                                      ForDefinition_t IsForDefinition) {
4755   // Lookup the entry, lazily creating it if necessary.
4756   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4757   unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace);
4758   if (Entry) {
4759     if (WeakRefReferences.erase(Entry)) {
4760       if (D && !D->hasAttr<WeakAttr>())
4761         Entry->setLinkage(llvm::Function::ExternalLinkage);
4762     }
4763 
4764     // Handle dropped DLL attributes.
4765     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
4766         !shouldMapVisibilityToDLLExport(D))
4767       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4768 
4769     if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
4770       getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
4771 
4772     if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS)
4773       return Entry;
4774 
4775     // If there are two attempts to define the same mangled name, issue an
4776     // error.
4777     if (IsForDefinition && !Entry->isDeclaration()) {
4778       GlobalDecl OtherGD;
4779       const VarDecl *OtherD;
4780 
4781       // Check that D is not yet in DiagnosedConflictingDefinitions is required
4782       // to make sure that we issue an error only once.
4783       if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
4784           (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
4785           (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
4786           OtherD->hasInit() &&
4787           DiagnosedConflictingDefinitions.insert(D).second) {
4788         getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4789             << MangledName;
4790         getDiags().Report(OtherGD.getDecl()->getLocation(),
4791                           diag::note_previous_definition);
4792       }
4793     }
4794 
4795     // Make sure the result is of the correct type.
4796     if (Entry->getType()->getAddressSpace() != TargetAS)
4797       return llvm::ConstantExpr::getAddrSpaceCast(
4798           Entry, llvm::PointerType::get(Ty->getContext(), TargetAS));
4799 
4800     // (If global is requested for a definition, we always need to create a new
4801     // global, not just return a bitcast.)
4802     if (!IsForDefinition)
4803       return Entry;
4804   }
4805 
4806   auto DAddrSpace = GetGlobalVarAddressSpace(D);
4807 
4808   auto *GV = new llvm::GlobalVariable(
4809       getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr,
4810       MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal,
4811       getContext().getTargetAddressSpace(DAddrSpace));
4812 
4813   // If we already created a global with the same mangled name (but different
4814   // type) before, take its name and remove it from its parent.
4815   if (Entry) {
4816     GV->takeName(Entry);
4817 
4818     if (!Entry->use_empty()) {
4819       Entry->replaceAllUsesWith(GV);
4820     }
4821 
4822     Entry->eraseFromParent();
4823   }
4824 
4825   // This is the first use or definition of a mangled name.  If there is a
4826   // deferred decl with this name, remember that we need to emit it at the end
4827   // of the file.
4828   auto DDI = DeferredDecls.find(MangledName);
4829   if (DDI != DeferredDecls.end()) {
4830     // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
4831     // list, and remove it from DeferredDecls (since we don't need it anymore).
4832     addDeferredDeclToEmit(DDI->second);
4833     DeferredDecls.erase(DDI);
4834   }
4835 
4836   // Handle things which are present even on external declarations.
4837   if (D) {
4838     if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
4839       getOpenMPRuntime().registerTargetGlobalVariable(D, GV);
4840 
4841     // FIXME: This code is overly simple and should be merged with other global
4842     // handling.
4843     GV->setConstant(D->getType().isConstantStorage(getContext(), false, false));
4844 
4845     GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
4846 
4847     setLinkageForGV(GV, D);
4848 
4849     if (D->getTLSKind()) {
4850       if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4851         CXXThreadLocals.push_back(D);
4852       setTLSMode(GV, *D);
4853     }
4854 
4855     setGVProperties(GV, D);
4856 
4857     // If required by the ABI, treat declarations of static data members with
4858     // inline initializers as definitions.
4859     if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
4860       EmitGlobalVarDefinition(D);
4861     }
4862 
4863     // Emit section information for extern variables.
4864     if (D->hasExternalStorage()) {
4865       if (const SectionAttr *SA = D->getAttr<SectionAttr>())
4866         GV->setSection(SA->getName());
4867     }
4868 
4869     // Handle XCore specific ABI requirements.
4870     if (getTriple().getArch() == llvm::Triple::xcore &&
4871         D->getLanguageLinkage() == CLanguageLinkage &&
4872         D->getType().isConstant(Context) &&
4873         isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
4874       GV->setSection(".cp.rodata");
4875 
4876     // Handle code model attribute
4877     if (const auto *CMA = D->getAttr<CodeModelAttr>())
4878       GV->setCodeModel(CMA->getModel());
4879 
4880     // Check if we a have a const declaration with an initializer, we may be
4881     // able to emit it as available_externally to expose it's value to the
4882     // optimizer.
4883     if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
4884         D->getType().isConstQualified() && !GV->hasInitializer() &&
4885         !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
4886       const auto *Record =
4887           Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
4888       bool HasMutableFields = Record && Record->hasMutableFields();
4889       if (!HasMutableFields) {
4890         const VarDecl *InitDecl;
4891         const Expr *InitExpr = D->getAnyInitializer(InitDecl);
4892         if (InitExpr) {
4893           ConstantEmitter emitter(*this);
4894           llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
4895           if (Init) {
4896             auto *InitType = Init->getType();
4897             if (GV->getValueType() != InitType) {
4898               // The type of the initializer does not match the definition.
4899               // This happens when an initializer has a different type from
4900               // the type of the global (because of padding at the end of a
4901               // structure for instance).
4902               GV->setName(StringRef());
4903               // Make a new global with the correct type, this is now guaranteed
4904               // to work.
4905               auto *NewGV = cast<llvm::GlobalVariable>(
4906                   GetAddrOfGlobalVar(D, InitType, IsForDefinition)
4907                       ->stripPointerCasts());
4908 
4909               // Erase the old global, since it is no longer used.
4910               GV->eraseFromParent();
4911               GV = NewGV;
4912             } else {
4913               GV->setInitializer(Init);
4914               GV->setConstant(true);
4915               GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
4916             }
4917             emitter.finalize(GV);
4918           }
4919         }
4920       }
4921     }
4922   }
4923 
4924   if (D &&
4925       D->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly) {
4926     getTargetCodeGenInfo().setTargetAttributes(D, GV, *this);
4927     // External HIP managed variables needed to be recorded for transformation
4928     // in both device and host compilations.
4929     if (getLangOpts().CUDA && D && D->hasAttr<HIPManagedAttr>() &&
4930         D->hasExternalStorage())
4931       getCUDARuntime().handleVarRegistration(D, *GV);
4932   }
4933 
4934   if (D)
4935     SanitizerMD->reportGlobal(GV, *D);
4936 
4937   LangAS ExpectedAS =
4938       D ? D->getType().getAddressSpace()
4939         : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
4940   assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
4941   if (DAddrSpace != ExpectedAS) {
4942     return getTargetCodeGenInfo().performAddrSpaceCast(
4943         *this, GV, DAddrSpace, ExpectedAS,
4944         llvm::PointerType::get(getLLVMContext(), TargetAS));
4945   }
4946 
4947   return GV;
4948 }
4949 
4950 llvm::Constant *
4951 CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition) {
4952   const Decl *D = GD.getDecl();
4953 
4954   if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
4955     return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
4956                                 /*DontDefer=*/false, IsForDefinition);
4957 
4958   if (isa<CXXMethodDecl>(D)) {
4959     auto FInfo =
4960         &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D));
4961     auto Ty = getTypes().GetFunctionType(*FInfo);
4962     return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4963                              IsForDefinition);
4964   }
4965 
4966   if (isa<FunctionDecl>(D)) {
4967     const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
4968     llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4969     return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4970                              IsForDefinition);
4971   }
4972 
4973   return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition);
4974 }
4975 
4976 llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable(
4977     StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
4978     llvm::Align Alignment) {
4979   llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
4980   llvm::GlobalVariable *OldGV = nullptr;
4981 
4982   if (GV) {
4983     // Check if the variable has the right type.
4984     if (GV->getValueType() == Ty)
4985       return GV;
4986 
4987     // Because C++ name mangling, the only way we can end up with an already
4988     // existing global with the same name is if it has been declared extern "C".
4989     assert(GV->isDeclaration() && "Declaration has wrong type!");
4990     OldGV = GV;
4991   }
4992 
4993   // Create a new variable.
4994   GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
4995                                 Linkage, nullptr, Name);
4996 
4997   if (OldGV) {
4998     // Replace occurrences of the old variable if needed.
4999     GV->takeName(OldGV);
5000 
5001     if (!OldGV->use_empty()) {
5002       OldGV->replaceAllUsesWith(GV);
5003     }
5004 
5005     OldGV->eraseFromParent();
5006   }
5007 
5008   if (supportsCOMDAT() && GV->isWeakForLinker() &&
5009       !GV->hasAvailableExternallyLinkage())
5010     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
5011 
5012   GV->setAlignment(Alignment);
5013 
5014   return GV;
5015 }
5016 
5017 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
5018 /// given global variable.  If Ty is non-null and if the global doesn't exist,
5019 /// then it will be created with the specified type instead of whatever the
5020 /// normal requested type would be. If IsForDefinition is true, it is guaranteed
5021 /// that an actual global with type Ty will be returned, not conversion of a
5022 /// variable with the same mangled name but some other type.
5023 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
5024                                                   llvm::Type *Ty,
5025                                            ForDefinition_t IsForDefinition) {
5026   assert(D->hasGlobalStorage() && "Not a global variable");
5027   QualType ASTTy = D->getType();
5028   if (!Ty)
5029     Ty = getTypes().ConvertTypeForMem(ASTTy);
5030 
5031   StringRef MangledName = getMangledName(D);
5032   return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D,
5033                                IsForDefinition);
5034 }
5035 
5036 /// CreateRuntimeVariable - Create a new runtime global variable with the
5037 /// specified type and name.
5038 llvm::Constant *
5039 CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
5040                                      StringRef Name) {
5041   LangAS AddrSpace = getContext().getLangOpts().OpenCL ? LangAS::opencl_global
5042                                                        : LangAS::Default;
5043   auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr);
5044   setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
5045   return Ret;
5046 }
5047 
5048 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
5049   assert(!D->getInit() && "Cannot emit definite definitions here!");
5050 
5051   StringRef MangledName = getMangledName(D);
5052   llvm::GlobalValue *GV = GetGlobalValue(MangledName);
5053 
5054   // We already have a definition, not declaration, with the same mangled name.
5055   // Emitting of declaration is not required (and actually overwrites emitted
5056   // definition).
5057   if (GV && !GV->isDeclaration())
5058     return;
5059 
5060   // If we have not seen a reference to this variable yet, place it into the
5061   // deferred declarations table to be emitted if needed later.
5062   if (!MustBeEmitted(D) && !GV) {
5063       DeferredDecls[MangledName] = D;
5064       return;
5065   }
5066 
5067   // The tentative definition is the only definition.
5068   EmitGlobalVarDefinition(D);
5069 }
5070 
5071 void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) {
5072   EmitExternalVarDeclaration(D);
5073 }
5074 
5075 CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
5076   return Context.toCharUnitsFromBits(
5077       getDataLayout().getTypeStoreSizeInBits(Ty));
5078 }
5079 
5080 LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
5081   if (LangOpts.OpenCL) {
5082     LangAS AS = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
5083     assert(AS == LangAS::opencl_global ||
5084            AS == LangAS::opencl_global_device ||
5085            AS == LangAS::opencl_global_host ||
5086            AS == LangAS::opencl_constant ||
5087            AS == LangAS::opencl_local ||
5088            AS >= LangAS::FirstTargetAddressSpace);
5089     return AS;
5090   }
5091 
5092   if (LangOpts.SYCLIsDevice &&
5093       (!D || D->getType().getAddressSpace() == LangAS::Default))
5094     return LangAS::sycl_global;
5095 
5096   if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
5097     if (D) {
5098       if (D->hasAttr<CUDAConstantAttr>())
5099         return LangAS::cuda_constant;
5100       if (D->hasAttr<CUDASharedAttr>())
5101         return LangAS::cuda_shared;
5102       if (D->hasAttr<CUDADeviceAttr>())
5103         return LangAS::cuda_device;
5104       if (D->getType().isConstQualified())
5105         return LangAS::cuda_constant;
5106     }
5107     return LangAS::cuda_device;
5108   }
5109 
5110   if (LangOpts.OpenMP) {
5111     LangAS AS;
5112     if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
5113       return AS;
5114   }
5115   return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
5116 }
5117 
5118 LangAS CodeGenModule::GetGlobalConstantAddressSpace() const {
5119   // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
5120   if (LangOpts.OpenCL)
5121     return LangAS::opencl_constant;
5122   if (LangOpts.SYCLIsDevice)
5123     return LangAS::sycl_global;
5124   if (LangOpts.HIP && LangOpts.CUDAIsDevice && getTriple().isSPIRV())
5125     // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
5126     // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
5127     // with OpVariable instructions with Generic storage class which is not
5128     // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
5129     // UniformConstant storage class is not viable as pointers to it may not be
5130     // casted to Generic pointers which are used to model HIP's "flat" pointers.
5131     return LangAS::cuda_device;
5132   if (auto AS = getTarget().getConstantAddressSpace())
5133     return *AS;
5134   return LangAS::Default;
5135 }
5136 
5137 // In address space agnostic languages, string literals are in default address
5138 // space in AST. However, certain targets (e.g. amdgcn) request them to be
5139 // emitted in constant address space in LLVM IR. To be consistent with other
5140 // parts of AST, string literal global variables in constant address space
5141 // need to be casted to default address space before being put into address
5142 // map and referenced by other part of CodeGen.
5143 // In OpenCL, string literals are in constant address space in AST, therefore
5144 // they should not be casted to default address space.
5145 static llvm::Constant *
5146 castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM,
5147                                        llvm::GlobalVariable *GV) {
5148   llvm::Constant *Cast = GV;
5149   if (!CGM.getLangOpts().OpenCL) {
5150     auto AS = CGM.GetGlobalConstantAddressSpace();
5151     if (AS != LangAS::Default)
5152       Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast(
5153           CGM, GV, AS, LangAS::Default,
5154           llvm::PointerType::get(
5155               CGM.getLLVMContext(),
5156               CGM.getContext().getTargetAddressSpace(LangAS::Default)));
5157   }
5158   return Cast;
5159 }
5160 
5161 template<typename SomeDecl>
5162 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
5163                                                llvm::GlobalValue *GV) {
5164   if (!getLangOpts().CPlusPlus)
5165     return;
5166 
5167   // Must have 'used' attribute, or else inline assembly can't rely on
5168   // the name existing.
5169   if (!D->template hasAttr<UsedAttr>())
5170     return;
5171 
5172   // Must have internal linkage and an ordinary name.
5173   if (!D->getIdentifier() || D->getFormalLinkage() != Linkage::Internal)
5174     return;
5175 
5176   // Must be in an extern "C" context. Entities declared directly within
5177   // a record are not extern "C" even if the record is in such a context.
5178   const SomeDecl *First = D->getFirstDecl();
5179   if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
5180     return;
5181 
5182   // OK, this is an internal linkage entity inside an extern "C" linkage
5183   // specification. Make a note of that so we can give it the "expected"
5184   // mangled name if nothing else is using that name.
5185   std::pair<StaticExternCMap::iterator, bool> R =
5186       StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
5187 
5188   // If we have multiple internal linkage entities with the same name
5189   // in extern "C" regions, none of them gets that name.
5190   if (!R.second)
5191     R.first->second = nullptr;
5192 }
5193 
5194 static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
5195   if (!CGM.supportsCOMDAT())
5196     return false;
5197 
5198   if (D.hasAttr<SelectAnyAttr>())
5199     return true;
5200 
5201   GVALinkage Linkage;
5202   if (auto *VD = dyn_cast<VarDecl>(&D))
5203     Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
5204   else
5205     Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
5206 
5207   switch (Linkage) {
5208   case GVA_Internal:
5209   case GVA_AvailableExternally:
5210   case GVA_StrongExternal:
5211     return false;
5212   case GVA_DiscardableODR:
5213   case GVA_StrongODR:
5214     return true;
5215   }
5216   llvm_unreachable("No such linkage");
5217 }
5218 
5219 bool CodeGenModule::supportsCOMDAT() const {
5220   return getTriple().supportsCOMDAT();
5221 }
5222 
5223 void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
5224                                           llvm::GlobalObject &GO) {
5225   if (!shouldBeInCOMDAT(*this, D))
5226     return;
5227   GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
5228 }
5229 
5230 /// Pass IsTentative as true if you want to create a tentative definition.
5231 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
5232                                             bool IsTentative) {
5233   // OpenCL global variables of sampler type are translated to function calls,
5234   // therefore no need to be translated.
5235   QualType ASTTy = D->getType();
5236   if (getLangOpts().OpenCL && ASTTy->isSamplerT())
5237     return;
5238 
5239   // If this is OpenMP device, check if it is legal to emit this global
5240   // normally.
5241   if (LangOpts.OpenMPIsTargetDevice && OpenMPRuntime &&
5242       OpenMPRuntime->emitTargetGlobalVariable(D))
5243     return;
5244 
5245   llvm::TrackingVH<llvm::Constant> Init;
5246   bool NeedsGlobalCtor = false;
5247   // Whether the definition of the variable is available externally.
5248   // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
5249   // since this is the job for its original source.
5250   bool IsDefinitionAvailableExternally =
5251       getContext().GetGVALinkageForVariable(D) == GVA_AvailableExternally;
5252   bool NeedsGlobalDtor =
5253       !IsDefinitionAvailableExternally &&
5254       D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
5255 
5256   const VarDecl *InitDecl;
5257   const Expr *InitExpr = D->getAnyInitializer(InitDecl);
5258 
5259   std::optional<ConstantEmitter> emitter;
5260 
5261   // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
5262   // as part of their declaration."  Sema has already checked for
5263   // error cases, so we just need to set Init to UndefValue.
5264   bool IsCUDASharedVar =
5265       getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
5266   // Shadows of initialized device-side global variables are also left
5267   // undefined.
5268   // Managed Variables should be initialized on both host side and device side.
5269   bool IsCUDAShadowVar =
5270       !getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5271       (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
5272        D->hasAttr<CUDASharedAttr>());
5273   bool IsCUDADeviceShadowVar =
5274       getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5275       (D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5276        D->getType()->isCUDADeviceBuiltinTextureType());
5277   if (getLangOpts().CUDA &&
5278       (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
5279     Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5280   else if (D->hasAttr<LoaderUninitializedAttr>())
5281     Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5282   else if (!InitExpr) {
5283     // This is a tentative definition; tentative definitions are
5284     // implicitly initialized with { 0 }.
5285     //
5286     // Note that tentative definitions are only emitted at the end of
5287     // a translation unit, so they should never have incomplete
5288     // type. In addition, EmitTentativeDefinition makes sure that we
5289     // never attempt to emit a tentative definition if a real one
5290     // exists. A use may still exists, however, so we still may need
5291     // to do a RAUW.
5292     assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
5293     Init = EmitNullConstant(D->getType());
5294   } else {
5295     initializedGlobalDecl = GlobalDecl(D);
5296     emitter.emplace(*this);
5297     llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl);
5298     if (!Initializer) {
5299       QualType T = InitExpr->getType();
5300       if (D->getType()->isReferenceType())
5301         T = D->getType();
5302 
5303       if (getLangOpts().CPlusPlus) {
5304         if (InitDecl->hasFlexibleArrayInit(getContext()))
5305           ErrorUnsupported(D, "flexible array initializer");
5306         Init = EmitNullConstant(T);
5307 
5308         if (!IsDefinitionAvailableExternally)
5309           NeedsGlobalCtor = true;
5310       } else {
5311         ErrorUnsupported(D, "static initializer");
5312         Init = llvm::UndefValue::get(getTypes().ConvertType(T));
5313       }
5314     } else {
5315       Init = Initializer;
5316       // We don't need an initializer, so remove the entry for the delayed
5317       // initializer position (just in case this entry was delayed) if we
5318       // also don't need to register a destructor.
5319       if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
5320         DelayedCXXInitPosition.erase(D);
5321 
5322 #ifndef NDEBUG
5323       CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) +
5324                           InitDecl->getFlexibleArrayInitChars(getContext());
5325       CharUnits CstSize = CharUnits::fromQuantity(
5326           getDataLayout().getTypeAllocSize(Init->getType()));
5327       assert(VarSize == CstSize && "Emitted constant has unexpected size");
5328 #endif
5329     }
5330   }
5331 
5332   llvm::Type* InitType = Init->getType();
5333   llvm::Constant *Entry =
5334       GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
5335 
5336   // Strip off pointer casts if we got them.
5337   Entry = Entry->stripPointerCasts();
5338 
5339   // Entry is now either a Function or GlobalVariable.
5340   auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
5341 
5342   // We have a definition after a declaration with the wrong type.
5343   // We must make a new GlobalVariable* and update everything that used OldGV
5344   // (a declaration or tentative definition) with the new GlobalVariable*
5345   // (which will be a definition).
5346   //
5347   // This happens if there is a prototype for a global (e.g.
5348   // "extern int x[];") and then a definition of a different type (e.g.
5349   // "int x[10];"). This also happens when an initializer has a different type
5350   // from the type of the global (this happens with unions).
5351   if (!GV || GV->getValueType() != InitType ||
5352       GV->getType()->getAddressSpace() !=
5353           getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) {
5354 
5355     // Move the old entry aside so that we'll create a new one.
5356     Entry->setName(StringRef());
5357 
5358     // Make a new global with the correct type, this is now guaranteed to work.
5359     GV = cast<llvm::GlobalVariable>(
5360         GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))
5361             ->stripPointerCasts());
5362 
5363     // Replace all uses of the old global with the new global
5364     llvm::Constant *NewPtrForOldDecl =
5365         llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV,
5366                                                              Entry->getType());
5367     Entry->replaceAllUsesWith(NewPtrForOldDecl);
5368 
5369     // Erase the old global, since it is no longer used.
5370     cast<llvm::GlobalValue>(Entry)->eraseFromParent();
5371   }
5372 
5373   MaybeHandleStaticInExternC(D, GV);
5374 
5375   if (D->hasAttr<AnnotateAttr>())
5376     AddGlobalAnnotations(D, GV);
5377 
5378   // Set the llvm linkage type as appropriate.
5379   llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(D);
5380 
5381   // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
5382   // the device. [...]"
5383   // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
5384   // __device__, declares a variable that: [...]
5385   // Is accessible from all the threads within the grid and from the host
5386   // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
5387   // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
5388   if (LangOpts.CUDA) {
5389     if (LangOpts.CUDAIsDevice) {
5390       if (Linkage != llvm::GlobalValue::InternalLinkage &&
5391           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
5392            D->getType()->isCUDADeviceBuiltinSurfaceType() ||
5393            D->getType()->isCUDADeviceBuiltinTextureType()))
5394         GV->setExternallyInitialized(true);
5395     } else {
5396       getCUDARuntime().internalizeDeviceSideVar(D, Linkage);
5397     }
5398     getCUDARuntime().handleVarRegistration(D, *GV);
5399   }
5400 
5401   GV->setInitializer(Init);
5402   if (emitter)
5403     emitter->finalize(GV);
5404 
5405   // If it is safe to mark the global 'constant', do so now.
5406   GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
5407                   D->getType().isConstantStorage(getContext(), true, true));
5408 
5409   // If it is in a read-only section, mark it 'constant'.
5410   if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
5411     const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
5412     if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
5413       GV->setConstant(true);
5414   }
5415 
5416   CharUnits AlignVal = getContext().getDeclAlign(D);
5417   // Check for alignment specifed in an 'omp allocate' directive.
5418   if (std::optional<CharUnits> AlignValFromAllocate =
5419           getOMPAllocateAlignment(D))
5420     AlignVal = *AlignValFromAllocate;
5421   GV->setAlignment(AlignVal.getAsAlign());
5422 
5423   // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
5424   // function is only defined alongside the variable, not also alongside
5425   // callers. Normally, all accesses to a thread_local go through the
5426   // thread-wrapper in order to ensure initialization has occurred, underlying
5427   // variable will never be used other than the thread-wrapper, so it can be
5428   // converted to internal linkage.
5429   //
5430   // However, if the variable has the 'constinit' attribute, it _can_ be
5431   // referenced directly, without calling the thread-wrapper, so the linkage
5432   // must not be changed.
5433   //
5434   // Additionally, if the variable isn't plain external linkage, e.g. if it's
5435   // weak or linkonce, the de-duplication semantics are important to preserve,
5436   // so we don't change the linkage.
5437   if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
5438       Linkage == llvm::GlobalValue::ExternalLinkage &&
5439       Context.getTargetInfo().getTriple().isOSDarwin() &&
5440       !D->hasAttr<ConstInitAttr>())
5441     Linkage = llvm::GlobalValue::InternalLinkage;
5442 
5443   GV->setLinkage(Linkage);
5444   if (D->hasAttr<DLLImportAttr>())
5445     GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
5446   else if (D->hasAttr<DLLExportAttr>())
5447     GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
5448   else
5449     GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
5450 
5451   if (Linkage == llvm::GlobalVariable::CommonLinkage) {
5452     // common vars aren't constant even if declared const.
5453     GV->setConstant(false);
5454     // Tentative definition of global variables may be initialized with
5455     // non-zero null pointers. In this case they should have weak linkage
5456     // since common linkage must have zero initializer and must not have
5457     // explicit section therefore cannot have non-zero initial value.
5458     if (!GV->getInitializer()->isNullValue())
5459       GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
5460   }
5461 
5462   setNonAliasAttributes(D, GV);
5463 
5464   if (D->getTLSKind() && !GV->isThreadLocal()) {
5465     if (D->getTLSKind() == VarDecl::TLS_Dynamic)
5466       CXXThreadLocals.push_back(D);
5467     setTLSMode(GV, *D);
5468   }
5469 
5470   maybeSetTrivialComdat(*D, *GV);
5471 
5472   // Emit the initializer function if necessary.
5473   if (NeedsGlobalCtor || NeedsGlobalDtor)
5474     EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
5475 
5476   SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
5477 
5478   // Emit global variable debug information.
5479   if (CGDebugInfo *DI = getModuleDebugInfo())
5480     if (getCodeGenOpts().hasReducedDebugInfo())
5481       DI->EmitGlobalVariable(GV, D);
5482 }
5483 
5484 void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
5485   if (CGDebugInfo *DI = getModuleDebugInfo())
5486     if (getCodeGenOpts().hasReducedDebugInfo()) {
5487       QualType ASTTy = D->getType();
5488       llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
5489       llvm::Constant *GV =
5490           GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D);
5491       DI->EmitExternalVariable(
5492           cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
5493     }
5494 }
5495 
5496 static bool isVarDeclStrongDefinition(const ASTContext &Context,
5497                                       CodeGenModule &CGM, const VarDecl *D,
5498                                       bool NoCommon) {
5499   // Don't give variables common linkage if -fno-common was specified unless it
5500   // was overridden by a NoCommon attribute.
5501   if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
5502     return true;
5503 
5504   // C11 6.9.2/2:
5505   //   A declaration of an identifier for an object that has file scope without
5506   //   an initializer, and without a storage-class specifier or with the
5507   //   storage-class specifier static, constitutes a tentative definition.
5508   if (D->getInit() || D->hasExternalStorage())
5509     return true;
5510 
5511   // A variable cannot be both common and exist in a section.
5512   if (D->hasAttr<SectionAttr>())
5513     return true;
5514 
5515   // A variable cannot be both common and exist in a section.
5516   // We don't try to determine which is the right section in the front-end.
5517   // If no specialized section name is applicable, it will resort to default.
5518   if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
5519       D->hasAttr<PragmaClangDataSectionAttr>() ||
5520       D->hasAttr<PragmaClangRelroSectionAttr>() ||
5521       D->hasAttr<PragmaClangRodataSectionAttr>())
5522     return true;
5523 
5524   // Thread local vars aren't considered common linkage.
5525   if (D->getTLSKind())
5526     return true;
5527 
5528   // Tentative definitions marked with WeakImportAttr are true definitions.
5529   if (D->hasAttr<WeakImportAttr>())
5530     return true;
5531 
5532   // A variable cannot be both common and exist in a comdat.
5533   if (shouldBeInCOMDAT(CGM, *D))
5534     return true;
5535 
5536   // Declarations with a required alignment do not have common linkage in MSVC
5537   // mode.
5538   if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
5539     if (D->hasAttr<AlignedAttr>())
5540       return true;
5541     QualType VarType = D->getType();
5542     if (Context.isAlignmentRequired(VarType))
5543       return true;
5544 
5545     if (const auto *RT = VarType->getAs<RecordType>()) {
5546       const RecordDecl *RD = RT->getDecl();
5547       for (const FieldDecl *FD : RD->fields()) {
5548         if (FD->isBitField())
5549           continue;
5550         if (FD->hasAttr<AlignedAttr>())
5551           return true;
5552         if (Context.isAlignmentRequired(FD->getType()))
5553           return true;
5554       }
5555     }
5556   }
5557 
5558   // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
5559   // common symbols, so symbols with greater alignment requirements cannot be
5560   // common.
5561   // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
5562   // alignments for common symbols via the aligncomm directive, so this
5563   // restriction only applies to MSVC environments.
5564   if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
5565       Context.getTypeAlignIfKnown(D->getType()) >
5566           Context.toBits(CharUnits::fromQuantity(32)))
5567     return true;
5568 
5569   return false;
5570 }
5571 
5572 llvm::GlobalValue::LinkageTypes
5573 CodeGenModule::getLLVMLinkageForDeclarator(const DeclaratorDecl *D,
5574                                            GVALinkage Linkage) {
5575   if (Linkage == GVA_Internal)
5576     return llvm::Function::InternalLinkage;
5577 
5578   if (D->hasAttr<WeakAttr>())
5579     return llvm::GlobalVariable::WeakAnyLinkage;
5580 
5581   if (const auto *FD = D->getAsFunction())
5582     if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally)
5583       return llvm::GlobalVariable::LinkOnceAnyLinkage;
5584 
5585   // We are guaranteed to have a strong definition somewhere else,
5586   // so we can use available_externally linkage.
5587   if (Linkage == GVA_AvailableExternally)
5588     return llvm::GlobalValue::AvailableExternallyLinkage;
5589 
5590   // Note that Apple's kernel linker doesn't support symbol
5591   // coalescing, so we need to avoid linkonce and weak linkages there.
5592   // Normally, this means we just map to internal, but for explicit
5593   // instantiations we'll map to external.
5594 
5595   // In C++, the compiler has to emit a definition in every translation unit
5596   // that references the function.  We should use linkonce_odr because
5597   // a) if all references in this translation unit are optimized away, we
5598   // don't need to codegen it.  b) if the function persists, it needs to be
5599   // merged with other definitions. c) C++ has the ODR, so we know the
5600   // definition is dependable.
5601   if (Linkage == GVA_DiscardableODR)
5602     return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
5603                                             : llvm::Function::InternalLinkage;
5604 
5605   // An explicit instantiation of a template has weak linkage, since
5606   // explicit instantiations can occur in multiple translation units
5607   // and must all be equivalent. However, we are not allowed to
5608   // throw away these explicit instantiations.
5609   //
5610   // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
5611   // so say that CUDA templates are either external (for kernels) or internal.
5612   // This lets llvm perform aggressive inter-procedural optimizations. For
5613   // -fgpu-rdc case, device function calls across multiple TU's are allowed,
5614   // therefore we need to follow the normal linkage paradigm.
5615   if (Linkage == GVA_StrongODR) {
5616     if (getLangOpts().AppleKext)
5617       return llvm::Function::ExternalLinkage;
5618     if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
5619         !getLangOpts().GPURelocatableDeviceCode)
5620       return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
5621                                           : llvm::Function::InternalLinkage;
5622     return llvm::Function::WeakODRLinkage;
5623   }
5624 
5625   // C++ doesn't have tentative definitions and thus cannot have common
5626   // linkage.
5627   if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
5628       !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
5629                                  CodeGenOpts.NoCommon))
5630     return llvm::GlobalVariable::CommonLinkage;
5631 
5632   // selectany symbols are externally visible, so use weak instead of
5633   // linkonce.  MSVC optimizes away references to const selectany globals, so
5634   // all definitions should be the same and ODR linkage should be used.
5635   // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
5636   if (D->hasAttr<SelectAnyAttr>())
5637     return llvm::GlobalVariable::WeakODRLinkage;
5638 
5639   // Otherwise, we have strong external linkage.
5640   assert(Linkage == GVA_StrongExternal);
5641   return llvm::GlobalVariable::ExternalLinkage;
5642 }
5643 
5644 llvm::GlobalValue::LinkageTypes
5645 CodeGenModule::getLLVMLinkageVarDefinition(const VarDecl *VD) {
5646   GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
5647   return getLLVMLinkageForDeclarator(VD, Linkage);
5648 }
5649 
5650 /// Replace the uses of a function that was declared with a non-proto type.
5651 /// We want to silently drop extra arguments from call sites
5652 static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
5653                                           llvm::Function *newFn) {
5654   // Fast path.
5655   if (old->use_empty()) return;
5656 
5657   llvm::Type *newRetTy = newFn->getReturnType();
5658   SmallVector<llvm::Value*, 4> newArgs;
5659 
5660   for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
5661          ui != ue; ) {
5662     llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
5663     llvm::User *user = use->getUser();
5664 
5665     // Recognize and replace uses of bitcasts.  Most calls to
5666     // unprototyped functions will use bitcasts.
5667     if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
5668       if (bitcast->getOpcode() == llvm::Instruction::BitCast)
5669         replaceUsesOfNonProtoConstant(bitcast, newFn);
5670       continue;
5671     }
5672 
5673     // Recognize calls to the function.
5674     llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
5675     if (!callSite) continue;
5676     if (!callSite->isCallee(&*use))
5677       continue;
5678 
5679     // If the return types don't match exactly, then we can't
5680     // transform this call unless it's dead.
5681     if (callSite->getType() != newRetTy && !callSite->use_empty())
5682       continue;
5683 
5684     // Get the call site's attribute list.
5685     SmallVector<llvm::AttributeSet, 8> newArgAttrs;
5686     llvm::AttributeList oldAttrs = callSite->getAttributes();
5687 
5688     // If the function was passed too few arguments, don't transform.
5689     unsigned newNumArgs = newFn->arg_size();
5690     if (callSite->arg_size() < newNumArgs)
5691       continue;
5692 
5693     // If extra arguments were passed, we silently drop them.
5694     // If any of the types mismatch, we don't transform.
5695     unsigned argNo = 0;
5696     bool dontTransform = false;
5697     for (llvm::Argument &A : newFn->args()) {
5698       if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
5699         dontTransform = true;
5700         break;
5701       }
5702 
5703       // Add any parameter attributes.
5704       newArgAttrs.push_back(oldAttrs.getParamAttrs(argNo));
5705       argNo++;
5706     }
5707     if (dontTransform)
5708       continue;
5709 
5710     // Okay, we can transform this.  Create the new call instruction and copy
5711     // over the required information.
5712     newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
5713 
5714     // Copy over any operand bundles.
5715     SmallVector<llvm::OperandBundleDef, 1> newBundles;
5716     callSite->getOperandBundlesAsDefs(newBundles);
5717 
5718     llvm::CallBase *newCall;
5719     if (isa<llvm::CallInst>(callSite)) {
5720       newCall =
5721           llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite);
5722     } else {
5723       auto *oldInvoke = cast<llvm::InvokeInst>(callSite);
5724       newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(),
5725                                          oldInvoke->getUnwindDest(), newArgs,
5726                                          newBundles, "", callSite);
5727     }
5728     newArgs.clear(); // for the next iteration
5729 
5730     if (!newCall->getType()->isVoidTy())
5731       newCall->takeName(callSite);
5732     newCall->setAttributes(
5733         llvm::AttributeList::get(newFn->getContext(), oldAttrs.getFnAttrs(),
5734                                  oldAttrs.getRetAttrs(), newArgAttrs));
5735     newCall->setCallingConv(callSite->getCallingConv());
5736 
5737     // Finally, remove the old call, replacing any uses with the new one.
5738     if (!callSite->use_empty())
5739       callSite->replaceAllUsesWith(newCall);
5740 
5741     // Copy debug location attached to CI.
5742     if (callSite->getDebugLoc())
5743       newCall->setDebugLoc(callSite->getDebugLoc());
5744 
5745     callSite->eraseFromParent();
5746   }
5747 }
5748 
5749 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
5750 /// implement a function with no prototype, e.g. "int foo() {}".  If there are
5751 /// existing call uses of the old function in the module, this adjusts them to
5752 /// call the new function directly.
5753 ///
5754 /// This is not just a cleanup: the always_inline pass requires direct calls to
5755 /// functions to be able to inline them.  If there is a bitcast in the way, it
5756 /// won't inline them.  Instcombine normally deletes these calls, but it isn't
5757 /// run at -O0.
5758 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
5759                                                       llvm::Function *NewFn) {
5760   // If we're redefining a global as a function, don't transform it.
5761   if (!isa<llvm::Function>(Old)) return;
5762 
5763   replaceUsesOfNonProtoConstant(Old, NewFn);
5764 }
5765 
5766 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
5767   auto DK = VD->isThisDeclarationADefinition();
5768   if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>())
5769     return;
5770 
5771   TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
5772   // If we have a definition, this might be a deferred decl. If the
5773   // instantiation is explicit, make sure we emit it at the end.
5774   if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
5775     GetAddrOfGlobalVar(VD);
5776 
5777   EmitTopLevelDecl(VD);
5778 }
5779 
5780 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
5781                                                  llvm::GlobalValue *GV) {
5782   const auto *D = cast<FunctionDecl>(GD.getDecl());
5783 
5784   // Compute the function info and LLVM type.
5785   const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
5786   llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
5787 
5788   // Get or create the prototype for the function.
5789   if (!GV || (GV->getValueType() != Ty))
5790     GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
5791                                                    /*DontDefer=*/true,
5792                                                    ForDefinition));
5793 
5794   // Already emitted.
5795   if (!GV->isDeclaration())
5796     return;
5797 
5798   // We need to set linkage and visibility on the function before
5799   // generating code for it because various parts of IR generation
5800   // want to propagate this information down (e.g. to local static
5801   // declarations).
5802   auto *Fn = cast<llvm::Function>(GV);
5803   setFunctionLinkage(GD, Fn);
5804 
5805   // FIXME: this is redundant with part of setFunctionDefinitionAttributes
5806   setGVProperties(Fn, GD);
5807 
5808   MaybeHandleStaticInExternC(D, Fn);
5809 
5810   maybeSetTrivialComdat(*D, *Fn);
5811 
5812   CodeGenFunction(*this).GenerateCode(GD, Fn, FI);
5813 
5814   setNonAliasAttributes(GD, Fn);
5815   SetLLVMFunctionAttributesForDefinition(D, Fn);
5816 
5817   if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
5818     AddGlobalCtor(Fn, CA->getPriority());
5819   if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
5820     AddGlobalDtor(Fn, DA->getPriority(), true);
5821   if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
5822     getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
5823 }
5824 
5825 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
5826   const auto *D = cast<ValueDecl>(GD.getDecl());
5827   const AliasAttr *AA = D->getAttr<AliasAttr>();
5828   assert(AA && "Not an alias?");
5829 
5830   StringRef MangledName = getMangledName(GD);
5831 
5832   if (AA->getAliasee() == MangledName) {
5833     Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
5834     return;
5835   }
5836 
5837   // If there is a definition in the module, then it wins over the alias.
5838   // This is dubious, but allow it to be safe.  Just ignore the alias.
5839   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
5840   if (Entry && !Entry->isDeclaration())
5841     return;
5842 
5843   Aliases.push_back(GD);
5844 
5845   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
5846 
5847   // Create a reference to the named value.  This ensures that it is emitted
5848   // if a deferred decl.
5849   llvm::Constant *Aliasee;
5850   llvm::GlobalValue::LinkageTypes LT;
5851   if (isa<llvm::FunctionType>(DeclTy)) {
5852     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
5853                                       /*ForVTable=*/false);
5854     LT = getFunctionLinkage(GD);
5855   } else {
5856     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
5857                                     /*D=*/nullptr);
5858     if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl()))
5859       LT = getLLVMLinkageVarDefinition(VD);
5860     else
5861       LT = getFunctionLinkage(GD);
5862   }
5863 
5864   // Create the new alias itself, but don't set a name yet.
5865   unsigned AS = Aliasee->getType()->getPointerAddressSpace();
5866   auto *GA =
5867       llvm::GlobalAlias::create(DeclTy, AS, LT, "", Aliasee, &getModule());
5868 
5869   if (Entry) {
5870     if (GA->getAliasee() == Entry) {
5871       Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
5872       return;
5873     }
5874 
5875     assert(Entry->isDeclaration());
5876 
5877     // If there is a declaration in the module, then we had an extern followed
5878     // by the alias, as in:
5879     //   extern int test6();
5880     //   ...
5881     //   int test6() __attribute__((alias("test7")));
5882     //
5883     // Remove it and replace uses of it with the alias.
5884     GA->takeName(Entry);
5885 
5886     Entry->replaceAllUsesWith(GA);
5887     Entry->eraseFromParent();
5888   } else {
5889     GA->setName(MangledName);
5890   }
5891 
5892   // Set attributes which are particular to an alias; this is a
5893   // specialization of the attributes which may be set on a global
5894   // variable/function.
5895   if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
5896       D->isWeakImported()) {
5897     GA->setLinkage(llvm::Function::WeakAnyLinkage);
5898   }
5899 
5900   if (const auto *VD = dyn_cast<VarDecl>(D))
5901     if (VD->getTLSKind())
5902       setTLSMode(GA, *VD);
5903 
5904   SetCommonAttributes(GD, GA);
5905 
5906   // Emit global alias debug information.
5907   if (isa<VarDecl>(D))
5908     if (CGDebugInfo *DI = getModuleDebugInfo())
5909       DI->EmitGlobalAlias(cast<llvm::GlobalValue>(GA->getAliasee()->stripPointerCasts()), GD);
5910 }
5911 
5912 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
5913   const auto *D = cast<ValueDecl>(GD.getDecl());
5914   const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
5915   assert(IFA && "Not an ifunc?");
5916 
5917   StringRef MangledName = getMangledName(GD);
5918 
5919   if (IFA->getResolver() == MangledName) {
5920     Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
5921     return;
5922   }
5923 
5924   // Report an error if some definition overrides ifunc.
5925   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
5926   if (Entry && !Entry->isDeclaration()) {
5927     GlobalDecl OtherGD;
5928     if (lookupRepresentativeDecl(MangledName, OtherGD) &&
5929         DiagnosedConflictingDefinitions.insert(GD).second) {
5930       Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name)
5931           << MangledName;
5932       Diags.Report(OtherGD.getDecl()->getLocation(),
5933                    diag::note_previous_definition);
5934     }
5935     return;
5936   }
5937 
5938   Aliases.push_back(GD);
5939 
5940   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
5941   llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(DeclTy);
5942   llvm::Constant *Resolver =
5943       GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {},
5944                               /*ForVTable=*/false);
5945   llvm::GlobalIFunc *GIF =
5946       llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage,
5947                                 "", Resolver, &getModule());
5948   if (Entry) {
5949     if (GIF->getResolver() == Entry) {
5950       Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
5951       return;
5952     }
5953     assert(Entry->isDeclaration());
5954 
5955     // If there is a declaration in the module, then we had an extern followed
5956     // by the ifunc, as in:
5957     //   extern int test();
5958     //   ...
5959     //   int test() __attribute__((ifunc("resolver")));
5960     //
5961     // Remove it and replace uses of it with the ifunc.
5962     GIF->takeName(Entry);
5963 
5964     Entry->replaceAllUsesWith(GIF);
5965     Entry->eraseFromParent();
5966   } else
5967     GIF->setName(MangledName);
5968   if (auto *F = dyn_cast<llvm::Function>(Resolver)) {
5969     F->addFnAttr(llvm::Attribute::DisableSanitizerInstrumentation);
5970   }
5971   SetCommonAttributes(GD, GIF);
5972 }
5973 
5974 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
5975                                             ArrayRef<llvm::Type*> Tys) {
5976   return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
5977                                          Tys);
5978 }
5979 
5980 static llvm::StringMapEntry<llvm::GlobalVariable *> &
5981 GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
5982                          const StringLiteral *Literal, bool TargetIsLSB,
5983                          bool &IsUTF16, unsigned &StringLength) {
5984   StringRef String = Literal->getString();
5985   unsigned NumBytes = String.size();
5986 
5987   // Check for simple case.
5988   if (!Literal->containsNonAsciiOrNull()) {
5989     StringLength = NumBytes;
5990     return *Map.insert(std::make_pair(String, nullptr)).first;
5991   }
5992 
5993   // Otherwise, convert the UTF8 literals into a string of shorts.
5994   IsUTF16 = true;
5995 
5996   SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
5997   const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
5998   llvm::UTF16 *ToPtr = &ToBuf[0];
5999 
6000   (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
6001                                  ToPtr + NumBytes, llvm::strictConversion);
6002 
6003   // ConvertUTF8toUTF16 returns the length in ToPtr.
6004   StringLength = ToPtr - &ToBuf[0];
6005 
6006   // Add an explicit null.
6007   *ToPtr = 0;
6008   return *Map.insert(std::make_pair(
6009                          StringRef(reinterpret_cast<const char *>(ToBuf.data()),
6010                                    (StringLength + 1) * 2),
6011                          nullptr)).first;
6012 }
6013 
6014 ConstantAddress
6015 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
6016   unsigned StringLength = 0;
6017   bool isUTF16 = false;
6018   llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
6019       GetConstantCFStringEntry(CFConstantStringMap, Literal,
6020                                getDataLayout().isLittleEndian(), isUTF16,
6021                                StringLength);
6022 
6023   if (auto *C = Entry.second)
6024     return ConstantAddress(
6025         C, C->getValueType(), CharUnits::fromQuantity(C->getAlignment()));
6026 
6027   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
6028   llvm::Constant *Zeros[] = { Zero, Zero };
6029 
6030   const ASTContext &Context = getContext();
6031   const llvm::Triple &Triple = getTriple();
6032 
6033   const auto CFRuntime = getLangOpts().CFRuntime;
6034   const bool IsSwiftABI =
6035       static_cast<unsigned>(CFRuntime) >=
6036       static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift);
6037   const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1;
6038 
6039   // If we don't already have it, get __CFConstantStringClassReference.
6040   if (!CFConstantStringClassRef) {
6041     const char *CFConstantStringClassName = "__CFConstantStringClassReference";
6042     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
6043     Ty = llvm::ArrayType::get(Ty, 0);
6044 
6045     switch (CFRuntime) {
6046     default: break;
6047     case LangOptions::CoreFoundationABI::Swift: [[fallthrough]];
6048     case LangOptions::CoreFoundationABI::Swift5_0:
6049       CFConstantStringClassName =
6050           Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN"
6051                               : "$s10Foundation19_NSCFConstantStringCN";
6052       Ty = IntPtrTy;
6053       break;
6054     case LangOptions::CoreFoundationABI::Swift4_2:
6055       CFConstantStringClassName =
6056           Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN"
6057                               : "$S10Foundation19_NSCFConstantStringCN";
6058       Ty = IntPtrTy;
6059       break;
6060     case LangOptions::CoreFoundationABI::Swift4_1:
6061       CFConstantStringClassName =
6062           Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN"
6063                               : "__T010Foundation19_NSCFConstantStringCN";
6064       Ty = IntPtrTy;
6065       break;
6066     }
6067 
6068     llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName);
6069 
6070     if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) {
6071       llvm::GlobalValue *GV = nullptr;
6072 
6073       if ((GV = dyn_cast<llvm::GlobalValue>(C))) {
6074         IdentifierInfo &II = Context.Idents.get(GV->getName());
6075         TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
6076         DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
6077 
6078         const VarDecl *VD = nullptr;
6079         for (const auto *Result : DC->lookup(&II))
6080           if ((VD = dyn_cast<VarDecl>(Result)))
6081             break;
6082 
6083         if (Triple.isOSBinFormatELF()) {
6084           if (!VD)
6085             GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6086         } else {
6087           GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6088           if (!VD || !VD->hasAttr<DLLExportAttr>())
6089             GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
6090           else
6091             GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
6092         }
6093 
6094         setDSOLocal(GV);
6095       }
6096     }
6097 
6098     // Decay array -> ptr
6099     CFConstantStringClassRef =
6100         IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty)
6101                    : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros);
6102   }
6103 
6104   QualType CFTy = Context.getCFConstantStringType();
6105 
6106   auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
6107 
6108   ConstantInitBuilder Builder(*this);
6109   auto Fields = Builder.beginStruct(STy);
6110 
6111   // Class pointer.
6112   Fields.add(cast<llvm::Constant>(CFConstantStringClassRef));
6113 
6114   // Flags.
6115   if (IsSwiftABI) {
6116     Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01);
6117     Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8);
6118   } else {
6119     Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8);
6120   }
6121 
6122   // String pointer.
6123   llvm::Constant *C = nullptr;
6124   if (isUTF16) {
6125     auto Arr = llvm::ArrayRef(
6126         reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
6127         Entry.first().size() / 2);
6128     C = llvm::ConstantDataArray::get(VMContext, Arr);
6129   } else {
6130     C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
6131   }
6132 
6133   // Note: -fwritable-strings doesn't make the backing store strings of
6134   // CFStrings writable.
6135   auto *GV =
6136       new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
6137                                llvm::GlobalValue::PrivateLinkage, C, ".str");
6138   GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6139   // Don't enforce the target's minimum global alignment, since the only use
6140   // of the string is via this class initializer.
6141   CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy)
6142                             : Context.getTypeAlignInChars(Context.CharTy);
6143   GV->setAlignment(Align.getAsAlign());
6144 
6145   // FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
6146   // Without it LLVM can merge the string with a non unnamed_addr one during
6147   // LTO.  Doing that changes the section it ends in, which surprises ld64.
6148   if (Triple.isOSBinFormatMachO())
6149     GV->setSection(isUTF16 ? "__TEXT,__ustring"
6150                            : "__TEXT,__cstring,cstring_literals");
6151   // Make sure the literal ends up in .rodata to allow for safe ICF and for
6152   // the static linker to adjust permissions to read-only later on.
6153   else if (Triple.isOSBinFormatELF())
6154     GV->setSection(".rodata");
6155 
6156   // String.
6157   llvm::Constant *Str =
6158       llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
6159 
6160   Fields.add(Str);
6161 
6162   // String length.
6163   llvm::IntegerType *LengthTy =
6164       llvm::IntegerType::get(getModule().getContext(),
6165                              Context.getTargetInfo().getLongWidth());
6166   if (IsSwiftABI) {
6167     if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 ||
6168         CFRuntime == LangOptions::CoreFoundationABI::Swift4_2)
6169       LengthTy = Int32Ty;
6170     else
6171       LengthTy = IntPtrTy;
6172   }
6173   Fields.addInt(LengthTy, StringLength);
6174 
6175   // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
6176   // properly aligned on 32-bit platforms.
6177   CharUnits Alignment =
6178       IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign();
6179 
6180   // The struct.
6181   GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
6182                                     /*isConstant=*/false,
6183                                     llvm::GlobalVariable::PrivateLinkage);
6184   GV->addAttribute("objc_arc_inert");
6185   switch (Triple.getObjectFormat()) {
6186   case llvm::Triple::UnknownObjectFormat:
6187     llvm_unreachable("unknown file format");
6188   case llvm::Triple::DXContainer:
6189   case llvm::Triple::GOFF:
6190   case llvm::Triple::SPIRV:
6191   case llvm::Triple::XCOFF:
6192     llvm_unreachable("unimplemented");
6193   case llvm::Triple::COFF:
6194   case llvm::Triple::ELF:
6195   case llvm::Triple::Wasm:
6196     GV->setSection("cfstring");
6197     break;
6198   case llvm::Triple::MachO:
6199     GV->setSection("__DATA,__cfstring");
6200     break;
6201   }
6202   Entry.second = GV;
6203 
6204   return ConstantAddress(GV, GV->getValueType(), Alignment);
6205 }
6206 
6207 bool CodeGenModule::getExpressionLocationsEnabled() const {
6208   return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo;
6209 }
6210 
6211 QualType CodeGenModule::getObjCFastEnumerationStateType() {
6212   if (ObjCFastEnumerationStateType.isNull()) {
6213     RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
6214     D->startDefinition();
6215 
6216     QualType FieldTypes[] = {
6217         Context.UnsignedLongTy, Context.getPointerType(Context.getObjCIdType()),
6218         Context.getPointerType(Context.UnsignedLongTy),
6219         Context.getConstantArrayType(Context.UnsignedLongTy, llvm::APInt(32, 5),
6220                                      nullptr, ArraySizeModifier::Normal, 0)};
6221 
6222     for (size_t i = 0; i < 4; ++i) {
6223       FieldDecl *Field = FieldDecl::Create(Context,
6224                                            D,
6225                                            SourceLocation(),
6226                                            SourceLocation(), nullptr,
6227                                            FieldTypes[i], /*TInfo=*/nullptr,
6228                                            /*BitWidth=*/nullptr,
6229                                            /*Mutable=*/false,
6230                                            ICIS_NoInit);
6231       Field->setAccess(AS_public);
6232       D->addDecl(Field);
6233     }
6234 
6235     D->completeDefinition();
6236     ObjCFastEnumerationStateType = Context.getTagDeclType(D);
6237   }
6238 
6239   return ObjCFastEnumerationStateType;
6240 }
6241 
6242 llvm::Constant *
6243 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
6244   assert(!E->getType()->isPointerType() && "Strings are always arrays");
6245 
6246   // Don't emit it as the address of the string, emit the string data itself
6247   // as an inline array.
6248   if (E->getCharByteWidth() == 1) {
6249     SmallString<64> Str(E->getString());
6250 
6251     // Resize the string to the right size, which is indicated by its type.
6252     const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
6253     assert(CAT && "String literal not of constant array type!");
6254     Str.resize(CAT->getSize().getZExtValue());
6255     return llvm::ConstantDataArray::getString(VMContext, Str, false);
6256   }
6257 
6258   auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
6259   llvm::Type *ElemTy = AType->getElementType();
6260   unsigned NumElements = AType->getNumElements();
6261 
6262   // Wide strings have either 2-byte or 4-byte elements.
6263   if (ElemTy->getPrimitiveSizeInBits() == 16) {
6264     SmallVector<uint16_t, 32> Elements;
6265     Elements.reserve(NumElements);
6266 
6267     for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6268       Elements.push_back(E->getCodeUnit(i));
6269     Elements.resize(NumElements);
6270     return llvm::ConstantDataArray::get(VMContext, Elements);
6271   }
6272 
6273   assert(ElemTy->getPrimitiveSizeInBits() == 32);
6274   SmallVector<uint32_t, 32> Elements;
6275   Elements.reserve(NumElements);
6276 
6277   for(unsigned i = 0, e = E->getLength(); i != e; ++i)
6278     Elements.push_back(E->getCodeUnit(i));
6279   Elements.resize(NumElements);
6280   return llvm::ConstantDataArray::get(VMContext, Elements);
6281 }
6282 
6283 static llvm::GlobalVariable *
6284 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
6285                       CodeGenModule &CGM, StringRef GlobalName,
6286                       CharUnits Alignment) {
6287   unsigned AddrSpace = CGM.getContext().getTargetAddressSpace(
6288       CGM.GetGlobalConstantAddressSpace());
6289 
6290   llvm::Module &M = CGM.getModule();
6291   // Create a global variable for this string
6292   auto *GV = new llvm::GlobalVariable(
6293       M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
6294       nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
6295   GV->setAlignment(Alignment.getAsAlign());
6296   GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6297   if (GV->isWeakForLinker()) {
6298     assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals");
6299     GV->setComdat(M.getOrInsertComdat(GV->getName()));
6300   }
6301   CGM.setDSOLocal(GV);
6302 
6303   return GV;
6304 }
6305 
6306 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
6307 /// constant array for the given string literal.
6308 ConstantAddress
6309 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
6310                                                   StringRef Name) {
6311   CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType());
6312 
6313   llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
6314   llvm::GlobalVariable **Entry = nullptr;
6315   if (!LangOpts.WritableStrings) {
6316     Entry = &ConstantStringMap[C];
6317     if (auto GV = *Entry) {
6318       if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6319         GV->setAlignment(Alignment.getAsAlign());
6320       return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6321                              GV->getValueType(), Alignment);
6322     }
6323   }
6324 
6325   SmallString<256> MangledNameBuffer;
6326   StringRef GlobalVariableName;
6327   llvm::GlobalValue::LinkageTypes LT;
6328 
6329   // Mangle the string literal if that's how the ABI merges duplicate strings.
6330   // Don't do it if they are writable, since we don't want writes in one TU to
6331   // affect strings in another.
6332   if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
6333       !LangOpts.WritableStrings) {
6334     llvm::raw_svector_ostream Out(MangledNameBuffer);
6335     getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
6336     LT = llvm::GlobalValue::LinkOnceODRLinkage;
6337     GlobalVariableName = MangledNameBuffer;
6338   } else {
6339     LT = llvm::GlobalValue::PrivateLinkage;
6340     GlobalVariableName = Name;
6341   }
6342 
6343   auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
6344 
6345   CGDebugInfo *DI = getModuleDebugInfo();
6346   if (DI && getCodeGenOpts().hasReducedDebugInfo())
6347     DI->AddStringLiteralDebugInfo(GV, S);
6348 
6349   if (Entry)
6350     *Entry = GV;
6351 
6352   SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>");
6353 
6354   return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6355                          GV->getValueType(), Alignment);
6356 }
6357 
6358 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
6359 /// array for the given ObjCEncodeExpr node.
6360 ConstantAddress
6361 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
6362   std::string Str;
6363   getContext().getObjCEncodingForType(E->getEncodedType(), Str);
6364 
6365   return GetAddrOfConstantCString(Str);
6366 }
6367 
6368 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
6369 /// the literal and a terminating '\0' character.
6370 /// The result has pointer to array type.
6371 ConstantAddress CodeGenModule::GetAddrOfConstantCString(
6372     const std::string &Str, const char *GlobalName) {
6373   StringRef StrWithNull(Str.c_str(), Str.size() + 1);
6374   CharUnits Alignment =
6375     getContext().getAlignOfGlobalVarInChars(getContext().CharTy);
6376 
6377   llvm::Constant *C =
6378       llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
6379 
6380   // Don't share any string literals if strings aren't constant.
6381   llvm::GlobalVariable **Entry = nullptr;
6382   if (!LangOpts.WritableStrings) {
6383     Entry = &ConstantStringMap[C];
6384     if (auto GV = *Entry) {
6385       if (uint64_t(Alignment.getQuantity()) > GV->getAlignment())
6386         GV->setAlignment(Alignment.getAsAlign());
6387       return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6388                              GV->getValueType(), Alignment);
6389     }
6390   }
6391 
6392   // Get the default prefix if a name wasn't specified.
6393   if (!GlobalName)
6394     GlobalName = ".str";
6395   // Create a global variable for this.
6396   auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
6397                                   GlobalName, Alignment);
6398   if (Entry)
6399     *Entry = GV;
6400 
6401   return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV),
6402                          GV->getValueType(), Alignment);
6403 }
6404 
6405 ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
6406     const MaterializeTemporaryExpr *E, const Expr *Init) {
6407   assert((E->getStorageDuration() == SD_Static ||
6408           E->getStorageDuration() == SD_Thread) && "not a global temporary");
6409   const auto *VD = cast<VarDecl>(E->getExtendingDecl());
6410 
6411   // If we're not materializing a subobject of the temporary, keep the
6412   // cv-qualifiers from the type of the MaterializeTemporaryExpr.
6413   QualType MaterializedType = Init->getType();
6414   if (Init == E->getSubExpr())
6415     MaterializedType = E->getType();
6416 
6417   CharUnits Align = getContext().getTypeAlignInChars(MaterializedType);
6418 
6419   auto InsertResult = MaterializedGlobalTemporaryMap.insert({E, nullptr});
6420   if (!InsertResult.second) {
6421     // We've seen this before: either we already created it or we're in the
6422     // process of doing so.
6423     if (!InsertResult.first->second) {
6424       // We recursively re-entered this function, probably during emission of
6425       // the initializer. Create a placeholder. We'll clean this up in the
6426       // outer call, at the end of this function.
6427       llvm::Type *Type = getTypes().ConvertTypeForMem(MaterializedType);
6428       InsertResult.first->second = new llvm::GlobalVariable(
6429           getModule(), Type, false, llvm::GlobalVariable::InternalLinkage,
6430           nullptr);
6431     }
6432     return ConstantAddress(InsertResult.first->second,
6433                            llvm::cast<llvm::GlobalVariable>(
6434                                InsertResult.first->second->stripPointerCasts())
6435                                ->getValueType(),
6436                            Align);
6437   }
6438 
6439   // FIXME: If an externally-visible declaration extends multiple temporaries,
6440   // we need to give each temporary the same name in every translation unit (and
6441   // we also need to make the temporaries externally-visible).
6442   SmallString<256> Name;
6443   llvm::raw_svector_ostream Out(Name);
6444   getCXXABI().getMangleContext().mangleReferenceTemporary(
6445       VD, E->getManglingNumber(), Out);
6446 
6447   APValue *Value = nullptr;
6448   if (E->getStorageDuration() == SD_Static && VD->evaluateValue()) {
6449     // If the initializer of the extending declaration is a constant
6450     // initializer, we should have a cached constant initializer for this
6451     // temporary. Note that this might have a different value from the value
6452     // computed by evaluating the initializer if the surrounding constant
6453     // expression modifies the temporary.
6454     Value = E->getOrCreateValue(false);
6455   }
6456 
6457   // Try evaluating it now, it might have a constant initializer.
6458   Expr::EvalResult EvalResult;
6459   if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
6460       !EvalResult.hasSideEffects())
6461     Value = &EvalResult.Val;
6462 
6463   LangAS AddrSpace = GetGlobalVarAddressSpace(VD);
6464 
6465   std::optional<ConstantEmitter> emitter;
6466   llvm::Constant *InitialValue = nullptr;
6467   bool Constant = false;
6468   llvm::Type *Type;
6469   if (Value) {
6470     // The temporary has a constant initializer, use it.
6471     emitter.emplace(*this);
6472     InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
6473                                                MaterializedType);
6474     Constant =
6475         MaterializedType.isConstantStorage(getContext(), /*ExcludeCtor*/ Value,
6476                                            /*ExcludeDtor*/ false);
6477     Type = InitialValue->getType();
6478   } else {
6479     // No initializer, the initialization will be provided when we
6480     // initialize the declaration which performed lifetime extension.
6481     Type = getTypes().ConvertTypeForMem(MaterializedType);
6482   }
6483 
6484   // Create a global variable for this lifetime-extended temporary.
6485   llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(VD);
6486   if (Linkage == llvm::GlobalVariable::ExternalLinkage) {
6487     const VarDecl *InitVD;
6488     if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
6489         isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
6490       // Temporaries defined inside a class get linkonce_odr linkage because the
6491       // class can be defined in multiple translation units.
6492       Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
6493     } else {
6494       // There is no need for this temporary to have external linkage if the
6495       // VarDecl has external linkage.
6496       Linkage = llvm::GlobalVariable::InternalLinkage;
6497     }
6498   }
6499   auto TargetAS = getContext().getTargetAddressSpace(AddrSpace);
6500   auto *GV = new llvm::GlobalVariable(
6501       getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
6502       /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
6503   if (emitter) emitter->finalize(GV);
6504   // Don't assign dllimport or dllexport to local linkage globals.
6505   if (!llvm::GlobalValue::isLocalLinkage(Linkage)) {
6506     setGVProperties(GV, VD);
6507     if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass)
6508       // The reference temporary should never be dllexport.
6509       GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
6510   }
6511   GV->setAlignment(Align.getAsAlign());
6512   if (supportsCOMDAT() && GV->isWeakForLinker())
6513     GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
6514   if (VD->getTLSKind())
6515     setTLSMode(GV, *VD);
6516   llvm::Constant *CV = GV;
6517   if (AddrSpace != LangAS::Default)
6518     CV = getTargetCodeGenInfo().performAddrSpaceCast(
6519         *this, GV, AddrSpace, LangAS::Default,
6520         llvm::PointerType::get(
6521             getLLVMContext(),
6522             getContext().getTargetAddressSpace(LangAS::Default)));
6523 
6524   // Update the map with the new temporary. If we created a placeholder above,
6525   // replace it with the new global now.
6526   llvm::Constant *&Entry = MaterializedGlobalTemporaryMap[E];
6527   if (Entry) {
6528     Entry->replaceAllUsesWith(CV);
6529     llvm::cast<llvm::GlobalVariable>(Entry)->eraseFromParent();
6530   }
6531   Entry = CV;
6532 
6533   return ConstantAddress(CV, Type, Align);
6534 }
6535 
6536 /// EmitObjCPropertyImplementations - Emit information for synthesized
6537 /// properties for an implementation.
6538 void CodeGenModule::EmitObjCPropertyImplementations(const
6539                                                     ObjCImplementationDecl *D) {
6540   for (const auto *PID : D->property_impls()) {
6541     // Dynamic is just for type-checking.
6542     if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
6543       ObjCPropertyDecl *PD = PID->getPropertyDecl();
6544 
6545       // Determine which methods need to be implemented, some may have
6546       // been overridden. Note that ::isPropertyAccessor is not the method
6547       // we want, that just indicates if the decl came from a
6548       // property. What we want to know is if the method is defined in
6549       // this implementation.
6550       auto *Getter = PID->getGetterMethodDecl();
6551       if (!Getter || Getter->isSynthesizedAccessorStub())
6552         CodeGenFunction(*this).GenerateObjCGetter(
6553             const_cast<ObjCImplementationDecl *>(D), PID);
6554       auto *Setter = PID->getSetterMethodDecl();
6555       if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub()))
6556         CodeGenFunction(*this).GenerateObjCSetter(
6557                                  const_cast<ObjCImplementationDecl *>(D), PID);
6558     }
6559   }
6560 }
6561 
6562 static bool needsDestructMethod(ObjCImplementationDecl *impl) {
6563   const ObjCInterfaceDecl *iface = impl->getClassInterface();
6564   for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
6565        ivar; ivar = ivar->getNextIvar())
6566     if (ivar->getType().isDestructedType())
6567       return true;
6568 
6569   return false;
6570 }
6571 
6572 static bool AllTrivialInitializers(CodeGenModule &CGM,
6573                                    ObjCImplementationDecl *D) {
6574   CodeGenFunction CGF(CGM);
6575   for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
6576        E = D->init_end(); B != E; ++B) {
6577     CXXCtorInitializer *CtorInitExp = *B;
6578     Expr *Init = CtorInitExp->getInit();
6579     if (!CGF.isTrivialInitializer(Init))
6580       return false;
6581   }
6582   return true;
6583 }
6584 
6585 /// EmitObjCIvarInitializations - Emit information for ivar initialization
6586 /// for an implementation.
6587 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
6588   // We might need a .cxx_destruct even if we don't have any ivar initializers.
6589   if (needsDestructMethod(D)) {
6590     IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
6591     Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
6592     ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(
6593         getContext(), D->getLocation(), D->getLocation(), cxxSelector,
6594         getContext().VoidTy, nullptr, D,
6595         /*isInstance=*/true, /*isVariadic=*/false,
6596         /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
6597         /*isImplicitlyDeclared=*/true,
6598         /*isDefined=*/false, ObjCImplementationControl::Required);
6599     D->addInstanceMethod(DTORMethod);
6600     CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
6601     D->setHasDestructors(true);
6602   }
6603 
6604   // If the implementation doesn't have any ivar initializers, we don't need
6605   // a .cxx_construct.
6606   if (D->getNumIvarInitializers() == 0 ||
6607       AllTrivialInitializers(*this, D))
6608     return;
6609 
6610   IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
6611   Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
6612   // The constructor returns 'self'.
6613   ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(
6614       getContext(), D->getLocation(), D->getLocation(), cxxSelector,
6615       getContext().getObjCIdType(), nullptr, D, /*isInstance=*/true,
6616       /*isVariadic=*/false,
6617       /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
6618       /*isImplicitlyDeclared=*/true,
6619       /*isDefined=*/false, ObjCImplementationControl::Required);
6620   D->addInstanceMethod(CTORMethod);
6621   CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
6622   D->setHasNonZeroConstructors(true);
6623 }
6624 
6625 // EmitLinkageSpec - Emit all declarations in a linkage spec.
6626 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
6627   if (LSD->getLanguage() != LinkageSpecLanguageIDs::C &&
6628       LSD->getLanguage() != LinkageSpecLanguageIDs::CXX) {
6629     ErrorUnsupported(LSD, "linkage spec");
6630     return;
6631   }
6632 
6633   EmitDeclContext(LSD);
6634 }
6635 
6636 void CodeGenModule::EmitTopLevelStmt(const TopLevelStmtDecl *D) {
6637   // Device code should not be at top level.
6638   if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
6639     return;
6640 
6641   std::unique_ptr<CodeGenFunction> &CurCGF =
6642       GlobalTopLevelStmtBlockInFlight.first;
6643 
6644   // We emitted a top-level stmt but after it there is initialization.
6645   // Stop squashing the top-level stmts into a single function.
6646   if (CurCGF && CXXGlobalInits.back() != CurCGF->CurFn) {
6647     CurCGF->FinishFunction(D->getEndLoc());
6648     CurCGF = nullptr;
6649   }
6650 
6651   if (!CurCGF) {
6652     // void __stmts__N(void)
6653     // FIXME: Ask the ABI name mangler to pick a name.
6654     std::string Name = "__stmts__" + llvm::utostr(CXXGlobalInits.size());
6655     FunctionArgList Args;
6656     QualType RetTy = getContext().VoidTy;
6657     const CGFunctionInfo &FnInfo =
6658         getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args);
6659     llvm::FunctionType *FnTy = getTypes().GetFunctionType(FnInfo);
6660     llvm::Function *Fn = llvm::Function::Create(
6661         FnTy, llvm::GlobalValue::InternalLinkage, Name, &getModule());
6662 
6663     CurCGF.reset(new CodeGenFunction(*this));
6664     GlobalTopLevelStmtBlockInFlight.second = D;
6665     CurCGF->StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
6666                           D->getBeginLoc(), D->getBeginLoc());
6667     CXXGlobalInits.push_back(Fn);
6668   }
6669 
6670   CurCGF->EmitStmt(D->getStmt());
6671 }
6672 
6673 void CodeGenModule::EmitDeclContext(const DeclContext *DC) {
6674   for (auto *I : DC->decls()) {
6675     // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
6676     // are themselves considered "top-level", so EmitTopLevelDecl on an
6677     // ObjCImplDecl does not recursively visit them. We need to do that in
6678     // case they're nested inside another construct (LinkageSpecDecl /
6679     // ExportDecl) that does stop them from being considered "top-level".
6680     if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
6681       for (auto *M : OID->methods())
6682         EmitTopLevelDecl(M);
6683     }
6684 
6685     EmitTopLevelDecl(I);
6686   }
6687 }
6688 
6689 /// EmitTopLevelDecl - Emit code for a single top level declaration.
6690 void CodeGenModule::EmitTopLevelDecl(Decl *D) {
6691   // Ignore dependent declarations.
6692   if (D->isTemplated())
6693     return;
6694 
6695   // Consteval function shouldn't be emitted.
6696   if (auto *FD = dyn_cast<FunctionDecl>(D); FD && FD->isImmediateFunction())
6697     return;
6698 
6699   switch (D->getKind()) {
6700   case Decl::CXXConversion:
6701   case Decl::CXXMethod:
6702   case Decl::Function:
6703     EmitGlobal(cast<FunctionDecl>(D));
6704     // Always provide some coverage mapping
6705     // even for the functions that aren't emitted.
6706     AddDeferredUnusedCoverageMapping(D);
6707     break;
6708 
6709   case Decl::CXXDeductionGuide:
6710     // Function-like, but does not result in code emission.
6711     break;
6712 
6713   case Decl::Var:
6714   case Decl::Decomposition:
6715   case Decl::VarTemplateSpecialization:
6716     EmitGlobal(cast<VarDecl>(D));
6717     if (auto *DD = dyn_cast<DecompositionDecl>(D))
6718       for (auto *B : DD->bindings())
6719         if (auto *HD = B->getHoldingVar())
6720           EmitGlobal(HD);
6721     break;
6722 
6723   // Indirect fields from global anonymous structs and unions can be
6724   // ignored; only the actual variable requires IR gen support.
6725   case Decl::IndirectField:
6726     break;
6727 
6728   // C++ Decls
6729   case Decl::Namespace:
6730     EmitDeclContext(cast<NamespaceDecl>(D));
6731     break;
6732   case Decl::ClassTemplateSpecialization: {
6733     const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
6734     if (CGDebugInfo *DI = getModuleDebugInfo())
6735       if (Spec->getSpecializationKind() ==
6736               TSK_ExplicitInstantiationDefinition &&
6737           Spec->hasDefinition())
6738         DI->completeTemplateDefinition(*Spec);
6739   } [[fallthrough]];
6740   case Decl::CXXRecord: {
6741     CXXRecordDecl *CRD = cast<CXXRecordDecl>(D);
6742     if (CGDebugInfo *DI = getModuleDebugInfo()) {
6743       if (CRD->hasDefinition())
6744         DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
6745       if (auto *ES = D->getASTContext().getExternalSource())
6746         if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
6747           DI->completeUnusedClass(*CRD);
6748     }
6749     // Emit any static data members, they may be definitions.
6750     for (auto *I : CRD->decls())
6751       if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
6752         EmitTopLevelDecl(I);
6753     break;
6754   }
6755     // No code generation needed.
6756   case Decl::UsingShadow:
6757   case Decl::ClassTemplate:
6758   case Decl::VarTemplate:
6759   case Decl::Concept:
6760   case Decl::VarTemplatePartialSpecialization:
6761   case Decl::FunctionTemplate:
6762   case Decl::TypeAliasTemplate:
6763   case Decl::Block:
6764   case Decl::Empty:
6765   case Decl::Binding:
6766     break;
6767   case Decl::Using:          // using X; [C++]
6768     if (CGDebugInfo *DI = getModuleDebugInfo())
6769         DI->EmitUsingDecl(cast<UsingDecl>(*D));
6770     break;
6771   case Decl::UsingEnum: // using enum X; [C++]
6772     if (CGDebugInfo *DI = getModuleDebugInfo())
6773       DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(*D));
6774     break;
6775   case Decl::NamespaceAlias:
6776     if (CGDebugInfo *DI = getModuleDebugInfo())
6777         DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
6778     break;
6779   case Decl::UsingDirective: // using namespace X; [C++]
6780     if (CGDebugInfo *DI = getModuleDebugInfo())
6781       DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
6782     break;
6783   case Decl::CXXConstructor:
6784     getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
6785     break;
6786   case Decl::CXXDestructor:
6787     getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
6788     break;
6789 
6790   case Decl::StaticAssert:
6791     // Nothing to do.
6792     break;
6793 
6794   // Objective-C Decls
6795 
6796   // Forward declarations, no (immediate) code generation.
6797   case Decl::ObjCInterface:
6798   case Decl::ObjCCategory:
6799     break;
6800 
6801   case Decl::ObjCProtocol: {
6802     auto *Proto = cast<ObjCProtocolDecl>(D);
6803     if (Proto->isThisDeclarationADefinition())
6804       ObjCRuntime->GenerateProtocol(Proto);
6805     break;
6806   }
6807 
6808   case Decl::ObjCCategoryImpl:
6809     // Categories have properties but don't support synthesize so we
6810     // can ignore them here.
6811     ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
6812     break;
6813 
6814   case Decl::ObjCImplementation: {
6815     auto *OMD = cast<ObjCImplementationDecl>(D);
6816     EmitObjCPropertyImplementations(OMD);
6817     EmitObjCIvarInitializations(OMD);
6818     ObjCRuntime->GenerateClass(OMD);
6819     // Emit global variable debug information.
6820     if (CGDebugInfo *DI = getModuleDebugInfo())
6821       if (getCodeGenOpts().hasReducedDebugInfo())
6822         DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
6823             OMD->getClassInterface()), OMD->getLocation());
6824     break;
6825   }
6826   case Decl::ObjCMethod: {
6827     auto *OMD = cast<ObjCMethodDecl>(D);
6828     // If this is not a prototype, emit the body.
6829     if (OMD->getBody())
6830       CodeGenFunction(*this).GenerateObjCMethod(OMD);
6831     break;
6832   }
6833   case Decl::ObjCCompatibleAlias:
6834     ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
6835     break;
6836 
6837   case Decl::PragmaComment: {
6838     const auto *PCD = cast<PragmaCommentDecl>(D);
6839     switch (PCD->getCommentKind()) {
6840     case PCK_Unknown:
6841       llvm_unreachable("unexpected pragma comment kind");
6842     case PCK_Linker:
6843       AppendLinkerOptions(PCD->getArg());
6844       break;
6845     case PCK_Lib:
6846         AddDependentLib(PCD->getArg());
6847       break;
6848     case PCK_Compiler:
6849     case PCK_ExeStr:
6850     case PCK_User:
6851       break; // We ignore all of these.
6852     }
6853     break;
6854   }
6855 
6856   case Decl::PragmaDetectMismatch: {
6857     const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
6858     AddDetectMismatch(PDMD->getName(), PDMD->getValue());
6859     break;
6860   }
6861 
6862   case Decl::LinkageSpec:
6863     EmitLinkageSpec(cast<LinkageSpecDecl>(D));
6864     break;
6865 
6866   case Decl::FileScopeAsm: {
6867     // File-scope asm is ignored during device-side CUDA compilation.
6868     if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
6869       break;
6870     // File-scope asm is ignored during device-side OpenMP compilation.
6871     if (LangOpts.OpenMPIsTargetDevice)
6872       break;
6873     // File-scope asm is ignored during device-side SYCL compilation.
6874     if (LangOpts.SYCLIsDevice)
6875       break;
6876     auto *AD = cast<FileScopeAsmDecl>(D);
6877     getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
6878     break;
6879   }
6880 
6881   case Decl::TopLevelStmt:
6882     EmitTopLevelStmt(cast<TopLevelStmtDecl>(D));
6883     break;
6884 
6885   case Decl::Import: {
6886     auto *Import = cast<ImportDecl>(D);
6887 
6888     // If we've already imported this module, we're done.
6889     if (!ImportedModules.insert(Import->getImportedModule()))
6890       break;
6891 
6892     // Emit debug information for direct imports.
6893     if (!Import->getImportedOwningModule()) {
6894       if (CGDebugInfo *DI = getModuleDebugInfo())
6895         DI->EmitImportDecl(*Import);
6896     }
6897 
6898     // For C++ standard modules we are done - we will call the module
6899     // initializer for imported modules, and that will likewise call those for
6900     // any imports it has.
6901     if (CXX20ModuleInits && Import->getImportedOwningModule() &&
6902         !Import->getImportedOwningModule()->isModuleMapModule())
6903       break;
6904 
6905     // For clang C++ module map modules the initializers for sub-modules are
6906     // emitted here.
6907 
6908     // Find all of the submodules and emit the module initializers.
6909     llvm::SmallPtrSet<clang::Module *, 16> Visited;
6910     SmallVector<clang::Module *, 16> Stack;
6911     Visited.insert(Import->getImportedModule());
6912     Stack.push_back(Import->getImportedModule());
6913 
6914     while (!Stack.empty()) {
6915       clang::Module *Mod = Stack.pop_back_val();
6916       if (!EmittedModuleInitializers.insert(Mod).second)
6917         continue;
6918 
6919       for (auto *D : Context.getModuleInitializers(Mod))
6920         EmitTopLevelDecl(D);
6921 
6922       // Visit the submodules of this module.
6923       for (auto *Submodule : Mod->submodules()) {
6924         // Skip explicit children; they need to be explicitly imported to emit
6925         // the initializers.
6926         if (Submodule->IsExplicit)
6927           continue;
6928 
6929         if (Visited.insert(Submodule).second)
6930           Stack.push_back(Submodule);
6931       }
6932     }
6933     break;
6934   }
6935 
6936   case Decl::Export:
6937     EmitDeclContext(cast<ExportDecl>(D));
6938     break;
6939 
6940   case Decl::OMPThreadPrivate:
6941     EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
6942     break;
6943 
6944   case Decl::OMPAllocate:
6945     EmitOMPAllocateDecl(cast<OMPAllocateDecl>(D));
6946     break;
6947 
6948   case Decl::OMPDeclareReduction:
6949     EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D));
6950     break;
6951 
6952   case Decl::OMPDeclareMapper:
6953     EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D));
6954     break;
6955 
6956   case Decl::OMPRequires:
6957     EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D));
6958     break;
6959 
6960   case Decl::Typedef:
6961   case Decl::TypeAlias: // using foo = bar; [C++11]
6962     if (CGDebugInfo *DI = getModuleDebugInfo())
6963       DI->EmitAndRetainType(
6964           getContext().getTypedefType(cast<TypedefNameDecl>(D)));
6965     break;
6966 
6967   case Decl::Record:
6968     if (CGDebugInfo *DI = getModuleDebugInfo())
6969       if (cast<RecordDecl>(D)->getDefinition())
6970         DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D)));
6971     break;
6972 
6973   case Decl::Enum:
6974     if (CGDebugInfo *DI = getModuleDebugInfo())
6975       if (cast<EnumDecl>(D)->getDefinition())
6976         DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(D)));
6977     break;
6978 
6979   case Decl::HLSLBuffer:
6980     getHLSLRuntime().addBuffer(cast<HLSLBufferDecl>(D));
6981     break;
6982 
6983   default:
6984     // Make sure we handled everything we should, every other kind is a
6985     // non-top-level decl.  FIXME: Would be nice to have an isTopLevelDeclKind
6986     // function. Need to recode Decl::Kind to do that easily.
6987     assert(isa<TypeDecl>(D) && "Unsupported decl kind");
6988     break;
6989   }
6990 }
6991 
6992 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
6993   // Do we need to generate coverage mapping?
6994   if (!CodeGenOpts.CoverageMapping)
6995     return;
6996   switch (D->getKind()) {
6997   case Decl::CXXConversion:
6998   case Decl::CXXMethod:
6999   case Decl::Function:
7000   case Decl::ObjCMethod:
7001   case Decl::CXXConstructor:
7002   case Decl::CXXDestructor: {
7003     if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
7004       break;
7005     SourceManager &SM = getContext().getSourceManager();
7006     if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
7007       break;
7008     DeferredEmptyCoverageMappingDecls.try_emplace(D, true);
7009     break;
7010   }
7011   default:
7012     break;
7013   };
7014 }
7015 
7016 void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) {
7017   // Do we need to generate coverage mapping?
7018   if (!CodeGenOpts.CoverageMapping)
7019     return;
7020   if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
7021     if (Fn->isTemplateInstantiation())
7022       ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
7023   }
7024   DeferredEmptyCoverageMappingDecls.insert_or_assign(D, false);
7025 }
7026 
7027 void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
7028   // We call takeVector() here to avoid use-after-free.
7029   // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because
7030   // we deserialize function bodies to emit coverage info for them, and that
7031   // deserializes more declarations. How should we handle that case?
7032   for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) {
7033     if (!Entry.second)
7034       continue;
7035     const Decl *D = Entry.first;
7036     switch (D->getKind()) {
7037     case Decl::CXXConversion:
7038     case Decl::CXXMethod:
7039     case Decl::Function:
7040     case Decl::ObjCMethod: {
7041       CodeGenPGO PGO(*this);
7042       GlobalDecl GD(cast<FunctionDecl>(D));
7043       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
7044                                   getFunctionLinkage(GD));
7045       break;
7046     }
7047     case Decl::CXXConstructor: {
7048       CodeGenPGO PGO(*this);
7049       GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
7050       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
7051                                   getFunctionLinkage(GD));
7052       break;
7053     }
7054     case Decl::CXXDestructor: {
7055       CodeGenPGO PGO(*this);
7056       GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
7057       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
7058                                   getFunctionLinkage(GD));
7059       break;
7060     }
7061     default:
7062       break;
7063     };
7064   }
7065 }
7066 
7067 void CodeGenModule::EmitMainVoidAlias() {
7068   // In order to transition away from "__original_main" gracefully, emit an
7069   // alias for "main" in the no-argument case so that libc can detect when
7070   // new-style no-argument main is in used.
7071   if (llvm::Function *F = getModule().getFunction("main")) {
7072     if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
7073         F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) {
7074       auto *GA = llvm::GlobalAlias::create("__main_void", F);
7075       GA->setVisibility(llvm::GlobalValue::HiddenVisibility);
7076     }
7077   }
7078 }
7079 
7080 /// Turns the given pointer into a constant.
7081 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
7082                                           const void *Ptr) {
7083   uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
7084   llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
7085   return llvm::ConstantInt::get(i64, PtrInt);
7086 }
7087 
7088 static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
7089                                    llvm::NamedMDNode *&GlobalMetadata,
7090                                    GlobalDecl D,
7091                                    llvm::GlobalValue *Addr) {
7092   if (!GlobalMetadata)
7093     GlobalMetadata =
7094       CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
7095 
7096   // TODO: should we report variant information for ctors/dtors?
7097   llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
7098                            llvm::ConstantAsMetadata::get(GetPointerConstant(
7099                                CGM.getLLVMContext(), D.getDecl()))};
7100   GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
7101 }
7102 
7103 bool CodeGenModule::CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem,
7104                                                  llvm::GlobalValue *CppFunc) {
7105   // Store the list of ifuncs we need to replace uses in.
7106   llvm::SmallVector<llvm::GlobalIFunc *> IFuncs;
7107   // List of ConstantExprs that we should be able to delete when we're done
7108   // here.
7109   llvm::SmallVector<llvm::ConstantExpr *> CEs;
7110 
7111   // It isn't valid to replace the extern-C ifuncs if all we find is itself!
7112   if (Elem == CppFunc)
7113     return false;
7114 
7115   // First make sure that all users of this are ifuncs (or ifuncs via a
7116   // bitcast), and collect the list of ifuncs and CEs so we can work on them
7117   // later.
7118   for (llvm::User *User : Elem->users()) {
7119     // Users can either be a bitcast ConstExpr that is used by the ifuncs, OR an
7120     // ifunc directly. In any other case, just give up, as we don't know what we
7121     // could break by changing those.
7122     if (auto *ConstExpr = dyn_cast<llvm::ConstantExpr>(User)) {
7123       if (ConstExpr->getOpcode() != llvm::Instruction::BitCast)
7124         return false;
7125 
7126       for (llvm::User *CEUser : ConstExpr->users()) {
7127         if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(CEUser)) {
7128           IFuncs.push_back(IFunc);
7129         } else {
7130           return false;
7131         }
7132       }
7133       CEs.push_back(ConstExpr);
7134     } else if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(User)) {
7135       IFuncs.push_back(IFunc);
7136     } else {
7137       // This user is one we don't know how to handle, so fail redirection. This
7138       // will result in an ifunc retaining a resolver name that will ultimately
7139       // fail to be resolved to a defined function.
7140       return false;
7141     }
7142   }
7143 
7144   // Now we know this is a valid case where we can do this alias replacement, we
7145   // need to remove all of the references to Elem (and the bitcasts!) so we can
7146   // delete it.
7147   for (llvm::GlobalIFunc *IFunc : IFuncs)
7148     IFunc->setResolver(nullptr);
7149   for (llvm::ConstantExpr *ConstExpr : CEs)
7150     ConstExpr->destroyConstant();
7151 
7152   // We should now be out of uses for the 'old' version of this function, so we
7153   // can erase it as well.
7154   Elem->eraseFromParent();
7155 
7156   for (llvm::GlobalIFunc *IFunc : IFuncs) {
7157     // The type of the resolver is always just a function-type that returns the
7158     // type of the IFunc, so create that here. If the type of the actual
7159     // resolver doesn't match, it just gets bitcast to the right thing.
7160     auto *ResolverTy =
7161         llvm::FunctionType::get(IFunc->getType(), /*isVarArg*/ false);
7162     llvm::Constant *Resolver = GetOrCreateLLVMFunction(
7163         CppFunc->getName(), ResolverTy, {}, /*ForVTable*/ false);
7164     IFunc->setResolver(Resolver);
7165   }
7166   return true;
7167 }
7168 
7169 /// For each function which is declared within an extern "C" region and marked
7170 /// as 'used', but has internal linkage, create an alias from the unmangled
7171 /// name to the mangled name if possible. People expect to be able to refer
7172 /// to such functions with an unmangled name from inline assembly within the
7173 /// same translation unit.
7174 void CodeGenModule::EmitStaticExternCAliases() {
7175   if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases())
7176     return;
7177   for (auto &I : StaticExternCValues) {
7178     IdentifierInfo *Name = I.first;
7179     llvm::GlobalValue *Val = I.second;
7180 
7181     // If Val is null, that implies there were multiple declarations that each
7182     // had a claim to the unmangled name. In this case, generation of the alias
7183     // is suppressed. See CodeGenModule::MaybeHandleStaticInExternC.
7184     if (!Val)
7185       break;
7186 
7187     llvm::GlobalValue *ExistingElem =
7188         getModule().getNamedValue(Name->getName());
7189 
7190     // If there is either not something already by this name, or we were able to
7191     // replace all uses from IFuncs, create the alias.
7192     if (!ExistingElem || CheckAndReplaceExternCIFuncs(ExistingElem, Val))
7193       addCompilerUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
7194   }
7195 }
7196 
7197 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
7198                                              GlobalDecl &Result) const {
7199   auto Res = Manglings.find(MangledName);
7200   if (Res == Manglings.end())
7201     return false;
7202   Result = Res->getValue();
7203   return true;
7204 }
7205 
7206 /// Emits metadata nodes associating all the global values in the
7207 /// current module with the Decls they came from.  This is useful for
7208 /// projects using IR gen as a subroutine.
7209 ///
7210 /// Since there's currently no way to associate an MDNode directly
7211 /// with an llvm::GlobalValue, we create a global named metadata
7212 /// with the name 'clang.global.decl.ptrs'.
7213 void CodeGenModule::EmitDeclMetadata() {
7214   llvm::NamedMDNode *GlobalMetadata = nullptr;
7215 
7216   for (auto &I : MangledDeclNames) {
7217     llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
7218     // Some mangled names don't necessarily have an associated GlobalValue
7219     // in this module, e.g. if we mangled it for DebugInfo.
7220     if (Addr)
7221       EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
7222   }
7223 }
7224 
7225 /// Emits metadata nodes for all the local variables in the current
7226 /// function.
7227 void CodeGenFunction::EmitDeclMetadata() {
7228   if (LocalDeclMap.empty()) return;
7229 
7230   llvm::LLVMContext &Context = getLLVMContext();
7231 
7232   // Find the unique metadata ID for this name.
7233   unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
7234 
7235   llvm::NamedMDNode *GlobalMetadata = nullptr;
7236 
7237   for (auto &I : LocalDeclMap) {
7238     const Decl *D = I.first;
7239     llvm::Value *Addr = I.second.getPointer();
7240     if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
7241       llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
7242       Alloca->setMetadata(
7243           DeclPtrKind, llvm::MDNode::get(
7244                            Context, llvm::ValueAsMetadata::getConstant(DAddr)));
7245     } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
7246       GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
7247       EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
7248     }
7249   }
7250 }
7251 
7252 void CodeGenModule::EmitVersionIdentMetadata() {
7253   llvm::NamedMDNode *IdentMetadata =
7254     TheModule.getOrInsertNamedMetadata("llvm.ident");
7255   std::string Version = getClangFullVersion();
7256   llvm::LLVMContext &Ctx = TheModule.getContext();
7257 
7258   llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
7259   IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
7260 }
7261 
7262 void CodeGenModule::EmitCommandLineMetadata() {
7263   llvm::NamedMDNode *CommandLineMetadata =
7264     TheModule.getOrInsertNamedMetadata("llvm.commandline");
7265   std::string CommandLine = getCodeGenOpts().RecordCommandLine;
7266   llvm::LLVMContext &Ctx = TheModule.getContext();
7267 
7268   llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)};
7269   CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
7270 }
7271 
7272 void CodeGenModule::EmitCoverageFile() {
7273   llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
7274   if (!CUNode)
7275     return;
7276 
7277   llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
7278   llvm::LLVMContext &Ctx = TheModule.getContext();
7279   auto *CoverageDataFile =
7280       llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile);
7281   auto *CoverageNotesFile =
7282       llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile);
7283   for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
7284     llvm::MDNode *CU = CUNode->getOperand(i);
7285     llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
7286     GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
7287   }
7288 }
7289 
7290 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
7291                                                        bool ForEH) {
7292   // Return a bogus pointer if RTTI is disabled, unless it's for EH.
7293   // FIXME: should we even be calling this method if RTTI is disabled
7294   // and it's not for EH?
7295   if (!shouldEmitRTTI(ForEH))
7296     return llvm::Constant::getNullValue(GlobalsInt8PtrTy);
7297 
7298   if (ForEH && Ty->isObjCObjectPointerType() &&
7299       LangOpts.ObjCRuntime.isGNUFamily())
7300     return ObjCRuntime->GetEHType(Ty);
7301 
7302   return getCXXABI().getAddrOfRTTIDescriptor(Ty);
7303 }
7304 
7305 void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
7306   // Do not emit threadprivates in simd-only mode.
7307   if (LangOpts.OpenMP && LangOpts.OpenMPSimd)
7308     return;
7309   for (auto RefExpr : D->varlists()) {
7310     auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
7311     bool PerformInit =
7312         VD->getAnyInitializer() &&
7313         !VD->getAnyInitializer()->isConstantInitializer(getContext(),
7314                                                         /*ForRef=*/false);
7315 
7316     Address Addr(GetAddrOfGlobalVar(VD),
7317                  getTypes().ConvertTypeForMem(VD->getType()),
7318                  getContext().getDeclAlign(VD));
7319     if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
7320             VD, Addr, RefExpr->getBeginLoc(), PerformInit))
7321       CXXGlobalInits.push_back(InitFunction);
7322   }
7323 }
7324 
7325 llvm::Metadata *
7326 CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
7327                                             StringRef Suffix) {
7328   if (auto *FnType = T->getAs<FunctionProtoType>())
7329     T = getContext().getFunctionType(
7330         FnType->getReturnType(), FnType->getParamTypes(),
7331         FnType->getExtProtoInfo().withExceptionSpec(EST_None));
7332 
7333   llvm::Metadata *&InternalId = Map[T.getCanonicalType()];
7334   if (InternalId)
7335     return InternalId;
7336 
7337   if (isExternallyVisible(T->getLinkage())) {
7338     std::string OutName;
7339     llvm::raw_string_ostream Out(OutName);
7340     getCXXABI().getMangleContext().mangleCanonicalTypeName(
7341         T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);
7342 
7343     if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
7344       Out << ".normalized";
7345 
7346     Out << Suffix;
7347 
7348     InternalId = llvm::MDString::get(getLLVMContext(), Out.str());
7349   } else {
7350     InternalId = llvm::MDNode::getDistinct(getLLVMContext(),
7351                                            llvm::ArrayRef<llvm::Metadata *>());
7352   }
7353 
7354   return InternalId;
7355 }
7356 
7357 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) {
7358   return CreateMetadataIdentifierImpl(T, MetadataIdMap, "");
7359 }
7360 
7361 llvm::Metadata *
7362 CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
7363   return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
7364 }
7365 
7366 // Generalize pointer types to a void pointer with the qualifiers of the
7367 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
7368 // generalize to 'const void *' while 'char *' and 'const char **' generalize to
7369 // 'void *'.
7370 static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
7371   if (!Ty->isPointerType())
7372     return Ty;
7373 
7374   return Ctx.getPointerType(
7375       QualType(Ctx.VoidTy).withCVRQualifiers(
7376           Ty->getPointeeType().getCVRQualifiers()));
7377 }
7378 
7379 // Apply type generalization to a FunctionType's return and argument types
7380 static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) {
7381   if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
7382     SmallVector<QualType, 8> GeneralizedParams;
7383     for (auto &Param : FnType->param_types())
7384       GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
7385 
7386     return Ctx.getFunctionType(
7387         GeneralizeType(Ctx, FnType->getReturnType()),
7388         GeneralizedParams, FnType->getExtProtoInfo());
7389   }
7390 
7391   if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
7392     return Ctx.getFunctionNoProtoType(
7393         GeneralizeType(Ctx, FnType->getReturnType()));
7394 
7395   llvm_unreachable("Encountered unknown FunctionType");
7396 }
7397 
7398 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
7399   return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
7400                                       GeneralizedMetadataIdMap, ".generalized");
7401 }
7402 
7403 /// Returns whether this module needs the "all-vtables" type identifier.
7404 bool CodeGenModule::NeedAllVtablesTypeId() const {
7405   // Returns true if at least one of vtable-based CFI checkers is enabled and
7406   // is not in the trapping mode.
7407   return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
7408            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) ||
7409           (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
7410            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) ||
7411           (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
7412            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) ||
7413           (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) &&
7414            !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast)));
7415 }
7416 
7417 void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable,
7418                                           CharUnits Offset,
7419                                           const CXXRecordDecl *RD) {
7420   llvm::Metadata *MD =
7421       CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
7422   VTable->addTypeMetadata(Offset.getQuantity(), MD);
7423 
7424   if (CodeGenOpts.SanitizeCfiCrossDso)
7425     if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
7426       VTable->addTypeMetadata(Offset.getQuantity(),
7427                               llvm::ConstantAsMetadata::get(CrossDsoTypeId));
7428 
7429   if (NeedAllVtablesTypeId()) {
7430     llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables");
7431     VTable->addTypeMetadata(Offset.getQuantity(), MD);
7432   }
7433 }
7434 
7435 llvm::SanitizerStatReport &CodeGenModule::getSanStats() {
7436   if (!SanStats)
7437     SanStats = std::make_unique<llvm::SanitizerStatReport>(&getModule());
7438 
7439   return *SanStats;
7440 }
7441 
7442 llvm::Value *
7443 CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E,
7444                                                   CodeGenFunction &CGF) {
7445   llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType());
7446   auto *SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr());
7447   auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
7448   auto *Call = CGF.EmitRuntimeCall(
7449       CreateRuntimeFunction(FTy, "__translate_sampler_initializer"), {C});
7450   return Call;
7451 }
7452 
7453 CharUnits CodeGenModule::getNaturalPointeeTypeAlignment(
7454     QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) {
7455   return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo,
7456                                  /* forPointeeType= */ true);
7457 }
7458 
7459 CharUnits CodeGenModule::getNaturalTypeAlignment(QualType T,
7460                                                  LValueBaseInfo *BaseInfo,
7461                                                  TBAAAccessInfo *TBAAInfo,
7462                                                  bool forPointeeType) {
7463   if (TBAAInfo)
7464     *TBAAInfo = getTBAAAccessInfo(T);
7465 
7466   // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But
7467   // that doesn't return the information we need to compute BaseInfo.
7468 
7469   // Honor alignment typedef attributes even on incomplete types.
7470   // We also honor them straight for C++ class types, even as pointees;
7471   // there's an expressivity gap here.
7472   if (auto TT = T->getAs<TypedefType>()) {
7473     if (auto Align = TT->getDecl()->getMaxAlignment()) {
7474       if (BaseInfo)
7475         *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType);
7476       return getContext().toCharUnitsFromBits(Align);
7477     }
7478   }
7479 
7480   bool AlignForArray = T->isArrayType();
7481 
7482   // Analyze the base element type, so we don't get confused by incomplete
7483   // array types.
7484   T = getContext().getBaseElementType(T);
7485 
7486   if (T->isIncompleteType()) {
7487     // We could try to replicate the logic from
7488     // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
7489     // type is incomplete, so it's impossible to test. We could try to reuse
7490     // getTypeAlignIfKnown, but that doesn't return the information we need
7491     // to set BaseInfo.  So just ignore the possibility that the alignment is
7492     // greater than one.
7493     if (BaseInfo)
7494       *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
7495     return CharUnits::One();
7496   }
7497 
7498   if (BaseInfo)
7499     *BaseInfo = LValueBaseInfo(AlignmentSource::Type);
7500 
7501   CharUnits Alignment;
7502   const CXXRecordDecl *RD;
7503   if (T.getQualifiers().hasUnaligned()) {
7504     Alignment = CharUnits::One();
7505   } else if (forPointeeType && !AlignForArray &&
7506              (RD = T->getAsCXXRecordDecl())) {
7507     // For C++ class pointees, we don't know whether we're pointing at a
7508     // base or a complete object, so we generally need to use the
7509     // non-virtual alignment.
7510     Alignment = getClassPointerAlignment(RD);
7511   } else {
7512     Alignment = getContext().getTypeAlignInChars(T);
7513   }
7514 
7515   // Cap to the global maximum type alignment unless the alignment
7516   // was somehow explicit on the type.
7517   if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) {
7518     if (Alignment.getQuantity() > MaxAlign &&
7519         !getContext().isAlignmentRequired(T))
7520       Alignment = CharUnits::fromQuantity(MaxAlign);
7521   }
7522   return Alignment;
7523 }
7524 
7525 bool CodeGenModule::stopAutoInit() {
7526   unsigned StopAfter = getContext().getLangOpts().TrivialAutoVarInitStopAfter;
7527   if (StopAfter) {
7528     // This number is positive only when -ftrivial-auto-var-init-stop-after=* is
7529     // used
7530     if (NumAutoVarInit >= StopAfter) {
7531       return true;
7532     }
7533     if (!NumAutoVarInit) {
7534       unsigned DiagID = getDiags().getCustomDiagID(
7535           DiagnosticsEngine::Warning,
7536           "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
7537           "number of times ftrivial-auto-var-init=%1 gets applied.");
7538       getDiags().Report(DiagID)
7539           << StopAfter
7540           << (getContext().getLangOpts().getTrivialAutoVarInit() ==
7541                       LangOptions::TrivialAutoVarInitKind::Zero
7542                   ? "zero"
7543                   : "pattern");
7544     }
7545     ++NumAutoVarInit;
7546   }
7547   return false;
7548 }
7549 
7550 void CodeGenModule::printPostfixForExternalizedDecl(llvm::raw_ostream &OS,
7551                                                     const Decl *D) const {
7552   // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers
7553   // postfix beginning with '.' since the symbol name can be demangled.
7554   if (LangOpts.HIP)
7555     OS << (isa<VarDecl>(D) ? ".static." : ".intern.");
7556   else
7557     OS << (isa<VarDecl>(D) ? "__static__" : "__intern__");
7558 
7559   // If the CUID is not specified we try to generate a unique postfix.
7560   if (getLangOpts().CUID.empty()) {
7561     SourceManager &SM = getContext().getSourceManager();
7562     PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
7563     assert(PLoc.isValid() && "Source location is expected to be valid.");
7564 
7565     // Get the hash of the user defined macros.
7566     llvm::MD5 Hash;
7567     llvm::MD5::MD5Result Result;
7568     for (const auto &Arg : PreprocessorOpts.Macros)
7569       Hash.update(Arg.first);
7570     Hash.final(Result);
7571 
7572     // Get the UniqueID for the file containing the decl.
7573     llvm::sys::fs::UniqueID ID;
7574     if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) {
7575       PLoc = SM.getPresumedLoc(D->getLocation(), /*UseLineDirectives=*/false);
7576       assert(PLoc.isValid() && "Source location is expected to be valid.");
7577       if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID))
7578         SM.getDiagnostics().Report(diag::err_cannot_open_file)
7579             << PLoc.getFilename() << EC.message();
7580     }
7581     OS << llvm::format("%x", ID.getFile()) << llvm::format("%x", ID.getDevice())
7582        << "_" << llvm::utohexstr(Result.low(), /*LowerCase=*/true, /*Width=*/8);
7583   } else {
7584     OS << getContext().getCUIDHash();
7585   }
7586 }
7587 
7588 void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {
7589   assert(DeferredDeclsToEmit.empty() &&
7590          "Should have emitted all decls deferred to emit.");
7591   assert(NewBuilder->DeferredDecls.empty() &&
7592          "Newly created module should not have deferred decls");
7593   NewBuilder->DeferredDecls = std::move(DeferredDecls);
7594   assert(EmittedDeferredDecls.empty() &&
7595          "Still have (unmerged) EmittedDeferredDecls deferred decls");
7596 
7597   assert(NewBuilder->DeferredVTables.empty() &&
7598          "Newly created module should not have deferred vtables");
7599   NewBuilder->DeferredVTables = std::move(DeferredVTables);
7600 
7601   assert(NewBuilder->MangledDeclNames.empty() &&
7602          "Newly created module should not have mangled decl names");
7603   assert(NewBuilder->Manglings.empty() &&
7604          "Newly created module should not have manglings");
7605   NewBuilder->Manglings = std::move(Manglings);
7606 
7607   NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
7608 
7609   NewBuilder->TBAA = std::move(TBAA);
7610 
7611   NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
7612 }
7613