CSFIActivity/ManagerDUGStandard.cpp

Go to the documentation of this file.
00001 
00015 #include "ManagerDUGStandard.hpp"
00016 
00017 
00018 
00019 #if defined(DEBUG_ALL) || defined(DEBUG_ManagerDUGStandard)
00020 static bool debug = true;
00021 #else
00022 static bool debug = false;
00023 #endif
00024 
00025 namespace OA {
00026   namespace DUG {
00027 
00028 
00036 class CreateLocationVisitor : public virtual MemRefExprVisitor {
00037 public:
00038     OA_ptr<Location> mLoc;
00039     CreateLocationVisitor(OA_ptr<DUGIRInterface> ir,
00040                           ProcHandle proc) : mIR(ir),mProc(proc) {}
00041     ~CreateLocationVisitor() {}
00042     void visitNamedRef(NamedRef& ref) 
00043         {
00044             mLoc = mIR->getLocation(mProc,ref.getSymHandle());
00045         }
00046 
00047     void visitAddressOf(AddressOf& ref) { mLoc = new UnknownLoc(); }
00048     void visitUnnamedRef(UnnamedRef& ref) { mLoc = new UnknownLoc; }
00049     void visitUnknownRef(UnknownRef& ref) { mLoc = new UnknownLoc; }
00050     void visitDeref(Deref& ref) { mLoc = new UnknownLoc; }
00051     // default handling of more specific SubSet specificiations
00052     void visitSubSetRef(SubSetRef& ref) 
00053         {
00054             // will set mLoc to our base location
00055             ref.getMemRefExpr()->acceptVisitor(*this);
00056             if (mLoc->isaNamed()) {
00057                 mLoc = new LocSubSet(mLoc,false);
00058             }
00059         }
00060 
00061 private:
00062     OA_ptr<DUGIRInterface> mIR;
00063     ProcHandle mProc;
00064 
00065 };
00066 
00067 ManagerDUGStandard::ManagerDUGStandard(OA_ptr<DUGIRInterface> _ir, 
00068                                        OA_ptr<Activity::ActivityIRInterface> _air) 
00069     : mIR(_ir), mActIR(_air)
00070 { 
00071 }
00072 
00073 
00074 bool ManagerDUGStandard::stmt_has_call(StmtHandle stmt)
00075 {
00076     bool callflag = false;
00077     OA_ptr<IRCallsiteIterator> callsiteItPtr = mIR->getCallsites(stmt);
00078     for ( ; callsiteItPtr->isValid(); ++(*callsiteItPtr)) {
00079         CallHandle call = callsiteItPtr->current();
00080         SymHandle sym = mIR->getSymHandle(call);
00081         ProcHandle proc = mIR->getProcHandle(sym);
00082 #ifdef DEBUG_DUAA
00083         if (debug) {
00084             std::cout << "sym for callee = " 
00085                       << mIR->toString(sym) << std::endl;
00086         }
00087 #endif
00088         if (proc!=ProcHandle(0)) {
00089             callflag = true;
00090         }
00091     }
00092     return callflag;
00093 }
00094 
00098 void ManagerDUGStandard::insertEdge( 
00099     SymHandle        from,
00100     SymHandle        to,
00101     EdgeType         etype,
00102     CallHandle       expr,
00103     ProcHandle       fromProc,
00104     ProcHandle       toProc,
00105     ProcHandle       proc)
00106 {
00107     OA_ptr<Node> fromNode = mDUG->getNode(from, fromProc);
00108     OA_ptr<Node> toNode   = mDUG->getNode(to,   toProc);
00109 
00110     if (from == to) {
00111         fromNode->setSelfDependent();
00112         return;
00113     }
00114     // duplicate edges
00115     if (etype == CFLOW_EDGE && mMatrix[etype][from][to]) return;
00116     mMatrix[etype][from][to] = true;
00117 
00118 #ifdef DEBUG_DUAA
00119     static const char *sEdgeTypeToString[] = {
00120         "CFLOW",
00121         "CALL",
00122         "RETURN",
00123         "PARAM"
00124     };
00125     std::cout << "insertEdge(" << sEdgeTypeToString[etype] << "): " 
00126               << mIR->toString(from) << "@" << mIR->toString(fromProc)
00127               << " -> " << mIR->toString(to) << "@" << mIR->toString(toProc) << std::endl;
00128 #endif
00129 
00130     // edge between 'from' and 'to' node
00131     OA_ptr<Edge> dugEdge;
00132     dugEdge = new Edge(mDUG,fromNode, toNode, etype, expr, proc);
00133     mDUG->addEdge(dugEdge);
00134 #ifdef SAC07
00135     sac07_numGlobalEdges++;
00136 #endif
00137 }
00138 
00139 
00140 
00144 void ManagerDUGStandard::labelCallRetEdges(
00145     StmtHandle stmt, ProcHandle proc)
00146 {
00147 #ifdef DEBUG_DUAA
00148     std::cout << "labelCallRetEdges:" << std::endl;
00149 #endif
00150     
00151     // for each call to a defined procedure,
00152     // add a call Edge and return edge
00153     OA_ptr<IRCallsiteIterator> callsiteItPtr;
00154     callsiteItPtr = mIR->getCallsites(stmt);
00155 
00156     for ( ; callsiteItPtr->isValid(); ++(*callsiteItPtr)) {
00157 
00158         CallHandle call = callsiteItPtr->current();
00159         SymHandle calleesym = mIR->getSymHandle(call);
00160         ProcHandle callee = mIR->getProcHandle(calleesym);
00161         // Undefined procedures are not processed
00162         if (callee==ProcHandle(0)) { continue; }
00163 
00164         // to process only reachable procedures
00165         mProcsOfInterest.insert(callee);
00166 
00167         mProcToCallsiteSet[callee].insert(call); 
00168         mCallsiteToProc[call] = proc;
00169 
00170         // iterate over formal parameters for callee procedure
00171         // iterate over actual parameters at the same time
00172         OA_ptr<IRCallsiteParamIterator> actualIter;
00173         actualIter = mIR->getCallsiteParams(call);
00174         for (int formalCnt=0; actualIter->isValid(); formalCnt++, ++(*actualIter)) 
00175         {
00176             // formal
00177             SymHandle formalSym = mIR->getFormalSym(callee, formalCnt);
00178             mProcToFormalSet[callee].insert(formalSym); 
00179 
00180             // actual
00181             ExprHandle param = actualIter->current();
00182             OA_ptr<ExprTree> eTreePtr; eTreePtr = mIR->getExprTree(param);
00183             ExprTree::NodesIterator nodes_iter(*eTreePtr);
00184 
00185             for ( ; nodes_iter.isValid(); ++nodes_iter) {
00186 
00187                 OA_ptr<ExprTree::Node> exprTreeNode; 
00188                 exprTreeNode = nodes_iter.current();
00189 
00190                 if ( exprTreeNode->isaMemRefNode() ) {
00191 
00192                     OA_ptr<ExprTree::MemRefNode> memRefNode;
00193                     memRefNode = exprTreeNode.convert<ExprTree::MemRefNode>();
00194                     MemRefHandle memref = memRefNode->getHandle();
00195 
00196                     // get the memory reference expressions for this handle
00197                     OA_ptr<MemRefExprIterator> mreIter;
00198                     mreIter = mIR->getMemRefExprIterator(memref);
00199       
00200                     // for each mem-ref-expr associated with this memref
00201           
00202                     for (; mreIter->isValid(); (*mreIter)++) {
00203               
00204                         OA_ptr<OA::MemRefExpr> mre; mre = mreIter->current();
00205                         SymHandle actualSym;
00206                         if(mre->isaRefOp())
00207                         {
00208                             OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00209                             if( refOp->isaAddressOf() ) { mre = refOp->getMemRefExpr(); }
00210                             if(mre->isaNamed()) {
00211                                 OA_ptr<NamedRef> namedRef = mre.convert<NamedRef>();
00212                                 actualSym = namedRef->getSymHandle();         
00213                             } else if(mre->isaRefOp()) {
00214                                 OA_ptr<RefOp> refrefop = mre.convert<RefOp>();
00215                                 actualSym = refrefop->getBaseSym();
00216                             } else {
00217                                 continue;
00218                             }
00219 
00220                         }  else if(mre->isaNamed()) {
00221                             OA_ptr<NamedRef> namedRef = mre.convert<NamedRef>();
00222                             actualSym = namedRef->getSymHandle();
00223 
00224                         }
00225                         mFormalToActualMap[call][formalSym].insert(actualSym);
00226                         insertEdge(actualSym, formalSym, CALL_EDGE, call, proc, callee, proc);
00227                         OA_ptr<Location> baseSymLoc = mIR->getLocation(callee, formalSym);
00228                         insertEdge(formalSym, actualSym, RETURN_EDGE, call, callee, proc, proc);
00229 
00230                         mDUG->mapSymToMemRefSet(actualSym, memref);
00231                         mDUG->mapSymToStmtSet(actualSym, stmt);
00232 
00233                     }
00234                 }
00235             }
00236         }
00237     }
00238 }
00239 
00240 
00244 void
00245 ManagerDUGStandard::collectIndependentSyms( ProcHandle proc)
00246 {
00247 #ifdef DEBUG_DUAA
00248     std::cout << "collectIndependentSyms: ---" << std::endl;
00249 #endif  
00250     OA_ptr<MemRefExprIterator> indepIter =mIR->getIndepMemRefExprIter(proc);
00251     for ( indepIter->reset(); indepIter->isValid(); (*indepIter)++ ) {
00252       
00253         OA_ptr<MemRefExpr> memref = indepIter->current();
00254         if(memref->isaRefOp()) {
00255             while(memref->isaRefOp()) {
00256                 OA_ptr<RefOp> refOp = memref.convert<RefOp>();
00257                 memref = refOp->getMemRefExpr();
00258             }
00259         } 
00260 
00261         if(memref->isaNamed()) {
00262             OA_ptr<NamedRef> named = memref.convert<NamedRef>(); 
00263             SymHandle sym = named->getSymHandle();
00264             mDUG->insertIndepSymList(sym, proc);
00265             mProcsOfInterest.insert(proc);
00266 #ifdef DEBUG_DUAA
00267             std::cout << "collectIndependentSyms: " 
00268                       << mIR->toString(sym) << std::endl;
00269 #endif  
00270         }
00271     }
00272 }
00273 
00274 
00275 
00279 void
00280 ManagerDUGStandard::collectDependentSyms( ProcHandle proc)
00281 {
00282 #ifdef DEBUG_DUAA
00283     std::cout << "collectDependentSyms: ---" << std::endl;
00284 #endif  
00285     OA_ptr<MemRefExprIterator> depIter =mIR->getDepMemRefExprIter(proc);
00286     for ( depIter->reset(); depIter->isValid(); (*depIter)++ ) {
00287 
00288         OA_ptr<MemRefExpr> memref = depIter->current();
00289         if(memref->isaRefOp()) {
00290             while(memref->isaRefOp()) {
00291                 OA_ptr<RefOp> refOp = memref.convert<RefOp>();
00292                 memref = refOp->getMemRefExpr();
00293             }
00294         }
00295 
00296         if(memref->isaNamed()) {
00297             OA_ptr<NamedRef> named = memref.convert<NamedRef>();
00298             SymHandle sym = named->getSymHandle();
00299             mDUG->insertDepSymList(sym, proc);
00300             mProcsOfInterest.insert(proc);
00301 #ifdef DEBUG_DUAA
00302             std::cout << "collectDependentSyms: " 
00303                       << mIR->toString(sym) << std::endl;
00304 #endif  
00305         }
00306     }
00307 }
00308 
00309 
00310 
00311 // for each (use, def) pair of a stmt
00312 void ManagerDUGStandard::labelUseDefEdges(
00313     StmtHandle stmt, ProcHandle proc)
00314 {
00315 
00316 #ifdef DEBUG_DUAA
00317     std::cout << "labelUseDefEdges: ---" << std::endl;
00318 #endif
00319     std::set<SymHandle> useSet, defSet, allSyms;
00320 
00321     // collect all locations in the stmt
00322     OA_ptr<MemRefHandleIterator> mrIterPtr;
00323 
00324     mrIterPtr = mIR->getAllMemRefs(stmt);
00325 
00326     for (; mrIterPtr->isValid(); (*mrIterPtr)++ )
00327     {
00328       
00329         MemRefHandle memref = mrIterPtr->current();
00330         // get the memory reference expressions for this handle
00331         OA_ptr<MemRefExprIterator> mreIter;
00332         mreIter = mIR->getMemRefExprIterator(memref);
00333      
00334         // for each mem-ref-expr associated with this memref
00335         for (; mreIter->isValid(); (*mreIter)++) {
00336             OA_ptr<OA::MemRefExpr> mre; mre = mreIter->current();
00337 
00338             if(mre->isaNamed())
00339             {
00340                 OA_ptr<NamedRef> namedRef = mre.convert<NamedRef>();
00341                 SymHandle sym = namedRef->getSymHandle();
00342                 allSyms.insert(sym);
00343 
00344             }
00345             if(mre->isaRefOp())
00346             {
00347                 OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00348             
00349                 if(refOp->isaAddressOf()) {
00350                     OA_ptr<MemRefExpr> subMemRef = refOp->getMemRefExpr();
00351                
00352                     if(subMemRef->isaNamed())
00353                     {
00354                         OA_ptr<NamedRef> nRef = subMemRef.convert<NamedRef>();
00355                         SymHandle sym = nRef->getSymHandle();
00356                         allSyms.insert(sym);
00357                     } 
00358                 } else {    
00359                     SymHandle sym = refOp->getBaseSym();
00360                     allSyms.insert(sym);
00361                 }   
00362             }
00363         }
00364     }
00365 
00366 
00367     // Want to add a visitor PLM 12/15/06
00368     // collect uses
00369     mrIterPtr = mIR->getUseMemRefs(stmt);
00370     for (; mrIterPtr->isValid(); (*mrIterPtr)++ ) {
00371         MemRefHandle mref = mrIterPtr->current();
00372 
00373         // get the memory reference expressions for this handle
00374         OA_ptr<MemRefExprIterator> mreIter; 
00375         mreIter = mIR->getMemRefExprIterator(mref);
00376       
00377         // for each mem-ref-expr associated with this memref
00378         for (; mreIter->isValid(); (*mreIter)++) {
00379             OA_ptr<OA::MemRefExpr> mre; mre = mreIter->current();
00380 
00381             if(mre->isaNamed())
00382             {
00383                 OA_ptr<NamedRef> namedRef = mre.convert<NamedRef>();
00384                 SymHandle use = namedRef->getSymHandle();
00385 
00386                 mDUG->mapSymToMemRefSet(use, mref);
00387                 mDUG->mapSymToStmtSet(use, stmt);
00388 
00389                 useSet.insert(use);
00390 
00391             }
00392             if(mre->isaRefOp())
00393             {
00394               
00395                 OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00396 
00397                 if(refOp->isaAddressOf()) {
00398                     OA_ptr<MemRefExpr> subMemRef = refOp->getMemRefExpr();
00399                     if(subMemRef->isaNamed())
00400                     {
00401                         OA_ptr<NamedRef> namedRef = mre.convert<NamedRef>();
00402                         SymHandle use = namedRef->getSymHandle();
00403 
00404                         mDUG->mapSymToMemRefSet(use, mref);
00405                         mDUG->mapSymToStmtSet(use, stmt);
00406 
00407                         useSet.insert(use);
00408 
00409                     } 
00410 
00411                 } else {
00412                     SymHandle use = refOp->getBaseSym();
00413 
00414                     mDUG->mapSymToMemRefSet(use, mref);
00415                     mDUG->mapSymToStmtSet(use, stmt);
00416 
00417                     useSet.insert(use);
00418                 }
00419             }
00420         }
00421     }
00422 
00423     // collect defs
00424 
00425     // Want to add a visitor PLM 12/15/06
00426     mrIterPtr = mIR->getDefMemRefs(stmt);
00427     for (; mrIterPtr->isValid(); (*mrIterPtr)++ ) {
00428         MemRefHandle mref = mrIterPtr->current();
00429 
00430         // get the memory reference expressions for this handle
00431         OA_ptr<MemRefExprIterator> mreIter; 
00432         mreIter = mIR->getMemRefExprIterator(mref);
00433       
00434         // for each mem-ref-expr associated with this memref
00435         for (; mreIter->isValid(); (*mreIter)++) {
00436             OA_ptr<OA::MemRefExpr> mre; mre = mreIter->current();
00437 
00438 
00439             if(mre->isaNamed())
00440             {
00441                 OA_ptr<NamedRef> namedRef = mre.convert<NamedRef>();
00442                 SymHandle def = namedRef->getSymHandle();
00443                 if (allSyms.find(def) != allSyms.end())
00444                 {
00445             
00446                     mDUG->mapSymToMemRefSet(def, mref);
00447                     mDUG->mapSymToStmtSet(def, stmt);
00448  
00449                     defSet.insert(def);
00450                 }
00451             }
00452             if(mre->isaRefOp())
00453             {
00454               
00455                 OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00456 
00457                 if(refOp->isaAddressOf()) {
00458                     OA_ptr<MemRefExpr> subMemRef = refOp->getMemRefExpr();
00459                     if(subMemRef->isaNamed())
00460                     {
00461                         OA_ptr<NamedRef> namedRef = subMemRef.convert<NamedRef>();
00462                         SymHandle def = namedRef->getSymHandle();
00463                         if (allSyms.find(def) != allSyms.end())
00464                         {
00465 
00466                             mDUG->mapSymToMemRefSet(def, mref);
00467                             mDUG->mapSymToStmtSet(def, stmt);
00468 
00469                             defSet.insert(def);
00470                         }
00471 
00472                     } 
00473 
00474                 } else {
00475                     SymHandle def = refOp->getBaseSym();
00476             
00477                     if (allSyms.find(def) != allSyms.end()) {
00478 
00479                         mDUG->mapSymToMemRefSet(def, mref);
00480                         mDUG->mapSymToStmtSet(def, stmt);
00481 
00482                         defSet.insert(def);
00483                     }
00484                 }
00485             }
00486 
00487 
00488         }
00489     }
00490 
00491     // map all uses to may defs and vice versa for this statement
00492     // have to do this after removing implicit deps in case it will be
00493     // explicitly put back in
00494   
00495     std::set<SymHandle>::iterator useIter, defIter;
00496     for (useIter=useSet.begin(); useIter!=useSet.end(); useIter++) {
00497         SymHandle use = *useIter;
00498         for (defIter=defSet.begin(); defIter!=defSet.end(); defIter++) {
00499             SymHandle def = *defIter;
00500             insertEdge(use, def, CFLOW_EDGE, CallHandle(0), proc, proc, proc);
00501 
00502             // build dependence matrix
00503             setDepMatrix(proc, use, def);
00504         }
00505     }
00506 }
00507 
00508 
00512 OA_ptr<DUGStandard> ManagerDUGStandard::performAnalysis( 
00513     OA_ptr<IRProcIterator> procIter,
00514     OA_ptr<DataFlow::ParamBindings> paramBind,
00515     OA_ptr<OA::CallGraph::CallGraphInterface> cgraph)
00516 {
00517 
00518     mParamBind  = paramBind;
00519 
00520     // create a Manager that generates dep information for each statement in
00521     OA_ptr<DUGStandard> dug; 
00522     mDUG = dug = new DUGStandard( mIR, paramBind);
00523   
00524 #ifdef DEBUG_DUAA
00525     std::cout << "ManagerDUGStandard::performAnalysis: ---" << std::endl;
00526 #endif
00527     // Mark 'independent' and 'dependent' variables
00528     for ( procIter->reset(); procIter->isValid(); (*procIter)++ ) {
00529         collectIndependentSyms(procIter->current());
00530         collectDependentSyms  (procIter->current());
00531     }
00532 
00533     OA_ptr<OA::CallGraph::NodesIteratorInterface> callGraphIter;
00534     callGraphIter = cgraph->getCallGraphReversePostDFSIterator(DGraph::DEdgeOrg);
00535 
00536     for ( ; callGraphIter->isValid(); ++(*callGraphIter)) {
00537         OA_ptr<CallGraph::NodeInterface> node; 
00538         node = callGraphIter->currentCallGraphNode();
00539         ProcHandle proc = node->getProc();
00540         dug->assignActiveStandard(proc);
00541     
00542         // skip unreachable procedures
00543         if(mProcsOfInterest.find(proc) == mProcsOfInterest.end()) continue;
00544 
00545         OA_ptr<OA::IRStmtIterator> sItPtr; 
00546         sItPtr = mIR->getStmtIterator(proc);
00547         for ( ; sItPtr->isValid(); (*sItPtr)++) {
00548             StmtHandle stmt = sItPtr->current();
00549             labelUseDefEdges(stmt, proc);
00550             if (stmt_has_call(stmt)){
00551                 labelCallRetEdges(stmt, proc);
00552             }
00553         }
00554     }
00555     return dug;
00556 }
00557 
00558 
00559 
00560 
00564 void ManagerDUGStandard::setDepMatrix( 
00565     ProcHandle proc, 
00566     SymHandle  use, 
00567     SymHandle  def)
00568 {
00569     mDUG->mapSymToProc(use, proc);
00570     mDUG->mapSymToProc(def, proc);
00571     if (use == def) return;
00572     mProcToSymSet[proc].insert(use);
00573     mProcToSymSet[proc].insert(def);
00574     mProcToMatrix[proc][use][def] = true;
00575 
00576 #ifdef DEBUG_DUAA
00577     std::cout << "setDepMatrix: " << mIR->toString(proc) << ": " << mIR->toString(use);
00578     std::cout << " ---> ";
00579     std::cout << mIR->toString(def) << std::endl;
00580 #endif
00581 }
00582 
00583 
00584 
00585 
00586 bool ManagerDUGStandard::hasEdgesToOtherProc(SymHandle sym, ProcHandle proc)
00587 {
00588     // to filter out a fake parameter '.len' representing the length of 
00589     // a string parameter.
00590     if (!mDUG->isNode(sym, proc)) return false;
00591     OA_ptr<Node> node = mDUG->getNode(sym, proc);
00592 
00593     std::set<SymHandle> visited;
00594     return node->hasEdgesToOtherProc(proc, visited);
00595 }
00596 
00597 
00598 
00599 
00600 bool ManagerDUGStandard::hasEdgesFromOtherProc(SymHandle sym, ProcHandle proc)
00601 {
00602     // to filter out a fake parameter '.len' representing the length of 
00603     // a string parameter.
00604     if (!mDUG->isNode(sym, proc)) return false;
00605     OA_ptr<Node> node = mDUG->getNode(sym, proc);
00606 
00607     std::set<SymHandle> visited;
00608     return node->hasEdgesFromOtherProc(proc, visited);
00609 }
00610 
00611 
00612 
00613 
00618 bool ManagerDUGStandard::isOutgoingToOtherProcs(
00619     SymHandle sym, ProcHandle proc )
00620 {
00621     assert(mDUG->isNode(sym, proc));
00622     OA_ptr<Node> node = mDUG->getNode(sym, proc);
00623 
00624     bool definedInside = false;
00625     OA_ptr<EdgesIteratorInterface> predIterPtr
00626         = node->getDUGIncomingEdgesIterator();
00627     for (; predIterPtr->isValid() && !definedInside; ++(*predIterPtr)) {
00628         OA_ptr<EdgeInterface> predEdge = predIterPtr->currentDUGEdge();
00629         if (predEdge->getType() != CFLOW_EDGE &&
00630             predEdge->getType() != RETURN_EDGE) continue;
00631   
00632         if (predEdge->getProc() == proc) definedInside = true;
00633     }
00634     if (!definedInside) return false;
00635 
00636 
00637     bool usedOutside = false;
00638     OA_ptr<EdgesIteratorInterface> succIterPtr
00639         = node->getDUGOutgoingEdgesIterator();
00640     for (; succIterPtr->isValid() && !usedOutside; ++(*succIterPtr)) {
00641         OA_ptr<EdgeInterface> succEdge = succIterPtr->currentDUGEdge();
00642         if (succEdge->getType() != CFLOW_EDGE) continue;
00643   
00644         if (succEdge->getProc() != proc) return true;
00645     }
00646 
00647     return false;
00648 }
00649 
00650 
00651 
00652 
00658 bool ManagerDUGStandard::isIncomingFromOtherProcs(
00659     SymHandle sym, ProcHandle proc )
00660 {
00661     assert(mDUG->isNode(sym, proc));
00662     OA_ptr<Node> node = mDUG->getNode(sym, proc);
00663 
00664     bool definedOutside = false;
00665     OA_ptr<EdgesIteratorInterface> predIterPtr
00666         = node->getDUGIncomingEdgesIterator();
00667     for (; predIterPtr->isValid() && !definedOutside; ++(*predIterPtr)) {
00668         OA_ptr<EdgeInterface> predEdge = predIterPtr->currentDUGEdge();
00669         if (predEdge->getType() != CFLOW_EDGE) continue;
00670   
00671         if (predEdge->getProc() != proc) definedOutside = true;
00672     }
00673     if (!definedOutside) return false;
00674 
00675 
00676     bool usedInside = false;
00677     OA_ptr<EdgesIteratorInterface> succIterPtr
00678         = node->getDUGOutgoingEdgesIterator();
00679     for (; succIterPtr->isValid() && !usedInside; ++(*succIterPtr)) {
00680         OA_ptr<EdgeInterface> succEdge = succIterPtr->currentDUGEdge();
00681         if (succEdge->getType() != CFLOW_EDGE &&
00682             succEdge->getType() != CALL_EDGE ) continue;
00683   
00684         if (succEdge->getProc() == proc) return true;
00685     }
00686 
00687     return false;
00688 }
00689 
00690 
00691 
00692 
00697 bool ManagerDUGStandard::isPathThruOtherProcs(
00698     SymHandle use, SymHandle def, ProcHandle proc )
00699 {
00700     OA_ptr<NodeInterface> defNode = mDUG->getNode(def, proc);
00701     OA_ptr<NodeInterface> useNode = mDUG->getNode(use, proc);
00702 
00703     std::set<OA_ptr<NodeInterface> > visited;
00704     visited.insert(defNode);
00705     return defNode->isPathFrom(useNode, visited);
00706 }
00707 
00708 
00709 
00710 
00717 void ManagerDUGStandard::setDepMatrix4Globals(
00718     SymHandle use, SymHandle def, ProcHandle proc )
00719 {
00720     std::map<SymHandle, std::map<SymHandle, bool> >&
00721         depMat = mProcToMatrix[proc];
00722 
00723     // Do nothing if 'use' is not used in other procs, 
00724     if (!isOutgoingToOtherProcs(use, proc)) return;
00725 
00726     // Do nothing if 'def' is not defined from other procs, 
00727     if (!isIncomingFromOtherProcs(def, proc)) return;
00728 
00729 #ifdef DEBUG_DUAA
00730     std::cout << "setDepMatrix4Globals, checking Paths(" << mIR->toString(proc) <<
00731         "): " << mIR->toString(use) << " -> " << mIR->toString(def) << std::endl;
00732 #endif
00733     // Do nothing if there is no path from 'use' to 'def',
00734     if (!isPathThruOtherProcs(use, def, proc)) return;
00735 
00736     depMat[use][def] = true;
00737 #ifdef DEBUG_DUAA
00738     std::cout << "Value passing globals(" << mIR->toString(proc) <<
00739         "): " << mIR->toString(use) << " -> " << mIR->toString(def) << std::endl;
00740 #endif
00741 }
00742 
00743 
00744 
00745 
00749 void ManagerDUGStandard::transitiveClosureDepMatrix(
00750     OA_ptr<OA::CallGraph::CallGraphInterface> cgraph
00751     )
00752 {
00753     // use 'DEdgeRev' to get PostDFSIterator
00754     // 'getPostDFSIterator' is not implemented
00755     OA_ptr<OA::CallGraph::NodesIteratorInterface> iter;
00756     iter = cgraph->getCallGraphReversePostDFSIterator(DGraph::DEdgeRev);
00757 
00758     for ( ; iter->isValid(); ++(*iter)) {
00759         OA_ptr<CallGraph::NodeInterface> node; 
00760         node = iter->currentCallGraphNode();
00761         ProcHandle proc = node->getProc();
00762         transitiveClosure(proc);
00763         edgesBetweenActuals(proc);
00764     }
00765 }
00766 
00767 
00768 
00769 
00770 bool ManagerDUGStandard::isLocal(SymHandle sym, ProcHandle proc)
00771 {
00772     OA_ptr<Location> loc = mIR->getLocation(proc, sym);
00773     if (loc.ptrEqual(0)) return false;
00774     return loc->isLocal();
00775 }
00776 
00777 
00778 
00783 void ManagerDUGStandard::transitiveClosure(ProcHandle proc)
00784 {
00785     std::map<SymHandle, std::map<SymHandle, bool> >& depMat = mProcToMatrix[proc];
00786 
00787 #ifdef DEBUG_DUAA
00788     std::cout << "*** transitiveClosure(" << mIR->toString(proc) 
00789               << ") ***" << std::endl;
00790 #endif
00791     std::set<SymHandle>& symSet = mProcToSymSet[proc];
00792     std::set<SymHandle>::iterator i, j, k;
00793     SymHandle use, def, var;
00794 
00795 #ifdef THIS_IS_TOO_EXPENSIVE
00796     // set the dep matrix if both symbols are globals and
00797     // there is a path from 'use' to 'def' through other
00798     // procedures.
00799     for (k=symSet.begin(); k!=symSet.end(); k++){
00800         use = *k;
00801         if (isLocal(use, proc)) continue;
00802         for (i=symSet.begin(); i!=symSet.end(); i++){
00803             def = *i;
00804             if (isLocal(def, proc)) continue;
00805             if (use != def && !depMat[use][def])
00806                 setDepMatrix4Globals(use, def, proc);
00807         }
00808     }
00809 #endif
00810     // transitive closure
00811     for (k=symSet.begin(); k!=symSet.end(); k++){
00812         var = *k;
00813         for (i=symSet.begin(); i!=symSet.end(); i++){
00814             use = *i;
00815             if (!depMat[use][var]) continue;
00816             for (j=symSet.begin(); j!=symSet.end(); j++){
00817                 def = *j;
00818                 if (!depMat[var][def]) continue;
00819                 depMat[use][def] = true;
00820             }
00821         }
00822     }
00823 #ifdef DEBUG_DUAA
00824     std::cout << "*** END:transitiveClosure(" << mIR->toString(proc) 
00825               << ") ***" << std::endl;
00826 #endif
00827 }
00828 
00829 
00830 
00831 
00835 void ManagerDUGStandard::edgesBetweenActuals(ProcHandle proc)
00836 {
00837     std::set<CallHandle>& callsites = mProcToCallsiteSet[proc];
00838     std::set<CallHandle>::iterator callIter;
00839 
00840     std::set<SymHandle>& formals = mProcToFormalSet[proc];
00841     std::set<SymHandle>::iterator i, j;
00842   
00843     for (i=formals.begin(); i!=formals.end(); i++){
00844         SymHandle formal1 = *i;
00845         for (j=formals.begin(); j!=formals.end(); j++){
00846             SymHandle formal2 = *j;
00847             if (formal1 == formal2) continue;
00848 
00849             //--------------------------------------------------
00850             // - Value flow between formals through two global variables
00851             //   not in this procedure
00852             // - This is necessary for PARAM_EDGES
00853             //--------------------------------------------------
00854             if (!mProcToMatrix[proc][formal1][formal2]){
00855                 if (!hasEdgesToOtherProc(formal1, proc)) continue;
00856                 if (!hasEdgesFromOtherProc(formal2, proc)) continue;
00857                 if (isPathThruOtherProcs(formal1, formal2, proc)) {
00858 #ifdef DEBUG_DUAA
00859                     std::cout << "foundPath(" << mIR->toString(proc) << "): " << mIR->toString(formal1)
00860                               << " -> " << mIR->toString(formal2) << std::endl;
00861 #endif
00862                     mProcToMatrix[proc][formal1][formal2] = true;
00863                 }
00864             }
00865 
00866             if (mProcToMatrix[proc][formal1][formal2]){
00867                 insertEdge(formal1, formal2, PARAM_EDGE, CallHandle(0), proc, proc, proc);
00868 
00869                 for (callIter = callsites.begin(); callIter!=callsites.end(); callIter++){
00870                     CallHandle call = *callIter;
00871                     // get calling procedure
00872                     ProcHandle caller = mCallsiteToProc[call];
00873                     assert(caller != ProcHandle(0));
00874                     std::set<SymHandle>& set1 = mFormalToActualMap[call][formal1];
00875                     std::set<SymHandle>& set2 = mFormalToActualMap[call][formal2];
00876                     assert(set2.size() <= 1); // only one actual can be assigned
00877                     std::set<SymHandle>::iterator i1;
00878                     std::set<SymHandle>::iterator i2;
00879                     for (i1=set1.begin(); i1 != set1.end(); i1++){
00880                         for (i2=set2.begin(); i2 != set2.end(); i2++){
00881                             setDepMatrix(caller, *i1, *i2);
00882                         }
00883                     }
00884                 }
00885             }
00886         }
00887     }
00888 }
00889 
00890 
00891 
00892 void IndepLocVisitor::visitNamedLoc(NamedLoc& loc)
00893 {
00894     mDUG->insertIndepSymList(loc.getSymHandle(), mProc);
00895     mProcsOfInterest.insert(mProc);
00896 
00897 }
00898 
00899 void IndepLocVisitor::visitUnnamedLoc(UnnamedLoc& loc)
00900 {
00901     //assert(0);
00902 }
00903 
00904 void IndepLocVisitor::visitInvisibleLoc(InvisibleLoc& loc)
00905 {
00906     mDUG->insertIndepSymList(loc.getBaseSym(), mProc);
00907     mProcsOfInterest.insert(mProc);
00908 }
00909 
00910 void IndepLocVisitor::visitUnknownLoc(UnknownLoc& loc)
00911 {
00912     assert(0);
00913 }
00914 
00915 void IndepLocVisitor::visitLocSubSet(LocSubSet& loc)
00916 {
00917     OA_ptr<Location> ll = loc.getBaseLoc();
00918     if (!ll.ptrEqual(0)) { ll->acceptVisitor(*this); }
00919 }
00920 
00921 void depLocVisitor::visitNamedLoc(NamedLoc& loc)
00922 {
00923     mDUG->insertDepSymList(loc.getSymHandle(), mProc);
00924     mProcsOfInterest.insert(mProc);
00925 
00926 }
00927 
00928 void depLocVisitor::visitUnnamedLoc(UnnamedLoc& loc)
00929 {
00930     //assert(0);
00931 }
00932 
00933 void depLocVisitor::visitInvisibleLoc(InvisibleLoc& loc)
00934 {
00935     
00936     mDUG->insertDepSymList(loc.getBaseSym(), mProc);
00937     mProcsOfInterest.insert(mProc);
00938 
00939 }
00940 
00941 void depLocVisitor::visitUnknownLoc(UnknownLoc& loc)
00942 {
00943     assert(0);
00944 }
00945 
00946 void depLocVisitor::visitLocSubSet(LocSubSet& loc)
00947 {
00948     OA_ptr<Location> ll = loc.getBaseLoc();
00949     if (!ll.ptrEqual(0)) { ll->acceptVisitor(*this); }
00950 }
00951 
00952 
00953 
00954 
00955   } // end of namespace MPDUG
00956 } // end of namespace OA