Fix problems linking against the reoptimizer; _llvm_regAllocState must have
[oota-llvm.git] / lib / CodeGen / RegAllocLocal.cpp
index f6eacda9c45be9ee7f9f68b15c8ac8654f8b0d91..8d19b694715412c720d1f5b65ec481b2b46e2adb 100644 (file)
@@ -1,4 +1,11 @@
 //===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
+// 
+//                     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 register allocator allocates registers to a basic block at a time,
 // attempting to keep values in registers and reusing registers as appropriate.
@@ -22,7 +29,7 @@
 namespace {
   Statistic<> NumSpilled ("ra-local", "Number of registers spilled");
   Statistic<> NumReloaded("ra-local", "Number of registers reloaded");
-  cl::opt<bool> DisableKill("no-kill", cl::Hidden, 
+  cl::opt<bool> DisableKill("disable-kill", cl::Hidden, 
                             cl::desc("Disable register kill in local-ra"));
 
   class RA : public MachineFunctionPass {
@@ -120,9 +127,10 @@ namespace {
     ///
     bool areRegsEqual(unsigned R1, unsigned R2) const {
       if (R1 == R2) return true;
-      if (const unsigned *AliasSet = RegInfo->getAliasSet(R2))
-        for (unsigned i = 0; AliasSet[i]; ++i)
-          if (AliasSet[i] == R1) return true;
+      for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
+           *AliasSet; ++AliasSet) {
+        if (*AliasSet == R1) return true;
+      }
       return false;
     }
 
@@ -143,10 +151,12 @@ namespace {
                       unsigned VirtReg, unsigned PhysReg);
 
     /// spillPhysReg - This method spills the specified physical register into
-    /// the virtual register slot associated with it.
+    /// the virtual register slot associated with it.  If OnlyVirtRegs is set to
+    /// true, then the request is ignored if the physical register does not
+    /// contain a virtual register.
     ///
     void spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
-                      unsigned PhysReg);
+                      unsigned PhysReg, bool OnlyVirtRegs = false);
 
     /// assignVirtToPhysReg - This method updates local state so that we know
     /// that PhysReg is the proper container for VirtReg now.  The physical
@@ -232,28 +242,26 @@ void RA::removePhysReg(unsigned PhysReg) {
 ///
 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
                       unsigned VirtReg, unsigned PhysReg) {
-
-  DEBUG(std::cerr << "  Spilling register " << RegInfo->getName(PhysReg));
-  if (VirtReg == 0) {
-    DEBUG(std::cerr << " which corresponds to no vreg, "
-                    << "must be spurious physreg: ignoring (WARNING)\n");
-  } else {
-    DEBUG(std::cerr << " containing %reg" << VirtReg;
-          if (!isVirtRegModified(VirtReg))
-           std::cerr << " which has not been modified, so no store necessary!");
-
-    // Otherwise, there is a virtual register corresponding to this physical
-    // register.  We only need to spill it into its stack slot if it has been
-    // modified.
-    if (isVirtRegModified(VirtReg)) {
-      const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
-      int FrameIndex = getStackSpaceFor(VirtReg, RC);
-      DEBUG(std::cerr << " to stack slot #" << FrameIndex);
-      RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
-      ++NumSpilled;   // Update statistics
-    }
-    Virt2PhysRegMap.erase(VirtReg);   // VirtReg no longer available
+  if (!VirtReg && DisableKill) return;
+  assert(VirtReg && "Spilling a physical register is illegal!"
+         " Must not have appropriate kill for the register or use exists beyond"
+         " the intended one.");
+  DEBUG(std::cerr << "  Spilling register " << RegInfo->getName(PhysReg);
+        std::cerr << " containing %reg" << VirtReg;
+        if (!isVirtRegModified(VirtReg))
+        std::cerr << " which has not been modified, so no store necessary!");
+
+  // Otherwise, there is a virtual register corresponding to this physical
+  // register.  We only need to spill it into its stack slot if it has been
+  // modified.
+  if (isVirtRegModified(VirtReg)) {
+    const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
+    int FrameIndex = getStackSpaceFor(VirtReg, RC);
+    DEBUG(std::cerr << " to stack slot #" << FrameIndex);
+    RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
+    ++NumSpilled;   // Update statistics
   }
+  Virt2PhysRegMap.erase(VirtReg);   // VirtReg no longer available
 
   DEBUG(std::cerr << "\n");
   removePhysReg(PhysReg);
@@ -261,20 +269,25 @@ void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
 
 
 /// spillPhysReg - This method spills the specified physical register into the
-/// virtual register slot associated with it.
+/// virtual register slot associated with it.  If OnlyVirtRegs is set to true,
+/// then the request is ignored if the physical register does not contain a
+/// virtual register.
 ///
 void RA::spillPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
-                      unsigned PhysReg) {
+                      unsigned PhysReg, bool OnlyVirtRegs) {
   std::map<unsigned, unsigned>::iterator PI = PhysRegsUsed.find(PhysReg);
   if (PI != PhysRegsUsed.end()) {             // Only spill it if it's used!
-    spillVirtReg(MBB, I, PI->second, PhysReg);
-  } else if (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg)) {
+    if (PI->second || !OnlyVirtRegs)
+      spillVirtReg(MBB, I, PI->second, PhysReg);
+  } else {
     // If the selected register aliases any other registers, we must make
     // sure that one of the aliases isn't alive...
-    for (unsigned i = 0; AliasSet[i]; ++i) {
-      PI = PhysRegsUsed.find(AliasSet[i]);
+    for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
+         *AliasSet; ++AliasSet) {
+      PI = PhysRegsUsed.find(*AliasSet);
       if (PI != PhysRegsUsed.end())     // Spill aliased register...
-       spillVirtReg(MBB, I, PI->second, AliasSet[i]);
+        if (PI->second || !OnlyVirtRegs)
+          spillVirtReg(MBB, I, PI->second, *AliasSet);
     }
   }
 }
@@ -304,10 +317,10 @@ bool RA::isPhysRegAvailable(unsigned PhysReg) const {
 
   // If the selected register aliases any other allocated registers, it is
   // not free!
-  if (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg))
-    for (unsigned i = 0; AliasSet[i]; ++i)
-      if (PhysRegsUsed.count(AliasSet[i])) // Aliased register in use?
-        return false;                      // Can't use this reg then.
+  for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
+       *AliasSet; ++AliasSet)
+    if (PhysRegsUsed.count(*AliasSet)) // Aliased register in use?
+      return false;                    // Can't use this reg then.
   return true;
 }
 
