[llvm] 345a03b - [nfc] Factoring out utility that can be used for other object-level tools

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 10 12:51:36 PDT 2023


Author: Dayann D'almeida
Date: 2023-07-10T12:51:15-07:00
New Revision: 345a03b29531b6353f4c028eb793a554a0d57386

URL: https://github.com/llvm/llvm-project/commit/345a03b29531b6353f4c028eb793a554a0d57386
DIFF: https://github.com/llvm/llvm-project/commit/345a03b29531b6353f4c028eb793a554a0d57386.diff

LOG: [nfc] Factoring out utility that can be used for other object-level tools

Related rfc can be found at https://discourse.llvm.org/t/rfc-llvm-cm-cost-model-evaluation-for-object-files-machine-code/71502. We want to reuse the instruction iterator for this tool.

Reviewed By: mtrofin, kazu, jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D152869

Added: 
    

Modified: 
    llvm/include/llvm/Object/ObjectFile.h
    llvm/tools/llvm-objdump/llvm-objdump.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Object/ObjectFile.h b/llvm/include/llvm/Object/ObjectFile.h
index 684e7b500478bf..2b614185c694b6 100644
--- a/llvm/include/llvm/Object/ObjectFile.h
+++ b/llvm/include/llvm/Object/ObjectFile.h
@@ -46,6 +46,7 @@ class WasmObjectFile;
 
 using section_iterator = content_iterator<SectionRef>;
 
+typedef std::function<bool(const SectionRef &)> SectionFilterPredicate;
 /// This is a value type class that represents a single relocation in the list
 /// of relocations in the object file.
 class RelocationRef {
@@ -399,6 +400,57 @@ class ObjectFile : public SymbolicFile {
   createWasmObjectFile(MemoryBufferRef Object);
 };
 
+/// A filtered iterator for SectionRefs that skips sections based on some given
+/// predicate.
+class SectionFilterIterator {
+public:
+  SectionFilterIterator(SectionFilterPredicate Pred,
+                        const section_iterator &Begin,
+                        const section_iterator &End)
+      : Predicate(std::move(Pred)), Iterator(Begin), End(End) {
+    scanPredicate();
+  }
+  const SectionRef &operator*() const { return *Iterator; }
+  SectionFilterIterator &operator++() {
+    ++Iterator;
+    scanPredicate();
+    return *this;
+  }
+  bool operator!=(const SectionFilterIterator &Other) const {
+    return Iterator != Other.Iterator;
+  }
+
+private:
+  void scanPredicate() {
+    while (Iterator != End && !Predicate(*Iterator)) {
+      ++Iterator;
+    }
+  }
+  SectionFilterPredicate Predicate;
+  section_iterator Iterator;
+  section_iterator End;
+};
+
+/// Creates an iterator range of SectionFilterIterators for a given Object and
+/// predicate.
+class SectionFilter {
+public:
+  SectionFilter(SectionFilterPredicate Pred, const ObjectFile &Obj)
+      : Predicate(std::move(Pred)), Object(Obj) {}
+  SectionFilterIterator begin() {
+    return SectionFilterIterator(Predicate, Object.section_begin(),
+                                 Object.section_end());
+  }
+  SectionFilterIterator end() {
+    return SectionFilterIterator(Predicate, Object.section_end(),
+                                 Object.section_end());
+  }
+
+private:
+  SectionFilterPredicate Predicate;
+  const ObjectFile &Object;
+};
+
 // Inline function definitions.
 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
     : BasicSymbolRef(SymbolP, Owner) {}

diff  --git a/llvm/tools/llvm-objdump/llvm-objdump.h b/llvm/tools/llvm-objdump/llvm-objdump.h
index 51dd3aa918d132..7acd892ce8d877 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.h
+++ b/llvm/tools/llvm-objdump/llvm-objdump.h
@@ -14,6 +14,7 @@
 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
 #include "llvm/MC/MCSubtargetInfo.h"
 #include "llvm/Object/Archive.h"
+#include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/FormattedStream.h"
@@ -64,59 +65,6 @@ extern bool UnwindInfo;
 
 extern StringSet<> FoundSectionSet;
 
-typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
-
-/// A filtered iterator for SectionRefs that skips sections based on some given
-/// predicate.
-class SectionFilterIterator {
-public:
-  SectionFilterIterator(FilterPredicate P,
-                        llvm::object::section_iterator const &I,
-                        llvm::object::section_iterator const &E)
-      : Predicate(std::move(P)), Iterator(I), End(E) {
-    ScanPredicate();
-  }
-  const llvm::object::SectionRef &operator*() const { return *Iterator; }
-  SectionFilterIterator &operator++() {
-    ++Iterator;
-    ScanPredicate();
-    return *this;
-  }
-  bool operator!=(SectionFilterIterator const &Other) const {
-    return Iterator != Other.Iterator;
-  }
-
-private:
-  void ScanPredicate() {
-    while (Iterator != End && !Predicate(*Iterator)) {
-      ++Iterator;
-    }
-  }
-  FilterPredicate Predicate;
-  llvm::object::section_iterator Iterator;
-  llvm::object::section_iterator End;
-};
-
-/// Creates an iterator range of SectionFilterIterators for a given Object and
-/// predicate.
-class SectionFilter {
-public:
-  SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
-      : Predicate(std::move(P)), Object(O) {}
-  SectionFilterIterator begin() {
-    return SectionFilterIterator(Predicate, Object.section_begin(),
-                                 Object.section_end());
-  }
-  SectionFilterIterator end() {
-    return SectionFilterIterator(Predicate, Object.section_end(),
-                                 Object.section_end());
-  }
-
-private:
-  FilterPredicate Predicate;
-  llvm::object::ObjectFile const &Object;
-};
-
 // Various helper functions.
 
 /// Creates a SectionFilter with a standard predicate that conditionally skips
@@ -125,8 +73,8 @@ class SectionFilter {
 /// Idx is an optional output parameter that keeps track of which section index
 /// this is. This may be 
diff erent than the actual section number, as some
 /// sections may be filtered (e.g. symbol tables).
-SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O,
-                                uint64_t *Idx = nullptr);
+object::SectionFilter ToolSectionFilter(const llvm::object::ObjectFile &O,
+                                        uint64_t *Idx = nullptr);
 
 bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
 void printRelocations(const object::ObjectFile *O);


        


More information about the llvm-commits mailing list