xref: /llvm-project/clang/lib/CodeGen/CodeGenModule.cpp (revision 1444bb9fc8ff56ae7dc2f9f22fa3518f91dc8d1f)
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 "CGCUDARuntime.h"
16 #include "CGCXXABI.h"
17 #include "CGCall.h"
18 #include "CGDebugInfo.h"
19 #include "CGObjCRuntime.h"
20 #include "CGOpenCLRuntime.h"
21 #include "CGOpenMPRuntime.h"
22 #include "CodeGenFunction.h"
23 #include "CodeGenPGO.h"
24 #include "CoverageMappingGen.h"
25 #include "CodeGenTBAA.h"
26 #include "TargetInfo.h"
27 #include "clang/AST/ASTContext.h"
28 #include "clang/AST/CharUnits.h"
29 #include "clang/AST/DeclCXX.h"
30 #include "clang/AST/DeclObjC.h"
31 #include "clang/AST/DeclTemplate.h"
32 #include "clang/AST/Mangle.h"
33 #include "clang/AST/RecordLayout.h"
34 #include "clang/AST/RecursiveASTVisitor.h"
35 #include "clang/Basic/Builtins.h"
36 #include "clang/Basic/CharInfo.h"
37 #include "clang/Basic/Diagnostic.h"
38 #include "clang/Basic/Module.h"
39 #include "clang/Basic/SourceManager.h"
40 #include "clang/Basic/TargetInfo.h"
41 #include "clang/Basic/Version.h"
42 #include "clang/Frontend/CodeGenOptions.h"
43 #include "clang/Sema/SemaDiagnostic.h"
44 #include "llvm/ADT/APSInt.h"
45 #include "llvm/ADT/Triple.h"
46 #include "llvm/IR/CallSite.h"
47 #include "llvm/IR/CallingConv.h"
48 #include "llvm/IR/DataLayout.h"
49 #include "llvm/IR/Intrinsics.h"
50 #include "llvm/IR/LLVMContext.h"
51 #include "llvm/IR/Module.h"
52 #include "llvm/ProfileData/InstrProfReader.h"
53 #include "llvm/Support/ConvertUTF.h"
54 #include "llvm/Support/ErrorHandling.h"
55 
56 using namespace clang;
57 using namespace CodeGen;
58 
59 static const char AnnotationSection[] = "llvm.metadata";
60 
61 static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
62   switch (CGM.getTarget().getCXXABI().getKind()) {
63   case TargetCXXABI::GenericAArch64:
64   case TargetCXXABI::GenericARM:
65   case TargetCXXABI::iOS:
66   case TargetCXXABI::iOS64:
67   case TargetCXXABI::GenericItanium:
68     return CreateItaniumCXXABI(CGM);
69   case TargetCXXABI::Microsoft:
70     return CreateMicrosoftCXXABI(CGM);
71   }
72 
73   llvm_unreachable("invalid C++ ABI kind");
74 }
75 
76 CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
77                              llvm::Module &M, const llvm::DataLayout &TD,
78                              DiagnosticsEngine &diags,
79                              CoverageSourceInfo *CoverageInfo)
80     : Context(C), LangOpts(C.getLangOpts()), CodeGenOpts(CGO), TheModule(M),
81       Diags(diags), TheDataLayout(TD), Target(C.getTargetInfo()),
82       ABI(createCXXABI(*this)), VMContext(M.getContext()), TBAA(nullptr),
83       TheTargetCodeGenInfo(nullptr), Types(*this), VTables(*this),
84       ObjCRuntime(nullptr), OpenCLRuntime(nullptr), OpenMPRuntime(nullptr),
85       CUDARuntime(nullptr), DebugInfo(nullptr), ARCData(nullptr),
86       NoObjCARCExceptionsMetadata(nullptr), RRData(nullptr), PGOReader(nullptr),
87       CFConstantStringClassRef(nullptr), ConstantStringClassRef(nullptr),
88       NSConstantStringType(nullptr), NSConcreteGlobalBlock(nullptr),
89       NSConcreteStackBlock(nullptr), BlockObjectAssign(nullptr),
90       BlockObjectDispose(nullptr), BlockDescriptorType(nullptr),
91       GenericBlockLiteralType(nullptr), LifetimeStartFn(nullptr),
92       LifetimeEndFn(nullptr), SanitizerMD(new SanitizerMetadata(*this)) {
93 
94   // Initialize the type cache.
95   llvm::LLVMContext &LLVMContext = M.getContext();
96   VoidTy = llvm::Type::getVoidTy(LLVMContext);
97   Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
98   Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
99   Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
100   Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
101   FloatTy = llvm::Type::getFloatTy(LLVMContext);
102   DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
103   PointerWidthInBits = C.getTargetInfo().getPointerWidth(0);
104   PointerAlignInBytes =
105   C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
106   IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
107   IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits);
108   Int8PtrTy = Int8Ty->getPointerTo(0);
109   Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
110 
111   RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
112 
113   if (LangOpts.ObjC1)
114     createObjCRuntime();
115   if (LangOpts.OpenCL)
116     createOpenCLRuntime();
117   if (LangOpts.OpenMP)
118     createOpenMPRuntime();
119   if (LangOpts.CUDA)
120     createCUDARuntime();
121 
122   // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
123   if (LangOpts.Sanitize.Thread ||
124       (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
125     TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(),
126                            getCXXABI().getMangleContext());
127 
128   // If debug info or coverage generation is enabled, create the CGDebugInfo
129   // object.
130   if (CodeGenOpts.getDebugInfo() != CodeGenOptions::NoDebugInfo ||
131       CodeGenOpts.EmitGcovArcs ||
132       CodeGenOpts.EmitGcovNotes)
133     DebugInfo = new CGDebugInfo(*this);
134 
135   Block.GlobalUniqueCount = 0;
136 
137   if (C.getLangOpts().ObjCAutoRefCount)
138     ARCData = new ARCEntrypoints();
139   RRData = new RREntrypoints();
140 
141   if (!CodeGenOpts.InstrProfileInput.empty()) {
142     if (std::error_code EC = llvm::IndexedInstrProfReader::create(
143             CodeGenOpts.InstrProfileInput, PGOReader)) {
144       unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
145                                               "Could not read profile: %0");
146       getDiags().Report(DiagID) << EC.message();
147     }
148   }
149 
150   // If coverage mapping generation is enabled, create the
151   // CoverageMappingModuleGen object.
152   if (CodeGenOpts.CoverageMapping)
153     CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
154 }
155 
156 CodeGenModule::~CodeGenModule() {
157   delete ObjCRuntime;
158   delete OpenCLRuntime;
159   delete OpenMPRuntime;
160   delete CUDARuntime;
161   delete TheTargetCodeGenInfo;
162   delete TBAA;
163   delete DebugInfo;
164   delete ARCData;
165   delete RRData;
166 }
167 
168 void CodeGenModule::createObjCRuntime() {
169   // This is just isGNUFamily(), but we want to force implementors of
170   // new ABIs to decide how best to do this.
171   switch (LangOpts.ObjCRuntime.getKind()) {
172   case ObjCRuntime::GNUstep:
173   case ObjCRuntime::GCC:
174   case ObjCRuntime::ObjFW:
175     ObjCRuntime = CreateGNUObjCRuntime(*this);
176     return;
177 
178   case ObjCRuntime::FragileMacOSX:
179   case ObjCRuntime::MacOSX:
180   case ObjCRuntime::iOS:
181     ObjCRuntime = CreateMacObjCRuntime(*this);
182     return;
183   }
184   llvm_unreachable("bad runtime kind");
185 }
186 
187 void CodeGenModule::createOpenCLRuntime() {
188   OpenCLRuntime = new CGOpenCLRuntime(*this);
189 }
190 
191 void CodeGenModule::createOpenMPRuntime() {
192   OpenMPRuntime = new CGOpenMPRuntime(*this);
193 }
194 
195 void CodeGenModule::createCUDARuntime() {
196   CUDARuntime = CreateNVCUDARuntime(*this);
197 }
198 
199 void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
200   Replacements[Name] = C;
201 }
202 
203 void CodeGenModule::applyReplacements() {
204   for (ReplacementsTy::iterator I = Replacements.begin(),
205                                 E = Replacements.end();
206        I != E; ++I) {
207     StringRef MangledName = I->first();
208     llvm::Constant *Replacement = I->second;
209     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
210     if (!Entry)
211       continue;
212     auto *OldF = cast<llvm::Function>(Entry);
213     auto *NewF = dyn_cast<llvm::Function>(Replacement);
214     if (!NewF) {
215       if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
216         NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
217       } else {
218         auto *CE = cast<llvm::ConstantExpr>(Replacement);
219         assert(CE->getOpcode() == llvm::Instruction::BitCast ||
220                CE->getOpcode() == llvm::Instruction::GetElementPtr);
221         NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
222       }
223     }
224 
225     // Replace old with new, but keep the old order.
226     OldF->replaceAllUsesWith(Replacement);
227     if (NewF) {
228       NewF->removeFromParent();
229       OldF->getParent()->getFunctionList().insertAfter(OldF, NewF);
230     }
231     OldF->eraseFromParent();
232   }
233 }
234 
235 // This is only used in aliases that we created and we know they have a
236 // linear structure.
237 static const llvm::GlobalObject *getAliasedGlobal(const llvm::GlobalAlias &GA) {
238   llvm::SmallPtrSet<const llvm::GlobalAlias*, 4> Visited;
239   const llvm::Constant *C = &GA;
240   for (;;) {
241     C = C->stripPointerCasts();
242     if (auto *GO = dyn_cast<llvm::GlobalObject>(C))
243       return GO;
244     // stripPointerCasts will not walk over weak aliases.
245     auto *GA2 = dyn_cast<llvm::GlobalAlias>(C);
246     if (!GA2)
247       return nullptr;
248     if (!Visited.insert(GA2))
249       return nullptr;
250     C = GA2->getAliasee();
251   }
252 }
253 
254 void CodeGenModule::checkAliases() {
255   // Check if the constructed aliases are well formed. It is really unfortunate
256   // that we have to do this in CodeGen, but we only construct mangled names
257   // and aliases during codegen.
258   bool Error = false;
259   DiagnosticsEngine &Diags = getDiags();
260   for (std::vector<GlobalDecl>::iterator I = Aliases.begin(),
261          E = Aliases.end(); I != E; ++I) {
262     const GlobalDecl &GD = *I;
263     const auto *D = cast<ValueDecl>(GD.getDecl());
264     const AliasAttr *AA = D->getAttr<AliasAttr>();
265     StringRef MangledName = getMangledName(GD);
266     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
267     auto *Alias = cast<llvm::GlobalAlias>(Entry);
268     const llvm::GlobalValue *GV = getAliasedGlobal(*Alias);
269     if (!GV) {
270       Error = true;
271       Diags.Report(AA->getLocation(), diag::err_cyclic_alias);
272     } else if (GV->isDeclaration()) {
273       Error = true;
274       Diags.Report(AA->getLocation(), diag::err_alias_to_undefined);
275     }
276 
277     llvm::Constant *Aliasee = Alias->getAliasee();
278     llvm::GlobalValue *AliaseeGV;
279     if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
280       AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
281     else
282       AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
283 
284     if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
285       StringRef AliasSection = SA->getName();
286       if (AliasSection != AliaseeGV->getSection())
287         Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
288             << AliasSection;
289     }
290 
291     // We have to handle alias to weak aliases in here. LLVM itself disallows
292     // this since the object semantics would not match the IL one. For
293     // compatibility with gcc we implement it by just pointing the alias
294     // to its aliasee's aliasee. We also warn, since the user is probably
295     // expecting the link to be weak.
296     if (auto GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
297       if (GA->mayBeOverridden()) {
298         Diags.Report(AA->getLocation(), diag::warn_alias_to_weak_alias)
299             << GV->getName() << GA->getName();
300         Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
301             GA->getAliasee(), Alias->getType());
302         Alias->setAliasee(Aliasee);
303       }
304     }
305   }
306   if (!Error)
307     return;
308 
309   for (std::vector<GlobalDecl>::iterator I = Aliases.begin(),
310          E = Aliases.end(); I != E; ++I) {
311     const GlobalDecl &GD = *I;
312     StringRef MangledName = getMangledName(GD);
313     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
314     auto *Alias = cast<llvm::GlobalAlias>(Entry);
315     Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
316     Alias->eraseFromParent();
317   }
318 }
319 
320 void CodeGenModule::clear() {
321   DeferredDeclsToEmit.clear();
322 }
323 
324 void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags,
325                                        StringRef MainFile) {
326   if (!hasDiagnostics())
327     return;
328   if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
329     if (MainFile.empty())
330       MainFile = "<stdin>";
331     Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
332   } else
333     Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Missing
334                                                       << Mismatched;
335 }
336 
337 void CodeGenModule::Release() {
338   EmitDeferred();
339   applyReplacements();
340   checkAliases();
341   EmitCXXGlobalInitFunc();
342   EmitCXXGlobalDtorFunc();
343   EmitCXXThreadLocalInitFunc();
344   if (ObjCRuntime)
345     if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
346       AddGlobalCtor(ObjCInitFunction);
347   if (getCodeGenOpts().ProfileInstrGenerate)
348     if (llvm::Function *PGOInit = CodeGenPGO::emitInitialization(*this))
349       AddGlobalCtor(PGOInit, 0);
350   if (PGOReader && PGOStats.hasDiagnostics())
351     PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
352   EmitCtorList(GlobalCtors, "llvm.global_ctors");
353   EmitCtorList(GlobalDtors, "llvm.global_dtors");
354   EmitGlobalAnnotations();
355   EmitStaticExternCAliases();
356   EmitDeferredUnusedCoverageMappings();
357   if (CoverageMapping)
358     CoverageMapping->emit();
359   emitLLVMUsed();
360 
361   if (CodeGenOpts.Autolink &&
362       (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
363     EmitModuleLinkOptions();
364   }
365   if (CodeGenOpts.DwarfVersion)
366     // We actually want the latest version when there are conflicts.
367     // We can change from Warning to Latest if such mode is supported.
368     getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version",
369                               CodeGenOpts.DwarfVersion);
370   if (DebugInfo)
371     // We support a single version in the linked module. The LLVM
372     // parser will drop debug info with a different version number
373     // (and warn about it, too).
374     getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
375                               llvm::DEBUG_METADATA_VERSION);
376 
377   // We need to record the widths of enums and wchar_t, so that we can generate
378   // the correct build attributes in the ARM backend.
379   llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
380   if (   Arch == llvm::Triple::arm
381       || Arch == llvm::Triple::armeb
382       || Arch == llvm::Triple::thumb
383       || Arch == llvm::Triple::thumbeb) {
384     // Width of wchar_t in bytes
385     uint64_t WCharWidth =
386         Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
387     getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
388 
389     // The minimum width of an enum in bytes
390     uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
391     getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
392   }
393 
394   SimplifyPersonality();
395 
396   if (getCodeGenOpts().EmitDeclMetadata)
397     EmitDeclMetadata();
398 
399   if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
400     EmitCoverageFile();
401 
402   if (DebugInfo)
403     DebugInfo->finalize();
404 
405   EmitVersionIdentMetadata();
406 
407   EmitTargetMetadata();
408 }
409 
410 void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
411   // Make sure that this type is translated.
412   Types.UpdateCompletedType(TD);
413 }
414 
415 llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
416   if (!TBAA)
417     return nullptr;
418   return TBAA->getTBAAInfo(QTy);
419 }
420 
421 llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() {
422   if (!TBAA)
423     return nullptr;
424   return TBAA->getTBAAInfoForVTablePtr();
425 }
426 
427 llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
428   if (!TBAA)
429     return nullptr;
430   return TBAA->getTBAAStructInfo(QTy);
431 }
432 
433 llvm::MDNode *CodeGenModule::getTBAAStructTypeInfo(QualType QTy) {
434   if (!TBAA)
435     return nullptr;
436   return TBAA->getTBAAStructTypeInfo(QTy);
437 }
438 
439 llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(QualType BaseTy,
440                                                   llvm::MDNode *AccessN,
441                                                   uint64_t O) {
442   if (!TBAA)
443     return nullptr;
444   return TBAA->getTBAAStructTagInfo(BaseTy, AccessN, O);
445 }
446 
447 /// Decorate the instruction with a TBAA tag. For both scalar TBAA
448 /// and struct-path aware TBAA, the tag has the same format:
449 /// base type, access type and offset.
450 /// When ConvertTypeToTag is true, we create a tag based on the scalar type.
451 void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
452                                         llvm::MDNode *TBAAInfo,
453                                         bool ConvertTypeToTag) {
454   if (ConvertTypeToTag && TBAA)
455     Inst->setMetadata(llvm::LLVMContext::MD_tbaa,
456                       TBAA->getTBAAScalarTagInfo(TBAAInfo));
457   else
458     Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
459 }
460 
461 void CodeGenModule::Error(SourceLocation loc, StringRef message) {
462   unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
463   getDiags().Report(Context.getFullLoc(loc), diagID) << message;
464 }
465 
466 /// ErrorUnsupported - Print out an error that codegen doesn't support the
467 /// specified stmt yet.
468 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
469   unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
470                                                "cannot compile this %0 yet");
471   std::string Msg = Type;
472   getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
473     << Msg << S->getSourceRange();
474 }
475 
476 /// ErrorUnsupported - Print out an error that codegen doesn't support the
477 /// specified decl yet.
478 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
479   unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
480                                                "cannot compile this %0 yet");
481   std::string Msg = Type;
482   getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
483 }
484 
485 llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
486   return llvm::ConstantInt::get(SizeTy, size.getQuantity());
487 }
488 
489 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
490                                         const NamedDecl *D) const {
491   // Internal definitions always have default visibility.
492   if (GV->hasLocalLinkage()) {
493     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
494     return;
495   }
496 
497   // Set visibility for definitions.
498   LinkageInfo LV = D->getLinkageAndVisibility();
499   if (LV.isVisibilityExplicit() || !GV->hasAvailableExternallyLinkage())
500     GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
501 }
502 
503 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
504   return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
505       .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
506       .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
507       .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
508       .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
509 }
510 
511 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(
512     CodeGenOptions::TLSModel M) {
513   switch (M) {
514   case CodeGenOptions::GeneralDynamicTLSModel:
515     return llvm::GlobalVariable::GeneralDynamicTLSModel;
516   case CodeGenOptions::LocalDynamicTLSModel:
517     return llvm::GlobalVariable::LocalDynamicTLSModel;
518   case CodeGenOptions::InitialExecTLSModel:
519     return llvm::GlobalVariable::InitialExecTLSModel;
520   case CodeGenOptions::LocalExecTLSModel:
521     return llvm::GlobalVariable::LocalExecTLSModel;
522   }
523   llvm_unreachable("Invalid TLS model!");
524 }
525 
526 void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
527   assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
528 
529   llvm::GlobalValue::ThreadLocalMode TLM;
530   TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel());
531 
532   // Override the TLS model if it is explicitly specified.
533   if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
534     TLM = GetLLVMTLSModel(Attr->getModel());
535   }
536 
537   GV->setThreadLocalMode(TLM);
538 }
539 
540 StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
541   StringRef &FoundStr = MangledDeclNames[GD.getCanonicalDecl()];
542   if (!FoundStr.empty())
543     return FoundStr;
544 
545   const auto *ND = cast<NamedDecl>(GD.getDecl());
546   SmallString<256> Buffer;
547   StringRef Str;
548   if (getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
549     llvm::raw_svector_ostream Out(Buffer);
550     if (const auto *D = dyn_cast<CXXConstructorDecl>(ND))
551       getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out);
552     else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND))
553       getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out);
554     else
555       getCXXABI().getMangleContext().mangleName(ND, Out);
556     Str = Out.str();
557   } else {
558     IdentifierInfo *II = ND->getIdentifier();
559     assert(II && "Attempt to mangle unnamed decl.");
560     Str = II->getName();
561   }
562 
563   // Keep the first result in the case of a mangling collision.
564   auto Result = Manglings.insert(std::make_pair(Str, GD));
565   return FoundStr = Result.first->first();
566 }
567 
568 StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD,
569                                              const BlockDecl *BD) {
570   MangleContext &MangleCtx = getCXXABI().getMangleContext();
571   const Decl *D = GD.getDecl();
572 
573   SmallString<256> Buffer;
574   llvm::raw_svector_ostream Out(Buffer);
575   if (!D)
576     MangleCtx.mangleGlobalBlock(BD,
577       dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
578   else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
579     MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
580   else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
581     MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
582   else
583     MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
584 
585   auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
586   return Result.first->first();
587 }
588 
589 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
590   return getModule().getNamedValue(Name);
591 }
592 
593 /// AddGlobalCtor - Add a function to the list that will be called before
594 /// main() runs.
595 void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
596                                   llvm::Constant *AssociatedData) {
597   // FIXME: Type coercion of void()* types.
598   GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData));
599 }
600 
601 /// AddGlobalDtor - Add a function to the list that will be called
602 /// when the module is unloaded.
603 void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) {
604   // FIXME: Type coercion of void()* types.
605   GlobalDtors.push_back(Structor(Priority, Dtor, nullptr));
606 }
607 
608 void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
609   // Ctor function type is void()*.
610   llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
611   llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
612 
613   // Get the type of a ctor entry, { i32, void ()*, i8* }.
614   llvm::StructType *CtorStructTy = llvm::StructType::get(
615       Int32Ty, llvm::PointerType::getUnqual(CtorFTy), VoidPtrTy, NULL);
616 
617   // Construct the constructor and destructor arrays.
618   SmallVector<llvm::Constant*, 8> Ctors;
619   for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) {
620     llvm::Constant *S[] = {
621       llvm::ConstantInt::get(Int32Ty, I->Priority, false),
622       llvm::ConstantExpr::getBitCast(I->Initializer, CtorPFTy),
623       (I->AssociatedData
624            ? llvm::ConstantExpr::getBitCast(I->AssociatedData, VoidPtrTy)
625            : llvm::Constant::getNullValue(VoidPtrTy))
626     };
627     Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
628   }
629 
630   if (!Ctors.empty()) {
631     llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
632     new llvm::GlobalVariable(TheModule, AT, false,
633                              llvm::GlobalValue::AppendingLinkage,
634                              llvm::ConstantArray::get(AT, Ctors),
635                              GlobalName);
636   }
637 }
638 
639 llvm::GlobalValue::LinkageTypes
640 CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
641   const auto *D = cast<FunctionDecl>(GD.getDecl());
642 
643   GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
644 
645   if (isa<CXXDestructorDecl>(D) &&
646       getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
647                                          GD.getDtorType())) {
648     // Destructor variants in the Microsoft C++ ABI are always internal or
649     // linkonce_odr thunks emitted on an as-needed basis.
650     return Linkage == GVA_Internal ? llvm::GlobalValue::InternalLinkage
651                                    : llvm::GlobalValue::LinkOnceODRLinkage;
652   }
653 
654   return getLLVMLinkageForDeclarator(D, Linkage, /*isConstantVariable=*/false);
655 }
656 
657 void CodeGenModule::setFunctionDefinitionAttributes(const FunctionDecl *D,
658                                                     llvm::Function *F) {
659   setNonAliasAttributes(D, F);
660 }
661 
662 void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
663                                               const CGFunctionInfo &Info,
664                                               llvm::Function *F) {
665   unsigned CallingConv;
666   AttributeListType AttributeList;
667   ConstructAttributeList(Info, D, AttributeList, CallingConv, false);
668   F->setAttributes(llvm::AttributeSet::get(getLLVMContext(), AttributeList));
669   F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
670 }
671 
672 /// Determines whether the language options require us to model
673 /// unwind exceptions.  We treat -fexceptions as mandating this
674 /// except under the fragile ObjC ABI with only ObjC exceptions
675 /// enabled.  This means, for example, that C with -fexceptions
676 /// enables this.
677 static bool hasUnwindExceptions(const LangOptions &LangOpts) {
678   // If exceptions are completely disabled, obviously this is false.
679   if (!LangOpts.Exceptions) return false;
680 
681   // If C++ exceptions are enabled, this is true.
682   if (LangOpts.CXXExceptions) return true;
683 
684   // If ObjC exceptions are enabled, this depends on the ABI.
685   if (LangOpts.ObjCExceptions) {
686     return LangOpts.ObjCRuntime.hasUnwindExceptions();
687   }
688 
689   return true;
690 }
691 
692 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
693                                                            llvm::Function *F) {
694   llvm::AttrBuilder B;
695 
696   if (CodeGenOpts.UnwindTables)
697     B.addAttribute(llvm::Attribute::UWTable);
698 
699   if (!hasUnwindExceptions(LangOpts))
700     B.addAttribute(llvm::Attribute::NoUnwind);
701 
702   if (D->hasAttr<NakedAttr>()) {
703     // Naked implies noinline: we should not be inlining such functions.
704     B.addAttribute(llvm::Attribute::Naked);
705     B.addAttribute(llvm::Attribute::NoInline);
706   } else if (D->hasAttr<OptimizeNoneAttr>()) {
707     // OptimizeNone implies noinline; we should not be inlining such functions.
708     B.addAttribute(llvm::Attribute::OptimizeNone);
709     B.addAttribute(llvm::Attribute::NoInline);
710   } else if (D->hasAttr<NoDuplicateAttr>()) {
711     B.addAttribute(llvm::Attribute::NoDuplicate);
712   } else if (D->hasAttr<NoInlineAttr>()) {
713     B.addAttribute(llvm::Attribute::NoInline);
714   } else if (D->hasAttr<AlwaysInlineAttr>() &&
715              !F->getAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex,
716                                               llvm::Attribute::NoInline)) {
717     // (noinline wins over always_inline, and we can't specify both in IR)
718     B.addAttribute(llvm::Attribute::AlwaysInline);
719   }
720 
721   if (D->hasAttr<ColdAttr>()) {
722     B.addAttribute(llvm::Attribute::OptimizeForSize);
723     B.addAttribute(llvm::Attribute::Cold);
724   }
725 
726   if (D->hasAttr<MinSizeAttr>())
727     B.addAttribute(llvm::Attribute::MinSize);
728 
729   if (D->hasAttr<OptimizeNoneAttr>()) {
730     // OptimizeNone wins over OptimizeForSize and MinSize.
731     B.removeAttribute(llvm::Attribute::OptimizeForSize);
732     B.removeAttribute(llvm::Attribute::MinSize);
733   }
734 
735   if (LangOpts.getStackProtector() == LangOptions::SSPOn)
736     B.addAttribute(llvm::Attribute::StackProtect);
737   else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
738     B.addAttribute(llvm::Attribute::StackProtectStrong);
739   else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
740     B.addAttribute(llvm::Attribute::StackProtectReq);
741 
742   // Add sanitizer attributes if function is not blacklisted.
743   if (!isInSanitizerBlacklist(F, D->getLocation())) {
744     // When AddressSanitizer is enabled, set SanitizeAddress attribute
745     // unless __attribute__((no_sanitize_address)) is used.
746     if (LangOpts.Sanitize.Address && !D->hasAttr<NoSanitizeAddressAttr>())
747       B.addAttribute(llvm::Attribute::SanitizeAddress);
748     // Same for ThreadSanitizer and __attribute__((no_sanitize_thread))
749     if (LangOpts.Sanitize.Thread && !D->hasAttr<NoSanitizeThreadAttr>())
750       B.addAttribute(llvm::Attribute::SanitizeThread);
751     // Same for MemorySanitizer and __attribute__((no_sanitize_memory))
752     if (LangOpts.Sanitize.Memory && !D->hasAttr<NoSanitizeMemoryAttr>())
753       B.addAttribute(llvm::Attribute::SanitizeMemory);
754   }
755 
756   F->addAttributes(llvm::AttributeSet::FunctionIndex,
757                    llvm::AttributeSet::get(
758                        F->getContext(), llvm::AttributeSet::FunctionIndex, B));
759 
760   if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
761     F->setUnnamedAddr(true);
762   else if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
763     if (MD->isVirtual())
764       F->setUnnamedAddr(true);
765 
766   unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
767   if (alignment)
768     F->setAlignment(alignment);
769 
770   // C++ ABI requires 2-byte alignment for member functions.
771   if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
772     F->setAlignment(2);
773 }
774 
775 void CodeGenModule::SetCommonAttributes(const Decl *D,
776                                         llvm::GlobalValue *GV) {
777   if (const auto *ND = dyn_cast<NamedDecl>(D))
778     setGlobalVisibility(GV, ND);
779   else
780     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
781 
782   if (D->hasAttr<UsedAttr>())
783     addUsedGlobal(GV);
784 }
785 
786 void CodeGenModule::setAliasAttributes(const Decl *D,
787                                        llvm::GlobalValue *GV) {
788   SetCommonAttributes(D, GV);
789 
790   // Process the dllexport attribute based on whether the original definition
791   // (not necessarily the aliasee) was exported.
792   if (D->hasAttr<DLLExportAttr>())
793     GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
794 }
795 
796 void CodeGenModule::setNonAliasAttributes(const Decl *D,
797                                           llvm::GlobalObject *GO) {
798   SetCommonAttributes(D, GO);
799 
800   if (const SectionAttr *SA = D->getAttr<SectionAttr>())
801     GO->setSection(SA->getName());
802 
803   getTargetCodeGenInfo().SetTargetAttributes(D, GO, *this);
804 }
805 
806 void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
807                                                   llvm::Function *F,
808                                                   const CGFunctionInfo &FI) {
809   SetLLVMFunctionAttributes(D, FI, F);
810   SetLLVMFunctionAttributesForDefinition(D, F);
811 
812   F->setLinkage(llvm::Function::InternalLinkage);
813 
814   setNonAliasAttributes(D, F);
815 }
816 
817 static void setLinkageAndVisibilityForGV(llvm::GlobalValue *GV,
818                                          const NamedDecl *ND) {
819   // Set linkage and visibility in case we never see a definition.
820   LinkageInfo LV = ND->getLinkageAndVisibility();
821   if (LV.getLinkage() != ExternalLinkage) {
822     // Don't set internal linkage on declarations.
823   } else {
824     if (ND->hasAttr<DLLImportAttr>()) {
825       GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
826       GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
827     } else if (ND->hasAttr<DLLExportAttr>()) {
828       GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
829       GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
830     } else if (ND->hasAttr<WeakAttr>() || ND->isWeakImported()) {
831       // "extern_weak" is overloaded in LLVM; we probably should have
832       // separate linkage types for this.
833       GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
834     }
835 
836     // Set visibility on a declaration only if it's explicit.
837     if (LV.isVisibilityExplicit())
838       GV->setVisibility(CodeGenModule::GetLLVMVisibility(LV.getVisibility()));
839   }
840 }
841 
842 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD,
843                                           llvm::Function *F,
844                                           bool IsIncompleteFunction) {
845   if (unsigned IID = F->getIntrinsicID()) {
846     // If this is an intrinsic function, set the function's attributes
847     // to the intrinsic's attributes.
848     F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(),
849                                                     (llvm::Intrinsic::ID)IID));
850     return;
851   }
852 
853   const auto *FD = cast<FunctionDecl>(GD.getDecl());
854 
855   if (!IsIncompleteFunction)
856     SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F);
857 
858   // Add the Returned attribute for "this", except for iOS 5 and earlier
859   // where substantial code, including the libstdc++ dylib, was compiled with
860   // GCC and does not actually return "this".
861   if (getCXXABI().HasThisReturn(GD) &&
862       !(getTarget().getTriple().isiOS() &&
863         getTarget().getTriple().isOSVersionLT(6))) {
864     assert(!F->arg_empty() &&
865            F->arg_begin()->getType()
866              ->canLosslesslyBitCastTo(F->getReturnType()) &&
867            "unexpected this return");
868     F->addAttribute(1, llvm::Attribute::Returned);
869   }
870 
871   // Only a few attributes are set on declarations; these may later be
872   // overridden by a definition.
873 
874   setLinkageAndVisibilityForGV(F, FD);
875 
876   if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(FD)) {
877     if (getCXXABI().useThunkForDtorVariant(Dtor, GD.getDtorType())) {
878       // Don't dllexport/import destructor thunks.
879       F->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
880     }
881   }
882 
883   if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
884     F->setSection(SA->getName());
885 
886   // A replaceable global allocation function does not act like a builtin by
887   // default, only if it is invoked by a new-expression or delete-expression.
888   if (FD->isReplaceableGlobalAllocationFunction())
889     F->addAttribute(llvm::AttributeSet::FunctionIndex,
890                     llvm::Attribute::NoBuiltin);
891 }
892 
893 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
894   assert(!GV->isDeclaration() &&
895          "Only globals with definition can force usage.");
896   LLVMUsed.push_back(GV);
897 }
898 
899 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
900   assert(!GV->isDeclaration() &&
901          "Only globals with definition can force usage.");
902   LLVMCompilerUsed.push_back(GV);
903 }
904 
905 static void emitUsed(CodeGenModule &CGM, StringRef Name,
906                      std::vector<llvm::WeakVH> &List) {
907   // Don't create llvm.used if there is no need.
908   if (List.empty())
909     return;
910 
911   // Convert List to what ConstantArray needs.
912   SmallVector<llvm::Constant*, 8> UsedArray;
913   UsedArray.resize(List.size());
914   for (unsigned i = 0, e = List.size(); i != e; ++i) {
915     UsedArray[i] =
916      llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*List[i]),
917                                     CGM.Int8PtrTy);
918   }
919 
920   if (UsedArray.empty())
921     return;
922   llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
923 
924   auto *GV = new llvm::GlobalVariable(
925       CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
926       llvm::ConstantArray::get(ATy, UsedArray), Name);
927 
928   GV->setSection("llvm.metadata");
929 }
930 
931 void CodeGenModule::emitLLVMUsed() {
932   emitUsed(*this, "llvm.used", LLVMUsed);
933   emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
934 }
935 
936 void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
937   llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
938   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
939 }
940 
941 void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
942   llvm::SmallString<32> Opt;
943   getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
944   llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
945   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
946 }
947 
948 void CodeGenModule::AddDependentLib(StringRef Lib) {
949   llvm::SmallString<24> Opt;
950   getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
951   llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
952   LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
953 }
954 
955 /// \brief Add link options implied by the given module, including modules
956 /// it depends on, using a postorder walk.
957 static void addLinkOptionsPostorder(CodeGenModule &CGM,
958                                     Module *Mod,
959                                     SmallVectorImpl<llvm::Value *> &Metadata,
960                                     llvm::SmallPtrSet<Module *, 16> &Visited) {
961   // Import this module's parent.
962   if (Mod->Parent && Visited.insert(Mod->Parent)) {
963     addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
964   }
965 
966   // Import this module's dependencies.
967   for (unsigned I = Mod->Imports.size(); I > 0; --I) {
968     if (Visited.insert(Mod->Imports[I-1]))
969       addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited);
970   }
971 
972   // Add linker options to link against the libraries/frameworks
973   // described by this module.
974   llvm::LLVMContext &Context = CGM.getLLVMContext();
975   for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
976     // Link against a framework.  Frameworks are currently Darwin only, so we
977     // don't to ask TargetCodeGenInfo for the spelling of the linker option.
978     if (Mod->LinkLibraries[I-1].IsFramework) {
979       llvm::Value *Args[2] = {
980         llvm::MDString::get(Context, "-framework"),
981         llvm::MDString::get(Context, Mod->LinkLibraries[I-1].Library)
982       };
983 
984       Metadata.push_back(llvm::MDNode::get(Context, Args));
985       continue;
986     }
987 
988     // Link against a library.
989     llvm::SmallString<24> Opt;
990     CGM.getTargetCodeGenInfo().getDependentLibraryOption(
991       Mod->LinkLibraries[I-1].Library, Opt);
992     llvm::Value *OptString = llvm::MDString::get(Context, Opt);
993     Metadata.push_back(llvm::MDNode::get(Context, OptString));
994   }
995 }
996 
997 void CodeGenModule::EmitModuleLinkOptions() {
998   // Collect the set of all of the modules we want to visit to emit link
999   // options, which is essentially the imported modules and all of their
1000   // non-explicit child modules.
1001   llvm::SetVector<clang::Module *> LinkModules;
1002   llvm::SmallPtrSet<clang::Module *, 16> Visited;
1003   SmallVector<clang::Module *, 16> Stack;
1004 
1005   // Seed the stack with imported modules.
1006   for (llvm::SetVector<clang::Module *>::iterator M = ImportedModules.begin(),
1007                                                MEnd = ImportedModules.end();
1008        M != MEnd; ++M) {
1009     if (Visited.insert(*M))
1010       Stack.push_back(*M);
1011   }
1012 
1013   // Find all of the modules to import, making a little effort to prune
1014   // non-leaf modules.
1015   while (!Stack.empty()) {
1016     clang::Module *Mod = Stack.pop_back_val();
1017 
1018     bool AnyChildren = false;
1019 
1020     // Visit the submodules of this module.
1021     for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
1022                                         SubEnd = Mod->submodule_end();
1023          Sub != SubEnd; ++Sub) {
1024       // Skip explicit children; they need to be explicitly imported to be
1025       // linked against.
1026       if ((*Sub)->IsExplicit)
1027         continue;
1028 
1029       if (Visited.insert(*Sub)) {
1030         Stack.push_back(*Sub);
1031         AnyChildren = true;
1032       }
1033     }
1034 
1035     // We didn't find any children, so add this module to the list of
1036     // modules to link against.
1037     if (!AnyChildren) {
1038       LinkModules.insert(Mod);
1039     }
1040   }
1041 
1042   // Add link options for all of the imported modules in reverse topological
1043   // order.  We don't do anything to try to order import link flags with respect
1044   // to linker options inserted by things like #pragma comment().
1045   SmallVector<llvm::Value *, 16> MetadataArgs;
1046   Visited.clear();
1047   for (llvm::SetVector<clang::Module *>::iterator M = LinkModules.begin(),
1048                                                MEnd = LinkModules.end();
1049        M != MEnd; ++M) {
1050     if (Visited.insert(*M))
1051       addLinkOptionsPostorder(*this, *M, MetadataArgs, Visited);
1052   }
1053   std::reverse(MetadataArgs.begin(), MetadataArgs.end());
1054   LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
1055 
1056   // Add the linker options metadata flag.
1057   getModule().addModuleFlag(llvm::Module::AppendUnique, "Linker Options",
1058                             llvm::MDNode::get(getLLVMContext(),
1059                                               LinkerOptionsMetadata));
1060 }
1061 
1062 void CodeGenModule::EmitDeferred() {
1063   // Emit code for any potentially referenced deferred decls.  Since a
1064   // previously unused static decl may become used during the generation of code
1065   // for a static function, iterate until no changes are made.
1066 
1067   while (true) {
1068     if (!DeferredVTables.empty()) {
1069       EmitDeferredVTables();
1070 
1071       // Emitting a v-table doesn't directly cause more v-tables to
1072       // become deferred, although it can cause functions to be
1073       // emitted that then need those v-tables.
1074       assert(DeferredVTables.empty());
1075     }
1076 
1077     // Stop if we're out of both deferred v-tables and deferred declarations.
1078     if (DeferredDeclsToEmit.empty()) break;
1079 
1080     DeferredGlobal &G = DeferredDeclsToEmit.back();
1081     GlobalDecl D = G.GD;
1082     llvm::GlobalValue *GV = G.GV;
1083     DeferredDeclsToEmit.pop_back();
1084 
1085     assert(GV == GetGlobalValue(getMangledName(D)));
1086     // Check to see if we've already emitted this.  This is necessary
1087     // for a couple of reasons: first, decls can end up in the
1088     // deferred-decls queue multiple times, and second, decls can end
1089     // up with definitions in unusual ways (e.g. by an extern inline
1090     // function acquiring a strong function redefinition).  Just
1091     // ignore these cases.
1092     if(!GV->isDeclaration())
1093       continue;
1094 
1095     // Otherwise, emit the definition and move on to the next one.
1096     EmitGlobalDefinition(D, GV);
1097   }
1098 }
1099 
1100 void CodeGenModule::EmitGlobalAnnotations() {
1101   if (Annotations.empty())
1102     return;
1103 
1104   // Create a new global variable for the ConstantStruct in the Module.
1105   llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
1106     Annotations[0]->getType(), Annotations.size()), Annotations);
1107   auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
1108                                       llvm::GlobalValue::AppendingLinkage,
1109                                       Array, "llvm.global.annotations");
1110   gv->setSection(AnnotationSection);
1111 }
1112 
1113 llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
1114   llvm::Constant *&AStr = AnnotationStrings[Str];
1115   if (AStr)
1116     return AStr;
1117 
1118   // Not found yet, create a new global.
1119   llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
1120   auto *gv =
1121       new llvm::GlobalVariable(getModule(), s->getType(), true,
1122                                llvm::GlobalValue::PrivateLinkage, s, ".str");
1123   gv->setSection(AnnotationSection);
1124   gv->setUnnamedAddr(true);
1125   AStr = gv;
1126   return gv;
1127 }
1128 
1129 llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
1130   SourceManager &SM = getContext().getSourceManager();
1131   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
1132   if (PLoc.isValid())
1133     return EmitAnnotationString(PLoc.getFilename());
1134   return EmitAnnotationString(SM.getBufferName(Loc));
1135 }
1136 
1137 llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
1138   SourceManager &SM = getContext().getSourceManager();
1139   PresumedLoc PLoc = SM.getPresumedLoc(L);
1140   unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
1141     SM.getExpansionLineNumber(L);
1142   return llvm::ConstantInt::get(Int32Ty, LineNo);
1143 }
1144 
1145 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
1146                                                 const AnnotateAttr *AA,
1147                                                 SourceLocation L) {
1148   // Get the globals for file name, annotation, and the line number.
1149   llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
1150                  *UnitGV = EmitAnnotationUnit(L),
1151                  *LineNoCst = EmitAnnotationLineNo(L);
1152 
1153   // Create the ConstantStruct for the global annotation.
1154   llvm::Constant *Fields[4] = {
1155     llvm::ConstantExpr::getBitCast(GV, Int8PtrTy),
1156     llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
1157     llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
1158     LineNoCst
1159   };
1160   return llvm::ConstantStruct::getAnon(Fields);
1161 }
1162 
1163 void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
1164                                          llvm::GlobalValue *GV) {
1165   assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
1166   // Get the struct elements for these annotations.
1167   for (const auto *I : D->specific_attrs<AnnotateAttr>())
1168     Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
1169 }
1170 
1171 bool CodeGenModule::isInSanitizerBlacklist(llvm::Function *Fn,
1172                                            SourceLocation Loc) const {
1173   const auto &SanitizerBL = getContext().getSanitizerBlacklist();
1174   // Blacklist by function name.
1175   if (SanitizerBL.isBlacklistedFunction(Fn->getName()))
1176     return true;
1177   // Blacklist by location.
1178   if (!Loc.isInvalid())
1179     return SanitizerBL.isBlacklistedLocation(Loc);
1180   // If location is unknown, this may be a compiler-generated function. Assume
1181   // it's located in the main file.
1182   auto &SM = Context.getSourceManager();
1183   if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1184     return SanitizerBL.isBlacklistedFile(MainFile->getName());
1185   }
1186   return false;
1187 }
1188 
1189 bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) {
1190   // Never defer when EmitAllDecls is specified.
1191   if (LangOpts.EmitAllDecls)
1192     return false;
1193 
1194   return !getContext().DeclMustBeEmitted(Global);
1195 }
1196 
1197 llvm::Constant *CodeGenModule::GetAddrOfUuidDescriptor(
1198     const CXXUuidofExpr* E) {
1199   // Sema has verified that IIDSource has a __declspec(uuid()), and that its
1200   // well-formed.
1201   StringRef Uuid = E->getUuidAsStringRef(Context);
1202   std::string Name = "_GUID_" + Uuid.lower();
1203   std::replace(Name.begin(), Name.end(), '-', '_');
1204 
1205   // Look for an existing global.
1206   if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
1207     return GV;
1208 
1209   llvm::Constant *Init = EmitUuidofInitializer(Uuid);
1210   assert(Init && "failed to initialize as constant");
1211 
1212   auto *GV = new llvm::GlobalVariable(
1213       getModule(), Init->getType(),
1214       /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
1215   return GV;
1216 }
1217 
1218 llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
1219   const AliasAttr *AA = VD->getAttr<AliasAttr>();
1220   assert(AA && "No alias?");
1221 
1222   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
1223 
1224   // See if there is already something with the target's name in the module.
1225   llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
1226   if (Entry) {
1227     unsigned AS = getContext().getTargetAddressSpace(VD->getType());
1228     return llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
1229   }
1230 
1231   llvm::Constant *Aliasee;
1232   if (isa<llvm::FunctionType>(DeclTy))
1233     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
1234                                       GlobalDecl(cast<FunctionDecl>(VD)),
1235                                       /*ForVTable=*/false);
1236   else
1237     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
1238                                     llvm::PointerType::getUnqual(DeclTy),
1239                                     nullptr);
1240 
1241   auto *F = cast<llvm::GlobalValue>(Aliasee);
1242   F->setLinkage(llvm::Function::ExternalWeakLinkage);
1243   WeakRefReferences.insert(F);
1244 
1245   return Aliasee;
1246 }
1247 
1248 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
1249   const auto *Global = cast<ValueDecl>(GD.getDecl());
1250 
1251   // Weak references don't produce any output by themselves.
1252   if (Global->hasAttr<WeakRefAttr>())
1253     return;
1254 
1255   // If this is an alias definition (which otherwise looks like a declaration)
1256   // emit it now.
1257   if (Global->hasAttr<AliasAttr>())
1258     return EmitAliasDefinition(GD);
1259 
1260   // If this is CUDA, be selective about which declarations we emit.
1261   if (LangOpts.CUDA) {
1262     if (CodeGenOpts.CUDAIsDevice) {
1263       if (!Global->hasAttr<CUDADeviceAttr>() &&
1264           !Global->hasAttr<CUDAGlobalAttr>() &&
1265           !Global->hasAttr<CUDAConstantAttr>() &&
1266           !Global->hasAttr<CUDASharedAttr>())
1267         return;
1268     } else {
1269       if (!Global->hasAttr<CUDAHostAttr>() && (
1270             Global->hasAttr<CUDADeviceAttr>() ||
1271             Global->hasAttr<CUDAConstantAttr>() ||
1272             Global->hasAttr<CUDASharedAttr>()))
1273         return;
1274     }
1275   }
1276 
1277   // Ignore declarations, they will be emitted on their first use.
1278   if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
1279     // Forward declarations are emitted lazily on first use.
1280     if (!FD->doesThisDeclarationHaveABody()) {
1281       if (!FD->doesDeclarationForceExternallyVisibleDefinition())
1282         return;
1283 
1284       StringRef MangledName = getMangledName(GD);
1285 
1286       // Compute the function info and LLVM type.
1287       const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
1288       llvm::Type *Ty = getTypes().GetFunctionType(FI);
1289 
1290       GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
1291                               /*DontDefer=*/false);
1292       return;
1293     }
1294   } else {
1295     const auto *VD = cast<VarDecl>(Global);
1296     assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
1297 
1298     if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
1299         !Context.isMSStaticDataMemberInlineDefinition(VD))
1300       return;
1301   }
1302 
1303   // Defer code generation when possible if this is a static definition, inline
1304   // function etc.  These we only want to emit if they are used.
1305   if (!MayDeferGeneration(Global)) {
1306     // Emit the definition if it can't be deferred.
1307     EmitGlobalDefinition(GD);
1308     return;
1309   }
1310 
1311   // If we're deferring emission of a C++ variable with an
1312   // initializer, remember the order in which it appeared in the file.
1313   if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
1314       cast<VarDecl>(Global)->hasInit()) {
1315     DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
1316     CXXGlobalInits.push_back(nullptr);
1317   }
1318 
1319   // If the value has already been used, add it directly to the
1320   // DeferredDeclsToEmit list.
1321   StringRef MangledName = getMangledName(GD);
1322   if (llvm::GlobalValue *GV = GetGlobalValue(MangledName))
1323     addDeferredDeclToEmit(GV, GD);
1324   else {
1325     // Otherwise, remember that we saw a deferred decl with this name.  The
1326     // first use of the mangled name will cause it to move into
1327     // DeferredDeclsToEmit.
1328     DeferredDecls[MangledName] = GD;
1329   }
1330 }
1331 
1332 namespace {
1333   struct FunctionIsDirectlyRecursive :
1334     public RecursiveASTVisitor<FunctionIsDirectlyRecursive> {
1335     const StringRef Name;
1336     const Builtin::Context &BI;
1337     bool Result;
1338     FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) :
1339       Name(N), BI(C), Result(false) {
1340     }
1341     typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base;
1342 
1343     bool TraverseCallExpr(CallExpr *E) {
1344       const FunctionDecl *FD = E->getDirectCallee();
1345       if (!FD)
1346         return true;
1347       AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
1348       if (Attr && Name == Attr->getLabel()) {
1349         Result = true;
1350         return false;
1351       }
1352       unsigned BuiltinID = FD->getBuiltinID();
1353       if (!BuiltinID)
1354         return true;
1355       StringRef BuiltinName = BI.GetName(BuiltinID);
1356       if (BuiltinName.startswith("__builtin_") &&
1357           Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
1358         Result = true;
1359         return false;
1360       }
1361       return true;
1362     }
1363   };
1364 }
1365 
1366 // isTriviallyRecursive - Check if this function calls another
1367 // decl that, because of the asm attribute or the other decl being a builtin,
1368 // ends up pointing to itself.
1369 bool
1370 CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
1371   StringRef Name;
1372   if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
1373     // asm labels are a special kind of mangling we have to support.
1374     AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
1375     if (!Attr)
1376       return false;
1377     Name = Attr->getLabel();
1378   } else {
1379     Name = FD->getName();
1380   }
1381 
1382   FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
1383   Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD));
1384   return Walker.Result;
1385 }
1386 
1387 bool
1388 CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
1389   if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
1390     return true;
1391   const auto *F = cast<FunctionDecl>(GD.getDecl());
1392   if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
1393     return false;
1394   // PR9614. Avoid cases where the source code is lying to us. An available
1395   // externally function should have an equivalent function somewhere else,
1396   // but a function that calls itself is clearly not equivalent to the real
1397   // implementation.
1398   // This happens in glibc's btowc and in some configure checks.
1399   return !isTriviallyRecursive(F);
1400 }
1401 
1402 /// If the type for the method's class was generated by
1403 /// CGDebugInfo::createContextChain(), the cache contains only a
1404 /// limited DIType without any declarations. Since EmitFunctionStart()
1405 /// needs to find the canonical declaration for each method, we need
1406 /// to construct the complete type prior to emitting the method.
1407 void CodeGenModule::CompleteDIClassType(const CXXMethodDecl* D) {
1408   if (!D->isInstance())
1409     return;
1410 
1411   if (CGDebugInfo *DI = getModuleDebugInfo())
1412     if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
1413       const auto *ThisPtr = cast<PointerType>(D->getThisType(getContext()));
1414       DI->getOrCreateRecordType(ThisPtr->getPointeeType(), D->getLocation());
1415     }
1416 }
1417 
1418 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
1419   const auto *D = cast<ValueDecl>(GD.getDecl());
1420 
1421   PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
1422                                  Context.getSourceManager(),
1423                                  "Generating code for declaration");
1424 
1425   if (isa<FunctionDecl>(D)) {
1426     // At -O0, don't generate IR for functions with available_externally
1427     // linkage.
1428     if (!shouldEmitFunction(GD))
1429       return;
1430 
1431     if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
1432       CompleteDIClassType(Method);
1433       // Make sure to emit the definition(s) before we emit the thunks.
1434       // This is necessary for the generation of certain thunks.
1435       if (const auto *CD = dyn_cast<CXXConstructorDecl>(Method))
1436         ABI->emitCXXStructor(CD, getFromCtorType(GD.getCtorType()));
1437       else if (const auto *DD = dyn_cast<CXXDestructorDecl>(Method))
1438         ABI->emitCXXStructor(DD, getFromDtorType(GD.getDtorType()));
1439       else
1440         EmitGlobalFunctionDefinition(GD, GV);
1441 
1442       if (Method->isVirtual())
1443         getVTables().EmitThunks(GD);
1444 
1445       return;
1446     }
1447 
1448     return EmitGlobalFunctionDefinition(GD, GV);
1449   }
1450 
1451   if (const auto *VD = dyn_cast<VarDecl>(D))
1452     return EmitGlobalVarDefinition(VD);
1453 
1454   llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
1455 }
1456 
1457 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
1458 /// module, create and return an llvm Function with the specified type. If there
1459 /// is something in the module with the specified name, return it potentially
1460 /// bitcasted to the right type.
1461 ///
1462 /// If D is non-null, it specifies a decl that correspond to this.  This is used
1463 /// to set the attributes on the function when it is first created.
1464 llvm::Constant *
1465 CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName,
1466                                        llvm::Type *Ty,
1467                                        GlobalDecl GD, bool ForVTable,
1468                                        bool DontDefer,
1469                                        llvm::AttributeSet ExtraAttrs) {
1470   const Decl *D = GD.getDecl();
1471 
1472   // Lookup the entry, lazily creating it if necessary.
1473   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
1474   if (Entry) {
1475     if (WeakRefReferences.erase(Entry)) {
1476       const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
1477       if (FD && !FD->hasAttr<WeakAttr>())
1478         Entry->setLinkage(llvm::Function::ExternalLinkage);
1479     }
1480 
1481     // Handle dropped DLL attributes.
1482     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
1483       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
1484 
1485     if (Entry->getType()->getElementType() == Ty)
1486       return Entry;
1487 
1488     // Make sure the result is of the correct type.
1489     return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
1490   }
1491 
1492   // This function doesn't have a complete type (for example, the return
1493   // type is an incomplete struct). Use a fake type instead, and make
1494   // sure not to try to set attributes.
1495   bool IsIncompleteFunction = false;
1496 
1497   llvm::FunctionType *FTy;
1498   if (isa<llvm::FunctionType>(Ty)) {
1499     FTy = cast<llvm::FunctionType>(Ty);
1500   } else {
1501     FTy = llvm::FunctionType::get(VoidTy, false);
1502     IsIncompleteFunction = true;
1503   }
1504 
1505   llvm::Function *F = llvm::Function::Create(FTy,
1506                                              llvm::Function::ExternalLinkage,
1507                                              MangledName, &getModule());
1508   assert(F->getName() == MangledName && "name was uniqued!");
1509   if (D)
1510     SetFunctionAttributes(GD, F, IsIncompleteFunction);
1511   if (ExtraAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex)) {
1512     llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeSet::FunctionIndex);
1513     F->addAttributes(llvm::AttributeSet::FunctionIndex,
1514                      llvm::AttributeSet::get(VMContext,
1515                                              llvm::AttributeSet::FunctionIndex,
1516                                              B));
1517   }
1518 
1519   if (!DontDefer) {
1520     // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
1521     // each other bottoming out with the base dtor.  Therefore we emit non-base
1522     // dtors on usage, even if there is no dtor definition in the TU.
1523     if (D && isa<CXXDestructorDecl>(D) &&
1524         getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
1525                                            GD.getDtorType()))
1526       addDeferredDeclToEmit(F, GD);
1527 
1528     // This is the first use or definition of a mangled name.  If there is a
1529     // deferred decl with this name, remember that we need to emit it at the end
1530     // of the file.
1531     auto DDI = DeferredDecls.find(MangledName);
1532     if (DDI != DeferredDecls.end()) {
1533       // Move the potentially referenced deferred decl to the
1534       // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
1535       // don't need it anymore).
1536       addDeferredDeclToEmit(F, DDI->second);
1537       DeferredDecls.erase(DDI);
1538 
1539       // Otherwise, if this is a sized deallocation function, emit a weak
1540       // definition
1541       // for it at the end of the translation unit.
1542     } else if (D && cast<FunctionDecl>(D)
1543                         ->getCorrespondingUnsizedGlobalDeallocationFunction()) {
1544       addDeferredDeclToEmit(F, GD);
1545 
1546       // Otherwise, there are cases we have to worry about where we're
1547       // using a declaration for which we must emit a definition but where
1548       // we might not find a top-level definition:
1549       //   - member functions defined inline in their classes
1550       //   - friend functions defined inline in some class
1551       //   - special member functions with implicit definitions
1552       // If we ever change our AST traversal to walk into class methods,
1553       // this will be unnecessary.
1554       //
1555       // We also don't emit a definition for a function if it's going to be an
1556       // entry in a vtable, unless it's already marked as used.
1557     } else if (getLangOpts().CPlusPlus && D) {
1558       // Look for a declaration that's lexically in a record.
1559       for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
1560            FD = FD->getPreviousDecl()) {
1561         if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
1562           if (FD->doesThisDeclarationHaveABody()) {
1563             addDeferredDeclToEmit(F, GD.getWithDecl(FD));
1564             break;
1565           }
1566         }
1567       }
1568     }
1569   }
1570 
1571   // Make sure the result is of the requested type.
1572   if (!IsIncompleteFunction) {
1573     assert(F->getType()->getElementType() == Ty);
1574     return F;
1575   }
1576 
1577   llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
1578   return llvm::ConstantExpr::getBitCast(F, PTy);
1579 }
1580 
1581 /// GetAddrOfFunction - Return the address of the given function.  If Ty is
1582 /// non-null, then this function will use the specified type if it has to
1583 /// create it (this occurs when we see a definition of the function).
1584 llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
1585                                                  llvm::Type *Ty,
1586                                                  bool ForVTable,
1587                                                  bool DontDefer) {
1588   // If there was no specific requested type, just convert it now.
1589   if (!Ty)
1590     Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
1591 
1592   StringRef MangledName = getMangledName(GD);
1593   return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer);
1594 }
1595 
1596 /// CreateRuntimeFunction - Create a new runtime function with the specified
1597 /// type and name.
1598 llvm::Constant *
1599 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy,
1600                                      StringRef Name,
1601                                      llvm::AttributeSet ExtraAttrs) {
1602   llvm::Constant *C =
1603       GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
1604                               /*DontDefer=*/false, ExtraAttrs);
1605   if (auto *F = dyn_cast<llvm::Function>(C))
1606     if (F->empty())
1607       F->setCallingConv(getRuntimeCC());
1608   return C;
1609 }
1610 
1611 /// isTypeConstant - Determine whether an object of this type can be emitted
1612 /// as a constant.
1613 ///
1614 /// If ExcludeCtor is true, the duration when the object's constructor runs
1615 /// will not be considered. The caller will need to verify that the object is
1616 /// not written to during its construction.
1617 bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
1618   if (!Ty.isConstant(Context) && !Ty->isReferenceType())
1619     return false;
1620 
1621   if (Context.getLangOpts().CPlusPlus) {
1622     if (const CXXRecordDecl *Record
1623           = Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
1624       return ExcludeCtor && !Record->hasMutableFields() &&
1625              Record->hasTrivialDestructor();
1626   }
1627 
1628   return true;
1629 }
1630 
1631 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
1632 /// create and return an llvm GlobalVariable with the specified type.  If there
1633 /// is something in the module with the specified name, return it potentially
1634 /// bitcasted to the right type.
1635 ///
1636 /// If D is non-null, it specifies a decl that correspond to this.  This is used
1637 /// to set the attributes on the global when it is first created.
1638 llvm::Constant *
1639 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
1640                                      llvm::PointerType *Ty,
1641                                      const VarDecl *D) {
1642   // Lookup the entry, lazily creating it if necessary.
1643   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
1644   if (Entry) {
1645     if (WeakRefReferences.erase(Entry)) {
1646       if (D && !D->hasAttr<WeakAttr>())
1647         Entry->setLinkage(llvm::Function::ExternalLinkage);
1648     }
1649 
1650     // Handle dropped DLL attributes.
1651     if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
1652       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
1653 
1654     if (Entry->getType() == Ty)
1655       return Entry;
1656 
1657     // Make sure the result is of the correct type.
1658     if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace())
1659       return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty);
1660 
1661     return llvm::ConstantExpr::getBitCast(Entry, Ty);
1662   }
1663 
1664   unsigned AddrSpace = GetGlobalVarAddressSpace(D, Ty->getAddressSpace());
1665   auto *GV = new llvm::GlobalVariable(
1666       getModule(), Ty->getElementType(), false,
1667       llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr,
1668       llvm::GlobalVariable::NotThreadLocal, AddrSpace);
1669 
1670   // This is the first use or definition of a mangled name.  If there is a
1671   // deferred decl with this name, remember that we need to emit it at the end
1672   // of the file.
1673   auto DDI = DeferredDecls.find(MangledName);
1674   if (DDI != DeferredDecls.end()) {
1675     // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
1676     // list, and remove it from DeferredDecls (since we don't need it anymore).
1677     addDeferredDeclToEmit(GV, DDI->second);
1678     DeferredDecls.erase(DDI);
1679   }
1680 
1681   // Handle things which are present even on external declarations.
1682   if (D) {
1683     // FIXME: This code is overly simple and should be merged with other global
1684     // handling.
1685     GV->setConstant(isTypeConstant(D->getType(), false));
1686 
1687     setLinkageAndVisibilityForGV(GV, D);
1688 
1689     if (D->getTLSKind()) {
1690       if (D->getTLSKind() == VarDecl::TLS_Dynamic)
1691         CXXThreadLocals.push_back(std::make_pair(D, GV));
1692       setTLSMode(GV, *D);
1693     }
1694 
1695     // If required by the ABI, treat declarations of static data members with
1696     // inline initializers as definitions.
1697     if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
1698       EmitGlobalVarDefinition(D);
1699     }
1700 
1701     // Handle XCore specific ABI requirements.
1702     if (getTarget().getTriple().getArch() == llvm::Triple::xcore &&
1703         D->getLanguageLinkage() == CLanguageLinkage &&
1704         D->getType().isConstant(Context) &&
1705         isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
1706       GV->setSection(".cp.rodata");
1707   }
1708 
1709   if (AddrSpace != Ty->getAddressSpace())
1710     return llvm::ConstantExpr::getAddrSpaceCast(GV, Ty);
1711 
1712   return GV;
1713 }
1714 
1715 
1716 llvm::GlobalVariable *
1717 CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name,
1718                                       llvm::Type *Ty,
1719                                       llvm::GlobalValue::LinkageTypes Linkage) {
1720   llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
1721   llvm::GlobalVariable *OldGV = nullptr;
1722 
1723   if (GV) {
1724     // Check if the variable has the right type.
1725     if (GV->getType()->getElementType() == Ty)
1726       return GV;
1727 
1728     // Because C++ name mangling, the only way we can end up with an already
1729     // existing global with the same name is if it has been declared extern "C".
1730     assert(GV->isDeclaration() && "Declaration has wrong type!");
1731     OldGV = GV;
1732   }
1733 
1734   // Create a new variable.
1735   GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
1736                                 Linkage, nullptr, Name);
1737 
1738   if (OldGV) {
1739     // Replace occurrences of the old variable if needed.
1740     GV->takeName(OldGV);
1741 
1742     if (!OldGV->use_empty()) {
1743       llvm::Constant *NewPtrForOldDecl =
1744       llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
1745       OldGV->replaceAllUsesWith(NewPtrForOldDecl);
1746     }
1747 
1748     OldGV->eraseFromParent();
1749   }
1750 
1751   return GV;
1752 }
1753 
1754 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
1755 /// given global variable.  If Ty is non-null and if the global doesn't exist,
1756 /// then it will be created with the specified type instead of whatever the
1757 /// normal requested type would be.
1758 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
1759                                                   llvm::Type *Ty) {
1760   assert(D->hasGlobalStorage() && "Not a global variable");
1761   QualType ASTTy = D->getType();
1762   if (!Ty)
1763     Ty = getTypes().ConvertTypeForMem(ASTTy);
1764 
1765   llvm::PointerType *PTy =
1766     llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
1767 
1768   StringRef MangledName = getMangledName(D);
1769   return GetOrCreateLLVMGlobal(MangledName, PTy, D);
1770 }
1771 
1772 /// CreateRuntimeVariable - Create a new runtime global variable with the
1773 /// specified type and name.
1774 llvm::Constant *
1775 CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
1776                                      StringRef Name) {
1777   return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), nullptr);
1778 }
1779 
1780 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
1781   assert(!D->getInit() && "Cannot emit definite definitions here!");
1782 
1783   if (MayDeferGeneration(D)) {
1784     // If we have not seen a reference to this variable yet, place it
1785     // into the deferred declarations table to be emitted if needed
1786     // later.
1787     StringRef MangledName = getMangledName(D);
1788     if (!GetGlobalValue(MangledName)) {
1789       DeferredDecls[MangledName] = D;
1790       return;
1791     }
1792   }
1793 
1794   // The tentative definition is the only definition.
1795   EmitGlobalVarDefinition(D);
1796 }
1797 
1798 CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
1799     return Context.toCharUnitsFromBits(
1800       TheDataLayout.getTypeStoreSizeInBits(Ty));
1801 }
1802 
1803 unsigned CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D,
1804                                                  unsigned AddrSpace) {
1805   if (LangOpts.CUDA && CodeGenOpts.CUDAIsDevice) {
1806     if (D->hasAttr<CUDAConstantAttr>())
1807       AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_constant);
1808     else if (D->hasAttr<CUDASharedAttr>())
1809       AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_shared);
1810     else
1811       AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_device);
1812   }
1813 
1814   return AddrSpace;
1815 }
1816 
1817 template<typename SomeDecl>
1818 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
1819                                                llvm::GlobalValue *GV) {
1820   if (!getLangOpts().CPlusPlus)
1821     return;
1822 
1823   // Must have 'used' attribute, or else inline assembly can't rely on
1824   // the name existing.
1825   if (!D->template hasAttr<UsedAttr>())
1826     return;
1827 
1828   // Must have internal linkage and an ordinary name.
1829   if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
1830     return;
1831 
1832   // Must be in an extern "C" context. Entities declared directly within
1833   // a record are not extern "C" even if the record is in such a context.
1834   const SomeDecl *First = D->getFirstDecl();
1835   if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
1836     return;
1837 
1838   // OK, this is an internal linkage entity inside an extern "C" linkage
1839   // specification. Make a note of that so we can give it the "expected"
1840   // mangled name if nothing else is using that name.
1841   std::pair<StaticExternCMap::iterator, bool> R =
1842       StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
1843 
1844   // If we have multiple internal linkage entities with the same name
1845   // in extern "C" regions, none of them gets that name.
1846   if (!R.second)
1847     R.first->second = nullptr;
1848 }
1849 
1850 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
1851   llvm::Constant *Init = nullptr;
1852   QualType ASTTy = D->getType();
1853   CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
1854   bool NeedsGlobalCtor = false;
1855   bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor();
1856 
1857   const VarDecl *InitDecl;
1858   const Expr *InitExpr = D->getAnyInitializer(InitDecl);
1859 
1860   if (!InitExpr) {
1861     // This is a tentative definition; tentative definitions are
1862     // implicitly initialized with { 0 }.
1863     //
1864     // Note that tentative definitions are only emitted at the end of
1865     // a translation unit, so they should never have incomplete
1866     // type. In addition, EmitTentativeDefinition makes sure that we
1867     // never attempt to emit a tentative definition if a real one
1868     // exists. A use may still exists, however, so we still may need
1869     // to do a RAUW.
1870     assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
1871     Init = EmitNullConstant(D->getType());
1872   } else {
1873     initializedGlobalDecl = GlobalDecl(D);
1874     Init = EmitConstantInit(*InitDecl);
1875 
1876     if (!Init) {
1877       QualType T = InitExpr->getType();
1878       if (D->getType()->isReferenceType())
1879         T = D->getType();
1880 
1881       if (getLangOpts().CPlusPlus) {
1882         Init = EmitNullConstant(T);
1883         NeedsGlobalCtor = true;
1884       } else {
1885         ErrorUnsupported(D, "static initializer");
1886         Init = llvm::UndefValue::get(getTypes().ConvertType(T));
1887       }
1888     } else {
1889       // We don't need an initializer, so remove the entry for the delayed
1890       // initializer position (just in case this entry was delayed) if we
1891       // also don't need to register a destructor.
1892       if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
1893         DelayedCXXInitPosition.erase(D);
1894     }
1895   }
1896 
1897   llvm::Type* InitType = Init->getType();
1898   llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
1899 
1900   // Strip off a bitcast if we got one back.
1901   if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
1902     assert(CE->getOpcode() == llvm::Instruction::BitCast ||
1903            CE->getOpcode() == llvm::Instruction::AddrSpaceCast ||
1904            // All zero index gep.
1905            CE->getOpcode() == llvm::Instruction::GetElementPtr);
1906     Entry = CE->getOperand(0);
1907   }
1908 
1909   // Entry is now either a Function or GlobalVariable.
1910   auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
1911 
1912   // We have a definition after a declaration with the wrong type.
1913   // We must make a new GlobalVariable* and update everything that used OldGV
1914   // (a declaration or tentative definition) with the new GlobalVariable*
1915   // (which will be a definition).
1916   //
1917   // This happens if there is a prototype for a global (e.g.
1918   // "extern int x[];") and then a definition of a different type (e.g.
1919   // "int x[10];"). This also happens when an initializer has a different type
1920   // from the type of the global (this happens with unions).
1921   if (!GV ||
1922       GV->getType()->getElementType() != InitType ||
1923       GV->getType()->getAddressSpace() !=
1924        GetGlobalVarAddressSpace(D, getContext().getTargetAddressSpace(ASTTy))) {
1925 
1926     // Move the old entry aside so that we'll create a new one.
1927     Entry->setName(StringRef());
1928 
1929     // Make a new global with the correct type, this is now guaranteed to work.
1930     GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType));
1931 
1932     // Replace all uses of the old global with the new global
1933     llvm::Constant *NewPtrForOldDecl =
1934         llvm::ConstantExpr::getBitCast(GV, Entry->getType());
1935     Entry->replaceAllUsesWith(NewPtrForOldDecl);
1936 
1937     // Erase the old global, since it is no longer used.
1938     cast<llvm::GlobalValue>(Entry)->eraseFromParent();
1939   }
1940 
1941   MaybeHandleStaticInExternC(D, GV);
1942 
1943   if (D->hasAttr<AnnotateAttr>())
1944     AddGlobalAnnotations(D, GV);
1945 
1946   GV->setInitializer(Init);
1947 
1948   // If it is safe to mark the global 'constant', do so now.
1949   GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
1950                   isTypeConstant(D->getType(), true));
1951 
1952   // If it is in a read-only section, mark it 'constant'.
1953   if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
1954     const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
1955     if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
1956       GV->setConstant(true);
1957   }
1958 
1959   GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
1960 
1961   // Set the llvm linkage type as appropriate.
1962   llvm::GlobalValue::LinkageTypes Linkage =
1963       getLLVMLinkageVarDefinition(D, GV->isConstant());
1964 
1965   // On Darwin, the backing variable for a C++11 thread_local variable always
1966   // has internal linkage; all accesses should just be calls to the
1967   // Itanium-specified entry point, which has the normal linkage of the
1968   // variable.
1969   if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
1970       Context.getTargetInfo().getTriple().isMacOSX())
1971     Linkage = llvm::GlobalValue::InternalLinkage;
1972 
1973   GV->setLinkage(Linkage);
1974   if (D->hasAttr<DLLImportAttr>())
1975     GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1976   else if (D->hasAttr<DLLExportAttr>())
1977     GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1978 
1979   if (Linkage == llvm::GlobalVariable::CommonLinkage)
1980     // common vars aren't constant even if declared const.
1981     GV->setConstant(false);
1982 
1983   setNonAliasAttributes(D, GV);
1984 
1985   if (D->getTLSKind() && !GV->isThreadLocal()) {
1986     if (D->getTLSKind() == VarDecl::TLS_Dynamic)
1987       CXXThreadLocals.push_back(std::make_pair(D, GV));
1988     setTLSMode(GV, *D);
1989   }
1990 
1991   // Emit the initializer function if necessary.
1992   if (NeedsGlobalCtor || NeedsGlobalDtor)
1993     EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
1994 
1995   SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
1996 
1997   // Emit global variable debug information.
1998   if (CGDebugInfo *DI = getModuleDebugInfo())
1999     if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
2000       DI->EmitGlobalVariable(GV, D);
2001 }
2002 
2003 static bool isVarDeclStrongDefinition(const ASTContext &Context,
2004                                       const VarDecl *D, bool NoCommon) {
2005   // Don't give variables common linkage if -fno-common was specified unless it
2006   // was overridden by a NoCommon attribute.
2007   if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
2008     return true;
2009 
2010   // C11 6.9.2/2:
2011   //   A declaration of an identifier for an object that has file scope without
2012   //   an initializer, and without a storage-class specifier or with the
2013   //   storage-class specifier static, constitutes a tentative definition.
2014   if (D->getInit() || D->hasExternalStorage())
2015     return true;
2016 
2017   // A variable cannot be both common and exist in a section.
2018   if (D->hasAttr<SectionAttr>())
2019     return true;
2020 
2021   // Thread local vars aren't considered common linkage.
2022   if (D->getTLSKind())
2023     return true;
2024 
2025   // Tentative definitions marked with WeakImportAttr are true definitions.
2026   if (D->hasAttr<WeakImportAttr>())
2027     return true;
2028 
2029   // Declarations with a required alignment do not have common linakge in MSVC
2030   // mode.
2031   if (Context.getLangOpts().MSVCCompat &&
2032       (Context.isAlignmentRequired(D->getType()) || D->hasAttr<AlignedAttr>()))
2033     return true;
2034 
2035   return false;
2036 }
2037 
2038 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator(
2039     const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) {
2040   if (Linkage == GVA_Internal)
2041     return llvm::Function::InternalLinkage;
2042 
2043   if (D->hasAttr<WeakAttr>()) {
2044     if (IsConstantVariable)
2045       return llvm::GlobalVariable::WeakODRLinkage;
2046     else
2047       return llvm::GlobalVariable::WeakAnyLinkage;
2048   }
2049 
2050   // We are guaranteed to have a strong definition somewhere else,
2051   // so we can use available_externally linkage.
2052   if (Linkage == GVA_AvailableExternally)
2053     return llvm::Function::AvailableExternallyLinkage;
2054 
2055   // Note that Apple's kernel linker doesn't support symbol
2056   // coalescing, so we need to avoid linkonce and weak linkages there.
2057   // Normally, this means we just map to internal, but for explicit
2058   // instantiations we'll map to external.
2059 
2060   // In C++, the compiler has to emit a definition in every translation unit
2061   // that references the function.  We should use linkonce_odr because
2062   // a) if all references in this translation unit are optimized away, we
2063   // don't need to codegen it.  b) if the function persists, it needs to be
2064   // merged with other definitions. c) C++ has the ODR, so we know the
2065   // definition is dependable.
2066   if (Linkage == GVA_DiscardableODR)
2067     return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
2068                                             : llvm::Function::InternalLinkage;
2069 
2070   // An explicit instantiation of a template has weak linkage, since
2071   // explicit instantiations can occur in multiple translation units
2072   // and must all be equivalent. However, we are not allowed to
2073   // throw away these explicit instantiations.
2074   if (Linkage == GVA_StrongODR)
2075     return !Context.getLangOpts().AppleKext ? llvm::Function::WeakODRLinkage
2076                                             : llvm::Function::ExternalLinkage;
2077 
2078   // C++ doesn't have tentative definitions and thus cannot have common
2079   // linkage.
2080   if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
2081       !isVarDeclStrongDefinition(Context, cast<VarDecl>(D),
2082                                  CodeGenOpts.NoCommon))
2083     return llvm::GlobalVariable::CommonLinkage;
2084 
2085   // selectany symbols are externally visible, so use weak instead of
2086   // linkonce.  MSVC optimizes away references to const selectany globals, so
2087   // all definitions should be the same and ODR linkage should be used.
2088   // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
2089   if (D->hasAttr<SelectAnyAttr>())
2090     return llvm::GlobalVariable::WeakODRLinkage;
2091 
2092   // Otherwise, we have strong external linkage.
2093   assert(Linkage == GVA_StrongExternal);
2094   return llvm::GlobalVariable::ExternalLinkage;
2095 }
2096 
2097 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition(
2098     const VarDecl *VD, bool IsConstant) {
2099   GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
2100   return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant);
2101 }
2102 
2103 /// Replace the uses of a function that was declared with a non-proto type.
2104 /// We want to silently drop extra arguments from call sites
2105 static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
2106                                           llvm::Function *newFn) {
2107   // Fast path.
2108   if (old->use_empty()) return;
2109 
2110   llvm::Type *newRetTy = newFn->getReturnType();
2111   SmallVector<llvm::Value*, 4> newArgs;
2112 
2113   for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
2114          ui != ue; ) {
2115     llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
2116     llvm::User *user = use->getUser();
2117 
2118     // Recognize and replace uses of bitcasts.  Most calls to
2119     // unprototyped functions will use bitcasts.
2120     if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
2121       if (bitcast->getOpcode() == llvm::Instruction::BitCast)
2122         replaceUsesOfNonProtoConstant(bitcast, newFn);
2123       continue;
2124     }
2125 
2126     // Recognize calls to the function.
2127     llvm::CallSite callSite(user);
2128     if (!callSite) continue;
2129     if (!callSite.isCallee(&*use)) continue;
2130 
2131     // If the return types don't match exactly, then we can't
2132     // transform this call unless it's dead.
2133     if (callSite->getType() != newRetTy && !callSite->use_empty())
2134       continue;
2135 
2136     // Get the call site's attribute list.
2137     SmallVector<llvm::AttributeSet, 8> newAttrs;
2138     llvm::AttributeSet oldAttrs = callSite.getAttributes();
2139 
2140     // Collect any return attributes from the call.
2141     if (oldAttrs.hasAttributes(llvm::AttributeSet::ReturnIndex))
2142       newAttrs.push_back(
2143         llvm::AttributeSet::get(newFn->getContext(),
2144                                 oldAttrs.getRetAttributes()));
2145 
2146     // If the function was passed too few arguments, don't transform.
2147     unsigned newNumArgs = newFn->arg_size();
2148     if (callSite.arg_size() < newNumArgs) continue;
2149 
2150     // If extra arguments were passed, we silently drop them.
2151     // If any of the types mismatch, we don't transform.
2152     unsigned argNo = 0;
2153     bool dontTransform = false;
2154     for (llvm::Function::arg_iterator ai = newFn->arg_begin(),
2155            ae = newFn->arg_end(); ai != ae; ++ai, ++argNo) {
2156       if (callSite.getArgument(argNo)->getType() != ai->getType()) {
2157         dontTransform = true;
2158         break;
2159       }
2160 
2161       // Add any parameter attributes.
2162       if (oldAttrs.hasAttributes(argNo + 1))
2163         newAttrs.
2164           push_back(llvm::
2165                     AttributeSet::get(newFn->getContext(),
2166                                       oldAttrs.getParamAttributes(argNo + 1)));
2167     }
2168     if (dontTransform)
2169       continue;
2170 
2171     if (oldAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex))
2172       newAttrs.push_back(llvm::AttributeSet::get(newFn->getContext(),
2173                                                  oldAttrs.getFnAttributes()));
2174 
2175     // Okay, we can transform this.  Create the new call instruction and copy
2176     // over the required information.
2177     newArgs.append(callSite.arg_begin(), callSite.arg_begin() + argNo);
2178 
2179     llvm::CallSite newCall;
2180     if (callSite.isCall()) {
2181       newCall = llvm::CallInst::Create(newFn, newArgs, "",
2182                                        callSite.getInstruction());
2183     } else {
2184       auto *oldInvoke = cast<llvm::InvokeInst>(callSite.getInstruction());
2185       newCall = llvm::InvokeInst::Create(newFn,
2186                                          oldInvoke->getNormalDest(),
2187                                          oldInvoke->getUnwindDest(),
2188                                          newArgs, "",
2189                                          callSite.getInstruction());
2190     }
2191     newArgs.clear(); // for the next iteration
2192 
2193     if (!newCall->getType()->isVoidTy())
2194       newCall->takeName(callSite.getInstruction());
2195     newCall.setAttributes(
2196                      llvm::AttributeSet::get(newFn->getContext(), newAttrs));
2197     newCall.setCallingConv(callSite.getCallingConv());
2198 
2199     // Finally, remove the old call, replacing any uses with the new one.
2200     if (!callSite->use_empty())
2201       callSite->replaceAllUsesWith(newCall.getInstruction());
2202 
2203     // Copy debug location attached to CI.
2204     if (!callSite->getDebugLoc().isUnknown())
2205       newCall->setDebugLoc(callSite->getDebugLoc());
2206     callSite->eraseFromParent();
2207   }
2208 }
2209 
2210 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
2211 /// implement a function with no prototype, e.g. "int foo() {}".  If there are
2212 /// existing call uses of the old function in the module, this adjusts them to
2213 /// call the new function directly.
2214 ///
2215 /// This is not just a cleanup: the always_inline pass requires direct calls to
2216 /// functions to be able to inline them.  If there is a bitcast in the way, it
2217 /// won't inline them.  Instcombine normally deletes these calls, but it isn't
2218 /// run at -O0.
2219 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
2220                                                       llvm::Function *NewFn) {
2221   // If we're redefining a global as a function, don't transform it.
2222   if (!isa<llvm::Function>(Old)) return;
2223 
2224   replaceUsesOfNonProtoConstant(Old, NewFn);
2225 }
2226 
2227 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
2228   TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
2229   // If we have a definition, this might be a deferred decl. If the
2230   // instantiation is explicit, make sure we emit it at the end.
2231   if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
2232     GetAddrOfGlobalVar(VD);
2233 
2234   EmitTopLevelDecl(VD);
2235 }
2236 
2237 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
2238                                                  llvm::GlobalValue *GV) {
2239   const auto *D = cast<FunctionDecl>(GD.getDecl());
2240 
2241   // Compute the function info and LLVM type.
2242   const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
2243   llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
2244 
2245   // Get or create the prototype for the function.
2246   if (!GV) {
2247     llvm::Constant *C =
2248         GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer*/ true);
2249 
2250     // Strip off a bitcast if we got one back.
2251     if (auto *CE = dyn_cast<llvm::ConstantExpr>(C)) {
2252       assert(CE->getOpcode() == llvm::Instruction::BitCast);
2253       GV = cast<llvm::GlobalValue>(CE->getOperand(0));
2254     } else {
2255       GV = cast<llvm::GlobalValue>(C);
2256     }
2257   }
2258 
2259   if (!GV->isDeclaration()) {
2260     getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name);
2261     GlobalDecl OldGD = Manglings.lookup(GV->getName());
2262     if (auto *Prev = OldGD.getDecl())
2263       getDiags().Report(Prev->getLocation(), diag::note_previous_definition);
2264     return;
2265   }
2266 
2267   if (GV->getType()->getElementType() != Ty) {
2268     // If the types mismatch then we have to rewrite the definition.
2269     assert(GV->isDeclaration() && "Shouldn't replace non-declaration");
2270 
2271     // F is the Function* for the one with the wrong type, we must make a new
2272     // Function* and update everything that used F (a declaration) with the new
2273     // Function* (which will be a definition).
2274     //
2275     // This happens if there is a prototype for a function
2276     // (e.g. "int f()") and then a definition of a different type
2277     // (e.g. "int f(int x)").  Move the old function aside so that it
2278     // doesn't interfere with GetAddrOfFunction.
2279     GV->setName(StringRef());
2280     auto *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
2281 
2282     // This might be an implementation of a function without a
2283     // prototype, in which case, try to do special replacement of
2284     // calls which match the new prototype.  The really key thing here
2285     // is that we also potentially drop arguments from the call site
2286     // so as to make a direct call, which makes the inliner happier
2287     // and suppresses a number of optimizer warnings (!) about
2288     // dropping arguments.
2289     if (!GV->use_empty()) {
2290       ReplaceUsesOfNonProtoTypeWithRealFunction(GV, NewFn);
2291       GV->removeDeadConstantUsers();
2292     }
2293 
2294     // Replace uses of F with the Function we will endow with a body.
2295     if (!GV->use_empty()) {
2296       llvm::Constant *NewPtrForOldDecl =
2297           llvm::ConstantExpr::getBitCast(NewFn, GV->getType());
2298       GV->replaceAllUsesWith(NewPtrForOldDecl);
2299     }
2300 
2301     // Ok, delete the old function now, which is dead.
2302     GV->eraseFromParent();
2303 
2304     GV = NewFn;
2305   }
2306 
2307   // We need to set linkage and visibility on the function before
2308   // generating code for it because various parts of IR generation
2309   // want to propagate this information down (e.g. to local static
2310   // declarations).
2311   auto *Fn = cast<llvm::Function>(GV);
2312   setFunctionLinkage(GD, Fn);
2313 
2314   // FIXME: this is redundant with part of setFunctionDefinitionAttributes
2315   setGlobalVisibility(Fn, D);
2316 
2317   MaybeHandleStaticInExternC(D, Fn);
2318 
2319   CodeGenFunction(*this).GenerateCode(D, Fn, FI);
2320 
2321   setFunctionDefinitionAttributes(D, Fn);
2322   SetLLVMFunctionAttributesForDefinition(D, Fn);
2323 
2324   if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
2325     AddGlobalCtor(Fn, CA->getPriority());
2326   if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
2327     AddGlobalDtor(Fn, DA->getPriority());
2328   if (D->hasAttr<AnnotateAttr>())
2329     AddGlobalAnnotations(D, Fn);
2330 }
2331 
2332 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
2333   const auto *D = cast<ValueDecl>(GD.getDecl());
2334   const AliasAttr *AA = D->getAttr<AliasAttr>();
2335   assert(AA && "Not an alias?");
2336 
2337   StringRef MangledName = getMangledName(GD);
2338 
2339   // If there is a definition in the module, then it wins over the alias.
2340   // This is dubious, but allow it to be safe.  Just ignore the alias.
2341   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
2342   if (Entry && !Entry->isDeclaration())
2343     return;
2344 
2345   Aliases.push_back(GD);
2346 
2347   llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
2348 
2349   // Create a reference to the named value.  This ensures that it is emitted
2350   // if a deferred decl.
2351   llvm::Constant *Aliasee;
2352   if (isa<llvm::FunctionType>(DeclTy))
2353     Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
2354                                       /*ForVTable=*/false);
2355   else
2356     Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
2357                                     llvm::PointerType::getUnqual(DeclTy),
2358                                     /*D=*/nullptr);
2359 
2360   // Create the new alias itself, but don't set a name yet.
2361   auto *GA = llvm::GlobalAlias::create(
2362       cast<llvm::PointerType>(Aliasee->getType())->getElementType(), 0,
2363       llvm::Function::ExternalLinkage, "", Aliasee, &getModule());
2364 
2365   if (Entry) {
2366     if (GA->getAliasee() == Entry) {
2367       Diags.Report(AA->getLocation(), diag::err_cyclic_alias);
2368       return;
2369     }
2370 
2371     assert(Entry->isDeclaration());
2372 
2373     // If there is a declaration in the module, then we had an extern followed
2374     // by the alias, as in:
2375     //   extern int test6();
2376     //   ...
2377     //   int test6() __attribute__((alias("test7")));
2378     //
2379     // Remove it and replace uses of it with the alias.
2380     GA->takeName(Entry);
2381 
2382     Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
2383                                                           Entry->getType()));
2384     Entry->eraseFromParent();
2385   } else {
2386     GA->setName(MangledName);
2387   }
2388 
2389   // Set attributes which are particular to an alias; this is a
2390   // specialization of the attributes which may be set on a global
2391   // variable/function.
2392   if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
2393       D->isWeakImported()) {
2394     GA->setLinkage(llvm::Function::WeakAnyLinkage);
2395   }
2396 
2397   if (const auto *VD = dyn_cast<VarDecl>(D))
2398     if (VD->getTLSKind())
2399       setTLSMode(GA, *VD);
2400 
2401   setAliasAttributes(D, GA);
2402 }
2403 
2404 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
2405                                             ArrayRef<llvm::Type*> Tys) {
2406   return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
2407                                          Tys);
2408 }
2409 
2410 static llvm::StringMapEntry<llvm::Constant*> &
2411 GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map,
2412                          const StringLiteral *Literal,
2413                          bool TargetIsLSB,
2414                          bool &IsUTF16,
2415                          unsigned &StringLength) {
2416   StringRef String = Literal->getString();
2417   unsigned NumBytes = String.size();
2418 
2419   // Check for simple case.
2420   if (!Literal->containsNonAsciiOrNull()) {
2421     StringLength = NumBytes;
2422     return Map.GetOrCreateValue(String);
2423   }
2424 
2425   // Otherwise, convert the UTF8 literals into a string of shorts.
2426   IsUTF16 = true;
2427 
2428   SmallVector<UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
2429   const UTF8 *FromPtr = (const UTF8 *)String.data();
2430   UTF16 *ToPtr = &ToBuf[0];
2431 
2432   (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
2433                            &ToPtr, ToPtr + NumBytes,
2434                            strictConversion);
2435 
2436   // ConvertUTF8toUTF16 returns the length in ToPtr.
2437   StringLength = ToPtr - &ToBuf[0];
2438 
2439   // Add an explicit null.
2440   *ToPtr = 0;
2441   return Map.
2442     GetOrCreateValue(StringRef(reinterpret_cast<const char *>(ToBuf.data()),
2443                                (StringLength + 1) * 2));
2444 }
2445 
2446 static llvm::StringMapEntry<llvm::Constant*> &
2447 GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map,
2448                        const StringLiteral *Literal,
2449                        unsigned &StringLength) {
2450   StringRef String = Literal->getString();
2451   StringLength = String.size();
2452   return Map.GetOrCreateValue(String);
2453 }
2454 
2455 llvm::Constant *
2456 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
2457   unsigned StringLength = 0;
2458   bool isUTF16 = false;
2459   llvm::StringMapEntry<llvm::Constant*> &Entry =
2460     GetConstantCFStringEntry(CFConstantStringMap, Literal,
2461                              getDataLayout().isLittleEndian(),
2462                              isUTF16, StringLength);
2463 
2464   if (llvm::Constant *C = Entry.getValue())
2465     return C;
2466 
2467   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
2468   llvm::Constant *Zeros[] = { Zero, Zero };
2469   llvm::Value *V;
2470 
2471   // If we don't already have it, get __CFConstantStringClassReference.
2472   if (!CFConstantStringClassRef) {
2473     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
2474     Ty = llvm::ArrayType::get(Ty, 0);
2475     llvm::Constant *GV = CreateRuntimeVariable(Ty,
2476                                            "__CFConstantStringClassReference");
2477     // Decay array -> ptr
2478     V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2479     CFConstantStringClassRef = V;
2480   }
2481   else
2482     V = CFConstantStringClassRef;
2483 
2484   QualType CFTy = getContext().getCFConstantStringType();
2485 
2486   auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
2487 
2488   llvm::Constant *Fields[4];
2489 
2490   // Class pointer.
2491   Fields[0] = cast<llvm::ConstantExpr>(V);
2492 
2493   // Flags.
2494   llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
2495   Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) :
2496     llvm::ConstantInt::get(Ty, 0x07C8);
2497 
2498   // String pointer.
2499   llvm::Constant *C = nullptr;
2500   if (isUTF16) {
2501     ArrayRef<uint16_t> Arr =
2502       llvm::makeArrayRef<uint16_t>(reinterpret_cast<uint16_t*>(
2503                                      const_cast<char *>(Entry.getKey().data())),
2504                                    Entry.getKey().size() / 2);
2505     C = llvm::ConstantDataArray::get(VMContext, Arr);
2506   } else {
2507     C = llvm::ConstantDataArray::getString(VMContext, Entry.getKey());
2508   }
2509 
2510   // Note: -fwritable-strings doesn't make the backing store strings of
2511   // CFStrings writable. (See <rdar://problem/10657500>)
2512   auto *GV =
2513       new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
2514                                llvm::GlobalValue::PrivateLinkage, C, ".str");
2515   GV->setUnnamedAddr(true);
2516   // Don't enforce the target's minimum global alignment, since the only use
2517   // of the string is via this class initializer.
2518   // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. Without
2519   // it LLVM can merge the string with a non unnamed_addr one during LTO. Doing
2520   // that changes the section it ends in, which surprises ld64.
2521   if (isUTF16) {
2522     CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
2523     GV->setAlignment(Align.getQuantity());
2524     GV->setSection("__TEXT,__ustring");
2525   } else {
2526     CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
2527     GV->setAlignment(Align.getQuantity());
2528     GV->setSection("__TEXT,__cstring,cstring_literals");
2529   }
2530 
2531   // String.
2532   Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2533 
2534   if (isUTF16)
2535     // Cast the UTF16 string to the correct type.
2536     Fields[2] = llvm::ConstantExpr::getBitCast(Fields[2], Int8PtrTy);
2537 
2538   // String length.
2539   Ty = getTypes().ConvertType(getContext().LongTy);
2540   Fields[3] = llvm::ConstantInt::get(Ty, StringLength);
2541 
2542   // The struct.
2543   C = llvm::ConstantStruct::get(STy, Fields);
2544   GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
2545                                 llvm::GlobalVariable::PrivateLinkage, C,
2546                                 "_unnamed_cfstring_");
2547   GV->setSection("__DATA,__cfstring");
2548   Entry.setValue(GV);
2549 
2550   return GV;
2551 }
2552 
2553 llvm::Constant *
2554 CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) {
2555   unsigned StringLength = 0;
2556   llvm::StringMapEntry<llvm::Constant*> &Entry =
2557     GetConstantStringEntry(CFConstantStringMap, Literal, StringLength);
2558 
2559   if (llvm::Constant *C = Entry.getValue())
2560     return C;
2561 
2562   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
2563   llvm::Constant *Zeros[] = { Zero, Zero };
2564   llvm::Value *V;
2565   // If we don't already have it, get _NSConstantStringClassReference.
2566   if (!ConstantStringClassRef) {
2567     std::string StringClass(getLangOpts().ObjCConstantStringClass);
2568     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
2569     llvm::Constant *GV;
2570     if (LangOpts.ObjCRuntime.isNonFragile()) {
2571       std::string str =
2572         StringClass.empty() ? "OBJC_CLASS_$_NSConstantString"
2573                             : "OBJC_CLASS_$_" + StringClass;
2574       GV = getObjCRuntime().GetClassGlobal(str);
2575       // Make sure the result is of the correct type.
2576       llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
2577       V = llvm::ConstantExpr::getBitCast(GV, PTy);
2578       ConstantStringClassRef = V;
2579     } else {
2580       std::string str =
2581         StringClass.empty() ? "_NSConstantStringClassReference"
2582                             : "_" + StringClass + "ClassReference";
2583       llvm::Type *PTy = llvm::ArrayType::get(Ty, 0);
2584       GV = CreateRuntimeVariable(PTy, str);
2585       // Decay array -> ptr
2586       V = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2587       ConstantStringClassRef = V;
2588     }
2589   }
2590   else
2591     V = ConstantStringClassRef;
2592 
2593   if (!NSConstantStringType) {
2594     // Construct the type for a constant NSString.
2595     RecordDecl *D = Context.buildImplicitRecord("__builtin_NSString");
2596     D->startDefinition();
2597 
2598     QualType FieldTypes[3];
2599 
2600     // const int *isa;
2601     FieldTypes[0] = Context.getPointerType(Context.IntTy.withConst());
2602     // const char *str;
2603     FieldTypes[1] = Context.getPointerType(Context.CharTy.withConst());
2604     // unsigned int length;
2605     FieldTypes[2] = Context.UnsignedIntTy;
2606 
2607     // Create fields
2608     for (unsigned i = 0; i < 3; ++i) {
2609       FieldDecl *Field = FieldDecl::Create(Context, D,
2610                                            SourceLocation(),
2611                                            SourceLocation(), nullptr,
2612                                            FieldTypes[i], /*TInfo=*/nullptr,
2613                                            /*BitWidth=*/nullptr,
2614                                            /*Mutable=*/false,
2615                                            ICIS_NoInit);
2616       Field->setAccess(AS_public);
2617       D->addDecl(Field);
2618     }
2619 
2620     D->completeDefinition();
2621     QualType NSTy = Context.getTagDeclType(D);
2622     NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy));
2623   }
2624 
2625   llvm::Constant *Fields[3];
2626 
2627   // Class pointer.
2628   Fields[0] = cast<llvm::ConstantExpr>(V);
2629 
2630   // String pointer.
2631   llvm::Constant *C =
2632     llvm::ConstantDataArray::getString(VMContext, Entry.getKey());
2633 
2634   llvm::GlobalValue::LinkageTypes Linkage;
2635   bool isConstant;
2636   Linkage = llvm::GlobalValue::PrivateLinkage;
2637   isConstant = !LangOpts.WritableStrings;
2638 
2639   auto *GV = new llvm::GlobalVariable(getModule(), C->getType(), isConstant,
2640                                       Linkage, C, ".str");
2641   GV->setUnnamedAddr(true);
2642   // Don't enforce the target's minimum global alignment, since the only use
2643   // of the string is via this class initializer.
2644   CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
2645   GV->setAlignment(Align.getQuantity());
2646   Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros);
2647 
2648   // String length.
2649   llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
2650   Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
2651 
2652   // The struct.
2653   C = llvm::ConstantStruct::get(NSConstantStringType, Fields);
2654   GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
2655                                 llvm::GlobalVariable::PrivateLinkage, C,
2656                                 "_unnamed_nsstring_");
2657   const char *NSStringSection = "__OBJC,__cstring_object,regular,no_dead_strip";
2658   const char *NSStringNonFragileABISection =
2659       "__DATA,__objc_stringobj,regular,no_dead_strip";
2660   // FIXME. Fix section.
2661   GV->setSection(LangOpts.ObjCRuntime.isNonFragile()
2662                      ? NSStringNonFragileABISection
2663                      : NSStringSection);
2664   Entry.setValue(GV);
2665 
2666   return GV;
2667 }
2668 
2669 QualType CodeGenModule::getObjCFastEnumerationStateType() {
2670   if (ObjCFastEnumerationStateType.isNull()) {
2671     RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
2672     D->startDefinition();
2673 
2674     QualType FieldTypes[] = {
2675       Context.UnsignedLongTy,
2676       Context.getPointerType(Context.getObjCIdType()),
2677       Context.getPointerType(Context.UnsignedLongTy),
2678       Context.getConstantArrayType(Context.UnsignedLongTy,
2679                            llvm::APInt(32, 5), ArrayType::Normal, 0)
2680     };
2681 
2682     for (size_t i = 0; i < 4; ++i) {
2683       FieldDecl *Field = FieldDecl::Create(Context,
2684                                            D,
2685                                            SourceLocation(),
2686                                            SourceLocation(), nullptr,
2687                                            FieldTypes[i], /*TInfo=*/nullptr,
2688                                            /*BitWidth=*/nullptr,
2689                                            /*Mutable=*/false,
2690                                            ICIS_NoInit);
2691       Field->setAccess(AS_public);
2692       D->addDecl(Field);
2693     }
2694 
2695     D->completeDefinition();
2696     ObjCFastEnumerationStateType = Context.getTagDeclType(D);
2697   }
2698 
2699   return ObjCFastEnumerationStateType;
2700 }
2701 
2702 llvm::Constant *
2703 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
2704   assert(!E->getType()->isPointerType() && "Strings are always arrays");
2705 
2706   // Don't emit it as the address of the string, emit the string data itself
2707   // as an inline array.
2708   if (E->getCharByteWidth() == 1) {
2709     SmallString<64> Str(E->getString());
2710 
2711     // Resize the string to the right size, which is indicated by its type.
2712     const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
2713     Str.resize(CAT->getSize().getZExtValue());
2714     return llvm::ConstantDataArray::getString(VMContext, Str, false);
2715   }
2716 
2717   auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
2718   llvm::Type *ElemTy = AType->getElementType();
2719   unsigned NumElements = AType->getNumElements();
2720 
2721   // Wide strings have either 2-byte or 4-byte elements.
2722   if (ElemTy->getPrimitiveSizeInBits() == 16) {
2723     SmallVector<uint16_t, 32> Elements;
2724     Elements.reserve(NumElements);
2725 
2726     for(unsigned i = 0, e = E->getLength(); i != e; ++i)
2727       Elements.push_back(E->getCodeUnit(i));
2728     Elements.resize(NumElements);
2729     return llvm::ConstantDataArray::get(VMContext, Elements);
2730   }
2731 
2732   assert(ElemTy->getPrimitiveSizeInBits() == 32);
2733   SmallVector<uint32_t, 32> Elements;
2734   Elements.reserve(NumElements);
2735 
2736   for(unsigned i = 0, e = E->getLength(); i != e; ++i)
2737     Elements.push_back(E->getCodeUnit(i));
2738   Elements.resize(NumElements);
2739   return llvm::ConstantDataArray::get(VMContext, Elements);
2740 }
2741 
2742 static llvm::GlobalVariable *
2743 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
2744                       CodeGenModule &CGM, StringRef GlobalName,
2745                       unsigned Alignment) {
2746   // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
2747   unsigned AddrSpace = 0;
2748   if (CGM.getLangOpts().OpenCL)
2749     AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant);
2750 
2751   // Create a global variable for this string
2752   auto *GV = new llvm::GlobalVariable(
2753       CGM.getModule(), C->getType(), !CGM.getLangOpts().WritableStrings, LT, C,
2754       GlobalName, nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
2755   GV->setAlignment(Alignment);
2756   GV->setUnnamedAddr(true);
2757   return GV;
2758 }
2759 
2760 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
2761 /// constant array for the given string literal.
2762 llvm::GlobalVariable *
2763 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
2764                                                   StringRef Name) {
2765   auto Alignment =
2766       getContext().getAlignOfGlobalVarInChars(S->getType()).getQuantity();
2767 
2768   llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
2769   llvm::GlobalVariable **Entry = nullptr;
2770   if (!LangOpts.WritableStrings) {
2771     Entry = &ConstantStringMap[C];
2772     if (auto GV = *Entry) {
2773       if (Alignment > GV->getAlignment())
2774         GV->setAlignment(Alignment);
2775       return GV;
2776     }
2777   }
2778 
2779   SmallString<256> MangledNameBuffer;
2780   StringRef GlobalVariableName;
2781   llvm::GlobalValue::LinkageTypes LT;
2782 
2783   // Mangle the string literal if the ABI allows for it.  However, we cannot
2784   // do this if  we are compiling with ASan or -fwritable-strings because they
2785   // rely on strings having normal linkage.
2786   if (!LangOpts.WritableStrings && !LangOpts.Sanitize.Address &&
2787       getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) {
2788     llvm::raw_svector_ostream Out(MangledNameBuffer);
2789     getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
2790     Out.flush();
2791 
2792     LT = llvm::GlobalValue::LinkOnceODRLinkage;
2793     GlobalVariableName = MangledNameBuffer;
2794   } else {
2795     LT = llvm::GlobalValue::PrivateLinkage;
2796     GlobalVariableName = Name;
2797   }
2798 
2799   auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
2800   if (Entry)
2801     *Entry = GV;
2802 
2803   SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>");
2804   return GV;
2805 }
2806 
2807 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
2808 /// array for the given ObjCEncodeExpr node.
2809 llvm::GlobalVariable *
2810 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
2811   std::string Str;
2812   getContext().getObjCEncodingForType(E->getEncodedType(), Str);
2813 
2814   return GetAddrOfConstantCString(Str);
2815 }
2816 
2817 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
2818 /// the literal and a terminating '\0' character.
2819 /// The result has pointer to array type.
2820 llvm::GlobalVariable *CodeGenModule::GetAddrOfConstantCString(
2821     const std::string &Str, const char *GlobalName, unsigned Alignment) {
2822   StringRef StrWithNull(Str.c_str(), Str.size() + 1);
2823   if (Alignment == 0) {
2824     Alignment = getContext()
2825                     .getAlignOfGlobalVarInChars(getContext().CharTy)
2826                     .getQuantity();
2827   }
2828 
2829   llvm::Constant *C =
2830       llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
2831 
2832   // Don't share any string literals if strings aren't constant.
2833   llvm::GlobalVariable **Entry = nullptr;
2834   if (!LangOpts.WritableStrings) {
2835     Entry = &ConstantStringMap[C];
2836     if (auto GV = *Entry) {
2837       if (Alignment > GV->getAlignment())
2838         GV->setAlignment(Alignment);
2839       return GV;
2840     }
2841   }
2842 
2843   // Get the default prefix if a name wasn't specified.
2844   if (!GlobalName)
2845     GlobalName = ".str";
2846   // Create a global variable for this.
2847   auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
2848                                   GlobalName, Alignment);
2849   if (Entry)
2850     *Entry = GV;
2851   return GV;
2852 }
2853 
2854 llvm::Constant *CodeGenModule::GetAddrOfGlobalTemporary(
2855     const MaterializeTemporaryExpr *E, const Expr *Init) {
2856   assert((E->getStorageDuration() == SD_Static ||
2857           E->getStorageDuration() == SD_Thread) && "not a global temporary");
2858   const auto *VD = cast<VarDecl>(E->getExtendingDecl());
2859 
2860   // If we're not materializing a subobject of the temporary, keep the
2861   // cv-qualifiers from the type of the MaterializeTemporaryExpr.
2862   QualType MaterializedType = Init->getType();
2863   if (Init == E->GetTemporaryExpr())
2864     MaterializedType = E->getType();
2865 
2866   llvm::Constant *&Slot = MaterializedGlobalTemporaryMap[E];
2867   if (Slot)
2868     return Slot;
2869 
2870   // FIXME: If an externally-visible declaration extends multiple temporaries,
2871   // we need to give each temporary the same name in every translation unit (and
2872   // we also need to make the temporaries externally-visible).
2873   SmallString<256> Name;
2874   llvm::raw_svector_ostream Out(Name);
2875   getCXXABI().getMangleContext().mangleReferenceTemporary(
2876       VD, E->getManglingNumber(), Out);
2877   Out.flush();
2878 
2879   APValue *Value = nullptr;
2880   if (E->getStorageDuration() == SD_Static) {
2881     // We might have a cached constant initializer for this temporary. Note
2882     // that this might have a different value from the value computed by
2883     // evaluating the initializer if the surrounding constant expression
2884     // modifies the temporary.
2885     Value = getContext().getMaterializedTemporaryValue(E, false);
2886     if (Value && Value->isUninit())
2887       Value = nullptr;
2888   }
2889 
2890   // Try evaluating it now, it might have a constant initializer.
2891   Expr::EvalResult EvalResult;
2892   if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
2893       !EvalResult.hasSideEffects())
2894     Value = &EvalResult.Val;
2895 
2896   llvm::Constant *InitialValue = nullptr;
2897   bool Constant = false;
2898   llvm::Type *Type;
2899   if (Value) {
2900     // The temporary has a constant initializer, use it.
2901     InitialValue = EmitConstantValue(*Value, MaterializedType, nullptr);
2902     Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
2903     Type = InitialValue->getType();
2904   } else {
2905     // No initializer, the initialization will be provided when we
2906     // initialize the declaration which performed lifetime extension.
2907     Type = getTypes().ConvertTypeForMem(MaterializedType);
2908   }
2909 
2910   // Create a global variable for this lifetime-extended temporary.
2911   llvm::GlobalValue::LinkageTypes Linkage =
2912       getLLVMLinkageVarDefinition(VD, Constant);
2913   // There is no need for this temporary to have global linkage if the global
2914   // variable has external linkage.
2915   if (Linkage == llvm::GlobalVariable::ExternalLinkage)
2916     Linkage = llvm::GlobalVariable::PrivateLinkage;
2917   unsigned AddrSpace = GetGlobalVarAddressSpace(
2918       VD, getContext().getTargetAddressSpace(MaterializedType));
2919   auto *GV = new llvm::GlobalVariable(
2920       getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
2921       /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal,
2922       AddrSpace);
2923   setGlobalVisibility(GV, VD);
2924   GV->setAlignment(
2925       getContext().getTypeAlignInChars(MaterializedType).getQuantity());
2926   if (VD->getTLSKind())
2927     setTLSMode(GV, *VD);
2928   Slot = GV;
2929   return GV;
2930 }
2931 
2932 /// EmitObjCPropertyImplementations - Emit information for synthesized
2933 /// properties for an implementation.
2934 void CodeGenModule::EmitObjCPropertyImplementations(const
2935                                                     ObjCImplementationDecl *D) {
2936   for (const auto *PID : D->property_impls()) {
2937     // Dynamic is just for type-checking.
2938     if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2939       ObjCPropertyDecl *PD = PID->getPropertyDecl();
2940 
2941       // Determine which methods need to be implemented, some may have
2942       // been overridden. Note that ::isPropertyAccessor is not the method
2943       // we want, that just indicates if the decl came from a
2944       // property. What we want to know is if the method is defined in
2945       // this implementation.
2946       if (!D->getInstanceMethod(PD->getGetterName()))
2947         CodeGenFunction(*this).GenerateObjCGetter(
2948                                  const_cast<ObjCImplementationDecl *>(D), PID);
2949       if (!PD->isReadOnly() &&
2950           !D->getInstanceMethod(PD->getSetterName()))
2951         CodeGenFunction(*this).GenerateObjCSetter(
2952                                  const_cast<ObjCImplementationDecl *>(D), PID);
2953     }
2954   }
2955 }
2956 
2957 static bool needsDestructMethod(ObjCImplementationDecl *impl) {
2958   const ObjCInterfaceDecl *iface = impl->getClassInterface();
2959   for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
2960        ivar; ivar = ivar->getNextIvar())
2961     if (ivar->getType().isDestructedType())
2962       return true;
2963 
2964   return false;
2965 }
2966 
2967 /// EmitObjCIvarInitializations - Emit information for ivar initialization
2968 /// for an implementation.
2969 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
2970   // We might need a .cxx_destruct even if we don't have any ivar initializers.
2971   if (needsDestructMethod(D)) {
2972     IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
2973     Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
2974     ObjCMethodDecl *DTORMethod =
2975       ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(),
2976                              cxxSelector, getContext().VoidTy, nullptr, D,
2977                              /*isInstance=*/true, /*isVariadic=*/false,
2978                           /*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true,
2979                              /*isDefined=*/false, ObjCMethodDecl::Required);
2980     D->addInstanceMethod(DTORMethod);
2981     CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
2982     D->setHasDestructors(true);
2983   }
2984 
2985   // If the implementation doesn't have any ivar initializers, we don't need
2986   // a .cxx_construct.
2987   if (D->getNumIvarInitializers() == 0)
2988     return;
2989 
2990   IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
2991   Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
2992   // The constructor returns 'self'.
2993   ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
2994                                                 D->getLocation(),
2995                                                 D->getLocation(),
2996                                                 cxxSelector,
2997                                                 getContext().getObjCIdType(),
2998                                                 nullptr, D, /*isInstance=*/true,
2999                                                 /*isVariadic=*/false,
3000                                                 /*isPropertyAccessor=*/true,
3001                                                 /*isImplicitlyDeclared=*/true,
3002                                                 /*isDefined=*/false,
3003                                                 ObjCMethodDecl::Required);
3004   D->addInstanceMethod(CTORMethod);
3005   CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
3006   D->setHasNonZeroConstructors(true);
3007 }
3008 
3009 /// EmitNamespace - Emit all declarations in a namespace.
3010 void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
3011   for (auto *I : ND->decls()) {
3012     if (const auto *VD = dyn_cast<VarDecl>(I))
3013       if (VD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
3014           VD->getTemplateSpecializationKind() != TSK_Undeclared)
3015         continue;
3016     EmitTopLevelDecl(I);
3017   }
3018 }
3019 
3020 // EmitLinkageSpec - Emit all declarations in a linkage spec.
3021 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
3022   if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
3023       LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
3024     ErrorUnsupported(LSD, "linkage spec");
3025     return;
3026   }
3027 
3028   for (auto *I : LSD->decls()) {
3029     // Meta-data for ObjC class includes references to implemented methods.
3030     // Generate class's method definitions first.
3031     if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
3032       for (auto *M : OID->methods())
3033         EmitTopLevelDecl(M);
3034     }
3035     EmitTopLevelDecl(I);
3036   }
3037 }
3038 
3039 /// EmitTopLevelDecl - Emit code for a single top level declaration.
3040 void CodeGenModule::EmitTopLevelDecl(Decl *D) {
3041   // Ignore dependent declarations.
3042   if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
3043     return;
3044 
3045   switch (D->getKind()) {
3046   case Decl::CXXConversion:
3047   case Decl::CXXMethod:
3048   case Decl::Function:
3049     // Skip function templates
3050     if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
3051         cast<FunctionDecl>(D)->isLateTemplateParsed())
3052       return;
3053 
3054     EmitGlobal(cast<FunctionDecl>(D));
3055     // Always provide some coverage mapping
3056     // even for the functions that aren't emitted.
3057     AddDeferredUnusedCoverageMapping(D);
3058     break;
3059 
3060   case Decl::Var:
3061     // Skip variable templates
3062     if (cast<VarDecl>(D)->getDescribedVarTemplate())
3063       return;
3064   case Decl::VarTemplateSpecialization:
3065     EmitGlobal(cast<VarDecl>(D));
3066     break;
3067 
3068   // Indirect fields from global anonymous structs and unions can be
3069   // ignored; only the actual variable requires IR gen support.
3070   case Decl::IndirectField:
3071     break;
3072 
3073   // C++ Decls
3074   case Decl::Namespace:
3075     EmitNamespace(cast<NamespaceDecl>(D));
3076     break;
3077     // No code generation needed.
3078   case Decl::UsingShadow:
3079   case Decl::ClassTemplate:
3080   case Decl::VarTemplate:
3081   case Decl::VarTemplatePartialSpecialization:
3082   case Decl::FunctionTemplate:
3083   case Decl::TypeAliasTemplate:
3084   case Decl::Block:
3085   case Decl::Empty:
3086     break;
3087   case Decl::Using:          // using X; [C++]
3088     if (CGDebugInfo *DI = getModuleDebugInfo())
3089         DI->EmitUsingDecl(cast<UsingDecl>(*D));
3090     return;
3091   case Decl::NamespaceAlias:
3092     if (CGDebugInfo *DI = getModuleDebugInfo())
3093         DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
3094     return;
3095   case Decl::UsingDirective: // using namespace X; [C++]
3096     if (CGDebugInfo *DI = getModuleDebugInfo())
3097       DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
3098     return;
3099   case Decl::CXXConstructor:
3100     // Skip function templates
3101     if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
3102         cast<FunctionDecl>(D)->isLateTemplateParsed())
3103       return;
3104 
3105     getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
3106     break;
3107   case Decl::CXXDestructor:
3108     if (cast<FunctionDecl>(D)->isLateTemplateParsed())
3109       return;
3110     getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
3111     break;
3112 
3113   case Decl::StaticAssert:
3114     // Nothing to do.
3115     break;
3116 
3117   // Objective-C Decls
3118 
3119   // Forward declarations, no (immediate) code generation.
3120   case Decl::ObjCInterface:
3121   case Decl::ObjCCategory:
3122     break;
3123 
3124   case Decl::ObjCProtocol: {
3125     auto *Proto = cast<ObjCProtocolDecl>(D);
3126     if (Proto->isThisDeclarationADefinition())
3127       ObjCRuntime->GenerateProtocol(Proto);
3128     break;
3129   }
3130 
3131   case Decl::ObjCCategoryImpl:
3132     // Categories have properties but don't support synthesize so we
3133     // can ignore them here.
3134     ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
3135     break;
3136 
3137   case Decl::ObjCImplementation: {
3138     auto *OMD = cast<ObjCImplementationDecl>(D);
3139     EmitObjCPropertyImplementations(OMD);
3140     EmitObjCIvarInitializations(OMD);
3141     ObjCRuntime->GenerateClass(OMD);
3142     // Emit global variable debug information.
3143     if (CGDebugInfo *DI = getModuleDebugInfo())
3144       if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
3145         DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
3146             OMD->getClassInterface()), OMD->getLocation());
3147     break;
3148   }
3149   case Decl::ObjCMethod: {
3150     auto *OMD = cast<ObjCMethodDecl>(D);
3151     // If this is not a prototype, emit the body.
3152     if (OMD->getBody())
3153       CodeGenFunction(*this).GenerateObjCMethod(OMD);
3154     break;
3155   }
3156   case Decl::ObjCCompatibleAlias:
3157     ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
3158     break;
3159 
3160   case Decl::LinkageSpec:
3161     EmitLinkageSpec(cast<LinkageSpecDecl>(D));
3162     break;
3163 
3164   case Decl::FileScopeAsm: {
3165     auto *AD = cast<FileScopeAsmDecl>(D);
3166     StringRef AsmString = AD->getAsmString()->getString();
3167 
3168     const std::string &S = getModule().getModuleInlineAsm();
3169     if (S.empty())
3170       getModule().setModuleInlineAsm(AsmString);
3171     else if (S.end()[-1] == '\n')
3172       getModule().setModuleInlineAsm(S + AsmString.str());
3173     else
3174       getModule().setModuleInlineAsm(S + '\n' + AsmString.str());
3175     break;
3176   }
3177 
3178   case Decl::Import: {
3179     auto *Import = cast<ImportDecl>(D);
3180 
3181     // Ignore import declarations that come from imported modules.
3182     if (clang::Module *Owner = Import->getOwningModule()) {
3183       if (getLangOpts().CurrentModule.empty() ||
3184           Owner->getTopLevelModule()->Name == getLangOpts().CurrentModule)
3185         break;
3186     }
3187 
3188     ImportedModules.insert(Import->getImportedModule());
3189     break;
3190   }
3191 
3192   case Decl::ClassTemplateSpecialization: {
3193     const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
3194     if (DebugInfo &&
3195         Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition)
3196       DebugInfo->completeTemplateDefinition(*Spec);
3197   }
3198 
3199   default:
3200     // Make sure we handled everything we should, every other kind is a
3201     // non-top-level decl.  FIXME: Would be nice to have an isTopLevelDeclKind
3202     // function. Need to recode Decl::Kind to do that easily.
3203     assert(isa<TypeDecl>(D) && "Unsupported decl kind");
3204   }
3205 }
3206 
3207 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
3208   // Do we need to generate coverage mapping?
3209   if (!CodeGenOpts.CoverageMapping)
3210     return;
3211   switch (D->getKind()) {
3212   case Decl::CXXConversion:
3213   case Decl::CXXMethod:
3214   case Decl::Function:
3215   case Decl::ObjCMethod:
3216   case Decl::CXXConstructor:
3217   case Decl::CXXDestructor: {
3218     if (!cast<FunctionDecl>(D)->hasBody())
3219       return;
3220     auto I = DeferredEmptyCoverageMappingDecls.find(D);
3221     if (I == DeferredEmptyCoverageMappingDecls.end())
3222       DeferredEmptyCoverageMappingDecls[D] = true;
3223     break;
3224   }
3225   default:
3226     break;
3227   };
3228 }
3229 
3230 void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) {
3231   // Do we need to generate coverage mapping?
3232   if (!CodeGenOpts.CoverageMapping)
3233     return;
3234   if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
3235     if (Fn->isTemplateInstantiation())
3236       ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
3237   }
3238   auto I = DeferredEmptyCoverageMappingDecls.find(D);
3239   if (I == DeferredEmptyCoverageMappingDecls.end())
3240     DeferredEmptyCoverageMappingDecls[D] = false;
3241   else
3242     I->second = false;
3243 }
3244 
3245 void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
3246   std::vector<const Decl *> DeferredDecls;
3247   for (const auto I : DeferredEmptyCoverageMappingDecls) {
3248     if (!I.second)
3249       continue;
3250     DeferredDecls.push_back(I.first);
3251   }
3252   // Sort the declarations by their location to make sure that the tests get a
3253   // predictable order for the coverage mapping for the unused declarations.
3254   if (CodeGenOpts.DumpCoverageMapping)
3255     std::sort(DeferredDecls.begin(), DeferredDecls.end(),
3256               [] (const Decl *LHS, const Decl *RHS) {
3257       return LHS->getLocStart() < RHS->getLocStart();
3258     });
3259   for (const auto *D : DeferredDecls) {
3260     switch (D->getKind()) {
3261     case Decl::CXXConversion:
3262     case Decl::CXXMethod:
3263     case Decl::Function:
3264     case Decl::ObjCMethod: {
3265       CodeGenPGO PGO(*this);
3266       GlobalDecl GD(cast<FunctionDecl>(D));
3267       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
3268                                   getFunctionLinkage(GD));
3269       break;
3270     }
3271     case Decl::CXXConstructor: {
3272       CodeGenPGO PGO(*this);
3273       GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
3274       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
3275                                   getFunctionLinkage(GD));
3276       break;
3277     }
3278     case Decl::CXXDestructor: {
3279       CodeGenPGO PGO(*this);
3280       GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
3281       PGO.emitEmptyCounterMapping(D, getMangledName(GD),
3282                                   getFunctionLinkage(GD));
3283       break;
3284     }
3285     default:
3286       break;
3287     };
3288   }
3289 }
3290 
3291 /// Turns the given pointer into a constant.
3292 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
3293                                           const void *Ptr) {
3294   uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
3295   llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
3296   return llvm::ConstantInt::get(i64, PtrInt);
3297 }
3298 
3299 static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
3300                                    llvm::NamedMDNode *&GlobalMetadata,
3301                                    GlobalDecl D,
3302                                    llvm::GlobalValue *Addr) {
3303   if (!GlobalMetadata)
3304     GlobalMetadata =
3305       CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
3306 
3307   // TODO: should we report variant information for ctors/dtors?
3308   llvm::Value *Ops[] = {
3309     Addr,
3310     GetPointerConstant(CGM.getLLVMContext(), D.getDecl())
3311   };
3312   GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
3313 }
3314 
3315 /// For each function which is declared within an extern "C" region and marked
3316 /// as 'used', but has internal linkage, create an alias from the unmangled
3317 /// name to the mangled name if possible. People expect to be able to refer
3318 /// to such functions with an unmangled name from inline assembly within the
3319 /// same translation unit.
3320 void CodeGenModule::EmitStaticExternCAliases() {
3321   for (StaticExternCMap::iterator I = StaticExternCValues.begin(),
3322                                   E = StaticExternCValues.end();
3323        I != E; ++I) {
3324     IdentifierInfo *Name = I->first;
3325     llvm::GlobalValue *Val = I->second;
3326     if (Val && !getModule().getNamedValue(Name->getName()))
3327       addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
3328   }
3329 }
3330 
3331 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
3332                                              GlobalDecl &Result) const {
3333   auto Res = Manglings.find(MangledName);
3334   if (Res == Manglings.end())
3335     return false;
3336   Result = Res->getValue();
3337   return true;
3338 }
3339 
3340 /// Emits metadata nodes associating all the global values in the
3341 /// current module with the Decls they came from.  This is useful for
3342 /// projects using IR gen as a subroutine.
3343 ///
3344 /// Since there's currently no way to associate an MDNode directly
3345 /// with an llvm::GlobalValue, we create a global named metadata
3346 /// with the name 'clang.global.decl.ptrs'.
3347 void CodeGenModule::EmitDeclMetadata() {
3348   llvm::NamedMDNode *GlobalMetadata = nullptr;
3349 
3350   // StaticLocalDeclMap
3351   for (auto &I : MangledDeclNames) {
3352     llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
3353     EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
3354   }
3355 }
3356 
3357 /// Emits metadata nodes for all the local variables in the current
3358 /// function.
3359 void CodeGenFunction::EmitDeclMetadata() {
3360   if (LocalDeclMap.empty()) return;
3361 
3362   llvm::LLVMContext &Context = getLLVMContext();
3363 
3364   // Find the unique metadata ID for this name.
3365   unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
3366 
3367   llvm::NamedMDNode *GlobalMetadata = nullptr;
3368 
3369   for (auto &I : LocalDeclMap) {
3370     const Decl *D = I.first;
3371     llvm::Value *Addr = I.second;
3372     if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
3373       llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
3374       Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, DAddr));
3375     } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
3376       GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
3377       EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
3378     }
3379   }
3380 }
3381 
3382 void CodeGenModule::EmitVersionIdentMetadata() {
3383   llvm::NamedMDNode *IdentMetadata =
3384     TheModule.getOrInsertNamedMetadata("llvm.ident");
3385   std::string Version = getClangFullVersion();
3386   llvm::LLVMContext &Ctx = TheModule.getContext();
3387 
3388   llvm::Value *IdentNode[] = {
3389     llvm::MDString::get(Ctx, Version)
3390   };
3391   IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
3392 }
3393 
3394 void CodeGenModule::EmitTargetMetadata() {
3395   // Warning, new MangledDeclNames may be appended within this loop.
3396   // We rely on MapVector insertions adding new elements to the end
3397   // of the container.
3398   // FIXME: Move this loop into the one target that needs it, and only
3399   // loop over those declarations for which we couldn't emit the target
3400   // metadata when we emitted the declaration.
3401   for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
3402     auto Val = *(MangledDeclNames.begin() + I);
3403     const Decl *D = Val.first.getDecl()->getMostRecentDecl();
3404     llvm::GlobalValue *GV = GetGlobalValue(Val.second);
3405     getTargetCodeGenInfo().emitTargetMD(D, GV, *this);
3406   }
3407 }
3408 
3409 void CodeGenModule::EmitCoverageFile() {
3410   if (!getCodeGenOpts().CoverageFile.empty()) {
3411     if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) {
3412       llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
3413       llvm::LLVMContext &Ctx = TheModule.getContext();
3414       llvm::MDString *CoverageFile =
3415           llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile);
3416       for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
3417         llvm::MDNode *CU = CUNode->getOperand(i);
3418         llvm::Value *node[] = { CoverageFile, CU };
3419         llvm::MDNode *N = llvm::MDNode::get(Ctx, node);
3420         GCov->addOperand(N);
3421       }
3422     }
3423   }
3424 }
3425 
3426 llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) {
3427   // Sema has checked that all uuid strings are of the form
3428   // "12345678-1234-1234-1234-1234567890ab".
3429   assert(Uuid.size() == 36);
3430   for (unsigned i = 0; i < 36; ++i) {
3431     if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-');
3432     else                                         assert(isHexDigit(Uuid[i]));
3433   }
3434 
3435   // The starts of all bytes of Field3 in Uuid. Field 3 is "1234-1234567890ab".
3436   const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 };
3437 
3438   llvm::Constant *Field3[8];
3439   for (unsigned Idx = 0; Idx < 8; ++Idx)
3440     Field3[Idx] = llvm::ConstantInt::get(
3441         Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16);
3442 
3443   llvm::Constant *Fields[4] = {
3444     llvm::ConstantInt::get(Int32Ty, Uuid.substr(0,  8), 16),
3445     llvm::ConstantInt::get(Int16Ty, Uuid.substr(9,  4), 16),
3446     llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16),
3447     llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3)
3448   };
3449 
3450   return llvm::ConstantStruct::getAnon(Fields);
3451 }
3452 
3453 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
3454                                                        bool ForEH) {
3455   // Return a bogus pointer if RTTI is disabled, unless it's for EH.
3456   // FIXME: should we even be calling this method if RTTI is disabled
3457   // and it's not for EH?
3458   if (!ForEH && !getLangOpts().RTTI)
3459     return llvm::Constant::getNullValue(Int8PtrTy);
3460 
3461   if (ForEH && Ty->isObjCObjectPointerType() &&
3462       LangOpts.ObjCRuntime.isGNUFamily())
3463     return ObjCRuntime->GetEHType(Ty);
3464 
3465   return getCXXABI().getAddrOfRTTIDescriptor(Ty);
3466 }
3467 
3468