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)
190 /* construct initial register assignment */
191 memset(register_values, 0, sizeof(ir_node*) * arch_env->n_registers);
193 assert(lv->sets_valid && "live sets must be computed");
194 DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
195 be_lv_foreach(lv, block, be_lv_state_end, l) {
196 ir_node *node = be_lv_get_irn(lv, block, l);
199 DB((dbg, LEVEL_1, "\nstart processing\n"));
201 /* walk the block from last insn to the first */
202 current_node = sched_last(block);
203 for ( ; !sched_is_begin(current_node);
204 current_node = sched_prev(current_node)) {
206 peephole_opt_func peephole_node;
208 assert(!is_Bad(current_node));
209 if (is_Phi(current_node))
212 clear_defs(current_node);
213 set_uses(current_node);
215 op = get_irn_op(current_node);
216 peephole_node = (peephole_opt_func)op->ops.generic;
217 if (peephole_node == NULL)
220 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
221 peephole_node(current_node);
222 assert(!is_Bad(current_node));
227 * Check whether the node has only one user. Explicitly ignore the anchor.
229 bool be_has_only_one_user(ir_node *node)
231 int n = get_irn_n_edges(node);
238 foreach_out_edge(node, edge) {
239 ir_node *src = get_edge_src_irn(edge);
240 /* ignore anchor and keep-alive edges */
241 if (is_Anchor(src) || is_End(src))
249 bool be_can_move_before(ir_heights_t *heights, const ir_node *node,
250 const ir_node *before)
252 int node_arity = get_irn_arity(node);
253 ir_node *schedpoint = sched_next(node);
255 while (schedpoint != before) {
257 unsigned n_outs = arch_get_irn_n_outs(schedpoint);
259 /* the node must not use our computed values */
260 if (heights_reachable_in_block(heights, schedpoint, node))
263 /* the node must not overwrite registers of our inputs */
264 for (i = 0; i < node_arity; ++i) {
265 ir_node *in = get_irn_n(node, i);
266 const arch_register_t *reg = arch_get_irn_register(in);
267 const arch_register_req_t *in_req
268 = arch_get_irn_register_req_in(node, i);
272 for (o = 0; o < n_outs; ++o) {
273 const arch_register_t *outreg
274 = arch_get_irn_register_out(schedpoint, o);
275 const arch_register_req_t *outreq
276 = arch_get_irn_register_req_out(schedpoint, o);
279 if (outreg->global_index >= reg->global_index
280 && outreg->global_index
281 < (unsigned)reg->global_index + in_req->width)
283 if (reg->global_index >= outreg->global_index
285 < (unsigned)outreg->global_index + outreq->width)
290 schedpoint = sched_next(schedpoint);
296 * Tries to optimize a beIncSP node with its previous IncSP node.
297 * Must be run from a be_peephole_opt() context.
299 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
304 ir_node *pred = be_get_IncSP_pred(node);
306 if (!be_is_IncSP(pred))
309 if (!be_has_only_one_user(pred))
312 pred_offs = be_get_IncSP_offset(pred);
313 curr_offs = be_get_IncSP_offset(node);
314 offs = curr_offs + pred_offs;
316 /* add node offset to pred and remove our IncSP */
317 be_set_IncSP_offset(pred, offs);
319 be_peephole_exchange(node, pred);
323 void be_peephole_opt(ir_graph *irg)
326 /* we sometimes find BadE nodes in float apps like optest_float.c or
327 * kahansum.c for example... */
328 be_invalidate_live_sets(irg);
330 be_assure_live_sets(irg);
332 arch_env = be_get_irg_arch_env(irg);
333 lv = be_get_irg_liveness(irg);
335 register_values = XMALLOCN(ir_node*, arch_env->n_registers);
337 irg_block_walk_graph(irg, process_block, NULL, NULL);
339 xfree(register_values);
342 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
343 void be_init_peephole(void)
345 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");