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