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