0c7c4cbf87d7bc290fcfac97bf47590490648642
[libfirm] / ir / ir / irgmod.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irgmod.h
4  * Purpose:     Support for ir graph modification.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 # include "irvrfy.h"
18 # include "irflag_t.h"
19 # include "irgwalk.h"
20 # include "irnode_t.h"
21 # include "irgraph_t.h"
22 # include "irgmod.h"
23 # include "array.h"
24 # include "ircons.h"
25 # include "firmstat.h"
26
27 /* Turns a node into a "useless" Tuple.  The Tuple just forms a tuple
28    from several inputs.
29    This is useful if a node returning a tuple is removed, but the Projs
30    extracting values from the tuple are not available. */
31 void
32 turn_into_tuple (ir_node *node, int arity)
33 {
34   assert(node);
35   set_irn_op(node, op_Tuple);
36   if (get_irn_arity(node) == arity) {
37     /* keep old array */
38   } else {
39     /* Allocate new array, don't free old in_array, it's on the obstack. */
40     ir_node *block = get_nodes_Block(node);
41     node->in = NEW_ARR_D (ir_node *, current_ir_graph->obst, arity+1);
42     set_nodes_Block(node, block);
43   }
44 }
45
46 /* Insert irnode `new' in place of irnode `old'
47    Since `new' may be bigger than `old' replace `old'
48    by an op_Id which is smaller than everything */
49 INLINE void
50 exchange (ir_node *old, ir_node *nw)
51 {
52   ir_node *block;
53   ir_graph *irg = get_irn_irg (old);
54
55   assert (irg);
56   assert(get_irn_op(old)->opar != oparity_dynamic);
57
58   stat_turn_into_id(old);
59
60   block = old->in[0];
61
62   old->op = op_Id;
63   old->in = NEW_ARR_D (ir_node *, irg->obst, 2);
64   old->in[0] = block;
65   old->in[1] = nw;
66 }
67
68 /*--------------------------------------------------------------------*/
69 /*  Functionality for collect_phis                                    */
70 /*--------------------------------------------------------------------*/
71
72 static void
73 clear_link (ir_node *n, void *env) {
74   set_irn_link(n, NULL);
75 }
76
77 static void
78 collect (ir_node *n, void *env) {
79   ir_node *pred;
80   if (get_irn_op(n) == op_Phi) {
81     set_irn_link(n, get_irn_link(get_nodes_Block(n)));
82     set_irn_link(get_nodes_Block(n), n);
83   }
84   if (get_irn_op(n) == op_Proj) {
85     pred = n;
86     while (get_irn_op(pred) == op_Proj)
87       pred = get_Proj_pred(pred);
88     set_irn_link(n, get_irn_link(pred));
89     set_irn_link(pred, n);
90   }
91 }
92
93 void collect_phiprojs(ir_graph *irg) {
94   ir_graph *rem;
95
96   /* Remember external state of current_ir_graph. */
97   rem = current_ir_graph;
98   current_ir_graph = irg;
99
100   irg_walk(get_irg_end(current_ir_graph), clear_link, collect, NULL);
101
102   current_ir_graph = rem;
103 }
104
105
106 /*--------------------------------------------------------------------*/
107 /*  Functionality for part_block                                      */
108 /*--------------------------------------------------------------------*/
109
110 /**
111  * Moves node and all predecessors of node from from_bl to to_bl.
112  * Does not move predecessors of Phi nodes (or block nodes).
113  */
114 static void move (ir_node *node, ir_node *from_bl, ir_node *to_bl) {
115   int i;
116   ir_node *proj, *pred;
117
118   /* move this node */
119   set_nodes_Block(node, to_bl);
120
121   /* move its projs */
122   if (get_irn_mode(node) == mode_T) {
123     proj = get_irn_link(node);
124     while (proj) {
125       if (get_nodes_Block(proj) == from_bl)
126     set_nodes_Block(proj, to_bl);
127       proj = get_irn_link(proj);
128     }
129   }
130
131   /* recursion ... */
132   if (get_irn_op(node) == op_Phi) return;
133
134   for (i = 0; i < get_irn_arity(node); i++) {
135     pred = get_irn_n(node, i);
136     if (get_nodes_Block(pred) == from_bl)
137       move(pred, from_bl, to_bl);
138   }
139 }
140
141 void part_block(ir_node *node) {
142   ir_node *new_block;
143   ir_node *old_block;
144   ir_node *phi;
145
146   /* Turn off optimizations so that blocks are not merged again. */
147   int rem_opt = get_opt_optimize();
148   set_optimize(0);
149
150   /* Transform the control flow */
151   old_block = get_nodes_Block(node);
152   new_block = new_Block(get_Block_n_cfgpreds(old_block),
153             get_Block_cfgpred_arr(old_block));
154   set_irg_current_block(current_ir_graph, new_block);
155   {
156     ir_node *in[1];
157     in[0] = new_Jmp();
158     set_irn_in(old_block, 1, in);
159     irn_vrfy_irg(old_block, current_ir_graph);
160   }
161
162   /* move node and its predecessors to new_block */
163   move(node, old_block, new_block);
164
165   /* move Phi nodes to new_block */
166   phi = get_irn_link(old_block);
167   set_irn_link(new_block, phi);
168   set_irn_link(old_block, NULL);
169   while (phi) {
170     if(get_nodes_Block(phi) == old_block);   /* @@@ inlinening chokes on phis that don't
171                            obey this condition.  How do they get into
172                            the list??? Example: InterfaceIII */
173       set_nodes_Block(phi, new_block);
174     phi = get_irn_link(phi);
175   }
176
177   set_optimize(rem_opt);
178 }