2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief conv node optimisation
23 * @author Matthias Braun, Christoph Mallon
26 * Try to minimize the number of conv nodes by changing modes of operations.
27 * The typical example is the following structure:
32 * Add Is gets transformed to |
36 * TODO: * try to optimize cmp modes
37 * * decide when it is useful to move the convs through phis
41 #include "iroptimize.h"
49 #include "iredges_t.h"
53 DEBUG_ONLY(static firm_dbg_module_t *dbg);
55 static inline int imin(int a, int b) { return a < b ? a : b; }
58 int is_optimizable_node(const ir_node *node)
60 switch (get_irn_opcode(node)) {
77 static tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
79 return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
83 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
86 mode_is_int(src_mode) &&
87 mode_is_int(dest_mode) &&
88 get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
92 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
94 ir_mode *mode = get_irn_mode(node);
99 if (mode == dest_mode)
102 if (is_Const(node)) {
103 /* TODO tarval module is incomplete and can't convert floats to ints */
104 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
108 is_downconv(mode, dest_mode) &&
109 get_irn_mode(get_Conv_op(node)) == dest_mode) {
113 if (get_irn_n_edges(node) > 1) {
114 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
119 /* Take the minimum of the conversion costs for Phi predecessors as only one
120 * branch is actually executed at a time */
123 size_t arity = get_Phi_n_preds(node);
126 costs = get_conv_costs(get_Phi_pred(node, 0), dest_mode);
127 for (i = 1; i < arity; ++i) {
128 ir_node *pred = get_Phi_pred(node, i);
129 int c = get_conv_costs(pred, dest_mode);
130 if (c < costs) costs = c;
137 if (!is_downconv(mode, dest_mode)) {
142 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
145 if (!is_optimizable_node(node)) {
150 // The shift count does not participate in the conv optimisation
151 arity = is_Shl(node) ? 1 : get_irn_arity(node);
152 for (i = 0; i < arity; ++i) {
153 ir_node *pred = get_irn_n(node, i);
154 costs += imin(get_conv_costs(pred, dest_mode), 1);
160 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
162 ir_node *block = get_nodes_block(node);
163 ir_node *conv = new_r_Conv(current_ir_graph, block, node, dest_mode);
168 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
170 ir_mode *mode = get_irn_mode(node);
174 if (mode == dest_mode)
177 if (is_Const(node)) {
178 /* TODO tarval module is incomplete and can't convert floats to ints */
179 tarval *tv = conv_const_tv(node, dest_mode);
180 if (tv == tarval_bad) {
181 return place_conv(node, dest_mode);
183 return new_Const(tv);
188 is_downconv(mode, dest_mode) &&
189 get_irn_mode(get_Conv_op(node)) == dest_mode) {
190 return get_Conv_op(node);
193 if (get_irn_n_edges(node) > 1) {
194 return place_conv(node, dest_mode);
197 if (!is_downconv(mode, dest_mode)) {
198 return place_conv(node, dest_mode);
202 return conv_transform(get_Conv_op(node), dest_mode);
205 if (!is_optimizable_node(node)) {
206 return place_conv(node, dest_mode);
209 // The shift count does not participate in the conv optimisation
210 arity = is_Shl(node) ? 1 : get_irn_arity(node);
211 for (i = 0; i < arity; i++) {
212 ir_node *pred = get_irn_n(node, i);
213 ir_node *transformed;
214 if (get_conv_costs(pred, dest_mode) > 0) {
215 transformed = place_conv(pred, dest_mode);
217 transformed = conv_transform(pred, dest_mode);
219 set_irn_n(node, i, transformed);
221 set_irn_mode(node, dest_mode);
225 /* TODO, backends (at least ia32) can't handle it at the moment,
226 and it's probably not more efficient on most archs */
229 void try_optimize_cmp(ir_node *node)
231 ir_node *left = get_Cmp_left(node);
232 ir_node *right = get_Cmp_right(node);
233 ir_node *conv = NULL;
242 void conv_opt_walker(ir_node *node, void *data)
244 ir_node *transformed;
253 try_optimize_cmp(node);
261 pred = get_Conv_op(node);
262 mode = get_irn_mode(node);
263 pred_mode = get_irn_mode(pred);
265 if (mode_is_reference(mode) || mode_is_reference(pred_mode))
268 if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
271 /* - 1 for the initial conv */
272 costs = get_conv_costs(pred, mode) - 1;
273 DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
274 if (costs > 0) return;
276 transformed = conv_transform(pred, mode);
277 if (node != transformed) {
278 exchange(node, transformed);
283 int conv_opt(ir_graph *irg)
286 FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
288 DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
293 irg_walk_graph(irg, NULL, conv_opt_walker, NULL);
294 local_optimize_graph(irg);
295 invalidate |= changed;
299 set_irg_outs_inconsistent(irg);