Replaced uses of deprecated `MachineFunction::get(BasicBlock *BB)'.
[oota-llvm.git] / lib / Target / SparcV9 / LiveVar / BBLiveVar.cpp
index aeb7f9184c55d139ddbbb2369d8b2eec01eaeef2..4b07ebc6323bb4fb346e317b91270fba1ba3243e 100644 (file)
-#include "llvm/Analysis/LiveVar/BBLiveVar.h"
-
-
-/********************* Implementation **************************************/
-
-BBLiveVar::BBLiveVar( const BasicBlock* baseBB, unsigned int RdfoId) 
-                      : DefSet(),  InSet(), OutSet(), PhiArgMap() {  
-    BaseBB = baseBB;   
-    InSetChanged = OutSetChanged = false;
-    POId = RdfoId;
+//===-- BBLiveVar.cpp - Live Variable Analysis for a BasicBlock -----------===//
+//
+// This is a wrapper class for BasicBlock which is used by live var analysis.
+//
+//===----------------------------------------------------------------------===//
+
+#include "BBLiveVar.h"
+#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
+#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/Support/CFG.h"
+#include "Support/SetOperations.h"
+
+/// BROKEN: Should not include sparc stuff directly into here
+#include "../../Target/Sparc/SparcInternals.h"  //  Only for PHI defn
+
+using std::cerr;
+
+static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
+
+BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock &BB, MachineBasicBlock &MBB,
+                                 unsigned POID) {
+  BBLiveVar *Result = new BBLiveVar(BB, MBB, POID);
+  BB.addAnnotation(Result);
+  return Result;
 }
 
+BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock &BB) {
+  return (BBLiveVar*)BB.getAnnotation(AID);
+}
 
+void BBLiveVar::RemoveFromBB(const BasicBlock &BB) {
+  bool Deleted = BB.deleteAnnotation(AID);
+  assert(Deleted && "BBLiveVar annotation did not exist!");
+}
 
-void BBLiveVar::calcDefUseSets()  // caluculates def and use sets for each BB
-{
-                                                // instructions in basic block 
-  const BasicBlock::InstListType&  InstListInBB = BaseBB->getInstList();   
-
-  BasicBlock::InstListType::const_reverse_iterator 
-    InstIterator = InstListInBB.rbegin();  // get the iterator for instructions
 
-                                     // iterate over all the instructions in BB
-  for( ; InstIterator != InstListInBB.rend(); InstIterator++) {  
+BBLiveVar::BBLiveVar(const BasicBlock &bb, MachineBasicBlock &mbb, unsigned id)
+  : Annotation(AID), BB(bb), MBB(mbb), POID(id) {
+  InSetChanged = OutSetChanged = false;
 
-    const Instruction * Inst  = *InstIterator;     // Inst is the current instr
-    assert(Inst);
+  calcDefUseSets();
+}
 
-    if( Inst->isDefinition() ) {  // add to Defs only if this instr is a def
-  
-      DefSet.add( Inst );   // nstruction is a def - so add to def set
-      InSet.remove( Inst);  // this definition kills any uses
-      InSetChanged = true; 
-      //cout << " adding inst to def "; printValue( Inst ); cout << endl;
+//-----------------------------------------------------------------------------
+// calculates def and use sets for each BB
+// There are two passes over operands of a machine instruction. This is
+// because, we can have instructions like V = V + 1, since we no longer
+// assume single definition.
+//-----------------------------------------------------------------------------
+
+void BBLiveVar::calcDefUseSets() {
+  // iterate over all the machine instructions in BB
+  for (MachineBasicBlock::const_reverse_iterator MII = MBB.rbegin(),
+         MIE = MBB.rend(); MII != MIE; ++MII) {
+    const MachineInstr *MI = *MII;
+    
+    if (DEBUG_LV >= LV_DEBUG_Verbose) {
+      cerr << " *Iterating over machine instr ";
+      MI->dump();
+      cerr << "\n";
     }
 
-    Instruction::op_const_iterator 
-      OpI = Inst->op_begin();                // get iterator for operands
+    // iterate over  MI operands to find defs
+    for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
+         OpI != OpE; ++OpI)
+      if (OpI.isDef())      // add to Defs only if this operand is a def
+       addDef(*OpI);
 
-    bool IsPhi=( Inst->getOpcode() == Instruction::PHINode );  // Is this a phi
+    // do for implicit operands as well
+    for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
+      if (MI->implicitRefIsDefined(i))
+       addDef(MI->getImplicitRef(i));
+    
+    // iterate over MI operands to find uses
+    for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
+         OpI != OpE; ++OpI) {
+      const Value *Op = *OpI;
+
+      if (isa<BasicBlock>(Op))
+       continue;             // don't process labels
+
+      if (!OpI.isDef() || OpI.isDefAndUse()) {
+                                // add to Uses only if this operand is a use
+        //
+        // *** WARNING: The following code for handling dummy PHI machine
+        //     instructions is untested.  The previous code was broken and I
+        //     fixed it, but it turned out to be unused as long as Phi
+        //     elimination is performed during instruction selection.
+        // 
+        // Put Phi operands in UseSet for the incoming edge, not node.
+        // They must not "hide" later defs, and must be handled specially
+        // during set propagation over the CFG.
+       if (MI->getOpCode() == PHI) {         // for a phi node
+          const Value *ArgVal = Op;
+         const BasicBlock *PredBB = cast<BasicBlock>(*++OpI); // next ptr is BB
+         
+         PredToEdgeInSetMap[PredBB].insert(ArgVal); 
+         
+         if (DEBUG_LV >= LV_DEBUG_Verbose)
+           cerr << "   - phi operand " << RAV(ArgVal) << " came from BB "
+                 << RAV(PredBB) << "\n";
+       } // if( IsPhi )
+        else {
+          // It is not a Phi use: add to regular use set and remove later defs.
+          addUse(Op);
+        }
+      } // if a use
+    } // for all operands
+
+    // do for implicit operands as well
+    for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
+      assert(MI->getOpCode() != PHI && "Phi cannot have implicit opeands");
+      const Value *Op = MI->getImplicitRef(i);
+
+      if (Op->getType() == Type::LabelTy)             // don't process labels
+       continue;
+
+      if (!MI->implicitRefIsDefined(i) || MI->implicitRefIsDefinedAndUsed(i) )
+       addUse(Op);
+    }
+  } // for all machine instructions
+} 
 
-    for(int OpNum=0 ; OpI != Inst->op_end() ; OpI++) { // iterate over operands
 
-      if ( ((*OpI)->getType())->isLabelType() ) 
-       continue;                                      // don't process labels 
+       
+//-----------------------------------------------------------------------------
+// To add an operand which is a def
+//-----------------------------------------------------------------------------
+void BBLiveVar::addDef(const Value *Op) {
+  DefSet.insert(Op);     // operand is a def - so add to def set
+  InSet.erase(Op);       // this definition kills any later uses
+  InSetChanged = true; 
+
+  if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << "  +Def: " << RAV(Op) << "\n";
+}
 
-      InSet.add( *OpI );      // An operand is a use - so add to use set
-      OutSet.remove( *OpI );  // remove if there is a definition below this use
 
-      if( IsPhi ) {           // for a phi node
-                              // put args into the PhiArgMap
-       PhiArgMap[ *OpI ] = ((PHINode *) Inst )->getIncomingBlock( OpNum++ ); 
-       assert( PhiArgMap[ *OpI ] );
-       //cout << " Phi operand "; printValue( *OpI ); 
-       //cout  << " came from BB "; printValue(PhiArgMap[*OpI]); cout<<endl;
-      }
+//-----------------------------------------------------------------------------
+// To add an operand which is a use
+//-----------------------------------------------------------------------------
+void  BBLiveVar::addUse(const Value *Op) {
+  InSet.insert(Op);   // An operand is a use - so add to use set
+  DefSet.erase(Op);   // remove if there is a def below this use
+  InSetChanged = true; 
 
-      InSetChanged = true; 
-      //cout << " adding operand to use "; printValue( *OpI ); cout << endl;
-    }
-    
-  }
-} 
-       
+  if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << "   Use: " << RAV(Op) << "\n";
+}
 
 
-bool BBLiveVar::applyTransferFunc() // calculates the InSet in terms of OutSet 
-{
+//-----------------------------------------------------------------------------
+// Applies the transfer function to a basic block to produce the InSet using
+// the OutSet. 
+//-----------------------------------------------------------------------------
 
+bool BBLiveVar::applyTransferFunc() {
   // IMPORTANT: caller should check whether the OutSet changed 
   //           (else no point in calling)
 
-  LiveVarSet OutMinusDef;                      // set to hold (Out[B] - Def[B])
-  OutMinusDef.setDifference( &OutSet, &DefSet);
-  InSetChanged = InSet.setUnion( &OutMinusDef );
+  ValueSet OutMinusDef = set_difference(OutSet, DefSet);
+  InSetChanged = set_union(InSet, OutMinusDef);
  
-  OutSetChanged = false;    // no change to OutSet since transfer func applied
-
+  OutSetChanged = false;      // no change to OutSet since transf func applied
   return InSetChanged;
 }
 
 
+//-----------------------------------------------------------------------------
+// calculates Out set using In sets of the successors
+//-----------------------------------------------------------------------------
 
-                        // calculates Out set using In sets of the predecessors
-bool BBLiveVar::setPropagate( LiveVarSet *const OutSet, 
-                             const LiveVarSet *const InSet, 
-                             const BasicBlock *const PredBB) {
-
-  LiveVarSet::const_iterator InIt;
-  pair<LiveVarSet::iterator, bool> result;
-  bool changed = false;
-  const BasicBlock *PredBBOfPhiArg;
-
-                                               // for all all elements in InSet
-  for( InIt = InSet->begin() ; InIt != InSet->end(); InIt++) {  
-    PredBBOfPhiArg =  PhiArgMap[ *InIt ];
-
-                        // if this var is not a phi arg or it came from this BB
-    if( !PredBBOfPhiArg || PredBBOfPhiArg == PredBB) {  
-      result = OutSet->insert( *InIt );               // insert to this set
-      if( result.second == true) changed = true;
-    }
-  }
-
-  return changed;
+bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet, 
+                             const BasicBlock *PredBB) {
+  bool Changed = false;
+  
+  // merge all members of InSet into OutSet of the predecessor
+  for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
+       InIt != InE; ++InIt)
+    if ((OutSet->insert(*InIt)).second)
+      Changed = true;
+  
+  // 
+  //**** WARNING: The following code for handling dummy PHI machine
+  //     instructions is untested.  See explanation above.
+  // 
+  // then merge all members of the EdgeInSet for the predecessor into the OutSet
+  const ValueSet& EdgeInSet = PredToEdgeInSetMap[PredBB];
+  for (ValueSet::const_iterator InIt = EdgeInSet.begin(), InE = EdgeInSet.end();
+       InIt != InE; ++InIt)
+    if ((OutSet->insert(*InIt)).second)
+      Changed = true;
+  // 
+  //****
+  
+  return Changed;
 } 
 
 
+//-----------------------------------------------------------------------------
+// propogates in set to OutSets of PREDECESSORs
+//-----------------------------------------------------------------------------
 
-                                // propogates in set to OutSets of PREDECESSORs
-bool BBLiveVar::applyFlowFunc(BBToBBLiveVarMapType LVMap) 
-{
-
+bool BBLiveVar::applyFlowFunc() {
   // IMPORTANT: caller should check whether inset changed 
   //            (else no point in calling)
+  
+  // If this BB changed any OutSets of preds whose POID is lower, than we need
+  // another iteration...
+  //
+  bool needAnotherIt = false;  
 
-  bool needAnotherIt= false;  // did this BB change any OutSets of pred.s 
-                              // whose POId is lower
-
-
-  cfg::pred_const_iterator PredBBI = cfg::pred_begin(BaseBB);
-
-  for( ; PredBBI != cfg::pred_end(BaseBB) ; PredBBI++) {
-    assert( *PredBBI );                 // assert that the predecessor is valid
-    BBLiveVar  *PredLVBB = LVMap[*PredBBI];
+  for (pred_const_iterator PI = pred_begin(&BB), PE = pred_end(&BB);
+       PI != PE ; ++PI) {
+    BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(**PI);
 
-                                                               // do set union
-    if(  setPropagate( &(PredLVBB->OutSet), &InSet, *PredBBI ) == true) {  
+    // do set union
+    if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {  
       PredLVBB->OutSetChanged = true;
 
-      if( PredLVBB->getPOId() <= POId) // if the predec POId is lower than mine
+      // if the predec POID is lower than mine
+      if (PredLVBB->getPOId() <= POID)
        needAnotherIt = true;   
     }
-  } // for
+  }  // for
 
   return needAnotherIt;
-
 }
 
 
 
+// ----------------- Methods For Debugging (Printing) -----------------
 
-
-/* ----------------- Methods For Debugging (Printing) ----------------- */
-
-void BBLiveVar::printAllSets() const
-{
-  cout << "Defs: ";   DefSet.printSet();  cout << endl;
-  cout << "In: ";   InSet.printSet();  cout << endl;
-  cout << "Out: ";   OutSet.printSet();  cout << endl;
+void BBLiveVar::printAllSets() const {
+  cerr << "  Defs: "; printSet(DefSet);  cerr << "\n";
+  cerr << "  In: ";  printSet(InSet);  cerr << "\n";
+  cerr << "  Out: "; printSet(OutSet);  cerr << "\n";
 }
 
-void BBLiveVar::printInOutSets() const
-{
-  cout << "In: ";   InSet.printSet();  cout << endl;
-  cout << "Out: ";   OutSet.printSet();  cout << endl;
+void BBLiveVar::printInOutSets() const {
+  cerr << "  In: ";   printSet(InSet);  cerr << "\n";
+  cerr << "  Out: ";  printSet(OutSet);  cerr << "\n";
 }
+
+
+
+