optimize next_use calculation (quadratic in number of outs not number of nodes in...
[libfirm] / ir / be / beutil.c
1 /*
2  * Copyright (C) 1995-2007 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       Contains some useful function for the backend.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #include "pset.h"
33
34 #include "irgraph.h"
35 #include "irgwalk.h"
36 #include "irdump_t.h"
37 #include "irdom_t.h"
38 #include "ircons.h"
39 #include "iropt.h"
40 #include "irgopt.h"
41 #include "irtools.h"
42 #include "irprintf.h"
43 #include "iredges.h"
44
45 #include "beutil.h"
46 #include "besched_t.h"
47 #include "bearch_t.h"
48
49 /* Get an always empty set. */
50 pset *be_empty_set(void)
51 {
52         static pset *empty_set = NULL;
53
54         if(!empty_set)
55                 empty_set = pset_new_ptr(1);
56
57         assert(pset_count(empty_set) == 0);
58         return empty_set;
59 }
60
61 struct dump_env {
62         FILE *f;
63         arch_env_t *env;
64 };
65
66 static void dump_allocated_block(ir_node *block, void *data)
67 {
68         int i, n;
69         const ir_node *irn;
70         struct dump_env *dump_env = data;
71         FILE *f = dump_env->f;
72         arch_env_t *env = dump_env->env;
73
74         ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
75         sched_foreach(block, irn) {
76                 const char *prefix = "";
77
78                 const arch_register_t *reg = arch_get_irn_register(env, irn);
79
80                 ir_fprintf(f, "\n");
81                 if(reg)
82                         ir_fprintf(f, "%s = ", arch_register_get_name(reg));
83
84                 ir_fprintf(f, "%n(", irn);
85
86                 if(block != get_irg_start_block(get_irn_irg(block))) {
87                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
88                                 ir_node *op = get_irn_n(irn, i);
89                                 if(arch_is_register_operand(dump_env->env, op, -1)) {
90                                         ir_fprintf(f, "%s%s", prefix,
91                                                 arch_register_get_name(arch_get_irn_register(env, op)));
92                                         prefix = ", ";
93                                 }
94                         }
95                 }
96
97                 ir_fprintf(f, ")");
98         }
99         ir_fprintf(f, "\"}\n");
100
101         if(get_irg_start_block(get_irn_irg(block)) != block) {
102                 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
103                         ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
104                         ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", block, pred_bl);
105                 }
106         }
107 }
108
109 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
110 {
111         char buf[1024];
112         struct dump_env env;
113
114         env.env = arch_env;
115
116         ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
117
118         if((env.f = fopen(buf, "wt")) != NULL) {
119                 fprintf(env.f, "graph:{title:\"prg\"\n");
120                 irg_block_walk_graph(irg, dump_allocated_block, NULL, &env);
121                 fprintf(env.f, "}\n");
122                 fclose(env.f);
123         }
124 }
125
126 /**
127  * Edge hook to dump the schedule edges.
128  */
129 static int sched_edge_hook(FILE *F, ir_node *irn)
130 {
131         if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
132                 ir_node *prev = sched_prev(irn);
133                 fprintf(F, "edge:{sourcename:\"");
134                 PRINT_NODEID(irn);
135                 fprintf(F, "\" targetname:\"");
136                 PRINT_NODEID(prev);
137                 fprintf(F, "\" color:magenta}\n");
138         }
139         return 1;
140 }
141
142 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
143         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
144
145         dump_consts_local(0);
146         set_dump_node_edge_hook(sched_edge_hook);
147         dump_ir_block_graph(irg, suffix);
148         set_dump_node_edge_hook(old);
149 }
150
151 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
152         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
153
154         dump_consts_local(0);
155         set_dump_node_edge_hook(sched_edge_hook);
156         dump_ir_extblock_graph(irg, suffix);
157         set_dump_node_edge_hook(old);
158 }
159
160 /**
161  * Dumps a graph and numbers all dumps.
162  * @param irg    The graph
163  * @param suffix A suffix to its file name.
164  * @param dumper The dump function
165  */
166 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
167         static ir_graph *last_irg = NULL;
168         static int       nr       = 0;
169         char             buf[128];
170
171         if (irg != last_irg) {
172                 last_irg = irg;
173                 nr       = strcmp(suffix, "-abi") ? 0 : 1;
174         }
175
176         snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
177         buf[sizeof(buf) - 1] = '\0';
178         dumper(irg, buf);
179 }
180
181
182
183 static void collect_phis(ir_node *irn, void *data)
184 {
185   if(is_Phi(irn)) {
186     ir_node *bl = get_nodes_block(irn);
187     set_irn_link(irn, get_irn_link(bl));
188     set_irn_link(bl, irn);
189   }
190 }
191
192 void be_clear_links(ir_graph *irg)
193 {
194         set_using_irn_link(irg);
195         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
196         clear_using_irn_link(irg);
197 }
198
199 void be_collect_phis(ir_graph *irg)
200 {
201         irg_walk_graph(irg, collect_phis, NULL, NULL);
202 }
203
204 static void count_num_reachable_nodes(ir_node *irn, void *env) {
205         int *num = env;
206         (*num)++;
207 }
208
209 unsigned get_num_reachable_nodes(ir_graph *irg) {
210         int num = 0;
211         irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
212         return num;
213 }
214
215 /**
216  * Sets all node inputs to BAD node.
217  */
218 void be_kill_node(ir_node *irn) {
219         ir_graph *irg = get_irn_irg(irn);
220
221         assert(!is_Bad(irn));
222
223 #ifdef DEBUG_libfirm
224         {
225         int i, first;
226         first = 0 - ! is_Block(irn);
227
228         for (i = get_irn_arity(irn) - 1; i >= first; --i) {
229                 set_irn_n(irn, i, get_irg_bad(irg));
230         }
231         }
232 #endif
233
234         edges_node_deleted(irn, irg);
235 }
236
237 /* FIXME: not used. can be deleted? */
238 ir_node *dom_up_search(pset *accept, ir_node *start_point_exclusive) {
239         ir_node *irn, *idom;
240
241         /* search the current block */
242         for (irn=sched_prev(start_point_exclusive); irn; irn=sched_prev(irn))
243                 if (pset_find_ptr(accept, irn))
244                         return irn;
245
246         /* FIXME: This is obviously buggy: after the first recursive call idom is a block
247            and get_nodes_block will fail.
248                  Moreover, why not a simple iteration instead of recursion */
249         idom = get_Block_idom(get_nodes_block(start_point_exclusive));
250
251         if (idom)
252                 return dom_up_search(accept, idom); /* continue search in idom-block */
253         else
254                 return NULL; /* this was the start block and we did not find an acceptable irn */
255 }
256
257 /**
258  * Gets the Proj with number pn from irn.
259  */
260 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn) {
261         const ir_edge_t *edge;
262         ir_node         *proj;
263         assert(get_irn_mode(irn) == mode_T && "need mode_T");
264
265         foreach_out_edge(irn, edge) {
266                 proj = get_edge_src_irn(edge);
267
268                 if (get_Proj_proj(proj) == pn)
269                         return proj;
270         }
271
272         return NULL;
273 }
274
275 FILE *be_ffopen(const char *base, const char *ext, const char *mode) {
276         FILE *out;
277         char buf[1024];
278
279         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
280         buf[sizeof(buf) - 1] = '\0';
281         if (! (out = fopen(buf, mode))) {
282                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
283                 return NULL;
284         }
285         return out;
286 }