//
//===----------------------------------------------------------------------===//
-#include "llvm/Transforms/Scalar/DCE.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/Local.h"
+#include "llvm/Instruction.h"
#include "llvm/Pass.h"
-#include "llvm/InstrTypes.h"
-#include "llvm/Function.h"
#include "llvm/Support/InstIterator.h"
+#include "Support/StatisticReporter.h"
#include <set>
-static inline bool isInstDead(Instruction *I) {
- return I->use_empty() && !I->hasSideEffects() && !isa<TerminatorInst>(I);
-}
-
-// dceInstruction - Inspect the instruction at *BBI and figure out if it's
-// [trivially] dead. If so, remove the instruction and update the iterator
-// to point to the instruction that immediately succeeded the original
-// instruction.
-//
-bool dceInstruction(BasicBlock::InstListType &BBIL,
- BasicBlock::iterator &BBI) {
- // Look for un"used" definitions...
- if (isInstDead(*BBI)) {
- delete BBIL.remove(BBI); // Bye bye
- return true;
- }
- return false;
-}
+static Statistic<> DIEEliminated("die\t\t- Number of insts removed");
+static Statistic<> DCEEliminated("dce\t\t- Number of insts removed");
//===----------------------------------------------------------------------===//
// DeadInstElimination pass implementation
struct DeadInstElimination : public BasicBlockPass {
const char *getPassName() const { return "Dead Instruction Elimination"; }
- virtual bool runOnBasicBlock(BasicBlock *BB) {
- BasicBlock::InstListType &Vals = BB->getInstList();
+ virtual bool runOnBasicBlock(BasicBlock &BB) {
bool Changed = false;
- for (BasicBlock::iterator DI = Vals.begin(); DI != Vals.end(); )
- if (dceInstruction(Vals, DI))
+ for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); )
+ if (dceInstruction(DI)) {
Changed = true;
- else
+ ++DIEEliminated;
+ } else
++DI;
return Changed;
}
struct DCE : public FunctionPass {
const char *getPassName() const { return "Dead Code Elimination"; }
- virtual bool runOnFunction(Function *F);
+ virtual bool runOnFunction(Function &F);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.preservesCFG();
};
}
-bool DCE::runOnFunction(Function *F) {
+bool DCE::runOnFunction(Function &F) {
// Start out with all of the instructions in the worklist...
std::vector<Instruction*> WorkList(inst_begin(F), inst_end(F));
std::set<Instruction*> DeadInsts;
Instruction *I = WorkList.back();
WorkList.pop_back();
- if (isInstDead(I)) { // If the instruction is dead...
+ if (isInstructionTriviallyDead(I)) { // If the instruction is dead...
// Loop over all of the values that the instruction uses, if there are
// instructions being used, add them to the worklist, because they might
// go dead after this one is removed.
if (DeadInsts.empty()) return false;
// Otherwise, loop over the program, removing and deleting the instructions...
- for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
- BasicBlock::InstListType &BBIL = (*I)->getInstList();
- for (BasicBlock::iterator BI = BBIL.begin(); BI != BBIL.end(); )
- if (DeadInsts.count(*BI)) { // Is this instruction dead?
- delete BBIL.remove(BI); // Yup, remove and delete inst
+ for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
+ for (BasicBlock::iterator BI = I->begin(); BI != I->end(); )
+ if (DeadInsts.count(BI)) { // Is this instruction dead?
+ BI = I->getInstList().erase(BI); // Yup, remove and delete inst
+ ++DCEEliminated;
} else { // This instruction is not dead
++BI; // Continue on to the next one...
}
- }
return true;
}