CFGDFSolver.cpp

Go to the documentation of this file.
00001 
00018 #include "CFGDFSolver.hpp"
00019 #include <Utils/Util.hpp>
00020 
00021 namespace OA {
00022   namespace DataFlow {
00023 
00024 static bool debug = false;
00025 
00026 
00027 CFGDFSolver::CFGDFSolver(DFDirectionType pDirection, CFGDFProblem& prob)
00028     : mDirection(pDirection), mDFProb(prob)
00029 {
00030     OA_DEBUG_CTRL_MACRO("DEBUG_CFGDFSolver:ALL", debug);
00031 }
00032 
00033 OA_ptr<DataFlowSet> CFGDFSolver::solve(OA_ptr<CFG::CFGInterface> cfg, 
00034                                        DFPImplement algorithm)
00035 {
00036     // remove all mappings from statements to in and out sets
00037 //    mStmtInSetMap.clear();
00038 //    mStmtOutSetMap.clear();
00039 
00040     mNodeInSetMap.clear();
00041     mNodeOutSetMap.clear();
00042     mNodeInitTransApp.clear();
00043 
00044     mTop = mDFProb.initializeTop();
00045     
00046     //mBottom = initializeBottom();
00047     
00048     
00049     DataFlow::DGraphSolverDFP::solve(cfg, 
00050             ((mDirection == Forward) ? DGraph::DEdgeOrg : DGraph::DEdgeRev),
00051             algorithm);
00052 
00053 
00054     // if forward then return DataFlowSet for exit
00055     if (mDirection==Forward) {
00056         OA_ptr<CFG::NodeInterface> exitnode = cfg->getExit();
00057         return mNodeInSetMap[exitnode];
00058 
00059     // if backward then return DataFlowSet for entry
00060     } else {
00061         OA_ptr<CFG::NodeInterface> entrynode = cfg->getEntry();
00062         return mNodeOutSetMap[entrynode];
00063     }
00064 }
00065 
00066   
00067 //========================================================
00068 // implementation of DGraphIterativeDFP callbacks
00069 //========================================================
00070   
00071 //--------------------------------------------------------
00072 // initialization upcall 
00073 //--------------------------------------------------------
00074 void CFGDFSolver::initialize(OA_ptr<DGraph::DGraphInterface> dg)
00075 {
00076     
00077     OA_ptr<CFG::CFGInterface> cfg = dg.convert<CFG::CFGInterface>();
00078 
00079 
00080     // iterate over all nodes and call initialization routine
00081     // that sets up DataFlowSets
00082     OA_ptr<CFG::NodesIteratorInterface> nodeIterPtr;
00083     nodeIterPtr = cfg->getCFGNodesIterator();
00084 
00085     
00086 
00087     for ( ;nodeIterPtr->isValid(); ++(*nodeIterPtr) ) {
00088 
00089 
00091         OA_ptr<CFG::NodeInterface> node = nodeIterPtr->currentCFGNode();    
00092         
00093         mNodeInSetMap[node]
00094                      = mDFProb.initializeNodeIN(node);
00095         
00096         mNodeOutSetMap[node]
00097                      = mDFProb.initializeNodeOUT(node);
00098         
00099         mNodeInitTransApp[node] = false;
00100 
00101     }
00102 
00103 }
00104 
00105 
00106 
00107 //--------------------------------------------------------
00108 // solver upcalls
00109 //--------------------------------------------------------
00110 
00111 bool CFGDFSolver::atDGraphNode( OA_ptr<DGraph::NodeInterface> pNode, 
00112                                  DGraph::DGraphEdgeDirection pOrient)
00113 {
00114     bool changed = false;
00115 
00116     OA_ptr<CFG::NodeInterface> node 
00117         = pNode.convert<OA::CFG::NodeInterface>();
00118 
00119     if (debug) {
00120         std::cout << "CFGDFSolver::atDGraphNode: CFG node = ";
00121         std::cout << node->getId() << std::endl;
00122     }
00123 
00124     //-----------------------------------------------------
00125     // do a meet of all out information from nodes that are
00126     // predecessors based on the flow direction
00127     //-----------------------------------------------------
00128     OA_ptr<DataFlowSet> meetPartialResult = mTop->clone();
00129     // added following for ReachConsts, should not bother other flows
00130     // because DFProblem has monotonicity
00131     if (pOrient==DGraph::DEdgeOrg) { // forward
00132         meetPartialResult =
00133             mDFProb.meet(meetPartialResult,mNodeInSetMap[node]);
00134     } else {
00135         meetPartialResult =
00136             mDFProb.meet(meetPartialResult,mNodeOutSetMap[node]);
00137     }
00138 
00139 
00140     // set up iterator for predecessor nodes
00141     OA_ptr<CFG::NodesIteratorInterface> predIterPtr;
00142     if (pOrient==DGraph::DEdgeOrg) {
00143       OA_ptr<CFG::NodesIteratorInterface> it =
00144         node->getCFGPredNodesIterator(); // Stupid Sun CC 5.4
00145       predIterPtr = it.convert<CFG::NodesIteratorInterface>();
00146     } else {
00147       OA_ptr<CFG::NodesIteratorInterface> it =
00148         node->getCFGSuccNodesIterator(); // Stupid Sun CC 5.4
00149       predIterPtr = it.convert<CFG::NodesIteratorInterface>();
00150     }
00151     // iterate over predecessors and do meet operation
00152     for (; predIterPtr->isValid(); ++(*predIterPtr)) {
00153       OA_ptr<CFG::NodeInterface> predNode = predIterPtr->currentCFGNode();
00154       if (pOrient==DGraph::DEdgeOrg) {
00155         meetPartialResult = mDFProb.meet(meetPartialResult, mNodeOutSetMap[predNode]);
00156       } else {
00157         meetPartialResult = mDFProb.meet(meetPartialResult, mNodeInSetMap[predNode]);
00158       }
00159     }
00160 
00161 
00162     // update the appropriate set for this node
00163     if (pOrient==DGraph::DEdgeOrg) { // forward
00164       if ( mNodeInSetMap[node] != meetPartialResult ) {
00165         if (debug) {
00166             std::cout << "%%%%%%%% CFGDFSolver:  There was a change" 
00167                       << std::endl;
00168             std::cout << "\tmNodeInSetMap != meetPartialResult" << std::endl;
00169         }
00170         mNodeInSetMap[node] = meetPartialResult;
00171         changed = true;
00172       }
00173     } else { // reverse
00174       if ( mNodeOutSetMap[node] != meetPartialResult ) {
00175         mNodeOutSetMap[node] = meetPartialResult;
00176         changed = true;
00177       }
00178     }
00179 
00180     // if the data flowing into this node has changed or if the
00181     // transfer functions have never been applied, then
00182     // loop through statements in the CFG node/(Basic Block) 
00183     // calculating the new node out
00184     if (debug) {
00185       std::cout << "\tchanged = " << changed << ", mNITA[node]=" 
00186                 << mNodeInitTransApp[node] << std::endl;
00187     }
00188     if (changed || !mNodeInitTransApp[node]) {
00189       changed = false;  // reuse to determine if there is a change based
00190                         // on the block transfer function
00191       mNodeInitTransApp[node] = true;
00192 
00193       // Forward direction
00194       if (pOrient==DGraph::DEdgeOrg) {
00195         OA_ptr<DataFlowSet> prevOut = mNodeInSetMap[node]->clone();
00196         // loop through statements in forward order
00197         if (debug) {
00198             std::cout << "CFGDFSolver: iterating over statements in node";
00199             std::cout << std::endl;
00200         }
00201         OA_ptr<CFG::NodeStatementsIteratorInterface> stmtIterPtr 
00202             = node->getNodeStatementsIterator();
00203         for (; stmtIterPtr->isValid(); ++(*stmtIterPtr)) {
00204           OA::StmtHandle stmt = stmtIterPtr->current();
00205           if (debug) {
00206               std::cout << "\tstmt(hval=" << stmt.hval() << ")" << std::endl;
00207           }
00208           prevOut = mDFProb.transfer(prevOut, stmt);
00209         }
00210         if (debug) {
00211             std::cout << "CFGDFSolver: done iterating over statements";
00212             std::cout << std::endl;
00213         }
00214 
00215         if (prevOut != mNodeOutSetMap[node] ) {
00216           changed = true;
00217           mNodeOutSetMap[node] = prevOut;
00218           if (debug) {
00219             std::cout << "%%%%%%%% CFGDFSolver:  There was a change" 
00220                       << std::endl;
00221             std::cout << "\tmNodeOutSetMap != prevOut" << std::endl;
00222           }
00223         }
00224       
00225       // Reverse direction
00226       } else { 
00227         OA_ptr<DataFlowSet> prevIn = mNodeOutSetMap[node]->clone();
00228         // loop through statements in reverse order
00229         OA_ptr<CFG::NodeStatementsRevIteratorInterface> stmtIterPtr 
00230             = node->getNodeStatementsRevIterator();
00231         for (; stmtIterPtr->isValid(); ++(*stmtIterPtr)) {
00232           OA::StmtHandle stmt = stmtIterPtr->current();
00233           if (debug) {
00234               std::cout << "\tstmt(hval=" << stmt.hval() << ")" << std::endl;
00235           }
00236           prevIn = mDFProb.transfer(prevIn, stmt);
00237         }
00238         if (prevIn != mNodeInSetMap[node] ) {
00239           changed = true;
00240           mNodeInSetMap[node] = prevIn;
00241         }
00242       }
00243 
00244     }
00245    
00246     if (debug) {
00247       std::cout << "CFGDFSolver::atDGraphNode: changed = " << changed << std::endl;
00248     }
00249     return changed;
00250 }
00251   
00252 //--------------------------------------------------------
00253 // finalization upcalls
00254 //--------------------------------------------------------
00255 void 
00256 CFGDFSolver::finalizeNode(OA_ptr<DGraph::NodeInterface> node)
00257 {
00258 }
00259 
00260 
00261 
00262 //DataFlowSet* CFGDFProblem::transfer(DataFlowSet* in, DGraph::Interface::Node* n)
00263 //{
00264 //}
00265 
00266 //--------------------------------------------------------
00267 // debugging upcall 
00268 //--------------------------------------------------------
00269 void CFGDFSolver::dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir)
00270 {
00290 }
00291 
00292   } // end of DataFlow namespace
00293 }  // end of OA namespace
00294 

Generated on Sat Oct 31 05:21:20 2009 for OpenAnalysis by  doxygen 1.6.1