[lld] 99a2d94 - [ELF] Speed up/simplify removeUnusedSyntheticSections. NFC
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 28 21:07:39 PST 2021
Author: Fangrui Song
Date: 2021-11-28T21:07:34-08:00
New Revision: 99a2d940dd77a9c3699c71024b38baee98df8292
URL: https://github.com/llvm/llvm-project/commit/99a2d940dd77a9c3699c71024b38baee98df8292
DIFF: https://github.com/llvm/llvm-project/commit/99a2d940dd77a9c3699c71024b38baee98df8292.diff
LOG: [ELF] Speed up/simplify removeUnusedSyntheticSections. NFC
Make one change: when the OutputSection is nullptr (due to /DISCARD/ or garbage
collected BssSection (replaceCommonSymbols)), discard the SyntheticSection as well.
Added:
Modified:
lld/ELF/Writer.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 42e63ee0e98a..77a6626e8989 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1836,33 +1836,26 @@ static void removeUnusedSyntheticSections() {
})
.base();
- DenseSet<InputSectionDescription *> isdSet;
// Mark unused synthetic sections for deletion
- auto end = std::stable_partition(
- start, inputSections.end(), [&](InputSectionBase *s) {
- SyntheticSection *ss = dyn_cast<SyntheticSection>(s);
- OutputSection *os = ss->getParent();
- if (!os || ss->isNeeded())
- return true;
-
- // If we reach here, then ss is an unused synthetic section and we want
- // to remove it from the corresponding input section description, and
- // orphanSections.
- for (SectionCommand *b : os->commands)
- if (auto *isd = dyn_cast<InputSectionDescription>(b))
- isdSet.insert(isd);
-
- llvm::erase_if(
- script->orphanSections,
- [=](const InputSectionBase *isec) { return isec == ss; });
-
- return false;
- });
-
+ auto end = std::stable_partition(start, inputSections.end(),
+ [&](InputSectionBase *s) {
+ auto *sec = cast<SyntheticSection>(s);
+ return sec->getParent() && sec->isNeeded();
+ });
+
+ // Remove unused synthetic sections from the corresponding input section
+ // description and orphanSections.
DenseSet<InputSectionBase *> unused(end, inputSections.end());
- for (auto *isd : isdSet)
- llvm::erase_if(isd->sections,
- [=](InputSection *isec) { return unused.count(isec); });
+ for (auto it = end; it != inputSections.end(); ++it)
+ if (OutputSection *osec = cast<SyntheticSection>(*it)->getParent())
+ for (SectionCommand *cmd : osec->commands)
+ if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
+ llvm::erase_if(isd->sections, [&](InputSection *isec) {
+ return unused.count(isec);
+ });
+ llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) {
+ return unused.count(sec);
+ });
// Erase unused synthetic sections.
inputSections.erase(end, inputSections.end());
More information about the llvm-commits
mailing list