2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation and computation of the callgraph.
23 * @author Goetz Lindenmaier
32 #include "callgraph.h"
36 #include "irgraph_t.h"
40 #include "execution_frequency.h"
45 #include "raw_bitset.h"
49 static ir_visited_t master_cg_visited = 0;
50 static inline int cg_irg_visited (ir_graph *n);
51 static inline void mark_cg_irg_visited(ir_graph *n);
53 /** Returns the callgraph state of the program representation. */
54 irp_callgraph_state get_irp_callgraph_state(void)
56 return irp->callgraph_state;
59 /* Sets the callgraph state of the program representation. */
60 void set_irp_callgraph_state(irp_callgraph_state s)
62 irp->callgraph_state = s;
65 /* Returns the number of procedures that call the given irg. */
66 int get_irg_n_callers(const ir_graph *irg)
68 if (irg->callers) return ARR_LEN(irg->callers);
72 /* Returns the caller at position pos. */
73 ir_graph *get_irg_caller(const ir_graph *irg, int pos)
75 assert(pos >= 0 && pos < get_irg_n_callers(irg));
76 if (irg->callers) return irg->callers[pos];
80 /* Returns non-zero if the caller at position pos is "a backedge", i.e. a recursion. */
81 int is_irg_caller_backedge(const ir_graph *irg, int pos)
83 assert(pos >= 0 && pos < get_irg_n_callers(irg));
84 return irg->caller_isbe != NULL ? rbitset_is_set(irg->caller_isbe, pos) : 0;
87 /** Search the caller in the list of all callers and set it's backedge property. */
88 static void set_irg_caller_backedge(ir_graph *irg, ir_graph *caller)
90 int i, n_callers = get_irg_n_callers(irg);
92 /* allocate a new array on demand */
93 if (irg->caller_isbe == NULL)
94 irg->caller_isbe = rbitset_malloc(n_callers);
95 for (i = 0; i < n_callers; ++i) {
96 if (get_irg_caller(irg, i) == caller) {
97 rbitset_set(irg->caller_isbe, i);
103 /* Returns non-zero if the irg has a backedge caller. */
104 int has_irg_caller_backedge(const ir_graph *irg)
106 int i, n_callers = get_irg_n_callers(irg);
108 if (irg->caller_isbe != NULL) {
109 for (i = 0; i < n_callers; ++i)
110 if (rbitset_is_set(irg->caller_isbe, i))
117 * Find the reversion position of a caller.
118 * Given the position pos_caller of an caller of irg, return
119 * irg's callee position on that caller.
121 static int reverse_pos(const ir_graph *callee, int pos_caller)
123 ir_graph *caller = get_irg_caller(callee, pos_caller);
124 /* search the other relation for the corresponding edge. */
126 int i, n_callees = get_irg_n_callees(caller);
127 for (i = 0; i < n_callees; ++i) {
128 if (get_irg_callee(caller, i) == callee) {
134 assert(pos_callee >= 0);
139 /* Returns the maximal loop depth of call nodes that call along this edge. */
140 int get_irg_caller_loop_depth(const ir_graph *irg, int pos)
142 ir_graph *caller = get_irg_caller(irg, pos);
143 int pos_callee = reverse_pos(irg, pos);
145 return get_irg_callee_loop_depth(caller, pos_callee);
149 /* Returns the number of procedures that are called by the given irg. */
150 int get_irg_n_callees(const ir_graph *irg)
152 if (irg->callees) return ARR_LEN(irg->callees);
156 /* Returns the callee at position pos. */
157 ir_graph *get_irg_callee(const ir_graph *irg, int pos)
159 assert(pos >= 0 && pos < get_irg_n_callees(irg));
160 if (irg->callees) return irg->callees[pos]->irg;
164 /* Returns non-zero if the callee at position pos is "a backedge", i.e. a recursion. */
165 int is_irg_callee_backedge(const ir_graph *irg, int pos)
167 assert(pos >= 0 && pos < get_irg_n_callees(irg));
168 return irg->callee_isbe != NULL ? rbitset_is_set(irg->callee_isbe, pos) : 0;
171 /* Returns non-zero if the irg has a backedge callee. */
172 int has_irg_callee_backedge(const ir_graph *irg)
174 int i, n_callees = get_irg_n_callees(irg);
176 if (irg->callee_isbe != NULL) {
177 for (i = 0; i < n_callees; ++i)
178 if (rbitset_is_set(irg->callee_isbe, i))
185 * Mark the callee at position pos as a backedge.
187 static void set_irg_callee_backedge(ir_graph *irg, int pos)
189 int n = get_irg_n_callees(irg);
191 /* allocate a new array on demand */
192 if (irg->callee_isbe == NULL)
193 irg->callee_isbe = rbitset_malloc(n);
194 assert(pos >= 0 && pos < n);
195 rbitset_set(irg->callee_isbe, pos);
198 /* Returns the maximal loop depth of call nodes that call along this edge. */
199 int get_irg_callee_loop_depth(const ir_graph *irg, int pos)
201 assert(pos >= 0 && pos < get_irg_n_callees(irg));
202 if (irg->callees) return irg->callees[pos]->max_depth;
206 static double get_irg_callee_execution_frequency(const ir_graph *irg, int pos)
208 ir_node **arr = irg->callees[pos]->call_list;
209 int i, n_Calls = ARR_LEN(arr);
212 for (i = 0; i < n_Calls; ++i) {
213 freq += get_irn_exec_freq(arr[i]);
218 static double get_irg_callee_method_execution_frequency(const ir_graph *irg,
221 double call_freq = get_irg_callee_execution_frequency(irg, pos);
222 double meth_freq = get_irg_method_execution_frequency(irg);
223 return call_freq * meth_freq;
226 static double get_irg_caller_method_execution_frequency(const ir_graph *irg,
229 ir_graph *caller = get_irg_caller(irg, pos);
230 int pos_callee = reverse_pos(irg, pos);
232 return get_irg_callee_method_execution_frequency(caller, pos_callee);
236 /* --------------------- Compute the callgraph ------------------------ */
239 * Walker called by compute_callgraph(), analyses all Call nodes.
241 static void ana_Call(ir_node *n, void *env)
247 if (! is_Call(n)) return;
249 irg = get_irn_irg(n);
250 n_callees = get_Call_n_callees(n);
251 for (i = 0; i < n_callees; ++i) {
252 ir_entity *callee_e = get_Call_callee(n, i);
253 ir_graph *callee = get_entity_irg(callee_e);
257 cg_callee_entry *found;
262 pset_insert((pset *)callee->callers, irg, HASH_PTR(irg));
263 found = pset_find((pset *)irg->callees, &buf, HASH_PTR(callee));
264 if (found) { /* add Call node to list, compute new nesting. */
265 ir_node **arr = found->call_list;
266 ARR_APP1(ir_node *, arr, n);
267 found->call_list = arr;
268 } else { /* New node, add Call node and init nesting. */
269 found = OALLOC(irg->obst, cg_callee_entry);
271 found->call_list = NEW_ARR_F(ir_node *, 1);
272 found->call_list[0] = n;
273 found->max_depth = 0;
274 pset_insert((pset *)irg->callees, found, HASH_PTR(callee));
276 depth = get_loop_depth(get_irn_loop(get_nodes_block(n)));
277 found->max_depth = (depth > found->max_depth) ? depth : found->max_depth;
282 /** compare two ir graphs in a cg_callee_entry */
283 static int cg_callee_entry_cmp(const void *elt, const void *key)
285 const cg_callee_entry *e1 = elt;
286 const cg_callee_entry *e2 = key;
287 return e1->irg != e2->irg;
290 /** compare two ir graphs for pointer identity */
291 static int graph_cmp(const void *elt, const void *key)
293 const ir_graph *e1 = elt;
294 const ir_graph *e2 = key;
299 /* Construct and destruct the callgraph. */
300 void compute_callgraph(void)
304 #ifdef INTERPROCEDURAL_VIEW
305 assert(! get_interprocedural_view()); /* Else walking will not reach the Call nodes. */
311 n_irgs = get_irp_n_irgs();
312 for (i = 0; i < n_irgs; ++i) {
313 ir_graph *irg = get_irp_irg(i);
314 assert(get_irg_callee_info_state(irg) == irg_callee_info_consistent);
315 irg->callees = (cg_callee_entry **)new_pset(cg_callee_entry_cmp, 8);
316 irg->callers = (ir_graph **)new_pset(graph_cmp, 8);
317 //construct_cf_backedges(irg);
320 /* Compute the call graph */
321 for (i = 0; i < n_irgs; ++i) {
322 ir_graph *irg = get_irp_irg(i);
323 construct_cf_backedges(irg); // We also find the maximal loop depth of a call.
324 irg_walk_graph(irg, ana_Call, NULL, NULL);
327 /* Change the sets to arrays. */
328 for (i = 0; i < n_irgs; ++i) {
330 cg_callee_entry *callee;
331 ir_graph *c, *irg = get_irp_irg(i);
332 pset *callee_set, *caller_set;
334 callee_set = (pset *)irg->callees;
335 count = pset_count(callee_set);
336 irg->callees = NEW_ARR_F(cg_callee_entry *, count);
337 irg->callee_isbe = NULL;
338 callee = pset_first(callee_set);
339 for (j = 0; j < count; ++j) {
340 irg->callees[j] = callee;
341 callee = pset_next(callee_set);
343 del_pset(callee_set);
344 assert(callee == NULL);
346 caller_set = (pset *)irg->callers;
347 count = pset_count(caller_set);
348 irg->callers = NEW_ARR_F(ir_graph *, count);
349 irg->caller_isbe = NULL;
350 c = pset_first(caller_set);
351 for (j = 0; j < count; ++j) {
353 c = pset_next(caller_set);
355 del_pset(caller_set);
358 set_irp_callgraph_state(irp_callgraph_consistent);
361 /* Destruct the callgraph. */
362 void free_callgraph(void)
364 int i, n_irgs = get_irp_n_irgs();
365 for (i = 0; i < n_irgs; ++i) {
366 ir_graph *irg = get_irp_irg(i);
367 if (irg->callees) DEL_ARR_F(irg->callees);
368 if (irg->callers) DEL_ARR_F(irg->callers);
369 if (irg->callee_isbe) free(irg->callee_isbe);
370 if (irg->caller_isbe) free(irg->caller_isbe);
373 irg->callee_isbe = NULL;
374 irg->caller_isbe = NULL;
376 set_irp_callgraph_state(irp_callgraph_none);
379 /* ----------------------------------------------------------------------------------- */
380 /* A walker for the callgraph */
381 /* ----------------------------------------------------------------------------------- */
384 static void do_walk(ir_graph *irg, callgraph_walk_func *pre, callgraph_walk_func *post, void *env)
388 if (cg_irg_visited(irg))
390 mark_cg_irg_visited(irg);
395 n_callees = get_irg_n_callees(irg);
396 for (i = 0; i < n_callees; i++) {
397 ir_graph *m = get_irg_callee(irg, i);
398 do_walk(m, pre, post, env);
405 void callgraph_walk(callgraph_walk_func *pre, callgraph_walk_func *post, void *env)
407 int i, n_irgs = get_irp_n_irgs();
410 /* roots are methods which have no callers in the current program */
411 for (i = 0; i < n_irgs; ++i) {
412 ir_graph *irg = get_irp_irg(i);
414 if (get_irg_n_callers(irg) == 0)
415 do_walk(irg, pre, post, env);
418 /* in case of unreachable call loops we haven't visited some irgs yet */
419 for (i = 0; i < n_irgs; i++) {
420 ir_graph *irg = get_irp_irg(i);
421 do_walk(irg, pre, post, env);
425 /* ----------------------------------------------------------------------------------- */
426 /* loop construction algorithm */
427 /* ----------------------------------------------------------------------------------- */
429 static ir_graph *outermost_ir_graph; /**< The outermost graph the scc is computed
431 static ir_loop *current_loop; /**< Current cfloop construction is working
433 static int loop_node_cnt = 0; /**< Counts the number of allocated cfloop nodes.
434 Each cfloop node gets a unique number.
435 What for? ev. remove. @@@ */
436 static int current_dfn = 1; /**< Counter to generate depth first numbering
439 /*-----------------*/
440 /* Node attributes */
441 /*-----------------*/
443 typedef struct scc_info {
444 int in_stack; /**< Marks whether node is on the stack. */
445 int dfn; /**< Depth first search number. */
446 int uplink; /**< dfn number of ancestor. */
451 * allocates a new scc_info on the obstack
453 static inline scc_info *new_scc_info(struct obstack *obst)
455 return OALLOCZ(obst, scc_info);
459 * Returns non-zero if a graph was already visited.
461 static inline int cg_irg_visited(ir_graph *irg)
463 return irg->self_visited >= master_cg_visited;
467 * Marks a graph as visited.
469 static inline void mark_cg_irg_visited(ir_graph *irg)
471 irg->self_visited = master_cg_visited;
475 * Set a graphs visited flag to i.
477 static inline void set_cg_irg_visited(ir_graph *irg, ir_visited_t i)
479 irg->self_visited = i;
483 * Returns the visited flag of a graph.
485 static inline ir_visited_t get_cg_irg_visited(ir_graph *irg)
487 return irg->self_visited;
490 static inline void mark_irg_in_stack(ir_graph *irg)
492 scc_info *info = get_irg_link(irg);
493 assert(info && "missing call to init_scc()");
497 static inline void mark_irg_not_in_stack(ir_graph *irg)
499 scc_info *info = get_irg_link(irg);
500 assert(info && "missing call to init_scc()");
504 static inline int irg_is_in_stack(ir_graph *irg)
506 scc_info *info = get_irg_link(irg);
507 assert(info && "missing call to init_scc()");
508 return info->in_stack;
511 static inline void set_irg_uplink(ir_graph *irg, int uplink)
513 scc_info *info = get_irg_link(irg);
514 assert(info && "missing call to init_scc()");
515 info->uplink = uplink;
518 static inline int get_irg_uplink(ir_graph *irg)
520 scc_info *info = get_irg_link(irg);
521 assert(info && "missing call to init_scc()");
525 static inline void set_irg_dfn(ir_graph *irg, int dfn)
527 scc_info *info = get_irg_link(irg);
528 assert(info && "missing call to init_scc()");
532 static inline int get_irg_dfn(ir_graph *irg)
534 scc_info *info = get_irg_link(irg);
535 assert(info && "missing call to init_scc()");
539 /**********************************************************************/
541 /**********************************************************************/
543 static ir_graph **stack = NULL;
544 static int tos = 0; /**< top of stack */
547 * Initialize the irg stack.
549 static inline void init_stack(void)
552 ARR_RESIZE(ir_graph *, stack, 1000);
554 stack = NEW_ARR_F(ir_graph *, 1000);
560 * push a graph on the irg stack
561 * @param n the graph to be pushed
563 static inline void push(ir_graph *irg)
565 if (tos == ARR_LEN(stack)) {
566 int nlen = ARR_LEN(stack) * 2;
567 ARR_RESIZE(ir_node *, stack, nlen);
570 mark_irg_in_stack(irg);
574 * return the topmost graph on the stack and pop it
576 static inline ir_graph *pop(void)
578 ir_graph *irg = stack[--tos];
579 mark_irg_not_in_stack(irg);
584 * The nodes up to irg belong to the current loop.
585 * Removes them from the stack and adds them to the current loop.
587 static inline void pop_scc_to_loop(ir_graph *irg)
594 set_irg_dfn(m, loop_node_cnt);
595 add_loop_irg(current_loop, m);
597 //m->callgraph_loop_depth = current_loop->depth;
601 /* GL ??? my last son is my grandson??? Removes cfloops with no
602 ir_nodes in them. Such loops have only another loop as son. (Why
603 can't they have two loops as sons? Does it never get that far? ) */
604 static void close_loop(ir_loop *l)
606 int last = get_loop_n_elements(l) - 1;
607 loop_element lelement = get_loop_element(l, last);
608 ir_loop *last_son = lelement.son;
610 if (get_kind(last_son) == k_ir_loop &&
611 get_loop_n_elements(last_son) == 1) {
614 lelement = get_loop_element(last_son, 0);
616 if (get_kind(gson) == k_ir_loop) {
617 loop_element new_last_son;
619 gson->outer_loop = l;
620 new_last_son.son = gson;
621 l->children[last] = new_last_son;
628 * Removes and unmarks all nodes up to n from the stack.
629 * The nodes must be visited once more to assign them to a scc.
631 static inline void pop_scc_unmark_visit(ir_graph *n)
637 set_cg_irg_visited(m, 0);
641 /**********************************************************************/
642 /* The loop data structure. **/
643 /**********************************************************************/
646 * Allocates a new loop as son of current_loop. Sets current_loop
647 * to the new loop and returns the father.
649 static ir_loop *new_loop(void)
651 ir_loop *father = current_loop;
652 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
659 /**********************************************************************/
660 /* Constructing and destructing the loop/backedge information. **/
661 /**********************************************************************/
663 /* Initialization steps. **********************************************/
665 static void init_scc(struct obstack *obst)
674 n_irgs = get_irp_n_irgs();
675 for (i = 0; i < n_irgs; ++i) {
676 ir_graph *irg = get_irp_irg(i);
677 set_irg_link(irg, new_scc_info(obst));
678 irg->callgraph_recursion_depth = 0;
679 irg->callgraph_loop_depth = 0;
683 /** Returns non-zero if n is a loop header, i.e., it is a Block node
684 * and has predecessors within the cfloop and out of the cfloop.
686 * @param root: only needed for assertion.
688 static int is_head(ir_graph *n, ir_graph *root)
691 int some_outof_loop = 0, some_in_loop = 0;
693 arity = get_irg_n_callees(n);
694 for (i = 0; i < arity; i++) {
695 ir_graph *pred = get_irg_callee(n, i);
696 if (is_irg_callee_backedge(n, i)) continue;
697 if (!irg_is_in_stack(pred)) {
700 if (get_irg_uplink(pred) < get_irg_uplink(root)) {
701 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
707 return some_outof_loop && some_in_loop;
711 * Returns non-zero if n is possible loop head of an endless loop.
712 * I.e., it is a Block, Phi or Filter node and has only predecessors
714 * @arg root: only needed for assertion.
716 static int is_endless_head(ir_graph *n, ir_graph *root)
719 int some_outof_loop = 0, some_in_loop = 0;
721 arity = get_irg_n_callees(n);
722 for (i = 0; i < arity; i++) {
723 ir_graph *pred = get_irg_callee(n, i);
725 if (is_irg_callee_backedge(n, i))
727 if (!irg_is_in_stack(pred)) {
730 if (get_irg_uplink(pred) < get_irg_uplink(root)) {
731 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
736 return !some_outof_loop && some_in_loop;
739 #ifdef INTERPROCEDURAL_VIEW
741 * Check whether there is a parallel edge in the ip control flow.
744 static int is_ip_head(ir_graph *n, ir_graph *pred)
748 int iv_rem = get_interprocedural_view();
749 set_interprocedural_view(1);
751 ir_node *sblock = get_irg_start_block(n);
752 int i, arity = get_Block_n_cfgpreds(sblock);
754 for (i = 0; i < arity; i++) {
755 ir_node *pred_cfop = skip_Proj(get_Block_cfgpred(sblock, i));
756 if (is_CallBegin(pred_cfop)) { /* could be Unknown */
757 ir_graph *ip_pred = get_irn_irg(pred_cfop);
758 if ((ip_pred == pred) && is_backedge(sblock, i)) {
764 set_interprocedural_view(iv_rem);
767 #endif /* INTERPROCEDURAL_VIEW */
770 * Returns index of the predecessor with the smallest dfn number
771 * greater-equal than limit.
773 static int smallest_dfn_pred(ir_graph *n, int limit)
775 int i, index = -2, min = -1;
777 int arity = get_irg_n_callees(n);
778 for (i = 0; i < arity; i++) {
779 ir_graph *pred = get_irg_callee(n, i);
780 if (is_irg_callee_backedge(n, i) || !irg_is_in_stack(pred))
782 if (get_irg_dfn(pred) >= limit && (min == -1 || get_irg_dfn(pred) < min)) {
784 min = get_irg_dfn(pred);
791 /** Returns index of the predecessor with the largest dfn number. */
792 static int largest_dfn_pred(ir_graph *n)
794 int i, index = -2, max = -1;
796 int arity = get_irg_n_callees(n);
797 for (i = 0; i < arity; ++i) {
798 ir_graph *pred = get_irg_callee(n, i);
799 if (is_irg_callee_backedge (n, i) || !irg_is_in_stack(pred)) continue;
800 if (get_irg_dfn(pred) > max) {
802 max = get_irg_dfn(pred);
809 #ifndef INTERPROCEDURAL_VIEW
810 static ir_graph *find_tail(ir_graph *n)
813 int i, res_index = -2;
816 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
818 m = stack[tos-1]; /* tos = top of stack */
819 if (is_head (m, n)) {
820 res_index = smallest_dfn_pred(m, 0);
821 if ((res_index == -2) && /* no smallest dfn pred found. */
825 if (m == n) return NULL; // Is this to catch Phi - self loops?
826 for (i = tos-2; i >= 0; --i) {
830 res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
831 if (res_index == -2) /* no smallest dfn pred found. */
832 res_index = largest_dfn_pred(m);
834 if ((m == n) && (res_index == -2)) {
840 /* We should not walk past our selves on the stack: The upcoming nodes
841 are not in this loop. We assume a loop not reachable from Start. */
850 /* A dead loop not reachable from Start. */
851 for (i = tos-2; i >= 0; --i) {
853 if (is_endless_head(m, n)) {
854 res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
855 if (res_index == -2) /* no smallest dfn pred found. */
856 res_index = largest_dfn_pred(m);
859 if (m == n) { break; } /* It's not an unreachable loop, either. */
861 //assert(0 && "no head found on stack");
865 assert (res_index > -2);
867 set_irg_callee_backedge(m, res_index);
868 return get_irg_callee(m, res_index);
871 static ir_graph *find_tail(ir_graph *n)
874 int i, res_index = -2;
877 ir_graph *in_and_out = NULL;
878 ir_graph *only_in = NULL;
879 ir_graph *ip_in_and_out = NULL;
880 ir_graph *ip_only_in = NULL;
882 for (i = tos-1; i >= 0; --i) {
883 ir_graph *pred = (i < tos -1) ? stack[i+1] : n;
887 //printf(" found 1a! "); DDM;
889 if (is_ip_head(pred, m)) {
890 //printf(" found 1b! "); DDM;
893 } else if (!ip_only_in && is_endless_head(m, n)) {
895 //printf(" found 2a! "); DDM;
896 if (is_ip_head(pred, m)) {
897 //printf(" found 2b! "); DDM;
900 } else if (is_ip_head(pred, m)) {
901 //printf(" found 3! "); DDM; This happens for self recursions in the second
902 //assert(0); scc iteration (the one to flip the loop.)
905 if (ip_in_and_out) break; /* That's what we really want. */
907 if (m == n) break; /* Don't walk past n on the stack! */
911 if (!in_and_out && !only_in)
912 /* There is no loop */
916 /* Is there a head in the callgraph without a head in the
918 assert(in_and_out || only_in);
920 m = (ip_in_and_out) ? ip_in_and_out : ip_only_in;
923 m = (in_and_out) ? in_and_out : only_in;
925 res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
926 if (res_index == -2) /* no smallest dfn pred found. */
927 res_index = largest_dfn_pred(m);
929 set_irg_callee_backedge(m, res_index);
930 res = get_irg_callee(m, res_index);
933 #endif /* INTERPROCEDURAL_VIEW */
935 /*-----------------------------------------------------------*
936 * The core algorithm. *
937 *-----------------------------------------------------------*/
940 static void cgscc(ir_graph *n)
944 if (cg_irg_visited(n)) return;
945 mark_cg_irg_visited(n);
947 /* Initialize the node */
948 set_irg_dfn(n, current_dfn); /* Depth first number for this node */
949 set_irg_uplink(n, current_dfn); /* ... is default uplink. */
953 arity = get_irg_n_callees(n);
954 for (i = 0; i < arity; i++) {
956 if (is_irg_callee_backedge(n, i)) continue;
957 m = get_irg_callee(n, i);
959 /** This marks the backedge, but does it guarantee a correct loop tree? */
960 //if (m == n) { set_irg_callee_backedge(n, i); continue; }
963 if (irg_is_in_stack(m)) {
964 /* Uplink of m is smaller if n->m is a backedge.
965 Propagate the uplink to mark the cfloop. */
966 if (get_irg_uplink(m) < get_irg_uplink(n))
967 set_irg_uplink(n, get_irg_uplink(m));
971 if (get_irg_dfn(n) == get_irg_uplink(n)) {
972 /* This condition holds for
973 1) the node with the incoming backedge.
974 That is: We found a cfloop!
975 2) Straight line code, because no uplink has been propagated, so the
976 uplink still is the same as the dfn.
978 But n might not be a proper cfloop head for the analysis. Proper cfloop
979 heads are Block and Phi nodes. find_tail searches the stack for
980 Block's and Phi's and takes those nodes as cfloop heads for the current
981 cfloop instead and marks the incoming edge as backedge. */
983 ir_graph *tail = find_tail(n);
985 /* We have a cfloop, that is no straight line code,
986 because we found a cfloop head!
987 Next actions: Open a new cfloop on the cfloop tree and
988 try to find inner cfloops */
991 ir_loop *l = new_loop();
993 /* Remove the cfloop from the stack ... */
994 pop_scc_unmark_visit(n);
996 /* The current backedge has been marked, that is temporarily eliminated,
997 by find tail. Start the scc algorithm
998 anew on the subgraph thats left (the current cfloop without the backedge)
999 in order to find more inner cfloops. */
1003 assert(cg_irg_visited(n));
1013 * reset the backedge information for all callers in all irgs
1015 static void reset_isbe(void)
1017 int i, n_irgs = get_irp_n_irgs();
1019 for (i = 0; i < n_irgs; ++i) {
1020 ir_graph *irg = get_irp_irg(i);
1022 if (irg->caller_isbe)
1023 xfree(irg->caller_isbe);
1024 irg->caller_isbe = NULL;
1026 if (irg->callee_isbe)
1027 xfree(irg->callee_isbe);
1028 irg->callee_isbe = NULL;
1032 /* ----------------------------------------------------------------------------------- */
1033 /* Another algorithm to compute recursion nesting depth */
1034 /* Walk the callgraph. For each crossed edge increase the loop depth by the edge */
1035 /* weight. Assign graphs the maximal depth. */
1036 /* ----------------------------------------------------------------------------------- */
1038 static void compute_loop_depth(ir_graph *irg, void *env)
1040 int current_nesting = *(int *) env;
1041 int old_nesting = irg->callgraph_loop_depth;
1042 ir_visited_t old_visited = get_cg_irg_visited(irg);
1047 if (cg_irg_visited(irg)) return;
1049 mark_cg_irg_visited(irg);
1051 if (old_nesting < current_nesting)
1052 irg->callgraph_loop_depth = current_nesting;
1054 if (current_nesting > irp->max_callgraph_loop_depth)
1055 irp->max_callgraph_loop_depth = current_nesting;
1057 if ((old_visited +1 < get_cg_irg_visited(irg)) || /* not yet visited */
1058 (old_nesting < current_nesting)) { /* propagate larger nesting */
1059 /* Don't walk the graph, but a tree that is an unfolded graph. */
1060 n_callees = get_irg_n_callees(irg);
1061 for (i = 0; i < n_callees; i++) {
1062 ir_graph *m = get_irg_callee(irg, i);
1063 *(int *)env += get_irg_callee_loop_depth(irg, i);
1064 compute_loop_depth(m, env);
1065 *(int *)env -= get_irg_callee_loop_depth(irg, i);
1069 set_cg_irg_visited(irg, master_cg_visited-1);
1072 /* ------------------------------------------------------------------------------------ */
1073 /* Another algorithm to compute recursion nesting depth */
1074 /* Walk the callgraph. For each crossed loop increase the nesting depth by one. */
1075 /* Assign graphs the maximal nesting depth. Don't increase if passing loops more than */
1077 /* ------------------------------------------------------------------------------------ */
1080 /* For callees, we want to remember the Call nodes, too. */
1081 typedef struct ana_entry2 {
1082 ir_loop **loop_stack; /**< a stack of ir_loop entries */
1083 int tos; /**< the top of stack entry */
1084 int recursion_nesting;
1088 * push a loop entry on the stack
1090 static void push2(ana_entry2 *e, ir_loop *g)
1092 if (ARR_LEN(e->loop_stack) == e->tos) {
1093 ARR_APP1(ir_loop *, e->loop_stack, g);
1095 e->loop_stack[e->tos] = g;
1101 * returns the top of stack and pop it
1103 static ir_loop *pop2(ana_entry2 *e)
1105 return e->loop_stack[--e->tos];
1109 * check if a loop g in on the stack. Did not check the TOS.
1111 static int in_stack(ana_entry2 *e, ir_loop *g)
1114 for (i = e->tos-1; i >= 0; --i) {
1115 if (e->loop_stack[i] == g) return 1;
1120 static void compute_rec_depth(ir_graph *irg, void *env)
1122 ana_entry2 *e = (ana_entry2 *)env;
1123 ir_loop *l = irg->l;
1124 int depth, old_depth = irg->callgraph_recursion_depth;
1128 if (cg_irg_visited(irg))
1130 mark_cg_irg_visited(irg);
1132 /* -- compute and set the new nesting value -- */
1133 if ((l != irp->outermost_cg_loop) && !in_stack(e, l)) {
1135 e->recursion_nesting++;
1138 depth = e->recursion_nesting;
1140 if (old_depth < depth)
1141 irg->callgraph_recursion_depth = depth;
1143 if (depth > irp->max_callgraph_recursion_depth)
1144 irp->max_callgraph_recursion_depth = depth;
1146 /* -- spread the nesting value -- */
1147 if (depth == 0 || old_depth < depth) {
1148 /* Don't walk the graph, but a tree that is an unfolded graph.
1149 Therefore we unset the visited flag at the end. */
1150 n_callees = get_irg_n_callees(irg);
1151 for (i = 0; i < n_callees; ++i) {
1152 ir_graph *m = get_irg_callee(irg, i);
1153 compute_rec_depth(m, env);
1157 /* -- clean up -- */
1160 e->recursion_nesting--;
1162 set_cg_irg_visited(irg, master_cg_visited-1);
1166 /* ----------------------------------------------------------------------------------- */
1167 /* Another algorithm to compute the execution frequency of methods ignoring recursions. */
1168 /* Walk the callgraph. Ignore backedges. Use sum of execution frequencies of Call */
1169 /* nodes to evaluate a callgraph edge. */
1170 /* ----------------------------------------------------------------------------------- */
1172 /* Returns the method execution frequency of a graph. */
1173 double get_irg_method_execution_frequency(const ir_graph *irg)
1175 return irg->method_execution_frequency;
1179 * Increase the method execution frequency to freq if its current value is
1180 * smaller then this.
1182 static void set_irg_method_execution_frequency(ir_graph *irg, double freq)
1184 irg->method_execution_frequency = freq;
1186 if (irp->max_method_execution_frequency < freq)
1187 irp->max_method_execution_frequency = freq;
1190 static void compute_method_execution_frequency(ir_graph *irg, void *env)
1198 if (cg_irg_visited(irg))
1201 /* We need the values of all predecessors (except backedges).
1202 So they must be marked. Else we will reach the node through
1203 one of the unmarked ones. */
1204 n_callers = get_irg_n_callers(irg);
1205 for (i = 0; i < n_callers; ++i) {
1206 ir_graph *m = get_irg_caller(irg, i);
1207 if (is_irg_caller_backedge(irg, i))
1209 if (!cg_irg_visited(m)) {
1213 mark_cg_irg_visited(irg);
1215 /* Compute the new frequency. */
1218 for (i = 0; i < n_callers; i++) {
1219 if (! is_irg_caller_backedge(irg, i)) {
1220 double edge_freq = get_irg_caller_method_execution_frequency(irg, i);
1221 assert(edge_freq >= 0);
1228 /* A starting point: method only called from outside,
1229 or only backedges as predecessors. */
1233 set_irg_method_execution_frequency(irg, freq);
1236 n_callees = get_irg_n_callees(irg);
1237 for (i = 0; i < n_callees; ++i) {
1238 compute_method_execution_frequency(get_irg_callee(irg, i), NULL);
1243 /* ----------------------------------------------------------------------------------- */
1244 /* The recursion stuff driver. */
1245 /* ----------------------------------------------------------------------------------- */
1247 /* Compute the backedges that represent recursions. */
1248 void find_callgraph_recursions(void)
1251 struct obstack temp;
1255 /* -- compute the looptree. -- */
1257 /* The outermost graph. We start here. Then we start at all
1258 functions in irg list that are never called, then at the remaining
1259 unvisited ones. The third step is needed for functions that are not
1260 reachable from the outermost graph, but call themselves in a cycle. */
1261 assert(get_irp_main_irg());
1262 outermost_ir_graph = get_irp_main_irg();
1263 obstack_init(&temp);
1266 current_loop = NULL;
1267 new_loop(); /* sets current_loop */
1269 ++master_cg_visited;
1270 cgscc(outermost_ir_graph);
1271 n_irgs = get_irp_n_irgs();
1272 for (i = 0; i < n_irgs; ++i) {
1273 ir_graph *irg = get_irp_irg(i);
1274 if (!cg_irg_visited(irg) && get_irg_n_callers(irg) == 0)
1277 for (i = 0; i < n_irgs; ++i) {
1278 ir_graph *irg = get_irp_irg(i);
1279 if (!cg_irg_visited(irg))
1282 obstack_free(&temp, NULL);
1284 irp->outermost_cg_loop = current_loop;
1285 mature_loops(current_loop, outermost_ir_graph->obst);
1287 /* -- Reverse the backedge information. -- */
1288 for (i = 0; i < n_irgs; ++i) {
1289 ir_graph *irg = get_irp_irg(i);
1290 int j, n_callees = get_irg_n_callees(irg);
1291 for (j = 0; j < n_callees; ++j) {
1292 if (is_irg_callee_backedge(irg, j))
1293 set_irg_caller_backedge(get_irg_callee(irg, j), irg);
1297 irp->callgraph_state = irp_callgraph_and_calltree_consistent;
1300 /* Compute interprocedural performance estimates. */
1301 void compute_performance_estimates(void)
1303 int i, n_irgs = get_irp_n_irgs();
1304 int current_nesting;
1307 assert(get_irp_exec_freq_state() != exec_freq_none && "execution frequency not calculated");
1309 /* -- compute the loop depth -- */
1310 current_nesting = 0;
1311 irp->max_callgraph_loop_depth = 0;
1312 master_cg_visited += 2;
1313 compute_loop_depth(get_irp_main_irg(), ¤t_nesting);
1314 for (i = 0; i < n_irgs; i++) {
1315 ir_graph *irg = get_irp_irg(i);
1316 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1317 get_irg_n_callers(irg) == 0) {
1318 compute_loop_depth(irg, ¤t_nesting);
1321 for (i = 0; i < n_irgs; i++) {
1322 ir_graph *irg = get_irp_irg(i);
1323 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1324 compute_loop_depth(irg, ¤t_nesting);
1329 /* -- compute the recursion depth -- */
1330 e.loop_stack = NEW_ARR_F(ir_loop *, 0);
1332 e.recursion_nesting = 0;
1334 irp->max_callgraph_recursion_depth = 0;
1336 master_cg_visited += 2;
1337 compute_rec_depth(get_irp_main_irg(), &e);
1338 for (i = 0; i < n_irgs; i++) {
1339 ir_graph *irg = get_irp_irg(i);
1340 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1341 get_irg_n_callers(irg) == 0) {
1342 compute_rec_depth(irg, &e);
1345 for (i = 0; i < n_irgs; i++) {
1346 ir_graph *irg = get_irp_irg(i);
1347 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1348 compute_rec_depth(irg, &e);
1352 DEL_ARR_F(e.loop_stack);
1354 /* -- compute the execution frequency -- */
1355 irp->max_method_execution_frequency = 0;
1356 master_cg_visited += 2;
1357 assert(get_irg_n_callers(get_irp_main_irg()) == 0);
1358 compute_method_execution_frequency(get_irp_main_irg(), NULL);
1359 for (i = 0; i < n_irgs; i++) {
1360 ir_graph *irg = get_irp_irg(i);
1361 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1362 get_irg_n_callers(irg) == 0) {
1363 compute_method_execution_frequency(irg, NULL);
1366 for (i = 0; i < n_irgs; i++) {
1367 ir_graph *irg = get_irp_irg(i);
1368 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1369 compute_method_execution_frequency(irg, NULL);
1374 /* Returns the maximal loop depth of all paths from an external visible method to
1376 int get_irg_loop_depth(const ir_graph *irg)
1378 assert(irp->callgraph_state == irp_callgraph_consistent ||
1379 irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1380 return irg->callgraph_loop_depth;
1383 /* Returns the maximal recursion depth of all paths from an external visible method to
1385 int get_irg_recursion_depth(const ir_graph *irg)
1387 assert(irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1388 return irg->callgraph_recursion_depth;
1391 /* Computes the interprocedural loop nesting information. */
1392 void analyse_loop_nesting_depth(void)
1394 ir_entity **free_methods = NULL;
1397 /* establish preconditions. */
1398 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
1399 cgana(&arr_len, &free_methods);
1402 if (irp_callgraph_consistent != get_irp_callgraph_state()) {
1403 compute_callgraph();
1406 find_callgraph_recursions();
1408 compute_performance_estimates();
1410 set_irp_loop_nesting_depth_state(loop_nesting_depth_consistent);
1413 loop_nesting_depth_state get_irp_loop_nesting_depth_state(void)
1415 return irp->lnd_state;
1417 void set_irp_loop_nesting_depth_state(loop_nesting_depth_state s)
1421 void set_irp_loop_nesting_depth_state_inconsistent(void)
1423 if (irp->lnd_state == loop_nesting_depth_consistent)
1424 irp->lnd_state = loop_nesting_depth_inconsistent;