Index: lib/Transforms/Scalar/InferAddressSpaces.cpp =================================================================== --- lib/Transforms/Scalar/InferAddressSpaces.cpp +++ lib/Transforms/Scalar/InferAddressSpaces.cpp @@ -815,6 +815,8 @@ NewV->setOperand(OperandNo, ValueWithNewAddrSpace.lookup(UndefUse->get())); } + std::vector DeadInstructions; + // Replaces the uses of the old address expressions with the new ones. for (Value *V : Postorder) { Value *NewV = ValueWithNewAddrSpace.lookup(V); @@ -888,7 +890,7 @@ unsigned NewAS = NewV->getType()->getPointerAddressSpace(); if (ASC->getDestAddressSpace() == NewAS) { ASC->replaceAllUsesWith(NewV); - ASC->eraseFromParent(); + DeadInstructions.push_back(ASC); continue; } } @@ -906,10 +908,15 @@ } } - if (V->use_empty()) - RecursivelyDeleteTriviallyDeadInstructions(V); + if (V->use_empty()) { + if (Instruction *I = dyn_cast(V)) + DeadInstructions.push_back(I); + } } + for (Instruction *I : DeadInstructions) + RecursivelyDeleteTriviallyDeadInstructions(I); + return true; }