c060127e2c09df8b6af035f9e8a92e188d2fe3b3
[oota-llvm.git] / lib / Transforms / Utils / SimplifyCFG.cpp
1 //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Peephole optimize the CFG.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "simplifycfg"
15 #include "llvm/Transforms/Utils/Local.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Type.h"
19 #include "llvm/Support/CFG.h"
20 #include "llvm/Support/Debug.h"
21 #include <algorithm>
22 #include <functional>
23 #include <set>
24 #include <map>
25 using namespace llvm;
26
27 // PropagatePredecessorsForPHIs - This gets "Succ" ready to have the
28 // predecessors from "BB".  This is a little tricky because "Succ" has PHI
29 // nodes, which need to have extra slots added to them to hold the merge edges
30 // from BB's predecessors, and BB itself might have had PHI nodes in it.  This
31 // function returns true (failure) if the Succ BB already has a predecessor that
32 // is a predecessor of BB and incoming PHI arguments would not be discernible.
33 //
34 // Assumption: Succ is the single successor for BB.
35 //
36 static bool PropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
37   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
38
39   if (!isa<PHINode>(Succ->front()))
40     return false;  // We can make the transformation, no problem.
41
42   // If there is more than one predecessor, and there are PHI nodes in
43   // the successor, then we need to add incoming edges for the PHI nodes
44   //
45   const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
46
47   // Check to see if one of the predecessors of BB is already a predecessor of
48   // Succ.  If so, we cannot do the transformation if there are any PHI nodes
49   // with incompatible values coming in from the two edges!
50   //
51   for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); PI != PE; ++PI)
52     if (std::find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) {
53       // Loop over all of the PHI nodes checking to see if there are
54       // incompatible values coming in.
55       for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
56         PHINode *PN = cast<PHINode>(I);
57         // Loop up the entries in the PHI node for BB and for *PI if the values
58         // coming in are non-equal, we cannot merge these two blocks (instead we
59         // should insert a conditional move or something, then merge the
60         // blocks).
61         int Idx1 = PN->getBasicBlockIndex(BB);
62         int Idx2 = PN->getBasicBlockIndex(*PI);
63         assert(Idx1 != -1 && Idx2 != -1 &&
64                "Didn't have entries for my predecessors??");
65         if (PN->getIncomingValue(Idx1) != PN->getIncomingValue(Idx2))
66           return true;  // Values are not equal...
67       }
68     }
69
70   // Loop over all of the PHI nodes in the successor BB.
71   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
72     PHINode *PN = cast<PHINode>(I);
73     Value *OldVal = PN->removeIncomingValue(BB, false);
74     assert(OldVal && "No entry in PHI for Pred BB!");
75
76     // If this incoming value is one of the PHI nodes in BB, the new entries in
77     // the PHI node are the entries from the old PHI.
78     if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
79       PHINode *OldValPN = cast<PHINode>(OldVal);
80       for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
81         PN->addIncoming(OldValPN->getIncomingValue(i),
82                         OldValPN->getIncomingBlock(i));
83     } else {
84       for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(),
85              End = BBPreds.end(); PredI != End; ++PredI) {
86         // Add an incoming value for each of the new incoming values...
87         PN->addIncoming(OldVal, *PredI);
88       }
89     }
90   }
91   return false;
92 }
93
94 /// GetIfCondition - Given a basic block (BB) with two predecessors (and
95 /// presumably PHI nodes in it), check to see if the merge at this block is due
96 /// to an "if condition".  If so, return the boolean condition that determines
97 /// which entry into BB will be taken.  Also, return by references the block
98 /// that will be entered from if the condition is true, and the block that will
99 /// be entered if the condition is false.
100 ///
101 ///
102 static Value *GetIfCondition(BasicBlock *BB,
103                              BasicBlock *&IfTrue, BasicBlock *&IfFalse) {
104   assert(std::distance(pred_begin(BB), pred_end(BB)) == 2 &&
105          "Function can only handle blocks with 2 predecessors!");
106   BasicBlock *Pred1 = *pred_begin(BB);
107   BasicBlock *Pred2 = *++pred_begin(BB);
108
109   // We can only handle branches.  Other control flow will be lowered to
110   // branches if possible anyway.
111   if (!isa<BranchInst>(Pred1->getTerminator()) ||
112       !isa<BranchInst>(Pred2->getTerminator()))
113     return 0;
114   BranchInst *Pred1Br = cast<BranchInst>(Pred1->getTerminator());
115   BranchInst *Pred2Br = cast<BranchInst>(Pred2->getTerminator());
116
117   // Eliminate code duplication by ensuring that Pred1Br is conditional if
118   // either are.
119   if (Pred2Br->isConditional()) {
120     // If both branches are conditional, we don't have an "if statement".  In
121     // reality, we could transform this case, but since the condition will be
122     // required anyway, we stand no chance of eliminating it, so the xform is
123     // probably not profitable.
124     if (Pred1Br->isConditional())
125       return 0;
126
127     std::swap(Pred1, Pred2);
128     std::swap(Pred1Br, Pred2Br);
129   }
130
131   if (Pred1Br->isConditional()) {
132     // If we found a conditional branch predecessor, make sure that it branches
133     // to BB and Pred2Br.  If it doesn't, this isn't an "if statement".
134     if (Pred1Br->getSuccessor(0) == BB &&
135         Pred1Br->getSuccessor(1) == Pred2) {
136       IfTrue = Pred1;
137       IfFalse = Pred2;
138     } else if (Pred1Br->getSuccessor(0) == Pred2 &&
139                Pred1Br->getSuccessor(1) == BB) {
140       IfTrue = Pred2;
141       IfFalse = Pred1;
142     } else {
143       // We know that one arm of the conditional goes to BB, so the other must
144       // go somewhere unrelated, and this must not be an "if statement".
145       return 0;
146     }
147
148     // The only thing we have to watch out for here is to make sure that Pred2
149     // doesn't have incoming edges from other blocks.  If it does, the condition
150     // doesn't dominate BB.
151     if (++pred_begin(Pred2) != pred_end(Pred2))
152       return 0;
153
154     return Pred1Br->getCondition();
155   }
156
157   // Ok, if we got here, both predecessors end with an unconditional branch to
158   // BB.  Don't panic!  If both blocks only have a single (identical)
159   // predecessor, and THAT is a conditional branch, then we're all ok!
160   if (pred_begin(Pred1) == pred_end(Pred1) ||
161       ++pred_begin(Pred1) != pred_end(Pred1) ||
162       pred_begin(Pred2) == pred_end(Pred2) ||
163       ++pred_begin(Pred2) != pred_end(Pred2) ||
164       *pred_begin(Pred1) != *pred_begin(Pred2))
165     return 0;
166
167   // Otherwise, if this is a conditional branch, then we can use it!
168   BasicBlock *CommonPred = *pred_begin(Pred1);
169   if (BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator())) {
170     assert(BI->isConditional() && "Two successors but not conditional?");
171     if (BI->getSuccessor(0) == Pred1) {
172       IfTrue = Pred1;
173       IfFalse = Pred2;
174     } else {
175       IfTrue = Pred2;
176       IfFalse = Pred1;
177     }
178     return BI->getCondition();
179   }
180   return 0;
181 }
182
183
184 // If we have a merge point of an "if condition" as accepted above, return true
185 // if the specified value dominates the block.  We don't handle the true
186 // generality of domination here, just a special case which works well enough
187 // for us.
188 //
189 // If AggressiveInsts is non-null, and if V does not dominate BB, we check to
190 // see if V (which must be an instruction) is cheap to compute and is
191 // non-trapping.  If both are true, the instruction is inserted into the set and
192 // true is returned.
193 static bool DominatesMergePoint(Value *V, BasicBlock *BB,
194                                 std::set<Instruction*> *AggressiveInsts) {
195   Instruction *I = dyn_cast<Instruction>(V);
196   if (!I) return true;    // Non-instructions all dominate instructions.
197   BasicBlock *PBB = I->getParent();
198
199   // We don't want to allow weird loops that might have the "if condition" in
200   // the bottom of this block.
201   if (PBB == BB) return false;
202
203   // If this instruction is defined in a block that contains an unconditional
204   // branch to BB, then it must be in the 'conditional' part of the "if
205   // statement".
206   if (BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator()))
207     if (BI->isUnconditional() && BI->getSuccessor(0) == BB) {
208       if (!AggressiveInsts) return false;
209       // Okay, it looks like the instruction IS in the "condition".  Check to
210       // see if its a cheap instruction to unconditionally compute, and if it
211       // only uses stuff defined outside of the condition.  If so, hoist it out.
212       switch (I->getOpcode()) {
213       default: return false;  // Cannot hoist this out safely.
214       case Instruction::Load:
215         // We can hoist loads that are non-volatile and obviously cannot trap.
216         if (cast<LoadInst>(I)->isVolatile())
217           return false;
218         if (!isa<AllocaInst>(I->getOperand(0)) &&
219             !isa<Constant>(I->getOperand(0)))
220           return false;
221
222         // Finally, we have to check to make sure there are no instructions
223         // before the load in its basic block, as we are going to hoist the loop
224         // out to its predecessor.
225         if (PBB->begin() != BasicBlock::iterator(I))
226           return false;
227         break;
228       case Instruction::Add:
229       case Instruction::Sub:
230       case Instruction::And:
231       case Instruction::Or:
232       case Instruction::Xor:
233       case Instruction::Shl:
234       case Instruction::Shr:
235       case Instruction::SetEQ:
236       case Instruction::SetNE:
237       case Instruction::SetLT:
238       case Instruction::SetGT:
239       case Instruction::SetLE:
240       case Instruction::SetGE:
241         break;   // These are all cheap and non-trapping instructions.
242       }
243
244       // Okay, we can only really hoist these out if their operands are not
245       // defined in the conditional region.
246       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
247         if (!DominatesMergePoint(I->getOperand(i), BB, 0))
248           return false;
249       // Okay, it's safe to do this!  Remember this instruction.
250       AggressiveInsts->insert(I);
251     }
252
253   return true;
254 }
255
256 // GatherConstantSetEQs - Given a potentially 'or'd together collection of seteq
257 // instructions that compare a value against a constant, return the value being
258 // compared, and stick the constant into the Values vector.
259 static Value *GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values){
260   if (Instruction *Inst = dyn_cast<Instruction>(V))
261     if (Inst->getOpcode() == Instruction::SetEQ) {
262       if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) {
263         Values.push_back(C);
264         return Inst->getOperand(0);
265       } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) {
266         Values.push_back(C);
267         return Inst->getOperand(1);
268       }
269     } else if (Inst->getOpcode() == Instruction::Or) {
270       if (Value *LHS = GatherConstantSetEQs(Inst->getOperand(0), Values))
271         if (Value *RHS = GatherConstantSetEQs(Inst->getOperand(1), Values))
272           if (LHS == RHS)
273             return LHS;
274     }
275   return 0;
276 }
277
278 // GatherConstantSetNEs - Given a potentially 'and'd together collection of
279 // setne instructions that compare a value against a constant, return the value
280 // being compared, and stick the constant into the Values vector.
281 static Value *GatherConstantSetNEs(Value *V, std::vector<ConstantInt*> &Values){
282   if (Instruction *Inst = dyn_cast<Instruction>(V))
283     if (Inst->getOpcode() == Instruction::SetNE) {
284       if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) {
285         Values.push_back(C);
286         return Inst->getOperand(0);
287       } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) {
288         Values.push_back(C);
289         return Inst->getOperand(1);
290       }
291     } else if (Inst->getOpcode() == Instruction::Cast) {
292       // Cast of X to bool is really a comparison against zero.
293       assert(Inst->getType() == Type::BoolTy && "Can only handle bool values!");
294       Values.push_back(ConstantInt::get(Inst->getOperand(0)->getType(), 0));
295       return Inst->getOperand(0);
296     } else if (Inst->getOpcode() == Instruction::And) {
297       if (Value *LHS = GatherConstantSetNEs(Inst->getOperand(0), Values))
298         if (Value *RHS = GatherConstantSetNEs(Inst->getOperand(1), Values))
299           if (LHS == RHS)
300             return LHS;
301     }
302   return 0;
303 }
304
305
306
307 /// GatherValueComparisons - If the specified Cond is an 'and' or 'or' of a
308 /// bunch of comparisons of one value against constants, return the value and
309 /// the constants being compared.
310 static bool GatherValueComparisons(Instruction *Cond, Value *&CompVal,
311                                    std::vector<ConstantInt*> &Values) {
312   if (Cond->getOpcode() == Instruction::Or) {
313     CompVal = GatherConstantSetEQs(Cond, Values);
314
315     // Return true to indicate that the condition is true if the CompVal is
316     // equal to one of the constants.
317     return true;
318   } else if (Cond->getOpcode() == Instruction::And) {
319     CompVal = GatherConstantSetNEs(Cond, Values);
320
321     // Return false to indicate that the condition is false if the CompVal is
322     // equal to one of the constants.
323     return false;
324   }
325   return false;
326 }
327
328 /// ErasePossiblyDeadInstructionTree - If the specified instruction is dead and
329 /// has no side effects, nuke it.  If it uses any instructions that become dead
330 /// because the instruction is now gone, nuke them too.
331 static void ErasePossiblyDeadInstructionTree(Instruction *I) {
332   if (isInstructionTriviallyDead(I)) {
333     std::vector<Value*> Operands(I->op_begin(), I->op_end());
334     I->getParent()->getInstList().erase(I);
335     for (unsigned i = 0, e = Operands.size(); i != e; ++i)
336       if (Instruction *OpI = dyn_cast<Instruction>(Operands[i]))
337         ErasePossiblyDeadInstructionTree(OpI);
338   }
339 }
340
341 /// SafeToMergeTerminators - Return true if it is safe to merge these two
342 /// terminator instructions together.
343 ///
344 static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
345   if (SI1 == SI2) return false;  // Can't merge with self!
346
347   // It is not safe to merge these two switch instructions if they have a common
348   // successor, and if that successor has a PHI node, and if *that* PHI node has
349   // conflicting incoming values from the two switch blocks.
350   BasicBlock *SI1BB = SI1->getParent();
351   BasicBlock *SI2BB = SI2->getParent();
352   std::set<BasicBlock*> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
353
354   for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
355     if (SI1Succs.count(*I))
356       for (BasicBlock::iterator BBI = (*I)->begin();
357            isa<PHINode>(BBI); ++BBI) {
358         PHINode *PN = cast<PHINode>(BBI);
359         if (PN->getIncomingValueForBlock(SI1BB) !=
360             PN->getIncomingValueForBlock(SI2BB))
361           return false;
362       }
363
364   return true;
365 }
366
367 /// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
368 /// now be entries in it from the 'NewPred' block.  The values that will be
369 /// flowing into the PHI nodes will be the same as those coming in from
370 /// ExistPred, an existing predecessor of Succ.
371 static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
372                                   BasicBlock *ExistPred) {
373   assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) !=
374          succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!");
375   if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
376
377   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
378     PHINode *PN = cast<PHINode>(I);
379     Value *V = PN->getIncomingValueForBlock(ExistPred);
380     PN->addIncoming(V, NewPred);
381   }
382 }
383
384 // isValueEqualityComparison - Return true if the specified terminator checks to
385 // see if a value is equal to constant integer value.
386 static Value *isValueEqualityComparison(TerminatorInst *TI) {
387   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
388     // Do not permit merging of large switch instructions into their
389     // predecessors unless there is only one predecessor.
390     if (SI->getNumSuccessors() * std::distance(pred_begin(SI->getParent()),
391                                                pred_end(SI->getParent())) > 128)
392       return 0;
393
394     return SI->getCondition();
395   }
396   if (BranchInst *BI = dyn_cast<BranchInst>(TI))
397     if (BI->isConditional() && BI->getCondition()->hasOneUse())
398       if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition()))
399         if ((SCI->getOpcode() == Instruction::SetEQ ||
400              SCI->getOpcode() == Instruction::SetNE) &&
401             isa<ConstantInt>(SCI->getOperand(1)))
402           return SCI->getOperand(0);
403   return 0;
404 }
405
406 // Given a value comparison instruction, decode all of the 'cases' that it
407 // represents and return the 'default' block.
408 static BasicBlock *
409 GetValueEqualityComparisonCases(TerminatorInst *TI,
410                                 std::vector<std::pair<ConstantInt*,
411                                                       BasicBlock*> > &Cases) {
412   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
413     Cases.reserve(SI->getNumCases());
414     for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
415       Cases.push_back(std::make_pair(SI->getCaseValue(i), SI->getSuccessor(i)));
416     return SI->getDefaultDest();
417   }
418
419   BranchInst *BI = cast<BranchInst>(TI);
420   SetCondInst *SCI = cast<SetCondInst>(BI->getCondition());
421   Cases.push_back(std::make_pair(cast<ConstantInt>(SCI->getOperand(1)),
422                                  BI->getSuccessor(SCI->getOpcode() ==
423                                                         Instruction::SetNE)));
424   return BI->getSuccessor(SCI->getOpcode() == Instruction::SetEQ);
425 }
426
427
428 // EliminateBlockCases - Given an vector of bb/value pairs, remove any entries
429 // in the list that match the specified block.
430 static void EliminateBlockCases(BasicBlock *BB,
431                std::vector<std::pair<ConstantInt*, BasicBlock*> > &Cases) {
432   for (unsigned i = 0, e = Cases.size(); i != e; ++i)
433     if (Cases[i].second == BB) {
434       Cases.erase(Cases.begin()+i);
435       --i; --e;
436     }
437 }
438
439 // ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as
440 // well.
441 static bool
442 ValuesOverlap(std::vector<std::pair<ConstantInt*, BasicBlock*> > &C1,
443               std::vector<std::pair<ConstantInt*, BasicBlock*> > &C2) {
444   std::vector<std::pair<ConstantInt*, BasicBlock*> > *V1 = &C1, *V2 = &C2;
445
446   // Make V1 be smaller than V2.
447   if (V1->size() > V2->size())
448     std::swap(V1, V2);
449
450   if (V1->size() == 0) return false;
451   if (V1->size() == 1) {
452     // Just scan V2.
453     ConstantInt *TheVal = (*V1)[0].first;
454     for (unsigned i = 0, e = V2->size(); i != e; ++i)
455       if (TheVal == (*V2)[i].first)
456         return true;
457   }
458
459   // Otherwise, just sort both lists and compare element by element.
460   std::sort(V1->begin(), V1->end());
461   std::sort(V2->begin(), V2->end());
462   unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
463   while (i1 != e1 && i2 != e2) {
464     if ((*V1)[i1].first == (*V2)[i2].first)
465       return true;
466     if ((*V1)[i1].first < (*V2)[i2].first)
467       ++i1;
468     else
469       ++i2;
470   }
471   return false;
472 }
473
474 // SimplifyEqualityComparisonWithOnlyPredecessor - If TI is known to be a
475 // terminator instruction and its block is known to only have a single
476 // predecessor block, check to see if that predecessor is also a value
477 // comparison with the same value, and if that comparison determines the outcome
478 // of this comparison.  If so, simplify TI.  This does a very limited form of
479 // jump threading.
480 static bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
481                                                           BasicBlock *Pred) {
482   Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
483   if (!PredVal) return false;  // Not a value comparison in predecessor.
484
485   Value *ThisVal = isValueEqualityComparison(TI);
486   assert(ThisVal && "This isn't a value comparison!!");
487   if (ThisVal != PredVal) return false;  // Different predicates.
488
489   // Find out information about when control will move from Pred to TI's block.
490   std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases;
491   BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(),
492                                                         PredCases);
493   EliminateBlockCases(PredDef, PredCases);  // Remove default from cases.
494
495   // Find information about how control leaves this block.
496   std::vector<std::pair<ConstantInt*, BasicBlock*> > ThisCases;
497   BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
498   EliminateBlockCases(ThisDef, ThisCases);  // Remove default from cases.
499
500   // If TI's block is the default block from Pred's comparison, potentially
501   // simplify TI based on this knowledge.
502   if (PredDef == TI->getParent()) {
503     // If we are here, we know that the value is none of those cases listed in
504     // PredCases.  If there are any cases in ThisCases that are in PredCases, we
505     // can simplify TI.
506     if (ValuesOverlap(PredCases, ThisCases)) {
507       if (BranchInst *BTI = dyn_cast<BranchInst>(TI)) {
508         // Okay, one of the successors of this condbr is dead.  Convert it to a
509         // uncond br.
510         assert(ThisCases.size() == 1 && "Branch can only have one case!");
511         Value *Cond = BTI->getCondition();
512         // Insert the new branch.
513         Instruction *NI = new BranchInst(ThisDef, TI);
514
515         // Remove PHI node entries for the dead edge.
516         ThisCases[0].second->removePredecessor(TI->getParent());
517
518         DEBUG(std::cerr << "Threading pred instr: " << *Pred->getTerminator()
519               << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
520
521         TI->eraseFromParent();   // Nuke the old one.
522         // If condition is now dead, nuke it.
523         if (Instruction *CondI = dyn_cast<Instruction>(Cond))
524           ErasePossiblyDeadInstructionTree(CondI);
525         return true;
526
527       } else {
528         SwitchInst *SI = cast<SwitchInst>(TI);
529         // Okay, TI has cases that are statically dead, prune them away.
530         std::set<Constant*> DeadCases;
531         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
532           DeadCases.insert(PredCases[i].first);
533
534         DEBUG(std::cerr << "Threading pred instr: " << *Pred->getTerminator()
535                   << "Through successor TI: " << *TI);
536
537         for (unsigned i = SI->getNumCases()-1; i != 0; --i)
538           if (DeadCases.count(SI->getCaseValue(i))) {
539             SI->getSuccessor(i)->removePredecessor(TI->getParent());
540             SI->removeCase(i);
541           }
542
543         DEBUG(std::cerr << "Leaving: " << *TI << "\n");
544         return true;
545       }
546     }
547
548   } else {
549     // Otherwise, TI's block must correspond to some matched value.  Find out
550     // which value (or set of values) this is.
551     ConstantInt *TIV = 0;
552     BasicBlock *TIBB = TI->getParent();
553     for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
554       if (PredCases[i].second == TIBB)
555         if (TIV == 0)
556           TIV = PredCases[i].first;
557         else
558           return false;  // Cannot handle multiple values coming to this block.
559     assert(TIV && "No edge from pred to succ?");
560
561     // Okay, we found the one constant that our value can be if we get into TI's
562     // BB.  Find out which successor will unconditionally be branched to.
563     BasicBlock *TheRealDest = 0;
564     for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
565       if (ThisCases[i].first == TIV) {
566         TheRealDest = ThisCases[i].second;
567         break;
568       }
569
570     // If not handled by any explicit cases, it is handled by the default case.
571     if (TheRealDest == 0) TheRealDest = ThisDef;
572
573     // Remove PHI node entries for dead edges.
574     BasicBlock *CheckEdge = TheRealDest;
575     for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
576       if (*SI != CheckEdge)
577         (*SI)->removePredecessor(TIBB);
578       else
579         CheckEdge = 0;
580
581     // Insert the new branch.
582     Instruction *NI = new BranchInst(TheRealDest, TI);
583
584     DEBUG(std::cerr << "Threading pred instr: " << *Pred->getTerminator()
585           << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
586     Instruction *Cond = 0;
587     if (BranchInst *BI = dyn_cast<BranchInst>(TI))
588       Cond = dyn_cast<Instruction>(BI->getCondition());
589     TI->eraseFromParent();   // Nuke the old one.
590
591     if (Cond) ErasePossiblyDeadInstructionTree(Cond);
592     return true;
593   }
594   return false;
595 }
596
597 // FoldValueComparisonIntoPredecessors - The specified terminator is a value
598 // equality comparison instruction (either a switch or a branch on "X == c").
599 // See if any of the predecessors of the terminator block are value comparisons
600 // on the same value.  If so, and if safe to do so, fold them together.
601 static bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI) {
602   BasicBlock *BB = TI->getParent();
603   Value *CV = isValueEqualityComparison(TI);  // CondVal
604   assert(CV && "Not a comparison?");
605   bool Changed = false;
606
607   std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
608   while (!Preds.empty()) {
609     BasicBlock *Pred = Preds.back();
610     Preds.pop_back();
611
612     // See if the predecessor is a comparison with the same value.
613     TerminatorInst *PTI = Pred->getTerminator();
614     Value *PCV = isValueEqualityComparison(PTI);  // PredCondVal
615
616     if (PCV == CV && SafeToMergeTerminators(TI, PTI)) {
617       // Figure out which 'cases' to copy from SI to PSI.
618       std::vector<std::pair<ConstantInt*, BasicBlock*> > BBCases;
619       BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
620
621       std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases;
622       BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
623
624       // Based on whether the default edge from PTI goes to BB or not, fill in
625       // PredCases and PredDefault with the new switch cases we would like to
626       // build.
627       std::vector<BasicBlock*> NewSuccessors;
628
629       if (PredDefault == BB) {
630         // If this is the default destination from PTI, only the edges in TI
631         // that don't occur in PTI, or that branch to BB will be activated.
632         std::set<ConstantInt*> PTIHandled;
633         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
634           if (PredCases[i].second != BB)
635             PTIHandled.insert(PredCases[i].first);
636           else {
637             // The default destination is BB, we don't need explicit targets.
638             std::swap(PredCases[i], PredCases.back());
639             PredCases.pop_back();
640             --i; --e;
641           }
642
643         // Reconstruct the new switch statement we will be building.
644         if (PredDefault != BBDefault) {
645           PredDefault->removePredecessor(Pred);
646           PredDefault = BBDefault;
647           NewSuccessors.push_back(BBDefault);
648         }
649         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
650           if (!PTIHandled.count(BBCases[i].first) &&
651               BBCases[i].second != BBDefault) {
652             PredCases.push_back(BBCases[i]);
653             NewSuccessors.push_back(BBCases[i].second);
654           }
655
656       } else {
657         // If this is not the default destination from PSI, only the edges
658         // in SI that occur in PSI with a destination of BB will be
659         // activated.
660         std::set<ConstantInt*> PTIHandled;
661         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
662           if (PredCases[i].second == BB) {
663             PTIHandled.insert(PredCases[i].first);
664             std::swap(PredCases[i], PredCases.back());
665             PredCases.pop_back();
666             --i; --e;
667           }
668
669         // Okay, now we know which constants were sent to BB from the
670         // predecessor.  Figure out where they will all go now.
671         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
672           if (PTIHandled.count(BBCases[i].first)) {
673             // If this is one we are capable of getting...
674             PredCases.push_back(BBCases[i]);
675             NewSuccessors.push_back(BBCases[i].second);
676             PTIHandled.erase(BBCases[i].first);// This constant is taken care of
677           }
678
679         // If there are any constants vectored to BB that TI doesn't handle,
680         // they must go to the default destination of TI.
681         for (std::set<ConstantInt*>::iterator I = PTIHandled.begin(),
682                E = PTIHandled.end(); I != E; ++I) {
683           PredCases.push_back(std::make_pair(*I, BBDefault));
684           NewSuccessors.push_back(BBDefault);
685         }
686       }
687
688       // Okay, at this point, we know which new successor Pred will get.  Make
689       // sure we update the number of entries in the PHI nodes for these
690       // successors.
691       for (unsigned i = 0, e = NewSuccessors.size(); i != e; ++i)
692         AddPredecessorToBlock(NewSuccessors[i], Pred, BB);
693
694       // Now that the successors are updated, create the new Switch instruction.
695       SwitchInst *NewSI = new SwitchInst(CV, PredDefault, PredCases.size(),PTI);
696       for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
697         NewSI->addCase(PredCases[i].first, PredCases[i].second);
698
699       Instruction *DeadCond = 0;
700       if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
701         // If PTI is a branch, remember the condition.
702         DeadCond = dyn_cast<Instruction>(BI->getCondition());
703       Pred->getInstList().erase(PTI);
704
705       // If the condition is dead now, remove the instruction tree.
706       if (DeadCond) ErasePossiblyDeadInstructionTree(DeadCond);
707
708       // Okay, last check.  If BB is still a successor of PSI, then we must
709       // have an infinite loop case.  If so, add an infinitely looping block
710       // to handle the case to preserve the behavior of the code.
711       BasicBlock *InfLoopBlock = 0;
712       for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
713         if (NewSI->getSuccessor(i) == BB) {
714           if (InfLoopBlock == 0) {
715             // Insert it at the end of the loop, because it's either code,
716             // or it won't matter if it's hot. :)
717             InfLoopBlock = new BasicBlock("infloop", BB->getParent());
718             new BranchInst(InfLoopBlock, InfLoopBlock);
719           }
720           NewSI->setSuccessor(i, InfLoopBlock);
721         }
722
723       Changed = true;
724     }
725   }
726   return Changed;
727 }
728
729 /// HoistThenElseCodeToIf - Given a conditional branch that codes to BB1 and
730 /// BB2, hoist any common code in the two blocks up into the branch block.  The
731 /// caller of this function guarantees that BI's block dominates BB1 and BB2.
732 static bool HoistThenElseCodeToIf(BranchInst *BI) {
733   // This does very trivial matching, with limited scanning, to find identical
734   // instructions in the two blocks.  In particular, we don't want to get into
735   // O(M*N) situations here where M and N are the sizes of BB1 and BB2.  As
736   // such, we currently just scan for obviously identical instructions in an
737   // identical order.
738   BasicBlock *BB1 = BI->getSuccessor(0);  // The true destination.
739   BasicBlock *BB2 = BI->getSuccessor(1);  // The false destination
740
741   Instruction *I1 = BB1->begin(), *I2 = BB2->begin();
742   if (I1->getOpcode() != I2->getOpcode() || !I1->isIdenticalTo(I2))
743     return false;
744
745   // If we get here, we can hoist at least one instruction.
746   BasicBlock *BIParent = BI->getParent();
747
748   do {
749     // If we are hoisting the terminator instruction, don't move one (making a
750     // broken BB), instead clone it, and remove BI.
751     if (isa<TerminatorInst>(I1))
752       goto HoistTerminator;
753
754     // For a normal instruction, we just move one to right before the branch,
755     // then replace all uses of the other with the first.  Finally, we remove
756     // the now redundant second instruction.
757     BIParent->getInstList().splice(BI, BB1->getInstList(), I1);
758     if (!I2->use_empty())
759       I2->replaceAllUsesWith(I1);
760     BB2->getInstList().erase(I2);
761
762     I1 = BB1->begin();
763     I2 = BB2->begin();
764   } while (I1->getOpcode() == I2->getOpcode() && I1->isIdenticalTo(I2));
765
766   return true;
767
768 HoistTerminator:
769   // Okay, it is safe to hoist the terminator.
770   Instruction *NT = I1->clone();
771   BIParent->getInstList().insert(BI, NT);
772   if (NT->getType() != Type::VoidTy) {
773     I1->replaceAllUsesWith(NT);
774     I2->replaceAllUsesWith(NT);
775     NT->setName(I1->getName());
776   }
777
778   // Hoisting one of the terminators from our successor is a great thing.
779   // Unfortunately, the successors of the if/else blocks may have PHI nodes in
780   // them.  If they do, all PHI entries for BB1/BB2 must agree for all PHI
781   // nodes, so we insert select instruction to compute the final result.
782   std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects;
783   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
784     PHINode *PN;
785     for (BasicBlock::iterator BBI = SI->begin();
786          (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
787       Value *BB1V = PN->getIncomingValueForBlock(BB1);
788       Value *BB2V = PN->getIncomingValueForBlock(BB2);
789       if (BB1V != BB2V) {
790         // These values do not agree.  Insert a select instruction before NT
791         // that determines the right value.
792         SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
793         if (SI == 0)
794           SI = new SelectInst(BI->getCondition(), BB1V, BB2V,
795                               BB1V->getName()+"."+BB2V->getName(), NT);
796         // Make the PHI node use the select for all incoming values for BB1/BB2
797         for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
798           if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2)
799             PN->setIncomingValue(i, SI);
800       }
801     }
802   }
803
804   // Update any PHI nodes in our new successors.
805   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
806     AddPredecessorToBlock(*SI, BIParent, BB1);
807
808   BI->eraseFromParent();
809   return true;
810 }
811
812 namespace {
813   /// ConstantIntOrdering - This class implements a stable ordering of constant
814   /// integers that does not depend on their address.  This is important for
815   /// applications that sort ConstantInt's to ensure uniqueness.
816   struct ConstantIntOrdering {
817     bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
818       return LHS->getRawValue() < RHS->getRawValue();
819     }
820   };
821 }
822
823
824 // SimplifyCFG - This function is used to do simplification of a CFG.  For
825 // example, it adjusts branches to branches to eliminate the extra hop, it
826 // eliminates unreachable basic blocks, and does other "peephole" optimization
827 // of the CFG.  It returns true if a modification was made.
828 //
829 // WARNING:  The entry node of a function may not be simplified.
830 //
831 bool llvm::SimplifyCFG(BasicBlock *BB) {
832   bool Changed = false;
833   Function *M = BB->getParent();
834
835   assert(BB && BB->getParent() && "Block not embedded in function!");
836   assert(BB->getTerminator() && "Degenerate basic block encountered!");
837   assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!");
838
839   // Remove basic blocks that have no predecessors... which are unreachable.
840   if (pred_begin(BB) == pred_end(BB) ||
841       *pred_begin(BB) == BB && ++pred_begin(BB) == pred_end(BB)) {
842     DEBUG(std::cerr << "Removing BB: \n" << *BB);
843
844     // Loop through all of our successors and make sure they know that one
845     // of their predecessors is going away.
846     for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
847       SI->removePredecessor(BB);
848
849     while (!BB->empty()) {
850       Instruction &I = BB->back();
851       // If this instruction is used, replace uses with an arbitrary
852       // constant value.  Because control flow can't get here, we don't care
853       // what we replace the value with.  Note that since this block is
854       // unreachable, and all values contained within it must dominate their
855       // uses, that all uses will eventually be removed.
856       if (!I.use_empty())
857         // Make all users of this instruction reference the constant instead
858         I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
859
860       // Remove the instruction from the basic block
861       BB->getInstList().pop_back();
862     }
863     M->getBasicBlockList().erase(BB);
864     return true;
865   }
866
867   // Check to see if we can constant propagate this terminator instruction
868   // away...
869   Changed |= ConstantFoldTerminator(BB);
870
871   // Check to see if this block has no non-phi instructions and only a single
872   // successor.  If so, replace references to this basic block with references
873   // to the successor.
874   succ_iterator SI(succ_begin(BB));
875   if (SI != succ_end(BB) && ++SI == succ_end(BB)) {  // One succ?
876     BasicBlock::iterator BBI = BB->begin();  // Skip over phi nodes...
877     while (isa<PHINode>(*BBI)) ++BBI;
878
879     BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor.
880     if (BBI->isTerminator() &&  // Terminator is the only non-phi instruction!
881         Succ != BB) {           // Don't hurt infinite loops!
882       // If our successor has PHI nodes, then we need to update them to include
883       // entries for BB's predecessors, not for BB itself.  Be careful though,
884       // if this transformation fails (returns true) then we cannot do this
885       // transformation!
886       //
887       if (!PropagatePredecessorsForPHIs(BB, Succ)) {
888         DEBUG(std::cerr << "Killing Trivial BB: \n" << *BB);
889
890         if (isa<PHINode>(&BB->front())) {
891           std::vector<BasicBlock*>
892             OldSuccPreds(pred_begin(Succ), pred_end(Succ));
893
894           // Move all PHI nodes in BB to Succ if they are alive, otherwise
895           // delete them.
896           while (PHINode *PN = dyn_cast<PHINode>(&BB->front()))
897             if (PN->use_empty() || Succ->getSinglePredecessor() == 0) {
898               // We can only move the PHI node into Succ if BB dominates Succ.
899               // Since BB only has a single successor (Succ), the PHI nodes
900               // will dominate Succ, unless Succ has multiple predecessors.  In
901               // this case, the PHIs are either dead, or have references in dead
902               // blocks.  In either case, we can just remove them.
903               if (!PN->use_empty())   // Uses in dead block?
904                 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
905               PN->eraseFromParent();  // Nuke instruction.
906             } else {
907               // The instruction is alive, so this means that Succ must have
908               // *ONLY* had BB as a predecessor, and the PHI node is still valid
909               // now.  Simply move it into Succ, because we know that BB
910               // strictly dominated Succ.
911               BB->getInstList().remove(BB->begin());
912               Succ->getInstList().push_front(PN);
913
914               // We need to add new entries for the PHI node to account for
915               // predecessors of Succ that the PHI node does not take into
916               // account.  At this point, since we know that BB dominated succ,
917               // this means that we should any newly added incoming edges should
918               // use the PHI node as the value for these edges, because they are
919               // loop back edges.
920               for (unsigned i = 0, e = OldSuccPreds.size(); i != e; ++i)
921                 if (OldSuccPreds[i] != BB)
922                   PN->addIncoming(PN, OldSuccPreds[i]);
923             }
924         }
925
926         // Everything that jumped to BB now goes to Succ.
927         std::string OldName = BB->getName();
928         BB->replaceAllUsesWith(Succ);
929         BB->eraseFromParent();              // Delete the old basic block.
930
931         if (!OldName.empty() && !Succ->hasName())  // Transfer name if we can
932           Succ->setName(OldName);
933         return true;
934       }
935     }
936   }
937
938   // If this is a returning block with only PHI nodes in it, fold the return
939   // instruction into any unconditional branch predecessors.
940   //
941   // If any predecessor is a conditional branch that just selects among
942   // different return values, fold the replace the branch/return with a select
943   // and return.
944   if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
945     BasicBlock::iterator BBI = BB->getTerminator();
946     if (BBI == BB->begin() || isa<PHINode>(--BBI)) {
947       // Find predecessors that end with branches.
948       std::vector<BasicBlock*> UncondBranchPreds;
949       std::vector<BranchInst*> CondBranchPreds;
950       for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
951         TerminatorInst *PTI = (*PI)->getTerminator();
952         if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
953           if (BI->isUnconditional())
954             UncondBranchPreds.push_back(*PI);
955           else
956             CondBranchPreds.push_back(BI);
957       }
958
959       // If we found some, do the transformation!
960       if (!UncondBranchPreds.empty()) {
961         while (!UncondBranchPreds.empty()) {
962           BasicBlock *Pred = UncondBranchPreds.back();
963           UncondBranchPreds.pop_back();
964           Instruction *UncondBranch = Pred->getTerminator();
965           // Clone the return and add it to the end of the predecessor.
966           Instruction *NewRet = RI->clone();
967           Pred->getInstList().push_back(NewRet);
968
969           // If the return instruction returns a value, and if the value was a
970           // PHI node in "BB", propagate the right value into the return.
971           if (NewRet->getNumOperands() == 1)
972             if (PHINode *PN = dyn_cast<PHINode>(NewRet->getOperand(0)))
973               if (PN->getParent() == BB)
974                 NewRet->setOperand(0, PN->getIncomingValueForBlock(Pred));
975           // Update any PHI nodes in the returning block to realize that we no
976           // longer branch to them.
977           BB->removePredecessor(Pred);
978           Pred->getInstList().erase(UncondBranch);
979         }
980
981         // If we eliminated all predecessors of the block, delete the block now.
982         if (pred_begin(BB) == pred_end(BB))
983           // We know there are no successors, so just nuke the block.
984           M->getBasicBlockList().erase(BB);
985
986         return true;
987       }
988
989       // Check out all of the conditional branches going to this return
990       // instruction.  If any of them just select between returns, change the
991       // branch itself into a select/return pair.
992       while (!CondBranchPreds.empty()) {
993         BranchInst *BI = CondBranchPreds.back();
994         CondBranchPreds.pop_back();
995         BasicBlock *TrueSucc = BI->getSuccessor(0);
996         BasicBlock *FalseSucc = BI->getSuccessor(1);
997         BasicBlock *OtherSucc = TrueSucc == BB ? FalseSucc : TrueSucc;
998
999         // Check to see if the non-BB successor is also a return block.
1000         if (isa<ReturnInst>(OtherSucc->getTerminator())) {
1001           // Check to see if there are only PHI instructions in this block.
1002           BasicBlock::iterator OSI = OtherSucc->getTerminator();
1003           if (OSI == OtherSucc->begin() || isa<PHINode>(--OSI)) {
1004             // Okay, we found a branch that is going to two return nodes.  If
1005             // there is no return value for this function, just change the
1006             // branch into a return.
1007             if (RI->getNumOperands() == 0) {
1008               TrueSucc->removePredecessor(BI->getParent());
1009               FalseSucc->removePredecessor(BI->getParent());
1010               new ReturnInst(0, BI);
1011               BI->getParent()->getInstList().erase(BI);
1012               return true;
1013             }
1014
1015             // Otherwise, figure out what the true and false return values are
1016             // so we can insert a new select instruction.
1017             Value *TrueValue = TrueSucc->getTerminator()->getOperand(0);
1018             Value *FalseValue = FalseSucc->getTerminator()->getOperand(0);
1019
1020             // Unwrap any PHI nodes in the return blocks.
1021             if (PHINode *TVPN = dyn_cast<PHINode>(TrueValue))
1022               if (TVPN->getParent() == TrueSucc)
1023                 TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
1024             if (PHINode *FVPN = dyn_cast<PHINode>(FalseValue))
1025               if (FVPN->getParent() == FalseSucc)
1026                 FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
1027
1028             TrueSucc->removePredecessor(BI->getParent());
1029             FalseSucc->removePredecessor(BI->getParent());
1030
1031             // Insert a new select instruction.
1032             Value *NewRetVal;
1033             Value *BrCond = BI->getCondition();
1034             if (TrueValue != FalseValue)
1035               NewRetVal = new SelectInst(BrCond, TrueValue,
1036                                          FalseValue, "retval", BI);
1037             else
1038               NewRetVal = TrueValue;
1039
1040             new ReturnInst(NewRetVal, BI);
1041             BI->getParent()->getInstList().erase(BI);
1042             if (BrCond->use_empty())
1043               if (Instruction *BrCondI = dyn_cast<Instruction>(BrCond))
1044                 BrCondI->getParent()->getInstList().erase(BrCondI);
1045             return true;
1046           }
1047         }
1048       }
1049     }
1050   } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->begin())) {
1051     // Check to see if the first instruction in this block is just an unwind.
1052     // If so, replace any invoke instructions which use this as an exception
1053     // destination with call instructions, and any unconditional branch
1054     // predecessor with an unwind.
1055     //
1056     std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
1057     while (!Preds.empty()) {
1058       BasicBlock *Pred = Preds.back();
1059       if (BranchInst *BI = dyn_cast<BranchInst>(Pred->getTerminator())) {
1060         if (BI->isUnconditional()) {
1061           Pred->getInstList().pop_back();  // nuke uncond branch
1062           new UnwindInst(Pred);            // Use unwind.
1063           Changed = true;
1064         }
1065       } else if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
1066         if (II->getUnwindDest() == BB) {
1067           // Insert a new branch instruction before the invoke, because this
1068           // is now a fall through...
1069           BranchInst *BI = new BranchInst(II->getNormalDest(), II);
1070           Pred->getInstList().remove(II);   // Take out of symbol table
1071
1072           // Insert the call now...
1073           std::vector<Value*> Args(II->op_begin()+3, II->op_end());
1074           CallInst *CI = new CallInst(II->getCalledValue(), Args,
1075                                       II->getName(), BI);
1076           CI->setCallingConv(II->getCallingConv());
1077           // If the invoke produced a value, the Call now does instead
1078           II->replaceAllUsesWith(CI);
1079           delete II;
1080           Changed = true;
1081         }
1082
1083       Preds.pop_back();
1084     }
1085
1086     // If this block is now dead, remove it.
1087     if (pred_begin(BB) == pred_end(BB)) {
1088       // We know there are no successors, so just nuke the block.
1089       M->getBasicBlockList().erase(BB);
1090       return true;
1091     }
1092
1093   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
1094     if (isValueEqualityComparison(SI)) {
1095       // If we only have one predecessor, and if it is a branch on this value,
1096       // see if that predecessor totally determines the outcome of this switch.
1097       if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
1098         if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred))
1099           return SimplifyCFG(BB) || 1;
1100
1101       // If the block only contains the switch, see if we can fold the block
1102       // away into any preds.
1103       if (SI == &BB->front())
1104         if (FoldValueComparisonIntoPredecessors(SI))
1105           return SimplifyCFG(BB) || 1;
1106     }
1107   } else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
1108     if (BI->isConditional()) {
1109       if (Value *CompVal = isValueEqualityComparison(BI)) {
1110         // If we only have one predecessor, and if it is a branch on this value,
1111         // see if that predecessor totally determines the outcome of this
1112         // switch.
1113         if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
1114           if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred))
1115             return SimplifyCFG(BB) || 1;
1116
1117         // This block must be empty, except for the setcond inst, if it exists.
1118         BasicBlock::iterator I = BB->begin();
1119         if (&*I == BI ||
1120             (&*I == cast<Instruction>(BI->getCondition()) &&
1121              &*++I == BI))
1122           if (FoldValueComparisonIntoPredecessors(BI))
1123             return SimplifyCFG(BB) | true;
1124       }
1125
1126       // If this basic block is ONLY a setcc and a branch, and if a predecessor
1127       // branches to us and one of our successors, fold the setcc into the
1128       // predecessor and use logical operations to pick the right destination.
1129       BasicBlock *TrueDest  = BI->getSuccessor(0);
1130       BasicBlock *FalseDest = BI->getSuccessor(1);
1131       if (BinaryOperator *Cond = dyn_cast<BinaryOperator>(BI->getCondition()))
1132         if (Cond->getParent() == BB && &BB->front() == Cond &&
1133             Cond->getNext() == BI && Cond->hasOneUse() &&
1134             TrueDest != BB && FalseDest != BB)
1135           for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI!=E; ++PI)
1136             if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
1137               if (PBI->isConditional() && SafeToMergeTerminators(BI, PBI)) {
1138                 BasicBlock *PredBlock = *PI;
1139                 if (PBI->getSuccessor(0) == FalseDest ||
1140                     PBI->getSuccessor(1) == TrueDest) {
1141                   // Invert the predecessors condition test (xor it with true),
1142                   // which allows us to write this code once.
1143                   Value *NewCond =
1144                     BinaryOperator::createNot(PBI->getCondition(),
1145                                     PBI->getCondition()->getName()+".not", PBI);
1146                   PBI->setCondition(NewCond);
1147                   BasicBlock *OldTrue = PBI->getSuccessor(0);
1148                   BasicBlock *OldFalse = PBI->getSuccessor(1);
1149                   PBI->setSuccessor(0, OldFalse);
1150                   PBI->setSuccessor(1, OldTrue);
1151                 }
1152
1153                 if (PBI->getSuccessor(0) == TrueDest ||
1154                     PBI->getSuccessor(1) == FalseDest) {
1155                   // Clone Cond into the predecessor basic block, and or/and the
1156                   // two conditions together.
1157                   Instruction *New = Cond->clone();
1158                   New->setName(Cond->getName());
1159                   Cond->setName(Cond->getName()+".old");
1160                   PredBlock->getInstList().insert(PBI, New);
1161                   Instruction::BinaryOps Opcode =
1162                     PBI->getSuccessor(0) == TrueDest ?
1163                     Instruction::Or : Instruction::And;
1164                   Value *NewCond =
1165                     BinaryOperator::create(Opcode, PBI->getCondition(),
1166                                            New, "bothcond", PBI);
1167                   PBI->setCondition(NewCond);
1168                   if (PBI->getSuccessor(0) == BB) {
1169                     AddPredecessorToBlock(TrueDest, PredBlock, BB);
1170                     PBI->setSuccessor(0, TrueDest);
1171                   }
1172                   if (PBI->getSuccessor(1) == BB) {
1173                     AddPredecessorToBlock(FalseDest, PredBlock, BB);
1174                     PBI->setSuccessor(1, FalseDest);
1175                   }
1176                   return SimplifyCFG(BB) | 1;
1177                 }
1178               }
1179
1180       // If this block ends with a branch instruction, and if there is one
1181       // predecessor, see if the previous block ended with a branch on the same
1182       // condition, which makes this conditional branch redundant.
1183       pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
1184       BasicBlock *OnlyPred = *PI++;
1185       for (; PI != PE; ++PI)// Search all predecessors, see if they are all same
1186         if (*PI != OnlyPred) {
1187           OnlyPred = 0;       // There are multiple different predecessors...
1188           break;
1189         }
1190
1191       if (OnlyPred)
1192         if (BranchInst *PBI = dyn_cast<BranchInst>(OnlyPred->getTerminator()))
1193           if (PBI->isConditional() &&
1194               PBI->getCondition() == BI->getCondition() &&
1195               (PBI->getSuccessor(0) != BB || PBI->getSuccessor(1) != BB)) {
1196             // Okay, the outcome of this conditional branch is statically
1197             // knowable.  Delete the outgoing CFG edge that is impossible to
1198             // execute.
1199             bool CondIsTrue = PBI->getSuccessor(0) == BB;
1200             BI->getSuccessor(CondIsTrue)->removePredecessor(BB);
1201             new BranchInst(BI->getSuccessor(!CondIsTrue), BB);
1202             BB->getInstList().erase(BI);
1203             return SimplifyCFG(BB) | true;
1204           }
1205     }
1206   } else if (isa<UnreachableInst>(BB->getTerminator())) {
1207     // If there are any instructions immediately before the unreachable that can
1208     // be removed, do so.
1209     Instruction *Unreachable = BB->getTerminator();
1210     while (Unreachable != BB->begin()) {
1211       BasicBlock::iterator BBI = Unreachable;
1212       --BBI;
1213       if (isa<CallInst>(BBI)) break;
1214       // Delete this instruction
1215       BB->getInstList().erase(BBI);
1216       Changed = true;
1217     }
1218
1219     // If the unreachable instruction is the first in the block, take a gander
1220     // at all of the predecessors of this instruction, and simplify them.
1221     if (&BB->front() == Unreachable) {
1222       std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
1223       for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1224         TerminatorInst *TI = Preds[i]->getTerminator();
1225
1226         if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1227           if (BI->isUnconditional()) {
1228             if (BI->getSuccessor(0) == BB) {
1229               new UnreachableInst(TI);
1230               TI->eraseFromParent();
1231               Changed = true;
1232             }
1233           } else {
1234             if (BI->getSuccessor(0) == BB) {
1235               new BranchInst(BI->getSuccessor(1), BI);
1236               BI->eraseFromParent();
1237             } else if (BI->getSuccessor(1) == BB) {
1238               new BranchInst(BI->getSuccessor(0), BI);
1239               BI->eraseFromParent();
1240               Changed = true;
1241             }
1242           }
1243         } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1244           for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1245             if (SI->getSuccessor(i) == BB) {
1246               BB->removePredecessor(SI->getParent());
1247               SI->removeCase(i);
1248               --i; --e;
1249               Changed = true;
1250             }
1251           // If the default value is unreachable, figure out the most popular
1252           // destination and make it the default.
1253           if (SI->getSuccessor(0) == BB) {
1254             std::map<BasicBlock*, unsigned> Popularity;
1255             for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1256               Popularity[SI->getSuccessor(i)]++;
1257
1258             // Find the most popular block.
1259             unsigned MaxPop = 0;
1260             BasicBlock *MaxBlock = 0;
1261             for (std::map<BasicBlock*, unsigned>::iterator
1262                    I = Popularity.begin(), E = Popularity.end(); I != E; ++I) {
1263               if (I->second > MaxPop) {
1264                 MaxPop = I->second;
1265                 MaxBlock = I->first;
1266               }
1267             }
1268             if (MaxBlock) {
1269               // Make this the new default, allowing us to delete any explicit
1270               // edges to it.
1271               SI->setSuccessor(0, MaxBlock);
1272               Changed = true;
1273
1274               // If MaxBlock has phinodes in it, remove MaxPop-1 entries from
1275               // it.
1276               if (isa<PHINode>(MaxBlock->begin()))
1277                 for (unsigned i = 0; i != MaxPop-1; ++i)
1278                   MaxBlock->removePredecessor(SI->getParent());
1279
1280               for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1281                 if (SI->getSuccessor(i) == MaxBlock) {
1282                   SI->removeCase(i);
1283                   --i; --e;
1284                 }
1285             }
1286           }
1287         } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
1288           if (II->getUnwindDest() == BB) {
1289             // Convert the invoke to a call instruction.  This would be a good
1290             // place to note that the call does not throw though.
1291             BranchInst *BI = new BranchInst(II->getNormalDest(), II);
1292             II->removeFromParent();   // Take out of symbol table
1293
1294             // Insert the call now...
1295             std::vector<Value*> Args(II->op_begin()+3, II->op_end());
1296             CallInst *CI = new CallInst(II->getCalledValue(), Args,
1297                                         II->getName(), BI);
1298             CI->setCallingConv(II->getCallingConv());
1299             // If the invoke produced a value, the Call does now instead.
1300             II->replaceAllUsesWith(CI);
1301             delete II;
1302             Changed = true;
1303           }
1304         }
1305       }
1306
1307       // If this block is now dead, remove it.
1308       if (pred_begin(BB) == pred_end(BB)) {
1309         // We know there are no successors, so just nuke the block.
1310         M->getBasicBlockList().erase(BB);
1311         return true;
1312       }
1313     }
1314   }
1315
1316   // Merge basic blocks into their predecessor if there is only one distinct
1317   // pred, and if there is only one distinct successor of the predecessor, and
1318   // if there are no PHI nodes.
1319   //
1320   pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
1321   BasicBlock *OnlyPred = *PI++;
1322   for (; PI != PE; ++PI)  // Search all predecessors, see if they are all same
1323     if (*PI != OnlyPred) {
1324       OnlyPred = 0;       // There are multiple different predecessors...
1325       break;
1326     }
1327
1328   BasicBlock *OnlySucc = 0;
1329   if (OnlyPred && OnlyPred != BB &&    // Don't break self loops
1330       OnlyPred->getTerminator()->getOpcode() != Instruction::Invoke) {
1331     // Check to see if there is only one distinct successor...
1332     succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred));
1333     OnlySucc = BB;
1334     for (; SI != SE; ++SI)
1335       if (*SI != OnlySucc) {
1336         OnlySucc = 0;     // There are multiple distinct successors!
1337         break;
1338       }
1339   }
1340
1341   if (OnlySucc) {
1342     DEBUG(std::cerr << "Merging: " << *BB << "into: " << *OnlyPred);
1343     TerminatorInst *Term = OnlyPred->getTerminator();
1344
1345     // Resolve any PHI nodes at the start of the block.  They are all
1346     // guaranteed to have exactly one entry if they exist, unless there are
1347     // multiple duplicate (but guaranteed to be equal) entries for the
1348     // incoming edges.  This occurs when there are multiple edges from
1349     // OnlyPred to OnlySucc.
1350     //
1351     while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
1352       PN->replaceAllUsesWith(PN->getIncomingValue(0));
1353       BB->getInstList().pop_front();  // Delete the phi node...
1354     }
1355
1356     // Delete the unconditional branch from the predecessor...
1357     OnlyPred->getInstList().pop_back();
1358
1359     // Move all definitions in the successor to the predecessor...
1360     OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
1361
1362     // Make all PHI nodes that referred to BB now refer to Pred as their
1363     // source...
1364     BB->replaceAllUsesWith(OnlyPred);
1365
1366     std::string OldName = BB->getName();
1367
1368     // Erase basic block from the function...
1369     M->getBasicBlockList().erase(BB);
1370
1371     // Inherit predecessors name if it exists...
1372     if (!OldName.empty() && !OnlyPred->hasName())
1373       OnlyPred->setName(OldName);
1374
1375     return true;
1376   }
1377
1378   // Otherwise, if this block only has a single predecessor, and if that block
1379   // is a conditional branch, see if we can hoist any code from this block up
1380   // into our predecessor.
1381   if (OnlyPred)
1382     if (BranchInst *BI = dyn_cast<BranchInst>(OnlyPred->getTerminator()))
1383       if (BI->isConditional()) {
1384         // Get the other block.
1385         BasicBlock *OtherBB = BI->getSuccessor(BI->getSuccessor(0) == BB);
1386         PI = pred_begin(OtherBB);
1387         ++PI;
1388         if (PI == pred_end(OtherBB)) {
1389           // We have a conditional branch to two blocks that are only reachable
1390           // from the condbr.  We know that the condbr dominates the two blocks,
1391           // so see if there is any identical code in the "then" and "else"
1392           // blocks.  If so, we can hoist it up to the branching block.
1393           Changed |= HoistThenElseCodeToIf(BI);
1394         }
1395       }
1396
1397   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1398     if (BranchInst *BI = dyn_cast<BranchInst>((*PI)->getTerminator()))
1399       // Change br (X == 0 | X == 1), T, F into a switch instruction.
1400       if (BI->isConditional() && isa<Instruction>(BI->getCondition())) {
1401         Instruction *Cond = cast<Instruction>(BI->getCondition());
1402         // If this is a bunch of seteq's or'd together, or if it's a bunch of
1403         // 'setne's and'ed together, collect them.
1404         Value *CompVal = 0;
1405         std::vector<ConstantInt*> Values;
1406         bool TrueWhenEqual = GatherValueComparisons(Cond, CompVal, Values);
1407         if (CompVal && CompVal->getType()->isInteger()) {
1408           // There might be duplicate constants in the list, which the switch
1409           // instruction can't handle, remove them now.
1410           std::sort(Values.begin(), Values.end(), ConstantIntOrdering());
1411           Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
1412
1413           // Figure out which block is which destination.
1414           BasicBlock *DefaultBB = BI->getSuccessor(1);
1415           BasicBlock *EdgeBB    = BI->getSuccessor(0);
1416           if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB);
1417
1418           // Create the new switch instruction now.
1419           SwitchInst *New = new SwitchInst(CompVal, DefaultBB,Values.size(),BI);
1420
1421           // Add all of the 'cases' to the switch instruction.
1422           for (unsigned i = 0, e = Values.size(); i != e; ++i)
1423             New->addCase(Values[i], EdgeBB);
1424
1425           // We added edges from PI to the EdgeBB.  As such, if there were any
1426           // PHI nodes in EdgeBB, they need entries to be added corresponding to
1427           // the number of edges added.
1428           for (BasicBlock::iterator BBI = EdgeBB->begin();
1429                isa<PHINode>(BBI); ++BBI) {
1430             PHINode *PN = cast<PHINode>(BBI);
1431             Value *InVal = PN->getIncomingValueForBlock(*PI);
1432             for (unsigned i = 0, e = Values.size()-1; i != e; ++i)
1433               PN->addIncoming(InVal, *PI);
1434           }
1435
1436           // Erase the old branch instruction.
1437           (*PI)->getInstList().erase(BI);
1438
1439           // Erase the potentially condition tree that was used to computed the
1440           // branch condition.
1441           ErasePossiblyDeadInstructionTree(Cond);
1442           return true;
1443         }
1444       }
1445
1446   // If there is a trivial two-entry PHI node in this basic block, and we can
1447   // eliminate it, do so now.
1448   if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
1449     if (PN->getNumIncomingValues() == 2) {
1450       // Ok, this is a two entry PHI node.  Check to see if this is a simple "if
1451       // statement", which has a very simple dominance structure.  Basically, we
1452       // are trying to find the condition that is being branched on, which
1453       // subsequently causes this merge to happen.  We really want control
1454       // dependence information for this check, but simplifycfg can't keep it up
1455       // to date, and this catches most of the cases we care about anyway.
1456       //
1457       BasicBlock *IfTrue, *IfFalse;
1458       if (Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse)) {
1459         DEBUG(std::cerr << "FOUND IF CONDITION!  " << *IfCond << "  T: "
1460               << IfTrue->getName() << "  F: " << IfFalse->getName() << "\n");
1461
1462         // Loop over the PHI's seeing if we can promote them all to select
1463         // instructions.  While we are at it, keep track of the instructions
1464         // that need to be moved to the dominating block.
1465         std::set<Instruction*> AggressiveInsts;
1466         bool CanPromote = true;
1467
1468         BasicBlock::iterator AfterPHIIt = BB->begin();
1469         while (isa<PHINode>(AfterPHIIt)) {
1470           PHINode *PN = cast<PHINode>(AfterPHIIt++);
1471           if (PN->getIncomingValue(0) == PN->getIncomingValue(1)) {
1472             if (PN->getIncomingValue(0) != PN)
1473               PN->replaceAllUsesWith(PN->getIncomingValue(0));
1474             else
1475               PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
1476           } else if (!DominatesMergePoint(PN->getIncomingValue(0), BB,
1477                                           &AggressiveInsts) ||
1478                      !DominatesMergePoint(PN->getIncomingValue(1), BB,
1479                                           &AggressiveInsts)) {
1480             CanPromote = false;
1481             break;
1482           }
1483         }
1484
1485         // Did we eliminate all PHI's?
1486         CanPromote |= AfterPHIIt == BB->begin();
1487
1488         // If we all PHI nodes are promotable, check to make sure that all
1489         // instructions in the predecessor blocks can be promoted as well.  If
1490         // not, we won't be able to get rid of the control flow, so it's not
1491         // worth promoting to select instructions.
1492         BasicBlock *DomBlock = 0, *IfBlock1 = 0, *IfBlock2 = 0;
1493         if (CanPromote) {
1494           PN = cast<PHINode>(BB->begin());
1495           BasicBlock *Pred = PN->getIncomingBlock(0);
1496           if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) {
1497             IfBlock1 = Pred;
1498             DomBlock = *pred_begin(Pred);
1499             for (BasicBlock::iterator I = Pred->begin();
1500                  !isa<TerminatorInst>(I); ++I)
1501               if (!AggressiveInsts.count(I)) {
1502                 // This is not an aggressive instruction that we can promote.
1503                 // Because of this, we won't be able to get rid of the control
1504                 // flow, so the xform is not worth it.
1505                 CanPromote = false;
1506                 break;
1507               }
1508           }
1509
1510           Pred = PN->getIncomingBlock(1);
1511           if (CanPromote &&
1512               cast<BranchInst>(Pred->getTerminator())->isUnconditional()) {
1513             IfBlock2 = Pred;
1514             DomBlock = *pred_begin(Pred);
1515             for (BasicBlock::iterator I = Pred->begin();
1516                  !isa<TerminatorInst>(I); ++I)
1517               if (!AggressiveInsts.count(I)) {
1518                 // This is not an aggressive instruction that we can promote.
1519                 // Because of this, we won't be able to get rid of the control
1520                 // flow, so the xform is not worth it.
1521                 CanPromote = false;
1522                 break;
1523               }
1524           }
1525         }
1526
1527         // If we can still promote the PHI nodes after this gauntlet of tests,
1528         // do all of the PHI's now.
1529         if (CanPromote) {
1530           // Move all 'aggressive' instructions, which are defined in the
1531           // conditional parts of the if's up to the dominating block.
1532           if (IfBlock1) {
1533             DomBlock->getInstList().splice(DomBlock->getTerminator(),
1534                                            IfBlock1->getInstList(),
1535                                            IfBlock1->begin(),
1536                                            IfBlock1->getTerminator());
1537           }
1538           if (IfBlock2) {
1539             DomBlock->getInstList().splice(DomBlock->getTerminator(),
1540                                            IfBlock2->getInstList(),
1541                                            IfBlock2->begin(),
1542                                            IfBlock2->getTerminator());
1543           }
1544
1545           while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
1546             // Change the PHI node into a select instruction.
1547             Value *TrueVal =
1548               PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
1549             Value *FalseVal =
1550               PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
1551
1552             std::string Name = PN->getName(); PN->setName("");
1553             PN->replaceAllUsesWith(new SelectInst(IfCond, TrueVal, FalseVal,
1554                                                   Name, AfterPHIIt));
1555             BB->getInstList().erase(PN);
1556           }
1557           Changed = true;
1558         }
1559       }
1560     }
1561
1562   return Changed;
1563 }