[lld] 3df4c5a - [NFC] Optimize vector usage in lld

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 26 17:32:47 PST 2023


Author: Jez Ng
Date: 2023-01-26T20:31:42-05:00
New Revision: 3df4c5a92f7f68d97ada6886c13d8900a5c7eef1

URL: https://github.com/llvm/llvm-project/commit/3df4c5a92f7f68d97ada6886c13d8900a5c7eef1
DIFF: https://github.com/llvm/llvm-project/commit/3df4c5a92f7f68d97ada6886c13d8900a5c7eef1.diff

LOG: [NFC] Optimize vector usage in lld

By using emplace_back, as well as converting some loops to for-each, we can do more efficient vectorization.

Make copy constructor for TemporaryFile noexcept.

Reviewed By: #lld-macho, int3

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

Added: 
    

Modified: 
    lld/COFF/Chunks.cpp
    lld/COFF/Driver.cpp
    lld/COFF/DriverUtils.cpp
    lld/COFF/MinGW.cpp
    lld/COFF/Writer.cpp
    lld/ELF/InputFiles.cpp
    lld/ELF/MapFile.cpp
    lld/ELF/OutputSections.cpp
    lld/MachO/Arch/ARM64.cpp
    lld/MachO/SyntheticSections.h

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index 65de868a5ffd7..7ec4829599906 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -658,8 +658,7 @@ void SectionChunk::getRuntimePseudoRelocs(
     }
     // sizeInBits is used to initialize the Flags field; currently no
     // other flags are defined.
-    res.emplace_back(
-        RuntimePseudoReloc(target, this, rel.VirtualAddress, sizeInBits));
+    res.emplace_back(target, this, rel.VirtualAddress, sizeInBits);
   }
 }
 

diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index edfa7f6167217..a927d414e7b18 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -1506,7 +1506,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   }
 
   // Construct search path list.
-  searchPaths.push_back("");
+  searchPaths.emplace_back("");
   for (auto *arg : args.filtered(OPT_libpath))
     searchPaths.push_back(arg->getValue());
   detectWinSysRoot(args);

diff  --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp
index d70f53aca5b94..c663a85f43cf1 100644
--- a/lld/COFF/DriverUtils.cpp
+++ b/lld/COFF/DriverUtils.cpp
@@ -347,9 +347,7 @@ class TemporaryFile {
     }
   }
 
