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.
23 enum dag_counting_options_t {
24 FIRMSTAT_COPY_CONSTANTS = 0x00000001, /**< if set, constants will be treated as they are in
25 the same block as its successors */
26 FIRMSTAT_LOAD_IS_LEAVE = 0x00000002, /**< Load nodes are always leaves */
27 FIRMSTAT_CALL_IS_LEAVE = 0x00000004, /**< Call nodes are always leaves */
28 FIRMSTAT_ARGS_ARE_ROOTS = 0x00000008, /**< arguments (Proj(Proj(Start)) are roots */
31 typedef struct _dag_entry_t dag_entry_t;
34 * Environment for connecting DAG's
36 typedef struct _dag_env_t {
39 dag_entry_t *list_of_dags;
40 unsigned options; /**< DAG counting options */
47 unsigned id; /**< assigned ID for this DAG */
48 ir_node *root; /**< one root of the DAG */
49 unsigned num_roots; /**< number of root nodes in the DAG */
50 unsigned num_nodes; /**< overall number of nodes in the DAG */
51 unsigned num_inner_nodes; /**< number of inner nodes in the DAG */
52 unsigned is_dead; /**< marks a dead entry */
53 dag_entry_t *next; /**< link all entries of a DAG */
54 dag_entry_t *link; /**< if set, this entry is an ID */
58 * return an DAG entry for the node n
60 static dag_entry_t *get_irn_dag_entry(ir_node *n)
62 dag_entry_t *res = get_irn_link(n);
67 for (p = res; p->link; p = p->link);
77 #define set_irn_dag_entry(n, e) set_irn_link(n, e)
80 * checks wether a node is an arg
82 static int is_arg(ir_node *node)
87 node = get_Proj_pred(node);
91 node = get_Proj_pred(node);
92 return get_irn_op(node) == op_Start;
96 * walker for connecting DAGs and counting.
98 static void connect_dags(ir_node *node, void *env)
100 dag_env_t *dag_env = env;
109 block = get_nodes_block(node);
111 /* ignore start end end blocks */
112 if (block == get_irg_start_block(current_ir_graph) ||
113 block == get_irg_end_block(current_ir_graph))
119 if (dag_env->options & FIRMSTAT_ARGS_ARE_ROOTS && is_arg(node))
122 mode = get_irn_mode(node);
123 if (mode == mode_X || mode == mode_M) {
124 /* do NOT count mode_X nodes */
128 entry = get_irn_dag_entry(node);
131 /* found a not assigned node, maybe a new root */
132 entry = obstack_alloc(&dag_env->obst, sizeof(*entry));
134 entry->num_nodes = 1;
135 entry->num_roots = 1;
136 entry->num_inner_nodes = 0;
139 entry->next = dag_env->list_of_dags;
142 ++dag_env->num_of_dags;
143 dag_env->list_of_dags = entry;
145 set_irn_dag_entry(node, entry);
148 /* if this option is set, Loads are allways leaves */
149 if (dag_env->options & FIRMSTAT_LOAD_IS_LEAVE && get_irn_op(node) == op_Load)
152 if (dag_env->options & FIRMSTAT_CALL_IS_LEAVE && get_irn_op(node) == op_Call)
155 /* put the predecessors into the same DAG as the current */
156 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
157 ir_node *prev = get_irn_n(node, i);
158 ir_mode *mode = get_irn_mode(prev);
163 if (mode == mode_X || mode == mode_M)
167 * copy constants if requested into the DAG's
168 * beware, do NOT add a link, as this will result in
169 * wrong intersections
171 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
172 if (get_irn_op(prev) == op_Const || get_irn_op(prev) == op_SymConst) {
174 ++entry->num_inner_nodes;
178 /* only nodes from the same block goes into the DAG */
179 if (get_nodes_block(prev) == block) {
180 dag_entry_t *prev_entry = get_irn_dag_entry(prev);
183 /* not assigned node, put it into the same DAG */
184 set_irn_dag_entry(prev, entry);
186 ++entry->num_inner_nodes;
189 if (prev_entry != entry) {
191 /* two DAGs intersect */
192 entry->num_roots += prev_entry->num_roots;
193 entry->num_nodes += prev_entry->num_nodes;
194 entry->num_inner_nodes += prev_entry->num_inner_nodes;
196 --dag_env->num_of_dags;
198 prev_entry->is_dead = 1;
199 prev_entry->link = entry;
206 #define DEFAULT_RET 1
209 static unsigned mark_options;
212 * a vcg attribute hook
214 static int stat_dag_mark_hook(FILE *F, ir_node *n, ir_node *l)
216 static const char *colors[] = { "purple", "pink", "lightblue", "orange", "khaki", "orchid", "lilac", "turquoise" };
219 /* do not count Bad / NoMem */
221 ir_op *op = get_irn_op(l);
223 if (op == op_NoMem || op == op_Bad)
226 /* check for additional options */
229 if (mark_options & FIRMSTAT_LOAD_IS_LEAVE && op == op_Load)
232 if (mark_options & FIRMSTAT_CALL_IS_LEAVE && op == op_Call)
236 entry = get_irn_dag_entry(n);
240 fprintf(F, "color: %s info3: \"DAG id: %u\"", colors[entry->id & 7], entry->id);
242 /* I know the color! */
247 * count the DAG's size of a graph
249 void count_dags_in_graph(graph_entry_t *global, graph_entry_t *graph)
255 /* do NOT check the const code irg */
256 if (graph->irg == get_const_code_irg())
259 /* first step, clear the links */
260 irg_walk_graph(graph->irg, firm_clear_link, NULL, NULL);
262 obstack_init(&root_env.obst);
263 root_env.num_of_dags = 0;
264 root_env.list_of_dags = NULL;
265 root_env.options = FIRMSTAT_COPY_CONSTANTS | FIRMSTAT_LOAD_IS_LEAVE | FIRMSTAT_CALL_IS_LEAVE;
268 irg_walk_graph(graph->irg, connect_dags, NULL, &root_env);
270 printf("Graph %p %s --- %d\n", (void *)graph->irg, get_entity_name(get_irg_entity(graph->irg)),
271 root_env.num_of_dags);
273 for (id = 0, entry = root_env.list_of_dags; entry; entry = entry->next) {
278 printf("number of roots %d number of nodes %d inner %d %ld\n",
281 entry->num_inner_nodes,
282 get_irn_node_nr(entry->root));
287 mark_options = root_env.options;
288 set_dump_node_vcgattr_hook(stat_dag_mark_hook);
289 dump_ir_block_graph(graph->irg, "-dag");
290 set_dump_node_vcgattr_hook(NULL);
293 assert(id == root_env.num_of_dags);
295 obstack_free(&root_env.obst, NULL);