2 * Contains some useful function for the backend.
3 * @author Sebastian Hack
26 #include "besched_t.h"
29 /* Get an always empty set. */
30 pset *be_empty_set(void)
32 static pset *empty_set = NULL;
35 empty_set = pset_new_ptr(1);
37 assert(pset_count(empty_set) == 0);
46 static void dump_allocated_block(ir_node *block, void *data)
50 struct dump_env *dump_env = data;
51 FILE *f = dump_env->f;
52 arch_env_t *env = dump_env->env;
54 ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
55 sched_foreach(block, irn) {
56 const char *prefix = "";
58 const arch_register_t *reg = arch_get_irn_register(env, irn);
62 ir_fprintf(f, "%s = ", arch_register_get_name(reg));
64 ir_fprintf(f, "%n(", irn);
66 if(block != get_irg_start_block(get_irn_irg(block))) {
67 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
68 ir_node *op = get_irn_n(irn, i);
69 if(arch_is_register_operand(dump_env->env, op, -1)) {
70 ir_fprintf(f, "%s%s", prefix,
71 arch_register_get_name(arch_get_irn_register(env, op)));
79 ir_fprintf(f, "\"}\n");
81 if(get_irg_start_block(get_irn_irg(block)) != block) {
82 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
83 ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
84 ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", block, pred_bl);
89 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
96 ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
98 if((env.f = fopen(buf, "wt")) != NULL) {
99 fprintf(env.f, "graph:{title:\"prg\"\n");
100 irg_block_walk_graph(irg, dump_allocated_block, NULL, &env);
101 fprintf(env.f, "}\n");
107 * Edge hook to dump the schedule edges.
109 static int sched_edge_hook(FILE *F, ir_node *irn)
111 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
112 ir_node *prev = sched_prev(irn);
113 fprintf(F, "edge:{sourcename:\"");
115 fprintf(F, "\" targetname:\"");
117 fprintf(F, "\" color:magenta}\n");
122 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
123 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
125 dump_consts_local(0);
126 set_dump_node_edge_hook(sched_edge_hook);
127 dump_ir_block_graph(irg, suffix);
128 set_dump_node_edge_hook(old);
131 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
132 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
134 dump_consts_local(0);
135 set_dump_node_edge_hook(sched_edge_hook);
136 dump_ir_extblock_graph(irg, suffix);
137 set_dump_node_edge_hook(old);
141 * Dumps a graph and numbers all dumps.
142 * @param irg The graph
143 * @param suffix A suffix to its file name.
144 * @param dumper The dump function
146 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
147 static ir_graph *last_irg = NULL;
151 if (irg != last_irg) {
153 nr = strcmp(suffix, "-abi") ? 0 : 1;
156 snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
157 buf[sizeof(buf) - 1] = '\0';
163 static void collect_phis(ir_node *irn, void *data)
166 ir_node *bl = get_nodes_block(irn);
167 set_irn_link(irn, get_irn_link(bl));
168 set_irn_link(bl, irn);
172 void be_clear_links(ir_graph *irg)
174 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
177 void be_collect_phis(ir_graph *irg)
179 irg_walk_graph(irg, collect_phis, NULL, NULL);
182 static void count_num_reachable_nodes(ir_node *irn, void *env) {
187 unsigned get_num_reachable_nodes(ir_graph *irg) {
189 irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
194 * Sets all node inputs to BAD node.
196 void be_kill_node(ir_node *irn) {
203 irg = get_irn_irg(irn);
204 first = 0 - ! is_Block(irn);
206 for (i = get_irn_arity(irn) - 1; i >= first; --i) {
207 set_irn_n(irn, i, get_irg_bad(irg));
211 /* FIXME: not used. can be deleted? */
212 ir_node *dom_up_search(pset *accept, ir_node *start_point_exclusive) {
215 /* search the current block */
216 for (irn=sched_prev(start_point_exclusive); irn; irn=sched_prev(irn))
217 if (pset_find_ptr(accept, irn))
220 /* FIXME: This is obviously buggy: after the first recursive call idom is a block
221 and get_nodes_block will fail.
222 Moreover, why not a simple iteration instead of recursion */
223 idom = get_Block_idom(get_nodes_block(start_point_exclusive));
226 return dom_up_search(accept, idom); /* continue search in idom-block */
228 return NULL; /* this was the start block and we did not find an acceptable irn */
232 * Gets the Proj with number pn from irn.
234 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn) {
235 const ir_edge_t *edge;
237 assert(get_irn_mode(irn) == mode_T && "need mode_T");
239 foreach_out_edge(irn, edge) {
240 proj = get_edge_src_irn(edge);
242 if (get_Proj_proj(proj) == pn)