2015-09-20 05:36:28 +08:00
|
|
|
//===- MarkLive.cpp -------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-09-20 05:36:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Chunks.h"
|
|
|
|
#include "Symbols.h"
|
2018-01-18 03:16:26 +08:00
|
|
|
#include "lld/Common/Timer.h"
|
2015-09-20 05:36:28 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2019-07-12 14:12:27 +08:00
|
|
|
static Timer gctimer("GC", Timer::root());
|
2018-01-18 03:16:26 +08:00
|
|
|
|
2017-05-22 14:01:37 +08:00
|
|
|
// Set live bit on for each reachable chunk. Unmarked (unreachable)
|
|
|
|
// COMDAT chunks will be ignored by Writer, so they will be excluded
|
|
|
|
// from the final output.
|
2017-12-08 09:09:21 +08:00
|
|
|
void markLive(ArrayRef<Chunk *> chunks) {
|
2019-07-12 14:12:27 +08:00
|
|
|
ScopedTimer t(gctimer);
|
2018-01-18 03:16:26 +08:00
|
|
|
|
2015-09-20 05:36:28 +08:00
|
|
|
// We build up a worklist of sections which have been marked as live. We only
|
|
|
|
// push into the worklist when we discover an unmarked section, and we mark
|
|
|
|
// as we push, so sections never appear twice in the list.
|
|
|
|
SmallVector<SectionChunk *, 256> worklist;
|
|
|
|
|
2017-05-22 14:01:37 +08:00
|
|
|
// COMDAT section chunks are dead by default. Add non-COMDAT chunks.
|
2015-09-20 05:36:28 +08:00
|
|
|
for (Chunk *c : chunks)
|
|
|
|
if (auto *sc = dyn_cast<SectionChunk>(c))
|
2018-08-31 15:45:20 +08:00
|
|
|
if (sc->live)
|
2015-09-20 05:36:28 +08:00
|
|
|
worklist.push_back(sc);
|
|
|
|
|
|
|
|
auto enqueue = [&](SectionChunk *c) {
|
2018-08-31 15:45:20 +08:00
|
|
|
if (c->live)
|
2015-09-20 05:36:28 +08:00
|
|
|
return;
|
2018-08-31 15:45:20 +08:00
|
|
|
c->live = true;
|
2015-09-20 05:36:28 +08:00
|
|
|
worklist.push_back(c);
|
|
|
|
};
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
auto addSym = [&](Symbol *b) {
|
2017-05-25 06:30:06 +08:00
|
|
|
if (auto *sym = dyn_cast<DefinedRegular>(b))
|
|
|
|
enqueue(sym->getChunk());
|
|
|
|
else if (auto *sym = dyn_cast<DefinedImportData>(b))
|
|
|
|
sym->file->live = true;
|
|
|
|
else if (auto *sym = dyn_cast<DefinedImportThunk>(b))
|
2018-05-11 03:01:28 +08:00
|
|
|
sym->wrappedSym->file->live = sym->wrappedSym->file->thunkLive = true;
|
2017-05-25 06:30:06 +08:00
|
|
|
};
|
|
|
|
|
2017-05-22 14:01:37 +08:00
|
|
|
// Add GC root chunks.
|
2019-07-12 14:12:27 +08:00
|
|
|
for (Symbol *b : config->gcroot)
|
2017-05-25 06:30:06 +08:00
|
|
|
addSym(b);
|
2015-09-20 05:36:28 +08:00
|
|
|
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
SectionChunk *sc = worklist.pop_back_val();
|
2018-08-31 15:45:20 +08:00
|
|
|
assert(sc->live && "We mark as live when pushing onto the worklist!");
|
2015-09-20 05:36:28 +08:00
|
|
|
|
|
|
|
// Mark all symbols listed in the relocation table for this section.
|
2017-11-04 05:21:47 +08:00
|
|
|
for (Symbol *b : sc->symbols())
|
2017-11-21 03:37:07 +08:00
|
|
|
if (b)
|
|
|
|
addSym(b);
|
2015-09-20 05:36:28 +08:00
|
|
|
|
|
|
|
// Mark associative sections if any.
|
[COFF] Reduce the size of Chunk and SectionChunk, NFC
Summary:
Reorder the fields in both to use padding more efficiently, and add more
comments on the purpose of the fields.
Replace `std::vector<SectionChunk*> AssociativeChildren` with a
singly-linked list. This avoids the separate vector allocation to list
associative children, and shrinks the 3 pointers used for the typically
empty vector down to 1.
In the end, this reduces the sum of heap allocations used to link
browser_tests.exe with NO PDB by 13.10%, going from 2,248,728 KB to
1,954,071 KB of heap. These numbers exclude memory mapped files, which
are of course a significant factor in LLD's memory usage.
Reviewers: ruiu, mstorsjo, aganea
Subscribers: jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59797
llvm-svn: 357535
2019-04-03 06:11:58 +08:00
|
|
|
for (SectionChunk &c : sc->children())
|
|
|
|
enqueue(&c);
|
2015-09-20 05:36:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|