cg_dfn.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 1983, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "gprof.h"
  30. #include "libiberty.h"
  31. #include "search_list.h"
  32. #include "source.h"
  33. #include "symtab.h"
  34. #include "cg_arcs.h"
  35. #include "cg_dfn.h"
  36. #include "utils.h"
  37. #define DFN_INCR_DEPTH (128)
  38. typedef struct
  39. {
  40. Sym *sym;
  41. int cycle_top;
  42. }
  43. DFN_Stack;
  44. static bool is_numbered (Sym *);
  45. static bool is_busy (Sym *);
  46. static void find_cycle (Sym *);
  47. static void pre_visit (Sym *);
  48. static void post_visit (Sym *);
  49. DFN_Stack *dfn_stack = NULL;
  50. int dfn_maxdepth = 0;
  51. int dfn_depth = 0;
  52. int dfn_counter = DFN_NAN;
  53. /*
  54. * Is CHILD already numbered?
  55. */
  56. static bool
  57. is_numbered (Sym *child)
  58. {
  59. return child->cg.top_order != DFN_NAN && child->cg.top_order != DFN_BUSY;
  60. }
  61. /*
  62. * Is CHILD already busy?
  63. */
  64. static bool
  65. is_busy (Sym *child)
  66. {
  67. if (child->cg.top_order == DFN_NAN)
  68. {
  69. return false;
  70. }
  71. return true;
  72. }
  73. /*
  74. * CHILD is part of a cycle. Find the top caller into this cycle
  75. * that is not part of the cycle and make all functions in cycle
  76. * members of that cycle (top caller == caller with smallest
  77. * depth-first number).
  78. */
  79. static void
  80. find_cycle (Sym *child)
  81. {
  82. Sym *head = 0;
  83. Sym *tail;
  84. int cycle_top;
  85. int cycle_index;
  86. for (cycle_top = dfn_depth; cycle_top > 0; --cycle_top)
  87. {
  88. head = dfn_stack[cycle_top].sym;
  89. if (child == head)
  90. {
  91. break;
  92. }
  93. if (child->cg.cyc.head != child && child->cg.cyc.head == head)
  94. {
  95. break;
  96. }
  97. }
  98. if (cycle_top <= 0)
  99. {
  100. fprintf (stderr, "[find_cycle] couldn't find head of cycle\n");
  101. done (1);
  102. }
  103. #ifdef DEBUG
  104. if (debug_level & DFNDEBUG)
  105. {
  106. printf ("[find_cycle] dfn_depth %d cycle_top %d ",
  107. dfn_depth, cycle_top);
  108. if (head)
  109. {
  110. print_name (head);
  111. }
  112. else
  113. {
  114. printf ("<unknown>");
  115. }
  116. printf ("\n");
  117. }
  118. #endif
  119. if (cycle_top == dfn_depth)
  120. {
  121. /*
  122. * This is previous function, e.g. this calls itself. Sort of
  123. * boring.
  124. *
  125. * Since we are taking out self-cycles elsewhere no need for
  126. * the special case, here.
  127. */
  128. DBG (DFNDEBUG,
  129. printf ("[find_cycle] ");
  130. print_name (child);
  131. printf ("\n"));
  132. }
  133. else
  134. {
  135. /*
  136. * Glom intervening functions that aren't already glommed into
  137. * this cycle. Things have been glommed when their cyclehead
  138. * field points to the head of the cycle they are glommed
  139. * into.
  140. */
  141. for (tail = head; tail->cg.cyc.next; tail = tail->cg.cyc.next)
  142. {
  143. /* void: chase down to tail of things already glommed */
  144. DBG (DFNDEBUG,
  145. printf ("[find_cycle] tail ");
  146. print_name (tail);
  147. printf ("\n"));
  148. }
  149. /*
  150. * If what we think is the top of the cycle has a cyclehead
  151. * field, then it's not really the head of the cycle, which is
  152. * really what we want.
  153. */
  154. if (head->cg.cyc.head != head)
  155. {
  156. head = head->cg.cyc.head;
  157. DBG (DFNDEBUG, printf ("[find_cycle] new cyclehead ");
  158. print_name (head);
  159. printf ("\n"));
  160. }
  161. for (cycle_index = cycle_top + 1; cycle_index <= dfn_depth; ++cycle_index)
  162. {
  163. child = dfn_stack[cycle_index].sym;
  164. if (child->cg.cyc.head == child)
  165. {
  166. /*
  167. * Not yet glommed anywhere, glom it and fix any
  168. * children it has glommed.
  169. */
  170. tail->cg.cyc.next = child;
  171. child->cg.cyc.head = head;
  172. DBG (DFNDEBUG, printf ("[find_cycle] glomming ");
  173. print_name (child);
  174. printf (" onto ");
  175. print_name (head);
  176. printf ("\n"));
  177. for (tail = child; tail->cg.cyc.next; tail = tail->cg.cyc.next)
  178. {
  179. tail->cg.cyc.next->cg.cyc.head = head;
  180. DBG (DFNDEBUG, printf ("[find_cycle] and its tail ");
  181. print_name (tail->cg.cyc.next);
  182. printf (" onto ");
  183. print_name (head);
  184. printf ("\n"));
  185. }
  186. }
  187. else if (child->cg.cyc.head != head /* firewall */ )
  188. {
  189. fprintf (stderr, "[find_cycle] glommed, but not to head\n");
  190. done (1);
  191. }
  192. }
  193. }
  194. }
  195. /*
  196. * Prepare for visiting the children of PARENT. Push a parent onto
  197. * the stack and mark it busy.
  198. */
  199. static void
  200. pre_visit (Sym *parent)
  201. {
  202. ++dfn_depth;
  203. if (dfn_depth >= dfn_maxdepth)
  204. {
  205. dfn_maxdepth += DFN_INCR_DEPTH;
  206. dfn_stack = (DFN_Stack *) xrealloc (dfn_stack,
  207. dfn_maxdepth * sizeof *dfn_stack);
  208. }
  209. dfn_stack[dfn_depth].sym = parent;
  210. dfn_stack[dfn_depth].cycle_top = dfn_depth;
  211. parent->cg.top_order = DFN_BUSY;
  212. DBG (DFNDEBUG, printf ("[pre_visit]\t\t%d:", dfn_depth);
  213. print_name (parent);
  214. printf ("\n"));
  215. }
  216. /*
  217. * Done with visiting node PARENT. Pop PARENT off dfn_stack
  218. * and number functions if PARENT is head of a cycle.
  219. */
  220. static void
  221. post_visit (Sym *parent)
  222. {
  223. Sym *member;
  224. DBG (DFNDEBUG, printf ("[post_visit]\t%d: ", dfn_depth);
  225. print_name (parent);
  226. printf ("\n"));
  227. /*
  228. * Number functions and things in their cycles unless the function
  229. * is itself part of a cycle:
  230. */
  231. if (parent->cg.cyc.head == parent)
  232. {
  233. ++dfn_counter;
  234. for (member = parent; member; member = member->cg.cyc.next)
  235. {
  236. member->cg.top_order = dfn_counter;
  237. DBG (DFNDEBUG, printf ("[post_visit]\t\tmember ");
  238. print_name (member);
  239. printf ("-> cg.top_order = %d\n", dfn_counter));
  240. }
  241. }
  242. else
  243. {
  244. DBG (DFNDEBUG, printf ("[post_visit]\t\tis part of a cycle\n"));
  245. }
  246. --dfn_depth;
  247. }
  248. /*
  249. * Given this PARENT, depth first number its children.
  250. */
  251. void
  252. cg_dfn (Sym *parent)
  253. {
  254. Arc *arc;
  255. DBG (DFNDEBUG, printf ("[dfn] dfn( ");
  256. print_name (parent);
  257. printf (")\n"));
  258. /*
  259. * If we're already numbered, no need to look any further:
  260. */
  261. if (is_numbered (parent))
  262. {
  263. return;
  264. }
  265. /*
  266. * If we're already busy, must be a cycle:
  267. */
  268. if (is_busy (parent))
  269. {
  270. find_cycle (parent);
  271. return;
  272. }
  273. pre_visit (parent);
  274. /*
  275. * Recursively visit children:
  276. */
  277. for (arc = parent->cg.children; arc; arc = arc->next_child)
  278. {
  279. cg_dfn (arc->child);
  280. }
  281. post_visit (parent);
  282. }