瀏覽代碼

[ThinLTO] Avoid unnecesary hash lookups during metadata linking (NFC)

Replace sequences of count() followed by operator[] with either
find() or insert(), depending on the context.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@258405 91177308-0d34-0410-b5e6-96231b3b80d8
Teresa Johnson 9 年之前
父節點
當前提交
490559682b
共有 2 個文件被更改,包括 20 次插入19 次删除
  1. 4 3
      lib/Bitcode/Reader/BitcodeReader.cpp
  2. 16 16
      lib/Linker/IRMover.cpp

+ 4 - 3
lib/Bitcode/Reader/BitcodeReader.cpp

@@ -3081,9 +3081,11 @@ void BitcodeReader::saveMetadataList(
     if (!OnlyTempMD || (N && N->isTemporary())) {
     if (!OnlyTempMD || (N && N->isTemporary())) {
       // Will call this after materializing each function, in order to
       // Will call this after materializing each function, in order to
       // handle remapping of the function's instructions/metadata.
       // handle remapping of the function's instructions/metadata.
+      auto IterBool = MetadataToIDs.insert(std::make_pair(MD, ID));
       // See if we already have an entry in that case.
       // See if we already have an entry in that case.
-      if (OnlyTempMD && MetadataToIDs.count(MD)) {
-        assert(MetadataToIDs[MD] == ID && "Inconsistent metadata value id");
+      if (OnlyTempMD && !IterBool.second) {
+        assert(IterBool.first->second == ID &&
+               "Inconsistent metadata value id");
         continue;
         continue;
       }
       }
       if (N && N->isTemporary())
       if (N && N->isTemporary())
@@ -3091,7 +3093,6 @@ void BitcodeReader::saveMetadataList(
         // metadata while it is the key of a map. The flag will be set back
         // metadata while it is the key of a map. The flag will be set back
         // to true when the saved metadata list is destroyed.
         // to true when the saved metadata list is destroyed.
         N->setCanReplace(false);
         N->setCanReplace(false);
-      MetadataToIDs[MD] = ID;
     }
     }
   }
   }
 }
 }

+ 16 - 16
lib/Linker/IRMover.cpp

@@ -659,17 +659,15 @@ Metadata *IRLinker::mapTemporaryMetadata(Metadata *MD) {
     return nullptr;
     return nullptr;
   // If this temporary metadata has a value id recorded during function
   // If this temporary metadata has a value id recorded during function
   // parsing, record that in the ValIDToTempMDMap if one was provided.
   // parsing, record that in the ValIDToTempMDMap if one was provided.
-  if (MetadataToIDs.count(MD)) {
-    unsigned Idx = MetadataToIDs[MD];
-    // Check if we created a temp MD when importing a different function from
-    // this module. If so, reuse it the same temporary metadata, otherwise
-    // add this temporary metadata to the map.
-    if (!ValIDToTempMDMap->count(Idx)) {
-      MDNode *Node = cast<MDNode>(MD);
-      assert(Node->isTemporary());
-      (*ValIDToTempMDMap)[Idx] = Node;
-    }
-    return (*ValIDToTempMDMap)[Idx];
+  auto I = MetadataToIDs.find(MD);
+  if (I != MetadataToIDs.end()) {
+    unsigned Idx = I->second;
+    MDNode *Node = cast<MDNode>(MD);
+    assert(Node->isTemporary());
+    // If we created a temp MD when importing a different function from
+    // this module, reuse the same temporary metadata.
+    auto IterBool = ValIDToTempMDMap->insert(std::make_pair(Idx, Node));
+    return IterBool.first->second;
   }
   }
   return nullptr;
   return nullptr;
 }
 }
@@ -686,16 +684,18 @@ void IRLinker::replaceTemporaryMetadata(const Metadata *OrigMD,
   // created during function importing was provided, and the source
   // created during function importing was provided, and the source
   // metadata has a value id recorded during metadata parsing, replace
   // metadata has a value id recorded during metadata parsing, replace
   // the temporary metadata with the final mapped metadata now.
   // the temporary metadata with the final mapped metadata now.
-  if (MetadataToIDs.count(OrigMD)) {
-    unsigned Idx = MetadataToIDs[OrigMD];
+  auto I = MetadataToIDs.find(OrigMD);
+  if (I != MetadataToIDs.end()) {
+    unsigned Idx = I->second;
+    auto VI = ValIDToTempMDMap->find(Idx);
     // Nothing to do if we didn't need to create a temporary metadata during
     // Nothing to do if we didn't need to create a temporary metadata during
     // function importing.
     // function importing.
-    if (!ValIDToTempMDMap->count(Idx))
+    if (VI == ValIDToTempMDMap->end())
       return;
       return;
-    MDNode *TempMD = (*ValIDToTempMDMap)[Idx];
+    MDNode *TempMD = VI->second;
     TempMD->replaceAllUsesWith(NewMD);
     TempMD->replaceAllUsesWith(NewMD);
     MDNode::deleteTemporary(TempMD);
     MDNode::deleteTemporary(TempMD);
-    ValIDToTempMDMap->erase(Idx);
+    ValIDToTempMDMap->erase(VI);
   }
   }
 }
 }