[PATCH] D73047: [ELF] Refactor uses of getInputSections to improve efficiency NFC

Andrew Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 20 07:49:39 PST 2020


andrewng created this revision.
Herald added subscribers: llvm-commits, MaskRay, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.
andrewng added reviewers: ruiu, MaskRay.

Add new method getFirstInputSection and use instead of getInputSections
where appropriate to avoid creation of an unneeded vector of input
sections.


https://reviews.llvm.org/D73047

Files:
  lld/ELF/LinkerScript.cpp
  lld/ELF/OutputSections.cpp
  lld/ELF/OutputSections.h


Index: lld/ELF/OutputSections.h
===================================================================
--- lld/ELF/OutputSections.h
+++ lld/ELF/OutputSections.h
@@ -118,7 +118,8 @@
 
 int getPriority(StringRef s);
 
-std::vector<InputSection *> getInputSections(OutputSection* os);
+InputSection *getFirstInputSection(const OutputSection *os);
+std::vector<InputSection *> getInputSections(const OutputSection *os);
 
 // All output sections that are handled by the linker specially are
 // globally accessible. Writer initializes them, so don't use them
Index: lld/ELF/OutputSections.cpp
===================================================================
--- lld/ELF/OutputSections.cpp
+++ lld/ELF/OutputSections.cpp
@@ -357,8 +357,7 @@
 }
 
 void OutputSection::finalize() {
-  std::vector<InputSection *> v = getInputSections(this);
-  InputSection *first = v.empty() ? nullptr : v[0];
+  InputSection *first = getFirstInputSection(this);
 
   if (flags & SHF_LINK_ORDER) {
     // We must preserve the link order dependency of sections with the
@@ -466,11 +465,33 @@
   return v;
 }
 
-std::vector<InputSection *> getInputSections(OutputSection *os) {
-  std::vector<InputSection *> ret;
+namespace {
+void forEachInputSectionDescription(
+    const OutputSection *os, function_ref<bool(InputSectionDescription *)> f) {
   for (BaseCommand *base : os->sectionCommands)
     if (auto *isd = dyn_cast<InputSectionDescription>(base))
-      ret.insert(ret.end(), isd->sections.begin(), isd->sections.end());
+      if (!f(isd))
+        break;
+}
+} // namespace
+
+InputSection *getFirstInputSection(const OutputSection *os) {
+  InputSection *ret = nullptr;
+  forEachInputSectionDescription(os, [&](InputSectionDescription *isd) {
+    if (isd->sections.empty())
+      return true;
+    ret = isd->sections[0];
+    return false;
+  });
+  return ret;
+}
+
+std::vector<InputSection *> getInputSections(const OutputSection *os) {
+  std::vector<InputSection *> ret;
+  forEachInputSectionDescription(os, [&](InputSectionDescription *isd) {
+    ret.insert(ret.end(), isd->sections.begin(), isd->sections.end());
+    return true;
+  });
   return ret;
 }
 
Index: lld/ELF/LinkerScript.cpp
===================================================================
--- lld/ELF/LinkerScript.cpp
+++ lld/ELF/LinkerScript.cpp
@@ -948,7 +948,7 @@
 
     // We do not want to keep any special flags for output section
     // in case it is empty.
-    bool isEmpty = getInputSections(sec).empty();
+    bool isEmpty = (getFirstInputSection(sec) == nullptr);
     if (isEmpty)
       sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
                             SHF_WRITE | SHF_EXECINSTR);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73047.239128.patch
Type: text/x-patch
Size: 2696 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200120/8a8cbaf3/attachment.bin>


More information about the llvm-commits mailing list