diff --git a/llvm/include/llvm/Transforms/Utils/Local.h b/llvm/include/llvm/Transforms/Utils/Local.h index 65438aa5e4ad..3fb053572332 100644 --- a/llvm/include/llvm/Transforms/Utils/Local.h +++ b/llvm/include/llvm/Transforms/Utils/Local.h @@ -24,7 +24,7 @@ class PHINode; class AllocaInst; class ConstantExpr; class TargetData; - + //===----------------------------------------------------------------------===// // Local constant propagation. // @@ -49,7 +49,11 @@ bool isInstructionTriviallyDead(Instruction *I); /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a /// trivially dead instruction, delete it. If that makes any of its operands /// trivially dead, delete them too, recursively. -void RecursivelyDeleteTriviallyDeadInstructions(Value *V); +/// +/// If DeadInst is specified, the vector is filled with the instructions that +/// are actually deleted. +void RecursivelyDeleteTriviallyDeadInstructions(Value *V, + SmallVectorImpl *DeadInst = 0); //===----------------------------------------------------------------------===// // Control Flow Graph Restructuring... diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index e3a0917eb02b..1e1b3869446e 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -176,7 +176,11 @@ bool llvm::isInstructionTriviallyDead(Instruction *I) { /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a /// trivially dead instruction, delete it. If that makes any of its operands /// trivially dead, delete them too, recursively. -void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) { +/// +/// If DeadInst is specified, the vector is filled with the instructions that +/// are actually deleted. +void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V, + SmallVectorImpl *DeadInst) { Instruction *I = dyn_cast(V); if (!I || !I->use_empty()) return; @@ -186,12 +190,16 @@ void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) { while (!Insts.empty()) { I = *Insts.begin(); Insts.erase(I); - if (isInstructionTriviallyDead(I)) { - for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) - if (Instruction *U = dyn_cast(I->getOperand(i))) - Insts.insert(U); - I->eraseFromParent(); - } + if (!isInstructionTriviallyDead(I)) + continue; + + for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) + if (Instruction *U = dyn_cast(I->getOperand(i))) + Insts.insert(U); + I->eraseFromParent(); + + if (DeadInst) + DeadInst->push_back(I); } }