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
00052 void visitSubSetRef(SubSetRef& ref)
00053 {
00054
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 std::map<ProcHandle,OA_ptr<UDDUChains::UDDUChainsStandard> >&
00070 ProcToUDDUChainsMap)
00071 : mIR(_ir), mActIR(_air), mProcToUDDUChainsMap(ProcToUDDUChainsMap)
00072
00073 {
00074 }
00075
00076
00077 bool ManagerDUGStandard::stmt_has_call(StmtHandle stmt)
00078 {
00079 bool callflag = false;
00080 OA_ptr<IRCallsiteIterator> callsiteItPtr = mIR->getCallsites(stmt);
00081 for ( ; callsiteItPtr->isValid(); ++(*callsiteItPtr)) {
00082 CallHandle call = callsiteItPtr->current();
00083 SymHandle sym = mIR->getSymHandle(call);
00084 ProcHandle proc = mIR->getProcHandle(sym);
00085 #ifdef DEBUG_DUAA
00086 if (debug) {
00087 std::cout << "sym for callee = "
00088 << mIR->toString(sym) << std::endl;
00089 }
00090 #endif
00091 if (proc!=ProcHandle(0)) {
00092 callflag = true;
00093 }
00094 }
00095 return callflag;
00096 }
00097
00098
00099 void ManagerDUGStandard::printIRSymHandle(IRSymHandle ish,
00100 ostream& os,
00101 OA_ptr<DUGIRInterface> ir)
00102 {
00103 IRHandle ih = ish.first;
00104 SymHandle sym = ish.second;
00105 os << ir->toString(sym) << "@" << ih.hval();
00106 }
00107
00108
00109
00110
00111 void ManagerDUGStandard::printEdge(IRSymHandle from,
00112 IRSymHandle to,
00113 EdgeType edgeType)
00114 {
00115 static const char *sEdgeTypeToString[] = {
00116 "CFLOW",
00117 "CALL",
00118 "RETURN",
00119 "PARAM"
00120 };
00121 printIRSymHandle(from, std::cout, mIR);
00122 std::cout << " -> ";
00123 printIRSymHandle(to, std::cout, mIR);
00124 std::cout << " (" << sEdgeTypeToString[edgeType]
00125 << ")" << std::endl;
00126 }
00127
00128
00129
00130
00134 void ManagerDUGStandard::insertEdge(
00135 IRSymHandle from,
00136 IRSymHandle to,
00137 EdgeType etype,
00138 CallHandle expr,
00139 ProcHandle fromProc,
00140 ProcHandle toProc,
00141 ProcHandle proc)
00142 {
00143 OA_ptr<Node> fromNode = mDUG->getNode(from, fromProc);
00144 OA_ptr<Node> toNode = mDUG->getNode(to, toProc);
00145
00146 if (fromNode == toNode) {
00147 fromNode->setSelfDependent();
00148 #ifdef DEBUG_DUAA
00149 std::cout << "*** insertEdge: head and tail nodes are same:";
00150 printIRSymHandle(from, std::cout, mIR);
00151 std::cout << std::endl;
00152 #endif
00153 return;
00154 }
00155
00156
00157 if (etype == CFLOW_EDGE) setDepMatrix(fromProc, from, to);
00158
00159
00160 if ((etype == CFLOW_EDGE || etype == PARAM_EDGE) &&
00161 mMatrix[etype][from][to]) {
00162 #ifdef DEBUG_DUAA
00163 std::cout << "*** insertEdge: edge exists:";
00164 printEdge(from, to, etype);
00165 #endif
00166 return;
00167 }
00168 mMatrix[etype][from][to] = true;
00169
00170 #ifdef DEBUG_DUAA
00171 std::cout << "insertEdge(inserting): ";
00172 printEdge(from, to, etype);
00173 #endif
00174
00175
00176 OA_ptr<Edge> dugEdge;
00177 dugEdge = new Edge(mDUG,fromNode, toNode, etype, expr, proc);
00178 mDUG->addEdge(dugEdge);
00179 }
00180
00181
00182
00186 void ManagerDUGStandard::getCallInfo(StmtHandle stmt, ProcHandle proc)
00187 {
00188 if (mCallInfoReady.find(stmt) != mCallInfoReady.end()) return;
00189 mCallInfoReady.insert(stmt);
00190
00191
00192 OA_ptr<IRCallsiteIterator> callsiteItPtr;
00193 callsiteItPtr = mIR->getCallsites(stmt);
00194
00195 for ( ; callsiteItPtr->isValid(); ++(*callsiteItPtr)) {
00196 CallHandle call = callsiteItPtr->current();
00197
00198 SymHandle calleesym = mIR->getSymHandle(call);
00199 ProcHandle callee = mIR->getProcHandle(calleesym);
00200
00201 if (callee==ProcHandle(0)) { continue; }
00202 #ifdef DEBUG_DUAA
00203 std::cout << "found a call to " << mIR->toString(callee) << std::endl;
00204 #endif
00205
00206
00207 mProcsOfInterest.insert(callee);
00208 mProcToCallsiteSet[callee].insert(call);
00209 mCallsiteToProc[call] = proc;
00210
00211
00212
00213 OA_ptr<IRCallsiteParamIterator> actualIter;
00214 actualIter = mIR->getCallsiteParams(call);
00215 for (int formalCnt=0; actualIter->isValid(); ++(*actualIter), formalCnt++)
00216 {
00217
00218 SymHandle formalSym = mIR->getFormalSym(callee, formalCnt);
00219 #ifdef DEBUG_DUAA
00220 std::cout << "formal:" << mIR->toString(formalSym) << std::endl;
00221 #endif
00222 std::set<SymHandle>& actualSet = mParamMap[call][formalSym];
00223
00224
00225 ExprHandle param = actualIter->current();
00226 OA_ptr<ExprTree> eTreePtr; eTreePtr = mIR->getExprTree(param);
00227 ExprTree::NodesIterator nodes_iter(*eTreePtr);
00228
00229 for ( ; nodes_iter.isValid(); ++nodes_iter) {
00230 OA_ptr<ExprTree::Node> exprTreeNode;
00231 exprTreeNode = nodes_iter.current();
00232
00233 if ( exprTreeNode->isaMemRefNode() ) {
00234 OA_ptr<ExprTree::MemRefNode> memRefNode;
00235 memRefNode = exprTreeNode.convert<ExprTree::MemRefNode>();
00236 MemRefHandle memref = memRefNode->getHandle();
00237
00238 #ifdef DEBUG_DUAA
00239 std::cout << "isaMemRefNode" << std::endl;
00240 #endif
00241
00242 OA_ptr<MemRefExprIterator> mreIter;
00243 mreIter = mIR->getMemRefExprIterator(memref);
00244
00245 for (; mreIter->isValid(); (*mreIter)++) {
00246 OA_ptr<OA::MemRefExpr> mre; mre = mreIter->current();
00247 SymHandle actualSym = getSymFromMRE(mre);
00248 if (actualSym == SymHandle(0)) continue;
00249
00250 #ifdef DEBUG_DUAA
00251 std::cout << "actual:" << mIR->toString(actualSym) << std::endl;
00252 #endif
00253 mDUG->mapSymToMemRefSet(actualSym, memref);
00254 mDUG->mapSymToStmtSet(actualSym, stmt);
00255
00256 actualSet.insert(actualSym);
00257 }
00258 }
00259 }
00260
00261 if (actualSet.size() == 1){
00262 mCallByRefActuals[stmt].insert(*(actualSet.begin()));
00263 }
00264 }
00265 }
00266 }
00267
00268
00269
00273 void ManagerDUGStandard::labelCallRetEdges(
00274 StmtHandle stmt, ProcHandle proc)
00275 {
00276 #ifdef DEBUG_DUAA
00277 std::cout << "labelCallRetEdges:proc(" << mIR->toString(proc) << "), stmt("
00278 << stmt.hval() << ")" << std::endl;
00279 #endif
00280 getCallInfo(stmt, proc);
00281
00282
00283 OA_ptr<UDDUChains::UDDUChainsStandard> UDDUChainForProc;
00284 UDDUChainForProc = mProcToUDDUChainsMap[proc];
00285
00286
00287
00288 OA_ptr<IRCallsiteIterator> callsiteItPtr;
00289 callsiteItPtr = mIR->getCallsites(stmt);
00290 for ( ; callsiteItPtr->isValid(); ++(*callsiteItPtr)) {
00291 CallHandle call = callsiteItPtr->current();
00292 SymHandle calleesym = mIR->getSymHandle(call);
00293 ProcHandle callee = mIR->getProcHandle(calleesym);
00294
00295 if (callee==ProcHandle(0)) { continue; }
00296
00297
00298 int formalCnt=0;
00299 SymHandle formalSym = mIR->getFormalSym(callee, formalCnt);
00300 for (; formalSym != SymHandle(0);
00301 formalSym = mIR->getFormalSym(callee, ++formalCnt))
00302 {
00303
00304 IRSymHandle def(StmtHandle(1),formalSym);
00305
00306
00307 std::set<SymHandle>& actualSet = mParamMap[call][formalSym];
00308 std::set<SymHandle>::iterator actualIter = actualSet.begin();
00309 for ( ; actualIter != actualSet.end(); actualIter++)
00310 {
00311 SymHandle actualSym = *actualIter;
00312
00313
00314 OA_ptr<StmtHandleIterator> UDStmtIter;
00315 UDStmtIter = UDDUChainForProc->getUDChainStmtIterator(stmt);
00316 for (; UDStmtIter->isValid(); (*UDStmtIter)++ ) {
00317 StmtHandle rdStmt = UDStmtIter->current();
00318 #ifdef DEBUG_DUAA
00319 std::cout << "rdStmt(" << rdStmt.hval() << ")"
00320 << " found" << std::endl;
00321 #endif
00322
00323
00324 if (rdStmt == StmtHandle(0)){
00325 #ifdef DEBUG_DUAA
00326 std::cout << "rdStmt(0): actualSym(" << mIR->toString(actualSym);
00327 #endif
00328
00329
00330 if (isFormal(actualSym, proc)){
00331 IRSymHandle use(StmtHandle(1),actualSym);
00332 insertEdge(use, def, CALL_EDGE, call, proc, callee, proc);
00333 mFormalToActualMap[call][def].insert(use);
00334 #ifdef DEBUG_DUAA
00335 std::cout << ") is formal.\n";
00336 #endif
00337 }
00338 else if (isGlobal(actualSym)){
00339 mGlobalUpUses[actualSym].insert(GlobalNode(def,callee,call,proc));
00340 #ifdef DEBUG_DUAA
00341 std::cout << ") is global.\n";
00342 #endif
00343 }
00344 #ifdef DEBUG_DUAA
00345 else {
00346 std::cout << ") is neither formal nor global.\n";
00347 }
00348 #endif
00349 continue;
00350 }
00351
00352 std::set<SymHandle>& rdDefSet = getDefs(rdStmt, proc);
00353 std::set<SymHandle>& callDefSet = getCallDefs(rdStmt, proc);
00354 if (rdDefSet.find(actualSym) != rdDefSet.end() ||
00355 callDefSet.find(actualSym) != callDefSet.end() ){
00356 #ifdef DEBUG_DUAA
00357 std::cout << "rdStmt defines the actualSym" << std::endl;
00358 #endif
00359 IRSymHandle use(rdStmt,actualSym);
00360 insertEdge(use, def, CALL_EDGE, call, proc, callee, proc);
00361 mFormalToActualMap[call][def].insert(use);
00362 }
00363 #ifdef DEBUG_DUAA
00364 else
00365 std::cout << "actualSym is not defined by rdDefSet.\n";
00366 #endif
00367 }
00368
00369
00370 if (actualSet.size() != 1) continue;
00371 #ifdef DEBUG_DUAA
00372 std::cout << "RETURN_EDGE" << std::endl;
00373 #endif
00374 IRSymHandle useRet(IRHandle(2),formalSym);
00375 IRSymHandle defRet(stmt,actualSym);
00376 insertEdge(useRet, defRet, RETURN_EDGE, call, callee, proc, proc);
00377 mFormalToActualMap[call][useRet].insert(defRet);
00378
00379 downwardExposedDefs(actualSym, stmt, proc, UDDUChainForProc);
00380 }
00381 }
00382 }
00383 }
00384
00385
00389 void
00390 ManagerDUGStandard::collectIndependentSyms( ProcHandle proc)
00391 {
00392 OA_ptr<MemRefExprIterator> indepIter =mIR->getIndepMemRefExprIter(proc);
00393 for ( indepIter->reset(); indepIter->isValid(); (*indepIter)++ ) {
00394
00395 OA_ptr<MemRefExpr> memref = indepIter->current();
00396 if(memref->isaRefOp()) {
00397 while(memref->isaRefOp()) {
00398 OA_ptr<RefOp> refOp = memref.convert<RefOp>();
00399 memref = refOp->getMemRefExpr();
00400 }
00401 }
00402
00403 if(memref->isaNamed()) {
00404 OA_ptr<NamedRef> named = memref.convert<NamedRef>();
00405 SymHandle sym = named->getSymHandle();
00406 mDUG->insertIndepSymList(sym, proc);
00407 mProcsOfInterest.insert(proc);
00408
00409 OA_ptr<Location> loc;loc = mIR->getLocation(proc, sym);
00410 if (!loc.ptrEqual(0)){
00411 OA_ptr<NamedLoc> nloc;nloc = loc.convert<NamedLoc>();
00412 OA_ptr<SymHandleIterator> symIter = nloc->getFullOverlapIter();
00413 for ( ; symIter->isValid(); (*symIter)++) {
00414 mDUG->insertIndepSymList(symIter->current(), proc);
00415 }
00416 }
00417 }
00418 }
00419 }
00420
00421
00422
00426 void
00427 ManagerDUGStandard::collectDependentSyms( ProcHandle proc)
00428 {
00429 OA_ptr<MemRefExprIterator> depIter =mIR->getDepMemRefExprIter(proc);
00430 for ( depIter->reset(); depIter->isValid(); (*depIter)++ ) {
00431
00432 OA_ptr<MemRefExpr> memref = depIter->current();
00433 if(memref->isaRefOp()) {
00434 while(memref->isaRefOp()) {
00435 OA_ptr<RefOp> refOp = memref.convert<RefOp>();
00436 memref = refOp->getMemRefExpr();
00437 }
00438 }
00439
00440 if(memref->isaNamed()) {
00441 OA_ptr<NamedRef> named = memref.convert<NamedRef>();
00442 SymHandle sym = named->getSymHandle();
00443 mDUG->insertDepSymList(sym, proc);
00444 mProcsOfInterest.insert(proc);
00445
00446 OA_ptr<Location> loc;loc = mIR->getLocation(proc, sym);
00447 if (!loc.ptrEqual(0)){
00448 OA_ptr<NamedLoc> nloc;nloc = loc.convert<NamedLoc>();
00449 OA_ptr<SymHandleIterator> symIter = nloc->getFullOverlapIter();
00450 for ( ; symIter->isValid(); (*symIter)++) {
00451 mDUG->insertDepSymList(symIter->current(), proc);
00452 }
00453 }
00454 }
00455 }
00456 }
00457
00458
00459
00460 void whatIsIt(std::string name, OA_ptr<OA::MemRefExpr> mre)
00461 {
00462 std::cout << name << ": mre is ";
00463 if (mre->isaNamed()) std::cout << "Named";
00464 if (mre->isaUnnamed()) std::cout << "Unnamed ";
00465 if (mre->isaUnknown()) std::cout << "Unknown ";
00466 if (mre->isaRefOp()) std::cout << "RefOp ";
00467 if (mre->isaDeref()) std::cout << "Deref ";
00468 if (mre->isaAddressOf()) std::cout << "AddressOf ";
00469 if (mre->isaSubSetRef()) std::cout << "SubSetRef ";
00470 if (mre->isaIdxAccess()) std::cout << "IdxAccess ";
00471 if (mre->isaIdxExprAccess()) std::cout << "IdxExprAccess ";
00472 if (mre->isaFieldAccess()) std::cout << "FieldAccess ";
00473 std::cout << std::endl;
00474 }
00475
00476
00477
00478 SymHandle
00479 ManagerDUGStandard::getModSymFromMRE(OA_ptr<OA::MemRefExpr> mre)
00480 {
00481 whatIsIt("getModSymFromMRE", mre);
00482 if(mre->isaRefOp())
00483 {
00484 OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00485
00486 if(refOp->isaAddressOf()) {
00487 OA_ptr<MemRefExpr> subMemRef = refOp->getMemRefExpr();
00488
00489 if(!subMemRef->isaUnnamed())
00490 {
00491 #ifdef DEBUG_DUAA
00492 std::cout << " getModSymFromMRE::RefOp,AddressOf,getBaseSym:: "
00493 << mIR->toString(refOp->getBaseSym()) << std::endl;
00494 #endif
00495 return refOp->getBaseSym();
00496 }
00497 }
00498 }
00499
00500 return SymHandle(0);
00501 }
00502
00503
00504
00505
00506 SymHandle
00507 ManagerDUGStandard::getSymFromMRE(OA_ptr<OA::MemRefExpr> mre)
00508 {
00509 #ifdef DEBUG_DUAA
00510 whatIsIt(" getSymFromMRE", mre);
00511 #endif
00512 if(mre->isaNamed())
00513 {
00514 OA_ptr<NamedRef> namedRef = mre.convert<NamedRef>();
00515 #ifdef DEBUG_DUAA
00516 std::cout << " getSymFromMRE-1::Named:: "
00517 << mIR->toString(namedRef->getSymHandle()) << std::endl;
00518 #endif
00519 return namedRef->getSymHandle();
00520 }
00521
00522 if(mre->isaRefOp())
00523 {
00524 OA_ptr<RefOp> refOp = mre.convert<RefOp>();
00525
00526 if(refOp->isaAddressOf()) {
00527 return getSymFromMRE(refOp->getMemRefExpr());
00528 }
00529 else {
00530 #ifdef DEBUG_DUAA
00531 std::cout << " getSymFromMRE-2::RefOp,non-AddressOf,getBaseSym:: "
00532 << mIR->toString(refOp->getBaseSym()) << std::endl;
00533 #endif
00534 return refOp->getBaseSym();
00535 }
00536 }
00537
00538 return SymHandle(0);
00539 }
00540
00541
00542
00543
00544 void ManagerDUGStandard::collectDefsUsesInStmt(
00545 StmtHandle stmt, ProcHandle proc)
00546 {
00547 #ifdef DEBUG_DUAA
00548 std::cout << "--------------------------------------------------\n";
00549 std::cout << "collectDefsUses(stmt:" << stmt.hval() << ")\n";
00550 #endif
00551
00552
00553 OA_ptr<MemRefHandleIterator> mrIterPtr;
00554 mrIterPtr = mIR->getAllMemRefs(stmt);
00555 for (; mrIterPtr->isValid(); (*mrIterPtr)++ )
00556 {
00557 MemRefHandle memref = mrIterPtr->current();
00558
00559 OA_ptr<MemRefExprIterator> mreIter;
00560 mreIter = mIR->getMemRefExprIterator(memref);
00561
00562
00563 for (; mreIter->isValid(); (*mreIter)++) {
00564 OA_ptr<OA::MemRefExpr> mre; mre = mreIter->current();
00565
00566 SymHandle sym = getSymFromMRE(mre);
00567 if (sym == SymHandle(0)) continue;
00568
00569 if (mre->isDef()) mStmtToDefdSyms[stmt].insert(sym);
00570 if (mre->isUse()) mStmtToUsedSyms[stmt].insert(sym);
00571
00572 #ifdef DEBUG_DUAA
00573 std::cout << mIR->toString(sym) << "(";
00574 if (mre->isDef()) std::cout << "def";
00575 if (mre->isUse()) std::cout << " use";
00576 std::cout << ")" << std::endl;
00577 #endif
00578
00579 OA_ptr<Location> loc = mIR->getLocation(proc, sym);
00580 if (!loc->isLocal()) mGlobals.insert(sym);
00581 }
00582 }
00583 #ifdef DEBUG_DUAA
00584 std::cout << std::endl;
00585 #endif
00586 }
00587
00588
00589
00590
00591
00592
00593
00594 std::set<SymHandle>&
00595 ManagerDUGStandard::getUses(StmtHandle stmt, ProcHandle proc)
00596 {
00597 #ifdef DEBUG_DUAA
00598 std::cout << "getUses(stmt:" << stmt.hval() << ")" << std::endl;
00599 #endif
00600
00601 if (mDefsUsesCollected.find(stmt) == mDefsUsesCollected.end()){
00602 collectDefsUsesInStmt(stmt, proc);
00603 mDefsUsesCollected.insert(stmt);
00604 }
00605
00606 return mStmtToUsedSyms[stmt];
00607 }
00608
00609
00610
00611
00612
00613 std::set<SymHandle>&
00614 ManagerDUGStandard::getDefs(StmtHandle stmt, ProcHandle proc)
00615 {
00616 #ifdef DEBUG_DUAA
00617 std::cout << "getDefs(stmt:" << stmt.hval() << ")" << std::endl;
00618 #endif
00619
00620 if (mDefsUsesCollected.find(stmt) == mDefsUsesCollected.end()){
00621 collectDefsUsesInStmt(stmt, proc);
00622 mDefsUsesCollected.insert(stmt);
00623 }
00624
00625 return mStmtToDefdSyms[stmt];
00626 }
00627
00628
00629
00630
00631
00632
00633 std::set<SymHandle>&
00634 ManagerDUGStandard::getCallDefs(StmtHandle stmt, ProcHandle proc)
00635 {
00636 #ifdef DEBUG_DUAA
00637 std::cout << "getCallDefs(stmt:" << stmt.hval() << ")" << std::endl;
00638 #endif
00639
00640 if (mCallInfoReady.find(stmt) == mCallInfoReady.end()){
00641 getCallInfo(stmt, proc);
00642 }
00643
00644 return mCallByRefActuals[stmt];
00645 }
00646
00647
00648
00649
00650
00651
00652 void
00653 ManagerDUGStandard::upwardExposedUses(SymHandle def, StmtHandle stmt,
00654 ProcHandle proc,
00655 OA_ptr<UDDUChains::UDDUChainsStandard> UDDUChainForProc)
00656 {
00657 #ifdef DEBUG_DUAA
00658 std::cout << "upwardExposedUses(stmt:" << stmt.hval() << "): " << std::endl;
00659 #endif
00660 IRSymHandle defIRH(stmt,def);
00661 std::set<SymHandle>& useSet = getUses(stmt, proc);
00662
00663 OA_ptr<StmtHandleIterator> rdStmtIter;
00664 rdStmtIter = UDDUChainForProc->getUDChainStmtIterator(stmt);
00665 for (rdStmtIter->reset(); rdStmtIter->isValid(); (*rdStmtIter)++ ){
00666 StmtHandle rdStmt = rdStmtIter->current();
00667
00668
00669 if (rdStmt == StmtHandle(0)){
00670
00671
00672 std::set<SymHandle>::iterator useIter;
00673 for (useIter=useSet.begin(); useIter!=useSet.end(); useIter++) {
00674 if (isFormal(*useIter, proc)){
00675 IRSymHandle useIRH(StmtHandle(1),*useIter);
00676 insertEdge(useIRH, defIRH, CFLOW_EDGE, CallHandle(0), proc, proc, proc);
00677 }
00678 else if (isGlobal(*useIter)){
00679 mGlobalUpUses[*useIter].insert(GlobalNode(defIRH,proc,CallHandle(0),proc));
00680 }
00681 }
00682 continue;
00683 }
00684 }
00685 }
00686
00687
00688
00689
00690
00691
00692 void
00693 ManagerDUGStandard::downwardExposedDefs(SymHandle def, StmtHandle stmt,
00694 ProcHandle proc,
00695 OA_ptr<UDDUChains::UDDUChainsStandard> UDDUChainForProc)
00696 {
00697 IRSymHandle defIRH(stmt,def);
00698
00699 if (!isFormal(def, proc) && !isGlobal(def)) return;
00700 #ifdef DEBUG_DUAA
00701 std::cout << "DUChain for stmt(" << stmt.hval() << "), var("
00702 << mIR->toString(def) << "): ";
00703 #endif
00704 OA_ptr<StmtHandleIterator> useStmtIter;
00705 useStmtIter = UDDUChainForProc->getDUChainStmtIterator(stmt);
00706 for (; useStmtIter->isValid(); (*useStmtIter)++ ){
00707 StmtHandle useStmt = useStmtIter->current();
00708 #ifdef DEBUG_DUAA
00709 std::cout << "useStmt(" << useStmt.hval() << "), ";
00710 #endif
00711
00712 if (useStmt != StmtHandle(0)) continue;
00713
00714 if (isFormal(def, proc)){
00715 IRSymHandle useIRH(StmtHandle(2),def);
00716 insertEdge(defIRH, useIRH, CFLOW_EDGE, CallHandle(0), proc, proc, proc);
00717 }
00718 else if (isGlobal(def)){
00719 mGlobalDnDefs[def].insert(GlobalNode(defIRH, proc, CallHandle(0), proc));
00720 #ifdef DEBUG_DUAA
00721 std::cout << mIR->toString(def) << "is added to GlobalDnDefs in stmt("
00722 << stmt.hval() << ")" << std::endl;
00723 #endif
00724 }
00725 }
00726 }
00727
00728
00729
00730
00731
00732 void
00733 ManagerDUGStandard::printEdgeDot(IRSymHandle from, ProcHandle fromProc,
00734 IRSymHandle to, ProcHandle toProc)
00735 {
00736 OA_ptr<Node> fromNode = mDUG->getNode(from, fromProc);
00737 OA_ptr<Node> toNode = mDUG->getNode(to, toProc);
00738
00739 fromNode->dumpdot(std::cout, mIR);
00740 std::cout << "->";
00741 toNode->dumpdot(std::cout, mIR);
00742 std::cout << std::endl;
00743 }
00744
00745
00746
00747
00748
00749 void ManagerDUGStandard::labelUseDefEdges(
00750 StmtHandle stmt, ProcHandle proc)
00751 {
00752 #ifdef DEBUG_DUAA
00753 std::cout << "labelUseDefEdges(stmt:" << stmt.hval() << "): " << std::endl;
00754 bool debug=false;
00755 unsigned debug_stmt_no = 1083755148;
00756 if (stmt.hval() == debug_stmt_no){
00757 std::cout << "==============debug_stmt_no================" << std::endl;
00758 debug = true;
00759 }
00760 #endif
00761 std::set<SymHandle>& defSet = getDefs(stmt, proc);
00762 std::set<SymHandle>& useSet = getUses(stmt, proc);
00763
00764 OA_ptr<UDDUChains::UDDUChainsStandard> UDDUChainForProc;
00765 UDDUChainForProc = mProcToUDDUChainsMap[proc];
00766
00767
00768 std::set<SymHandle>::iterator defIter;
00769 for (defIter=defSet.begin(); defIter!=defSet.end(); defIter++) {
00770 IRSymHandle def(stmt,*defIter);
00771 #ifdef DEBUG_DUAA
00772 if (debug)
00773 std::cout << " debug_stmt_no: inside defIter:"
00774 << mIR->toString(*defIter) << std::endl;
00775 #endif
00776
00777 upwardExposedUses(*defIter, stmt, proc, UDDUChainForProc);
00778
00779
00780 std::set<SymHandle>::iterator useIter=useSet.begin();
00781 for (; useIter!=useSet.end(); useIter++) {
00782 #ifdef DEBUG_DUAA
00783 if (debug)
00784 std::cout << " debug_stmt_no: inside useIter:"
00785 << mIR->toString(*useIter) << std::endl;
00786 #endif
00787
00788 OA_ptr<StmtHandleIterator> rdStmtIter;
00789 rdStmtIter = UDDUChainForProc->getUDChainStmtIterator(stmt);
00790 for (; rdStmtIter->isValid(); (*rdStmtIter)++ ){
00791 StmtHandle rdStmt = rdStmtIter->current();
00792 #ifdef DEBUG_DUAA
00793 if (debug)
00794 std::cout << " debug_stmt_no: inside rdStmtIter:"
00795 << rdStmt.hval() << std::endl;
00796 #endif
00797 std::set<SymHandle>& rdDefSet = getDefs(rdStmt, proc);
00798 std::set<SymHandle>& rdCallDefs = getCallDefs(rdStmt, proc);
00799 if (rdDefSet.find(*useIter) != rdDefSet.end() ||
00800 rdCallDefs.find(*useIter) != rdCallDefs.end() ){
00801 IRSymHandle use(rdStmt,*useIter);
00802 #ifdef DEBUG_DUAA
00803 std::cout << "inserting ... "; printEdge(use, def, CFLOW_EDGE);
00804 std::cout << std::endl;
00805 #endif
00806 insertEdge(use, def, CFLOW_EDGE, CallHandle(0), proc, proc, proc);
00807 }
00808 #ifdef DEBUG_DUAA
00809 else
00810 std::cout << mIR->toString(*useIter) << " is not defined in rdStmt("
00811 << rdStmt.hval() << ")" << std::endl;
00812 #endif
00813 }
00814 }
00815
00816 downwardExposedDefs(*defIter, stmt, proc, UDDUChainForProc);
00817 }
00818 }
00819
00820
00821
00825 void ManagerDUGStandard::connectGlobals()
00826 {
00827 #ifdef DEBUG_DUAA
00828 std::cout << "connectGlobals: ";
00829 #endif
00830 std::map<SymHandle, std::set<GlobalNode> >::iterator globalUseIter;
00831 globalUseIter=mGlobalUpUses.begin();
00832 for(; globalUseIter!=mGlobalUpUses.end(); globalUseIter++){
00833 SymHandle global = globalUseIter->first;
00834 std::set<GlobalNode>& globalUses = globalUseIter->second;
00835 std::set<GlobalNode>& globalDefs = mGlobalDnDefs[global];
00836 #ifdef DEBUG_DUAA
00837 std::cout << mIR->toString(global) << ", ";
00838 #endif
00839
00840 std::set<GlobalNode>::iterator dIter, uIter;
00841 for(uIter=globalUses.begin(); uIter!=globalUses.end(); uIter++){
00842 IRSymHandle use = uIter->mIRnSym;
00843 ProcHandle useProc = uIter->mProc;
00844 ProcHandle useStmtProc = uIter->mUseProc;
00845 EdgeType etype = (uIter->mCall == CallHandle(0) ? CFLOW_EDGE : CALL_EDGE);
00846 for(dIter=globalDefs.begin(); dIter!=globalDefs.end(); dIter++){
00847 IRSymHandle def = dIter->mIRnSym;
00848 ProcHandle defProc = dIter->mProc;
00849
00850 insertEdge(def, use, etype, uIter->mCall, defProc, useProc, useStmtProc);
00851 }
00852 if (globalDefs.empty() && (mDUG->isIndependent(useProc, global))){
00853 IRSymHandle indepGlobal(IRHandle(0), global);
00854 insertEdge(indepGlobal, use, CFLOW_EDGE, CallHandle(0), useProc,
00855 useProc, useStmtProc);
00856 }
00857 }
00858 }
00859 #ifdef DEBUG_DUAA
00860 std::cout << std::endl;
00861 #endif
00862 }
00863
00864
00865 void
00866 ManagerDUGStandard::collectFormalParameters(ProcHandle proc)
00867 {
00868 int cnt = 0;
00869
00870 SymHandle sym = mIR->getFormalSym(proc, cnt++);
00871 while (sym != SymHandle(0))
00872 {
00873 mProcToFormalSet[proc].insert(sym);
00874 #ifdef DEBUG_DUAA
00875 std::cout << "collectFormalParameters(inserting): "
00876 << mIR->toString(sym) << std::endl;
00877 #endif
00878 sym = mIR->getFormalSym(proc, cnt++);
00879 }
00880 }
00881
00882
00883
00887 OA_ptr<DUGStandard> ManagerDUGStandard::performAnalysis(
00888 OA_ptr<IRProcIterator> procIter,
00889 OA_ptr<DataFlow::ParamBindings> paramBind,
00890 OA_ptr<OA::CallGraph::CallGraphInterface> cgraph)
00891 {
00892
00893 mParamBind = paramBind;
00894
00895
00896 OA_ptr<DUGStandard> dug;
00897 mDUG = dug = new DUGStandard( mIR, paramBind);
00898
00899 #ifdef DEBUG_DUAA
00900 std::cout << "ManagerDUGStandard::performAnalysis: ---" << std::endl;
00901 #endif
00902
00903 for ( procIter->reset(); procIter->isValid(); (*procIter)++ ) {
00904
00905 collectIndependentSyms(procIter->current());
00906 collectDependentSyms (procIter->current());
00907 collectFormalParameters(procIter->current());
00908 }
00909
00910 OA_ptr<OA::CallGraph::NodesIteratorInterface> callGraphIter;
00911 callGraphIter = cgraph->getCallGraphReversePostDFSIterator(DGraph::DEdgeOrg);
00912
00913 for ( ; callGraphIter->isValid(); ++(*callGraphIter)) {
00914 OA_ptr<CallGraph::NodeInterface> node;
00915 node = callGraphIter->currentCallGraphNode();
00916 ProcHandle proc = node->getProc();
00917 dug->assignActiveStandard(proc);
00918
00919
00920 if(mProcsOfInterest.find(proc) == mProcsOfInterest.end()) continue;
00921
00922 OA_ptr<OA::IRStmtIterator> sItPtr;
00923 sItPtr = mIR->getStmtIterator(proc);
00924 for ( ; sItPtr->isValid(); (*sItPtr)++) {
00925 StmtHandle stmt = sItPtr->current();
00926 labelUseDefEdges(stmt, proc);
00927 if (stmt_has_call(stmt)){
00928 labelCallRetEdges(stmt, proc);
00929 }
00930 }
00931 }
00932 connectGlobals();
00933
00934 return dug;
00935 }
00936
00937
00938
00939
00943 void ManagerDUGStandard::setDepMatrix(
00944 ProcHandle proc,
00945 IRSymHandle use,
00946 IRSymHandle def)
00947 {
00948 if (use == def) return;
00949 mProcToIRSymSet[proc].insert(use);
00950 mProcToIRSymSet[proc].insert(def);
00951 mProcToMatrix[proc][use][def] = true;
00952
00953 #ifdef DEBUG_DUAA
00954 std::cout << mIR->toString(proc) << ": ";
00955 printIRSymHandle(use, std::cout, mIR);
00956 std::cout << " ---> ";
00957 printIRSymHandle(def, std::cout, mIR);
00958 std::cout << std::endl;
00959 #endif
00960 }
00961
00962
00963
00964
00969 void ManagerDUGStandard::findOutgoingNodes(
00970 IRSymHandle ish, ProcHandle proc,
00971 std::set<IRSymHandle>& OutGoingNodeSet)
00972 {
00973 if (!mDUG->isNode(ish, proc)) return;
00974 OA_ptr<Node> node = mDUG->getNode(ish, proc);
00975
00976 std::set<IRSymHandle> visited;
00977 node->findOutgoingNodes(proc, OutGoingNodeSet, visited);
00978 }
00979
00980
00981
00982
00987 void ManagerDUGStandard::findIncomingNodes(
00988 IRSymHandle ish, ProcHandle proc,
00989 std::set<IRSymHandle>& IncomingNodeSet)
00990 {
00991 if (!mDUG->isNode(ish, proc)) return;
00992 OA_ptr<Node> node = mDUG->getNode(ish, proc);
00993
00994 std::set<IRSymHandle> visited;
00995 node->findIncomingNodes(proc, IncomingNodeSet, visited);
00996 }
00997
00998
00999
01000
01001 bool ManagerDUGStandard::hasEdgesToOtherProc(IRSymHandle ish, ProcHandle proc)
01002 {
01003
01004
01005 if (!mDUG->isNode(ish, proc)) return false;
01006 OA_ptr<Node> node = mDUG->getNode(ish, proc);
01007
01008 std::set<IRSymHandle> visited;
01009 return node->hasEdgesToOtherProc(proc, visited);
01010 }
01011
01012
01013
01014
01015 bool ManagerDUGStandard::hasEdgesFromOtherProc(IRSymHandle ish, ProcHandle proc)
01016 {
01017
01018
01019 if (!mDUG->isNode(ish, proc)) return false;
01020 OA_ptr<Node> node = mDUG->getNode(ish, proc);
01021
01022 std::set<IRSymHandle> visited;
01023 return node->hasEdgesFromOtherProc(proc, visited);
01024 }
01025
01026
01027
01032 bool ManagerDUGStandard::isOutgoingToOtherProcs(
01033 IRSymHandle ish, ProcHandle proc )
01034 {
01035 assert(mDUG->isNode(ish, proc));
01036 OA_ptr<Node> node = mDUG->getNode(ish, proc);
01037
01038 bool definedInside = false;
01039 OA_ptr<EdgesIteratorInterface> predIterPtr
01040 = node->getDUGIncomingEdgesIterator();
01041 for (; predIterPtr->isValid() && !definedInside; ++(*predIterPtr)) {
01042 OA_ptr<EdgeInterface> predEdge = predIterPtr->currentDUGEdge();
01043 if (predEdge->getType() != CFLOW_EDGE) continue;
01044
01045 if (predEdge->getProc() == proc) definedInside = true;
01046 }
01047 if (!definedInside) return false;
01048
01049
01050 bool usedOutside = false;
01051 OA_ptr<EdgesIteratorInterface> succIterPtr
01052 = node->getDUGOutgoingEdgesIterator();
01053 for (; succIterPtr->isValid() && !usedOutside; ++(*succIterPtr)) {
01054 OA_ptr<EdgeInterface> succEdge = succIterPtr->currentDUGEdge();
01055 if (succEdge->getType() != CFLOW_EDGE) continue;
01056
01057 if (succEdge->getProc() != proc) return true;
01058 }
01059
01060 return false;
01061 }
01062
01063
01064
01065
01071 bool ManagerDUGStandard::isIncomingFromOtherProcs(
01072 IRSymHandle ish, ProcHandle proc )
01073 {
01074 assert(mDUG->isNode(ish, proc));
01075 OA_ptr<Node> node = mDUG->getNode(ish, proc);
01076
01077 bool definedOutside = false;
01078 OA_ptr<EdgesIteratorInterface> predIterPtr
01079 = node->getDUGIncomingEdgesIterator();
01080 for (; predIterPtr->isValid() && !definedOutside; ++(*predIterPtr)) {
01081 OA_ptr<EdgeInterface> predEdge = predIterPtr->currentDUGEdge();
01082 if (predEdge->getType() != CFLOW_EDGE) continue;
01083
01084 if (predEdge->getProc() != proc) definedOutside = true;
01085 }
01086 if (!definedOutside) return false;
01087
01088
01089 bool usedInside = false;
01090 OA_ptr<EdgesIteratorInterface> succIterPtr
01091 = node->getDUGOutgoingEdgesIterator();
01092 for (; succIterPtr->isValid() && !usedInside; ++(*succIterPtr)) {
01093 OA_ptr<EdgeInterface> succEdge = succIterPtr->currentDUGEdge();
01094 if (succEdge->getType() != CFLOW_EDGE) continue;
01095
01096 if (succEdge->getProc() == proc) return true;
01097 }
01098
01099 return false;
01100 }
01101
01102
01103
01104
01109 bool ManagerDUGStandard::isPathThruOtherProcs(
01110 IRSymHandle use, IRSymHandle def, ProcHandle proc )
01111 {
01112 OA_ptr<NodeInterface> defNode = mDUG->getNode(def, proc);
01113 OA_ptr<NodeInterface> useNode = mDUG->getNode(use, proc);
01114
01115 std::set<OA_ptr<NodeInterface> > visited;
01116 visited.insert(defNode);
01117 return defNode->isPathFrom(useNode, visited);
01118 }
01119
01120
01121
01122
01129 void ManagerDUGStandard::setDepMatrix4Globals(
01130 IRSymHandle use, IRSymHandle def, ProcHandle proc )
01131 {
01132 std::map<IRSymHandle, std::map<IRSymHandle, bool> >&
01133 depMat = mProcToMatrix[proc];
01134
01135
01136 if (!isOutgoingToOtherProcs(use, proc)) return;
01137
01138
01139 if (!isIncomingFromOtherProcs(def, proc)) return;
01140
01141 #ifdef DEBUG_DUAA
01142 std::cout << "setDepMatrix4Globals, checking Paths(" << mIR->toString(proc) <<
01143 "): " << mIR->toString(use.second) << " -> " << mIR->toString(def.second) << std::endl;
01144 #endif
01145
01146 if (!isPathThruOtherProcs(use, def, proc)) return;
01147
01148 depMat[use][def] = true;
01149 #ifdef DEBUG_DUAA
01150 std::cout << "Value passing globals(" << mIR->toString(proc) << "): "
01151 << mIR->toString(use.second) << " -> " << mIR->toString(def.second)
01152 << std::endl;
01153 #endif
01154 }
01155
01156
01157
01158
01162 void ManagerDUGStandard::transitiveClosureDepMatrix(
01163 OA_ptr<OA::CallGraph::CallGraphInterface> cgraph
01164 )
01165 {
01166
01167
01168 OA_ptr<OA::CallGraph::NodesIteratorInterface> iter;
01169 iter = cgraph->getCallGraphReversePostDFSIterator(DGraph::DEdgeRev);
01170
01171 for ( ; iter->isValid(); ++(*iter)) {
01172 OA_ptr<CallGraph::NodeInterface> node;
01173 node = iter->currentCallGraphNode();
01174 ProcHandle proc = node->getProc();
01175 transitiveClosure(proc);
01176 edgesBetweenFormals(proc);
01177 }
01178 }
01179
01180
01181
01186 void ManagerDUGStandard::transitiveClosure(ProcHandle proc)
01187 {
01188 std::map<IRSymHandle, std::map<IRSymHandle, bool> >& depMat = mProcToMatrix[proc];
01189
01190 #ifdef DEBUG_DUAA
01191 std::cout << "*** transitiveClosure(" << mIR->toString(proc)
01192 << ") ***" << std::endl;
01193 #endif
01194 std::set<IRSymHandle>& irSymSet = mProcToIRSymSet[proc];
01195 std::set<IRSymHandle>::iterator i, j, k;
01196 IRSymHandle use, def, var;
01197 for (k=irSymSet.begin(); k!=irSymSet.end(); k++){
01198 var = *k;
01199 for (i=irSymSet.begin(); i!=irSymSet.end(); i++){
01200 use = *i;
01201 if (!depMat[use][var]) continue;
01202 for (j=irSymSet.begin(); j!=irSymSet.end(); j++){
01203 def = *j;
01204 if (!depMat[var][def]) continue;
01205 depMat[use][def] = true;
01206 }
01207 }
01208 }
01209 #ifdef DEBUG_DUAA
01210 std::cout << "*** END:transitiveClosure(" << mIR->toString(proc)
01211 << ") ***" << std::endl;
01212 #endif
01213 }
01214
01215
01216
01217
01221 void ManagerDUGStandard::edgesBetweenFormals(ProcHandle proc)
01222 {
01223 std::map<IRSymHandle, std::map<IRSymHandle, bool> >& depMat = mProcToMatrix[proc];
01224
01225 std::set<CallHandle>& callsites = mProcToCallsiteSet[proc];
01226 std::set<CallHandle>::iterator callIter;
01227
01228 std::set<SymHandle>& formals = mProcToFormalSet[proc];
01229 std::set<SymHandle>::iterator i, j;
01230
01231 for (i=formals.begin(); i!=formals.end(); i++){
01232 SymHandle formal1 = *i;
01233 for (j=formals.begin(); j!=formals.end(); j++){
01234 SymHandle formal2 = *j;
01235
01236
01237
01238 IRSymHandle formalNodeIn(IRHandle(1), formal1);
01239 IRSymHandle formalNodeOut(IRHandle(2), formal2);
01240
01241
01242
01243
01244
01245 if (!depMat[formalNodeIn][formalNodeOut]){
01246 if (!hasEdgesToOtherProc(formalNodeIn, proc)) continue;
01247 if (!hasEdgesFromOtherProc(formalNodeOut, proc)) continue;
01248 if (isPathThruOtherProcs(formalNodeIn, formalNodeOut, proc)) {
01249 #ifdef DEBUG_DUAA
01250 std::cout << "foundPath(" << mIR->toString(proc) << "): " << mIR->toString(formal1)
01251 << " -> " << mIR->toString(formal2) << std::endl;
01252 #endif
01253 depMat[formalNodeIn][formalNodeOut] = true;
01254 }
01255 }
01256
01257 if (depMat[formalNodeIn][formalNodeOut]){
01258 insertEdge(formalNodeIn, formalNodeOut, PARAM_EDGE, CallHandle(0), proc,
01259 proc, proc);
01260
01261 for (callIter = callsites.begin(); callIter!=callsites.end(); callIter++){
01262 CallHandle call = *callIter;
01263
01264 ProcHandle caller = mCallsiteToProc[call];
01265 assert(caller != ProcHandle(0));
01266 std::set<IRSymHandle>& set1 = mFormalToActualMap[call][formalNodeIn];
01267 std::set<IRSymHandle>& set2 = mFormalToActualMap[call][formalNodeOut];
01268 std::set<IRSymHandle>::iterator i1;
01269 std::set<IRSymHandle>::iterator i2;
01270 for (i1=set1.begin(); i1 != set1.end(); i1++){
01271 for (i2=set2.begin(); i2 != set2.end(); i2++){
01272 setDepMatrix(caller, *i1, *i2);
01273 }
01274 }
01275 }
01276 }
01277 }
01278 }
01279 }
01280
01281
01282
01283 void IndepLocVisitor::visitNamedLoc(NamedLoc& loc)
01284 {
01285 mDUG->insertIndepSymList(loc.getSymHandle(), mProc);
01286 mProcsOfInterest.insert(mProc);
01287
01288 }
01289
01290 void IndepLocVisitor::visitUnnamedLoc(UnnamedLoc& loc)
01291 {
01292
01293 }
01294
01295 void IndepLocVisitor::visitInvisibleLoc(InvisibleLoc& loc)
01296 {
01297 mDUG->insertIndepSymList(loc.getBaseSym(), mProc);
01298 mProcsOfInterest.insert(mProc);
01299 }
01300
01301 void IndepLocVisitor::visitUnknownLoc(UnknownLoc& loc)
01302 {
01303 assert(0);
01304 }
01305
01306 void IndepLocVisitor::visitLocSubSet(LocSubSet& loc)
01307 {
01308 OA_ptr<Location> ll = loc.getBaseLoc();
01309 if (!ll.ptrEqual(0)) { ll->acceptVisitor(*this); }
01310 }
01311
01312 void depLocVisitor::visitNamedLoc(NamedLoc& loc)
01313 {
01314 mDUG->insertDepSymList(loc.getSymHandle(), mProc);
01315 mProcsOfInterest.insert(mProc);
01316
01317 }
01318
01319 void depLocVisitor::visitUnnamedLoc(UnnamedLoc& loc)
01320 {
01321
01322 }
01323
01324 void depLocVisitor::visitInvisibleLoc(InvisibleLoc& loc)
01325 {
01326
01327 mDUG->insertDepSymList(loc.getBaseSym(), mProc);
01328 mProcsOfInterest.insert(mProc);
01329
01330 }
01331
01332 void depLocVisitor::visitUnknownLoc(UnknownLoc& loc)
01333 {
01334 assert(0);
01335 }
01336
01337 void depLocVisitor::visitLocSubSet(LocSubSet& loc)
01338 {
01339 OA_ptr<Location> ll = loc.getBaseLoc();
01340 if (!ll.ptrEqual(0)) { ll->acceptVisitor(*this); }
01341 }
01342
01343
01344
01345
01346 }
01347 }