[llvm] f7e9f4f - SCC: Allow ReplaceNode to safely support insertion

Warren Ristow via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 10:33:04 PST 2020


Author: Warren Ristow
Date: 2020-01-14T10:30:24-08:00
New Revision: f7e9f4f4c50245d10ca9869a9f8f3d431dfb6948

URL: https://github.com/llvm/llvm-project/commit/f7e9f4f4c50245d10ca9869a9f8f3d431dfb6948
DIFF: https://github.com/llvm/llvm-project/commit/f7e9f4f4c50245d10ca9869a9f8f3d431dfb6948.diff

LOG: SCC: Allow ReplaceNode to safely support insertion

If scc_iterator::ReplaceNode is inserting a new entry in the map,
rather than replacing an existing entry, the possibility of growing
the map could cause a failure.  This change safely implements the
insertion.

Reviewed By: probinson

Differential Revision: https://reviews.llvm.org/D72469

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SCCIterator.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SCCIterator.h b/llvm/include/llvm/ADT/SCCIterator.h
index eb1a5d0938cf..1e642b9f75d3 100644
--- a/llvm/include/llvm/ADT/SCCIterator.h
+++ b/llvm/include/llvm/ADT/SCCIterator.h
@@ -134,7 +134,10 @@ class scc_iterator : public iterator_facade_base<
   /// has been deleted, and \c New is to be used in its place.
   void ReplaceNode(NodeRef Old, NodeRef New) {
     assert(nodeVisitNumbers.count(Old) && "Old not in scc_iterator?");
-    nodeVisitNumbers[New] = nodeVisitNumbers[Old];
+    // Do the assignment in two steps, in case 'New' is not yet in the map, and
+    // inserting it causes the map to grow.
+    auto tempVal = nodeVisitNumbers[Old];
+    nodeVisitNumbers[New] = tempVal;
     nodeVisitNumbers.erase(Old);
   }
 };


        


More information about the llvm-commits mailing list