2 * Contains some useful function for the backend.
3 * @author Sebastian Hack
25 #include "besched_t.h"
28 /* Get an always empty set. */
29 pset *be_empty_set(void)
31 static pset *empty_set = NULL;
34 empty_set = pset_new_ptr(1);
36 assert(pset_count(empty_set) == 0);
45 static void dump_allocated_block(ir_node *block, void *data)
49 struct dump_env *dump_env = data;
50 FILE *f = dump_env->f;
51 arch_env_t *env = dump_env->env;
53 ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"", block);
54 sched_foreach(block, irn) {
55 const char *prefix = "";
57 const arch_register_t *reg = arch_get_irn_register(env, irn);
61 ir_fprintf(f, "%s = ", arch_register_get_name(reg));
63 ir_fprintf(f, "%n(", irn);
65 if(block != get_irg_start_block(get_irn_irg(block))) {
66 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
67 ir_node *op = get_irn_n(irn, i);
68 if(arch_is_register_operand(dump_env->env, op, -1)) {
69 ir_fprintf(f, "%s%s", prefix,
70 arch_register_get_name(arch_get_irn_register(env, op)));
78 ir_fprintf(f, "\"}\n");
80 if(get_irg_start_block(get_irn_irg(block)) != block) {
81 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
82 ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
83 ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", block, pred_bl);
88 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
95 ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
97 if((env.f = fopen(buf, "wt")) != NULL) {
98 fprintf(env.f, "graph:{title:\"prg\"\n");
99 irg_block_walk_graph(irg, dump_allocated_block, NULL, &env);
100 fprintf(env.f, "}\n");
106 * Edge hook to dump the schedule edges.
108 static int sched_edge_hook(FILE *F, ir_node *irn)
110 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
111 ir_node *prev = sched_prev(irn);
112 fprintf(F, "edge:{sourcename:\"");
114 fprintf(F, "\" targetname:\"");
116 fprintf(F, "\" color:magenta}\n");
121 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
122 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
124 dump_consts_local(0);
125 set_dump_node_edge_hook(sched_edge_hook);
126 dump_ir_block_graph(irg, suffix);
127 set_dump_node_edge_hook(old);
130 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix) {
131 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
133 dump_consts_local(0);
134 set_dump_node_edge_hook(sched_edge_hook);
135 dump_ir_extblock_graph(irg, suffix);
136 set_dump_node_edge_hook(old);
140 * Dumps a graph and numbers all dumps.
141 * @param irg The graph
142 * @param suffix A suffix to its file name.
143 * @param dumper The dump function
145 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *)) {
146 static ir_graph *last_irg = NULL;
150 if (irg != last_irg) {
152 nr = strcmp(suffix, "-abi") ? 0 : 1;
155 snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
156 buf[sizeof(buf) - 1] = '\0';
162 static void collect_phis(ir_node *irn, void *data)
165 ir_node *bl = get_nodes_block(irn);
166 set_irn_link(irn, get_irn_link(bl));
167 set_irn_link(bl, irn);
171 void be_clear_links(ir_graph *irg)
173 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
176 void be_collect_phis(ir_graph *irg)
178 irg_walk_graph(irg, collect_phis, NULL, NULL);
181 static void count_num_reachable_nodes(ir_node *irn, void *env) {
186 unsigned get_num_reachable_nodes(ir_graph *irg) {
188 irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
193 * Sets all node inputs to BAD node.
195 void be_kill_node(ir_node *irn) {
202 irg = get_irn_irg(irn);
204 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
205 set_irn_n(irn, i, get_irg_bad(irg));
209 /* FIXME: not used. can be deleted? */
210 ir_node *dom_up_search(pset *accept, ir_node *start_point_exclusive) {
213 /* search the current block */
214 for (irn=sched_prev(start_point_exclusive); irn; irn=sched_prev(irn))
215 if (pset_find_ptr(accept, irn))
218 /* FIXME: This is obviously buggy: after the first recursive call idom is a block
219 and get_nodes_block will fail.
220 Moreover, why not a simple iteration instead of recursion */
221 idom = get_Block_idom(get_nodes_block(start_point_exclusive));
224 return dom_up_search(accept, idom); /* continue search in idom-block */
226 return NULL; /* this was the start block and we did not find an acceptable irn */