remove $Id$, it doesn't work with git anyway
[libfirm] / ir / ir / irgmod.c
1 /*
2  * Copyright (C) 1995-2008 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    Support for ir graph modification.
23  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier
24  */
25 #include "config.h"
26
27 #include "irflag_t.h"
28 #include "irgwalk.h"
29 #include "irnode_t.h"
30 #include "irgraph_t.h"
31 #include "irgmod.h"
32 #include "array.h"
33 #include "ircons.h"
34 #include "irhooks.h"
35 #include "iredges_t.h"
36 #include "irtools.h"
37 #include "error.h"
38
39 /**
40  * Turns a node into a "useless" Tuple.  The Tuple just forms a tuple
41  * from several inputs.
42  * This is useful if a node returning a tuple is removed, but the Projs
43  * extracting values from the tuple are not available.
44  */
45 void turn_into_tuple(ir_node *node, int arity)
46 {
47         ir_graph *irg = get_irn_irg(node);
48         ir_node **in  = ALLOCAN(ir_node*, arity);
49         ir_node  *bad = new_r_Bad(irg, mode_ANY);
50         int       i;
51
52         /* construct a new in array, with every input being bad */
53         for (i = 0; i < arity; ++i) {
54                 in[i] = bad;
55         }
56         set_irn_in(node, arity, in);
57         set_irn_op(node, op_Tuple);
58 }
59
60 /**
61  * Insert irnode `new' in place of irnode `old'
62  * Since `new' may be bigger than `old' replace `old'
63  * by an op_Id which is smaller than everything.
64  */
65 void exchange(ir_node *old, ir_node *nw)
66 {
67         ir_graph *irg;
68
69         assert(old && nw);
70         assert(old != nw && "Exchanging node with itself is not allowed");
71
72         irg = get_irn_irg(old);
73         assert(irg == get_irn_irg(nw) && "New node must be in same irg as old node");
74
75         hook_replace(old, nw);
76
77         /*
78          * If new outs are on, we can skip the id node creation and reroute
79          * the edges from the old node to the new directly.
80          */
81         if (edges_activated(irg)) {
82                 /* copy all dependencies from old to new */
83                 add_irn_deps(nw, old);
84
85                 edges_reroute(old, nw);
86                 edges_reroute_kind(old, nw, EDGE_KIND_DEP);
87                 edges_node_deleted(old);
88                 /* noone is allowed to reference this node anymore */
89                 set_irn_op(old, op_Deleted);
90         } else {
91                 /* Else, do it the old-fashioned way. */
92                 ir_node *block;
93
94                 hook_turn_into_id(old);
95
96                 block = old->in[0];
97                 if (! block) {
98                         block = is_Block(nw) ? nw : get_nodes_block(nw);
99
100                         if (! block) {
101                                 panic("cannot find legal block for id");
102                         }
103                 }
104
105                 if (get_irn_op(old)->opar == oparity_dynamic) {
106                         DEL_ARR_F(get_irn_in(old));
107                 }
108
109                 old->op    = op_Id;
110                 old->in    = NEW_ARR_D (ir_node *, irg->obst, 2);
111                 old->in[0] = block;
112                 old->in[1] = nw;
113         }
114
115         /* update irg flags */
116         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS
117                            | IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
118 }
119
120 /*--------------------------------------------------------------------*/
121 /*  Functionality for collect_phis                                    */
122 /*--------------------------------------------------------------------*/
123
124 /**
125  * Walker: links all Phi nodes to their Blocks lists,
126  *         all Proj nodes to there predecessors.
127  */
128 static void collect_phiprojs_walker(ir_node *n, void *env)
129 {
130         ir_node *pred;
131         (void) env;
132
133         if (is_Phi(n)) {
134                 ir_node *block = get_nodes_block(n);
135                 add_Block_phi(block, n);
136         } else if (is_Proj(n)) {
137                 pred = n;
138                 do {
139                         pred = get_Proj_pred(pred);
140                 } while (is_Proj(pred));
141
142                 set_irn_link(n, get_irn_link(pred));
143                 set_irn_link(pred, n);
144         }
145 }
146
147 void collect_phiprojs(ir_graph *irg)
148 {
149         assert((ir_resources_reserved(irg) & (IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST)) ==
150                 (IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST));
151         irg_walk_graph(irg, firm_clear_node_and_phi_links, collect_phiprojs_walker, NULL);
152 }
153
154 /*--------------------------------------------------------------------*/
155 /*  Functionality for part_block                                      */
156 /*--------------------------------------------------------------------*/
157
158 /**
159  * Moves node and all predecessors of node from from_bl to to_bl.
160  * Does not move predecessors of Phi nodes (or block nodes).
161  */
162 static void move(ir_node *node, ir_node *from_bl, ir_node *to_bl)
163 {
164         int i, arity;
165
166         /* move this node */
167         set_nodes_block(node, to_bl);
168
169         /* move its Projs */
170         if (get_irn_mode(node) == mode_T) {
171                 ir_node *proj = (ir_node*)get_irn_link(node);
172                 while (proj) {
173                         if (get_nodes_block(proj) == from_bl)
174                                 set_nodes_block(proj, to_bl);
175                         proj = (ir_node*)get_irn_link(proj);
176                 }
177         }
178
179         if (is_Phi(node))
180                 return;
181
182         /* recursion ... */
183         arity = get_irn_arity(node);
184         for (i = 0; i < arity; i++) {
185                 ir_node *pred = get_irn_n(node, i);
186                 if (get_nodes_block(pred) == from_bl)
187                         move(pred, from_bl, to_bl);
188         }
189 }
190
191 static void move_projs(const ir_node *node, ir_node *to_bl)
192 {
193         const ir_edge_t *edge;
194
195         if (get_irn_mode(node) != mode_T)
196                 return;
197
198         foreach_out_edge(node, edge) {
199                 ir_node *proj = get_edge_src_irn(edge);
200                 if (!is_Proj(proj))
201                         continue;
202                 set_nodes_block(proj, to_bl);
203                 move_projs(proj, to_bl);
204         }
205 }
206
207 /**
208  * Moves node and all predecessors of node from from_bl to to_bl.
209  * Does not move predecessors of Phi nodes (or block nodes).
210  */
211 static void move_edges(ir_node *node, ir_node *from_bl, ir_node *to_bl)
212 {
213         int i, arity;
214
215         /* move this node */
216         set_nodes_block(node, to_bl);
217
218         /* move its Projs */
219         move_projs(node, to_bl);
220
221         /* We must not move predecessors of Phi nodes, even if they are in
222          * from_bl. (because these are values from an earlier loop iteration
223          * which are not predecessors of node here)
224          */
225         if (is_Phi(node))
226                 return;
227
228         /* recursion ... */
229         arity = get_irn_arity(node);
230         for (i = 0; i < arity; i++) {
231                 ir_node *pred = get_irn_n(node, i);
232                 if (get_nodes_block(pred) == from_bl)
233                         move_edges(pred, from_bl, to_bl);
234         }
235 }
236
237 void part_block(ir_node *node)
238 {
239         ir_graph *irg = get_irn_irg(node);
240         ir_node  *new_block, *old_block;
241         ir_node  *phi, *jmp;
242
243         /* Turn off optimizations so that blocks are not merged again. */
244         int rem_opt = get_opt_optimize();
245         set_optimize(0);
246
247         /* Transform the control flow */
248         old_block = get_nodes_block(node);
249         new_block = new_r_Block(irg, get_Block_n_cfgpreds(old_block),
250                                 get_Block_cfgpred_arr(old_block));
251
252         /* create a jump from new_block to old_block, which is now the lower one */
253         jmp = new_r_Jmp(new_block);
254         set_irn_in(old_block, 1, &jmp);
255
256         /* move node and its predecessors to new_block */
257         move(node, old_block, new_block);
258
259         /* move Phi nodes to new_block */
260         phi = get_Block_phis(old_block);
261         set_Block_phis(new_block, phi);
262         set_Block_phis(old_block, NULL);
263         while (phi) {
264                 set_nodes_block(phi, new_block);
265                 phi = get_Phi_next(phi);
266         }
267
268         set_optimize(rem_opt);
269 }
270
271 ir_node *part_block_edges(ir_node *node)
272 {
273         ir_graph        *irg       = get_irn_irg(node);
274         ir_node         *old_block = get_nodes_block(node);
275         ir_node         *new_block = new_r_Block(irg,
276                                                  get_Block_n_cfgpreds(old_block),
277                                                  get_Block_cfgpred_arr(old_block));
278         const ir_edge_t *edge;
279         const ir_edge_t *next;
280
281         /* old_block has no predecessors anymore for now */
282         set_irn_in(old_block, 0, NULL);
283
284         /* move node and its predecessors to new_block */
285         move_edges(node, old_block, new_block);
286
287         /* move Phi nodes to new_block */
288         foreach_out_edge_safe(old_block, edge, next) {
289                 ir_node *phi = get_edge_src_irn(edge);
290                 if (!is_Phi(phi))
291                         continue;
292                 set_nodes_block(phi, new_block);
293         }
294
295         return old_block;
296 }
297
298 void kill_node(ir_node *node)
299 {
300         ir_graph *irg = get_irn_irg(node);
301
302         if (edges_activated(irg)) {
303                 edges_node_deleted(node);
304         }
305         /* noone is allowed to reference this node anymore */
306         set_irn_op(node, op_Deleted);
307 }