Fix typos in comments: s/wether/whether/ and related corrections.
[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 "beirgmod.h"
54 #include "besched.h"
55 #include "benode.h"
56 #include "belive.h"
57
58 static const arch_register_class_t *flag_class;
59 static const arch_register_t       *flags_reg;
60 static func_rematerialize           remat;
61 static check_modifies_flags         check_modify;
62 static int                          changed;
63
64 static ir_node *default_remat(ir_node *node, ir_node *after)
65 {
66         ir_node *block, *copy;
67         if (is_Block(after))
68                 block = after;
69         else
70                 block = get_nodes_block(after);
71
72         copy = exact_copy(node);
73         set_nodes_block(copy, block);
74         sched_add_after(after, copy);
75
76         return copy;
77 }
78
79 static bool default_check_modifies(const ir_node *node)
80 {
81         return arch_irn_is(node, modify_flags);
82 }
83
84 /**
85  * tests whether we can legally move node node after node after
86  * (only works for nodes in same block)
87  */
88 static bool can_move(ir_node *node, ir_node *after)
89 {
90         const ir_edge_t *edge;
91         ir_node *node_block = get_nodes_block(node);
92         assert(node_block == get_nodes_block(after));
93
94         /* TODO respect dep edges */
95         assert(get_irn_n_edges_kind(node, EDGE_KIND_DEP) == 0);
96
97         /** all users have to be after the after node */
98         foreach_out_edge(node, edge) {
99                 ir_node *out = get_edge_src_irn(edge);
100                 if (is_Proj(out)) {
101                         const ir_edge_t *edge2;
102                         assert(get_irn_n_edges_kind(out, EDGE_KIND_DEP) == 0);
103                         foreach_out_edge(out, edge2) {
104                                 ir_node *out2 = get_edge_src_irn(edge2);
105                                 if (get_nodes_block(out2) != node_block)
106                                         continue;
107                                 /* Phi or End represents a usage at block end. */
108                                 if (is_Phi(out2) || is_End(out2))
109                                         continue;
110                                 if (is_Sync(out2)) {
111                                         const ir_edge_t *edge3;
112                                         foreach_out_edge(out2, edge3) {
113                                                 ir_node *out3 = get_edge_src_irn(edge3);
114                                                 /* Phi or End represents a usage at block end. */
115                                                 if (is_Phi(out3) || is_End(out3))
116                                                         continue;
117                                                 assert(!is_Sync(out3));
118                                                 if (sched_get_time_step(out3) <= sched_get_time_step(after)) {
119                                                         return false;
120                                                 }
121                                         }
122                                 } else if (sched_get_time_step(out2) <= sched_get_time_step(after)) {
123                                         return false;
124                                 }
125                         }
126                 } else {
127                         if (get_nodes_block(out) != node_block)
128                                 continue;
129                         /* phi represents a usage at block end */
130                         if (is_Phi(out))
131                                 continue;
132                         if (sched_get_time_step(out) <= sched_get_time_step(after)) {
133                                 return false;
134                         }
135                 }
136         }
137
138         return true;
139 }
140
141 static void rematerialize_or_move(ir_node *flags_needed, ir_node *node,
142                                   ir_node *flag_consumers, int pn)
143 {
144         ir_node *n;
145         ir_node *copy;
146         ir_node *value;
147
148         if (!is_Block(node) &&
149                         get_nodes_block(flags_needed) == get_nodes_block(node) &&
150                         can_move(flags_needed, node)) {
151                 /* move it */
152                 sched_remove(flags_needed);
153                 sched_add_after(node, flags_needed);
154                 /* No need to update liveness, because the node stays in the same block */
155                 return;
156         }
157
158         changed = 1;
159         copy    = remat(flags_needed, node);
160
161         if (get_irn_mode(copy) == mode_T) {
162                 ir_mode *mode = flag_class->mode;
163                 value = new_rd_Proj(NULL, copy, mode, pn);
164         } else {
165                 value = copy;
166         }
167
168         n = flag_consumers;
169         do {
170                 int i;
171                 int arity = get_irn_arity(n);
172                 for (i = 0; i < arity; ++i) {
173                         ir_node *in = get_irn_n(n, i);
174                         in = skip_Proj(in);
175                         if (in == flags_needed) {
176                                 set_irn_n(n, i, value);
177                                 break;
178                         }
179                 }
180                 n = (ir_node*)get_irn_link(n);
181         } while (n != NULL);
182
183         /* No need to introduce the copy, because it only lives in this block, but
184          * we have to update the liveness of all operands */
185         if (is_Block(node) ||
186                         get_nodes_block(node) != get_nodes_block(flags_needed)) {
187                 ir_graph *irg = get_irn_irg(node);
188                 be_lv_t  *lv  = be_get_irg_liveness(irg);
189                 int       i;
190
191                 if (lv != NULL) {
192                         for (i = get_irn_arity(copy) - 1; i >= 0; --i) {
193                                 be_liveness_update(lv, get_irn_n(copy, i));
194                         }
195                 }
196         }
197 }
198
199 /**
200  * walks up the schedule and makes sure there are no flag-destroying nodes
201  * between a flag-consumer -> flag-producer chain. Fixes problematic situations
202  * by moving and/or rematerialisation of the flag-producers.
203  * (This can be extended in the future to do some register allocation on targets
204  *  like ppc32 where we conceptually have 8 flag registers)
205  */
206 static void fix_flags_walker(ir_node *block, void *env)
207 {
208         ir_node *node;
209         ir_node *flags_needed   = NULL;
210         ir_node *flag_consumers = NULL;
211         int      pn = -1;
212         (void) env;
213
214         sched_foreach_reverse(block, node) {
215                 int i, arity;
216                 ir_node *new_flags_needed = NULL;
217                 ir_node *test;
218
219                 if (is_Phi(node))
220                         break;
221
222                 if (node == flags_needed) {
223                         /* all ok */
224                         flags_needed   = NULL;
225                         flag_consumers = NULL;
226                 }
227
228                 /* test whether node destroys the flags */
229                 test = node;
230                 if (be_is_Keep(test))
231                         test = sched_prev(test);
232
233                 if (flags_needed != NULL && check_modify(test)) {
234                         /* rematerialize */
235                         rematerialize_or_move(flags_needed, node, flag_consumers, pn);
236                         flags_needed   = NULL;
237                         flag_consumers = NULL;
238                 }
239
240                 /* test whether the current node needs flags */
241                 arity = get_irn_arity(node);
242                 for (i = 0; i < arity; ++i) {
243                         const arch_register_class_t *cls = arch_get_irn_reg_class(node, i);
244                         if (cls == flag_class) {
245                                 assert(new_flags_needed == NULL);
246                                 new_flags_needed = get_irn_n(node, i);
247                         }
248                 }
249
250                 if (new_flags_needed == NULL)
251                         continue;
252
253                 /* spiller can't (correctly) remat flag consumers at the moment */
254                 assert(!arch_irn_is(node, rematerializable));
255
256                 if (skip_Proj(new_flags_needed) != flags_needed) {
257                         if (flags_needed != NULL) {
258                                 /* rematerialize node */
259                                 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
260                                 flags_needed   = NULL;
261                                 flag_consumers = NULL;
262                         }
263
264                         flags_needed = new_flags_needed;
265                         arch_set_irn_register(flags_needed, flags_reg);
266                         if (is_Proj(flags_needed)) {
267                                 pn           = get_Proj_proj(flags_needed);
268                                 flags_needed = get_Proj_pred(flags_needed);
269                         }
270                         flag_consumers = node;
271                         set_irn_link(flag_consumers, NULL);
272                         assert(arch_irn_is(flags_needed, rematerializable));
273                 } else {
274                         /* link all consumers in a list */
275                         set_irn_link(node, flag_consumers);
276                         flag_consumers = node;
277                 }
278         }
279
280         if (flags_needed != NULL) {
281                 assert(get_nodes_block(flags_needed) != block);
282                 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
283                 flags_needed   = NULL;
284                 flag_consumers = NULL;
285         }
286
287         assert(flags_needed   == NULL);
288         assert(flag_consumers == NULL);
289 }
290
291 void be_sched_fix_flags(ir_graph *irg, const arch_register_class_t *flag_cls,
292                         func_rematerialize remat_func,
293                         check_modifies_flags check_modifies_flags_func)
294 {
295         flag_class   = flag_cls;
296         flags_reg    = & flag_class->regs[0];
297         remat        = remat_func;
298         check_modify = check_modifies_flags_func;
299         changed      = 0;
300         if (remat == NULL)
301                 remat = &default_remat;
302         if (check_modify == NULL)
303                 check_modify = &default_check_modifies;
304
305         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
306         irg_block_walk_graph(irg, fix_flags_walker, NULL, NULL);
307         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
308
309         if (changed) {
310                 be_remove_dead_nodes_from_schedule(irg);
311         }
312 }