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"
31 static int cmp_topo
PARAMS ((const PTR
, const PTR
));
32 static void propagate_time
PARAMS ((Sym
*));
33 static void cycle_time
PARAMS ((void));
34 static void cycle_link
PARAMS ((void));
35 static void inherit_flags
PARAMS ((Sym
*));
36 static void propagate_flags
PARAMS ((Sym
**));
37 static int cmp_total
PARAMS ((const PTR
, const PTR
));
40 unsigned int num_cycles
;
45 * Return TRUE iff PARENT has an arc to covers the address
46 * range covered by CHILD.
49 arc_lookup (parent
, child
)
55 if (!parent
|| !child
)
57 printf ("[arc_lookup] parent == 0 || child == 0\n");
60 DBG (LOOKUPDEBUG
, printf ("[arc_lookup] parent %s child %s\n",
61 parent
->name
, child
->name
));
62 for (arc
= parent
->cg
.children
; arc
; arc
= arc
->next_child
)
64 DBG (LOOKUPDEBUG
, printf ("[arc_lookup]\t parent %s child %s\n",
65 arc
->parent
->name
, arc
->child
->name
));
66 if (child
->addr
>= arc
->child
->addr
67 && child
->end_addr
<= arc
->child
->end_addr
)
77 * Add (or just increment) an arc:
80 arc_add (parent
, child
, count
)
85 static unsigned int maxarcs
= 0;
88 DBG (TALLYDEBUG
, printf ("[arc_add] %lu arcs from %s to %s\n",
89 count
, parent
->name
, child
->name
));
90 arc
= arc_lookup (parent
, child
);
94 * A hit: just increment the count.
96 DBG (TALLYDEBUG
, printf ("[tally] hit %lu += %lu\n",
101 arc
= (Arc
*) xmalloc (sizeof (*arc
));
102 memset (arc
, 0, sizeof (*arc
));
103 arc
->parent
= parent
;
107 /* If this isn't an arc for a recursive call to parent, then add it
108 to the array of arcs. */
111 /* If we've exhausted space in our current array, get a new one
112 and copy the contents. We might want to throttle the doubling
114 if (numarcs
== maxarcs
)
116 /* Determine how much space we want to allocate. */
121 /* Allocate the new array. */
122 newarcs
= (Arc
**)xmalloc(sizeof (Arc
*) * maxarcs
);
124 /* Copy the old array's contents into the new array. */
125 memcpy (newarcs
, arcs
, numarcs
* sizeof (Arc
*));
127 /* Free up the old array. */
130 /* And make the new array be the current array. */
134 /* Place this arc in the arc array. */
135 arcs
[numarcs
++] = arc
;
138 /* prepend this child to the children of this parent: */
139 arc
->next_child
= parent
->cg
.children
;
140 parent
->cg
.children
= arc
;
142 /* prepend this parent to the parents of this child: */
143 arc
->next_parent
= child
->cg
.parents
;
144 child
->cg
.parents
= arc
;
153 const Sym
*left
= *(const Sym
**) lp
;
154 const Sym
*right
= *(const Sym
**) rp
;
156 return left
->cg
.top_order
- right
->cg
.top_order
;
161 propagate_time (parent
)
166 double share
, prop_share
;
168 if (parent
->cg
.prop
.fract
== 0.0)
173 /* gather time from children of this parent: */
175 for (arc
= parent
->cg
.children
; arc
; arc
= arc
->next_child
)
178 if (arc
->count
== 0 || child
== parent
|| child
->cg
.prop
.fract
== 0)
182 if (child
->cg
.cyc
.head
!= child
)
184 if (parent
->cg
.cyc
.num
== child
->cg
.cyc
.num
)
188 if (parent
->cg
.top_order
<= child
->cg
.top_order
)
190 fprintf (stderr
, "[propagate] toporder botches\n");
192 child
= child
->cg
.cyc
.head
;
196 if (parent
->cg
.top_order
<= child
->cg
.top_order
)
198 fprintf (stderr
, "[propagate] toporder botches\n");
202 if (child
->ncalls
== 0)
207 /* distribute time for this arc: */
208 arc
->time
= child
->hist
.time
* (((double) arc
->count
)
209 / ((double) child
->ncalls
));
210 arc
->child_time
= child
->cg
.child_time
211 * (((double) arc
->count
) / ((double) child
->ncalls
));
212 share
= arc
->time
+ arc
->child_time
;
213 parent
->cg
.child_time
+= share
;
215 /* (1 - cg.prop.fract) gets lost along the way: */
216 prop_share
= parent
->cg
.prop
.fract
* share
;
218 /* fix things for printing: */
219 parent
->cg
.prop
.child
+= prop_share
;
220 arc
->time
*= parent
->cg
.prop
.fract
;
221 arc
->child_time
*= parent
->cg
.prop
.fract
;
223 /* add this share to the parent's cycle header, if any: */
224 if (parent
->cg
.cyc
.head
!= parent
)
226 parent
->cg
.cyc
.head
->cg
.child_time
+= share
;
227 parent
->cg
.cyc
.head
->cg
.prop
.child
+= prop_share
;
230 printf ("[prop_time] child \t");
232 printf (" with %f %f %lu/%lu\n", child
->hist
.time
,
233 child
->cg
.child_time
, arc
->count
, child
->ncalls
);
234 printf ("[prop_time] parent\t");
236 printf ("\n[prop_time] share %f\n", share
));
242 * Compute the time of a cycle as the sum of the times of all
250 for (cyc
= &cycle_header
[1]; cyc
<= &cycle_header
[num_cycles
]; ++cyc
)
252 for (member
= cyc
->cg
.cyc
.next
; member
; member
= member
->cg
.cyc
.next
)
254 if (member
->cg
.prop
.fract
== 0.0)
257 * All members have the same propfraction except those
258 * that were excluded with -E.
262 cyc
->hist
.time
+= member
->hist
.time
;
264 cyc
->cg
.prop
.self
= cyc
->cg
.prop
.fract
* cyc
->hist
.time
;
272 Sym
*sym
, *cyc
, *member
;
276 /* count the number of cycles, and initialize the cycle lists: */
279 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
281 /* this is how you find unattached cycles: */
282 if (sym
->cg
.cyc
.head
== sym
&& sym
->cg
.cyc
.next
)
289 * cycle_header is indexed by cycle number: i.e. it is origin 1,
292 cycle_header
= (Sym
*) xmalloc ((num_cycles
+ 1) * sizeof (Sym
));
295 * Now link cycles to true cycle-heads, number them, accumulate
296 * the data for the cycle.
300 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
302 if (!(sym
->cg
.cyc
.head
== sym
&& sym
->cg
.cyc
.next
!= 0))
309 cyc
->cg
.print_flag
= true; /* should this be printed? */
310 cyc
->cg
.top_order
= DFN_NAN
; /* graph call chain top-sort order */
311 cyc
->cg
.cyc
.num
= num
; /* internal number of cycle on */
312 cyc
->cg
.cyc
.head
= cyc
; /* pointer to head of cycle */
313 cyc
->cg
.cyc
.next
= sym
; /* pointer to next member of cycle */
314 DBG (CYCLEDEBUG
, printf ("[cycle_link] ");
316 printf (" is the head of cycle %d\n", num
));
318 /* link members to cycle header: */
319 for (member
= sym
; member
; member
= member
->cg
.cyc
.next
)
321 member
->cg
.cyc
.num
= num
;
322 member
->cg
.cyc
.head
= cyc
;
326 * Count calls from outside the cycle and those among cycle
329 for (member
= sym
; member
; member
= member
->cg
.cyc
.next
)
331 for (arc
= member
->cg
.parents
; arc
; arc
= arc
->next_parent
)
333 if (arc
->parent
== member
)
337 if (arc
->parent
->cg
.cyc
.num
== num
)
339 cyc
->cg
.self_calls
+= arc
->count
;
343 cyc
->ncalls
+= arc
->count
;
352 * Check if any parent of this child (or outside parents of this
353 * cycle) have their print flags on and set the print flag of the
354 * child (cycle) appropriately. Similarly, deal with propagation
355 * fractions from parents.
358 inherit_flags (child
)
361 Sym
*head
, *parent
, *member
;
364 head
= child
->cg
.cyc
.head
;
367 /* just a regular child, check its parents: */
368 child
->cg
.print_flag
= false;
369 child
->cg
.prop
.fract
= 0.0;
370 for (arc
= child
->cg
.parents
; arc
; arc
= arc
->next_parent
)
372 parent
= arc
->parent
;
377 child
->cg
.print_flag
|= parent
->cg
.print_flag
;
379 * If the child was never actually called (e.g., this arc
380 * is static (and all others are, too)) no time propagates
383 if (child
->ncalls
!= 0)
385 child
->cg
.prop
.fract
+= parent
->cg
.prop
.fract
386 * (((double) arc
->count
) / ((double) child
->ncalls
));
393 * Its a member of a cycle, look at all parents from outside
396 head
->cg
.print_flag
= false;
397 head
->cg
.prop
.fract
= 0.0;
398 for (member
= head
->cg
.cyc
.next
; member
; member
= member
->cg
.cyc
.next
)
400 for (arc
= member
->cg
.parents
; arc
; arc
= arc
->next_parent
)
402 if (arc
->parent
->cg
.cyc
.head
== head
)
406 parent
= arc
->parent
;
407 head
->cg
.print_flag
|= parent
->cg
.print_flag
;
409 * If the cycle was never actually called (e.g. this
410 * arc is static (and all others are, too)) no time
411 * propagates along this arc.
413 if (head
->ncalls
!= 0)
415 head
->cg
.prop
.fract
+= parent
->cg
.prop
.fract
416 * (((double) arc
->count
) / ((double) head
->ncalls
));
420 for (member
= head
; member
; member
= member
->cg
.cyc
.next
)
422 member
->cg
.print_flag
= head
->cg
.print_flag
;
423 member
->cg
.prop
.fract
= head
->cg
.prop
.fract
;
430 * In one top-to-bottom pass over the topologically sorted symbols
432 * cg.print_flag as the union of parents' print_flags
433 * propfraction as the sum of fractional parents' propfractions
434 * and while we're here, sum time for functions.
437 propagate_flags (symbols
)
441 Sym
*old_head
, *child
;
444 for (index
= symtab
.len
- 1; index
>= 0; --index
)
446 child
= symbols
[index
];
448 * If we haven't done this function or cycle, inherit things
449 * from parent. This way, we are linear in the number of arcs
450 * since we do all members of a cycle (and the cycle itself)
451 * as we hit the first member of the cycle.
453 if (child
->cg
.cyc
.head
!= old_head
)
455 old_head
= child
->cg
.cyc
.head
;
456 inherit_flags (child
);
459 printf ("[prop_flags] ");
461 printf ("inherits print-flag %d and prop-fract %f\n",
462 child
->cg
.print_flag
, child
->cg
.prop
.fract
));
463 if (!child
->cg
.print_flag
)
466 * Printflag is off. It gets turned on by being in the
467 * INCL_GRAPH table, or there being an empty INCL_GRAPH
468 * table and not being in the EXCL_GRAPH table.
470 if (sym_lookup (&syms
[INCL_GRAPH
], child
->addr
)
471 || (syms
[INCL_GRAPH
].len
== 0
472 && !sym_lookup (&syms
[EXCL_GRAPH
], child
->addr
)))
474 child
->cg
.print_flag
= true;
480 * This function has printing parents: maybe someone wants
481 * to shut it up by putting it in the EXCL_GRAPH table.
482 * (But favor INCL_GRAPH over EXCL_GRAPH.)
484 if (!sym_lookup (&syms
[INCL_GRAPH
], child
->addr
)
485 && sym_lookup (&syms
[EXCL_GRAPH
], child
->addr
))
487 child
->cg
.print_flag
= false;
490 if (child
->cg
.prop
.fract
== 0.0)
493 * No parents to pass time to. Collect time from children
494 * if its in the INCL_TIME table, or there is an empty
495 * INCL_TIME table and its not in the EXCL_TIME table.
497 if (sym_lookup (&syms
[INCL_TIME
], child
->addr
)
498 || (syms
[INCL_TIME
].len
== 0
499 && !sym_lookup (&syms
[EXCL_TIME
], child
->addr
)))
501 child
->cg
.prop
.fract
= 1.0;
507 * It has parents to pass time to, but maybe someone wants
508 * to shut it up by puttting it in the EXCL_TIME table.
509 * (But favor being in INCL_TIME tabe over being in
512 if (!sym_lookup (&syms
[INCL_TIME
], child
->addr
)
513 && sym_lookup (&syms
[EXCL_TIME
], child
->addr
))
515 child
->cg
.prop
.fract
= 0.0;
518 child
->cg
.prop
.self
= child
->hist
.time
* child
->cg
.prop
.fract
;
519 print_time
+= child
->cg
.prop
.self
;
521 printf ("[prop_flags] ");
523 printf (" ends up with printflag %d and prop-fract %f\n",
524 child
->cg
.print_flag
, child
->cg
.prop
.fract
);
525 printf ("[prop_flags] time %f propself %f print_time %f\n",
526 child
->hist
.time
, child
->cg
.prop
.self
, print_time
));
532 * Compare by decreasing propagated time. If times are equal, but one
533 * is a cycle header, say that's first (e.g. less, i.e. -1). If one's
534 * name doesn't have an underscore and the other does, say that one is
535 * first. All else being equal, compare by names.
542 const Sym
*left
= *(const Sym
**) lp
;
543 const Sym
*right
= *(const Sym
**) rp
;
546 diff
= (left
->cg
.prop
.self
+ left
->cg
.prop
.child
)
547 - (right
->cg
.prop
.self
+ right
->cg
.prop
.child
);
556 if (!left
->name
&& left
->cg
.cyc
.num
!= 0)
560 if (!right
->name
&& right
->cg
.cyc
.num
!= 0)
572 if (left
->name
[0] != '_' && right
->name
[0] == '_')
576 if (left
->name
[0] == '_' && right
->name
[0] != '_')
580 if (left
->ncalls
> right
->ncalls
)
584 if (left
->ncalls
< right
->ncalls
)
588 return strcmp (left
->name
, right
->name
);
593 * Topologically sort the graph (collapsing cycles), and propagates
594 * time bottom up and flags top down.
599 Sym
*parent
, **time_sorted_syms
, **top_sorted_syms
;
604 * initialize various things:
605 * zero out child times.
606 * count self-recursive calls.
607 * indicate that nothing is on cycles.
609 for (parent
= symtab
.base
; parent
< symtab
.limit
; parent
++)
611 parent
->cg
.child_time
= 0.0;
612 arc
= arc_lookup (parent
, parent
);
613 if (arc
&& parent
== arc
->child
)
615 parent
->ncalls
-= arc
->count
;
616 parent
->cg
.self_calls
= arc
->count
;
620 parent
->cg
.self_calls
= 0;
622 parent
->cg
.prop
.fract
= 0.0;
623 parent
->cg
.prop
.self
= 0.0;
624 parent
->cg
.prop
.child
= 0.0;
625 parent
->cg
.print_flag
= false;
626 parent
->cg
.top_order
= DFN_NAN
;
627 parent
->cg
.cyc
.num
= 0;
628 parent
->cg
.cyc
.head
= parent
;
629 parent
->cg
.cyc
.next
= 0;
630 if (ignore_direct_calls
)
632 find_call (parent
, parent
->addr
, (parent
+ 1)->addr
);
636 * Topologically order things. If any node is unnumbered, number
637 * it and any of its descendents.
639 for (parent
= symtab
.base
; parent
< symtab
.limit
; parent
++)
641 if (parent
->cg
.top_order
== DFN_NAN
)
647 /* link together nodes on the same cycle: */
650 /* sort the symbol table in reverse topological order: */
651 top_sorted_syms
= (Sym
**) xmalloc (symtab
.len
* sizeof (Sym
*));
652 for (index
= 0; index
< symtab
.len
; ++index
)
654 top_sorted_syms
[index
] = &symtab
.base
[index
];
656 qsort (top_sorted_syms
, symtab
.len
, sizeof (Sym
*), cmp_topo
);
658 printf ("[cg_assemble] topological sort listing\n");
659 for (index
= 0; index
< symtab
.len
; ++index
)
661 printf ("[cg_assemble] ");
662 printf ("%d:", top_sorted_syms
[index
]->cg
.top_order
);
663 print_name (top_sorted_syms
[index
]);
668 * Starting from the topological top, propagate print flags to
669 * children. also, calculate propagation fractions. this happens
670 * before time propagation since time propagation uses the
673 propagate_flags (top_sorted_syms
);
676 * Starting from the topological bottom, propogate children times
680 for (index
= 0; index
< symtab
.len
; ++index
)
682 propagate_time (top_sorted_syms
[index
]);
685 free (top_sorted_syms
);
688 * Now, sort by CG.PROP.SELF + CG.PROP.CHILD. Sorting both the regular
689 * function names and cycle headers.
691 time_sorted_syms
= (Sym
**) xmalloc ((symtab
.len
+ num_cycles
) * sizeof (Sym
*));
692 for (index
= 0; index
< symtab
.len
; index
++)
694 time_sorted_syms
[index
] = &symtab
.base
[index
];
696 for (index
= 1; index
<= num_cycles
; index
++)
698 time_sorted_syms
[symtab
.len
+ index
- 1] = &cycle_header
[index
];
700 qsort (time_sorted_syms
, symtab
.len
+ num_cycles
, sizeof (Sym
*),
702 for (index
= 0; index
< symtab
.len
+ num_cycles
; index
++)
704 time_sorted_syms
[index
]->cg
.index
= index
+ 1;
706 return time_sorted_syms
;
This page took 0.042599 seconds and 4 git commands to generate.