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"
45 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
47 static const arch_env_t *arch_env;
49 static ir_node *current_node;
50 ir_node ***register_values;
52 static void clear_reg_value(ir_node *node)
54 const arch_register_t *reg;
55 const arch_register_class_t *cls;
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 cls = arch_register_get_class(reg);
69 reg_idx = arch_register_get_index(reg);
70 cls_idx = arch_register_class_index(cls);
72 //assert(register_values[cls_idx][reg_idx] != NULL);
73 DB((dbg, LEVEL_1, "Clear Register %s\n", reg->name));
74 register_values[cls_idx][reg_idx] = NULL;
77 static void set_reg_value(ir_node *node)
79 const arch_register_t *reg;
80 const arch_register_class_t *cls;
84 if (!mode_is_data(get_irn_mode(node)))
87 reg = arch_get_irn_register(node);
89 panic("No register assigned at %+F", node);
91 if (reg->type & arch_register_type_virtual)
93 cls = arch_register_get_class(reg);
94 reg_idx = arch_register_get_index(reg);
95 cls_idx = arch_register_class_index(cls);
97 DB((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node));
98 register_values[cls_idx][reg_idx] = node;
101 static void clear_defs(ir_node *node)
103 /* clear values defined */
104 if (get_irn_mode(node) == mode_T) {
105 const ir_edge_t *edge;
106 foreach_out_edge(node, edge) {
107 ir_node *proj = get_edge_src_irn(edge);
108 clear_reg_value(proj);
111 clear_reg_value(node);
115 static void set_uses(ir_node *node)
119 /* set values used */
120 arity = get_irn_arity(node);
121 for (i = 0; i < arity; ++i) {
122 ir_node *in = get_irn_n(node, i);
127 void be_peephole_new_node(ir_node * nw)
129 be_liveness_introduce(lv, nw);
133 * must be called from peephole optimisations before a node will be killed
134 * and its users will be redirected to new_node.
135 * so bepeephole can update its internal state.
137 * Note: killing a node and rewiring is only allowed if new_node produces
138 * the same registers as old_node.
140 static void be_peephole_before_exchange(const ir_node *old_node,
143 const arch_register_t *reg;
144 const arch_register_class_t *cls;
148 DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
150 if (current_node == old_node) {
151 /* next node to be processed will be killed. Its scheduling predecessor
152 * must be processed next. */
153 current_node = sched_next(current_node);
154 assert (!is_Bad(current_node));
157 if (!mode_is_data(get_irn_mode(old_node)))
160 reg = arch_get_irn_register(old_node);
162 panic("No register assigned at %+F", old_node);
164 assert(reg == arch_get_irn_register(new_node) &&
165 "KILLING a node and replacing by different register is not allowed");
167 cls = arch_register_get_class(reg);
168 reg_idx = arch_register_get_index(reg);
169 cls_idx = arch_register_class_index(cls);
171 if (register_values[cls_idx][reg_idx] == old_node) {
172 register_values[cls_idx][reg_idx] = new_node;
175 be_liveness_remove(lv, old_node);
178 void be_peephole_exchange(ir_node *old, ir_node *nw)
180 be_peephole_before_exchange(old, nw);
183 be_peephole_new_node(nw);
187 * block-walker: run peephole optimization on the given block.
189 static void process_block(ir_node *block, void *data)
196 /* construct initial register assignment */
197 n_classes = arch_env->n_register_classes;
198 for (i = 0; i < n_classes; ++i) {
199 const arch_register_class_t *cls = &arch_env->register_classes[i];
200 unsigned n_regs = arch_register_class_n_regs(cls);
201 memset(register_values[i], 0, sizeof(ir_node*) * n_regs);
204 assert(lv->nodes && "live sets must be computed");
205 DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
206 be_lv_foreach(lv, block, be_lv_state_end, l) {
207 ir_node *node = be_lv_get_irn(lv, block, l);
210 DB((dbg, LEVEL_1, "\nstart processing\n"));
212 /* walk the block from last insn to the first */
213 current_node = sched_last(block);
214 for ( ; !sched_is_begin(current_node);
215 current_node = sched_prev(current_node)) {
217 peephole_opt_func peephole_node;
219 assert(!is_Bad(current_node));
220 if (is_Phi(current_node))
223 clear_defs(current_node);
224 set_uses(current_node);
226 op = get_irn_op(current_node);
227 peephole_node = (peephole_opt_func)op->ops.generic;
228 if (peephole_node == NULL)
231 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
232 peephole_node(current_node);
233 assert(!is_Bad(current_node));
238 * Check whether the node has only one user. Explicitly ignore the anchor.
240 bool be_has_only_one_user(ir_node *node)
242 int n = get_irn_n_edges(node);
244 const ir_edge_t *edge;
250 foreach_out_edge(node, edge) {
251 ir_node *src = get_edge_src_irn(edge);
252 /* ignore anchor and keep-alive edges */
253 if (is_Anchor(src) || is_End(src))
262 * Tries to optimize a beIncSP node with its previous IncSP node.
263 * Must be run from a be_peephole_opt() context.
265 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
270 ir_node *pred = be_get_IncSP_pred(node);
272 if (!be_is_IncSP(pred))
275 if (!be_has_only_one_user(pred))
278 pred_offs = be_get_IncSP_offset(pred);
279 curr_offs = be_get_IncSP_offset(node);
280 offs = curr_offs + pred_offs;
282 /* add node offset to pred and remove our IncSP */
283 be_set_IncSP_offset(pred, offs);
285 be_peephole_exchange(node, pred);
289 void be_peephole_opt(ir_graph *irg)
294 /* we sometimes find BadE nodes in float apps like optest_float.c or
295 * kahansum.c for example... */
296 be_liveness_invalidate(be_get_irg_liveness(irg));
297 be_liveness_assure_sets(be_assure_liveness(irg));
299 arch_env = be_get_irg_arch_env(irg);
300 lv = be_get_irg_liveness(irg);
302 n_classes = arch_env->n_register_classes;
303 register_values = XMALLOCN(ir_node**, n_classes);
304 for (i = 0; i < n_classes; ++i) {
305 const arch_register_class_t *cls = &arch_env->register_classes[i];
306 unsigned n_regs = arch_register_class_n_regs(cls);
307 register_values[i] = XMALLOCN(ir_node*, n_regs);
310 irg_block_walk_graph(irg, process_block, NULL, NULL);
312 for (i = 0; i < n_classes; ++i) {
313 xfree(register_values[i]);
315 xfree(register_values);
318 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
319 void be_init_peephole(void)
321 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");