blob: a17e7f1399896bff2295f50b09c9a4cc7a95ad5d [file] [log] [blame]
Chris Lattner01d1ee32002-05-21 20:50:24 +00001//===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
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 Lattner01d1ee32002-05-21 20:50:24 +00009//
Chris Lattnerbb190ac2002-10-08 21:36:33 +000010// Peephole optimize the CFG.
Chris Lattner01d1ee32002-05-21 20:50:24 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner218a8222004-06-20 01:13:18 +000014#define DEBUG_TYPE "simplifycfg"
Chris Lattner01d1ee32002-05-21 20:50:24 +000015#include "llvm/Transforms/Utils/Local.h"
Chris Lattner723c66d2004-02-11 03:36:04 +000016#include "llvm/Constants.h"
17#include "llvm/Instructions.h"
Chris Lattner0d560082004-02-24 05:38:11 +000018#include "llvm/Type.h"
Chris Lattner01d1ee32002-05-21 20:50:24 +000019#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/Debug.h"
Chris Lattnereaba3a12005-09-19 23:49:37 +000021#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattner01d1ee32002-05-21 20:50:24 +000022#include <algorithm>
23#include <functional>
Chris Lattnerd52c2612004-02-24 07:23:58 +000024#include <set>
Chris Lattner698f96f2004-10-18 04:07:22 +000025#include <map>
Chris Lattnerf7703df2004-01-09 06:12:26 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner2bdcb562005-08-03 00:19:45 +000028/// SafeToMergeTerminators - Return true if it is safe to merge these two
29/// terminator instructions together.
30///
31static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
32 if (SI1 == SI2) return false; // Can't merge with self!
33
34 // It is not safe to merge these two switch instructions if they have a common
35 // successor, and if that successor has a PHI node, and if *that* PHI node has
36 // conflicting incoming values from the two switch blocks.
37 BasicBlock *SI1BB = SI1->getParent();
38 BasicBlock *SI2BB = SI2->getParent();
39 std::set<BasicBlock*> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
40
41 for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
42 if (SI1Succs.count(*I))
43 for (BasicBlock::iterator BBI = (*I)->begin();
44 isa<PHINode>(BBI); ++BBI) {
45 PHINode *PN = cast<PHINode>(BBI);
46 if (PN->getIncomingValueForBlock(SI1BB) !=
47 PN->getIncomingValueForBlock(SI2BB))
48 return false;
49 }
50
51 return true;
52}
53
54/// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
55/// now be entries in it from the 'NewPred' block. The values that will be
56/// flowing into the PHI nodes will be the same as those coming in from
57/// ExistPred, an existing predecessor of Succ.
58static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
59 BasicBlock *ExistPred) {
60 assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) !=
61 succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!");
62 if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
63
64 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
65 PHINode *PN = cast<PHINode>(I);
66 Value *V = PN->getIncomingValueForBlock(ExistPred);
67 PN->addIncoming(V, NewPred);
68 }
69}
70
Chris Lattner3b3efc72005-08-03 00:29:26 +000071// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
72// almost-empty BB ending in an unconditional branch to Succ, into succ.
Chris Lattner01d1ee32002-05-21 20:50:24 +000073//
74// Assumption: Succ is the single successor for BB.
75//
Chris Lattner3b3efc72005-08-03 00:29:26 +000076static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
Chris Lattner01d1ee32002-05-21 20:50:24 +000077 assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
Chris Lattner3abb95d2002-09-24 00:09:26 +000078
Chris Lattner01d1ee32002-05-21 20:50:24 +000079 // Check to see if one of the predecessors of BB is already a predecessor of
Chris Lattnere2ca5402003-03-05 21:01:52 +000080 // Succ. If so, we cannot do the transformation if there are any PHI nodes
81 // with incompatible values coming in from the two edges!
Chris Lattner01d1ee32002-05-21 20:50:24 +000082 //
Chris Lattnerdc88dbe2005-08-03 00:38:27 +000083 if (isa<PHINode>(Succ->front())) {
84 std::set<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
Chris Lattner8e75ee22005-12-03 18:25:58 +000085 for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
Chris Lattnerdc88dbe2005-08-03 00:38:27 +000086 PI != PE; ++PI)
87 if (std::find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) {
88 // Loop over all of the PHI nodes checking to see if there are
89 // incompatible values coming in.
90 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
91 PHINode *PN = cast<PHINode>(I);
92 // Loop up the entries in the PHI node for BB and for *PI if the
93 // values coming in are non-equal, we cannot merge these two blocks
94 // (instead we should insert a conditional move or something, then
95 // merge the blocks).
96 if (PN->getIncomingValueForBlock(BB) !=
97 PN->getIncomingValueForBlock(*PI))
98 return false; // Values are not equal...
99 }
100 }
101 }
Chris Lattner1aad9212005-08-03 00:59:12 +0000102
103 // Finally, if BB has PHI nodes that are used by things other than the PHIs in
104 // Succ and Succ has predecessors that are not Succ and not Pred, we cannot
105 // fold these blocks, as we don't know whether BB dominates Succ or not to
106 // update the PHI nodes correctly.
107 if (!isa<PHINode>(BB->begin()) || Succ->getSinglePredecessor()) return true;
Chris Lattner01d1ee32002-05-21 20:50:24 +0000108
Chris Lattner1aad9212005-08-03 00:59:12 +0000109 // If the predecessors of Succ are only BB and Succ itself, we can handle this.
110 bool IsSafe = true;
111 for (pred_iterator PI = pred_begin(Succ), E = pred_end(Succ); PI != E; ++PI)
112 if (*PI != Succ && *PI != BB) {
113 IsSafe = false;
114 break;
115 }
116 if (IsSafe) return true;
117
Chris Lattner8e75ee22005-12-03 18:25:58 +0000118 // If the PHI nodes in BB are only used by instructions in Succ, we are ok if
119 // BB and Succ have no common predecessors.
Chris Lattnera0fcc3e2006-05-14 18:45:44 +0000120 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) {
Chris Lattner1aad9212005-08-03 00:59:12 +0000121 PHINode *PN = cast<PHINode>(I);
122 for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E;
123 ++UI)
Chris Lattner8e75ee22005-12-03 18:25:58 +0000124 if (cast<Instruction>(*UI)->getParent() != Succ)
125 return false;
Chris Lattner1aad9212005-08-03 00:59:12 +0000126 }
127
Chris Lattner8e75ee22005-12-03 18:25:58 +0000128 // Scan the predecessor sets of BB and Succ, making sure there are no common
129 // predecessors. Common predecessors would cause us to build a phi node with
130 // differing incoming values, which is not legal.
131 std::set<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
132 for (pred_iterator PI = pred_begin(Succ), E = pred_end(Succ); PI != E; ++PI)
133 if (BBPreds.count(*PI))
134 return false;
135
136 return true;
Chris Lattner01d1ee32002-05-21 20:50:24 +0000137}
138
Chris Lattner7e663482005-08-03 00:11:16 +0000139/// TryToSimplifyUncondBranchFromEmptyBlock - BB contains an unconditional
140/// branch to Succ, and contains no instructions other than PHI nodes and the
141/// branch. If possible, eliminate BB.
142static bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
143 BasicBlock *Succ) {
144 // If our successor has PHI nodes, then we need to update them to include
145 // entries for BB's predecessors, not for BB itself. Be careful though,
146 // if this transformation fails (returns true) then we cannot do this
147 // transformation!
148 //
Chris Lattner3b3efc72005-08-03 00:29:26 +0000149 if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
Chris Lattner7e663482005-08-03 00:11:16 +0000150
Bill Wendling0d45a092006-11-26 10:17:54 +0000151 DOUT << "Killing Trivial BB: \n" << *BB;
Chris Lattner7e663482005-08-03 00:11:16 +0000152
Chris Lattner3b3efc72005-08-03 00:29:26 +0000153 if (isa<PHINode>(Succ->begin())) {
154 // If there is more than one pred of succ, and there are PHI nodes in
155 // the successor, then we need to add incoming edges for the PHI nodes
156 //
157 const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
158
159 // Loop over all of the PHI nodes in the successor of BB.
160 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
161 PHINode *PN = cast<PHINode>(I);
162 Value *OldVal = PN->removeIncomingValue(BB, false);
163 assert(OldVal && "No entry in PHI for Pred BB!");
164
Chris Lattnerdc88dbe2005-08-03 00:38:27 +0000165 // If this incoming value is one of the PHI nodes in BB, the new entries
166 // in the PHI node are the entries from the old PHI.
Chris Lattner3b3efc72005-08-03 00:29:26 +0000167 if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
168 PHINode *OldValPN = cast<PHINode>(OldVal);
169 for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
170 PN->addIncoming(OldValPN->getIncomingValue(i),
171 OldValPN->getIncomingBlock(i));
172 } else {
173 for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(),
174 End = BBPreds.end(); PredI != End; ++PredI) {
175 // Add an incoming value for each of the new incoming values...
176 PN->addIncoming(OldVal, *PredI);
177 }
178 }
179 }
180 }
181
Chris Lattner7e663482005-08-03 00:11:16 +0000182 if (isa<PHINode>(&BB->front())) {
183 std::vector<BasicBlock*>
184 OldSuccPreds(pred_begin(Succ), pred_end(Succ));
185
186 // Move all PHI nodes in BB to Succ if they are alive, otherwise
187 // delete them.
188 while (PHINode *PN = dyn_cast<PHINode>(&BB->front()))
Chris Lattnerdc88dbe2005-08-03 00:38:27 +0000189 if (PN->use_empty()) {
190 // Just remove the dead phi. This happens if Succ's PHIs were the only
191 // users of the PHI nodes.
192 PN->eraseFromParent();
Chris Lattner7e663482005-08-03 00:11:16 +0000193 } else {
194 // The instruction is alive, so this means that Succ must have
195 // *ONLY* had BB as a predecessor, and the PHI node is still valid
196 // now. Simply move it into Succ, because we know that BB
197 // strictly dominated Succ.
Chris Lattnerd423b8b2005-08-03 00:23:42 +0000198 Succ->getInstList().splice(Succ->begin(),
199 BB->getInstList(), BB->begin());
Chris Lattner7e663482005-08-03 00:11:16 +0000200
201 // We need to add new entries for the PHI node to account for
202 // predecessors of Succ that the PHI node does not take into
203 // account. At this point, since we know that BB dominated succ,
204 // this means that we should any newly added incoming edges should
205 // use the PHI node as the value for these edges, because they are
206 // loop back edges.
207 for (unsigned i = 0, e = OldSuccPreds.size(); i != e; ++i)
208 if (OldSuccPreds[i] != BB)
209 PN->addIncoming(PN, OldSuccPreds[i]);
210 }
211 }
212
213 // Everything that jumped to BB now goes to Succ.
214 std::string OldName = BB->getName();
215 BB->replaceAllUsesWith(Succ);
216 BB->eraseFromParent(); // Delete the old basic block.
217
218 if (!OldName.empty() && !Succ->hasName()) // Transfer name if we can
219 Succ->setName(OldName);
220 return true;
221}
222
Chris Lattner723c66d2004-02-11 03:36:04 +0000223/// GetIfCondition - Given a basic block (BB) with two predecessors (and
224/// presumably PHI nodes in it), check to see if the merge at this block is due
225/// to an "if condition". If so, return the boolean condition that determines
226/// which entry into BB will be taken. Also, return by references the block
227/// that will be entered from if the condition is true, and the block that will
228/// be entered if the condition is false.
Misha Brukmanfd939082005-04-21 23:48:37 +0000229///
Chris Lattner723c66d2004-02-11 03:36:04 +0000230///
231static Value *GetIfCondition(BasicBlock *BB,
232 BasicBlock *&IfTrue, BasicBlock *&IfFalse) {
233 assert(std::distance(pred_begin(BB), pred_end(BB)) == 2 &&
234 "Function can only handle blocks with 2 predecessors!");
235 BasicBlock *Pred1 = *pred_begin(BB);
236 BasicBlock *Pred2 = *++pred_begin(BB);
237
238 // We can only handle branches. Other control flow will be lowered to
239 // branches if possible anyway.
240 if (!isa<BranchInst>(Pred1->getTerminator()) ||
241 !isa<BranchInst>(Pred2->getTerminator()))
242 return 0;
243 BranchInst *Pred1Br = cast<BranchInst>(Pred1->getTerminator());
244 BranchInst *Pred2Br = cast<BranchInst>(Pred2->getTerminator());
245
246 // Eliminate code duplication by ensuring that Pred1Br is conditional if
247 // either are.
248 if (Pred2Br->isConditional()) {
249 // If both branches are conditional, we don't have an "if statement". In
250 // reality, we could transform this case, but since the condition will be
251 // required anyway, we stand no chance of eliminating it, so the xform is
252 // probably not profitable.
253 if (Pred1Br->isConditional())
254 return 0;
255
256 std::swap(Pred1, Pred2);
257 std::swap(Pred1Br, Pred2Br);
258 }
259
260 if (Pred1Br->isConditional()) {
261 // If we found a conditional branch predecessor, make sure that it branches
262 // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
263 if (Pred1Br->getSuccessor(0) == BB &&
264 Pred1Br->getSuccessor(1) == Pred2) {
265 IfTrue = Pred1;
266 IfFalse = Pred2;
267 } else if (Pred1Br->getSuccessor(0) == Pred2 &&
268 Pred1Br->getSuccessor(1) == BB) {
269 IfTrue = Pred2;
270 IfFalse = Pred1;
271 } else {
272 // We know that one arm of the conditional goes to BB, so the other must
273 // go somewhere unrelated, and this must not be an "if statement".
274 return 0;
275 }
276
277 // The only thing we have to watch out for here is to make sure that Pred2
278 // doesn't have incoming edges from other blocks. If it does, the condition
279 // doesn't dominate BB.
280 if (++pred_begin(Pred2) != pred_end(Pred2))
281 return 0;
282
283 return Pred1Br->getCondition();
284 }
285
286 // Ok, if we got here, both predecessors end with an unconditional branch to
287 // BB. Don't panic! If both blocks only have a single (identical)
288 // predecessor, and THAT is a conditional branch, then we're all ok!
289 if (pred_begin(Pred1) == pred_end(Pred1) ||
290 ++pred_begin(Pred1) != pred_end(Pred1) ||
291 pred_begin(Pred2) == pred_end(Pred2) ||
292 ++pred_begin(Pred2) != pred_end(Pred2) ||
293 *pred_begin(Pred1) != *pred_begin(Pred2))
294 return 0;
295
296 // Otherwise, if this is a conditional branch, then we can use it!
297 BasicBlock *CommonPred = *pred_begin(Pred1);
298 if (BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator())) {
299 assert(BI->isConditional() && "Two successors but not conditional?");
300 if (BI->getSuccessor(0) == Pred1) {
301 IfTrue = Pred1;
302 IfFalse = Pred2;
303 } else {
304 IfTrue = Pred2;
305 IfFalse = Pred1;
306 }
307 return BI->getCondition();
308 }
309 return 0;
310}
311
312
313// If we have a merge point of an "if condition" as accepted above, return true
314// if the specified value dominates the block. We don't handle the true
315// generality of domination here, just a special case which works well enough
316// for us.
Chris Lattner9c078662004-10-14 05:13:36 +0000317//
318// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
319// see if V (which must be an instruction) is cheap to compute and is
320// non-trapping. If both are true, the instruction is inserted into the set and
321// true is returned.
322static bool DominatesMergePoint(Value *V, BasicBlock *BB,
323 std::set<Instruction*> *AggressiveInsts) {
Chris Lattner570751c2004-04-09 22:50:22 +0000324 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattnerb74b1812006-10-20 00:42:07 +0000325 if (!I) {
326 // Non-instructions all dominate instructions, but not all constantexprs
327 // can be executed unconditionally.
328 if (ConstantExpr *C = dyn_cast<ConstantExpr>(V))
329 if (C->canTrap())
330 return false;
331 return true;
332 }
Chris Lattner570751c2004-04-09 22:50:22 +0000333 BasicBlock *PBB = I->getParent();
Chris Lattner723c66d2004-02-11 03:36:04 +0000334
Chris Lattnerda895d62005-02-27 06:18:25 +0000335 // We don't want to allow weird loops that might have the "if condition" in
Chris Lattner570751c2004-04-09 22:50:22 +0000336 // the bottom of this block.
337 if (PBB == BB) return false;
Chris Lattner723c66d2004-02-11 03:36:04 +0000338
Chris Lattner570751c2004-04-09 22:50:22 +0000339 // If this instruction is defined in a block that contains an unconditional
340 // branch to BB, then it must be in the 'conditional' part of the "if
341 // statement".
342 if (BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator()))
343 if (BI->isUnconditional() && BI->getSuccessor(0) == BB) {
Chris Lattner9c078662004-10-14 05:13:36 +0000344 if (!AggressiveInsts) return false;
Chris Lattner570751c2004-04-09 22:50:22 +0000345 // Okay, it looks like the instruction IS in the "condition". Check to
346 // see if its a cheap instruction to unconditionally compute, and if it
347 // only uses stuff defined outside of the condition. If so, hoist it out.
348 switch (I->getOpcode()) {
349 default: return false; // Cannot hoist this out safely.
350 case Instruction::Load:
351 // We can hoist loads that are non-volatile and obviously cannot trap.
352 if (cast<LoadInst>(I)->isVolatile())
353 return false;
354 if (!isa<AllocaInst>(I->getOperand(0)) &&
Reid Spencer460f16c2004-07-18 00:32:14 +0000355 !isa<Constant>(I->getOperand(0)))
Chris Lattner570751c2004-04-09 22:50:22 +0000356 return false;
357
358 // Finally, we have to check to make sure there are no instructions
359 // before the load in its basic block, as we are going to hoist the loop
360 // out to its predecessor.
361 if (PBB->begin() != BasicBlock::iterator(I))
362 return false;
363 break;
364 case Instruction::Add:
365 case Instruction::Sub:
366 case Instruction::And:
367 case Instruction::Or:
368 case Instruction::Xor:
369 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +0000370 case Instruction::LShr:
371 case Instruction::AShr:
Chris Lattnerbf5d4fb2005-04-21 05:31:13 +0000372 case Instruction::SetEQ:
373 case Instruction::SetNE:
374 case Instruction::SetLT:
375 case Instruction::SetGT:
376 case Instruction::SetLE:
377 case Instruction::SetGE:
Chris Lattner570751c2004-04-09 22:50:22 +0000378 break; // These are all cheap and non-trapping instructions.
379 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000380
Chris Lattner570751c2004-04-09 22:50:22 +0000381 // Okay, we can only really hoist these out if their operands are not
382 // defined in the conditional region.
383 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Chris Lattner9c078662004-10-14 05:13:36 +0000384 if (!DominatesMergePoint(I->getOperand(i), BB, 0))
Chris Lattner570751c2004-04-09 22:50:22 +0000385 return false;
Chris Lattner9c078662004-10-14 05:13:36 +0000386 // Okay, it's safe to do this! Remember this instruction.
387 AggressiveInsts->insert(I);
Chris Lattner570751c2004-04-09 22:50:22 +0000388 }
389
Chris Lattner723c66d2004-02-11 03:36:04 +0000390 return true;
391}
Chris Lattner01d1ee32002-05-21 20:50:24 +0000392
Chris Lattner0d560082004-02-24 05:38:11 +0000393// GatherConstantSetEQs - Given a potentially 'or'd together collection of seteq
394// instructions that compare a value against a constant, return the value being
395// compared, and stick the constant into the Values vector.
Chris Lattner1654cff2004-06-19 07:02:14 +0000396static Value *GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values){
Chris Lattner0d560082004-02-24 05:38:11 +0000397 if (Instruction *Inst = dyn_cast<Instruction>(V))
398 if (Inst->getOpcode() == Instruction::SetEQ) {
Chris Lattner1654cff2004-06-19 07:02:14 +0000399 if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) {
Chris Lattner0d560082004-02-24 05:38:11 +0000400 Values.push_back(C);
401 return Inst->getOperand(0);
Chris Lattner1654cff2004-06-19 07:02:14 +0000402 } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) {
Chris Lattner0d560082004-02-24 05:38:11 +0000403 Values.push_back(C);
404 return Inst->getOperand(1);
405 }
406 } else if (Inst->getOpcode() == Instruction::Or) {
407 if (Value *LHS = GatherConstantSetEQs(Inst->getOperand(0), Values))
408 if (Value *RHS = GatherConstantSetEQs(Inst->getOperand(1), Values))
409 if (LHS == RHS)
410 return LHS;
411 }
412 return 0;
413}
414
415// GatherConstantSetNEs - Given a potentially 'and'd together collection of
416// setne instructions that compare a value against a constant, return the value
417// being compared, and stick the constant into the Values vector.
Chris Lattner1654cff2004-06-19 07:02:14 +0000418static Value *GatherConstantSetNEs(Value *V, std::vector<ConstantInt*> &Values){
Chris Lattner0d560082004-02-24 05:38:11 +0000419 if (Instruction *Inst = dyn_cast<Instruction>(V))
420 if (Inst->getOpcode() == Instruction::SetNE) {
Chris Lattner1654cff2004-06-19 07:02:14 +0000421 if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) {
Chris Lattner0d560082004-02-24 05:38:11 +0000422 Values.push_back(C);
423 return Inst->getOperand(0);
Chris Lattner1654cff2004-06-19 07:02:14 +0000424 } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) {
Chris Lattner0d560082004-02-24 05:38:11 +0000425 Values.push_back(C);
426 return Inst->getOperand(1);
427 }
428 } else if (Inst->getOpcode() == Instruction::Cast) {
429 // Cast of X to bool is really a comparison against zero.
430 assert(Inst->getType() == Type::BoolTy && "Can only handle bool values!");
Chris Lattner1654cff2004-06-19 07:02:14 +0000431 Values.push_back(ConstantInt::get(Inst->getOperand(0)->getType(), 0));
Chris Lattner0d560082004-02-24 05:38:11 +0000432 return Inst->getOperand(0);
433 } else if (Inst->getOpcode() == Instruction::And) {
434 if (Value *LHS = GatherConstantSetNEs(Inst->getOperand(0), Values))
435 if (Value *RHS = GatherConstantSetNEs(Inst->getOperand(1), Values))
436 if (LHS == RHS)
437 return LHS;
438 }
439 return 0;
440}
441
442
443
444/// GatherValueComparisons - If the specified Cond is an 'and' or 'or' of a
445/// bunch of comparisons of one value against constants, return the value and
446/// the constants being compared.
447static bool GatherValueComparisons(Instruction *Cond, Value *&CompVal,
Chris Lattner1654cff2004-06-19 07:02:14 +0000448 std::vector<ConstantInt*> &Values) {
Chris Lattner0d560082004-02-24 05:38:11 +0000449 if (Cond->getOpcode() == Instruction::Or) {
450 CompVal = GatherConstantSetEQs(Cond, Values);
451
452 // Return true to indicate that the condition is true if the CompVal is
453 // equal to one of the constants.
454 return true;
455 } else if (Cond->getOpcode() == Instruction::And) {
456 CompVal = GatherConstantSetNEs(Cond, Values);
Misha Brukmanfd939082005-04-21 23:48:37 +0000457
Chris Lattner0d560082004-02-24 05:38:11 +0000458 // Return false to indicate that the condition is false if the CompVal is
459 // equal to one of the constants.
460 return false;
461 }
462 return false;
463}
464
465/// ErasePossiblyDeadInstructionTree - If the specified instruction is dead and
466/// has no side effects, nuke it. If it uses any instructions that become dead
467/// because the instruction is now gone, nuke them too.
468static void ErasePossiblyDeadInstructionTree(Instruction *I) {
Chris Lattner8cfe6332006-08-03 21:40:24 +0000469 if (!isInstructionTriviallyDead(I)) return;
470
471 std::vector<Instruction*> InstrsToInspect;
472 InstrsToInspect.push_back(I);
473
474 while (!InstrsToInspect.empty()) {
475 I = InstrsToInspect.back();
476 InstrsToInspect.pop_back();
477
478 if (!isInstructionTriviallyDead(I)) continue;
479
480 // If I is in the work list multiple times, remove previous instances.
481 for (unsigned i = 0, e = InstrsToInspect.size(); i != e; ++i)
482 if (InstrsToInspect[i] == I) {
483 InstrsToInspect.erase(InstrsToInspect.begin()+i);
484 --i, --e;
485 }
486
487 // Add operands of dead instruction to worklist.
488 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
489 if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i)))
490 InstrsToInspect.push_back(OpI);
491
492 // Remove dead instruction.
493 I->eraseFromParent();
Chris Lattner0d560082004-02-24 05:38:11 +0000494 }
495}
496
Chris Lattner542f1492004-02-28 21:28:10 +0000497// isValueEqualityComparison - Return true if the specified terminator checks to
498// see if a value is equal to constant integer value.
499static Value *isValueEqualityComparison(TerminatorInst *TI) {
Chris Lattner4bebf082004-03-16 19:45:22 +0000500 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
501 // Do not permit merging of large switch instructions into their
502 // predecessors unless there is only one predecessor.
503 if (SI->getNumSuccessors() * std::distance(pred_begin(SI->getParent()),
504 pred_end(SI->getParent())) > 128)
505 return 0;
506
Chris Lattner542f1492004-02-28 21:28:10 +0000507 return SI->getCondition();
Chris Lattner4bebf082004-03-16 19:45:22 +0000508 }
Chris Lattner542f1492004-02-28 21:28:10 +0000509 if (BranchInst *BI = dyn_cast<BranchInst>(TI))
510 if (BI->isConditional() && BI->getCondition()->hasOneUse())
511 if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition()))
512 if ((SCI->getOpcode() == Instruction::SetEQ ||
Misha Brukmanfd939082005-04-21 23:48:37 +0000513 SCI->getOpcode() == Instruction::SetNE) &&
Chris Lattner542f1492004-02-28 21:28:10 +0000514 isa<ConstantInt>(SCI->getOperand(1)))
515 return SCI->getOperand(0);
516 return 0;
517}
518
519// Given a value comparison instruction, decode all of the 'cases' that it
520// represents and return the 'default' block.
521static BasicBlock *
Misha Brukmanfd939082005-04-21 23:48:37 +0000522GetValueEqualityComparisonCases(TerminatorInst *TI,
Chris Lattner542f1492004-02-28 21:28:10 +0000523 std::vector<std::pair<ConstantInt*,
524 BasicBlock*> > &Cases) {
525 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
526 Cases.reserve(SI->getNumCases());
527 for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
Chris Lattnerbe54dcc2005-02-26 18:33:28 +0000528 Cases.push_back(std::make_pair(SI->getCaseValue(i), SI->getSuccessor(i)));
Chris Lattner542f1492004-02-28 21:28:10 +0000529 return SI->getDefaultDest();
530 }
531
532 BranchInst *BI = cast<BranchInst>(TI);
533 SetCondInst *SCI = cast<SetCondInst>(BI->getCondition());
534 Cases.push_back(std::make_pair(cast<ConstantInt>(SCI->getOperand(1)),
535 BI->getSuccessor(SCI->getOpcode() ==
536 Instruction::SetNE)));
537 return BI->getSuccessor(SCI->getOpcode() == Instruction::SetEQ);
538}
539
540
Chris Lattner623369a2005-02-24 06:17:52 +0000541// EliminateBlockCases - Given an vector of bb/value pairs, remove any entries
542// in the list that match the specified block.
Misha Brukmanfd939082005-04-21 23:48:37 +0000543static void EliminateBlockCases(BasicBlock *BB,
Chris Lattner623369a2005-02-24 06:17:52 +0000544 std::vector<std::pair<ConstantInt*, BasicBlock*> > &Cases) {
545 for (unsigned i = 0, e = Cases.size(); i != e; ++i)
546 if (Cases[i].second == BB) {
547 Cases.erase(Cases.begin()+i);
548 --i; --e;
549 }
550}
551
552// ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as
553// well.
554static bool
555ValuesOverlap(std::vector<std::pair<ConstantInt*, BasicBlock*> > &C1,
556 std::vector<std::pair<ConstantInt*, BasicBlock*> > &C2) {
557 std::vector<std::pair<ConstantInt*, BasicBlock*> > *V1 = &C1, *V2 = &C2;
558
559 // Make V1 be smaller than V2.
560 if (V1->size() > V2->size())
561 std::swap(V1, V2);
562
563 if (V1->size() == 0) return false;
564 if (V1->size() == 1) {
565 // Just scan V2.
566 ConstantInt *TheVal = (*V1)[0].first;
567 for (unsigned i = 0, e = V2->size(); i != e; ++i)
568 if (TheVal == (*V2)[i].first)
569 return true;
570 }
571
572 // Otherwise, just sort both lists and compare element by element.
573 std::sort(V1->begin(), V1->end());
574 std::sort(V2->begin(), V2->end());
575 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
576 while (i1 != e1 && i2 != e2) {
577 if ((*V1)[i1].first == (*V2)[i2].first)
578 return true;
579 if ((*V1)[i1].first < (*V2)[i2].first)
580 ++i1;
581 else
582 ++i2;
583 }
584 return false;
585}
586
587// SimplifyEqualityComparisonWithOnlyPredecessor - If TI is known to be a
588// terminator instruction and its block is known to only have a single
589// predecessor block, check to see if that predecessor is also a value
590// comparison with the same value, and if that comparison determines the outcome
591// of this comparison. If so, simplify TI. This does a very limited form of
592// jump threading.
593static bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
594 BasicBlock *Pred) {
595 Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
596 if (!PredVal) return false; // Not a value comparison in predecessor.
597
598 Value *ThisVal = isValueEqualityComparison(TI);
599 assert(ThisVal && "This isn't a value comparison!!");
600 if (ThisVal != PredVal) return false; // Different predicates.
601
602 // Find out information about when control will move from Pred to TI's block.
603 std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases;
604 BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(),
605 PredCases);
606 EliminateBlockCases(PredDef, PredCases); // Remove default from cases.
Misha Brukmanfd939082005-04-21 23:48:37 +0000607
Chris Lattner623369a2005-02-24 06:17:52 +0000608 // Find information about how control leaves this block.
609 std::vector<std::pair<ConstantInt*, BasicBlock*> > ThisCases;
610 BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
611 EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases.
612
613 // If TI's block is the default block from Pred's comparison, potentially
614 // simplify TI based on this knowledge.
615 if (PredDef == TI->getParent()) {
616 // If we are here, we know that the value is none of those cases listed in
617 // PredCases. If there are any cases in ThisCases that are in PredCases, we
618 // can simplify TI.
619 if (ValuesOverlap(PredCases, ThisCases)) {
620 if (BranchInst *BTI = dyn_cast<BranchInst>(TI)) {
621 // Okay, one of the successors of this condbr is dead. Convert it to a
622 // uncond br.
623 assert(ThisCases.size() == 1 && "Branch can only have one case!");
624 Value *Cond = BTI->getCondition();
625 // Insert the new branch.
626 Instruction *NI = new BranchInst(ThisDef, TI);
627
628 // Remove PHI node entries for the dead edge.
629 ThisCases[0].second->removePredecessor(TI->getParent());
630
Bill Wendling0d45a092006-11-26 10:17:54 +0000631 DOUT << "Threading pred instr: " << *Pred->getTerminator()
632 << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n";
Chris Lattner623369a2005-02-24 06:17:52 +0000633
634 TI->eraseFromParent(); // Nuke the old one.
635 // If condition is now dead, nuke it.
636 if (Instruction *CondI = dyn_cast<Instruction>(Cond))
637 ErasePossiblyDeadInstructionTree(CondI);
638 return true;
639
640 } else {
641 SwitchInst *SI = cast<SwitchInst>(TI);
642 // Okay, TI has cases that are statically dead, prune them away.
643 std::set<Constant*> DeadCases;
644 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
645 DeadCases.insert(PredCases[i].first);
646
Bill Wendling0d45a092006-11-26 10:17:54 +0000647 DOUT << "Threading pred instr: " << *Pred->getTerminator()
648 << "Through successor TI: " << *TI;
Chris Lattner623369a2005-02-24 06:17:52 +0000649
650 for (unsigned i = SI->getNumCases()-1; i != 0; --i)
651 if (DeadCases.count(SI->getCaseValue(i))) {
652 SI->getSuccessor(i)->removePredecessor(TI->getParent());
653 SI->removeCase(i);
654 }
655
Bill Wendling0d45a092006-11-26 10:17:54 +0000656 DOUT << "Leaving: " << *TI << "\n";
Chris Lattner623369a2005-02-24 06:17:52 +0000657 return true;
658 }
659 }
660
661 } else {
662 // Otherwise, TI's block must correspond to some matched value. Find out
663 // which value (or set of values) this is.
664 ConstantInt *TIV = 0;
665 BasicBlock *TIBB = TI->getParent();
666 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
667 if (PredCases[i].second == TIBB)
668 if (TIV == 0)
669 TIV = PredCases[i].first;
670 else
671 return false; // Cannot handle multiple values coming to this block.
672 assert(TIV && "No edge from pred to succ?");
673
674 // Okay, we found the one constant that our value can be if we get into TI's
675 // BB. Find out which successor will unconditionally be branched to.
676 BasicBlock *TheRealDest = 0;
677 for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
678 if (ThisCases[i].first == TIV) {
679 TheRealDest = ThisCases[i].second;
680 break;
681 }
682
683 // If not handled by any explicit cases, it is handled by the default case.
684 if (TheRealDest == 0) TheRealDest = ThisDef;
685
686 // Remove PHI node entries for dead edges.
687 BasicBlock *CheckEdge = TheRealDest;
688 for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
689 if (*SI != CheckEdge)
690 (*SI)->removePredecessor(TIBB);
691 else
692 CheckEdge = 0;
693
694 // Insert the new branch.
695 Instruction *NI = new BranchInst(TheRealDest, TI);
696
Bill Wendling0d45a092006-11-26 10:17:54 +0000697 DOUT << "Threading pred instr: " << *Pred->getTerminator()
698 << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n";
Chris Lattner623369a2005-02-24 06:17:52 +0000699 Instruction *Cond = 0;
700 if (BranchInst *BI = dyn_cast<BranchInst>(TI))
701 Cond = dyn_cast<Instruction>(BI->getCondition());
702 TI->eraseFromParent(); // Nuke the old one.
703
704 if (Cond) ErasePossiblyDeadInstructionTree(Cond);
705 return true;
706 }
707 return false;
708}
709
Chris Lattner542f1492004-02-28 21:28:10 +0000710// FoldValueComparisonIntoPredecessors - The specified terminator is a value
711// equality comparison instruction (either a switch or a branch on "X == c").
712// See if any of the predecessors of the terminator block are value comparisons
713// on the same value. If so, and if safe to do so, fold them together.
714static bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI) {
715 BasicBlock *BB = TI->getParent();
716 Value *CV = isValueEqualityComparison(TI); // CondVal
717 assert(CV && "Not a comparison?");
718 bool Changed = false;
719
720 std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
721 while (!Preds.empty()) {
722 BasicBlock *Pred = Preds.back();
723 Preds.pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000724
Chris Lattner542f1492004-02-28 21:28:10 +0000725 // See if the predecessor is a comparison with the same value.
726 TerminatorInst *PTI = Pred->getTerminator();
727 Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
728
729 if (PCV == CV && SafeToMergeTerminators(TI, PTI)) {
730 // Figure out which 'cases' to copy from SI to PSI.
731 std::vector<std::pair<ConstantInt*, BasicBlock*> > BBCases;
732 BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
733
734 std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases;
735 BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
736
737 // Based on whether the default edge from PTI goes to BB or not, fill in
738 // PredCases and PredDefault with the new switch cases we would like to
739 // build.
740 std::vector<BasicBlock*> NewSuccessors;
741
742 if (PredDefault == BB) {
743 // If this is the default destination from PTI, only the edges in TI
744 // that don't occur in PTI, or that branch to BB will be activated.
745 std::set<ConstantInt*> PTIHandled;
746 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
747 if (PredCases[i].second != BB)
748 PTIHandled.insert(PredCases[i].first);
749 else {
750 // The default destination is BB, we don't need explicit targets.
751 std::swap(PredCases[i], PredCases.back());
752 PredCases.pop_back();
753 --i; --e;
754 }
755
756 // Reconstruct the new switch statement we will be building.
757 if (PredDefault != BBDefault) {
758 PredDefault->removePredecessor(Pred);
759 PredDefault = BBDefault;
760 NewSuccessors.push_back(BBDefault);
761 }
762 for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
763 if (!PTIHandled.count(BBCases[i].first) &&
764 BBCases[i].second != BBDefault) {
765 PredCases.push_back(BBCases[i]);
766 NewSuccessors.push_back(BBCases[i].second);
767 }
768
769 } else {
770 // If this is not the default destination from PSI, only the edges
771 // in SI that occur in PSI with a destination of BB will be
772 // activated.
773 std::set<ConstantInt*> PTIHandled;
774 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
775 if (PredCases[i].second == BB) {
776 PTIHandled.insert(PredCases[i].first);
777 std::swap(PredCases[i], PredCases.back());
778 PredCases.pop_back();
779 --i; --e;
780 }
781
782 // Okay, now we know which constants were sent to BB from the
783 // predecessor. Figure out where they will all go now.
784 for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
785 if (PTIHandled.count(BBCases[i].first)) {
786 // If this is one we are capable of getting...
787 PredCases.push_back(BBCases[i]);
788 NewSuccessors.push_back(BBCases[i].second);
789 PTIHandled.erase(BBCases[i].first);// This constant is taken care of
790 }
791
792 // If there are any constants vectored to BB that TI doesn't handle,
793 // they must go to the default destination of TI.
794 for (std::set<ConstantInt*>::iterator I = PTIHandled.begin(),
795 E = PTIHandled.end(); I != E; ++I) {
796 PredCases.push_back(std::make_pair(*I, BBDefault));
797 NewSuccessors.push_back(BBDefault);
798 }
799 }
800
801 // Okay, at this point, we know which new successor Pred will get. Make
802 // sure we update the number of entries in the PHI nodes for these
803 // successors.
804 for (unsigned i = 0, e = NewSuccessors.size(); i != e; ++i)
805 AddPredecessorToBlock(NewSuccessors[i], Pred, BB);
806
807 // Now that the successors are updated, create the new Switch instruction.
Chris Lattner37880592005-01-29 00:38:26 +0000808 SwitchInst *NewSI = new SwitchInst(CV, PredDefault, PredCases.size(),PTI);
Chris Lattner542f1492004-02-28 21:28:10 +0000809 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
810 NewSI->addCase(PredCases[i].first, PredCases[i].second);
Chris Lattner13b2f762005-01-01 16:02:12 +0000811
812 Instruction *DeadCond = 0;
813 if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
814 // If PTI is a branch, remember the condition.
815 DeadCond = dyn_cast<Instruction>(BI->getCondition());
Chris Lattner542f1492004-02-28 21:28:10 +0000816 Pred->getInstList().erase(PTI);
817
Chris Lattner13b2f762005-01-01 16:02:12 +0000818 // If the condition is dead now, remove the instruction tree.
819 if (DeadCond) ErasePossiblyDeadInstructionTree(DeadCond);
820
Chris Lattner542f1492004-02-28 21:28:10 +0000821 // Okay, last check. If BB is still a successor of PSI, then we must
822 // have an infinite loop case. If so, add an infinitely looping block
823 // to handle the case to preserve the behavior of the code.
824 BasicBlock *InfLoopBlock = 0;
825 for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
826 if (NewSI->getSuccessor(i) == BB) {
827 if (InfLoopBlock == 0) {
828 // Insert it at the end of the loop, because it's either code,
829 // or it won't matter if it's hot. :)
830 InfLoopBlock = new BasicBlock("infloop", BB->getParent());
831 new BranchInst(InfLoopBlock, InfLoopBlock);
832 }
833 NewSI->setSuccessor(i, InfLoopBlock);
834 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000835
Chris Lattner542f1492004-02-28 21:28:10 +0000836 Changed = true;
837 }
838 }
839 return Changed;
840}
841
Chris Lattner6306d072005-08-03 17:59:45 +0000842/// HoistThenElseCodeToIf - Given a conditional branch that goes to BB1 and
Chris Lattner37dc9382004-11-30 00:29:14 +0000843/// BB2, hoist any common code in the two blocks up into the branch block. The
844/// caller of this function guarantees that BI's block dominates BB1 and BB2.
845static bool HoistThenElseCodeToIf(BranchInst *BI) {
846 // This does very trivial matching, with limited scanning, to find identical
847 // instructions in the two blocks. In particular, we don't want to get into
848 // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As
849 // such, we currently just scan for obviously identical instructions in an
850 // identical order.
851 BasicBlock *BB1 = BI->getSuccessor(0); // The true destination.
852 BasicBlock *BB2 = BI->getSuccessor(1); // The false destination
853
854 Instruction *I1 = BB1->begin(), *I2 = BB2->begin();
Chris Lattner6306d072005-08-03 17:59:45 +0000855 if (I1->getOpcode() != I2->getOpcode() || !I1->isIdenticalTo(I2) ||
Chris Lattner120bc212006-10-29 21:21:20 +0000856 isa<PHINode>(I1) || isa<InvokeInst>(I1))
Chris Lattner37dc9382004-11-30 00:29:14 +0000857 return false;
858
859 // If we get here, we can hoist at least one instruction.
860 BasicBlock *BIParent = BI->getParent();
Chris Lattner37dc9382004-11-30 00:29:14 +0000861
862 do {
863 // If we are hoisting the terminator instruction, don't move one (making a
864 // broken BB), instead clone it, and remove BI.
865 if (isa<TerminatorInst>(I1))
866 goto HoistTerminator;
Misha Brukmanfd939082005-04-21 23:48:37 +0000867
Chris Lattner37dc9382004-11-30 00:29:14 +0000868 // For a normal instruction, we just move one to right before the branch,
869 // then replace all uses of the other with the first. Finally, we remove
870 // the now redundant second instruction.
871 BIParent->getInstList().splice(BI, BB1->getInstList(), I1);
872 if (!I2->use_empty())
873 I2->replaceAllUsesWith(I1);
874 BB2->getInstList().erase(I2);
Misha Brukmanfd939082005-04-21 23:48:37 +0000875
Chris Lattner37dc9382004-11-30 00:29:14 +0000876 I1 = BB1->begin();
877 I2 = BB2->begin();
Chris Lattner37dc9382004-11-30 00:29:14 +0000878 } while (I1->getOpcode() == I2->getOpcode() && I1->isIdenticalTo(I2));
879
880 return true;
881
882HoistTerminator:
883 // Okay, it is safe to hoist the terminator.
884 Instruction *NT = I1->clone();
885 BIParent->getInstList().insert(BI, NT);
886 if (NT->getType() != Type::VoidTy) {
887 I1->replaceAllUsesWith(NT);
888 I2->replaceAllUsesWith(NT);
889 NT->setName(I1->getName());
890 }
891
892 // Hoisting one of the terminators from our successor is a great thing.
893 // Unfortunately, the successors of the if/else blocks may have PHI nodes in
894 // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI
895 // nodes, so we insert select instruction to compute the final result.
896 std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects;
897 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
898 PHINode *PN;
899 for (BasicBlock::iterator BBI = SI->begin();
Chris Lattner0f535c62004-11-30 07:47:34 +0000900 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
Chris Lattner37dc9382004-11-30 00:29:14 +0000901 Value *BB1V = PN->getIncomingValueForBlock(BB1);
902 Value *BB2V = PN->getIncomingValueForBlock(BB2);
903 if (BB1V != BB2V) {
904 // These values do not agree. Insert a select instruction before NT
905 // that determines the right value.
906 SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
907 if (SI == 0)
908 SI = new SelectInst(BI->getCondition(), BB1V, BB2V,
909 BB1V->getName()+"."+BB2V->getName(), NT);
910 // Make the PHI node use the select for all incoming values for BB1/BB2
911 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
912 if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2)
913 PN->setIncomingValue(i, SI);
914 }
915 }
916 }
917
918 // Update any PHI nodes in our new successors.
919 for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
920 AddPredecessorToBlock(*SI, BIParent, BB1);
Misha Brukmanfd939082005-04-21 23:48:37 +0000921
Chris Lattner37dc9382004-11-30 00:29:14 +0000922 BI->eraseFromParent();
923 return true;
924}
925
Chris Lattner2e42e362005-09-20 00:43:16 +0000926/// BlockIsSimpleEnoughToThreadThrough - Return true if we can thread a branch
927/// across this block.
928static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
929 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
Chris Lattnere9487f02005-09-20 01:48:40 +0000930 unsigned Size = 0;
931
Chris Lattner2e42e362005-09-20 00:43:16 +0000932 // If this basic block contains anything other than a PHI (which controls the
933 // branch) and branch itself, bail out. FIXME: improve this in the future.
Chris Lattnere9487f02005-09-20 01:48:40 +0000934 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI, ++Size) {
935 if (Size > 10) return false; // Don't clone large BB's.
Chris Lattner2e42e362005-09-20 00:43:16 +0000936
Chris Lattnere9487f02005-09-20 01:48:40 +0000937 // We can only support instructions that are do not define values that are
938 // live outside of the current basic block.
939 for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
940 UI != E; ++UI) {
941 Instruction *U = cast<Instruction>(*UI);
942 if (U->getParent() != BB || isa<PHINode>(U)) return false;
943 }
Chris Lattner2e42e362005-09-20 00:43:16 +0000944
945 // Looks ok, continue checking.
946 }
Chris Lattnere9487f02005-09-20 01:48:40 +0000947
Chris Lattner2e42e362005-09-20 00:43:16 +0000948 return true;
949}
950
Chris Lattnereaba3a12005-09-19 23:49:37 +0000951/// FoldCondBranchOnPHI - If we have a conditional branch on a PHI node value
952/// that is defined in the same block as the branch and if any PHI entries are
953/// constants, thread edges corresponding to that entry to be branches to their
954/// ultimate destination.
955static bool FoldCondBranchOnPHI(BranchInst *BI) {
956 BasicBlock *BB = BI->getParent();
957 PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
Chris Lattner9c88d982005-09-19 23:57:04 +0000958 // NOTE: we currently cannot transform this case if the PHI node is used
959 // outside of the block.
Chris Lattner2e42e362005-09-20 00:43:16 +0000960 if (!PN || PN->getParent() != BB || !PN->hasOneUse())
961 return false;
Chris Lattnereaba3a12005-09-19 23:49:37 +0000962
963 // Degenerate case of a single entry PHI.
964 if (PN->getNumIncomingValues() == 1) {
965 if (PN->getIncomingValue(0) != PN)
966 PN->replaceAllUsesWith(PN->getIncomingValue(0));
967 else
968 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
969 PN->eraseFromParent();
970 return true;
971 }
972
973 // Now we know that this block has multiple preds and two succs.
Chris Lattner2e42e362005-09-20 00:43:16 +0000974 if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false;
Chris Lattnereaba3a12005-09-19 23:49:37 +0000975
976 // Okay, this is a simple enough basic block. See if any phi values are
977 // constants.
978 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
979 if (ConstantBool *CB = dyn_cast<ConstantBool>(PN->getIncomingValue(i))) {
980 // Okay, we now know that all edges from PredBB should be revectored to
981 // branch to RealDest.
982 BasicBlock *PredBB = PN->getIncomingBlock(i);
983 BasicBlock *RealDest = BI->getSuccessor(!CB->getValue());
984
Chris Lattnere9487f02005-09-20 01:48:40 +0000985 if (RealDest == BB) continue; // Skip self loops.
Chris Lattnereaba3a12005-09-19 23:49:37 +0000986
Chris Lattnere9487f02005-09-20 01:48:40 +0000987 // The dest block might have PHI nodes, other predecessors and other
988 // difficult cases. Instead of being smart about this, just insert a new
989 // block that jumps to the destination block, effectively splitting
990 // the edge we are about to create.
991 BasicBlock *EdgeBB = new BasicBlock(RealDest->getName()+".critedge",
992 RealDest->getParent(), RealDest);
993 new BranchInst(RealDest, EdgeBB);
994 PHINode *PN;
995 for (BasicBlock::iterator BBI = RealDest->begin();
996 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
997 Value *V = PN->getIncomingValueForBlock(BB);
998 PN->addIncoming(V, EdgeBB);
999 }
1000
1001 // BB may have instructions that are being threaded over. Clone these
1002 // instructions into EdgeBB. We know that there will be no uses of the
1003 // cloned instructions outside of EdgeBB.
1004 BasicBlock::iterator InsertPt = EdgeBB->begin();
1005 std::map<Value*, Value*> TranslateMap; // Track translated values.
1006 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
1007 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
1008 TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
1009 } else {
1010 // Clone the instruction.
1011 Instruction *N = BBI->clone();
1012 if (BBI->hasName()) N->setName(BBI->getName()+".c");
1013
1014 // Update operands due to translation.
1015 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1016 std::map<Value*, Value*>::iterator PI =
1017 TranslateMap.find(N->getOperand(i));
1018 if (PI != TranslateMap.end())
1019 N->setOperand(i, PI->second);
1020 }
1021
1022 // Check for trivial simplification.
1023 if (Constant *C = ConstantFoldInstruction(N)) {
Chris Lattnere9487f02005-09-20 01:48:40 +00001024 TranslateMap[BBI] = C;
1025 delete N; // Constant folded away, don't need actual inst
1026 } else {
1027 // Insert the new instruction into its new home.
1028 EdgeBB->getInstList().insert(InsertPt, N);
1029 if (!BBI->use_empty())
1030 TranslateMap[BBI] = N;
1031 }
1032 }
1033 }
1034
Chris Lattnereaba3a12005-09-19 23:49:37 +00001035 // Loop over all of the edges from PredBB to BB, changing them to branch
Chris Lattnere9487f02005-09-20 01:48:40 +00001036 // to EdgeBB instead.
Chris Lattnereaba3a12005-09-19 23:49:37 +00001037 TerminatorInst *PredBBTI = PredBB->getTerminator();
1038 for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
1039 if (PredBBTI->getSuccessor(i) == BB) {
1040 BB->removePredecessor(PredBB);
Chris Lattnere9487f02005-09-20 01:48:40 +00001041 PredBBTI->setSuccessor(i, EdgeBB);
Chris Lattnereaba3a12005-09-19 23:49:37 +00001042 }
1043
Chris Lattnereaba3a12005-09-19 23:49:37 +00001044 // Recurse, simplifying any other constants.
1045 return FoldCondBranchOnPHI(BI) | true;
1046 }
1047
1048 return false;
1049}
1050
Chris Lattnerf58c1a52005-09-23 06:39:30 +00001051/// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry
1052/// PHI node, see if we can eliminate it.
1053static bool FoldTwoEntryPHINode(PHINode *PN) {
1054 // Ok, this is a two entry PHI node. Check to see if this is a simple "if
1055 // statement", which has a very simple dominance structure. Basically, we
1056 // are trying to find the condition that is being branched on, which
1057 // subsequently causes this merge to happen. We really want control
1058 // dependence information for this check, but simplifycfg can't keep it up
1059 // to date, and this catches most of the cases we care about anyway.
1060 //
1061 BasicBlock *BB = PN->getParent();
1062 BasicBlock *IfTrue, *IfFalse;
1063 Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
1064 if (!IfCond) return false;
1065
Chris Lattner822a8792006-11-18 19:19:36 +00001066 // Okay, we found that we can merge this two-entry phi node into a select.
1067 // Doing so would require us to fold *all* two entry phi nodes in this block.
1068 // At some point this becomes non-profitable (particularly if the target
1069 // doesn't support cmov's). Only do this transformation if there are two or
1070 // fewer PHI nodes in this block.
1071 unsigned NumPhis = 0;
1072 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
1073 if (NumPhis > 2)
1074 return false;
1075
Bill Wendling0d45a092006-11-26 10:17:54 +00001076 DOUT << "FOUND IF CONDITION! " << *IfCond << " T: "
1077 << IfTrue->getName() << " F: " << IfFalse->getName() << "\n";
Chris Lattnerf58c1a52005-09-23 06:39:30 +00001078
1079 // Loop over the PHI's seeing if we can promote them all to select
1080 // instructions. While we are at it, keep track of the instructions
1081 // that need to be moved to the dominating block.
1082 std::set<Instruction*> AggressiveInsts;
1083
Chris Lattnerf58c1a52005-09-23 06:39:30 +00001084 BasicBlock::iterator AfterPHIIt = BB->begin();
1085 while (isa<PHINode>(AfterPHIIt)) {
1086 PHINode *PN = cast<PHINode>(AfterPHIIt++);
1087 if (PN->getIncomingValue(0) == PN->getIncomingValue(1)) {
1088 if (PN->getIncomingValue(0) != PN)
1089 PN->replaceAllUsesWith(PN->getIncomingValue(0));
1090 else
1091 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
1092 } else if (!DominatesMergePoint(PN->getIncomingValue(0), BB,
1093 &AggressiveInsts) ||
1094 !DominatesMergePoint(PN->getIncomingValue(1), BB,
1095 &AggressiveInsts)) {
Chris Lattner055dc102005-09-23 07:23:18 +00001096 return false;
Chris Lattnerf58c1a52005-09-23 06:39:30 +00001097 }
1098 }
1099
Chris Lattnerf58c1a52005-09-23 06:39:30 +00001100 // If we all PHI nodes are promotable, check to make sure that all
1101 // instructions in the predecessor blocks can be promoted as well. If
1102 // not, we won't be able to get rid of the control flow, so it's not
1103 // worth promoting to select instructions.
1104 BasicBlock *DomBlock = 0, *IfBlock1 = 0, *IfBlock2 = 0;
1105 PN = cast<PHINode>(BB->begin());
1106 BasicBlock *Pred = PN->getIncomingBlock(0);
1107 if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) {
1108 IfBlock1 = Pred;
1109 DomBlock = *pred_begin(Pred);
1110 for (BasicBlock::iterator I = Pred->begin();
1111 !isa<TerminatorInst>(I); ++I)
1112 if (!AggressiveInsts.count(I)) {
1113 // This is not an aggressive instruction that we can promote.
1114 // Because of this, we won't be able to get rid of the control
1115 // flow, so the xform is not worth it.
1116 return false;
1117 }
1118 }
1119
1120 Pred = PN->getIncomingBlock(1);
1121 if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) {
1122 IfBlock2 = Pred;
1123 DomBlock = *pred_begin(Pred);
1124 for (BasicBlock::iterator I = Pred->begin();
1125 !isa<TerminatorInst>(I); ++I)
1126 if (!AggressiveInsts.count(I)) {
1127 // This is not an aggressive instruction that we can promote.
1128 // Because of this, we won't be able to get rid of the control
1129 // flow, so the xform is not worth it.
1130 return false;
1131 }
1132 }
1133
1134 // If we can still promote the PHI nodes after this gauntlet of tests,
1135 // do all of the PHI's now.
1136
1137 // Move all 'aggressive' instructions, which are defined in the
1138 // conditional parts of the if's up to the dominating block.
1139 if (IfBlock1) {
1140 DomBlock->getInstList().splice(DomBlock->getTerminator(),
1141 IfBlock1->getInstList(),
1142 IfBlock1->begin(),
1143 IfBlock1->getTerminator());
1144 }
1145 if (IfBlock2) {
1146 DomBlock->getInstList().splice(DomBlock->getTerminator(),
1147 IfBlock2->getInstList(),
1148 IfBlock2->begin(),
1149 IfBlock2->getTerminator());
1150 }
1151
1152 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
1153 // Change the PHI node into a select instruction.
1154 Value *TrueVal =
1155 PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
1156 Value *FalseVal =
1157 PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
1158
1159 std::string Name = PN->getName(); PN->setName("");
1160 PN->replaceAllUsesWith(new SelectInst(IfCond, TrueVal, FalseVal,
1161 Name, AfterPHIIt));
1162 BB->getInstList().erase(PN);
1163 }
1164 return true;
1165}
Chris Lattnereaba3a12005-09-19 23:49:37 +00001166
Chris Lattner1654cff2004-06-19 07:02:14 +00001167namespace {
1168 /// ConstantIntOrdering - This class implements a stable ordering of constant
1169 /// integers that does not depend on their address. This is important for
1170 /// applications that sort ConstantInt's to ensure uniqueness.
1171 struct ConstantIntOrdering {
1172 bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
Reid Spencerb83eb642006-10-20 07:07:24 +00001173 return LHS->getZExtValue() < RHS->getZExtValue();
Chris Lattner1654cff2004-06-19 07:02:14 +00001174 }
1175 };
1176}
1177
Chris Lattner01d1ee32002-05-21 20:50:24 +00001178// SimplifyCFG - This function is used to do simplification of a CFG. For
1179// example, it adjusts branches to branches to eliminate the extra hop, it
1180// eliminates unreachable basic blocks, and does other "peephole" optimization
Chris Lattnere2ca5402003-03-05 21:01:52 +00001181// of the CFG. It returns true if a modification was made.
Chris Lattner01d1ee32002-05-21 20:50:24 +00001182//
1183// WARNING: The entry node of a function may not be simplified.
1184//
Chris Lattnerf7703df2004-01-09 06:12:26 +00001185bool llvm::SimplifyCFG(BasicBlock *BB) {
Chris Lattnerdc3602b2003-08-24 18:36:16 +00001186 bool Changed = false;
Chris Lattner01d1ee32002-05-21 20:50:24 +00001187 Function *M = BB->getParent();
1188
1189 assert(BB && BB->getParent() && "Block not embedded in function!");
1190 assert(BB->getTerminator() && "Degenerate basic block encountered!");
Chris Lattner18961502002-06-25 16:12:52 +00001191 assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!");
Chris Lattner01d1ee32002-05-21 20:50:24 +00001192
Chris Lattner01d1ee32002-05-21 20:50:24 +00001193 // Remove basic blocks that have no predecessors... which are unreachable.
Chris Lattnerd52c2612004-02-24 07:23:58 +00001194 if (pred_begin(BB) == pred_end(BB) ||
1195 *pred_begin(BB) == BB && ++pred_begin(BB) == pred_end(BB)) {
Bill Wendling0d45a092006-11-26 10:17:54 +00001196 DOUT << "Removing BB: \n" << *BB;
Chris Lattner01d1ee32002-05-21 20:50:24 +00001197
1198 // Loop through all of our successors and make sure they know that one
1199 // of their predecessors is going away.
Chris Lattner151c80b2005-04-12 18:51:33 +00001200 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
1201 SI->removePredecessor(BB);
Chris Lattner01d1ee32002-05-21 20:50:24 +00001202
1203 while (!BB->empty()) {
Chris Lattner18961502002-06-25 16:12:52 +00001204 Instruction &I = BB->back();
Chris Lattner01d1ee32002-05-21 20:50:24 +00001205 // If this instruction is used, replace uses with an arbitrary
Chris Lattnerf5e982d2005-08-02 23:29:23 +00001206 // value. Because control flow can't get here, we don't care
Misha Brukmanfd939082005-04-21 23:48:37 +00001207 // what we replace the value with. Note that since this block is
Chris Lattner01d1ee32002-05-21 20:50:24 +00001208 // unreachable, and all values contained within it must dominate their
1209 // uses, that all uses will eventually be removed.
Misha Brukmanfd939082005-04-21 23:48:37 +00001210 if (!I.use_empty())
Chris Lattnerf5e982d2005-08-02 23:29:23 +00001211 // Make all users of this instruction use undef instead
1212 I.replaceAllUsesWith(UndefValue::get(I.getType()));
Misha Brukmanfd939082005-04-21 23:48:37 +00001213
Chris Lattner01d1ee32002-05-21 20:50:24 +00001214 // Remove the instruction from the basic block
Chris Lattner18961502002-06-25 16:12:52 +00001215 BB->getInstList().pop_back();
Chris Lattner01d1ee32002-05-21 20:50:24 +00001216 }
Chris Lattner18961502002-06-25 16:12:52 +00001217 M->getBasicBlockList().erase(BB);
Chris Lattner01d1ee32002-05-21 20:50:24 +00001218 return true;
1219 }
1220
Chris Lattner694e37f2003-08-17 19:41:53 +00001221 // Check to see if we can constant propagate this terminator instruction
1222 // away...
Chris Lattnerdc3602b2003-08-24 18:36:16 +00001223 Changed |= ConstantFoldTerminator(BB);
Chris Lattner694e37f2003-08-17 19:41:53 +00001224
Chris Lattner19831ec2004-02-16 06:35:48 +00001225 // If this is a returning block with only PHI nodes in it, fold the return
1226 // instruction into any unconditional branch predecessors.
Chris Lattner147af6b2004-04-02 18:13:43 +00001227 //
1228 // If any predecessor is a conditional branch that just selects among
1229 // different return values, fold the replace the branch/return with a select
1230 // and return.
Chris Lattner19831ec2004-02-16 06:35:48 +00001231 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
1232 BasicBlock::iterator BBI = BB->getTerminator();
1233 if (BBI == BB->begin() || isa<PHINode>(--BBI)) {
Chris Lattner147af6b2004-04-02 18:13:43 +00001234 // Find predecessors that end with branches.
Chris Lattner19831ec2004-02-16 06:35:48 +00001235 std::vector<BasicBlock*> UncondBranchPreds;
Chris Lattner147af6b2004-04-02 18:13:43 +00001236 std::vector<BranchInst*> CondBranchPreds;
Chris Lattner19831ec2004-02-16 06:35:48 +00001237 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1238 TerminatorInst *PTI = (*PI)->getTerminator();
1239 if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
1240 if (BI->isUnconditional())
1241 UncondBranchPreds.push_back(*PI);
Chris Lattner147af6b2004-04-02 18:13:43 +00001242 else
1243 CondBranchPreds.push_back(BI);
Chris Lattner19831ec2004-02-16 06:35:48 +00001244 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001245
Chris Lattner19831ec2004-02-16 06:35:48 +00001246 // If we found some, do the transformation!
1247 if (!UncondBranchPreds.empty()) {
1248 while (!UncondBranchPreds.empty()) {
1249 BasicBlock *Pred = UncondBranchPreds.back();
Bill Wendling0d45a092006-11-26 10:17:54 +00001250 DOUT << "FOLDING: " << *BB
1251 << "INTO UNCOND BRANCH PRED: " << *Pred;
Chris Lattner19831ec2004-02-16 06:35:48 +00001252 UncondBranchPreds.pop_back();
1253 Instruction *UncondBranch = Pred->getTerminator();
1254 // Clone the return and add it to the end of the predecessor.
1255 Instruction *NewRet = RI->clone();
1256 Pred->getInstList().push_back(NewRet);
1257
1258 // If the return instruction returns a value, and if the value was a
1259 // PHI node in "BB", propagate the right value into the return.
1260 if (NewRet->getNumOperands() == 1)
1261 if (PHINode *PN = dyn_cast<PHINode>(NewRet->getOperand(0)))
1262 if (PN->getParent() == BB)
1263 NewRet->setOperand(0, PN->getIncomingValueForBlock(Pred));
1264 // Update any PHI nodes in the returning block to realize that we no
1265 // longer branch to them.
1266 BB->removePredecessor(Pred);
1267 Pred->getInstList().erase(UncondBranch);
1268 }
1269
1270 // If we eliminated all predecessors of the block, delete the block now.
1271 if (pred_begin(BB) == pred_end(BB))
1272 // We know there are no successors, so just nuke the block.
1273 M->getBasicBlockList().erase(BB);
1274
Chris Lattner19831ec2004-02-16 06:35:48 +00001275 return true;
1276 }
Chris Lattner147af6b2004-04-02 18:13:43 +00001277
1278 // Check out all of the conditional branches going to this return
1279 // instruction. If any of them just select between returns, change the
1280 // branch itself into a select/return pair.
1281 while (!CondBranchPreds.empty()) {
1282 BranchInst *BI = CondBranchPreds.back();
1283 CondBranchPreds.pop_back();
1284 BasicBlock *TrueSucc = BI->getSuccessor(0);
1285 BasicBlock *FalseSucc = BI->getSuccessor(1);
1286 BasicBlock *OtherSucc = TrueSucc == BB ? FalseSucc : TrueSucc;
1287
1288 // Check to see if the non-BB successor is also a return block.
1289 if (isa<ReturnInst>(OtherSucc->getTerminator())) {
1290 // Check to see if there are only PHI instructions in this block.
1291 BasicBlock::iterator OSI = OtherSucc->getTerminator();
1292 if (OSI == OtherSucc->begin() || isa<PHINode>(--OSI)) {
1293 // Okay, we found a branch that is going to two return nodes. If
1294 // there is no return value for this function, just change the
1295 // branch into a return.
1296 if (RI->getNumOperands() == 0) {
1297 TrueSucc->removePredecessor(BI->getParent());
1298 FalseSucc->removePredecessor(BI->getParent());
1299 new ReturnInst(0, BI);
1300 BI->getParent()->getInstList().erase(BI);
1301 return true;
1302 }
1303
1304 // Otherwise, figure out what the true and false return values are
1305 // so we can insert a new select instruction.
1306 Value *TrueValue = TrueSucc->getTerminator()->getOperand(0);
1307 Value *FalseValue = FalseSucc->getTerminator()->getOperand(0);
1308
1309 // Unwrap any PHI nodes in the return blocks.
1310 if (PHINode *TVPN = dyn_cast<PHINode>(TrueValue))
1311 if (TVPN->getParent() == TrueSucc)
1312 TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
1313 if (PHINode *FVPN = dyn_cast<PHINode>(FalseValue))
1314 if (FVPN->getParent() == FalseSucc)
1315 FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
1316
Chris Lattnerb74b1812006-10-20 00:42:07 +00001317 // In order for this transformation to be safe, we must be able to
1318 // unconditionally execute both operands to the return. This is
1319 // normally the case, but we could have a potentially-trapping
1320 // constant expression that prevents this transformation from being
1321 // safe.
1322 if ((!isa<ConstantExpr>(TrueValue) ||
1323 !cast<ConstantExpr>(TrueValue)->canTrap()) &&
1324 (!isa<ConstantExpr>(TrueValue) ||
1325 !cast<ConstantExpr>(TrueValue)->canTrap())) {
1326 TrueSucc->removePredecessor(BI->getParent());
1327 FalseSucc->removePredecessor(BI->getParent());
Chris Lattner7aa773b2004-04-02 18:15:10 +00001328
Chris Lattnerb74b1812006-10-20 00:42:07 +00001329 // Insert a new select instruction.
1330 Value *NewRetVal;
1331 Value *BrCond = BI->getCondition();
1332 if (TrueValue != FalseValue)
1333 NewRetVal = new SelectInst(BrCond, TrueValue,
1334 FalseValue, "retval", BI);
1335 else
1336 NewRetVal = TrueValue;
1337
Bill Wendling0d45a092006-11-26 10:17:54 +00001338 DOUT << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
1339 << "\n " << *BI << "Select = " << *NewRetVal
1340 << "TRUEBLOCK: " << *TrueSucc << "FALSEBLOCK: "<< *FalseSucc;
Chris Lattner0ed7f422004-09-29 05:43:32 +00001341
Chris Lattnerb74b1812006-10-20 00:42:07 +00001342 new ReturnInst(NewRetVal, BI);
1343 BI->eraseFromParent();
1344 if (Instruction *BrCondI = dyn_cast<Instruction>(BrCond))
1345 if (isInstructionTriviallyDead(BrCondI))
1346 BrCondI->eraseFromParent();
1347 return true;
1348 }
Chris Lattner147af6b2004-04-02 18:13:43 +00001349 }
1350 }
1351 }
Chris Lattner19831ec2004-02-16 06:35:48 +00001352 }
Reid Spencer3ed469c2006-11-02 20:25:50 +00001353 } else if (isa<UnwindInst>(BB->begin())) {
Chris Lattnere14ea082004-02-24 05:54:22 +00001354 // Check to see if the first instruction in this block is just an unwind.
1355 // If so, replace any invoke instructions which use this as an exception
Chris Lattneraf17b1d2004-07-20 01:17:38 +00001356 // destination with call instructions, and any unconditional branch
1357 // predecessor with an unwind.
Chris Lattnere14ea082004-02-24 05:54:22 +00001358 //
1359 std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
1360 while (!Preds.empty()) {
1361 BasicBlock *Pred = Preds.back();
Chris Lattneraf17b1d2004-07-20 01:17:38 +00001362 if (BranchInst *BI = dyn_cast<BranchInst>(Pred->getTerminator())) {
1363 if (BI->isUnconditional()) {
1364 Pred->getInstList().pop_back(); // nuke uncond branch
1365 new UnwindInst(Pred); // Use unwind.
1366 Changed = true;
1367 }
1368 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
Chris Lattnere14ea082004-02-24 05:54:22 +00001369 if (II->getUnwindDest() == BB) {
1370 // Insert a new branch instruction before the invoke, because this
1371 // is now a fall through...
1372 BranchInst *BI = new BranchInst(II->getNormalDest(), II);
1373 Pred->getInstList().remove(II); // Take out of symbol table
Misha Brukmanfd939082005-04-21 23:48:37 +00001374
Chris Lattnere14ea082004-02-24 05:54:22 +00001375 // Insert the call now...
1376 std::vector<Value*> Args(II->op_begin()+3, II->op_end());
1377 CallInst *CI = new CallInst(II->getCalledValue(), Args,
1378 II->getName(), BI);
Chris Lattner16d0db22005-05-14 12:21:56 +00001379 CI->setCallingConv(II->getCallingConv());
Chris Lattnere14ea082004-02-24 05:54:22 +00001380 // If the invoke produced a value, the Call now does instead
1381 II->replaceAllUsesWith(CI);
1382 delete II;
1383 Changed = true;
1384 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001385
Chris Lattnere14ea082004-02-24 05:54:22 +00001386 Preds.pop_back();
1387 }
Chris Lattner8e509dd2004-02-24 16:09:21 +00001388
1389 // If this block is now dead, remove it.
1390 if (pred_begin(BB) == pred_end(BB)) {
1391 // We know there are no successors, so just nuke the block.
1392 M->getBasicBlockList().erase(BB);
1393 return true;
1394 }
1395
Chris Lattner623369a2005-02-24 06:17:52 +00001396 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
1397 if (isValueEqualityComparison(SI)) {
1398 // If we only have one predecessor, and if it is a branch on this value,
1399 // see if that predecessor totally determines the outcome of this switch.
1400 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
1401 if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred))
1402 return SimplifyCFG(BB) || 1;
1403
1404 // If the block only contains the switch, see if we can fold the block
1405 // away into any preds.
1406 if (SI == &BB->front())
1407 if (FoldValueComparisonIntoPredecessors(SI))
1408 return SimplifyCFG(BB) || 1;
1409 }
Chris Lattner542f1492004-02-28 21:28:10 +00001410 } else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
Chris Lattner7e663482005-08-03 00:11:16 +00001411 if (BI->isUnconditional()) {
1412 BasicBlock::iterator BBI = BB->begin(); // Skip over phi nodes...
1413 while (isa<PHINode>(*BBI)) ++BBI;
1414
1415 BasicBlock *Succ = BI->getSuccessor(0);
1416 if (BBI->isTerminator() && // Terminator is the only non-phi instruction!
1417 Succ != BB) // Don't hurt infinite loops!
1418 if (TryToSimplifyUncondBranchFromEmptyBlock(BB, Succ))
1419 return 1;
1420
1421 } else { // Conditional branch
Reid Spencer3ed469c2006-11-02 20:25:50 +00001422 if (isValueEqualityComparison(BI)) {
Chris Lattner623369a2005-02-24 06:17:52 +00001423 // If we only have one predecessor, and if it is a branch on this value,
1424 // see if that predecessor totally determines the outcome of this
1425 // switch.
1426 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
1427 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred))
1428 return SimplifyCFG(BB) || 1;
1429
Chris Lattnere67fa052004-05-01 23:35:43 +00001430 // This block must be empty, except for the setcond inst, if it exists.
1431 BasicBlock::iterator I = BB->begin();
1432 if (&*I == BI ||
1433 (&*I == cast<Instruction>(BI->getCondition()) &&
1434 &*++I == BI))
1435 if (FoldValueComparisonIntoPredecessors(BI))
1436 return SimplifyCFG(BB) | true;
1437 }
Chris Lattnereaba3a12005-09-19 23:49:37 +00001438
1439 // If this is a branch on a phi node in the current block, thread control
1440 // through this block if any PHI node entries are constants.
1441 if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
1442 if (PN->getParent() == BI->getParent())
1443 if (FoldCondBranchOnPHI(BI))
1444 return SimplifyCFG(BB) | true;
Chris Lattnere67fa052004-05-01 23:35:43 +00001445
1446 // If this basic block is ONLY a setcc and a branch, and if a predecessor
1447 // branches to us and one of our successors, fold the setcc into the
1448 // predecessor and use logical operations to pick the right destination.
Chris Lattner12fe2b12004-05-02 05:02:03 +00001449 BasicBlock *TrueDest = BI->getSuccessor(0);
1450 BasicBlock *FalseDest = BI->getSuccessor(1);
Chris Lattnerbdcc0b82004-05-02 05:19:36 +00001451 if (BinaryOperator *Cond = dyn_cast<BinaryOperator>(BI->getCondition()))
Chris Lattnere67fa052004-05-01 23:35:43 +00001452 if (Cond->getParent() == BB && &BB->front() == Cond &&
Chris Lattner12fe2b12004-05-02 05:02:03 +00001453 Cond->getNext() == BI && Cond->hasOneUse() &&
1454 TrueDest != BB && FalseDest != BB)
Chris Lattnere67fa052004-05-01 23:35:43 +00001455 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI!=E; ++PI)
1456 if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
Chris Lattnera1f79fb2004-05-02 01:00:44 +00001457 if (PBI->isConditional() && SafeToMergeTerminators(BI, PBI)) {
Chris Lattner2636c1b2004-06-21 07:19:01 +00001458 BasicBlock *PredBlock = *PI;
Chris Lattnere67fa052004-05-01 23:35:43 +00001459 if (PBI->getSuccessor(0) == FalseDest ||
1460 PBI->getSuccessor(1) == TrueDest) {
1461 // Invert the predecessors condition test (xor it with true),
1462 // which allows us to write this code once.
1463 Value *NewCond =
1464 BinaryOperator::createNot(PBI->getCondition(),
1465 PBI->getCondition()->getName()+".not", PBI);
1466 PBI->setCondition(NewCond);
1467 BasicBlock *OldTrue = PBI->getSuccessor(0);
1468 BasicBlock *OldFalse = PBI->getSuccessor(1);
1469 PBI->setSuccessor(0, OldFalse);
1470 PBI->setSuccessor(1, OldTrue);
1471 }
1472
Chris Lattner299520d2006-02-18 00:33:17 +00001473 if ((PBI->getSuccessor(0) == TrueDest && FalseDest != BB) ||
1474 (PBI->getSuccessor(1) == FalseDest && TrueDest != BB)) {
Chris Lattner2636c1b2004-06-21 07:19:01 +00001475 // Clone Cond into the predecessor basic block, and or/and the
Chris Lattnere67fa052004-05-01 23:35:43 +00001476 // two conditions together.
1477 Instruction *New = Cond->clone();
1478 New->setName(Cond->getName());
1479 Cond->setName(Cond->getName()+".old");
Chris Lattner2636c1b2004-06-21 07:19:01 +00001480 PredBlock->getInstList().insert(PBI, New);
Chris Lattnere67fa052004-05-01 23:35:43 +00001481 Instruction::BinaryOps Opcode =
1482 PBI->getSuccessor(0) == TrueDest ?
1483 Instruction::Or : Instruction::And;
Misha Brukmanfd939082005-04-21 23:48:37 +00001484 Value *NewCond =
Chris Lattnere67fa052004-05-01 23:35:43 +00001485 BinaryOperator::create(Opcode, PBI->getCondition(),
1486 New, "bothcond", PBI);
1487 PBI->setCondition(NewCond);
1488 if (PBI->getSuccessor(0) == BB) {
Chris Lattner2636c1b2004-06-21 07:19:01 +00001489 AddPredecessorToBlock(TrueDest, PredBlock, BB);
Chris Lattnere67fa052004-05-01 23:35:43 +00001490 PBI->setSuccessor(0, TrueDest);
1491 }
1492 if (PBI->getSuccessor(1) == BB) {
Chris Lattner2636c1b2004-06-21 07:19:01 +00001493 AddPredecessorToBlock(FalseDest, PredBlock, BB);
Chris Lattnere67fa052004-05-01 23:35:43 +00001494 PBI->setSuccessor(1, FalseDest);
1495 }
1496 return SimplifyCFG(BB) | 1;
1497 }
1498 }
Chris Lattnere67fa052004-05-01 23:35:43 +00001499
Chris Lattner263d1e42005-09-23 18:47:20 +00001500 // Scan predessor blocks for conditional branchs.
Chris Lattner2e42e362005-09-20 00:43:16 +00001501 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1502 if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
Chris Lattner263d1e42005-09-23 18:47:20 +00001503 if (PBI != BI && PBI->isConditional()) {
1504
1505 // If this block ends with a branch instruction, and if there is a
1506 // predecessor that ends on a branch of the same condition, make this
1507 // conditional branch redundant.
1508 if (PBI->getCondition() == BI->getCondition() &&
1509 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
1510 // Okay, the outcome of this conditional branch is statically
1511 // knowable. If this block had a single pred, handle specially.
1512 if (BB->getSinglePredecessor()) {
1513 // Turn this into a branch on constant.
1514 bool CondIsTrue = PBI->getSuccessor(0) == BB;
1515 BI->setCondition(ConstantBool::get(CondIsTrue));
1516 return SimplifyCFG(BB); // Nuke the branch on constant.
1517 }
1518
1519 // Otherwise, if there are multiple predecessors, insert a PHI that
1520 // merges in the constant and simplify the block result.
1521 if (BlockIsSimpleEnoughToThreadThrough(BB)) {
1522 PHINode *NewPN = new PHINode(Type::BoolTy,
1523 BI->getCondition()->getName()+".pr",
1524 BB->begin());
1525 for (PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1526 if ((PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) &&
1527 PBI != BI && PBI->isConditional() &&
1528 PBI->getCondition() == BI->getCondition() &&
1529 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
1530 bool CondIsTrue = PBI->getSuccessor(0) == BB;
1531 NewPN->addIncoming(ConstantBool::get(CondIsTrue), *PI);
1532 } else {
1533 NewPN->addIncoming(BI->getCondition(), *PI);
1534 }
1535
1536 BI->setCondition(NewPN);
1537 // This will thread the branch.
1538 return SimplifyCFG(BB) | true;
1539 }
Chris Lattner2e42e362005-09-20 00:43:16 +00001540 }
1541
Chris Lattner263d1e42005-09-23 18:47:20 +00001542 // If this is a conditional branch in an empty block, and if any
1543 // predecessors is a conditional branch to one of our destinations,
1544 // fold the conditions into logical ops and one cond br.
1545 if (&BB->front() == BI) {
1546 int PBIOp, BIOp;
1547 if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
1548 PBIOp = BIOp = 0;
1549 } else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
1550 PBIOp = 0; BIOp = 1;
1551 } else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
1552 PBIOp = 1; BIOp = 0;
1553 } else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
1554 PBIOp = BIOp = 1;
1555 } else {
1556 PBIOp = BIOp = -1;
1557 }
Chris Lattner2e42e362005-09-20 00:43:16 +00001558
Chris Lattner299520d2006-02-18 00:33:17 +00001559 // Check to make sure that the other destination of this branch
1560 // isn't BB itself. If so, this is an infinite loop that will
1561 // keep getting unwound.
1562 if (PBIOp != -1 && PBI->getSuccessor(PBIOp) == BB)
1563 PBIOp = BIOp = -1;
Chris Lattner822a8792006-11-18 19:19:36 +00001564
1565 // Do not perform this transformation if it would require
1566 // insertion of a large number of select instructions. For targets
1567 // without predication/cmovs, this is a big pessimization.
1568 if (PBIOp != -1) {
1569 BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
1570
1571 unsigned NumPhis = 0;
1572 for (BasicBlock::iterator II = CommonDest->begin();
1573 isa<PHINode>(II); ++II, ++NumPhis) {
1574 if (NumPhis > 2) {
1575 // Disable this xform.
1576 PBIOp = -1;
1577 break;
1578 }
1579 }
1580 }
Chris Lattner7f2e1dd2006-06-12 20:18:01 +00001581
Chris Lattner263d1e42005-09-23 18:47:20 +00001582 // Finally, if everything is ok, fold the branches to logical ops.
1583 if (PBIOp != -1) {
1584 BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
1585 BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
1586
Chris Lattner7f2e1dd2006-06-12 20:18:01 +00001587 // If OtherDest *is* BB, then this is a basic block with just
1588 // a conditional branch in it, where one edge (OtherDesg) goes
1589 // back to the block. We know that the program doesn't get
1590 // stuck in the infinite loop, so the condition must be such
1591 // that OtherDest isn't branched through. Forward to CommonDest,
1592 // and avoid an infinite loop at optimizer time.
1593 if (OtherDest == BB)
1594 OtherDest = CommonDest;
1595
Bill Wendling0d45a092006-11-26 10:17:54 +00001596 DOUT << "FOLDING BRs:" << *PBI->getParent()
1597 << "AND: " << *BI->getParent();
Chris Lattner263d1e42005-09-23 18:47:20 +00001598
1599 // BI may have other predecessors. Because of this, we leave
1600 // it alone, but modify PBI.
1601
1602 // Make sure we get to CommonDest on True&True directions.
1603 Value *PBICond = PBI->getCondition();
1604 if (PBIOp)
1605 PBICond = BinaryOperator::createNot(PBICond,
1606 PBICond->getName()+".not",
1607 PBI);
1608 Value *BICond = BI->getCondition();
1609 if (BIOp)
1610 BICond = BinaryOperator::createNot(BICond,
1611 BICond->getName()+".not",
1612 PBI);
1613 // Merge the conditions.
1614 Value *Cond =
1615 BinaryOperator::createOr(PBICond, BICond, "brmerge", PBI);
1616
1617 // Modify PBI to branch on the new condition to the new dests.
1618 PBI->setCondition(Cond);
1619 PBI->setSuccessor(0, CommonDest);
1620 PBI->setSuccessor(1, OtherDest);
1621
1622 // OtherDest may have phi nodes. If so, add an entry from PBI's
1623 // block that are identical to the entries for BI's block.
1624 PHINode *PN;
1625 for (BasicBlock::iterator II = OtherDest->begin();
1626 (PN = dyn_cast<PHINode>(II)); ++II) {
1627 Value *V = PN->getIncomingValueForBlock(BB);
1628 PN->addIncoming(V, PBI->getParent());
1629 }
1630
1631 // We know that the CommonDest already had an edge from PBI to
1632 // it. If it has PHIs though, the PHIs may have different
1633 // entries for BB and PBI's BB. If so, insert a select to make
1634 // them agree.
1635 for (BasicBlock::iterator II = CommonDest->begin();
1636 (PN = dyn_cast<PHINode>(II)); ++II) {
1637 Value * BIV = PN->getIncomingValueForBlock(BB);
1638 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
1639 Value *PBIV = PN->getIncomingValue(PBBIdx);
1640 if (BIV != PBIV) {
1641 // Insert a select in PBI to pick the right value.
1642 Value *NV = new SelectInst(PBICond, PBIV, BIV,
1643 PBIV->getName()+".mux", PBI);
1644 PN->setIncomingValue(PBBIdx, NV);
1645 }
1646 }
1647
Bill Wendling0d45a092006-11-26 10:17:54 +00001648 DOUT << "INTO: " << *PBI->getParent();
Chris Lattner263d1e42005-09-23 18:47:20 +00001649
1650 // This basic block is probably dead. We know it has at least
1651 // one fewer predecessor.
1652 return SimplifyCFG(BB) | true;
1653 }
Chris Lattner2e42e362005-09-20 00:43:16 +00001654 }
Chris Lattner92da2c22004-05-01 22:36:37 +00001655 }
Chris Lattnerd52c2612004-02-24 07:23:58 +00001656 }
Chris Lattner698f96f2004-10-18 04:07:22 +00001657 } else if (isa<UnreachableInst>(BB->getTerminator())) {
1658 // If there are any instructions immediately before the unreachable that can
1659 // be removed, do so.
1660 Instruction *Unreachable = BB->getTerminator();
1661 while (Unreachable != BB->begin()) {
1662 BasicBlock::iterator BBI = Unreachable;
1663 --BBI;
1664 if (isa<CallInst>(BBI)) break;
1665 // Delete this instruction
1666 BB->getInstList().erase(BBI);
1667 Changed = true;
1668 }
1669
1670 // If the unreachable instruction is the first in the block, take a gander
1671 // at all of the predecessors of this instruction, and simplify them.
1672 if (&BB->front() == Unreachable) {
1673 std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
1674 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1675 TerminatorInst *TI = Preds[i]->getTerminator();
1676
1677 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1678 if (BI->isUnconditional()) {
1679 if (BI->getSuccessor(0) == BB) {
1680 new UnreachableInst(TI);
1681 TI->eraseFromParent();
1682 Changed = true;
1683 }
1684 } else {
1685 if (BI->getSuccessor(0) == BB) {
1686 new BranchInst(BI->getSuccessor(1), BI);
1687 BI->eraseFromParent();
1688 } else if (BI->getSuccessor(1) == BB) {
1689 new BranchInst(BI->getSuccessor(0), BI);
1690 BI->eraseFromParent();
1691 Changed = true;
1692 }
1693 }
1694 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1695 for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1696 if (SI->getSuccessor(i) == BB) {
Chris Lattner42eb7522005-05-20 22:19:54 +00001697 BB->removePredecessor(SI->getParent());
Chris Lattner698f96f2004-10-18 04:07:22 +00001698 SI->removeCase(i);
1699 --i; --e;
1700 Changed = true;
1701 }
1702 // If the default value is unreachable, figure out the most popular
1703 // destination and make it the default.
1704 if (SI->getSuccessor(0) == BB) {
1705 std::map<BasicBlock*, unsigned> Popularity;
1706 for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1707 Popularity[SI->getSuccessor(i)]++;
1708
1709 // Find the most popular block.
1710 unsigned MaxPop = 0;
1711 BasicBlock *MaxBlock = 0;
1712 for (std::map<BasicBlock*, unsigned>::iterator
1713 I = Popularity.begin(), E = Popularity.end(); I != E; ++I) {
1714 if (I->second > MaxPop) {
1715 MaxPop = I->second;
1716 MaxBlock = I->first;
1717 }
1718 }
1719 if (MaxBlock) {
1720 // Make this the new default, allowing us to delete any explicit
1721 // edges to it.
1722 SI->setSuccessor(0, MaxBlock);
1723 Changed = true;
1724
Chris Lattner42eb7522005-05-20 22:19:54 +00001725 // If MaxBlock has phinodes in it, remove MaxPop-1 entries from
1726 // it.
1727 if (isa<PHINode>(MaxBlock->begin()))
1728 for (unsigned i = 0; i != MaxPop-1; ++i)
1729 MaxBlock->removePredecessor(SI->getParent());
1730
Chris Lattner698f96f2004-10-18 04:07:22 +00001731 for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1732 if (SI->getSuccessor(i) == MaxBlock) {
1733 SI->removeCase(i);
1734 --i; --e;
1735 }
1736 }
1737 }
1738 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
1739 if (II->getUnwindDest() == BB) {
1740 // Convert the invoke to a call instruction. This would be a good
1741 // place to note that the call does not throw though.
1742 BranchInst *BI = new BranchInst(II->getNormalDest(), II);
1743 II->removeFromParent(); // Take out of symbol table
Misha Brukmanfd939082005-04-21 23:48:37 +00001744
Chris Lattner698f96f2004-10-18 04:07:22 +00001745 // Insert the call now...
1746 std::vector<Value*> Args(II->op_begin()+3, II->op_end());
1747 CallInst *CI = new CallInst(II->getCalledValue(), Args,
1748 II->getName(), BI);
Chris Lattner16d0db22005-05-14 12:21:56 +00001749 CI->setCallingConv(II->getCallingConv());
Chris Lattner698f96f2004-10-18 04:07:22 +00001750 // If the invoke produced a value, the Call does now instead.
1751 II->replaceAllUsesWith(CI);
1752 delete II;
1753 Changed = true;
1754 }
1755 }
1756 }
1757
1758 // If this block is now dead, remove it.
1759 if (pred_begin(BB) == pred_end(BB)) {
1760 // We know there are no successors, so just nuke the block.
1761 M->getBasicBlockList().erase(BB);
1762 return true;
1763 }
1764 }
Chris Lattner19831ec2004-02-16 06:35:48 +00001765 }
1766
Chris Lattner01d1ee32002-05-21 20:50:24 +00001767 // Merge basic blocks into their predecessor if there is only one distinct
1768 // pred, and if there is only one distinct successor of the predecessor, and
1769 // if there are no PHI nodes.
1770 //
Chris Lattner2355f942004-02-11 01:17:07 +00001771 pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
1772 BasicBlock *OnlyPred = *PI++;
1773 for (; PI != PE; ++PI) // Search all predecessors, see if they are all same
1774 if (*PI != OnlyPred) {
1775 OnlyPred = 0; // There are multiple different predecessors...
1776 break;
1777 }
Chris Lattner92da2c22004-05-01 22:36:37 +00001778
Chris Lattner2355f942004-02-11 01:17:07 +00001779 BasicBlock *OnlySucc = 0;
1780 if (OnlyPred && OnlyPred != BB && // Don't break self loops
1781 OnlyPred->getTerminator()->getOpcode() != Instruction::Invoke) {
1782 // Check to see if there is only one distinct successor...
1783 succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred));
1784 OnlySucc = BB;
1785 for (; SI != SE; ++SI)
1786 if (*SI != OnlySucc) {
1787 OnlySucc = 0; // There are multiple distinct successors!
Chris Lattner01d1ee32002-05-21 20:50:24 +00001788 break;
1789 }
Chris Lattner2355f942004-02-11 01:17:07 +00001790 }
1791
1792 if (OnlySucc) {
Bill Wendling0d45a092006-11-26 10:17:54 +00001793 DOUT << "Merging: " << *BB << "into: " << *OnlyPred;
Chris Lattner2355f942004-02-11 01:17:07 +00001794
1795 // Resolve any PHI nodes at the start of the block. They are all
1796 // guaranteed to have exactly one entry if they exist, unless there are
1797 // multiple duplicate (but guaranteed to be equal) entries for the
1798 // incoming edges. This occurs when there are multiple edges from
1799 // OnlyPred to OnlySucc.
1800 //
1801 while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
1802 PN->replaceAllUsesWith(PN->getIncomingValue(0));
1803 BB->getInstList().pop_front(); // Delete the phi node...
Chris Lattner01d1ee32002-05-21 20:50:24 +00001804 }
1805
Chris Lattner2355f942004-02-11 01:17:07 +00001806 // Delete the unconditional branch from the predecessor...
1807 OnlyPred->getInstList().pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +00001808
Chris Lattner2355f942004-02-11 01:17:07 +00001809 // Move all definitions in the successor to the predecessor...
1810 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
Misha Brukmanfd939082005-04-21 23:48:37 +00001811
Chris Lattner2355f942004-02-11 01:17:07 +00001812 // Make all PHI nodes that referred to BB now refer to Pred as their
1813 // source...
1814 BB->replaceAllUsesWith(OnlyPred);
Chris Lattner18961502002-06-25 16:12:52 +00001815
Chris Lattner2355f942004-02-11 01:17:07 +00001816 std::string OldName = BB->getName();
Chris Lattner18961502002-06-25 16:12:52 +00001817
Misha Brukmanfd939082005-04-21 23:48:37 +00001818 // Erase basic block from the function...
Chris Lattner2355f942004-02-11 01:17:07 +00001819 M->getBasicBlockList().erase(BB);
Chris Lattner18961502002-06-25 16:12:52 +00001820
Chris Lattner2355f942004-02-11 01:17:07 +00001821 // Inherit predecessors name if it exists...
1822 if (!OldName.empty() && !OnlyPred->hasName())
1823 OnlyPred->setName(OldName);
Misha Brukmanfd939082005-04-21 23:48:37 +00001824
Chris Lattner2355f942004-02-11 01:17:07 +00001825 return true;
Chris Lattner01d1ee32002-05-21 20:50:24 +00001826 }
Chris Lattner723c66d2004-02-11 03:36:04 +00001827
Chris Lattner37dc9382004-11-30 00:29:14 +00001828 // Otherwise, if this block only has a single predecessor, and if that block
1829 // is a conditional branch, see if we can hoist any code from this block up
1830 // into our predecessor.
1831 if (OnlyPred)
Chris Lattner76134372004-12-10 17:42:31 +00001832 if (BranchInst *BI = dyn_cast<BranchInst>(OnlyPred->getTerminator()))
1833 if (BI->isConditional()) {
1834 // Get the other block.
1835 BasicBlock *OtherBB = BI->getSuccessor(BI->getSuccessor(0) == BB);
1836 PI = pred_begin(OtherBB);
1837 ++PI;
1838 if (PI == pred_end(OtherBB)) {
1839 // We have a conditional branch to two blocks that are only reachable
1840 // from the condbr. We know that the condbr dominates the two blocks,
1841 // so see if there is any identical code in the "then" and "else"
1842 // blocks. If so, we can hoist it up to the branching block.
1843 Changed |= HoistThenElseCodeToIf(BI);
1844 }
Chris Lattner37dc9382004-11-30 00:29:14 +00001845 }
Chris Lattner37dc9382004-11-30 00:29:14 +00001846
Chris Lattner0d560082004-02-24 05:38:11 +00001847 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1848 if (BranchInst *BI = dyn_cast<BranchInst>((*PI)->getTerminator()))
1849 // Change br (X == 0 | X == 1), T, F into a switch instruction.
1850 if (BI->isConditional() && isa<Instruction>(BI->getCondition())) {
1851 Instruction *Cond = cast<Instruction>(BI->getCondition());
1852 // If this is a bunch of seteq's or'd together, or if it's a bunch of
1853 // 'setne's and'ed together, collect them.
1854 Value *CompVal = 0;
Chris Lattner1654cff2004-06-19 07:02:14 +00001855 std::vector<ConstantInt*> Values;
Chris Lattner0d560082004-02-24 05:38:11 +00001856 bool TrueWhenEqual = GatherValueComparisons(Cond, CompVal, Values);
1857 if (CompVal && CompVal->getType()->isInteger()) {
1858 // There might be duplicate constants in the list, which the switch
1859 // instruction can't handle, remove them now.
Chris Lattner1654cff2004-06-19 07:02:14 +00001860 std::sort(Values.begin(), Values.end(), ConstantIntOrdering());
Chris Lattner0d560082004-02-24 05:38:11 +00001861 Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
Misha Brukmanfd939082005-04-21 23:48:37 +00001862
Chris Lattner0d560082004-02-24 05:38:11 +00001863 // Figure out which block is which destination.
1864 BasicBlock *DefaultBB = BI->getSuccessor(1);
1865 BasicBlock *EdgeBB = BI->getSuccessor(0);
1866 if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB);
Misha Brukmanfd939082005-04-21 23:48:37 +00001867
Chris Lattner0d560082004-02-24 05:38:11 +00001868 // Create the new switch instruction now.
Chris Lattner37880592005-01-29 00:38:26 +00001869 SwitchInst *New = new SwitchInst(CompVal, DefaultBB,Values.size(),BI);
Misha Brukmanfd939082005-04-21 23:48:37 +00001870
Chris Lattner0d560082004-02-24 05:38:11 +00001871 // Add all of the 'cases' to the switch instruction.
1872 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1873 New->addCase(Values[i], EdgeBB);
Misha Brukmanfd939082005-04-21 23:48:37 +00001874
Chris Lattner0d560082004-02-24 05:38:11 +00001875 // We added edges from PI to the EdgeBB. As such, if there were any
1876 // PHI nodes in EdgeBB, they need entries to be added corresponding to
1877 // the number of edges added.
1878 for (BasicBlock::iterator BBI = EdgeBB->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +00001879 isa<PHINode>(BBI); ++BBI) {
1880 PHINode *PN = cast<PHINode>(BBI);
Chris Lattner0d560082004-02-24 05:38:11 +00001881 Value *InVal = PN->getIncomingValueForBlock(*PI);
1882 for (unsigned i = 0, e = Values.size()-1; i != e; ++i)
1883 PN->addIncoming(InVal, *PI);
1884 }
1885
1886 // Erase the old branch instruction.
1887 (*PI)->getInstList().erase(BI);
1888
1889 // Erase the potentially condition tree that was used to computed the
1890 // branch condition.
1891 ErasePossiblyDeadInstructionTree(Cond);
1892 return true;
1893 }
1894 }
1895
Chris Lattner723c66d2004-02-11 03:36:04 +00001896 // If there is a trivial two-entry PHI node in this basic block, and we can
1897 // eliminate it, do so now.
1898 if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
Chris Lattnerf58c1a52005-09-23 06:39:30 +00001899 if (PN->getNumIncomingValues() == 2)
1900 Changed |= FoldTwoEntryPHINode(PN);
Misha Brukmanfd939082005-04-21 23:48:37 +00001901
Chris Lattner694e37f2003-08-17 19:41:53 +00001902 return Changed;
Chris Lattner01d1ee32002-05-21 20:50:24 +00001903}