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 Statistics for Firm. DAG's in graphs.
23 * @author Michael Beck
35 enum dag_counting_options_t {
36 FIRMSTAT_COPY_CONSTANTS = 0x00000001, /**< if set, constants will be treated as they are in
37 the same block as its successors */
38 FIRMSTAT_LOAD_IS_LEAVE = 0x00000002, /**< Load nodes are always leaves */
39 FIRMSTAT_CALL_IS_LEAVE = 0x00000004, /**< Call nodes are always leaves */
40 FIRMSTAT_ARGS_ARE_ROOTS = 0x00000008, /**< arguments (Proj(Proj(Start)) are roots */
43 typedef struct _dag_entry_t dag_entry_t;
46 * Environment for connecting DAG's
48 typedef struct _dag_env_t {
50 unsigned num_of_dags; /**< Number of found DAGs so far. */
51 dag_entry_t *list_of_dags; /**< List of found DAGs. */
52 unsigned options; /**< DAG counting options. */
59 unsigned id; /**< assigned ID for this DAG */
60 ir_node *root; /**< one root of the DAG */
61 unsigned num_roots; /**< number of root nodes in the DAG */
62 unsigned num_nodes; /**< overall number of nodes in the DAG */
63 unsigned num_inner_nodes; /**< number of inner nodes in the DAG */
64 unsigned is_dead:1; /**< marks a dead entry */
65 unsigned is_tree:1; /**< True if this DAG is a tree. */
66 unsigned is_ext_ref:1; /**< True if this DAG is external referenced, so it cannot be combined. */
67 dag_entry_t *next; /**< link all entries of a DAG */
68 dag_entry_t *link; /**< if set, this entry is an ID */
72 * return an DAG entry for the node n
74 static dag_entry_t *get_irn_dag_entry(ir_node *n)
76 dag_entry_t *p = get_irn_link(n);
79 /* skip any dead links */
83 } while (p->link != NULL);
88 } /* get_irn_dag_entry */
90 #define set_irn_dag_entry(n, e) set_irn_link(n, e)
93 * checks whether a node is an arg
95 static int is_arg(ir_node *node)
100 node = get_Proj_pred(node);
104 node = get_Proj_pred(node);
105 return is_Start(node);
109 * Allocate a new DAG entry.
111 static dag_entry_t *new_dag_entry(dag_env_t *dag_env, ir_node *node) {
112 dag_entry_t *entry = obstack_alloc(&dag_env->obst, sizeof(*entry));
114 entry->num_nodes = 1;
115 entry->num_roots = 1;
116 entry->num_inner_nodes = 0;
120 entry->is_ext_ref = 0;
121 entry->next = dag_env->list_of_dags;
124 ++dag_env->num_of_dags;
125 dag_env->list_of_dags = entry;
127 set_irn_dag_entry(node, entry);
129 } /* new_dag_entry */
132 * Post-walker to detect DAG roots that are referenced form other blocks
134 static void find_dag_roots(ir_node *node, void *env)
136 dag_env_t *dag_env = env;
144 block = get_nodes_block(node);
146 /* ignore start end end blocks */
147 if (block == get_irg_start_block(current_ir_graph) ||
148 block == get_irg_end_block(current_ir_graph)) {
152 /* Phi nodes always references nodes from "other" block */
154 if (get_irn_mode(node) != mode_M) {
155 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
156 ir_node *prev = get_irn_n(node, i);
161 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
162 if (is_irn_constlike(prev))
166 entry = get_irn_dag_entry(prev);
169 /* found an unassigned node, a new root */
170 entry = new_dag_entry(dag_env, node);
171 entry->is_ext_ref = 1;
177 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
178 ir_node *prev = get_irn_n(node, i);
179 ir_mode *mode = get_irn_mode(prev);
181 if (mode == mode_X || mode == mode_M)
187 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
188 if (is_irn_constlike(prev))
192 if (get_nodes_block(prev) != block) {
193 /* The predecessor is from another block. It forms
195 entry = get_irn_dag_entry(prev);
197 /* found an unassigned node, a new root */
198 entry = new_dag_entry(dag_env, node);
199 entry->is_ext_ref = 1;
204 } /* find_dag_roots */
207 * Pre-walker for connecting DAGs and counting.
209 static void connect_dags(ir_node *node, void *env)
211 dag_env_t *dag_env = env;
220 block = get_nodes_block(node);
222 /* ignore start end end blocks */
223 if (block == get_irg_start_block(current_ir_graph) ||
224 block == get_irg_end_block(current_ir_graph)) {
228 /* ignore Phi nodes */
232 if (dag_env->options & FIRMSTAT_ARGS_ARE_ROOTS && is_arg(node))
235 mode = get_irn_mode(node);
236 if (mode == mode_X || mode == mode_M) {
237 /* do NOT count mode_X and mode_M nodes */
241 /* if this option is set, Loads are always leaves */
242 if (dag_env->options & FIRMSTAT_LOAD_IS_LEAVE && is_Load(node))
245 if (dag_env->options & FIRMSTAT_CALL_IS_LEAVE && is_Call(node))
248 entry = get_irn_dag_entry(node);
251 /* found an unassigned node, maybe a new root */
252 entry = new_dag_entry(dag_env, node);
255 /* put the predecessors into the same DAG as the current */
256 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
257 ir_node *prev = get_irn_n(node, i);
258 ir_mode *mode = get_irn_mode(prev);
263 if (mode == mode_X || mode == mode_M)
267 * copy constants if requested into the DAG's
268 * beware, do NOT add a link, as this will result in
269 * wrong intersections
271 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
272 if (is_irn_constlike(prev)) {
274 ++entry->num_inner_nodes;
278 /* only nodes from the same block goes into the DAG */
279 if (get_nodes_block(prev) == block) {
280 dag_entry_t *prev_entry = get_irn_dag_entry(prev);
283 /* not assigned node, put it into the same DAG */
284 set_irn_dag_entry(prev, entry);
286 ++entry->num_inner_nodes;
288 if (prev_entry == entry) {
289 /* We found a node that is already assigned to this DAG.
290 This DAG is not a tree. */
293 /* two DAGs intersect: copy the data to one of them
294 and kill the other */
295 entry->num_roots += prev_entry->num_roots;
296 entry->num_nodes += prev_entry->num_nodes;
297 entry->num_inner_nodes += prev_entry->num_inner_nodes;
298 entry->is_tree &= prev_entry->is_tree;
300 --dag_env->num_of_dags;
302 prev_entry->is_dead = 1;
303 prev_entry->link = entry;
310 #define DEFAULT_RET 1
313 static unsigned mark_options;
316 * a vcg attribute hook
318 static int stat_dag_mark_hook(FILE *F, ir_node *n, ir_node *l)
320 static const char *colors[] = { "purple", "pink", "lightblue", "orange", "khaki", "orchid", "lilac", "turquoise" };
323 /* do not count Bad / NoMem */
325 ir_op *op = get_irn_op(l);
327 if (op == op_NoMem || op == op_Bad)
330 /* check for additional options */
333 if (mark_options & FIRMSTAT_LOAD_IS_LEAVE && op == op_Load)
336 if (mark_options & FIRMSTAT_CALL_IS_LEAVE && op == op_Call)
340 entry = get_irn_dag_entry(n);
344 fprintf(F, "color: %s info3: \"DAG id: %u\"", colors[entry->id & 7], entry->id);
346 /* I know the color! */
348 } /* stat_dag_mark_hook */
351 * count the DAG's size of a graph
353 * @param global the global entry
354 * @param graph the current graph entry
356 void count_dags_in_graph(graph_entry_t *global, graph_entry_t *graph)
363 /* do NOT check the const code irg */
364 if (graph->irg == get_const_code_irg())
367 /* first step, clear the links */
368 irg_walk_graph(graph->irg, firm_clear_link, NULL, NULL);
370 obstack_init(&root_env.obst);
371 root_env.num_of_dags = 0;
372 root_env.list_of_dags = NULL;
373 root_env.options = FIRMSTAT_COPY_CONSTANTS | FIRMSTAT_LOAD_IS_LEAVE | FIRMSTAT_CALL_IS_LEAVE;
375 /* find the DAG roots that are referenced from other block */
376 irg_walk_graph(graph->irg, NULL, find_dag_roots, &root_env);
378 /* connect and count them */
379 irg_walk_graph(graph->irg, connect_dags, NULL, &root_env);
381 printf("Graph %p %s --- %d\n", (void *)graph->irg, get_entity_name(get_irg_entity(graph->irg)),
382 root_env.num_of_dags);
384 for (id = 0, entry = root_env.list_of_dags; entry; entry = entry->next) {
389 printf("number of roots %d number of nodes %d inner %d tree %u %ld\n",
392 entry->num_inner_nodes,
394 get_irn_node_nr(entry->root));
399 mark_options = root_env.options;
400 set_dump_node_vcgattr_hook(stat_dag_mark_hook);
401 dump_ir_block_graph(graph->irg, "-dag");
402 set_dump_node_vcgattr_hook(NULL);
405 assert(id == root_env.num_of_dags);
407 obstack_free(&root_env.obst, NULL);
408 } /* count_dags_in_graph */