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"
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)
63 switch (get_irn_opcode(node)) {
80 static ir_tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
82 return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
85 static int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
88 mode_is_int(src_mode) &&
89 mode_is_int(dest_mode) &&
90 get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
93 static int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
95 ir_mode *mode = get_irn_mode(node);
100 if (mode == dest_mode)
103 if (is_Const(node)) {
104 /* TODO tarval module is incomplete and can't convert floats to ints */
105 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
109 is_downconv(mode, dest_mode) &&
110 get_irn_mode(get_Conv_op(node)) == dest_mode) {
114 if (get_irn_n_edges(node) > 1) {
115 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
120 /* Take the minimum of the conversion costs for Phi predecessors as only one
121 * branch is actually executed at a time */
124 int arity = get_Phi_n_preds(node);
127 costs = get_conv_costs(get_Phi_pred(node, 0), dest_mode);
128 for (i = 1; i < arity; ++i) {
129 ir_node *pred = get_Phi_pred(node, i);
130 int c = get_conv_costs(pred, dest_mode);
131 if (c < costs) costs = c;
138 if (!is_downconv(mode, dest_mode)) {
143 ir_node *pred = get_Conv_op(node);
144 ir_mode *pred_mode = get_irn_mode(pred);
146 if (!values_in_mode(dest_mode, pred_mode)) {
149 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
152 if (!is_optimizable_node(node)) {
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 /* TODO tarval module is incomplete and can't convert floats to ints */
189 ir_tarval *tv = conv_const_tv(node, dest_mode);
190 if (tv == tarval_bad) {
191 return place_conv(node, dest_mode);
193 return new_r_Const(irg, tv);
198 is_downconv(mode, dest_mode) &&
199 get_irn_mode(get_Conv_op(node)) == dest_mode) {
200 return get_Conv_op(node);
203 if (get_irn_n_edges(node) > 1) {
204 return place_conv(node, dest_mode);
207 if (!is_downconv(mode, dest_mode)) {
208 return place_conv(node, dest_mode);
212 ir_node *pred = get_Conv_op(node);
213 ir_mode *pred_mode = get_irn_mode(pred);
215 if (!values_in_mode(dest_mode, pred_mode)) {
216 return place_conv(node, dest_mode);
218 return conv_transform(get_Conv_op(node), dest_mode);
221 if (!is_optimizable_node(node)) {
222 return place_conv(node, dest_mode);
225 // We want to create a new node with the right mode
226 arity = get_irn_arity(node);
227 ins = ALLOCAN(ir_node *, arity);
229 // The shift count does not participate in the conv optimisation
230 conv_arity = is_Shl(node) ? 1 : arity;
231 for (i = 0; i < conv_arity; i++) {
232 ir_node *pred = get_irn_n(node, i);
233 ir_node *transformed;
234 if (get_conv_costs(pred, dest_mode) > 0) {
235 transformed = place_conv(pred, dest_mode);
237 transformed = conv_transform(pred, dest_mode);
239 ins[i] = transformed;
242 for (i = conv_arity; i < arity; i++) {
243 ins[i] = get_irn_n(node, i);
246 new_node = new_ir_node(get_irn_dbg_info(node),
248 get_nodes_block(node),
253 copy_node_attr(irg, node, new_node);
258 static void conv_opt_walker(ir_node *node, void *data)
260 ir_node *transformed;
265 bool *changed = (bool*)data;
270 pred = get_Conv_op(node);
271 mode = get_irn_mode(node);
272 pred_mode = get_irn_mode(pred);
274 if (mode_is_reference(mode) || mode_is_reference(pred_mode))
277 if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
280 /* - 1 for the initial conv */
281 costs = get_conv_costs(pred, mode) - 1;
282 DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
286 transformed = conv_transform(pred, mode);
287 if (node != transformed) {
290 exchange(node, transformed);
291 vrp = vrp_get_info(transformed);
292 if (vrp && vrp->valid) {
293 vrp->range_type = VRP_VARYING;
294 vrp->bits_set = tarval_convert_to(vrp->bits_set, mode);
295 vrp->bits_not_set = tarval_convert_to(vrp->bits_not_set, mode);
302 int conv_opt(ir_graph *irg)
305 bool invalidate = false;
306 FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
308 DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
313 irg_walk_graph(irg, NULL, conv_opt_walker, &changed);
314 local_optimize_graph(irg);
315 invalidate |= changed;
321 /* Creates an ir_graph pass for conv_opt. */
322 ir_graph_pass_t *conv_opt_pass(const char *name)
324 ir_graph_pass_t *path = def_graph_pass_ret(name ? name : "conv_opt", conv_opt);
326 /* safe to run parallel on all irgs */
327 ir_graph_pass_set_parallel(path, 1);