Use getPrimitiveSizeInBits() instead of getPrimitiveSize()*8
[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/Constants.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Module.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Pass.h"
28 #include "llvm/ADT/Statistic.h"
29 #include <set>
30 using namespace llvm;
31
32 namespace {
33   Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified");
34
35   struct CFGSimplifyPass : public FunctionPass {
36     virtual bool runOnFunction(Function &F);
37   };
38   RegisterOpt<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
39 }
40
41 // Public interface to the CFGSimplification pass
42 FunctionPass *llvm::createCFGSimplificationPass() {
43   return new CFGSimplifyPass();
44 }
45
46 static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
47   if (Reachable.count(BB)) return false;
48   Reachable.insert(BB);
49
50   // Do a quick scan of the basic block, turning any obviously unreachable
51   // instructions into LLVM unreachable insts.  The instruction combining pass
52   // canonnicalizes unreachable insts into stores to null or undef.
53   for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ++BBI)
54     if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
55       if (isa<ConstantPointerNull>(SI->getOperand(1)) ||
56           isa<UndefValue>(SI->getOperand(1))) {
57         // Loop over all of the successors, removing BB's entry from any PHI
58         // nodes.
59         for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
60           (*I)->removePredecessor(BB);
61
62         new UnreachableInst(SI);
63
64         // All instructions after this are dead.
65         for (; BBI != E; ) {
66           if (!BBI->use_empty())
67             BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
68           BB->getInstList().erase(BBI++);
69         }
70         break;
71       }
72
73
74   bool Changed = ConstantFoldTerminator(BB);
75   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
76     MarkAliveBlocks(*SI, Reachable);
77
78   return Changed;
79 }
80
81
82 // It is possible that we may require multiple passes over the code to fully
83 // simplify the CFG.
84 //
85 bool CFGSimplifyPass::runOnFunction(Function &F) {
86   std::set<BasicBlock*> Reachable;
87   bool Changed = MarkAliveBlocks(F.begin(), Reachable);
88
89   // If there are unreachable blocks in the CFG...
90   if (Reachable.size() != F.size()) {
91     assert(Reachable.size() < F.size());
92     NumSimpl += F.size()-Reachable.size();
93
94     // Loop over all of the basic blocks that are not reachable, dropping all of
95     // their internal references...
96     for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
97       if (!Reachable.count(BB)) {
98         for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
99           if (Reachable.count(*SI))
100             (*SI)->removePredecessor(BB);
101         BB->dropAllReferences();
102       }
103
104     for (Function::iterator I = ++F.begin(); I != F.end();)
105       if (!Reachable.count(I))
106         I = F.getBasicBlockList().erase(I);
107       else
108         ++I;
109
110     Changed = true;
111   }
112
113   bool LocalChange = true;
114   while (LocalChange) {
115     LocalChange = false;
116
117     // Loop over all of the basic blocks (except the first one) and remove them
118     // if they are unneeded...
119     //
120     for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
121       if (SimplifyCFG(BBIt++)) {
122         LocalChange = true;
123         ++NumSimpl;
124       }
125     }
126     Changed |= LocalChange;
127   }
128
129   return Changed;
130 }