[lld] r326296 - [WebAssembly] Simplify initializeSymbols and merge it with ObjFile::parse. NFC.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 18:57:37 PST 2018


Author: ruiu
Date: Tue Feb 27 18:57:37 2018
New Revision: 326296

URL: http://llvm.org/viewvc/llvm-project?rev=326296&view=rev
Log:
[WebAssembly] Simplify initializeSymbols and merge it with ObjFile::parse. NFC.

This patch simplifies initializeSymbols. Since that function is called
at the tail context of ObjFile::parse, and the function is called only
once from that function, that's effectively just a continuation of
ObjFile::parse. So this patch merge it with ObjFile::parse.

Differential Revision: https://reviews.llvm.org/D43848

Modified:
    lld/trunk/wasm/InputFiles.cpp
    lld/trunk/wasm/InputFiles.h

Modified: lld/trunk/wasm/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.cpp?rev=326296&r1=326295&r2=326296&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.cpp (original)
+++ lld/trunk/wasm/InputFiles.cpp Tue Feb 27 18:57:37 2018
@@ -43,10 +43,10 @@ Optional<MemoryBufferRef> lld::wasm::rea
 }
 
 void ObjFile::dumpInfo() const {
-  log("info for: " + getName() + "\n" +
-      "              Symbols : " + Twine(Symbols.size()) + "\n" +
-      "     Function Imports : " + Twine(NumFunctionImports) + "\n" +
-      "       Global Imports : " + Twine(NumGlobalImports) + "\n");
+  log("info for: " + getName() +
+      "\n              Symbols : " + Twine(Symbols.size()) +
+      "\n     Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) +
+      "\n       Global Imports : " + Twine(WasmObj->getNumImportedGlobals()));
 }
 
 // Relocations contain an index into the function, global or table index
