add TODO items
[libfirm] / ir / opt / convopt.c
1 /*
2  * Copyright (C) 1995-2007 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: condeval.c 13543 2007-04-29 19:29:02Z beck $
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 <assert.h>
44 #include "debug.h"
45 #include "ircons.h"
46 #include "irgmod.h"
47 #include "irnode_t.h"
48 #include "iredges_t.h"
49 #include "irgwalk.h"
50 #include "irprintf.h"
51
52 DEBUG_ONLY(static firm_dbg_module_t *dbg);
53
54 static
55 int is_optimizable_node(const ir_node *node)
56 {
57         if(is_Const(node)) {
58                 ir_mode *mode = get_irn_mode(node);
59                 /* tarval module is incomplete and can't convert floats to ints */
60                 if(!mode_is_int(mode))
61                         return 0;
62                 return 1;
63         }
64         return is_Add(node) || is_Sub(node) || is_Mul(node);
65 }
66
67 static
68 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
69 {
70         ir_mode *mode = get_irn_mode(node);
71
72         if(mode == dest_mode)
73                 return 0;
74
75         /* TODO... */
76         if(!is_Const(node) && get_irn_n_edges(node) > 1) {
77                 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
78                 return 1;
79         }
80
81         if(is_Conv(node)) {
82                 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
83         }
84
85         if(is_optimizable_node(node)) {
86                 int i;
87                 int arity = get_irn_arity(node);
88                 int costs = 0;
89
90                 for(i = 0; i < arity; ++i) {
91                         ir_node *pred = get_irn_n(node, i);
92                         costs += get_conv_costs(pred, dest_mode);
93                 }
94
95                 return costs;
96         }
97
98         return 1;
99 }
100
101 static
102 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
103 {
104         size_t arity;
105         size_t i;
106
107         if (get_irn_mode(node) == dest_mode)
108                 return node;
109
110         if (is_Const(node)) {
111                 tarval *tv = tarval_convert_to(get_Const_tarval(node), dest_mode);
112                 assert(get_tarval_mode(tv) == dest_mode);
113                 return new_Const(dest_mode, tv);
114         }
115
116         if (!is_optimizable_node(node) || get_irn_n_edges(node) > 1) {
117                 ir_node *block = get_nodes_block(node);
118                 ir_node *conv = new_r_Conv(current_ir_graph, block, node, dest_mode);
119                 return conv;
120         }
121
122         if (is_Conv(node))
123                 return conv_transform(get_Conv_op(node), dest_mode);
124
125         arity = get_irn_arity(node);
126         for (i = 0; i < arity; i++) {
127                 ir_node *pred = get_irn_n(node, i);
128                 ir_node *transformed = conv_transform(pred, dest_mode);
129                 set_irn_n(node, i, transformed);
130         }
131         set_irn_mode(node, dest_mode);
132         return node;
133 }
134
135 static
136 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
137 {
138         if(!mode_is_int(src_mode) || !mode_is_int(dest_mode))
139                 return 0;
140         if(get_mode_size_bits(dest_mode) >= get_mode_size_bits(src_mode))
141                 return 0;
142
143         return 1;
144 }
145
146 /* TODO, backends can't handle and it's probably not more efficient on most
147    archs */
148 #if 0
149 static
150 void try_optimize_cmp(ir_node *node)
151 {
152         ir_node *left  = get_Cmp_left(node);
153         ir_node *right = get_Cmp_right(node);
154         ir_node *conv  = NULL;
155
156         if(is_downconv
157 }
158 #endif
159
160 static
161 void conv_opt_walker(ir_node *node, void *data)
162 {
163         ir_node *transformed;
164         ir_node *pred;
165         ir_mode *pred_mode;
166         ir_mode *mode;
167         int costs;
168
169 #if 0
170         if(is_Cmp(node)) {
171                 try_optimize_cmp(node);
172                 return;
173         }
174 #endif
175
176         if(!is_Conv(node))
177                 return;
178
179         pred      = get_Conv_op(node);
180         mode      = get_irn_mode(node);
181         pred_mode = get_irn_mode(pred);
182
183         if(!is_downconv(pred_mode, mode))
184                 return;
185
186         costs = get_conv_costs(pred, mode);
187         DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
188         if (costs >= 0) return;
189
190         transformed = conv_transform(pred, mode);
191         exchange(node, transformed);
192 }
193
194 void conv_opt(ir_graph *irg)
195 {
196         FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
197
198         DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
199
200         edges_assure(irg);
201         irg_walk_graph(irg, conv_opt_walker, NULL, NULL);
202 }