Fix SingleSource/Regression/C/2005-05-06-LongLongSignedShift.c, we were not
[oota-llvm.git] / lib / CodeGen / LiveVariables.cpp
index 883b7813e7f132d80b3245b81f51cf057765fc4c..803161026012bf22c3d740791109f0ec7a5f5484 100644 (file)
@@ -31,8 +31,9 @@
 #include "llvm/Target/MRegisterInfo.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetMachine.h"
-#include "Support/DepthFirstIterator.h"
-#include "Support/STLExtras.h"
+#include "llvm/ADT/DepthFirstIterator.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Config/alloca.h"
 using namespace llvm;
 
 static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis");
@@ -59,7 +60,7 @@ void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
   // Check to see if this basic block is one of the killing blocks.  If so,
   // remove it...
   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
-    if (VRInfo.Kills[i].first == MBB) {
+    if (VRInfo.Kills[i]->getParent() == MBB) {
       VRInfo.Kills.erase(VRInfo.Kills.begin()+i);  // Erase entry
       break;
     }
@@ -82,27 +83,28 @@ void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
 
 void LiveVariables::HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
                                      MachineInstr *MI) {
+  assert(VRInfo.DefInst && "Register use before def!");
+
   // Check to see if this basic block is already a kill block...
-  if (!VRInfo.Kills.empty() && VRInfo.Kills.back().first == MBB) {
+  if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
     // Yes, this register is killed in this basic block already.  Increase the
     // live range by updating the kill instruction.
-    VRInfo.Kills.back().second = MI;
+    VRInfo.Kills.back() = MI;
     return;
   }
 
 #ifndef NDEBUG
   for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
-    assert(VRInfo.Kills[i].first != MBB && "entry should be at end!");
+    assert(VRInfo.Kills[i]->getParent() != MBB && "entry should be at end!");
 #endif
 
   assert(MBB != VRInfo.DefInst->getParent() && 
          "Should have kill for defblock!");
 
   // Add a new kill entry for this basic block.
-  VRInfo.Kills.push_back(std::make_pair(MBB, MI));
+  VRInfo.Kills.push_back(MI);
 
   // Update all dominating blocks to mark them known live.
-  const BasicBlock *BB = MBB->getBasicBlock();
   for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
          E = MBB->pred_end(); PI != E; ++PI)
     MarkVirtRegAliveInBlock(VRInfo, *PI);
@@ -148,29 +150,16 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
   RegInfo = MF.getTarget().getRegisterInfo();
   assert(RegInfo && "Target doesn't have register information?");
 
-  // First time though, initialize AllocatablePhysicalRegisters for the target
-  if (AllocatablePhysicalRegisters.empty()) {
-    // Make space, initializing to false...
-    AllocatablePhysicalRegisters.resize(RegInfo->getNumRegs());
-
-    // Loop over all of the register classes...
-    for (MRegisterInfo::regclass_iterator RCI = RegInfo->regclass_begin(),
-           E = RegInfo->regclass_end(); RCI != E; ++RCI)
-      // Loop over all of the allocatable registers in the function...
-      for (TargetRegisterClass::iterator I = (*RCI)->allocation_order_begin(MF),
-             E = (*RCI)->allocation_order_end(MF); I != E; ++I)
-        AllocatablePhysicalRegisters[*I] = true;  // The reg is allocatable!
-  }
+  AllocatablePhysicalRegisters = RegInfo->getAllocatableSet(MF);
 
   // PhysRegInfo - Keep track of which instruction was the last use of a
   // physical register.  This is a purely local property, because all physical
   // register references as presumed dead across basic blocks.
   //
-  MachineInstr *PhysRegInfoA[RegInfo->getNumRegs()];
-  bool          PhysRegUsedA[RegInfo->getNumRegs()];
-  std::fill(PhysRegInfoA, PhysRegInfoA+RegInfo->getNumRegs(), (MachineInstr*)0);
-  PhysRegInfo = PhysRegInfoA;
-  PhysRegUsed = PhysRegUsedA;
+  PhysRegInfo = (MachineInstr**)alloca(sizeof(MachineInstr*) * 
+                                       RegInfo->getNumRegs());
+  PhysRegUsed = (bool*)alloca(sizeof(bool)*RegInfo->getNumRegs());
+  std::fill(PhysRegInfo, PhysRegInfo+RegInfo->getNumRegs(), (MachineInstr*)0);
 
   /// Get some space for a respectable number of registers...
   VirtRegInfo.resize(64);
@@ -233,7 +222,8 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
 
             assert(VRInfo.DefInst == 0 && "Variable multiply defined!");
             VRInfo.DefInst = MI;
-            VRInfo.Kills.push_back(std::make_pair(MBB, MI)); // Defaults to dead
+            // Defaults to dead
+            VRInfo.Kills.push_back(MI);
           } else if (MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
                      AllocatablePhysicalRegisters[MO.getReg()]) {
             HandlePhysRegDef(MO.getReg(), MI);
@@ -282,12 +272,12 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
   //
   for (unsigned i = 0, e = VirtRegInfo.size(); i != e; ++i)
     for (unsigned j = 0, e = VirtRegInfo[i].Kills.size(); j != e; ++j) {
-      if (VirtRegInfo[i].Kills[j].second == VirtRegInfo[i].DefInst)
-        RegistersDead.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
+      if (VirtRegInfo[i].Kills[j] == VirtRegInfo[i].DefInst)
+        RegistersDead.insert(std::make_pair(VirtRegInfo[i].Kills[j],
                              i + MRegisterInfo::FirstVirtualRegister));
 
       else
-        RegistersKilled.insert(std::make_pair(VirtRegInfo[i].Kills[j].second,
+        RegistersKilled.insert(std::make_pair(VirtRegInfo[i].Kills[j],
                                i + MRegisterInfo::FirstVirtualRegister));
     }
 
@@ -312,12 +302,20 @@ void LiveVariables::instructionChanged(MachineInstr *OldMI,
   // the instruction.
   for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
     MachineOperand &MO = OldMI->getOperand(i);
-    if (MO.isRegister() && MO.isDef() && MO.getReg() &&
+    if (MO.isRegister() && MO.getReg() &&
         MRegisterInfo::isVirtualRegister(MO.getReg())) {
       unsigned Reg = MO.getReg();
       VarInfo &VI = getVarInfo(Reg);
-      if (VI.DefInst == OldMI)
-        VI.DefInst = NewMI;
+      if (MO.isDef()) {
+        // Update the defining instruction.
+        if (VI.DefInst == OldMI)
+          VI.DefInst = NewMI;
+      }
+      if (MO.isUse()) {
+        // If this is a kill of the value, update the VI kills list.
+        if (VI.removeKill(OldMI))
+          VI.Kills.push_back(NewMI);   // Yes, there was a kill of it
+      }
     }
   }