reducible.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /********************************************************************
  2. * Checks for reducibility of a graph by intervals, and
  3. * constructs an equivalent reducible graph if one is not found.
  4. * (C) Cristina Cifuentes
  5. ********************************************************************/
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <stdint.h>
  11. #include "dcc.h"
  12. static int numInt; /* Number of intervals */
  13. #define nonEmpty(q) (q != NULL)
  14. /* Returns whether the queue q is empty or not */
  15. bool trivialGraph(BB *G)
  16. {
  17. return G->edges.empty();
  18. }
  19. /* Returns whether the graph is a trivial graph or not */
  20. /* Returns the first element in the queue Q, and removes this element
  21. * from the list. Q is not an empty queue. */
  22. static BB *firstOfQueue (queue &Q)
  23. {
  24. assert(!Q.empty());
  25. BB *res=Q.front();
  26. Q.pop_front();
  27. return res;
  28. }
  29. /* Appends pointer to node at the end of the queue Q if node is not present
  30. * in this queue. Returns the queue node just appended. */
  31. queue::iterator appendQueue (queue &Q, BB *node)
  32. {
  33. auto iter=std::find(Q.begin(),Q.end(),node);
  34. if(iter!=Q.end())
  35. return iter;
  36. Q.push_back(node);
  37. iter=Q.end();
  38. --iter;
  39. return iter;
  40. }
  41. /* Returns the next unprocessed node of the interval list (pointed to by
  42. * pI->currNode). Removes this element logically from the list, by updating
  43. * the currNode pointer to the next unprocessed element. */
  44. BB *interval::firstOfInt ()
  45. {
  46. auto pq = currNode;
  47. if (pq == nodes.end())
  48. return 0;
  49. ++currNode;
  50. return *pq;
  51. }
  52. /* Appends node @node to the end of the interval list @pI, updates currNode
  53. * if necessary, and removes the node from the header list @pqH if it is
  54. * there. The interval header information is placed in the field
  55. * node->inInterval.
  56. * Note: nodes are added to the interval list in interval order (which
  57. * topsorts the dominance relation). */
  58. void interval::appendNodeInt(queue &pqH, BB *node)
  59. {
  60. queue::iterator pq; /* Pointer to current node of the list */
  61. /* Append node if it is not already in the interval list */
  62. pq = appendQueue (nodes, node);
  63. /* Update currNode if necessary */
  64. if (currNode == nodes.end())
  65. currNode = pq;
  66. /* Check header list for occurrence of node, if found, remove it
  67. * and decrement number of out-edges from this interval. */
  68. if (node->beenOnH && !pqH.empty())
  69. {
  70. auto found_iter=std::find(pqH.begin(),pqH.end(),node);
  71. if(found_iter!=pqH.end())
  72. {
  73. numOutEdges -= (uint8_t)(*found_iter)->inEdges.size() - 1;
  74. pqH.erase(found_iter);
  75. }
  76. }
  77. /* Update interval header information for this basic block */
  78. node->inInterval = this;
  79. }
  80. /* Finds the intervals of graph derivedGi->Gi and places them in the list
  81. * of intervals derivedGi->Ii.
  82. * Algorithm by M.S.Hecht. */
  83. void derSeq_Entry::findIntervals (Function *c)
  84. {
  85. interval *pI, /* Interval being processed */
  86. *J; /* ^ last interval in derivedGi->Ii */
  87. BB *h, /* Node being processed */
  88. *header, /* Current interval's header node */
  89. *succ; /* Successor basic block */
  90. queue H; /* Queue of possible header nodes */
  91. boolT first = true; /* First pass through the loop */
  92. appendQueue (H, Gi); /* H = {first node of G} */
  93. Gi->beenOnH = true;
  94. Gi->reachingInt = BB::Create(0,"",c); /* ^ empty BB */
  95. /* Process header nodes list H */
  96. while (!H.empty())
  97. {
  98. header = firstOfQueue (H);
  99. pI = new interval;
  100. pI->numInt = (uint8_t)numInt++;
  101. if (first) /* ^ to first interval */
  102. {
  103. Ii = J = pI;
  104. m_intervals.push_back(pI);
  105. }
  106. pI->appendNodeInt (H, header); /* pI(header) = {header} */
  107. /* Process all nodes in the current interval list */
  108. while ((h = pI->firstOfInt()) != NULL)
  109. {
  110. /* Check all immediate successors of h */
  111. for (size_t i = 0; i < h->edges.size(); i++)
  112. {
  113. succ = h->edges[i].BBptr;
  114. succ->inEdgeCount--;
  115. if (succ->reachingInt == NULL) /* first visit */
  116. {
  117. succ->reachingInt = header;
  118. if (succ->inEdgeCount == 0)
  119. pI->appendNodeInt (H, succ);
  120. else if (! succ->beenOnH) /* out edge */
  121. {
  122. appendQueue (H, succ);
  123. succ->beenOnH = true;
  124. pI->numOutEdges++;
  125. }
  126. }
  127. else /* node has been visited before */
  128. if (succ->inEdgeCount == 0)
  129. {
  130. if (succ->reachingInt == header || succ->inInterval == pI) /* same interval */
  131. {
  132. if (succ != header)
  133. pI->appendNodeInt (H, succ);
  134. }
  135. else /* out edge */
  136. pI->numOutEdges++;
  137. }
  138. else if (succ != header && succ->beenOnH)
  139. pI->numOutEdges++;
  140. }
  141. }
  142. /* Link interval I to list of intervals */
  143. if (! first)
  144. {
  145. m_intervals.push_back(pI);
  146. J->next = pI;
  147. J = pI;
  148. }
  149. else /* first interval */
  150. first = false;
  151. }
  152. }
  153. /* Displays the intervals of the graph Gi. */
  154. static void displayIntervals (interval *pI)
  155. {
  156. while (pI)
  157. {
  158. printf (" Interval #: %d\t#OutEdges: %d\n", pI->numInt, pI->numOutEdges);
  159. for(BB *node : pI->nodes)
  160. {
  161. if (node->correspInt == NULL) /* real BBs */
  162. printf (" Node: %d\n", node->begin()->loc_ip);
  163. else // BBs represent intervals
  164. printf (" Node (corresp int): %d\n", node->correspInt->numInt);
  165. }
  166. pI = pI->next;
  167. }
  168. }
  169. /* Allocates space for a new derSeq node. */
  170. //static derSeq_Entry *newDerivedSeq()
  171. //{
  172. // return new derSeq_Entry;
  173. //}
  174. /* Frees the storage allocated for the queue q*/
  175. //static void freeQueue (queue &q)
  176. //{
  177. // q.clear();
  178. //}
  179. /* Frees the storage allocated for the interval pI */
  180. static void freeInterval (interval **pI)
  181. {
  182. interval *Iptr;
  183. while (*pI)
  184. {
  185. (*pI)->nodes.clear();
  186. Iptr = *pI;
  187. *pI = (*pI)->next;
  188. delete (Iptr);
  189. }
  190. }
  191. /* Frees the storage allocated by the derived sequence structure, except
  192. * for the original graph cfg (derivedG->Gi). */
  193. void freeDerivedSeq(derSeq &derivedG)
  194. {
  195. derivedG.clear();
  196. }
  197. derSeq_Entry::~derSeq_Entry()
  198. {
  199. freeInterval (&Ii);
  200. // if(Gi && Gi->nodeType == INTERVAL_NODE)
  201. // freeCFG (Gi);
  202. }
  203. /* Finds the next order graph of derivedGi->Gi according to its intervals
  204. * (derivedGi->Ii), and places it in derivedGi->next->Gi. */
  205. bool Function::nextOrderGraph (derSeq &derivedGi)
  206. {
  207. interval *Ii; /* Interval being processed */
  208. BB *BBnode; /* New basic block of intervals */
  209. bool sameGraph; /* Boolean, isomorphic graphs */
  210. /* Process Gi's intervals */
  211. derSeq_Entry &prev_entry(derivedGi.back());
  212. derivedGi.push_back(derSeq_Entry());
  213. derSeq_Entry &new_entry(derivedGi.back());
  214. sameGraph = true;
  215. BBnode = 0;
  216. std::vector<BB *> bbs;
  217. for(Ii = prev_entry.Ii; Ii != nullptr; Ii = Ii->next)
  218. {
  219. BBnode = BB::CreateIntervalBB(this);
  220. BBnode->correspInt = Ii;
  221. bbs.push_back(BBnode);
  222. const queue &listIi(Ii->nodes);
  223. /* Check for more than 1 interval */
  224. if (sameGraph && (listIi.size()>1))
  225. sameGraph = false;
  226. /* Find out edges */
  227. if (Ii->numOutEdges <= 0)
  228. continue;
  229. for(BB *curr : listIi)
  230. {
  231. for (size_t j = 0; j < curr->edges.size(); j++)
  232. {
  233. BB *successor_node = curr->edges[j].BBptr;
  234. if (successor_node->inInterval != curr->inInterval)
  235. BBnode->addOutEdgeInterval(successor_node->inInterval);
  236. }
  237. }
  238. }
  239. /* Convert list of pointers to intervals into a real graph.
  240. * Determines the number of in edges to each new BB, and places it
  241. * in numInEdges and inEdgeCount for later interval processing. */
  242. //curr = new_entry.Gi = bbs.front();
  243. new_entry.Gi = bbs.front();
  244. for(BB *curr : bbs)
  245. {
  246. for(TYPEADR_TYPE &edge : curr->edges)
  247. {
  248. BBnode = new_entry.Gi; /* BB of an interval */
  249. auto iter= std::find_if(bbs.begin(),bbs.end(),
  250. [&edge](BB *node)->bool { return edge.intPtr==node->correspInt;});
  251. if(iter==bbs.end())
  252. fatalError (INVALID_INT_BB);
  253. edge.BBptr = *iter;
  254. (*iter)->inEdges.push_back((BB *)nullptr);
  255. (*iter)->inEdgeCount++;
  256. }
  257. }
  258. return not sameGraph;
  259. }
  260. /* Finds the derived sequence of the graph derivedG->Gi (ie. cfg).
  261. * Constructs the n-th order graph and places all the intermediate graphs
  262. * in the derivedG list sequence. */
  263. uint8_t Function::findDerivedSeq (derSeq &derivedGi)
  264. {
  265. BB *Gi; /* Current derived sequence graph */
  266. derSeq::iterator iter=derivedGi.begin();
  267. assert(iter!=derivedGi.end());
  268. Gi = iter->Gi;
  269. while (! trivialGraph (Gi))
  270. {
  271. /* Find the intervals of Gi and place them in derivedGi->Ii */
  272. iter->findIntervals(this);
  273. /* Create Gi+1 and check if it is equivalent to Gi */
  274. if (! nextOrderGraph (derivedGi))
  275. break;
  276. ++iter;
  277. Gi = iter->Gi;
  278. stats.nOrder++;
  279. }
  280. if (! trivialGraph (Gi))
  281. {
  282. ++iter;
  283. derivedGi.erase(iter,derivedGi.end()); /* remove Gi+1 */
  284. // freeDerivedSeq(derivedGi->next);
  285. // derivedGi->next = NULL;
  286. return false;
  287. }
  288. derivedGi.back().findIntervals (this);
  289. return true;
  290. }
  291. /* Displays the derived sequence and intervals of the graph G */
  292. void derSeq::display()
  293. {
  294. int n = 1; /* Derived sequence number */
  295. printf ("\nDerived Sequence Intervals\n");
  296. derSeq::iterator iter=this->begin();
  297. while (iter!=this->end())
  298. {
  299. printf ("\nIntervals for G%X\n", n++);
  300. displayIntervals (iter->Ii);
  301. ++iter;
  302. }
  303. }
  304. /* Checks whether the control flow graph, cfg, is reducible or not.
  305. * If it is not reducible, it is converted into an equivalent reducible
  306. * graph by node splitting. The derived sequence of graphs built from cfg
  307. * are returned in the pointer *derivedG.
  308. */
  309. derSeq * Function::checkReducibility()
  310. {
  311. derSeq * der_seq;
  312. uint8_t reducible; /* Reducible graph flag */
  313. numInt = 1; /* reinitialize no. of intervals*/
  314. stats.nOrder = 1; /* nOrder(cfg) = 1 */
  315. der_seq = new derSeq;
  316. der_seq->resize(1);
  317. der_seq->back().Gi = *m_actual_cfg.begin(); /*m_cfg.front()*/;
  318. reducible = findDerivedSeq(*der_seq);
  319. if (! reducible)
  320. {
  321. flg |= GRAPH_IRRED;
  322. m_actual_cfg.nodeSplitting();
  323. }
  324. return der_seq;
  325. }