reducible.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 -= (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 = 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 = (uint8_t)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 (size_t 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. while (pI)
  154. {
  155. printf (" Interval #: %d\t#OutEdges: %d\n", pI->numInt, pI->numOutEdges);
  156. for(BB *node : pI->nodes)
  157. {
  158. if (node->correspInt == NULL) /* real BBs */
  159. printf (" Node: %d\n", node->begin()->loc_ip);
  160. else // BBs represent intervals
  161. printf (" Node (corresp int): %d\n", node->correspInt->numInt);
  162. }
  163. pI = pI->next;
  164. }
  165. }
  166. /* Allocates space for a new derSeq node. */
  167. //static derSeq_Entry *newDerivedSeq()
  168. //{
  169. // return new derSeq_Entry;
  170. //}
  171. /* Frees the storage allocated for the queue q*/
  172. //static void freeQueue (queue &q)
  173. //{
  174. // q.clear();
  175. //}
  176. /* Frees the storage allocated for the interval pI */
  177. static void freeInterval (interval **pI)
  178. {
  179. interval *Iptr;
  180. while (*pI)
  181. {
  182. (*pI)->nodes.clear();
  183. Iptr = *pI;
  184. *pI = (*pI)->next;
  185. delete (Iptr);
  186. }
  187. }
  188. /* Frees the storage allocated by the derived sequence structure, except
  189. * for the original graph cfg (derivedG->Gi). */
  190. void freeDerivedSeq(derSeq &derivedG)
  191. {
  192. derivedG.clear();
  193. }
  194. derSeq_Entry::~derSeq_Entry()
  195. {
  196. freeInterval (&Ii);
  197. // if(Gi && Gi->nodeType == INTERVAL_NODE)
  198. // freeCFG (Gi);
  199. }
  200. /* Finds the next order graph of derivedGi->Gi according to its intervals
  201. * (derivedGi->Ii), and places it in derivedGi->next->Gi. */
  202. bool Function::nextOrderGraph (derSeq &derivedGi)
  203. {
  204. interval *Ii; /* Interval being processed */
  205. BB *BBnode, /* New basic block of intervals */
  206. //*curr, /* BB being checked for out edges */
  207. *succ /* Successor node */
  208. ;
  209. //queue *listIi; /* List of intervals */
  210. int i; /* Index to outEdges array */
  211. /*j;*/ /* Index to successors */
  212. boolT sameGraph; /* Boolean, isomorphic graphs */
  213. /* Process Gi's intervals */
  214. derSeq_Entry &prev_entry(derivedGi.back());
  215. derivedGi.push_back(derSeq_Entry());
  216. derSeq_Entry &new_entry(derivedGi.back());
  217. Ii = prev_entry.Ii;
  218. sameGraph = true;
  219. BBnode = 0;
  220. std::vector<BB *> bbs;
  221. while (Ii)
  222. {
  223. i = 0;
  224. bbs.push_back(BB::Create(-1, -1, INTERVAL_NODE, Ii->numOutEdges, this));
  225. BBnode = bbs.back();
  226. BBnode->correspInt = Ii;
  227. const queue &listIi(Ii->nodes);
  228. /* Check for more than 1 interval */
  229. if (sameGraph && (listIi.size()>1))
  230. sameGraph = false;
  231. /* Find out edges */
  232. if (BBnode->edges.size() > 0)
  233. {
  234. for(BB *curr : listIi)
  235. {
  236. for (size_t j = 0; j < curr->edges.size(); j++)
  237. {
  238. succ = curr->edges[j].BBptr;
  239. if (succ->inInterval != curr->inInterval)
  240. BBnode->edges[i++].intPtr = succ->inInterval;
  241. }
  242. }
  243. }
  244. /* Next interval */
  245. Ii = Ii->next;
  246. }
  247. /* Convert list of pointers to intervals into a real graph.
  248. * Determines the number of in edges to each new BB, and places it
  249. * in numInEdges and inEdgeCount for later interval processing. */
  250. //curr = new_entry.Gi = bbs.front();
  251. new_entry.Gi = bbs.front();
  252. for(BB *curr : bbs)
  253. {
  254. for(TYPEADR_TYPE &edge : curr->edges)
  255. {
  256. BBnode = new_entry.Gi; /* BB of an interval */
  257. auto iter= std::find_if(bbs.begin(),bbs.end(),
  258. [&edge](BB *node)->bool { return edge.intPtr==node->correspInt;});
  259. if(iter==bbs.end())
  260. fatalError (INVALID_INT_BB);
  261. edge.BBptr = *iter;
  262. (*iter)->inEdges.push_back((BB *)nullptr);
  263. (*iter)->inEdgeCount++;
  264. }
  265. }
  266. return (boolT)(! sameGraph);
  267. }
  268. /* Finds the derived sequence of the graph derivedG->Gi (ie. cfg).
  269. * Constructs the n-th order graph and places all the intermediate graphs
  270. * in the derivedG list sequence. */
  271. uint8_t Function::findDerivedSeq (derSeq &derivedGi)
  272. {
  273. BB *Gi; /* Current derived sequence graph */
  274. derSeq::iterator iter=derivedGi.begin();
  275. assert(iter!=derivedGi.end());
  276. Gi = iter->Gi;
  277. while (! trivialGraph (Gi))
  278. {
  279. /* Find the intervals of Gi and place them in derivedGi->Ii */
  280. iter->findIntervals(this);
  281. /* Create Gi+1 and check if it is equivalent to Gi */
  282. if (! nextOrderGraph (derivedGi))
  283. break;
  284. ++iter;
  285. Gi = iter->Gi;
  286. stats.nOrder++;
  287. }
  288. if (! trivialGraph (Gi))
  289. {
  290. ++iter;
  291. derivedGi.erase(iter,derivedGi.end()); /* remove Gi+1 */
  292. // freeDerivedSeq(derivedGi->next);
  293. // derivedGi->next = NULL;
  294. return false;
  295. }
  296. derivedGi.back().findIntervals (this);
  297. return true;
  298. }
  299. /* Converts the irreducible graph G into an equivalent reducible one, by
  300. * means of node splitting. */
  301. static void nodeSplitting (std::list<BB *> &/*G*/)
  302. {
  303. fprintf(stderr,"Attempt to perform node splitting: NOT IMPLEMENTED\n");
  304. }
  305. /* Displays the derived sequence and intervals of the graph G */
  306. void derSeq::display()
  307. {
  308. int n = 1; /* Derived sequence number */
  309. printf ("\nDerived Sequence Intervals\n");
  310. derSeq::iterator iter=this->begin();
  311. while (iter!=this->end())
  312. {
  313. printf ("\nIntervals for G%X\n", n++);
  314. displayIntervals (iter->Ii);
  315. ++iter;
  316. }
  317. }
  318. /* Checks whether the control flow graph, cfg, is reducible or not.
  319. * If it is not reducible, it is converted into an equivalent reducible
  320. * graph by node splitting. The derived sequence of graphs built from cfg
  321. * are returned in the pointer *derivedG.
  322. */
  323. derSeq * Function::checkReducibility()
  324. {
  325. derSeq * der_seq;
  326. uint8_t reducible; /* Reducible graph flag */
  327. numInt = 1; /* reinitialize no. of intervals*/
  328. stats.nOrder = 1; /* nOrder(cfg) = 1 */
  329. der_seq = new derSeq;
  330. der_seq->resize(1);
  331. der_seq->back().Gi = m_cfg.front();
  332. reducible = findDerivedSeq(*der_seq);
  333. if (! reducible)
  334. {
  335. flg |= GRAPH_IRRED;
  336. nodeSplitting (m_cfg);
  337. }
  338. return der_seq;
  339. }