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);
52 static inline void set_cg_irg_visited (ir_graph *n, ir_visited_t i);
54 /** Returns the callgraph state of the program representation. */
55 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) {
61 irp->callgraph_state = s;
64 /* Returns the number of procedures that call the given irg. */
65 int get_irg_n_callers(const ir_graph *irg) {
66 if (irg->callers) return ARR_LEN(irg->callers);
70 /* Returns the caller at position pos. */
71 ir_graph *get_irg_caller(const ir_graph *irg, int pos) {
72 assert(pos >= 0 && pos < get_irg_n_callers(irg));
73 if (irg->callers) return irg->callers[pos];
77 /* Returns non-zero if the caller at position pos is "a backedge", i.e. a recursion. */
78 int is_irg_caller_backedge(const ir_graph *irg, int pos) {
79 assert(pos >= 0 && pos < get_irg_n_callers(irg));
80 return irg->caller_isbe != NULL ? rbitset_is_set(irg->caller_isbe, pos) : 0;
83 /** Search the caller in the list of all callers and set it's backedge property. */
84 static void set_irg_caller_backedge(ir_graph *irg, ir_graph *caller) {
85 int i, n_callers = get_irg_n_callers(irg);
87 /* allocate a new array on demand */
88 if (irg->caller_isbe == NULL)
89 irg->caller_isbe = rbitset_malloc(n_callers);
90 for (i = 0; i < n_callers; ++i) {
91 if (get_irg_caller(irg, i) == caller) {
92 rbitset_set(irg->caller_isbe, i);
98 /* Returns non-zero if the irg has a backedge caller. */
99 int has_irg_caller_backedge(const ir_graph *irg) {
100 int i, n_callers = get_irg_n_callers(irg);
102 if (irg->caller_isbe != NULL) {
103 for (i = 0; i < n_callers; ++i)
104 if (rbitset_is_set(irg->caller_isbe, i))
111 * Find the reversion position of a caller.
112 * Given the position pos_caller of an caller of irg, return
113 * irg's callee position on that caller.
115 static int reverse_pos(const ir_graph *callee, int pos_caller) {
116 ir_graph *caller = get_irg_caller(callee, pos_caller);
117 /* search the other relation for the corresponding edge. */
119 int i, n_callees = get_irg_n_callees(caller);
120 for (i = 0; i < n_callees; ++i) {
121 if (get_irg_callee(caller, i) == callee) {
127 assert(pos_callee >= 0);
132 /* Returns the maximal loop depth of call nodes that call along this edge. */
133 int get_irg_caller_loop_depth(const ir_graph *irg, int pos) {
134 ir_graph *caller = get_irg_caller(irg, pos);
135 int pos_callee = reverse_pos(irg, pos);
137 return get_irg_callee_loop_depth(caller, pos_callee);
141 /* Returns the number of procedures that are called by the given irg. */
142 int get_irg_n_callees(const ir_graph *irg) {
143 if (irg->callees) return ARR_LEN(irg->callees);
147 /* Returns the callee at position pos. */
148 ir_graph *get_irg_callee(const ir_graph *irg, int pos) {
149 assert(pos >= 0 && pos < get_irg_n_callees(irg));
150 if (irg->callees) return irg->callees[pos]->irg;
154 /* Returns non-zero if the callee at position pos is "a backedge", i.e. a recursion. */
155 int is_irg_callee_backedge(const ir_graph *irg, int pos) {
156 assert(pos >= 0 && pos < get_irg_n_callees(irg));
157 return irg->callee_isbe != NULL ? rbitset_is_set(irg->callee_isbe, pos) : 0;
160 /* Returns non-zero if the irg has a backedge callee. */
161 int has_irg_callee_backedge(const ir_graph *irg) {
162 int i, n_callees = get_irg_n_callees(irg);
164 if (irg->callee_isbe != NULL) {
165 for (i = 0; i < n_callees; ++i)
166 if (rbitset_is_set(irg->callee_isbe, i))
173 * Mark the callee at position pos as a backedge.
175 static void set_irg_callee_backedge(ir_graph *irg, int pos) {
176 int n = get_irg_n_callees(irg);
178 /* allocate a new array on demand */
179 if (irg->callee_isbe == NULL)
180 irg->callee_isbe = rbitset_malloc(n);
181 assert(pos >= 0 && pos < n);
182 rbitset_set(irg->callee_isbe, pos);
185 /* Returns the maximal loop depth of call nodes that call along this edge. */
186 int get_irg_callee_loop_depth(const ir_graph *irg, int pos) {
187 assert(pos >= 0 && pos < get_irg_n_callees(irg));
188 if (irg->callees) return irg->callees[pos]->max_depth;
193 double get_irg_callee_execution_frequency(const ir_graph *irg, int pos) {
194 ir_node **arr = irg->callees[pos]->call_list;
195 int i, n_Calls = ARR_LEN(arr);
198 for (i = 0; i < n_Calls; ++i) {
199 freq += get_irn_exec_freq(arr[i]);
204 double get_irg_callee_method_execution_frequency(const ir_graph *irg, int pos) {
205 double call_freq = get_irg_callee_execution_frequency(irg, pos);
206 double meth_freq = get_irg_method_execution_frequency(irg);
207 return call_freq * meth_freq;
211 double get_irg_caller_method_execution_frequency(const ir_graph *irg, int pos) {
212 ir_graph *caller = get_irg_caller(irg, pos);
213 int pos_callee = reverse_pos(irg, pos);
215 return get_irg_callee_method_execution_frequency(caller, pos_callee);
220 /* --------------------- Compute the callgraph ------------------------ */
223 * Walker called by compute_callgraph(), analyses all Call nodes.
225 static void ana_Call(ir_node *n, void *env) {
230 if (! is_Call(n)) return;
232 irg = get_irn_irg(n);
233 n_callees = get_Call_n_callees(n);
234 for (i = 0; i < n_callees; ++i) {
235 ir_entity *callee_e = get_Call_callee(n, i);
236 ir_graph *callee = get_entity_irg(callee_e);
240 cg_callee_entry *found;
245 pset_insert((pset *)callee->callers, irg, HASH_PTR(irg));
246 found = pset_find((pset *)irg->callees, &buf, HASH_PTR(callee));
247 if (found) { /* add Call node to list, compute new nesting. */
248 ir_node **arr = found->call_list;
249 ARR_APP1(ir_node *, arr, n);
250 found->call_list = arr;
251 } else { /* New node, add Call node and init nesting. */
252 found = OALLOC(irg->obst, cg_callee_entry);
254 found->call_list = NEW_ARR_F(ir_node *, 1);
255 found->call_list[0] = n;
256 found->max_depth = 0;
257 pset_insert((pset *)irg->callees, found, HASH_PTR(callee));
259 depth = get_loop_depth(get_irn_loop(get_nodes_block(n)));
260 found->max_depth = (depth > found->max_depth) ? depth : found->max_depth;
265 /** compare two ir graphs in a cg_callee_entry */
266 static int cg_callee_entry_cmp(const void *elt, const void *key) {
267 const cg_callee_entry *e1 = elt;
268 const cg_callee_entry *e2 = key;
269 return e1->irg != e2->irg;
272 /** compare two ir graphs for pointer identity */
273 static int graph_cmp(const void *elt, const void *key) {
274 const ir_graph *e1 = elt;
275 const ir_graph *e2 = key;
280 /* Construct and destruct the callgraph. */
281 void compute_callgraph(void) {
284 #ifdef INTERPROCEDURAL_VIEW
285 assert(! get_interprocedural_view()); /* Else walking will not reach the Call nodes. */
291 n_irgs = get_irp_n_irgs();
292 for (i = 0; i < n_irgs; ++i) {
293 ir_graph *irg = get_irp_irg(i);
294 assert(get_irg_callee_info_state(irg) == irg_callee_info_consistent);
295 irg->callees = (cg_callee_entry **)new_pset(cg_callee_entry_cmp, 8);
296 irg->callers = (ir_graph **)new_pset(graph_cmp, 8);
297 //construct_cf_backedges(irg);
300 /* Compute the call graph */
301 for (i = 0; i < n_irgs; ++i) {
302 ir_graph *irg = get_irp_irg(i);
303 construct_cf_backedges(irg); // We also find the maximal loop depth of a call.
304 irg_walk_graph(irg, ana_Call, NULL, NULL);
307 /* Change the sets to arrays. */
308 for (i = 0; i < n_irgs; ++i) {
310 cg_callee_entry *callee;
311 ir_graph *c, *irg = get_irp_irg(i);
312 pset *callee_set, *caller_set;
314 callee_set = (pset *)irg->callees;
315 count = pset_count(callee_set);
316 irg->callees = NEW_ARR_F(cg_callee_entry *, count);
317 irg->callee_isbe = NULL;
318 callee = pset_first(callee_set);
319 for (j = 0; j < count; ++j) {
320 irg->callees[j] = callee;
321 callee = pset_next(callee_set);
323 del_pset(callee_set);
324 assert(callee == NULL);
326 caller_set = (pset *)irg->callers;
327 count = pset_count(caller_set);
328 irg->callers = NEW_ARR_F(ir_graph *, count);
329 irg->caller_isbe = NULL;
330 c = pset_first(caller_set);
331 for (j = 0; j < count; ++j) {
333 c = pset_next(caller_set);
335 del_pset(caller_set);
338 set_irp_callgraph_state(irp_callgraph_consistent);
341 /* Destruct the callgraph. */
342 void free_callgraph(void) {
343 int i, n_irgs = get_irp_n_irgs();
344 for (i = 0; i < n_irgs; ++i) {
345 ir_graph *irg = get_irp_irg(i);
346 if (irg->callees) DEL_ARR_F(irg->callees);
347 if (irg->callers) DEL_ARR_F(irg->callers);
348 if (irg->callee_isbe) free(irg->callee_isbe);
349 if (irg->caller_isbe) free(irg->caller_isbe);
352 irg->callee_isbe = NULL;
353 irg->caller_isbe = NULL;
355 set_irp_callgraph_state(irp_callgraph_none);
358 /* ----------------------------------------------------------------------------------- */
359 /* A walker for the callgraph */
360 /* ----------------------------------------------------------------------------------- */
363 static void do_walk(ir_graph *irg, callgraph_walk_func *pre, callgraph_walk_func *post, void *env) {
366 if (cg_irg_visited(irg))
368 mark_cg_irg_visited(irg);
373 n_callees = get_irg_n_callees(irg);
374 for (i = 0; i < n_callees; i++) {
375 ir_graph *m = get_irg_callee(irg, i);
376 do_walk(m, pre, post, env);
383 void callgraph_walk(callgraph_walk_func *pre, callgraph_walk_func *post, void *env) {
384 int i, n_irgs = get_irp_n_irgs();
387 /* roots are methods which have no callers in the current program */
388 for (i = 0; i < n_irgs; ++i) {
389 ir_graph *irg = get_irp_irg(i);
391 if (get_irg_n_callers(irg) == 0)
392 do_walk(irg, pre, post, env);
395 /* in case of unreachable call loops we haven't visited some irgs yet */
396 for (i = 0; i < n_irgs; i++) {
397 ir_graph *irg = get_irp_irg(i);
398 do_walk(irg, pre, post, env);
402 /* ----------------------------------------------------------------------------------- */
403 /* loop construction algorithm */
404 /* ----------------------------------------------------------------------------------- */
406 static ir_graph *outermost_ir_graph; /**< The outermost graph the scc is computed
408 static ir_loop *current_loop; /**< Current cfloop construction is working
410 static int loop_node_cnt = 0; /**< Counts the number of allocated cfloop nodes.
411 Each cfloop node gets a unique number.
412 What for? ev. remove. @@@ */
413 static int current_dfn = 1; /**< Counter to generate depth first numbering
416 /*-----------------*/
417 /* Node attributes */
418 /*-----------------*/
420 typedef struct scc_info {
421 int in_stack; /**< Marks whether node is on the stack. */
422 int dfn; /**< Depth first search number. */
423 int uplink; /**< dfn number of ancestor. */
428 * allocates a new scc_info on the obstack
430 static inline scc_info *new_scc_info(struct obstack *obst) {
431 return OALLOCZ(obst, scc_info);
435 * Returns non-zero if a graph was already visited.
437 static inline int cg_irg_visited(ir_graph *irg) {
438 return irg->self_visited >= master_cg_visited;
442 * Marks a graph as visited.
444 static inline void mark_cg_irg_visited(ir_graph *irg) {
445 irg->self_visited = master_cg_visited;
449 * Set a graphs visited flag to i.
451 static inline void set_cg_irg_visited(ir_graph *irg, ir_visited_t i) {
452 irg->self_visited = i;
456 * Returns the visited flag of a graph.
458 static inline ir_visited_t get_cg_irg_visited(ir_graph *irg) {
459 return irg->self_visited;
462 static inline void mark_irg_in_stack(ir_graph *irg) {
463 scc_info *info = get_irg_link(irg);
464 assert(info && "missing call to init_scc()");
468 static inline void mark_irg_not_in_stack(ir_graph *irg) {
469 scc_info *info = get_irg_link(irg);
470 assert(info && "missing call to init_scc()");
474 static inline int irg_is_in_stack(ir_graph *irg) {
475 scc_info *info = get_irg_link(irg);
476 assert(info && "missing call to init_scc()");
477 return info->in_stack;
480 static inline void set_irg_uplink(ir_graph *irg, int uplink) {
481 scc_info *info = get_irg_link(irg);
482 assert(info && "missing call to init_scc()");
483 info->uplink = uplink;
486 static inline int get_irg_uplink(ir_graph *irg) {
487 scc_info *info = get_irg_link(irg);
488 assert(info && "missing call to init_scc()");
492 static inline void set_irg_dfn(ir_graph *irg, int dfn) {
493 scc_info *info = get_irg_link(irg);
494 assert(info && "missing call to init_scc()");
498 static inline int get_irg_dfn(ir_graph *irg) {
499 scc_info *info = get_irg_link(irg);
500 assert(info && "missing call to init_scc()");
504 /**********************************************************************/
506 /**********************************************************************/
508 static ir_graph **stack = NULL;
509 static int tos = 0; /**< top of stack */
512 * Initialize the irg stack.
514 static inline void init_stack(void) {
516 ARR_RESIZE(ir_graph *, stack, 1000);
518 stack = NEW_ARR_F(ir_graph *, 1000);
524 * push a graph on the irg stack
525 * @param n the graph to be pushed
527 static inline void push(ir_graph *irg) {
528 if (tos == ARR_LEN(stack)) {
529 int nlen = ARR_LEN(stack) * 2;
530 ARR_RESIZE(ir_node *, stack, nlen);
533 mark_irg_in_stack(irg);
537 * return the topmost graph on the stack and pop it
539 static inline ir_graph *pop(void) {
540 ir_graph *irg = stack[--tos];
541 mark_irg_not_in_stack(irg);
546 * The nodes up to irg belong to the current loop.
547 * Removes them from the stack and adds them to the current loop.
549 static inline void pop_scc_to_loop(ir_graph *irg) {
555 set_irg_dfn(m, loop_node_cnt);
556 add_loop_irg(current_loop, m);
558 //m->callgraph_loop_depth = current_loop->depth;
562 /* GL ??? my last son is my grandson??? Removes cfloops with no
563 ir_nodes in them. Such loops have only another loop as son. (Why
564 can't they have two loops as sons? Does it never get that far? ) */
565 static void close_loop(ir_loop *l) {
566 int last = get_loop_n_elements(l) - 1;
567 loop_element lelement = get_loop_element(l, last);
568 ir_loop *last_son = lelement.son;
570 if (get_kind(last_son) == k_ir_loop &&
571 get_loop_n_elements(last_son) == 1) {
574 lelement = get_loop_element(last_son, 0);
576 if (get_kind(gson) == k_ir_loop) {
577 loop_element new_last_son;
579 gson->outer_loop = l;
580 new_last_son.son = gson;
581 l->children[last] = new_last_son;
588 * Removes and unmarks all nodes up to n from the stack.
589 * The nodes must be visited once more to assign them to a scc.
591 static inline void pop_scc_unmark_visit(ir_graph *n) {
596 set_cg_irg_visited(m, 0);
600 /**********************************************************************/
601 /* The loop data structure. **/
602 /**********************************************************************/
605 * Allocates a new loop as son of current_loop. Sets current_loop
606 * to the new loop and returns the father.
608 static ir_loop *new_loop(void) {
609 ir_loop *father = current_loop;
610 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
617 /**********************************************************************/
618 /* Constructing and destructing the loop/backedge information. **/
619 /**********************************************************************/
621 /* Initialization steps. **********************************************/
623 static void init_scc(struct obstack *obst) {
631 n_irgs = get_irp_n_irgs();
632 for (i = 0; i < n_irgs; ++i) {
633 ir_graph *irg = get_irp_irg(i);
634 set_irg_link(irg, new_scc_info(obst));
635 irg->callgraph_recursion_depth = 0;
636 irg->callgraph_loop_depth = 0;
640 /** Returns non-zero if n is a loop header, i.e., it is a Block node
641 * and has predecessors within the cfloop and out of the cfloop.
643 * @param root: only needed for assertion.
645 static int is_head(ir_graph *n, ir_graph *root) {
647 int some_outof_loop = 0, some_in_loop = 0;
649 arity = get_irg_n_callees(n);
650 for (i = 0; i < arity; i++) {
651 ir_graph *pred = get_irg_callee(n, i);
652 if (is_irg_callee_backedge(n, i)) continue;
653 if (!irg_is_in_stack(pred)) {
656 if (get_irg_uplink(pred) < get_irg_uplink(root)) {
657 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
663 return some_outof_loop & some_in_loop;
667 * Returns non-zero if n is possible loop head of an endless loop.
668 * I.e., it is a Block, Phi or Filter node and has only predecessors
670 * @arg root: only needed for assertion.
672 static int is_endless_head(ir_graph *n, ir_graph *root)
675 int some_outof_loop = 0, some_in_loop = 0;
677 arity = get_irg_n_callees(n);
678 for (i = 0; i < arity; i++) {
679 ir_graph *pred = get_irg_callee(n, i);
681 if (is_irg_callee_backedge(n, i))
683 if (!irg_is_in_stack(pred)) {
686 if (get_irg_uplink(pred) < get_irg_uplink(root)) {
687 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
692 return !some_outof_loop & some_in_loop;
695 #ifdef INTERPROCEDURAL_VIEW
697 * Check whether there is a parallel edge in the ip control flow.
700 static int is_ip_head(ir_graph *n, ir_graph *pred)
704 int iv_rem = get_interprocedural_view();
705 set_interprocedural_view(1);
707 ir_node *sblock = get_irg_start_block(n);
708 int i, arity = get_Block_n_cfgpreds(sblock);
710 //printf(" edge from "); DDMG(n);
711 //printf(" to pred "); DDMG(pred);
712 //printf(" sblock "); DDMN(sblock);
714 for (i = 0; i < arity; i++) {
715 ir_node *pred_cfop = skip_Proj(get_Block_cfgpred(sblock, i));
716 //printf(" "); DDMN(pred_cfop);
717 if (is_CallBegin(pred_cfop)) { /* could be Unknown */
718 ir_graph *ip_pred = get_irn_irg(pred_cfop);
719 //printf(" "); DDMG(ip_pred);
720 if ((ip_pred == pred) && is_backedge(sblock, i)) {
721 //printf(" found\n");
727 set_interprocedural_view(iv_rem);
730 #endif /* INTERPROCEDURAL_VIEW */
733 * Returns index of the predecessor with the smallest dfn number
734 * greater-equal than limit.
736 static int smallest_dfn_pred(ir_graph *n, int limit)
738 int i, index = -2, min = -1;
740 int arity = get_irg_n_callees(n);
741 for (i = 0; i < arity; i++) {
742 ir_graph *pred = get_irg_callee(n, i);
743 if (is_irg_callee_backedge(n, i) || !irg_is_in_stack(pred))
745 if (get_irg_dfn(pred) >= limit && (min == -1 || get_irg_dfn(pred) < min)) {
747 min = get_irg_dfn(pred);
754 /** Returns index of the predecessor with the largest dfn number. */
755 static int largest_dfn_pred(ir_graph *n) {
756 int i, index = -2, max = -1;
758 int arity = get_irg_n_callees(n);
759 for (i = 0; i < arity; ++i) {
760 ir_graph *pred = get_irg_callee(n, i);
761 if (is_irg_callee_backedge (n, i) || !irg_is_in_stack(pred)) continue;
762 if (get_irg_dfn(pred) > max) {
764 max = get_irg_dfn(pred);
771 #ifndef INTERPROCEDURAL_VIEW
772 static ir_graph *find_tail(ir_graph *n) {
774 int i, res_index = -2;
777 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
779 m = stack[tos-1]; /* tos = top of stack */
780 if (is_head (m, n)) {
781 res_index = smallest_dfn_pred(m, 0);
782 if ((res_index == -2) && /* no smallest dfn pred found. */
786 if (m == n) return NULL; // Is this to catch Phi - self loops?
787 for (i = tos-2; i >= 0; --i) {
791 res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
792 if (res_index == -2) /* no smallest dfn pred found. */
793 res_index = largest_dfn_pred(m);
795 if ((m == n) && (res_index == -2)) {
801 /* We should not walk past our selves on the stack: The upcoming nodes
802 are not in this loop. We assume a loop not reachable from Start. */
811 /* A dead loop not reachable from Start. */
812 for (i = tos-2; i >= 0; --i) {
814 if (is_endless_head(m, n)) {
815 res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
816 if (res_index == -2) /* no smallest dfn pred found. */
817 res_index = largest_dfn_pred(m);
820 if (m == n) { break; } /* It's not an unreachable loop, either. */
822 //assert(0 && "no head found on stack");
826 assert (res_index > -2);
828 set_irg_callee_backedge(m, res_index);
829 return get_irg_callee(m, res_index);
832 static ir_graph *find_tail(ir_graph *n) {
834 int i, res_index = -2;
837 ir_graph *in_and_out = NULL;
838 ir_graph *only_in = NULL;
839 ir_graph *ip_in_and_out = NULL;
840 ir_graph *ip_only_in = NULL;
842 //printf("find tail for "); DDMG(n);
844 for (i = tos-1; i >= 0; --i) {
845 ir_graph *pred = (i < tos -1) ? stack[i+1] : n;
849 //printf(" found 1a! "); DDM;
851 if (is_ip_head(pred, m)) {
852 //printf(" found 1b! "); DDM;
855 } else if (!ip_only_in && is_endless_head(m, n)) {
857 //printf(" found 2a! "); DDM;
858 if (is_ip_head(pred, m)) {
859 //printf(" found 2b! "); DDM;
862 } else if (is_ip_head(pred, m)) {
863 //printf(" found 3! "); DDM; This happens for self recursions in the second
864 //assert(0); scc iteration (the one to flip the loop.)
867 if (ip_in_and_out) break; /* That's what we really want. */
869 if (m == n) break; /* Don't walk past n on the stack! */
873 if (!in_and_out && !only_in)
874 /* There is no loop */
878 /* Is there a head in the callgraph without a head in the
880 assert(in_and_out || only_in);
882 m = (ip_in_and_out) ? ip_in_and_out : ip_only_in;
885 m = (in_and_out) ? in_and_out : only_in;
887 //printf("*** head is "); DDMG(m);
889 res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
890 if (res_index == -2) /* no smallest dfn pred found. */
891 res_index = largest_dfn_pred(m);
893 set_irg_callee_backedge(m, res_index);
894 res = get_irg_callee(m, res_index);
895 //printf("*** tail is "); DDMG(res);
898 #endif /* INTERPROCEDURAL_VIEW */
900 /*-----------------------------------------------------------*
901 * The core algorithm. *
902 *-----------------------------------------------------------*/
905 static void cgscc(ir_graph *n) {
908 if (cg_irg_visited(n)) return;
909 mark_cg_irg_visited(n);
911 /* Initialize the node */
912 set_irg_dfn(n, current_dfn); /* Depth first number for this node */
913 set_irg_uplink(n, current_dfn); /* ... is default uplink. */
917 arity = get_irg_n_callees(n);
918 for (i = 0; i < arity; i++) {
920 if (is_irg_callee_backedge(n, i)) continue;
921 m = get_irg_callee(n, i);
923 /** This marks the backedge, but does it guarantee a correct loop tree? */
924 //if (m == n) { set_irg_callee_backedge(n, i); continue; }
927 if (irg_is_in_stack(m)) {
928 /* Uplink of m is smaller if n->m is a backedge.
929 Propagate the uplink to mark the cfloop. */
930 if (get_irg_uplink(m) < get_irg_uplink(n))
931 set_irg_uplink(n, get_irg_uplink(m));
935 if (get_irg_dfn(n) == get_irg_uplink(n)) {
936 /* This condition holds for
937 1) the node with the incoming backedge.
938 That is: We found a cfloop!
939 2) Straight line code, because no uplink has been propagated, so the
940 uplink still is the same as the dfn.
942 But n might not be a proper cfloop head for the analysis. Proper cfloop
943 heads are Block and Phi nodes. find_tail searches the stack for
944 Block's and Phi's and takes those nodes as cfloop heads for the current
945 cfloop instead and marks the incoming edge as backedge. */
947 ir_graph *tail = find_tail(n);
949 /* We have a cfloop, that is no straight line code,
950 because we found a cfloop head!
951 Next actions: Open a new cfloop on the cfloop tree and
952 try to find inner cfloops */
955 ir_loop *l = new_loop();
957 /* Remove the cfloop from the stack ... */
958 pop_scc_unmark_visit(n);
960 /* The current backedge has been marked, that is temporarily eliminated,
961 by find tail. Start the scc algorithm
962 anew on the subgraph thats left (the current cfloop without the backedge)
963 in order to find more inner cfloops. */
967 assert(cg_irg_visited(n));
977 * reset the backedge information for all callers in all irgs
979 static void reset_isbe(void) {
980 int i, n_irgs = get_irp_n_irgs();
982 for (i = 0; i < n_irgs; ++i) {
983 ir_graph *irg = get_irp_irg(i);
985 if (irg->caller_isbe)
986 xfree(irg->caller_isbe);
987 irg->caller_isbe = NULL;
989 if (irg->callee_isbe)
990 xfree(irg->callee_isbe);
991 irg->callee_isbe = NULL;
995 /* ----------------------------------------------------------------------------------- */
996 /* Another algorithm to compute recursion nesting depth */
997 /* Walk the callgraph. For each crossed edge increase the loop depth by the edge */
998 /* weight. Assign graphs the maximal depth. */
999 /* ----------------------------------------------------------------------------------- */
1001 static void compute_loop_depth(ir_graph *irg, void *env) {
1002 int current_nesting = *(int *) env;
1003 int old_nesting = irg->callgraph_loop_depth;
1004 ir_visited_t old_visited = get_cg_irg_visited(irg);
1009 if (cg_irg_visited(irg)) return;
1011 mark_cg_irg_visited(irg);
1013 //printf(" old: %d new %d master %d", old_visited, get_cg_irg_visited(irg), master_cg_visited); DDMG(irg);
1016 if (old_nesting < current_nesting)
1017 irg->callgraph_loop_depth = current_nesting;
1019 if (current_nesting > irp->max_callgraph_loop_depth)
1020 irp->max_callgraph_loop_depth = current_nesting;
1022 if ((old_visited +1 < get_cg_irg_visited(irg)) || /* not yet visited */
1023 (old_nesting < current_nesting)) { /* propagate larger nesting */
1024 /* Don't walk the graph, but a tree that is an unfolded graph. */
1025 n_callees = get_irg_n_callees(irg);
1026 for (i = 0; i < n_callees; i++) {
1027 ir_graph *m = get_irg_callee(irg, i);
1028 *(int *)env += get_irg_callee_loop_depth(irg, i);
1029 compute_loop_depth(m, env);
1030 *(int *)env -= get_irg_callee_loop_depth(irg, i);
1034 set_cg_irg_visited(irg, master_cg_visited-1);
1037 /* ------------------------------------------------------------------------------------ */
1038 /* Another algorithm to compute recursion nesting depth */
1039 /* Walk the callgraph. For each crossed loop increase the nesting depth by one. */
1040 /* Assign graphs the maximal nesting depth. Don't increase if passing loops more than */
1042 /* ------------------------------------------------------------------------------------ */
1045 /* For callees, we want to remember the Call nodes, too. */
1046 typedef struct ana_entry2 {
1047 ir_loop **loop_stack; /**< a stack of ir_loop entries */
1048 int tos; /**< the top of stack entry */
1049 int recursion_nesting;
1053 * push a loop entry on the stack
1055 static void push2(ana_entry2 *e, ir_loop *g) {
1056 if (ARR_LEN(e->loop_stack) == e->tos) {
1057 ARR_APP1(ir_loop *, e->loop_stack, g);
1059 e->loop_stack[e->tos] = g;
1065 * returns the top of stack and pop it
1067 static ir_loop *pop2(ana_entry2 *e) {
1068 return e->loop_stack[--e->tos];
1072 * check if a loop g in on the stack. Did not check the TOS.
1074 static int in_stack(ana_entry2 *e, ir_loop *g) {
1076 for (i = e->tos-1; i >= 0; --i) {
1077 if (e->loop_stack[i] == g) return 1;
1082 static void compute_rec_depth(ir_graph *irg, void *env) {
1083 ana_entry2 *e = (ana_entry2 *)env;
1084 ir_loop *l = irg->l;
1085 int depth, old_depth = irg->callgraph_recursion_depth;
1089 if (cg_irg_visited(irg))
1091 mark_cg_irg_visited(irg);
1093 /* -- compute and set the new nesting value -- */
1094 if ((l != irp->outermost_cg_loop) && !in_stack(e, l)) {
1096 e->recursion_nesting++;
1099 depth = e->recursion_nesting;
1101 if (old_depth < depth)
1102 irg->callgraph_recursion_depth = depth;
1104 if (depth > irp->max_callgraph_recursion_depth)
1105 irp->max_callgraph_recursion_depth = depth;
1107 /* -- spread the nesting value -- */
1108 if (depth == 0 || old_depth < depth) {
1109 /* Don't walk the graph, but a tree that is an unfolded graph.
1110 Therefore we unset the visited flag at the end. */
1111 n_callees = get_irg_n_callees(irg);
1112 for (i = 0; i < n_callees; ++i) {
1113 ir_graph *m = get_irg_callee(irg, i);
1114 compute_rec_depth(m, env);
1118 /* -- clean up -- */
1121 e->recursion_nesting--;
1123 set_cg_irg_visited(irg, master_cg_visited-1);
1127 /* ----------------------------------------------------------------------------------- */
1128 /* Another algorithm to compute the execution frequency of methods ignoring recursions. */
1129 /* Walk the callgraph. Ignore backedges. Use sum of execution frequencies of Call */
1130 /* nodes to evaluate a callgraph edge. */
1131 /* ----------------------------------------------------------------------------------- */
1133 /* Returns the method execution frequency of a graph. */
1134 double get_irg_method_execution_frequency(const ir_graph *irg) {
1135 return irg->method_execution_frequency;
1139 * Increase the method execution frequency to freq if its current value is
1140 * smaller then this.
1142 static void set_irg_method_execution_frequency(ir_graph *irg, double freq) {
1143 irg->method_execution_frequency = freq;
1145 if (irp->max_method_execution_frequency < freq)
1146 irp->max_method_execution_frequency = freq;
1149 static void compute_method_execution_frequency(ir_graph *irg, void *env) {
1156 if (cg_irg_visited(irg))
1159 /* We need the values of all predecessors (except backedges).
1160 So they must be marked. Else we will reach the node through
1161 one of the unmarked ones. */
1162 n_callers = get_irg_n_callers(irg);
1163 for (i = 0; i < n_callers; ++i) {
1164 ir_graph *m = get_irg_caller(irg, i);
1165 if (is_irg_caller_backedge(irg, i))
1167 if (!cg_irg_visited(m)) {
1171 mark_cg_irg_visited(irg);
1173 /* Compute the new frequency. */
1176 for (i = 0; i < n_callers; i++) {
1177 if (! is_irg_caller_backedge(irg, i)) {
1178 double edge_freq = get_irg_caller_method_execution_frequency(irg, i);
1179 assert(edge_freq >= 0);
1186 /* A starting point: method only called from outside,
1187 or only backedges as predecessors. */
1191 set_irg_method_execution_frequency(irg, freq);
1194 n_callees = get_irg_n_callees(irg);
1195 for (i = 0; i < n_callees; ++i) {
1196 compute_method_execution_frequency(get_irg_callee(irg, i), NULL);
1201 /* ----------------------------------------------------------------------------------- */
1202 /* The recursion stuff driver. */
1203 /* ----------------------------------------------------------------------------------- */
1205 /* Compute the backedges that represent recursions. */
1206 void find_callgraph_recursions(void) {
1208 struct obstack temp;
1212 /* -- compute the looptree. -- */
1214 /* The outermost graph. We start here. Then we start at all
1215 functions in irg list that are never called, then at the remaining
1216 unvisited ones. The third step is needed for functions that are not
1217 reachable from the outermost graph, but call themselves in a cycle. */
1218 assert(get_irp_main_irg());
1219 outermost_ir_graph = get_irp_main_irg();
1220 obstack_init(&temp);
1223 current_loop = NULL;
1224 new_loop(); /* sets current_loop */
1226 ++master_cg_visited;
1227 cgscc(outermost_ir_graph);
1228 n_irgs = get_irp_n_irgs();
1229 for (i = 0; i < n_irgs; ++i) {
1230 ir_graph *irg = get_irp_irg(i);
1231 if (!cg_irg_visited(irg) && get_irg_n_callers(irg) == 0)
1234 for (i = 0; i < n_irgs; ++i) {
1235 ir_graph *irg = get_irp_irg(i);
1236 if (!cg_irg_visited(irg))
1239 obstack_free(&temp, NULL);
1241 irp->outermost_cg_loop = current_loop;
1242 mature_loops(current_loop, outermost_ir_graph->obst);
1244 /* -- Reverse the backedge information. -- */
1245 for (i = 0; i < n_irgs; ++i) {
1246 ir_graph *irg = get_irp_irg(i);
1247 int j, n_callees = get_irg_n_callees(irg);
1248 for (j = 0; j < n_callees; ++j) {
1249 if (is_irg_callee_backedge(irg, j))
1250 set_irg_caller_backedge(get_irg_callee(irg, j), irg);
1254 irp->callgraph_state = irp_callgraph_and_calltree_consistent;
1257 /* Compute interprocedural performance estimates. */
1258 void compute_performance_estimates(void) {
1259 int i, n_irgs = get_irp_n_irgs();
1260 int current_nesting;
1263 assert(get_irp_exec_freq_state() != exec_freq_none && "execution frequency not calculated");
1265 /* -- compute the loop depth -- */
1266 current_nesting = 0;
1267 irp->max_callgraph_loop_depth = 0;
1268 master_cg_visited += 2;
1269 //printf(" ** starting at "); DDMG(get_irp_main_irg());
1270 compute_loop_depth(get_irp_main_irg(), ¤t_nesting);
1271 for (i = 0; i < n_irgs; i++) {
1272 ir_graph *irg = get_irp_irg(i);
1273 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1274 get_irg_n_callers(irg) == 0) {
1275 compute_loop_depth(irg, ¤t_nesting);
1276 //printf(" ** starting at "); DDMG(irg);
1279 for (i = 0; i < n_irgs; i++) {
1280 ir_graph *irg = get_irp_irg(i);
1281 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1282 compute_loop_depth(irg, ¤t_nesting);
1283 //printf(" ** starting at "); DDMG(irg);
1288 /* -- compute the recursion depth -- */
1289 e.loop_stack = NEW_ARR_F(ir_loop *, 0);
1291 e.recursion_nesting = 0;
1293 irp->max_callgraph_recursion_depth = 0;
1295 master_cg_visited += 2;
1296 compute_rec_depth(get_irp_main_irg(), &e);
1297 //printf(" ++ starting at "); DDMG(get_irp_main_irg());
1298 for (i = 0; i < n_irgs; i++) {
1299 ir_graph *irg = get_irp_irg(i);
1300 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1301 get_irg_n_callers(irg) == 0) {
1302 compute_rec_depth(irg, &e);
1303 //printf(" ++ starting at "); DDMG(irg);
1306 for (i = 0; i < n_irgs; i++) {
1307 ir_graph *irg = get_irp_irg(i);
1308 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1309 compute_rec_depth(irg, &e);
1310 //printf(" ++ starting at "); DDMG(irg);
1314 DEL_ARR_F(e.loop_stack);
1316 /* -- compute the execution frequency -- */
1317 irp->max_method_execution_frequency = 0;
1318 master_cg_visited += 2;
1319 assert(get_irg_n_callers(get_irp_main_irg()) == 0);
1320 compute_method_execution_frequency(get_irp_main_irg(), NULL);
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 get_irg_n_callers(irg) == 0) {
1325 compute_method_execution_frequency(irg, NULL);
1328 for (i = 0; i < n_irgs; i++) {
1329 ir_graph *irg = get_irp_irg(i);
1330 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1331 compute_method_execution_frequency(irg, NULL);
1336 /* Returns the maximal loop depth of all paths from an external visible method to
1338 int get_irg_loop_depth(const ir_graph *irg) {
1339 assert(irp->callgraph_state == irp_callgraph_consistent ||
1340 irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1341 return irg->callgraph_loop_depth;
1344 /* Returns the maximal recursion depth of all paths from an external visible method to
1346 int get_irg_recursion_depth(const ir_graph *irg) {
1347 assert(irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1348 return irg->callgraph_recursion_depth;
1351 /* Computes the interprocedural loop nesting information. */
1352 void analyse_loop_nesting_depth(void) {
1353 ir_entity **free_methods = NULL;
1356 /* establish preconditions. */
1357 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
1358 cgana(&arr_len, &free_methods);
1361 if (irp_callgraph_consistent != get_irp_callgraph_state()) {
1362 compute_callgraph();
1365 find_callgraph_recursions();
1367 compute_performance_estimates();
1369 set_irp_loop_nesting_depth_state(loop_nesting_depth_consistent);
1372 loop_nesting_depth_state get_irp_loop_nesting_depth_state(void) {
1373 return irp->lnd_state;
1375 void set_irp_loop_nesting_depth_state(loop_nesting_depth_state s) {
1378 void set_irp_loop_nesting_depth_state_inconsistent(void) {
1379 if (irp->lnd_state == loop_nesting_depth_consistent)
1380 irp->lnd_state = loop_nesting_depth_inconsistent;