Changes For Bug 352
[oota-llvm.git] / lib / Transforms / Utils / SimplifyCFG.cpp
index 87bd00cefcf11803c189112336786e8225ce5c75..c93217333041609bc4f63b98506597a6093a27b6 100644 (file)
 #include "llvm/Instructions.h"
 #include "llvm/Type.h"
 #include "llvm/Support/CFG.h"
-#include "Support/Debug.h"
+#include "llvm/Support/Debug.h"
 #include <algorithm>
 #include <functional>
 #include <set>
+
 using namespace llvm;
 
 // PropagatePredecessorsForPHIs - This gets "Succ" ready to have the
@@ -209,8 +210,7 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB, bool AllowAggressive){
         if (cast<LoadInst>(I)->isVolatile())
           return false;
         if (!isa<AllocaInst>(I->getOperand(0)) &&
-            !isa<Constant>(I->getOperand(0)) &&
-            !isa<GlobalValue>(I->getOperand(0)))
+            !isa<Constant>(I->getOperand(0)))
           return false;
 
         // Finally, we have to check to make sure there are no instructions
@@ -332,7 +332,7 @@ static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
   if (SI1 == SI2) return false;  // Can't merge with self!
 
   // It is not safe to merge these two switch instructions if they have a common
-  // successor, and if that successor has a PHI node, and if that PHI node has
+  // successor, and if that successor has a PHI node, and if *that* PHI node has
   // conflicting incoming values from the two switch blocks.
   BasicBlock *SI1BB = SI1->getParent();
   BasicBlock *SI2BB = SI2->getParent();
@@ -352,7 +352,7 @@ static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
 /// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
 /// now be entries in it from the 'NewPred' block.  The values that will be
 /// flowing into the PHI nodes will be the same as those coming in from
-/// ExistPred, and existing predecessor of Succ.
+/// ExistPred, an existing predecessor of Succ.
 static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
                                   BasicBlock *ExistPred) {
   assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) !=
@@ -565,7 +565,7 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
   // Remove basic blocks that have no predecessors... which are unreachable.
   if (pred_begin(BB) == pred_end(BB) ||
       *pred_begin(BB) == BB && ++pred_begin(BB) == pred_end(BB)) {
-    DEBUG(std::cerr << "Removing BB: \n" << BB);
+    DEBUG(std::cerr << "Removing BB: \n" << *BB);
 
     // Loop through all of our successors and make sure they know that one
     // of their predecessors is going away.
@@ -613,7 +613,7 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
         // we cannot do this transformation!
         //
        if (!PropagatePredecessorsForPHIs(BB, Succ)) {
-          DEBUG(std::cerr << "Killing Trivial BB: \n" << BB);
+          DEBUG(std::cerr << "Killing Trivial BB: \n" << *BB);
           std::string OldName = BB->getName();
 
           std::vector<BasicBlock*>
@@ -763,12 +763,19 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
   } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->begin())) {
     // Check to see if the first instruction in this block is just an unwind.
     // If so, replace any invoke instructions which use this as an exception
-    // destination with call instructions.
+    // destination with call instructions, and any unconditional branch
+    // predecessor with an unwind.
     //
     std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
     while (!Preds.empty()) {
       BasicBlock *Pred = Preds.back();
-      if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
+      if (BranchInst *BI = dyn_cast<BranchInst>(Pred->getTerminator())) {
+        if (BI->isUnconditional()) {
+          Pred->getInstList().pop_back();  // nuke uncond branch
+          new UnwindInst(Pred);            // Use unwind.
+          Changed = true;
+        }
+      } else if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
         if (II->getUnwindDest() == BB) {
           // Insert a new branch instruction before the invoke, because this
           // is now a fall through...
@@ -823,6 +830,7 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
           for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI!=E; ++PI)
             if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
               if (PBI->isConditional() && SafeToMergeTerminators(BI, PBI)) {
+                BasicBlock *PredBlock = *PI;
                 if (PBI->getSuccessor(0) == FalseDest ||
                     PBI->getSuccessor(1) == TrueDest) {
                   // Invert the predecessors condition test (xor it with true),
@@ -839,12 +847,12 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
 
                 if (PBI->getSuccessor(0) == TrueDest ||
                     PBI->getSuccessor(1) == FalseDest) {
-                  // Clone Cond into the predecessor basic block, and and the
+                  // Clone Cond into the predecessor basic block, and or/and the
                   // two conditions together.
                   Instruction *New = Cond->clone();
                   New->setName(Cond->getName());
                   Cond->setName(Cond->getName()+".old");
-                  (*PI)->getInstList().insert(PBI, New);
+                  PredBlock->getInstList().insert(PBI, New);
                   Instruction::BinaryOps Opcode =
                     PBI->getSuccessor(0) == TrueDest ?
                     Instruction::Or : Instruction::And;
@@ -853,11 +861,11 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
                                            New, "bothcond", PBI);
                   PBI->setCondition(NewCond);
                   if (PBI->getSuccessor(0) == BB) {
-                    AddPredecessorToBlock(TrueDest, *PI, BB);
+                    AddPredecessorToBlock(TrueDest, PredBlock, BB);
                     PBI->setSuccessor(0, TrueDest);
                   }
                   if (PBI->getSuccessor(1) == BB) {
-                    AddPredecessorToBlock(FalseDest, *PI, BB);
+                    AddPredecessorToBlock(FalseDest, PredBlock, BB);
                     PBI->setSuccessor(1, FalseDest);
                   }
                   return SimplifyCFG(BB) | 1;
@@ -918,7 +926,7 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
   }
 
   if (OnlySucc) {
-    DEBUG(std::cerr << "Merging: " << BB << "into: " << OnlyPred);
+    DEBUG(std::cerr << "Merging: " << *BB << "into: " << *OnlyPred);
     TerminatorInst *Term = OnlyPred->getTerminator();
 
     // Resolve any PHI nodes at the start of the block.  They are all