Changes For Bug 352
[oota-llvm.git] / lib / Transforms / Utils / SimplifyCFG.cpp
index c3ed43d1f0cf20f4849a256c76a8cb6e80b1f811..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>
-#include <iostream>
 
 using namespace llvm;
 
@@ -211,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
@@ -765,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...