Don't modify the DenseMap being iterated over from within the loop
authorSanjoy Das <sanjoy@playingwithpointers.com>
Fri, 27 Feb 2015 02:24:16 +0000 (02:24 +0000)
committerSanjoy Das <sanjoy@playingwithpointers.com>
Fri, 27 Feb 2015 02:24:16 +0000 (02:24 +0000)
that is iterating over it

Inserting elements into a `DenseMap` invalidated iterators pointing
into the `DenseMap` instance.

Differential Revision: http://reviews.llvm.org/D7924

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230719 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenPrepare.cpp

index c0d7dca277526573c524bd575f72afffe96813f1..81a149a99e3696c9e02f4e392b7a871714d7ad53 100644 (file)
@@ -561,12 +561,15 @@ static void computeBaseDerivedRelocateMap(
 
     IntrinsicInst *I = Item.second;
     auto BaseKey = std::make_pair(Key.first, Key.first);
-    IntrinsicInst *Base = RelocateIdxMap[BaseKey];
-    if (!Base)
+
+    // We're iterating over RelocateIdxMap so we cannot modify it.
+    auto MaybeBase = RelocateIdxMap.find(BaseKey);
+    if (MaybeBase == RelocateIdxMap.end())
       // TODO: We might want to insert a new base object relocate and gep off
       // that, if there are enough derived object relocates.
       continue;
-    RelocateInstMap[Base].push_back(I);
+
+    RelocateInstMap[MaybeBase->second].push_back(I);
   }
 }