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