[JITLink] Fix edge removal iterator invalidation.
authorLang Hames <lhames@gmail.com>
Wed, 22 Apr 2020 17:18:44 +0000 (10:18 -0700)
committerLang Hames <lhames@gmail.com>
Wed, 22 Apr 2020 21:16:46 +0000 (14:16 -0700)
This patch changes Block::removeEdge to return a valid iterator to the new next
element, and uses this to update the edge removal algorithm in
LinkGraph::splitBlock.

llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
llvm/lib/ExecutionEngine/JITLink/JITLink.cpp

index 967397f..000dd18 100644 (file)
@@ -248,8 +248,8 @@ public:
   bool edges_empty() const { return Edges.empty(); }
 
   /// Remove the edge pointed to by the given iterator.
-  /// Invalidates all iterators that point to or past the given one.
-  void removeEdge(const_edge_iterator I) { Edges.erase(I); }
+  /// Returns an iterator to the new next element.
+  edge_iterator removeEdge(edge_iterator I) { return Edges.erase(I); }
 
 private:
   static constexpr uint64_t MaxAlignmentOffset = (1ULL << 57) - 1;
index 6c924f8..c60076e 100644 (file)
@@ -180,18 +180,14 @@ Block &LinkGraph::splitBlock(Block &B, size_t SplitIndex,
     // Copy edges to NewBlock (recording their iterators so that we can remove
     // them from B), and update of Edges remaining on B.
     std::vector<Block::edge_iterator> EdgesToRemove;
-    for (auto I = B.edges().begin(), E = B.edges().end(); I != E; ++I) {
+    for (auto I = B.edges().begin(); I != B.edges().end();) {
       if (I->getOffset() < SplitIndex) {
         NewBlock.addEdge(*I);
-        EdgesToRemove.push_back(I);
-      } else
+        I = B.removeEdge(I);
+      } else {
         I->setOffset(I->getOffset() - SplitIndex);
-    }
-
-    // Remove edges that were transfered to NewBlock from B.
-    while (!EdgesToRemove.empty()) {
-      B.removeEdge(EdgesToRemove.back());
-      EdgesToRemove.pop_back();
+        ++I;
+      }
     }
   }