- first real peephole optimisation mov 0, reg -> xor reg, reg when we don't
[libfirm] / ir / be / bepeephole.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Peephole optimisation framework keeps track of which registers contain which values
23  * @author      Matthias Braun
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "bepeephole.h"
31
32 #include "iredges_t.h"
33 #include "irgwalk.h"
34 #include "irprintf.h"
35 #include "error.h"
36
37 #include "beirg_t.h"
38 #include "belive_t.h"
39 #include "bearch_t.h"
40 #include "besched_t.h"
41
42 static const arch_env_t *arch_env;
43 static be_lv_t          *lv;
44 ir_node               ***register_values;
45
46 static void clear_value(ir_node *node)
47 {
48         const arch_register_t       *reg;
49         const arch_register_class_t *cls;
50         unsigned                     reg_idx;
51         unsigned                     cls_idx;
52
53         if(!mode_is_data(get_irn_mode(node)))
54                 return;
55
56         reg     = arch_get_irn_register(arch_env, node);
57         if(reg == NULL) {
58                 panic("No register assigned at %+F\n", node);
59         }
60         if(arch_register_type_is(reg, virtual))
61                 return;
62         cls     = arch_register_get_class(reg);
63         reg_idx = arch_register_get_index(reg);
64         cls_idx = arch_register_class_index(cls);
65
66         //assert(register_values[cls_idx][reg_idx] != NULL);
67         register_values[cls_idx][reg_idx] = NULL;
68 }
69
70 static void set_value(ir_node *node)
71 {
72         const arch_register_t       *reg;
73         const arch_register_class_t *cls;
74         unsigned                     reg_idx;
75         unsigned                     cls_idx;
76
77         if(!mode_is_data(get_irn_mode(node)))
78                 return;
79
80         reg     = arch_get_irn_register(arch_env, node);
81         if(reg == NULL) {
82                 panic("No register assigned at %+F\n", node);
83         }
84         if(arch_register_type_is(reg, virtual))
85                 return;
86         cls     = arch_register_get_class(reg);
87         reg_idx = arch_register_get_index(reg);
88         cls_idx = arch_register_class_index(cls);
89
90 #ifdef DEBUG_libfirm
91         {
92                 ir_node *old_value = register_values[cls_idx][reg_idx];
93                 assert(old_value == NULL || old_value == node);
94         }
95 #endif
96         register_values[cls_idx][reg_idx] = node;
97 }
98
99 static void clear_defs(ir_node *node)
100 {
101         /* clear values defined */
102         if(get_irn_mode(node) == mode_T) {
103                 const ir_edge_t *edge;
104                 foreach_out_edge(node, edge) {
105                         ir_node *proj = get_edge_src_irn(edge);
106                         clear_value(proj);
107                 }
108         } else {
109                 clear_value(node);
110         }
111 }
112
113 static void set_uses(ir_node *node)
114 {
115         int i, arity;
116
117         /* set values used */
118         arity = get_irn_arity(node);
119         for(i = 0; i < arity; ++i) {
120                 ir_node *in = get_irn_n(node, i);
121                 set_value(in);
122         }
123 }
124
125 static void process_block(ir_node *block, void *data)
126 {
127         arch_isa_t *isa = arch_env->isa;
128         unsigned n_classes;
129         unsigned i;
130         int l;
131         ir_node *node;
132         (void) data;
133
134         /* construct initial register assignment */
135         n_classes = arch_isa_get_n_reg_class(isa);
136         for(i = 0; i < n_classes; ++i) {
137                 const arch_register_class_t *cls    = arch_isa_get_reg_class(isa, i);
138                 unsigned                     n_regs = arch_register_class_n_regs(cls);
139                 memset(register_values[i], 0, sizeof(ir_node*) * n_regs);
140         }
141
142         assert(lv->nodes && "live sets must be computed");
143         be_lv_foreach(lv, block, be_lv_state_end, l) {
144                 ir_node *node = be_lv_get_irn(lv, block, l);
145                 set_value(node);
146         }
147
148         /* walk the block */
149         node = sched_last(block);
150         for( ; !sched_is_begin(node); node = sched_prev(node)) {
151                 ir_op             *op;
152                 peephole_opt_func  func;
153
154                 if(is_Phi(node))
155                         break;
156
157                 clear_defs(node);
158                 set_uses(node);
159
160                 op   = get_irn_op(node);
161                 func = (peephole_opt_func) op->ops.generic;
162                 if(func != NULL) {
163                         ir_node *new_node = func(node);
164                         if(new_node != NULL && new_node != node) {
165                                 be_liveness_remove(lv, node);
166                                 be_liveness_introduce(lv, new_node);
167                                 node = new_node;
168                                 set_uses(node);
169                         }
170                 }
171         }
172 }
173
174 void be_peephole_opt(be_irg_t *birg)
175 {
176         arch_isa_t *isa;
177         ir_graph   *irg = be_get_birg_irg(birg);
178         unsigned n_classes;
179         unsigned i;
180
181         /* we sometimes find BadE nodes in float apps like optest_float.c or
182          * kahansum.c for example... */
183         be_liveness_invalidate(birg->lv);
184         be_liveness_assure_sets(be_assure_liveness(birg));
185
186         arch_env = be_get_birg_arch_env(birg);
187         lv       = be_get_birg_liveness(birg);
188         isa      = arch_env->isa;
189
190         n_classes = arch_isa_get_n_reg_class(isa);
191         register_values = alloca(sizeof(register_values[0]) * n_classes);
192         for(i = 0; i < n_classes; ++i) {
193                 const arch_register_class_t *cls    = arch_isa_get_reg_class(isa, i);
194                 unsigned                     n_regs = arch_register_class_n_regs(cls);
195                 register_values[i] = alloca(sizeof(ir_node*) * n_regs);
196         }
197
198         irg_block_walk_graph(irg, process_block, NULL, NULL);
199 }
200
201 void be_peephole_init(void)
202 {
203         clear_irp_opcodes_generic_func();
204 }