2 * Copyright (c) 1983 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 "call_graph.h"
32 * Return TRUE iff PARENT has an arc to covers the address
33 * range covered by CHILD.
36 DEFUN(arc_lookup, (parent, child), Sym *parent AND Sym *child)
40 if (!parent || !child) {
41 printf("[arc_lookup] parent == 0 || child == 0\n");
44 DBG(LOOKUPDEBUG, printf("[arc_lookup] parent %s child %s\n",
45 parent->name, child->name));
46 for (arc = parent->cg.children; arc; arc = arc->next_child) {
47 DBG(LOOKUPDEBUG, printf("[arc_lookup]\t parent %s child %s\n",
48 arc->parent->name, arc->child->name));
49 if (child->addr >= arc->child->addr
50 && child->end_addr <= arc->child->end_addr)
60 * Add (or just increment) an arc:
63 DEFUN(arc_add, (parent, child, count),
64 Sym *parent AND Sym *child AND int count)
68 DBG(TALLYDEBUG, printf("[arc_add] %d arcs from %s to %s\n",
69 count, parent->name, child->name));
70 arc = arc_lookup(parent, child);
73 * A hit: just increment the count.
75 DBG(TALLYDEBUG, printf("[tally] hit %d += %d\n",
80 arc = (Arc*)xmalloc(sizeof(*arc));
85 /* prepend this child to the children of this parent: */
86 arc->next_child = parent->cg.children;
87 parent->cg.children = arc;
89 /* prepend this parent to the parents of this child: */
90 arc->next_parent = child->cg.parents;
91 child->cg.parents = arc;
96 DEFUN(cmp_topo, (lp, rp), const PTR lp AND const PTR rp)
98 const Sym *left = *(const Sym **) lp;
99 const Sym *right = *(const Sym **) rp;
101 return left->cg.top_order - right->cg.top_order;
106 DEFUN(propagate_time, (parent), Sym *parent)
110 double share, prop_share;
112 if (parent->cg.prop.fract == 0.0) {
116 /* gather time from children of this parent: */
118 for (arc = parent->cg.children; arc; arc = arc->next_child) {
120 if (arc->count == 0 || child == parent || child->cg.prop.fract == 0) {
123 if (child->cg.cyc.head != child) {
124 if (parent->cg.cyc.num == child->cg.cyc.num) {
127 if (parent->cg.top_order <= child->cg.top_order) {
128 fprintf(stderr, "[propagate] toporder botches\n");
130 child = child->cg.cyc.head;
132 if (parent->cg.top_order <= child->cg.top_order) {
133 fprintf(stderr, "[propagate] toporder botches\n");
137 if (child->ncalls == 0) {
141 /* distribute time for this arc: */
142 arc->time = child->hist.time * (((double) arc->count)
143 / ((double) child->ncalls));
144 arc->child_time = child->cg.child_time
145 * (((double) arc->count) / ((double) child->ncalls));
146 share = arc->time + arc->child_time;
147 parent->cg.child_time += share;
149 /* (1 - cg.prop.fract) gets lost along the way: */
150 prop_share = parent->cg.prop.fract * share;
152 /* fix things for printing: */
153 parent->cg.prop.child += prop_share;
154 arc->time *= parent->cg.prop.fract;
155 arc->child_time *= parent->cg.prop.fract;
157 /* add this share to the parent's cycle header, if any: */
158 if (parent->cg.cyc.head != parent) {
159 parent->cg.cyc.head->cg.child_time += share;
160 parent->cg.cyc.head->cg.prop.child += prop_share;
163 printf("[prop_time] child \t");
165 printf(" with %f %f %d/%d\n", child->hist.time,
166 child->cg.child_time, arc->count, child->ncalls);
167 printf("[prop_time] parent\t");
169 printf("\n[prop_time] share %f\n", share));
171 } /* propagate_time */
175 * Compute the time of a cycle as the sum of the times of all
179 DEFUN_VOID(cycle_time)
183 for (cyc = &cycle_header[1]; cyc <= &cycle_header[num_cycles]; ++cyc) {
184 for (member = cyc->cg.cyc.next; member; member = member->cg.cyc.next) {
185 if (member->cg.prop.fract == 0.0) {
187 * All members have the same propfraction except those
188 * that were excluded with -E.
192 cyc->hist.time += member->hist.time;
194 cyc->cg.prop.self = cyc->cg.prop.fract * cyc->hist.time;
200 DEFUN_VOID(cycle_link)
202 Sym *sym, *cyc, *member;
206 /* count the number of cycles, and initialize the cycle lists: */
209 for (sym = symtab.base; sym < symtab.limit; ++sym) {
210 /* this is how you find unattached cycles: */
211 if (sym->cg.cyc.head == sym && sym->cg.cyc.next) {
217 * cycle_header is indexed by cycle number: i.e. it is origin 1,
220 cycle_header = (Sym*)xmalloc((num_cycles + 1) * sizeof(Sym));
223 * Now link cycles to true cycle-heads, number them, accumulate
224 * the data for the cycle.
226 num = 0; cyc = cycle_header;
227 for (sym = symtab.base; sym < symtab.limit; ++sym) {
228 if (!(sym->cg.cyc.head == sym && sym->cg.cyc.next != 0)) {
233 cyc->cg.print_flag = TRUE; /* should this be printed? */
234 cyc->cg.top_order = DFN_NAN; /* graph call chain top-sort order */
235 cyc->cg.cyc.num = num; /* internal number of cycle on */
236 cyc->cg.cyc.head = cyc; /* pointer to head of cycle */
237 cyc->cg.cyc.next = sym; /* pointer to next member of cycle */
238 DBG(CYCLEDEBUG, printf("[cycle_link] "); print_name(sym);
239 printf(" is the head of cycle %d\n", num));
241 /* link members to cycle header: */
242 for (member = sym; member; member = member->cg.cyc.next) {
243 member->cg.cyc.num = num;
244 member->cg.cyc.head = cyc;
248 * Count calls from outside the cycle and those among cycle
251 for (member = sym; member; member = member->cg.cyc.next) {
252 for (arc = member->cg.parents; arc; arc = arc->next_parent) {
253 if (arc->parent == member) {
256 if (arc->parent->cg.cyc.num == num) {
257 cyc->cg.self_calls += arc->count;
259 cyc->ncalls += arc->count;
268 * Check if any parent of this child (or outside parents of this
269 * cycle) have their print flags on and set the print flag of the
270 * child (cycle) appropriately. Similarly, deal with propagation
271 * fractions from parents.
274 DEFUN(inherit_flags, (child), Sym *child)
276 Sym *head, *parent, *member;
279 head = child->cg.cyc.head;
281 /* just a regular child, check its parents: */
282 child->cg.print_flag = FALSE;
283 child->cg.prop.fract = 0.0;
284 for (arc = child->cg.parents; arc; arc = arc->next_parent) {
285 parent = arc->parent;
286 if (child == parent) {
289 child->cg.print_flag |= parent->cg.print_flag;
291 * If the child was never actually called (e.g., this arc
292 * is static (and all others are, too)) no time propagates
296 child->cg.prop.fract += parent->cg.prop.fract
297 * (((double) arc->count) / ((double) child->ncalls));
302 * Its a member of a cycle, look at all parents from outside
305 head->cg.print_flag = FALSE;
306 head->cg.prop.fract = 0.0;
307 for (member = head->cg.cyc.next; member; member = member->cg.cyc.next)
309 for (arc = member->cg.parents; arc; arc = arc->next_parent) {
310 if (arc->parent->cg.cyc.head == head) {
313 parent = arc->parent;
314 head->cg.print_flag |= parent->cg.print_flag;
316 * If the cycle was never actually called (e.g. this
317 * arc is static (and all others are, too)) no time
318 * propagates along this arc.
321 head->cg.prop.fract += parent->cg.prop.fract
322 * (((double) arc->count) / ((double) head->ncalls));
326 for (member = head; member; member = member->cg.cyc.next) {
327 member->cg.print_flag = head->cg.print_flag;
328 member->cg.prop.fract = head->cg.prop.fract;
331 } /* inherit_flags */
335 * In one top-to-bottom pass over the topologically sorted symbols
337 * cg.print_flag as the union of parents' print_flags
338 * propfraction as the sum of fractional parents' propfractions
339 * and while we're here, sum time for functions.
342 DEFUN(propagate_flags, (symbols), Sym **symbols)
345 Sym *old_head, *child;
348 for (index = symtab.len - 1; index >= 0; --index) {
349 child = symbols[index];
351 * If we haven't done this function or cycle, inherit things
352 * from parent. This way, we are linear in the number of arcs
353 * since we do all members of a cycle (and the cycle itself)
354 * as we hit the first member of the cycle.
356 if (child->cg.cyc.head != old_head) {
357 old_head = child->cg.cyc.head;
358 inherit_flags(child);
361 printf("[prop_flags] ");
363 printf("inherits print-flag %d and prop-fract %f\n",
364 child->cg.print_flag, child->cg.prop.fract));
365 if (!child->cg.print_flag) {
367 * Printflag is off. It gets turned on by being in the
368 * INCL_GRAPH table, or there being an empty INCL_GRAPH
369 * table and not being in the EXCL_GRAPH table.
371 if (sym_lookup(&syms[INCL_GRAPH], child->addr)
372 || (syms[INCL_GRAPH].len == 0
373 && !sym_lookup(&syms[EXCL_GRAPH], child->addr)))
375 child->cg.print_flag = TRUE;
379 * This function has printing parents: maybe someone wants
380 * to shut it up by putting it in the EXCL_GRAPH table.
381 * (But favor INCL_GRAPH over EXCL_GRAPH.)
383 if (!sym_lookup(&syms[INCL_GRAPH], child->addr)
384 && sym_lookup(&syms[EXCL_GRAPH], child->addr))
386 child->cg.print_flag = FALSE;
389 if (child->cg.prop.fract == 0.0) {
391 * No parents to pass time to. Collect time from children
392 * if its in the INCL_TIME table, or there is an empty
393 * INCL_TIME table and its not in the EXCL_TIME table.
395 if (sym_lookup(&syms[INCL_TIME], child->addr)
396 || (syms[INCL_TIME].len == 0
397 && !sym_lookup(&syms[EXCL_TIME], child->addr)))
399 child->cg.prop.fract = 1.0;
403 * It has parents to pass time to, but maybe someone wants
404 * to shut it up by puttting it in the EXCL_TIME table.
405 * (But favor being in INCL_TIME tabe over being in
408 if (!sym_lookup(&syms[INCL_TIME], child->addr)
409 && sym_lookup(&syms[EXCL_TIME], child->addr))
411 child->cg.prop.fract = 0.0;
414 child->cg.prop.self = child->hist.time * child->cg.prop.fract;
415 print_time += child->cg.prop.self;
417 printf("[prop_flags] ");
419 printf(" ends up with printflag %d and prop-fract %f\n",
420 child->cg.print_flag, child->cg.prop.fract);
421 printf("[prop_flags] time %f propself %f print_time %f\n",
422 child->hist.time, child->cg.prop.self, print_time));
424 } /* propagate_flags */
428 * Compare by decreasing propagated time. If times are equal, but one
429 * is a cycle header, say that's first (e.g. less, i.e. -1). If one's
430 * name doesn't have an underscore and the other does, say that one is
431 * first. All else being equal, compare by names.
434 DEFUN(cmp_total, (lp, rp), const PTR lp AND const PTR rp)
436 const Sym *left = *(const Sym**)lp;
437 const Sym *right = *(const Sym**)rp;
440 diff = (left->cg.prop.self + left->cg.prop.child)
441 - (right->cg.prop.self + right->cg.prop.child);
448 if (!left->name && left->cg.cyc.num != 0) {
451 if (!right->name && right->cg.cyc.num != 0) {
460 if (left->name[0] != '_' && right->name[0] == '_') {
463 if (left->name[0] == '_' && right->name[0] != '_') {
466 if (left->ncalls > right->ncalls) {
469 if (left->ncalls < right->ncalls) {
472 return strcmp(left->name, right->name);
477 * Topologically sort the graph (collapsing cycles), and propagates
478 * time bottom up and flags top down.
481 DEFUN_VOID(cg_assemble)
483 Sym *parent, **time_sorted_syms, **top_sorted_syms;
486 extern void find_call PARAMS((Sym *parent,
487 bfd_vma p_lowpc, bfd_vma p_highpc));
489 * initialize various things:
490 * zero out child times.
491 * count self-recursive calls.
492 * indicate that nothing is on cycles.
494 for (parent = symtab.base; parent < symtab.limit; parent++) {
495 parent->cg.child_time = 0.0;
496 arc = arc_lookup(parent, parent);
497 if (arc && parent == arc->child) {
498 parent->ncalls -= arc->count;
499 parent->cg.self_calls = arc->count;
501 parent->cg.self_calls = 0;
503 parent->cg.prop.fract = 0.0;
504 parent->cg.prop.self = 0.0;
505 parent->cg.prop.child = 0.0;
506 parent->cg.print_flag = FALSE;
507 parent->cg.top_order = DFN_NAN;
508 parent->cg.cyc.num = 0;
509 parent->cg.cyc.head = parent;
510 parent->cg.cyc.next = 0;
511 if (ignore_direct_calls) {
512 find_call(parent, parent->addr, (parent+1)->addr);
516 * Topologically order things. If any node is unnumbered, number
517 * it and any of its descendents.
519 for (parent = symtab.base; parent < symtab.limit; parent++) {
520 if (parent->cg.top_order == DFN_NAN) {
525 /* link together nodes on the same cycle: */
528 /* sort the symbol table in reverse topological order: */
529 top_sorted_syms = (Sym**)xmalloc(symtab.len * sizeof(Sym*));
530 for (index = 0; index < symtab.len; ++index) {
531 top_sorted_syms[index] = &symtab.base[index];
533 qsort(top_sorted_syms, symtab.len, sizeof(Sym *), cmp_topo);
535 printf("[cg_assemble] topological sort listing\n");
536 for (index = 0; index < symtab.len; ++index) {
537 printf("[cg_assemble] ");
538 printf("%d:", top_sorted_syms[index]->cg.top_order);
539 print_name(top_sorted_syms[index]);
543 * Starting from the topological top, propagate print flags to
544 * children. also, calculate propagation fractions. this happens
545 * before time propagation since time propagation uses the
548 propagate_flags(top_sorted_syms);
551 * Starting from the topological bottom, propogate children times
555 for (index = 0; index < symtab.len; ++index) {
556 propagate_time(top_sorted_syms[index]);
559 free(top_sorted_syms);
562 * Now, sort by CG.PROP.SELF + CG.PROP.CHILD. Sorting both the regular
563 * function names and cycle headers.
565 time_sorted_syms = (Sym**)xmalloc((symtab.len + num_cycles)*sizeof(Sym*));
566 for (index = 0; index < symtab.len; index++) {
567 time_sorted_syms[index] = &symtab.base[index];
569 for (index = 1; index <= num_cycles; index++) {
570 time_sorted_syms[symtab.len + index - 1] = &cycle_header[index];
572 qsort(time_sorted_syms, symtab.len + num_cycles, sizeof(Sym*),
574 for (index = 0; index < symtab.len + num_cycles; index++) {
575 time_sorted_syms[index]->cg.index = index + 1;
577 return time_sorted_syms;
580 /*** end of cg_arcs.c ***/