3 * File name: ir/ir/dags.c
4 * Purpose: Statistics for Firm. DAG's in graphs.
8 * Copyright: (c) 2004 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 enum dag_counting_options_t {
23 FIRMSTAT_COPY_CONSTANTS = 0x00000001, /**< if set, constants will be treated as they are in
24 the same block as its successors */
28 * walker for clearing node links
30 static void clear_links(ir_node *node, void *env)
32 set_irn_link(node, NULL);
35 typedef struct _dag_entry_t dag_entry_t;
38 * Environment for connecting DAG's
40 typedef struct _dag_env_t {
43 dag_entry_t *list_of_dags;
44 unsigned options; /**< DAG counting options */
51 unsigned id; /**< assigned ID for this DAG */
52 ir_node *root; /**< one root of the DAG */
53 unsigned num_roots; /**< number of root nodes in the DAG */
54 unsigned num_nodes; /**< overall number of nodes in the DAG */
55 unsigned num_inner_nodes; /**< number of inner nodes in the DAG */
56 unsigned is_dead; /**< marks a dead entry */
57 dag_entry_t *next; /**< link all entries of a DAG */
58 dag_entry_t *link; /**< if set, this entry is an ID */
62 * return an DAG entry for the node n
64 static dag_entry_t *get_irn_dag_entry(ir_node *n)
66 dag_entry_t *res = get_irn_link(n);
71 for (p = res; p->link; p = p->link);
81 #define set_irn_dag_entry(n, e) set_irn_link(n, e)
84 * walker for connecting DAGs and counting.
86 static void connect_dags(ir_node *node, void *env)
88 dag_env_t *dag_env = env;
97 block = get_nodes_block(node);
99 /* ignore start end end blocks */
100 if (block == get_irg_start_block(current_ir_graph) ||
101 block == get_irg_end_block(current_ir_graph))
107 mode = get_irn_mode(node);
108 if (mode == mode_X || mode == mode_M) {
109 /* do NOT count mode_X nodes */
113 entry = get_irn_dag_entry(node);
116 /* found a not assigned node, maybe a new root */
117 entry = obstack_alloc(&dag_env->obst, sizeof(*entry));
119 entry->num_nodes = 1;
120 entry->num_roots = 1;
121 entry->num_inner_nodes = 0;
124 entry->next = dag_env->list_of_dags;
127 ++dag_env->num_of_dags;
128 dag_env->list_of_dags = entry;
130 set_irn_dag_entry(node, entry);
133 /* put the predecessors into the same DAG as the current */
134 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
135 ir_node *prev = get_irn_n(node, i);
136 ir_mode *mode = get_irn_mode(prev);
141 if (mode == mode_X || mode == mode_M)
145 * copy constants if requested into the DAG's
146 * beware, do NOT add a link, as this will result in
147 * wrong intersections
149 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
150 if (get_irn_op(prev) == op_Const || get_irn_op(prev) == op_SymConst) {
152 ++entry->num_inner_nodes;
156 /* only nodes from the same block goes into the DAG */
157 if (get_nodes_block(prev) == block) {
158 dag_entry_t *prev_entry = get_irn_dag_entry(prev);
161 /* not assigned node, put it into the same DAG */
162 set_irn_dag_entry(prev, entry);
164 ++entry->num_inner_nodes;
167 if (prev_entry != entry) {
169 /* two DAGs intersect */
170 entry->num_roots += prev_entry->num_roots;
171 entry->num_nodes += prev_entry->num_nodes;
172 entry->num_inner_nodes += prev_entry->num_inner_nodes;
174 --dag_env->num_of_dags;
176 prev_entry->is_dead = 1;
177 prev_entry->link = entry;
185 * a vcg attribute hook
187 static int stat_dag_mark_hook(FILE *F, ir_node *n, ir_node *l)
189 static const char *colors[] = { "purple", "pink", "lightblue", "orange", "khaki", "orchid", "lilac", "turquoise" };
192 /* do not count Bad / NoMem */
193 if (l && (get_irn_op(l) == op_NoMem || get_irn_op(l) == op_Bad))
196 entry = get_irn_dag_entry(n);
200 fprintf(F, "color: %s info3: \"DAG id: %u\"", colors[entry->id & 7], entry->id);
202 /* I know the color! */
207 * count the DAG's size of a graph
209 void count_dags_in_graph(graph_entry_t *global, graph_entry_t *graph)
215 /* do NOT check the const code irg */
216 if (graph->irg == get_const_code_irg())
219 /* first step, clear the links */
220 irg_walk_graph(graph->irg, clear_links, NULL, NULL);
222 obstack_init(&root_env.obst);
223 root_env.num_of_dags = 0;
224 root_env.list_of_dags = NULL;
225 root_env.options = FIRMSTAT_COPY_CONSTANTS;
228 irg_walk_graph(graph->irg, connect_dags, NULL, &root_env);
230 printf("Graph %p %s --- %d\n", graph->irg, get_entity_name(get_irg_entity(graph->irg)),
231 root_env.num_of_dags);
233 for (id = 0, entry = root_env.list_of_dags; entry; entry = entry->next) {
238 printf("number of roots %d number of nodes %d inner %d %ld\n",
241 entry->num_inner_nodes,
242 get_irn_node_nr(entry->root));
247 set_dump_node_vcgattr_hook(stat_dag_mark_hook);
248 dump_ir_block_graph(graph->irg, "-dag");
249 set_dump_node_vcgattr_hook(NULL);
252 assert(id == root_env.num_of_dags);
254 obstack_free(&root_env.obst, NULL);