Fix spelling typo
[binutils.git] / gprof / cg_dfn.c
blob463f22e5d25b8ca386485f46ec8d8768c19c7d47
1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 #include "libiberty.h"
20 #include "gprof.h"
21 #include "search_list.h"
22 #include "source.h"
23 #include "symtab.h"
24 #include "cg_arcs.h"
25 #include "cg_dfn.h"
26 #include "utils.h"
28 #define DFN_INCR_DEPTH (128)
30 typedef struct
32 Sym *sym;
33 int cycle_top;
35 DFN_Stack;
37 static boolean is_numbered PARAMS ((Sym *));
38 static boolean is_busy PARAMS ((Sym *));
39 static void find_cycle PARAMS ((Sym *));
40 static void pre_visit PARAMS ((Sym *));
41 static void post_visit PARAMS ((Sym *));
43 DFN_Stack *dfn_stack = NULL;
44 int dfn_maxdepth = 0;
45 int dfn_depth = 0;
46 int dfn_counter = DFN_NAN;
50 * Is CHILD already numbered?
52 static boolean
53 is_numbered (child)
54 Sym *child;
56 return child->cg.top_order != DFN_NAN && child->cg.top_order != DFN_BUSY;
61 * Is CHILD already busy?
63 static boolean
64 is_busy (child)
65 Sym *child;
67 if (child->cg.top_order == DFN_NAN)
69 return false;
71 return true;
76 * CHILD is part of a cycle. Find the top caller into this cycle
77 * that is not part of the cycle and make all functions in cycle
78 * members of that cycle (top caller == caller with smallest
79 * depth-first number).
81 static void
82 find_cycle (child)
83 Sym *child;
85 Sym *head = 0;
86 Sym *tail;
87 int cycle_top;
88 int index;
90 for (cycle_top = dfn_depth; cycle_top > 0; --cycle_top)
92 head = dfn_stack[cycle_top].sym;
93 if (child == head)
95 break;
97 if (child->cg.cyc.head != child && child->cg.cyc.head == head)
99 break;
102 if (cycle_top <= 0)
104 fprintf (stderr, "[find_cycle] couldn't find head of cycle\n");
105 done (1);
107 #ifdef DEBUG
108 if (debug_level & DFNDEBUG)
110 printf ("[find_cycle] dfn_depth %d cycle_top %d ",
111 dfn_depth, cycle_top);
112 if (head)
114 print_name (head);
116 else
118 printf ("<unknown>");
120 printf ("\n");
122 #endif
123 if (cycle_top == dfn_depth)
126 * This is previous function, e.g. this calls itself. Sort of
127 * boring.
129 * Since we are taking out self-cycles elsewhere no need for
130 * the special case, here.
132 DBG (DFNDEBUG,
133 printf ("[find_cycle] ");
134 print_name (child);
135 printf ("\n"));
137 else
140 * Glom intervening functions that aren't already glommed into
141 * this cycle. Things have been glommed when their cyclehead
142 * field points to the head of the cycle they are glommed
143 * into.
145 for (tail = head; tail->cg.cyc.next; tail = tail->cg.cyc.next)
147 /* void: chase down to tail of things already glommed */
148 DBG (DFNDEBUG,
149 printf ("[find_cycle] tail ");
150 print_name (tail);
151 printf ("\n"));
154 * If what we think is the top of the cycle has a cyclehead
155 * field, then it's not really the head of the cycle, which is
156 * really what we want.
158 if (head->cg.cyc.head != head)
160 head = head->cg.cyc.head;
161 DBG (DFNDEBUG, printf ("[find_cycle] new cyclehead ");
162 print_name (head);
163 printf ("\n"));
165 for (index = cycle_top + 1; index <= dfn_depth; ++index)
167 child = dfn_stack[index].sym;
168 if (child->cg.cyc.head == child)
171 * Not yet glommed anywhere, glom it and fix any
172 * children it has glommed.
174 tail->cg.cyc.next = child;
175 child->cg.cyc.head = head;
176 DBG (DFNDEBUG, printf ("[find_cycle] glomming ");
177 print_name (child);
178 printf (" onto ");
179 print_name (head);
180 printf ("\n"));
181 for (tail = child; tail->cg.cyc.next; tail = tail->cg.cyc.next)
183 tail->cg.cyc.next->cg.cyc.head = head;
184 DBG (DFNDEBUG, printf ("[find_cycle] and its tail ");
185 print_name (tail->cg.cyc.next);
186 printf (" onto ");
187 print_name (head);
188 printf ("\n"));
191 else if (child->cg.cyc.head != head /* firewall */ )
193 fprintf (stderr, "[find_cycle] glommed, but not to head\n");
194 done (1);
202 * Prepare for visiting the children of PARENT. Push a parent onto
203 * the stack and mark it busy.
205 static void
206 pre_visit (parent)
207 Sym *parent;
209 ++dfn_depth;
211 if (dfn_depth >= dfn_maxdepth)
213 dfn_maxdepth += DFN_INCR_DEPTH;
214 dfn_stack = xrealloc (dfn_stack, dfn_maxdepth * sizeof *dfn_stack);
217 dfn_stack[dfn_depth].sym = parent;
218 dfn_stack[dfn_depth].cycle_top = dfn_depth;
219 parent->cg.top_order = DFN_BUSY;
220 DBG (DFNDEBUG, printf ("[pre_visit]\t\t%d:", dfn_depth);
221 print_name (parent);
222 printf ("\n"));
227 * Done with visiting node PARENT. Pop PARENT off dfn_stack
228 * and number functions if PARENT is head of a cycle.
230 static void
231 post_visit (parent)
232 Sym *parent;
234 Sym *member;
236 DBG (DFNDEBUG, printf ("[post_visit]\t%d: ", dfn_depth);
237 print_name (parent);
238 printf ("\n"));
240 * Number functions and things in their cycles unless the function
241 * is itself part of a cycle:
243 if (parent->cg.cyc.head == parent)
245 ++dfn_counter;
246 for (member = parent; member; member = member->cg.cyc.next)
248 member->cg.top_order = dfn_counter;
249 DBG (DFNDEBUG, printf ("[post_visit]\t\tmember ");
250 print_name (member);
251 printf ("-> cg.top_order = %d\n", dfn_counter));
254 else
256 DBG (DFNDEBUG, printf ("[post_visit]\t\tis part of a cycle\n"));
258 --dfn_depth;
263 * Given this PARENT, depth first number its children.
265 void
266 cg_dfn (parent)
267 Sym *parent;
269 Arc *arc;
271 DBG (DFNDEBUG, printf ("[dfn] dfn( ");
272 print_name (parent);
273 printf (")\n"));
275 * If we're already numbered, no need to look any further:
277 if (is_numbered (parent))
279 return;
282 * If we're already busy, must be a cycle:
284 if (is_busy (parent))
286 find_cycle (parent);
287 return;
289 pre_visit (parent);
291 * Recursively visit children:
293 for (arc = parent->cg.children; arc; arc = arc->next_child)
295 cg_dfn (arc->child);
297 post_visit (parent);