Improvement to the previous fix: branch following a delay slot of
[oota-llvm.git] / lib / Target / SparcV9 / LiveVar / FunctionLiveVarInfo.cpp
index 297b64a93c7e29660fbea5c9b2fce337b84e78a9..764ec36f37faea3a07bcc129a2c696bb9515585d 100644 (file)
@@ -5,25 +5,32 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
-#include "BBLiveVar.h"
+#include "llvm/CodeGen/FunctionLiveVarInfo.h"
 #include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Support/CFG.h"
 #include "Support/PostOrderIterator.h"
 #include "Support/SetOperations.h"
 #include "Support/CommandLine.h"
-#include <iostream>
+#include "BBLiveVar.h"
 
-AnalysisID FunctionLiveVarInfo::ID(AnalysisID::create<FunctionLiveVarInfo>());
+static RegisterAnalysis<FunctionLiveVarInfo>
+X("livevar", "Live Variable Analysis");
 
 LiveVarDebugLevel_t DEBUG_LV;
 
-static cl::Enum<LiveVarDebugLevel_t> DEBUG_LV_opt(DEBUG_LV, "dlivevar", cl::Hidden,
-  "enable live-variable debugging information",
-  clEnumValN(LV_DEBUG_None   , "n", "disable debug output"),
-  clEnumValN(LV_DEBUG_Normal , "y", "enable debug output"),
-  clEnumValN(LV_DEBUG_Instr,   "i", "print live-var sets before/after every machine instrn"),
-  clEnumValN(LV_DEBUG_Verbose, "v", "print def, use sets for every instrn also"), 0);
+static cl::opt<LiveVarDebugLevel_t, true>
+DEBUG_LV_opt("dlivevar", cl::Hidden, cl::location(DEBUG_LV),
+             cl::desc("enable live-variable debugging information"),
+             cl::values(
+clEnumValN(LV_DEBUG_None   , "n", "disable debug output"),
+clEnumValN(LV_DEBUG_Normal , "y", "enable debug output"),
+clEnumValN(LV_DEBUG_Instr,   "i", "print live-var sets before/after "
+           "every machine instrn"),
+clEnumValN(LV_DEBUG_Verbose, "v", "print def, use sets for every instrn also"),
+                        0));
 
 
 
@@ -35,11 +42,17 @@ static cl::Enum<LiveVarDebugLevel_t> DEBUG_LV_opt(DEBUG_LV, "dlivevar", cl::Hidd
 const ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
   return BBLiveVar::GetFromBB(*BB)->getOutSet();
 }
+      ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB)       {
+  return BBLiveVar::GetFromBB(*BB)->getOutSet();
+}
 
 // gets InSet of a BB
 const ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
   return BBLiveVar::GetFromBB(*BB)->getInSet();
 }
+      ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB)       {
+  return BBLiveVar::GetFromBB(*BB)->getInSet();
+}
 
 
 //-----------------------------------------------------------------------------
