2002-05-21 20:49:37 +00:00
|
|
|
//===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-21 20:49:37 +00:00
|
|
|
//
|
|
|
|
// This file implements dead code elimination and basic block merging.
|
|
|
|
//
|
|
|
|
// Specifically, this:
|
|
|
|
// * removes basic blocks with no predecessors
|
|
|
|
// * merges a basic block into its predecessor if there is only one and the
|
|
|
|
// predecessor only has one successor.
|
|
|
|
// * Eliminates PHI nodes for basic blocks with a single predecessor
|
|
|
|
// * Eliminates a basic block that only contains an unconditional branch
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-10-18 03:00:50 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2002-05-21 20:49:37 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Support/CFG.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2002-05-21 20:49:37 +00:00
|
|
|
#include <set>
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-05-21 20:49:37 +00:00
|
|
|
namespace {
|
2002-10-01 22:38:41 +00:00
|
|
|
Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified");
|
|
|
|
|
2002-05-21 20:49:37 +00:00
|
|
|
struct CFGSimplifyPass : public FunctionPass {
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-05-21 20:49:37 +00:00
|
|
|
};
|
2002-07-26 21:12:46 +00:00
|
|
|
RegisterOpt<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
|
2002-05-21 20:49:37 +00:00
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
// Public interface to the CFGSimplification pass
|
2004-01-09 06:02:20 +00:00
|
|
|
FunctionPass *llvm::createCFGSimplificationPass() {
|
2002-05-21 20:49:37 +00:00
|
|
|
return new CFGSimplifyPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
|
|
|
|
if (Reachable.count(BB)) return false;
|
|
|
|
Reachable.insert(BB);
|
|
|
|
|
2004-10-18 03:00:50 +00:00
|
|
|
// Do a quick scan of the basic block, turning any obviously unreachable
|
|
|
|
// instructions into LLVM unreachable insts. The instruction combining pass
|
|
|
|
// canonnicalizes unreachable insts into stores to null or undef.
|
|
|
|
for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ++BBI)
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
|
|
|
|
if (isa<ConstantPointerNull>(SI->getOperand(1)) ||
|
|
|
|
isa<UndefValue>(SI->getOperand(1))) {
|
|
|
|
// Loop over all of the successors, removing BB's entry from any PHI
|
|
|
|
// nodes.
|
|
|
|
for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
|
|
|
|
(*I)->removePredecessor(BB);
|
|
|
|
|
|
|
|
new UnreachableInst(SI);
|
|
|
|
|
|
|
|
// All instructions after this are dead.
|
|
|
|
for (; BBI != E; ) {
|
|
|
|
if (!BBI->use_empty())
|
|
|
|
BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
|
|
|
|
BB->getInstList().erase(BBI++);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-21 20:49:37 +00:00
|
|
|
bool Changed = ConstantFoldTerminator(BB);
|
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
|
|
|
|
MarkAliveBlocks(*SI, Reachable);
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// It is possible that we may require multiple passes over the code to fully
|
|
|
|
// simplify the CFG.
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool CFGSimplifyPass::runOnFunction(Function &F) {
|
2002-05-21 20:49:37 +00:00
|
|
|
std::set<BasicBlock*> Reachable;
|
2002-06-25 16:13:24 +00:00
|
|
|
bool Changed = MarkAliveBlocks(F.begin(), Reachable);
|
2002-05-21 20:49:37 +00:00
|
|
|
|
|
|
|
// If there are unreachable blocks in the CFG...
|
2002-06-25 16:13:24 +00:00
|
|
|
if (Reachable.size() != F.size()) {
|
|
|
|
assert(Reachable.size() < F.size());
|
|
|
|
NumSimpl += F.size()-Reachable.size();
|
2002-05-21 20:49:37 +00:00
|
|
|
|
|
|
|
// Loop over all of the basic blocks that are not reachable, dropping all of
|
|
|
|
// their internal references...
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
if (!Reachable.count(BB)) {
|
2002-05-21 20:49:37 +00:00
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
|
|
|
|
if (Reachable.count(*SI))
|
|
|
|
(*SI)->removePredecessor(BB);
|
|
|
|
BB->dropAllReferences();
|
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Function::iterator I = ++F.begin(); I != F.end();)
|
|
|
|
if (!Reachable.count(I))
|
|
|
|
I = F.getBasicBlockList().erase(I);
|
2002-05-21 20:49:37 +00:00
|
|
|
else
|
|
|
|
++I;
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalChange = true;
|
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
|
|
|
|
|
|
|
// Loop over all of the basic blocks (except the first one) and remove them
|
|
|
|
// if they are unneeded...
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
|
|
|
|
if (SimplifyCFG(BBIt++)) {
|
2002-05-21 20:49:37 +00:00
|
|
|
LocalChange = true;
|
|
|
|
++NumSimpl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Changed |= LocalChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|