196e3568d9a36ab828b9802fc697a3d9c37e2ee5
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"
34 * Return TRUE iff PARENT has an arc to covers the address
35 * range covered by CHILD.
38 DEFUN (arc_lookup
, (parent
, child
), Sym
* parent AND Sym
* child
)
42 if (!parent
|| !child
)
44 printf ("[arc_lookup] parent == 0 || child == 0\n");
47 DBG (LOOKUPDEBUG
, printf ("[arc_lookup] parent %s child %s\n",
48 parent
->name
, child
->name
));
49 for (arc
= parent
->cg
.children
; arc
; arc
= arc
->next_child
)
51 DBG (LOOKUPDEBUG
, printf ("[arc_lookup]\t parent %s child %s\n",
52 arc
->parent
->name
, arc
->child
->name
));
53 if (child
->addr
>= arc
->child
->addr
54 && child
->end_addr
<= arc
->child
->end_addr
)
64 * Add (or just increment) an arc:
67 DEFUN (arc_add
, (parent
, child
, count
),
68 Sym
* parent AND Sym
* child AND
int count
)
70 static int maxarcs
= 0;
73 DBG (TALLYDEBUG
, printf ("[arc_add] %d arcs from %s to %s\n",
74 count
, parent
->name
, child
->name
));
75 arc
= arc_lookup (parent
, child
);
79 * A hit: just increment the count.
81 DBG (TALLYDEBUG
, printf ("[tally] hit %d += %d\n",
86 arc
= (Arc
*) xmalloc (sizeof (*arc
));
91 /* If this isn't an arc for a recursive call to parent, then add it
92 to the array of arcs. */
95 /* If we've exhausted space in our current array, get a new one
96 and copy the contents. We might want to throttle the doubling
98 if (numarcs
== maxarcs
)
100 /* Determine how much space we want to allocate. */
105 /* Allocate the new array. */
106 newarcs
= (Arc
**)xmalloc(sizeof (Arc
*) * maxarcs
);
108 /* Copy the old array's contents into the new array. */
109 memcpy (newarcs
, arcs
, numarcs
* sizeof (Arc
*));
111 /* Free up the old array. */
114 /* And make the new array be the current array. */
118 /* Place this arc in the arc array. */
119 arcs
[numarcs
++] = arc
;
122 /* prepend this child to the children of this parent: */
123 arc
->next_child
= parent
->cg
.children
;
124 parent
->cg
.children
= arc
;
126 /* prepend this parent to the parents of this child: */
127 arc
->next_parent
= child
->cg
.parents
;
128 child
->cg
.parents
= arc
;
133 DEFUN (cmp_topo
, (lp
, rp
), const PTR lp AND
const PTR rp
)
135 const Sym
*left
= *(const Sym
**) lp
;
136 const Sym
*right
= *(const Sym
**) rp
;
138 return left
->cg
.top_order
- right
->cg
.top_order
;
143 DEFUN (propagate_time
, (parent
), Sym
* parent
)
147 double share
, prop_share
;
149 if (parent
->cg
.prop
.fract
== 0.0)
154 /* gather time from children of this parent: */
156 for (arc
= parent
->cg
.children
; arc
; arc
= arc
->next_child
)
159 if (arc
->count
== 0 || child
== parent
|| child
->cg
.prop
.fract
== 0)
163 if (child
->cg
.cyc
.head
!= child
)
165 if (parent
->cg
.cyc
.num
== child
->cg
.cyc
.num
)
169 if (parent
->cg
.top_order
<= child
->cg
.top_order
)
171 fprintf (stderr
, "[propagate] toporder botches\n");
173 child
= child
->cg
.cyc
.head
;
177 if (parent
->cg
.top_order
<= child
->cg
.top_order
)
179 fprintf (stderr
, "[propagate] toporder botches\n");
183 if (child
->ncalls
== 0)
188 /* distribute time for this arc: */
189 arc
->time
= child
->hist
.time
* (((double) arc
->count
)
190 / ((double) child
->ncalls
));
191 arc
->child_time
= child
->cg
.child_time
192 * (((double) arc
->count
) / ((double) child
->ncalls
));
193 share
= arc
->time
+ arc
->child_time
;
194 parent
->cg
.child_time
+= share
;
196 /* (1 - cg.prop.fract) gets lost along the way: */
197 prop_share
= parent
->cg
.prop
.fract
* share
;
199 /* fix things for printing: */
200 parent
->cg
.prop
.child
+= prop_share
;
201 arc
->time
*= parent
->cg
.prop
.fract
;
202 arc
->child_time
*= parent
->cg
.prop
.fract
;
204 /* add this share to the parent's cycle header, if any: */
205 if (parent
->cg
.cyc
.head
!= parent
)
207 parent
->cg
.cyc
.head
->cg
.child_time
+= share
;
208 parent
->cg
.cyc
.head
->cg
.prop
.child
+= prop_share
;
211 printf ("[prop_time] child \t");
213 printf (" with %f %f %d/%d\n", child
->hist
.time
,
214 child
->cg
.child_time
, arc
->count
, child
->ncalls
);
215 printf ("[prop_time] parent\t");
217 printf ("\n[prop_time] share %f\n", share
));
223 * Compute the time of a cycle as the sum of the times of all
227 DEFUN_VOID (cycle_time
)
231 for (cyc
= &cycle_header
[1]; cyc
<= &cycle_header
[num_cycles
]; ++cyc
)
233 for (member
= cyc
->cg
.cyc
.next
; member
; member
= member
->cg
.cyc
.next
)
235 if (member
->cg
.prop
.fract
== 0.0)
238 * All members have the same propfraction except those
239 * that were excluded with -E.
243 cyc
->hist
.time
+= member
->hist
.time
;
245 cyc
->cg
.prop
.self
= cyc
->cg
.prop
.fract
* cyc
->hist
.time
;
251 DEFUN_VOID (cycle_link
)
253 Sym
*sym
, *cyc
, *member
;
257 /* count the number of cycles, and initialize the cycle lists: */
260 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
262 /* this is how you find unattached cycles: */
263 if (sym
->cg
.cyc
.head
== sym
&& sym
->cg
.cyc
.next
)
270 * cycle_header is indexed by cycle number: i.e. it is origin 1,
273 cycle_header
= (Sym
*) xmalloc ((num_cycles
+ 1) * sizeof (Sym
));
276 * Now link cycles to true cycle-heads, number them, accumulate
277 * the data for the cycle.
281 for (sym
= symtab
.base
; sym
< symtab
.limit
; ++sym
)
283 if (!(sym
->cg
.cyc
.head
== sym
&& sym
->cg
.cyc
.next
!= 0))
290 cyc
->cg
.print_flag
= TRUE
; /* should this be printed? */
291 cyc
->cg
.top_order
= DFN_NAN
; /* graph call chain top-sort order */
292 cyc
->cg
.cyc
.num
= num
; /* internal number of cycle on */
293 cyc
->cg
.cyc
.head
= cyc
; /* pointer to head of cycle */
294 cyc
->cg
.cyc
.next
= sym
; /* pointer to next member of cycle */
295 DBG (CYCLEDEBUG
, printf ("[cycle_link] ");
297 printf (" is the head of cycle %d\n", num
));
299 /* link members to cycle header: */
300 for (member
= sym
; member
; member
= member
->cg
.cyc
.next
)
302 member
->cg
.cyc
.num
= num
;
303 member
->cg
.cyc
.head
= cyc
;
307 * Count calls from outside the cycle and those among cycle
310 for (member
= sym
; member
; member
= member
->cg
.cyc
.next
)
312 for (arc
= member
->cg
.parents
; arc
; arc
= arc
->next_parent
)
314 if (arc
->parent
== member
)
318 if (arc
->parent
->cg
.cyc
.num
== num
)
320 cyc
->cg
.self_calls
+= arc
->count
;
324 cyc
->ncalls
+= arc
->count
;
333 * Check if any parent of this child (or outside parents of this
334 * cycle) have their print flags on and set the print flag of the
335 * child (cycle) appropriately. Similarly, deal with propagation
336 * fractions from parents.
339 DEFUN (inherit_flags
, (child
), Sym
* child
)
341 Sym
*head
, *parent
, *member
;
344 head
= child
->cg
.cyc
.head
;
347 /* just a regular child, check its parents: */
348 child
->cg
.print_flag
= FALSE
;
349 child
->cg
.prop
.fract
= 0.0;
350 for (arc
= child
->cg
.parents
; arc
; arc
= arc
->next_parent
)
352 parent
= arc
->parent
;
357 child
->cg
.print_flag
|= parent
->cg
.print_flag
;
359 * If the child was never actually called (e.g., this arc
360 * is static (and all others are, too)) no time propagates
365 child
->cg
.prop
.fract
+= parent
->cg
.prop
.fract
366 * (((double) arc
->count
) / ((double) child
->ncalls
));
373 * Its a member of a cycle, look at all parents from outside
376 head
->cg
.print_flag
= FALSE
;
377 head
->cg
.prop
.fract
= 0.0;
378 for (member
= head
->cg
.cyc
.next
; member
; member
= member
->cg
.cyc
.next
)
380 for (arc
= member
->cg
.parents
; arc
; arc
= arc
->next_parent
)
382 if (arc
->parent
->cg
.cyc
.head
== head
)
386 parent
= arc
->parent
;
387 head
->cg
.print_flag
|= parent
->cg
.print_flag
;
389 * If the cycle was never actually called (e.g. this
390 * arc is static (and all others are, too)) no time
391 * propagates along this arc.
395 head
->cg
.prop
.fract
+= parent
->cg
.prop
.fract
396 * (((double) arc
->count
) / ((double) head
->ncalls
));
400 for (member
= head
; member
; member
= member
->cg
.cyc
.next
)
402 member
->cg
.print_flag
= head
->cg
.print_flag
;
403 member
->cg
.prop
.fract
= head
->cg
.prop
.fract
;
410 * In one top-to-bottom pass over the topologically sorted symbols
412 * cg.print_flag as the union of parents' print_flags
413 * propfraction as the sum of fractional parents' propfractions
414 * and while we're here, sum time for functions.
417 DEFUN (propagate_flags
, (symbols
), Sym
** symbols
)
420 Sym
*old_head
, *child
;
423 for (index
= symtab
.len
- 1; index
>= 0; --index
)
425 child
= symbols
[index
];
427 * If we haven't done this function or cycle, inherit things
428 * from parent. This way, we are linear in the number of arcs
429 * since we do all members of a cycle (and the cycle itself)
430 * as we hit the first member of the cycle.
432 if (child
->cg
.cyc
.head
!= old_head
)
434 old_head
= child
->cg
.cyc
.head
;
435 inherit_flags (child
);
438 printf ("[prop_flags] ");
440 printf ("inherits print-flag %d and prop-fract %f\n",
441 child
->cg
.print_flag
, child
->cg
.prop
.fract
));
442 if (!child
->cg
.print_flag
)
445 * Printflag is off. It gets turned on by being in the
446 * INCL_GRAPH table, or there being an empty INCL_GRAPH
447 * table and not being in the EXCL_GRAPH table.
449 if (sym_lookup (&syms
[INCL_GRAPH
], child
->addr
)
450 || (syms
[INCL_GRAPH
].len
== 0
451 && !sym_lookup (&syms
[EXCL_GRAPH
], child
->addr
)))
453 child
->cg
.print_flag
= TRUE
;
459 * This function has printing parents: maybe someone wants
460 * to shut it up by putting it in the EXCL_GRAPH table.
461 * (But favor INCL_GRAPH over EXCL_GRAPH.)
463 if (!sym_lookup (&syms
[INCL_GRAPH
], child
->addr
)
464 && sym_lookup (&syms
[EXCL_GRAPH
], child
->addr
))
466 child
->cg
.print_flag
= FALSE
;
469 if (child
->cg
.prop
.fract
== 0.0)
472 * No parents to pass time to. Collect time from children
473 * if its in the INCL_TIME table, or there is an empty
474 * INCL_TIME table and its not in the EXCL_TIME table.
476 if (sym_lookup (&syms
[INCL_TIME
], child
->addr
)
477 || (syms
[INCL_TIME
].len
== 0
478 && !sym_lookup (&syms
[EXCL_TIME
], child
->addr
)))
480 child
->cg
.prop
.fract
= 1.0;
486 * It has parents to pass time to, but maybe someone wants
487 * to shut it up by puttting it in the EXCL_TIME table.
488 * (But favor being in INCL_TIME tabe over being in
491 if (!sym_lookup (&syms
[INCL_TIME
], child
->addr
)
492 && sym_lookup (&syms
[EXCL_TIME
], child
->addr
))
494 child
->cg
.prop
.fract
= 0.0;
497 child
->cg
.prop
.self
= child
->hist
.time
* child
->cg
.prop
.fract
;
498 print_time
+= child
->cg
.prop
.self
;
500 printf ("[prop_flags] ");
502 printf (" ends up with printflag %d and prop-fract %f\n",
503 child
->cg
.print_flag
, child
->cg
.prop
.fract
);
504 printf ("[prop_flags] time %f propself %f print_time %f\n",
505 child
->hist
.time
, child
->cg
.prop
.self
, print_time
));
511 * Compare by decreasing propagated time. If times are equal, but one
512 * is a cycle header, say that's first (e.g. less, i.e. -1). If one's
513 * name doesn't have an underscore and the other does, say that one is
514 * first. All else being equal, compare by names.
517 DEFUN (cmp_total
, (lp
, rp
), const PTR lp AND
const PTR rp
)
519 const Sym
*left
= *(const Sym
**) lp
;
520 const Sym
*right
= *(const Sym
**) rp
;
523 diff
= (left
->cg
.prop
.self
+ left
->cg
.prop
.child
)
524 - (right
->cg
.prop
.self
+ right
->cg
.prop
.child
);
533 if (!left
->name
&& left
->cg
.cyc
.num
!= 0)
537 if (!right
->name
&& right
->cg
.cyc
.num
!= 0)
549 if (left
->name
[0] != '_' && right
->name
[0] == '_')
553 if (left
->name
[0] == '_' && right
->name
[0] != '_')
557 if (left
->ncalls
> right
->ncalls
)
561 if (left
->ncalls
< right
->ncalls
)
565 return strcmp (left
->name
, right
->name
);
570 * Topologically sort the graph (collapsing cycles), and propagates
571 * time bottom up and flags top down.
574 DEFUN_VOID (cg_assemble
)
576 Sym
*parent
, **time_sorted_syms
, **top_sorted_syms
;
581 * initialize various things:
582 * zero out child times.
583 * count self-recursive calls.
584 * indicate that nothing is on cycles.
586 for (parent
= symtab
.base
; parent
< symtab
.limit
; parent
++)
588 parent
->cg
.child_time
= 0.0;
589 arc
= arc_lookup (parent
, parent
);
590 if (arc
&& parent
== arc
->child
)
592 parent
->ncalls
-= arc
->count
;
593 parent
->cg
.self_calls
= arc
->count
;
597 parent
->cg
.self_calls
= 0;
599 parent
->cg
.prop
.fract
= 0.0;
600 parent
->cg
.prop
.self
= 0.0;
601 parent
->cg
.prop
.child
= 0.0;
602 parent
->cg
.print_flag
= FALSE
;
603 parent
->cg
.top_order
= DFN_NAN
;
604 parent
->cg
.cyc
.num
= 0;
605 parent
->cg
.cyc
.head
= parent
;
606 parent
->cg
.cyc
.next
= 0;
607 if (ignore_direct_calls
)
609 find_call (parent
, parent
->addr
, (parent
+ 1)->addr
);
613 * Topologically order things. If any node is unnumbered, number
614 * it and any of its descendents.
616 for (parent
= symtab
.base
; parent
< symtab
.limit
; parent
++)
618 if (parent
->cg
.top_order
== DFN_NAN
)
624 /* link together nodes on the same cycle: */
627 /* sort the symbol table in reverse topological order: */
628 top_sorted_syms
= (Sym
**) xmalloc (symtab
.len
* sizeof (Sym
*));
629 for (index
= 0; index
< symtab
.len
; ++index
)
631 top_sorted_syms
[index
] = &symtab
.base
[index
];
633 qsort (top_sorted_syms
, symtab
.len
, sizeof (Sym
*), cmp_topo
);
635 printf ("[cg_assemble] topological sort listing\n");
636 for (index
= 0; index
< symtab
.len
; ++index
)
638 printf ("[cg_assemble] ");
639 printf ("%d:", top_sorted_syms
[index
]->cg
.top_order
);
640 print_name (top_sorted_syms
[index
]);
645 * Starting from the topological top, propagate print flags to
646 * children. also, calculate propagation fractions. this happens
647 * before time propagation since time propagation uses the
650 propagate_flags (top_sorted_syms
);
653 * Starting from the topological bottom, propogate children times
657 for (index
= 0; index
< symtab
.len
; ++index
)
659 propagate_time (top_sorted_syms
[index
]);
662 free (top_sorted_syms
);
665 * Now, sort by CG.PROP.SELF + CG.PROP.CHILD. Sorting both the regular
666 * function names and cycle headers.
668 time_sorted_syms
= (Sym
**) xmalloc ((symtab
.len
+ num_cycles
) * sizeof (Sym
*));
669 for (index
= 0; index
< symtab
.len
; index
++)
671 time_sorted_syms
[index
] = &symtab
.base
[index
];
673 for (index
= 1; index
<= num_cycles
; index
++)
675 time_sorted_syms
[symtab
.len
+ index
- 1] = &cycle_header
[index
];
677 qsort (time_sorted_syms
, symtab
.len
+ num_cycles
, sizeof (Sym
*),
679 for (index
= 0; index
< symtab
.len
+ num_cycles
; index
++)
681 time_sorted_syms
[index
]->cg
.index
= index
+ 1;
683 return time_sorted_syms
;
This page took 0.043404 seconds and 4 git commands to generate.