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