<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 21, 2016 at 8:46 AM, Teresa Johnson via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: tejohnson<br>
Date: Thu Jan 21 10:46:40 2016<br>
New Revision: 258405<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=258405&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=258405&view=rev</a><br>
Log:<br>
[ThinLTO] Avoid unnecesary hash lookups during metadata linking (NFC)<br>
<br>
Replace sequences of count() followed by operator[] with either<br>
find() or insert(), depending on the context.<br>
<br>
Modified:<br>
llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp<br>
llvm/trunk/lib/Linker/IRMover.cpp<br>
<br>
Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=258405&r1=258404&r2=258405&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=258405&r1=258404&r2=258405&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)<br>
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Thu Jan 21 10:46:40 2016<br>
@@ -3081,9 +3081,11 @@ void BitcodeReader::saveMetadataList(<br>
if (!OnlyTempMD || (N && N->isTemporary())) {<br>
// Will call this after materializing each function, in order to<br>
// handle remapping of the function's instructions/metadata.<br>
+ auto IterBool = MetadataToIDs.insert(std::make_pair(MD, ID));<br>
// See if we already have an entry in that case.<br>
- if (OnlyTempMD && MetadataToIDs.count(MD)) {<br>
- assert(MetadataToIDs[MD] == ID && "Inconsistent metadata value id");<br>
+ if (OnlyTempMD && !IterBool.second) {<br>
+ assert(IterBool.first->second == ID &&<br>
+ "Inconsistent metadata value id");<br>
continue;<br>
}<br>
if (N && N->isTemporary())<br>
@@ -3091,7 +3093,6 @@ void BitcodeReader::saveMetadataList(<br>
// metadata while it is the key of a map. The flag will be set back<br>
// to true when the saved metadata list is destroyed.<br>
N->setCanReplace(false);<br>
- MetadataToIDs[MD] = ID;<br>
}<br>
}<br>
}<br>
<br>
Modified: llvm/trunk/lib/Linker/IRMover.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/IRMover.cpp?rev=258405&r1=258404&r2=258405&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/IRMover.cpp?rev=258405&r1=258404&r2=258405&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Linker/IRMover.cpp (original)<br>
+++ llvm/trunk/lib/Linker/IRMover.cpp Thu Jan 21 10:46:40 2016<br>
@@ -659,17 +659,15 @@ Metadata *IRLinker::mapTemporaryMetadata<br>
return nullptr;<br>
// If this temporary metadata has a value id recorded during function<br>
// parsing, record that in the ValIDToTempMDMap if one was provided.<br>
- if (MetadataToIDs.count(MD)) {<br>
- unsigned Idx = MetadataToIDs[MD];<br>
- // Check if we created a temp MD when importing a different function from<br>
- // this module. If so, reuse it the same temporary metadata, otherwise<br>
- // add this temporary metadata to the map.<br>
- if (!ValIDToTempMDMap->count(Idx)) {<br>
- MDNode *Node = cast<MDNode>(MD);<br>
- assert(Node->isTemporary());<br>
- (*ValIDToTempMDMap)[Idx] = Node;<br>
- }<br>
- return (*ValIDToTempMDMap)[Idx];<br>
+ auto I = MetadataToIDs.find(MD);<br>
+ if (I != MetadataToIDs.end()) {<br></blockquote><div><br></div><div>You could reduce indentation by switching this condition around and returning early, if you like:<br><br> if (I == end())<br> return;<br> unsigned Idx = ...;</div><div> ...</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ unsigned Idx = I->second;<br>
+ MDNode *Node = cast<MDNode>(MD);<br>
+ assert(Node->isTemporary());<br>
+ // If we created a temp MD when importing a different function from<br>
+ // this module, reuse the same temporary metadata.<br>
+ auto IterBool = ValIDToTempMDMap->insert(std::make_pair(Idx, Node));<br>
+ return IterBool.first->second;<br>
}<br>
return nullptr;<br>
}<br>
@@ -686,16 +684,18 @@ void IRLinker::replaceTemporaryMetadata(<br>
// created during function importing was provided, and the source<br>
// metadata has a value id recorded during metadata parsing, replace<br>
// the temporary metadata with the final mapped metadata now.<br>
- if (MetadataToIDs.count(OrigMD)) {<br>
- unsigned Idx = MetadataToIDs[OrigMD];<br>
+ auto I = MetadataToIDs.find(OrigMD);<br>
+ if (I != MetadataToIDs.end()) {<br>
+ unsigned Idx = I->second;<br>
+ auto VI = ValIDToTempMDMap->find(Idx);<br>
// Nothing to do if we didn't need to create a temporary metadata during<br>
// function importing.<br>
- if (!ValIDToTempMDMap->count(Idx))<br>
+ if (VI == ValIDToTempMDMap->end())<br>
return;<br>
- MDNode *TempMD = (*ValIDToTempMDMap)[Idx];<br>
+ MDNode *TempMD = VI->second;<br>
TempMD->replaceAllUsesWith(NewMD);<br>
MDNode::deleteTemporary(TempMD);<br>
- ValIDToTempMDMap->erase(Idx);<br>
+ ValIDToTempMDMap->erase(VI);<br>
}<br>
}<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>