#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Type.h"
-#include "llvm/Analysis/Dominators.h"
+#include "llvm/Analysis/PostDominators.h"
#include "llvm/iTerminators.h"
#include "llvm/iPHINode.h"
#include "llvm/Constant.h"
#include "llvm/Support/CFG.h"
#include "Support/STLExtras.h"
#include "Support/DepthFirstIterator.h"
-#include "Support/StatisticReporter.h"
+#include "Support/Statistic.h"
#include <algorithm>
-#include <iostream>
using std::cerr;
using std::vector;
-static Statistic<> NumBlockRemoved("adce\t\t- Number of basic blocks removed");
-static Statistic<> NumInstRemoved ("adce\t\t- Number of instructions removed");
-
namespace {
+ Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed");
+ Statistic<> NumInstRemoved ("adce", "Number of instructions removed");
//===----------------------------------------------------------------------===//
// ADCE Class
// getAnalysisUsage - We require post dominance frontiers (aka Control
// Dependence Graph)
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequired(PostDominatorTree::ID);
- AU.addRequired(PostDominanceFrontier::ID);
+ AU.addRequired<PostDominatorTree>();
+ AU.addRequired<PostDominanceFrontier>();
}
void markBlockAlive(BasicBlock *BB);
+
+ // dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the
+ // instructions in the specified basic block, dropping references on
+ // instructions that are dead according to LiveSet.
+ bool dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB);
+
inline void markInstructionLive(Instruction *I) {
if (LiveSet.count(I)) return;
DEBUG(cerr << "Insn Live: " << I);
markTerminatorLive(BB);
}
+// dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the
+// instructions in the specified basic block, dropping references on
+// instructions that are dead according to LiveSet.
+bool ADCE::dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB) {
+ bool Changed = false;
+ for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; )
+ if (!LiveSet.count(I)) { // Is this instruction alive?
+ I->dropAllReferences(); // Nope, drop references...
+ if (PHINode *PN = dyn_cast<PHINode>(&*I)) {
+ // We don't want to leave PHI nodes in the program that have
+ // #arguments != #predecessors, so we remove them now.
+ //
+ PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
+
+ // Delete the instruction...
+ I = BB->getInstList().erase(I);
+ Changed = true;
+ } else {
+ ++I;
+ }
+ } else {
+ ++I;
+ }
+ return Changed;
+}
+
// doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning
// true if the function was modified.
//
PostDominatorTree &DT = getAnalysis<PostDominatorTree>();
- // If there are some blocks dead...
- if (AliveBlocks.size() != Func->size()) {
- // Insert a new entry node to eliminate the entry node as a special case.
- BasicBlock *NewEntry = new BasicBlock();
- NewEntry->getInstList().push_back(new BranchInst(&Func->front()));
- Func->getBasicBlockList().push_front(NewEntry);
- AliveBlocks.insert(NewEntry); // This block is always alive!
+
+ if (AliveBlocks.size() == Func->size()) { // No dead blocks?
+ for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I)
+ // Loop over all of the instructions in the function, telling dead
+ // instructions to drop their references. This is so that the next sweep
+ // over the program can safely delete dead instructions without other dead
+ // instructions still refering to them.
+ //
+ dropReferencesOfDeadInstructionsInLiveBlock(I);
+
+ } else { // If there are some blocks dead...
+ // If the entry node is dead, insert a new entry node to eliminate the entry
+ // node as a special case.
+ //
+ if (!AliveBlocks.count(&Func->front())) {
+ BasicBlock *NewEntry = new BasicBlock();
+ NewEntry->getInstList().push_back(new BranchInst(&Func->front()));
+ Func->getBasicBlockList().push_front(NewEntry);
+ AliveBlocks.insert(NewEntry); // This block is always alive!
+ }
// Loop over all of the alive blocks in the function. If any successor
// blocks are not alive, we adjust the outgoing branches to branch to the
// sweep over the program can safely delete dead instructions without
// other dead instructions still refering to them.
//
- for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; )
- if (!LiveSet.count(I)) { // Is this instruction alive?
- I->dropAllReferences(); // Nope, drop references...
- if (PHINode *PN = dyn_cast<PHINode>(&*I)) {
- // We don't want to leave PHI nodes in the program that have
- // #arguments != #predecessors, so we remove them now.
- //
- PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
-
- // Delete the instruction...
- I = BB->getInstList().erase(I);
- } else {
- ++I;
- }
- } else {
- ++I;
- }
+ dropReferencesOfDeadInstructionsInLiveBlock(BB);
}
}
// Delete the old terminator instruction...
BB->getInstList().pop_back();
const Type *RetTy = Func->getReturnType();
- Instruction *New = new ReturnInst(RetTy != Type::VoidTy ?
- Constant::getNullValue(RetTy) : 0);
- BB->getInstList().push_back(New);
+ BB->getInstList().push_back(new ReturnInst(RetTy != Type::VoidTy ?
+ Constant::getNullValue(RetTy) : 0));
}
BB->dropAllReferences();
// instructions from alive blocks.
//
for (Function::iterator BI = Func->begin(); BI != Func->end(); )
- if (!AliveBlocks.count(BI))
+ if (!AliveBlocks.count(BI)) { // Delete dead blocks...
BI = Func->getBasicBlockList().erase(BI);
- else {
+ } else { // Scan alive blocks...
for (BasicBlock::iterator II = BI->begin(); II != --BI->end(); )
if (!LiveSet.count(II)) { // Is this instruction alive?
// Nope... remove the instruction from it's basic block...