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/Pass.h"
29 #include "llvm/ADT/Statistic.h"
33 STATISTIC(NumSimpl, "Number of blocks simplified");
36 struct CFGSimplifyPass : public FunctionPass {
37 virtual bool runOnFunction(Function &F);
39 RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
42 // Public interface to the CFGSimplification pass
43 FunctionPass *llvm::createCFGSimplificationPass() {
44 return new CFGSimplifyPass();
47 static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
48 if (Reachable.count(BB)) return false;
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
60 for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
61 (*I)->removePredecessor(BB);
63 new UnreachableInst(SI);
65 // All instructions after this are dead.
67 if (!BBI->use_empty())
68 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
69 BB->getInstList().erase(BBI++);
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);
83 // It is possible that we may require multiple passes over the code to fully
86 bool CFGSimplifyPass::runOnFunction(Function &F) {
87 std::set<BasicBlock*> Reachable;
88 bool Changed = MarkAliveBlocks(F.begin(), Reachable);
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();
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();
105 for (Function::iterator I = ++F.begin(); I != F.end();)
106 if (!Reachable.count(I))
107 I = F.getBasicBlockList().erase(I);
114 bool LocalChange = true;
115 while (LocalChange) {
118 // Loop over all of the basic blocks (except the first one) and remove them
119 // if they are unneeded...
121 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
122 if (SimplifyCFG(BBIt++)) {
127 Changed |= LocalChange;