Remove the unused parameter const arch_env_t *env from arch_irn_get_flags(), arch_irn...
[libfirm] / ir / be / beutil.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       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
73         ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
74         sched_foreach(block, irn) {
75                 const char *prefix = "";
76
77                 const arch_register_t *reg = arch_get_irn_register(irn);
78
79                 ir_fprintf(f, "\n");
80                 if(reg)
81                         ir_fprintf(f, "%s = ", arch_register_get_name(reg));
82
83                 ir_fprintf(f, "%n(", irn);
84
85                 if(block != get_irg_start_block(get_irn_irg(block))) {
86                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
87                                 ir_node *op = get_irn_n(irn, i);
88                                 if (arch_is_register_operand(op, -1)) {
89                                         ir_fprintf(f, "%s%s", prefix,
90                                                 arch_register_get_name(arch_get_irn_register(op)));
91                                         prefix = ", ";
92                                 }
93                         }
94                 }
95
96                 ir_fprintf(f, ")");
97         }
98         ir_fprintf(f, "\"}\n");
99
100         if(get_irg_start_block(get_irn_irg(block)) != block) {
101                 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
102                         ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
103                         ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", block, pred_bl);
104                 }
105         }
106 }
107
108 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
109 {
110         char buf[1024];
111         struct dump_env env;
112
113         env.env = arch_env;
114
115         ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
116
117         if((env.f = fopen(buf, "wt")) != NULL) {
118                 fprintf(env.f, "graph:{title:\"prg\"\n");
119                 irg_block_walk_graph(irg, dump_allocated_block, NULL, &env);
120                 fprintf(env.f, "}\n");
121                 fclose(env.f);
122         }
123 }
124
125 /**
126  * Edge hook to dump the schedule edges.
127  */
128 static int sched_edge_hook(FILE *F, ir_node *irn)
129 {
130         if (is_Proj(irn))
131                 return 1;
132         if (sched_is_scheduled(irn) && sched_has_prev(irn)) {
133                 ir_node *prev = sched_prev(irn);
134                 fprintf(F, "edge:{sourcename:\"");
135                 PRINT_NODEID(irn);
136                 fprintf(F, "\" targetname:\"");
137                 PRINT_NODEID(prev);
138                 fprintf(F, "\" color:magenta}\n");
139         }
140         return 1;
141 }
142
143 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
144         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
145
146         dump_consts_local(0);
147         if (have_sched_info(irg))
148                 set_dump_node_edge_hook(sched_edge_hook);
149         dump_ir_block_graph(irg, suffix);
150         set_dump_node_edge_hook(old);
151 }
152
153 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
154         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
155
156         dump_consts_local(0);
157         if (have_sched_info(irg))
158                 set_dump_node_edge_hook(sched_edge_hook);
159         dump_ir_extblock_graph(irg, suffix);
160         set_dump_node_edge_hook(old);
161 }
162
163 /**
164  * Dumps a graph and numbers all dumps.
165  * @param irg    The graph
166  * @param suffix A suffix to its file name.
167  * @param dumper The dump function
168  */
169 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
170         static ir_graph *last_irg = NULL;
171         static int       nr       = 0;
172         char             buf[128];
173
174         if (irg != last_irg) {
175                 last_irg = irg;
176                 nr       = strcmp(suffix, "-abi") ? 0 : 1;
177         }
178
179         snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
180         buf[sizeof(buf) - 1] = '\0';
181         dumper(irg, buf);
182 }
183
184
185
186 static void collect_phis(ir_node *irn, void *data)
187 {
188         (void) data;
189         if(is_Phi(irn)) {
190                 ir_node *bl = get_nodes_block(irn);
191                 set_irn_link(irn, get_irn_link(bl));
192                 set_irn_link(bl, irn);
193         }
194 }
195
196 void be_clear_links(ir_graph *irg)
197 {
198         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
199         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
200         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
201 }
202
203 void be_collect_phis(ir_graph *irg)
204 {
205         irg_walk_graph(irg, collect_phis, NULL, NULL);
206 }
207
208 static void count_num_reachable_nodes(ir_node *irn, void *env)
209 {
210         int *num = env;
211         (*num)++;
212         (void) irn;
213 }
214
215 unsigned get_num_reachable_nodes(ir_graph *irg) {
216         int num = 0;
217         irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
218         return num;
219 }
220
221 /**
222  * Gets the Proj with number pn from irn.
223  */
224 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn) {
225         const ir_edge_t *edge;
226         ir_node         *proj;
227         assert(get_irn_mode(irn) == mode_T && "need mode_T");
228
229         foreach_out_edge(irn, edge) {
230                 proj = get_edge_src_irn(edge);
231
232                 if (is_Proj(proj) && get_Proj_proj(proj) == pn)
233                         return proj;
234         }
235
236         return NULL;
237 }
238
239 FILE *be_ffopen(const char *base, const char *ext, const char *mode) {
240         FILE *out;
241         char buf[1024];
242
243         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
244         buf[sizeof(buf) - 1] = '\0';
245         if (! (out = fopen(buf, mode))) {
246                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
247                 return NULL;
248         }
249         return out;
250 }