remove Abs node, backends can match the abs patterns themselfes
[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 <stdbool.h>
45 #include "debug.h"
46 #include "ircons.h"
47 #include "irgmod.h"
48 #include "irgopt.h"
49 #include "irnode_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)
62 {
63         switch (get_irn_opcode(node)) {
64         case iro_Add:
65         case iro_And:
66         case iro_Eor:
67         case iro_Minus:
68         case iro_Mul:
69         case iro_Not:
70         case iro_Or:
71         case iro_Phi:
72         case iro_Shl:
73         case iro_Sub:
74                 return true;
75         default:
76                 return false;
77         }
78 }
79
80 static tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
81 {
82         return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
83 }
84
85 static 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 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                 ir_node *pred      = get_Conv_op(node);
144                 ir_mode *pred_mode = get_irn_mode(pred);
145
146                 if (!values_in_mode(dest_mode, pred_mode)) {
147                         return 1;
148                 }
149                 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
150         }
151
152         if (!is_optimizable_node(node)) {
153                 return 1;
154         }
155
156         costs = 0;
157         // The shift count does not participate in the conv optimisation
158         arity = is_Shl(node) ? 1 : get_irn_arity(node);
159         for (i = 0; i < arity; ++i) {
160                 ir_node *pred = get_irn_n(node, i);
161                 costs += imin(get_conv_costs(pred, dest_mode), 1);
162         }
163
164         return costs;
165 }
166
167 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
168 {
169         ir_node *block = get_nodes_block(node);
170         ir_node *conv = new_r_Conv(block, node, dest_mode);
171         return conv;
172 }
173
174 static ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
175 {
176         ir_mode  *mode = get_irn_mode(node);
177         size_t    arity;
178         size_t    conv_arity;
179         size_t    i;
180         ir_node  *new_node;
181         ir_graph *irg;
182         ir_node **ins;
183
184         if (mode == dest_mode)
185                 return node;
186
187         if (is_Const(node)) {
188                 /* TODO tarval module is incomplete and can't convert floats to ints */
189                 tarval *tv = conv_const_tv(node, dest_mode);
190                 if (tv == tarval_bad) {
191                         return place_conv(node, dest_mode);
192                 } else {
193                         return new_Const(tv);
194                 }
195         }
196
197         if (is_Conv(node) &&
198                         is_downconv(mode, dest_mode) &&
199                         get_irn_mode(get_Conv_op(node)) == dest_mode) {
200                 return get_Conv_op(node);
201         }
202
203         if (get_irn_n_edges(node) > 1) {
204                 return place_conv(node, dest_mode);
205         }
206
207         if (!is_downconv(mode, dest_mode)) {
208                 return place_conv(node, dest_mode);
209         }
210
211         if (is_Conv(node)) {
212                 ir_node *pred      = get_Conv_op(node);
213                 ir_mode *pred_mode = get_irn_mode(pred);
214
215                 if (!values_in_mode(dest_mode, pred_mode)) {
216                         return place_conv(node, dest_mode);
217                 }
218                 return conv_transform(get_Conv_op(node), dest_mode);
219         }
220
221         if (!is_optimizable_node(node)) {
222                 return place_conv(node, dest_mode);
223         }
224
225         // We want to create a new node with the right mode
226         arity = get_irn_arity(node);
227         irg = get_irn_irg(node);
228         ins = ALLOCAN(ir_node *, arity);
229
230         // The shift count does not participate in the conv optimisation
231         conv_arity = is_Shl(node) ? 1 : arity;
232         for (i = 0; i < conv_arity; i++) {
233                 ir_node *pred = get_irn_n(node, i);
234                 ir_node *transformed;
235                 if (get_conv_costs(pred, dest_mode) > 0) {
236                         transformed = place_conv(pred, dest_mode);
237                 } else {
238                         transformed = conv_transform(pred, dest_mode);
239                 }
240                 ins[i] = transformed;
241         }
242
243         for (i = conv_arity; i < arity; i++) {
244                 ins[i] = get_irn_n(node, i);
245         }
246
247         new_node = new_ir_node(get_irn_dbg_info(node),
248                                 irg,
249                                 get_nodes_block(node),
250                                 get_irn_op(node),
251                                 dest_mode,
252                                 arity,
253                                 ins);
254         copy_node_attr(irg, node, new_node);
255
256         return new_node;
257 }
258
259 /* TODO, backends (at least ia32) can't handle it at the moment,
260    and it's probably not more efficient on most archs */
261 #if 0
262 static void try_optimize_cmp(ir_node *node)
263 {
264         ir_node *left  = get_Cmp_left(node);
265         ir_node *right = get_Cmp_right(node);
266         ir_node *conv  = NULL;
267
268         if (is_downconv
269 }
270 #endif
271
272 static void conv_opt_walker(ir_node *node, void *data)
273 {
274         ir_node *transformed;
275         ir_node *pred;
276         ir_mode *pred_mode;
277         ir_mode *mode;
278         int costs;
279         bool *changed = data;
280
281 #if 0
282         if (is_Cmp(node)) {
283                 try_optimize_cmp(node);
284                 return;
285         }
286 #endif
287
288         if (!is_Conv(node))
289                 return;
290
291         pred      = get_Conv_op(node);
292         mode      = get_irn_mode(node);
293         pred_mode = get_irn_mode(pred);
294
295         if (mode_is_reference(mode) || mode_is_reference(pred_mode))
296                 return;
297
298         if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
299                 return;
300
301         /* - 1 for the initial conv */
302         costs = get_conv_costs(pred, mode) - 1;
303         DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
304         if (costs > 0)
305                 return;
306
307         transformed = conv_transform(pred, mode);
308         if (node != transformed) {
309                 vrp_attr *vrp;
310
311                 exchange(node, transformed);
312                 vrp = vrp_get_info(transformed);
313                 if (vrp && vrp->valid) {
314                         vrp->range_type = VRP_VARYING;
315                         vrp->bits_set = tarval_convert_to(vrp->bits_set, mode);
316                         vrp->bits_not_set = tarval_convert_to(vrp->bits_not_set, mode);
317                 }
318
319                 *changed = true;
320         }
321 }
322
323 int conv_opt(ir_graph *irg)
324 {
325         bool changed;
326         bool invalidate = false;
327         FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
328
329         DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
330
331         edges_assure(irg);
332         do {
333                 changed = false;
334                 irg_walk_graph(irg, NULL, conv_opt_walker, &changed);
335                 local_optimize_graph(irg);
336                 invalidate |= changed;
337         } while (changed);
338
339         if (invalidate) {
340                 set_irg_outs_inconsistent(irg);
341         }
342         return invalidate;
343 }
344
345 /* Creates an ir_graph pass for conv_opt. */
346 ir_graph_pass_t *conv_opt_pass(const char *name)
347 {
348         ir_graph_pass_t *path = def_graph_pass_ret(name ? name : "conv_opt", conv_opt);
349
350         /* safe to run parallel on all irgs */
351         ir_graph_pass_set_parallel(path, 1);
352
353         return path;
354 }