[lld] r248099 - COFF: Move markLive() from Writer.cpp to its own file.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 19 14:36:28 PDT 2015


Author: ruiu
Date: Sat Sep 19 16:36:28 2015
New Revision: 248099

URL: http://llvm.org/viewvc/llvm-project?rev=248099&view=rev
Log:
COFF: Move markLive() from Writer.cpp to its own file.

Conceptually, garbage collection is not part of Writer,
so move the function out of the file.

Added:
    lld/trunk/COFF/MarkLive.cpp
Modified:
    lld/trunk/COFF/CMakeLists.txt
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Driver.h
    lld/trunk/COFF/Writer.cpp
    lld/trunk/COFF/Writer.h

Modified: lld/trunk/COFF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/CMakeLists.txt?rev=248099&r1=248098&r2=248099&view=diff
==============================================================================
--- lld/trunk/COFF/CMakeLists.txt (original)
+++ lld/trunk/COFF/CMakeLists.txt Sat Sep 19 16:36:28 2015
@@ -10,6 +10,7 @@ add_llvm_library(lldCOFF
   Error.cpp
   ICF.cpp
   InputFiles.cpp
+  MarkLive.cpp
   ModuleDef.cpp
   SymbolTable.cpp
   Symbols.cpp

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=248099&r1=248098&r2=248099&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Sat Sep 19 16:36:28 2015
@@ -644,6 +644,14 @@ void LinkerDriver::link(llvm::ArrayRef<c
   if (auto *Arg = Args.getLastArg(OPT_pdb))
     touchFile(Arg->getValue());
 
+  // Identify unreferenced COMDAT sections.
+  if (Config->DoGC)
+    markLive(Symtab.getChunks());
+
+  // Identify identical COMDAT sections to merge them.
+  if (Config->DoICF)
+    doICF(Symtab.getChunks());
+
   // Write the result.
   writeResult(&Symtab);
 

Modified: lld/trunk/COFF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.h?rev=248099&r1=248098&r2=248099&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.h (original)
+++ lld/trunk/COFF/Driver.h Sat Sep 19 16:36:28 2015
@@ -37,6 +37,12 @@ class InputFile;
 // Entry point of the COFF linker.
 void link(llvm::ArrayRef<const char *> Args);
 
+// Implemented in MarkLive.cpp.
+void markLive(const std::vector<Chunk *> &Chunks);
+
+// Implemented in ICF.cpp.
+void doICF(const std::vector<Chunk *> &Chunks);
+
 class ArgParser {
 public:
   ArgParser() : Alloc(AllocAux) {}

Added: lld/trunk/COFF/MarkLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/MarkLive.cpp?rev=248099&view=auto
==============================================================================
--- lld/trunk/COFF/MarkLive.cpp (added)
+++ lld/trunk/COFF/MarkLive.cpp Sat Sep 19 16:36:28 2015
@@ -0,0 +1,61 @@
+//===- MarkLive.cpp -------------------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Chunks.h"
+#include "Symbols.h"
+#include "llvm/ADT/STLExtras.h"
+#include <vector>
+
+namespace lld {
+namespace coff {
+
+// 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.
+void markLive(const std::vector<Chunk *> &Chunks) {
+  // 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;
+
+  // COMDAT section chunks are dead by default. Add non-COMDAT chunks.
+  for (Chunk *C : Chunks)
+    if (auto *SC = dyn_cast<SectionChunk>(C))
+      if (SC->isLive())
+        Worklist.push_back(SC);
+
+  auto Enqueue = [&](SectionChunk *C) {
+    if (C->isLive())
+      return;
+    C->markLive();
+    Worklist.push_back(C);
+  };
+
+  // Add GC root chunks.
+  for (Undefined *U : Config->GCRoot)
+    if (auto *D = dyn_cast<DefinedRegular>(U->repl()))
+      Enqueue(D->getChunk());
+
+  while (!Worklist.empty()) {
+    SectionChunk *SC = Worklist.pop_back_val();
+    assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
+
+    // Mark all symbols listed in the relocation table for this section.
+    for (SymbolBody *S : SC->symbols())
+      if (auto *D = dyn_cast<DefinedRegular>(S->repl()))
+        Enqueue(D->getChunk());
+
+    // Mark associative sections if any.
+    for (SectionChunk *C : SC->children())
+      Enqueue(C);
+  }
+}
+
+}
+}

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=248099&r1=248098&r2=248099&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Sat Sep 19 16:36:28 2015
@@ -49,8 +49,6 @@ public:
   void run();
 
 private:
-  void markLive();
-  void dedupCOMDATs();
   void createSections();
   void createMiscChunks();
   void createImportTables();
@@ -216,8 +214,6 @@ bool Defined::isExecutable() {
 
 // The main function of the writer.
 void Writer::run() {
-  markLive();
-  dedupCOMDATs();
   createSections();
   createMiscChunks();
   createImportTables();
@@ -239,57 +235,6 @@ void Writer::run() {
   error(Buffer->commit(), "Failed to write the output file");
 }
 
-// Set live bit on for each reachable chunk. Unmarked (unreachable)
-// COMDAT chunks will be ignored in the next step, so that they don't
-// come to the final output file.
-void Writer::markLive() {
-  if (!Config->DoGC)
-    return;
-
-  // 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;
-
-  // COMDAT section chunks are dead by default. Add non-COMDAT chunks.
-  for (Chunk *C : Symtab->getChunks())
-    if (auto *SC = dyn_cast<SectionChunk>(C))
-      if (SC->isLive())
-        Worklist.push_back(SC);
-
-  auto Enqueue = [&](SectionChunk *C) {
-    if (C->isLive())
-      return;
-    C->markLive();
-    Worklist.push_back(C);
-  };
-
-  // Add GC root chunks.
-  for (Undefined *U : Config->GCRoot)
-    if (auto *D = dyn_cast<DefinedRegular>(U->repl()))
-      Enqueue(D->getChunk());
-
-  while (!Worklist.empty()) {
-    SectionChunk *SC = Worklist.pop_back_val();
-    assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
-
-    // Mark all symbols listed in the relocation table for this section.
-    for (SymbolBody *S : SC->symbols())
-      if (auto *D = dyn_cast<DefinedRegular>(S->repl()))
-        Enqueue(D->getChunk());
-
-    // Mark associative sections if any.
-    for (SectionChunk *C : SC->children())
-      Enqueue(C);
-  }
-}
-
-// Merge identical COMDAT sections.
-void Writer::dedupCOMDATs() {
-  if (Config->DoICF)
-    doICF(Symtab->getChunks());
-}
-
 static StringRef getOutputSection(StringRef Name) {
   StringRef S = Name.split('$').first;
   auto It = Config->Merge.find(S);

Modified: lld/trunk/COFF/Writer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.h?rev=248099&r1=248098&r2=248099&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.h (original)
+++ lld/trunk/COFF/Writer.h Sat Sep 19 16:36:28 2015
@@ -20,9 +20,6 @@ class OutputSection;
 
 void writeResult(SymbolTable *T);
 
-// Implemented in ICF.cpp.
-void doICF(const std::vector<Chunk *> &Chunks);
-
 }
 }
 




More information about the llvm-commits mailing list