Refactor rewinding code for finding the first terminator of a basic
authorAlkis Evlogimenos <alkis@evlogimenos.com>
Mon, 23 Feb 2004 18:14:48 +0000 (18:14 +0000)
committerAlkis Evlogimenos <alkis@evlogimenos.com>
Mon, 23 Feb 2004 18:14:48 +0000 (18:14 +0000)
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

include/llvm/CodeGen/MachineBasicBlock.h
lib/CodeGen/MachineBasicBlock.cpp
lib/CodeGen/PHIElimination.cpp
lib/CodeGen/RegAllocLocal.cpp
lib/Target/X86/InstSelectSimple.cpp
lib/Target/X86/X86ISelSimple.cpp

index 44cab9e9e3fc552480c69890ca61bb09feee640f..0fec6a2a2bc1fbb22d3b672f782b8077c2d13c7d 100644 (file)
@@ -95,6 +95,11 @@ public:
   reverse_iterator       rend  ()       { return Insts.rend();   }
   const_reverse_iterator rend  () const { return Insts.rend();   }
 
+  /// getFirstTerminator - returns an iterator to the first terminator
+  /// instruction of this basic block. If a terminator does not exist,
+  /// it returns end()
+  iterator getFirstTerminator();
+
   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
   template<typename IT>
   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
index 9d65453edff2ef877d83aacfca51c4b8eeb83e84..89763ba4f3bca91e70cbd2af3dcaf79f5dbc4921 100644 (file)
@@ -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 @@ void ilist_traits<MachineInstr>::transferNodesFromList(
             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);
index 06305826e80e7e93ffea3e65a9b6287cbb104462..ea464c942324669fd7794786caf30406905c4c9d 100644 (file)
@@ -143,22 +143,7 @@ bool PNE::EliminatePHINodes(MachineFunction &MF, MachineBasicBlock &MBB) {
       // source path the PHI.
       MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
 
-      // Figure out where to insert the copy, which is at the end of the
-      // predecessor basic block, but before any terminator/branch
-      // instructions...
-      MachineBasicBlock::iterator I = opBlock.end();
-      if (I != opBlock.begin()) {  // Handle empty blocks
-        --I;
-        // must backtrack over ALL the branches in the previous block
-        while (MII.isTerminatorInstr(I->getOpcode()) &&
-               I != opBlock.begin())
-          --I;
-        
-        // move back to the first branch instruction so new instructions
-        // are inserted right in front of it and not in front of a non-branch
-        if (!MII.isTerminatorInstr(I->getOpcode()))
-          ++I;
-      }
+      MachineBasicBlock::iterator I = opBlock.getFirstTerminator();
       
       // Check to make sure we haven't already emitted the copy for this block.
       // This can happen because PHI nodes may have multiple entries for the
index 38e00733602c46c7469baac083c8036bf7f18e38..a3d66391cce4b70e106ebd9b08ae53c6019d52be 100644 (file)
@@ -649,11 +649,7 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
     }
   }
 
-  // Rewind the iterator to point to the first flow control instruction...
-  const TargetInstrInfo &TII = TM->getInstrInfo();
-  MI = MBB.end();
-  while (MI != MBB.begin() && TII.isTerminatorInstr((--MI)->getOpcode()));
-  if (MI != MBB.end()) ++MI;
+  MI = MBB.getFirstTerminator();
 
   // Spill all physical registers holding virtual registers now.
   for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
index 2675a04ccf714d10427b8b572d67dd9299a691f8..4ebffeb81be61c8a93b635624c3d5d6341a29d66 100644 (file)
@@ -679,7 +679,6 @@ static bool RequiresFPRegKill(const BasicBlock *BB) {
 //
 void ISel::InsertFPRegKills() {
   SSARegMap &RegMap = *F->getSSARegMap();
-  const TargetInstrInfo &TII = TM.getInstrInfo();
 
   for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
     for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
@@ -709,11 +708,7 @@ void ISel::InsertFPRegKills() {
     // it's not an unwind/return), insert the FP_REG_KILL instruction.
     if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
         RequiresFPRegKill(BB->getBasicBlock())) {
-      // Rewind past any terminator instructions that might exist.
-      MachineBasicBlock::iterator I = BB->end();
-      while (I != BB->begin() && TII.isTerminatorInstr((--I)->getOpcode()));
-      if (I != BB->end()) ++I;
-      BMI(BB, I, X86::FP_REG_KILL, 0);
+      BMI(BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
       ++NumFPKill;
     }
   }
index 2675a04ccf714d10427b8b572d67dd9299a691f8..4ebffeb81be61c8a93b635624c3d5d6341a29d66 100644 (file)
@@ -679,7 +679,6 @@ static bool RequiresFPRegKill(const BasicBlock *BB) {
 //
 void ISel::InsertFPRegKills() {
   SSARegMap &RegMap = *F->getSSARegMap();
-  const TargetInstrInfo &TII = TM.getInstrInfo();
 
   for (MachineFunction::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
     for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
@@ -709,11 +708,7 @@ void ISel::InsertFPRegKills() {
     // it's not an unwind/return), insert the FP_REG_KILL instruction.
     if (BB->getBasicBlock()->getTerminator()->getNumSuccessors() &&
         RequiresFPRegKill(BB->getBasicBlock())) {
-      // Rewind past any terminator instructions that might exist.
-      MachineBasicBlock::iterator I = BB->end();
-      while (I != BB->begin() && TII.isTerminatorInstr((--I)->getOpcode()));
-      if (I != BB->end()) ++I;
-      BMI(BB, I, X86::FP_REG_KILL, 0);
+      BMI(BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
       ++NumFPKill;
     }
   }