1 //===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements dead code elimination and basic block merging.
12 // Specifically, this:
13 // * removes basic blocks with no predecessors
14 // * merges a basic block into its predecessor if there is only one and the
15 // predecessor only has one successor.
16 // * Eliminates PHI nodes for basic blocks with a single predecessor
17 // * Eliminates a basic block that only contains an unconditional branch
19 //===----------------------------------------------------------------------===//
21 #define DEBUG_TYPE "simplifycfg"
22 #include "llvm/Transforms/Scalar.h"
23 #include "llvm/Transforms/Utils/Local.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Module.h"
27 #include "llvm/Support/CFG.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Pass.h"
30 #include "llvm/ADT/Statistic.h"
34 STATISTIC(NumSimpl, "Number of blocks simplified");
37 struct VISIBILITY_HIDDEN CFGSimplifyPass : public FunctionPass {
38 virtual bool runOnFunction(Function &F);
40 RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
43 // Public interface to the CFGSimplification pass
44 FunctionPass *llvm::createCFGSimplificationPass() {
45 return new CFGSimplifyPass();
48 static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
49 if (Reachable.count(BB)) return false;
52 // Do a quick scan of the basic block, turning any obviously unreachable
53 // instructions into LLVM unreachable insts. The instruction combining pass
54 // canonnicalizes unreachable insts into stores to null or undef.
55 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ++BBI)
56 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
57 if (isa<ConstantPointerNull>(SI->getOperand(1)) ||
58 isa<UndefValue>(SI->getOperand(1))) {
59 // Loop over all of the successors, removing BB's entry from any PHI
61 for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
62 (*I)->removePredecessor(BB);
64 new UnreachableInst(SI);
66 // All instructions after this are dead.
68 if (!BBI->use_empty())
69 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
70 BB->getInstList().erase(BBI++);
76 bool Changed = ConstantFoldTerminator(BB);
77 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
78 Changed |= MarkAliveBlocks(*SI, Reachable);
84 // It is possible that we may require multiple passes over the code to fully
87 bool CFGSimplifyPass::runOnFunction(Function &F) {
88 std::set<BasicBlock*> Reachable;
89 bool Changed = MarkAliveBlocks(F.begin(), Reachable);
91 // If there are unreachable blocks in the CFG...
92 if (Reachable.size() != F.size()) {
93 assert(Reachable.size() < F.size());
94 NumSimpl += F.size()-Reachable.size();
96 // Loop over all of the basic blocks that are not reachable, dropping all of
97 // their internal references...
98 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
99 if (!Reachable.count(BB)) {
100 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
101 if (Reachable.count(*SI))
102 (*SI)->removePredecessor(BB);
103 BB->dropAllReferences();
106 for (Function::iterator I = ++F.begin(); I != F.end();)
107 if (!Reachable.count(I))
108 I = F.getBasicBlockList().erase(I);
115 bool LocalChange = true;
116 while (LocalChange) {
119 // Loop over all of the basic blocks (except the first one) and remove them
120 // if they are unneeded...
122 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
123 if (SimplifyCFG(BBIt++)) {
128 Changed |= LocalChange;