remove #ifdef HAVE_CONFIG_Hs
[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 #include "config.h"
27
28 #include <stdio.h>
29
30 #include "pset.h"
31
32 #include "irgraph.h"
33 #include "irgwalk.h"
34 #include "irdump_t.h"
35 #include "irdom_t.h"
36 #include "ircons.h"
37 #include "iropt.h"
38 #include "irgopt.h"
39 #include "irtools.h"
40 #include "irprintf.h"
41 #include "iredges.h"
42
43 #include "beutil.h"
44 #include "besched_t.h"
45 #include "bearch_t.h"
46
47 /* Get an always empty set. */
48 pset *be_empty_set(void)
49 {
50         static pset *empty_set = NULL;
51
52         if(!empty_set)
53                 empty_set = pset_new_ptr(1);
54
55         assert(pset_count(empty_set) == 0);
56         return empty_set;
57 }
58
59 static void dump_allocated_block(ir_node *block, void *data)
60 {
61         FILE          *f = data;
62         const ir_node *irn;
63         int            n;
64         int            i;
65
66         ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
67         sched_foreach(block, irn) {
68                 const char *prefix = "";
69
70                 const arch_register_t *reg = arch_get_irn_register(irn);
71
72                 ir_fprintf(f, "\n");
73                 if(reg)
74                         ir_fprintf(f, "%s = ", arch_register_get_name(reg));
75
76                 ir_fprintf(f, "%n(", irn);
77
78                 if(block != get_irg_start_block(get_irn_irg(block))) {
79                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
80                                 ir_node *op = get_irn_n(irn, i);
81                                 if (arch_is_register_operand(op, -1)) {
82                                         ir_fprintf(f, "%s%s", prefix,
83                                                 arch_register_get_name(arch_get_irn_register(op)));
84                                         prefix = ", ";
85                                 }
86                         }
87                 }
88
89                 ir_fprintf(f, ")");
90         }
91         ir_fprintf(f, "\"}\n");
92
93         if(get_irg_start_block(get_irn_irg(block)) != block) {
94                 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
95                         ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
96                         ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", block, pred_bl);
97                 }
98         }
99 }
100
101 void dump_allocated_irg(ir_graph *irg, char *suffix)
102 {
103         char  buf[1024];
104         FILE *f;
105
106         ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
107
108         f = fopen(buf, "wt");
109         if (f != NULL) {
110                 fprintf(f, "graph:{title:\"prg\"\n");
111                 irg_block_walk_graph(irg, dump_allocated_block, NULL, f);
112                 fprintf(f, "}\n");
113                 fclose(f);
114         }
115 }
116
117 /**
118  * Edge hook to dump the schedule edges.
119  */
120 static int sched_edge_hook(FILE *F, ir_node *irn)
121 {
122         if (is_Proj(irn))
123                 return 1;
124         if (sched_is_scheduled(irn) && sched_has_prev(irn)) {
125                 ir_node *prev = sched_prev(irn);
126                 fprintf(F, "edge:{sourcename:\"");
127                 PRINT_NODEID(irn);
128                 fprintf(F, "\" targetname:\"");
129                 PRINT_NODEID(prev);
130                 fprintf(F, "\" color:magenta}\n");
131         }
132         return 1;
133 }
134
135 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
136         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
137
138         dump_consts_local(0);
139         if (have_sched_info(irg))
140                 set_dump_node_edge_hook(sched_edge_hook);
141         dump_ir_block_graph(irg, suffix);
142         set_dump_node_edge_hook(old);
143 }
144
145 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
146         DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
147
148         dump_consts_local(0);
149         if (have_sched_info(irg))
150                 set_dump_node_edge_hook(sched_edge_hook);
151         dump_ir_extblock_graph(irg, suffix);
152         set_dump_node_edge_hook(old);
153 }
154
155 /**
156  * Dumps a graph and numbers all dumps.
157  * @param irg    The graph
158  * @param suffix A suffix to its file name.
159  * @param dumper The dump function
160  */
161 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
162         static ir_graph *last_irg = NULL;
163         static int       nr       = 0;
164         char             buf[128];
165
166         if (irg != last_irg) {
167                 last_irg = irg;
168                 nr       = strcmp(suffix, "-abi") ? 0 : 1;
169         }
170
171         snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
172         buf[sizeof(buf) - 1] = '\0';
173         dumper(irg, buf);
174 }
175
176
177
178 static void collect_phis(ir_node *irn, void *data)
179 {
180         (void) data;
181         if(is_Phi(irn)) {
182                 ir_node *bl = get_nodes_block(irn);
183                 set_irn_link(irn, get_irn_link(bl));
184                 set_irn_link(bl, irn);
185         }
186 }
187
188 void be_clear_links(ir_graph *irg)
189 {
190         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
191         irg_walk_graph(irg, firm_clear_link, NULL, NULL);
192         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
193 }
194
195 void be_collect_phis(ir_graph *irg)
196 {
197         irg_walk_graph(irg, collect_phis, NULL, NULL);
198 }
199
200 static void count_num_reachable_nodes(ir_node *irn, void *env)
201 {
202         int *num = env;
203         (*num)++;
204         (void) irn;
205 }
206
207 unsigned get_num_reachable_nodes(ir_graph *irg) {
208         int num = 0;
209         irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
210         return num;
211 }
212
213 /**
214  * Gets the Proj with number pn from irn.
215  */
216 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn) {
217         const ir_edge_t *edge;
218         ir_node         *proj;
219         assert(get_irn_mode(irn) == mode_T && "need mode_T");
220
221         foreach_out_edge(irn, edge) {
222                 proj = get_edge_src_irn(edge);
223
224                 if (is_Proj(proj) && get_Proj_proj(proj) == pn)
225                         return proj;
226         }
227
228         return NULL;
229 }
230
231 FILE *be_ffopen(const char *base, const char *ext, const char *mode) {
232         FILE *out;
233         char buf[1024];
234
235         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
236         buf[sizeof(buf) - 1] = '\0';
237         if (! (out = fopen(buf, mode))) {
238                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
239                 return NULL;
240         }
241         return out;
242 }