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