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(new_node));
153 assert(value_dominates(new_node, old_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));
164 if (!mode_is_data(get_irn_mode(old_node)))
167 reg = arch_get_irn_register(old_node);
169 panic("No register assigned at %+F", old_node);
171 assert(reg == arch_get_irn_register(new_node) &&
172 "KILLING a node and replacing by different register is not allowed");
174 cls = arch_register_get_class(reg);
175 reg_idx = arch_register_get_index(reg);
176 cls_idx = arch_register_class_index(cls);
178 if (register_values[cls_idx][reg_idx] == old_node || old_is_current) {
179 register_values[cls_idx][reg_idx] = new_node;
182 be_liveness_remove(lv, old_node);
185 void be_peephole_exchange(ir_node *old, ir_node *nw)
187 be_peephole_before_exchange(old, nw);
190 be_peephole_new_node(nw);
194 * block-walker: run peephole optimization on the given block.
196 static void process_block(ir_node *block, void *data)
203 /* construct initial register assignment */
204 n_classes = arch_env->n_register_classes;
205 for (i = 0; i < n_classes; ++i) {
206 const arch_register_class_t *cls = &arch_env->register_classes[i];
207 unsigned n_regs = arch_register_class_n_regs(cls);
208 memset(register_values[i], 0, sizeof(ir_node*) * n_regs);
211 assert(lv->nodes && "live sets must be computed");
212 DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
213 be_lv_foreach(lv, block, be_lv_state_end, l) {
214 ir_node *node = be_lv_get_irn(lv, block, l);
217 DB((dbg, LEVEL_1, "\nstart processing\n"));
219 /* walk the block from last insn to the first */
220 current_node = sched_last(block);
221 for ( ; !sched_is_begin(current_node);
222 current_node = sched_prev(current_node)) {
224 peephole_opt_func peephole_node;
226 assert(!is_Bad(current_node));
227 if (is_Phi(current_node))
230 clear_defs(current_node);
231 set_uses(current_node);
233 op = get_irn_op(current_node);
234 peephole_node = (peephole_opt_func)op->ops.generic;
235 if (peephole_node == NULL)
238 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
239 peephole_node(current_node);
240 assert(!is_Bad(current_node));
245 * Check whether the node has only one user. Explicitly ignore the anchor.
247 bool be_has_only_one_user(ir_node *node)
249 int n = get_irn_n_edges(node);
251 const ir_edge_t *edge;
257 foreach_out_edge(node, edge) {
258 ir_node *src = get_edge_src_irn(edge);
259 /* ignore anchor and keep-alive edges */
260 if (is_Anchor(src) || is_End(src))
269 * Tries to optimize a beIncSP node with its previous IncSP node.
270 * Must be run from a be_peephole_opt() context.
272 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
277 ir_node *pred = be_get_IncSP_pred(node);
279 if (!be_is_IncSP(pred))
282 if (!be_has_only_one_user(pred))
285 pred_offs = be_get_IncSP_offset(pred);
286 curr_offs = be_get_IncSP_offset(node);
287 offs = curr_offs + pred_offs;
289 /* add node offset to pred and remove our IncSP */
290 be_set_IncSP_offset(pred, offs);
292 be_peephole_exchange(node, pred);
296 void be_peephole_opt(ir_graph *irg)
301 /* we sometimes find BadE nodes in float apps like optest_float.c or
302 * kahansum.c for example... */
303 be_liveness_invalidate(be_get_irg_liveness(irg));
304 be_liveness_assure_sets(be_assure_liveness(irg));
306 arch_env = be_get_irg_arch_env(irg);
307 lv = be_get_irg_liveness(irg);
309 n_classes = arch_env->n_register_classes;
310 register_values = XMALLOCN(ir_node**, n_classes);
311 for (i = 0; i < n_classes; ++i) {
312 const arch_register_class_t *cls = &arch_env->register_classes[i];
313 unsigned n_regs = arch_register_class_n_regs(cls);
314 register_values[i] = XMALLOCN(ir_node*, n_regs);
317 irg_block_walk_graph(irg, process_block, NULL, NULL);
319 for (i = 0; i < n_classes; ++i) {
320 xfree(register_values[i]);
322 xfree(register_values);
325 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
326 void be_init_peephole(void)
328 FIRM_DBG_REGISTER(dbg, "firm.be.peephole");