2008-05-14 20:38:44 +00:00
|
|
|
//===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file 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
|
|
|
//
|
2007-12-03 19:43:18 +00:00
|
|
|
// This file implements dead code elimination and basic block merging, along
|
|
|
|
// with a collection of other peephole control flow optimizations. For example:
|
2002-05-21 20:49:37 +00:00
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// * Removes basic blocks with no predecessors.
|
|
|
|
// * Merges a basic block into its predecessor if there is only one and the
|
2002-05-21 20:49:37 +00:00
|
|
|
// predecessor only has one successor.
|
2007-11-04 16:15:04 +00:00
|
|
|
// * Eliminates PHI nodes for basic blocks with a single predecessor.
|
|
|
|
// * Eliminates a basic block that only contains an unconditional branch.
|
2007-12-03 19:43:18 +00:00
|
|
|
// * Changes invoke instructions to nounwind functions to be calls.
|
|
|
|
// * Change things like "if (x) if (y)" into "if (x&y)".
|
|
|
|
// * etc..
|
2002-05-21 20:49:37 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
2009-07-03 19:42:02 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2002-05-21 20:49:37 +00:00
|
|
|
#include "llvm/Module.h"
|
2008-09-23 23:03:40 +00:00
|
|
|
#include "llvm/Attributes.h"
|
2002-05-21 20:49:37 +00:00
|
|
|
#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-11-13 07:32:38 +00:00
|
|
|
#include "llvm/ADT/SmallVector.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
|
2008-09-04 17:05:41 +00:00
|
|
|
CFGSimplifyPass() : FunctionPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-05-21 20:49:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGSimplifyPass::ID = 0;
|
|
|
|
static RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
|
|
|
|
|
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-11-14 06:19:25 +00:00
|
|
|
/// ChangeToUnreachable - Insert an unreachable instruction before the specified
|
|
|
|
/// instruction, making it and the rest of the code in the block dead.
|
2009-07-22 00:24:57 +00:00
|
|
|
static void ChangeToUnreachable(Instruction *I, LLVMContext &Context) {
|
2007-11-14 06:19:25 +00:00
|
|
|
BasicBlock *BB = I->getParent();
|
|
|
|
// Loop over all of the successors, removing BB's entry from any PHI
|
|
|
|
// nodes.
|
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
|
|
|
|
(*SI)->removePredecessor(BB);
|
|
|
|
|
|
|
|
new UnreachableInst(I);
|
|
|
|
|
|
|
|
// All instructions after this are dead.
|
|
|
|
BasicBlock::iterator BBI = I, BBE = BB->end();
|
|
|
|
while (BBI != BBE) {
|
|
|
|
if (!BBI->use_empty())
|
2009-07-22 00:24:57 +00:00
|
|
|
BBI->replaceAllUsesWith(Context.getUndef(BBI->getType()));
|
2007-11-14 06:19:25 +00:00
|
|
|
BB->getInstList().erase(BBI++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-22 22:24:59 +00:00
|
|
|
/// ChangeToCall - Convert the specified invoke into a normal call.
|
|
|
|
static void ChangeToCall(InvokeInst *II) {
|
|
|
|
BasicBlock *BB = II->getParent();
|
2009-03-13 21:15:59 +00:00
|
|
|
SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
|
2008-04-06 20:25:17 +00:00
|
|
|
CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args.begin(),
|
|
|
|
Args.end(), "", II);
|
2007-11-22 22:24:59 +00:00
|
|
|
NewCall->takeName(II);
|
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2008-09-25 21:00:45 +00:00
|
|
|
NewCall->setAttributes(II->getAttributes());
|
2007-11-22 22:24:59 +00:00
|
|
|
II->replaceAllUsesWith(NewCall);
|
|
|
|
|
|
|
|
// Follow the call by a branch to the normal destination.
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(II->getNormalDest(), II);
|
2007-11-22 22:24:59 +00:00
|
|
|
|
|
|
|
// Update PHI nodes in the unwind destination
|
|
|
|
II->getUnwindDest()->removePredecessor(BB);
|
|
|
|
BB->getInstList().erase(II);
|
|
|
|
}
|
|
|
|
|
2007-03-04 04:20:48 +00:00
|
|
|
static bool MarkAliveBlocks(BasicBlock *BB,
|
2009-07-03 19:42:02 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 128> &Reachable,
|
2009-07-22 00:24:57 +00:00
|
|
|
LLVMContext &Context) {
|
2007-04-05 01:27:02 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
SmallVector<BasicBlock*, 128> Worklist;
|
2007-04-05 01:27:02 +00:00
|
|
|
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
|
2007-11-22 22:24:59 +00:00
|
|
|
// canonicalizes unreachable insts into stores to null or undef.
|
2007-11-14 06:19:25 +00:00
|
|
|
for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(BBI)) {
|
2007-12-18 09:59:50 +00:00
|
|
|
if (CI->doesNotReturn()) {
|
2007-11-14 06:19:25 +00:00
|
|
|
// If we found a call to a no-return function, insert an unreachable
|
|
|
|
// instruction after it. Make sure there isn't *already* one there
|
|
|
|
// though.
|
|
|
|
++BBI;
|
|
|
|
if (!isa<UnreachableInst>(BBI)) {
|
2009-07-03 19:42:02 +00:00
|
|
|
ChangeToUnreachable(BBI, Context);
|
2007-11-14 06:19:25 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 21:01:07 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
|
|
|
|
Value *Ptr = SI->getOperand(1);
|
2009-07-14 17:48:25 +00:00
|
|
|
|
|
|
|
if (isa<UndefValue>(Ptr) ||
|
|
|
|
(isa<ConstantPointerNull>(Ptr) &&
|
|
|
|
cast<PointerType>(Ptr->getType())->getAddressSpace() == 0)) {
|
|
|
|
ChangeToUnreachable(SI, Context);
|
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-06-12 21:01:07 +00:00
|
|
|
}
|
2007-11-14 06:19:25 +00:00
|
|
|
}
|
2004-10-18 03:00:50 +00:00
|
|
|
|
2007-11-22 22:24:59 +00:00
|
|
|
// Turn invokes that call 'nounwind' functions into ordinary calls.
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
|
2007-12-18 09:59:50 +00:00
|
|
|
if (II->doesNotThrow()) {
|
2007-11-22 22:24:59 +00:00
|
|
|
ChangeToCall(II);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-09-08 22:14:17 +00:00
|
|
|
/// RemoveUnreachableBlocksFromFn - Remove blocks that are not reachable, even
|
|
|
|
/// if they are in a dead cycle. Return true if a change was made, false
|
|
|
|
/// otherwise.
|
|
|
|
static bool RemoveUnreachableBlocksFromFn(Function &F) {
|
2007-11-13 07:32:38 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 128> Reachable;
|
2009-07-03 19:42:02 +00:00
|
|
|
bool Changed = MarkAliveBlocks(F.begin(), Reachable, F.getContext());
|
2007-11-13 07:32:38 +00:00
|
|
|
|
2002-05-21 20:49:37 +00:00
|
|
|
// If there are unreachable blocks in the CFG...
|
2008-01-26 06:51:24 +00:00
|
|
|
if (Reachable.size() == F.size())
|
2007-11-13 07:32:38 +00:00
|
|
|
return Changed;
|
|
|
|
|
|
|
|
assert(Reachable.size() < F.size());
|
|
|
|
NumSimpl += F.size()-Reachable.size();
|
|
|
|
|
|
|
|
// Loop over all of the basic blocks that are not reachable, dropping all of
|
|
|
|
// their internal references...
|
2008-02-14 07:39:01 +00:00
|
|
|
for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) {
|
|
|
|
if (Reachable.count(BB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
|
|
|
|
if (Reachable.count(*SI))
|
|
|
|
(*SI)->removePredecessor(BB);
|
|
|
|
BB->dropAllReferences();
|
|
|
|
}
|
2007-11-13 07:32:38 +00:00
|
|
|
|
|
|
|
for (Function::iterator I = ++F.begin(); I != F.end();)
|
|
|
|
if (!Reachable.count(I))
|
|
|
|
I = F.getBasicBlockList().erase(I);
|
|
|
|
else
|
|
|
|
++I;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2002-05-21 20:49:37 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
/// IterativeSimplifyCFG - Call SimplifyCFG on all the blocks in the function,
|
|
|
|
/// iterating until no more changes are made.
|
|
|
|
static bool IterativeSimplifyCFG(Function &F) {
|
|
|
|
bool Changed = false;
|
2002-05-21 20:49:37 +00:00
|
|
|
bool LocalChange = true;
|
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
2007-11-13 07:32:38 +00:00
|
|
|
|
2002-05-21 20:49:37 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2007-11-13 07:32:38 +00:00
|
|
|
|
|
|
|
// It is possible that we may require multiple passes over the code to fully
|
|
|
|
// simplify the CFG.
|
|
|
|
//
|
|
|
|
bool CFGSimplifyPass::runOnFunction(Function &F) {
|
2008-09-08 22:14:17 +00:00
|
|
|
bool EverChanged = RemoveUnreachableBlocksFromFn(F);
|
2007-11-13 07:32:38 +00:00
|
|
|
EverChanged |= IterativeSimplifyCFG(F);
|
|
|
|
|
|
|
|
// If neither pass changed anything, we're done.
|
|
|
|
if (!EverChanged) return false;
|
|
|
|
|
|
|
|
// IterativeSimplifyCFG can (rarely) make some loops dead. If this happens,
|
2008-09-08 22:14:17 +00:00
|
|
|
// RemoveUnreachableBlocksFromFn is needed to nuke them, which means we should
|
2007-11-13 07:32:38 +00:00
|
|
|
// iterate between the two optimizations. We structure the code like this to
|
|
|
|
// avoid reruning IterativeSimplifyCFG if the second pass of
|
2008-09-08 22:14:17 +00:00
|
|
|
// RemoveUnreachableBlocksFromFn doesn't do anything.
|
|
|
|
if (!RemoveUnreachableBlocksFromFn(F))
|
2007-11-13 07:32:38 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
do {
|
|
|
|
EverChanged = IterativeSimplifyCFG(F);
|
2008-09-08 22:14:17 +00:00
|
|
|
EverChanged |= RemoveUnreachableBlocksFromFn(F);
|
2007-11-13 07:32:38 +00:00
|
|
|
} while (EverChanged);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|