DebugInfo: Remove dead DIDescriptor::getDescriptorField()
[oota-llvm.git] / lib / IR / DebugInfo.cpp
index 4c836924d880c926d83596cdac3519abab0d00fd..70a6722568591129b69b732595f2ea3baa994c3a 100644 (file)
 using namespace llvm;
 using namespace llvm::dwarf;
 
-//===----------------------------------------------------------------------===//
-// DIDescriptor
-//===----------------------------------------------------------------------===//
-
-static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
-  if (!DbgNode || Elt >= DbgNode->getNumOperands())
-    return nullptr;
-  return DbgNode->getOperand(Elt);
-}
-
-static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
-  return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
-}
-
-DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
-  MDNode *Field = getNodeField(DbgNode, Elt);
-  return DIDescriptor(Field);
-}
-
 /// \brief Return the size reported by the variable's type.
 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
   DIType Ty = getType().resolve(Map);
@@ -96,35 +77,6 @@ void DIDescriptor::replaceAllUsesWith(MDNode *D) {
   Node->replaceAllUsesWith(D);
 }
 
-#ifndef NDEBUG
-/// \brief Check if a value can be a reference to a type.
-static bool isTypeRef(const Metadata *MD) {
-  if (!MD)
-    return true;
-  if (auto *S = dyn_cast<MDString>(MD))
-    return !S->getString().empty();
-  return isa<MDType>(MD);
-}
-
-/// \brief Check if a value can be a ScopeRef.
-static bool isScopeRef(const Metadata *MD) {
-  if (!MD)
-    return true;
-  if (auto *S = dyn_cast<MDString>(MD))
-    return !S->getString().empty();
-  return isa<MDScope>(MD);
-}
-
-/// \brief Check if a value can be a DescriptorRef.
-static bool isDescriptorRef(const Metadata *MD) {
-  if (!MD)
-    return true;
-  if (auto *S = dyn_cast<MDString>(MD))
-    return !S->getString().empty();
-  return isa<MDNode>(MD);
-}
-#endif
-
 DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
 
 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
@@ -165,17 +117,17 @@ DIScopeRef DIScope::getContext() const {
     return T.getContext();
 
   if (DISubprogram SP = dyn_cast<MDSubprogram>(*this))
-    return DIScopeRef(SP.getContext());
+    return MDScopeRef(SP.getContext());
 
   if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(*this))
-    return DIScopeRef(LB.getContext());
+    return MDScopeRef(LB.getContext());
 
   if (DINameSpace NS = dyn_cast<MDNamespace>(*this))
-    return DIScopeRef(NS.getContext());
+    return MDScopeRef(NS.getContext());
 
   assert((isa<MDFile>(*this) || isa<MDCompileUnit>(*this)) &&
          "Unhandled type of scope.");
-  return DIScopeRef(nullptr);
+  return MDScopeRef();
 }
 
 StringRef DIScope::getName() const {
@@ -206,11 +158,11 @@ StringRef DIScope::getDirectory() const {
 }
 
 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
-  get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
+  get()->replaceSubprograms(MDSubprogramArray(Subprograms));
 }
 
 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
-  get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
+  get()->replaceGlobalVariables(MDGlobalVariableArray(GlobalVariables));
 }
 
 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
@@ -282,10 +234,10 @@ llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
   for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
     DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
     DIArray Retain = CU.getRetainedTypes();
-    for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
-      if (!isa<MDCompositeType>(Retain.getElement(Ti)))
+    for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
+      if (!isa<MDCompositeType>(Retain[Ti]))
         continue;
-      DICompositeType Ty = cast<MDCompositeType>(Retain.getElement(Ti));
+      DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
       if (MDString *TypeId = Ty.getIdentifier()) {
         // Definition has priority over declaration.
         // Try to insert (TypeId, Ty) to Map.
@@ -330,26 +282,19 @@ void DebugInfoFinder::processModule(const Module &M) {
     for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
       DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
       addCompileUnit(CU);
-      DIArray GVs = CU.getGlobalVariables();
-      for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
-        DIGlobalVariable DIG = cast<MDGlobalVariable>(GVs.getElement(i));
+      for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
         if (addGlobalVariable(DIG)) {
           processScope(DIG.getContext());
           processType(DIG.getType().resolve(TypeIdentifierMap));
         }
       }
-      DIArray SPs = CU.getSubprograms();
-      for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
-        processSubprogram(cast<MDSubprogram>(SPs.getElement(i)));
-      DIArray EnumTypes = CU.getEnumTypes();
-      for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
-        processType(cast<MDType>(EnumTypes.getElement(i)));
-      DIArray RetainedTypes = CU.getRetainedTypes();
-      for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
-        processType(cast<MDType>(RetainedTypes.getElement(i)));
-      DIArray Imports = CU.getImportedEntities();
-      for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
-        DIImportedEntity Import = cast<MDImportedEntity>(Imports.getElement(i));
+      for (auto *SP : CU->getSubprograms())
+        processSubprogram(SP);
+      for (auto *ET : CU->getEnumTypes())
+        processType(ET);
+      for (auto *RT : CU->getRetainedTypes())
+        processType(RT);
+      for (DIImportedEntity Import : CU->getImportedEntities()) {
         DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
         if (auto *T = dyn_cast<MDType>(Entity))
           processType(T);
@@ -377,14 +322,11 @@ void DebugInfoFinder::processType(DIType DT) {
   if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
     processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
     if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
-      DITypeArray DTA = ST.getTypeArray();
-      for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
-        processType(DTA.getElement(i).resolve(TypeIdentifierMap));
+      for (MDTypeRef Ref : ST->getTypeArray())
+        processType(Ref.resolve(TypeIdentifierMap));
       return;
     }
-    DIArray DA = DCT.getElements();
-    for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
-      DIDescriptor D = DA.getElement(i);
+    for (Metadata *D : DCT->getElements()->operands()) {
       if (DIType T = dyn_cast<MDType>(D))
         processType(T);
       else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
@@ -424,9 +366,7 @@ void DebugInfoFinder::processSubprogram(DISubprogram SP) {
     return;
   processScope(SP.getContext().resolve(TypeIdentifierMap));
   processType(SP.getType());
-  DIArray TParams = SP.getTemplateParams();
-  for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
-    DIDescriptor Element = TParams.getElement(I);
+  for (auto *Element : SP.getTemplateParams()) {
     if (DITemplateTypeParameter TType =
             dyn_cast<MDTemplateTypeParameter>(Element)) {
       processType(TType.getType().resolve(TypeIdentifierMap));
@@ -576,29 +516,6 @@ void DIVariable::printExtendedName(raw_ostream &OS) const {
   }
 }
 
-template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
-  assert(isDescriptorRef(V) &&
-         "DIDescriptorRef should be a MDString or MDNode");
-}
-template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
-  assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
-}
-template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
-  assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
-}
-
-template <>
-DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
-  return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
-}
-template <>
-DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
-  return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
-}
-template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
-  return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
-}
-
 template <>
 DIDescriptor
 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
@@ -685,9 +602,7 @@ llvm::makeSubprogramMap(const Module &M) {
 
   for (MDNode *N : CU_Nodes->operands()) {
     DICompileUnit CUNode = cast<MDCompileUnit>(N);
-    DIArray SPs = CUNode.getSubprograms();
-    for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
-      DISubprogram SP = cast<MDSubprogram>(SPs.getElement(i));
+    for (DISubprogram SP : CUNode->getSubprograms()) {
       if (Function *F = SP.getFunction())
         R.insert(std::make_pair(F, SP));
     }