@@ -396,19 +409,28 @@ unsigned RA::getReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
              "Couldn't find a register of the appropriate class!");
       
       unsigned R = PhysRegsUseOrder[i];
-      // If the current register is compatible, use it.
-      if (RegInfo->getRegClass(R) == RC) {
-       PhysReg = R;
-       break;
-      } else {
-       // If one of the registers aliased to the current register is
-       // compatible, use it.
-       if (const unsigned *AliasSet = RegInfo->getAliasSet(R))
-         for (unsigned a = 0; AliasSet[a]; ++a)
-           if (RegInfo->getRegClass(AliasSet[a]) == RC) {
-             PhysReg = AliasSet[a];    // Take an aliased register
-             break;
-           }
+
+      // We can only use this register if it holds a virtual register (ie, it
+      // can be spilled).  Do not use it if it is an explicitly allocated
+      // physical register!
+      assert(PhysRegsUsed.count(R) &&
+             "PhysReg in PhysRegsUseOrder, but is not allocated?");
+      if (PhysRegsUsed[R]) {
+        // If the current register is compatible, use it.
+        if (RegInfo->getRegClass(R) == RC) {
+          PhysReg = R;
+          break;
+        } else {
+          // If one of the registers aliased to the current register is
+          // compatible, use it.
+          for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
+               *AliasSet; ++AliasSet) {
+            if (RegInfo->getRegClass(*AliasSet) == RC) {
+              PhysReg = *AliasSet;    // Take an aliased register
+              break;
+            }
+          }
+        }
       }
     }
 
