#include vector which we will need here soon
[oota-llvm.git] / lib / CodeGen / LiveVariables.cpp
index 81fdbe04c914d89c0ff762f4440f760e5c003ac5..2a9b70d3b01aace0b7a59d41efdddb089d3688e3 100644 (file)
 
 static RegisterAnalysis<LiveVariables> X("livevars", "Live Variable Analysis");
 
+const std::pair<MachineBasicBlock*, unsigned> &
+LiveVariables::getMachineBasicBlockInfo(MachineBasicBlock *MBB) const{
+  return BBMap.find(MBB->getBasicBlock())->second;
+}
+
+LiveVariables::VarInfo &LiveVariables::getVarInfo(unsigned RegIdx) {
+  assert(RegIdx >= MRegisterInfo::FirstVirtualRegister &&
+         "getVarInfo: not a virtual register!");
+  RegIdx -= MRegisterInfo::FirstVirtualRegister;
+  if (RegIdx >= VirtRegInfo.size()) {
+    if (RegIdx >= 2*VirtRegInfo.size())
+      VirtRegInfo.resize(RegIdx*2);
+    else
+      VirtRegInfo.resize(2*VirtRegInfo.size());
+  }
+  return VirtRegInfo[RegIdx];
+}
+
+
+
 void LiveVariables::MarkVirtRegAliveInBlock(VarInfo &VRInfo,
                                            const BasicBlock *BB) {
   const std::pair<MachineBasicBlock*,unsigned> &Info = BBMap.find(BB)->second;
@@ -88,12 +108,14 @@ void LiveVariables::HandlePhysRegUse(unsigned Reg, MachineInstr *MI) {
   if (PhysRegInfo[Reg]) {
     PhysRegInfo[Reg] = MI;
     PhysRegUsed[Reg] = true;
-  } else if (const unsigned *AliasSet = RegInfo->getAliasSet(Reg)) {
-    for (; unsigned NReg = AliasSet[0]; ++AliasSet)
-      if (MachineInstr *LastUse = PhysRegInfo[NReg]) {
-       PhysRegInfo[NReg] = MI;
-       PhysRegUsed[NReg] = true;
+  } else {
+    for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
+         *AliasSet; ++AliasSet) {
+      if (MachineInstr *LastUse = PhysRegInfo[*AliasSet]) {
+       PhysRegInfo[*AliasSet] = MI;
+       PhysRegUsed[*AliasSet] = true;
       }
+    }
   }
 }
 
@@ -104,15 +126,17 @@ void LiveVariables::HandlePhysRegDef(unsigned Reg, MachineInstr *MI) {
       RegistersKilled.insert(std::make_pair(LastUse, Reg));
     else
       RegistersDead.insert(std::make_pair(LastUse, Reg));
-  } else if (const unsigned *AliasSet = RegInfo->getAliasSet(Reg)) {
-    for (; unsigned NReg = AliasSet[0]; ++AliasSet)
-      if (MachineInstr *LastUse = PhysRegInfo[NReg]) {
-       if (PhysRegUsed[NReg])
-         RegistersKilled.insert(std::make_pair(LastUse, NReg));
+  } else {
+    for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
+         *AliasSet; ++AliasSet) {
+      if (MachineInstr *LastUse = PhysRegInfo[*AliasSet]) {
+       if (PhysRegUsed[*AliasSet])
+         RegistersKilled.insert(std::make_pair(LastUse, *AliasSet));
        else
-         RegistersDead.insert(std::make_pair(LastUse, NReg));
-       PhysRegInfo[NReg] = 0;  // Kill the aliased register
+         RegistersDead.insert(std::make_pair(LastUse, *AliasSet));
+       PhysRegInfo[*AliasSet] = 0;  // Kill the aliased register
       }
+    }
   }
   PhysRegInfo[Reg] = MI;
   PhysRegUsed[Reg] = false;
@@ -186,17 +210,16 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
        NumOperandsToProcess = 1;
 
       // Loop over implicit uses, using them.
-      if (const unsigned *ImplicitUses = MID.ImplicitUses)
-       for (unsigned i = 0; ImplicitUses[i]; ++i)
-         HandlePhysRegUse(ImplicitUses[i], MI);
+      for (const unsigned *ImplicitUses = MID.ImplicitUses;
+           *ImplicitUses; ++ImplicitUses)
+       HandlePhysRegUse(*ImplicitUses, MI);
 
       // Process all explicit uses...
       for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
        MachineOperand &MO = MI->getOperand(i);
        if (MO.opIsUse() || MO.opIsDefAndUse()) {
          if (MO.isVirtualRegister() && !MO.getVRegValueOrNull()) {
-           unsigned RegIdx = MO.getReg()-MRegisterInfo::FirstVirtualRegister;
-           HandleVirtRegUse(getVarInfo(RegIdx), MBB, MI);
+           HandleVirtRegUse(getVarInfo(MO.getReg()), MBB, MI);
          } else if (MO.isPhysicalRegister() && 
                      AllocatablePhysicalRegisters[MO.getReg()]) {
            HandlePhysRegUse(MO.getReg(), MI);
@@ -212,10 +235,9 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
       // Process all explicit defs...
       for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
        MachineOperand &MO = MI->getOperand(i);
-       if (MO.opIsDef() || MO.opIsDefAndUse()) {
+       if (MO.opIsDefOnly() || MO.opIsDefAndUse()) {
          if (MO.isVirtualRegister()) {
-           unsigned RegIdx = MO.getReg()-MRegisterInfo::FirstVirtualRegister;
-           VarInfo &VRInfo = getVarInfo(RegIdx);
+           VarInfo &VRInfo = getVarInfo(MO.getReg());
 
            assert(VRInfo.DefBlock == 0 && "Variable multiply defined!");
            VRInfo.DefBlock = MBB;                           // Created here...
@@ -245,8 +267,7 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
          if (MI->getOperand(i+1).getMachineBasicBlock() == MBB) {
            MachineOperand &MO = MI->getOperand(i);
            if (!MO.getVRegValueOrNull()) {
-             unsigned RegIdx = MO.getReg()-MRegisterInfo::FirstVirtualRegister;
-             VarInfo &VRInfo = getVarInfo(RegIdx);
+             VarInfo &VRInfo = getVarInfo(MO.getReg());
 
              // Only mark it alive only in the block we are representing...
              MarkVirtRegAliveInBlock(VRInfo, BB);
@@ -263,8 +284,6 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
        HandlePhysRegDef(i, 0);
   }
 
-  BBMap.clear();
-
   // Convert the information we have gathered into VirtRegInfo and transform it
   // into a form usable by RegistersKilled.
   //