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