Do not deconv reference modes.
[libfirm] / ir / opt / convopt.c
1 /*
2  * Copyright (C) 1995-2008 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   conv node optimisation
23  * @author  Matthias Braun, Christoph Mallon
24  * @version $Id$
25  *
26  * Try to minimize the number of conv nodes by changing modes of operations.
27  * The typical example is the following structure:
28  *    (some node mode_Hs)
29  *            |                                       (some node_Hs)
30  *         Conv Is                                          |
31  *            |                                          Add Hs
32  *          Add Is            gets transformed to           |
33  *            |
34  *         Conv Hs
35  *
36  * TODO: * try to optimize cmp modes
37  *       * decide when it is useful to move the convs through phis
38  */
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "iroptimize.h"
44
45 #include <assert.h>
46 #include "debug.h"
47 #include "ircons.h"
48 #include "irgmod.h"
49 #include "irgopt.h"
50 #include "irnode_t.h"
51 #include "iredges_t.h"
52 #include "irgwalk.h"
53 #include "irprintf.h"
54
55 DEBUG_ONLY(static firm_dbg_module_t *dbg);
56
57 static INLINE int imin(int a, int b) { return a < b ? a : b; }
58
59 static
60 int is_optimizable_node(const ir_node *node)
61 {
62         switch (get_irn_opcode(node)) {
63                 case iro_Add:
64                 case iro_And:
65                 case iro_Eor:
66                 case iro_Minus:
67                 case iro_Mul:
68                 case iro_Not:
69                 case iro_Or:
70                 case iro_Phi:
71                 case iro_Shl:
72                 case iro_Sub:
73                         return 1;
74
75                 default: return 0;
76         }
77 }
78
79 static tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
80 {
81         return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
82 }
83
84 static
85 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
86 {
87         return
88                 mode_is_int(src_mode) &&
89                 mode_is_int(dest_mode) &&
90                 get_mode_size_bits(dest_mode) < get_mode_size_bits(src_mode);
91 }
92
93 static
94 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
95 {
96         ir_mode *mode = get_irn_mode(node);
97         size_t arity;
98         size_t i;
99         int costs;
100
101         if (mode == dest_mode)
102                 return 0;
103
104         if (is_Const(node)) {
105                 /* TODO tarval module is incomplete and can't convert floats to ints */
106                 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
107         }
108
109         if (is_Conv(node) &&
110                         is_downconv(get_irn_mode(node), dest_mode) &&
111                         get_irn_mode(get_Conv_op(node)) == dest_mode) {
112                 return -1;
113         }
114
115         if (get_irn_n_edges(node) > 1) {
116                 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
117                 return 1;
118         }
119
120         if (is_Conv(node) && is_downconv(get_irn_mode(node), dest_mode)) {
121                 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
122         }
123
124 #if 0 // TODO
125         /* Take the minimum of the conversion costs for Phi predecessors as only one
126          * branch is actually executed at a time */
127         if (is_Phi(node)) {
128                 size_t i;
129                 size_t arity = get_Phi_n_preds(node);
130                 int costs;
131
132                 costs = get_conv_costs(get_Phi_pred(node, 0), dest_mode);
133                 for (i = 1; i < arity; ++i) {
134                         ir_node *pred = get_Phi_pred(node, i);
135                         int c = get_conv_costs(pred, dest_mode);
136                         if (c < costs) costs = c;
137                 }
138
139                 return costs;
140         }
141 #endif
142
143         if (!is_optimizable_node(node)) {
144                 return 1;
145         }
146
147         costs = 0;
148         // The shift count does not participate in the conv optimisation
149         arity = is_Shl(node) ? 1 : get_irn_arity(node);
150         for (i = 0; i < arity; ++i) {
151                 ir_node *pred = get_irn_n(node, i);
152                 costs += imin(get_conv_costs(pred, dest_mode), 1);
153         }
154
155         return costs;
156 }
157
158 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
159 {
160         ir_node *block = get_nodes_block(node);
161         ir_node *conv = new_r_Conv(current_ir_graph, block, node, dest_mode);
162         return conv;
163 }
164
165 static
166 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
167 {
168         size_t arity;
169         size_t i;
170
171         if (get_irn_mode(node) == dest_mode)
172                 return node;
173
174         if (is_Const(node)) {
175                 /* TODO tarval module is incomplete and can't convert floats to ints */
176                 tarval *tv = conv_const_tv(node, dest_mode);
177                 if (tv == tarval_bad) {
178                         return place_conv(node, dest_mode);
179                 } else {
180                         return new_Const(dest_mode, tv);
181                 }
182         }
183
184         if (is_Conv(node) &&
185                         is_downconv(get_irn_mode(node), dest_mode) &&
186                         get_irn_mode(get_Conv_op(node)) == dest_mode) {
187                 return get_Conv_op(node);
188         }
189
190         if (get_irn_n_edges(node) > 1) {
191                 return place_conv(node, dest_mode);
192         }
193
194         if (is_Conv(node) && is_downconv(get_irn_mode(node), dest_mode)) {
195                 return conv_transform(get_Conv_op(node), dest_mode);
196         }
197
198         if (!is_optimizable_node(node)) {
199                 return place_conv(node, dest_mode);
200         }
201
202         // The shift count does not participate in the conv optimisation
203         arity = is_Shl(node) ? 1 : get_irn_arity(node);
204         for (i = 0; i < arity; i++) {
205                 ir_node *pred = get_irn_n(node, i);
206                 ir_node *transformed;
207                 if (get_conv_costs(pred, dest_mode) > 0) {
208                         transformed = place_conv(pred, dest_mode);
209                 } else {
210                         transformed = conv_transform(pred, dest_mode);
211                 }
212                 set_irn_n(node, i, transformed);
213         }
214         set_irn_mode(node, dest_mode);
215         return node;
216 }
217
218 /* TODO, backends (at least ia32) can't handle it at the moment,
219    and it's probably not more efficient on most archs */
220 #if 0
221 static
222 void try_optimize_cmp(ir_node *node)
223 {
224         ir_node *left  = get_Cmp_left(node);
225         ir_node *right = get_Cmp_right(node);
226         ir_node *conv  = NULL;
227
228         if(is_downconv
229 }
230 #endif
231
232 static char changed;
233
234 static
235 void conv_opt_walker(ir_node *node, void *data)
236 {
237         ir_node *transformed;
238         ir_node *pred;
239         ir_mode *pred_mode;
240         ir_mode *mode;
241         int costs;
242         (void) data;
243
244 #if 0
245         if(is_Cmp(node)) {
246                 try_optimize_cmp(node);
247                 return;
248         }
249 #endif
250
251         if (!is_Conv(node))
252                 return;
253
254         pred      = get_Conv_op(node);
255         mode      = get_irn_mode(node);
256         pred_mode = get_irn_mode(pred);
257
258         if (mode_is_reference(mode) || mode_is_reference(pred_mode))
259                 return;
260
261         if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
262                 return;
263
264         /* - 1 for the initial conv */
265         costs = get_conv_costs(pred, mode) - 1;
266         DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
267         if (costs > 0) return;
268
269         transformed = conv_transform(pred, mode);
270         if (node != transformed) {
271                 exchange(node, transformed);
272                 changed = 1;
273         }
274 }
275
276 void conv_opt(ir_graph *irg)
277 {
278         char invalidate = 0;
279         FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
280
281         DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
282
283         edges_assure(irg);
284         do {
285                 changed = 0;
286                 irg_walk_graph(irg, NULL, conv_opt_walker, NULL);
287                 local_optimize_graph(irg);
288                 invalidate |= changed;
289         } while (changed);
290
291         if (invalidate) {
292                 set_irg_outs_inconsistent(irg);
293         }
294 }