2 * Copyright (c) 1983, 1993, 2001
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "libiberty.h"
31 #include "search_list.h"
34 #include "call_graph.h"
42 static int cmp_topo (const void *, const void *);
43 static void propagate_time (Sym
*);
44 static void cycle_time (void);
45 static void cycle_link (void);
46 static void inherit_flags (Sym
*);
47 static void propagate_flags (Sym
**);
48 static int cmp_total (const void *, const void *);
51 unsigned int num_cycles
;
56 * Return TRUE iff PARENT has an arc to covers the address
57 * range covered by CHILD.
60 arc_lookup (Sym
*parent
, Sym
*child
)
64 if (!parent
|| !child
)
66 printf ("[arc_lookup] parent == 0 || child == 0\n");
69 DBG (LOOKUPDEBUG
, printf ("[arc_lookup] parent %s child %s\n",
70 parent
->name
, child
->name
));
71 for (arc
= parent
->cg
.children
; arc
; arc
= arc
->next_child
)
73 DBG (LOOKUPDEBUG
, printf ("[arc_lookup]\t parent %s child %s\n",
74 arc
->parent
->name
, arc
->child
->name
));
75 if (child
->addr
>= arc
->child
->addr
76 && child
->end_addr
<= arc
->child
->end_addr
)
86 * Add (or just increment) an arc:
89 arc_add (Sym
*parent
, Sym
*child
, unsigned long count
)
91 static unsigned int maxarcs
= 0;
94 DBG (TALLYDEBUG
, printf ("[arc_add] %lu arcs from %s to %s\n",
95 count
, parent
->name
, child
->name
));
96 arc
= arc_lookup (parent
, child
);
100 * A hit: just increment the count.
102 DBG (TALLYDEBUG
, printf ("[tally] hit %lu += %lu\n",
107 arc
= (Arc
*) xmalloc (sizeof (*arc
));
108 memset (arc
, 0, sizeof (*arc
));
109 arc
->parent
= parent
;
113 /* If this isn't an arc for a recursive call to parent, then add it
114 to the array of arcs. */
117 /* If we've exhausted space in our current array, get a new one
118 and copy the contents. We might want to throttle the doubling
120 if (numarcs
== maxarcs
)
122 /* Determine how much space we want to allocate. */
127 /* Allocate the new array. */
128 newarcs
= (Arc
**)xmalloc(sizeof (Arc
*) * maxarcs
);
130 /* Copy the old array's contents into the new array. */
131 memcpy (newarcs
, arcs
, numarcs
* sizeof (Arc
*));
133 /* Free up the old array. */
136 /* And make the new array be the current array. */
140 /* Place this arc in the arc array. */
141 arcs
[numarcs
++] = arc
;
144 /* prepend this child to the children of this parent: */
145 arc
->next_child
= parent
->cg
.children
;
146 parent
->cg
.children
= arc
;
148 /* prepend this parent to the parents of this child: */
149 arc
->next_parent
= child
->cg
.parents
;
150 child
->cg
.parents
= arc
;
155 cmp_topo (const void *lp
, const void *rp
)
157 const Sym
*left
= *(const Sym
**) lp
;
158 const Sym
*right
= *(const Sym
**) rp
;
160 return left
->cg
.top_order
- right
->cg
.top_order
;
165 propagate_time (Sym
*parent
)
169 double share
, prop_share
;
171 if (parent
->cg
.prop
.fract
== 0.0)
176 /* gather time from children of this parent: */
178 for (arc
= parent
->cg
.children
; arc
; arc
= arc
->next_child
)
181 if (arc
->count
== 0 || child
== parent
|| child
->cg
.prop
.fract
== 0)
185 if (child
->cg
.cyc
.head
!= child
)
187 if (parent
->cg
.cyc
.num
== child
->cg
.cyc
.num
)
191 if (parent
->cg
.top_order
<= child
->cg
.top_order
)
193 fprintf (stderr
, "[propagate] toporder botches\n");
195 child
= child
->cg
.cyc
.head
;
199 if (parent
->cg
.top_order
<= child
->cg
.top_order
)
201 fprintf (stderr
, "[propagate] toporder botches\n");
205 if (child
->ncalls
== 0)
210 /* distribute time for this arc: */
211 arc
->time
= child
->hist
.time
* (((double) arc
->count
)
212 / ((double) child
->ncalls
));
213 arc
->child_time
= child
->cg
.child_time
214 * (((double) arc
->count
) / ((double) child
->ncalls
));
215 share
= arc
->time
+ arc
->child_time
;
216 parent
->cg
.child_time
+= share
;
218 /* (1 - cg.prop.fract) gets lost along the way: */
219 prop_share
= parent
->cg
.prop
.fract
* share
;
221 /* fix things for printing: */
222 parent
->cg
.prop
.child
+= prop_share
;
223 arc
->time
*= parent
->cg
.prop
.fract
;
224 arc
->child_time
*= parent
->cg
.prop
.fract
;
226 /* add this share to the parent's cycle header, if any: */
227 if (parent
->cg
.cyc
.head
!= parent
)
229 parent
->cg
.cyc
.head
->cg
.child_time
+= share
;
230 parent
->cg
.cyc
.head
->cg
.prop
.child
+= prop_share
;
233 printf ("[prop_time] child \t");
235 printf (" with %f %f %lu/%lu\n", child
->hist
.time
,
236 child
->cg
.child_time
, arc
->count
, child
->ncalls
);
237 printf ("[prop_time] parent\t");
239 printf ("\n[prop_time] share %f\n", share
));
245 * Compute the time of a cycle as the sum of the times of all
253 for (cyc
= &cycle_header
[1]; cyc
<= &cycle_header
[num_cycles
]; ++cyc
)
255 for (member
= cyc
->cg
.cyc
.next
; member
; member
= member
->cg
.cyc
.next
)
257 if (member
->cg
.prop
.fract
== 0.0)
260 * All members have the same propfraction except those
261 * that were excluded with -E.
265 cyc
->hist
.time
+= member
->hist
.time
;
267 cyc
->cg
.prop
.self
= cyc
->cg
.prop
.fract
* cyc
->hist
.time
;
275 Sym
*sym
, *cyc
, *member
;
279 /* count the number of cycles, and initialize the cycle lists: */
282 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
284 /* this is how you find unattached cycles: */
285 if (sym
->cg
.cyc
.head
== sym
&& sym
->cg
.cyc
.next
)
292 * cycle_header is indexed by cycle number: i.e. it is origin 1,
295 cycle_header
= (Sym
*) xmalloc ((num_cycles
+ 1) * sizeof (Sym
));
298 * Now link cycles to true cycle-heads, number them, accumulate
299 * the data for the cycle.
303 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
305 if (!(sym
->cg
.cyc
.head
== sym
&& sym
->cg
.cyc
.next
!= 0))
312 cyc
->cg
.print_flag
= true; /* should this be printed? */
313 cyc
->cg
.top_order
= DFN_NAN
; /* graph call chain top-sort order */
314 cyc
->cg
.cyc
.num
= num
; /* internal number of cycle on */
315 cyc
->cg
.cyc
.head
= cyc
; /* pointer to head of cycle */
316 cyc
->cg
.cyc
.next
= sym
; /* pointer to next member of cycle */
317 DBG (CYCLEDEBUG
, printf ("[cycle_link] ");
319 printf (" is the head of cycle %d\n", num
));
321 /* link members to cycle header: */
322 for (member
= sym
; member
; member
= member
->cg
.cyc
.next
)
324 member
->cg
.cyc
.num
= num
;
325 member
->cg
.cyc
.head
= cyc
;
329 * Count calls from outside the cycle and those among cycle
332 for (member
= sym
; member
; member
= member
->cg
.cyc
.next
)
334 for (arc
= member
->cg
.parents
; arc
; arc
= arc
->next_parent
)
336 if (arc
->parent
== member
)
340 if (arc
->parent
->cg
.cyc
.num
== num
)
342 cyc
->cg
.self_calls
+= arc
->count
;
346 cyc
->ncalls
+= arc
->count
;
355 * Check if any parent of this child (or outside parents of this
356 * cycle) have their print flags on and set the print flag of the
357 * child (cycle) appropriately. Similarly, deal with propagation
358 * fractions from parents.
361 inherit_flags (Sym
*child
)
363 Sym
*head
, *parent
, *member
;
366 head
= child
->cg
.cyc
.head
;
369 /* just a regular child, check its parents: */
370 child
->cg
.print_flag
= false;
371 child
->cg
.prop
.fract
= 0.0;
372 for (arc
= child
->cg
.parents
; arc
; arc
= arc
->next_parent
)
374 parent
= arc
->parent
;
379 child
->cg
.print_flag
|= parent
->cg
.print_flag
;
381 * If the child was never actually called (e.g., this arc
382 * is static (and all others are, too)) no time propagates
385 if (child
->ncalls
!= 0)
387 child
->cg
.prop
.fract
+= parent
->cg
.prop
.fract
388 * (((double) arc
->count
) / ((double) child
->ncalls
));
395 * Its a member of a cycle, look at all parents from outside
398 head
->cg
.print_flag
= false;
399 head
->cg
.prop
.fract
= 0.0;
400 for (member
= head
->cg
.cyc
.next
; member
; member
= member
->cg
.cyc
.next
)
402 for (arc
= member
->cg
.parents
; arc
; arc
= arc
->next_parent
)
404 if (arc
->parent
->cg
.cyc
.head
== head
)
408 parent
= arc
->parent
;
409 head
->cg
.print_flag
|= parent
->cg
.print_flag
;
411 * If the cycle was never actually called (e.g. this
412 * arc is static (and all others are, too)) no time
413 * propagates along this arc.
415 if (head
->ncalls
!= 0)
417 head
->cg
.prop
.fract
+= parent
->cg
.prop
.fract
418 * (((double) arc
->count
) / ((double) head
->ncalls
));
422 for (member
= head
; member
; member
= member
->cg
.cyc
.next
)
424 member
->cg
.print_flag
= head
->cg
.print_flag
;
425 member
->cg
.prop
.fract
= head
->cg
.prop
.fract
;
432 * In one top-to-bottom pass over the topologically sorted symbols
434 * cg.print_flag as the union of parents' print_flags
435 * propfraction as the sum of fractional parents' propfractions
436 * and while we're here, sum time for functions.
439 propagate_flags (Sym
**symbols
)
442 Sym
*old_head
, *child
;
445 for (sym_index
= symtab
.len
- 1; sym_index
>= 0; --sym_index
)
447 child
= symbols
[sym_index
];
449 * If we haven't done this function or cycle, inherit things
450 * from parent. This way, we are linear in the number of arcs
451 * since we do all members of a cycle (and the cycle itself)
452 * as we hit the first member of the cycle.
454 if (child
->cg
.cyc
.head
!= old_head
)
456 old_head
= child
->cg
.cyc
.head
;
457 inherit_flags (child
);
460 printf ("[prop_flags] ");
462 printf ("inherits print-flag %d and prop-fract %f\n",
463 child
->cg
.print_flag
, child
->cg
.prop
.fract
));
464 if (!child
->cg
.print_flag
)
467 * Printflag is off. It gets turned on by being in the
468 * INCL_GRAPH table, or there being an empty INCL_GRAPH
469 * table and not being in the EXCL_GRAPH table.
471 if (sym_lookup (&syms
[INCL_GRAPH
], child
->addr
)
472 || (syms
[INCL_GRAPH
].len
== 0
473 && !sym_lookup (&syms
[EXCL_GRAPH
], child
->addr
)))
475 child
->cg
.print_flag
= true;
481 * This function has printing parents: maybe someone wants
482 * to shut it up by putting it in the EXCL_GRAPH table.
483 * (But favor INCL_GRAPH over EXCL_GRAPH.)
485 if (!sym_lookup (&syms
[INCL_GRAPH
], child
->addr
)
486 && sym_lookup (&syms
[EXCL_GRAPH
], child
->addr
))
488 child
->cg
.print_flag
= false;
491 if (child
->cg
.prop
.fract
== 0.0)
494 * No parents to pass time to. Collect time from children
495 * if its in the INCL_TIME table, or there is an empty
496 * INCL_TIME table and its not in the EXCL_TIME table.
498 if (sym_lookup (&syms
[INCL_TIME
], child
->addr
)
499 || (syms
[INCL_TIME
].len
== 0
500 && !sym_lookup (&syms
[EXCL_TIME
], child
->addr
)))
502 child
->cg
.prop
.fract
= 1.0;
508 * It has parents to pass time to, but maybe someone wants
509 * to shut it up by puttting it in the EXCL_TIME table.
510 * (But favor being in INCL_TIME tabe over being in
513 if (!sym_lookup (&syms
[INCL_TIME
], child
->addr
)
514 && sym_lookup (&syms
[EXCL_TIME
], child
->addr
))
516 child
->cg
.prop
.fract
= 0.0;
519 child
->cg
.prop
.self
= child
->hist
.time
* child
->cg
.prop
.fract
;
520 print_time
+= child
->cg
.prop
.self
;
522 printf ("[prop_flags] ");
524 printf (" ends up with printflag %d and prop-fract %f\n",
525 child
->cg
.print_flag
, child
->cg
.prop
.fract
);
526 printf ("[prop_flags] time %f propself %f print_time %f\n",
527 child
->hist
.time
, child
->cg
.prop
.self
, print_time
));
533 * Compare by decreasing propagated time. If times are equal, but one
534 * is a cycle header, say that's first (e.g. less, i.e. -1). If one's
535 * name doesn't have an underscore and the other does, say that one is
536 * first. All else being equal, compare by names.
539 cmp_total (const void *lp
, const void *rp
)
541 const Sym
*left
= *(const Sym
**) lp
;
542 const Sym
*right
= *(const Sym
**) rp
;
545 diff
= (left
->cg
.prop
.self
+ left
->cg
.prop
.child
)
546 - (right
->cg
.prop
.self
+ right
->cg
.prop
.child
);
555 if (!left
->name
&& left
->cg
.cyc
.num
!= 0)
559 if (!right
->name
&& right
->cg
.cyc
.num
!= 0)
571 if (left
->name
[0] != '_' && right
->name
[0] == '_')
575 if (left
->name
[0] == '_' && right
->name
[0] != '_')
579 if (left
->ncalls
> right
->ncalls
)
583 if (left
->ncalls
< right
->ncalls
)
587 return strcmp (left
->name
, right
->name
);
591 /* Topologically sort the graph (collapsing cycles), and propagates
592 time bottom up and flags top down. */
597 Sym
*parent
, **time_sorted_syms
, **top_sorted_syms
;
598 unsigned int sym_index
;
601 /* Initialize various things:
602 Zero out child times.
603 Count self-recursive calls.
604 Indicate that nothing is on cycles. */
605 for (parent
= symtab
.base
; parent
< symtab
.limit
; parent
++)
607 parent
->cg
.child_time
= 0.0;
608 arc
= arc_lookup (parent
, parent
);
609 if (arc
&& parent
== arc
->child
)
611 parent
->ncalls
-= arc
->count
;
612 parent
->cg
.self_calls
= arc
->count
;
616 parent
->cg
.self_calls
= 0;
618 parent
->cg
.prop
.fract
= 0.0;
619 parent
->cg
.prop
.self
= 0.0;
620 parent
->cg
.prop
.child
= 0.0;
621 parent
->cg
.print_flag
= false;
622 parent
->cg
.top_order
= DFN_NAN
;
623 parent
->cg
.cyc
.num
= 0;
624 parent
->cg
.cyc
.head
= parent
;
625 parent
->cg
.cyc
.next
= 0;
626 if (ignore_direct_calls
627 && parent
->addr
>= core_text_sect
->vma
628 && parent
->addr
< core_text_sect
->vma
+ core_text_sect
->size
629 && (parent
+ 1)->addr
>= core_text_sect
->vma
630 && (parent
+ 1)->addr
<= core_text_sect
->vma
+ core_text_sect
->size
)
631 find_call (parent
, parent
->addr
, (parent
+ 1)->addr
);
634 /* Topologically order things. If any node is unnumbered, number
635 it and any of its descendents. */
636 for (parent
= symtab
.base
; parent
< symtab
.limit
; parent
++)
638 if (parent
->cg
.top_order
== DFN_NAN
)
642 /* Link together nodes on the same cycle. */
645 /* Sort the symbol table in reverse topological order. */
646 top_sorted_syms
= (Sym
**) xmalloc (symtab
.len
* sizeof (Sym
*));
647 for (sym_index
= 0; sym_index
< symtab
.len
; ++sym_index
)
648 top_sorted_syms
[sym_index
] = &symtab
.base
[sym_index
];
650 qsort (top_sorted_syms
, symtab
.len
, sizeof (Sym
*), cmp_topo
);
652 printf ("[cg_assemble] topological sort listing\n");
653 for (sym_index
= 0; sym_index
< symtab
.len
; ++sym_index
)
655 printf ("[cg_assemble] ");
656 printf ("%d:", top_sorted_syms
[sym_index
]->cg
.top_order
);
657 print_name (top_sorted_syms
[sym_index
]);
662 /* Starting from the topological top, propagate print flags to
663 children. also, calculate propagation fractions. this happens
664 before time propagation since time propagation uses the
666 propagate_flags (top_sorted_syms
);
668 /* Starting from the topological bottom, propagate children times
671 for (sym_index
= 0; sym_index
< symtab
.len
; ++sym_index
)
672 propagate_time (top_sorted_syms
[sym_index
]);
674 free (top_sorted_syms
);
676 /* Now, sort by CG.PROP.SELF + CG.PROP.CHILD. Sorting both the regular
677 function names and cycle headers. */
678 time_sorted_syms
= (Sym
**) xmalloc ((symtab
.len
+ num_cycles
) * sizeof (Sym
*));
679 for (sym_index
= 0; sym_index
< symtab
.len
; sym_index
++)
680 time_sorted_syms
[sym_index
] = &symtab
.base
[sym_index
];
682 for (sym_index
= 1; sym_index
<= num_cycles
; sym_index
++)
683 time_sorted_syms
[symtab
.len
+ sym_index
- 1] = &cycle_header
[sym_index
];
685 qsort (time_sorted_syms
, symtab
.len
+ num_cycles
, sizeof (Sym
*),
688 for (sym_index
= 0; sym_index
< symtab
.len
+ num_cycles
; sym_index
++)
689 time_sorted_syms
[sym_index
]->cg
.index
= sym_index
+ 1;
691 return time_sorted_syms
;