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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
#define DEBUG_TYPE "simplifycfg"
|
2002-05-21 20:49:37 +00:00
|
|
|
#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"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2002-05-21 20:49:37 +00:00
|
|
|
#include "llvm/Pass.h"
|
2007-03-04 04:20:48 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
STATISTIC(NumSimpl, "Number of blocks simplified");
|
2002-10-01 22:38:41 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
struct VISIBILITY_HIDDEN CFGSimplifyPass : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-05-01 21:15:47 +00:00
|
|
|
CFGSimplifyPass() : FunctionPass((intptr_t)&ID) {}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-05-21 20:49:37 +00:00
|
|
|
};
|
2007-05-03 01:11:54 +00:00
|
|
|
char CFGSimplifyPass::ID = 0;
|
2006-08-27 22:42:52 +00:00
|
|
|
RegisterPass<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();
|
|
|
|
}
|
|
|
|
|
2007-03-04 04:20:48 +00:00
|
|
|
static bool MarkAliveBlocks(BasicBlock *BB,
|
|
|
|
SmallPtrSet<BasicBlock*, 16> &Reachable) {
|
2007-04-05 01:27:02 +00:00
|
|
|
|
|
|
|
std::vector<BasicBlock*> Worklist;
|
|
|
|
Worklist.push_back(BB);
|
|
|
|
bool Changed = false;
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
BB = Worklist.back();
|
|
|
|
Worklist.pop_back();
|
|
|
|
|
|
|
|
if (!Reachable.insert(BB))
|
|
|
|
continue;
|
2002-05-21 20:49:37 +00:00
|
|
|
|
2007-04-05 01:27:02 +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);
|
2004-10-18 03:00:50 +00:00
|
|
|
|
2007-04-05 01:27:02 +00:00
|
|
|
new UnreachableInst(SI);
|
2004-10-18 03:00:50 +00:00
|
|
|
|
2007-04-05 01:27:02 +00:00
|
|
|
// All instructions after this are dead.
|
|
|
|
while (BBI != E) {
|
|
|
|
if (!BBI->use_empty())
|
|
|
|
BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
|
|
|
|
BB->getInstList().erase(BBI++);
|
|
|
|
}
|
|
|
|
break;
|
2004-10-18 03:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-05 01:27:02 +00:00
|
|
|
Changed |= ConstantFoldTerminator(BB);
|
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
|
|
|
|
Worklist.push_back(*SI);
|
|
|
|
}
|
2002-05-21 20:49:37 +00:00
|
|
|
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) {
|
2007-03-04 04:20:48 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 16> 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;
|
|
|
|
}
|