Refactor rewinding code for finding the first terminator of a basic
block into MachineBasicBlock::getFirstTerminator().

This also fixes a bug in the implementation of the above in both
RegAllocLocal and InstrSched, where instructions where added after the
terminator if the basic block's only instruction was a terminator (it
shouldn't matter for RegAllocLocal since this case never occurs in
practice).


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11748 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/MachineBasicBlock.cpp b/lib/CodeGen/MachineBasicBlock.cpp
index 9d65453..89763ba 100644
--- a/lib/CodeGen/MachineBasicBlock.cpp
+++ b/lib/CodeGen/MachineBasicBlock.cpp
@@ -15,6 +15,8 @@
 #include "llvm/BasicBlock.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/Target/TargetInstrInfo.h"
+#include "llvm/Target/TargetMachine.h"
 #include "Support/LeakDetector.h"
 using namespace llvm;
 
@@ -56,6 +58,16 @@
             first->parent = toList.parent;
 }
 
+MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator()
+{
+  const TargetInstrInfo& TII = MachineFunction::get(
+    getBasicBlock()->getParent()).getTarget().getInstrInfo();
+  iterator I = end();
+  while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
+  if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
+  return I;
+}
+
 void MachineBasicBlock::dump() const
 {
     print(std::cerr);