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
41 #include "iredges_t.h"
47 /* Get an always empty set. */
48 pset *be_empty_set(void)
50 static pset *empty_set = NULL;
53 empty_set = pset_new_ptr(1);
55 assert(pset_count(empty_set) == 0);
60 * Edge hook to dump the schedule edges.
62 static int sched_edge_hook(FILE *F, ir_node *irn)
66 if (sched_is_scheduled(irn) && sched_has_prev(irn)) {
67 ir_node *prev = sched_prev(irn);
68 fprintf(F, "edge:{sourcename:\"");
70 fprintf(F, "\" targetname:\"");
72 fprintf(F, "\" color:magenta}\n");
77 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix)
79 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
82 if (have_sched_info(irg))
83 set_dump_node_edge_hook(sched_edge_hook);
84 dump_ir_block_graph(irg, suffix);
85 set_dump_node_edge_hook(old);
88 void dump_ir_extblock_graph_sched(ir_graph *irg, const char *suffix)
90 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
93 if (have_sched_info(irg))
94 set_dump_node_edge_hook(sched_edge_hook);
95 dump_ir_extblock_graph(irg, suffix);
96 set_dump_node_edge_hook(old);
100 * Dumps a graph and numbers all dumps.
101 * @param irg The graph
102 * @param suffix A suffix to its file name.
103 * @param dumper The dump function
105 void be_dump(ir_graph *irg, const char *suffix, void (*dumper)(ir_graph *, const char *))
107 static ir_graph *last_irg = NULL;
111 if (irg != last_irg) {
113 nr = strcmp(suffix, "-abi") ? 0 : 1;
116 snprintf(buf, sizeof(buf), "-%02d%s", nr++, suffix);
117 buf[sizeof(buf) - 1] = '\0';
121 void be_clear_links(ir_graph *irg)
123 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
126 static void count_num_reachable_nodes(ir_node *irn, void *env)
133 unsigned get_num_reachable_nodes(ir_graph *irg)
136 irg_walk_graph(irg, count_num_reachable_nodes, NULL, &num);
141 * Gets the Proj with number pn from irn.
143 ir_node *be_get_Proj_for_pn(const ir_node *irn, long pn)
145 const ir_edge_t *edge;
147 assert(get_irn_mode(irn) == mode_T && "need mode_T");
149 foreach_out_edge(irn, edge) {
150 proj = get_edge_src_irn(edge);
152 if (is_Proj(proj) && get_Proj_proj(proj) == pn)
159 FILE *be_ffopen(const char *base, const char *ext, const char *mode)
164 snprintf(buf, sizeof(buf), "%s.%s", base, ext);
165 buf[sizeof(buf) - 1] = '\0';
166 if (! (out = fopen(buf, mode))) {
167 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
173 static void add_to_postorder(ir_node *block, void *data)
175 ir_node ***list = (ir_node***) data;
176 ARR_APP1(ir_node*, *list, block);
179 ir_node **be_get_cfgpostorder(ir_graph *irg)
181 ir_node **list = NEW_ARR_F(ir_node*, 0);
182 ir_node *end_block = get_irg_end_block(irg);
184 /* end block may be unreachable in case of endless loops */
185 if (get_Block_n_cfgpreds(end_block) == 0)
186 ARR_APP1(ir_node*, list, end_block);
189 irg_block_edges_walk(get_irg_start_block(irg), NULL, add_to_postorder,
195 ir_node *get_first_block_succ(const ir_node *block)
197 const ir_edge_t *edge = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
198 assert(edge != NULL);
199 return get_edge_src_irn(edge);