[lld] [lld][RISCV] Avoid second map lookup in mergeArch. NFC (PR #84687)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 10 11:54:35 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/84687

Instead of using find and then an inserting into the map, we can use insert and fix up the version using the iterator if the insert fails.

>From f59b49151eb4ed97039abba57a3b8a2c27e2ff04 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 8 Mar 2024 17:09:25 -0800
Subject: [PATCH] [lld][RISCV] Avoid second map lookup in mergeArch. NFC

Instead of using find and then an inserting into the map, we can
use insert and fix up the version using the iterator if the insert
fails.
---
 lld/ELF/Arch/RISCV.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp
index 4798c86f7d1b61..20de1b9b7bde96 100644
--- a/lld/ELF/Arch/RISCV.cpp
+++ b/lld/ELF/Arch/RISCV.cpp
@@ -1074,12 +1074,12 @@ static void mergeArch(RISCVISAInfo::OrderedExtensionMap &mergedExts,
     mergedXlen = info.getXLen();
   } else {
     for (const auto &ext : info.getExtensions()) {
-      if (auto it = mergedExts.find(ext.first); it != mergedExts.end()) {
-        if (std::tie(it->second.Major, it->second.Minor) >=
+      auto p = mergedExts.insert(ext);
+      if (!p.second) {
+        if (std::tie(p.first->second.Major, p.first->second.Minor) <
             std::tie(ext.second.Major, ext.second.Minor))
-          continue;
+          p.first->second = ext.second;
       }
-      mergedExts[ext.first] = ext.second;
     }
   }
 }



More information about the llvm-commits mailing list