diff options
Diffstat (limited to 'lib/Bitcode/Reader')
| -rw-r--r-- | lib/Bitcode/Reader/MetadataLoader.cpp | 56 | 
1 files changed, 51 insertions, 5 deletions
| diff --git a/lib/Bitcode/Reader/MetadataLoader.cpp b/lib/Bitcode/Reader/MetadataLoader.cpp index 145b9c50367a..b89f5be4a369 100644 --- a/lib/Bitcode/Reader/MetadataLoader.cpp +++ b/lib/Bitcode/Reader/MetadataLoader.cpp @@ -448,6 +448,7 @@ class MetadataLoader::MetadataLoaderImpl {    bool StripTBAA = false;    bool HasSeenOldLoopTags = false; +  bool NeedUpgradeToDIGlobalVariableExpression = false;    /// True if metadata is being parsed for a module being ThinLTO imported.    bool IsImporting = false; @@ -473,6 +474,45 @@ class MetadataLoader::MetadataLoaderImpl {      CUSubprograms.clear();    } +  /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions. +  void upgradeCUVariables() { +    if (!NeedUpgradeToDIGlobalVariableExpression) +      return; + +    // Upgrade list of variables attached to the CUs. +    if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu")) +      for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) { +        auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I)); +        if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables())) +          for (unsigned I = 0; I < GVs->getNumOperands(); I++) +            if (auto *GV = +                    dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) { +              auto *DGVE = +                  DIGlobalVariableExpression::getDistinct(Context, GV, nullptr); +              GVs->replaceOperandWith(I, DGVE); +            } +      } + +    // Upgrade variables attached to globals. +    for (auto &GV : TheModule.globals()) { +      SmallVector<MDNode *, 1> MDs, NewMDs; +      GV.getMetadata(LLVMContext::MD_dbg, MDs); +      GV.eraseMetadata(LLVMContext::MD_dbg); +      for (auto *MD : MDs) +        if (auto *DGV = dyn_cast_or_null<DIGlobalVariable>(MD)) { +          auto *DGVE = +              DIGlobalVariableExpression::getDistinct(Context, DGV, nullptr); +          GV.addMetadata(LLVMContext::MD_dbg, *DGVE); +        } else +          GV.addMetadata(LLVMContext::MD_dbg, *MD); +    } +  } + +  void upgradeDebugInfo() { +    upgradeCUSubprograms(); +    upgradeCUVariables(); +  } +  public:    MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,                       BitcodeReaderValueList &ValueList, @@ -726,7 +766,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {        // Reading the named metadata created forward references and/or        // placeholders, that we flush here.        resolveForwardRefsAndPlaceholders(Placeholders); -      upgradeCUSubprograms(); +      upgradeDebugInfo();        // Return at the beginning of the block, since it is easy to skip it        // entirely from there.        Stream.ReadBlockEnd(); // Pop the abbrev block context. @@ -750,7 +790,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {        return error("Malformed block");      case BitstreamEntry::EndBlock:        resolveForwardRefsAndPlaceholders(Placeholders); -      upgradeCUSubprograms(); +      upgradeDebugInfo();        return Error::success();      case BitstreamEntry::Record:        // The interesting case. @@ -1420,11 +1460,17 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(             getDITypeRefOrNull(Record[6]), Record[7], Record[8],             getMDOrNull(Record[10]), AlignInBits)); -      auto *DGVE = DIGlobalVariableExpression::getDistinct(Context, DGV, Expr); -      MetadataList.assignValue(DGVE, NextMetadataNo); -      NextMetadataNo++; +      DIGlobalVariableExpression *DGVE = nullptr; +      if (Attach || Expr) +        DGVE = DIGlobalVariableExpression::getDistinct(Context, DGV, Expr); +      else +        NeedUpgradeToDIGlobalVariableExpression = true;        if (Attach)          Attach->addDebugInfo(DGVE); + +      auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV); +      MetadataList.assignValue(MDNode, NextMetadataNo); +      NextMetadataNo++;      } else        return error("Invalid record"); | 
