b77557087ea8da521bfa7795a4692b41c2bff6ca
[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$
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 "convopt.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
54 DEBUG_ONLY(static firm_dbg_module_t *dbg);
55
56 static
57 int is_optimizable_node(const ir_node *node)
58 {
59         return
60                 is_Add(node) ||
61                 is_Sub(node) ||
62                 is_Mul(node) ||
63                 is_Phi(node);
64 }
65
66 static tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
67 {
68         return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
69 }
70
71 static
72 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
73 {
74         ir_mode *mode = get_irn_mode(node);
75         size_t arity;
76         size_t i;
77         int costs;
78
79         if (mode == dest_mode)
80                 return 0;
81
82         if (is_Const(node)) {
83                 /* TODO tarval module is incomplete and can't convert floats to ints */
84                 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
85         }
86
87         if (get_irn_n_edges(node) > 1) {
88                 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
89                 return 1;
90         }
91
92         if (is_Conv(node)) {
93                 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
94         }
95
96         if (!is_optimizable_node(node)) {
97                 return 1;
98         }
99
100         costs = 0;
101         arity = get_irn_arity(node);
102         for (i = 0; i < arity; ++i) {
103                 ir_node *pred = get_irn_n(node, i);
104                 costs += get_conv_costs(pred, dest_mode);
105         }
106
107         return costs;
108 }
109
110 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
111 {
112         ir_node *block = get_nodes_block(node);
113         ir_node *conv = new_r_Conv(current_ir_graph, block, node, dest_mode);
114         return conv;
115 }
116
117 static
118 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
119 {
120         size_t arity;
121         size_t i;
122
123         if (get_irn_mode(node) == dest_mode)
124                 return node;
125
126         if (is_Const(node)) {
127                 /* TODO tarval module is incomplete and can't convert floats to ints */
128                 tarval *tv = conv_const_tv(node, dest_mode);
129                 if (tv == tarval_bad) {
130                         return place_conv(node, dest_mode);
131                 } else {
132                         return new_Const(dest_mode, tv);
133                 }
134         }
135
136         if (get_irn_n_edges(node) > 1) {
137                 return place_conv(node, dest_mode);
138         }
139
140         if (is_Conv(node)) {
141                 return conv_transform(get_Conv_op(node), dest_mode);
142         }
143
144         if (!is_optimizable_node(node)) {
145                 return place_conv(node, dest_mode);
146         }
147
148         arity = get_irn_arity(node);
149         for (i = 0; i < arity; i++) {
150                 ir_node *pred = get_irn_n(node, i);
151                 ir_node *transformed = conv_transform(pred, dest_mode);
152                 set_irn_n(node, i, transformed);
153         }
154         set_irn_mode(node, dest_mode);
155         return node;
156 }
157
158 static
159 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
160 {
161         return
162                 mode_is_int(src_mode) &&
163                 mode_is_int(dest_mode) &&
164                 get_mode_size_bits(dest_mode) < get_mode_size_bits(src_mode);
165 }
166
167 /* TODO, backends can't handle and it's probably not more efficient on most
168    archs */
169 #if 0
170 static
171 void try_optimize_cmp(ir_node *node)
172 {
173         ir_node *left  = get_Cmp_left(node);
174         ir_node *right = get_Cmp_right(node);
175         ir_node *conv  = NULL;
176
177         if(is_downconv
178 }
179 #endif
180
181 static char changed;
182
183 static
184 void conv_opt_walker(ir_node *node, void *data)
185 {
186         ir_node *transformed;
187         ir_node *pred;
188         ir_mode *pred_mode;
189         ir_mode *mode;
190         int costs;
191
192 #if 0
193         if(is_Cmp(node)) {
194                 try_optimize_cmp(node);
195                 return;
196         }
197 #endif
198
199         if (!is_Conv(node))
200                 return;
201
202         pred      = get_Conv_op(node);
203         mode      = get_irn_mode(node);
204         pred_mode = get_irn_mode(pred);
205
206         if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
207                 return;
208
209         /* - 1 for the initial conv */
210         costs = get_conv_costs(pred, mode) - 1;
211         DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
212         if (costs >= 0) return;
213
214         transformed = conv_transform(pred, mode);
215         exchange(node, transformed);
216         changed = 1;
217 }
218
219 void conv_opt(ir_graph *irg)
220 {
221         FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
222
223         DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
224
225         edges_assure(irg);
226         do {
227                 changed = 0;
228                 irg_walk_graph(irg, NULL, conv_opt_walker, NULL);
229                 local_optimize_graph(irg);
230         } while (changed);
231 }