switch MarkAliveBlocks over to using SmallPtrSet instead of std::set, speeding
[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 #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/SmallPtrSet.h"
31 #include "llvm/ADT/Statistic.h"
32 using namespace llvm;
33
34 STATISTIC(NumSimpl, "Number of blocks simplified");
35
36 namespace {
37   struct VISIBILITY_HIDDEN CFGSimplifyPass : public FunctionPass {
38     virtual bool runOnFunction(Function &F);
39   };
40   RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
41 }
42
43 // Public interface to the CFGSimplification pass
44 FunctionPass *llvm::createCFGSimplificationPass() {
45   return new CFGSimplifyPass();
46 }
47
48 static bool MarkAliveBlocks(BasicBlock *BB,
49                             SmallPtrSet<BasicBlock*, 16> &Reachable) {
50   if (!Reachable.insert(BB)) return false;
51
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
60         // nodes.
61         for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
62           (*I)->removePredecessor(BB);
63
64         new UnreachableInst(SI);
65
66         // All instructions after this are dead.
67         for (; BBI != E; ) {
68           if (!BBI->use_empty())
69             BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
70           BB->getInstList().erase(BBI++);
71         }
72         break;
73       }
74
75
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);
79
80   return Changed;
81 }
82
83
84 // It is possible that we may require multiple passes over the code to fully
85 // simplify the CFG.
86 //
87 bool CFGSimplifyPass::runOnFunction(Function &F) {
88   SmallPtrSet<BasicBlock*, 16> Reachable;
89   bool Changed = MarkAliveBlocks(F.begin(), Reachable);
90
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();
95
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();
104       }
105
106     for (Function::iterator I = ++F.begin(); I != F.end();)
107       if (!Reachable.count(I))
108         I = F.getBasicBlockList().erase(I);
109       else
110         ++I;
111
112     Changed = true;
113   }
114
115   bool LocalChange = true;
116   while (LocalChange) {
117     LocalChange = false;
118
119     // Loop over all of the basic blocks (except the first one) and remove them
120     // if they are unneeded...
121     //
122     for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
123       if (SimplifyCFG(BBIt++)) {
124         LocalChange = true;
125         ++NumSimpl;
126       }
127     }
128     Changed |= LocalChange;
129   }
130
131   return Changed;
132 }