@@ -26,17 +26,17 @@ template <> struct lld::BPOrdererTraits<struct BPOrdererELF> {
2626};
2727namespace {
2828struct BPOrdererELF : lld::BPOrderer<BPOrdererELF> {
29+ DenseMap<const InputSectionBase *, Defined *> secToSym;
30+
2931 static uint64_t getSize (const Section &sec) { return sec.getSize (); }
3032 static bool isCodeSection (const Section &sec) {
3133 return sec.flags & llvm::ELF::SHF_EXECINSTR;
3234 }
33- static SmallVector<Defined *, 0 > getSymbols (const Section &sec) {
34- SmallVector<Defined *, 0 > symbols;
35- for (auto *sym : sec.file ->getSymbols ())
36- if (auto *d = llvm::dyn_cast_or_null<Defined>(sym))
37- if (d->size > 0 && d->section == &sec)
38- symbols.emplace_back (d);
39- return symbols;
35+ ArrayRef<Defined *> getSymbols (const Section &sec) {
36+ auto it = secToSym.find (&sec);
37+ if (it == secToSym.end ())
38+ return {};
39+ return ArrayRef (it->second );
4040 }
4141
4242 static void
@@ -69,28 +69,27 @@ DenseMap<const InputSectionBase *, int> elf::runBalancedPartitioning(
6969 // Collect candidate sections and associated symbols.
7070 SmallVector<InputSectionBase *> sections;
7171 DenseMap<CachedHashStringRef, DenseSet<unsigned >> rootSymbolToSectionIdxs;
72- DenseSet< const InputSectionBase *> seenSections ;
72+ BPOrdererELF orderer ;
7373
7474 auto addSection = [&](Symbol &sym) {
7575 auto *d = dyn_cast<Defined>(&sym);
76- if (!d || d-> size == 0 )
76+ if (!d)
7777 return ;
7878 auto *sec = dyn_cast_or_null<InputSectionBase>(d->section );
79- if (sec && seenSections. insert (sec).second ) {
80- rootSymbolToSectionIdxs[ CachedHashStringRef ( getRootSymbol (sym. getName ()))]
81- . insert (sections. size ());
82- sections.emplace_back (sec );
83- }
79+ if (! sec || sec-> size == 0 || !orderer. secToSym . try_emplace (sec, d ).second )
80+ return ;
81+ rootSymbolToSectionIdxs[ CachedHashStringRef ( getRootSymbol (sym. getName ()))]
82+ . insert ( sections.size () );
83+ sections. emplace_back (sec);
8484 };
8585
8686 for (Symbol *sym : ctx.symtab ->getSymbols ())
8787 addSection (*sym);
8888 for (ELFFileBase *file : ctx.objectFiles )
8989 for (Symbol *sym : file->getLocalSymbols ())
9090 addSection (*sym);
91-
92- return BPOrdererELF::computeOrder (profilePath, forFunctionCompression,
93- forDataCompression,
94- compressionSortStartupFunctions, verbose,
95- sections, rootSymbolToSectionIdxs);
91+ return orderer.computeOrder (profilePath, forFunctionCompression,
92+ forDataCompression,
93+ compressionSortStartupFunctions, verbose,
94+ sections, rootSymbolToSectionIdxs);
9695}
0 commit comments