reducible.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "dcc.h"
  9. #include <stdio.h>
  10. #include <malloc.h> /* For free() */
  11. #include <string.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. static void appendNodeInt (queue &pqH, BB *node, interval *pI)
  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 (pI->nodes, node);
  63. /* Update currNode if necessary */
  64. if (pI->currNode == pI->nodes.end())
  65. pI->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. pI->numOutEdges -= (byte)(*found_iter)->inEdges.size() - 1;
  74. pqH.erase(found_iter);
  75. }
  76. }
  77. /* Update interval header information for this basic block */
  78. node->inInterval = pI;
  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. Int i; /* Counter */
  91. queue H; /* Queue of possible header nodes */
  92. boolT first = TRUE; /* First pass through the loop */
  93. appendQueue (H, Gi); /* H = {first node of G} */
  94. Gi->beenOnH = TRUE;
  95. Gi->reachingInt = BB::Create(0,"",c); /* ^ empty BB */
  96. /* Process header nodes list H */
  97. while (!H.empty())
  98. {
  99. header = firstOfQueue (H);
  100. pI = new interval;
  101. pI->numInt = (byte)numInt++;
  102. if (first) /* ^ to first interval */
  103. Ii = J = pI;
  104. appendNodeInt (H, header, pI); /* pI(header) = {header} */
  105. /* Process all nodes in the current interval list */
  106. while ((h = pI->firstOfInt()) != NULL)
  107. {
  108. /* Check all immediate successors of h */
  109. for (i = 0; i < h->edges.size(); i++)
  110. {
  111. succ = h->edges[i].BBptr;
  112. succ->inEdgeCount--;
  113. if (succ->reachingInt == NULL) /* first visit */
  114. {
  115. succ->reachingInt = header;
  116. if (succ->inEdgeCount == 0)
  117. appendNodeInt (H, succ, pI);
  118. else if (! succ->beenOnH) /* out edge */
  119. {
  120. appendQueue (H, succ);
  121. succ->beenOnH = TRUE;
  122. pI->numOutEdges++;
  123. }
  124. }
  125. else /* node has been visited before */
  126. if (succ->inEdgeCount == 0)
  127. {
  128. if (succ->reachingInt == header || succ->inInterval == pI) /* same interval */
  129. {
  130. if (succ != header)
  131. appendNodeInt (H, succ, pI);
  132. }
  133. else /* out edge */
  134. pI->numOutEdges++;
  135. }
  136. else if (succ != header && succ->beenOnH)
  137. pI->numOutEdges++;
  138. }
  139. }
  140. /* Link interval I to list of intervals */
  141. if (! first)
  142. {
  143. J->next = pI;
  144. J = pI;
  145. }
  146. else /* first interval */
  147. first = false;
  148. }
  149. }
  150. /* Displays the intervals of the graph Gi. */
  151. static void displayIntervals (interval *pI)
  152. {
  153. queue::iterator nodePtr;
  154. while (pI)
  155. {
  156. nodePtr = pI->nodes.begin();
  157. printf (" Interval #: %ld\t#OutEdges: %ld\n", pI->numInt, pI->numOutEdges);
  158. while (nodePtr!=pI->nodes.end())
  159. {
  160. if ((*nodePtr)->correspInt == NULL) /* real BBs */
  161. printf (" Node: %ld\n", (*nodePtr)->begin());
  162. else // BBs represent intervals
  163. printf (" Node (corresp int): %d\n", (*nodePtr)->correspInt->numInt);
  164. ++nodePtr;
  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. 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. *curr, /* BB being checked for out edges */
  210. *succ /* Successor node */
  211. ;
  212. //queue *listIi; /* List of intervals */
  213. Int i, /* Index to outEdges array */
  214. j; /* Index to successors */
  215. boolT sameGraph; /* Boolean, isomorphic graphs */
  216. /* Process Gi's intervals */
  217. derSeq_Entry &prev_entry(derivedGi->back());
  218. derivedGi->push_back(derSeq_Entry());
  219. derSeq_Entry &new_entry(derivedGi->back());
  220. Ii = prev_entry.Ii;
  221. sameGraph = TRUE;
  222. BBnode = 0;
  223. std::vector<BB *> bbs;
  224. while (Ii)
  225. {
  226. i = 0;
  227. bbs.push_back(BB::Create(-1, -1, INTERVAL_NODE, Ii->numOutEdges, this));
  228. BBnode = bbs.back();
  229. BBnode->correspInt = Ii;
  230. const queue &listIi(Ii->nodes);
  231. /* Check for more than 1 interval */
  232. if (sameGraph && (listIi.size()>1))
  233. sameGraph = FALSE;
  234. /* Find out edges */
  235. if (BBnode->edges.size() > 0)
  236. {
  237. for(BB *curr : listIi)
  238. {
  239. for (j = 0; j < curr->edges.size(); j++)
  240. {
  241. succ = curr->edges[j].BBptr;
  242. if (succ->inInterval != curr->inInterval)
  243. BBnode->edges[i++].intPtr = succ->inInterval;
  244. }
  245. }
  246. }
  247. /* Next interval */
  248. Ii = Ii->next;
  249. }
  250. /* Convert list of pointers to intervals into a real graph.
  251. * Determines the number of in edges to each new BB, and places it
  252. * in numInEdges and inEdgeCount for later interval processing. */
  253. curr = new_entry.Gi = bbs.front();
  254. for(BB *curr : bbs)
  255. {
  256. for(TYPEADR_TYPE &edge : curr->edges)
  257. {
  258. BBnode = new_entry.Gi; /* BB of an interval */
  259. auto iter= std::find_if(bbs.begin(),bbs.end(),
  260. [&edge](BB *node)->bool { return edge.intPtr==node->correspInt;});
  261. if(iter==bbs.end())
  262. fatalError (INVALID_INT_BB);
  263. edge.BBptr = *iter;
  264. (*iter)->inEdges.push_back(0);
  265. (*iter)->inEdgeCount++;
  266. }
  267. }
  268. return (boolT)(! sameGraph);
  269. }
  270. /* Finds the derived sequence of the graph derivedG->Gi (ie. cfg).
  271. * Constructs the n-th order graph and places all the intermediate graphs
  272. * in the derivedG list sequence. */
  273. byte Function::findDerivedSeq (derSeq *derivedGi)
  274. {
  275. BB *Gi; /* Current derived sequence graph */
  276. derSeq::iterator iter=derivedGi->begin();
  277. Gi = iter->Gi;
  278. while (! trivialGraph (Gi))
  279. {
  280. /* Find the intervals of Gi and place them in derivedGi->Ii */
  281. iter->findIntervals(this);
  282. /* Create Gi+1 and check if it is equivalent to Gi */
  283. if (! nextOrderGraph (derivedGi))
  284. break;
  285. ++iter;
  286. Gi = iter->Gi;
  287. stats.nOrder++;
  288. }
  289. if (! trivialGraph (Gi))
  290. {
  291. ++iter;
  292. derivedGi->erase(iter,derivedGi->end()); /* remove Gi+1 */
  293. // freeDerivedSeq(derivedGi->next);
  294. // derivedGi->next = NULL;
  295. return FALSE;
  296. }
  297. derivedGi->back().findIntervals (this);
  298. return TRUE;
  299. }
  300. /* Converts the irreducible graph G into an equivalent reducible one, by
  301. * means of node splitting. */
  302. static void nodeSplitting (std::vector<BB *> &G)
  303. {
  304. fprintf(stderr,"Attempt to perform node splitting: NOT IMPLEMENTED\n");
  305. }
  306. /* Displays the derived sequence and intervals of the graph G */
  307. void derSeq::display()
  308. {
  309. Int n = 1; /* Derived sequence number */
  310. printf ("\nDerived Sequence Intervals\n");
  311. derSeq::iterator iter=this->begin();
  312. while (iter!=this->end())
  313. {
  314. printf ("\nIntervals for G%lX\n", n++);
  315. displayIntervals (iter->Ii);
  316. ++iter;
  317. }
  318. }
  319. /* Checks whether the control flow graph, cfg, is reducible or not.
  320. * If it is not reducible, it is converted into an equivalent reducible
  321. * graph by node splitting. The derived sequence of graphs built from cfg
  322. * are returned in the pointer *derivedG.
  323. */
  324. derSeq * Function::checkReducibility()
  325. {
  326. derSeq * der_seq;
  327. byte reducible; /* Reducible graph flag */
  328. numInt = 1; /* reinitialize no. of intervals*/
  329. stats.nOrder = 1; /* nOrder(cfg) = 1 */
  330. der_seq = new derSeq;
  331. der_seq->resize(1);
  332. der_seq->back().Gi = m_cfg.front();
  333. reducible = findDerivedSeq(der_seq);
  334. if (! reducible)
  335. {
  336. flg |= GRAPH_IRRED;
  337. nodeSplitting (m_cfg);
  338. }
  339. return der_seq;
  340. }