[Attributor][FIX] Avoid dangling pointers during code deletion
It can happen that we have instructions in the ToBeDeletedInsts set
which are deleted earlier already. To avoid dangling pointers we use
weak tracking handles.
diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp
index c65925b..648dc8f 100644
--- a/llvm/lib/Transforms/IPO/Attributor.cpp
+++ b/llvm/lib/Transforms/IPO/Attributor.cpp
@@ -6131,12 +6131,14 @@
for (Instruction *I : TerminatorsToFold)
ConstantFoldTerminator(I->getParent());
- for (Instruction *I : ToBeDeletedInsts) {
- I->replaceAllUsesWith(UndefValue::get(I->getType()));
- if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
- DeadInsts.push_back(I);
- else
- I->eraseFromParent();
+ for (auto &V : ToBeDeletedInsts) {
+ if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
+ I->replaceAllUsesWith(UndefValue::get(I->getType()));
+ if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
+ DeadInsts.push_back(I);
+ else
+ I->eraseFromParent();
+ }
}
RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);