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"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-01-07 03:08:10 +00:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Attributes.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2002-05-21 20:49:37 +00:00
|
|
|
#include "llvm/Pass.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.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 {
|
2009-09-02 06:11:42 +00:00
|
|
|
struct CFGSimplifyPass : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
CFGSimplifyPass() : FunctionPass(ID) {
|
|
|
|
initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2013-01-07 03:53:25 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TargetTransformInfo>();
|
|
|
|
}
|
2002-05-21 20:49:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGSimplifyPass::ID = 0;
|
2013-01-07 03:53:25 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG",
|
|
|
|
false, false)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
|
|
|
|
INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG",
|
|
|
|
false, false)
|
2008-05-13 00:00:25 +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();
|
|
|
|
}
|
|
|
|
|
2012-09-06 00:59:08 +00:00
|
|
|
/// changeToUnreachable - Insert an unreachable instruction before the specified
|
2007-11-14 06:19:25 +00:00
|
|
|
/// instruction, making it and the rest of the code in the block dead.
|
2012-09-06 00:59:08 +00:00
|
|
|
static void changeToUnreachable(Instruction *I, bool UseLLVMTrap) {
|
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);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-08 22:15:59 +00:00
|
|
|
// Insert a call to llvm.trap right before this. This turns the undefined
|
|
|
|
// behavior into a hard fail instead of falling through into random code.
|
|
|
|
if (UseLLVMTrap) {
|
|
|
|
Function *TrapFn =
|
|
|
|
Intrinsic::getDeclaration(BB->getParent()->getParent(), Intrinsic::trap);
|
2011-04-27 17:59:27 +00:00
|
|
|
CallInst *CallTrap = CallInst::Create(TrapFn, "", I);
|
|
|
|
CallTrap->setDebugLoc(I->getDebugLoc());
|
2010-05-08 22:15:59 +00:00
|
|
|
}
|
2009-08-13 21:58:54 +00:00
|
|
|
new UnreachableInst(I->getContext(), I);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2007-11-14 06:19:25 +00:00
|
|
|
// All instructions after this are dead.
|
|
|
|
BasicBlock::iterator BBI = I, BBE = BB->end();
|
|
|
|
while (BBI != BBE) {
|
|
|
|
if (!BBI->use_empty())
|
2009-07-30 23:03:37 +00:00
|
|
|
BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
|
2007-11-14 06:19:25 +00:00
|
|
|
BB->getInstList().erase(BBI++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 00:59:08 +00:00
|
|
|
/// changeToCall - Convert the specified invoke into a normal call.
|
|
|
|
static void changeToCall(InvokeInst *II) {
|
2010-03-24 13:21:49 +00:00
|
|
|
SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3);
|
2011-07-15 08:37:34 +00:00
|
|
|
CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args, "", 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());
|
2011-06-02 22:46:58 +00:00
|
|
|
NewCall->setDebugLoc(II->getDebugLoc());
|
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);
|
2012-07-02 16:14:47 +00:00
|
|
|
|
|
|
|
// Update PHI nodes in the unwind destination
|
|
|
|
II->getUnwindDest()->removePredecessor(II->getParent());
|
2012-06-28 22:32:27 +00:00
|
|
|
II->eraseFromParent();
|
2007-11-22 22:24:59 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 00:59:08 +00:00
|
|
|
static bool markAliveBlocks(BasicBlock *BB,
|
2009-11-23 03:34:29 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 128> &Reachable) {
|
2012-07-24 10:51:42 +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);
|
2012-12-17 14:28:00 +00:00
|
|
|
Reachable.insert(BB);
|
2007-04-05 01:27:02 +00:00
|
|
|
bool Changed = false;
|
2010-01-05 16:27:25 +00:00
|
|
|
do {
|
|
|
|
BB = Worklist.pop_back_val();
|
2012-07-24 10:51:42 +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)) {
|
2010-05-08 22:15:59 +00:00
|
|
|
// Don't insert a call to llvm.trap right before the unreachable.
|
2012-09-06 00:59:08 +00:00
|
|
|
changeToUnreachable(BBI, false);
|
2007-11-14 06:19:25 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-11-01 18:42:03 +00: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 21:01:07 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
|
2010-06-13 14:35:54 +00:00
|
|
|
// Don't touch volatile stores.
|
|
|
|
if (SI->isVolatile()) continue;
|
|
|
|
|
2009-06-12 21:01:07 +00:00
|
|
|
Value *Ptr = SI->getOperand(1);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-07-14 17:48:25 +00:00
|
|
|
if (isa<UndefValue>(Ptr) ||
|
|
|
|
(isa<ConstantPointerNull>(Ptr) &&
|
2009-08-30 20:06:40 +00:00
|
|
|
SI->getPointerAddressSpace() == 0)) {
|
2012-09-06 00:59:08 +00:00
|
|
|
changeToUnreachable(SI, true);
|
2009-07-14 17:48:25 +00:00
|
|
|
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.
|
2012-06-25 17:11:47 +00:00
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
|
|
|
|
Value *Callee = II->getCalledValue();
|
|
|
|
if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
|
2012-09-06 00:59:08 +00:00
|
|
|
changeToUnreachable(II, true);
|
2012-06-25 17:11:47 +00:00
|
|
|
Changed = true;
|
|
|
|
} else if (II->doesNotThrow()) {
|
2012-06-28 22:32:27 +00:00
|
|
|
if (II->use_empty() && II->onlyReadsMemory()) {
|
|
|
|
// jump to the normal destination branch.
|
|
|
|
BranchInst::Create(II->getNormalDest(), II);
|
2012-07-16 22:49:40 +00:00
|
|
|
II->getUnwindDest()->removePredecessor(II->getParent());
|
2012-06-28 22:32:27 +00:00
|
|
|
II->eraseFromParent();
|
|
|
|
} else
|
2012-09-06 00:59:08 +00:00
|
|
|
changeToCall(II);
|
2007-11-22 22:24:59 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
2012-06-25 17:11:47 +00:00
|
|
|
}
|
2007-11-22 22:24:59 +00:00
|
|
|
|
2011-05-22 16:24:18 +00:00
|
|
|
Changed |= ConstantFoldTerminator(BB, true);
|
2007-04-05 01:27:02 +00:00
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
|
2012-12-17 14:28:00 +00:00
|
|
|
if (Reachable.insert(*SI))
|
|
|
|
Worklist.push_back(*SI);
|
2010-01-05 16:27:25 +00:00
|
|
|
} while (!Worklist.empty());
|
2002-05-21 20:49:37 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2012-09-06 00:59:08 +00:00
|
|
|
/// removeUnreachableBlocksFromFn - Remove blocks that are not reachable, even
|
2012-07-24 10:51:42 +00:00
|
|
|
/// if they are in a dead cycle. Return true if a change was made, false
|
2008-09-08 22:14:17 +00:00
|
|
|
/// otherwise.
|
2012-09-06 00:59:08 +00:00
|
|
|
static bool removeUnreachableBlocksFromFn(Function &F) {
|
2007-11-13 07:32:38 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 128> Reachable;
|
2012-09-06 00:59:08 +00:00
|
|
|
bool Changed = markAliveBlocks(F.begin(), Reachable);
|
2012-07-24 10:51:42 +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;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
assert(Reachable.size() < F.size());
|
|
|
|
NumSimpl += F.size()-Reachable.size();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
// 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;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2008-02-14 07:39:01 +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();
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
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;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2002-05-21 20:49:37 +00:00
|
|
|
|
2012-09-06 00:59:08 +00:00
|
|
|
/// mergeEmptyReturnBlocks - If we have more than one empty (other than phi
|
2009-12-22 06:07:30 +00:00
|
|
|
/// node) return blocks, merge them together to promote recursive block merging.
|
2012-09-06 00:59:08 +00:00
|
|
|
static bool mergeEmptyReturnBlocks(Function &F) {
|
2009-12-22 06:07:30 +00:00
|
|
|
bool Changed = false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
BasicBlock *RetBlock = 0;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// Scan all the blocks in the function, looking for empty return blocks.
|
|
|
|
for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; ) {
|
|
|
|
BasicBlock &BB = *BBI++;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// Only look at return blocks.
|
|
|
|
ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator());
|
|
|
|
if (Ret == 0) continue;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// Only look at the block if it is empty or the only other thing in it is a
|
|
|
|
// single PHI node that is the operand to the return.
|
|
|
|
if (Ret != &BB.front()) {
|
|
|
|
// Check for something else in the block.
|
|
|
|
BasicBlock::iterator I = Ret;
|
|
|
|
--I;
|
2010-03-14 10:40:55 +00:00
|
|
|
// Skip over debug info.
|
|
|
|
while (isa<DbgInfoIntrinsic>(I) && I != BB.begin())
|
|
|
|
--I;
|
|
|
|
if (!isa<DbgInfoIntrinsic>(I) &&
|
|
|
|
(!isa<PHINode>(I) || I != BB.begin() ||
|
|
|
|
Ret->getNumOperands() == 0 ||
|
|
|
|
Ret->getOperand(0) != I))
|
2009-12-22 06:07:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-14 10:40:55 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// If this is the first returning block, remember it and keep going.
|
|
|
|
if (RetBlock == 0) {
|
|
|
|
RetBlock = &BB;
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// Otherwise, we found a duplicate return block. Merge the two.
|
|
|
|
Changed = true;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// Case when there is no input to the return or when the returned values
|
|
|
|
// agree is trivial. Note that they can't agree if there are phis in the
|
|
|
|
// blocks.
|
|
|
|
if (Ret->getNumOperands() == 0 ||
|
2012-07-24 10:51:42 +00:00
|
|
|
Ret->getOperand(0) ==
|
2009-12-22 06:07:30 +00:00
|
|
|
cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0)) {
|
|
|
|
BB.replaceAllUsesWith(RetBlock);
|
|
|
|
BB.eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// If the canonical return block has no PHI node, create one now.
|
|
|
|
PHINode *RetBlockPHI = dyn_cast<PHINode>(RetBlock->begin());
|
|
|
|
if (RetBlockPHI == 0) {
|
2010-03-15 19:05:46 +00:00
|
|
|
Value *InVal = cast<ReturnInst>(RetBlock->getTerminator())->getOperand(0);
|
2011-03-30 11:19:20 +00:00
|
|
|
pred_iterator PB = pred_begin(RetBlock), PE = pred_end(RetBlock);
|
2011-03-30 11:28:46 +00:00
|
|
|
RetBlockPHI = PHINode::Create(Ret->getOperand(0)->getType(),
|
|
|
|
std::distance(PB, PE), "merge",
|
2009-12-22 06:07:30 +00:00
|
|
|
&RetBlock->front());
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-03-30 11:19:20 +00:00
|
|
|
for (pred_iterator PI = PB; PI != PE; ++PI)
|
2009-12-22 06:07:30 +00:00
|
|
|
RetBlockPHI->addIncoming(InVal, *PI);
|
|
|
|
RetBlock->getTerminator()->setOperand(0, RetBlockPHI);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
// Turn BB into a block that just unconditionally branches to the return
|
|
|
|
// block. This handles the case when the two return blocks have a common
|
|
|
|
// predecessor but that return different things.
|
|
|
|
RetBlockPHI->addIncoming(Ret->getOperand(0), &BB);
|
|
|
|
BB.getTerminator()->eraseFromParent();
|
|
|
|
BranchInst::Create(RetBlock, &BB);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-22 06:07:30 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2012-09-06 00:59:08 +00:00
|
|
|
/// iterativelySimplifyCFG - Call SimplifyCFG on all the blocks in the function,
|
2007-11-13 07:32:38 +00:00
|
|
|
/// iterating until no more changes are made.
|
2013-01-07 03:53:25 +00:00
|
|
|
static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
|
|
|
|
const DataLayout *TD) {
|
2007-11-13 07:32:38 +00:00
|
|
|
bool Changed = false;
|
2002-05-21 20:49:37 +00:00
|
|
|
bool LocalChange = true;
|
|
|
|
while (LocalChange) {
|
|
|
|
LocalChange = false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-08-14 00:29:42 +00:00
|
|
|
// Loop over all of the basic blocks and remove them if they are unneeded...
|
2002-05-21 20:49:37 +00:00
|
|
|
//
|
2010-08-14 00:29:42 +00:00
|
|
|
for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
|
2013-01-07 03:53:25 +00:00
|
|
|
if (SimplifyCFG(BBIt++, TTI, TD)) {
|
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) {
|
2013-01-07 03:53:25 +00:00
|
|
|
const TargetTransformInfo &TTI = getAnalysis<TargetTransformInfo>();
|
2012-10-08 16:38:25 +00:00
|
|
|
const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
|
2012-09-06 00:59:08 +00:00
|
|
|
bool EverChanged = removeUnreachableBlocksFromFn(F);
|
|
|
|
EverChanged |= mergeEmptyReturnBlocks(F);
|
2013-01-07 03:53:25 +00:00
|
|
|
EverChanged |= iterativelySimplifyCFG(F, TTI, TD);
|
2010-02-05 22:03:18 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
// If neither pass changed anything, we're done.
|
|
|
|
if (!EverChanged) return false;
|
|
|
|
|
2012-09-06 00:59:08 +00:00
|
|
|
// iterativelySimplifyCFG can (rarely) make some loops dead. If this happens,
|
|
|
|
// 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
|
2012-09-06 00:59:08 +00:00
|
|
|
// avoid reruning iterativelySimplifyCFG if the second pass of
|
|
|
|
// removeUnreachableBlocksFromFn doesn't do anything.
|
|
|
|
if (!removeUnreachableBlocksFromFn(F))
|
2007-11-13 07:32:38 +00:00
|
|
|
return true;
|
2010-02-05 22:03:18 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
do {
|
2013-01-07 03:53:25 +00:00
|
|
|
EverChanged = iterativelySimplifyCFG(F, TTI, TD);
|
2012-09-06 00:59:08 +00:00
|
|
|
EverChanged |= removeUnreachableBlocksFromFn(F);
|
2007-11-13 07:32:38 +00:00
|
|
|
} while (EverChanged);
|
2010-02-05 22:03:18 +00:00
|
|
|
|
2007-11-13 07:32:38 +00:00
|
|
|
return true;
|
|
|
|
}
|