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