2 * Copyright (c) 1983, 2001 Regents of the University of California.
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"
21 #include "search_list.h"
24 #include "call_graph.h"
32 unsigned int num_cycles;
37 * Return TRUE iff PARENT has an arc to covers the address
38 * range covered by CHILD.
41 DEFUN (arc_lookup, (parent, child), Sym * parent AND Sym * child)
45 if (!parent || !child)
47 printf ("[arc_lookup] parent == 0 || child == 0\n");
50 DBG (LOOKUPDEBUG, printf ("[arc_lookup] parent %s child %s\n",
51 parent->name, child->name));
52 for (arc = parent->cg.children; arc; arc = arc->next_child)
54 DBG (LOOKUPDEBUG, printf ("[arc_lookup]\t parent %s child %s\n",
55 arc->parent->name, arc->child->name));
56 if (child->addr >= arc->child->addr
57 && child->end_addr <= arc->child->end_addr)
67 * Add (or just increment) an arc:
70 DEFUN (arc_add, (parent, child, count),
71 Sym * parent AND Sym * child AND unsigned long count)
73 static unsigned int maxarcs = 0;
76 DBG (TALLYDEBUG, printf ("[arc_add] %lu arcs from %s to %s\n",
77 count, parent->name, child->name));
78 arc = arc_lookup (parent, child);
82 * A hit: just increment the count.
84 DBG (TALLYDEBUG, printf ("[tally] hit %lu += %lu\n",
89 arc = (Arc *) xmalloc (sizeof (*arc));
90 memset (arc, 0, sizeof (*arc));
95 /* If this isn't an arc for a recursive call to parent, then add it
96 to the array of arcs. */
99 /* If we've exhausted space in our current array, get a new one
100 and copy the contents. We might want to throttle the doubling
102 if (numarcs == maxarcs)
104 /* Determine how much space we want to allocate. */
109 /* Allocate the new array. */
110 newarcs = (Arc **)xmalloc(sizeof (Arc *) * maxarcs);
112 /* Copy the old array's contents into the new array. */
113 memcpy (newarcs, arcs, numarcs * sizeof (Arc *));
115 /* Free up the old array. */
118 /* And make the new array be the current array. */
122 /* Place this arc in the arc array. */
123 arcs[numarcs++] = arc;
126 /* prepend this child to the children of this parent: */
127 arc->next_child = parent->cg.children;
128 parent->cg.children = arc;
130 /* prepend this parent to the parents of this child: */
131 arc->next_parent = child->cg.parents;
132 child->cg.parents = arc;
137 DEFUN (cmp_topo, (lp, rp), const PTR lp AND const PTR rp)
139 const Sym *left = *(const Sym **) lp;
140 const Sym *right = *(const Sym **) rp;
142 return left->cg.top_order - right->cg.top_order;
147 DEFUN (propagate_time, (parent), Sym * parent)
151 double share, prop_share;
153 if (parent->cg.prop.fract == 0.0)
158 /* gather time from children of this parent: */
160 for (arc = parent->cg.children; arc; arc = arc->next_child)
163 if (arc->count == 0 || child == parent || child->cg.prop.fract == 0)
167 if (child->cg.cyc.head != child)
169 if (parent->cg.cyc.num == child->cg.cyc.num)
173 if (parent->cg.top_order <= child->cg.top_order)
175 fprintf (stderr, "[propagate] toporder botches\n");
177 child = child->cg.cyc.head;
181 if (parent->cg.top_order <= child->cg.top_order)
183 fprintf (stderr, "[propagate] toporder botches\n");
187 if (child->ncalls == 0)
192 /* distribute time for this arc: */
193 arc->time = child->hist.time * (((double) arc->count)
194 / ((double) child->ncalls));
195 arc->child_time = child->cg.child_time
196 * (((double) arc->count) / ((double) child->ncalls));
197 share = arc->time + arc->child_time;
198 parent->cg.child_time += share;
200 /* (1 - cg.prop.fract) gets lost along the way: */
201 prop_share = parent->cg.prop.fract * share;
203 /* fix things for printing: */
204 parent->cg.prop.child += prop_share;
205 arc->time *= parent->cg.prop.fract;
206 arc->child_time *= parent->cg.prop.fract;
208 /* add this share to the parent's cycle header, if any: */
209 if (parent->cg.cyc.head != parent)
211 parent->cg.cyc.head->cg.child_time += share;
212 parent->cg.cyc.head->cg.prop.child += prop_share;
215 printf ("[prop_time] child \t");
217 printf (" with %f %f %lu/%lu\n", child->hist.time,
218 child->cg.child_time, arc->count, child->ncalls);
219 printf ("[prop_time] parent\t");
221 printf ("\n[prop_time] share %f\n", share));
227 * Compute the time of a cycle as the sum of the times of all
231 DEFUN_VOID (cycle_time)
235 for (cyc = &cycle_header[1]; cyc <= &cycle_header[num_cycles]; ++cyc)
237 for (member = cyc->cg.cyc.next; member; member = member->cg.cyc.next)
239 if (member->cg.prop.fract == 0.0)
242 * All members have the same propfraction except those
243 * that were excluded with -E.
247 cyc->hist.time += member->hist.time;
249 cyc->cg.prop.self = cyc->cg.prop.fract * cyc->hist.time;
255 DEFUN_VOID (cycle_link)
257 Sym *sym, *cyc, *member;
261 /* count the number of cycles, and initialize the cycle lists: */
264 for (sym = symtab.base; sym < symtab.limit; ++sym)
266 /* this is how you find unattached cycles: */
267 if (sym->cg.cyc.head == sym && sym->cg.cyc.next)
274 * cycle_header is indexed by cycle number: i.e. it is origin 1,
277 cycle_header = (Sym *) xmalloc ((num_cycles + 1) * sizeof (Sym));
280 * Now link cycles to true cycle-heads, number them, accumulate
281 * the data for the cycle.
285 for (sym = symtab.base; sym < symtab.limit; ++sym)
287 if (!(sym->cg.cyc.head == sym && sym->cg.cyc.next != 0))
294 cyc->cg.print_flag = true; /* should this be printed? */
295 cyc->cg.top_order = DFN_NAN; /* graph call chain top-sort order */
296 cyc->cg.cyc.num = num; /* internal number of cycle on */
297 cyc->cg.cyc.head = cyc; /* pointer to head of cycle */
298 cyc->cg.cyc.next = sym; /* pointer to next member of cycle */
299 DBG (CYCLEDEBUG, printf ("[cycle_link] ");
301 printf (" is the head of cycle %d\n", num));
303 /* link members to cycle header: */
304 for (member = sym; member; member = member->cg.cyc.next)
306 member->cg.cyc.num = num;
307 member->cg.cyc.head = cyc;
311 * Count calls from outside the cycle and those among cycle
314 for (member = sym; member; member = member->cg.cyc.next)
316 for (arc = member->cg.parents; arc; arc = arc->next_parent)
318 if (arc->parent == member)
322 if (arc->parent->cg.cyc.num == num)
324 cyc->cg.self_calls += arc->count;
328 cyc->ncalls += arc->count;
337 * Check if any parent of this child (or outside parents of this
338 * cycle) have their print flags on and set the print flag of the
339 * child (cycle) appropriately. Similarly, deal with propagation
340 * fractions from parents.
343 DEFUN (inherit_flags, (child), Sym * child)
345 Sym *head, *parent, *member;
348 head = child->cg.cyc.head;
351 /* just a regular child, check its parents: */
352 child->cg.print_flag = false;
353 child->cg.prop.fract = 0.0;
354 for (arc = child->cg.parents; arc; arc = arc->next_parent)
356 parent = arc->parent;
361 child->cg.print_flag |= parent->cg.print_flag;
363 * If the child was never actually called (e.g., this arc
364 * is static (and all others are, too)) no time propagates
367 if (child->ncalls != 0)
369 child->cg.prop.fract += parent->cg.prop.fract
370 * (((double) arc->count) / ((double) child->ncalls));
377 * Its a member of a cycle, look at all parents from outside
380 head->cg.print_flag = false;
381 head->cg.prop.fract = 0.0;
382 for (member = head->cg.cyc.next; member; member = member->cg.cyc.next)
384 for (arc = member->cg.parents; arc; arc = arc->next_parent)
386 if (arc->parent->cg.cyc.head == head)
390 parent = arc->parent;
391 head->cg.print_flag |= parent->cg.print_flag;
393 * If the cycle was never actually called (e.g. this
394 * arc is static (and all others are, too)) no time
395 * propagates along this arc.
397 if (head->ncalls != 0)
399 head->cg.prop.fract += parent->cg.prop.fract
400 * (((double) arc->count) / ((double) head->ncalls));
404 for (member = head; member; member = member->cg.cyc.next)
406 member->cg.print_flag = head->cg.print_flag;
407 member->cg.prop.fract = head->cg.prop.fract;
414 * In one top-to-bottom pass over the topologically sorted symbols
416 * cg.print_flag as the union of parents' print_flags
417 * propfraction as the sum of fractional parents' propfractions
418 * and while we're here, sum time for functions.
421 DEFUN (propagate_flags, (symbols), Sym ** symbols)
424 Sym *old_head, *child;
427 for (index = symtab.len - 1; index >= 0; --index)
429 child = symbols[index];
431 * If we haven't done this function or cycle, inherit things
432 * from parent. This way, we are linear in the number of arcs
433 * since we do all members of a cycle (and the cycle itself)
434 * as we hit the first member of the cycle.
436 if (child->cg.cyc.head != old_head)
438 old_head = child->cg.cyc.head;
439 inherit_flags (child);
442 printf ("[prop_flags] ");
444 printf ("inherits print-flag %d and prop-fract %f\n",
445 child->cg.print_flag, child->cg.prop.fract));
446 if (!child->cg.print_flag)
449 * Printflag is off. It gets turned on by being in the
450 * INCL_GRAPH table, or there being an empty INCL_GRAPH
451 * table and not being in the EXCL_GRAPH table.
453 if (sym_lookup (&syms[INCL_GRAPH], child->addr)
454 || (syms[INCL_GRAPH].len == 0
455 && !sym_lookup (&syms[EXCL_GRAPH], child->addr)))
457 child->cg.print_flag = true;
463 * This function has printing parents: maybe someone wants
464 * to shut it up by putting it in the EXCL_GRAPH table.
465 * (But favor INCL_GRAPH over EXCL_GRAPH.)
467 if (!sym_lookup (&syms[INCL_GRAPH], child->addr)
468 && sym_lookup (&syms[EXCL_GRAPH], child->addr))
470 child->cg.print_flag = false;
473 if (child->cg.prop.fract == 0.0)
476 * No parents to pass time to. Collect time from children
477 * if its in the INCL_TIME table, or there is an empty
478 * INCL_TIME table and its not in the EXCL_TIME table.
480 if (sym_lookup (&syms[INCL_TIME], child->addr)
481 || (syms[INCL_TIME].len == 0
482 && !sym_lookup (&syms[EXCL_TIME], child->addr)))
484 child->cg.prop.fract = 1.0;
490 * It has parents to pass time to, but maybe someone wants
491 * to shut it up by puttting it in the EXCL_TIME table.
492 * (But favor being in INCL_TIME tabe over being in
495 if (!sym_lookup (&syms[INCL_TIME], child->addr)
496 && sym_lookup (&syms[EXCL_TIME], child->addr))
498 child->cg.prop.fract = 0.0;
501 child->cg.prop.self = child->hist.time * child->cg.prop.fract;
502 print_time += child->cg.prop.self;
504 printf ("[prop_flags] ");
506 printf (" ends up with printflag %d and prop-fract %f\n",
507 child->cg.print_flag, child->cg.prop.fract);
508 printf ("[prop_flags] time %f propself %f print_time %f\n",
509 child->hist.time, child->cg.prop.self, print_time));
515 * Compare by decreasing propagated time. If times are equal, but one
516 * is a cycle header, say that's first (e.g. less, i.e. -1). If one's
517 * name doesn't have an underscore and the other does, say that one is
518 * first. All else being equal, compare by names.
521 DEFUN (cmp_total, (lp, rp), const PTR lp AND const PTR rp)
523 const Sym *left = *(const Sym **) lp;
524 const Sym *right = *(const Sym **) rp;
527 diff = (left->cg.prop.self + left->cg.prop.child)
528 - (right->cg.prop.self + right->cg.prop.child);
537 if (!left->name && left->cg.cyc.num != 0)
541 if (!right->name && right->cg.cyc.num != 0)
553 if (left->name[0] != '_' && right->name[0] == '_')
557 if (left->name[0] == '_' && right->name[0] != '_')
561 if (left->ncalls > right->ncalls)
565 if (left->ncalls < right->ncalls)
569 return strcmp (left->name, right->name);
574 * Topologically sort the graph (collapsing cycles), and propagates
575 * time bottom up and flags top down.
578 DEFUN_VOID (cg_assemble)
580 Sym *parent, **time_sorted_syms, **top_sorted_syms;
585 * initialize various things:
586 * zero out child times.
587 * count self-recursive calls.
588 * indicate that nothing is on cycles.
590 for (parent = symtab.base; parent < symtab.limit; parent++)
592 parent->cg.child_time = 0.0;
593 arc = arc_lookup (parent, parent);
594 if (arc && parent == arc->child)
596 parent->ncalls -= arc->count;
597 parent->cg.self_calls = arc->count;
601 parent->cg.self_calls = 0;
603 parent->cg.prop.fract = 0.0;
604 parent->cg.prop.self = 0.0;
605 parent->cg.prop.child = 0.0;
606 parent->cg.print_flag = false;
607 parent->cg.top_order = DFN_NAN;
608 parent->cg.cyc.num = 0;
609 parent->cg.cyc.head = parent;
610 parent->cg.cyc.next = 0;
611 if (ignore_direct_calls)
613 find_call (parent, parent->addr, (parent + 1)->addr);
617 * Topologically order things. If any node is unnumbered, number
618 * it and any of its descendents.
620 for (parent = symtab.base; parent < symtab.limit; parent++)
622 if (parent->cg.top_order == DFN_NAN)
628 /* link together nodes on the same cycle: */
631 /* sort the symbol table in reverse topological order: */
632 top_sorted_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
633 for (index = 0; index < symtab.len; ++index)
635 top_sorted_syms[index] = &symtab.base[index];
637 qsort (top_sorted_syms, symtab.len, sizeof (Sym *), cmp_topo);
639 printf ("[cg_assemble] topological sort listing\n");
640 for (index = 0; index < symtab.len; ++index)
642 printf ("[cg_assemble] ");
643 printf ("%d:", top_sorted_syms[index]->cg.top_order);
644 print_name (top_sorted_syms[index]);
649 * Starting from the topological top, propagate print flags to
650 * children. also, calculate propagation fractions. this happens
651 * before time propagation since time propagation uses the
654 propagate_flags (top_sorted_syms);
657 * Starting from the topological bottom, propogate children times
661 for (index = 0; index < symtab.len; ++index)
663 propagate_time (top_sorted_syms[index]);
666 free (top_sorted_syms);
669 * Now, sort by CG.PROP.SELF + CG.PROP.CHILD. Sorting both the regular
670 * function names and cycle headers.
672 time_sorted_syms = (Sym **) xmalloc ((symtab.len + num_cycles) * sizeof (Sym *));
673 for (index = 0; index < symtab.len; index++)
675 time_sorted_syms[index] = &symtab.base[index];
677 for (index = 1; index <= num_cycles; index++)
679 time_sorted_syms[symtab.len + index - 1] = &cycle_header[index];
681 qsort (time_sorted_syms, symtab.len + num_cycles, sizeof (Sym *),
683 for (index = 0; index < symtab.len + num_cycles; index++)
685 time_sorted_syms[index]->cg.index = index + 1;
687 return time_sorted_syms;