Use foreach_set() instead of reimplementing it.
[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  *
25  * Fixup schedule to respect flag constraints by moving and rematerialisation of
26  * nodes.
27  *
28  * Flags are modeled as register classes with ignore registers. However to avoid
29  * bloating the graph, only flag-consumer -> producer dependencies are
30  * explicitely modeled in the graph. Nodes that just change the flags are only
31  * marked with the arch_irn_flags_modify_flags flag.
32  *
33  * Flags are usually a limited resource that can't (or at least shouldn't) be
34  * spilled. So in some situations (for example 2 adc-nodes that use the flags of
35  * a single add node on x86) operations have to be repeated to work correctly.
36  */
37 #include "config.h"
38
39 #include <stdbool.h>
40
41 #include "irgwalk.h"
42 #include "irnode_t.h"
43 #include "irtools.h"
44 #include "ircons.h"
45 #include "iredges_t.h"
46 #include "irprintf.h"
47 #include "error.h"
48
49 #include "beflags.h"
50 #include "bearch.h"
51 #include "beirg.h"
52 #include "beirgmod.h"
53 #include "besched.h"
54 #include "benode.h"
55 #include "belive.h"
56 #include "beabihelper.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                 be_add_missing_keeps_node(copy);
165         } else {
166                 value = copy;
167         }
168
169         n = flag_consumers;
170         do {
171                 int i;
172                 int arity = get_irn_arity(n);
173                 for (i = 0; i < arity; ++i) {
174                         ir_node *in = get_irn_n(n, i);
175                         in = skip_Proj(in);
176                         if (in == flags_needed) {
177                                 set_irn_n(n, i, value);
178                                 break;
179                         }
180                 }
181                 n = (ir_node*)get_irn_link(n);
182         } while (n != NULL);
183
184         /* No need to introduce the copy, because it only lives in this block, but
185          * we have to update the liveness of all operands */
186         if (is_Block(node) ||
187                         get_nodes_block(node) != get_nodes_block(flags_needed)) {
188                 ir_graph *irg = get_irn_irg(node);
189                 be_lv_t  *lv  = be_get_irg_liveness(irg);
190                 int       i;
191
192                 if (lv != NULL) {
193                         for (i = get_irn_arity(copy) - 1; i >= 0; --i) {
194                                 be_liveness_update(lv, get_irn_n(copy, i));
195                         }
196                 }
197         }
198 }
199
200 /**
201  * walks up the schedule and makes sure there are no flag-destroying nodes
202  * between a flag-consumer -> flag-producer chain. Fixes problematic situations
203  * by moving and/or rematerialisation of the flag-producers.
204  * (This can be extended in the future to do some register allocation on targets
205  *  like ppc32 where we conceptually have 8 flag registers)
206  */
207 static void fix_flags_walker(ir_node *block, void *env)
208 {
209         ir_node *node;
210         ir_node *flags_needed   = NULL;
211         ir_node *flag_consumers = NULL;
212         int      pn = -1;
213         (void) env;
214
215         sched_foreach_reverse(block, node) {
216                 int i, arity;
217                 ir_node *new_flags_needed = NULL;
218                 ir_node *test;
219
220                 if (is_Phi(node))
221                         break;
222
223                 if (node == flags_needed) {
224                         /* all ok */
225                         flags_needed   = NULL;
226                         flag_consumers = NULL;
227                 }
228
229                 /* test whether node destroys the flags */
230                 test = node;
231                 if (be_is_Keep(test))
232                         test = sched_prev(test);
233
234                 if (flags_needed != NULL && check_modify(test)) {
235                         /* rematerialize */
236                         rematerialize_or_move(flags_needed, node, flag_consumers, pn);
237                         flags_needed   = NULL;
238                         flag_consumers = NULL;
239                 }
240
241                 /* test whether the current node needs flags */
242                 arity = get_irn_arity(node);
243                 for (i = 0; i < arity; ++i) {
244                         const arch_register_req_t *req
245                                 = arch_get_irn_register_req_in(node, i);
246                         if (req->cls == flag_class) {
247                                 assert(new_flags_needed == NULL);
248                                 new_flags_needed = get_irn_n(node, i);
249                         }
250                 }
251
252                 if (new_flags_needed == NULL)
253                         continue;
254
255                 /* spiller can't (correctly) remat flag consumers at the moment */
256                 assert(!arch_irn_is(node, rematerializable));
257
258                 if (skip_Proj(new_flags_needed) != flags_needed) {
259                         if (flags_needed != NULL) {
260                                 /* rematerialize node */
261                                 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
262                                 flags_needed   = NULL;
263                                 flag_consumers = NULL;
264                         }
265
266                         flags_needed = new_flags_needed;
267                         arch_set_irn_register(flags_needed, flags_reg);
268                         if (is_Proj(flags_needed)) {
269                                 pn           = get_Proj_proj(flags_needed);
270                                 flags_needed = get_Proj_pred(flags_needed);
271                         }
272                         flag_consumers = node;
273                         set_irn_link(flag_consumers, NULL);
274                         assert(arch_irn_is(flags_needed, rematerializable));
275                 } else {
276                         /* link all consumers in a list */
277                         set_irn_link(node, flag_consumers);
278                         flag_consumers = node;
279                 }
280         }
281
282         if (flags_needed != NULL) {
283                 assert(get_nodes_block(flags_needed) != block);
284                 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
285                 flags_needed   = NULL;
286                 flag_consumers = NULL;
287         }
288
289         assert(flags_needed   == NULL);
290         assert(flag_consumers == NULL);
291 }
292
293 void be_sched_fix_flags(ir_graph *irg, const arch_register_class_t *flag_cls,
294                         func_rematerialize remat_func,
295                         check_modifies_flags check_modifies_flags_func)
296 {
297         flag_class   = flag_cls;
298         flags_reg    = & flag_class->regs[0];
299         remat        = remat_func;
300         check_modify = check_modifies_flags_func;
301         changed      = 0;
302         if (remat == NULL)
303                 remat = &default_remat;
304         if (check_modify == NULL)
305                 check_modify = &default_check_modifies;
306
307         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
308         irg_block_walk_graph(irg, fix_flags_walker, NULL, NULL);
309         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
310
311         if (changed) {
312                 be_remove_dead_nodes_from_schedule(irg);
313         }
314 }