175fd0b74Schristos /*
275fd0b74Schristos * Copyright (c) 1983, 1993
375fd0b74Schristos * The Regents of the University of California. All rights reserved.
475fd0b74Schristos *
575fd0b74Schristos * Redistribution and use in source and binary forms, with or without
675fd0b74Schristos * modification, are permitted provided that the following conditions
775fd0b74Schristos * are met:
875fd0b74Schristos * 1. Redistributions of source code must retain the above copyright
975fd0b74Schristos * notice, this list of conditions and the following disclaimer.
1075fd0b74Schristos * 2. Redistributions in binary form must reproduce the above copyright
1175fd0b74Schristos * notice, this list of conditions and the following disclaimer in the
1275fd0b74Schristos * documentation and/or other materials provided with the distribution.
1375fd0b74Schristos * 3. Neither the name of the University nor the names of its contributors
1475fd0b74Schristos * may be used to endorse or promote products derived from this software
1575fd0b74Schristos * without specific prior written permission.
1675fd0b74Schristos *
1775fd0b74Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1875fd0b74Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1975fd0b74Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2075fd0b74Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2175fd0b74Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2275fd0b74Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2375fd0b74Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2475fd0b74Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2575fd0b74Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2675fd0b74Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2775fd0b74Schristos * SUCH DAMAGE.
2875fd0b74Schristos */
2975fd0b74Schristos #include "gprof.h"
3075fd0b74Schristos #include "libiberty.h"
3175fd0b74Schristos #include "search_list.h"
3275fd0b74Schristos #include "source.h"
3375fd0b74Schristos #include "symtab.h"
3475fd0b74Schristos #include "cg_arcs.h"
3575fd0b74Schristos #include "cg_dfn.h"
3675fd0b74Schristos #include "utils.h"
3775fd0b74Schristos
3875fd0b74Schristos #define DFN_INCR_DEPTH (128)
3975fd0b74Schristos
4075fd0b74Schristos typedef struct
4175fd0b74Schristos {
4275fd0b74Schristos Sym *sym;
4375fd0b74Schristos int cycle_top;
4475fd0b74Schristos }
4575fd0b74Schristos DFN_Stack;
4675fd0b74Schristos
47*e992f068Schristos static bool is_numbered (Sym *);
48*e992f068Schristos static bool is_busy (Sym *);
4975fd0b74Schristos static void find_cycle (Sym *);
5075fd0b74Schristos static void pre_visit (Sym *);
5175fd0b74Schristos static void post_visit (Sym *);
5275fd0b74Schristos
5375fd0b74Schristos DFN_Stack *dfn_stack = NULL;
5475fd0b74Schristos int dfn_maxdepth = 0;
5575fd0b74Schristos int dfn_depth = 0;
5675fd0b74Schristos int dfn_counter = DFN_NAN;
5775fd0b74Schristos
5875fd0b74Schristos
5975fd0b74Schristos /*
6075fd0b74Schristos * Is CHILD already numbered?
6175fd0b74Schristos */
62*e992f068Schristos static bool
is_numbered(Sym * child)6375fd0b74Schristos is_numbered (Sym *child)
6475fd0b74Schristos {
6575fd0b74Schristos return child->cg.top_order != DFN_NAN && child->cg.top_order != DFN_BUSY;
6675fd0b74Schristos }
6775fd0b74Schristos
6875fd0b74Schristos
6975fd0b74Schristos /*
7075fd0b74Schristos * Is CHILD already busy?
7175fd0b74Schristos */
72*e992f068Schristos static bool
is_busy(Sym * child)7375fd0b74Schristos is_busy (Sym *child)
7475fd0b74Schristos {
7575fd0b74Schristos if (child->cg.top_order == DFN_NAN)
7675fd0b74Schristos {
77*e992f068Schristos return false;
7875fd0b74Schristos }
79*e992f068Schristos return true;
8075fd0b74Schristos }
8175fd0b74Schristos
8275fd0b74Schristos
8375fd0b74Schristos /*
8475fd0b74Schristos * CHILD is part of a cycle. Find the top caller into this cycle
8575fd0b74Schristos * that is not part of the cycle and make all functions in cycle
8675fd0b74Schristos * members of that cycle (top caller == caller with smallest
8775fd0b74Schristos * depth-first number).
8875fd0b74Schristos */
8975fd0b74Schristos static void
find_cycle(Sym * child)9075fd0b74Schristos find_cycle (Sym *child)
9175fd0b74Schristos {
9275fd0b74Schristos Sym *head = 0;
9375fd0b74Schristos Sym *tail;
9475fd0b74Schristos int cycle_top;
9575fd0b74Schristos int cycle_index;
9675fd0b74Schristos
9775fd0b74Schristos for (cycle_top = dfn_depth; cycle_top > 0; --cycle_top)
9875fd0b74Schristos {
9975fd0b74Schristos head = dfn_stack[cycle_top].sym;
10075fd0b74Schristos if (child == head)
10175fd0b74Schristos {
10275fd0b74Schristos break;
10375fd0b74Schristos }
10475fd0b74Schristos if (child->cg.cyc.head != child && child->cg.cyc.head == head)
10575fd0b74Schristos {
10675fd0b74Schristos break;
10775fd0b74Schristos }
10875fd0b74Schristos }
10975fd0b74Schristos if (cycle_top <= 0)
11075fd0b74Schristos {
11175fd0b74Schristos fprintf (stderr, "[find_cycle] couldn't find head of cycle\n");
11275fd0b74Schristos done (1);
11375fd0b74Schristos }
11475fd0b74Schristos #ifdef DEBUG
11575fd0b74Schristos if (debug_level & DFNDEBUG)
11675fd0b74Schristos {
11775fd0b74Schristos printf ("[find_cycle] dfn_depth %d cycle_top %d ",
11875fd0b74Schristos dfn_depth, cycle_top);
11975fd0b74Schristos if (head)
12075fd0b74Schristos {
12175fd0b74Schristos print_name (head);
12275fd0b74Schristos }
12375fd0b74Schristos else
12475fd0b74Schristos {
12575fd0b74Schristos printf ("<unknown>");
12675fd0b74Schristos }
12775fd0b74Schristos printf ("\n");
12875fd0b74Schristos }
12975fd0b74Schristos #endif
13075fd0b74Schristos if (cycle_top == dfn_depth)
13175fd0b74Schristos {
13275fd0b74Schristos /*
13375fd0b74Schristos * This is previous function, e.g. this calls itself. Sort of
13475fd0b74Schristos * boring.
13575fd0b74Schristos *
13675fd0b74Schristos * Since we are taking out self-cycles elsewhere no need for
13775fd0b74Schristos * the special case, here.
13875fd0b74Schristos */
13975fd0b74Schristos DBG (DFNDEBUG,
14075fd0b74Schristos printf ("[find_cycle] ");
14175fd0b74Schristos print_name (child);
14275fd0b74Schristos printf ("\n"));
14375fd0b74Schristos }
14475fd0b74Schristos else
14575fd0b74Schristos {
14675fd0b74Schristos /*
14775fd0b74Schristos * Glom intervening functions that aren't already glommed into
14875fd0b74Schristos * this cycle. Things have been glommed when their cyclehead
14975fd0b74Schristos * field points to the head of the cycle they are glommed
15075fd0b74Schristos * into.
15175fd0b74Schristos */
15275fd0b74Schristos for (tail = head; tail->cg.cyc.next; tail = tail->cg.cyc.next)
15375fd0b74Schristos {
15475fd0b74Schristos /* void: chase down to tail of things already glommed */
15575fd0b74Schristos DBG (DFNDEBUG,
15675fd0b74Schristos printf ("[find_cycle] tail ");
15775fd0b74Schristos print_name (tail);
15875fd0b74Schristos printf ("\n"));
15975fd0b74Schristos }
16075fd0b74Schristos /*
16175fd0b74Schristos * If what we think is the top of the cycle has a cyclehead
16275fd0b74Schristos * field, then it's not really the head of the cycle, which is
16375fd0b74Schristos * really what we want.
16475fd0b74Schristos */
16575fd0b74Schristos if (head->cg.cyc.head != head)
16675fd0b74Schristos {
16775fd0b74Schristos head = head->cg.cyc.head;
16875fd0b74Schristos DBG (DFNDEBUG, printf ("[find_cycle] new cyclehead ");
16975fd0b74Schristos print_name (head);
17075fd0b74Schristos printf ("\n"));
17175fd0b74Schristos }
17275fd0b74Schristos for (cycle_index = cycle_top + 1; cycle_index <= dfn_depth; ++cycle_index)
17375fd0b74Schristos {
17475fd0b74Schristos child = dfn_stack[cycle_index].sym;
17575fd0b74Schristos if (child->cg.cyc.head == child)
17675fd0b74Schristos {
17775fd0b74Schristos /*
17875fd0b74Schristos * Not yet glommed anywhere, glom it and fix any
17975fd0b74Schristos * children it has glommed.
18075fd0b74Schristos */
18175fd0b74Schristos tail->cg.cyc.next = child;
18275fd0b74Schristos child->cg.cyc.head = head;
18375fd0b74Schristos DBG (DFNDEBUG, printf ("[find_cycle] glomming ");
18475fd0b74Schristos print_name (child);
18575fd0b74Schristos printf (" onto ");
18675fd0b74Schristos print_name (head);
18775fd0b74Schristos printf ("\n"));
18875fd0b74Schristos for (tail = child; tail->cg.cyc.next; tail = tail->cg.cyc.next)
18975fd0b74Schristos {
19075fd0b74Schristos tail->cg.cyc.next->cg.cyc.head = head;
19175fd0b74Schristos DBG (DFNDEBUG, printf ("[find_cycle] and its tail ");
19275fd0b74Schristos print_name (tail->cg.cyc.next);
19375fd0b74Schristos printf (" onto ");
19475fd0b74Schristos print_name (head);
19575fd0b74Schristos printf ("\n"));
19675fd0b74Schristos }
19775fd0b74Schristos }
19875fd0b74Schristos else if (child->cg.cyc.head != head /* firewall */ )
19975fd0b74Schristos {
20075fd0b74Schristos fprintf (stderr, "[find_cycle] glommed, but not to head\n");
20175fd0b74Schristos done (1);
20275fd0b74Schristos }
20375fd0b74Schristos }
20475fd0b74Schristos }
20575fd0b74Schristos }
20675fd0b74Schristos
20775fd0b74Schristos
20875fd0b74Schristos /*
20975fd0b74Schristos * Prepare for visiting the children of PARENT. Push a parent onto
21075fd0b74Schristos * the stack and mark it busy.
21175fd0b74Schristos */
21275fd0b74Schristos static void
pre_visit(Sym * parent)21375fd0b74Schristos pre_visit (Sym *parent)
21475fd0b74Schristos {
21575fd0b74Schristos ++dfn_depth;
21675fd0b74Schristos
21775fd0b74Schristos if (dfn_depth >= dfn_maxdepth)
21875fd0b74Schristos {
21975fd0b74Schristos dfn_maxdepth += DFN_INCR_DEPTH;
22075fd0b74Schristos dfn_stack = (DFN_Stack *) xrealloc (dfn_stack,
22175fd0b74Schristos dfn_maxdepth * sizeof *dfn_stack);
22275fd0b74Schristos }
22375fd0b74Schristos
22475fd0b74Schristos dfn_stack[dfn_depth].sym = parent;
22575fd0b74Schristos dfn_stack[dfn_depth].cycle_top = dfn_depth;
22675fd0b74Schristos parent->cg.top_order = DFN_BUSY;
22775fd0b74Schristos DBG (DFNDEBUG, printf ("[pre_visit]\t\t%d:", dfn_depth);
22875fd0b74Schristos print_name (parent);
22975fd0b74Schristos printf ("\n"));
23075fd0b74Schristos }
23175fd0b74Schristos
23275fd0b74Schristos
23375fd0b74Schristos /*
23475fd0b74Schristos * Done with visiting node PARENT. Pop PARENT off dfn_stack
23575fd0b74Schristos * and number functions if PARENT is head of a cycle.
23675fd0b74Schristos */
23775fd0b74Schristos static void
post_visit(Sym * parent)23875fd0b74Schristos post_visit (Sym *parent)
23975fd0b74Schristos {
24075fd0b74Schristos Sym *member;
24175fd0b74Schristos
24275fd0b74Schristos DBG (DFNDEBUG, printf ("[post_visit]\t%d: ", dfn_depth);
24375fd0b74Schristos print_name (parent);
24475fd0b74Schristos printf ("\n"));
24575fd0b74Schristos /*
24675fd0b74Schristos * Number functions and things in their cycles unless the function
24775fd0b74Schristos * is itself part of a cycle:
24875fd0b74Schristos */
24975fd0b74Schristos if (parent->cg.cyc.head == parent)
25075fd0b74Schristos {
25175fd0b74Schristos ++dfn_counter;
25275fd0b74Schristos for (member = parent; member; member = member->cg.cyc.next)
25375fd0b74Schristos {
25475fd0b74Schristos member->cg.top_order = dfn_counter;
25575fd0b74Schristos DBG (DFNDEBUG, printf ("[post_visit]\t\tmember ");
25675fd0b74Schristos print_name (member);
25775fd0b74Schristos printf ("-> cg.top_order = %d\n", dfn_counter));
25875fd0b74Schristos }
25975fd0b74Schristos }
26075fd0b74Schristos else
26175fd0b74Schristos {
26275fd0b74Schristos DBG (DFNDEBUG, printf ("[post_visit]\t\tis part of a cycle\n"));
26375fd0b74Schristos }
26475fd0b74Schristos --dfn_depth;
26575fd0b74Schristos }
26675fd0b74Schristos
26775fd0b74Schristos
26875fd0b74Schristos /*
26975fd0b74Schristos * Given this PARENT, depth first number its children.
27075fd0b74Schristos */
27175fd0b74Schristos void
cg_dfn(Sym * parent)27275fd0b74Schristos cg_dfn (Sym *parent)
27375fd0b74Schristos {
27475fd0b74Schristos Arc *arc;
27575fd0b74Schristos
27675fd0b74Schristos DBG (DFNDEBUG, printf ("[dfn] dfn( ");
27775fd0b74Schristos print_name (parent);
27875fd0b74Schristos printf (")\n"));
27975fd0b74Schristos /*
28075fd0b74Schristos * If we're already numbered, no need to look any further:
28175fd0b74Schristos */
28275fd0b74Schristos if (is_numbered (parent))
28375fd0b74Schristos {
28475fd0b74Schristos return;
28575fd0b74Schristos }
28675fd0b74Schristos /*
28775fd0b74Schristos * If we're already busy, must be a cycle:
28875fd0b74Schristos */
28975fd0b74Schristos if (is_busy (parent))
29075fd0b74Schristos {
29175fd0b74Schristos find_cycle (parent);
29275fd0b74Schristos return;
29375fd0b74Schristos }
29475fd0b74Schristos pre_visit (parent);
29575fd0b74Schristos /*
29675fd0b74Schristos * Recursively visit children:
29775fd0b74Schristos */
29875fd0b74Schristos for (arc = parent->cg.children; arc; arc = arc->next_child)
29975fd0b74Schristos {
30075fd0b74Schristos cg_dfn (arc->child);
30175fd0b74Schristos }
30275fd0b74Schristos post_visit (parent);
30375fd0b74Schristos }
304