use get_irn_n(-1) instead of get_nodes_block
[libfirm] / ir / common / irtools.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Some often needed tool-functions
23  * @author    Michael Beck
24  * @version   $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "pset.h"
31
32 #include <stdlib.h>
33 #include "irnode_t.h"
34 #include "irbackedge_t.h"
35 #include "irtools.h"
36 #include "irprintf.h"
37
38 /* the famous clear_link implementation. */
39 void firm_clear_link(ir_node *n, void *env) {
40   (void) env;
41   set_irn_link(n, NULL);
42 }
43
44 /*
45  * Copies a node to a new irg. The Ins of the new node point to
46  * the predecessors on the old irg.  n->link points to the new node.
47  *
48  * Does NOT copy standard nodes like Start, End etc that are fixed
49  * in an irg. Instead, the corresponding nodes of the new irg are returned.
50  * Note further, that the new nodes have no block.
51  */
52 void
53 copy_irn_to_irg(ir_node *n, ir_graph *irg)
54 {
55   ir_op *op = get_irn_op(n);
56   ir_graph *old_irg;
57   ir_node *nn = NULL;
58
59   /* do not copy standard nodes */
60   if (op == op_Bad)
61     nn = get_irg_bad(irg);
62   else if (op == op_NoMem)
63     n = get_irg_no_mem(irg);
64   else if (op == op_Block) {
65     old_irg = get_irn_irg(n);
66
67     if (n == get_irg_start_block(old_irg))
68       nn = get_irg_start_block(irg);
69     else if (n == get_irg_end_block(old_irg))
70       nn = get_irg_end_block(irg);
71   }
72   else if (op == op_Start)
73     nn = get_irg_start(irg);
74   else if (op == op_End)
75     nn = get_irg_end(irg);
76   else if (op == op_Proj) {
77     old_irg = get_irn_irg(n);
78
79     if (n == get_irg_frame(old_irg))
80       nn = get_irg_frame(irg);
81     else if (n == get_irg_globals(old_irg))
82       nn = get_irg_globals(irg);
83     else if (n == get_irg_initial_mem(old_irg))
84       nn = get_irg_initial_mem(irg);
85     else if (n == get_irg_args(old_irg))
86       nn = get_irg_args(irg);
87   }
88
89   if (nn) {
90     set_irn_link(n, nn);
91     return;
92   }
93
94   nn = new_ir_node(get_irn_dbg_info(n),
95          irg,
96          NULL,            /* no block yet, will be set later */
97          op,
98          get_irn_mode(n),
99          get_irn_arity(n),
100          get_irn_in(n) + 1);
101
102
103   /* Copy the attributes.  These might point to additional data.  If this
104      was allocated on the old obstack the pointers now are dangling.  This
105      frees e.g. the memory of the graph_arr allocated in new_immBlock. */
106   copy_node_attr(n, nn);
107   new_backedge_info(nn);
108   set_irn_link(n, nn);
109
110   /* fix the irg for blocks */
111   if (is_Block(nn))
112     nn->attr.block.irg = irg;
113 }
114
115 /*
116  * Creates an exact copy of a node.
117  * The copy resides in the same graph in the same block.
118  */
119 ir_node *exact_copy(const ir_node *n) {
120         ir_graph *irg = get_irn_irg(n);
121         ir_node *res, *block = NULL;
122
123         if (is_no_Block(n))
124                 block = get_irn_n(n, -1);
125
126         res = new_ir_node(get_irn_dbg_info(n),
127                 irg,
128                 block,
129                 get_irn_op(n),
130                 get_irn_mode(n),
131                 get_irn_arity(n),
132                 get_irn_in(n) + 1);
133
134
135         /* Copy the attributes.  These might point to additional data.  If this
136            was allocated on the old obstack the pointers now are dangling.  This
137            frees e.g. the memory of the graph_arr allocated in new_immBlock. */
138         copy_node_attr(n, res);
139         new_backedge_info(res);
140         return res;
141 }
142
143 void firm_pset_dump(pset *set)
144 {
145         void *obj;
146
147         foreach_pset(set, obj) {
148                 ir_fprintf(stderr, "%+F\n", obj);
149         }
150 }