2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief conv node optimisation
9 * @author Matthias Braun, Christoph Mallon
11 * Try to minimize the number of conv nodes by changing modes of operations.
12 * The typical example is the following structure:
17 * Add Is gets transformed to |
21 * TODO: * try to optimize cmp modes
22 * * decide when it is useful to move the convs through phis
26 #include "iroptimize.h"
36 #include "iredges_t.h"
43 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
45 static inline int imin(int a, int b) { return a < b ? a : b; }
47 static bool is_optimizable_node(const ir_node *node, ir_mode *dest_mode)
49 switch (get_irn_opcode(node)) {
60 if (mode_is_float(get_irn_mode(node)))
64 int modulo_shift = get_mode_modulo_shift(dest_mode);
65 int old_shift = get_mode_modulo_shift(get_irn_mode(node));
66 /* bail out if modulo shift changes */
67 if (modulo_shift != old_shift)
77 static ir_tarval* conv_const_tv(const ir_node* cnst, ir_mode* dest_mode)
79 return tarval_convert_to(get_Const_tarval(cnst), dest_mode);
82 static bool is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
84 return ((mode_is_int(src_mode) && mode_is_int(dest_mode))
85 || (mode_is_float(src_mode) && mode_is_float(dest_mode)))
86 && get_mode_size_bits(dest_mode) <= get_mode_size_bits(src_mode);
89 static int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
91 ir_mode *mode = get_irn_mode(node);
96 if (mode == dest_mode)
100 return conv_const_tv(node, dest_mode) == tarval_bad ? 1 : 0;
104 is_downconv(mode, dest_mode) &&
105 get_irn_mode(get_Conv_op(node)) == dest_mode) {
109 if (get_irn_n_edges(node) > 1) {
110 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
114 if (ir_zero_when_converted(node, dest_mode)) {
118 /* TODO: Phi nodes */
120 if (!is_downconv(mode, dest_mode)) {
125 ir_node *pred = get_Conv_op(node);
126 ir_mode *pred_mode = get_irn_mode(pred);
128 if (smaller_mode(pred_mode, dest_mode)) {
129 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
131 if (may_leave_out_middle_conv(pred_mode, mode, dest_mode)) {
138 if (!is_optimizable_node(node, dest_mode)) {
143 // The shift count does not participate in the conv optimisation
144 arity = is_Shl(node) ? 1 : get_irn_arity(node);
145 for (i = 0; i < arity; ++i) {
146 ir_node *pred = get_irn_n(node, i);
147 costs += imin(get_conv_costs(pred, dest_mode), 1);
153 static ir_node *place_conv(ir_node *node, ir_mode *dest_mode)
155 ir_node *block = get_nodes_block(node);
156 ir_node *conv = new_r_Conv(block, node, dest_mode);
160 static ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
162 ir_mode *mode = get_irn_mode(node);
163 ir_graph *irg = get_irn_irg(node);
170 if (mode == dest_mode)
173 if (is_Const(node)) {
174 ir_tarval *tv = conv_const_tv(node, dest_mode);
175 if (tv == tarval_bad) {
176 return place_conv(node, dest_mode);
178 return new_r_Const(irg, tv);
183 is_downconv(mode, dest_mode) &&
184 get_irn_mode(get_Conv_op(node)) == dest_mode) {
185 return get_Conv_op(node);
188 if (get_irn_n_edges(node) > 1) {
189 return place_conv(node, dest_mode);
192 if (!is_downconv(mode, dest_mode)) {
193 return place_conv(node, dest_mode);
197 ir_node *pred = get_Conv_op(node);
198 ir_mode *pred_mode = get_irn_mode(pred);
200 if (smaller_mode(pred_mode, dest_mode)) {
201 return conv_transform(get_Conv_op(node), dest_mode);
203 return place_conv(node, dest_mode);
206 if (!is_optimizable_node(node, dest_mode)) {
207 return place_conv(node, dest_mode);
210 // We want to create a new node with the right mode
211 arity = get_irn_arity(node);
212 ins = ALLOCAN(ir_node *, arity);
214 // The shift count does not participate in the conv optimisation
215 conv_arity = is_Shl(node) ? 1 : arity;
216 for (i = 0; i < conv_arity; i++) {
217 ir_node *pred = get_irn_n(node, i);
218 ir_node *transformed;
219 if (get_conv_costs(pred, dest_mode) > 0) {
220 transformed = place_conv(pred, dest_mode);
222 transformed = conv_transform(pred, dest_mode);
224 ins[i] = transformed;
227 for (i = conv_arity; i < arity; i++) {
228 ins[i] = get_irn_n(node, i);
231 new_node = new_ir_node(get_irn_dbg_info(node),
233 get_nodes_block(node),
238 copy_node_attr(irg, node, new_node);
243 static void conv_opt_walker(ir_node *node, void *data)
245 ir_node *transformed;
250 bool *changed = (bool*)data;
255 pred = get_Conv_op(node);
256 mode = get_irn_mode(node);
257 pred_mode = get_irn_mode(pred);
259 if (mode_is_reference(mode) || mode_is_reference(pred_mode))
262 if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
265 /* - 1 for the initial conv */
266 costs = get_conv_costs(pred, mode) - 1;
267 DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
271 transformed = conv_transform(pred, mode);
272 if (node != transformed) {
273 exchange(node, transformed);
278 void conv_opt(ir_graph *irg)
280 bool global_changed = false;
282 FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
284 assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
286 DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
290 irg_walk_graph(irg, NULL, conv_opt_walker, &changed);
291 local_optimize_graph(irg);
292 global_changed |= changed;
295 confirm_irg_properties(irg,
296 global_changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
299 /* Creates an ir_graph pass for conv_opt. */
300 ir_graph_pass_t *conv_opt_pass(const char *name)
302 ir_graph_pass_t *path = def_graph_pass(name ? name : "conv_opt", conv_opt);
304 /* safe to run parallel on all irgs */
305 ir_graph_pass_set_parallel(path, 1);