@@ -66,31 +79,36 @@ bool FunctionLiveVarInfo::runOnFunction(Function &F) {
 // constructs BBLiveVars and init Def and In sets
 //-----------------------------------------------------------------------------
 
-void FunctionLiveVarInfo::constructBBs(const Function *M) {
-  unsigned int POId = 0;                // Reverse Depth-first Order ID
-  
-  for(po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
-      BBI != BBE; ++BBI, ++POId) { 
-    const BasicBlock &BB = **BBI;        // get the current BB 
+void FunctionLiveVarInfo::constructBBs(const Function *F) {
+  unsigned POId = 0;                // Reverse Depth-first Order ID
+  std::map<const BasicBlock*, unsigned> PONumbering;
 
+  for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
+      BBI != BBE; ++BBI)
+    PONumbering[*BBI] = POId++;
+
+  MachineFunction &MF = MachineFunction::get(F);
+  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
+    const BasicBlock &BB = *I->getBasicBlock();        // get the current BB 
     if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
 
-    // create a new BBLiveVar
-    BBLiveVar *LVBB = BBLiveVar::CreateOnBB(BB, POId);  
+    BBLiveVar *LVBB;
+    std::map<const BasicBlock*, unsigned>::iterator POI = PONumbering.find(&BB);
+    if (POI != PONumbering.end()) {
+      // create a new BBLiveVar
+      LVBB = BBLiveVar::CreateOnBB(BB, *I, POId);  
+    } else {
+      // The PO iterator does not discover unreachable blocks, but the random
+      // iterator later may access these blocks.  We must make sure to
+      // initialize unreachable blocks as well.  However, LV info is not correct
+      // for those blocks (they are not analyzed)
+      //
+      LVBB = BBLiveVar::CreateOnBB(BB, *I, ++POId);
+    }
     
     if (DEBUG_LV)
       LVBB->printAllSets();
   }
-
-  // Since the PO iterator does not discover unreachable blocks,
-  // go over the random iterator and init those blocks as well.
-  // However, LV info is not correct for those blocks (they are not
-  // analyzed)
-  //
-  for (Function::const_iterator BBRI = M->begin(), BBRE = M->end();
-       BBRI != BBRE; ++BBRI, ++POId)
-    if (!BBLiveVar::GetFromBB(*BBRI))                 // Not yet processed?
-      BBLiveVar::CreateOnBB(*BBRI, POId);
 }
 
 
@@ -135,12 +153,13 @@ void FunctionLiveVarInfo::releaseMemory() {
   M = 0;
 
   // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
-  // and entered into  MInst2LVSetBI and  MInst2LVSetAI (these are caches
-  // to return ValueSet's before/after a machine instruction quickly). It
-  // is sufficient to free up all ValueSet using only one cache since 
-  // both caches refer to the same sets
+  // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
+  // to return ValueSet's before/after a machine instruction quickly).
+  // We do not need to free up ValueSets in MInst2LVSetAI because it holds
+  // pointers to the same sets as in MInst2LVSetBI (for all instructions
+  // except the last one in a BB) or in BBLiveVar (for the last instruction).
   //
-  for (std::map<const MachineInstr*, const ValueSet*>::iterator
+  for (hash_map<const MachineInstr*, ValueSet*>::iterator
          MI = MInst2LVSetBI.begin(),
          ME = MInst2LVSetBI.end(); MI != ME; ++MI)
     delete MI->second;           // delete all ValueSets in  MInst2LVSetBI
@@ -168,30 +187,31 @@ void FunctionLiveVarInfo::releaseMemory() {
 //-----------------------------------------------------------------------------
 
 const ValueSet &
-FunctionLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
+FunctionLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MI,
                                               const BasicBlock *BB) {
-  if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
-    return *LVSet;                      // if found, just return the set
-  } else { 
-    calcLiveVarSetsForBB(BB);          // else, calc for all instrs in BB
-    return *MInst2LVSetBI[MInst];
+  ValueSet* &LVSet = MInst2LVSetBI[MI]; // ref. to map entry
+  if (LVSet == NULL && BB != NULL) {    // if not found and BB provided
+    calcLiveVarSetsForBB(BB);           // calc LVSet for all instrs in BB
+    assert(LVSet != NULL);
   }
+  return *LVSet;
 }
 
 
 //-----------------------------------------------------------------------------
 // Gives live variable information after a machine instruction
 //-----------------------------------------------------------------------------
+
 const ValueSet & 
 FunctionLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
                                              const BasicBlock *BB) {
 
-  if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
-    return *LVSet;                      // if found, just return the set
-  } else { 
-    calcLiveVarSetsForBB(BB);           // else, calc for all instrs in BB
-    return *MInst2LVSetAI[MI];
+  ValueSet* &LVSet = MInst2LVSetAI[MI]; // ref. to map entry
+  if (LVSet == NULL && BB != NULL) {    // if not found and BB provided 
+    calcLiveVarSetsForBB(BB);           // calc LVSet for all instrs in BB
+    assert(LVSet != NULL);
   }
+  return *LVSet;
 }
 
 // This function applies a machine instr to a live var set (accepts OutSet) and
@@ -203,26 +223,29 @@ FunctionLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
 static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
   for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
          OpE = MInst->end(); OpI != OpE; ++OpI) {
-    if (OpI.isDef())           // kill only if this operand is a def
-      LVS.erase(*OpI);         // this definition kills any uses
+    if (OpI.isDefOnly() || OpI.isDefAndUse()) // kill if this operand is a def
+      LVS.erase(*OpI);                        // this definition kills any uses
   }
 
   // do for implicit operands as well
   for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
-    if (MInst->implicitRefIsDefined(i))
+    if (MInst->getImplicitOp(i).opIsDefOnly() ||
+        MInst->getImplicitOp(i).opIsDefAndUse())
       LVS.erase(MInst->getImplicitRef(i));
   }
 
   for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
          OpE = MInst->end(); OpI != OpE; ++OpI) {
     if (!isa<BasicBlock>(*OpI))      // don't process labels
-      if (!OpI.isDef())              // add only if this operand is a use
+      // add only if this operand is a use
+      if (!OpI.isDefOnly() || OpI.isDefAndUse() )
         LVS.insert(*OpI);            // An operand is a use - so add to use set
   }
 
   // do for implicit operands as well
   for (unsigned i = 0, e = MInst->getNumImplicitRefs(); i != e; ++i)
