Make sorting of ConstantInt be APInt clean through use of ult function.
[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/DerivedTypes.h"
20 #include "llvm/Support/CFG.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Analysis/ConstantFolding.h"
23 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include <algorithm>
26 #include <functional>
27 #include <set>
28 #include <map>
29 using namespace llvm;
30
31 /// SafeToMergeTerminators - Return true if it is safe to merge these two
32 /// terminator instructions together.
33 ///
34 static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
35   if (SI1 == SI2) return false;  // Can't merge with self!
36   
37   // It is not safe to merge these two switch instructions if they have a common
38   // successor, and if that successor has a PHI node, and if *that* PHI node has
39   // conflicting incoming values from the two switch blocks.
40   BasicBlock *SI1BB = SI1->getParent();
41   BasicBlock *SI2BB = SI2->getParent();
42   std::set<BasicBlock*> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
43   
44   for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
45     if (SI1Succs.count(*I))
46       for (BasicBlock::iterator BBI = (*I)->begin();
47            isa<PHINode>(BBI); ++BBI) {
48         PHINode *PN = cast<PHINode>(BBI);
49         if (PN->getIncomingValueForBlock(SI1BB) !=
50             PN->getIncomingValueForBlock(SI2BB))
51           return false;
52       }
53         
54   return true;
55 }
56
57 /// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
58 /// now be entries in it from the 'NewPred' block.  The values that will be
59 /// flowing into the PHI nodes will be the same as those coming in from
60 /// ExistPred, an existing predecessor of Succ.
61 static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
62                                   BasicBlock *ExistPred) {
63   assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) !=
64          succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!");
65   if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
66   
67   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
68     PHINode *PN = cast<PHINode>(I);
69     Value *V = PN->getIncomingValueForBlock(ExistPred);
70     PN->addIncoming(V, NewPred);
71   }
72 }
73
74 // CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
75 // almost-empty BB ending in an unconditional branch to Succ, into succ.
76 //
77 // Assumption: Succ is the single successor for BB.
78 //
79 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
80   assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
81
82   // Check to see if one of the predecessors of BB is already a predecessor of
83   // Succ.  If so, we cannot do the transformation if there are any PHI nodes
84   // with incompatible values coming in from the two edges!
85   //
86   if (isa<PHINode>(Succ->front())) {
87     std::set<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
88     for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
89          PI != PE; ++PI)
90       if (std::find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) {
91         // Loop over all of the PHI nodes checking to see if there are
92         // incompatible values coming in.
93         for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
94           PHINode *PN = cast<PHINode>(I);
95           // Loop up the entries in the PHI node for BB and for *PI if the
96           // values coming in are non-equal, we cannot merge these two blocks
97           // (instead we should insert a conditional move or something, then
98           // merge the blocks).
99           if (PN->getIncomingValueForBlock(BB) !=
100               PN->getIncomingValueForBlock(*PI))
101             return false;  // Values are not equal...
102         }
103       }
104   }
105     
106   // Finally, if BB has PHI nodes that are used by things other than the PHIs in
107   // Succ and Succ has predecessors that are not Succ and not Pred, we cannot
108   // fold these blocks, as we don't know whether BB dominates Succ or not to
109   // update the PHI nodes correctly.
110   if (!isa<PHINode>(BB->begin()) || Succ->getSinglePredecessor()) return true;
111
112   // If the predecessors of Succ are only BB and Succ itself, we can handle this.
113   bool IsSafe = true;
114   for (pred_iterator PI = pred_begin(Succ), E = pred_end(Succ); PI != E; ++PI)
115     if (*PI != Succ && *PI != BB) {
116       IsSafe = false;
117       break;
118     }
119   if (IsSafe) return true;
120   
121   // If the PHI nodes in BB are only used by instructions in Succ, we are ok if
122   // BB and Succ have no common predecessors.
123   for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) {
124     PHINode *PN = cast<PHINode>(I);
125     for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E;
126          ++UI)
127       if (cast<Instruction>(*UI)->getParent() != Succ)
128         return false;
129   }
130   
131   // Scan the predecessor sets of BB and Succ, making sure there are no common
132   // predecessors.  Common predecessors would cause us to build a phi node with
133   // differing incoming values, which is not legal.
134   std::set<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
135   for (pred_iterator PI = pred_begin(Succ), E = pred_end(Succ); PI != E; ++PI)
136     if (BBPreds.count(*PI))
137       return false;
138     
139   return true;
140 }
141
142 /// TryToSimplifyUncondBranchFromEmptyBlock - BB contains an unconditional
143 /// branch to Succ, and contains no instructions other than PHI nodes and the
144 /// branch.  If possible, eliminate BB.
145 static bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
146                                                     BasicBlock *Succ) {
147   // If our successor has PHI nodes, then we need to update them to include
148   // entries for BB's predecessors, not for BB itself.  Be careful though,
149   // if this transformation fails (returns true) then we cannot do this
150   // transformation!
151   //
152   if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
153   
154   DOUT << "Killing Trivial BB: \n" << *BB;
155   
156   if (isa<PHINode>(Succ->begin())) {
157     // If there is more than one pred of succ, and there are PHI nodes in
158     // the successor, then we need to add incoming edges for the PHI nodes
159     //
160     const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
161     
162     // Loop over all of the PHI nodes in the successor of BB.
163     for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
164       PHINode *PN = cast<PHINode>(I);
165       Value *OldVal = PN->removeIncomingValue(BB, false);
166       assert(OldVal && "No entry in PHI for Pred BB!");
167       
168       // If this incoming value is one of the PHI nodes in BB, the new entries
169       // in the PHI node are the entries from the old PHI.
170       if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
171         PHINode *OldValPN = cast<PHINode>(OldVal);
172         for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
173           PN->addIncoming(OldValPN->getIncomingValue(i),
174                           OldValPN->getIncomingBlock(i));
175       } else {
176         for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(),
177              End = BBPreds.end(); PredI != End; ++PredI) {
178           // Add an incoming value for each of the new incoming values...
179           PN->addIncoming(OldVal, *PredI);
180         }
181       }
182     }
183   }
184   
185   if (isa<PHINode>(&BB->front())) {
186     std::vector<BasicBlock*>
187     OldSuccPreds(pred_begin(Succ), pred_end(Succ));
188     
189     // Move all PHI nodes in BB to Succ if they are alive, otherwise
190     // delete them.
191     while (PHINode *PN = dyn_cast<PHINode>(&BB->front()))
192       if (PN->use_empty()) {
193         // Just remove the dead phi.  This happens if Succ's PHIs were the only
194         // users of the PHI nodes.
195         PN->eraseFromParent();
196       } else {
197         // The instruction is alive, so this means that Succ must have
198         // *ONLY* had BB as a predecessor, and the PHI node is still valid
199         // now.  Simply move it into Succ, because we know that BB
200         // strictly dominated Succ.
201         Succ->getInstList().splice(Succ->begin(),
202                                    BB->getInstList(), BB->begin());
203         
204         // We need to add new entries for the PHI node to account for
205         // predecessors of Succ that the PHI node does not take into
206         // account.  At this point, since we know that BB dominated succ,
207         // this means that we should any newly added incoming edges should
208         // use the PHI node as the value for these edges, because they are
209         // loop back edges.
210         for (unsigned i = 0, e = OldSuccPreds.size(); i != e; ++i)
211           if (OldSuccPreds[i] != BB)
212             PN->addIncoming(PN, OldSuccPreds[i]);
213       }
214   }
215     
216   // Everything that jumped to BB now goes to Succ.
217   BB->replaceAllUsesWith(Succ);
218   if (!Succ->hasName()) Succ->takeName(BB);
219   BB->eraseFromParent();              // Delete the old basic block.
220   return true;
221 }
222
223 /// 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.
229 ///
230 ///
231 static 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.
317 //
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.
322 static bool DominatesMergePoint(Value *V, BasicBlock *BB,
323                                 std::set<Instruction*> *AggressiveInsts) {
324   Instruction *I = dyn_cast<Instruction>(V);
325   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   }
333   BasicBlock *PBB = I->getParent();
334
335   // We don't want to allow weird loops that might have the "if condition" in
336   // the bottom of this block.
337   if (PBB == BB) return false;
338
339   // 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) {
344       if (!AggressiveInsts) return false;
345       // 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)) &&
355             !isa<Constant>(I->getOperand(0)))
356           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:
370       case Instruction::LShr:
371       case Instruction::AShr:
372       case Instruction::ICmp:
373       case Instruction::FCmp:
374         break;   // These are all cheap and non-trapping instructions.
375       }
376
377       // Okay, we can only really hoist these out if their operands are not
378       // defined in the conditional region.
379       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
380         if (!DominatesMergePoint(I->getOperand(i), BB, 0))
381           return false;
382       // Okay, it's safe to do this!  Remember this instruction.
383       AggressiveInsts->insert(I);
384     }
385
386   return true;
387 }
388
389 // GatherConstantSetEQs - Given a potentially 'or'd together collection of 
390 // icmp_eq instructions that compare a value against a constant, return the 
391 // value being compared, and stick the constant into the Values vector.
392 static Value *GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values){
393   if (Instruction *Inst = dyn_cast<Instruction>(V))
394     if (Inst->getOpcode() == Instruction::ICmp &&
395         cast<ICmpInst>(Inst)->getPredicate() == ICmpInst::ICMP_EQ) {
396       if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) {
397         Values.push_back(C);
398         return Inst->getOperand(0);
399       } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) {
400         Values.push_back(C);
401         return Inst->getOperand(1);
402       }
403     } else if (Inst->getOpcode() == Instruction::Or) {
404       if (Value *LHS = GatherConstantSetEQs(Inst->getOperand(0), Values))
405         if (Value *RHS = GatherConstantSetEQs(Inst->getOperand(1), Values))
406           if (LHS == RHS)
407             return LHS;
408     }
409   return 0;
410 }
411
412 // GatherConstantSetNEs - Given a potentially 'and'd together collection of
413 // setne instructions that compare a value against a constant, return the value
414 // being compared, and stick the constant into the Values vector.
415 static Value *GatherConstantSetNEs(Value *V, std::vector<ConstantInt*> &Values){
416   if (Instruction *Inst = dyn_cast<Instruction>(V))
417     if (Inst->getOpcode() == Instruction::ICmp &&
418                cast<ICmpInst>(Inst)->getPredicate() == ICmpInst::ICMP_NE) {
419       if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) {
420         Values.push_back(C);
421         return Inst->getOperand(0);
422       } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) {
423         Values.push_back(C);
424         return Inst->getOperand(1);
425       }
426     } else if (Inst->getOpcode() == Instruction::And) {
427       if (Value *LHS = GatherConstantSetNEs(Inst->getOperand(0), Values))
428         if (Value *RHS = GatherConstantSetNEs(Inst->getOperand(1), Values))
429           if (LHS == RHS)
430             return LHS;
431     }
432   return 0;
433 }
434
435
436
437 /// GatherValueComparisons - If the specified Cond is an 'and' or 'or' of a
438 /// bunch of comparisons of one value against constants, return the value and
439 /// the constants being compared.
440 static bool GatherValueComparisons(Instruction *Cond, Value *&CompVal,
441                                    std::vector<ConstantInt*> &Values) {
442   if (Cond->getOpcode() == Instruction::Or) {
443     CompVal = GatherConstantSetEQs(Cond, Values);
444
445     // Return true to indicate that the condition is true if the CompVal is
446     // equal to one of the constants.
447     return true;
448   } else if (Cond->getOpcode() == Instruction::And) {
449     CompVal = GatherConstantSetNEs(Cond, Values);
450
451     // Return false to indicate that the condition is false if the CompVal is
452     // equal to one of the constants.
453     return false;
454   }
455   return false;
456 }
457
458 /// ErasePossiblyDeadInstructionTree - If the specified instruction is dead and
459 /// has no side effects, nuke it.  If it uses any instructions that become dead
460 /// because the instruction is now gone, nuke them too.
461 static void ErasePossiblyDeadInstructionTree(Instruction *I) {
462   if (!isInstructionTriviallyDead(I)) return;
463   
464   std::vector<Instruction*> InstrsToInspect;
465   InstrsToInspect.push_back(I);
466
467   while (!InstrsToInspect.empty()) {
468     I = InstrsToInspect.back();
469     InstrsToInspect.pop_back();
470
471     if (!isInstructionTriviallyDead(I)) continue;
472
473     // If I is in the work list multiple times, remove previous instances.
474     for (unsigned i = 0, e = InstrsToInspect.size(); i != e; ++i)
475       if (InstrsToInspect[i] == I) {
476         InstrsToInspect.erase(InstrsToInspect.begin()+i);
477         --i, --e;
478       }
479
480     // Add operands of dead instruction to worklist.
481     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
482       if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i)))
483         InstrsToInspect.push_back(OpI);
484
485     // Remove dead instruction.
486     I->eraseFromParent();
487   }
488 }
489
490 // isValueEqualityComparison - Return true if the specified terminator checks to
491 // see if a value is equal to constant integer value.
492 static Value *isValueEqualityComparison(TerminatorInst *TI) {
493   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
494     // Do not permit merging of large switch instructions into their
495     // predecessors unless there is only one predecessor.
496     if (SI->getNumSuccessors() * std::distance(pred_begin(SI->getParent()),
497                                                pred_end(SI->getParent())) > 128)
498       return 0;
499
500     return SI->getCondition();
501   }
502   if (BranchInst *BI = dyn_cast<BranchInst>(TI))
503     if (BI->isConditional() && BI->getCondition()->hasOneUse())
504       if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()))
505         if ((ICI->getPredicate() == ICmpInst::ICMP_EQ ||
506              ICI->getPredicate() == ICmpInst::ICMP_NE) &&
507             isa<ConstantInt>(ICI->getOperand(1)))
508           return ICI->getOperand(0);
509   return 0;
510 }
511
512 // Given a value comparison instruction, decode all of the 'cases' that it
513 // represents and return the 'default' block.
514 static BasicBlock *
515 GetValueEqualityComparisonCases(TerminatorInst *TI,
516                                 std::vector<std::pair<ConstantInt*,
517                                                       BasicBlock*> > &Cases) {
518   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
519     Cases.reserve(SI->getNumCases());
520     for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
521       Cases.push_back(std::make_pair(SI->getCaseValue(i), SI->getSuccessor(i)));
522     return SI->getDefaultDest();
523   }
524
525   BranchInst *BI = cast<BranchInst>(TI);
526   ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
527   Cases.push_back(std::make_pair(cast<ConstantInt>(ICI->getOperand(1)),
528                                  BI->getSuccessor(ICI->getPredicate() ==
529                                                   ICmpInst::ICMP_NE)));
530   return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
531 }
532
533
534 // EliminateBlockCases - Given an vector of bb/value pairs, remove any entries
535 // in the list that match the specified block.
536 static void EliminateBlockCases(BasicBlock *BB,
537                std::vector<std::pair<ConstantInt*, BasicBlock*> > &Cases) {
538   for (unsigned i = 0, e = Cases.size(); i != e; ++i)
539     if (Cases[i].second == BB) {
540       Cases.erase(Cases.begin()+i);
541       --i; --e;
542     }
543 }
544
545 // ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as
546 // well.
547 static bool
548 ValuesOverlap(std::vector<std::pair<ConstantInt*, BasicBlock*> > &C1,
549               std::vector<std::pair<ConstantInt*, BasicBlock*> > &C2) {
550   std::vector<std::pair<ConstantInt*, BasicBlock*> > *V1 = &C1, *V2 = &C2;
551
552   // Make V1 be smaller than V2.
553   if (V1->size() > V2->size())
554     std::swap(V1, V2);
555
556   if (V1->size() == 0) return false;
557   if (V1->size() == 1) {
558     // Just scan V2.
559     ConstantInt *TheVal = (*V1)[0].first;
560     for (unsigned i = 0, e = V2->size(); i != e; ++i)
561       if (TheVal == (*V2)[i].first)
562         return true;
563   }
564
565   // Otherwise, just sort both lists and compare element by element.
566   std::sort(V1->begin(), V1->end());
567   std::sort(V2->begin(), V2->end());
568   unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
569   while (i1 != e1 && i2 != e2) {
570     if ((*V1)[i1].first == (*V2)[i2].first)
571       return true;
572     if ((*V1)[i1].first < (*V2)[i2].first)
573       ++i1;
574     else
575       ++i2;
576   }
577   return false;
578 }
579
580 // SimplifyEqualityComparisonWithOnlyPredecessor - If TI is known to be a
581 // terminator instruction and its block is known to only have a single
582 // predecessor block, check to see if that predecessor is also a value
583 // comparison with the same value, and if that comparison determines the outcome
584 // of this comparison.  If so, simplify TI.  This does a very limited form of
585 // jump threading.
586 static bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
587                                                           BasicBlock *Pred) {
588   Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
589   if (!PredVal) return false;  // Not a value comparison in predecessor.
590
591   Value *ThisVal = isValueEqualityComparison(TI);
592   assert(ThisVal && "This isn't a value comparison!!");
593   if (ThisVal != PredVal) return false;  // Different predicates.
594
595   // Find out information about when control will move from Pred to TI's block.
596   std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases;
597   BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(),
598                                                         PredCases);
599   EliminateBlockCases(PredDef, PredCases);  // Remove default from cases.
600
601   // Find information about how control leaves this block.
602   std::vector<std::pair<ConstantInt*, BasicBlock*> > ThisCases;
603   BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
604   EliminateBlockCases(ThisDef, ThisCases);  // Remove default from cases.
605
606   // If TI's block is the default block from Pred's comparison, potentially
607   // simplify TI based on this knowledge.
608   if (PredDef == TI->getParent()) {
609     // If we are here, we know that the value is none of those cases listed in
610     // PredCases.  If there are any cases in ThisCases that are in PredCases, we
611     // can simplify TI.
612     if (ValuesOverlap(PredCases, ThisCases)) {
613       if (BranchInst *BTI = dyn_cast<BranchInst>(TI)) {
614         // Okay, one of the successors of this condbr is dead.  Convert it to a
615         // uncond br.
616         assert(ThisCases.size() == 1 && "Branch can only have one case!");
617         Value *Cond = BTI->getCondition();
618         // Insert the new branch.
619         Instruction *NI = new BranchInst(ThisDef, TI);
620
621         // Remove PHI node entries for the dead edge.
622         ThisCases[0].second->removePredecessor(TI->getParent());
623
624         DOUT << "Threading pred instr: " << *Pred->getTerminator()
625              << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n";
626
627         TI->eraseFromParent();   // Nuke the old one.
628         // If condition is now dead, nuke it.
629         if (Instruction *CondI = dyn_cast<Instruction>(Cond))
630           ErasePossiblyDeadInstructionTree(CondI);
631         return true;
632
633       } else {
634         SwitchInst *SI = cast<SwitchInst>(TI);
635         // Okay, TI has cases that are statically dead, prune them away.
636         std::set<Constant*> DeadCases;
637         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
638           DeadCases.insert(PredCases[i].first);
639
640         DOUT << "Threading pred instr: " << *Pred->getTerminator()
641              << "Through successor TI: " << *TI;
642
643         for (unsigned i = SI->getNumCases()-1; i != 0; --i)
644           if (DeadCases.count(SI->getCaseValue(i))) {
645             SI->getSuccessor(i)->removePredecessor(TI->getParent());
646             SI->removeCase(i);
647           }
648
649         DOUT << "Leaving: " << *TI << "\n";
650         return true;
651       }
652     }
653
654   } else {
655     // Otherwise, TI's block must correspond to some matched value.  Find out
656     // which value (or set of values) this is.
657     ConstantInt *TIV = 0;
658     BasicBlock *TIBB = TI->getParent();
659     for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
660       if (PredCases[i].second == TIBB)
661         if (TIV == 0)
662           TIV = PredCases[i].first;
663         else
664           return false;  // Cannot handle multiple values coming to this block.
665     assert(TIV && "No edge from pred to succ?");
666
667     // Okay, we found the one constant that our value can be if we get into TI's
668     // BB.  Find out which successor will unconditionally be branched to.
669     BasicBlock *TheRealDest = 0;
670     for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
671       if (ThisCases[i].first == TIV) {
672         TheRealDest = ThisCases[i].second;
673         break;
674       }
675
676     // If not handled by any explicit cases, it is handled by the default case.
677     if (TheRealDest == 0) TheRealDest = ThisDef;
678
679     // Remove PHI node entries for dead edges.
680     BasicBlock *CheckEdge = TheRealDest;
681     for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
682       if (*SI != CheckEdge)
683         (*SI)->removePredecessor(TIBB);
684       else
685         CheckEdge = 0;
686
687     // Insert the new branch.
688     Instruction *NI = new BranchInst(TheRealDest, TI);
689
690     DOUT << "Threading pred instr: " << *Pred->getTerminator()
691          << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n";
692     Instruction *Cond = 0;
693     if (BranchInst *BI = dyn_cast<BranchInst>(TI))
694       Cond = dyn_cast<Instruction>(BI->getCondition());
695     TI->eraseFromParent();   // Nuke the old one.
696
697     if (Cond) ErasePossiblyDeadInstructionTree(Cond);
698     return true;
699   }
700   return false;
701 }
702
703 // FoldValueComparisonIntoPredecessors - The specified terminator is a value
704 // equality comparison instruction (either a switch or a branch on "X == c").
705 // See if any of the predecessors of the terminator block are value comparisons
706 // on the same value.  If so, and if safe to do so, fold them together.
707 static bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI) {
708   BasicBlock *BB = TI->getParent();
709   Value *CV = isValueEqualityComparison(TI);  // CondVal
710   assert(CV && "Not a comparison?");
711   bool Changed = false;
712
713   std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
714   while (!Preds.empty()) {
715     BasicBlock *Pred = Preds.back();
716     Preds.pop_back();
717
718     // See if the predecessor is a comparison with the same value.
719     TerminatorInst *PTI = Pred->getTerminator();
720     Value *PCV = isValueEqualityComparison(PTI);  // PredCondVal
721
722     if (PCV == CV && SafeToMergeTerminators(TI, PTI)) {
723       // Figure out which 'cases' to copy from SI to PSI.
724       std::vector<std::pair<ConstantInt*, BasicBlock*> > BBCases;
725       BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
726
727       std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases;
728       BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
729
730       // Based on whether the default edge from PTI goes to BB or not, fill in
731       // PredCases and PredDefault with the new switch cases we would like to
732       // build.
733       std::vector<BasicBlock*> NewSuccessors;
734
735       if (PredDefault == BB) {
736         // If this is the default destination from PTI, only the edges in TI
737         // that don't occur in PTI, or that branch to BB will be activated.
738         std::set<ConstantInt*> PTIHandled;
739         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
740           if (PredCases[i].second != BB)
741             PTIHandled.insert(PredCases[i].first);
742           else {
743             // The default destination is BB, we don't need explicit targets.
744             std::swap(PredCases[i], PredCases.back());
745             PredCases.pop_back();
746             --i; --e;
747           }
748
749         // Reconstruct the new switch statement we will be building.
750         if (PredDefault != BBDefault) {
751           PredDefault->removePredecessor(Pred);
752           PredDefault = BBDefault;
753           NewSuccessors.push_back(BBDefault);
754         }
755         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
756           if (!PTIHandled.count(BBCases[i].first) &&
757               BBCases[i].second != BBDefault) {
758             PredCases.push_back(BBCases[i]);
759             NewSuccessors.push_back(BBCases[i].second);
760           }
761
762       } else {
763         // If this is not the default destination from PSI, only the edges
764         // in SI that occur in PSI with a destination of BB will be
765         // activated.
766         std::set<ConstantInt*> PTIHandled;
767         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
768           if (PredCases[i].second == BB) {
769             PTIHandled.insert(PredCases[i].first);
770             std::swap(PredCases[i], PredCases.back());
771             PredCases.pop_back();
772             --i; --e;
773           }
774
775         // Okay, now we know which constants were sent to BB from the
776         // predecessor.  Figure out where they will all go now.
777         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
778           if (PTIHandled.count(BBCases[i].first)) {
779             // If this is one we are capable of getting...
780             PredCases.push_back(BBCases[i]);
781             NewSuccessors.push_back(BBCases[i].second);
782             PTIHandled.erase(BBCases[i].first);// This constant is taken care of
783           }
784
785         // If there are any constants vectored to BB that TI doesn't handle,
786         // they must go to the default destination of TI.
787         for (std::set<ConstantInt*>::iterator I = PTIHandled.begin(),
788                E = PTIHandled.end(); I != E; ++I) {
789           PredCases.push_back(std::make_pair(*I, BBDefault));
790           NewSuccessors.push_back(BBDefault);
791         }
792       }
793
794       // Okay, at this point, we know which new successor Pred will get.  Make
795       // sure we update the number of entries in the PHI nodes for these
796       // successors.
797       for (unsigned i = 0, e = NewSuccessors.size(); i != e; ++i)
798         AddPredecessorToBlock(NewSuccessors[i], Pred, BB);
799
800       // Now that the successors are updated, create the new Switch instruction.
801       SwitchInst *NewSI = new SwitchInst(CV, PredDefault, PredCases.size(),PTI);
802       for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
803         NewSI->addCase(PredCases[i].first, PredCases[i].second);
804
805       Instruction *DeadCond = 0;
806       if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
807         // If PTI is a branch, remember the condition.
808         DeadCond = dyn_cast<Instruction>(BI->getCondition());
809       Pred->getInstList().erase(PTI);
810
811       // If the condition is dead now, remove the instruction tree.
812       if (DeadCond) ErasePossiblyDeadInstructionTree(DeadCond);
813
814       // Okay, last check.  If BB is still a successor of PSI, then we must
815       // have an infinite loop case.  If so, add an infinitely looping block
816       // to handle the case to preserve the behavior of the code.
817       BasicBlock *InfLoopBlock = 0;
818       for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
819         if (NewSI->getSuccessor(i) == BB) {
820           if (InfLoopBlock == 0) {
821             // Insert it at the end of the loop, because it's either code,
822             // or it won't matter if it's hot. :)
823             InfLoopBlock = new BasicBlock("infloop", BB->getParent());
824             new BranchInst(InfLoopBlock, InfLoopBlock);
825           }
826           NewSI->setSuccessor(i, InfLoopBlock);
827         }
828
829       Changed = true;
830     }
831   }
832   return Changed;
833 }
834
835 /// HoistThenElseCodeToIf - Given a conditional branch that goes to BB1 and
836 /// BB2, hoist any common code in the two blocks up into the branch block.  The
837 /// caller of this function guarantees that BI's block dominates BB1 and BB2.
838 static bool HoistThenElseCodeToIf(BranchInst *BI) {
839   // This does very trivial matching, with limited scanning, to find identical
840   // instructions in the two blocks.  In particular, we don't want to get into
841   // O(M*N) situations here where M and N are the sizes of BB1 and BB2.  As
842   // such, we currently just scan for obviously identical instructions in an
843   // identical order.
844   BasicBlock *BB1 = BI->getSuccessor(0);  // The true destination.
845   BasicBlock *BB2 = BI->getSuccessor(1);  // The false destination
846
847   Instruction *I1 = BB1->begin(), *I2 = BB2->begin();
848   if (I1->getOpcode() != I2->getOpcode() || isa<PHINode>(I1) || 
849       isa<InvokeInst>(I1) || !I1->isIdenticalTo(I2))
850     return false;
851
852   // If we get here, we can hoist at least one instruction.
853   BasicBlock *BIParent = BI->getParent();
854
855   do {
856     // If we are hoisting the terminator instruction, don't move one (making a
857     // broken BB), instead clone it, and remove BI.
858     if (isa<TerminatorInst>(I1))
859       goto HoistTerminator;
860
861     // For a normal instruction, we just move one to right before the branch,
862     // then replace all uses of the other with the first.  Finally, we remove
863     // the now redundant second instruction.
864     BIParent->getInstList().splice(BI, BB1->getInstList(), I1);
865     if (!I2->use_empty())
866       I2->replaceAllUsesWith(I1);
867     BB2->getInstList().erase(I2);
868
869     I1 = BB1->begin();
870     I2 = BB2->begin();
871   } while (I1->getOpcode() == I2->getOpcode() && I1->isIdenticalTo(I2));
872
873   return true;
874
875 HoistTerminator:
876   // Okay, it is safe to hoist the terminator.
877   Instruction *NT = I1->clone();
878   BIParent->getInstList().insert(BI, NT);
879   if (NT->getType() != Type::VoidTy) {
880     I1->replaceAllUsesWith(NT);
881     I2->replaceAllUsesWith(NT);
882     NT->takeName(I1);
883   }
884
885   // Hoisting one of the terminators from our successor is a great thing.
886   // Unfortunately, the successors of the if/else blocks may have PHI nodes in
887   // them.  If they do, all PHI entries for BB1/BB2 must agree for all PHI
888   // nodes, so we insert select instruction to compute the final result.
889   std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects;
890   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
891     PHINode *PN;
892     for (BasicBlock::iterator BBI = SI->begin();
893          (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
894       Value *BB1V = PN->getIncomingValueForBlock(BB1);
895       Value *BB2V = PN->getIncomingValueForBlock(BB2);
896       if (BB1V != BB2V) {
897         // These values do not agree.  Insert a select instruction before NT
898         // that determines the right value.
899         SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
900         if (SI == 0)
901           SI = new SelectInst(BI->getCondition(), BB1V, BB2V,
902                               BB1V->getName()+"."+BB2V->getName(), NT);
903         // Make the PHI node use the select for all incoming values for BB1/BB2
904         for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
905           if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2)
906             PN->setIncomingValue(i, SI);
907       }
908     }
909   }
910
911   // Update any PHI nodes in our new successors.
912   for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
913     AddPredecessorToBlock(*SI, BIParent, BB1);
914
915   BI->eraseFromParent();
916   return true;
917 }
918
919 /// BlockIsSimpleEnoughToThreadThrough - Return true if we can thread a branch
920 /// across this block.
921 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
922   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
923   unsigned Size = 0;
924   
925   // If this basic block contains anything other than a PHI (which controls the
926   // branch) and branch itself, bail out.  FIXME: improve this in the future.
927   for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI, ++Size) {
928     if (Size > 10) return false;  // Don't clone large BB's.
929     
930     // We can only support instructions that are do not define values that are
931     // live outside of the current basic block.
932     for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
933          UI != E; ++UI) {
934       Instruction *U = cast<Instruction>(*UI);
935       if (U->getParent() != BB || isa<PHINode>(U)) return false;
936     }
937     
938     // Looks ok, continue checking.
939   }
940
941   return true;
942 }
943
944 /// FoldCondBranchOnPHI - If we have a conditional branch on a PHI node value
945 /// that is defined in the same block as the branch and if any PHI entries are
946 /// constants, thread edges corresponding to that entry to be branches to their
947 /// ultimate destination.
948 static bool FoldCondBranchOnPHI(BranchInst *BI) {
949   BasicBlock *BB = BI->getParent();
950   PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
951   // NOTE: we currently cannot transform this case if the PHI node is used
952   // outside of the block.
953   if (!PN || PN->getParent() != BB || !PN->hasOneUse())
954     return false;
955   
956   // Degenerate case of a single entry PHI.
957   if (PN->getNumIncomingValues() == 1) {
958     if (PN->getIncomingValue(0) != PN)
959       PN->replaceAllUsesWith(PN->getIncomingValue(0));
960     else
961       PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
962     PN->eraseFromParent();
963     return true;    
964   }
965
966   // Now we know that this block has multiple preds and two succs.
967   if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false;
968   
969   // Okay, this is a simple enough basic block.  See if any phi values are
970   // constants.
971   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
972     ConstantInt *CB;
973     if ((CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i))) &&
974         CB->getType() == Type::Int1Ty) {
975       // Okay, we now know that all edges from PredBB should be revectored to
976       // branch to RealDest.
977       BasicBlock *PredBB = PN->getIncomingBlock(i);
978       BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
979       
980       if (RealDest == BB) continue;  // Skip self loops.
981       
982       // The dest block might have PHI nodes, other predecessors and other
983       // difficult cases.  Instead of being smart about this, just insert a new
984       // block that jumps to the destination block, effectively splitting
985       // the edge we are about to create.
986       BasicBlock *EdgeBB = new BasicBlock(RealDest->getName()+".critedge",
987                                           RealDest->getParent(), RealDest);
988       new BranchInst(RealDest, EdgeBB);
989       PHINode *PN;
990       for (BasicBlock::iterator BBI = RealDest->begin();
991            (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
992         Value *V = PN->getIncomingValueForBlock(BB);
993         PN->addIncoming(V, EdgeBB);
994       }
995
996       // BB may have instructions that are being threaded over.  Clone these
997       // instructions into EdgeBB.  We know that there will be no uses of the
998       // cloned instructions outside of EdgeBB.
999       BasicBlock::iterator InsertPt = EdgeBB->begin();
1000       std::map<Value*, Value*> TranslateMap;  // Track translated values.
1001       for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
1002         if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
1003           TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
1004         } else {
1005           // Clone the instruction.
1006           Instruction *N = BBI->clone();
1007           if (BBI->hasName()) N->setName(BBI->getName()+".c");
1008           
1009           // Update operands due to translation.
1010           for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1011             std::map<Value*, Value*>::iterator PI =
1012               TranslateMap.find(N->getOperand(i));
1013             if (PI != TranslateMap.end())
1014               N->setOperand(i, PI->second);
1015           }
1016           
1017           // Check for trivial simplification.
1018           if (Constant *C = ConstantFoldInstruction(N)) {
1019             TranslateMap[BBI] = C;
1020             delete N;   // Constant folded away, don't need actual inst
1021           } else {
1022             // Insert the new instruction into its new home.
1023             EdgeBB->getInstList().insert(InsertPt, N);
1024             if (!BBI->use_empty())
1025               TranslateMap[BBI] = N;
1026           }
1027         }
1028       }
1029
1030       // Loop over all of the edges from PredBB to BB, changing them to branch
1031       // to EdgeBB instead.
1032       TerminatorInst *PredBBTI = PredBB->getTerminator();
1033       for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
1034         if (PredBBTI->getSuccessor(i) == BB) {
1035           BB->removePredecessor(PredBB);
1036           PredBBTI->setSuccessor(i, EdgeBB);
1037         }
1038       
1039       // Recurse, simplifying any other constants.
1040       return FoldCondBranchOnPHI(BI) | true;
1041     }
1042   }
1043
1044   return false;
1045 }
1046
1047 /// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry
1048 /// PHI node, see if we can eliminate it.
1049 static bool FoldTwoEntryPHINode(PHINode *PN) {
1050   // Ok, this is a two entry PHI node.  Check to see if this is a simple "if
1051   // statement", which has a very simple dominance structure.  Basically, we
1052   // are trying to find the condition that is being branched on, which
1053   // subsequently causes this merge to happen.  We really want control
1054   // dependence information for this check, but simplifycfg can't keep it up
1055   // to date, and this catches most of the cases we care about anyway.
1056   //
1057   BasicBlock *BB = PN->getParent();
1058   BasicBlock *IfTrue, *IfFalse;
1059   Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
1060   if (!IfCond) return false;
1061   
1062   // Okay, we found that we can merge this two-entry phi node into a select.
1063   // Doing so would require us to fold *all* two entry phi nodes in this block.
1064   // At some point this becomes non-profitable (particularly if the target
1065   // doesn't support cmov's).  Only do this transformation if there are two or
1066   // fewer PHI nodes in this block.
1067   unsigned NumPhis = 0;
1068   for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
1069     if (NumPhis > 2)
1070       return false;
1071   
1072   DOUT << "FOUND IF CONDITION!  " << *IfCond << "  T: "
1073        << IfTrue->getName() << "  F: " << IfFalse->getName() << "\n";
1074   
1075   // Loop over the PHI's seeing if we can promote them all to select
1076   // instructions.  While we are at it, keep track of the instructions
1077   // that need to be moved to the dominating block.
1078   std::set<Instruction*> AggressiveInsts;
1079   
1080   BasicBlock::iterator AfterPHIIt = BB->begin();
1081   while (isa<PHINode>(AfterPHIIt)) {
1082     PHINode *PN = cast<PHINode>(AfterPHIIt++);
1083     if (PN->getIncomingValue(0) == PN->getIncomingValue(1)) {
1084       if (PN->getIncomingValue(0) != PN)
1085         PN->replaceAllUsesWith(PN->getIncomingValue(0));
1086       else
1087         PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
1088     } else if (!DominatesMergePoint(PN->getIncomingValue(0), BB,
1089                                     &AggressiveInsts) ||
1090                !DominatesMergePoint(PN->getIncomingValue(1), BB,
1091                                     &AggressiveInsts)) {
1092       return false;
1093     }
1094   }
1095   
1096   // If we all PHI nodes are promotable, check to make sure that all
1097   // instructions in the predecessor blocks can be promoted as well.  If
1098   // not, we won't be able to get rid of the control flow, so it's not
1099   // worth promoting to select instructions.
1100   BasicBlock *DomBlock = 0, *IfBlock1 = 0, *IfBlock2 = 0;
1101   PN = cast<PHINode>(BB->begin());
1102   BasicBlock *Pred = PN->getIncomingBlock(0);
1103   if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) {
1104     IfBlock1 = Pred;
1105     DomBlock = *pred_begin(Pred);
1106     for (BasicBlock::iterator I = Pred->begin();
1107          !isa<TerminatorInst>(I); ++I)
1108       if (!AggressiveInsts.count(I)) {
1109         // This is not an aggressive instruction that we can promote.
1110         // Because of this, we won't be able to get rid of the control
1111         // flow, so the xform is not worth it.
1112         return false;
1113       }
1114   }
1115     
1116   Pred = PN->getIncomingBlock(1);
1117   if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) {
1118     IfBlock2 = Pred;
1119     DomBlock = *pred_begin(Pred);
1120     for (BasicBlock::iterator I = Pred->begin();
1121          !isa<TerminatorInst>(I); ++I)
1122       if (!AggressiveInsts.count(I)) {
1123         // This is not an aggressive instruction that we can promote.
1124         // Because of this, we won't be able to get rid of the control
1125         // flow, so the xform is not worth it.
1126         return false;
1127       }
1128   }
1129       
1130   // If we can still promote the PHI nodes after this gauntlet of tests,
1131   // do all of the PHI's now.
1132
1133   // Move all 'aggressive' instructions, which are defined in the
1134   // conditional parts of the if's up to the dominating block.
1135   if (IfBlock1) {
1136     DomBlock->getInstList().splice(DomBlock->getTerminator(),
1137                                    IfBlock1->getInstList(),
1138                                    IfBlock1->begin(),
1139                                    IfBlock1->getTerminator());
1140   }
1141   if (IfBlock2) {
1142     DomBlock->getInstList().splice(DomBlock->getTerminator(),
1143                                    IfBlock2->getInstList(),
1144                                    IfBlock2->begin(),
1145                                    IfBlock2->getTerminator());
1146   }
1147   
1148   while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
1149     // Change the PHI node into a select instruction.
1150     Value *TrueVal =
1151       PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
1152     Value *FalseVal =
1153       PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
1154     
1155     Value *NV = new SelectInst(IfCond, TrueVal, FalseVal, "", AfterPHIIt);
1156     PN->replaceAllUsesWith(NV);
1157     NV->takeName(PN);
1158     
1159     BB->getInstList().erase(PN);
1160   }
1161   return true;
1162 }
1163
1164 namespace {
1165   /// ConstantIntOrdering - This class implements a stable ordering of constant
1166   /// integers that does not depend on their address.  This is important for
1167   /// applications that sort ConstantInt's to ensure uniqueness.
1168   struct ConstantIntOrdering {
1169     bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
1170       return LHS->getValue().ult(RHS->getValue());
1171     }
1172   };
1173 }
1174
1175 // SimplifyCFG - This function is used to do simplification of a CFG.  For
1176 // example, it adjusts branches to branches to eliminate the extra hop, it
1177 // eliminates unreachable basic blocks, and does other "peephole" optimization
1178 // of the CFG.  It returns true if a modification was made.
1179 //
1180 // WARNING:  The entry node of a function may not be simplified.
1181 //
1182 bool llvm::SimplifyCFG(BasicBlock *BB) {
1183   bool Changed = false;
1184   Function *M = BB->getParent();
1185
1186   assert(BB && BB->getParent() && "Block not embedded in function!");
1187   assert(BB->getTerminator() && "Degenerate basic block encountered!");
1188   assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!");
1189
1190   // Remove basic blocks that have no predecessors... which are unreachable.
1191   if (pred_begin(BB) == pred_end(BB) ||
1192       *pred_begin(BB) == BB && ++pred_begin(BB) == pred_end(BB)) {
1193     DOUT << "Removing BB: \n" << *BB;
1194
1195     // Loop through all of our successors and make sure they know that one
1196     // of their predecessors is going away.
1197     for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
1198       SI->removePredecessor(BB);
1199
1200     while (!BB->empty()) {
1201       Instruction &I = BB->back();
1202       // If this instruction is used, replace uses with an arbitrary
1203       // value.  Because control flow can't get here, we don't care
1204       // what we replace the value with.  Note that since this block is
1205       // unreachable, and all values contained within it must dominate their
1206       // uses, that all uses will eventually be removed.
1207       if (!I.use_empty())
1208         // Make all users of this instruction use undef instead
1209         I.replaceAllUsesWith(UndefValue::get(I.getType()));
1210
1211       // Remove the instruction from the basic block
1212       BB->getInstList().pop_back();
1213     }
1214     M->getBasicBlockList().erase(BB);
1215     return true;
1216   }
1217
1218   // Check to see if we can constant propagate this terminator instruction
1219   // away...
1220   Changed |= ConstantFoldTerminator(BB);
1221
1222   // If this is a returning block with only PHI nodes in it, fold the return
1223   // instruction into any unconditional branch predecessors.
1224   //
1225   // If any predecessor is a conditional branch that just selects among
1226   // different return values, fold the replace the branch/return with a select
1227   // and return.
1228   if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
1229     BasicBlock::iterator BBI = BB->getTerminator();
1230     if (BBI == BB->begin() || isa<PHINode>(--BBI)) {
1231       // Find predecessors that end with branches.
1232       std::vector<BasicBlock*> UncondBranchPreds;
1233       std::vector<BranchInst*> CondBranchPreds;
1234       for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1235         TerminatorInst *PTI = (*PI)->getTerminator();
1236         if (BranchInst *BI = dyn_cast<BranchInst>(PTI))
1237           if (BI->isUnconditional())
1238             UncondBranchPreds.push_back(*PI);
1239           else
1240             CondBranchPreds.push_back(BI);
1241       }
1242
1243       // If we found some, do the transformation!
1244       if (!UncondBranchPreds.empty()) {
1245         while (!UncondBranchPreds.empty()) {
1246           BasicBlock *Pred = UncondBranchPreds.back();
1247           DOUT << "FOLDING: " << *BB
1248                << "INTO UNCOND BRANCH PRED: " << *Pred;
1249           UncondBranchPreds.pop_back();
1250           Instruction *UncondBranch = Pred->getTerminator();
1251           // Clone the return and add it to the end of the predecessor.
1252           Instruction *NewRet = RI->clone();
1253           Pred->getInstList().push_back(NewRet);
1254
1255           // If the return instruction returns a value, and if the value was a
1256           // PHI node in "BB", propagate the right value into the return.
1257           if (NewRet->getNumOperands() == 1)
1258             if (PHINode *PN = dyn_cast<PHINode>(NewRet->getOperand(0)))
1259               if (PN->getParent() == BB)
1260                 NewRet->setOperand(0, PN->getIncomingValueForBlock(Pred));
1261           // Update any PHI nodes in the returning block to realize that we no
1262           // longer branch to them.
1263           BB->removePredecessor(Pred);
1264           Pred->getInstList().erase(UncondBranch);
1265         }
1266
1267         // If we eliminated all predecessors of the block, delete the block now.
1268         if (pred_begin(BB) == pred_end(BB))
1269           // We know there are no successors, so just nuke the block.
1270           M->getBasicBlockList().erase(BB);
1271
1272         return true;
1273       }
1274
1275       // Check out all of the conditional branches going to this return
1276       // instruction.  If any of them just select between returns, change the
1277       // branch itself into a select/return pair.
1278       while (!CondBranchPreds.empty()) {
1279         BranchInst *BI = CondBranchPreds.back();
1280         CondBranchPreds.pop_back();
1281         BasicBlock *TrueSucc = BI->getSuccessor(0);
1282         BasicBlock *FalseSucc = BI->getSuccessor(1);
1283         BasicBlock *OtherSucc = TrueSucc == BB ? FalseSucc : TrueSucc;
1284
1285         // Check to see if the non-BB successor is also a return block.
1286         if (isa<ReturnInst>(OtherSucc->getTerminator())) {
1287           // Check to see if there are only PHI instructions in this block.
1288           BasicBlock::iterator OSI = OtherSucc->getTerminator();
1289           if (OSI == OtherSucc->begin() || isa<PHINode>(--OSI)) {
1290             // Okay, we found a branch that is going to two return nodes.  If
1291             // there is no return value for this function, just change the
1292             // branch into a return.
1293             if (RI->getNumOperands() == 0) {
1294               TrueSucc->removePredecessor(BI->getParent());
1295               FalseSucc->removePredecessor(BI->getParent());
1296               new ReturnInst(0, BI);
1297               BI->getParent()->getInstList().erase(BI);
1298               return true;
1299             }
1300
1301             // Otherwise, figure out what the true and false return values are
1302             // so we can insert a new select instruction.
1303             Value *TrueValue = TrueSucc->getTerminator()->getOperand(0);
1304             Value *FalseValue = FalseSucc->getTerminator()->getOperand(0);
1305
1306             // Unwrap any PHI nodes in the return blocks.
1307             if (PHINode *TVPN = dyn_cast<PHINode>(TrueValue))
1308               if (TVPN->getParent() == TrueSucc)
1309                 TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
1310             if (PHINode *FVPN = dyn_cast<PHINode>(FalseValue))
1311               if (FVPN->getParent() == FalseSucc)
1312                 FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
1313
1314             // In order for this transformation to be safe, we must be able to
1315             // unconditionally execute both operands to the return.  This is
1316             // normally the case, but we could have a potentially-trapping
1317             // constant expression that prevents this transformation from being
1318             // safe.
1319             if ((!isa<ConstantExpr>(TrueValue) ||
1320                  !cast<ConstantExpr>(TrueValue)->canTrap()) &&
1321                 (!isa<ConstantExpr>(TrueValue) ||
1322                  !cast<ConstantExpr>(TrueValue)->canTrap())) {
1323               TrueSucc->removePredecessor(BI->getParent());
1324               FalseSucc->removePredecessor(BI->getParent());
1325
1326               // Insert a new select instruction.
1327               Value *NewRetVal;
1328               Value *BrCond = BI->getCondition();
1329               if (TrueValue != FalseValue)
1330                 NewRetVal = new SelectInst(BrCond, TrueValue,
1331                                            FalseValue, "retval", BI);
1332               else
1333                 NewRetVal = TrueValue;
1334               
1335               DOUT << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
1336                    << "\n  " << *BI << "Select = " << *NewRetVal
1337                    << "TRUEBLOCK: " << *TrueSucc << "FALSEBLOCK: "<< *FalseSucc;
1338
1339               new ReturnInst(NewRetVal, BI);
1340               BI->eraseFromParent();
1341               if (Instruction *BrCondI = dyn_cast<Instruction>(BrCond))
1342                 if (isInstructionTriviallyDead(BrCondI))
1343                   BrCondI->eraseFromParent();
1344               return true;
1345             }
1346           }
1347         }
1348       }
1349     }
1350   } else if (isa<UnwindInst>(BB->begin())) {
1351     // Check to see if the first instruction in this block is just an unwind.
1352     // If so, replace any invoke instructions which use this as an exception
1353     // destination with call instructions, and any unconditional branch
1354     // predecessor with an unwind.
1355     //
1356     std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
1357     while (!Preds.empty()) {
1358       BasicBlock *Pred = Preds.back();
1359       if (BranchInst *BI = dyn_cast<BranchInst>(Pred->getTerminator())) {
1360         if (BI->isUnconditional()) {
1361           Pred->getInstList().pop_back();  // nuke uncond branch
1362           new UnwindInst(Pred);            // Use unwind.
1363           Changed = true;
1364         }
1365       } else if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
1366         if (II->getUnwindDest() == BB) {
1367           // Insert a new branch instruction before the invoke, because this
1368           // is now a fall through...
1369           BranchInst *BI = new BranchInst(II->getNormalDest(), II);
1370           Pred->getInstList().remove(II);   // Take out of symbol table
1371
1372           // Insert the call now...
1373           SmallVector<Value*,8> Args(II->op_begin()+3, II->op_end());
1374           CallInst *CI = new CallInst(II->getCalledValue(),
1375                                       &Args[0], Args.size(), II->getName(), BI);
1376           CI->setCallingConv(II->getCallingConv());
1377           // If the invoke produced a value, the Call now does instead
1378           II->replaceAllUsesWith(CI);
1379           delete II;
1380           Changed = true;
1381         }
1382
1383       Preds.pop_back();
1384     }
1385
1386     // If this block is now dead, remove it.
1387     if (pred_begin(BB) == pred_end(BB)) {
1388       // We know there are no successors, so just nuke the block.
1389       M->getBasicBlockList().erase(BB);
1390       return true;
1391     }
1392
1393   } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
1394     if (isValueEqualityComparison(SI)) {
1395       // If we only have one predecessor, and if it is a branch on this value,
1396       // see if that predecessor totally determines the outcome of this switch.
1397       if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
1398         if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred))
1399           return SimplifyCFG(BB) || 1;
1400
1401       // If the block only contains the switch, see if we can fold the block
1402       // away into any preds.
1403       if (SI == &BB->front())
1404         if (FoldValueComparisonIntoPredecessors(SI))
1405           return SimplifyCFG(BB) || 1;
1406     }
1407   } else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
1408     if (BI->isUnconditional()) {
1409       BasicBlock::iterator BBI = BB->begin();  // Skip over phi nodes...
1410       while (isa<PHINode>(*BBI)) ++BBI;
1411
1412       BasicBlock *Succ = BI->getSuccessor(0);
1413       if (BBI->isTerminator() &&  // Terminator is the only non-phi instruction!
1414           Succ != BB)             // Don't hurt infinite loops!
1415         if (TryToSimplifyUncondBranchFromEmptyBlock(BB, Succ))
1416           return 1;
1417       
1418     } else {  // Conditional branch
1419       if (isValueEqualityComparison(BI)) {
1420         // If we only have one predecessor, and if it is a branch on this value,
1421         // see if that predecessor totally determines the outcome of this
1422         // switch.
1423         if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
1424           if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred))
1425             return SimplifyCFG(BB) || 1;
1426
1427         // This block must be empty, except for the setcond inst, if it exists.
1428         BasicBlock::iterator I = BB->begin();
1429         if (&*I == BI ||
1430             (&*I == cast<Instruction>(BI->getCondition()) &&
1431              &*++I == BI))
1432           if (FoldValueComparisonIntoPredecessors(BI))
1433             return SimplifyCFG(BB) | true;
1434       }
1435       
1436       // If this is a branch on a phi node in the current block, thread control
1437       // through this block if any PHI node entries are constants.
1438       if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
1439         if (PN->getParent() == BI->getParent())
1440           if (FoldCondBranchOnPHI(BI))
1441             return SimplifyCFG(BB) | true;
1442
1443       // If this basic block is ONLY a setcc and a branch, and if a predecessor
1444       // branches to us and one of our successors, fold the setcc into the
1445       // predecessor and use logical operations to pick the right destination.
1446       BasicBlock *TrueDest  = BI->getSuccessor(0);
1447       BasicBlock *FalseDest = BI->getSuccessor(1);
1448       if (Instruction *Cond = dyn_cast<Instruction>(BI->getCondition()))
1449         if ((isa<CmpInst>(Cond) || isa<BinaryOperator>(Cond)) &&
1450             Cond->getParent() == BB && &BB->front() == Cond &&
1451             Cond->getNext() == BI && Cond->hasOneUse() &&
1452             TrueDest != BB && FalseDest != BB)
1453           for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI!=E; ++PI)
1454             if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
1455               if (PBI->isConditional() && SafeToMergeTerminators(BI, PBI)) {
1456                 BasicBlock *PredBlock = *PI;
1457                 if (PBI->getSuccessor(0) == FalseDest ||
1458                     PBI->getSuccessor(1) == TrueDest) {
1459                   // Invert the predecessors condition test (xor it with true),
1460                   // which allows us to write this code once.
1461                   Value *NewCond =
1462                     BinaryOperator::createNot(PBI->getCondition(),
1463                                     PBI->getCondition()->getName()+".not", PBI);
1464                   PBI->setCondition(NewCond);
1465                   BasicBlock *OldTrue = PBI->getSuccessor(0);
1466                   BasicBlock *OldFalse = PBI->getSuccessor(1);
1467                   PBI->setSuccessor(0, OldFalse);
1468                   PBI->setSuccessor(1, OldTrue);
1469                 }
1470
1471                 if ((PBI->getSuccessor(0) == TrueDest && FalseDest != BB) ||
1472                     (PBI->getSuccessor(1) == FalseDest && TrueDest != BB)) {
1473                   // Clone Cond into the predecessor basic block, and or/and the
1474                   // two conditions together.
1475                   Instruction *New = Cond->clone();
1476                   PredBlock->getInstList().insert(PBI, New);
1477                   New->takeName(Cond);
1478                   Cond->setName(New->getName()+".old");
1479                   Instruction::BinaryOps Opcode =
1480                     PBI->getSuccessor(0) == TrueDest ?
1481                     Instruction::Or : Instruction::And;
1482                   Value *NewCond =
1483                     BinaryOperator::create(Opcode, PBI->getCondition(),
1484                                            New, "bothcond", PBI);
1485                   PBI->setCondition(NewCond);
1486                   if (PBI->getSuccessor(0) == BB) {
1487                     AddPredecessorToBlock(TrueDest, PredBlock, BB);
1488                     PBI->setSuccessor(0, TrueDest);
1489                   }
1490                   if (PBI->getSuccessor(1) == BB) {
1491                     AddPredecessorToBlock(FalseDest, PredBlock, BB);
1492                     PBI->setSuccessor(1, FalseDest);
1493                   }
1494                   return SimplifyCFG(BB) | 1;
1495                 }
1496               }
1497
1498       // Scan predessor blocks for conditional branchs.
1499       for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1500         if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
1501           if (PBI != BI && PBI->isConditional()) {
1502               
1503             // If this block ends with a branch instruction, and if there is a
1504             // predecessor that ends on a branch of the same condition, make 
1505             // this conditional branch redundant.
1506             if (PBI->getCondition() == BI->getCondition() &&
1507                 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
1508               // Okay, the outcome of this conditional branch is statically
1509               // knowable.  If this block had a single pred, handle specially.
1510               if (BB->getSinglePredecessor()) {
1511                 // Turn this into a branch on constant.
1512                 bool CondIsTrue = PBI->getSuccessor(0) == BB;
1513                 BI->setCondition(ConstantInt::get(Type::Int1Ty, CondIsTrue));
1514                 return SimplifyCFG(BB);  // Nuke the branch on constant.
1515               }
1516               
1517               // Otherwise, if there are multiple predecessors, insert a PHI 
1518               // that merges in the constant and simplify the block result.
1519               if (BlockIsSimpleEnoughToThreadThrough(BB)) {
1520                 PHINode *NewPN = new PHINode(Type::Int1Ty,
1521                                             BI->getCondition()->getName()+".pr",
1522                                             BB->begin());
1523                 for (PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1524                   if ((PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) &&
1525                       PBI != BI && PBI->isConditional() &&
1526                       PBI->getCondition() == BI->getCondition() &&
1527                       PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
1528                     bool CondIsTrue = PBI->getSuccessor(0) == BB;
1529                     NewPN->addIncoming(ConstantInt::get(Type::Int1Ty, 
1530                                                         CondIsTrue), *PI);
1531                   } else {
1532                     NewPN->addIncoming(BI->getCondition(), *PI);
1533                   }
1534                 
1535                 BI->setCondition(NewPN);
1536                 // This will thread the branch.
1537                 return SimplifyCFG(BB) | true;
1538               }
1539             }
1540             
1541             // If this is a conditional branch in an empty block, and if any
1542             // predecessors is a conditional branch to one of our destinations,
1543             // fold the conditions into logical ops and one cond br.
1544             if (&BB->front() == BI) {
1545               int PBIOp, BIOp;
1546               if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
1547                 PBIOp = BIOp = 0;
1548               } else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
1549                 PBIOp = 0; BIOp = 1;
1550               } else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
1551                 PBIOp = 1; BIOp = 0;
1552               } else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
1553                 PBIOp = BIOp = 1;
1554               } else {
1555                 PBIOp = BIOp = -1;
1556               }
1557               
1558               // Check to make sure that the other destination of this branch
1559               // isn't BB itself.  If so, this is an infinite loop that will
1560               // keep getting unwound.
1561               if (PBIOp != -1 && PBI->getSuccessor(PBIOp) == BB)
1562                 PBIOp = BIOp = -1;
1563               
1564               // Do not perform this transformation if it would require 
1565               // insertion of a large number of select instructions. For targets
1566               // without predication/cmovs, this is a big pessimization.
1567               if (PBIOp != -1) {
1568                 BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
1569            
1570                 unsigned NumPhis = 0;
1571                 for (BasicBlock::iterator II = CommonDest->begin();
1572                      isa<PHINode>(II); ++II, ++NumPhis) {
1573                   if (NumPhis > 2) {
1574                     // Disable this xform.
1575                     PBIOp = -1;
1576                     break;
1577                   }
1578                 }
1579               }
1580
1581               // Finally, if everything is ok, fold the branches to logical ops.
1582               if (PBIOp != -1) {
1583                 BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
1584                 BasicBlock *OtherDest  = BI->getSuccessor(BIOp ^ 1);
1585
1586                 // If OtherDest *is* BB, then this is a basic block with just
1587                 // a conditional branch in it, where one edge (OtherDesg) goes
1588                 // back to the block.  We know that the program doesn't get
1589                 // stuck in the infinite loop, so the condition must be such
1590                 // that OtherDest isn't branched through. Forward to CommonDest,
1591                 // and avoid an infinite loop at optimizer time.
1592                 if (OtherDest == BB)
1593                   OtherDest = CommonDest;
1594                 
1595                 DOUT << "FOLDING BRs:" << *PBI->getParent()
1596                      << "AND: " << *BI->getParent();
1597                                 
1598                 // BI may have other predecessors.  Because of this, we leave
1599                 // it alone, but modify PBI.
1600                 
1601                 // Make sure we get to CommonDest on True&True directions.
1602                 Value *PBICond = PBI->getCondition();
1603                 if (PBIOp)
1604                   PBICond = BinaryOperator::createNot(PBICond,
1605                                                       PBICond->getName()+".not",
1606                                                       PBI);
1607                 Value *BICond = BI->getCondition();
1608                 if (BIOp)
1609                   BICond = BinaryOperator::createNot(BICond,
1610                                                      BICond->getName()+".not",
1611                                                      PBI);
1612                 // Merge the conditions.
1613                 Value *Cond =
1614                   BinaryOperator::createOr(PBICond, BICond, "brmerge", PBI);
1615                 
1616                 // Modify PBI to branch on the new condition to the new dests.
1617                 PBI->setCondition(Cond);
1618                 PBI->setSuccessor(0, CommonDest);
1619                 PBI->setSuccessor(1, OtherDest);
1620
1621                 // OtherDest may have phi nodes.  If so, add an entry from PBI's
1622                 // block that are identical to the entries for BI's block.
1623                 PHINode *PN;
1624                 for (BasicBlock::iterator II = OtherDest->begin();
1625                      (PN = dyn_cast<PHINode>(II)); ++II) {
1626                   Value *V = PN->getIncomingValueForBlock(BB);
1627                   PN->addIncoming(V, PBI->getParent());
1628                 }
1629                 
1630                 // We know that the CommonDest already had an edge from PBI to
1631                 // it.  If it has PHIs though, the PHIs may have different
1632                 // entries for BB and PBI's BB.  If so, insert a select to make
1633                 // them agree.
1634                 for (BasicBlock::iterator II = CommonDest->begin();
1635                      (PN = dyn_cast<PHINode>(II)); ++II) {
1636                   Value * BIV = PN->getIncomingValueForBlock(BB);
1637                   unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
1638                   Value *PBIV = PN->getIncomingValue(PBBIdx);
1639                   if (BIV != PBIV) {
1640                     // Insert a select in PBI to pick the right value.
1641                     Value *NV = new SelectInst(PBICond, PBIV, BIV,
1642                                                PBIV->getName()+".mux", PBI);
1643                     PN->setIncomingValue(PBBIdx, NV);
1644                   }
1645                 }
1646
1647                 DOUT << "INTO: " << *PBI->getParent();
1648
1649                 // This basic block is probably dead.  We know it has at least
1650                 // one fewer predecessor.
1651                 return SimplifyCFG(BB) | true;
1652               }
1653             }
1654           }
1655     }
1656   } else if (isa<UnreachableInst>(BB->getTerminator())) {
1657     // If there are any instructions immediately before the unreachable that can
1658     // be removed, do so.
1659     Instruction *Unreachable = BB->getTerminator();
1660     while (Unreachable != BB->begin()) {
1661       BasicBlock::iterator BBI = Unreachable;
1662       --BBI;
1663       if (isa<CallInst>(BBI)) break;
1664       // Delete this instruction
1665       BB->getInstList().erase(BBI);
1666       Changed = true;
1667     }
1668
1669     // If the unreachable instruction is the first in the block, take a gander
1670     // at all of the predecessors of this instruction, and simplify them.
1671     if (&BB->front() == Unreachable) {
1672       std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
1673       for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1674         TerminatorInst *TI = Preds[i]->getTerminator();
1675
1676         if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1677           if (BI->isUnconditional()) {
1678             if (BI->getSuccessor(0) == BB) {
1679               new UnreachableInst(TI);
1680               TI->eraseFromParent();
1681               Changed = true;
1682             }
1683           } else {
1684             if (BI->getSuccessor(0) == BB) {
1685               new BranchInst(BI->getSuccessor(1), BI);
1686               BI->eraseFromParent();
1687             } else if (BI->getSuccessor(1) == BB) {
1688               new BranchInst(BI->getSuccessor(0), BI);
1689               BI->eraseFromParent();
1690               Changed = true;
1691             }
1692           }
1693         } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1694           for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1695             if (SI->getSuccessor(i) == BB) {
1696               BB->removePredecessor(SI->getParent());
1697               SI->removeCase(i);
1698               --i; --e;
1699               Changed = true;
1700             }
1701           // If the default value is unreachable, figure out the most popular
1702           // destination and make it the default.
1703           if (SI->getSuccessor(0) == BB) {
1704             std::map<BasicBlock*, unsigned> Popularity;
1705             for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1706               Popularity[SI->getSuccessor(i)]++;
1707
1708             // Find the most popular block.
1709             unsigned MaxPop = 0;
1710             BasicBlock *MaxBlock = 0;
1711             for (std::map<BasicBlock*, unsigned>::iterator
1712                    I = Popularity.begin(), E = Popularity.end(); I != E; ++I) {
1713               if (I->second > MaxPop) {
1714                 MaxPop = I->second;
1715                 MaxBlock = I->first;
1716               }
1717             }
1718             if (MaxBlock) {
1719               // Make this the new default, allowing us to delete any explicit
1720               // edges to it.
1721               SI->setSuccessor(0, MaxBlock);
1722               Changed = true;
1723
1724               // If MaxBlock has phinodes in it, remove MaxPop-1 entries from
1725               // it.
1726               if (isa<PHINode>(MaxBlock->begin()))
1727                 for (unsigned i = 0; i != MaxPop-1; ++i)
1728                   MaxBlock->removePredecessor(SI->getParent());
1729
1730               for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i)
1731                 if (SI->getSuccessor(i) == MaxBlock) {
1732                   SI->removeCase(i);
1733                   --i; --e;
1734                 }
1735             }
1736           }
1737         } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
1738           if (II->getUnwindDest() == BB) {
1739             // Convert the invoke to a call instruction.  This would be a good
1740             // place to note that the call does not throw though.
1741             BranchInst *BI = new BranchInst(II->getNormalDest(), II);
1742             II->removeFromParent();   // Take out of symbol table
1743
1744             // Insert the call now...
1745             SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end());
1746             CallInst *CI = new CallInst(II->getCalledValue(),
1747                                         &Args[0], Args.size(),
1748                                         II->getName(), BI);
1749             CI->setCallingConv(II->getCallingConv());
1750             // 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     }
1765   }
1766
1767   // 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   //
1771   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     }
1778
1779   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!
1788         break;
1789       }
1790   }
1791
1792   if (OnlySucc) {
1793     DOUT << "Merging: " << *BB << "into: " << *OnlyPred;
1794
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.
1804     }
1805
1806     // Delete the unconditional branch from the predecessor.
1807     OnlyPred->getInstList().pop_back();
1808
1809     // Move all definitions in the successor to the predecessor.
1810     OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
1811
1812     // Make all PHI nodes that referred to BB now refer to Pred as their
1813     // source.
1814     BB->replaceAllUsesWith(OnlyPred);
1815
1816     // Inherit predecessors name if it exists.
1817     if (!OnlyPred->hasName())
1818       OnlyPred->takeName(BB);
1819     
1820     // Erase basic block from the function.
1821     M->getBasicBlockList().erase(BB);
1822
1823     return true;
1824   }
1825
1826   // Otherwise, if this block only has a single predecessor, and if that block
1827   // is a conditional branch, see if we can hoist any code from this block up
1828   // into our predecessor.
1829   if (OnlyPred)
1830     if (BranchInst *BI = dyn_cast<BranchInst>(OnlyPred->getTerminator()))
1831       if (BI->isConditional()) {
1832         // Get the other block.
1833         BasicBlock *OtherBB = BI->getSuccessor(BI->getSuccessor(0) == BB);
1834         PI = pred_begin(OtherBB);
1835         ++PI;
1836         if (PI == pred_end(OtherBB)) {
1837           // We have a conditional branch to two blocks that are only reachable
1838           // from the condbr.  We know that the condbr dominates the two blocks,
1839           // so see if there is any identical code in the "then" and "else"
1840           // blocks.  If so, we can hoist it up to the branching block.
1841           Changed |= HoistThenElseCodeToIf(BI);
1842         }
1843       }
1844
1845   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1846     if (BranchInst *BI = dyn_cast<BranchInst>((*PI)->getTerminator()))
1847       // Change br (X == 0 | X == 1), T, F into a switch instruction.
1848       if (BI->isConditional() && isa<Instruction>(BI->getCondition())) {
1849         Instruction *Cond = cast<Instruction>(BI->getCondition());
1850         // If this is a bunch of seteq's or'd together, or if it's a bunch of
1851         // 'setne's and'ed together, collect them.
1852         Value *CompVal = 0;
1853         std::vector<ConstantInt*> Values;
1854         bool TrueWhenEqual = GatherValueComparisons(Cond, CompVal, Values);
1855         if (CompVal && CompVal->getType()->isInteger()) {
1856           // There might be duplicate constants in the list, which the switch
1857           // instruction can't handle, remove them now.
1858           std::sort(Values.begin(), Values.end(), ConstantIntOrdering());
1859           Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
1860
1861           // Figure out which block is which destination.
1862           BasicBlock *DefaultBB = BI->getSuccessor(1);
1863           BasicBlock *EdgeBB    = BI->getSuccessor(0);
1864           if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB);
1865
1866           // Create the new switch instruction now.
1867           SwitchInst *New = new SwitchInst(CompVal, DefaultBB,Values.size(),BI);
1868
1869           // Add all of the 'cases' to the switch instruction.
1870           for (unsigned i = 0, e = Values.size(); i != e; ++i)
1871             New->addCase(Values[i], EdgeBB);
1872
1873           // We added edges from PI to the EdgeBB.  As such, if there were any
1874           // PHI nodes in EdgeBB, they need entries to be added corresponding to
1875           // the number of edges added.
1876           for (BasicBlock::iterator BBI = EdgeBB->begin();
1877                isa<PHINode>(BBI); ++BBI) {
1878             PHINode *PN = cast<PHINode>(BBI);
1879             Value *InVal = PN->getIncomingValueForBlock(*PI);
1880             for (unsigned i = 0, e = Values.size()-1; i != e; ++i)
1881               PN->addIncoming(InVal, *PI);
1882           }
1883
1884           // Erase the old branch instruction.
1885           (*PI)->getInstList().erase(BI);
1886
1887           // Erase the potentially condition tree that was used to computed the
1888           // branch condition.
1889           ErasePossiblyDeadInstructionTree(Cond);
1890           return true;
1891         }
1892       }
1893
1894   // If there is a trivial two-entry PHI node in this basic block, and we can
1895   // eliminate it, do so now.
1896   if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
1897     if (PN->getNumIncomingValues() == 2)
1898       Changed |= FoldTwoEntryPHINode(PN); 
1899
1900   return Changed;
1901 }