2 * Copyright (C) 1995-2007 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
21 * Contains some useful function for the backend.
22 * @author Sebastian Hack
45 #include "besched_t.h"
48 /* Get an always empty set. */
49 pset *be_empty_set(void)
51 static pset *empty_set = NULL;
54 empty_set = pset_new_ptr(1);
56 assert(pset_count(empty_set) == 0);
65 static void dump_allocated_block(ir_node *block, void *data)
69 struct dump_env *dump_env = data;
70 FILE *f = dump_env->f;
71 arch_env_t *env = dump_env->env;
73 ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
74 sched_foreach(block, irn) {
75 const char *prefix = "";
77 const arch_register_t *reg = arch_get_irn_register(env, irn);
81 ir_fprintf(f, "%s = ", arch_register_get_name(reg));
83 ir_fprintf(f, "%n(", irn);
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(dump_env->env, op, -1)) {
89 ir_fprintf(f, "%s%s", prefix,
90 arch_register_get_name(arch_get_irn_register(env, op)));
98 ir_fprintf(f, "\"}\n");
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);
108 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
115 ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
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");
126 * Edge hook to dump the schedule edges.
128 static int sched_edge_hook(FILE *F, ir_node *irn)
130 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
131 ir_node *prev = sched_prev(irn);
132 fprintf(F, "edge:{sourcename:\"");
134 fprintf(F, "\" targetname:\"");
136 fprintf(F, "\" color:magenta}\n");
141 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
142 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
144 dump_consts_local(0);
145 set_dump_node_edge_hook(sched_edge_hook);
146 dump_ir_block_graph(irg, suffix);
147 set_dump_node_edge_hook(old);
150 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
151 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
153 dump_consts_local(0);
154 set_dump_node_edge_hook(sched_edge_hook);
155 dump_ir_extblock_graph(irg, suffix);
156 set_dump_node_edge_hook(old);
160 * Dumps a graph and numbers all dumps.
161 * @param irg The graph
162 * @param suffix A suffix to its file name.
163 * @param dumper The dump function
165 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
166 static ir_graph *last_irg = NULL;
170 if (irg != last_irg) {
172 nr = strcmp(suffix, "-abi") ? 0 : 1;
175 snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
176 buf[sizeof(buf) - 1] = '\0';
182 static void collect_phis(ir_node *irn, void *data)
185 ir_node *bl = get_nodes_block(irn);
186 set_irn_link(irn, get_irn_link(bl));
187 set_irn_link(bl, irn);
191 void be_clear_links(ir_graph *irg)
193 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
196 void be_collect_phis(ir_graph *irg)
198 irg_walk_graph(irg, collect_phis, NULL, NULL);
201 static void count_num_reachable_nodes(ir_node *irn, void *env) {
206 unsigned get_num_reachable_nodes(ir_graph *irg) {
208 irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
213 * Sets all node inputs to BAD node.
215 void be_kill_node(ir_node *irn) {
216 ir_graph *irg = get_irn_irg(irn);
218 assert(!is_Bad(irn));
223 first = 0 - ! is_Block(irn);
225 for (i = get_irn_arity(irn) - 1; i >= first; --i) {
226 set_irn_n(irn, i, get_irg_bad(irg));
231 edges_node_deleted(irn, irg);
234 /* FIXME: not used. can be deleted? */
235 ir_node *dom_up_search(pset *accept, ir_node *start_point_exclusive) {
238 /* search the current block */
239 for (irn=sched_prev(start_point_exclusive); irn; irn=sched_prev(irn))
240 if (pset_find_ptr(accept, irn))
243 /* FIXME: This is obviously buggy: after the first recursive call idom is a block
244 and get_nodes_block will fail.
245 Moreover, why not a simple iteration instead of recursion */
246 idom = get_Block_idom(get_nodes_block(start_point_exclusive));
249 return dom_up_search(accept, idom); /* continue search in idom-block */
251 return NULL; /* this was the start block and we did not find an acceptable irn */
255 * Gets the Proj with number pn from irn.
257 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn) {
258 const ir_edge_t *edge;
260 assert(get_irn_mode(irn) == mode_T && "need mode_T");
262 foreach_out_edge(irn, edge) {
263 proj = get_edge_src_irn(edge);
265 if (get_Proj_proj(proj) == pn)