CSFSActivity/DUGStandard.hpp

Go to the documentation of this file.
00001 
00015 //--------------------------------------------------------------------
00016 //--------------------------------------------------------------------
00017 
00018 #ifndef DUGStandard_H
00019 #define DUGStandard_H
00020 
00021 // #define DEBUG_DUAA
00022 
00023 //--------------------------------------------------------------------
00024 // STL headers
00025 #include <list>
00026 #include <map>
00027 
00028 // OpenAnalysis headers
00029 #include <OpenAnalysis/CallGraph/ManagerCallGraph.hpp>
00030 #include <OpenAnalysis/Utils/OA_ptr.hpp>
00031 #include <OpenAnalysis/Utils/DGraph/DGraphInterface.hpp>
00032 #include <OpenAnalysis/CFG/CFG.hpp>
00033 #include <OpenAnalysis/Location/Locations.hpp>
00034 #include <OpenAnalysis/DataFlow/LocDFSet.hpp>
00035 #include <OpenAnalysis/IRInterface/ActivityIRInterface.hpp>
00036 #include <OpenAnalysis/IRInterface/DUGIRInterface.hpp>
00037 #include <OpenAnalysis/CSFIActivity/DUGInterface.hpp>
00038 #include <OpenAnalysis/Utils/DGraph/DGraphImplement.hpp>
00039 #include <OpenAnalysis/Activity/ActiveStandard.hpp>
00040 #include <OpenAnalysis/UDDUChains/Interface.hpp>
00041 #include <OpenAnalysis/UDDUChains/ManagerUDDUChainsStandard.hpp>
00042 //--------------------------------------------------------------------
00043 
00044 
00045 namespace OA {
00046     namespace DUG {
00047       
00048 class Node;
00049 class Edge;
00050 class NodesIterator;
00051 class EdgesIterator;
00052 class DUGStandard;
00053  
00054 void filterStr(std::string&);
00055 
00056 //--------------------------------------------------------
00057 class Node : public virtual NodeInterface {
00058 public:
00059     Node (OA_ptr<DUGStandard> pDUG, ProcHandle pProc, IRHandle ih, SymHandle pSym)
00060         : mDUG(pDUG), mProc(pProc), mType(NONEFORMAL_NODE), mDef(ih), mSym(pSym) {
00061         Ctor();
00062     }
00063 
00064     ~Node () { }
00065  
00066     //========================================================
00067     // Info specific to DUG
00068     //========================================================
00069     
00070     NodeType getType() const { return mType; }
00071 
00072     ProcHandle getProc() const { return mProc; }
00073     OA_ptr<Location> getLoc() const { return mLoc; }
00074     SymHandle getSym() const { return mSym; }
00075     IRHandle getDef() const { return mDef; }
00076     IRSymHandle getIRSymHandle() const { return IRSymHandle(mDef,mSym); }
00077     
00078     void markVaried(std::list<CallHandle>&, 
00079                     OA_ptr<Activity::ActivityIRInterface>,
00080                     std::set<OA_ptr<EdgeInterface> >&,
00081                     std::set<std::pair<unsigned,unsigned> >&,
00082                     ProcHandle, SymHandle,
00083                     OA_ptr<EdgeInterface>);
00084     void markUseful(std::list<CallHandle>&, 
00085                     OA_ptr<Activity::ActivityIRInterface>,
00086                     std::set<OA_ptr<EdgeInterface> >&,
00087                     std::set<std::pair<unsigned,unsigned> >&,
00088                     ProcHandle, SymHandle,
00089                     OA_ptr<EdgeInterface>);
00090 
00091     bool isVaried(){ return mVaried;}
00092     void setVaried()
00093         {
00094             mVaried = true;
00095         }
00096     void unsetVaried()
00097         { 
00098             mVaried = false;
00099         }
00100 
00101     bool isUseful(){ return mUseful;}
00102     void setUseful(){ mUseful = true;};
00103     void unsetUseful(){ mUseful = false;};
00104 
00105     void setActive();
00106     void setActive(SymHandle);
00107 
00108     bool isSelfDependent(){ return mSelfDependent;}
00109     void setSelfDependent(){ mSelfDependent = true;};
00110 
00111     bool isPathFrom(OA_ptr<NodeInterface>,
00112                     std::set<OA_ptr<NodeInterface> >&);
00113     bool hasOutgoingEdgesToOtherProcs(ProcHandle);
00114     bool hasIncomingEdgesFromOtherProcs(ProcHandle);
00115     void findOutgoingNodes(ProcHandle, std::set<IRSymHandle>&, std::set<IRSymHandle>&);
00116     void findIncomingNodes(ProcHandle, std::set<IRSymHandle>&, std::set<IRSymHandle>&);
00117     bool hasEdgesToOtherProc(ProcHandle, std::set<IRSymHandle>&);
00118     bool hasEdgesFromOtherProc(ProcHandle, std::set<IRSymHandle>&);
00119 
00120 
00121     unsigned int size () const {
00122         assert(0);  //yet to be implemented PLM 09/19/06
00123         return 0; 
00124     }
00125 
00126 
00127     //========================================================
00128     // DGraph::Interface::Node interface
00129     //========================================================
00130     
00132     unsigned int getId() const 
00133         { 
00134             return mDGNode->getId();
00135             //return mId; 
00136         }
00137     
00139     int num_incoming () const { return mDGNode->num_incoming(); }
00140     
00142     int num_outgoing () const { return mDGNode->num_outgoing(); }
00143 
00145     bool isAnEntry() const { return mDGNode->isAnEntry(); }
00146 
00148     bool isAnExit() const { return mDGNode->isAnExit(); }
00149 
00150     bool operator==(DGraph::NodeInterface& other);
00151     bool operator<(DGraph::NodeInterface& other);
00152    
00153     //========================================================
00154     // Output
00155     //========================================================
00156     void dump(std::ostream& os) { }  // for full override
00157     void dumpbase(std::ostream& os) {}
00158     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00159     void dumpdot(std::ostream& os, OA_ptr<DUGIRInterface> ir);
00160     void longdump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00161     void output(OA::IRHandlesIRInterface& ir) { 
00162         
00163         mDGNode->output(ir);
00164 
00165     }
00166     
00167 public:
00168     //========================================================
00169     // These methods shadow the same named methods in
00170     // the DGraph::Interface class and allow us to return
00171     // the more specific subclass
00172     //========================================================
00173     OA_ptr<DGraph::EdgesIteratorInterface> getIncomingEdgesIterator() const;
00174 
00175     OA_ptr<DGraph::EdgesIteratorInterface> getOutgoingEdgesIterator() const;
00176 
00177     OA_ptr<DGraph::NodesIteratorInterface> getSourceNodesIterator() const;
00178 
00179     OA_ptr<DGraph::NodesIteratorInterface> getSinkNodesIterator() const;
00180 
00181     OA_ptr<EdgesIteratorInterface> getDUGIncomingEdgesIterator() const;
00182 
00183     OA_ptr<EdgesIteratorInterface> getDUGOutgoingEdgesIterator() const;
00184 
00185     OA_ptr<NodesIteratorInterface> getDUGSourceNodesIterator() const;
00186 
00187     OA_ptr<NodesIteratorInterface> getDUGSinkNodesIterator() const;
00188 
00189     //========================================================
00190     // Construction
00191     //========================================================
00192     void addIncomingEdge(OA_ptr<DGraph::EdgeInterface> e)
00193         {
00194             return mDGNode->addIncomingEdge(e);  
00195         }
00196 
00197     void addOutgoingEdge(OA_ptr<DGraph::EdgeInterface> e)
00198         {
00199             return mDGNode->addOutgoingEdge(e); 
00200         }
00201 
00202     void removeIncomingEdge(OA_ptr<DGraph::EdgeInterface> e)
00203         {
00204 
00205             return mDGNode->removeIncomingEdge(e); 
00206         }
00207 
00208     void removeOutgoingEdge(OA_ptr<DGraph::EdgeInterface> e)
00209         {
00210             return mDGNode->removeOutgoingEdge(e);
00211         }  
00212 
00213 private:
00214     void Ctor();
00215    
00216 
00217     unsigned int mId;               // 0 is reserved; first instance is 1
00218     OA_ptr<DUGStandard> mDUG;       // enclosing DUG
00219     ProcHandle mProc;               // enclosing procedure
00220     NodeType mType;                 // node type
00221 
00222     OA_ptr<DGraph::NodeImplement> mDGNode;  // corresponding DGraph node
00223     // in DGraph that keeps 
00224     // structure
00225 
00226     OA_ptr<Location> mLoc;
00227     IRHandle mDef;                   // node key: CallHandle or StmtHandle
00228     SymHandle mSym;                  // node key
00229     bool mVaried;                    // 'true' if varied
00230     bool mUseful;                    // 'true' if useful
00231     bool mSelfDependent;             // 'true' if the variable has a self dependence.
00232     friend class DUGStandard;
00233     friend class Edge;
00234 };
00235   
00236  
00237 //--------------------------------------------------------
00238 class Edge : public virtual EdgeInterface {
00239 public:
00240     Edge (OA_ptr<DUGStandard> pDUG,
00241           OA_ptr<Node> pNode1, OA_ptr<Node> pNode2, EdgeType pType,
00242           CallHandle call, ProcHandle p); 
00243     ~Edge () {}
00244     
00245     //========================================================
00246     // Info specific to DUG
00247     //========================================================
00248     
00249     EdgeType getType() const { return mType; }
00250 
00251     ProcHandle getSourceProc() const { return mNode1->getProc(); }
00252     ProcHandle getSinkProc() const { return mNode2->getProc(); }
00253     CallHandle getCall() const { return mCall; }
00254     ProcHandle getProc() const { return mProc; }
00255 
00256     //========================================================
00257     // DGraph::Interface::Edge interface
00258     //========================================================
00259     
00261     unsigned int getId() const 
00262         {
00263             return mDGEdge->getId();
00264         }
00265 
00267     OA_ptr<DGraph::NodeInterface> getSource () const { return mNode1; }
00268     // FIXME: tail and head should be done like source and sink
00269     // in DGraph::Interface and here
00270     OA_ptr<DGraph::NodeInterface> tail () const { return mNode1; }
00271     
00273     OA_ptr<DGraph::NodeInterface> getSink () const { return mNode2; }
00274     OA_ptr<DGraph::NodeInterface> head () const { return mNode2; }
00275 
00276     OA_ptr<NodeInterface> getDUGSource() const
00277         {
00278             return getSource().convert<Node>();
00279         }
00280 
00281     OA_ptr<NodeInterface> getDUGSink() const
00282         {
00283             return getSink().convert<Node>();
00284         }
00285 
00286 
00287     bool operator==(DGraph::EdgeInterface& other);
00288     bool operator<(DGraph::EdgeInterface& other);
00289 
00290     void dump (std::ostream& os);
00291     void dumpdot (std::ostream&, OA_ptr<DUGIRInterface>);
00292     void dumpdot_label (std::ostream&, OA_ptr<DUGIRInterface>);
00293     void dumpbase (std::ostream& os) {}
00294     void output(OA::IRHandlesIRInterface& ir) { mDGEdge->output(ir); }
00295 
00296 private:
00297 
00298     OA_ptr<DUGStandard> mDUG;
00299     OA_ptr<Node> mNode1, mNode2;
00300     OA_ptr<DGraph::EdgeImplement> mDGEdge;
00301 
00302     EdgeType mType;
00303     CallHandle mCall;
00304     unsigned int mId; // 0 is reserved; first instance is 1
00305     ProcHandle mProc; // proc where this edge is used
00306 
00307     friend class DUGStandard;
00308     friend class Node;
00309 
00310 }; 
00311   
00312 //------------------------------------------------------------------
00316 class NodesIterator : public DGraph::NodesIteratorImplement,
00317                       public virtual NodesIteratorInterface
00318 {
00319 public:
00320     NodesIterator(OA_ptr<DGraph::NodesIteratorInterface> ni) 
00321         : DGraph::NodesIteratorImplement(ni) { }
00322 
00323     ~NodesIterator () {}
00324 
00325     OA_ptr<NodeInterface> currentDUGNode() const ;
00326 
00327 };
00328   
00329 //------------------------------------------------------------------
00333 class EdgesIterator : public DGraph::EdgesIteratorImplement, 
00334                       public virtual EdgesIteratorInterface
00335 {
00336 public:
00337     EdgesIterator(OA_ptr<DGraph::EdgesIteratorInterface> ni)
00338         : DGraph::EdgesIteratorImplement(ni) { }
00339 
00340     ~EdgesIterator () {}
00341 
00342     OA_ptr<EdgeInterface> currentDUGEdge() const;
00343 
00344 };
00345   
00346 
00347 //------------------------------------------------------------------
00348 class DUGStandard : public virtual DUGInterface  
00349 {
00350 public:
00351 
00352     friend class Node;  
00353     
00354     DUGStandard (
00355         OA_ptr<DUGIRInterface>,
00356         OA_ptr<DataFlow::ParamBindings>);
00357     virtual ~DUGStandard ();
00358   
00359     //-------------------------------------
00360     // DUG information access
00361     //-------------------------------------
00362 
00365     OA_ptr<Edge> getDUGEdge(OA_ptr<DGraph::EdgeInterface> dgEdge) const;
00366 
00369     OA_ptr<Node> getDUGNode(OA_ptr<DGraph::NodeInterface> dgNode) const;
00370   
00371     std::set<OA_ptr<Node> >& getInDepSymNodeSet(SymHandle sym){
00372         return mInDepSymToNodes[sym];
00373     }
00374     std::list<std::pair<SymHandle, ProcHandle> >& getIndepSyms(){
00375         return mIndepSymList;
00376     }
00377     std::list<std::pair<SymHandle, ProcHandle> >& getDepSyms(){
00378         return mDepSymList;
00379     }
00380 
00381     void insertIndepSymList(SymHandle sym, ProcHandle proc){
00382         mIndepSymList.push_back(std::pair<SymHandle, ProcHandle>(sym, proc));
00383         mIndepSymSet.insert(sym);
00384     }
00385 
00386     void insertDepSymList(SymHandle sym, ProcHandle proc){
00387         mDepSymList.push_back(std::pair<SymHandle, ProcHandle>(sym, proc));
00388         mDepSymSet.insert(sym);
00389     }
00390 
00391     void mapSymToProc(SymHandle sym, ProcHandle proc){
00392         mSymToProc[sym] = proc;
00393     }
00394 
00395     void insertActiveSymSet(SymHandle sym){
00396 #ifdef DEBUG_DUAA
00397         std::cout << "DUGStandard::insertActiveSymSet:(sym: "; 
00398         std::cout << sym << " )" << std::endl;
00399 #endif 
00400         mActiveSymSet->insert(sym);
00401     }
00402 
00403     std::map<ProcHandle,OA_ptr<OA::Activity::ActiveStandard> >& getActiveMap(){
00404         return mActiveMap;
00405     }
00406 
00407     OA_ptr<std::set<SymHandle> > getActiveSymSet(){
00408         return mActiveSymSet;
00409     }
00410     void insertActiveSymSet(OA_ptr<Location>);
00411 
00413     bool isIndependent(ProcHandle proc, SymHandle sym){
00414         if (mIndepSymSet.find(sym) != mIndepSymSet.end()) return true;
00415 
00416         OA_ptr<Location> loc = mIR->getLocation(proc, sym);
00417         if (loc.ptrEqual(0)) return false;
00418         OA_ptr<NamedLoc> nloc = loc.convert<NamedLoc>();
00419         OA_ptr<SymHandleIterator> symIter = nloc->getFullOverlapIter();
00420         for ( ; symIter->isValid(); (*symIter)++) {
00421             SymHandle temp = symIter->current();
00422             if (mIndepSymSet.find(temp) != mIndepSymSet.end()) return true;
00423         }
00424         return false;
00425     }
00426 
00428     bool isDependent(ProcHandle proc, SymHandle sym){
00429         if (mDepSymSet.find(sym) != mDepSymSet.end()) return true;
00430 
00431         OA_ptr<Location> loc = mIR->getLocation(proc, sym);
00432         if (loc.ptrEqual(0)) return false;
00433         OA_ptr<NamedLoc> nloc = loc.convert<NamedLoc>();
00434         OA_ptr<SymHandleIterator> symIter = nloc->getFullOverlapIter();
00435         for ( ; symIter->isValid(); (*symIter)++) {
00436             SymHandle temp = symIter->current();
00437             if (mDepSymSet.find(temp) != mDepSymSet.end()) return true;
00438         }
00439         return false;
00440     }
00441 
00443     bool isActive(SymHandle sym);
00444 
00446     bool isActive(StmtHandle stmt);
00447 
00449     bool isActive(MemRefHandle memref);
00450 
00451     void insertActiveStmtSet(StmtHandle stmt, ProcHandle proc){
00452         mActiveStmtSet->insert(stmt);
00453         mActiveMap[proc]->insertStmt(stmt);
00454     }
00455 
00456 
00457     void insertActiveMemRefSet(MemRefHandle mref, ProcHandle proc){
00458         mActiveMemRefSet->insert(mref);
00459         mActiveMap[proc]->insertMemRef(mref);
00460     }
00461 
00462     void assignActiveStandard(ProcHandle proc) {
00463     
00464         if (mActiveMap[proc].ptrEqual(0)) {
00465             mActiveMap[proc] = new OA::Activity::ActiveStandard(proc);
00466         }
00467     }
00468     
00469     //========================================================
00470     // Construction
00471     //========================================================
00472   
00473   
00474     void addNode(OA_ptr<DGraph::NodeInterface> pNode);
00475     void addEdge(OA_ptr<DGraph::EdgeInterface> pEdge);
00476   
00477 
00478     //========================================================
00479     // DGraph::Interface::Edge interface
00480     //========================================================
00481  
00482     int getNumNodes () { return mDGraph->getNumNodes(); }
00483     int getNumEdges () { return mDGraph->getNumEdges(); }
00484 
00485   
00486     // Output Functionalities 
00487     void dump (std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00488     void dumpdot (std::ostream& os, OA_ptr<DUGIRInterface> ir);
00489     void dumpbase(std::ostream& os) {}
00490     void output(OA::IRHandlesIRInterface& ir) { mDGraph->output(ir); }
00491 
00492   
00493     //------------------------------------------------------------------
00494     // Using trick of imitating covariance when getting all iterators
00495     // so that get an iterator specific to actual subclass
00496     // This is why have these getBlahIterator methods that shadow those
00497     // in DGraph::Interface and also have the protected ones
00498     // that must be defined here which override implementation in 
00499     // DGraph::Interface
00500     //------------------------------------------------------------------
00501   
00502     OA_ptr<DGraph::NodesIteratorInterface> getNodesIterator() const;
00503 
00504     OA_ptr<DGraph::NodesIteratorInterface> getEntryNodesIterator() const;
00505 
00506     OA_ptr<DGraph::NodesIteratorInterface> getExitNodesIterator() const;
00507 
00508     OA_ptr<DGraph::EdgesIteratorInterface> getEdgesIterator() const;
00509 
00510     OA_ptr<DGraph::NodesIteratorInterface> getDFSIterator(OA_ptr<DGraph::NodeInterface> n);
00511 
00512     OA_ptr<DGraph::NodesIteratorInterface> getBFSIterator();
00513 
00514     OA_ptr<DGraph::NodesIteratorInterface> 
00515     getReversePostDFSIterator(DGraph::DGraphEdgeDirection pOrient);
00516 
00517     OA_ptr<DGraph::NodesIteratorInterface>
00518     getReversePostDFSIterator(OA_ptr<DGraph::NodeInterface> root, 
00519                               DGraph::DGraphEdgeDirection pOrient);
00520 
00521     OA_ptr<DGraph::NodesIteratorInterface> getPostDFSIterator(
00522         DGraph::DGraphEdgeDirection pOrient);
00523 
00524     OA_ptr<DGraph::NodesIteratorInterface>
00525     getPostDFSIterator(OA_ptr<DGraph::NodeInterface> root, 
00526                        DGraph::DGraphEdgeDirection pOrient);
00527 
00528 
00529     //==================================================================
00530   
00531     OA_ptr<NodesIteratorInterface>
00532     getDUGNodesIterator() const;
00533 
00534     OA_ptr<EdgesIteratorInterface>
00535     getDUGEdgesIterator() const;
00536 
00537     OA_ptr<NodesIteratorInterface>
00538     getDUGEntryNodesIterator() const;
00539 
00540     OA_ptr<NodesIteratorInterface>
00541     getDUGExitNodesIterator() const;
00542 
00543     OA_ptr<NodesIteratorInterface>
00544     getDUGReversePostDFSIterator(DGraph::DGraphEdgeDirection pOrient);
00545 
00546     OA_ptr<NodesIteratorInterface>
00547     getDUGDFSIterator(OA_ptr<NodeInterface> n);
00548 
00550     void mapSymToMemRefSet(SymHandle sym, MemRefHandle mref) {
00551         mSymToMemRefSet[sym].insert(mref);
00552     }
00553 
00555     void mapSymToStmtSet(SymHandle sym, StmtHandle stmt) {
00556         mSymToStmtSet[sym].insert(stmt);
00557     }
00558 
00561     std::set<MemRefHandle> getMemRefSet(SymHandle sym) {
00562         return mSymToMemRefSet[sym];
00563     }
00564 
00567     std::set<StmtHandle> getStmtSet(SymHandle sym) {
00568         return mSymToStmtSet[sym];
00569     }
00570  
00571     //==================================================================
00572 
00573     OA_ptr<Node> getNode(IRSymHandle, ProcHandle);
00574     bool isNode(IRSymHandle, ProcHandle);
00575 
00576 private:
00577     OA_ptr<Node> mEntry; 
00578     OA_ptr<Node> mExit;
00579 
00580     // dgraph that will store underlying graph structure
00581     //  OA_ptr<DGraph::DGraphImplement> mDGraph;
00582   
00583     OA_ptr<DGraph::DGraphImplement> mDGraph;
00584 
00585     // using lists instead of sets because some of the iterators
00586     // need an ordered list of things and only want to have one
00587     // NodeIterator and EdgesIterator implementation
00588     OA_ptr<std::list<OA_ptr<Node> > > mCallNodes;
00589     OA_ptr<std::list<OA_ptr<Node> > > mReturnNodes;
00590 
00591     // map from SymHandle to Node
00592     std::map<std::pair<IRHandle,SymHandle>, OA_ptr<Node> > mSymToNode;
00593 
00594     // map from SymHandle to Location
00595     std::map<SymHandle, OA_ptr<Location> > mSymToLoc;
00596     std::map<SymHandle, ProcHandle> mSymToProc;
00597 
00598     // DUAA specific
00599     OA_ptr<DUGIRInterface>            mIR;
00600     //OA_ptr<IRHandlesIRInterface>      mIR;
00601     OA_ptr<DataFlow::ParamBindings>   mParamBind;
00602 
00603 
00604     std::list<std::pair<SymHandle, ProcHandle> > mIndepSymList;
00605     std::list<std::pair<SymHandle, ProcHandle> > mDepSymList;
00606     std::set<SymHandle>             mIndepSymSet;
00607     std::set<SymHandle>             mDepSymSet;
00608 
00609     OA_ptr<std::set<StmtHandle> > mActiveStmtSet;
00610     OA_ptr<std::set<MemRefHandle> > mActiveMemRefSet;
00611     OA_ptr<std::set<SymHandle> >    mActiveSymSet;
00612     std::map<ProcHandle,OA_ptr<OA::Activity::ActiveStandard> > mActiveMap;
00613 
00614     bool mUnknownLocActive;
00615 
00616 
00617 
00618     std::map<SymHandle, std::set<MemRefHandle> > mSymToMemRefSet;
00619 
00620     std::map<SymHandle, std::set<StmtHandle> > mSymToStmtSet;
00621 
00622     std::map<OA_ptr<Location>, OA_ptr<std::set<MemRefHandle> > > mLocToMemRefSet;
00623 
00624     std::map<OA_ptr<Location>, OA_ptr<std::set<StmtHandle> > > mLocToStmtSet;
00625 
00626     // map from dependent, independent SymHandle to the set of Nodes
00627     std::map<SymHandle, std::set<OA_ptr<Node> > > mInDepSymToNodes;
00628 };
00629 //--------------------------------------------------------------------
00630 
00631   } // end of DUG namespace
00632 } // end of OA namespace
00633 
00634 #endif