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 Some often needed tool-functions
23 * @author Michael Beck
32 #include "irbackedge_t.h"
37 /* the famous clear_link implementation. */
38 void firm_clear_link(ir_node *n, void *env) {
40 set_irn_link(n, NULL);
43 /* the famous clear_node_and_phi_links() implementation. */
44 void firm_clear_node_and_phi_links(ir_node *n, void *env) {
46 set_irn_link(n, NULL);
48 set_Block_phis(n, NULL);
50 set_Phi_next(n, NULL);
54 * Copies a node to a new irg. The Ins of the new node point to
55 * the predecessors on the old irg. n->link points to the new node.
57 * Does NOT copy standard nodes like Start, End etc that are fixed
58 * in an irg. Instead, the corresponding nodes of the new irg are returned.
59 * Note further, that the new nodes have no block.
62 copy_irn_to_irg(ir_node *n, ir_graph *irg) {
63 ir_op *op = get_irn_op(n);
67 /* do not copy standard nodes */
69 nn = get_irg_bad(irg);
70 else if (op == op_NoMem)
71 n = get_irg_no_mem(irg);
72 else if (op == op_Block) {
73 old_irg = get_irn_irg(n);
75 if (n == get_irg_start_block(old_irg))
76 nn = get_irg_start_block(irg);
77 else if (n == get_irg_end_block(old_irg))
78 nn = get_irg_end_block(irg);
80 else if (op == op_Start)
81 nn = get_irg_start(irg);
82 else if (op == op_End)
83 nn = get_irg_end(irg);
84 else if (op == op_Proj) {
85 old_irg = get_irn_irg(n);
87 if (n == get_irg_initial_exec(old_irg))
88 nn = get_irg_initial_exec(irg);
89 else if (n == get_irg_frame(old_irg))
90 nn = get_irg_frame(irg);
91 else if (n == get_irg_initial_mem(old_irg))
92 nn = get_irg_initial_mem(irg);
93 else if (n == get_irg_args(old_irg))
94 nn = get_irg_args(irg);
102 nn = new_ir_node(get_irn_dbg_info(n),
104 NULL, /* no block yet, will be set later */
111 /* Copy the attributes. These might point to additional data. If this
112 was allocated on the old obstack the pointers now are dangling. This
113 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
114 copy_node_attr(n, nn);
115 new_backedge_info(nn);
118 /* fix the irg for blocks */
120 nn->attr.block.irg.irg = irg;
122 /* we cannot allow blocks WITHOUT macroblock input */
123 set_Block_MacroBlock(nn, get_Block_MacroBlock(n));
128 * Creates an exact copy of a node.
129 * The copy resides in the same graph in the same block.
131 ir_node *exact_copy(const ir_node *n) {
132 ir_graph *irg = get_irn_irg(n);
133 ir_node *res, *block = NULL;
136 block = get_nodes_block(n);
138 res = new_ir_node(get_irn_dbg_info(n),
147 /* Copy the attributes. These might point to additional data. If this
148 was allocated on the old obstack the pointers now are dangling. This
149 frees e.g. the memory of the graph_arr allocated in new_immBlock. */
150 copy_node_attr(n, res);
151 new_backedge_info(res);
154 set_Block_MacroBlock(res, get_Block_MacroBlock(n));
160 * Dump a pset containing Firm objects.
162 void firm_pset_dump(pset *set) {
165 foreach_pset(set, obj) {
166 ir_fprintf(stderr, "%+F\n", obj);
171 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
173 static int void_graph_wrapper(ir_graph *irg, void *context) {
174 void (*function)(ir_graph *irg) = context;
177 } /* void_graph_wrapper */
179 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
180 ir_graph_pass_t *def_graph_pass(
181 const char *name, void (*function)(ir_graph *irg))
183 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
185 pass->kind = k_ir_graph_pass;
186 pass->run_on_irg = void_graph_wrapper;
187 pass->context = function;
190 INIT_LIST_HEAD(&pass->list);
193 } /* def_graph_pass */
196 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
198 static int int_graph_wrapper(ir_graph *irg, void *context) {
199 int (*function)(ir_graph *irg) = context;
200 return function(irg);
201 } /* int_graph_wrapper */
203 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
204 ir_graph_pass_t *def_graph_pass_ret(
205 const char *name, int (*function)(ir_graph *irg))
207 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
209 pass->kind = k_ir_graph_pass;
210 pass->run_on_irg = int_graph_wrapper;
211 pass->context = function;
214 INIT_LIST_HEAD(&pass->list);
217 } /* def_graph_pass_ret */
220 * Wrapper for running void function(void) as an ir_prog pass.
222 static int void_prog_wrapper(ir_prog *irp, void *context) {
223 void (*function)(void) = context;
228 } /* void_graph_wrapper */
230 /* Creates an ir_prog pass for running void function(void). */
231 ir_prog_pass_t *def_prog_pass(
233 void (*function)(void))
235 struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
237 pass->kind = k_ir_prog_pass;
238 pass->run_on_irprog = void_prog_wrapper;
239 pass->context = function;
242 INIT_LIST_HEAD(&pass->list);
245 } /* def_prog_pass */