2 * Copyright (C) 1995-2007 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 Statistics for Firm. DAG's in graphs.
23 * @author Michael Beck
37 enum dag_counting_options_t {
38 FIRMSTAT_COPY_CONSTANTS = 0x00000001, /**< if set, constants will be treated as they are in
39 the same block as its successors */
40 FIRMSTAT_LOAD_IS_LEAVE = 0x00000002, /**< Load nodes are always leaves */
41 FIRMSTAT_CALL_IS_LEAVE = 0x00000004, /**< Call nodes are always leaves */
42 FIRMSTAT_ARGS_ARE_ROOTS = 0x00000008, /**< arguments (Proj(Proj(Start)) are roots */
45 typedef struct _dag_entry_t dag_entry_t;
48 * Environment for connecting DAG's
50 typedef struct _dag_env_t {
52 unsigned num_of_dags; /**< Number of found DAGs so far. */
53 dag_entry_t *list_of_dags; /**< List of found DAGs. */
54 unsigned options; /**< DAG counting options. */
61 unsigned id; /**< assigned ID for this DAG */
62 ir_node *root; /**< one root of the DAG */
63 unsigned num_roots; /**< number of root nodes in the DAG */
64 unsigned num_nodes; /**< overall number of nodes in the DAG */
65 unsigned num_inner_nodes; /**< number of inner nodes in the DAG */
66 unsigned is_dead:1; /**< marks a dead entry */
67 unsigned is_tree:1; /**< True if this DAG is a tree. */
68 unsigned is_ext_ref:1; /**< True if this DAG is external referenced, so it cannot be combined. */
69 dag_entry_t *next; /**< link all entries of a DAG */
70 dag_entry_t *link; /**< if set, this entry is an ID */
74 * return an DAG entry for the node n
76 static dag_entry_t *get_irn_dag_entry(ir_node *n)
78 dag_entry_t *p = get_irn_link(n);
81 /* skip any dead links */
85 } while (p->link != NULL);
90 } /* get_irn_dag_entry */
92 #define set_irn_dag_entry(n, e) set_irn_link(n, e)
95 * checks whether a node is an arg
97 static int is_arg(ir_node *node)
102 node = get_Proj_pred(node);
106 node = get_Proj_pred(node);
107 return get_irn_op(node) == op_Start;
111 * Allocate a new DAG entry.
113 static dag_entry_t *new_dag_entry(dag_env_t *dag_env, ir_node *node) {
114 dag_entry_t *entry = obstack_alloc(&dag_env->obst, sizeof(*entry));
116 entry->num_nodes = 1;
117 entry->num_roots = 1;
118 entry->num_inner_nodes = 0;
122 entry->is_ext_ref = 0;
123 entry->next = dag_env->list_of_dags;
126 ++dag_env->num_of_dags;
127 dag_env->list_of_dags = entry;
129 set_irn_dag_entry(node, entry);
131 } /* new_dag_entry */
134 * Post-walker to detect DAG roots that are referenced form other blocks
136 static void find_dag_roots(ir_node *node, void *env)
138 dag_env_t *dag_env = env;
146 block = get_nodes_block(node);
148 /* ignore start end end blocks */
149 if (block == get_irg_start_block(current_ir_graph) ||
150 block == get_irg_end_block(current_ir_graph)) {
154 /* Phi nodes always references nodes from "other" block */
156 if (get_irn_mode(node) != mode_M) {
157 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
158 ir_node *prev = get_irn_n(node, i);
163 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
164 if (is_irn_constlike(prev))
168 entry = get_irn_dag_entry(prev);
171 /* found an unassigned node, a new root */
172 entry = new_dag_entry(dag_env, node);
173 entry->is_ext_ref = 1;
179 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
180 ir_node *prev = get_irn_n(node, i);
181 ir_mode *mode = get_irn_mode(prev);
183 if (mode == mode_X || mode == mode_M)
189 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
190 if (is_irn_constlike(prev))
194 if (get_nodes_block(prev) != block) {
195 /* The predecessor is from another block. It forms
197 entry = get_irn_dag_entry(prev);
199 /* found an unassigned node, a new root */
200 entry = new_dag_entry(dag_env, node);
201 entry->is_ext_ref = 1;
206 } /* find_dag_roots */
209 * Pre-walker for connecting DAGs and counting.
211 static void connect_dags(ir_node *node, void *env)
213 dag_env_t *dag_env = env;
222 block = get_nodes_block(node);
224 /* ignore start end end blocks */
225 if (block == get_irg_start_block(current_ir_graph) ||
226 block == get_irg_end_block(current_ir_graph)) {
230 /* ignore Phi nodes */
234 if (dag_env->options & FIRMSTAT_ARGS_ARE_ROOTS && is_arg(node))
237 mode = get_irn_mode(node);
238 if (mode == mode_X || mode == mode_M) {
239 /* do NOT count mode_X and mode_M nodes */
243 /* if this option is set, Loads are always leaves */
244 if (dag_env->options & FIRMSTAT_LOAD_IS_LEAVE && get_irn_op(node) == op_Load)
247 if (dag_env->options & FIRMSTAT_CALL_IS_LEAVE && get_irn_op(node) == op_Call)
250 entry = get_irn_dag_entry(node);
253 /* found an unassigned node, maybe a new root */
254 entry = new_dag_entry(dag_env, node);
257 /* put the predecessors into the same DAG as the current */
258 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
259 ir_node *prev = get_irn_n(node, i);
260 ir_mode *mode = get_irn_mode(prev);
265 if (mode == mode_X || mode == mode_M)
269 * copy constants if requested into the DAG's
270 * beware, do NOT add a link, as this will result in
271 * wrong intersections
273 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
274 if (is_irn_constlike(prev)) {
276 ++entry->num_inner_nodes;
280 /* only nodes from the same block goes into the DAG */
281 if (get_nodes_block(prev) == block) {
282 dag_entry_t *prev_entry = get_irn_dag_entry(prev);
285 /* not assigned node, put it into the same DAG */
286 set_irn_dag_entry(prev, entry);
288 ++entry->num_inner_nodes;
290 if (prev_entry == entry) {
291 /* We found a node that is already assigned to this DAG.
292 This DAG is not a tree. */
295 /* two DAGs intersect: copy the data to one of them
296 and kill the other */
297 entry->num_roots += prev_entry->num_roots;
298 entry->num_nodes += prev_entry->num_nodes;
299 entry->num_inner_nodes += prev_entry->num_inner_nodes;
300 entry->is_tree &= prev_entry->is_tree;
302 --dag_env->num_of_dags;
304 prev_entry->is_dead = 1;
305 prev_entry->link = entry;
312 #define DEFAULT_RET 1
315 static unsigned mark_options;
318 * a vcg attribute hook
320 static int stat_dag_mark_hook(FILE *F, ir_node *n, ir_node *l)
322 static const char *colors[] = { "purple", "pink", "lightblue", "orange", "khaki", "orchid", "lilac", "turquoise" };
325 /* do not count Bad / NoMem */
327 ir_op *op = get_irn_op(l);
329 if (op == op_NoMem || op == op_Bad)
332 /* check for additional options */
335 if (mark_options & FIRMSTAT_LOAD_IS_LEAVE && op == op_Load)
338 if (mark_options & FIRMSTAT_CALL_IS_LEAVE && op == op_Call)
342 entry = get_irn_dag_entry(n);
346 fprintf(F, "color: %s info3: \"DAG id: %u\"", colors[entry->id & 7], entry->id);
348 /* I know the color! */
350 } /* stat_dag_mark_hook */
353 * count the DAG's size of a graph
355 * @param global the global entry
356 * @param graph the current graph entry
358 void count_dags_in_graph(graph_entry_t *global, graph_entry_t *graph)
365 /* do NOT check the const code irg */
366 if (graph->irg == get_const_code_irg())
369 /* first step, clear the links */
370 irg_walk_graph(graph->irg, firm_clear_link, NULL, NULL);
372 obstack_init(&root_env.obst);
373 root_env.num_of_dags = 0;
374 root_env.list_of_dags = NULL;
375 root_env.options = FIRMSTAT_COPY_CONSTANTS | FIRMSTAT_LOAD_IS_LEAVE | FIRMSTAT_CALL_IS_LEAVE;
377 /* find the DAG roots that are referenced from other block */
378 irg_walk_graph(graph->irg, NULL, find_dag_roots, &root_env);
380 /* connect and count them */
381 irg_walk_graph(graph->irg, connect_dags, NULL, &root_env);
383 printf("Graph %p %s --- %d\n", (void *)graph->irg, get_entity_name(get_irg_entity(graph->irg)),
384 root_env.num_of_dags);
386 for (id = 0, entry = root_env.list_of_dags; entry; entry = entry->next) {
391 printf("number of roots %d number of nodes %d inner %d tree %u %ld\n",
394 entry->num_inner_nodes,
396 get_irn_node_nr(entry->root));
401 mark_options = root_env.options;
402 set_dump_node_vcgattr_hook(stat_dag_mark_hook);
403 dump_ir_block_graph(graph->irg, "-dag");
404 set_dump_node_vcgattr_hook(NULL);
407 assert(id == root_env.num_of_dags);
409 obstack_free(&root_env.obst, NULL);
410 } /* count_dags_in_graph */