diff --git a/lib/VMCore/LLVMContext.cpp b/lib/VMCore/LLVMContext.cpp index 0ed21fb754e..887679e08d3 100644 --- a/lib/VMCore/LLVMContext.cpp +++ b/lib/VMCore/LLVMContext.cpp @@ -17,6 +17,7 @@ #include "llvm/Constants.h" #include "llvm/Instruction.h" #include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/ValueHandle.h" #include "LLVMContextImpl.h" #include @@ -45,24 +46,25 @@ GetElementPtrConstantExpr::GetElementPtrConstantExpr } bool LLVMContext::RemoveDeadMetadata() { - std::vector DeadMDNodes; + std::vector DeadMDNodes; bool Changed = false; while (1) { for (FoldingSet::iterator I = pImpl->MDNodeSet.begin(), E = pImpl->MDNodeSet.end(); I != E; ++I) { - const MDNode *N = &(*I); + MDNode *N = &(*I); if (N->use_empty()) - DeadMDNodes.push_back(N); + DeadMDNodes.push_back(WeakVH(N)); } if (DeadMDNodes.empty()) return Changed; while (!DeadMDNodes.empty()) { - const MDNode *N = DeadMDNodes.back(); DeadMDNodes.pop_back(); - delete N; + const MDNode *N = cast(DeadMDNodes.back()); DeadMDNodes.pop_back(); + if (N->use_empty()) + delete N; } } return Changed;