[llvm-commits] CVS: llvm/lib/CodeGen/RegAllocLinearScan.cpp

Chris Lattner lattner at cs.uiuc.edu
Mon Aug 22 09:55:34 PDT 2005



Changes in directory llvm/lib/CodeGen:

RegAllocLinearScan.cpp updated: 1.108 -> 1.109
---
Log message:

Speed up this loop a bit, based on some observations that Nate made, and
add some comments.  This loop really needs to be reevaluated!


---
Diffs of the changes:  (+34 -8)

 RegAllocLinearScan.cpp |   42 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 34 insertions(+), 8 deletions(-)


Index: llvm/lib/CodeGen/RegAllocLinearScan.cpp
diff -u llvm/lib/CodeGen/RegAllocLinearScan.cpp:1.108 llvm/lib/CodeGen/RegAllocLinearScan.cpp:1.109
--- llvm/lib/CodeGen/RegAllocLinearScan.cpp:1.108	Thu Apr 21 17:33:33 2005
+++ llvm/lib/CodeGen/RegAllocLinearScan.cpp	Mon Aug 22 11:55:22 2005
@@ -603,6 +603,8 @@
 unsigned RA::getFreePhysReg(LiveInterval* cur)
 {
   std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
+  unsigned MaxInactiveCount = 0;
+  
   for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
        i != e; ++i) {
     unsigned reg = i->first->reg;
@@ -610,19 +612,43 @@
            "Can only allocate virtual registers!");
     reg = vrm_->getPhys(reg);
     ++inactiveCounts[reg];
+    MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
   }
 
   const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
 
-  unsigned freeReg = 0;
-  for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_),
-       e = rc->allocation_order_end(*mf_); i != e; ++i) {
-    unsigned reg = *i;
-    if (prt_->isRegAvail(reg) &&
-        (!freeReg || inactiveCounts[freeReg] < inactiveCounts[reg]))
-        freeReg = reg;
+  unsigned FreeReg = 0;
+  unsigned FreeRegInactiveCount = 0;
+  
+  // Scan for the first available register.
+  TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
+  TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
+  for (; I != E; ++I)
+    if (prt_->isRegAvail(*I)) {
+      FreeReg = *I;
+      FreeRegInactiveCount = inactiveCounts[FreeReg];
+      break;
+    }
+  
+  // If there are no free regs, or if this reg has the max inactive count,
+  // return this register.
+  if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
+  
+  // Continue scanning the registers, looking for the one with the highest
+  // inactive count.  Alkis found that this reduced register pressure very
+  // slightly on X86 (in rev 1.94 of this file), though this should probably be
+  // reevaluated now.
+  for (; I != E; ++I) {
+    unsigned Reg = *I;
+    if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
+      FreeReg = Reg;
+      FreeRegInactiveCount = inactiveCounts[Reg];
+      if (FreeRegInactiveCount == MaxInactiveCount)
+        break;    // We found the one with the max inactive count.
+    }
   }
-  return freeReg;
+  
+  return FreeReg;
 }
 
 FunctionPass* llvm::createLinearScanRegisterAllocator() {






More information about the llvm-commits mailing list