2 * Copyright (C) 1995-2007 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
30 #include "bepeephole.h"
32 #include "iredges_t.h"
40 #include "besched_t.h"
43 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
45 static const arch_env_t *arch_env;
47 static ir_node *current_node;
48 ir_node ***register_values;
50 static void clear_reg_value(ir_node *node)
52 const arch_register_t *reg;
53 const arch_register_class_t *cls;
57 if(!mode_is_data(get_irn_mode(node)))
60 reg = arch_get_irn_register(arch_env, node);
62 panic("No register assigned at %+F\n", node);
64 if(arch_register_type_is(reg, virtual))
66 cls = arch_register_get_class(reg);
67 reg_idx = arch_register_get_index(reg);
68 cls_idx = arch_register_class_index(cls);
70 //assert(register_values[cls_idx][reg_idx] != NULL);
71 DBG((dbg, LEVEL_1, "Clear Register %s\n", reg->name));
72 register_values[cls_idx][reg_idx] = NULL;
75 static void set_reg_value(ir_node *node)
77 const arch_register_t *reg;
78 const arch_register_class_t *cls;
82 if(!mode_is_data(get_irn_mode(node)))
85 reg = arch_get_irn_register(arch_env, node);
87 panic("No register assigned at %+F\n", node);
89 if(arch_register_type_is(reg, virtual))
91 cls = arch_register_get_class(reg);
92 reg_idx = arch_register_get_index(reg);
93 cls_idx = arch_register_class_index(cls);
97 ir_node *old_value = register_values[cls_idx][reg_idx];
98 assert(old_value == NULL || old_value == node);
101 DBG((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node));
102 register_values[cls_idx][reg_idx] = node;
105 static void clear_defs(ir_node *node)
107 /* clear values defined */
108 if(get_irn_mode(node) == mode_T) {
109 const ir_edge_t *edge;
110 foreach_out_edge(node, edge) {
111 ir_node *proj = get_edge_src_irn(edge);
112 clear_reg_value(proj);
115 clear_reg_value(node);
119 static void set_uses(ir_node *node)
123 /* set values used */
124 arity = get_irn_arity(node);
125 for(i = 0; i < arity; ++i) {
126 ir_node *in = get_irn_n(node, i);
131 void be_peephole_before_exchange(const ir_node *old_node, ir_node *new_node)
133 const arch_register_t *reg;
134 const arch_register_class_t *cls;
138 DBG((dbg, LEVEL_1, "About to exchange %+F with %+F\n", old_node, new_node));
140 if(old_node == current_node)
141 current_node = new_node;
143 if(!mode_is_data(get_irn_mode(old_node)))
146 reg = arch_get_irn_register(arch_env, old_node);
148 panic("No register assigned at %+F\n", old_node);
150 cls = arch_register_get_class(reg);
151 reg_idx = arch_register_get_index(reg);
152 cls_idx = arch_register_class_index(cls);
154 if(register_values[cls_idx][reg_idx] == old_node) {
155 register_values[cls_idx][reg_idx] = new_node;
158 be_liveness_remove(lv, old_node);
161 void be_peephole_after_exchange(ir_node *new_node)
163 be_liveness_introduce(lv, new_node);
166 static void process_block(ir_node *block, void *data)
168 arch_isa_t *isa = arch_env->isa;
174 /* construct initial register assignment */
175 n_classes = arch_isa_get_n_reg_class(isa);
176 for(i = 0; i < n_classes; ++i) {
177 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
178 unsigned n_regs = arch_register_class_n_regs(cls);
179 memset(register_values[i], 0, sizeof(ir_node*) * n_regs);
182 assert(lv->nodes && "live sets must be computed");
183 DBG((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
184 be_lv_foreach(lv, block, be_lv_state_end, l) {
185 ir_node *node = be_lv_get_irn(lv, block, l);
188 DBG((dbg, LEVEL_1, "\nstart processing\n"));
190 /* walk the block from last insn to the first */
191 current_node = sched_last(block);
192 for( ; !sched_is_begin(current_node);
193 current_node = sched_prev(current_node)) {
196 peephole_opt_func func;
198 if(is_Phi(current_node))
201 clear_defs(current_node);
202 set_uses(current_node);
204 op = get_irn_op(current_node);
205 func = (peephole_opt_func) op->ops.generic;
211 /* was the current node replaced? */
212 if(current_node != last) {
213 set_uses(current_node);
218 void be_peephole_opt(be_irg_t *birg)
221 ir_graph *irg = be_get_birg_irg(birg);
225 /* we sometimes find BadE nodes in float apps like optest_float.c or
226 * kahansum.c for example... */
227 be_liveness_invalidate(birg->lv);
228 be_liveness_assure_sets(be_assure_liveness(birg));
230 arch_env = be_get_birg_arch_env(birg);
231 lv = be_get_birg_liveness(birg);
234 n_classes = arch_isa_get_n_reg_class(isa);
235 register_values = alloca(sizeof(register_values[0]) * n_classes);
236 for(i = 0; i < n_classes; ++i) {
237 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
238 unsigned n_regs = arch_register_class_n_regs(cls);
239 register_values[i] = alloca(sizeof(ir_node*) * n_regs);
242 irg_block_walk_graph(irg, process_block, NULL, NULL);
245 void be_peephole_init(void)
247 clear_irp_opcodes_generic_func();
250 void be_init_peephole(void)
252 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");
255 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);