* Finegrainify namespacification
[oota-llvm.git] / lib / Transforms / Scalar / SimplifyCFG.cpp
1 //===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements dead code elimination and basic block merging.
11 //
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
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "llvm/Transforms/Scalar.h"
22 #include "llvm/Transforms/Utils/Local.h"
23 #include "llvm/Module.h"
24 #include "llvm/Support/CFG.h"
25 #include "llvm/Pass.h"
26 #include "Support/Statistic.h"
27 #include <set>
28
29 namespace llvm {
30
31 namespace {
32   Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified");
33
34   struct CFGSimplifyPass : public FunctionPass {
35     virtual bool runOnFunction(Function &F);
36   };
37   RegisterOpt<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
38 }
39
40 // Public interface to the CFGSimplification pass
41 FunctionPass *createCFGSimplificationPass() {
42   return new CFGSimplifyPass();
43 }
44
45 static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
46   if (Reachable.count(BB)) return false;
47   Reachable.insert(BB);
48
49   bool Changed = ConstantFoldTerminator(BB);
50   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
51     MarkAliveBlocks(*SI, Reachable);
52
53   return Changed;
54 }
55
56
57 // It is possible that we may require multiple passes over the code to fully
58 // simplify the CFG.
59 //
60 bool CFGSimplifyPass::runOnFunction(Function &F) {
61   std::set<BasicBlock*> Reachable;
62   bool Changed = MarkAliveBlocks(F.begin(), Reachable);
63
64   // If there are unreachable blocks in the CFG...
65   if (Reachable.size() != F.size()) {
66     assert(Reachable.size() < F.size());
67     NumSimpl += F.size()-Reachable.size();
68
69     // Loop over all of the basic blocks that are not reachable, dropping all of
70     // their internal references...
71     for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
72       if (!Reachable.count(BB)) {
73         for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
74           if (Reachable.count(*SI))
75             (*SI)->removePredecessor(BB);
76         BB->dropAllReferences();
77       }
78     
79     for (Function::iterator I = ++F.begin(); I != F.end();)
80       if (!Reachable.count(I))
81         I = F.getBasicBlockList().erase(I);
82       else
83         ++I;
84
85     Changed = true;
86   }
87
88   bool LocalChange = true;
89   while (LocalChange) {
90     LocalChange = false;
91
92     // Loop over all of the basic blocks (except the first one) and remove them
93     // if they are unneeded...
94     //
95     for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
96       if (SimplifyCFG(BBIt++)) {
97         LocalChange = true;
98         ++NumSimpl;
99       }
100     }
101     Changed |= LocalChange;
102   }
103
104   return Changed;
105 }
106
107 } // End llvm namespace