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 #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"
33 Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified");
35 struct CFGSimplifyPass : public FunctionPass {
36 virtual bool runOnFunction(Function &F);
38 RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
41 // Public interface to the CFGSimplification pass
42 FunctionPass *llvm::createCFGSimplificationPass() {
43 return new CFGSimplifyPass();
46 static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
47 if (Reachable.count(BB)) return false;
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
59 for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
60 (*I)->removePredecessor(BB);
62 new UnreachableInst(SI);
64 // All instructions after this are dead.
66 if (!BBI->use_empty())
67 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
68 BB->getInstList().erase(BBI++);
74 bool Changed = ConstantFoldTerminator(BB);
75 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
76 MarkAliveBlocks(*SI, Reachable);
82 // It is possible that we may require multiple passes over the code to fully
85 bool CFGSimplifyPass::runOnFunction(Function &F) {
86 std::set<BasicBlock*> Reachable;
87 bool Changed = MarkAliveBlocks(F.begin(), Reachable);
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();
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();
104 for (Function::iterator I = ++F.begin(); I != F.end();)
105 if (!Reachable.count(I))
106 I = F.getBasicBlockList().erase(I);
113 bool LocalChange = true;
114 while (LocalChange) {
117 // Loop over all of the basic blocks (except the first one) and remove them
118 // if they are unneeded...
120 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
121 if (SimplifyCFG(BBIt++)) {
126 Changed |= LocalChange;