xref: /freebsd/usr.bin/gprof/arcs.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
32e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
33b34f7debSPhilippe Charnier #include <err.h>
349b50d902SRodney W. Grimes #include "gprof.h"
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifdef DEBUG
379b50d902SRodney W. Grimes int visited;
389b50d902SRodney W. Grimes int viable;
399b50d902SRodney W. Grimes int newcycle;
409b50d902SRodney W. Grimes int oldcycle;
410fb7a0beSGarrett Wollman #endif /* DEBUG */
429b50d902SRodney W. Grimes 
43af2637dbSPhilippe Charnier int topcmp(const void *, const void *);
44af2637dbSPhilippe Charnier 
459b50d902SRodney W. Grimes     /*
469b50d902SRodney W. Grimes      *	add (or just increment) an arc
479b50d902SRodney W. Grimes      */
4897fa9b77SPhilippe Charnier void
addarc(nltype * parentp,nltype * childp,long count)49af2637dbSPhilippe Charnier addarc(nltype *parentp, nltype *childp, long count)
509b50d902SRodney W. Grimes {
519b50d902SRodney W. Grimes     arctype		*arcp;
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #   ifdef DEBUG
549b50d902SRodney W. Grimes 	if ( debug & TALLYDEBUG ) {
558d57b8d3SBruce Evans 	    printf( "[addarc] %ld arcs from %s to %s\n" ,
569b50d902SRodney W. Grimes 		    count , parentp -> name , childp -> name );
579b50d902SRodney W. Grimes 	}
580fb7a0beSGarrett Wollman #   endif /* DEBUG */
599b50d902SRodney W. Grimes     arcp = arclookup( parentp , childp );
609b50d902SRodney W. Grimes     if ( arcp != 0 ) {
619b50d902SRodney W. Grimes 	    /*
629b50d902SRodney W. Grimes 	     *	a hit:  just increment the count.
639b50d902SRodney W. Grimes 	     */
649b50d902SRodney W. Grimes #	ifdef DEBUG
659b50d902SRodney W. Grimes 	    if ( debug & TALLYDEBUG ) {
668d57b8d3SBruce Evans 		printf( "[tally] hit %ld += %ld\n" ,
679b50d902SRodney W. Grimes 			arcp -> arc_count , count );
689b50d902SRodney W. Grimes 	    }
690fb7a0beSGarrett Wollman #	endif /* DEBUG */
709b50d902SRodney W. Grimes 	arcp -> arc_count += count;
719b50d902SRodney W. Grimes 	return;
729b50d902SRodney W. Grimes     }
739b50d902SRodney W. Grimes     arcp = (arctype *)calloc( 1 , sizeof *arcp );
7497fa9b77SPhilippe Charnier     if (arcp == NULL)
7597fa9b77SPhilippe Charnier 	errx( 1 , "malloc failed" );
769b50d902SRodney W. Grimes     arcp -> arc_parentp = parentp;
779b50d902SRodney W. Grimes     arcp -> arc_childp = childp;
789b50d902SRodney W. Grimes     arcp -> arc_count = count;
799b50d902SRodney W. Grimes 	/*
809b50d902SRodney W. Grimes 	 *	prepend this child to the children of this parent
819b50d902SRodney W. Grimes 	 */
829b50d902SRodney W. Grimes     arcp -> arc_childlist = parentp -> children;
839b50d902SRodney W. Grimes     parentp -> children = arcp;
849b50d902SRodney W. Grimes 	/*
859b50d902SRodney W. Grimes 	 *	prepend this parent to the parents of this child
869b50d902SRodney W. Grimes 	 */
879b50d902SRodney W. Grimes     arcp -> arc_parentlist = childp -> parents;
889b50d902SRodney W. Grimes     childp -> parents = arcp;
899b50d902SRodney W. Grimes }
909b50d902SRodney W. Grimes 
919b50d902SRodney W. Grimes     /*
929b50d902SRodney W. Grimes      *	the code below topologically sorts the graph (collapsing cycles),
939b50d902SRodney W. Grimes      *	and propagates time bottom up and flags top down.
949b50d902SRodney W. Grimes      */
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes     /*
979b50d902SRodney W. Grimes      *	the topologically sorted name list pointers
989b50d902SRodney W. Grimes      */
999b50d902SRodney W. Grimes nltype	**topsortnlp;
1009b50d902SRodney W. Grimes 
10197fa9b77SPhilippe Charnier int
topcmp(const void * v1,const void * v2)102af2637dbSPhilippe Charnier topcmp(const void *v1, const void *v2)
1039b50d902SRodney W. Grimes {
104af2637dbSPhilippe Charnier     const nltype **npp1 = (const nltype **)v1;
105af2637dbSPhilippe Charnier     const nltype **npp2 = (const nltype **)v2;
106af2637dbSPhilippe Charnier 
1079b50d902SRodney W. Grimes     return (*npp1) -> toporder - (*npp2) -> toporder;
1089b50d902SRodney W. Grimes }
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes nltype **
doarcs(void)111af2637dbSPhilippe Charnier doarcs(void)
1129b50d902SRodney W. Grimes {
1139b50d902SRodney W. Grimes     nltype	*parentp, **timesortnlp;
1149b50d902SRodney W. Grimes     arctype	*arcp;
1159b50d902SRodney W. Grimes     long	index;
1169b50d902SRodney W. Grimes     long	pass;
1179b50d902SRodney W. Grimes 
1189b50d902SRodney W. Grimes 	/*
1199b50d902SRodney W. Grimes 	 *	initialize various things:
1209b50d902SRodney W. Grimes 	 *	    zero out child times.
1219b50d902SRodney W. Grimes 	 *	    count self-recursive calls.
1229b50d902SRodney W. Grimes 	 *	    indicate that nothing is on cycles.
1239b50d902SRodney W. Grimes 	 */
1249b50d902SRodney W. Grimes     for ( parentp = nl ; parentp < npe ; parentp++ ) {
1259b50d902SRodney W. Grimes 	parentp -> childtime = 0.0;
1269b50d902SRodney W. Grimes 	arcp = arclookup( parentp , parentp );
1279b50d902SRodney W. Grimes 	if ( arcp != 0 ) {
1289b50d902SRodney W. Grimes 	    parentp -> ncall -= arcp -> arc_count;
1299b50d902SRodney W. Grimes 	    parentp -> selfcalls = arcp -> arc_count;
1309b50d902SRodney W. Grimes 	} else {
1319b50d902SRodney W. Grimes 	    parentp -> selfcalls = 0;
1329b50d902SRodney W. Grimes 	}
1339b50d902SRodney W. Grimes 	parentp -> npropcall = parentp -> ncall;
1349b50d902SRodney W. Grimes 	parentp -> propfraction = 0.0;
1359b50d902SRodney W. Grimes 	parentp -> propself = 0.0;
1369b50d902SRodney W. Grimes 	parentp -> propchild = 0.0;
1379b50d902SRodney W. Grimes 	parentp -> printflag = FALSE;
1389b50d902SRodney W. Grimes 	parentp -> toporder = DFN_NAN;
1399b50d902SRodney W. Grimes 	parentp -> cycleno = 0;
1409b50d902SRodney W. Grimes 	parentp -> cyclehead = parentp;
1419b50d902SRodney W. Grimes 	parentp -> cnext = 0;
1429b50d902SRodney W. Grimes     }
1439b50d902SRodney W. Grimes     for ( pass = 1 ; ; pass++ ) {
1449b50d902SRodney W. Grimes 	    /*
1459b50d902SRodney W. Grimes 	     *	topologically order things
1469b50d902SRodney W. Grimes 	     *	if any node is unnumbered,
1479b50d902SRodney W. Grimes 	     *	    number it and any of its descendents.
1489b50d902SRodney W. Grimes 	     */
1499b50d902SRodney W. Grimes 	for ( dfn_init() , parentp = nl ; parentp < npe ; parentp++ ) {
1509b50d902SRodney W. Grimes 	    if ( parentp -> toporder == DFN_NAN ) {
1519b50d902SRodney W. Grimes 		dfn( parentp );
1529b50d902SRodney W. Grimes 	    }
1539b50d902SRodney W. Grimes 	}
1549b50d902SRodney W. Grimes 	    /*
1559b50d902SRodney W. Grimes 	     *	link together nodes on the same cycle
1569b50d902SRodney W. Grimes 	     */
1579b50d902SRodney W. Grimes 	cyclelink();
1589b50d902SRodney W. Grimes 	    /*
1599b50d902SRodney W. Grimes 	     *	if no cycles to break up, proceed
1609b50d902SRodney W. Grimes 	     */
1619b50d902SRodney W. Grimes 	if ( ! Cflag )
1629b50d902SRodney W. Grimes 	    break;
1639b50d902SRodney W. Grimes 	    /*
1649b50d902SRodney W. Grimes 	     *	analyze cycles to determine breakup
1659b50d902SRodney W. Grimes 	     */
1669b50d902SRodney W. Grimes #	ifdef DEBUG
1679b50d902SRodney W. Grimes 	    if ( debug & BREAKCYCLE ) {
1688d57b8d3SBruce Evans 		printf("[doarcs] pass %ld, cycle(s) %d\n" , pass , ncycle );
1699b50d902SRodney W. Grimes 	    }
1700fb7a0beSGarrett Wollman #	endif /* DEBUG */
1719b50d902SRodney W. Grimes 	if ( pass == 1 ) {
1729b50d902SRodney W. Grimes 	    printf( "\n\n%s %s\n%s %d:\n" ,
1739b50d902SRodney W. Grimes 		"The following arcs were deleted" ,
1749b50d902SRodney W. Grimes 		"from the propagation calculation" ,
1759b50d902SRodney W. Grimes 		"to reduce the maximum cycle size to", cyclethreshold );
1769b50d902SRodney W. Grimes 	}
1779b50d902SRodney W. Grimes 	if ( cycleanalyze() )
1789b50d902SRodney W. Grimes 	    break;
1799b50d902SRodney W. Grimes 	free ( cyclenl );
1809b50d902SRodney W. Grimes 	ncycle = 0;
1819b50d902SRodney W. Grimes 	for ( parentp = nl ; parentp < npe ; parentp++ ) {
1829b50d902SRodney W. Grimes 	    parentp -> toporder = DFN_NAN;
1839b50d902SRodney W. Grimes 	    parentp -> cycleno = 0;
1849b50d902SRodney W. Grimes 	    parentp -> cyclehead = parentp;
1859b50d902SRodney W. Grimes 	    parentp -> cnext = 0;
1869b50d902SRodney W. Grimes 	}
1879b50d902SRodney W. Grimes     }
1889b50d902SRodney W. Grimes     if ( pass > 1 ) {
1899b50d902SRodney W. Grimes 	printf( "\f\n" );
1909b50d902SRodney W. Grimes     } else {
1919b50d902SRodney W. Grimes 	printf( "\tNone\n\n" );
1929b50d902SRodney W. Grimes     }
1939b50d902SRodney W. Grimes 	/*
1949b50d902SRodney W. Grimes 	 *	Sort the symbol table in reverse topological order
1959b50d902SRodney W. Grimes 	 */
1969b50d902SRodney W. Grimes     topsortnlp = (nltype **) calloc( nname , sizeof(nltype *) );
19797fa9b77SPhilippe Charnier     if ( topsortnlp == (nltype **) 0 )
19897fa9b77SPhilippe Charnier 	errx( 1 , "[doarcs] ran out of memory for topo sorting" );
1999b50d902SRodney W. Grimes     for ( index = 0 ; index < nname ; index += 1 ) {
2009b50d902SRodney W. Grimes 	topsortnlp[ index ] = &nl[ index ];
2019b50d902SRodney W. Grimes     }
2029b50d902SRodney W. Grimes     qsort( topsortnlp , nname , sizeof(nltype *) , topcmp );
2039b50d902SRodney W. Grimes #   ifdef DEBUG
2049b50d902SRodney W. Grimes 	if ( debug & DFNDEBUG ) {
2059b50d902SRodney W. Grimes 	    printf( "[doarcs] topological sort listing\n" );
2069b50d902SRodney W. Grimes 	    for ( index = 0 ; index < nname ; index += 1 ) {
2079b50d902SRodney W. Grimes 		printf( "[doarcs] " );
2089b50d902SRodney W. Grimes 		printf( "%d:" , topsortnlp[ index ] -> toporder );
2099b50d902SRodney W. Grimes 		printname( topsortnlp[ index ] );
2109b50d902SRodney W. Grimes 		printf( "\n" );
2119b50d902SRodney W. Grimes 	    }
2129b50d902SRodney W. Grimes 	}
2130fb7a0beSGarrett Wollman #   endif /* DEBUG */
2149b50d902SRodney W. Grimes 	/*
2159b50d902SRodney W. Grimes 	 *	starting from the topological top,
2169b50d902SRodney W. Grimes 	 *	propagate print flags to children.
2179b50d902SRodney W. Grimes 	 *	also, calculate propagation fractions.
2189b50d902SRodney W. Grimes 	 *	this happens before time propagation
2199b50d902SRodney W. Grimes 	 *	since time propagation uses the fractions.
2209b50d902SRodney W. Grimes 	 */
2219b50d902SRodney W. Grimes     doflags();
2229b50d902SRodney W. Grimes 	/*
2239b50d902SRodney W. Grimes 	 *	starting from the topological bottom,
22497fa9b77SPhilippe Charnier 	 *	propagate children times up to parents.
2259b50d902SRodney W. Grimes 	 */
2269b50d902SRodney W. Grimes     dotime();
2279b50d902SRodney W. Grimes 	/*
2289b50d902SRodney W. Grimes 	 *	Now, sort by propself + propchild.
2299b50d902SRodney W. Grimes 	 *	sorting both the regular function names
2309b50d902SRodney W. Grimes 	 *	and cycle headers.
2319b50d902SRodney W. Grimes 	 */
2329b50d902SRodney W. Grimes     timesortnlp = (nltype **) calloc( nname + ncycle , sizeof(nltype *) );
23397fa9b77SPhilippe Charnier     if ( timesortnlp == (nltype **) 0 )
23497fa9b77SPhilippe Charnier 	errx( 1 , "ran out of memory for sorting" );
2359b50d902SRodney W. Grimes     for ( index = 0 ; index < nname ; index++ ) {
2369b50d902SRodney W. Grimes 	timesortnlp[index] = &nl[index];
2379b50d902SRodney W. Grimes     }
2389b50d902SRodney W. Grimes     for ( index = 1 ; index <= ncycle ; index++ ) {
2399b50d902SRodney W. Grimes 	timesortnlp[nname+index-1] = &cyclenl[index];
2409b50d902SRodney W. Grimes     }
2419b50d902SRodney W. Grimes     qsort( timesortnlp , nname + ncycle , sizeof(nltype *) , totalcmp );
2429b50d902SRodney W. Grimes     for ( index = 0 ; index < nname + ncycle ; index++ ) {
2439b50d902SRodney W. Grimes 	timesortnlp[ index ] -> index = index + 1;
2449b50d902SRodney W. Grimes     }
2459b50d902SRodney W. Grimes     return( timesortnlp );
2469b50d902SRodney W. Grimes }
2479b50d902SRodney W. Grimes 
24897fa9b77SPhilippe Charnier void
dotime(void)249af2637dbSPhilippe Charnier dotime(void)
2509b50d902SRodney W. Grimes {
2519b50d902SRodney W. Grimes     int	index;
2529b50d902SRodney W. Grimes 
2539b50d902SRodney W. Grimes     cycletime();
2549b50d902SRodney W. Grimes     for ( index = 0 ; index < nname ; index += 1 ) {
2559b50d902SRodney W. Grimes 	timepropagate( topsortnlp[ index ] );
2569b50d902SRodney W. Grimes     }
2579b50d902SRodney W. Grimes }
2589b50d902SRodney W. Grimes 
25997fa9b77SPhilippe Charnier void
timepropagate(nltype * parentp)260af2637dbSPhilippe Charnier timepropagate(nltype *parentp)
2619b50d902SRodney W. Grimes {
2629b50d902SRodney W. Grimes     arctype	*arcp;
2639b50d902SRodney W. Grimes     nltype	*childp;
2649b50d902SRodney W. Grimes     double	share;
2659b50d902SRodney W. Grimes     double	propshare;
2669b50d902SRodney W. Grimes 
2679b50d902SRodney W. Grimes     if ( parentp -> propfraction == 0.0 ) {
2689b50d902SRodney W. Grimes 	return;
2699b50d902SRodney W. Grimes     }
2709b50d902SRodney W. Grimes 	/*
2719b50d902SRodney W. Grimes 	 *	gather time from children of this parent.
2729b50d902SRodney W. Grimes 	 */
2739b50d902SRodney W. Grimes     for ( arcp = parentp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
2749b50d902SRodney W. Grimes 	childp = arcp -> arc_childp;
2759b50d902SRodney W. Grimes 	if ( arcp -> arc_flags & DEADARC ) {
2769b50d902SRodney W. Grimes 	    continue;
2779b50d902SRodney W. Grimes 	}
2789b50d902SRodney W. Grimes 	if ( arcp -> arc_count == 0 ) {
2799b50d902SRodney W. Grimes 	    continue;
2809b50d902SRodney W. Grimes 	}
2819b50d902SRodney W. Grimes 	if ( childp == parentp ) {
2829b50d902SRodney W. Grimes 	    continue;
2839b50d902SRodney W. Grimes 	}
2849b50d902SRodney W. Grimes 	if ( childp -> propfraction == 0.0 ) {
2859b50d902SRodney W. Grimes 	    continue;
2869b50d902SRodney W. Grimes 	}
2879b50d902SRodney W. Grimes 	if ( childp -> cyclehead != childp ) {
2889b50d902SRodney W. Grimes 	    if ( parentp -> cycleno == childp -> cycleno ) {
2899b50d902SRodney W. Grimes 		continue;
2909b50d902SRodney W. Grimes 	    }
2919b50d902SRodney W. Grimes 	    if ( parentp -> toporder <= childp -> toporder ) {
2929b50d902SRodney W. Grimes 		fprintf( stderr , "[propagate] toporder botches\n" );
2939b50d902SRodney W. Grimes 	    }
2949b50d902SRodney W. Grimes 	    childp = childp -> cyclehead;
2959b50d902SRodney W. Grimes 	} else {
2969b50d902SRodney W. Grimes 	    if ( parentp -> toporder <= childp -> toporder ) {
2979b50d902SRodney W. Grimes 		fprintf( stderr , "[propagate] toporder botches\n" );
2989b50d902SRodney W. Grimes 		continue;
2999b50d902SRodney W. Grimes 	    }
3009b50d902SRodney W. Grimes 	}
3019b50d902SRodney W. Grimes 	if ( childp -> npropcall == 0 ) {
3029b50d902SRodney W. Grimes 	    continue;
3039b50d902SRodney W. Grimes 	}
3049b50d902SRodney W. Grimes 	    /*
3059b50d902SRodney W. Grimes 	     *	distribute time for this arc
3069b50d902SRodney W. Grimes 	     */
3079b50d902SRodney W. Grimes 	arcp -> arc_time = childp -> time
3089b50d902SRodney W. Grimes 			        * ( ( (double) arcp -> arc_count ) /
3099b50d902SRodney W. Grimes 				    ( (double) childp -> npropcall ) );
3109b50d902SRodney W. Grimes 	arcp -> arc_childtime = childp -> childtime
3119b50d902SRodney W. Grimes 			        * ( ( (double) arcp -> arc_count ) /
3129b50d902SRodney W. Grimes 				    ( (double) childp -> npropcall ) );
3139b50d902SRodney W. Grimes 	share = arcp -> arc_time + arcp -> arc_childtime;
3149b50d902SRodney W. Grimes 	parentp -> childtime += share;
3159b50d902SRodney W. Grimes 	    /*
3169b50d902SRodney W. Grimes 	     *	( 1 - propfraction ) gets lost along the way
3179b50d902SRodney W. Grimes 	     */
3189b50d902SRodney W. Grimes 	propshare = parentp -> propfraction * share;
3199b50d902SRodney W. Grimes 	    /*
3209b50d902SRodney W. Grimes 	     *	fix things for printing
3219b50d902SRodney W. Grimes 	     */
3229b50d902SRodney W. Grimes 	parentp -> propchild += propshare;
3239b50d902SRodney W. Grimes 	arcp -> arc_time *= parentp -> propfraction;
3249b50d902SRodney W. Grimes 	arcp -> arc_childtime *= parentp -> propfraction;
3259b50d902SRodney W. Grimes 	    /*
3269b50d902SRodney W. Grimes 	     *	add this share to the parent's cycle header, if any.
3279b50d902SRodney W. Grimes 	     */
3289b50d902SRodney W. Grimes 	if ( parentp -> cyclehead != parentp ) {
3299b50d902SRodney W. Grimes 	    parentp -> cyclehead -> childtime += share;
3309b50d902SRodney W. Grimes 	    parentp -> cyclehead -> propchild += propshare;
3319b50d902SRodney W. Grimes 	}
3329b50d902SRodney W. Grimes #	ifdef DEBUG
3339b50d902SRodney W. Grimes 	    if ( debug & PROPDEBUG ) {
3349b50d902SRodney W. Grimes 		printf( "[dotime] child \t" );
3359b50d902SRodney W. Grimes 		printname( childp );
3368d57b8d3SBruce Evans 		printf( " with %f %f %ld/%ld\n" ,
3379b50d902SRodney W. Grimes 			childp -> time , childp -> childtime ,
3389b50d902SRodney W. Grimes 			arcp -> arc_count , childp -> npropcall );
3399b50d902SRodney W. Grimes 		printf( "[dotime] parent\t" );
3409b50d902SRodney W. Grimes 		printname( parentp );
3419b50d902SRodney W. Grimes 		printf( "\n[dotime] share %f\n" , share );
3429b50d902SRodney W. Grimes 	    }
3430fb7a0beSGarrett Wollman #	endif /* DEBUG */
3449b50d902SRodney W. Grimes     }
3459b50d902SRodney W. Grimes }
3469b50d902SRodney W. Grimes 
34797fa9b77SPhilippe Charnier void
cyclelink(void)348af2637dbSPhilippe Charnier cyclelink(void)
3499b50d902SRodney W. Grimes {
3509b50d902SRodney W. Grimes     register nltype	*nlp;
3519b50d902SRodney W. Grimes     register nltype	*cyclenlp;
3529b50d902SRodney W. Grimes     int			cycle;
3539b50d902SRodney W. Grimes     nltype		*memberp;
3549b50d902SRodney W. Grimes     arctype		*arcp;
3559b50d902SRodney W. Grimes 
3569b50d902SRodney W. Grimes 	/*
35797fa9b77SPhilippe Charnier 	 *	Count the number of cycles, and initialize the cycle lists
3589b50d902SRodney W. Grimes 	 */
3599b50d902SRodney W. Grimes     ncycle = 0;
3609b50d902SRodney W. Grimes     for ( nlp = nl ; nlp < npe ; nlp++ ) {
3619b50d902SRodney W. Grimes 	    /*
3629b50d902SRodney W. Grimes 	     *	this is how you find unattached cycles
3639b50d902SRodney W. Grimes 	     */
3649b50d902SRodney W. Grimes 	if ( nlp -> cyclehead == nlp && nlp -> cnext != 0 ) {
3659b50d902SRodney W. Grimes 	    ncycle += 1;
3669b50d902SRodney W. Grimes 	}
3679b50d902SRodney W. Grimes     }
3689b50d902SRodney W. Grimes 	/*
3699b50d902SRodney W. Grimes 	 *	cyclenl is indexed by cycle number:
3709b50d902SRodney W. Grimes 	 *	i.e. it is origin 1, not origin 0.
3719b50d902SRodney W. Grimes 	 */
3729b50d902SRodney W. Grimes     cyclenl = (nltype *) calloc( ncycle + 1 , sizeof( nltype ) );
37326d0a722SMarcelo Araujo     if ( cyclenl == NULL )
3747853817dSDimitry Andric 	errx( 1 , "no room for %zu bytes of cycle headers" ,
375297b9492SPhilippe Charnier 		   ( ncycle + 1 ) * sizeof( nltype ) );
3769b50d902SRodney W. Grimes 	/*
3779b50d902SRodney W. Grimes 	 *	now link cycles to true cycleheads,
3789b50d902SRodney W. Grimes 	 *	number them, accumulate the data for the cycle
3799b50d902SRodney W. Grimes 	 */
3809b50d902SRodney W. Grimes     cycle = 0;
3819b50d902SRodney W. Grimes     for ( nlp = nl ; nlp < npe ; nlp++ ) {
3829b50d902SRodney W. Grimes 	if ( !( nlp -> cyclehead == nlp && nlp -> cnext != 0 ) ) {
3839b50d902SRodney W. Grimes 	    continue;
3849b50d902SRodney W. Grimes 	}
3859b50d902SRodney W. Grimes 	cycle += 1;
3869b50d902SRodney W. Grimes 	cyclenlp = &cyclenl[cycle];
3879b50d902SRodney W. Grimes         cyclenlp -> name = 0;		/* the name */
3889b50d902SRodney W. Grimes         cyclenlp -> value = 0;		/* the pc entry point */
3899b50d902SRodney W. Grimes         cyclenlp -> time = 0.0;		/* ticks in this routine */
3909b50d902SRodney W. Grimes         cyclenlp -> childtime = 0.0;	/* cumulative ticks in children */
3919b50d902SRodney W. Grimes 	cyclenlp -> ncall = 0;		/* how many times called */
3929b50d902SRodney W. Grimes 	cyclenlp -> selfcalls = 0;	/* how many calls to self */
3939b50d902SRodney W. Grimes 	cyclenlp -> propfraction = 0.0;	/* what % of time propagates */
3949b50d902SRodney W. Grimes 	cyclenlp -> propself = 0.0;	/* how much self time propagates */
3959b50d902SRodney W. Grimes 	cyclenlp -> propchild = 0.0;	/* how much child time propagates */
3969b50d902SRodney W. Grimes 	cyclenlp -> printflag = TRUE;	/* should this be printed? */
3979b50d902SRodney W. Grimes 	cyclenlp -> index = 0;		/* index in the graph list */
3989b50d902SRodney W. Grimes 	cyclenlp -> toporder = DFN_NAN;	/* graph call chain top-sort order */
3999b50d902SRodney W. Grimes 	cyclenlp -> cycleno = cycle;	/* internal number of cycle on */
4009b50d902SRodney W. Grimes 	cyclenlp -> cyclehead = cyclenlp;	/* pointer to head of cycle */
4019b50d902SRodney W. Grimes 	cyclenlp -> cnext = nlp;	/* pointer to next member of cycle */
4029b50d902SRodney W. Grimes 	cyclenlp -> parents = 0;	/* list of caller arcs */
4039b50d902SRodney W. Grimes 	cyclenlp -> children = 0;	/* list of callee arcs */
4049b50d902SRodney W. Grimes #	ifdef DEBUG
4059b50d902SRodney W. Grimes 	    if ( debug & CYCLEDEBUG ) {
4069b50d902SRodney W. Grimes 		printf( "[cyclelink] " );
4079b50d902SRodney W. Grimes 		printname( nlp );
4089b50d902SRodney W. Grimes 		printf( " is the head of cycle %d\n" , cycle );
4099b50d902SRodney W. Grimes 	    }
4100fb7a0beSGarrett Wollman #	endif /* DEBUG */
4119b50d902SRodney W. Grimes 	    /*
4129b50d902SRodney W. Grimes 	     *	link members to cycle header
4139b50d902SRodney W. Grimes 	     */
4149b50d902SRodney W. Grimes 	for ( memberp = nlp ; memberp ; memberp = memberp -> cnext ) {
4159b50d902SRodney W. Grimes 	    memberp -> cycleno = cycle;
4169b50d902SRodney W. Grimes 	    memberp -> cyclehead = cyclenlp;
4179b50d902SRodney W. Grimes 	}
4189b50d902SRodney W. Grimes 	    /*
4199b50d902SRodney W. Grimes 	     *	count calls from outside the cycle
4209b50d902SRodney W. Grimes 	     *	and those among cycle members
4219b50d902SRodney W. Grimes 	     */
4229b50d902SRodney W. Grimes 	for ( memberp = nlp ; memberp ; memberp = memberp -> cnext ) {
4239b50d902SRodney W. Grimes 	    for ( arcp=memberp->parents ; arcp ; arcp=arcp->arc_parentlist ) {
4249b50d902SRodney W. Grimes 		if ( arcp -> arc_parentp == memberp ) {
4259b50d902SRodney W. Grimes 		    continue;
4269b50d902SRodney W. Grimes 		}
4279b50d902SRodney W. Grimes 		if ( arcp -> arc_parentp -> cycleno == cycle ) {
4289b50d902SRodney W. Grimes 		    cyclenlp -> selfcalls += arcp -> arc_count;
4299b50d902SRodney W. Grimes 		} else {
4309b50d902SRodney W. Grimes 		    cyclenlp -> npropcall += arcp -> arc_count;
4319b50d902SRodney W. Grimes 		}
4329b50d902SRodney W. Grimes 	    }
4339b50d902SRodney W. Grimes 	}
4349b50d902SRodney W. Grimes     }
4359b50d902SRodney W. Grimes }
4369b50d902SRodney W. Grimes 
4379b50d902SRodney W. Grimes     /*
4389b50d902SRodney W. Grimes      *	analyze cycles to determine breakup
4399b50d902SRodney W. Grimes      */
44097fa9b77SPhilippe Charnier bool
cycleanalyze(void)441af2637dbSPhilippe Charnier cycleanalyze(void)
4429b50d902SRodney W. Grimes {
4439b50d902SRodney W. Grimes     arctype	**cyclestack;
4449b50d902SRodney W. Grimes     arctype	**stkp;
4459b50d902SRodney W. Grimes     arctype	**arcpp;
4469b50d902SRodney W. Grimes     arctype	**endlist;
4479b50d902SRodney W. Grimes     arctype	*arcp;
4489b50d902SRodney W. Grimes     nltype	*nlp;
4499b50d902SRodney W. Grimes     cltype	*clp;
4509b50d902SRodney W. Grimes     bool	ret;
4519b50d902SRodney W. Grimes     bool	done;
4529b50d902SRodney W. Grimes     int		size;
4539b50d902SRodney W. Grimes     int		cycleno;
4549b50d902SRodney W. Grimes 
4559b50d902SRodney W. Grimes 	/*
4569b50d902SRodney W. Grimes 	 *	calculate the size of the cycle, and find nodes that
4579b50d902SRodney W. Grimes 	 *	exit the cycle as they are desirable targets to cut
4589b50d902SRodney W. Grimes 	 *	some of their parents
4599b50d902SRodney W. Grimes 	 */
4609b50d902SRodney W. Grimes     for ( done = TRUE , cycleno = 1 ; cycleno <= ncycle ; cycleno++ ) {
4619b50d902SRodney W. Grimes 	size = 0;
4629b50d902SRodney W. Grimes 	for (nlp = cyclenl[ cycleno ] . cnext; nlp; nlp = nlp -> cnext) {
4639b50d902SRodney W. Grimes 	    size += 1;
4649b50d902SRodney W. Grimes 	    nlp -> parentcnt = 0;
4659b50d902SRodney W. Grimes 	    nlp -> flags &= ~HASCYCLEXIT;
4669b50d902SRodney W. Grimes 	    for ( arcp = nlp -> parents; arcp; arcp = arcp -> arc_parentlist ) {
4679b50d902SRodney W. Grimes 		nlp -> parentcnt += 1;
4689b50d902SRodney W. Grimes 		if ( arcp -> arc_parentp -> cycleno != cycleno )
4699b50d902SRodney W. Grimes 		    nlp -> flags |= HASCYCLEXIT;
4709b50d902SRodney W. Grimes 	    }
4719b50d902SRodney W. Grimes 	}
4729b50d902SRodney W. Grimes 	if ( size <= cyclethreshold )
4739b50d902SRodney W. Grimes 	    continue;
4749b50d902SRodney W. Grimes 	done = FALSE;
4759b50d902SRodney W. Grimes         cyclestack = (arctype **) calloc( size + 1 , sizeof( arctype *) );
47626d0a722SMarcelo Araujo 	if ( cyclestack == NULL )
4777853817dSDimitry Andric 	    errx( 1, "no room for %zu bytes of cycle stack" ,
478297b9492SPhilippe Charnier 			   ( size + 1 ) * sizeof( arctype * ) );
4799b50d902SRodney W. Grimes #	ifdef DEBUG
4809b50d902SRodney W. Grimes 	    if ( debug & BREAKCYCLE ) {
4819b50d902SRodney W. Grimes 		printf( "[cycleanalyze] starting cycle %d of %d, size %d\n" ,
4829b50d902SRodney W. Grimes 		    cycleno , ncycle , size );
4839b50d902SRodney W. Grimes 	    }
4840fb7a0beSGarrett Wollman #	endif /* DEBUG */
4859b50d902SRodney W. Grimes 	for ( nlp = cyclenl[ cycleno ] . cnext ; nlp ; nlp = nlp -> cnext ) {
4869b50d902SRodney W. Grimes 	    stkp = &cyclestack[0];
4879b50d902SRodney W. Grimes 	    nlp -> flags |= CYCLEHEAD;
4889b50d902SRodney W. Grimes 	    ret = descend ( nlp , cyclestack , stkp );
4899b50d902SRodney W. Grimes 	    nlp -> flags &= ~CYCLEHEAD;
4909b50d902SRodney W. Grimes 	    if ( ret == FALSE )
4919b50d902SRodney W. Grimes 		break;
4929b50d902SRodney W. Grimes 	}
4939b50d902SRodney W. Grimes 	free( cyclestack );
4949b50d902SRodney W. Grimes 	if ( cyclecnt > 0 ) {
4959b50d902SRodney W. Grimes 	    compresslist();
4969b50d902SRodney W. Grimes 	    for ( clp = cyclehead ; clp ; ) {
4979b50d902SRodney W. Grimes 		endlist = &clp -> list[ clp -> size ];
4989b50d902SRodney W. Grimes 		for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ )
4999b50d902SRodney W. Grimes 		    (*arcpp) -> arc_cyclecnt--;
5009b50d902SRodney W. Grimes 		cyclecnt--;
5019b50d902SRodney W. Grimes 		clp = clp -> next;
5029b50d902SRodney W. Grimes 		free( clp );
5039b50d902SRodney W. Grimes 	    }
5049b50d902SRodney W. Grimes 	    cyclehead = 0;
5059b50d902SRodney W. Grimes 	}
5069b50d902SRodney W. Grimes     }
5079b50d902SRodney W. Grimes #   ifdef DEBUG
5089b50d902SRodney W. Grimes 	if ( debug & BREAKCYCLE ) {
5099b50d902SRodney W. Grimes 	    printf("%s visited %d, viable %d, newcycle %d, oldcycle %d\n",
5109b50d902SRodney W. Grimes 		"[doarcs]" , visited , viable , newcycle , oldcycle);
5119b50d902SRodney W. Grimes 	}
5120fb7a0beSGarrett Wollman #   endif /* DEBUG */
5139b50d902SRodney W. Grimes     return( done );
5149b50d902SRodney W. Grimes }
5159b50d902SRodney W. Grimes 
51697fa9b77SPhilippe Charnier bool
descend(nltype * node,arctype ** stkstart,arctype ** stkp)517af2637dbSPhilippe Charnier descend(nltype *node, arctype **stkstart, arctype **stkp)
5189b50d902SRodney W. Grimes {
5199b50d902SRodney W. Grimes     arctype	*arcp;
5209b50d902SRodney W. Grimes     bool	ret;
5219b50d902SRodney W. Grimes 
5229b50d902SRodney W. Grimes     for ( arcp = node -> children ; arcp ; arcp = arcp -> arc_childlist ) {
5239b50d902SRodney W. Grimes #	ifdef DEBUG
5249b50d902SRodney W. Grimes 	    visited++;
5250fb7a0beSGarrett Wollman #	endif /* DEBUG */
5269b50d902SRodney W. Grimes 	if ( arcp -> arc_childp -> cycleno != node -> cycleno
5279b50d902SRodney W. Grimes 	    || ( arcp -> arc_childp -> flags & VISITED )
5289b50d902SRodney W. Grimes 	    || ( arcp -> arc_flags & DEADARC ) )
5299b50d902SRodney W. Grimes 	    continue;
5309b50d902SRodney W. Grimes #	ifdef DEBUG
5319b50d902SRodney W. Grimes 	    viable++;
5320fb7a0beSGarrett Wollman #	endif /* DEBUG */
5339b50d902SRodney W. Grimes 	*stkp = arcp;
5349b50d902SRodney W. Grimes 	if ( arcp -> arc_childp -> flags & CYCLEHEAD ) {
5359b50d902SRodney W. Grimes 	    if ( addcycle( stkstart , stkp ) == FALSE )
5369b50d902SRodney W. Grimes 		return( FALSE );
5379b50d902SRodney W. Grimes 	    continue;
5389b50d902SRodney W. Grimes 	}
5399b50d902SRodney W. Grimes 	arcp -> arc_childp -> flags |= VISITED;
5409b50d902SRodney W. Grimes 	ret = descend( arcp -> arc_childp , stkstart , stkp + 1 );
5419b50d902SRodney W. Grimes 	arcp -> arc_childp -> flags &= ~VISITED;
5429b50d902SRodney W. Grimes 	if ( ret == FALSE )
5439b50d902SRodney W. Grimes 	    return( FALSE );
5449b50d902SRodney W. Grimes     }
54597fa9b77SPhilippe Charnier     return( TRUE );
5469b50d902SRodney W. Grimes }
5479b50d902SRodney W. Grimes 
54897fa9b77SPhilippe Charnier bool
addcycle(arctype ** stkstart,arctype ** stkend)549af2637dbSPhilippe Charnier addcycle(arctype **stkstart, arctype **stkend)
5509b50d902SRodney W. Grimes {
5519b50d902SRodney W. Grimes     arctype	**arcpp;
5529b50d902SRodney W. Grimes     arctype	**stkloc;
5539b50d902SRodney W. Grimes     arctype	**stkp;
5549b50d902SRodney W. Grimes     arctype	**endlist;
5559b50d902SRodney W. Grimes     arctype	*minarc;
5569b50d902SRodney W. Grimes     arctype	*arcp;
5579b50d902SRodney W. Grimes     cltype	*clp;
5589b50d902SRodney W. Grimes     int		size;
5599b50d902SRodney W. Grimes 
5609b50d902SRodney W. Grimes     size = stkend - stkstart + 1;
5619b50d902SRodney W. Grimes     if ( size <= 1 )
5629b50d902SRodney W. Grimes 	return( TRUE );
5639b50d902SRodney W. Grimes     for ( arcpp = stkstart , minarc = *arcpp ; arcpp <= stkend ; arcpp++ ) {
5649b50d902SRodney W. Grimes 	if ( *arcpp > minarc )
5659b50d902SRodney W. Grimes 	    continue;
5669b50d902SRodney W. Grimes 	minarc = *arcpp;
5679b50d902SRodney W. Grimes 	stkloc = arcpp;
5689b50d902SRodney W. Grimes     }
5699b50d902SRodney W. Grimes     for ( clp = cyclehead ; clp ; clp = clp -> next ) {
5709b50d902SRodney W. Grimes 	if ( clp -> size != size )
5719b50d902SRodney W. Grimes 	    continue;
5729b50d902SRodney W. Grimes 	stkp = stkloc;
5739b50d902SRodney W. Grimes 	endlist = &clp -> list[ size ];
5749b50d902SRodney W. Grimes 	for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ ) {
5759b50d902SRodney W. Grimes 	    if ( *stkp++ != *arcpp )
5769b50d902SRodney W. Grimes 		break;
5779b50d902SRodney W. Grimes 	    if ( stkp > stkend )
5789b50d902SRodney W. Grimes 		stkp = stkstart;
5799b50d902SRodney W. Grimes 	}
5809b50d902SRodney W. Grimes 	if ( arcpp == endlist ) {
5819b50d902SRodney W. Grimes #	    ifdef DEBUG
5829b50d902SRodney W. Grimes 		oldcycle++;
5830fb7a0beSGarrett Wollman #	    endif /* DEBUG */
5849b50d902SRodney W. Grimes 	    return( TRUE );
5859b50d902SRodney W. Grimes 	}
5869b50d902SRodney W. Grimes     }
5879b50d902SRodney W. Grimes     clp = (cltype *)
5889b50d902SRodney W. Grimes 	calloc( 1 , sizeof ( cltype ) + ( size - 1 ) * sizeof( arctype * ) );
58926d0a722SMarcelo Araujo     if ( clp == NULL ) {
5907853817dSDimitry Andric 	warnx( "no room for %zu bytes of subcycle storage" ,
591b34f7debSPhilippe Charnier 	    sizeof ( cltype ) + ( size - 1 ) * sizeof( arctype * ) );
5929b50d902SRodney W. Grimes 	return( FALSE );
5939b50d902SRodney W. Grimes     }
5949b50d902SRodney W. Grimes     stkp = stkloc;
5959b50d902SRodney W. Grimes     endlist = &clp -> list[ size ];
5969b50d902SRodney W. Grimes     for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ ) {
5979b50d902SRodney W. Grimes 	arcp = *arcpp = *stkp++;
5989b50d902SRodney W. Grimes 	if ( stkp > stkend )
5999b50d902SRodney W. Grimes 	    stkp = stkstart;
6009b50d902SRodney W. Grimes 	arcp -> arc_cyclecnt++;
6019b50d902SRodney W. Grimes 	if ( ( arcp -> arc_flags & ONLIST ) == 0 ) {
6029b50d902SRodney W. Grimes 	    arcp -> arc_flags |= ONLIST;
6039b50d902SRodney W. Grimes 	    arcp -> arc_next = archead;
6049b50d902SRodney W. Grimes 	    archead = arcp;
6059b50d902SRodney W. Grimes 	}
6069b50d902SRodney W. Grimes     }
6079b50d902SRodney W. Grimes     clp -> size = size;
6089b50d902SRodney W. Grimes     clp -> next = cyclehead;
6099b50d902SRodney W. Grimes     cyclehead = clp;
6109b50d902SRodney W. Grimes #   ifdef DEBUG
6119b50d902SRodney W. Grimes 	newcycle++;
6129b50d902SRodney W. Grimes 	if ( debug & SUBCYCLELIST ) {
6139b50d902SRodney W. Grimes 	    printsubcycle( clp );
6149b50d902SRodney W. Grimes 	}
6150fb7a0beSGarrett Wollman #   endif /* DEBUG */
6169b50d902SRodney W. Grimes     cyclecnt++;
6179b50d902SRodney W. Grimes     if ( cyclecnt >= CYCLEMAX )
6189b50d902SRodney W. Grimes 	return( FALSE );
6199b50d902SRodney W. Grimes     return( TRUE );
6209b50d902SRodney W. Grimes }
6219b50d902SRodney W. Grimes 
62297fa9b77SPhilippe Charnier void
compresslist(void)623af2637dbSPhilippe Charnier compresslist(void)
6249b50d902SRodney W. Grimes {
6259b50d902SRodney W. Grimes     cltype	*clp;
6269b50d902SRodney W. Grimes     cltype	**prev;
6279b50d902SRodney W. Grimes     arctype	**arcpp;
6289b50d902SRodney W. Grimes     arctype	**endlist;
6299b50d902SRodney W. Grimes     arctype	*arcp;
6309b50d902SRodney W. Grimes     arctype	*maxarcp;
6319b50d902SRodney W. Grimes     arctype	*maxexitarcp;
6329b50d902SRodney W. Grimes     arctype	*maxwithparentarcp;
6339b50d902SRodney W. Grimes     arctype	*maxnoparentarcp;
6349b50d902SRodney W. Grimes     int		maxexitcnt;
6359b50d902SRodney W. Grimes     int		maxwithparentcnt;
6369b50d902SRodney W. Grimes     int		maxnoparentcnt;
63707ac83f0SBruce Evans #   ifdef DEBUG
63807ac83f0SBruce Evans 	const char	*type;
6390fb7a0beSGarrett Wollman #   endif /* DEBUG */
6409b50d902SRodney W. Grimes 
6419b50d902SRodney W. Grimes     maxexitcnt = 0;
6429b50d902SRodney W. Grimes     maxwithparentcnt = 0;
6439b50d902SRodney W. Grimes     maxnoparentcnt = 0;
6449b50d902SRodney W. Grimes     for ( endlist = &archead , arcp = archead ; arcp ; ) {
6459b50d902SRodney W. Grimes 	if ( arcp -> arc_cyclecnt == 0 ) {
6469b50d902SRodney W. Grimes 	    arcp -> arc_flags &= ~ONLIST;
6479b50d902SRodney W. Grimes 	    *endlist = arcp -> arc_next;
6489b50d902SRodney W. Grimes 	    arcp -> arc_next = 0;
6499b50d902SRodney W. Grimes 	    arcp = *endlist;
6509b50d902SRodney W. Grimes 	    continue;
6519b50d902SRodney W. Grimes 	}
6529b50d902SRodney W. Grimes 	if ( arcp -> arc_childp -> flags & HASCYCLEXIT ) {
6539b50d902SRodney W. Grimes 	    if ( arcp -> arc_cyclecnt > maxexitcnt ||
6549b50d902SRodney W. Grimes 		( arcp -> arc_cyclecnt == maxexitcnt &&
6559b50d902SRodney W. Grimes 		arcp -> arc_cyclecnt < maxexitarcp -> arc_count ) ) {
6569b50d902SRodney W. Grimes 		maxexitcnt = arcp -> arc_cyclecnt;
6579b50d902SRodney W. Grimes 		maxexitarcp = arcp;
6589b50d902SRodney W. Grimes 	    }
6599b50d902SRodney W. Grimes 	} else if ( arcp -> arc_childp -> parentcnt > 1 ) {
6609b50d902SRodney W. Grimes 	    if ( arcp -> arc_cyclecnt > maxwithparentcnt ||
6619b50d902SRodney W. Grimes 		( arcp -> arc_cyclecnt == maxwithparentcnt &&
6629b50d902SRodney W. Grimes 		arcp -> arc_cyclecnt < maxwithparentarcp -> arc_count ) ) {
6639b50d902SRodney W. Grimes 		maxwithparentcnt = arcp -> arc_cyclecnt;
6649b50d902SRodney W. Grimes 		maxwithparentarcp = arcp;
6659b50d902SRodney W. Grimes 	    }
6669b50d902SRodney W. Grimes 	} else {
6679b50d902SRodney W. Grimes 	    if ( arcp -> arc_cyclecnt > maxnoparentcnt ||
6689b50d902SRodney W. Grimes 		( arcp -> arc_cyclecnt == maxnoparentcnt &&
6699b50d902SRodney W. Grimes 		arcp -> arc_cyclecnt < maxnoparentarcp -> arc_count ) ) {
6709b50d902SRodney W. Grimes 		maxnoparentcnt = arcp -> arc_cyclecnt;
6719b50d902SRodney W. Grimes 		maxnoparentarcp = arcp;
6729b50d902SRodney W. Grimes 	    }
6739b50d902SRodney W. Grimes 	}
6749b50d902SRodney W. Grimes 	endlist = &arcp -> arc_next;
6759b50d902SRodney W. Grimes 	arcp = arcp -> arc_next;
6769b50d902SRodney W. Grimes     }
6779b50d902SRodney W. Grimes     if ( maxexitcnt > 0 ) {
6789b50d902SRodney W. Grimes 	/*
6799b50d902SRodney W. Grimes 	 *	first choice is edge leading to node with out-of-cycle parent
6809b50d902SRodney W. Grimes 	 */
6819b50d902SRodney W. Grimes 	maxarcp = maxexitarcp;
6829b50d902SRodney W. Grimes #	ifdef DEBUG
6839b50d902SRodney W. Grimes 	    type = "exit";
6840fb7a0beSGarrett Wollman #	endif /* DEBUG */
6859b50d902SRodney W. Grimes     } else if ( maxwithparentcnt > 0 ) {
6869b50d902SRodney W. Grimes 	/*
6879b50d902SRodney W. Grimes 	 *	second choice is edge leading to node with at least one
6889b50d902SRodney W. Grimes 	 *	other in-cycle parent
6899b50d902SRodney W. Grimes 	 */
6909b50d902SRodney W. Grimes 	maxarcp = maxwithparentarcp;
6919b50d902SRodney W. Grimes #	ifdef DEBUG
6929b50d902SRodney W. Grimes 	    type = "internal";
6930fb7a0beSGarrett Wollman #	endif /* DEBUG */
6949b50d902SRodney W. Grimes     } else {
6959b50d902SRodney W. Grimes 	/*
6969b50d902SRodney W. Grimes 	 *	last choice is edge leading to node with only this arc as
6979b50d902SRodney W. Grimes 	 *	a parent (as it will now be orphaned)
6989b50d902SRodney W. Grimes 	 */
6999b50d902SRodney W. Grimes 	maxarcp = maxnoparentarcp;
7009b50d902SRodney W. Grimes #	ifdef DEBUG
7019b50d902SRodney W. Grimes 	    type = "orphan";
7020fb7a0beSGarrett Wollman #	endif /* DEBUG */
7039b50d902SRodney W. Grimes     }
7049b50d902SRodney W. Grimes     maxarcp -> arc_flags |= DEADARC;
7059b50d902SRodney W. Grimes     maxarcp -> arc_childp -> parentcnt -= 1;
7069b50d902SRodney W. Grimes     maxarcp -> arc_childp -> npropcall -= maxarcp -> arc_count;
7079b50d902SRodney W. Grimes #   ifdef DEBUG
7089b50d902SRodney W. Grimes 	if ( debug & BREAKCYCLE ) {
7098d57b8d3SBruce Evans 	    printf( "%s delete %s arc: %s (%ld) -> %s from %u cycle(s)\n" ,
7109b50d902SRodney W. Grimes 		"[compresslist]" , type , maxarcp -> arc_parentp -> name ,
7119b50d902SRodney W. Grimes 		maxarcp -> arc_count , maxarcp -> arc_childp -> name ,
7129b50d902SRodney W. Grimes 		maxarcp -> arc_cyclecnt );
7139b50d902SRodney W. Grimes 	}
7140fb7a0beSGarrett Wollman #   endif /* DEBUG */
7158d57b8d3SBruce Evans     printf( "\t%s to %s with %ld calls\n" , maxarcp -> arc_parentp -> name ,
7169b50d902SRodney W. Grimes 	maxarcp -> arc_childp -> name , maxarcp -> arc_count );
7179b50d902SRodney W. Grimes     prev = &cyclehead;
7189b50d902SRodney W. Grimes     for ( clp = cyclehead ; clp ; ) {
7199b50d902SRodney W. Grimes 	endlist = &clp -> list[ clp -> size ];
7209b50d902SRodney W. Grimes 	for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ )
7219b50d902SRodney W. Grimes 	    if ( (*arcpp) -> arc_flags & DEADARC )
7229b50d902SRodney W. Grimes 		break;
7239b50d902SRodney W. Grimes 	if ( arcpp == endlist ) {
7249b50d902SRodney W. Grimes 	    prev = &clp -> next;
7259b50d902SRodney W. Grimes 	    clp = clp -> next;
7269b50d902SRodney W. Grimes 	    continue;
7279b50d902SRodney W. Grimes 	}
7289b50d902SRodney W. Grimes 	for ( arcpp = clp -> list ; arcpp < endlist ; arcpp++ )
7299b50d902SRodney W. Grimes 	    (*arcpp) -> arc_cyclecnt--;
7309b50d902SRodney W. Grimes 	cyclecnt--;
7319b50d902SRodney W. Grimes 	*prev = clp -> next;
7329b50d902SRodney W. Grimes 	clp = clp -> next;
7339b50d902SRodney W. Grimes 	free( clp );
7349b50d902SRodney W. Grimes     }
7359b50d902SRodney W. Grimes }
7369b50d902SRodney W. Grimes 
7379b50d902SRodney W. Grimes #ifdef DEBUG
73897fa9b77SPhilippe Charnier void
printsubcycle(cltype * clp)739af2637dbSPhilippe Charnier printsubcycle(cltype *clp)
7409b50d902SRodney W. Grimes {
7419b50d902SRodney W. Grimes     arctype	**arcpp;
7429b50d902SRodney W. Grimes     arctype	**endlist;
7439b50d902SRodney W. Grimes 
7449b50d902SRodney W. Grimes     arcpp = clp -> list;
7459b50d902SRodney W. Grimes     printf( "%s <cycle %d>\n" , (*arcpp) -> arc_parentp -> name ,
7469b50d902SRodney W. Grimes 	(*arcpp) -> arc_parentp -> cycleno ) ;
7479b50d902SRodney W. Grimes     for ( endlist = &clp -> list[ clp -> size ]; arcpp < endlist ; arcpp++ )
7488d57b8d3SBruce Evans 	printf( "\t(%ld) -> %s\n" , (*arcpp) -> arc_count ,
7499b50d902SRodney W. Grimes 	    (*arcpp) -> arc_childp -> name ) ;
7509b50d902SRodney W. Grimes }
7510fb7a0beSGarrett Wollman #endif /* DEBUG */
7529b50d902SRodney W. Grimes 
75397fa9b77SPhilippe Charnier void
cycletime(void)754af2637dbSPhilippe Charnier cycletime(void)
7559b50d902SRodney W. Grimes {
7569b50d902SRodney W. Grimes     int			cycle;
7579b50d902SRodney W. Grimes     nltype		*cyclenlp;
7589b50d902SRodney W. Grimes     nltype		*childp;
7599b50d902SRodney W. Grimes 
7609b50d902SRodney W. Grimes     for ( cycle = 1 ; cycle <= ncycle ; cycle += 1 ) {
7619b50d902SRodney W. Grimes 	cyclenlp = &cyclenl[ cycle ];
7629b50d902SRodney W. Grimes 	for ( childp = cyclenlp -> cnext ; childp ; childp = childp -> cnext ) {
7639b50d902SRodney W. Grimes 	    if ( childp -> propfraction == 0.0 ) {
7649b50d902SRodney W. Grimes 		    /*
7659b50d902SRodney W. Grimes 		     * all members have the same propfraction except those
7669b50d902SRodney W. Grimes 		     *	that were excluded with -E
7679b50d902SRodney W. Grimes 		     */
7689b50d902SRodney W. Grimes 		continue;
7699b50d902SRodney W. Grimes 	    }
7709b50d902SRodney W. Grimes 	    cyclenlp -> time += childp -> time;
7719b50d902SRodney W. Grimes 	}
7729b50d902SRodney W. Grimes 	cyclenlp -> propself = cyclenlp -> propfraction * cyclenlp -> time;
7739b50d902SRodney W. Grimes     }
7749b50d902SRodney W. Grimes }
7759b50d902SRodney W. Grimes 
7769b50d902SRodney W. Grimes     /*
7779b50d902SRodney W. Grimes      *	in one top to bottom pass over the topologically sorted namelist
7789b50d902SRodney W. Grimes      *	propagate:
7799b50d902SRodney W. Grimes      *		printflag as the union of parents' printflags
7809b50d902SRodney W. Grimes      *		propfraction as the sum of fractional parents' propfractions
7819b50d902SRodney W. Grimes      *	and while we're here, sum time for functions.
7829b50d902SRodney W. Grimes      */
78397fa9b77SPhilippe Charnier void
doflags(void)784af2637dbSPhilippe Charnier doflags(void)
7859b50d902SRodney W. Grimes {
7869b50d902SRodney W. Grimes     int		index;
7879b50d902SRodney W. Grimes     nltype	*childp;
7889b50d902SRodney W. Grimes     nltype	*oldhead;
7899b50d902SRodney W. Grimes 
7909b50d902SRodney W. Grimes     oldhead = 0;
7919b50d902SRodney W. Grimes     for ( index = nname-1 ; index >= 0 ; index -= 1 ) {
7929b50d902SRodney W. Grimes 	childp = topsortnlp[ index ];
7939b50d902SRodney W. Grimes 	    /*
7949b50d902SRodney W. Grimes 	     *	if we haven't done this function or cycle,
7959b50d902SRodney W. Grimes 	     *	inherit things from parent.
7969b50d902SRodney W. Grimes 	     *	this way, we are linear in the number of arcs
7979b50d902SRodney W. Grimes 	     *	since we do all members of a cycle (and the cycle itself)
7989b50d902SRodney W. Grimes 	     *	as we hit the first member of the cycle.
7999b50d902SRodney W. Grimes 	     */
8009b50d902SRodney W. Grimes 	if ( childp -> cyclehead != oldhead ) {
8019b50d902SRodney W. Grimes 	    oldhead = childp -> cyclehead;
8029b50d902SRodney W. Grimes 	    inheritflags( childp );
8039b50d902SRodney W. Grimes 	}
8049b50d902SRodney W. Grimes #	ifdef DEBUG
8059b50d902SRodney W. Grimes 	    if ( debug & PROPDEBUG ) {
8069b50d902SRodney W. Grimes 		printf( "[doflags] " );
8079b50d902SRodney W. Grimes 		printname( childp );
8089b50d902SRodney W. Grimes 		printf( " inherits printflag %d and propfraction %f\n" ,
8099b50d902SRodney W. Grimes 			childp -> printflag , childp -> propfraction );
8109b50d902SRodney W. Grimes 	    }
8110fb7a0beSGarrett Wollman #	endif /* DEBUG */
8129b50d902SRodney W. Grimes 	if ( ! childp -> printflag ) {
8139b50d902SRodney W. Grimes 		/*
8149b50d902SRodney W. Grimes 		 *	printflag is off
8159b50d902SRodney W. Grimes 		 *	it gets turned on by
8169b50d902SRodney W. Grimes 		 *	being on -f list,
8179b50d902SRodney W. Grimes 		 *	or there not being any -f list and not being on -e list.
8189b50d902SRodney W. Grimes 		 */
8199b50d902SRodney W. Grimes 	    if (   onlist( flist , childp -> name )
8209b50d902SRodney W. Grimes 		|| ( !fflag && !onlist( elist , childp -> name ) ) ) {
8219b50d902SRodney W. Grimes 		childp -> printflag = TRUE;
8229b50d902SRodney W. Grimes 	    }
8239b50d902SRodney W. Grimes 	} else {
8249b50d902SRodney W. Grimes 		/*
8259b50d902SRodney W. Grimes 		 *	this function has printing parents:
8269b50d902SRodney W. Grimes 		 *	maybe someone wants to shut it up
8279b50d902SRodney W. Grimes 		 *	by putting it on -e list.  (but favor -f over -e)
8289b50d902SRodney W. Grimes 		 */
8299b50d902SRodney W. Grimes 	    if (  ( !onlist( flist , childp -> name ) )
8309b50d902SRodney W. Grimes 		&& onlist( elist , childp -> name ) ) {
8319b50d902SRodney W. Grimes 		childp -> printflag = FALSE;
8329b50d902SRodney W. Grimes 	    }
8339b50d902SRodney W. Grimes 	}
8349b50d902SRodney W. Grimes 	if ( childp -> propfraction == 0.0 ) {
8359b50d902SRodney W. Grimes 		/*
8369b50d902SRodney W. Grimes 		 *	no parents to pass time to.
8379b50d902SRodney W. Grimes 		 *	collect time from children if
8389b50d902SRodney W. Grimes 		 *	its on -F list,
8399b50d902SRodney W. Grimes 		 *	or there isn't any -F list and its not on -E list.
8409b50d902SRodney W. Grimes 		 */
8419b50d902SRodney W. Grimes 	    if ( onlist( Flist , childp -> name )
8429b50d902SRodney W. Grimes 		|| ( !Fflag && !onlist( Elist , childp -> name ) ) ) {
8439b50d902SRodney W. Grimes 		    childp -> propfraction = 1.0;
8449b50d902SRodney W. Grimes 	    }
8459b50d902SRodney W. Grimes 	} else {
8469b50d902SRodney W. Grimes 		/*
8479b50d902SRodney W. Grimes 		 *	it has parents to pass time to,
8489b50d902SRodney W. Grimes 		 *	but maybe someone wants to shut it up
84997fa9b77SPhilippe Charnier 		 *	by putting it on -E list.  (but favor -F over -E)
8509b50d902SRodney W. Grimes 		 */
8519b50d902SRodney W. Grimes 	    if (  !onlist( Flist , childp -> name )
8529b50d902SRodney W. Grimes 		&& onlist( Elist , childp -> name ) ) {
8539b50d902SRodney W. Grimes 		childp -> propfraction = 0.0;
8549b50d902SRodney W. Grimes 	    }
8559b50d902SRodney W. Grimes 	}
8569b50d902SRodney W. Grimes 	childp -> propself = childp -> time * childp -> propfraction;
8579b50d902SRodney W. Grimes 	printtime += childp -> propself;
8589b50d902SRodney W. Grimes #	ifdef DEBUG
8599b50d902SRodney W. Grimes 	    if ( debug & PROPDEBUG ) {
8609b50d902SRodney W. Grimes 		printf( "[doflags] " );
8619b50d902SRodney W. Grimes 		printname( childp );
8629b50d902SRodney W. Grimes 		printf( " ends up with printflag %d and propfraction %f\n" ,
8639b50d902SRodney W. Grimes 			childp -> printflag , childp -> propfraction );
8649b50d902SRodney W. Grimes 		printf( "time %f propself %f printtime %f\n" ,
8659b50d902SRodney W. Grimes 			childp -> time , childp -> propself , printtime );
8669b50d902SRodney W. Grimes 	    }
8670fb7a0beSGarrett Wollman #	endif /* DEBUG */
8689b50d902SRodney W. Grimes     }
8699b50d902SRodney W. Grimes }
8709b50d902SRodney W. Grimes 
8719b50d902SRodney W. Grimes     /*
8729b50d902SRodney W. Grimes      *	check if any parent of this child
8739b50d902SRodney W. Grimes      *	(or outside parents of this cycle)
8749b50d902SRodney W. Grimes      *	have their print flags on and set the
8759b50d902SRodney W. Grimes      *	print flag of the child (cycle) appropriately.
8769b50d902SRodney W. Grimes      *	similarly, deal with propagation fractions from parents.
8779b50d902SRodney W. Grimes      */
87897fa9b77SPhilippe Charnier void
inheritflags(nltype * childp)879af2637dbSPhilippe Charnier inheritflags(nltype *childp)
8809b50d902SRodney W. Grimes {
8819b50d902SRodney W. Grimes     nltype	*headp;
8829b50d902SRodney W. Grimes     arctype	*arcp;
8839b50d902SRodney W. Grimes     nltype	*parentp;
8849b50d902SRodney W. Grimes     nltype	*memp;
8859b50d902SRodney W. Grimes 
8869b50d902SRodney W. Grimes     headp = childp -> cyclehead;
8879b50d902SRodney W. Grimes     if ( childp == headp ) {
8889b50d902SRodney W. Grimes 	    /*
8899b50d902SRodney W. Grimes 	     *	just a regular child, check its parents
8909b50d902SRodney W. Grimes 	     */
8919b50d902SRodney W. Grimes 	childp -> printflag = FALSE;
8929b50d902SRodney W. Grimes 	childp -> propfraction = 0.0;
8939b50d902SRodney W. Grimes 	for (arcp = childp -> parents ; arcp ; arcp = arcp -> arc_parentlist) {
8949b50d902SRodney W. Grimes 	    parentp = arcp -> arc_parentp;
8959b50d902SRodney W. Grimes 	    if ( childp == parentp ) {
8969b50d902SRodney W. Grimes 		continue;
8979b50d902SRodney W. Grimes 	    }
8989b50d902SRodney W. Grimes 	    childp -> printflag |= parentp -> printflag;
8999b50d902SRodney W. Grimes 		/*
9009b50d902SRodney W. Grimes 		 *	if the child was never actually called
9019b50d902SRodney W. Grimes 		 *	(e.g. this arc is static (and all others are, too))
9029b50d902SRodney W. Grimes 		 *	no time propagates along this arc.
9039b50d902SRodney W. Grimes 		 */
9049b50d902SRodney W. Grimes 	    if ( arcp -> arc_flags & DEADARC ) {
9059b50d902SRodney W. Grimes 		continue;
9069b50d902SRodney W. Grimes 	    }
9079b50d902SRodney W. Grimes 	    if ( childp -> npropcall ) {
9089b50d902SRodney W. Grimes 		childp -> propfraction += parentp -> propfraction
9099b50d902SRodney W. Grimes 					* ( ( (double) arcp -> arc_count )
9109b50d902SRodney W. Grimes 					  / ( (double) childp -> npropcall ) );
9119b50d902SRodney W. Grimes 	    }
9129b50d902SRodney W. Grimes 	}
9139b50d902SRodney W. Grimes     } else {
9149b50d902SRodney W. Grimes 	    /*
9159b50d902SRodney W. Grimes 	     *	its a member of a cycle, look at all parents from
9169b50d902SRodney W. Grimes 	     *	outside the cycle
9179b50d902SRodney W. Grimes 	     */
9189b50d902SRodney W. Grimes 	headp -> printflag = FALSE;
9199b50d902SRodney W. Grimes 	headp -> propfraction = 0.0;
9209b50d902SRodney W. Grimes 	for ( memp = headp -> cnext ; memp ; memp = memp -> cnext ) {
9219b50d902SRodney W. Grimes 	    for (arcp = memp->parents ; arcp ; arcp = arcp->arc_parentlist) {
9229b50d902SRodney W. Grimes 		if ( arcp -> arc_parentp -> cyclehead == headp ) {
9239b50d902SRodney W. Grimes 		    continue;
9249b50d902SRodney W. Grimes 		}
9259b50d902SRodney W. Grimes 		parentp = arcp -> arc_parentp;
9269b50d902SRodney W. Grimes 		headp -> printflag |= parentp -> printflag;
9279b50d902SRodney W. Grimes 		    /*
9289b50d902SRodney W. Grimes 		     *	if the cycle was never actually called
9299b50d902SRodney W. Grimes 		     *	(e.g. this arc is static (and all others are, too))
9309b50d902SRodney W. Grimes 		     *	no time propagates along this arc.
9319b50d902SRodney W. Grimes 		     */
9329b50d902SRodney W. Grimes 		if ( arcp -> arc_flags & DEADARC ) {
9339b50d902SRodney W. Grimes 		    continue;
9349b50d902SRodney W. Grimes 		}
9359b50d902SRodney W. Grimes 		if ( headp -> npropcall ) {
9369b50d902SRodney W. Grimes 		    headp -> propfraction += parentp -> propfraction
9379b50d902SRodney W. Grimes 					* ( ( (double) arcp -> arc_count )
9389b50d902SRodney W. Grimes 					  / ( (double) headp -> npropcall ) );
9399b50d902SRodney W. Grimes 		}
9409b50d902SRodney W. Grimes 	    }
9419b50d902SRodney W. Grimes 	}
9429b50d902SRodney W. Grimes 	for ( memp = headp ; memp ; memp = memp -> cnext ) {
9439b50d902SRodney W. Grimes 	    memp -> printflag = headp -> printflag;
9449b50d902SRodney W. Grimes 	    memp -> propfraction = headp -> propfraction;
9459b50d902SRodney W. Grimes 	}
9469b50d902SRodney W. Grimes     }
9479b50d902SRodney W. Grimes }
948