dump: Remove extra leading dashes from dump suffixes.
[libfirm] / ir / stat / dags.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Statistics for Firm. DAG's in graphs.
23  * @author  Michael Beck
24  */
25 #include "config.h"
26
27 #include <assert.h>
28
29 #include "irprintf.h"
30 #include "irdump.h"
31 #include "dags.h"
32 #include "irtools.h"
33 #include "ircons.h"
34
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 */
41 };
42
43 typedef struct dag_entry_t dag_entry_t;
44
45 /**
46  * Environment for connecting DAG's
47  */
48 typedef struct dag_env_t {
49         struct obstack obst;
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. */
53 } dag_env_t;
54
55 /**
56  * a DAG Entry
57  */
58 struct dag_entry_t {
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 */
69 };
70
71 /**
72  * return an DAG entry for the node n
73  */
74 static dag_entry_t *get_irn_dag_entry(const ir_node *n)
75 {
76         dag_entry_t *p = (dag_entry_t*)get_irn_link(n);
77
78         if (p) {
79                 /* skip any dead links */
80                 if (p->link) {
81                         do {
82                                 p = p->link;
83                         } while (p->link != NULL);
84                         /* hacky cast to ir_node* */
85                         set_irn_link((ir_node*)n, p);
86                 }
87         }
88         return p;
89 }
90
91 #define set_irn_dag_entry(n, e) set_irn_link(n, e)
92
93 /**
94  * checks whether a node is an arg
95  */
96 static int is_arg(ir_node *node)
97 {
98         if (! is_Proj(node))
99                 return 0;
100
101         node = get_Proj_pred(node);
102         if (! is_Proj(node))
103                 return 0;
104
105         node = get_Proj_pred(node);
106         return is_Start(node);
107 }
108
109 /**
110  * Allocate a new DAG entry.
111  */
112 static dag_entry_t *new_dag_entry(dag_env_t *dag_env, ir_node *node)
113 {
114         dag_entry_t *entry = OALLOC(&dag_env->obst, dag_entry_t);
115
116         entry->num_nodes       = 1;
117         entry->num_roots       = 1;
118         entry->num_inner_nodes = 0;
119         entry->root            = node;
120         entry->is_dead         = 0;
121         entry->is_tree         = 1;
122         entry->is_ext_ref      = 0;
123         entry->next            = dag_env->list_of_dags;
124         entry->link            = NULL;
125
126         ++dag_env->num_of_dags;
127         dag_env->list_of_dags = entry;
128
129         set_irn_dag_entry(node, entry);
130         return entry;
131 }
132
133 /**
134  * Post-walker to detect DAG roots that are referenced form other blocks
135  */
136 static void find_dag_roots(ir_node *node, void *env)
137 {
138         dag_env_t   *dag_env = (dag_env_t*)env;
139         int         i, arity;
140         dag_entry_t *entry;
141         ir_node     *block;
142
143         if (is_Block(node))
144                 return;
145
146         block = get_nodes_block(node);
147
148         /* ignore start end end blocks */
149         ir_graph *const irg = get_Block_irg(block);
150         if (block == get_irg_start_block(irg) || block == get_irg_end_block(irg))
151                 return;
152
153         /* Phi nodes always references nodes from "other" block */
154         if (is_Phi(node)) {
155                 if (get_irn_mode(node) != mode_M) {
156                         for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
157                                 ir_node *prev = get_irn_n(node, i);
158
159                                 if (is_Phi(prev))
160                                         continue;
161
162                                 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
163                                         if (is_irn_constlike(prev))
164                                                 continue;
165                                 }
166
167                                 entry = get_irn_dag_entry(prev);
168
169                                 if (! entry) {
170                                         /* found an unassigned node, a new root */
171                                         entry = new_dag_entry(dag_env, node);
172                                         entry->is_ext_ref = 1;
173                                 }
174                         }
175                 }
176         } else {
177
178                 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
179                                 ir_node *prev = get_irn_n(node, i);
180                                 ir_mode *mode = get_irn_mode(prev);
181
182                                 if (mode == mode_X || mode == mode_M)
183                                         continue;
184
185                                 if (is_Phi(prev))
186                                         continue;
187
188                                 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
189                                         if (is_irn_constlike(prev))
190                                                 continue;
191                                 }
192
193                                 if (get_nodes_block(prev) != block) {
194                                         /* The predecessor is from another block. It forms
195                                            a root. */
196                                         entry = get_irn_dag_entry(prev);
197                                         if (! entry) {
198                                                 /* found an unassigned node, a new root */
199                                                 entry = new_dag_entry(dag_env, node);
200                                                 entry->is_ext_ref = 1;
201                                         }
202                                 }
203                         }
204         }
205 }
206
207 /**
208  * Pre-walker for connecting DAGs and counting.
209  */
210 static void connect_dags(ir_node *node, void *env)
211 {
212         dag_env_t   *dag_env = (dag_env_t*)env;
213         int         i, arity;
214         ir_node     *block;
215         dag_entry_t *entry;
216         ir_mode     *mode;
217
218         if (is_Block(node))
219                 return;
220
221         block = get_nodes_block(node);
222
223         /* ignore start end end blocks */
224         ir_graph *const irg = get_Block_irg(block);
225         if (block == get_irg_start_block(irg) || block == get_irg_end_block(irg))
226                 return;
227
228         /* ignore Phi nodes */
229         if (is_Phi(node))
230                 return;
231
232         if (dag_env->options & FIRMSTAT_ARGS_ARE_ROOTS && is_arg(node))
233                 return;
234
235         mode = get_irn_mode(node);
236         if (mode == mode_X || mode == mode_M) {
237                 /* do NOT count mode_X and mode_M nodes */
238                 return;
239         }
240
241         /* if this option is set, Loads are always leaves */
242         if (dag_env->options & FIRMSTAT_LOAD_IS_LEAVE && is_Load(node))
243                 return;
244
245         if (dag_env->options & FIRMSTAT_CALL_IS_LEAVE && is_Call(node))
246                 return;
247
248         entry = get_irn_dag_entry(node);
249
250         if (! entry) {
251                 /* found an unassigned node, maybe a new root */
252                 entry = new_dag_entry(dag_env, node);
253         }
254
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);
259
260                 if (is_Phi(prev))
261                         continue;
262
263                 if (mode == mode_X || mode == mode_M)
264                         continue;
265
266                 /*
267                  * copy constants if requested into the DAG's
268                  * beware, do NOT add a link, as this will result in
269                  * wrong intersections
270                  */
271                 if (dag_env->options & FIRMSTAT_COPY_CONSTANTS) {
272                         if (is_irn_constlike(prev)) {
273                                 ++entry->num_nodes;
274                                 ++entry->num_inner_nodes;
275                         }
276                 }
277
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);
281
282                         if (! prev_entry) {
283                                 /* not assigned node, put it into the same DAG */
284                                 set_irn_dag_entry(prev, entry);
285                                 ++entry->num_nodes;
286                                 ++entry->num_inner_nodes;
287                         } else {
288                                 if (prev_entry == entry) {
289                                         /* We found a node that is already assigned to this DAG.
290                                            This DAG is not a tree. */
291                                         entry->is_tree = 0;
292                                 } else {
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;
299
300                                         --dag_env->num_of_dags;
301
302                                         prev_entry->is_dead = 1;
303                                         prev_entry->link    = entry;
304                                 }
305                         }
306                 }
307         }
308 }
309
310 #define DEFAULT_RET     1
311 #define COLOR_RET       1
312
313 static unsigned mark_options;
314
315 /**
316  * a vcg attribute hook
317  */
318 static int stat_dag_mark_hook(FILE *F, const ir_node *n, const ir_node *l)
319 {
320         static const char *colors[] = { "purple", "pink", "lightblue", "orange", "khaki", "orchid", "lilac", "turquoise" };
321         dag_entry_t *entry;
322
323         /* do not count Bad / NoMem */
324         if (l) {
325                 if (is_NoMem(l) || is_Bad(l))
326                         return DEFAULT_RET;
327
328                 /* check for additional options */
329                 if (mark_options & FIRMSTAT_LOAD_IS_LEAVE && is_Load(n))
330                         return DEFAULT_RET;
331
332                 if (mark_options & FIRMSTAT_CALL_IS_LEAVE && is_Call(n))
333                         return DEFAULT_RET;
334         }
335
336         entry = get_irn_dag_entry(n);
337         if (! entry)
338                 return DEFAULT_RET;
339
340         fprintf(F, "color: %s info3: \"DAG id: %u\"", colors[entry->id & 7], entry->id);
341
342         /* I know the color! */
343         return COLOR_RET;
344 }
345
346 /**
347  * count the DAG's size of a graph
348  *
349  * @param global  the global entry
350  * @param graph   the current graph entry
351  */
352 void count_dags_in_graph(graph_entry_t *global, graph_entry_t *graph)
353 {
354         dag_env_t   root_env;
355         dag_entry_t *entry;
356         unsigned id;
357         (void) global;
358
359         /* do NOT check the const code irg */
360         if (graph->irg == get_const_code_irg())
361                 return;
362
363         /* first step, clear the links */
364         irg_walk_graph(graph->irg, firm_clear_link, NULL, NULL);
365
366         obstack_init(&root_env.obst);
367         root_env.num_of_dags  = 0;
368         root_env.list_of_dags = NULL;
369         root_env.options      = FIRMSTAT_COPY_CONSTANTS | FIRMSTAT_LOAD_IS_LEAVE | FIRMSTAT_CALL_IS_LEAVE;
370
371         /* find the DAG roots that are referenced from other block */
372         irg_walk_graph(graph->irg, NULL, find_dag_roots, &root_env);
373
374         /* connect and count them */
375         irg_walk_graph(graph->irg, connect_dags, NULL, &root_env);
376
377         printf("Graph %p %s --- %u\n", (void *)graph->irg, get_entity_name(get_irg_entity(graph->irg)),
378                 root_env.num_of_dags);
379
380         for (id = 0, entry = root_env.list_of_dags; entry; entry = entry->next) {
381                 if (entry->is_dead)
382                         continue;
383                 entry->id = id++;
384
385                 printf("number of roots %u number of nodes %u inner %u tree %u %ld\n",
386                         entry->num_roots,
387                         entry->num_nodes,
388                         entry->num_inner_nodes,
389                         (unsigned)entry->is_tree,
390                         get_irn_node_nr(entry->root));
391         }
392
393         /* dump for test */
394         mark_options = root_env.options;
395         set_dump_node_vcgattr_hook(stat_dag_mark_hook);
396         dump_ir_graph(graph->irg, "dag");
397         set_dump_node_vcgattr_hook(NULL);
398
399         assert(id == root_env.num_of_dags);
400
401         obstack_free(&root_env.obst, NULL);
402 }