blob: 8b9c518de9f9427ddfbfb37cb3713ed784371d39 [file] [log] [blame]
Chris Lattner573527b2002-05-21 20:49:37 +00001//===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner573527b2002-05-21 20:49:37 +00009//
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"
Chris Lattnerfc5c1bb02004-10-18 03:00:50 +000023#include "llvm/Constants.h"
24#include "llvm/Instructions.h"
Chris Lattner573527b2002-05-21 20:49:37 +000025#include "llvm/Module.h"
Chris Lattner573527b2002-05-21 20:49:37 +000026#include "llvm/Support/CFG.h"
27#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
Chris Lattner573527b2002-05-21 20:49:37 +000029#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner573527b2002-05-21 20:49:37 +000032namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000033 Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified");
34
Chris Lattner573527b2002-05-21 20:49:37 +000035 struct CFGSimplifyPass : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000036 virtual bool runOnFunction(Function &F);
Chris Lattner573527b2002-05-21 20:49:37 +000037 };
Chris Lattnera6275cc2002-07-26 21:12:46 +000038 RegisterOpt<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
Chris Lattner573527b2002-05-21 20:49:37 +000039}
40
Brian Gaeked0fde302003-11-11 22:41:34 +000041// Public interface to the CFGSimplification pass
Chris Lattnerd7456022004-01-09 06:02:20 +000042FunctionPass *llvm::createCFGSimplificationPass() {
Chris Lattner573527b2002-05-21 20:49:37 +000043 return new CFGSimplifyPass();
44}
45
46static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
47 if (Reachable.count(BB)) return false;
48 Reachable.insert(BB);
49
Chris Lattnerfc5c1bb02004-10-18 03:00:50 +000050 // 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);
Chris Lattnerfc5c1bb02004-10-18 03:00:50 +000063
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
Chris Lattner573527b2002-05-21 20:49:37 +000074 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//
Chris Lattner7e708292002-06-25 16:13:24 +000085bool CFGSimplifyPass::runOnFunction(Function &F) {
Chris Lattner573527b2002-05-21 20:49:37 +000086 std::set<BasicBlock*> Reachable;
Chris Lattner7e708292002-06-25 16:13:24 +000087 bool Changed = MarkAliveBlocks(F.begin(), Reachable);
Chris Lattner573527b2002-05-21 20:49:37 +000088
89 // If there are unreachable blocks in the CFG...
Chris Lattner7e708292002-06-25 16:13:24 +000090 if (Reachable.size() != F.size()) {
91 assert(Reachable.size() < F.size());
92 NumSimpl += F.size()-Reachable.size();
Chris Lattner573527b2002-05-21 20:49:37 +000093
94 // Loop over all of the basic blocks that are not reachable, dropping all of
95 // their internal references...
Chris Lattner7e708292002-06-25 16:13:24 +000096 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
97 if (!Reachable.count(BB)) {
Chris Lattner573527b2002-05-21 20:49:37 +000098 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 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000103
Chris Lattner7e708292002-06-25 16:13:24 +0000104 for (Function::iterator I = ++F.begin(); I != F.end();)
105 if (!Reachable.count(I))
106 I = F.getBasicBlockList().erase(I);
Chris Lattner573527b2002-05-21 20:49:37 +0000107 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 //
Chris Lattner7e708292002-06-25 16:13:24 +0000120 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
121 if (SimplifyCFG(BBIt++)) {
Chris Lattner573527b2002-05-21 20:49:37 +0000122 LocalChange = true;
123 ++NumSimpl;
Chris Lattner573527b2002-05-21 20:49:37 +0000124 }
125 }
126 Changed |= LocalChange;
127 }
128
129 return Changed;
130}