[lld] 4e8116f - [ELF] Refactor uses of getInputSections to improve efficiency NFC

Andrew Ng via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 21 04:36:43 PST 2020


Author: Andrew Ng
Date: 2020-01-21T12:27:52Z
New Revision: 4e8116f4692e9b5787ba91c6b557b113aae7e244

URL: https://github.com/llvm/llvm-project/commit/4e8116f4692e9b5787ba91c6b557b113aae7e244
DIFF: https://github.com/llvm/llvm-project/commit/4e8116f4692e9b5787ba91c6b557b113aae7e244.diff

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

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

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index aaa7744a7809..db9794319d5d 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -953,7 +953,7 @@ void LinkerScript::adjustSectionsBeforeSorting() {
 
     // 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);

diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 6142cb0783ce..e5360c1ce23a 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -357,8 +357,7 @@ static void finalizeShtGroup(OutputSection *os,
 }
 
 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,7 +465,15 @@ int getPriority(StringRef s) {
   return v;
 }
 
-std::vector<InputSection *> getInputSections(OutputSection *os) {
+InputSection *getFirstInputSection(const OutputSection *os) {
+  for (BaseCommand *base : os->sectionCommands)
+    if (auto *isd = dyn_cast<InputSectionDescription>(base))
+      if (!isd->sections.empty())
+        return isd->sections[0];
+  return nullptr;
+}
+
+std::vector<InputSection *> getInputSections(const OutputSection *os) {
   std::vector<InputSection *> ret;
   for (BaseCommand *base : os->sectionCommands)
     if (auto *isd = dyn_cast<InputSectionDescription>(base))

diff  --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h
index a24294eedf35..d5686f11ec8e 100644
--- a/lld/ELF/OutputSections.h
+++ b/lld/ELF/OutputSections.h
@@ -118,7 +118,8 @@ class OutputSection final : public BaseCommand, public SectionBase {
 
 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


        


More information about the llvm-commits mailing list