RIFG.cpp

Go to the documentation of this file.
00001 
00023 //************************** System Include Files ***************************
00024 
00025 #ifdef NO_STD_CHEADERS
00026 # include <assert.h>
00027 # include <limits.h>
00028 #else
00029 # include <cassert>
00030 # include <climits>
00031 #endif
00032 
00033 #include <inttypes.h>
00034 
00035 //*************************** User Include Files ****************************
00036 
00037 #include <OpenAnalysis/Utils/RIFG.hpp>
00038 #include <OpenAnalysis/CFG/CFGInterface.hpp>
00039 
00040 //*************************** Forward Declarations **************************
00041 
00042 //***************************************************************************
00043 
00044 //***************************************************************************
00045 // Representation Independent Flowgraph
00046 //***************************************************************************
00047 
00048 namespace OA {
00049 
00050 unsigned int RIFG::NIL = 0;
00051 
00052 RIFG::RIFG(OA_ptr<DGraph::DGraphInterface> graph_, 
00053            OA_ptr<DGraph::NodeInterface> source,
00054            OA_ptr<DGraph::NodeInterface> sink)
00055   : graph(graph_), mSource(source), mSink(sink)
00056 {
00057   // Sanity check
00058   assert(graph->getNumNodes() <= UINT_MAX);
00059   assert(graph->getNumEdges() <= UINT_MAX);
00060   
00061   // Initialize the node-id to node map: ids are 1...num_nodes
00062   highWaterMarkNodeId = graph->getNumNodes();
00063   OA::OA_ptr<OA::DGraph::NodesIteratorInterface> it1 = 
00064     graph->getNodesIterator();
00065   for (NodeId i = 1; it1->isValid(); ++(*it1), ++i) {
00066     OA::OA_ptr<OA::DGraph::NodeInterface> node = it1->current();
00067     node_to_id_map[node] = i;
00068     id_to_node_map[i] = node;
00069   }
00070   
00071   // Initialize the edge-id to edge map: ids are 1...num_edges
00072   highWaterMarkEdgeId = graph->getNumEdges();
00073   OA::OA_ptr<OA::DGraph::EdgesIteratorInterface> it2 = 
00074     graph->getEdgesIterator();
00075   for (NodeId i = 1; it2->isValid(); ++(*it2), ++i) {
00076     OA::OA_ptr<OA::DGraph::EdgeInterface> edge = it2->current();
00077     edge_to_id_map[edge] = i;
00078     id_to_edge_map[i] = edge;
00079   }
00080 }
00081 
00082 
00083 RIFG::~RIFG()
00084 {
00085   node_to_id_map.clear();
00086   id_to_node_map.clear();
00087   edge_to_id_map.clear();
00088   id_to_edge_map.clear();
00089 }
00090 
00091 
00092 bool 
00093 RIFG::isValid(RIFG::NodeId nid) const
00094 {
00095   return true;
00096 }
00097 
00098 
00099 RIFG::NodeId 
00100 RIFG::getSource() const
00101 {
00102   return (getNodeId(mSource));
00103 }
00104 
00105 
00106 RIFG::NodeId 
00107 RIFG::getSink() const
00108 {
00109   return (getNodeId(mSink));
00110 }
00111 
00112 RIFG::NodeId 
00113 RIFG::getEdgeSrc(RIFG::EdgeId eid) const
00114 {
00115   OA::OA_ptr<OA::DGraph::EdgeInterface> e = getEdge(eid);
00116   OA::OA_ptr<OA::DGraph::NodeInterface> n = e->getSource();
00117   return (getNodeId(n));
00118 }
00119 
00120 
00121 RIFG::NodeId 
00122 RIFG::getEdgeSink(RIFG::EdgeId eid) const
00123 {
00124   OA::OA_ptr<OA::DGraph::EdgeInterface> e = getEdge(eid);
00125   OA::OA_ptr<OA::DGraph::NodeInterface> n = e->getSink();
00126   return (getNodeId(n));
00127 }
00128 
00129 
00130 OA_ptr<RIFG::NodesIterator>
00131 RIFG::getNodesIterator() const
00132 {
00133   OA_ptr<RIFG::NodesIterator> it;
00134   it = new NodesIterator(*this);
00135   return it;
00136 }
00137 
00138 
00139 OA_ptr<RIFG::IncomingEdgesIterator>
00140 RIFG::getIncomingEdgesIterator(NodeId nid) const
00141 {
00142   OA_ptr<RIFG::IncomingEdgesIterator> it;
00143   it = new IncomingEdgesIterator(*this, nid);
00144   return it;
00145 }
00146 
00147 
00148 OA_ptr<RIFG::OutgoingEdgesIterator>
00149 RIFG::getOutgoingEdgesIterator(NodeId nid) const
00150 {
00151   OA_ptr<RIFG::OutgoingEdgesIterator> it;
00152   it = new OutgoingEdgesIterator(*this, nid);
00153   return it;
00154 }
00155 
00156 
00157 void 
00158 RIFG::dumpNode(std::ostream& os, RIFG::NodeId nid)
00159 {
00160   // getNode(nid)->dump(os);
00161 }
00162 
00163 
00164 //***************************************************************************
00165 // 
00166 //***************************************************************************
00167 
00168 OA_ptr<DGraph::NodeInterface> 
00169 RIFG::getSourceNode(OA_ptr<DGraph::DGraphInterface> graph)
00170 {
00171   if (graph.isa<OA::CFG::CFGInterface>()) {
00172     OA_ptr<OA::CFG::CFGInterface> g = graph.convert<OA::CFG::CFGInterface>();
00173     OA_ptr<OA::CFG::NodeInterface> n = g->getEntry();
00174     return n;
00175   }
00176   else {
00177     OA_ptr<DGraph::NodesIteratorInterface> it = 
00178       graph->getEntryNodesIterator();
00179     assert(it->isValid());
00180     OA_ptr<DGraph::NodeInterface> n = it->current();
00181     return n;
00182   }
00183 }
00184 
00185 OA_ptr<DGraph::NodeInterface> 
00186 RIFG::getSinkNode(OA_ptr<DGraph::DGraphInterface> graph)
00187 {
00188   if (graph.isa<OA::CFG::CFGInterface>()) {
00189     OA_ptr<OA::CFG::CFGInterface> g = graph.convert<OA::CFG::CFGInterface>();
00190     OA_ptr<OA::CFG::NodeInterface> n = g->getExit();
00191     return n;
00192   }
00193   else {
00194     OA_ptr<DGraph::NodesIteratorInterface> it = 
00195       graph->getExitNodesIterator();
00196     assert(it->isValid());
00197     OA_ptr<DGraph::NodeInterface> n = it->current();
00198     return n;
00199   }
00200 }
00201 
00202 } // end of namespace OA
00203