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