- New callback to ask nodes about constant stack pointer adjustment
[libfirm] / ir / be / beutil.c
1 /**
2  * Contains some useful function for the backend.
3  * @author Sebastian Hack
4  * @cvsid  $Id$
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <stdio.h>
11
12 #include "pset.h"
13
14 #include "irgraph.h"
15 #include "irgwalk.h"
16 #include "irdump_t.h"
17 #include "irdom_t.h"
18 #include "ircons.h"
19 #include "iropt.h"
20 #include "irgopt.h"
21 #include "irtools.h"
22 #include "irprintf.h"
23
24 #include "beutil.h"
25 #include "besched_t.h"
26 #include "bearch.h"
27
28 /* Get an always empty set. */
29 pset *be_empty_set(void)
30 {
31         static pset *empty_set = NULL;
32
33         if(!empty_set)
34                 empty_set = pset_new_ptr(1);
35
36         assert(pset_count(empty_set) == 0);
37         return empty_set;
38 }
39
40 struct dump_env {
41         FILE *f;
42         arch_env_t *env;
43 };
44
45 static void dump_allocated_block(ir_node *block, void *data)
46 {
47         int i, n;
48         const ir_node *irn;
49         struct dump_env *dump_env = data;
50         FILE *f = dump_env->f;
51         arch_env_t *env = dump_env->env;
52
53         ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
54         sched_foreach(block, irn) {
55                 const char *prefix = "";
56
57                 const arch_register_t *reg = arch_get_irn_register(env, irn);
58
59                 ir_fprintf(f, "\n");
60                 if(reg)
61                         ir_fprintf(f, "%s = ", arch_register_get_name(reg));
62
63                 ir_fprintf(f, "%n(", irn);
64
65                 if(block != get_irg_start_block(get_irn_irg(block))) {
66                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
67                                 ir_node *op = get_irn_n(irn, i);
68                                 if(arch_is_register_operand(dump_env->env, op, -1)) {
69                                         ir_fprintf(f, "%s%s", prefix,
70                                                 arch_register_get_name(arch_get_irn_register(env, op)));
71                                         prefix = ", ";
72                                 }
73                         }
74                 }
75
76                 ir_fprintf(f, ")");
77         }
78         ir_fprintf(f, "\"}\n");
79
80         if(get_irg_start_block(get_irn_irg(block)) != block) {
81                 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
82                         ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
83                         ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", block, pred_bl);
84                 }
85         }
86 }
87
88 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
89 {
90         char buf[1024];
91         struct dump_env env;
92
93         env.env = arch_env;
94
95         ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
96
97         if((env.f = fopen(buf, "wt")) != NULL) {
98                 fprintf(env.f, "graph:{title:\"prg\"\n");
99                 irg_block_walk_graph(irg, dump_allocated_block, NULL, &env);
100                 fprintf(env.f, "}\n");
101                 fclose(env.f);
102         }
103 }
104
105 static void localize_const_walker(ir_node *irn, void *data)
106 {
107         if(!is_Block(irn)) {
108                 int i, n;
109
110                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
111                         ir_node *op = get_irn_n(irn, i);
112                         if(get_irn_opcode(op) == iro_Const) {
113                                 ir_node *tgt_block, *cnst;
114
115                                 /* Special treatment for phi nodes, because phi-usage is different */
116                                 tgt_block = get_nodes_block(irn);
117                                 if(is_Phi(irn))
118                                         tgt_block = get_nodes_block(get_irn_n(tgt_block, i));
119
120                                 /*
121                                  * We have to create the const node by ourselves, since the
122                                  * firmcons implementation always places it in the start block.
123                                  */
124                                 cnst = new_ir_node(NULL, get_irn_irg(irn),
125                                                 tgt_block, op_Const, get_irn_mode(op), 0, NULL);
126                                 cnst->attr.con.tv = get_Const_tarval(op);
127                                 set_irn_n(irn, i, cnst);
128                         }
129                 }
130         }
131 }
132
133 void localize_consts(ir_graph *irg)
134 {
135         irg_walk_graph(irg, localize_const_walker, NULL, NULL);
136         dead_node_elimination(irg);
137 }
138
139 /**
140  * Edge hook to dump the schedule edges.
141  */
142 static int sched_edge_hook(FILE *F, ir_node *irn)
143 {
144         if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
145                 ir_node *prev = sched_prev(irn);
146                 fprintf(F, "edge:{sourcename:\"");
147                 PRINT_NODEID(irn);
148                 fprintf(F, "\" targetname:\"");
149                 PRINT_NODEID(prev);
150                 fprintf(F, "\" color:magenta}\n");
151         }
152         return 1;
153 }
154
155 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
156         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
157
158         dump_consts_local(0);
159         set_dump_node_edge_hook(sched_edge_hook);
160         dump_ir_block_graph(irg, suffix);
161         set_dump_node_edge_hook(old);
162 }
163
164 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
165         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
166
167         dump_consts_local(0);
168         set_dump_node_edge_hook(sched_edge_hook);
169         dump_ir_extblock_graph(irg, suffix);
170         set_dump_node_edge_hook(old);
171 }
172
173 /**
174  * Dumps a graph and numbers all dumps.
175  * @param irg    The graph
176  * @param suffix A suffix to its file name.
177  * @param dumper The dump function
178  */
179 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
180         static ir_graph *last_irg = NULL;
181         static int       nr       = 0;
182         char             buf[128];
183
184         if (irg != last_irg) {
185                 last_irg = irg;
186                 nr       = 0;
187         }
188
189         snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
190         buf[sizeof(buf) - 1] = '\0';
191         dumper(irg, buf);
192 }
193
194
195
196 static void collect_phis(ir_node *irn, void *data)
197 {
198   if(is_Phi(irn)) {
199     ir_node *bl = get_nodes_block(irn);
200     set_irn_link(irn, get_irn_link(bl));
201     set_irn_link(bl, irn);
202   }
203 }
204
205 void be_clear_links(ir_graph *irg)
206 {
207         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
208 }
209
210 void be_collect_phis(ir_graph *irg)
211 {
212         irg_walk_graph(irg, collect_phis, NULL, NULL);
213 }
214
215 static void count_num_reachable_nodes(ir_node *irn, void *env) {
216         int *num = env;
217         (*num)++;
218 }
219
220 unsigned get_num_reachable_nodes(ir_graph *irg) {
221         int num = 0;
222         irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
223         return num;
224 }
225
226 /* FIXME: not used. can be deleted? */
227 ir_node *dom_up_search(pset *accept, ir_node *start_point_exclusive) {
228         ir_node *irn, *idom;
229
230         /* search the current block */
231         for (irn=sched_prev(start_point_exclusive); irn; irn=sched_prev(irn))
232                 if (pset_find_ptr(accept, irn))
233                         return irn;
234
235         /* FIXME: This is obviously buggy: after the first recursive call idom is a block
236            and get_nodes_block will fail.
237                  Moreover, why not a simple iteration instead of recursion */
238         idom = get_Block_idom(get_nodes_block(start_point_exclusive));
239
240         if (idom)
241                 return dom_up_search(accept, idom); /* continue search in idom-block */
242         else
243                 return NULL; /* this was the start block and we did not find an acceptable irn */
244 }