[llvm] [bugpoint] Avoid repeated hash lookups (NFC) (PR #133616)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 29 21:22:43 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-objectyaml

@llvm/pr-subscribers-debuginfo

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/133616.diff


15 Files Affected:

- (modified) llvm/tools/bugpoint/CrashDebugger.cpp (+5-11) 
- (modified) llvm/tools/bugpoint/OptimizerDriver.cpp (+1-2) 
- (modified) llvm/tools/bugpoint/ToolRunner.cpp (+12-24) 
- (modified) llvm/tools/dsymutil/Reproducer.cpp (+1-2) 
- (modified) llvm/tools/llvm-cov/CodeCoverage.cpp (+1-2) 
- (modified) llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp (+1-2) 
- (modified) llvm/tools/llvm-libtool-darwin/DependencyInfo.h (+1-2) 
- (modified) llvm/tools/llvm-lipo/llvm-lipo.cpp (+2-2) 
- (modified) llvm/tools/llvm-lto2/llvm-lto2.cpp (+1-2) 
- (modified) llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp (+1-2) 
- (modified) llvm/tools/llvm-rc/llvm-rc.cpp (+1-2) 
- (modified) llvm/tools/llvm-xray/xray-stacks.cpp (+8-15) 
- (modified) llvm/tools/lto/lto.cpp (+2-4) 
- (modified) llvm/tools/obj2yaml/elf2yaml.cpp (+2-4) 
- (modified) llvm/tools/obj2yaml/macho2yaml.cpp (+2-5) 


``````````diff
diff --git a/llvm/tools/bugpoint/CrashDebugger.cpp b/llvm/tools/bugpoint/CrashDebugger.cpp
index a7777f778f66e..e2f7e104c58e3 100644
--- a/llvm/tools/bugpoint/CrashDebugger.cpp
+++ b/llvm/tools/bugpoint/CrashDebugger.cpp
@@ -390,9 +390,7 @@ bool ReduceCrashingFunctionAttributes::TestFuncAttrs(
 
     // Pass along the set of attributes that caused the crash.
     Attrs.clear();
-    for (Attribute A : NewAttrs.getFnAttrs()) {
-      Attrs.push_back(A);
-    }
+    llvm::append_range(Attrs, NewAttrs.getFnAttrs());
     return true;
   }
   return false;
@@ -800,8 +798,7 @@ bool ReduceCrashingInstructions::TestInsts(
     // Make sure to use instruction pointers that point into the now-current
     // module, and that they don't include any deleted blocks.
     Insts.clear();
-    for (Instruction *Inst : Instructions)
-      Insts.push_back(Inst);
+    llvm::append_range(Insts, Instructions);
     return true;
   }
   // It didn't crash, try something else.
@@ -870,8 +867,7 @@ bool ReduceCrashingMetadata::TestInsts(std::vector<Instruction *> &Insts) {
     // Make sure to use instruction pointers that point into the now-current
     // module, and that they don't include any deleted blocks.
     Insts.clear();
-    for (Instruction *I : Instructions)
-      Insts.push_back(I);
+    llvm::append_range(Insts, Instructions);
     return true;
   }
   // It didn't crash, try something else.
@@ -1211,8 +1207,7 @@ static Error DebugACrash(BugDriver &BD, BugTester TestFn) {
         assert(Fn && "Could not find function?");
 
         std::vector<Attribute> Attrs;
-        for (Attribute A : Fn->getAttributes().getFnAttrs())
-          Attrs.push_back(A);
+        llvm::append_range(Attrs, Fn->getAttributes().getFnAttrs());
 
         OldSize += Attrs.size();
         Expected<bool> Result =
@@ -1319,8 +1314,7 @@ static Error DebugACrash(BugDriver &BD, BugTester TestFn) {
       // contribute to the crash, bisect the operands of the remaining ones
       std::vector<const MDNode *> NamedMDOps;
       for (auto &NamedMD : BD.getProgram().named_metadata())
-        for (auto *op : NamedMD.operands())
-          NamedMDOps.push_back(op);
+        llvm::append_range(NamedMDOps, NamedMD.operands());
       Expected<bool> Result =
           ReduceCrashingNamedMDOps(BD, TestFn).reduceList(NamedMDOps);
       if (Error E = Result.takeError())
diff --git a/llvm/tools/bugpoint/OptimizerDriver.cpp b/llvm/tools/bugpoint/OptimizerDriver.cpp
index 0b29a1f17d879..56a0fa4d5ec9e 100644
--- a/llvm/tools/bugpoint/OptimizerDriver.cpp
+++ b/llvm/tools/bugpoint/OptimizerDriver.cpp
@@ -203,8 +203,7 @@ bool BugDriver::runPasses(Module &Program,
   } else
     Args.push_back(tool);
 
-  for (unsigned i = 0, e = OptArgs.size(); i != e; ++i)
-    Args.push_back(OptArgs[i]);
+  llvm::append_range(Args, OptArgs);
   // Pin to legacy PM since bugpoint has lots of infra and hacks revolving
   // around the legacy PM.
   Args.push_back("-bugpoint-enable-legacy-pm");
diff --git a/llvm/tools/bugpoint/ToolRunner.cpp b/llvm/tools/bugpoint/ToolRunner.cpp
index e45c89b746aeb..f2f5966ad9d04 100644
--- a/llvm/tools/bugpoint/ToolRunner.cpp
+++ b/llvm/tools/bugpoint/ToolRunner.cpp
@@ -181,13 +181,11 @@ Expected<int> LLI::ExecuteProgram(const std::string &Bitcode,
   }
 
   // Add any extra LLI args.
-  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
-    LLIArgs.push_back(ToolArgs[i]);
+  llvm::append_range(LLIArgs, ToolArgs);
 
   LLIArgs.push_back(Bitcode);
   // Add optional parameters to the running program from Argv
-  for (unsigned i = 0, e = Args.size(); i != e; ++i)
-    LLIArgs.push_back(Args[i]);
+  llvm::append_range(LLIArgs, Args);
 
   outs() << "<lli>";
   outs().flush();
@@ -268,13 +266,11 @@ Error CustomCompiler::compileProgram(const std::string &Bitcode,
   std::vector<StringRef> ProgramArgs;
   ProgramArgs.push_back(CompilerCommand);
 
-  for (const auto &Arg : CompilerArgs)
-    ProgramArgs.push_back(Arg);
+  llvm::append_range(ProgramArgs, CompilerArgs);
   ProgramArgs.push_back(Bitcode);
 
   // Add optional parameters to the running program from Argv
-  for (const auto &Arg : CompilerArgs)
-    ProgramArgs.push_back(Arg);
+  llvm::append_range(ProgramArgs, CompilerArgs);
 
   if (RunProgramWithTimeout(CompilerCommand, ProgramArgs, "", "", "", Timeout,
                             MemoryLimit))
@@ -317,13 +313,11 @@ Expected<int> CustomExecutor::ExecuteProgram(
   std::vector<StringRef> ProgramArgs;
   ProgramArgs.push_back(ExecutionCommand);
 
-  for (std::size_t i = 0; i < ExecutorArgs.size(); ++i)
-    ProgramArgs.push_back(ExecutorArgs[i]);
+  llvm::append_range(ProgramArgs, ExecutorArgs);
   ProgramArgs.push_back(Bitcode);
 
   // Add optional parameters to the running program from Argv
-  for (unsigned i = 0, e = Args.size(); i != e; ++i)
-    ProgramArgs.push_back(Args[i]);
+  llvm::append_range(ProgramArgs, Args);
 
   return RunProgramWithTimeout(ExecutionCommand, ProgramArgs, InputFile,
                                OutputFile, OutputFile, Timeout, MemoryLimit);
@@ -447,8 +441,7 @@ Expected<CC::FileType> LLC::OutputCode(const std::string &Bitcode,
   LLCArgs.push_back(LLCPath);
 
   // Add any extra LLC args.
-  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
-    LLCArgs.push_back(ToolArgs[i]);
+  llvm::append_range(LLCArgs, ToolArgs);
 
   LLCArgs.push_back("-o");
   LLCArgs.push_back(OutputAsmFile); // Output to the Asm file
@@ -563,8 +556,7 @@ Expected<int> JIT::ExecuteProgram(const std::string &Bitcode,
   JITArgs.push_back("-force-interpreter=false");
 
   // Add any extra LLI args.
-  for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
-    JITArgs.push_back(ToolArgs[i]);
+  llvm::append_range(JITArgs, ToolArgs);
 
   for (unsigned i = 0, e = SharedLibs.size(); i != e; ++i) {
     JITArgs.push_back("-load");
@@ -572,8 +564,7 @@ Expected<int> JIT::ExecuteProgram(const std::string &Bitcode,
   }
   JITArgs.push_back(Bitcode);
   // Add optional parameters to the running program from Argv
-  for (unsigned i = 0, e = Args.size(); i != e; ++i)
-    JITArgs.push_back(Args[i]);
+  llvm::append_range(JITArgs, Args);
 
   outs() << "<jit>";
   outs().flush();
@@ -674,8 +665,7 @@ Expected<int> CC::ExecuteProgram(const std::string &ProgramFile,
   // most likely -L and -l options that need to come before other libraries but
   // after the source. Other options won't be sensitive to placement on the
   // command line, so this should be safe.
-  for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
-    CCArgs.push_back(ArgsForCC[i]);
+  llvm::append_range(CCArgs, ArgsForCC);
 
   CCArgs.push_back("-lm"); // Hard-code the math library...
   CCArgs.push_back("-O2"); // Optimize the program a bit...
@@ -725,8 +715,7 @@ Expected<int> CC::ExecuteProgram(const std::string &ProgramFile,
   }
 
   // Add optional parameters to the running program from Argv
-  for (unsigned i = 0, e = Args.size(); i != e; ++i)
-    ProgramArgs.push_back(Args[i]);
+  llvm::append_range(ProgramArgs, Args);
 
   // Now that we have a binary, run it!
   outs() << "<program>";
@@ -823,8 +812,7 @@ Error CC::MakeSharedObject(const std::string &InputFile, FileType fileType,
   // most likely -L and -l options that need to come before other libraries but
   // after the source. Other options won't be sensitive to placement on the
   // command line, so this should be safe.
-  for (unsigned i = 0, e = ArgsForCC.size(); i != e; ++i)
-    CCArgs.push_back(ArgsForCC[i]);
+  llvm::append_range(CCArgs, ArgsForCC);
 
   outs() << "<CC>";
   outs().flush();
diff --git a/llvm/tools/dsymutil/Reproducer.cpp b/llvm/tools/dsymutil/Reproducer.cpp
index a6cc10424dc52..31e49cdd0518c 100644
--- a/llvm/tools/dsymutil/Reproducer.cpp
+++ b/llvm/tools/dsymutil/Reproducer.cpp
@@ -36,8 +36,7 @@ Reproducer::~Reproducer() = default;
 ReproducerGenerate::ReproducerGenerate(std::error_code &EC, int Argc,
                                        char **Argv, bool GenerateOnExit)
     : Root(createReproducerDir(EC)), GenerateOnExit(GenerateOnExit) {
-  for (int I = 0; I < Argc; ++I)
-    Args.push_back(Argv[I]);
+  llvm::append_range(Args, ArrayRef(Argv, Argc));
   if (!Root.empty())
     FC = std::make_shared<FileCollector>(Root, Root);
   VFS = FileCollector::createCollectorVFS(vfs::getRealFileSystem(), FC);
diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp
index 921f283deedc7..a740cdd45b901 100644
--- a/llvm/tools/llvm-cov/CodeCoverage.cpp
+++ b/llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -588,8 +588,7 @@ void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
   // Invoke the demangler.
   std::vector<StringRef> ArgsV;
   ArgsV.reserve(ViewOpts.DemanglerOpts.size());
-  for (StringRef Arg : ViewOpts.DemanglerOpts)
-    ArgsV.push_back(Arg);
+  llvm::append_range(ArgsV, ViewOpts.DemanglerOpts);
   std::optional<StringRef> Redirects[] = {
       InputPath.str(), OutputPath.str(), {""}};
   std::string ErrMsg;
diff --git a/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp b/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp
index 2859a36c80b0b..7b85166c1b4ae 100644
--- a/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp
+++ b/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp
@@ -126,8 +126,7 @@ int llvm_debuginfod_main(int argc, char **argv, const llvm::ToolContext &) {
   parseArgs(argc, argv);
 
   SmallVector<StringRef, 1> Paths;
-  for (const std::string &Path : ScanPaths)
-    Paths.push_back(Path);
+  llvm::append_range(Paths, ScanPaths);
 
   DefaultThreadPool Pool(hardware_concurrency(MaxConcurrency));
   DebuginfodLog Log;
diff --git a/llvm/tools/llvm-libtool-darwin/DependencyInfo.h b/llvm/tools/llvm-libtool-darwin/DependencyInfo.h
index 7b2f94bdbeb81..784ec3f50cd53 100644
--- a/llvm/tools/llvm-libtool-darwin/DependencyInfo.h
+++ b/llvm/tools/llvm-libtool-darwin/DependencyInfo.h
@@ -50,8 +50,7 @@ class DependencyInfo {
     // Sort the input by its names.
     std::vector<llvm::StringRef> InputNames;
     InputNames.reserve(Inputs.size());
-    for (const auto &F : Inputs)
-      InputNames.push_back(F);
+    llvm::append_range(InputNames, Inputs);
     llvm::sort(InputNames);
 
     for (const auto &In : InputNames)
diff --git a/llvm/tools/llvm-lipo/llvm-lipo.cpp b/llvm/tools/llvm-lipo/llvm-lipo.cpp
index 3c0197e8b7bac..8c588021391b4 100644
--- a/llvm/tools/llvm-lipo/llvm-lipo.cpp
+++ b/llvm/tools/llvm-lipo/llvm-lipo.cpp
@@ -249,8 +249,8 @@ static Config parseLipoOptions(ArrayRef<const char *> ArgsArr) {
 
   switch (ActionArgs[0]->getOption().getID()) {
   case LIPO_verify_arch:
-    for (auto A : InputArgs.getAllArgValues(LIPO_verify_arch))
-      C.VerifyArchList.push_back(A);
+    llvm::append_range(C.VerifyArchList,
+                       InputArgs.getAllArgValues(LIPO_verify_arch));
     if (C.VerifyArchList.empty())
       reportError(
           "verify_arch requires at least one architecture to be specified");
diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp
index 4c9b47d78a1bb..76ff11b8d6412 100644
--- a/llvm/tools/llvm-lto2/llvm-lto2.cpp
+++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp
@@ -326,8 +326,7 @@ static int run(int argc, char **argv) {
 
   Conf.OptLevel = OptLevel - '0';
   Conf.Freestanding = EnableFreestanding;
-  for (auto &PluginFN : PassPlugins)
-    Conf.PassPlugins.push_back(PluginFN);
+  llvm::append_range(Conf.PassPlugins, PassPlugins);
   if (auto Level = CodeGenOpt::parseLevel(CGOptLevel)) {
     Conf.CGOptLevel = *Level;
   } else {
diff --git a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
index 680218e3fc96c..4cb64bdbe8ef9 100644
--- a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
+++ b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
@@ -1538,8 +1538,7 @@ int main(int Argc, const char **Argv) {
 
   // Initialize the filters for LinePrinter.
   auto propagate = [&](auto &Target, auto &Reference) {
-    for (std::string &Option : Reference)
-      Target.push_back(Option);
+    llvm::append_range(Target, Reference);
   };
 
   propagate(opts::Filters.ExcludeTypes, opts::pretty::ExcludeTypes);
diff --git a/llvm/tools/llvm-rc/llvm-rc.cpp b/llvm/tools/llvm-rc/llvm-rc.cpp
index a77188c462afe..6feadc5f259b3 100644
--- a/llvm/tools/llvm-rc/llvm-rc.cpp
+++ b/llvm/tools/llvm-rc/llvm-rc.cpp
@@ -266,8 +266,7 @@ void preprocess(StringRef Src, StringRef Dst, const RcOptions &Opts,
       }
     }
   }
-  for (const auto &S : Opts.PreprocessArgs)
-    Args.push_back(S);
+  llvm::append_range(Args, Opts.PreprocessArgs);
   Args.push_back(Src);
   Args.push_back("-o");
   Args.push_back(Dst);
diff --git a/llvm/tools/llvm-xray/xray-stacks.cpp b/llvm/tools/llvm-xray/xray-stacks.cpp
index aebca9048d4d4..cbf6faeb32960 100644
--- a/llvm/tools/llvm-xray/xray-stacks.cpp
+++ b/llvm/tools/llvm-xray/xray-stacks.cpp
@@ -267,15 +267,11 @@ static StackDuration mergeStackDuration(const StackDuration &Left,
   Data.IntermediateDurations.reserve(Left.IntermediateDurations.size() +
                                      Right.IntermediateDurations.size());
   // Aggregate the durations.
-  for (auto duration : Left.TerminalDurations)
-    Data.TerminalDurations.push_back(duration);
-  for (auto duration : Right.TerminalDurations)
-    Data.TerminalDurations.push_back(duration);
-
-  for (auto duration : Left.IntermediateDurations)
-    Data.IntermediateDurations.push_back(duration);
-  for (auto duration : Right.IntermediateDurations)
-    Data.IntermediateDurations.push_back(duration);
+  llvm::append_range(Data.TerminalDurations, Left.TerminalDurations);
+  llvm::append_range(Data.TerminalDurations, Right.TerminalDurations);
+
+  llvm::append_range(Data.IntermediateDurations, Left.IntermediateDurations);
+  llvm::append_range(Data.IntermediateDurations, Right.IntermediateDurations);
   return Data;
 }
 
@@ -506,8 +502,7 @@ class StackTrie {
     for (const auto &RootNodeRange :
          make_range(map_iterator(Roots.begin(), MapValueFn),
                     map_iterator(Roots.end(), MapValueFn))) {
-      for (auto *RootNode : RootNodeRange)
-        RootValues.push_back(RootNode);
+      llvm::append_range(RootValues, RootNodeRange);
     }
 
     print(OS, FN, RootValues);
@@ -565,8 +560,7 @@ class StackTrie {
       while (!S.empty()) {
         auto *Top = S.pop_back_val();
         printSingleStack<AggType>(OS, FN, ReportThread, ThreadId, Top);
-        for (const auto *C : Top->Callees)
-          S.push_back(C);
+        llvm::append_range(S, Top->Callees);
       }
     }
   }
@@ -641,8 +635,7 @@ class StackTrie {
               TopStacksByCount.pop_back();
           }
         }
-        for (const auto *C : Top->Callees)
-          S.push_back(C);
+        llvm::append_range(S, Top->Callees);
       }
     }
 
diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp
index b377693444189..29219c9114522 100644
--- a/llvm/tools/lto/lto.cpp
+++ b/llvm/tools/lto/lto.cpp
@@ -475,8 +475,7 @@ void lto_set_debug_options(const char *const *options, int number) {
   // Need to put each suboption in a null-terminated string before passing to
   // parseCommandLineOptions().
   std::vector<std::string> Options;
-  for (int i = 0; i < number; ++i)
-    Options.push_back(options[i]);
+  llvm::append_range(Options, ArrayRef(options, number));
 
   llvm::parseCommandLineOptions(Options);
   optionParsingState = OptParsingState::Early;
@@ -498,8 +497,7 @@ void lto_codegen_debug_options_array(lto_code_gen_t cg,
   assert(optionParsingState != OptParsingState::Early &&
          "early option processing already happened");
   SmallVector<StringRef, 4> Options;
-  for (int i = 0; i < number; ++i)
-    Options.push_back(options[i]);
+  llvm::append_range(Options, ArrayRef(options, number));
   unwrap(cg)->setCodeGenDebugOptions(ArrayRef(Options));
 }
 
diff --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp
index b1c8032ea2192..c56ed15501b40 100644
--- a/llvm/tools/obj2yaml/elf2yaml.cpp
+++ b/llvm/tools/obj2yaml/elf2yaml.cpp
@@ -1266,8 +1266,7 @@ ELFDumper<ELFT>::dumpSymtabShndxSection(const Elf_Shdr *Shdr) {
     return EntriesOrErr.takeError();
 
   S->Entries.emplace();
-  for (const Elf_Word &E : *EntriesOrErr)
-    S->Entries->push_back(E);
+  llvm::append_range(*S->Entries, *EntriesOrErr);
   return S.release();
 }
 
@@ -1490,8 +1489,7 @@ ELFDumper<ELFT>::dumpSymverSection(const Elf_Shdr *Shdr) {
     return VersionsOrErr.takeError();
 
   S->Entries.emplace();
-  for (const Elf_Half &E : *VersionsOrErr)
-    S->Entries->push_back(E);
+  llvm::append_range(*S->Entries, *VersionsOrErr);
 
   return S.release();
 }
diff --git a/llvm/tools/obj2yaml/macho2yaml.cpp b/llvm/tools/obj2yaml/macho2yaml.cpp
index cdd871e8c1d68..00220123e8189 100644
--- a/llvm/tools/obj2yaml/macho2yaml.cpp
+++ b/llvm/tools/obj2yaml/macho2yaml.cpp
@@ -364,8 +364,7 @@ void MachODumper::dumpFunctionStarts(std::unique_ptr<MachOYAML::Object> &Y) {
   MachOYAML::LinkEditData &LEData = Y->LinkEdit;
 
   auto FunctionStarts = Obj.getFunctionStarts();
-  for (auto Addr : FunctionStarts)
-    LEData.FunctionStarts.push_back(Addr);
+  llvm::append_range(LEData.FunctionStarts, FunctionStarts);
 }
 
 void MachODumper::dumpRebaseOpcodes(std::unique_ptr<MachOYAML::Object> &Y) {
@@ -637,9 +636,7 @@ void MachODumper::dumpChainedFixups(std::unique_ptr<MachOYAML::Object> &Y) {
         assert(DC.dataoff < Obj.getData().size());
         assert(DC.dataoff + DC.datasize <= Obj.getData().size());
         const char *Bytes = Obj.getData().data() + DC.dataoff;
-        for (size_t Idx = 0; Idx < DC.datasize; Idx++) {
-          LEData.ChainedFixups.push_back(Bytes[Idx]);
-        }
+        llvm::append_range(LEData.ChainedFixups, ArrayRef(Bytes, DC.datasize));
       }
       break;
     }

``````````

</details>


https://github.com/llvm/llvm-project/pull/133616


More information about the llvm-commits mailing list