@@ -473,11 +495,11 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
 
     // Loop over the implicit uses, making sure that they are at the head of the
     // use order list, so they don't get reallocated.
-    if (const unsigned *ImplicitUses = TID.ImplicitUses)
-      for (unsigned i = 0; ImplicitUses[i]; ++i)
-        MarkPhysRegRecentlyUsed(ImplicitUses[i]);
+    for (const unsigned *ImplicitUses = TID.ImplicitUses;
+         *ImplicitUses; ++ImplicitUses)
+        MarkPhysRegRecentlyUsed(*ImplicitUses);
 
-    // Get the used operands into registers.  This has the potiential to spill
+    // Get the used operands into registers.  This has the potential to spill
     // incoming values if we are out of registers.  Note that we completely
     // ignore physical register uses here.  We assume that if an explicit
     // physical register is referenced by the instruction, that it is guaranteed
@@ -510,12 +532,7 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
         if (PhysReg) {
           DEBUG(std::cerr << "  Last use of " << RegInfo->getName(PhysReg)
                       << "[%reg" << VirtReg <<"], removing it from live set\n");
-          // If the physical register was used, but there was no definition of
-          // the physical register (we are reading garbage), Live Variables will
-          // tell us that this is the last use of the register even though we
-          // don't know of anything in the register.  No need to remove it.
-          if (VirtReg != PhysReg || PhysRegsUsed.count(PhysReg))
-            removePhysReg(PhysReg);
+          removePhysReg(PhysReg);
         }
       }
     }
@@ -527,7 +544,7 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
            MI->getOperand(i).opIsDefAndUse()) &&
           MI->getOperand(i).isPhysicalRegister()) {
         unsigned Reg = MI->getOperand(i).getAllocatedRegNum();
-        spillPhysReg(MBB, I, Reg);        // Spill any existing value in the reg
+        spillPhysReg(MBB, I, Reg, true);  // Spill any existing value in the reg
         PhysRegsUsed[Reg] = 0;            // It is free and reserved now
         PhysRegsUseOrder.push_back(Reg);
       }
@@ -614,8 +631,10 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
 
   // Spill all physical registers holding virtual registers now.
   while (!PhysRegsUsed.empty())
-    spillVirtReg(MBB, I, PhysRegsUsed.begin()->second,
-                 PhysRegsUsed.begin()->first);
+    if (unsigned VirtReg = PhysRegsUsed.begin()->second)
+      spillVirtReg(MBB, I, VirtReg, PhysRegsUsed.begin()->first);
+    else
+      removePhysReg(PhysRegsUsed.begin()->first);
 
   for (std::map<unsigned, unsigned>::iterator I = Virt2PhysRegMap.begin(),
          E = Virt2PhysRegMap.end(); I != E; ++I)
@@ -623,7 +642,11 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
               << I->second << "\n";
 
   assert(Virt2PhysRegMap.empty() && "Virtual registers still in phys regs?");
-  assert(PhysRegsUseOrder.empty() && "Physical regs still allocated?");
+  
+  // Clear any physical register which appear live at the end of the basic
+  // block, but which do not hold any virtual registers.  e.g., the stack
+  // pointer.
+  PhysRegsUseOrder.clear();
 }
 
 
@@ -648,6 +671,6 @@ bool RA::runOnMachineFunction(MachineFunction &Fn) {
   return true;
 }
 
-Pass *createLocalRegisterAllocator() {
+FunctionPass *createLocalRegisterAllocator() {
   return new RA();
 }