added new licence header
[libfirm] / ir / ir / irgmod.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  * Project:     libFIRM
22  * File name:   ir/ir/irgmod.c
23  * Purpose:     Support for ir graph modification.
24  * Author:      Martin Trapp, Christian Schaefer
25  * Modified by: Goetz Lindenmaier
26  * Created:
27  * CVS-ID:      $Id$
28  * Copyright:   (c) 1998-2003 Universität Karlsruhe
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include "irvrfy.h"
36 #include "irflag_t.h"
37 #include "irgwalk.h"
38 #include "irnode_t.h"
39 #include "irgraph_t.h"
40 #include "irgmod.h"
41 #include "array.h"
42 #include "ircons.h"
43 #include "irhooks.h"
44 #include "iredges_t.h"
45 #include "irtools.h"
46
47 /**
48  * Turns a node into a "useless" Tuple.  The Tuple just forms a tuple
49  * from several inputs.
50  * This is useful if a node returning a tuple is removed, but the Projs
51  * extracting values from the tuple are not available.
52  */
53 void turn_into_tuple (ir_node *node, int arity)
54 {
55         assert(node);
56         set_irn_op(node, op_Tuple);
57         if (get_irn_arity(node) == arity) {
58                 /* keep old array */
59         } else {
60                 /* Allocate new array, don't free old in_array, it's on the obstack. */
61                 ir_node *block = get_nodes_block(node);
62                 edges_node_deleted(node, current_ir_graph);
63                 node->in = NEW_ARR_D(ir_node *, current_ir_graph->obst, arity+1);
64                 /* clear the new in array, else edge_notify tries to delete garbage */
65                 memset(node->in, 0, (arity+1) * sizeof(node->in[0]));
66                 set_nodes_block(node, block);
67         }
68 }
69
70 /**
71  * Insert irnode `new' in place of irnode `old'
72  * Since `new' may be bigger than `old' replace `old'
73  * by an op_Id which is smaller than everything.
74  */
75 void exchange(ir_node *old, ir_node *nw)
76 {
77         ir_graph *irg;
78
79         assert(old && nw);
80         assert(old != nw && "Exchanging node with itself is not allowed");
81
82         irg = get_irn_irg(old);
83         assert(irg == get_irn_irg(nw) && "New node must be in same irg as old node");
84
85         hook_replace(old, nw);
86
87         /*
88         * If new outs are on, we can skip the id node creation and reroute
89         * the edges from the old node to the new directly.
90         */
91         if (edges_activated(irg)) {
92                 /* copy all dependencies from old to new */
93                 add_irn_deps(nw, old);
94
95                 edges_reroute(old, nw, irg);
96                 edges_reroute_kind(old, nw, EDGE_KIND_DEP, irg);
97                 edges_node_deleted(old, irg);
98                 old->op = op_Bad;
99         }
100         else {
101                 /* Else, do it the old-fashioned way. */
102                 ir_node *block;
103
104                 assert(get_irn_op(old)->opar != oparity_dynamic);
105
106                 hook_turn_into_id(old);
107
108                 block = old->in[0];
109                 if (! block) {
110                         block = is_Block(nw) ? nw : get_nodes_block(nw);
111
112                         if (! block) {
113                                 DDMN(old);
114                                 DDMN(nw);
115                                 assert(0 && "cannot find legal block for id");
116                         }
117                 }
118
119                 old->op    = op_Id;
120                 old->in    = NEW_ARR_D (ir_node *, irg->obst, 2);
121                 old->in[0] = block;
122                 old->in[1] = nw;
123         }
124 }
125
126 /*--------------------------------------------------------------------*/
127 /*  Functionality for collect_phis                                    */
128 /*--------------------------------------------------------------------*/
129
130 /**
131  * Walker: links all Phi nodes to their Blocks and
132  *         all Proj nodes to there predecessors
133  */
134 static void collect(ir_node *n, void *env) {
135         ir_node *pred;
136
137         if (is_Phi(n)) {
138                 set_irn_link(n, get_irn_link(get_nodes_block(n)));
139                 set_irn_link(get_nodes_block(n), n);
140         }
141         else if (is_Proj(n)) {
142                 pred = n;
143                 do {
144                         pred = get_Proj_pred(pred);
145                 } while (is_Proj(pred));
146
147                 set_irn_link(n, get_irn_link(pred));
148                 set_irn_link(pred, n);
149         }
150 }
151
152 void collect_phiprojs(ir_graph *irg) {
153         irg_walk_graph(irg, firm_clear_link, collect, NULL);
154 }
155
156
157 /*--------------------------------------------------------------------*/
158 /*  Functionality for part_block                                      */
159 /*--------------------------------------------------------------------*/
160
161 /**
162  * Moves node and all predecessors of node from from_bl to to_bl.
163  * Does not move predecessors of Phi nodes (or block nodes).
164  */
165 static void move(ir_node *node, ir_node *from_bl, ir_node *to_bl) {
166         int i, arity;
167         ir_node *proj, *pred;
168
169         /* move this node */
170         set_nodes_block(node, to_bl);
171
172         /* move its projs */
173         if (get_irn_mode(node) == mode_T) {
174                 proj = get_irn_link(node);
175                 while (proj) {
176                         if (get_nodes_block(proj) == from_bl)
177                                 set_nodes_block(proj, to_bl);
178                         proj = get_irn_link(proj);
179                 }
180         }
181
182         /* recursion ... */
183         if (get_irn_op(node) == op_Phi) return;
184
185         arity = get_irn_arity(node);
186         for (i = 0; i < arity; i++) {
187                 pred = get_irn_n(node, i);
188                 if (get_nodes_block(pred) == from_bl)
189                         move(pred, from_bl, to_bl);
190         }
191 }
192
193 void part_block(ir_node *node) {
194         ir_node *new_block;
195         ir_node *old_block;
196         ir_node *phi;
197
198         /* Turn off optimizations so that blocks are not merged again. */
199         int rem_opt = get_opt_optimize();
200         set_optimize(0);
201
202         /* Transform the control flow */
203         old_block = get_nodes_block(node);
204         new_block = new_Block(get_Block_n_cfgpreds(old_block),
205                 get_Block_cfgpred_arr(old_block));
206         set_irg_current_block(current_ir_graph, new_block);
207         {
208                 ir_node *jmp = new_Jmp();
209                 set_irn_in(old_block, 1, &jmp);
210                 irn_vrfy_irg(old_block, current_ir_graph);
211         }
212
213         /* move node and its predecessors to new_block */
214         move(node, old_block, new_block);
215
216         /* move Phi nodes to new_block */
217         phi = get_irn_link(old_block);
218         set_irn_link(new_block, phi);
219         set_irn_link(old_block, NULL);
220         while (phi) {
221                 if(get_nodes_block(phi) == old_block);   /* @@@ inlinening chokes on phis that don't
222                                                                                                  obey this condition.  How do they get into
223                                                                                                  the list??? Example: InterfaceIII */
224                 set_nodes_block(phi, new_block);
225                 phi = get_irn_link(phi);
226         }
227
228         set_optimize(rem_opt);
229 }