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