blob: 97ea9f4ce55df651856b45ca2e04b77a7ff46426 [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/ADT/Statistic.h"
Chris Lattnerfa7dad82002-05-21 20:49:37 +000031#include <set>
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 {
Chris Lattner113f4f42002-06-25 16:13:24 +000038 virtual bool runOnFunction(Function &F);
Chris Lattnerfa7dad82002-05-21 20:49:37 +000039 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000040 RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
Chris Lattnerfa7dad82002-05-21 20:49:37 +000041}
42
Brian Gaeke960707c2003-11-11 22:41:34 +000043// Public interface to the CFGSimplification pass
Chris Lattner49525f82004-01-09 06:02:20 +000044FunctionPass *llvm::createCFGSimplificationPass() {
Chris Lattnerfa7dad82002-05-21 20:49:37 +000045 return new CFGSimplifyPass();
46}
47
48static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
49 if (Reachable.count(BB)) return false;
50 Reachable.insert(BB);
51
Chris Lattnera67dd322004-10-18 03:00:50 +000052 // Do a quick scan of the basic block, turning any obviously unreachable
53 // instructions into LLVM unreachable insts. The instruction combining pass
54 // canonnicalizes unreachable insts into stores to null or undef.
55 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ++BBI)
56 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
57 if (isa<ConstantPointerNull>(SI->getOperand(1)) ||
58 isa<UndefValue>(SI->getOperand(1))) {
59 // Loop over all of the successors, removing BB's entry from any PHI
60 // nodes.
61 for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
62 (*I)->removePredecessor(BB);
63
64 new UnreachableInst(SI);
Chris Lattnera67dd322004-10-18 03:00:50 +000065
66 // All instructions after this are dead.
67 for (; BBI != E; ) {
68 if (!BBI->use_empty())
69 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
70 BB->getInstList().erase(BBI++);
71 }
72 break;
73 }
74
75
Chris Lattnerfa7dad82002-05-21 20:49:37 +000076 bool Changed = ConstantFoldTerminator(BB);
77 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
Reid Spencerefe5c862006-12-08 21:52:01 +000078 Changed |= MarkAliveBlocks(*SI, Reachable);
Chris Lattnerfa7dad82002-05-21 20:49:37 +000079
80 return Changed;
81}
82
83
84// It is possible that we may require multiple passes over the code to fully
85// simplify the CFG.
86//
Chris Lattner113f4f42002-06-25 16:13:24 +000087bool CFGSimplifyPass::runOnFunction(Function &F) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +000088 std::set<BasicBlock*> Reachable;
Chris Lattner113f4f42002-06-25 16:13:24 +000089 bool Changed = MarkAliveBlocks(F.begin(), Reachable);
Chris Lattnerfa7dad82002-05-21 20:49:37 +000090
91 // If there are unreachable blocks in the CFG...
Chris Lattner113f4f42002-06-25 16:13:24 +000092 if (Reachable.size() != F.size()) {
93 assert(Reachable.size() < F.size());
94 NumSimpl += F.size()-Reachable.size();
Chris Lattnerfa7dad82002-05-21 20:49:37 +000095
96 // Loop over all of the basic blocks that are not reachable, dropping all of
97 // their internal references...
Chris Lattner113f4f42002-06-25 16:13:24 +000098 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
99 if (!Reachable.count(BB)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000100 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
101 if (Reachable.count(*SI))
102 (*SI)->removePredecessor(BB);
103 BB->dropAllReferences();
104 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000105
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 for (Function::iterator I = ++F.begin(); I != F.end();)
107 if (!Reachable.count(I))
108 I = F.getBasicBlockList().erase(I);
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000109 else
110 ++I;
111
112 Changed = true;
113 }
114
115 bool LocalChange = true;
116 while (LocalChange) {
117 LocalChange = false;
118
119 // Loop over all of the basic blocks (except the first one) and remove them
120 // if they are unneeded...
121 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000122 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
123 if (SimplifyCFG(BBIt++)) {
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000124 LocalChange = true;
125 ++NumSimpl;
Chris Lattnerfa7dad82002-05-21 20:49:37 +0000126 }
127 }
128 Changed |= LocalChange;
129 }
130
131 return Changed;
132}