3de1fa2c724990a68e8b9ba6eb7efa829634a7ef
[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(old != nw);
56   assert (irg);
57   assert(get_irn_op(old)->opar != oparity_dynamic);
58
59   stat_turn_into_id(old);
60
61   block = old->in[0];
62   if (!block) {
63     if (is_Block(nw)) block = nw;
64     else (block = nw->in[0]);
65     if (!block) { DDMN(old); DDMN(nw); assert(0 && "cannot find legal block for id"); }
66   }
67
68   old->op = op_Id;
69   old->in = NEW_ARR_D (ir_node *, irg->obst, 2);
70   old->in[0] = block;
71   old->in[1] = nw;
72 }
73
74 /*--------------------------------------------------------------------*/
75 /*  Functionality for collect_phis                                    */
76 /*--------------------------------------------------------------------*/
77
78 static void
79 clear_link (ir_node *n, void *env) {
80   set_irn_link(n, NULL);
81 }
82
83 static void
84 collect (ir_node *n, void *env) {
85   ir_node *pred;
86   if (get_irn_op(n) == op_Phi) {
87     set_irn_link(n, get_irn_link(get_nodes_block(n)));
88     set_irn_link(get_nodes_block(n), n);
89   }
90   if (get_irn_op(n) == op_Proj) {
91     pred = n;
92     while (get_irn_op(pred) == op_Proj)
93       pred = get_Proj_pred(pred);
94     set_irn_link(n, get_irn_link(pred));
95     set_irn_link(pred, n);
96   }
97 }
98
99 void collect_phiprojs(ir_graph *irg) {
100   ir_graph *rem;
101
102   /* Remember external state of current_ir_graph. */
103   rem = current_ir_graph;
104   current_ir_graph = irg;
105
106   irg_walk(get_irg_end(current_ir_graph), clear_link, collect, NULL);
107
108   current_ir_graph = rem;
109 }
110
111
112 /*--------------------------------------------------------------------*/
113 /*  Functionality for part_block                                      */
114 /*--------------------------------------------------------------------*/
115
116 /**
117  * Moves node and all predecessors of node from from_bl to to_bl.
118  * Does not move predecessors of Phi nodes (or block nodes).
119  */
120 static void move (ir_node *node, ir_node *from_bl, ir_node *to_bl) {
121   int i, arity;
122   ir_node *proj, *pred;
123
124   /* move this node */
125   set_nodes_block(node, to_bl);
126
127   /* move its projs */
128   if (get_irn_mode(node) == mode_T) {
129     proj = get_irn_link(node);
130     while (proj) {
131       if (get_nodes_block(proj) == from_bl)
132         set_nodes_block(proj, to_bl);
133       proj = get_irn_link(proj);
134     }
135   }
136
137   /* recursion ... */
138   if (get_irn_op(node) == op_Phi) return;
139
140   arity = get_irn_arity(node);
141   for (i = 0; i < arity; i++) {
142     pred = get_irn_n(node, i);
143     if (get_nodes_block(pred) == from_bl)
144       move(pred, from_bl, to_bl);
145   }
146 }
147
148 void part_block(ir_node *node) {
149   ir_node *new_block;
150   ir_node *old_block;
151   ir_node *phi;
152
153   /* Turn off optimizations so that blocks are not merged again. */
154   int rem_opt = get_opt_optimize();
155   set_optimize(0);
156
157   /* Transform the control flow */
158   old_block = get_nodes_block(node);
159   new_block = new_Block(get_Block_n_cfgpreds(old_block),
160             get_Block_cfgpred_arr(old_block));
161   set_irg_current_block(current_ir_graph, new_block);
162   {
163     ir_node *in[1];
164     in[0] = new_Jmp();
165     set_irn_in(old_block, 1, in);
166     irn_vrfy_irg(old_block, current_ir_graph);
167   }
168
169   /* move node and its predecessors to new_block */
170   move(node, old_block, new_block);
171
172   /* move Phi nodes to new_block */
173   phi = get_irn_link(old_block);
174   set_irn_link(new_block, phi);
175   set_irn_link(old_block, NULL);
176   while (phi) {
177     if(get_nodes_block(phi) == old_block);   /* @@@ inlinening chokes on phis that don't
178                            obey this condition.  How do they get into
179                            the list??? Example: InterfaceIII */
180       set_nodes_block(phi, new_block);
181     phi = get_irn_link(phi);
182   }
183
184   set_optimize(rem_opt);
185 }