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
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
43 #include "iroptimize.h"
51 #include "iredges_t.h"
55 DEBUG_ONLY(static firm_dbg_module_t *dbg);
58 int is_optimizable_node(const ir_node *node)
67 static tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
69 return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
73 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
75 ir_mode *mode = get_irn_mode(node);
80 if (mode == dest_mode)
84 /* TODO tarval module is incomplete and can't convert floats to ints */
85 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
88 if (get_irn_n_edges(node) > 1) {
89 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
94 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
97 if (!is_optimizable_node(node)) {
102 arity = get_irn_arity(node);
103 for (i = 0; i < arity; ++i) {
104 ir_node *pred = get_irn_n(node, i);
105 costs += get_conv_costs(pred, dest_mode);
111 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
113 ir_node *block = get_nodes_block(node);
114 ir_node *conv = new_r_Conv(current_ir_graph, block, node, dest_mode);
119 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
124 if (get_irn_mode(node) == dest_mode)
127 if (is_Const(node)) {
128 /* TODO tarval module is incomplete and can't convert floats to ints */
129 tarval *tv = conv_const_tv(node, dest_mode);
130 if (tv == tarval_bad) {
131 return place_conv(node, dest_mode);
133 return new_Const(dest_mode, tv);
137 if (get_irn_n_edges(node) > 1) {
138 return place_conv(node, dest_mode);
142 return conv_transform(get_Conv_op(node), dest_mode);
145 if (!is_optimizable_node(node)) {
146 return place_conv(node, dest_mode);
149 arity = get_irn_arity(node);
150 for (i = 0; i < arity; i++) {
151 ir_node *pred = get_irn_n(node, i);
152 ir_node *transformed = conv_transform(pred, dest_mode);
153 set_irn_n(node, i, transformed);
155 set_irn_mode(node, dest_mode);
160 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
163 mode_is_int(src_mode) &&
164 mode_is_int(dest_mode) &&
165 get_mode_size_bits(dest_mode) < get_mode_size_bits(src_mode);
168 /* TODO, backends (at least ia23) can't handle it at the moment,
169 and it's probably not more efficient on most
173 void try_optimize_cmp(ir_node *node)
175 ir_node *left = get_Cmp_left(node);
176 ir_node *right = get_Cmp_right(node);
177 ir_node *conv = NULL;
186 void conv_opt_walker(ir_node *node, void *data)
188 ir_node *transformed;
197 try_optimize_cmp(node);
205 pred = get_Conv_op(node);
206 mode = get_irn_mode(node);
207 pred_mode = get_irn_mode(pred);
209 if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
212 /* - 1 for the initial conv */
213 costs = get_conv_costs(pred, mode) - 1;
214 DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
215 if (costs >= 0) return;
217 transformed = conv_transform(pred, mode);
218 exchange(node, transformed);
222 void conv_opt(ir_graph *irg)
225 FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
227 DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
232 irg_walk_graph(irg, NULL, conv_opt_walker, NULL);
233 local_optimize_graph(irg);
234 invalidate |= changed;
238 set_irg_outs_inconsistent(irg);