2 * Copyright (C) 1995-2011 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 size_t get_irg_n_callers(const ir_graph *irg)
69 return irg->callers ? ARR_LEN(irg->callers) : 0;
72 /* Returns the caller at position pos. */
73 ir_graph *get_irg_caller(const ir_graph *irg, size_t pos)
75 assert(pos < get_irg_n_callers(irg));
76 return irg->callers ? irg->callers[pos] : NULL;
79 /* Returns non-zero if the caller at position pos is "a backedge", i.e. a recursion. */
80 int is_irg_caller_backedge(const ir_graph *irg, size_t pos)
82 assert(pos < get_irg_n_callers(irg));
83 return irg->caller_isbe != NULL ? rbitset_is_set(irg->caller_isbe, pos) : 0;
86 /** Search the caller in the list of all callers and set it's backedge property. */
87 static void set_irg_caller_backedge(ir_graph *irg, const ir_graph *caller)
89 size_t i, n_callers = get_irg_n_callers(irg);
91 /* allocate a new array on demand */
92 if (irg->caller_isbe == NULL)
93 irg->caller_isbe = rbitset_malloc(n_callers);
94 for (i = 0; i < n_callers; ++i) {
95 if (get_irg_caller(irg, i) == caller) {
96 rbitset_set(irg->caller_isbe, i);
102 /* Returns non-zero if the irg has a backedge caller. */
103 int has_irg_caller_backedge(const ir_graph *irg)
105 size_t i, n_callers = get_irg_n_callers(irg);
107 if (irg->caller_isbe != NULL) {
108 for (i = 0; i < n_callers; ++i)
109 if (rbitset_is_set(irg->caller_isbe, i))
116 * Find the reversion position of a caller.
117 * Given the position pos_caller of an caller of irg, return
118 * irg's callee position on that caller.
120 static size_t reverse_pos(const ir_graph *callee, size_t pos_caller)
122 ir_graph *caller = get_irg_caller(callee, pos_caller);
123 /* search the other relation for the corresponding edge. */
125 size_t i, n_callees = get_irg_n_callees(caller);
126 for (i = 0; i < n_callees; ++i) {
127 if (get_irg_callee(caller, i) == callee) {
132 assert(!"reverse_pos() did not find position");
137 /* Returns the maximal loop depth of call nodes that call along this edge. */
138 size_t get_irg_caller_loop_depth(const ir_graph *irg, size_t pos)
140 ir_graph *caller = get_irg_caller(irg, pos);
141 size_t pos_callee = reverse_pos(irg, pos);
143 return get_irg_callee_loop_depth(caller, pos_callee);
147 /* Returns the number of procedures that are called by the given irg. */
148 size_t get_irg_n_callees(const ir_graph *irg)
150 assert(irg->callees);
151 return irg->callees ? ARR_LEN(irg->callees) : 0;
154 /* Returns the callee at position pos. */
155 ir_graph *get_irg_callee(const ir_graph *irg, size_t pos)
157 assert(pos < get_irg_n_callees(irg));
158 return irg->callees ? irg->callees[pos]->irg : NULL;
161 /* Returns non-zero if the callee at position pos is "a backedge", i.e. a recursion. */
162 int is_irg_callee_backedge(const ir_graph *irg, size_t pos)
164 assert(pos < get_irg_n_callees(irg));
165 return irg->callee_isbe != NULL ? rbitset_is_set(irg->callee_isbe, pos) : 0;
168 /* Returns non-zero if the irg has a backedge callee. */
169 int has_irg_callee_backedge(const ir_graph *irg)
171 size_t i, n_callees = get_irg_n_callees(irg);
173 if (irg->callee_isbe != NULL) {
174 for (i = 0; i < n_callees; ++i)
175 if (rbitset_is_set(irg->callee_isbe, i))
182 * Mark the callee at position pos as a backedge.
184 static void set_irg_callee_backedge(ir_graph *irg, size_t pos)
186 size_t n = get_irg_n_callees(irg);
188 /* allocate a new array on demand */
189 if (irg->callee_isbe == NULL)
190 irg->callee_isbe = rbitset_malloc(n);
192 rbitset_set(irg->callee_isbe, pos);
195 /* Returns the maximal loop depth of call nodes that call along this edge. */
196 size_t get_irg_callee_loop_depth(const ir_graph *irg, size_t pos)
198 assert(pos < get_irg_n_callees(irg));
199 return irg->callees ? irg->callees[pos]->max_depth : 0;
202 static double get_irg_callee_execution_frequency(const ir_graph *irg, size_t pos)
204 ir_node **arr = irg->callees[pos]->call_list;
205 size_t i, n_Calls = ARR_LEN(arr);
208 for (i = 0; i < n_Calls; ++i) {
209 freq += get_irn_exec_freq(arr[i]);
214 static double get_irg_callee_method_execution_frequency(const ir_graph *irg,
217 double call_freq = get_irg_callee_execution_frequency(irg, pos);
218 double meth_freq = get_irg_method_execution_frequency(irg);
219 return call_freq * meth_freq;
222 static double get_irg_caller_method_execution_frequency(const ir_graph *irg,
225 ir_graph *caller = get_irg_caller(irg, pos);
226 size_t pos_callee = reverse_pos(irg, pos);
228 return get_irg_callee_method_execution_frequency(caller, pos_callee);
232 /* --------------------- Compute the callgraph ------------------------ */
235 * Pre-Walker called by compute_callgraph(), analyses all Call nodes.
237 static void ana_Call(ir_node *n, void *env)
243 if (! is_Call(n)) return;
245 irg = get_irn_irg(n);
246 n_callees = get_Call_n_callees(n);
247 for (i = 0; i < n_callees; ++i) {
248 ir_entity *callee_e = get_Call_callee(n, i);
249 ir_graph *callee = get_entity_irg(callee_e);
253 cg_callee_entry *found;
258 pset_insert((pset *)callee->callers, irg, HASH_PTR(irg));
259 found = (cg_callee_entry*) pset_find((pset *)irg->callees, &buf, HASH_PTR(callee));
260 if (found) { /* add Call node to list, compute new nesting. */
261 ir_node **arr = found->call_list;
262 ARR_APP1(ir_node *, arr, n);
263 found->call_list = arr;
264 } else { /* New node, add Call node and init nesting. */
265 found = OALLOC(irg->obst, cg_callee_entry);
267 found->call_list = NEW_ARR_F(ir_node *, 1);
268 found->call_list[0] = n;
269 found->max_depth = 0;
270 pset_insert((pset *)irg->callees, found, HASH_PTR(callee));
272 depth = get_loop_depth(get_irn_loop(get_nodes_block(n)));
273 found->max_depth = (depth > found->max_depth) ? depth : found->max_depth;
278 /** compare two ir graphs in a cg_callee_entry */
279 static int cg_callee_entry_cmp(const void *elt, const void *key)
281 const cg_callee_entry *e1 = (const cg_callee_entry*) elt;
282 const cg_callee_entry *e2 = (const cg_callee_entry*) key;
283 return e1->irg != e2->irg;
286 /** compare two ir graphs for pointer identity */
287 static int graph_cmp(const void *elt, const void *key)
289 const ir_graph *e1 = (const ir_graph*) elt;
290 const ir_graph *e2 = (const ir_graph*) key;
295 /* Construct and destruct the callgraph. */
296 void compute_callgraph(void)
303 n_irgs = get_irp_n_irgs();
304 for (i = 0; i < n_irgs; ++i) {
305 ir_graph *irg = get_irp_irg(i);
306 assert(get_irg_callee_info_state(irg) == irg_callee_info_consistent);
307 irg->callees = (cg_callee_entry **)new_pset(cg_callee_entry_cmp, 8);
308 irg->callers = (ir_graph **)new_pset(graph_cmp, 8);
309 //construct_cf_backedges(irg);
312 /* Compute the call graph */
313 for (i = 0; i < n_irgs; ++i) {
314 ir_graph *irg = get_irp_irg(i);
315 construct_cf_backedges(irg); // We also find the maximal loop depth of a call.
316 irg_walk_graph(irg, ana_Call, NULL, NULL);
319 /* Change the sets to arrays. */
320 for (i = 0; i < n_irgs; ++i) {
322 cg_callee_entry *callee;
323 ir_graph *c, *irg = get_irp_irg(i);
324 pset *callee_set, *caller_set;
326 callee_set = (pset *)irg->callees;
327 count = pset_count(callee_set);
328 irg->callees = NEW_ARR_F(cg_callee_entry *, count);
329 irg->callee_isbe = NULL;
330 callee = (cg_callee_entry*) pset_first(callee_set);
331 for (j = 0; j < count; ++j) {
332 irg->callees[j] = callee;
333 callee = (cg_callee_entry*) pset_next(callee_set);
335 del_pset(callee_set);
336 assert(callee == NULL);
338 caller_set = (pset *)irg->callers;
339 count = pset_count(caller_set);
340 irg->callers = NEW_ARR_F(ir_graph *, count);
341 irg->caller_isbe = NULL;
342 c = (ir_graph*) pset_first(caller_set);
343 for (j = 0; j < count; ++j) {
345 c = (ir_graph*) pset_next(caller_set);
347 del_pset(caller_set);
350 set_irp_callgraph_state(irp_callgraph_consistent);
353 /* Destruct the callgraph. */
354 void free_callgraph(void)
356 size_t i, n_irgs = get_irp_n_irgs();
357 for (i = 0; i < n_irgs; ++i) {
358 ir_graph *irg = get_irp_irg(i);
359 if (irg->callees) DEL_ARR_F(irg->callees);
360 if (irg->callers) DEL_ARR_F(irg->callers);
361 if (irg->callee_isbe) free(irg->callee_isbe);
362 if (irg->caller_isbe) free(irg->caller_isbe);
365 irg->callee_isbe = NULL;
366 irg->caller_isbe = NULL;
368 set_irp_callgraph_state(irp_callgraph_none);
371 /* ----------------------------------------------------------------------------------- */
372 /* A walker for the callgraph */
373 /* ----------------------------------------------------------------------------------- */
376 static void do_walk(ir_graph *irg, callgraph_walk_func *pre, callgraph_walk_func *post, void *env)
380 if (cg_irg_visited(irg))
382 mark_cg_irg_visited(irg);
387 n_callees = get_irg_n_callees(irg);
388 for (i = 0; i < n_callees; i++) {
389 ir_graph *m = get_irg_callee(irg, i);
390 do_walk(m, pre, post, env);
397 void callgraph_walk(callgraph_walk_func *pre, callgraph_walk_func *post, void *env)
399 size_t i, n_irgs = get_irp_n_irgs();
402 /* roots are methods which have no callers in the current program */
403 for (i = 0; i < n_irgs; ++i) {
404 ir_graph *irg = get_irp_irg(i);
406 if (get_irg_n_callers(irg) == 0)
407 do_walk(irg, pre, post, env);
410 /* in case of unreachable call loops we haven't visited some irgs yet */
411 for (i = 0; i < n_irgs; i++) {
412 ir_graph *irg = get_irp_irg(i);
413 do_walk(irg, pre, post, env);
417 /* ----------------------------------------------------------------------------------- */
418 /* loop construction algorithm */
419 /* ----------------------------------------------------------------------------------- */
421 static ir_graph *outermost_ir_graph; /**< The outermost graph the scc is computed
423 static ir_loop *current_loop; /**< Current cfloop construction is working
425 static size_t loop_node_cnt = 0; /**< Counts the number of allocated cfloop nodes.
426 Each cfloop node gets a unique number.
427 What for? ev. remove. @@@ */
428 static size_t current_dfn = 1; /**< Counter to generate depth first numbering
431 /*-----------------*/
432 /* Node attributes */
433 /*-----------------*/
435 typedef struct scc_info {
436 size_t dfn; /**< Depth first search number. */
437 size_t uplink; /**< dfn number of ancestor. */
438 ir_visited_t visited; /**< visited counter */
439 int in_stack; /**< Marks whether node is on the stack. */
443 * allocates a new scc_info on the obstack
445 static inline scc_info *new_scc_info(struct obstack *obst)
447 return OALLOCZ(obst, scc_info);
451 * Returns non-zero if a graph was already visited.
453 static inline int cg_irg_visited(ir_graph *irg)
455 return irg->self_visited >= master_cg_visited;
459 * Marks a graph as visited.
461 static inline void mark_cg_irg_visited(ir_graph *irg)
463 irg->self_visited = master_cg_visited;
467 * Set a graphs visited flag to i.
469 static inline void set_cg_irg_visited(ir_graph *irg, ir_visited_t i)
471 irg->self_visited = i;
475 * Returns the visited flag of a graph.
477 static inline ir_visited_t get_cg_irg_visited(const ir_graph *irg)
479 return irg->self_visited;
482 static inline void mark_irg_in_stack(ir_graph *irg)
484 scc_info *info = (scc_info*) get_irg_link(irg);
485 assert(info && "missing call to init_scc()");
489 static inline void mark_irg_not_in_stack(ir_graph *irg)
491 scc_info *info = (scc_info*) get_irg_link(irg);
492 assert(info && "missing call to init_scc()");
496 static inline int irg_is_in_stack(const ir_graph *irg)
498 scc_info *info = (scc_info*) get_irg_link(irg);
499 assert(info && "missing call to init_scc()");
500 return info->in_stack;
503 static inline void set_irg_uplink(ir_graph *irg, size_t uplink)
505 scc_info *info = (scc_info*) get_irg_link(irg);
506 assert(info && "missing call to init_scc()");
507 info->uplink = uplink;
510 static inline size_t get_irg_uplink(const ir_graph *irg)
512 const scc_info *info = (scc_info*) get_irg_link(irg);
513 assert(info && "missing call to init_scc()");
517 static inline void set_irg_dfn(ir_graph *irg, size_t dfn)
519 scc_info *info = (scc_info*) get_irg_link(irg);
520 assert(info && "missing call to init_scc()");
524 static inline size_t get_irg_dfn(const ir_graph *irg)
526 const scc_info *info = (scc_info*) get_irg_link(irg);
527 assert(info && "missing call to init_scc()");
531 /**********************************************************************/
533 /**********************************************************************/
535 static ir_graph **stack = NULL;
536 static size_t tos = 0; /**< top of stack */
539 * Initialize the irg stack.
541 static inline void init_stack(void)
544 ARR_RESIZE(ir_graph *, stack, 1000);
546 stack = NEW_ARR_F(ir_graph *, 1000);
552 * push a graph on the irg stack
553 * @param n the graph to be pushed
555 static inline void push(ir_graph *irg)
557 if (tos == ARR_LEN(stack)) {
558 size_t nlen = ARR_LEN(stack) * 2;
559 ARR_RESIZE(ir_graph*, stack, nlen);
562 mark_irg_in_stack(irg);
566 * return the topmost graph on the stack and pop it
568 static inline ir_graph *pop(void)
574 mark_irg_not_in_stack(irg);
579 * The nodes up to irg belong to the current loop.
580 * Removes them from the stack and adds them to the current loop.
582 static inline void pop_scc_to_loop(ir_graph *irg)
589 set_irg_dfn(m, loop_node_cnt);
590 add_loop_irg(current_loop, m);
592 //m->callgraph_loop_depth = current_loop->depth;
596 /* GL ??? my last son is my grandson??? Removes cfloops with no
597 ir_nodes in them. Such loops have only another loop as son. (Why
598 can't they have two loops as sons? Does it never get that far? ) */
599 static void close_loop(ir_loop *l)
601 size_t last = get_loop_n_elements(l) - 1;
602 loop_element lelement = get_loop_element(l, last);
603 ir_loop *last_son = lelement.son;
605 if (get_kind(last_son) == k_ir_loop &&
606 get_loop_n_elements(last_son) == 1) {
609 lelement = get_loop_element(last_son, 0);
611 if (get_kind(gson) == k_ir_loop) {
612 loop_element new_last_son;
614 gson->outer_loop = l;
615 new_last_son.son = gson;
616 l->children[last] = new_last_son;
623 * Removes and unmarks all nodes up to n from the stack.
624 * The nodes must be visited once more to assign them to a scc.
626 static inline void pop_scc_unmark_visit(ir_graph *n)
632 set_cg_irg_visited(m, 0);
636 /**********************************************************************/
637 /* The loop data structure. **/
638 /**********************************************************************/
641 * Allocates a new loop as son of current_loop. Sets current_loop
642 * to the new loop and returns the father.
644 static ir_loop *new_loop(void)
646 ir_loop *father = current_loop;
647 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
654 /**********************************************************************/
655 /* Constructing and destructing the loop/backedge information. **/
656 /**********************************************************************/
658 /* Initialization steps. **********************************************/
660 static void init_scc(struct obstack *obst)
668 n_irgs = get_irp_n_irgs();
669 for (i = 0; i < n_irgs; ++i) {
670 ir_graph *irg = get_irp_irg(i);
671 set_irg_link(irg, new_scc_info(obst));
672 irg->callgraph_recursion_depth = 0;
673 irg->callgraph_loop_depth = 0;
677 /** Returns non-zero if n is a loop header, i.e., it is a Block node
678 * and has predecessors within the cfloop and out of the cfloop.
680 * @param root: only needed for assertion.
682 static int is_head(const ir_graph *n, const ir_graph *root)
685 int some_outof_loop = 0, some_in_loop = 0;
687 n_callees = get_irg_n_callees(n);
688 for (i = 0; i < n_callees; ++i) {
689 const ir_graph *pred = get_irg_callee(n, i);
690 if (is_irg_callee_backedge(n, i)) continue;
691 if (!irg_is_in_stack(pred)) {
694 if (get_irg_uplink(pred) < get_irg_uplink(root)) {
695 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
701 return some_outof_loop && some_in_loop;
705 * Returns non-zero if n is possible loop head of an endless loop.
706 * I.e., it is a Block or Phi node and has only predecessors
708 * @arg root: only needed for assertion.
710 static int is_endless_head(const ir_graph *n, const ir_graph *root)
713 int some_outof_loop = 0, some_in_loop = 0;
715 n_calless = get_irg_n_callees(n);
716 for (i = 0; i < n_calless; ++i) {
717 const ir_graph *pred = get_irg_callee(n, i);
719 if (is_irg_callee_backedge(n, i))
721 if (!irg_is_in_stack(pred)) {
724 if (get_irg_uplink(pred) < get_irg_uplink(root)) {
725 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
730 return !some_outof_loop && some_in_loop;
734 * Finds index of the predecessor with the smallest dfn number
735 * greater-equal than limit.
737 static bool smallest_dfn_pred(const ir_graph *n, size_t limit, size_t *result)
739 size_t index = 0, min = 0;
742 size_t i, n_callees = get_irg_n_callees(n);
743 for (i = 0; i < n_callees; ++i) {
744 const ir_graph *pred = get_irg_callee(n, i);
745 if (is_irg_callee_backedge(n, i) || !irg_is_in_stack(pred))
747 if (get_irg_dfn(pred) >= limit && (!found || get_irg_dfn(pred) < min)) {
749 min = get_irg_dfn(pred);
758 /** Finds index of the predecessor with the largest dfn number. */
759 static bool largest_dfn_pred(const ir_graph *n, size_t *result)
761 size_t index = 0, max = 0;
764 size_t i, n_callees = get_irg_n_callees(n);
765 for (i = 0; i < n_callees; ++i) {
766 const ir_graph *pred = get_irg_callee(n, i);
767 if (is_irg_callee_backedge (n, i) || !irg_is_in_stack(pred))
769 /* Note: dfn is always > 0 */
770 if (get_irg_dfn(pred) > max) {
772 max = get_irg_dfn(pred);
781 static ir_graph *find_tail(const ir_graph *n)
788 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
790 m = stack[tos - 1]; /* tos = top of stack */
792 found = smallest_dfn_pred(m, 0, &res_index);
793 if (!found && /* no smallest dfn pred found. */
797 if (m == n) return NULL; // Is this to catch Phi - self loops?
798 for (i = tos - 1; i > 0;) {
802 found = smallest_dfn_pred(m, get_irg_dfn(m) + 1, &res_index);
803 if (! found) /* no smallest dfn pred found. */
804 found = largest_dfn_pred(m, &res_index);
809 /* We should not walk past our selves on the stack: The upcoming nodes
810 are not in this loop. We assume a loop not reachable from Start. */
819 /* A dead loop not reachable from Start. */
820 for (i = tos-1; i > 0;) {
822 if (is_endless_head(m, n)) {
823 found = smallest_dfn_pred(m, get_irg_dfn(m) + 1, &res_index);
824 if (!found) /* no smallest dfn pred found. */
825 found = largest_dfn_pred(m, &res_index);
828 if (m == n) { break; } /* It's not an unreachable loop, either. */
830 //assert(0 && "no head found on stack");
836 set_irg_callee_backedge(m, res_index);
837 return get_irg_callee(m, res_index);
840 /*-----------------------------------------------------------*
841 * The core algorithm. *
842 *-----------------------------------------------------------*/
845 static void cgscc(ir_graph *n)
849 if (cg_irg_visited(n)) return;
850 mark_cg_irg_visited(n);
852 /* Initialize the node */
853 set_irg_dfn(n, current_dfn); /* Depth first number for this node */
854 set_irg_uplink(n, current_dfn); /* ... is default uplink. */
858 n_callees = get_irg_n_callees(n);
859 for (i = 0; i < n_callees; ++i) {
861 if (is_irg_callee_backedge(n, i)) continue;
862 m = get_irg_callee(n, i);
864 /** This marks the backedge, but does it guarantee a correct loop tree? */
865 //if (m == n) { set_irg_callee_backedge(n, i); continue; }
868 if (irg_is_in_stack(m)) {
869 /* Uplink of m is smaller if n->m is a backedge.
870 Propagate the uplink to mark the cfloop. */
871 if (get_irg_uplink(m) < get_irg_uplink(n))
872 set_irg_uplink(n, get_irg_uplink(m));
876 if (get_irg_dfn(n) == get_irg_uplink(n)) {
877 /* This condition holds for
878 1) the node with the incoming backedge.
879 That is: We found a cfloop!
880 2) Straight line code, because no uplink has been propagated, so the
881 uplink still is the same as the dfn.
883 But n might not be a proper cfloop head for the analysis. Proper cfloop
884 heads are Block and Phi nodes. find_tail searches the stack for
885 Block's and Phi's and takes those nodes as cfloop heads for the current
886 cfloop instead and marks the incoming edge as backedge. */
888 ir_graph *tail = find_tail(n);
890 /* We have a cfloop, that is no straight line code,
891 because we found a cfloop head!
892 Next actions: Open a new cfloop on the cfloop tree and
893 try to find inner cfloops */
896 ir_loop *l = new_loop();
898 /* Remove the cfloop from the stack ... */
899 pop_scc_unmark_visit(n);
901 /* The current backedge has been marked, that is temporarily eliminated,
902 by find tail. Start the scc algorithm
903 anew on the subgraph thats left (the current cfloop without the backedge)
904 in order to find more inner cfloops. */
908 assert(cg_irg_visited(n));
918 * reset the backedge information for all callers in all irgs
920 static void reset_isbe(void)
922 size_t i, n_irgs = get_irp_n_irgs();
924 for (i = 0; i < n_irgs; ++i) {
925 ir_graph *irg = get_irp_irg(i);
927 if (irg->caller_isbe)
928 xfree(irg->caller_isbe);
929 irg->caller_isbe = NULL;
931 if (irg->callee_isbe)
932 xfree(irg->callee_isbe);
933 irg->callee_isbe = NULL;
937 /* ----------------------------------------------------------------------------------- */
938 /* Another algorithm to compute recursion nesting depth */
939 /* Walk the callgraph. For each crossed edge increase the loop depth by the edge */
940 /* weight. Assign graphs the maximal depth. */
941 /* ----------------------------------------------------------------------------------- */
943 static void compute_loop_depth(ir_graph *irg, void *env)
945 size_t current_nesting = *(size_t *) env;
946 size_t old_nesting = irg->callgraph_loop_depth;
947 ir_visited_t old_visited = get_cg_irg_visited(irg);
949 if (cg_irg_visited(irg)) return;
951 mark_cg_irg_visited(irg);
953 if (old_nesting < current_nesting)
954 irg->callgraph_loop_depth = current_nesting;
956 if (current_nesting > irp->max_callgraph_loop_depth)
957 irp->max_callgraph_loop_depth = current_nesting;
959 if ((old_visited +1 < get_cg_irg_visited(irg)) || /* not yet visited */
960 (old_nesting < current_nesting)) { /* propagate larger nesting */
963 /* Don't walk the graph, but a tree that is an unfolded graph. */
964 n_callees = get_irg_n_callees(irg);
965 for (i = 0; i < n_callees; ++i) {
966 ir_graph *m = get_irg_callee(irg, i);
967 *(size_t *)env += get_irg_callee_loop_depth(irg, i);
968 compute_loop_depth(m, env);
969 *(size_t *)env -= get_irg_callee_loop_depth(irg, i);
973 set_cg_irg_visited(irg, master_cg_visited-1);
976 /* ------------------------------------------------------------------------------------ */
977 /* Another algorithm to compute recursion nesting depth */
978 /* Walk the callgraph. For each crossed loop increase the nesting depth by one. */
979 /* Assign graphs the maximal nesting depth. Don't increase if passing loops more than */
981 /* ------------------------------------------------------------------------------------ */
984 /* For callees, we want to remember the Call nodes, too. */
985 typedef struct ana_entry2 {
986 ir_loop **loop_stack; /**< a stack of ir_loop entries */
987 size_t tos; /**< the top of stack entry */
988 size_t recursion_nesting;
992 * push a loop entry on the stack
994 static void push2(ana_entry2 *e, ir_loop *g)
996 if (ARR_LEN(e->loop_stack) == e->tos) {
997 ARR_APP1(ir_loop *, e->loop_stack, g);
999 e->loop_stack[e->tos] = g;
1005 * returns the top of stack and pop it
1007 static ir_loop *pop2(ana_entry2 *e)
1009 return e->loop_stack[--e->tos];
1013 * check if a loop g in on the stack. Did not check the TOS.
1015 static int in_stack(ana_entry2 *e, ir_loop *g)
1018 for (i = e->tos; i != 0;) {
1019 if (e->loop_stack[--i] == g) return 1;
1024 static void compute_rec_depth(ir_graph *irg, void *env)
1026 ana_entry2 *e = (ana_entry2 *)env;
1027 ir_loop *l = irg->l;
1028 size_t depth, old_depth = irg->callgraph_recursion_depth;
1031 if (cg_irg_visited(irg))
1033 mark_cg_irg_visited(irg);
1035 /* -- compute and set the new nesting value -- */
1036 if ((l != irp->outermost_cg_loop) && !in_stack(e, l)) {
1038 ++e->recursion_nesting;
1041 depth = e->recursion_nesting;
1043 if (old_depth < depth)
1044 irg->callgraph_recursion_depth = depth;
1046 if (depth > irp->max_callgraph_recursion_depth)
1047 irp->max_callgraph_recursion_depth = depth;
1049 /* -- spread the nesting value -- */
1050 if (depth == 0 || old_depth < depth) {
1051 size_t i, n_callees;
1053 /* Don't walk the graph, but a tree that is an unfolded graph.
1054 Therefore we unset the visited flag at the end. */
1055 n_callees = get_irg_n_callees(irg);
1056 for (i = 0; i < n_callees; ++i) {
1057 ir_graph *m = get_irg_callee(irg, i);
1058 compute_rec_depth(m, env);
1062 /* -- clean up -- */
1065 --e->recursion_nesting;
1067 set_cg_irg_visited(irg, master_cg_visited-1);
1071 /* ----------------------------------------------------------------------------------- */
1072 /* Another algorithm to compute the execution frequency of methods ignoring recursions. */
1073 /* Walk the callgraph. Ignore backedges. Use sum of execution frequencies of Call */
1074 /* nodes to evaluate a callgraph edge. */
1075 /* ----------------------------------------------------------------------------------- */
1077 /* Returns the method execution frequency of a graph. */
1078 double get_irg_method_execution_frequency(const ir_graph *irg)
1080 return irg->method_execution_frequency;
1084 * Increase the method execution frequency to freq if its current value is
1085 * smaller then this.
1087 static void set_irg_method_execution_frequency(ir_graph *irg, double freq)
1089 irg->method_execution_frequency = freq;
1091 if (irp->max_method_execution_frequency < freq)
1092 irp->max_method_execution_frequency = freq;
1095 static void compute_method_execution_frequency(ir_graph *irg, void *env)
1097 size_t i, n_callers;
1103 if (cg_irg_visited(irg))
1106 /* We need the values of all predecessors (except backedges).
1107 So they must be marked. Else we will reach the node through
1108 one of the unmarked ones. */
1109 n_callers = get_irg_n_callers(irg);
1110 for (i = 0; i < n_callers; ++i) {
1111 ir_graph *m = get_irg_caller(irg, i);
1112 if (is_irg_caller_backedge(irg, i))
1114 if (!cg_irg_visited(m)) {
1118 mark_cg_irg_visited(irg);
1120 /* Compute the new frequency. */
1123 for (i = 0; i < n_callers; ++i) {
1124 if (! is_irg_caller_backedge(irg, i)) {
1125 double edge_freq = get_irg_caller_method_execution_frequency(irg, i);
1126 assert(edge_freq >= 0);
1133 /* A starting point: method only called from outside,
1134 or only backedges as predecessors. */
1138 set_irg_method_execution_frequency(irg, freq);
1141 n_callees = get_irg_n_callees(irg);
1142 for (i = 0; i < n_callees; ++i) {
1143 compute_method_execution_frequency(get_irg_callee(irg, i), NULL);
1148 /* ----------------------------------------------------------------------------------- */
1149 /* The recursion stuff driver. */
1150 /* ----------------------------------------------------------------------------------- */
1152 /* Compute the backedges that represent recursions. */
1153 void find_callgraph_recursions(void)
1156 struct obstack temp;
1160 /* -- compute the looptree. -- */
1162 /* The outermost graph. We start here. Then we start at all
1163 functions in irg list that are never called, then at the remaining
1164 unvisited ones. The third step is needed for functions that are not
1165 reachable from the outermost graph, but call themselves in a cycle. */
1166 assert(get_irp_main_irg());
1167 outermost_ir_graph = get_irp_main_irg();
1168 obstack_init(&temp);
1171 current_loop = NULL;
1172 new_loop(); /* sets current_loop */
1174 ++master_cg_visited;
1175 cgscc(outermost_ir_graph);
1176 n_irgs = get_irp_n_irgs();
1177 for (i = 0; i < n_irgs; ++i) {
1178 ir_graph *irg = get_irp_irg(i);
1179 if (!cg_irg_visited(irg) && get_irg_n_callers(irg) == 0)
1182 for (i = 0; i < n_irgs; ++i) {
1183 ir_graph *irg = get_irp_irg(i);
1184 if (!cg_irg_visited(irg))
1187 obstack_free(&temp, NULL);
1189 irp->outermost_cg_loop = current_loop;
1190 mature_loops(current_loop, outermost_ir_graph->obst);
1192 /* -- Reverse the backedge information. -- */
1193 for (i = 0; i < n_irgs; ++i) {
1194 ir_graph *irg = get_irp_irg(i);
1195 size_t j, n_callees = get_irg_n_callees(irg);
1196 for (j = 0; j < n_callees; ++j) {
1197 if (is_irg_callee_backedge(irg, j))
1198 set_irg_caller_backedge(get_irg_callee(irg, j), irg);
1202 irp->callgraph_state = irp_callgraph_and_calltree_consistent;
1205 /* Compute interprocedural performance estimates. */
1206 void compute_performance_estimates(void)
1208 size_t i, n_irgs = get_irp_n_irgs();
1209 size_t current_nesting;
1212 assert(get_irp_exec_freq_state() != exec_freq_none && "execution frequency not calculated");
1214 /* -- compute the loop depth -- */
1215 current_nesting = 0;
1216 irp->max_callgraph_loop_depth = 0;
1217 master_cg_visited += 2;
1218 compute_loop_depth(get_irp_main_irg(), ¤t_nesting);
1219 for (i = 0; i < n_irgs; ++i) {
1220 ir_graph *irg = get_irp_irg(i);
1221 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1222 get_irg_n_callers(irg) == 0) {
1223 compute_loop_depth(irg, ¤t_nesting);
1226 for (i = 0; i < n_irgs; ++i) {
1227 ir_graph *irg = get_irp_irg(i);
1228 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1229 compute_loop_depth(irg, ¤t_nesting);
1234 /* -- compute the recursion depth -- */
1235 e.loop_stack = NEW_ARR_F(ir_loop *, 0);
1237 e.recursion_nesting = 0;
1239 irp->max_callgraph_recursion_depth = 0;
1241 master_cg_visited += 2;
1242 compute_rec_depth(get_irp_main_irg(), &e);
1243 for (i = 0; i < n_irgs; ++i) {
1244 ir_graph *irg = get_irp_irg(i);
1245 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1246 get_irg_n_callers(irg) == 0) {
1247 compute_rec_depth(irg, &e);
1250 for (i = 0; i < n_irgs; ++i) {
1251 ir_graph *irg = get_irp_irg(i);
1252 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1253 compute_rec_depth(irg, &e);
1257 DEL_ARR_F(e.loop_stack);
1259 /* -- compute the execution frequency -- */
1260 irp->max_method_execution_frequency = 0;
1261 master_cg_visited += 2;
1262 assert(get_irg_n_callers(get_irp_main_irg()) == 0);
1263 compute_method_execution_frequency(get_irp_main_irg(), NULL);
1264 for (i = 0; i < n_irgs; ++i) {
1265 ir_graph *irg = get_irp_irg(i);
1266 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1267 get_irg_n_callers(irg) == 0) {
1268 compute_method_execution_frequency(irg, NULL);
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 compute_method_execution_frequency(irg, NULL);
1279 /* Returns the maximal loop depth of all paths from an external visible method to
1281 size_t get_irg_loop_depth(const ir_graph *irg)
1283 assert(irp->callgraph_state == irp_callgraph_consistent ||
1284 irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1285 return irg->callgraph_loop_depth;
1288 /* Returns the maximal recursion depth of all paths from an external visible method to
1290 size_t get_irg_recursion_depth(const ir_graph *irg)
1292 assert(irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1293 return irg->callgraph_recursion_depth;
1296 /* Computes the interprocedural loop nesting information. */
1297 void analyse_loop_nesting_depth(void)
1299 /* establish preconditions. */
1300 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
1301 ir_entity **free_methods = NULL;
1303 cgana(&free_methods);
1304 xfree(free_methods);
1307 if (irp_callgraph_consistent != get_irp_callgraph_state()) {
1308 compute_callgraph();
1311 find_callgraph_recursions();
1313 compute_performance_estimates();
1315 set_irp_loop_nesting_depth_state(loop_nesting_depth_consistent);
1318 loop_nesting_depth_state get_irp_loop_nesting_depth_state(void)
1320 return irp->lnd_state;
1322 void set_irp_loop_nesting_depth_state(loop_nesting_depth_state s)
1326 void set_irp_loop_nesting_depth_state_inconsistent(void)
1328 if (irp->lnd_state == loop_nesting_depth_consistent)
1329 irp->lnd_state = loop_nesting_depth_inconsistent;