Commit 5429c82d authored by Johannes Doerfert's avatar Johannes Doerfert
Browse files

[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.
parent ff6254dc
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1192,7 +1192,7 @@ private:
  ///{
  SmallPtrSet<Function *, 8> ToBeDeletedFunctions;
  SmallPtrSet<BasicBlock *, 8> ToBeDeletedBlocks;
  SmallPtrSet<Instruction *, 8> ToBeDeletedInsts;
  SmallDenseSet<WeakVH, 8> ToBeDeletedInsts;
  ///}
};

+8 −6
Original line number Diff line number Diff line
@@ -6131,13 +6131,15 @@ ChangeStatus Attributor::run(Module &M) {
    for (Instruction *I : TerminatorsToFold)
      ConstantFoldTerminator(I->getParent());

    for (Instruction *I : ToBeDeletedInsts) {
    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);