2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Contains some useful function for the backend.
23 * @author Sebastian Hack
46 #include "besched_t.h"
49 /* Get an always empty set. */
50 pset *be_empty_set(void)
52 static pset *empty_set = NULL;
55 empty_set = pset_new_ptr(1);
57 assert(pset_count(empty_set) == 0);
61 static void dump_allocated_block(ir_node *block, void *data)
68 ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
69 sched_foreach(block, irn) {
70 const char *prefix = "";
72 const arch_register_t *reg = arch_get_irn_register(irn);
76 ir_fprintf(f, "%s = ", arch_register_get_name(reg));
78 ir_fprintf(f, "%n(", irn);
80 if(block != get_irg_start_block(get_irn_irg(block))) {
81 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
82 ir_node *op = get_irn_n(irn, i);
83 if (arch_is_register_operand(op, -1)) {
84 ir_fprintf(f, "%s%s", prefix,
85 arch_register_get_name(arch_get_irn_register(op)));
93 ir_fprintf(f, "\"}\n");
95 if(get_irg_start_block(get_irn_irg(block)) != block) {
96 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
97 ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
98 ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", block, pred_bl);
103 void dump_allocated_irg(ir_graph *irg, char *suffix)
108 ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
110 f = fopen(buf, "wt");
112 fprintf(f, "graph:{title:\"prg\"\n");
113 irg_block_walk_graph(irg, dump_allocated_block, NULL, f);
120 * Edge hook to dump the schedule edges.
122 static int sched_edge_hook(FILE *F, ir_node *irn)
126 if (sched_is_scheduled(irn) && sched_has_prev(irn)) {
127 ir_node *prev = sched_prev(irn);
128 fprintf(F, "edge:{sourcename:\"");
130 fprintf(F, "\" targetname:\"");
132 fprintf(F, "\" color:magenta}\n");
137 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
138 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
140 dump_consts_local(0);
141 if (have_sched_info(irg))
142 set_dump_node_edge_hook(sched_edge_hook);
143 dump_ir_block_graph(irg, suffix);
144 set_dump_node_edge_hook(old);
147 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
148 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
150 dump_consts_local(0);
151 if (have_sched_info(irg))
152 set_dump_node_edge_hook(sched_edge_hook);
153 dump_ir_extblock_graph(irg, suffix);
154 set_dump_node_edge_hook(old);
158 * Dumps a graph and numbers all dumps.
159 * @param irg The graph
160 * @param suffix A suffix to its file name.
161 * @param dumper The dump function
163 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
164 static ir_graph *last_irg = NULL;
168 if (irg != last_irg) {
170 nr = strcmp(suffix, "-abi") ? 0 : 1;
173 snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
174 buf[sizeof(buf) - 1] = '\0';
180 static void collect_phis(ir_node *irn, void *data)
184 ir_node *bl = get_nodes_block(irn);
185 set_irn_link(irn, get_irn_link(bl));
186 set_irn_link(bl, irn);
190 void be_clear_links(ir_graph *irg)
192 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
193 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
194 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
197 void be_collect_phis(ir_graph *irg)
199 irg_walk_graph(irg, collect_phis, NULL, NULL);
202 static void count_num_reachable_nodes(ir_node *irn, void *env)
209 unsigned get_num_reachable_nodes(ir_graph *irg) {
211 irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
216 * Gets the Proj with number pn from irn.
218 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn) {
219 const ir_edge_t *edge;
221 assert(get_irn_mode(irn) == mode_T && "need mode_T");
223 foreach_out_edge(irn, edge) {
224 proj = get_edge_src_irn(edge);
226 if (is_Proj(proj) && get_Proj_proj(proj) == pn)
233 FILE *be_ffopen(const char *base, const char *ext, const char *mode) {
237 snprintf(buf, sizeof(buf), "%s.%s", base, ext);
238 buf[sizeof(buf) - 1] = '\0';
239 if (! (out = fopen(buf, mode))) {
240 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);