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