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 be_foreach_value(node, value,
97 clear_reg_value(value);
101 static void set_uses(ir_node *node)
105 /* set values used */
106 arity = get_irn_arity(node);
107 for (i = 0; i < arity; ++i) {
108 ir_node *in = get_irn_n(node, i);
113 void be_peephole_new_node(ir_node * nw)
115 be_liveness_introduce(lv, nw);
119 * must be called from peephole optimisations before a node will be killed
120 * and its users will be redirected to new_node.
121 * so bepeephole can update its internal state.
123 * Note: killing a node and rewiring is only allowed if new_node produces
124 * the same registers as old_node.
126 static void be_peephole_before_exchange(const ir_node *old_node,
129 const arch_register_t *reg;
131 bool old_is_current = false;
133 DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
135 assert(sched_is_scheduled(skip_Proj_const(old_node)));
136 assert(sched_is_scheduled(skip_Proj(new_node)));
138 if (current_node == old_node) {
139 old_is_current = true;
141 /* next node to be processed will be killed. Its scheduling predecessor
142 * must be processed next. */
143 current_node = sched_next(current_node);
144 assert (!is_Bad(current_node));
146 /* we can't handle liveness updates correctly when exchange current node
147 * with something behind it */
148 assert(value_dominates(skip_Proj(new_node), skip_Proj_const(old_node)));
151 if (!mode_is_data(get_irn_mode(old_node)))
154 reg = arch_get_irn_register(old_node);
156 panic("No register assigned at %+F", old_node);
158 assert(reg == arch_get_irn_register(new_node) &&
159 "KILLING a node and replacing by different register is not allowed");
161 reg_idx = reg->global_index;
162 if (register_values[reg_idx] == old_node || old_is_current) {
163 register_values[reg_idx] = new_node;
166 be_liveness_remove(lv, old_node);
169 void be_peephole_exchange(ir_node *old, ir_node *nw)
171 be_peephole_before_exchange(old, nw);
174 be_peephole_new_node(nw);
178 * block-walker: run peephole optimization on the given block.
180 static void process_block(ir_node *block, void *data)
184 /* construct initial register assignment */
185 memset(register_values, 0, sizeof(ir_node*) * arch_env->n_registers);
187 assert(lv->sets_valid && "live sets must be computed");
188 DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
189 be_lv_foreach(lv, block, be_lv_state_end, node) {
192 DB((dbg, LEVEL_1, "\nstart processing\n"));
194 /* walk the block from last insn to the first */
195 current_node = sched_last(block);
196 for ( ; !sched_is_begin(current_node);
197 current_node = sched_prev(current_node)) {
199 peephole_opt_func peephole_node;
201 assert(!is_Bad(current_node));
202 if (is_Phi(current_node))
205 clear_defs(current_node);
206 set_uses(current_node);
208 op = get_irn_op(current_node);
209 peephole_node = (peephole_opt_func)op->ops.generic;
210 if (peephole_node == NULL)
213 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
214 peephole_node(current_node);
215 assert(!is_Bad(current_node));
220 * Check whether the node has only one user. Explicitly ignore the anchor.
222 bool be_has_only_one_user(ir_node *node)
224 int n = get_irn_n_edges(node);
231 foreach_out_edge(node, edge) {
232 ir_node *src = get_edge_src_irn(edge);
233 /* ignore anchor and keep-alive edges */
234 if (is_Anchor(src) || is_End(src))
242 static inline bool overlapping_regs(const arch_register_t *reg0,
243 const arch_register_req_t *req0, const arch_register_t *reg1,
244 const arch_register_req_t *req1)
246 if (reg0 == NULL || reg1 == NULL)
248 return reg0->global_index < (unsigned)reg1->global_index + req1->width
249 && reg1->global_index < (unsigned)reg0->global_index + req0->width;
252 bool be_can_move_down(ir_heights_t *heights, const ir_node *node,
253 const ir_node *before)
255 assert(get_nodes_block(node) == get_nodes_block(before));
256 assert(sched_get_time_step(node) < sched_get_time_step(before));
258 int node_arity = get_irn_arity(node);
259 ir_node *schedpoint = sched_next(node);
261 while (schedpoint != before) {
262 /* schedpoint must not use our computed values */
263 if (heights_reachable_in_block(heights, schedpoint, node))
266 /* schedpoint must not overwrite registers of our inputs */
267 for (int 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);
272 const arch_register_req_t *in_req
273 = arch_get_irn_register_req_in(node, i);
274 be_foreach_out(schedpoint, o) {
275 const arch_register_t *outreg
276 = arch_get_irn_register_out(schedpoint, o);
277 const arch_register_req_t *outreq
278 = arch_get_irn_register_req_out(schedpoint, o);
279 if (overlapping_regs(reg, in_req, outreg, outreq))
284 schedpoint = sched_next(schedpoint);
289 bool be_can_move_up(ir_heights_t *heights, const ir_node *node,
290 const ir_node *after)
292 const ir_node *node_block = get_nodes_block(node);
293 const ir_node *after_block = get_block_const(after);
294 const ir_node *schedpoint;
295 if (node_block != after_block) {
296 /* currently we can move up exactly 1 block */
297 assert(get_Block_cfgpred_block(node_block, 0) == after_block);
298 ir_node *first = sched_first(node_block);
300 /* do not move nodes changing memory */
301 if (is_memop(node)) {
302 ir_node *meminput = get_memop_mem(node);
303 if (!is_NoMem(meminput))
307 /* make sure we can move to the beginning of the succ block */
308 if (node != first && !be_can_move_up(heights, node, sched_prev(first)))
311 /* check if node overrides any of live-in values of other successors */
312 ir_graph *irg = get_irn_irg(node);
313 be_lv_t *lv = be_get_irg_liveness(irg);
314 foreach_block_succ(after_block, edge) {
315 ir_node *succ = get_edge_src_irn(edge);
316 if (succ == node_block)
319 be_lv_foreach(lv, succ, be_lv_state_in, live_node) {
320 const arch_register_t *reg = arch_get_irn_register(live_node);
321 const arch_register_req_t *req = arch_get_irn_register_req(live_node);
322 be_foreach_out(node, o) {
323 const arch_register_t *outreg
324 = arch_get_irn_register_out(node, o);
325 const arch_register_req_t *outreq
326 = arch_get_irn_register_req_out(node, o);
327 if (overlapping_regs(outreg, outreq, reg, req))
331 sched_foreach(succ, phi) {
334 const arch_register_t *reg = arch_get_irn_register(phi);
335 const arch_register_req_t *req = arch_get_irn_register_req(phi);
336 be_foreach_out(node, o) {
337 const arch_register_t *outreg
338 = arch_get_irn_register_out(node, o);
339 const arch_register_req_t *outreq
340 = arch_get_irn_register_req_out(node, o);
341 if (overlapping_regs(outreg, outreq, reg, req))
346 schedpoint = sched_last(after_block);
348 schedpoint = sched_prev(node);
351 /* move schedule upwards until we hit the "after" node */
352 while (schedpoint != after) {
353 /* TODO: the following heights query only works for nodes in the same
354 * block, otherwise we have to be conservative here */
355 if (get_nodes_block(node) != get_nodes_block(schedpoint))
357 /* node must not depend on schedpoint */
358 if (heights_reachable_in_block(heights, node, schedpoint))
361 /* node must not overwrite registers used by schedpoint */
362 int arity = get_irn_arity(schedpoint);
363 for (int i = 0; i < arity; ++i) {
364 const arch_register_t *reg
365 = arch_get_irn_register_in(schedpoint, i);
368 const arch_register_req_t *in_req
369 = arch_get_irn_register_req_in(schedpoint, i);
370 be_foreach_out(node, o) {
371 const arch_register_t *outreg
372 = arch_get_irn_register_out(node, o);
373 const arch_register_req_t *outreq
374 = arch_get_irn_register_req_out(node, o);
375 if (overlapping_regs(outreg, outreq, reg, in_req))
380 schedpoint = sched_prev(schedpoint);
386 * Tries to optimize a beIncSP node with its previous IncSP node.
387 * Must be run from a be_peephole_opt() context.
389 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
394 ir_node *pred = be_get_IncSP_pred(node);
396 if (!be_is_IncSP(pred))
399 if (!be_has_only_one_user(pred))
402 pred_offs = be_get_IncSP_offset(pred);
403 curr_offs = be_get_IncSP_offset(node);
404 offs = curr_offs + pred_offs;
406 /* add node offset to pred and remove our IncSP */
407 be_set_IncSP_offset(pred, offs);
409 be_peephole_exchange(node, pred);
413 void be_peephole_opt(ir_graph *irg)
415 be_assure_live_sets(irg);
417 arch_env = be_get_irg_arch_env(irg);
418 lv = be_get_irg_liveness(irg);
420 register_values = XMALLOCN(ir_node*, arch_env->n_registers);
422 irg_block_walk_graph(irg, process_block, NULL, NULL);
424 xfree(register_values);
427 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
428 void be_init_peephole(void)
430 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");