blob: cbd0d48c76b6bc8bd7f0b6138ca81a71ad3b16eb [file] [log] [blame]
Chris Lattnerfa7dad82002-05-21 20:49:37 +00001//===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerfa7dad82002-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
Chris Lattner79a42ac2006-12-19 21:40:18 +000021#define DEBUG_TYPE "simplifycfg"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000022#include "llvm/Transforms/Scalar.h"
23#include "llvm/Transforms/Utils/Local.h"
Chris Lattnera67dd322004-10-18 03:00:50 +000024#include "llvm/Constants.h"
25#include "llvm/Instructions.h"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000026#include "llvm/Module.h"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000027#include "llvm/Support/CFG.h"
Reid Spencer557ab152007-02-05 23:32:05 +000028#include "llvm/Support/Compiler.h"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000029#include "llvm/Pass.h"
Chris Lattnera5403a52007-03-04 04:20:48 +000030#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/ADT/Statistic.h"
Chris Lattner49525f82004-01-09 06:02:20 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner79a42ac2006-12-19 21:40:18 +000034STATISTIC(NumSimpl, "Number of blocks simplified");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000035
Chris Lattner79a42ac2006-12-19 21:40:18 +000036namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000037 struct VISIBILITY_HIDDEN CFGSimplifyPass : public FunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000038 static char ID; // Pass identifcation, replacement for typeid
Devang Patel09f162c2007-05-01 21:15:47 +000039 CFGSimplifyPass() : FunctionPass((intptr_t)&ID) {}
40
Chris Lattner113f4f42002-06-25 16:13:24 +000041 virtual bool runOnFunction(Function &F);
Chris Lattnerfa7dad82002-05-21 20:49:37 +000042 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000043 char CFGSimplifyPass::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000044 RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
Chris Lattnerfa7dad82002-05-21 20:49:37 +000045}
46
Brian Gaeke960707c2003-11-11 22:41:34 +000047// Public interface to the CFGSimplification pass
Chris Lattner49525f82004-01-09 06:02:20 +000048FunctionPass *llvm::createCFGSimplificationPass() {
Chris Lattnerfa7dad82002-05-21 20:49:37 +000049 return new CFGSimplifyPass();
50}
51
Chris Lattnera5403a52007-03-04 04:20:48 +000052static bool MarkAliveBlocks(BasicBlock *BB,
53 SmallPtrSet<BasicBlock*, 16> &Reachable) {
Chris Lattner108083e2007-04-05 01:27:02 +000054
55 std::vector<BasicBlock*> Worklist;
56 Worklist.push_back(BB);
57 bool Changed = false;
58 while (!Worklist.empty()) {
59 BB = Worklist.back();
60 Worklist.pop_back();
61
62 if (!Reachable.insert(BB))
63 continue;
Chris Lattnerfa7dad82002-05-21 20:49:37 +000064
Chris Lattner108083e2007-04-05 01:27:02 +000065 // Do a quick scan of the basic block, turning any obviously unreachable
66 // instructions into LLVM unreachable insts. The instruction combining pass
67 // canonnicalizes unreachable insts into stores to null or undef.
68 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ++BBI)
69 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
70 if (isa<ConstantPointerNull>(SI->getOperand(1)) ||
71 isa<UndefValue>(SI->getOperand(1))) {
72 // Loop over all of the successors, removing BB's entry from any PHI
73 // nodes.
74 for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE;++I)
75 (*I)->removePredecessor(BB);
Chris Lattnera67dd322004-10-18 03:00:50 +000076
Chris Lattner108083e2007-04-05 01:27:02 +000077 new UnreachableInst(SI);
Chris Lattnera67dd322004-10-18 03:00:50 +000078
Chris Lattner108083e2007-04-05 01:27:02 +000079 // All instructions after this are dead.
80 while (BBI != E) {
81 if (!BBI->use_empty())
82 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
83 BB->getInstList().erase(BBI++);
84 }
85 break;
Chris Lattnera67dd322004-10-18 03:00:50 +000086 }
Chris Lattnera67dd322004-10-18 03:00:50 +000087
88
Chris Lattner108083e2007-04-05 01:27:02 +000089 Changed |= ConstantFoldTerminator(BB);
90 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
91 Worklist.push_back(*SI);
92 }
Chris Lattnerfa7dad82002-05-21 20:49:37 +000093 return Changed;
94}
95
96
97// It is possible that we may require multiple passes over the code to fully
98// simplify the CFG.
99//
Chris Lattner113f4f42002-06-25 16:13:24 +0000100bool CFGSimplifyPass::runOnFunction(Function &F) {
Chris Lattnera5403a52007-03-04 04:20:48 +0000101 SmallPtrSet<BasicBlock*, 16> Reachable;
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 bool Changed = MarkAliveBlocks(F.begin(), Reachable);
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000103
104 // If there are unreachable blocks in the CFG...
Chris Lattner113f4f42002-06-25 16:13:24 +0000105 if (Reachable.size() != F.size()) {
106 assert(Reachable.size() < F.size());
107 NumSimpl += F.size()-Reachable.size();
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000108
109 // Loop over all of the basic blocks that are not reachable, dropping all of
110 // their internal references...
Chris Lattner113f4f42002-06-25 16:13:24 +0000111 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
112 if (!Reachable.count(BB)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000113 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
114 if (Reachable.count(*SI))
115 (*SI)->removePredecessor(BB);
116 BB->dropAllReferences();
117 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000118
Chris Lattner113f4f42002-06-25 16:13:24 +0000119 for (Function::iterator I = ++F.begin(); I != F.end();)
120 if (!Reachable.count(I))
121 I = F.getBasicBlockList().erase(I);
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000122 else
123 ++I;
124
125 Changed = true;
126 }
127
128 bool LocalChange = true;
129 while (LocalChange) {
130 LocalChange = false;
131
132 // Loop over all of the basic blocks (except the first one) and remove them
133 // if they are unneeded...
134 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000135 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
136 if (SimplifyCFG(BBIt++)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000137 LocalChange = true;
138 ++NumSimpl;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000139 }
140 }
141 Changed |= LocalChange;
142 }
143
144 return Changed;
145}