2 * Copyright (C) 1995-2011 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 Peephole optimisation framework keeps track of which registers contain which values
23 * @author Matthias Braun
28 #include "bepeephole.h"
30 #include "iredges_t.h"
41 #include "beintlive_t.h"
46 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
48 static const arch_env_t *arch_env;
50 static ir_node *current_node;
51 ir_node **register_values;
53 static void clear_reg_value(ir_node *node)
55 const arch_register_t *reg;
58 if (!mode_is_data(get_irn_mode(node)))
61 reg = arch_get_irn_register(node);
63 panic("No register assigned at %+F", node);
65 if (reg->type & arch_register_type_virtual)
67 reg_idx = reg->global_index;
69 DB((dbg, LEVEL_1, "Clear Register %s\n", reg->name));
70 register_values[reg_idx] = NULL;
73 static void set_reg_value(ir_node *node)
75 const arch_register_t *reg;
78 if (!mode_is_data(get_irn_mode(node)))
81 reg = arch_get_irn_register(node);
83 panic("No register assigned at %+F", node);
85 if (reg->type & arch_register_type_virtual)
87 reg_idx = reg->global_index;
89 DB((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node));
90 register_values[reg_idx] = node;
93 static void clear_defs(ir_node *node)
95 /* clear values defined */
96 if (get_irn_mode(node) == mode_T) {
97 foreach_out_edge(node, edge) {
98 ir_node *proj = get_edge_src_irn(edge);
99 clear_reg_value(proj);
102 clear_reg_value(node);
106 static void set_uses(ir_node *node)
110 /* set values used */
111 arity = get_irn_arity(node);
112 for (i = 0; i < arity; ++i) {
113 ir_node *in = get_irn_n(node, i);
118 void be_peephole_new_node(ir_node * nw)
120 be_liveness_introduce(lv, nw);
124 * must be called from peephole optimisations before a node will be killed
125 * and its users will be redirected to new_node.
126 * so bepeephole can update its internal state.
128 * Note: killing a node and rewiring is only allowed if new_node produces
129 * the same registers as old_node.
131 static void be_peephole_before_exchange(const ir_node *old_node,
134 const arch_register_t *reg;
136 bool old_is_current = false;
138 DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
140 assert(sched_is_scheduled(skip_Proj_const(old_node)));
141 assert(sched_is_scheduled(skip_Proj(new_node)));
143 if (current_node == old_node) {
144 old_is_current = true;
146 /* next node to be processed will be killed. Its scheduling predecessor
147 * must be processed next. */
148 current_node = sched_next(current_node);
149 assert (!is_Bad(current_node));
151 /* we can't handle liveness updates correctly when exchange current node
152 * with something behind it */
153 assert(value_dominates(skip_Proj(new_node), skip_Proj_const(old_node)));
156 if (!mode_is_data(get_irn_mode(old_node)))
159 reg = arch_get_irn_register(old_node);
161 panic("No register assigned at %+F", old_node);
163 assert(reg == arch_get_irn_register(new_node) &&
164 "KILLING a node and replacing by different register is not allowed");
166 reg_idx = reg->global_index;
167 if (register_values[reg_idx] == old_node || old_is_current) {
168 register_values[reg_idx] = new_node;
171 be_liveness_remove(lv, old_node);
174 void be_peephole_exchange(ir_node *old, ir_node *nw)
176 be_peephole_before_exchange(old, nw);
179 be_peephole_new_node(nw);
183 * block-walker: run peephole optimization on the given block.
185 static void process_block(ir_node *block, void *data)
189 /* construct initial register assignment */
190 memset(register_values, 0, sizeof(ir_node*) * arch_env->n_registers);
192 assert(lv->sets_valid && "live sets must be computed");
193 DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
194 be_lv_foreach(lv, block, be_lv_state_end, node) {
197 DB((dbg, LEVEL_1, "\nstart processing\n"));
199 /* walk the block from last insn to the first */
200 current_node = sched_last(block);
201 for ( ; !sched_is_begin(current_node);
202 current_node = sched_prev(current_node)) {
204 peephole_opt_func peephole_node;
206 assert(!is_Bad(current_node));
207 if (is_Phi(current_node))
210 clear_defs(current_node);
211 set_uses(current_node);
213 op = get_irn_op(current_node);
214 peephole_node = (peephole_opt_func)op->ops.generic;
215 if (peephole_node == NULL)
218 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
219 peephole_node(current_node);
220 assert(!is_Bad(current_node));
225 * Check whether the node has only one user. Explicitly ignore the anchor.
227 bool be_has_only_one_user(ir_node *node)
229 int n = get_irn_n_edges(node);
236 foreach_out_edge(node, edge) {
237 ir_node *src = get_edge_src_irn(edge);
238 /* ignore anchor and keep-alive edges */
239 if (is_Anchor(src) || is_End(src))
247 static inline bool overlapping_regs(const arch_register_t *reg0,
248 const arch_register_req_t *req0, const arch_register_t *reg1,
249 const arch_register_req_t *req1)
251 if (reg0 == NULL || reg1 == NULL)
253 return reg0->global_index < (unsigned)reg1->global_index + req1->width
254 && reg1->global_index < (unsigned)reg0->global_index + req0->width;
257 bool be_can_move_down(ir_heights_t *heights, const ir_node *node,
258 const ir_node *before)
260 assert(get_nodes_block(node) == get_nodes_block(before));
261 assert(sched_get_time_step(node) < sched_get_time_step(before));
263 int node_arity = get_irn_arity(node);
264 ir_node *schedpoint = sched_next(node);
266 while (schedpoint != before) {
267 /* schedpoint must not use our computed values */
268 if (heights_reachable_in_block(heights, schedpoint, node))
271 /* schedpoint must not overwrite registers of our inputs */
272 unsigned n_outs = arch_get_irn_n_outs(schedpoint);
273 for (int i = 0; i < node_arity; ++i) {
274 ir_node *in = get_irn_n(node, i);
275 const arch_register_t *reg = arch_get_irn_register(in);
278 const arch_register_req_t *in_req
279 = arch_get_irn_register_req_in(node, i);
280 for (unsigned o = 0; o < n_outs; ++o) {
281 const arch_register_t *outreg
282 = arch_get_irn_register_out(schedpoint, o);
283 const arch_register_req_t *outreq
284 = arch_get_irn_register_req_out(schedpoint, o);
285 if (overlapping_regs(reg, in_req, outreg, outreq))
290 schedpoint = sched_next(schedpoint);
295 bool be_can_move_up(ir_heights_t *heights, const ir_node *node,
296 const ir_node *after)
298 unsigned n_outs = arch_get_irn_n_outs(node);
299 const ir_node *node_block = get_nodes_block(node);
300 const ir_node *after_block = get_block_const(after);
301 const ir_node *schedpoint;
302 if (node_block != after_block) {
303 /* currently we can move up exactly 1 block */
304 assert(get_Block_cfgpred_block(node_block, 0) == after_block);
305 ir_node *first = sched_first(node_block);
306 /* make sure we can move to the beginning of the succ block */
307 if (node != first && !be_can_move_up(heights, node, sched_prev(first)))
310 /* check if node overrides any of live-in values of other successors */
311 ir_graph *irg = get_irn_irg(node);
312 be_lv_t *lv = be_get_irg_liveness(irg);
313 foreach_block_succ(after_block, edge) {
314 ir_node *succ = get_edge_src_irn(edge);
315 if (succ == node_block)
318 be_lv_foreach(lv, succ, be_lv_state_in, live_node) {
319 const arch_register_t *reg = arch_get_irn_register(live_node);
320 const arch_register_req_t *req = arch_get_irn_register_req(live_node);
321 for (unsigned o = 0; o < n_outs; ++o) {
322 const arch_register_t *outreg
323 = arch_get_irn_register_out(node, o);
324 const arch_register_req_t *outreq
325 = arch_get_irn_register_req_out(node, o);
326 if (overlapping_regs(outreg, outreq, reg, req))
330 sched_foreach(succ, phi) {
333 const arch_register_t *reg = arch_get_irn_register(phi);
334 const arch_register_req_t *req = arch_get_irn_register_req(phi);
335 for (unsigned o = 0; o < n_outs; ++o) {
336 const arch_register_t *outreg
337 = arch_get_irn_register_out(node, o);
338 const arch_register_req_t *outreq
339 = arch_get_irn_register_req_out(node, o);
340 if (overlapping_regs(outreg, outreq, reg, req))
345 schedpoint = sched_last(after_block);
347 schedpoint = sched_prev(node);
350 /* move schedule upwards until we hit the "after" node */
351 while (schedpoint != after) {
352 /* TODO: the following heights query only works for nodes in the same
353 * block, otherwise we have to be conservative here */
354 if (get_nodes_block(node) != get_nodes_block(schedpoint))
356 /* node must not depend on schedpoint */
357 if (heights_reachable_in_block(heights, node, schedpoint))
360 /* node must not overwrite registers used by schedpoint */
361 int arity = get_irn_arity(schedpoint);
362 for (int i = 0; i < arity; ++i) {
363 const arch_register_t *reg
364 = arch_get_irn_register_in(schedpoint, i);
367 const arch_register_req_t *in_req
368 = arch_get_irn_register_req_in(schedpoint, i);
369 for (unsigned o = 0; o < n_outs; ++o) {
370 const arch_register_t *outreg
371 = arch_get_irn_register_out(node, o);
372 const arch_register_req_t *outreq
373 = arch_get_irn_register_req_out(node, o);
374 if (overlapping_regs(outreg, outreq, reg, in_req))
379 schedpoint = sched_prev(schedpoint);
385 * Tries to optimize a beIncSP node with its previous IncSP node.
386 * Must be run from a be_peephole_opt() context.
388 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
393 ir_node *pred = be_get_IncSP_pred(node);
395 if (!be_is_IncSP(pred))
398 if (!be_has_only_one_user(pred))
401 pred_offs = be_get_IncSP_offset(pred);
402 curr_offs = be_get_IncSP_offset(node);
403 offs = curr_offs + pred_offs;
405 /* add node offset to pred and remove our IncSP */
406 be_set_IncSP_offset(pred, offs);
408 be_peephole_exchange(node, pred);
412 void be_peephole_opt(ir_graph *irg)
415 /* we sometimes find BadE nodes in float apps like optest_float.c or
416 * kahansum.c for example... */
417 be_invalidate_live_sets(irg);
419 be_assure_live_sets(irg);
421 arch_env = be_get_irg_arch_env(irg);
422 lv = be_get_irg_liveness(irg);
424 register_values = XMALLOCN(ir_node*, arch_env->n_registers);
426 irg_block_walk_graph(irg, process_block, NULL, NULL);
428 xfree(register_values);
431 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
432 void be_init_peephole(void)
434 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");