1 //===- MarkLive.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "llvm/ADT/STLExtras.h"
18 // Set live bit on for each reachable chunk. Unmarked (unreachable)
19 // COMDAT chunks will be ignored by Writer, so they will be excluded
20 // from the final output.
21 void markLive(const std::vector<Chunk *> &Chunks) {
22 // We build up a worklist of sections which have been marked as live. We only
23 // push into the worklist when we discover an unmarked section, and we mark
24 // as we push, so sections never appear twice in the list.
25 SmallVector<SectionChunk *, 256> Worklist;
27 // COMDAT section chunks are dead by default. Add non-COMDAT chunks.
28 for (Chunk *C : Chunks)
29 if (auto *SC = dyn_cast<SectionChunk>(C))
31 Worklist.push_back(SC);
33 auto Enqueue = [&](SectionChunk *C) {
37 Worklist.push_back(C);
40 // Add GC root chunks.
41 for (SymbolBody *B : Config->GCRoot)
42 if (auto *D = dyn_cast<DefinedRegular>(B))
43 Enqueue(D->getChunk());
45 while (!Worklist.empty()) {
46 SectionChunk *SC = Worklist.pop_back_val();
47 assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
49 // Mark all symbols listed in the relocation table for this section.
50 for (SymbolBody *S : SC->symbols())
51 if (auto *D = dyn_cast<DefinedRegular>(S))
52 Enqueue(D->getChunk());
54 // Mark associative sections if any.
55 for (SectionChunk *C : SC->children())