sparc: preliminary SwitchJmp implementation
[libfirm] / ir / be / beflags.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       modifies schedule so flags dependencies are respected.
23  * @author      Matthias Braun, Christoph Mallon
24  * @version     $Id: besched.h 14693 2007-06-21 15:35:49Z beck $
25  *
26  * Fixup schedule to respect flag constraints by moving and rematerialisation of
27  * nodes.
28  *
29  * Flags are modeled as register classes with ignore registers. However to avoid
30  * bloating the graph, only flag-consumer -> producer dependencies are
31  * explicitely modeled in the graph. Nodes that just change the flags are only
32  * marked with the arch_irn_flags_modify_flags flag.
33  *
34  * Flags are usually a limited resource that can't (or at least shouldn't) be
35  * spilled. So in some situations (for example 2 adc-nodes that use the flags of
36  * a single add node on x86) operations have to be repeated to work correctly.
37  */
38 #include "config.h"
39
40 #include <stdbool.h>
41
42 #include "irgwalk.h"
43 #include "irnode_t.h"
44 #include "irtools.h"
45 #include "ircons.h"
46 #include "iredges_t.h"
47 #include "irprintf.h"
48 #include "error.h"
49
50 #include "beflags.h"
51 #include "bearch.h"
52 #include "beirg.h"
53 #include "besched.h"
54 #include "benode.h"
55 #include "belive.h"
56
57 static const arch_register_class_t *flag_class;
58 static const arch_register_t       *flags_reg;
59 static func_rematerialize           remat;
60 static check_modifies_flags         check_modify;
61 static int                          changed;
62
63 static ir_node *default_remat(ir_node *node, ir_node *after)
64 {
65         ir_node *block, *copy;
66         if (is_Block(after))
67                 block = after;
68         else
69                 block = get_nodes_block(after);
70
71         copy = exact_copy(node);
72         set_nodes_block(copy, block);
73         sched_add_after(after, copy);
74
75         return copy;
76 }
77
78 static bool default_check_modifies(const ir_node *node)
79 {
80         return arch_irn_is(node, modify_flags);
81 }
82
83 /**
84  * tests whether we can legally move node node after node after
85  * (only works for nodes in same block)
86  */
87 static bool can_move(ir_node *node, ir_node *after)
88 {
89         const ir_edge_t *edge;
90         assert(get_nodes_block(node) == get_nodes_block(after));
91
92         /* TODO respect dep edges */
93         assert(get_irn_n_edges_kind(node, EDGE_KIND_DEP) == 0);
94
95         /** all users have to be after the after node */
96         foreach_out_edge(node, edge) {
97                 ir_node *out = get_edge_src_irn(edge);
98                 if (is_Proj(out)) {
99                         const ir_edge_t *edge2;
100                         assert(get_irn_n_edges_kind(out, EDGE_KIND_DEP) == 0);
101                         foreach_out_edge(out, edge2) {
102                                 ir_node *out2 = get_edge_src_irn(edge2);
103                                 /* Phi or End represents a usage at block end. */
104                                 if (is_Phi(out2) || is_End(out2))
105                                         continue;
106                                 if (is_Sync(out2)) {
107                                         const ir_edge_t *edge3;
108                                         foreach_out_edge(out2, edge3) {
109                                                 ir_node *out3 = get_edge_src_irn(edge3);
110                                                 /* Phi or End represents a usage at block end. */
111                                                 if (is_Phi(out3) || is_End(out3))
112                                                         continue;
113                                                 assert(!is_Sync(out3));
114                                                 if (sched_get_time_step(out3) <= sched_get_time_step(after)) {
115                                                         return false;
116                                                 }
117                                         }
118                                 } else if (sched_get_time_step(out2) <= sched_get_time_step(after)) {
119                                         return false;
120                                 }
121                         }
122                 } else {
123                         /* phi represents a usage at block end */
124                         if (is_Phi(out))
125                                 continue;
126                         if (sched_get_time_step(out) <= sched_get_time_step(after)) {
127                                 return false;
128                         }
129                 }
130         }
131
132         return true;
133 }
134
135 static void rematerialize_or_move(ir_node *flags_needed, ir_node *node,
136                                   ir_node *flag_consumers, int pn)
137 {
138         ir_node *n;
139         ir_node *copy;
140         ir_node *value;
141
142         if (!is_Block(node) &&
143                         get_nodes_block(flags_needed) == get_nodes_block(node) &&
144                         can_move(flags_needed, node)) {
145                 /* move it */
146                 sched_remove(flags_needed);
147                 sched_add_after(node, flags_needed);
148                 /* No need to update liveness, because the node stays in the same block */
149                 return;
150         }
151
152         changed = 1;
153         copy    = remat(flags_needed, node);
154
155         if (get_irn_mode(copy) == mode_T) {
156                 ir_mode *mode = flag_class->mode;
157                 value = new_rd_Proj(NULL, copy, mode, pn);
158         } else {
159                 value = copy;
160         }
161
162         n = flag_consumers;
163         do {
164                 int i;
165                 int arity = get_irn_arity(n);
166                 for (i = 0; i < arity; ++i) {
167                         ir_node *in = get_irn_n(n, i);
168                         in = skip_Proj(in);
169                         if (in == flags_needed) {
170                                 set_irn_n(n, i, value);
171                                 break;
172                         }
173                 }
174                 n = get_irn_link(n);
175         } while (n != NULL);
176
177         /* No need to introduce the copy, because it only lives in this block, but
178          * we have to update the liveness of all operands */
179         if (is_Block(node) ||
180                         get_nodes_block(node) != get_nodes_block(flags_needed)) {
181                 ir_graph *irg = get_irn_irg(node);
182                 be_lv_t  *lv  = be_get_irg_liveness(irg);
183                 int       i;
184
185                 if (lv != NULL) {
186                         for (i = get_irn_arity(copy) - 1; i >= 0; --i) {
187                                 be_liveness_update(lv, get_irn_n(copy, i));
188                         }
189                 }
190         }
191 }
192
193 /**
194  * walks up the schedule and makes sure there are no flag-destroying nodes
195  * between a flag-consumer -> flag-producer chain. Fixes problematic situations
196  * by moving and/or rematerialisation of the flag-producers.
197  * (This can be extended in the future to do some register allocation on targets
198  *  like ppc32 where we conceptually have 8 flag registers)
199  */
200 static void fix_flags_walker(ir_node *block, void *env)
201 {
202         ir_node *node;
203         ir_node *flags_needed   = NULL;
204         ir_node *flag_consumers = NULL;
205         int      pn = -1;
206         (void) env;
207
208         sched_foreach_reverse(block, node) {
209                 int i, arity;
210                 ir_node *new_flags_needed = NULL;
211                 ir_node *test;
212
213                 if (is_Phi(node))
214                         break;
215
216                 if (node == flags_needed) {
217                         /* all ok */
218                         flags_needed   = NULL;
219                         flag_consumers = NULL;
220                 }
221
222                 /* test whether node destroys the flags */
223                 test = node;
224                 if (be_is_Keep(test))
225                         test = sched_prev(test);
226
227                 if (flags_needed != NULL && check_modify(test)) {
228                         /* rematerialize */
229                         rematerialize_or_move(flags_needed, node, flag_consumers, pn);
230                         flags_needed   = NULL;
231                         flag_consumers = NULL;
232                 }
233
234                 /* test whether the current node needs flags */
235                 arity = get_irn_arity(node);
236                 for (i = 0; i < arity; ++i) {
237                         const arch_register_class_t *cls = arch_get_irn_reg_class(node, i);
238                         if (cls == flag_class) {
239                                 assert(new_flags_needed == NULL);
240                                 new_flags_needed = get_irn_n(node, i);
241                         }
242                 }
243
244                 if (new_flags_needed == NULL)
245                         continue;
246
247                 /* spiller can't (correctly) remat flag consumers at the moment */
248                 assert(!arch_irn_is(node, rematerializable));
249
250                 if (skip_Proj(new_flags_needed) != flags_needed) {
251                         if (flags_needed != NULL) {
252                                 /* rematerialize node */
253                                 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
254                                 flags_needed   = NULL;
255                                 flag_consumers = NULL;
256                         }
257
258                         flags_needed = new_flags_needed;
259                         arch_set_irn_register(flags_needed, flags_reg);
260                         if (is_Proj(flags_needed)) {
261                                 pn           = get_Proj_proj(flags_needed);
262                                 flags_needed = get_Proj_pred(flags_needed);
263                         }
264                         flag_consumers = node;
265                         set_irn_link(flag_consumers, NULL);
266                         assert(arch_irn_is(flags_needed, rematerializable));
267                 } else {
268                         /* link all consumers in a list */
269                         set_irn_link(node, flag_consumers);
270                         flag_consumers = node;
271                 }
272         }
273
274         if (flags_needed != NULL) {
275                 assert(get_nodes_block(flags_needed) != block);
276                 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
277                 flags_needed   = NULL;
278                 flag_consumers = NULL;
279         }
280
281         assert(flags_needed   == NULL);
282         assert(flag_consumers == NULL);
283 }
284
285 void be_sched_fix_flags(ir_graph *irg, const arch_register_class_t *flag_cls,
286                         func_rematerialize remat_func,
287                         check_modifies_flags check_modifies_flags_func)
288 {
289         flag_class   = flag_cls;
290         flags_reg    = & flag_class->regs[0];
291         remat        = remat_func;
292         check_modify = check_modifies_flags_func;
293         changed      = 0;
294         if (remat == NULL)
295                 remat = &default_remat;
296         if (check_modify == NULL)
297                 check_modify = &default_check_modifies;
298
299         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
300         irg_block_walk_graph(irg, fix_flags_walker, NULL, NULL);
301         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
302
303         if (changed) {
304                 be_remove_dead_nodes_from_schedule(irg);
305         }
306 }