sparc: Do not force the object file format to ELF.
[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         ir_node *node_block = get_nodes_block(node);
91         assert(node_block == get_nodes_block(after));
92
93         /* TODO respect dep edges */
94         assert(get_irn_n_edges_kind(node, EDGE_KIND_DEP) == 0);
95
96         /** all users have to be after the after node */
97         foreach_out_edge(node, edge) {
98                 ir_node *out = get_edge_src_irn(edge);
99                 if (is_Proj(out)) {
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                                 if (get_nodes_block(out2) != node_block)
104                                         continue;
105                                 /* Phi or End represents a usage at block end. */
106                                 if (is_Phi(out2) || is_End(out2))
107                                         continue;
108                                 if (is_Sync(out2)) {
109                                         foreach_out_edge(out2, edge3) {
110                                                 ir_node *out3 = get_edge_src_irn(edge3);
111                                                 /* Phi or End represents a usage at block end. */
112                                                 if (is_Phi(out3) || is_End(out3))
113                                                         continue;
114                                                 assert(!is_Sync(out3));
115                                                 if (sched_get_time_step(out3) <= sched_get_time_step(after)) {
116                                                         return false;
117                                                 }
118                                         }
119                                 } else if (sched_get_time_step(out2) <= sched_get_time_step(after)) {
120                                         return false;
121                                 }
122                         }
123                 } else {
124                         if (get_nodes_block(out) != node_block)
125                                 continue;
126                         /* phi represents a usage at block end */
127                         if (is_Phi(out))
128                                 continue;
129                         if (sched_get_time_step(out) <= sched_get_time_step(after)) {
130                                 return false;
131                         }
132                 }
133         }
134
135         return true;
136 }
137
138 static void rematerialize_or_move(ir_node *flags_needed, ir_node *node,
139                                   ir_node *flag_consumers, int pn)
140 {
141         ir_node *n;
142         ir_node *copy;
143         ir_node *value;
144
145         if (!is_Block(node) &&
146                         get_nodes_block(flags_needed) == get_nodes_block(node) &&
147                         can_move(flags_needed, node)) {
148                 /* move it */
149                 sched_remove(flags_needed);
150                 sched_add_after(node, flags_needed);
151                 /* No need to update liveness, because the node stays in the same block */
152                 return;
153         }
154
155         changed = 1;
156         copy    = remat(flags_needed, node);
157
158         if (get_irn_mode(copy) == mode_T) {
159                 ir_mode *mode = flag_class->mode;
160                 value = new_rd_Proj(NULL, copy, mode, pn);
161                 be_add_missing_keeps_node(copy);
162         } else {
163                 value = copy;
164         }
165
166         n = flag_consumers;
167         do {
168                 int i;
169                 int arity = get_irn_arity(n);
170                 for (i = 0; i < arity; ++i) {
171                         ir_node *in = get_irn_n(n, i);
172                         in = skip_Proj(in);
173                         if (in == flags_needed) {
174                                 set_irn_n(n, i, value);
175                                 break;
176                         }
177                 }
178                 n = (ir_node*)get_irn_link(n);
179         } while (n != NULL);
180
181         /* No need to introduce the copy, because it only lives in this block, but
182          * we have to update the liveness of all operands */
183         if (is_Block(node) ||
184                         get_nodes_block(node) != get_nodes_block(flags_needed)) {
185                 ir_graph *irg = get_irn_irg(node);
186                 be_lv_t  *lv  = be_get_irg_liveness(irg);
187                 int       i;
188
189                 if (lv != NULL) {
190                         for (i = get_irn_arity(copy) - 1; i >= 0; --i) {
191                                 be_liveness_update(lv, get_irn_n(copy, i));
192                         }
193                 }
194         }
195 }
196
197 /**
198  * walks up the schedule and makes sure there are no flag-destroying nodes
199  * between a flag-consumer -> flag-producer chain. Fixes problematic situations
200  * by moving and/or rematerialisation of the flag-producers.
201  * (This can be extended in the future to do some register allocation on targets
202  *  like ppc32 where we conceptually have 8 flag registers)
203  */
204 static void fix_flags_walker(ir_node *block, void *env)
205 {
206         ir_node *flags_needed   = NULL;
207         ir_node *flag_consumers = NULL;
208         int      pn = -1;
209         (void) env;
210
211         ir_node *place = block;
212         sched_foreach_reverse(block, node) {
213                 int i, arity;
214                 ir_node *new_flags_needed = NULL;
215                 ir_node *test;
216
217                 if (is_Phi(node)) {
218                         place = node;
219                         break;
220                 }
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_req_t *req
244                                 = arch_get_irn_register_req_in(node, i);
245                         if (req->cls == flag_class) {
246                                 assert(new_flags_needed == NULL);
247                                 new_flags_needed = get_irn_n(node, i);
248                         }
249                 }
250
251                 if (new_flags_needed == NULL)
252                         continue;
253
254                 /* spiller can't (correctly) remat flag consumers at the moment */
255                 assert(!arch_irn_is(node, rematerializable));
256
257                 if (skip_Proj(new_flags_needed) != flags_needed) {
258                         if (flags_needed != NULL) {
259                                 /* rematerialize node */
260                                 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
261                                 flags_needed   = NULL;
262                                 flag_consumers = NULL;
263                         }
264
265                         flags_needed = new_flags_needed;
266                         arch_set_irn_register(flags_needed, flags_reg);
267                         if (is_Proj(flags_needed)) {
268                                 pn           = get_Proj_proj(flags_needed);
269                                 flags_needed = get_Proj_pred(flags_needed);
270                         }
271                         flag_consumers = node;
272                         set_irn_link(flag_consumers, NULL);
273                         assert(arch_irn_is(flags_needed, rematerializable));
274                 } else {
275                         /* link all consumers in a list */
276                         set_irn_link(node, flag_consumers);
277                         flag_consumers = node;
278                 }
279         }
280
281         if (flags_needed != NULL) {
282                 assert(get_nodes_block(flags_needed) != block);
283                 rematerialize_or_move(flags_needed, place, flag_consumers, pn);
284                 flags_needed   = NULL;
285                 flag_consumers = NULL;
286         }
287
288         assert(flags_needed   == NULL);
289         assert(flag_consumers == NULL);
290 }
291
292 void be_sched_fix_flags(ir_graph *irg, const arch_register_class_t *flag_cls,
293                         func_rematerialize remat_func,
294                         check_modifies_flags check_modifies_flags_func)
295 {
296         flag_class   = flag_cls;
297         flags_reg    = & flag_class->regs[0];
298         remat        = remat_func;
299         check_modify = check_modifies_flags_func;
300         changed      = 0;
301         if (remat == NULL)
302                 remat = &default_remat;
303         if (check_modify == NULL)
304                 check_modify = &default_check_modifies;
305
306         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
307         irg_block_walk_graph(irg, fix_flags_walker, NULL, NULL);
308         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
309
310         if (changed) {
311                 be_remove_dead_nodes_from_schedule(irg);
312         }
313 }