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