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