blob: 74005757206aa03869828492ddd02409dba2bab0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- SimplifyCFG.cpp - CFG Simplification Pass --------------------------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements dead code elimination and basic block merging.
Gordon Henriksen79ed5872007-11-04 16:15:04 +000011// Specifically:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000012//
Gordon Henriksen79ed5872007-11-04 16:15:04 +000013// * Removes basic blocks with no predecessors.
14// * Merges a basic block into its predecessor if there is only one and the
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015// predecessor only has one successor.
Gordon Henriksen79ed5872007-11-04 16:15:04 +000016// * Eliminates PHI nodes for basic blocks with a single predecessor.
17// * Eliminates a basic block that only contains an unconditional branch.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018//
19//===----------------------------------------------------------------------===//
20
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"
Chris Lattner6d79e882007-11-14 06:19:25 +000027#include "llvm/ParameterAttributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/CFG.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Pass.h"
Chris Lattnereeaa8f62007-11-13 07:32:38 +000031#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/ADT/Statistic.h"
34using namespace llvm;
35
36STATISTIC(NumSimpl, "Number of blocks simplified");
37
38namespace {
39 struct VISIBILITY_HIDDEN CFGSimplifyPass : public FunctionPass {
40 static char ID; // Pass identification, replacement for typeid
41 CFGSimplifyPass() : FunctionPass((intptr_t)&ID) {}
42
43 virtual bool runOnFunction(Function &F);
44 };
45 char CFGSimplifyPass::ID = 0;
46 RegisterPass<CFGSimplifyPass> X("simplifycfg", "Simplify the CFG");
47}
48
49// Public interface to the CFGSimplification pass
50FunctionPass *llvm::createCFGSimplificationPass() {
51 return new CFGSimplifyPass();
52}
53
Chris Lattner6d79e882007-11-14 06:19:25 +000054/// ChangeToUnreachable - Insert an unreachable instruction before the specified
55/// instruction, making it and the rest of the code in the block dead.
56static void ChangeToUnreachable(Instruction *I) {
57 BasicBlock *BB = I->getParent();
58 // Loop over all of the successors, removing BB's entry from any PHI
59 // nodes.
60 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
61 (*SI)->removePredecessor(BB);
62
63 new UnreachableInst(I);
64
65 // All instructions after this are dead.
66 BasicBlock::iterator BBI = I, BBE = BB->end();
67 while (BBI != BBE) {
68 if (!BBI->use_empty())
69 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
70 BB->getInstList().erase(BBI++);
71 }
72}
73
74/// IsNoReturn - Return true if the specified call is to a no-return function.
75static bool IsNoReturn(const CallInst *CI) {
76 if (const ParamAttrsList *Attrs = CI->getParamAttrs())
77 if (Attrs->paramHasAttr(0, ParamAttr::NoReturn))
78 return true;
79
80 if (const Function *Callee = CI->getCalledFunction()) {
81 if (const ParamAttrsList *Attrs = Callee->getParamAttrs())
82 if (Attrs->paramHasAttr(0, ParamAttr::NoReturn))
83 return true;
84
85 const FunctionType *FT = Callee->getFunctionType();
86 if (const ParamAttrsList *Attrs = FT->getParamAttrs())
87 if (Attrs->paramHasAttr(0, ParamAttr::NoReturn))
88 return true;
89 }
90 return false;
91}
92
93
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094static bool MarkAliveBlocks(BasicBlock *BB,
Chris Lattnereeaa8f62007-11-13 07:32:38 +000095 SmallPtrSet<BasicBlock*, 128> &Reachable) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
Chris Lattnereeaa8f62007-11-13 07:32:38 +000097 SmallVector<BasicBlock*, 128> Worklist;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 Worklist.push_back(BB);
99 bool Changed = false;
100 while (!Worklist.empty()) {
101 BB = Worklist.back();
102 Worklist.pop_back();
103
104 if (!Reachable.insert(BB))
105 continue;
106
107 // Do a quick scan of the basic block, turning any obviously unreachable
108 // instructions into LLVM unreachable insts. The instruction combining pass
109 // canonnicalizes unreachable insts into stores to null or undef.
Chris Lattner6d79e882007-11-14 06:19:25 +0000110 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){
111 if (CallInst *CI = dyn_cast<CallInst>(BBI)) {
112 if (IsNoReturn(CI)) {
113 // If we found a call to a no-return function, insert an unreachable
114 // instruction after it. Make sure there isn't *already* one there
115 // though.
116 ++BBI;
117 if (!isa<UnreachableInst>(BBI)) {
118 ChangeToUnreachable(BBI);
119 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 }
121 break;
122 }
Chris Lattner6d79e882007-11-14 06:19:25 +0000123 }
124
125 if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
126 if (isa<ConstantPointerNull>(SI->getOperand(1)) ||
127 isa<UndefValue>(SI->getOperand(1))) {
128 ChangeToUnreachable(SI);
129 Changed = true;
130 break;
131 }
132 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
134 Changed |= ConstantFoldTerminator(BB);
135 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
136 Worklist.push_back(*SI);
137 }
138 return Changed;
139}
140
Chris Lattnereeaa8f62007-11-13 07:32:38 +0000141/// RemoveUnreachableBlocks - Remove blocks that are not reachable, even if they
142/// are in a dead cycle. Return true if a change was made, false otherwise.
143static bool RemoveUnreachableBlocks(Function &F) {
144 SmallPtrSet<BasicBlock*, 128> Reachable;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 bool Changed = MarkAliveBlocks(F.begin(), Reachable);
Chris Lattnereeaa8f62007-11-13 07:32:38 +0000146
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 // If there are unreachable blocks in the CFG...
Chris Lattnereeaa8f62007-11-13 07:32:38 +0000148 if (Reachable.size() == F.size())
149 return Changed;
150
151 assert(Reachable.size() < F.size());
152 NumSimpl += F.size()-Reachable.size();
153
154 // Loop over all of the basic blocks that are not reachable, dropping all of
155 // their internal references...
156 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
157 if (!Reachable.count(BB)) {
158 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
159 if (Reachable.count(*SI))
160 (*SI)->removePredecessor(BB);
161 BB->dropAllReferences();
162 }
163
164 for (Function::iterator I = ++F.begin(); I != F.end();)
165 if (!Reachable.count(I))
166 I = F.getBasicBlockList().erase(I);
167 else
168 ++I;
169
170 return true;
171}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172
Chris Lattnereeaa8f62007-11-13 07:32:38 +0000173/// IterativeSimplifyCFG - Call SimplifyCFG on all the blocks in the function,
174/// iterating until no more changes are made.
175static bool IterativeSimplifyCFG(Function &F) {
176 bool Changed = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 bool LocalChange = true;
178 while (LocalChange) {
179 LocalChange = false;
Chris Lattnereeaa8f62007-11-13 07:32:38 +0000180
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 // Loop over all of the basic blocks (except the first one) and remove them
182 // if they are unneeded...
183 //
184 for (Function::iterator BBIt = ++F.begin(); BBIt != F.end(); ) {
185 if (SimplifyCFG(BBIt++)) {
186 LocalChange = true;
187 ++NumSimpl;
188 }
189 }
190 Changed |= LocalChange;
191 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 return Changed;
193}
Chris Lattnereeaa8f62007-11-13 07:32:38 +0000194
195// It is possible that we may require multiple passes over the code to fully
196// simplify the CFG.
197//
198bool CFGSimplifyPass::runOnFunction(Function &F) {
199 bool EverChanged = RemoveUnreachableBlocks(F);
200 EverChanged |= IterativeSimplifyCFG(F);
201
202 // If neither pass changed anything, we're done.
203 if (!EverChanged) return false;
204
205 // IterativeSimplifyCFG can (rarely) make some loops dead. If this happens,
206 // RemoveUnreachableBlocks is needed to nuke them, which means we should
207 // iterate between the two optimizations. We structure the code like this to
208 // avoid reruning IterativeSimplifyCFG if the second pass of
209 // RemoveUnreachableBlocks doesn't do anything.
210 if (!RemoveUnreachableBlocks(F))
211 return true;
212
213 do {
214 EverChanged = IterativeSimplifyCFG(F);
215 EverChanged |= RemoveUnreachableBlocks(F);
216 } while (EverChanged);
217
218 return true;
219}