2002-05-07 22:11:39 +00:00
|
|
|
//===- ADCE.cpp - Code to perform aggressive dead code elimination --------===//
|
2001-06-30 06:39:11 +00:00
|
|
|
//
|
2002-05-07 22:11:39 +00:00
|
|
|
// This file implements "aggressive" dead code elimination. ADCE is DCe where
|
2001-06-30 06:39:11 +00:00
|
|
|
// values are assumed to be dead until proven otherwise. This is similar to
|
|
|
|
// SCCP, except applied to the liveness of values.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-05-07 22:11:39 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2001-06-30 06:39:11 +00:00
|
|
|
#include "llvm/Type.h"
|
2001-07-06 16:32:07 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/Writer.h"
|
2001-09-09 22:26:47 +00:00
|
|
|
#include "llvm/iTerminators.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iPHINode.h"
|
2002-02-12 21:07:25 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/STLExtras.h"
|
|
|
|
#include "Support/DepthFirstIterator.h"
|
2001-07-08 18:38:36 +00:00
|
|
|
#include <algorithm>
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
using std::cerr;
|
2001-06-30 06:39:11 +00:00
|
|
|
|
2001-09-28 00:06:42 +00:00
|
|
|
#define DEBUG_ADCE 1
|
2001-09-09 22:26:47 +00:00
|
|
|
|
2002-05-06 17:27:57 +00:00
|
|
|
namespace {
|
|
|
|
|
2001-06-30 06:39:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ADCE Class
|
|
|
|
//
|
2002-05-07 22:11:39 +00:00
|
|
|
// This class does all of the work of Aggressive Dead Code Elimination.
|
2001-06-30 06:39:11 +00:00
|
|
|
// It's public interface consists of a constructor and a doADCE() method.
|
|
|
|
//
|
2002-05-06 17:27:57 +00:00
|
|
|
class ADCE : public FunctionPass {
|
|
|
|
Function *Func; // The function that we are working on
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<Instruction*> WorkList; // Instructions that just became live
|
|
|
|
std::set<Instruction*> LiveSet; // The set of live instructions
|
2001-09-09 22:26:47 +00:00
|
|
|
bool MadeChanges;
|
2001-06-30 06:39:11 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// The public interface for this class
|
|
|
|
//
|
|
|
|
public:
|
2002-05-06 17:27:57 +00:00
|
|
|
const char *getPassName() const { return "Aggressive Dead Code Elimination"; }
|
|
|
|
|
2002-05-07 22:11:39 +00:00
|
|
|
// doADCE - Execute the Aggressive Dead Code Elimination Algorithm
|
2002-05-06 17:27:57 +00:00
|
|
|
//
|
|
|
|
virtual bool runOnFunction(Function *F) {
|
|
|
|
Func = F; MadeChanges = false;
|
|
|
|
doADCE(getAnalysis<DominanceFrontier>(DominanceFrontier::PostDomID));
|
|
|
|
assert(WorkList.empty());
|
|
|
|
LiveSet.clear();
|
|
|
|
return MadeChanges;
|
|
|
|
}
|
|
|
|
// getAnalysisUsage - We require post dominance frontiers (aka Control
|
|
|
|
// Dependence Graph)
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired(DominanceFrontier::PostDomID);
|
|
|
|
}
|
2001-06-30 06:39:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// The implementation of this class
|
|
|
|
//
|
|
|
|
private:
|
2002-05-07 22:11:39 +00:00
|
|
|
// doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning
|
2002-05-06 17:27:57 +00:00
|
|
|
// true if the function was modified.
|
|
|
|
//
|
|
|
|
void doADCE(DominanceFrontier &CDG);
|
|
|
|
|
2001-06-30 06:39:11 +00:00
|
|
|
inline void markInstructionLive(Instruction *I) {
|
|
|
|
if (LiveSet.count(I)) return;
|
2001-09-09 22:26:47 +00:00
|
|
|
#ifdef DEBUG_ADCE
|
2001-06-30 06:39:11 +00:00
|
|
|
cerr << "Insn Live: " << I;
|
2001-09-09 22:26:47 +00:00
|
|
|
#endif
|
2001-06-30 06:39:11 +00:00
|
|
|
LiveSet.insert(I);
|
|
|
|
WorkList.push_back(I);
|
|
|
|
}
|
|
|
|
|
2001-07-08 18:38:36 +00:00
|
|
|
inline void markTerminatorLive(const BasicBlock *BB) {
|
2001-09-09 22:26:47 +00:00
|
|
|
#ifdef DEBUG_ADCE
|
|
|
|
cerr << "Terminat Live: " << BB->getTerminator();
|
|
|
|
#endif
|
|
|
|
markInstructionLive((Instruction*)BB->getTerminator());
|
2001-07-08 18:38:36 +00:00
|
|
|
}
|
2001-09-09 22:26:47 +00:00
|
|
|
|
|
|
|
// fixupCFG - Walk the CFG in depth first order, eliminating references to
|
|
|
|
// dead blocks.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
BasicBlock *fixupCFG(BasicBlock *Head, std::set<BasicBlock*> &VisitedBlocks,
|
|
|
|
const std::set<BasicBlock*> &AliveBlocks);
|
2001-06-30 06:39:11 +00:00
|
|
|
};
|
|
|
|
|
2002-05-06 17:27:57 +00:00
|
|
|
} // End of anonymous namespace
|
|
|
|
|
2002-05-07 22:11:39 +00:00
|
|
|
Pass *createAggressiveDCEPass() {
|
2002-05-06 17:27:57 +00:00
|
|
|
return new ADCE();
|
|
|
|
}
|
2001-06-30 06:39:11 +00:00
|
|
|
|
|
|
|
|
2002-05-07 22:11:39 +00:00
|
|
|
// doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning
|
2002-04-27 06:56:12 +00:00
|
|
|
// true if the function was modified.
|
2001-06-30 06:39:11 +00:00
|
|
|
//
|
2002-05-06 17:27:57 +00:00
|
|
|
void ADCE::doADCE(DominanceFrontier &CDG) {
|
2001-09-09 22:26:47 +00:00
|
|
|
#ifdef DEBUG_ADCE
|
2002-05-06 17:27:57 +00:00
|
|
|
cerr << "Function: " << Func;
|
2001-09-09 22:26:47 +00:00
|
|
|
#endif
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
// Iterate over all of the instructions in the function, eliminating trivially
|
2001-06-30 06:39:11 +00:00
|
|
|
// dead instructions, and marking instructions live that are known to be
|
2001-09-09 22:26:47 +00:00
|
|
|
// needed. Perform the walk in depth first order so that we avoid marking any
|
|
|
|
// instructions live in basic blocks that are unreachable. These blocks will
|
|
|
|
// be eliminated later, along with the instructions inside.
|
2001-06-30 06:39:11 +00:00
|
|
|
//
|
2002-05-06 17:27:57 +00:00
|
|
|
for (df_iterator<Function*> BBI = df_begin(Func), BBE = df_end(Func);
|
2001-09-09 22:26:47 +00:00
|
|
|
BBI != BBE; ++BBI) {
|
|
|
|
BasicBlock *BB = *BBI;
|
|
|
|
for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
|
|
|
|
Instruction *I = *II;
|
|
|
|
|
|
|
|
if (I->hasSideEffects() || I->getOpcode() == Instruction::Ret) {
|
|
|
|
markInstructionLive(I);
|
2002-05-07 22:11:39 +00:00
|
|
|
++II; // Increment the inst iterator if the inst wasn't deleted
|
|
|
|
} else if (isInstructionTriviallyDead(I)) {
|
|
|
|
// Remove the instruction from it's basic block...
|
|
|
|
delete BB->getInstList().remove(II);
|
|
|
|
MadeChanges = true;
|
2001-09-09 22:26:47 +00:00
|
|
|
} else {
|
2002-05-07 22:11:39 +00:00
|
|
|
++II; // Increment the inst iterator if the inst wasn't deleted
|
2001-06-30 06:39:11 +00:00
|
|
|
}
|
2001-09-09 22:26:47 +00:00
|
|
|
}
|
2001-06-30 06:39:11 +00:00
|
|
|
}
|
|
|
|
|
2001-09-09 22:26:47 +00:00
|
|
|
#ifdef DEBUG_ADCE
|
2001-06-30 06:39:11 +00:00
|
|
|
cerr << "Processing work list\n";
|
2001-09-09 22:26:47 +00:00
|
|
|
#endif
|
2001-06-30 06:39:11 +00:00
|
|
|
|
2001-07-08 18:38:36 +00:00
|
|
|
// AliveBlocks - Set of basic blocks that we know have instructions that are
|
|
|
|
// alive in them...
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::set<BasicBlock*> AliveBlocks;
|
2001-07-08 18:38:36 +00:00
|
|
|
|
2001-06-30 06:39:11 +00:00
|
|
|
// Process the work list of instructions that just became live... if they
|
|
|
|
// became live, then that means that all of their operands are neccesary as
|
|
|
|
// well... make them live as well.
|
|
|
|
//
|
|
|
|
while (!WorkList.empty()) {
|
2001-07-08 18:38:36 +00:00
|
|
|
Instruction *I = WorkList.back(); // Get an instruction that became live...
|
2001-06-30 06:39:11 +00:00
|
|
|
WorkList.pop_back();
|
|
|
|
|
2001-07-08 18:38:36 +00:00
|
|
|
BasicBlock *BB = I->getParent();
|
|
|
|
if (AliveBlocks.count(BB) == 0) { // Basic block not alive yet...
|
|
|
|
// Mark the basic block as being newly ALIVE... and mark all branches that
|
|
|
|
// this block is control dependant on as being alive also...
|
|
|
|
//
|
|
|
|
AliveBlocks.insert(BB); // Block is now ALIVE!
|
2002-04-28 16:21:30 +00:00
|
|
|
DominanceFrontier::const_iterator It = CDG.find(BB);
|
2001-07-08 18:38:36 +00:00
|
|
|
if (It != CDG.end()) {
|
|
|
|
// Get the blocks that this node is control dependant on...
|
2002-04-28 16:21:30 +00:00
|
|
|
const DominanceFrontier::DomSetType &CDB = It->second;
|
2001-07-08 18:38:36 +00:00
|
|
|
for_each(CDB.begin(), CDB.end(), // Mark all their terminators as live
|
|
|
|
bind_obj(this, &ADCE::markTerminatorLive));
|
|
|
|
}
|
2001-09-09 22:26:47 +00:00
|
|
|
|
|
|
|
// If this basic block is live, then the terminator must be as well!
|
|
|
|
markTerminatorLive(BB);
|
2001-07-08 18:38:36 +00:00
|
|
|
}
|
|
|
|
|
2001-09-09 22:26:47 +00:00
|
|
|
// Loop over all of the operands of the live instruction, making sure that
|
|
|
|
// they are known to be alive as well...
|
|
|
|
//
|
2002-05-07 22:11:39 +00:00
|
|
|
for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op)
|
2001-10-01 16:18:37 +00:00
|
|
|
if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op)))
|
2001-09-09 22:26:47 +00:00
|
|
|
markInstructionLive(Operand);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ADCE
|
2002-04-08 22:03:00 +00:00
|
|
|
cerr << "Current Function: X = Live\n";
|
2002-05-06 17:27:57 +00:00
|
|
|
for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I)
|
2002-02-12 21:07:25 +00:00
|
|
|
for (BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
if (LiveSet.count(*BI)) cerr << "X ";
|
|
|
|
cerr << *BI;
|
|
|
|
}
|
2001-09-09 22:26:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// After the worklist is processed, recursively walk the CFG in depth first
|
|
|
|
// order, patching up references to dead blocks...
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::set<BasicBlock*> VisitedBlocks;
|
2002-05-06 17:27:57 +00:00
|
|
|
BasicBlock *EntryBlock = fixupCFG(Func->front(), VisitedBlocks, AliveBlocks);
|
2001-06-30 06:39:11 +00:00
|
|
|
|
2001-09-09 22:26:47 +00:00
|
|
|
// Now go through and tell dead blocks to drop all of their references so they
|
2002-05-10 15:37:35 +00:00
|
|
|
// can be safely deleted. Also, as we are doing so, if the block has
|
|
|
|
// successors that are still live (and that have PHI nodes in them), remove
|
|
|
|
// the entry for this block from the phi nodes.
|
2001-06-30 06:39:11 +00:00
|
|
|
//
|
2002-05-06 17:27:57 +00:00
|
|
|
for (Function::iterator BI = Func->begin(), BE = Func->end(); BI != BE; ++BI){
|
2001-09-09 22:26:47 +00:00
|
|
|
BasicBlock *BB = *BI;
|
|
|
|
if (!AliveBlocks.count(BB)) {
|
2002-05-10 15:37:35 +00:00
|
|
|
// Remove entries from successors PHI nodes if they are still alive...
|
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
|
|
|
|
if (AliveBlocks.count(*SI)) { // Only if the successor is alive...
|
|
|
|
BasicBlock *Succ = *SI;
|
|
|
|
for (BasicBlock::iterator I = Succ->begin();// Loop over all PHI nodes
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(*I); ++I)
|
|
|
|
PN->removeIncomingValue(BB); // Remove value for this block
|
|
|
|
}
|
|
|
|
|
2001-09-09 22:26:47 +00:00
|
|
|
BB->dropAllReferences();
|
2001-06-30 06:39:11 +00:00
|
|
|
}
|
2001-09-09 22:26:47 +00:00
|
|
|
}
|
2001-06-30 06:39:11 +00:00
|
|
|
|
2002-05-10 15:37:35 +00:00
|
|
|
cerr << "Before Deleting Blocks: " << Func;
|
|
|
|
|
2001-09-09 22:26:47 +00:00
|
|
|
// Now loop through all of the blocks and delete them. We can safely do this
|
|
|
|
// now because we know that there are no references to dead blocks (because
|
|
|
|
// they have dropped all of their references...
|
|
|
|
//
|
2002-05-06 17:27:57 +00:00
|
|
|
for (Function::iterator BI = Func->begin(); BI != Func->end();) {
|
2001-09-09 22:26:47 +00:00
|
|
|
if (!AliveBlocks.count(*BI)) {
|
2002-05-06 17:27:57 +00:00
|
|
|
delete Func->getBasicBlocks().remove(BI);
|
2001-09-09 22:26:47 +00:00
|
|
|
MadeChanges = true;
|
|
|
|
continue; // Don't increment iterator
|
|
|
|
}
|
|
|
|
++BI; // Increment iterator...
|
2001-06-30 06:39:11 +00:00
|
|
|
}
|
2002-05-10 15:37:35 +00:00
|
|
|
|
|
|
|
if (EntryBlock && EntryBlock != Func->front()) {
|
|
|
|
// We need to move the new entry block to be the first bb of the function
|
|
|
|
Function::iterator EBI = find(Func->begin(), Func->end(), EntryBlock);
|
|
|
|
std::swap(*EBI, *Func->begin()); // Exchange old location with start of fn
|
|
|
|
}
|
|
|
|
|
|
|
|
while (PHINode *PN = dyn_cast<PHINode>(EntryBlock->front())) {
|
|
|
|
assert(PN->getNumIncomingValues() == 1 &&
|
|
|
|
"Can only have a single incoming value at this point...");
|
|
|
|
// The incoming value must be outside of the scope of the function, a
|
|
|
|
// global variable, constant or parameter maybe...
|
|
|
|
//
|
|
|
|
PN->replaceAllUsesWith(PN->getIncomingValue(0));
|
|
|
|
|
|
|
|
// Nuke the phi node...
|
|
|
|
delete EntryBlock->getInstList().remove(EntryBlock->begin());
|
|
|
|
}
|
2001-09-09 22:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// fixupCFG - Walk the CFG in depth first order, eliminating references to
|
|
|
|
// dead blocks:
|
|
|
|
// If the BB is alive (in AliveBlocks):
|
|
|
|
// 1. Eliminate all dead instructions in the BB
|
|
|
|
// 2. Recursively traverse all of the successors of the BB:
|
|
|
|
// - If the returned successor is non-null, update our terminator to
|
|
|
|
// reference the returned BB
|
|
|
|
// 3. Return 0 (no update needed)
|
|
|
|
//
|
|
|
|
// If the BB is dead (not in AliveBlocks):
|
|
|
|
// 1. Add the BB to the dead set
|
|
|
|
// 2. Recursively traverse all of the successors of the block:
|
|
|
|
// - Only one shall return a nonnull value (or else this block should have
|
|
|
|
// been in the alive set).
|
|
|
|
// 3. Return the nonnull child, or 0 if no non-null children.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
BasicBlock *ADCE::fixupCFG(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks,
|
|
|
|
const std::set<BasicBlock*> &AliveBlocks) {
|
2001-09-09 22:26:47 +00:00
|
|
|
if (VisitedBlocks.count(BB)) return 0; // Revisiting a node? No update.
|
|
|
|
VisitedBlocks.insert(BB); // We have now visited this node!
|
|
|
|
|
|
|
|
#ifdef DEBUG_ADCE
|
|
|
|
cerr << "Fixing up BB: " << BB;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (AliveBlocks.count(BB)) { // Is the block alive?
|
|
|
|
// Yes it's alive: loop through and eliminate all dead instructions in block
|
2002-05-07 22:11:39 +00:00
|
|
|
for (BasicBlock::iterator II = BB->begin(); II != BB->end()-1; )
|
|
|
|
if (!LiveSet.count(*II)) { // Is this instruction alive?
|
2001-09-09 22:26:47 +00:00
|
|
|
// Nope... remove the instruction from it's basic block...
|
|
|
|
delete BB->getInstList().remove(II);
|
|
|
|
MadeChanges = true;
|
2002-05-07 22:11:39 +00:00
|
|
|
} else {
|
|
|
|
++II;
|
2001-09-09 22:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively traverse successors of this basic block.
|
2002-02-12 22:39:50 +00:00
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) {
|
2001-09-09 22:26:47 +00:00
|
|
|
BasicBlock *Succ = *SI;
|
|
|
|
BasicBlock *Repl = fixupCFG(Succ, VisitedBlocks, AliveBlocks);
|
|
|
|
if (Repl && Repl != Succ) { // We have to replace the successor
|
|
|
|
Succ->replaceAllUsesWith(Repl);
|
|
|
|
MadeChanges = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BB;
|
|
|
|
} else { // Otherwise the block is dead...
|
|
|
|
BasicBlock *ReturnBB = 0; // Default to nothing live down here
|
|
|
|
|
|
|
|
// Recursively traverse successors of this basic block.
|
2002-02-12 22:39:50 +00:00
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) {
|
2001-09-09 22:26:47 +00:00
|
|
|
BasicBlock *RetBB = fixupCFG(*SI, VisitedBlocks, AliveBlocks);
|
|
|
|
if (RetBB) {
|
2002-05-10 15:37:35 +00:00
|
|
|
assert(ReturnBB == 0 && "At most one live child allowed!");
|
2001-09-09 22:26:47 +00:00
|
|
|
ReturnBB = RetBB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ReturnBB; // Return the result of traversal
|
|
|
|
}
|
2001-06-30 06:39:11 +00:00
|
|
|
}
|
|
|
|
|