2 * Copyright (C) 1995-2007 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
24 * @version $Id: condeval.c 13543 2007-04-29 19:29:02Z beck $
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
48 #include "iredges_t.h"
52 DEBUG_ONLY(static firm_dbg_module_t *dbg);
55 int is_optimizable_node(const ir_node *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))
64 return is_Add(node) || is_Sub(node) || is_Mul(node);
68 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
70 ir_mode *mode = get_irn_mode(node);
76 if(!is_Const(node) && get_irn_n_edges(node) > 1) {
77 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
82 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
85 if(is_optimizable_node(node)) {
87 int arity = get_irn_arity(node);
90 for(i = 0; i < arity; ++i) {
91 ir_node *pred = get_irn_n(node, i);
92 costs += get_conv_costs(pred, dest_mode);
102 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
107 if (get_irn_mode(node) == dest_mode)
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);
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);
123 return conv_transform(get_Conv_op(node), dest_mode);
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);
131 set_irn_mode(node, dest_mode);
136 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
138 if(!mode_is_int(src_mode) || !mode_is_int(dest_mode))
140 if(get_mode_size_bits(dest_mode) >= get_mode_size_bits(src_mode))
146 /* TODO, backends can't handle and it's probably not more efficient on most
150 void try_optimize_cmp(ir_node *node)
152 ir_node *left = get_Cmp_left(node);
153 ir_node *right = get_Cmp_right(node);
154 ir_node *conv = NULL;
161 void conv_opt_walker(ir_node *node, void *data)
163 ir_node *transformed;
171 try_optimize_cmp(node);
179 pred = get_Conv_op(node);
180 mode = get_irn_mode(node);
181 pred_mode = get_irn_mode(pred);
183 if(!is_downconv(pred_mode, mode))
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;
190 transformed = conv_transform(pred, mode);
191 exchange(node, transformed);
194 void conv_opt(ir_graph *irg)
196 FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
198 DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
201 irg_walk_graph(irg, conv_opt_walker, NULL, NULL);