Provide a function that ensures MBB numbering is dense and inorder. This
authorChris Lattner <sabre@nondot.org>
Tue, 3 Oct 2006 19:18:57 +0000 (19:18 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 3 Oct 2006 19:18:57 +0000 (19:18 +0000)
can be used by MachineFunctionPasses who need this property.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30706 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/MachineFunction.h
lib/CodeGen/MachineFunction.cpp

index b06dd639228e0bbe2e249f0cb71c969e629a0f56..f264c91c11d4527bbea24cb365b585e69aac06bb 100644 (file)
@@ -224,8 +224,16 @@ public:
   }
 
   /// getNumBlockIDs - Return the number of MBB ID's allocated.
+  ///
   unsigned getNumBlockIDs() const { return MBBNumbering.size(); }
   
+  /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
+  /// recomputes them.  This guarantees that the MBB numbers are sequential,
+  /// dense, and match the ordering of the blocks within the function.  If a
+  /// specific MachineBasicBlock is specified, only that block and those after
+  /// it are renumbered.
+  void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
+  
   /// print - Print out the MachineFunction in a format suitable for debugging
   /// to the specified stream.
   ///
index 4a31ee5716c7606fa26cff4c034096e485eba03b..21f41b95b00cb20f9091da95dd698ada2fe0d3d7 100644 (file)
@@ -132,6 +132,53 @@ MachineFunction::~MachineFunction() {
   delete[] UsedPhysRegs;
 }
 
+
+/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
+/// recomputes them.  This guarantees that the MBB numbers are sequential,
+/// dense, and match the ordering of the blocks within the function.  If a
+/// specific MachineBasicBlock is specified, only that block and those after
+/// it are renumbered.
+void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
+  if (empty()) { MBBNumbering.clear(); return; }
+  MachineFunction::iterator MBBI, E = end();
+  if (MBB == 0)
+    MBBI = begin();
+  else
+    MBBI = MBB;
+  
+  // Figure out the block number this should have.
+  unsigned BlockNo = 0;
+  if (MBB != &front()) {
+    MachineFunction::iterator I = MBB;
+    --I;
+    BlockNo = I->getNumber()+1;
+  }
+  
+  for (; MBBI != E; ++MBBI, ++BlockNo) {
+    if (MBBI->getNumber() != (int)BlockNo) {
+      // Remove use of the old number.
+      if (MBBI->getNumber() != -1) {
+        assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
+               "MBB number mismatch!");
+        MBBNumbering[MBBI->getNumber()] = 0;
+      }
+      
+      // If BlockNo is already taken, set that block's number to -1.
+      if (MBBNumbering[BlockNo])
+        MBBNumbering[BlockNo]->setNumber(-1);
+
+      MBBNumbering[BlockNo] = MBBI;
+      MBBI->setNumber(BlockNo);
+    }
+  }    
+
+  // Okay, all the blocks are renumbered.  If we have compactified the block
+  // numbering, shrink MBBNumbering now.
+  assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
+  MBBNumbering.resize(BlockNo);
+}
+
+
 void MachineFunction::dump() const { print(std::cerr); }
 
 void MachineFunction::print(std::ostream &OS) const {