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