2008-05-14 22:38:44 +02:00
|
|
|
//===- SimplifyCFGPass.cpp - CFG Simplification Pass ----------------------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-21 22:49:37 +02:00
|
|
|
//
|
2007-12-03 20:43:18 +01: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 22:49:37 +02:00
|
|
|
//
|
2007-11-04 17:15:04 +01:00
|
|
|
// * Removes basic blocks with no predecessors.
|
|
|
|
// * Merges a basic block into its predecessor if there is only one and the
|
2002-05-21 22:49:37 +02:00
|
|
|
// predecessor only has one successor.
|
2007-11-04 17:15:04 +01:00
|
|
|
// * Eliminates PHI nodes for basic blocks with a single predecessor.
|
|
|
|
// * Eliminates a basic block that only contains an unconditional branch.
|
2007-12-03 20:43:18 +01: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 22:49:37 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
#define DEBUG_TYPE "simplifycfg"
|
2002-05-21 22:49:37 +02:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-10-18 05:00:50 +02:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2002-05-21 22:49:37 +02:00
|
|
|
#include "llvm/Module.h"
|
2008-09-24 01:03:40 +02:00
|
|
|
#include "llvm/Attributes.h"
|
2002-05-21 22:49:37 +02:00
|
|
|
#include "llvm/Support/CFG.h"
|
|
|
|
#include "llvm/Pass.h"
|
2007-11-13 08:32:38 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-03-04 05:20:48 +01:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-01-09 07:02:20 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
STATISTIC(NumSimpl, "Number of blocks simplified");
|
2002-10-02 00:38:41 +02:00
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
namespace {
|
2009-09-02 08:11:42 +02:00
|
|
|
struct CFGSimplifyPass : public FunctionPass {
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 19:05:41 +02:00
|
|
|
CFGSimplifyPass() : FunctionPass(&ID) {}
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2002-06-25 18:13:24 +02:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-05-21 22:49:37 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char CFGSimplifyPass::ID = 0;
|
|
|
|
static RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
// Public interface to the CFGSimplification pass
|
2004-01-09 07:02:20 +01:00
|
|
|
FunctionPass *llvm::createCFGSimplificationPass() {
|
2002-05-21 22:49:37 +02:00
|
|
|
return new CFGSimplifyPass();
|
|
|
|
}
|
|
|
|
|
2007-11-14 07:19:25 +01:00
|
|
|
/// ChangeToUnreachable - Insert an unreachable instruction before the specified
|
|
|
|
/// instruction, making it and the rest of the code in the block dead.
|
2009-11-23 04:34:29 +01:00
|
|
|
static void ChangeToUnreachable(Instruction *I) {
|
2007-11-14 07:19:25 +01: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);
|
|
|
|
|
2009-08-13 23:58:54 +02:00
|
|
|
new UnreachableInst(I->getContext(), I);
|
2007-11-14 07:19:25 +01:00
|
|
|
|
|
|
|
// All instructions after this are dead.
|
|
|
|
BasicBlock::iterator BBI = I, BBE = BB->end();
|
|
|
|
while (BBI != BBE) {
|
|
|
|
if (!BBI->use_empty())
|
2009-07-31 01:03:37 +02:00
|
|
|
BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
|
2007-11-14 07:19:25 +01:00
|
|
|
BB->getInstList().erase(BBI++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-22 23:24:59 +01:00
|
|
|
/// ChangeToCall - Convert the specified invoke into a normal call.
|
|
|
|
static void ChangeToCall(InvokeInst *II) {
|
|
|
|
BasicBlock *BB = II->getParent();
|
2009-09-03 04:02:59 +02:00
|
|
|
SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
|
2008-04-06 22:25:17 +02:00
|
|
|
CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args.begin(),
|
|
|
|
Args.end(), "", II);
|
2007-11-22 23:24:59 +01:00
|
|
|
NewCall->takeName(II);
|
|
|
|
NewCall->setCallingConv(II->getCallingConv());
|
2008-09-25 23:00:45 +02:00
|
|
|
NewCall->setAttributes(II->getAttributes());
|
2007-11-22 23:24:59 +01:00
|
|
|
II->replaceAllUsesWith(NewCall);
|
|
|
|
|
|
|
|
// Follow the call by a branch to the normal destination.
|
2008-04-06 22:25:17 +02:00
|
|
|
BranchInst::Create(II->getNormalDest(), II);
|
2007-11-22 23:24:59 +01:00
|
|
|
|
|
|
|
// Update PHI nodes in the unwind destination
|
|
|
|
II->getUnwindDest()->removePredecessor(BB);
|
|
|
|
BB->getInstList().erase(II);
|
|
|
|
}
|
|
|
|
|
2007-03-04 05:20:48 +01:00
|
|
|
static bool MarkAliveBlocks(BasicBlock *BB,
|
2009-11-23 04:34:29 +01:00
|
|
|
SmallPtrSet<BasicBlock*, 128> &Reachable) {
|
2007-04-05 03:27:02 +02:00
|
|
|
|
2007-11-13 08:32:38 +01:00
|
|
|
SmallVector<BasicBlock*, 128> Worklist;
|
2007-04-05 03:27:02 +02: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 22:49:37 +02:00
|
|
|
|
2007-04-05 03:27:02 +02: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 23:24:59 +01:00
|
|
|
// canonicalizes unreachable insts into stores to null or undef.
|
2007-11-14 07:19:25 +01:00
|
|
|
for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(BBI)) {
|
2007-12-18 10:59:50 +01:00
|
|
|
if (CI->doesNotReturn()) {
|
2007-11-14 07:19:25 +01: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-11-23 04:34:29 +01:00
|
|
|
ChangeToUnreachable(BBI);
|
2007-11-14 07:19:25 +01:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-01 19:42:03 +01:00
|
|
|
// Store to undef and store to null are undefined and used to signal that
|
|
|
|
// they should be changed to unreachable by passes that can't modify the
|
|
|
|
// CFG.
|
2009-06-12 23:01:07 +02:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
|
|
|
|
Value *Ptr = SI->getOperand(1);
|
2009-07-14 19:48:25 +02:00
|
|
|
|
|
|
|
if (isa<UndefValue>(Ptr) ||
|
|
|
|
(isa<ConstantPointerNull>(Ptr) &&
|
2009-08-30 22:06:40 +02:00
|
|
|
SI->getPointerAddressSpace() == 0)) {
|
2009-11-23 04:34:29 +01:00
|
|
|
ChangeToUnreachable(SI);
|
2009-07-14 19:48:25 +02:00
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-06-12 23:01:07 +02:00
|
|
|
}
|
2007-11-14 07:19:25 +01:00
|
|
|
}
|
2004-10-18 05:00:50 +02:00
|
|
|
|
2007-11-22 23:24:59 +01:00
|
|
|
// Turn invokes that call 'nounwind' functions into ordinary calls.
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
|
2007-12-18 10:59:50 +01:00
|
|
|
if (II->doesNotThrow()) {
|
2007-11-22 23:24:59 +01:00
|
|
|
ChangeToCall(II);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2007-04-05 03:27:02 +02: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 22:49:37 +02:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2008-09-09 00:14:17 +02: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 08:32:38 +01:00
|
|
|
SmallPtrSet<BasicBlock*, 128> Reachable;
|
2009-11-23 04:34:29 +01:00
|
|
|
bool Changed = MarkAliveBlocks(F.begin(), Reachable);
|
2007-11-13 08:32:38 +01:00
|
|
|
|
2002-05-21 22:49:37 +02:00
|
|
|
// If there are unreachable blocks in the CFG...
|
2008-01-26 07:51:24 +01:00
|
|
|
if (Reachable.size() == F.size())
|
2007-11-13 08:32:38 +01: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 08:39:01 +01: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 08:32:38 +01: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 22:49:37 +02:00
|
|
|
|
2007-11-13 08:32:38 +01: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 22:49:37 +02:00
|
|
|
bool LocalChange = true;
|
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
2007-11-13 08:32:38 +01:00
|
|
|
|
2002-05-21 22:49:37 +02:00
|
|
|
// Loop over all of the basic blocks (except the first one) and remove them
|
|
|
|
// if they are unneeded...
|
|
|
|
//
|
2002-06-25 18:13:24 +02:00
|
|
|
for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
|
|
|
|
if (SimplifyCFG(BBIt++)) {
|
2002-05-21 22:49:37 +02:00
|
|
|
LocalChange = true;
|
|
|
|
++NumSimpl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Changed |= LocalChange;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
2007-11-13 08:32:38 +01: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-09 00:14:17 +02:00
|
|
|
bool EverChanged = RemoveUnreachableBlocksFromFn(F);
|
2007-11-13 08:32:38 +01: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-09 00:14:17 +02:00
|
|
|
// RemoveUnreachableBlocksFromFn is needed to nuke them, which means we should
|
2007-11-13 08:32:38 +01:00
|
|
|
// iterate between the two optimizations. We structure the code like this to
|
|
|
|
// avoid reruning IterativeSimplifyCFG if the second pass of
|
2008-09-09 00:14:17 +02:00
|
|
|
// RemoveUnreachableBlocksFromFn doesn't do anything.
|
|
|
|
if (!RemoveUnreachableBlocksFromFn(F))
|
2007-11-13 08:32:38 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
do {
|
|
|
|
EverChanged = IterativeSimplifyCFG(F);
|
2008-09-09 00:14:17 +02:00
|
|
|
EverChanged |= RemoveUnreachableBlocksFromFn(F);
|
2007-11-13 08:32:38 +01:00
|
|
|
} while (EverChanged);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|