convopt: also perform convopt for float nodes
[libfirm] / ir / opt / convopt.c
1 /*
2  * Copyright (C) 1995-2011 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  *
25  * Try to minimize the number of conv nodes by changing modes of operations.
26  * The typical example is the following structure:
27  *    (some node mode_Hs)
28  *            |                                       (some node_Hs)
29  *         Conv Is                                          |
30  *            |                                          Add Hs
31  *          Add Is            gets transformed to           |
32  *            |
33  *         Conv Hs
34  *
35  * TODO: * try to optimize cmp modes
36  *       * decide when it is useful to move the convs through phis
37  */
38 #include "config.h"
39
40 #include "iroptimize.h"
41
42 #include <assert.h>
43 #include <stdbool.h>
44 #include "debug.h"
45 #include "ircons.h"
46 #include "irgmod.h"
47 #include "irgopt.h"
48 #include "irnode_t.h"
49 #include "iropt_t.h"
50 #include "iredges_t.h"
51 #include "irgwalk.h"
52 #include "irprintf.h"
53 #include "irpass_t.h"
54 #include "tv.h"
55 #include "vrp.h"
56
57 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
58
59 static inline int imin(int a, int b) { return a < b ? a : b; }
60
61 static bool is_optimizable_node(const ir_node *node, ir_mode *dest_mode)
62 {
63         switch (get_irn_opcode(node)) {
64         case iro_Minus:
65         case iro_Phi:
66         case iro_And:
67         case iro_Eor:
68         case iro_Or:
69         case iro_Not:
70                 return true;
71         case iro_Add:
72         case iro_Mul:
73         case iro_Sub:
74                 if (mode_is_float(get_irn_mode(node)))
75                         return false;
76                 return true;
77         case iro_Shl: {
78                 int modulo_shift = get_mode_modulo_shift(dest_mode);
79                 int old_shift    = get_mode_modulo_shift(get_irn_mode(node));
80                 /* bail out if modulo shift changes */
81                 if (modulo_shift != old_shift)
82                         return false;
83                 return true;
84         }
85
86         default:
87                 return false;
88         }
89 }
90
91 static ir_tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
92 {
93         return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
94 }
95
96 static bool is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
97 {
98         return ((mode_is_int(src_mode) && mode_is_int(dest_mode))
99                 || (mode_is_float(src_mode) && mode_is_float(dest_mode)))
100                 && get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
101 }
102
103 static int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
104 {
105         ir_mode *mode = get_irn_mode(node);
106         int arity;
107         int i;
108         int costs;
109
110         if (mode == dest_mode)
111                 return 0;
112
113         if (is_Const(node)) {
114                 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
115         }
116
117         if (is_Conv(node) &&
118                         is_downconv(mode, dest_mode) &&
119                         get_irn_mode(get_Conv_op(node)) == dest_mode) {
120                 return -1;
121         }
122
123         if (get_irn_n_edges(node) > 1) {
124                 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
125                 return 1;
126         }
127
128         if (ir_zero_when_converted(node, dest_mode)) {
129                 return -1;
130         }
131
132 #if 0 // TODO
133         /* Take the minimum of the conversion costs for Phi predecessors as only one
134          * branch is actually executed at a time */
135         if (is_Phi(node)) {
136                 int i;
137                 int arity = get_Phi_n_preds(node);
138                 int costs;
139
140                 costs = get_conv_costs(get_Phi_pred(node, 0), dest_mode);
141                 for (i = 1; i < arity; ++i) {
142                         ir_node *pred = get_Phi_pred(node, i);
143                         int c = get_conv_costs(pred, dest_mode);
144                         if (c < costs) costs = c;
145                 }
146
147                 return costs;
148         }
149 #endif
150
151         if (!is_downconv(mode, dest_mode)) {
152                 return 1;
153         }
154
155         if (is_Conv(node)) {
156                 ir_node *pred      = get_Conv_op(node);
157                 ir_mode *pred_mode = get_irn_mode(pred);
158
159                 if (smaller_mode(pred_mode, dest_mode)) {
160                         return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
161                 }
162                 if (may_leave_out_middle_conv(pred_mode, mode, dest_mode)) {
163                         return 0;
164                 } else {
165                         return 1;
166                 }
167         }
168
169         if (!is_optimizable_node(node, dest_mode)) {
170                 return 1;
171         }
172
173         costs = 0;
174         // The shift count does not participate in the conv optimisation
175         arity = is_Shl(node) ? 1 : get_irn_arity(node);
176         for (i = 0; i < arity; ++i) {
177                 ir_node *pred = get_irn_n(node, i);
178                 costs += imin(get_conv_costs(pred, dest_mode), 1);
179         }
180
181         return costs;
182 }
183
184 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
185 {
186         ir_node *block = get_nodes_block(node);
187         ir_node *conv = new_r_Conv(block, node, dest_mode);
188         return conv;
189 }
190
191 static ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
192 {
193         ir_mode  *mode = get_irn_mode(node);
194         ir_graph *irg  = get_irn_irg(node);
195         int       arity;
196         int       conv_arity;
197         int       i;
198         ir_node  *new_node;
199         ir_node **ins;
200
201         if (mode == dest_mode)
202                 return node;
203
204         if (is_Const(node)) {
205                 ir_tarval *tv = conv_const_tv(node, dest_mode);
206                 if (tv == tarval_bad) {
207                         return place_conv(node, dest_mode);
208                 } else {
209                         return new_r_Const(irg, tv);
210                 }
211         }
212
213         if (is_Conv(node) &&
214                         is_downconv(mode, dest_mode) &&
215                         get_irn_mode(get_Conv_op(node)) == dest_mode) {
216                 return get_Conv_op(node);
217         }
218
219         if (get_irn_n_edges(node) > 1) {
220                 return place_conv(node, dest_mode);
221         }
222
223         if (!is_downconv(mode, dest_mode)) {
224                 return place_conv(node, dest_mode);
225         }
226
227         if (is_Conv(node)) {
228                 ir_node *pred      = get_Conv_op(node);
229                 ir_mode *pred_mode = get_irn_mode(pred);
230
231                 if (smaller_mode(pred_mode, dest_mode)) {
232                         return conv_transform(get_Conv_op(node), dest_mode);
233                 }
234                 return place_conv(node, dest_mode);
235         }
236
237         if (!is_optimizable_node(node, dest_mode)) {
238                 return place_conv(node, dest_mode);
239         }
240
241         // We want to create a new node with the right mode
242         arity = get_irn_arity(node);
243         ins = ALLOCAN(ir_node *, arity);
244
245         // The shift count does not participate in the conv optimisation
246         conv_arity = is_Shl(node) ? 1 : arity;
247         for (i = 0; i < conv_arity; i++) {
248                 ir_node *pred = get_irn_n(node, i);
249                 ir_node *transformed;
250                 if (get_conv_costs(pred, dest_mode) > 0) {
251                         transformed = place_conv(pred, dest_mode);
252                 } else {
253                         transformed = conv_transform(pred, dest_mode);
254                 }
255                 ins[i] = transformed;
256         }
257
258         for (i = conv_arity; i < arity; i++) {
259                 ins[i] = get_irn_n(node, i);
260         }
261
262         new_node = new_ir_node(get_irn_dbg_info(node),
263                                 irg,
264                                 get_nodes_block(node),
265                                 get_irn_op(node),
266                                 dest_mode,
267                                 arity,
268                                 ins);
269         copy_node_attr(irg, node, new_node);
270
271         return new_node;
272 }
273
274 static void conv_opt_walker(ir_node *node, void *data)
275 {
276         ir_node *transformed;
277         ir_node *pred;
278         ir_mode *pred_mode;
279         ir_mode *mode;
280         int costs;
281         bool *changed = (bool*)data;
282
283         if (!is_Conv(node))
284                 return;
285
286         pred      = get_Conv_op(node);
287         mode      = get_irn_mode(node);
288         pred_mode = get_irn_mode(pred);
289
290         if (mode_is_reference(mode) || mode_is_reference(pred_mode))
291                 return;
292
293         if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
294                 return;
295
296         /* - 1 for the initial conv */
297         costs = get_conv_costs(pred, mode) - 1;
298         DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
299         if (costs > 0)
300                 return;
301
302         transformed = conv_transform(pred, mode);
303         if (node != transformed) {
304                 exchange(node, transformed);
305                 *changed = true;
306         }
307 }
308
309 void conv_opt(ir_graph *irg)
310 {
311         bool global_changed = false;
312         bool changed;
313         FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
314
315         assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
316
317         DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
318
319         do {
320                 changed = false;
321                 irg_walk_graph(irg, NULL, conv_opt_walker, &changed);
322                 local_optimize_graph(irg);
323                 global_changed |= changed;
324         } while (changed);
325
326         confirm_irg_properties(irg,
327                 global_changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
328 }
329
330 /* Creates an ir_graph pass for conv_opt. */
331 ir_graph_pass_t *conv_opt_pass(const char *name)
332 {
333         ir_graph_pass_t *path = def_graph_pass(name ? name : "conv_opt", conv_opt);
334
335         /* safe to run parallel on all irgs */
336         ir_graph_pass_set_parallel(path, 1);
337
338         return path;
339 }