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