12a6b7db3Sskrll /*
22a6b7db3Sskrll * Copyright (c) 1983, 1993
32a6b7db3Sskrll * The Regents of the University of California. All rights reserved.
42a6b7db3Sskrll *
52a6b7db3Sskrll * Redistribution and use in source and binary forms, with or without
62a6b7db3Sskrll * modification, are permitted provided that the following conditions
72a6b7db3Sskrll * are met:
82a6b7db3Sskrll * 1. Redistributions of source code must retain the above copyright
92a6b7db3Sskrll * notice, this list of conditions and the following disclaimer.
102a6b7db3Sskrll * 2. Redistributions in binary form must reproduce the above copyright
112a6b7db3Sskrll * notice, this list of conditions and the following disclaimer in the
122a6b7db3Sskrll * documentation and/or other materials provided with the distribution.
132a6b7db3Sskrll * 3. Neither the name of the University nor the names of its contributors
142a6b7db3Sskrll * may be used to endorse or promote products derived from this software
152a6b7db3Sskrll * without specific prior written permission.
162a6b7db3Sskrll *
172a6b7db3Sskrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
182a6b7db3Sskrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192a6b7db3Sskrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202a6b7db3Sskrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
212a6b7db3Sskrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222a6b7db3Sskrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232a6b7db3Sskrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242a6b7db3Sskrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252a6b7db3Sskrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262a6b7db3Sskrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272a6b7db3Sskrll * SUCH DAMAGE.
282a6b7db3Sskrll */
292a6b7db3Sskrll #include "gprof.h"
302a6b7db3Sskrll #include "libiberty.h"
312a6b7db3Sskrll #include "search_list.h"
322a6b7db3Sskrll #include "source.h"
332a6b7db3Sskrll #include "symtab.h"
342a6b7db3Sskrll #include "cg_arcs.h"
352a6b7db3Sskrll #include "cg_dfn.h"
362a6b7db3Sskrll #include "utils.h"
372a6b7db3Sskrll
382a6b7db3Sskrll #define DFN_INCR_DEPTH (128)
392a6b7db3Sskrll
402a6b7db3Sskrll typedef struct
412a6b7db3Sskrll {
422a6b7db3Sskrll Sym *sym;
432a6b7db3Sskrll int cycle_top;
442a6b7db3Sskrll }
452a6b7db3Sskrll DFN_Stack;
462a6b7db3Sskrll
47*4f645668Schristos static bool is_numbered (Sym *);
48*4f645668Schristos static bool is_busy (Sym *);
492a6b7db3Sskrll static void find_cycle (Sym *);
502a6b7db3Sskrll static void pre_visit (Sym *);
512a6b7db3Sskrll static void post_visit (Sym *);
522a6b7db3Sskrll
532a6b7db3Sskrll DFN_Stack *dfn_stack = NULL;
542a6b7db3Sskrll int dfn_maxdepth = 0;
552a6b7db3Sskrll int dfn_depth = 0;
562a6b7db3Sskrll int dfn_counter = DFN_NAN;
572a6b7db3Sskrll
582a6b7db3Sskrll
592a6b7db3Sskrll /*
602a6b7db3Sskrll * Is CHILD already numbered?
612a6b7db3Sskrll */
62*4f645668Schristos static bool
is_numbered(Sym * child)632a6b7db3Sskrll is_numbered (Sym *child)
642a6b7db3Sskrll {
652a6b7db3Sskrll return child->cg.top_order != DFN_NAN && child->cg.top_order != DFN_BUSY;
662a6b7db3Sskrll }
672a6b7db3Sskrll
682a6b7db3Sskrll
692a6b7db3Sskrll /*
702a6b7db3Sskrll * Is CHILD already busy?
712a6b7db3Sskrll */
72*4f645668Schristos static bool
is_busy(Sym * child)732a6b7db3Sskrll is_busy (Sym *child)
742a6b7db3Sskrll {
752a6b7db3Sskrll if (child->cg.top_order == DFN_NAN)
762a6b7db3Sskrll {
77*4f645668Schristos return false;
782a6b7db3Sskrll }
79*4f645668Schristos return true;
802a6b7db3Sskrll }
812a6b7db3Sskrll
822a6b7db3Sskrll
832a6b7db3Sskrll /*
842a6b7db3Sskrll * CHILD is part of a cycle. Find the top caller into this cycle
852a6b7db3Sskrll * that is not part of the cycle and make all functions in cycle
862a6b7db3Sskrll * members of that cycle (top caller == caller with smallest
872a6b7db3Sskrll * depth-first number).
882a6b7db3Sskrll */
892a6b7db3Sskrll static void
find_cycle(Sym * child)902a6b7db3Sskrll find_cycle (Sym *child)
912a6b7db3Sskrll {
922a6b7db3Sskrll Sym *head = 0;
932a6b7db3Sskrll Sym *tail;
942a6b7db3Sskrll int cycle_top;
95be12b8bcSchristos int cycle_index;
962a6b7db3Sskrll
972a6b7db3Sskrll for (cycle_top = dfn_depth; cycle_top > 0; --cycle_top)
982a6b7db3Sskrll {
992a6b7db3Sskrll head = dfn_stack[cycle_top].sym;
1002a6b7db3Sskrll if (child == head)
1012a6b7db3Sskrll {
1022a6b7db3Sskrll break;
1032a6b7db3Sskrll }
1042a6b7db3Sskrll if (child->cg.cyc.head != child && child->cg.cyc.head == head)
1052a6b7db3Sskrll {
1062a6b7db3Sskrll break;
1072a6b7db3Sskrll }
1082a6b7db3Sskrll }
1092a6b7db3Sskrll if (cycle_top <= 0)
1102a6b7db3Sskrll {
1112a6b7db3Sskrll fprintf (stderr, "[find_cycle] couldn't find head of cycle\n");
1122a6b7db3Sskrll done (1);
1132a6b7db3Sskrll }
1142a6b7db3Sskrll #ifdef DEBUG
1152a6b7db3Sskrll if (debug_level & DFNDEBUG)
1162a6b7db3Sskrll {
1172a6b7db3Sskrll printf ("[find_cycle] dfn_depth %d cycle_top %d ",
1182a6b7db3Sskrll dfn_depth, cycle_top);
1192a6b7db3Sskrll if (head)
1202a6b7db3Sskrll {
1212a6b7db3Sskrll print_name (head);
1222a6b7db3Sskrll }
1232a6b7db3Sskrll else
1242a6b7db3Sskrll {
1252a6b7db3Sskrll printf ("<unknown>");
1262a6b7db3Sskrll }
1272a6b7db3Sskrll printf ("\n");
1282a6b7db3Sskrll }
1292a6b7db3Sskrll #endif
1302a6b7db3Sskrll if (cycle_top == dfn_depth)
1312a6b7db3Sskrll {
1322a6b7db3Sskrll /*
1332a6b7db3Sskrll * This is previous function, e.g. this calls itself. Sort of
1342a6b7db3Sskrll * boring.
1352a6b7db3Sskrll *
1362a6b7db3Sskrll * Since we are taking out self-cycles elsewhere no need for
1372a6b7db3Sskrll * the special case, here.
1382a6b7db3Sskrll */
1392a6b7db3Sskrll DBG (DFNDEBUG,
1402a6b7db3Sskrll printf ("[find_cycle] ");
1412a6b7db3Sskrll print_name (child);
1422a6b7db3Sskrll printf ("\n"));
1432a6b7db3Sskrll }
1442a6b7db3Sskrll else
1452a6b7db3Sskrll {
1462a6b7db3Sskrll /*
1472a6b7db3Sskrll * Glom intervening functions that aren't already glommed into
1482a6b7db3Sskrll * this cycle. Things have been glommed when their cyclehead
1492a6b7db3Sskrll * field points to the head of the cycle they are glommed
1502a6b7db3Sskrll * into.
1512a6b7db3Sskrll */
1522a6b7db3Sskrll for (tail = head; tail->cg.cyc.next; tail = tail->cg.cyc.next)
1532a6b7db3Sskrll {
1542a6b7db3Sskrll /* void: chase down to tail of things already glommed */
1552a6b7db3Sskrll DBG (DFNDEBUG,
1562a6b7db3Sskrll printf ("[find_cycle] tail ");
1572a6b7db3Sskrll print_name (tail);
1582a6b7db3Sskrll printf ("\n"));
1592a6b7db3Sskrll }
1602a6b7db3Sskrll /*
1612a6b7db3Sskrll * If what we think is the top of the cycle has a cyclehead
1622a6b7db3Sskrll * field, then it's not really the head of the cycle, which is
1632a6b7db3Sskrll * really what we want.
1642a6b7db3Sskrll */
1652a6b7db3Sskrll if (head->cg.cyc.head != head)
1662a6b7db3Sskrll {
1672a6b7db3Sskrll head = head->cg.cyc.head;
1682a6b7db3Sskrll DBG (DFNDEBUG, printf ("[find_cycle] new cyclehead ");
1692a6b7db3Sskrll print_name (head);
1702a6b7db3Sskrll printf ("\n"));
1712a6b7db3Sskrll }
172be12b8bcSchristos for (cycle_index = cycle_top + 1; cycle_index <= dfn_depth; ++cycle_index)
1732a6b7db3Sskrll {
174be12b8bcSchristos child = dfn_stack[cycle_index].sym;
1752a6b7db3Sskrll if (child->cg.cyc.head == child)
1762a6b7db3Sskrll {
1772a6b7db3Sskrll /*
1782a6b7db3Sskrll * Not yet glommed anywhere, glom it and fix any
1792a6b7db3Sskrll * children it has glommed.
1802a6b7db3Sskrll */
1812a6b7db3Sskrll tail->cg.cyc.next = child;
1822a6b7db3Sskrll child->cg.cyc.head = head;
1832a6b7db3Sskrll DBG (DFNDEBUG, printf ("[find_cycle] glomming ");
1842a6b7db3Sskrll print_name (child);
1852a6b7db3Sskrll printf (" onto ");
1862a6b7db3Sskrll print_name (head);
1872a6b7db3Sskrll printf ("\n"));
1882a6b7db3Sskrll for (tail = child; tail->cg.cyc.next; tail = tail->cg.cyc.next)
1892a6b7db3Sskrll {
1902a6b7db3Sskrll tail->cg.cyc.next->cg.cyc.head = head;
1912a6b7db3Sskrll DBG (DFNDEBUG, printf ("[find_cycle] and its tail ");
1922a6b7db3Sskrll print_name (tail->cg.cyc.next);
1932a6b7db3Sskrll printf (" onto ");
1942a6b7db3Sskrll print_name (head);
1952a6b7db3Sskrll printf ("\n"));
1962a6b7db3Sskrll }
1972a6b7db3Sskrll }
1982a6b7db3Sskrll else if (child->cg.cyc.head != head /* firewall */ )
1992a6b7db3Sskrll {
2002a6b7db3Sskrll fprintf (stderr, "[find_cycle] glommed, but not to head\n");
2012a6b7db3Sskrll done (1);
2022a6b7db3Sskrll }
2032a6b7db3Sskrll }
2042a6b7db3Sskrll }
2052a6b7db3Sskrll }
2062a6b7db3Sskrll
2072a6b7db3Sskrll
2082a6b7db3Sskrll /*
2092a6b7db3Sskrll * Prepare for visiting the children of PARENT. Push a parent onto
2102a6b7db3Sskrll * the stack and mark it busy.
2112a6b7db3Sskrll */
2122a6b7db3Sskrll static void
pre_visit(Sym * parent)2132a6b7db3Sskrll pre_visit (Sym *parent)
2142a6b7db3Sskrll {
2152a6b7db3Sskrll ++dfn_depth;
2162a6b7db3Sskrll
2172a6b7db3Sskrll if (dfn_depth >= dfn_maxdepth)
2182a6b7db3Sskrll {
2192a6b7db3Sskrll dfn_maxdepth += DFN_INCR_DEPTH;
220be12b8bcSchristos dfn_stack = (DFN_Stack *) xrealloc (dfn_stack,
221be12b8bcSchristos dfn_maxdepth * sizeof *dfn_stack);
2222a6b7db3Sskrll }
2232a6b7db3Sskrll
2242a6b7db3Sskrll dfn_stack[dfn_depth].sym = parent;
2252a6b7db3Sskrll dfn_stack[dfn_depth].cycle_top = dfn_depth;
2262a6b7db3Sskrll parent->cg.top_order = DFN_BUSY;
2272a6b7db3Sskrll DBG (DFNDEBUG, printf ("[pre_visit]\t\t%d:", dfn_depth);
2282a6b7db3Sskrll print_name (parent);
2292a6b7db3Sskrll printf ("\n"));
2302a6b7db3Sskrll }
2312a6b7db3Sskrll
2322a6b7db3Sskrll
2332a6b7db3Sskrll /*
2342a6b7db3Sskrll * Done with visiting node PARENT. Pop PARENT off dfn_stack
2352a6b7db3Sskrll * and number functions if PARENT is head of a cycle.
2362a6b7db3Sskrll */
2372a6b7db3Sskrll static void
post_visit(Sym * parent)2382a6b7db3Sskrll post_visit (Sym *parent)
2392a6b7db3Sskrll {
2402a6b7db3Sskrll Sym *member;
2412a6b7db3Sskrll
2422a6b7db3Sskrll DBG (DFNDEBUG, printf ("[post_visit]\t%d: ", dfn_depth);
2432a6b7db3Sskrll print_name (parent);
2442a6b7db3Sskrll printf ("\n"));
2452a6b7db3Sskrll /*
2462a6b7db3Sskrll * Number functions and things in their cycles unless the function
2472a6b7db3Sskrll * is itself part of a cycle:
2482a6b7db3Sskrll */
2492a6b7db3Sskrll if (parent->cg.cyc.head == parent)
2502a6b7db3Sskrll {
2512a6b7db3Sskrll ++dfn_counter;
2522a6b7db3Sskrll for (member = parent; member; member = member->cg.cyc.next)
2532a6b7db3Sskrll {
2542a6b7db3Sskrll member->cg.top_order = dfn_counter;
2552a6b7db3Sskrll DBG (DFNDEBUG, printf ("[post_visit]\t\tmember ");
2562a6b7db3Sskrll print_name (member);
2572a6b7db3Sskrll printf ("-> cg.top_order = %d\n", dfn_counter));
2582a6b7db3Sskrll }
2592a6b7db3Sskrll }
2602a6b7db3Sskrll else
2612a6b7db3Sskrll {
2622a6b7db3Sskrll DBG (DFNDEBUG, printf ("[post_visit]\t\tis part of a cycle\n"));
2632a6b7db3Sskrll }
2642a6b7db3Sskrll --dfn_depth;
2652a6b7db3Sskrll }
2662a6b7db3Sskrll
2672a6b7db3Sskrll
2682a6b7db3Sskrll /*
2692a6b7db3Sskrll * Given this PARENT, depth first number its children.
2702a6b7db3Sskrll */
2712a6b7db3Sskrll void
cg_dfn(Sym * parent)2722a6b7db3Sskrll cg_dfn (Sym *parent)
2732a6b7db3Sskrll {
2742a6b7db3Sskrll Arc *arc;
2752a6b7db3Sskrll
2762a6b7db3Sskrll DBG (DFNDEBUG, printf ("[dfn] dfn( ");
2772a6b7db3Sskrll print_name (parent);
2782a6b7db3Sskrll printf (")\n"));
2792a6b7db3Sskrll /*
2802a6b7db3Sskrll * If we're already numbered, no need to look any further:
2812a6b7db3Sskrll */
2822a6b7db3Sskrll if (is_numbered (parent))
2832a6b7db3Sskrll {
2842a6b7db3Sskrll return;
2852a6b7db3Sskrll }
2862a6b7db3Sskrll /*
2872a6b7db3Sskrll * If we're already busy, must be a cycle:
2882a6b7db3Sskrll */
2892a6b7db3Sskrll if (is_busy (parent))
2902a6b7db3Sskrll {
2912a6b7db3Sskrll find_cycle (parent);
2922a6b7db3Sskrll return;
2932a6b7db3Sskrll }
2942a6b7db3Sskrll pre_visit (parent);
2952a6b7db3Sskrll /*
2962a6b7db3Sskrll * Recursively visit children:
2972a6b7db3Sskrll */
2982a6b7db3Sskrll for (arc = parent->cg.children; arc; arc = arc->next_child)
2992a6b7db3Sskrll {
3002a6b7db3Sskrll cg_dfn (arc->child);
3012a6b7db3Sskrll }
3022a6b7db3Sskrll post_visit (parent);
3032a6b7db3Sskrll }
304