[NFC] Optimize vector usage in lld
authorJez Ng <jezng@fb.com>
Fri, 27 Jan 2023 01:28:58 +0000 (20:28 -0500)
committerJez Ng <jezng@fb.com>
Fri, 27 Jan 2023 01:31:42 +0000 (20:31 -0500)
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

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

index 65de868..7ec4829 100644 (file)
@@ -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);
   }
 }
 
index edfa7f6..a927d41 100644 (file)
@@ -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);
index d70f53a..c663a85 100644 (file)
@@ -347,9 +347,7 @@ public:
     }
   }
 
-  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()));
     }
   }
 
index 01372cb..1212569 100644 (file)
@@ -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;
   });
 }
index a8fc38c..0909b14 100644 (file)
@@ -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.
index 7dacdeb..4278bf6 100644 (file)
@@ -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();
 
index e28656c..9bda8f1 100644 (file)
@@ -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";
 
-  OutputSectionosec = nullptr;
+  OutputSection *osec = nullptr;
   for (SectionCommand *cmd : script->sectionCommands) {
     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
       if (assign->provide && !assign->sym)
index dc40f07..2251ecc 100644 (file)
@@ -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)
index 6627d41..e28e82a 100644 (file)
@@ -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()) {
index b17e991..8332188 100644 (file)
@@ -162,7 +162,7 @@ public:
 
   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>