-  TemporaryFile(TemporaryFile &&obj) {
-    std::swap(path, obj.path);
-  }
+  TemporaryFile(TemporaryFile &&obj) noexcept { std::swap(path, obj.path); }
 
   ~TemporaryFile() {
     if (path.empty())
@@ -869,7 +867,7 @@ opt::InputArgList ArgParser::parse(ArrayRef<const char *> argv) {
   ctx.config.argv = {argv[0]};
   for (opt::Arg *arg : args) {
     if (arg->getOption().getKind() != opt::Option::InputClass) {
-      ctx.config.argv.push_back(args.getArgString(arg->getIndex()));
+      ctx.config.argv.emplace_back(args.getArgString(arg->getIndex()));
     }
   }
 

diff  --git a/lld/COFF/MinGW.cpp b/lld/COFF/MinGW.cpp
index 01372cbdf29ac..1212569285678 100644
--- a/lld/COFF/MinGW.cpp
+++ b/lld/COFF/MinGW.cpp
@@ -267,8 +267,8 @@ void lld::coff::wrapSymbols(COFFLinkerContext &ctx,
   // Update pointers in input files.
   parallelForEach(ctx.objFileInstances, [&](ObjFile *file) {
     MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
-    for (size_t i = 0, e = syms.size(); i != e; ++i)
-      if (Symbol *s = map.lookup(syms[i]))
-        syms[i] = s;
+    for (auto &sym : syms)
+      if (Symbol *s = map.lookup(sym))
+        sym = s;
   });
 }

diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index a8fc38c5fa3e5..0909b14d81901 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -488,7 +488,7 @@ bool Writer::createThunks(OutputSection *os, int margin) {
       uint32_t &thunkSymbolIndex = insertion.first->second;
       if (insertion.second)
         thunkSymbolIndex = file->addRangeThunkSymbol(thunk);
-      relocReplacements.push_back({j, thunkSymbolIndex});
+      relocReplacements.emplace_back(j, thunkSymbolIndex);
     }
 
     // Get a writable copy of this section's relocations so they can be
@@ -531,8 +531,7 @@ bool Writer::verifyRanges(const std::vector<Chunk *> chunks) {
       continue;
 
     ArrayRef<coff_relocation> relocs = sc->getRelocs();
-    for (size_t j = 0, e = relocs.size(); j < e; ++j) {
-      const coff_relocation &rel = relocs[j];
+    for (const coff_relocation &rel : relocs) {
       Symbol *relocTarget = sc->file->getSymbol(rel.SymbolTableIndex);
 
       Defined *sym = dyn_cast_or_null<Defined>(relocTarget);
@@ -1034,13 +1033,13 @@ void Writer::createMiscChunks() {
     // allowing a debugger to match a PDB and an executable.  So we need it even
     // if we're ultimately not going to write CodeView data to the PDB.
     buildId = make<CVDebugRecordChunk>(ctx);
-    debugRecords.push_back({COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId});
+    debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_CODEVIEW, buildId);
   }
 
   if (config->cetCompat) {
-    debugRecords.push_back({COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS,
-                            make<ExtendedDllCharacteristicsChunk>(
-                                IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT)});
+    debugRecords.emplace_back(COFF::IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS,
+                              make<ExtendedDllCharacteristicsChunk>(
+                                  IMAGE_DLL_CHARACTERISTICS_EX_CET_COMPAT));
   }
 
   // Align and add each chunk referenced by the debug data directory.

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 7dacdeb9f0422..4278bf678ca0f 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1706,9 +1706,9 @@ void BinaryFile::parse() {
   // user programs can access blobs by name. Non-alphanumeric
   // characters in a filename are replaced with underscore.
   std::string s = "_binary_" + mb.getBufferIdentifier().str();
-  for (size_t i = 0; i < s.size(); ++i)
-    if (!isAlnum(s[i]))
-      s[i] = '_';
+  for (char &c : s)
+    if (!isAlnum(c))
+      c = '_';
 
   llvm::StringSaver &saver = lld::saver();
 

diff  --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp
index e28656cd38dca..9bda8f11467a6 100644
--- a/lld/ELF/MapFile.cpp
+++ b/lld/ELF/MapFile.cpp
@@ -157,7 +157,7 @@ static void writeMapFile(raw_fd_ostream &os) {
   os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
      << "     Size Align Out     In      Symbol\n";
 
-  OutputSection* osec = nullptr;
+  OutputSection *osec = nullptr;
   for (SectionCommand *cmd : script->sectionCommands) {
     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
       if (assign->provide && !assign->sym)

diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index dc40f07ca6a10..2251ecc83cde6 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -242,7 +242,7 @@ static void sortByOrder(MutableArrayRef<InputSection *> in,
                         llvm::function_ref<int(InputSectionBase *s)> order) {
   std::vector<std::pair<int, InputSection *>> v;
   for (InputSection *s : in)
-    v.push_back({order(s), s});
+    v.emplace_back(order(s), s);
   llvm::stable_sort(v, less_first());
 
   for (size_t i = 0; i < v.size(); ++i)

diff  --git a/lld/MachO/Arch/ARM64.cpp b/lld/MachO/Arch/ARM64.cpp
index 6627d41fbec36..e28e82a78cbd1 100644
--- a/lld/MachO/Arch/ARM64.cpp
+++ b/lld/MachO/Arch/ARM64.cpp
@@ -139,14 +139,14 @@ void ARM64::populateThunk(InputSection *thunk, Symbol *funcSym) {
   thunk->align = 4;
   thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode),
                  sizeof(thunkCode)};
-  thunk->relocs.push_back({/*type=*/ARM64_RELOC_PAGEOFF12,
-                           /*pcrel=*/false, /*length=*/2,
-                           /*offset=*/4, /*addend=*/0,
-                           /*referent=*/funcSym});
-  thunk->relocs.push_back({/*type=*/ARM64_RELOC_PAGE21,
-                           /*pcrel=*/true, /*length=*/2,
-                           /*offset=*/0, /*addend=*/0,
-                           /*referent=*/funcSym});
+  thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGEOFF12,
+                             /*pcrel=*/false, /*length=*/2,
+                             /*offset=*/4, /*addend=*/0,
+                             /*referent=*/funcSym);
+  thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGE21,
+                             /*pcrel=*/true, /*length=*/2,
+                             /*offset=*/0, /*addend=*/0,
+                             /*referent=*/funcSym);
 }
 
 ARM64::ARM64() : ARM64Common(LP64()) {

diff  --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h
index b17e991c41429..83321889fdde1 100644
--- a/lld/MachO/SyntheticSections.h
+++ b/lld/MachO/SyntheticSections.h
@@ -162,7 +162,7 @@ class RebaseSection final : public LinkEditSection {
 
   void addEntry(const InputSection *isec, uint64_t offset) {
     if (config->isPic)
-      locations.push_back({isec, offset});
+      locations.emplace_back(isec, offset);
   }
 
 private:
@@ -174,7 +174,7 @@ struct BindingEntry {
   int64_t addend;
   Location target;
   BindingEntry(int64_t addend, Location target)
-      : addend(addend), target(std::move(target)) {}
+      : addend(addend), target(target) {}
 };
 
 template <class Sym>


        


More information about the llvm-commits mailing list