Use live out sets for return values instead of imp_defs, which is cleaner and faster.
[oota-llvm.git] / lib / CodeGen / LiveVariables.cpp
index 630477d7b654cae1d87264ffc166e6ea84a90020..175b5fd7c1679655728e7da500c961430f093f75 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");
@@ -155,14 +156,21 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
   // 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);
+
+  // Mark live-in registers as live-in.
+  for (MachineFunction::liveinout_iterator I = MF.livein_begin(),
+         E = MF.livein_end(); I != E; ++I) {
+    assert(MRegisterInfo::isPhysicalRegister(*I) &&
+           "Cannot have a live-in virtual register!");
+    HandlePhysRegDef(*I, 0);
+  }
   
   // Calculate live variable information in depth first order on the CFG of the
   // function.  This guarantees that we will see the definition of a virtual
@@ -260,6 +268,18 @@ bool LiveVariables::runOnMachineFunction(MachineFunction &MF) {
       }
     }
     
+    // Finally, if the last block in the function is a return, make sure to mark
+    // it as using all of the live-out values in the function.
+    if (!MBB->empty() && TII.isReturn(MBB->back().getOpcode())) {
+      MachineInstr *Ret = &MBB->back();
+      for (MachineFunction::liveinout_iterator I = MF.liveout_begin(),
+             E = MF.liveout_end(); I != E; ++I) {
+        assert(MRegisterInfo::isPhysicalRegister(*I) &&
+               "Cannot have a live-in virtual register!");
+        HandlePhysRegUse(*I, Ret);
+      }
+    }
+
     // Loop over PhysRegInfo, killing any registers that are available at the
     // end of the basic block.  This also resets the PhysRegInfo map.
     for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
@@ -302,12 +322,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
+      }
     }
   }