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