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
29 #include "bepeephole.h"
31 #include "iredges_t.h"
42 #include "beintlive_t.h"
47 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
49 static const arch_env_t *arch_env;
51 static ir_node *current_node;
52 ir_node **register_values;
54 static void clear_reg_value(ir_node *node)
56 const arch_register_t *reg;
59 if (!mode_is_data(get_irn_mode(node)))
62 reg = arch_get_irn_register(node);
64 panic("No register assigned at %+F", node);
66 if (reg->type & arch_register_type_virtual)
68 reg_idx = reg->global_index;
70 DB((dbg, LEVEL_1, "Clear Register %s\n", reg->name));
71 register_values[reg_idx] = NULL;
74 static void set_reg_value(ir_node *node)
76 const arch_register_t *reg;
79 if (!mode_is_data(get_irn_mode(node)))
82 reg = arch_get_irn_register(node);
84 panic("No register assigned at %+F", node);
86 if (reg->type & arch_register_type_virtual)
88 reg_idx = reg->global_index;
90 DB((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node));
91 register_values[reg_idx] = node;
94 static void clear_defs(ir_node *node)
96 /* clear values defined */
97 if (get_irn_mode(node) == mode_T) {
98 const ir_edge_t *edge;
99 foreach_out_edge(node, edge) {
100 ir_node *proj = get_edge_src_irn(edge);
101 clear_reg_value(proj);
104 clear_reg_value(node);
108 static void set_uses(ir_node *node)
112 /* set values used */
113 arity = get_irn_arity(node);
114 for (i = 0; i < arity; ++i) {
115 ir_node *in = get_irn_n(node, i);
120 void be_peephole_new_node(ir_node * nw)
122 be_liveness_introduce(lv, nw);
126 * must be called from peephole optimisations before a node will be killed
127 * and its users will be redirected to new_node.
128 * so bepeephole can update its internal state.
130 * Note: killing a node and rewiring is only allowed if new_node produces
131 * the same registers as old_node.
133 static void be_peephole_before_exchange(const ir_node *old_node,
136 const arch_register_t *reg;
138 bool old_is_current = false;
140 DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
142 assert(sched_is_scheduled(skip_Proj_const(old_node)));
143 assert(sched_is_scheduled(skip_Proj(new_node)));
145 if (current_node == old_node) {
146 old_is_current = true;
148 /* next node to be processed will be killed. Its scheduling predecessor
149 * must be processed next. */
150 current_node = sched_next(current_node);
151 assert (!is_Bad(current_node));
153 /* we can't handle liveness updates correctly when exchange current node
154 * with something behind it */
155 assert(value_dominates(skip_Proj(new_node), skip_Proj_const(old_node)));
158 if (!mode_is_data(get_irn_mode(old_node)))
161 reg = arch_get_irn_register(old_node);
163 panic("No register assigned at %+F", old_node);
165 assert(reg == arch_get_irn_register(new_node) &&
166 "KILLING a node and replacing by different register is not allowed");
168 reg_idx = reg->global_index;
169 if (register_values[reg_idx] == old_node || old_is_current) {
170 register_values[reg_idx] = new_node;
173 be_liveness_remove(lv, old_node);
176 void be_peephole_exchange(ir_node *old, ir_node *nw)
178 be_peephole_before_exchange(old, nw);
181 be_peephole_new_node(nw);
185 * block-walker: run peephole optimization on the given block.
187 static void process_block(ir_node *block, void *data)
192 /* construct initial register assignment */
193 memset(register_values, 0, sizeof(ir_node*) * arch_env->n_registers);
195 assert(lv->nodes && "live sets must be computed");
196 DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
197 be_lv_foreach(lv, block, be_lv_state_end, l) {
198 ir_node *node = be_lv_get_irn(lv, block, l);
201 DB((dbg, LEVEL_1, "\nstart processing\n"));
203 /* walk the block from last insn to the first */
204 current_node = sched_last(block);
205 for ( ; !sched_is_begin(current_node);
206 current_node = sched_prev(current_node)) {
208 peephole_opt_func peephole_node;
210 assert(!is_Bad(current_node));
211 if (is_Phi(current_node))
214 clear_defs(current_node);
215 set_uses(current_node);
217 op = get_irn_op(current_node);
218 peephole_node = (peephole_opt_func)op->ops.generic;
219 if (peephole_node == NULL)
222 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
223 peephole_node(current_node);
224 assert(!is_Bad(current_node));
229 * Check whether the node has only one user. Explicitly ignore the anchor.
231 bool be_has_only_one_user(ir_node *node)
233 int n = get_irn_n_edges(node);
235 const ir_edge_t *edge;
241 foreach_out_edge(node, edge) {
242 ir_node *src = get_edge_src_irn(edge);
243 /* ignore anchor and keep-alive edges */
244 if (is_Anchor(src) || is_End(src))
252 bool be_can_move_before(ir_heights_t *heights, const ir_node *node,
253 const ir_node *before)
255 int node_arity = get_irn_arity(node);
256 ir_node *schedpoint = sched_next(node);
258 while (schedpoint != before) {
260 unsigned n_outs = arch_get_irn_n_outs(schedpoint);
262 /* the node must not use our computed values */
263 if (heights_reachable_in_block(heights, schedpoint, node))
266 /* the node must not overwrite registers of our inputs */
267 for (i = 0; i < node_arity; ++i) {
268 ir_node *in = get_irn_n(node, i);
269 const arch_register_t *reg = arch_get_irn_register(in);
270 const arch_register_req_t *in_req
271 = arch_get_irn_register_req_in(node, i);
275 for (o = 0; o < n_outs; ++o) {
276 const arch_register_t *outreg
277 = arch_get_irn_register_out(schedpoint, o);
278 const arch_register_req_t *outreq
279 = arch_get_irn_register_req_out(schedpoint, o);
282 if (outreg->global_index >= reg->global_index
283 && outreg->global_index
284 < (unsigned)reg->global_index + in_req->width)
286 if (reg->global_index >= outreg->global_index
288 < (unsigned)outreg->global_index + outreq->width)
293 schedpoint = sched_next(schedpoint);
299 * Tries to optimize a beIncSP node with its previous IncSP node.
300 * Must be run from a be_peephole_opt() context.
302 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
307 ir_node *pred = be_get_IncSP_pred(node);
309 if (!be_is_IncSP(pred))
312 if (!be_has_only_one_user(pred))
315 pred_offs = be_get_IncSP_offset(pred);
316 curr_offs = be_get_IncSP_offset(node);
317 offs = curr_offs + pred_offs;
319 /* add node offset to pred and remove our IncSP */
320 be_set_IncSP_offset(pred, offs);
322 be_peephole_exchange(node, pred);
326 void be_peephole_opt(ir_graph *irg)
328 /* we sometimes find BadE nodes in float apps like optest_float.c or
329 * kahansum.c for example... */
330 be_liveness_invalidate(be_get_irg_liveness(irg));
331 be_liveness_assure_sets(be_assure_liveness(irg));
333 arch_env = be_get_irg_arch_env(irg);
334 lv = be_get_irg_liveness(irg);
336 register_values = XMALLOCN(ir_node*, arch_env->n_registers);
338 irg_block_walk_graph(irg, process_block, NULL, NULL);
340 xfree(register_values);
343 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
344 void be_init_peephole(void)
346 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");