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"
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;
56 const arch_register_class_t *cls;
60 if (!mode_is_data(get_irn_mode(node)))
63 reg = arch_get_irn_register(node);
65 panic("No register assigned at %+F", node);
67 if (reg->type & arch_register_type_virtual)
69 cls = arch_register_get_class(reg);
70 reg_idx = arch_register_get_index(reg);
71 cls_idx = arch_register_class_index(cls);
73 //assert(register_values[cls_idx][reg_idx] != NULL);
74 DB((dbg, LEVEL_1, "Clear Register %s\n", reg->name));
75 register_values[cls_idx][reg_idx] = NULL;
78 static void set_reg_value(ir_node *node)
80 const arch_register_t *reg;
81 const arch_register_class_t *cls;
85 if (!mode_is_data(get_irn_mode(node)))
88 reg = arch_get_irn_register(node);
90 panic("No register assigned at %+F", node);
92 if (reg->type & arch_register_type_virtual)
94 cls = arch_register_get_class(reg);
95 reg_idx = arch_register_get_index(reg);
96 cls_idx = arch_register_class_index(cls);
98 DB((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node));
99 register_values[cls_idx][reg_idx] = node;
102 static void clear_defs(ir_node *node)
104 /* clear values defined */
105 if (get_irn_mode(node) == mode_T) {
106 const ir_edge_t *edge;
107 foreach_out_edge(node, edge) {
108 ir_node *proj = get_edge_src_irn(edge);
109 clear_reg_value(proj);
112 clear_reg_value(node);
116 static void set_uses(ir_node *node)
120 /* set values used */
121 arity = get_irn_arity(node);
122 for (i = 0; i < arity; ++i) {
123 ir_node *in = get_irn_n(node, i);
128 void be_peephole_new_node(ir_node * nw)
130 be_liveness_introduce(lv, nw);
134 * must be called from peephole optimisations before a node will be killed
135 * and its users will be redirected to new_node.
136 * so bepeephole can update its internal state.
138 * Note: killing a node and rewiring is only allowed if new_node produces
139 * the same registers as old_node.
141 static void be_peephole_before_exchange(const ir_node *old_node,
144 const arch_register_t *reg;
145 const arch_register_class_t *cls;
148 bool old_is_current = false;
150 DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
152 assert(sched_is_scheduled(skip_Proj_const(old_node)));
153 assert(sched_is_scheduled(skip_Proj(new_node)));
155 if (current_node == old_node) {
156 old_is_current = true;
158 /* next node to be processed will be killed. Its scheduling predecessor
159 * must be processed next. */
160 current_node = sched_next(current_node);
161 assert (!is_Bad(current_node));
163 /* we can't handle liveness updates correctly when exchange current node
164 * with something behind it */
165 assert(value_dominates(skip_Proj(new_node), skip_Proj_const(old_node)));
168 if (!mode_is_data(get_irn_mode(old_node)))
171 reg = arch_get_irn_register(old_node);
173 panic("No register assigned at %+F", old_node);
175 assert(reg == arch_get_irn_register(new_node) &&
176 "KILLING a node and replacing by different register is not allowed");
178 cls = arch_register_get_class(reg);
179 reg_idx = arch_register_get_index(reg);
180 cls_idx = arch_register_class_index(cls);
182 if (register_values[cls_idx][reg_idx] == old_node || old_is_current) {
183 register_values[cls_idx][reg_idx] = new_node;
186 be_liveness_remove(lv, old_node);
189 void be_peephole_exchange(ir_node *old, ir_node *nw)
191 be_peephole_before_exchange(old, nw);
194 be_peephole_new_node(nw);
198 * block-walker: run peephole optimization on the given block.
200 static void process_block(ir_node *block, void *data)
207 /* construct initial register assignment */
208 n_classes = arch_env->n_register_classes;
209 for (i = 0; i < n_classes; ++i) {
210 const arch_register_class_t *cls = &arch_env->register_classes[i];
211 unsigned n_regs = arch_register_class_n_regs(cls);
212 memset(register_values[i], 0, sizeof(ir_node*) * n_regs);
215 assert(lv->nodes && "live sets must be computed");
216 DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
217 be_lv_foreach(lv, block, be_lv_state_end, l) {
218 ir_node *node = be_lv_get_irn(lv, block, l);
221 DB((dbg, LEVEL_1, "\nstart processing\n"));
223 /* walk the block from last insn to the first */
224 current_node = sched_last(block);
225 for ( ; !sched_is_begin(current_node);
226 current_node = sched_prev(current_node)) {
228 peephole_opt_func peephole_node;
230 assert(!is_Bad(current_node));
231 if (is_Phi(current_node))
234 clear_defs(current_node);
235 set_uses(current_node);
237 op = get_irn_op(current_node);
238 peephole_node = (peephole_opt_func)op->ops.generic;
239 if (peephole_node == NULL)
242 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
243 peephole_node(current_node);
244 assert(!is_Bad(current_node));
249 * Check whether the node has only one user. Explicitly ignore the anchor.
251 bool be_has_only_one_user(ir_node *node)
253 int n = get_irn_n_edges(node);
255 const ir_edge_t *edge;
261 foreach_out_edge(node, edge) {
262 ir_node *src = get_edge_src_irn(edge);
263 /* ignore anchor and keep-alive edges */
264 if (is_Anchor(src) || is_End(src))
273 * Tries to optimize a beIncSP node with its previous IncSP node.
274 * Must be run from a be_peephole_opt() context.
276 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
281 ir_node *pred = be_get_IncSP_pred(node);
283 if (!be_is_IncSP(pred))
286 if (!be_has_only_one_user(pred))
289 pred_offs = be_get_IncSP_offset(pred);
290 curr_offs = be_get_IncSP_offset(node);
291 offs = curr_offs + pred_offs;
293 /* add node offset to pred and remove our IncSP */
294 be_set_IncSP_offset(pred, offs);
296 be_peephole_exchange(node, pred);
300 void be_peephole_opt(ir_graph *irg)
305 /* we sometimes find BadE nodes in float apps like optest_float.c or
306 * kahansum.c for example... */
307 be_liveness_invalidate(be_get_irg_liveness(irg));
308 be_liveness_assure_sets(be_assure_liveness(irg));
310 arch_env = be_get_irg_arch_env(irg);
311 lv = be_get_irg_liveness(irg);
313 n_classes = arch_env->n_register_classes;
314 register_values = XMALLOCN(ir_node**, n_classes);
315 for (i = 0; i < n_classes; ++i) {
316 const arch_register_class_t *cls = &arch_env->register_classes[i];
317 unsigned n_regs = arch_register_class_n_regs(cls);
318 register_values[i] = XMALLOCN(ir_node*, n_regs);
321 irg_block_walk_graph(irg, process_block, NULL, NULL);
323 for (i = 0; i < n_classes; ++i) {
324 xfree(register_values[i]);
326 xfree(register_values);
329 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
330 void be_init_peephole(void)
332 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");