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 * File name: ir/ir/dags.c
23 * Purpose: Statistics for Firm. DAG's in graphs.
24 * Author: Michael Beck
27 * Copyright: (c) 2004 Universität Karlsruhe
41 enum dag_counting_options_t {
42 FIRMSTAT_COPY_CONSTANTS = 0x00000001, /**< if set, constants will be treated as they are in
43 the same block as its successors */
44 FIRMSTAT_LOAD_IS_LEAVE = 0x00000002, /**< Load nodes are always leaves */
45 FIRMSTAT_CALL_IS_LEAVE = 0x00000004, /**< Call nodes are always leaves */
46 FIRMSTAT_ARGS_ARE_ROOTS = 0x00000008, /**< arguments (Proj(Proj(Start)) are roots */
49 typedef struct _dag_entry_t dag_entry_t;
52 * Environment for connecting DAG's
54 typedef struct _dag_env_t {
56 unsigned num_of_dags; /**< Number of found DAGs so far. */
57 dag_entry_t *list_of_dags; /**< List of found DAGs. */
58 unsigned options; /**< DAG counting options. */
65 unsigned id; /**< assigned ID for this DAG */
66 ir_node *root; /**< one root of the DAG */
67 unsigned num_roots; /**< number of root nodes in the DAG */
68 unsigned num_nodes; /**< overall number of nodes in the DAG */
69 unsigned num_inner_nodes; /**< number of inner nodes in the DAG */
70 unsigned is_dead:1; /**< marks a dead entry */
71 unsigned is_tree:1; /**< True if this DAG is a tree. */
72 unsigned is_ext_ref:1; /**< True if this DAG is external referenced, so it cannot be combined. */
73 dag_entry_t *next; /**< link all entries of a DAG */
74 dag_entry_t *link; /**< if set, this entry is an ID */
78 * return an DAG entry for the node n
80 static dag_entry_t *get_irn_dag_entry(ir_node *n)
82 dag_entry_t *p = get_irn_link(n);
85 /* skip any dead links */
89 } while (p->link != NULL);
94 } /* get_irn_dag_entry */
96 #define set_irn_dag_entry(n, e) set_irn_link(n, e)
99 * checks whether a node is an arg
101 static int is_arg(ir_node *node)
106 node = get_Proj_pred(node);
110 node = get_Proj_pred(node);
111 return get_irn_op(node) == op_Start;
115 * Allocate a new DAG entry.
117 static dag_entry_t *new_dag_entry(dag_env_t *dag_env, ir_node *node) {
118 dag_entry_t *entry = obstack_alloc(&dag_env->obst, sizeof(*entry));
120 entry->num_nodes = 1;
121 entry->num_roots = 1;
122 entry->num_inner_nodes = 0;
126 entry->is_ext_ref = 0;
127 entry->next = dag_env->list_of_dags;
130 ++dag_env->num_of_dags;
131 dag_env->list_of_dags = entry;
133 set_irn_dag_entry(node, entry);
135 } /* new_dag_entry */
138 * Post-walker to detect DAG roots that are referenced form other blocks
140 static void find_dag_roots(ir_node *node, void *env)
142 dag_env_t *dag_env = env;
150 block = get_nodes_block(node);
152 /* ignore start end end blocks */
153 if (block == get_irg_start_block(current_ir_graph) ||
154 block == get_irg_end_block(current_ir_graph)) {
158 /* Phi nodes always references nodes from "other" block */
160 if (get_irn_mode(node) != mode_M) {
161 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
162 ir_node *prev = get_irn_n(node, i);
167 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
168 if (is_irn_constlike(prev))
172 entry = get_irn_dag_entry(prev);
175 /* found an unassigned node, a new root */
176 entry = new_dag_entry(dag_env, node);
177 entry->is_ext_ref = 1;
183 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
184 ir_node *prev = get_irn_n(node, i);
185 ir_mode *mode = get_irn_mode(prev);
187 if (mode == mode_X || mode == mode_M)
193 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
194 if (is_irn_constlike(prev))
198 if (get_nodes_block(prev) != block) {
199 /* The predecessor is from another block. It forms
201 entry = get_irn_dag_entry(prev);
203 /* found an unassigned node, a new root */
204 entry = new_dag_entry(dag_env, node);
205 entry->is_ext_ref = 1;
210 } /* find_dag_roots */
213 * Pre-walker for connecting DAGs and counting.
215 static void connect_dags(ir_node *node, void *env)
217 dag_env_t *dag_env = env;
226 block = get_nodes_block(node);
228 /* ignore start end end blocks */
229 if (block == get_irg_start_block(current_ir_graph) ||
230 block == get_irg_end_block(current_ir_graph)) {
234 /* ignore Phi nodes */
238 if (dag_env->options & FIRMSTAT_ARGS_ARE_ROOTS && is_arg(node))
241 mode = get_irn_mode(node);
242 if (mode == mode_X || mode == mode_M) {
243 /* do NOT count mode_X and mode_M nodes */
247 /* if this option is set, Loads are always leaves */
248 if (dag_env->options & FIRMSTAT_LOAD_IS_LEAVE && get_irn_op(node) == op_Load)
251 if (dag_env->options & FIRMSTAT_CALL_IS_LEAVE && get_irn_op(node) == op_Call)
254 entry = get_irn_dag_entry(node);
257 /* found an unassigned node, maybe a new root */
258 entry = new_dag_entry(dag_env, node);
261 /* put the predecessors into the same DAG as the current */
262 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
263 ir_node *prev = get_irn_n(node, i);
264 ir_mode *mode = get_irn_mode(prev);
269 if (mode == mode_X || mode == mode_M)
273 * copy constants if requested into the DAG's
274 * beware, do NOT add a link, as this will result in
275 * wrong intersections
277 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
278 if (is_irn_constlike(prev)) {
280 ++entry->num_inner_nodes;
284 /* only nodes from the same block goes into the DAG */
285 if (get_nodes_block(prev) == block) {
286 dag_entry_t *prev_entry = get_irn_dag_entry(prev);
289 /* not assigned node, put it into the same DAG */
290 set_irn_dag_entry(prev, entry);
292 ++entry->num_inner_nodes;
294 if (prev_entry == entry) {
295 /* We found a node that is already assigned to this DAG.
296 This DAG is not a tree. */
299 /* two DAGs intersect: copy the data to one of them
300 and kill the other */
301 entry->num_roots += prev_entry->num_roots;
302 entry->num_nodes += prev_entry->num_nodes;
303 entry->num_inner_nodes += prev_entry->num_inner_nodes;
304 entry->is_tree &= prev_entry->is_tree;
306 --dag_env->num_of_dags;
308 prev_entry->is_dead = 1;
309 prev_entry->link = entry;
316 #define DEFAULT_RET 1
319 static unsigned mark_options;
322 * a vcg attribute hook
324 static int stat_dag_mark_hook(FILE *F, ir_node *n, ir_node *l)
326 static const char *colors[] = { "purple", "pink", "lightblue", "orange", "khaki", "orchid", "lilac", "turquoise" };
329 /* do not count Bad / NoMem */
331 ir_op *op = get_irn_op(l);
333 if (op == op_NoMem || op == op_Bad)
336 /* check for additional options */
339 if (mark_options & FIRMSTAT_LOAD_IS_LEAVE && op == op_Load)
342 if (mark_options & FIRMSTAT_CALL_IS_LEAVE && op == op_Call)
346 entry = get_irn_dag_entry(n);
350 fprintf(F, "color: %s info3: \"DAG id: %u\"", colors[entry->id & 7], entry->id);
352 /* I know the color! */
354 } /* stat_dag_mark_hook */
357 * count the DAG's size of a graph
359 * @param global the global entry
360 * @param graph the current graph entry
362 void count_dags_in_graph(graph_entry_t *global, graph_entry_t *graph)
368 /* do NOT check the const code irg */
369 if (graph->irg == get_const_code_irg())
372 /* first step, clear the links */
373 irg_walk_graph(graph->irg, firm_clear_link, NULL, NULL);
375 obstack_init(&root_env.obst);
376 root_env.num_of_dags = 0;
377 root_env.list_of_dags = NULL;
378 root_env.options = FIRMSTAT_COPY_CONSTANTS | FIRMSTAT_LOAD_IS_LEAVE | FIRMSTAT_CALL_IS_LEAVE;
380 /* find the DAG roots that are referenced from other block */
381 irg_walk_graph(graph->irg, NULL, find_dag_roots, &root_env);
383 /* connect and count them */
384 irg_walk_graph(graph->irg, connect_dags, NULL, &root_env);
386 printf("Graph %p %s --- %d\n", (void *)graph->irg, get_entity_name(get_irg_entity(graph->irg)),
387 root_env.num_of_dags);
389 for (id = 0, entry = root_env.list_of_dags; entry; entry = entry->next) {
394 printf("number of roots %d number of nodes %d inner %d tree %u %ld\n",
397 entry->num_inner_nodes,
399 get_irn_node_nr(entry->root));
404 mark_options = root_env.options;
405 set_dump_node_vcgattr_hook(stat_dag_mark_hook);
406 dump_ir_block_graph(graph->irg, "-dag");
407 set_dump_node_vcgattr_hook(NULL);
410 assert(id == root_env.num_of_dags);
412 obstack_free(&root_env.obst, NULL);
413 } /* count_dags_in_graph */