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