blob: b1a1bdee0575fec979250706cebd881f79dc907a [file] [log] [blame]
Chris Lattner01d1ee32002-05-21 20:50:24 +00001//===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
2//
Chris Lattnerbb190ac2002-10-08 21:36:33 +00003// Peephole optimize the CFG.
Chris Lattner01d1ee32002-05-21 20:50:24 +00004//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/Transforms/Utils/Local.h"
8#include "llvm/Constant.h"
9#include "llvm/iPHINode.h"
10#include "llvm/Support/CFG.h"
11#include <algorithm>
12#include <functional>
13
Misha Brukmana3bbcb52002-10-29 23:06:16 +000014// PropagatePredecessors - This gets "Succ" ready to have the predecessors from
Chris Lattner01d1ee32002-05-21 20:50:24 +000015// "BB". This is a little tricky because "Succ" has PHI nodes, which need to
16// have extra slots added to them to hold the merge edges from BB's
Chris Lattnere2ca5402003-03-05 21:01:52 +000017// predecessors, and BB itself might have had PHI nodes in it. This function
18// returns true (failure) if the Succ BB already has a predecessor that is a
19// predecessor of BB and incoming PHI arguments would not be discernable.
Chris Lattner01d1ee32002-05-21 20:50:24 +000020//
21// Assumption: Succ is the single successor for BB.
22//
Misha Brukmana3bbcb52002-10-29 23:06:16 +000023static bool PropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
Chris Lattner01d1ee32002-05-21 20:50:24 +000024 assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
Chris Lattner3abb95d2002-09-24 00:09:26 +000025
26 if (!isa<PHINode>(Succ->front()))
27 return false; // We can make the transformation, no problem.
Chris Lattner01d1ee32002-05-21 20:50:24 +000028
29 // If there is more than one predecessor, and there are PHI nodes in
30 // the successor, then we need to add incoming edges for the PHI nodes
31 //
32 const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
33
34 // Check to see if one of the predecessors of BB is already a predecessor of
Chris Lattnere2ca5402003-03-05 21:01:52 +000035 // Succ. If so, we cannot do the transformation if there are any PHI nodes
36 // with incompatible values coming in from the two edges!
Chris Lattner01d1ee32002-05-21 20:50:24 +000037 //
Chris Lattnere2ca5402003-03-05 21:01:52 +000038 for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); PI != PE; ++PI)
39 if (find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) {
40 // Loop over all of the PHI nodes checking to see if there are
41 // incompatible values coming in.
42 BasicBlock::iterator BBI = Succ->begin();
43 while (PHINode *PN = dyn_cast<PHINode>(&*BBI++)) {
44 // Loop up the entries in the PHI node for BB and for *PI if the values
45 // coming in are non-equal, we cannot merge these two blocks (instead we
46 // should insert a conditional move or something, then merge the
47 // blocks).
48 int Idx1 = PN->getBasicBlockIndex(BB);
49 int Idx2 = PN->getBasicBlockIndex(*PI);
50 assert(Idx1 != -1 && Idx2 != -1 &&
51 "Didn't have entries for my predecessors??");
52 if (PN->getIncomingValue(Idx1) != PN->getIncomingValue(Idx2))
53 return true; // Values are not equal...
54 }
55 }
Chris Lattner01d1ee32002-05-21 20:50:24 +000056
57 // Loop over all of the PHI nodes in the successor BB
58 for (BasicBlock::iterator I = Succ->begin();
Chris Lattner18961502002-06-25 16:12:52 +000059 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
Chris Lattnerbb190ac2002-10-08 21:36:33 +000060 Value *OldVal = PN->removeIncomingValue(BB, false);
Chris Lattner01d1ee32002-05-21 20:50:24 +000061 assert(OldVal && "No entry in PHI for Pred BB!");
62
63 for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(),
64 End = BBPreds.end(); PredI != End; ++PredI) {
65 // Add an incoming value for each of the new incoming values...
66 PN->addIncoming(OldVal, *PredI);
67 }
68 }
69 return false;
70}
71
72
73// SimplifyCFG - This function is used to do simplification of a CFG. For
74// example, it adjusts branches to branches to eliminate the extra hop, it
75// eliminates unreachable basic blocks, and does other "peephole" optimization
Chris Lattnere2ca5402003-03-05 21:01:52 +000076// of the CFG. It returns true if a modification was made.
Chris Lattner01d1ee32002-05-21 20:50:24 +000077//
78// WARNING: The entry node of a function may not be simplified.
79//
Chris Lattner18961502002-06-25 16:12:52 +000080bool SimplifyCFG(BasicBlock *BB) {
Chris Lattner01d1ee32002-05-21 20:50:24 +000081 Function *M = BB->getParent();
82
83 assert(BB && BB->getParent() && "Block not embedded in function!");
84 assert(BB->getTerminator() && "Degenerate basic block encountered!");
Chris Lattner18961502002-06-25 16:12:52 +000085 assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!");
Chris Lattner01d1ee32002-05-21 20:50:24 +000086
87
88 // Remove basic blocks that have no predecessors... which are unreachable.
89 if (pred_begin(BB) == pred_end(BB) &&
90 !BB->hasConstantReferences()) {
91 //cerr << "Removing BB: \n" << BB;
92
93 // Loop through all of our successors and make sure they know that one
94 // of their predecessors is going away.
95 for_each(succ_begin(BB), succ_end(BB),
96 std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB));
97
98 while (!BB->empty()) {
Chris Lattner18961502002-06-25 16:12:52 +000099 Instruction &I = BB->back();
Chris Lattner01d1ee32002-05-21 20:50:24 +0000100 // If this instruction is used, replace uses with an arbitrary
101 // constant value. Because control flow can't get here, we don't care
102 // what we replace the value with. Note that since this block is
103 // unreachable, and all values contained within it must dominate their
104 // uses, that all uses will eventually be removed.
Chris Lattner18961502002-06-25 16:12:52 +0000105 if (!I.use_empty())
Chris Lattner01d1ee32002-05-21 20:50:24 +0000106 // Make all users of this instruction reference the constant instead
Chris Lattner18961502002-06-25 16:12:52 +0000107 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
Chris Lattner01d1ee32002-05-21 20:50:24 +0000108
109 // Remove the instruction from the basic block
Chris Lattner18961502002-06-25 16:12:52 +0000110 BB->getInstList().pop_back();
Chris Lattner01d1ee32002-05-21 20:50:24 +0000111 }
Chris Lattner18961502002-06-25 16:12:52 +0000112 M->getBasicBlockList().erase(BB);
Chris Lattner01d1ee32002-05-21 20:50:24 +0000113 return true;
114 }
115
116 // Check to see if this block has no instructions and only a single
117 // successor. If so, replace block references with successor.
118 succ_iterator SI(succ_begin(BB));
119 if (SI != succ_end(BB) && ++SI == succ_end(BB)) { // One succ?
Chris Lattner18961502002-06-25 16:12:52 +0000120 if (BB->front().isTerminator()) { // Terminator is the only instruction!
Chris Lattner01d1ee32002-05-21 20:50:24 +0000121 BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor
122
123 if (Succ != BB) { // Arg, don't hurt infinite loops!
124 // If our successor has PHI nodes, then we need to update them to
125 // include entries for BB's predecessors, not for BB itself.
126 // Be careful though, if this transformation fails (returns true) then
127 // we cannot do this transformation!
128 //
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000129 if (!PropagatePredecessorsForPHIs(BB, Succ)) {
Chris Lattner01d1ee32002-05-21 20:50:24 +0000130 //cerr << "Killing Trivial BB: \n" << BB;
Chris Lattner01d1ee32002-05-21 20:50:24 +0000131 BB->replaceAllUsesWith(Succ);
Chris Lattner18961502002-06-25 16:12:52 +0000132 std::string OldName = BB->getName();
133
134 // Delete the old basic block...
135 M->getBasicBlockList().erase(BB);
Chris Lattner01d1ee32002-05-21 20:50:24 +0000136
Chris Lattner18961502002-06-25 16:12:52 +0000137 if (!OldName.empty() && !Succ->hasName()) // Transfer name if we can
138 Succ->setName(OldName);
Chris Lattner01d1ee32002-05-21 20:50:24 +0000139
140 //cerr << "Function after removal: \n" << M;
141 return true;
142 }
143 }
144 }
145 }
146
147 // Merge basic blocks into their predecessor if there is only one distinct
148 // pred, and if there is only one distinct successor of the predecessor, and
149 // if there are no PHI nodes.
150 //
Chris Lattner91b65c02002-07-29 21:26:30 +0000151 if (!BB->hasConstantReferences()) {
Chris Lattner01d1ee32002-05-21 20:50:24 +0000152 pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
153 BasicBlock *OnlyPred = *PI++;
154 for (; PI != PE; ++PI) // Search all predecessors, see if they are all same
155 if (*PI != OnlyPred) {
156 OnlyPred = 0; // There are multiple different predecessors...
157 break;
158 }
159
160 BasicBlock *OnlySucc = 0;
161 if (OnlyPred && OnlyPred != BB) { // Don't break self loops
162 // Check to see if there is only one distinct successor...
163 succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred));
164 OnlySucc = BB;
165 for (; SI != SE; ++SI)
166 if (*SI != OnlySucc) {
167 OnlySucc = 0; // There are multiple distinct successors!
168 break;
169 }
170 }
171
172 if (OnlySucc) {
173 //cerr << "Merging: " << BB << "into: " << OnlyPred;
174 TerminatorInst *Term = OnlyPred->getTerminator();
175
Chris Lattner91b65c02002-07-29 21:26:30 +0000176 // Resolve any PHI nodes at the start of the block. They are all
Chris Lattner929b2c62002-09-24 16:09:17 +0000177 // guaranteed to have exactly one entry if they exist, unless there are
178 // multiple duplicate (but guaranteed to be equal) entries for the
179 // incoming edges. This occurs when there are multiple edges from
180 // OnlyPred to OnlySucc.
Chris Lattner91b65c02002-07-29 21:26:30 +0000181 //
182 while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
Chris Lattner91b65c02002-07-29 21:26:30 +0000183 PN->replaceAllUsesWith(PN->getIncomingValue(0));
184 BB->getInstList().pop_front(); // Delete the phi node...
185 }
186
Chris Lattner01d1ee32002-05-21 20:50:24 +0000187 // Delete the unconditional branch from the predecessor...
Chris Lattner18961502002-06-25 16:12:52 +0000188 OnlyPred->getInstList().pop_back();
Chris Lattner01d1ee32002-05-21 20:50:24 +0000189
190 // Move all definitions in the succecessor to the predecessor...
Chris Lattner18961502002-06-25 16:12:52 +0000191 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
192
Chris Lattner01d1ee32002-05-21 20:50:24 +0000193 // Make all PHI nodes that refered to BB now refer to Pred as their
194 // source...
195 BB->replaceAllUsesWith(OnlyPred);
Chris Lattner18961502002-06-25 16:12:52 +0000196
197 std::string OldName = BB->getName();
198
199 // Erase basic block from the function...
200 M->getBasicBlockList().erase(BB);
201
Chris Lattner01d1ee32002-05-21 20:50:24 +0000202 // Inherit predecessors name if it exists...
Chris Lattner18961502002-06-25 16:12:52 +0000203 if (!OldName.empty() && !OnlyPred->hasName())
204 OnlyPred->setName(OldName);
Chris Lattner01d1ee32002-05-21 20:50:24 +0000205
Chris Lattner01d1ee32002-05-21 20:50:24 +0000206 return true;
207 }
208 }
209
210 return false;
211}