[SCEV] Fix usage of invalid IP with FoldingSet
authorEhud Katz <ehudkatz@gmail.com>
Fri, 13 Mar 2020 16:31:47 +0000 (18:31 +0200)
committerEhud Katz <ehudkatz@gmail.com>
Fri, 13 Mar 2020 16:36:58 +0000 (18:36 +0200)
Fix the use of invalid Insertion Point pointer with the UniqueSCEVs FoldingSet,
which caused memory corruption.

llvm/lib/Analysis/ScalarEvolution.cpp

index debd03b..030de8c 100644 (file)
@@ -3339,6 +3339,10 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
     }
   }
 
+  // The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs
+  // changes). Make sure we get a new one.
+  IP = nullptr;
+  if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
   SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
                                              LHS, RHS);
   UniqueSCEVs.InsertNode(S, IP);