committ the conv optimisation
[libfirm] / ir / opt / convopt.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   conv node optimisation
23  * @author  Matthias Braun, Christoph Mallon
24  * @version $Id: condeval.c 13543 2007-04-29 19:29:02Z beck $
25  *
26  * Try to minimize the number of conv nodes by changing modes of operations.
27  * The typical example is the following structure:
28  *    (some node mode_Hs)
29  *            |                                       (some node_Hs)
30  *         Conv Is                                          |
31  *            |                                          Add Hs
32  *          Add Is            gets transformed to           |
33  *            |
34  *         Conv Hs
35  */
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <assert.h>
41 #include "debug.h"
42 #include "ircons.h"
43 #include "irgmod.h"
44 #include "irnode_t.h"
45 #include "iredges_t.h"
46 #include "irgwalk.h"
47 #include "irprintf.h"
48
49 DEBUG_ONLY(static firm_dbg_module_t *dbg);
50
51 static
52 int is_optimizable_node(const ir_node *node)
53 {
54         if(is_Const(node)) {
55                 ir_mode *mode = get_irn_mode(node);
56                 /* tarval module is incomplete and can't convert floats to ints */
57                 if(!mode_is_int(mode))
58                         return 0;
59                 return 1;
60         }
61         return is_Add(node) || is_Sub(node) || is_Mul(node);
62 }
63
64 static
65 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
66 {
67         ir_mode *mode = get_irn_mode(node);
68
69         if(mode == dest_mode)
70                 return 0;
71
72         /* TODO... */
73         if(!is_Const(node) && get_irn_n_edges(node) > 1) {
74                 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
75                 return 1;
76         }
77
78         if(is_Conv(node)) {
79                 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
80         }
81
82         if(is_optimizable_node(node)) {
83                 int i;
84                 int arity = get_irn_arity(node);
85                 int costs = 0;
86
87                 for(i = 0; i < arity; ++i) {
88                         ir_node *pred = get_irn_n(node, i);
89                         costs += get_conv_costs(pred, dest_mode);
90                 }
91
92                 return costs;
93         }
94
95         return 1;
96 }
97
98 static
99 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
100 {
101         size_t arity;
102         size_t i;
103
104         if (get_irn_mode(node) == dest_mode)
105                 return node;
106
107         if (is_Const(node)) {
108                 tarval *tv = tarval_convert_to(get_Const_tarval(node), dest_mode);
109                 assert(get_tarval_mode(tv) == dest_mode);
110                 return new_Const(dest_mode, tv);
111         }
112
113         if (!is_optimizable_node(node) || get_irn_n_edges(node) > 1) {
114                 ir_node *block = get_nodes_block(node);
115                 ir_node *conv = new_r_Conv(current_ir_graph, block, node, dest_mode);
116                 return conv;
117         }
118
119         if (is_Conv(node))
120                 return conv_transform(get_Conv_op(node), dest_mode);
121
122         arity = get_irn_arity(node);
123         for (i = 0; i < arity; i++) {
124                 ir_node *pred = get_irn_n(node, i);
125                 ir_node *transformed = conv_transform(pred, dest_mode);
126                 set_irn_n(node, i, transformed);
127         }
128         set_irn_mode(node, dest_mode);
129         return node;
130 }
131
132 static
133 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
134 {
135         if(!mode_is_int(src_mode) || !mode_is_int(dest_mode))
136                 return 0;
137         if(get_mode_size_bits(dest_mode) >= get_mode_size_bits(src_mode))
138                 return 0;
139
140         return 1;
141 }
142
143 /* TODO, backends can't handle and it's probably not more efficient on most
144    archs */
145 #if 0
146 static
147 void try_optimize_cmp(ir_node *node)
148 {
149         ir_node *left  = get_Cmp_left(node);
150         ir_node *right = get_Cmp_right(node);
151         ir_node *conv  = NULL;
152
153         if(is_downconv
154 }
155 #endif
156
157 static
158 void conv_opt_walker(ir_node *node, void *data)
159 {
160         ir_node *transformed;
161         ir_node *pred;
162         ir_mode *pred_mode;
163         ir_mode *mode;
164         int costs;
165
166 #if 0
167         if(is_Cmp(node)) {
168                 try_optimize_cmp(node);
169                 return;
170         }
171 #endif
172
173         if(!is_Conv(node))
174                 return;
175
176         pred      = get_Conv_op(node);
177         mode      = get_irn_mode(node);
178         pred_mode = get_irn_mode(pred);
179
180         if(!is_downconv(pred_mode, mode))
181                 return;
182
183         costs = get_conv_costs(pred, mode);
184         DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
185         if (costs >= 0) return;
186
187         transformed = conv_transform(pred, mode);
188         exchange(node, transformed);
189 }
190
191 void conv_opt(ir_graph *irg)
192 {
193         FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
194
195         DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
196
197         edges_assure(irg);
198         irg_walk_graph(irg, conv_opt_walker, NULL, NULL);
199 }