2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief modifies schedule so flags dependencies are respected.
23 * @author Matthias Braun, Christoph Mallon
25 * Fixup schedule to respect flag constraints by moving and rematerialisation of
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.
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.
45 #include "iredges_t.h"
56 #include "beabihelper.h"
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;
64 static ir_node *default_remat(ir_node *node, ir_node *after)
66 ir_node *block, *copy;
70 block = get_nodes_block(after);
72 copy = exact_copy(node);
73 set_nodes_block(copy, block);
74 sched_add_after(after, copy);
79 static bool default_check_modifies(const ir_node *node)
81 return arch_irn_is(node, modify_flags);
85 * tests whether we can legally move node node after node after
86 * (only works for nodes in same block)
88 static bool can_move(ir_node *node, ir_node *after)
90 ir_node *node_block = get_nodes_block(node);
91 assert(node_block == get_nodes_block(after));
93 /* TODO respect dep edges */
94 assert(get_irn_n_edges_kind(node, EDGE_KIND_DEP) == 0);
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);
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)
105 /* Phi or End represents a usage at block end. */
106 if (is_Phi(out2) || is_End(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))
114 assert(!is_Sync(out3));
115 if (sched_get_time_step(out3) <= sched_get_time_step(after)) {
119 } else if (sched_get_time_step(out2) <= sched_get_time_step(after)) {
124 if (get_nodes_block(out) != node_block)
126 /* phi represents a usage at block end */
129 if (sched_get_time_step(out) <= sched_get_time_step(after)) {
138 static void rematerialize_or_move(ir_node *flags_needed, ir_node *node,
139 ir_node *flag_consumers, int pn)
145 if (!is_Block(node) &&
146 get_nodes_block(flags_needed) == get_nodes_block(node) &&
147 can_move(flags_needed, node)) {
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 */
156 copy = remat(flags_needed, node);
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);
169 int arity = get_irn_arity(n);
170 for (i = 0; i < arity; ++i) {
171 ir_node *in = get_irn_n(n, i);
173 if (in == flags_needed) {
174 set_irn_n(n, i, value);
178 n = (ir_node*)get_irn_link(n);
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);
190 for (i = get_irn_arity(copy) - 1; i >= 0; --i) {
191 be_liveness_update(lv, get_irn_n(copy, i));
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)
204 static void fix_flags_walker(ir_node *block, void *env)
206 ir_node *flags_needed = NULL;
207 ir_node *flag_consumers = NULL;
211 ir_node *place = block;
212 sched_foreach_reverse(block, node) {
214 ir_node *new_flags_needed = NULL;
222 if (node == flags_needed) {
225 flag_consumers = NULL;
228 /* test whether node destroys the flags */
230 if (be_is_Keep(test))
231 test = sched_prev(test);
233 if (flags_needed != NULL && check_modify(test)) {
235 rematerialize_or_move(flags_needed, node, flag_consumers, pn);
237 flag_consumers = NULL;
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);
251 if (new_flags_needed == NULL)
254 /* spiller can't (correctly) remat flag consumers at the moment */
255 assert(!arch_irn_is(node, rematerializable));
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);
262 flag_consumers = NULL;
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);
271 flag_consumers = node;
272 set_irn_link(flag_consumers, NULL);
273 assert(arch_irn_is(flags_needed, rematerializable));
275 /* link all consumers in a list */
276 set_irn_link(node, flag_consumers);
277 flag_consumers = node;
281 if (flags_needed != NULL) {
282 assert(get_nodes_block(flags_needed) != block);
283 rematerialize_or_move(flags_needed, place, flag_consumers, pn);
285 flag_consumers = NULL;
288 assert(flags_needed == NULL);
289 assert(flag_consumers == NULL);
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)
296 flag_class = flag_cls;
297 flags_reg = & flag_class->regs[0];
299 check_modify = check_modifies_flags_func;
302 remat = &default_remat;
303 if (check_modify == NULL)
304 check_modify = &default_check_modifies;
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);
311 be_remove_dead_nodes_from_schedule(irg);