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
25 * Try to minimize the number of conv nodes by changing modes of operations.
26 * The typical example is the following structure:
31 * Add Is gets transformed to |
35 * TODO: * try to optimize cmp modes
36 * * decide when it is useful to move the convs through phis
40 #include "iroptimize.h"
50 #include "iredges_t.h"
57 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
59 static inline int imin(int a, int b) { return a < b ? a : b; }
61 static bool is_optimizable_node(const ir_node *node, ir_mode *dest_mode)
63 switch (get_irn_opcode(node)) {
74 if (mode_is_float(get_irn_mode(node)))
78 int modulo_shift = get_mode_modulo_shift(dest_mode);
79 int old_shift = get_mode_modulo_shift(get_irn_mode(node));
80 /* bail out if modulo shift changes */
81 if (modulo_shift != old_shift)
91 static ir_tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
93 return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
96 static bool is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
98 return ((mode_is_int(src_mode) && mode_is_int(dest_mode))
99 || (mode_is_float(src_mode) && mode_is_float(dest_mode)))
100 && get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
103 static int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
105 ir_mode *mode = get_irn_mode(node);
110 if (mode == dest_mode)
113 if (is_Const(node)) {
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)) {
132 /* TODO: Phi nodes */
134 if (!is_downconv(mode, dest_mode)) {
139 ir_node *pred = get_Conv_op(node);
140 ir_mode *pred_mode = get_irn_mode(pred);
142 if (smaller_mode(pred_mode, dest_mode)) {
143 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
145 if (may_leave_out_middle_conv(pred_mode, mode, dest_mode)) {
152 if (!is_optimizable_node(node, dest_mode)) {
157 // The shift count does not participate in the conv optimisation
158 arity = is_Shl(node) ? 1 : get_irn_arity(node);
159 for (i = 0; i < arity; ++i) {
160 ir_node *pred = get_irn_n(node, i);
161 costs += imin(get_conv_costs(pred, dest_mode), 1);
167 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
169 ir_node *block = get_nodes_block(node);
170 ir_node *conv = new_r_Conv(block, node, dest_mode);
174 static ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
176 ir_mode *mode = get_irn_mode(node);
177 ir_graph *irg = get_irn_irg(node);
184 if (mode == dest_mode)
187 if (is_Const(node)) {
188 ir_tarval *tv = conv_const_tv(node, dest_mode);
189 if (tv == tarval_bad) {
190 return place_conv(node, dest_mode);
192 return new_r_Const(irg, tv);
197 is_downconv(mode, dest_mode) &&
198 get_irn_mode(get_Conv_op(node)) == dest_mode) {
199 return get_Conv_op(node);
202 if (get_irn_n_edges(node) > 1) {
203 return place_conv(node, dest_mode);
206 if (!is_downconv(mode, dest_mode)) {
207 return place_conv(node, dest_mode);
211 ir_node *pred = get_Conv_op(node);
212 ir_mode *pred_mode = get_irn_mode(pred);
214 if (smaller_mode(pred_mode, dest_mode)) {
215 return conv_transform(get_Conv_op(node), dest_mode);
217 return place_conv(node, dest_mode);
220 if (!is_optimizable_node(node, dest_mode)) {
221 return place_conv(node, dest_mode);
224 // We want to create a new node with the right mode
225 arity = get_irn_arity(node);
226 ins = ALLOCAN(ir_node *, arity);
228 // The shift count does not participate in the conv optimisation
229 conv_arity = is_Shl(node) ? 1 : arity;
230 for (i = 0; i < conv_arity; i++) {
231 ir_node *pred = get_irn_n(node, i);
232 ir_node *transformed;
233 if (get_conv_costs(pred, dest_mode) > 0) {
234 transformed = place_conv(pred, dest_mode);
236 transformed = conv_transform(pred, dest_mode);
238 ins[i] = transformed;
241 for (i = conv_arity; i < arity; i++) {
242 ins[i] = get_irn_n(node, i);
245 new_node = new_ir_node(get_irn_dbg_info(node),
247 get_nodes_block(node),
252 copy_node_attr(irg, node, new_node);
257 static void conv_opt_walker(ir_node *node, void *data)
259 ir_node *transformed;
264 bool *changed = (bool*)data;
269 pred = get_Conv_op(node);
270 mode = get_irn_mode(node);
271 pred_mode = get_irn_mode(pred);
273 if (mode_is_reference(mode) || mode_is_reference(pred_mode))
276 if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
279 /* - 1 for the initial conv */
280 costs = get_conv_costs(pred, mode) - 1;
281 DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
285 transformed = conv_transform(pred, mode);
286 if (node != transformed) {
287 exchange(node, transformed);
292 void conv_opt(ir_graph *irg)
294 bool global_changed = false;
296 FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
298 assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
300 DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
304 irg_walk_graph(irg, NULL, conv_opt_walker, &changed);
305 local_optimize_graph(irg);
306 global_changed |= changed;
309 confirm_irg_properties(irg,
310 global_changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
313 /* Creates an ir_graph pass for conv_opt. */
314 ir_graph_pass_t *conv_opt_pass(const char *name)
316 ir_graph_pass_t *path = def_graph_pass(name ? name : "conv_opt", conv_opt);
318 /* safe to run parallel on all irgs */
319 ir_graph_pass_set_parallel(path, 1);