2 * Copyright (C) 1995-2007 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 Support for ir graph modification.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier
34 #include "irgraph_t.h"
39 #include "iredges_t.h"
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.
49 void turn_into_tuple(ir_node *node, int arity) {
51 set_irn_op(node, op_Tuple);
52 if (get_irn_arity(node) == arity) {
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 edges_node_deleted(node, current_ir_graph);
58 /* Allocate new array, don't free old in_array, it's on the obstack. */
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 the block back */
63 set_irn_n(node, -1, block);
68 * Insert irnode `new' in place of irnode `old'
69 * Since `new' may be bigger than `old' replace `old'
70 * by an op_Id which is smaller than everything.
72 void exchange(ir_node *old, ir_node *nw) {
76 assert(old != nw && "Exchanging node with itself is not allowed");
78 irg = get_irn_irg(old);
79 assert(irg == get_irn_irg(nw) && "New node must be in same irg as old node");
81 hook_replace(old, nw);
84 * If new outs are on, we can skip the id node creation and reroute
85 * the edges from the old node to the new directly.
87 if (edges_activated(irg)) {
88 /* copy all dependencies from old to new */
89 add_irn_deps(nw, old);
91 edges_reroute(old, nw, irg);
92 edges_reroute_kind(old, nw, EDGE_KIND_DEP, irg);
93 edges_node_deleted(old, irg);
96 /* Else, do it the old-fashioned way. */
99 assert(get_irn_op(old)->opar != oparity_dynamic);
101 hook_turn_into_id(old);
105 block = is_Block(nw) ? nw : get_nodes_block(nw);
108 panic("cannot find legal block for id");
113 old->in = NEW_ARR_D (ir_node *, irg->obst, 2);
119 /*--------------------------------------------------------------------*/
120 /* Functionality for collect_phis */
121 /*--------------------------------------------------------------------*/
124 * Walker: links all Phi nodes to their Blocks and
125 * all Proj nodes to there predecessors
127 static void collect(ir_node *n, void *env) {
132 set_irn_link(n, get_irn_link(get_nodes_block(n)));
133 set_irn_link(get_nodes_block(n), n);
134 } else if (is_Proj(n)) {
137 pred = get_Proj_pred(pred);
138 } while (is_Proj(pred));
140 set_irn_link(n, get_irn_link(pred));
141 set_irn_link(pred, n);
145 void collect_phiprojs(ir_graph *irg) {
146 irg_walk_graph(irg, firm_clear_link, collect, NULL);
150 /*--------------------------------------------------------------------*/
151 /* Functionality for part_block */
152 /*--------------------------------------------------------------------*/
155 * Moves node and all predecessors of node from from_bl to to_bl.
156 * Does not move predecessors of Phi nodes (or block nodes).
158 static void move(ir_node *node, ir_node *from_bl, ir_node *to_bl) {
160 ir_node *proj, *pred;
163 set_nodes_block(node, to_bl);
166 if (get_irn_mode(node) == mode_T) {
167 proj = get_irn_link(node);
169 if (get_nodes_block(proj) == from_bl)
170 set_nodes_block(proj, to_bl);
171 proj = get_irn_link(proj);
176 if (get_irn_op(node) == op_Phi) return;
178 arity = get_irn_arity(node);
179 for (i = 0; i < arity; i++) {
180 pred = get_irn_n(node, i);
181 if (get_nodes_block(pred) == from_bl)
182 move(pred, from_bl, to_bl);
186 void part_block(ir_node *node) {
191 /* Turn off optimizations so that blocks are not merged again. */
192 int rem_opt = get_opt_optimize();
195 /* Transform the control flow */
196 old_block = get_nodes_block(node);
197 new_block = new_Block(get_Block_n_cfgpreds(old_block),
198 get_Block_cfgpred_arr(old_block));
199 set_irg_current_block(current_ir_graph, new_block);
201 ir_node *jmp = new_Jmp();
202 set_irn_in(old_block, 1, &jmp);
203 irn_vrfy_irg(old_block, current_ir_graph);
206 /* move node and its predecessors to new_block */
207 move(node, old_block, new_block);
209 /* move Phi nodes to new_block */
210 phi = get_irn_link(old_block);
211 set_irn_link(new_block, phi);
212 set_irn_link(old_block, NULL);
214 set_nodes_block(phi, new_block);
215 phi = get_irn_link(phi);
218 set_optimize(rem_opt);
221 /* kill a node by setting its predecessors to Bad and finally exchange the node by Bad itself. */
222 void kill_node(ir_node *node) {
223 ir_graph *irg = get_irn_irg(node);
224 ir_node *bad = get_irg_bad(irg);
227 for (i = get_irn_arity(node) - 1; i >= -1; --i) {
228 set_irn_n(node, i, bad);