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