Fix order of eval problem from when I refactored this into a function.
[oota-llvm.git] / lib / CodeGen / BranchFolding.cpp
index 0a6bd39ee6a984febf5db4128cf64a98d68718d6..a4b3b02ea4970d1984cb1dd10ee8c9ae58b38799 100644 (file)
@@ -1,10 +1,10 @@
 //===-- BranchFolding.cpp - Fold machine code branch instructions ---------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // This pass forwards branches to unconditional branches to make them branch
@@ -20,7 +20,7 @@
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetMachine.h"
-#include "Support/STLExtras.h"
+#include "llvm/ADT/STLExtras.h"
 using namespace llvm;
 
 namespace {
@@ -96,7 +96,7 @@ static void ReplaceUsesOfBlockWith(MachineBasicBlock *BB,
 
   // If BB falls through into Old, insert an unconditional branch to New.
   MachineFunction::iterator BBSucc = BB; ++BBSucc;
-  if (&*BBSucc == Old)
+  if (BBSucc != BB->getParent()->end() && &*BBSucc == Old)
     TII.insertGoto(*BB, *New);
 
   std::vector<MachineBasicBlock*> Succs(BB->succ_begin(), BB->succ_end());
@@ -158,7 +158,7 @@ bool BranchFolder::OptimizeBlock(MachineFunction::iterator MBB,
     assert(Br->getNumOperands() == 1 && Br->getOperand(0).isMachineBasicBlock()
            && "Uncond branch should take one MBB argument!");
     MachineBasicBlock *Dest = Br->getOperand(0).getMachineBasicBlock();
-    
+
     while (!MBB->pred_empty()) {
       MachineBasicBlock *Pred = *(MBB->pred_end()-1);
       ReplaceUsesOfBlockWith(Pred, MBB, Dest, TII);