-    if (!MInst->implicitRefIsDefined(i))
+    if (MInst->getImplicitOp(i).opIsUse() ||
+        MInst->getImplicitOp(i).opIsDefAndUse())
       LVS.insert(MInst->getImplicitRef(i));
 }
 
@@ -233,18 +256,21 @@ static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
 //-----------------------------------------------------------------------------
 
 void FunctionLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
-  const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
+  BBLiveVar *BBLV = BBLiveVar::GetFromBB(*BB);
+  assert(BBLV && "BBLiveVar annotation doesn't exist?");
+  const MachineBasicBlock &MIVec = BBLV->getMachineBasicBlock();
+  const MachineFunction &MF = MachineFunction::get(M);
+  const TargetMachine &TM = MF.getTarget();
 
   if (DEBUG_LV >= LV_DEBUG_Instr)
     std::cerr << "\n======For BB " << BB->getName()
               << ": Live var sets for instructions======\n";
   
-  ValueSet CurSet;
-  const ValueSet *SetAI = &getOutSetOfBB(BB);  // init SetAI with OutSet
-  set_union(CurSet, *SetAI);                   // CurSet now contains OutSet
+  ValueSet *SetAI = &getOutSetOfBB(BB);         // init SetAI with OutSet
+  ValueSet CurSet(*SetAI);                      // CurSet now contains OutSet
 
   // iterate over all the machine instructions in BB
-  for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
+  for (MachineBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
          MIE = MIVec.rend(); MII != MIE; ++MII) {  
     // MI is cur machine inst
     const MachineInstr *MI = *MII;  
@@ -252,11 +278,26 @@ void FunctionLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
     MInst2LVSetAI[MI] = SetAI;                 // record in After Inst map
 
     applyTranferFuncForMInst(CurSet, MI);      // apply the transfer Func
-    ValueSet *NewSet = new ValueSet();         // create a new set and
-    set_union(*NewSet, CurSet);                // copy the set after T/F to it
+    ValueSet *NewSet = new ValueSet(CurSet);   // create a new set with a copy
+                                               // of the set after T/F
     MInst2LVSetBI[MI] = NewSet;                // record in Before Inst map
 
+    // If the current machine instruction has delay slots, mark values
+    // used by this instruction as live before and after each delay slot
+    // instruction (After(MI) is the same as Before(MI+1) except for last MI).
+    if (unsigned DS = TM.getInstrInfo().getNumDelaySlots(MI->getOpCode())) {
+      MachineBasicBlock::const_iterator fwdMII = MII.base(); // ptr to *next* MI
+      for (unsigned i = 0; i < DS; ++i, ++fwdMII) {
+        assert(fwdMII != MIVec.end() && "Missing instruction in delay slot?");
+        MachineInstr* DelaySlotMI = *fwdMII;
+        if (! TM.getInstrInfo().isNop(DelaySlotMI->getOpCode())) {
+          set_union(*MInst2LVSetBI[DelaySlotMI], *NewSet);
+          if (i+1 == DS)
+            set_union(*MInst2LVSetAI[DelaySlotMI], *NewSet);
+        }
+      }
+    }
+
     if (DEBUG_LV >= LV_DEBUG_Instr) {
       std::cerr << "\nLive var sets before/after instruction " << *MI;
       std::cerr << "  Before: ";   printSet(*NewSet);  std::cerr << "\n";