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