2 * Copyright (C) 1995-2011 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"
51 #include "iredges_t.h"
58 DEBUG_ONLY(static firm_dbg_module_t *dbg);
60 static inline int imin(int a, int b) { return a < b ? a : b; }
62 static bool is_optimizable_node(const ir_node *node, ir_mode *dest_mode)
64 switch (get_irn_opcode(node)) {
76 int modulo_shift = get_mode_modulo_shift(dest_mode);
77 int old_shift = get_mode_modulo_shift(get_irn_mode(node));
78 /* bail out if modulo shift changes */
79 if (modulo_shift != old_shift)
89 static ir_tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
91 return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
94 static int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
97 mode_is_int(src_mode) &&
98 mode_is_int(dest_mode) &&
99 get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
102 static int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
104 ir_mode *mode = get_irn_mode(node);
109 if (mode == dest_mode)
112 if (is_Const(node)) {
113 /* TODO tarval module is incomplete and can't convert floats to ints */
114 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
118 is_downconv(mode, dest_mode) &&
119 get_irn_mode(get_Conv_op(node)) == dest_mode) {
123 if (get_irn_n_edges(node) > 1) {
124 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
128 if (ir_zero_when_converted(node, dest_mode)) {
133 /* Take the minimum of the conversion costs for Phi predecessors as only one
134 * branch is actually executed at a time */
137 int arity = get_Phi_n_preds(node);
140 costs = get_conv_costs(get_Phi_pred(node, 0), dest_mode);
141 for (i = 1; i < arity; ++i) {
142 ir_node *pred = get_Phi_pred(node, i);
143 int c = get_conv_costs(pred, dest_mode);
144 if (c < costs) costs = c;
151 if (!is_downconv(mode, dest_mode)) {
156 ir_node *pred = get_Conv_op(node);
157 ir_mode *pred_mode = get_irn_mode(pred);
159 if (!values_in_mode(dest_mode, pred_mode)) {
162 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
165 if (!is_optimizable_node(node, dest_mode)) {
170 // The shift count does not participate in the conv optimisation
171 arity = is_Shl(node) ? 1 : get_irn_arity(node);
172 for (i = 0; i < arity; ++i) {
173 ir_node *pred = get_irn_n(node, i);
174 costs += imin(get_conv_costs(pred, dest_mode), 1);
180 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
182 ir_node *block = get_nodes_block(node);
183 ir_node *conv = new_r_Conv(block, node, dest_mode);
187 static ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
189 ir_mode *mode = get_irn_mode(node);
190 ir_graph *irg = get_irn_irg(node);
197 if (mode == dest_mode)
200 if (is_Const(node)) {
201 /* TODO tarval module is incomplete and can't convert floats to ints */
202 ir_tarval *tv = conv_const_tv(node, dest_mode);
203 if (tv == tarval_bad) {
204 return place_conv(node, dest_mode);
206 return new_r_Const(irg, tv);
211 is_downconv(mode, dest_mode) &&
212 get_irn_mode(get_Conv_op(node)) == dest_mode) {
213 return get_Conv_op(node);
216 if (get_irn_n_edges(node) > 1) {
217 return place_conv(node, dest_mode);
220 if (!is_downconv(mode, dest_mode)) {
221 return place_conv(node, dest_mode);
225 ir_node *pred = get_Conv_op(node);
226 ir_mode *pred_mode = get_irn_mode(pred);
228 if (!values_in_mode(dest_mode, pred_mode)) {
229 return place_conv(node, dest_mode);
231 return conv_transform(get_Conv_op(node), dest_mode);
234 if (!is_optimizable_node(node, dest_mode)) {
235 return place_conv(node, dest_mode);
238 // We want to create a new node with the right mode
239 arity = get_irn_arity(node);
240 ins = ALLOCAN(ir_node *, arity);
242 // The shift count does not participate in the conv optimisation
243 conv_arity = is_Shl(node) ? 1 : arity;
244 for (i = 0; i < conv_arity; i++) {
245 ir_node *pred = get_irn_n(node, i);
246 ir_node *transformed;
247 if (get_conv_costs(pred, dest_mode) > 0) {
248 transformed = place_conv(pred, dest_mode);
250 transformed = conv_transform(pred, dest_mode);
252 ins[i] = transformed;
255 for (i = conv_arity; i < arity; i++) {
256 ins[i] = get_irn_n(node, i);
259 new_node = new_ir_node(get_irn_dbg_info(node),
261 get_nodes_block(node),
266 copy_node_attr(irg, node, new_node);
271 static void conv_opt_walker(ir_node *node, void *data)
273 ir_node *transformed;
278 bool *changed = (bool*)data;
283 pred = get_Conv_op(node);
284 mode = get_irn_mode(node);
285 pred_mode = get_irn_mode(pred);
287 if (mode_is_reference(mode) || mode_is_reference(pred_mode))
290 if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
293 /* - 1 for the initial conv */
294 costs = get_conv_costs(pred, mode) - 1;
295 DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
299 transformed = conv_transform(pred, mode);
300 if (node != transformed) {
303 exchange(node, transformed);
304 vrp = vrp_get_info(transformed);
305 if (vrp && vrp->valid) {
306 vrp->range_type = VRP_VARYING;
307 vrp->bits_set = tarval_convert_to(vrp->bits_set, mode);
308 vrp->bits_not_set = tarval_convert_to(vrp->bits_not_set, mode);
315 int conv_opt(ir_graph *irg)
318 bool invalidate = false;
319 FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
321 DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
326 irg_walk_graph(irg, NULL, conv_opt_walker, &changed);
327 local_optimize_graph(irg);
328 invalidate |= changed;
334 /* Creates an ir_graph pass for conv_opt. */
335 ir_graph_pass_t *conv_opt_pass(const char *name)
337 ir_graph_pass_t *path = def_graph_pass_ret(name ? name : "conv_opt", conv_opt);
339 /* safe to run parallel on all irgs */
340 ir_graph_pass_set_parallel(path, 1);