@@ -117,77 +117,37 @@ void ObjFile::parse() {
   TypeMap.resize(getWasmObj()->types().size());
   TypeIsUsed.resize(getWasmObj()->types().size(), false);
 
-  initializeSymbols();
-}
-
-// Return the InputSegment in which a given symbol is defined.
-InputSegment *ObjFile::getSegment(const WasmSymbol &WasmSym) const {
-  return Segments[WasmSym.Info.DataRef.Segment];
-}
-
-InputFunction *ObjFile::getFunction(const WasmSymbol &Sym) const {
-  assert(Sym.Info.ElementIndex >= NumFunctionImports);
-  uint32_t FunctionIndex = Sym.Info.ElementIndex - NumFunctionImports;
-  return Functions[FunctionIndex];
-}
-
-InputGlobal *ObjFile::getGlobal(const WasmSymbol &Sym) const {
-  assert(Sym.Info.ElementIndex >= NumGlobalImports);
-  uint32_t GlobalIndex = Sym.Info.ElementIndex - NumGlobalImports;
-  return Globals[GlobalIndex];
-}
-
-bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const {
-  StringRef Comdat = Chunk->getComdat();
-  return !Comdat.empty() && Symtab->findComdat(Comdat) != this;
-}
-
-FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const {
-  return cast<FunctionSymbol>(Symbols[Index]);
-}
-
-GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const {
-  return cast<GlobalSymbol>(Symbols[Index]);
-}
-
-DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const {
-  return cast<DataSymbol>(Symbols[Index]);
-}
-
-void ObjFile::initializeSymbols() {
-  Symbols.reserve(WasmObj->getNumberOfSymbols());
-
-  NumFunctionImports = WasmObj->getNumImportedFunctions();
-  NumGlobalImports = WasmObj->getNumImportedGlobals();
-
-  ArrayRef<WasmFunction> Funcs = WasmObj->functions();
-  ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes();
-  ArrayRef<WasmSignature> Types = WasmObj->types();
-
   for (StringRef Name : WasmObj->comdats())
     Symtab->addComdat(Name, this);
 
+  // Populate `Segments`.
   for (const WasmSegment &S : WasmObj->dataSegments()) {
     InputSegment *Seg = make<InputSegment>(S, this);
     Seg->copyRelocations(*DataSection);
     Segments.emplace_back(Seg);
   }
 
-  for (const WasmGlobal &G : WasmObj->globals())
-    Globals.emplace_back(make<InputGlobal>(G));
+  // Populate `Functions`.
+  ArrayRef<WasmFunction> Funcs = WasmObj->functions();
+  ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes();
+  ArrayRef<WasmSignature> Types = WasmObj->types();
+  Functions.reserve(Funcs.size());
 
-  for (size_t I = 0; I < Funcs.size(); ++I) {
-    const WasmFunction &Func = Funcs[I];
-    const WasmSignature &Sig = Types[FuncTypes[I]];
-    InputFunction *F = make<InputFunction>(Sig, &Func, this);
+  for (size_t I = 0, E = Funcs.size(); I != E; ++I) {
+    InputFunction *F =
+        make<InputFunction>(Types[FuncTypes[I]], &Funcs[I], this);
     F->copyRelocations(*CodeSection);
     Functions.emplace_back(F);
   }
 
-  // Populate `Symbols` based on the WasmSymbols in the object
+  // Populate `Globals`.
+  for (const WasmGlobal &G : WasmObj->globals())
+    Globals.emplace_back(make<InputGlobal>(G));
+
+  // Populate `Symbols` based on the WasmSymbols in the object.
+  Symbols.reserve(WasmObj->getNumberOfSymbols());
   for (const SymbolRef &Sym : WasmObj->symbols()) {
     const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
-
     if (Symbol *Sym = createDefined(WasmSym))
       Symbols.push_back(Sym);
     else
@@ -195,13 +155,31 @@ void ObjFile::initializeSymbols() {
   }
 }
 
+bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const {
+  StringRef Comdat = Chunk->getComdat();
+  return !Comdat.empty() && Symtab->findComdat(Comdat) != this;
+}
+
+FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const {
+  return cast<FunctionSymbol>(Symbols[Index]);
+}
+
+GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const {
+  return cast<GlobalSymbol>(Symbols[Index]);
+}
+
+DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const {
+  return cast<DataSymbol>(Symbols[Index]);
+}
+
 Symbol *ObjFile::createDefined(const WasmSymbol &Sym) {
   if (!Sym.isDefined())
     return nullptr;
 
   switch (Sym.Info.Kind) {
   case WASM_SYMBOL_TYPE_FUNCTION: {
-    InputFunction *Func = getFunction(Sym);
+    InputFunction *Func =
+        Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()];
     if (isExcludedByComdat(Func)) {
       Func->Live = false;
       return nullptr;
@@ -213,7 +191,7 @@ Symbol *ObjFile::createDefined(const Was
                                       Func);
   }
   case WASM_SYMBOL_TYPE_DATA: {
-    InputSegment *Seg = getSegment(Sym);
+    InputSegment *Seg = Segments[Sym.Info.DataRef.Segment];
     if (isExcludedByComdat(Seg)) {
       Seg->Live = false;
       return nullptr;
@@ -229,11 +207,12 @@ Symbol *ObjFile::createDefined(const Was
                                   Offset, Size);
   }
   case WASM_SYMBOL_TYPE_GLOBAL:
+    InputGlobal *Global =
+        Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()];
     if (Sym.isBindingLocal())
-      return make<DefinedGlobal>(Sym.Info.Name, Sym.Info.Flags, this,
-                                 getGlobal(Sym));
+      return make<DefinedGlobal>(Sym.Info.Name, Sym.Info.Flags, this, Global);
     return Symtab->addDefinedGlobal(Sym.Info.Name, Sym.Info.Flags, this,
-                                    getGlobal(Sym));
+                                    Global);
   }
   llvm_unreachable("unkown symbol kind");
 }

Modified: lld/trunk/wasm/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.h?rev=326296&r1=326295&r2=326296&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.h (original)
+++ lld/trunk/wasm/InputFiles.h Tue Feb 27 18:57:37 2018
@@ -114,17 +114,11 @@ private:
   Symbol *createDefined(const WasmSymbol &Sym);
   Symbol *createUndefined(const WasmSymbol &Sym);
 
-  void initializeSymbols();
-  InputSegment *getSegment(const WasmSymbol &WasmSym) const;
-  InputFunction *getFunction(const WasmSymbol &Sym) const;
-  InputGlobal *getGlobal(const WasmSymbol &Sym) const;
   bool isExcludedByComdat(InputChunk *Chunk) const;
 
   // List of all symbols referenced or defined by this file.
   std::vector<Symbol *> Symbols;
 
-  uint32_t NumGlobalImports = 0;
-  uint32_t NumFunctionImports = 0;
   std::unique_ptr<WasmObjectFile> WasmObj;
 };
 




More information about the llvm-commits mailing list