9eacd79ff26fa200dfdec2eb36ec3234de8e780f
[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$
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  * TODO: * try to optimize cmp modes
37  *       * decide when it is useful to move the convs through phis
38  */
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <assert.h>
44 #include "convopt.h"
45 #include "debug.h"
46 #include "ircons.h"
47 #include "irgmod.h"
48 #include "irnode_t.h"
49 #include "iredges_t.h"
50 #include "irgwalk.h"
51 #include "irprintf.h"
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg);
54
55 static
56 int is_optimizable_node(const ir_node *node)
57 {
58         if(is_Const(node)) {
59                 ir_mode *mode = get_irn_mode(node);
60                 /* tarval module is incomplete and can't convert floats to ints */
61                 if(!mode_is_int(mode))
62                         return 0;
63                 return 1;
64         }
65         return is_Add(node) || is_Sub(node) || is_Mul(node) || is_Phi(node);
66 }
67
68 static
69 int get_conv_costs(const ir_node *node, ir_mode *dest_mode)
70 {
71         ir_mode *mode = get_irn_mode(node);
72
73         if(mode == dest_mode)
74                 return 0;
75
76         /* TODO... */
77         if(!is_Const(node) && get_irn_n_edges(node) > 1) {
78                 DB((dbg, LEVEL_3, "multi outs at %+F\n", node));
79                 return 1;
80         }
81
82         if(is_Conv(node)) {
83                 return get_conv_costs(get_Conv_op(node), dest_mode) - 1;
84         }
85
86         if(is_optimizable_node(node)) {
87                 int i;
88                 int arity = get_irn_arity(node);
89                 int costs = 0;
90
91                 for(i = 0; i < arity; ++i) {
92                         ir_node *pred = get_irn_n(node, i);
93                         costs += get_conv_costs(pred, dest_mode);
94                 }
95
96                 return costs;
97         }
98
99         return 1;
100 }
101
102 static
103 ir_node *conv_transform(ir_node *node, ir_mode *dest_mode)
104 {
105         size_t arity;
106         size_t i;
107
108         if (get_irn_mode(node) == dest_mode)
109                 return node;
110
111         if (is_Const(node)) {
112                 tarval *tv = tarval_convert_to(get_Const_tarval(node), dest_mode);
113                 assert(get_tarval_mode(tv) == dest_mode);
114                 return new_Const(dest_mode, tv);
115         }
116
117         if (is_Conv(node)) {
118                 return conv_transform(get_Conv_op(node), dest_mode);
119         }
120
121         if (!is_optimizable_node(node) || get_irn_n_edges(node) > 1) {
122                 ir_node *block = get_nodes_block(node);
123                 ir_node *conv = new_r_Conv(current_ir_graph, block, node, dest_mode);
124                 return conv;
125         }
126
127         arity = get_irn_arity(node);
128         for (i = 0; i < arity; i++) {
129                 ir_node *pred = get_irn_n(node, i);
130                 ir_node *transformed = conv_transform(pred, dest_mode);
131                 set_irn_n(node, i, transformed);
132         }
133         set_irn_mode(node, dest_mode);
134         return node;
135 }
136
137 static
138 int is_downconv(ir_mode *src_mode, ir_mode *dest_mode)
139 {
140         if(!mode_is_int(src_mode) || !mode_is_int(dest_mode))
141                 return 0;
142         if(get_mode_size_bits(dest_mode) >= get_mode_size_bits(src_mode))
143                 return 0;
144
145         return 1;
146 }
147
148 /* TODO, backends can't handle and it's probably not more efficient on most
149    archs */
150 #if 0
151 static
152 void try_optimize_cmp(ir_node *node)
153 {
154         ir_node *left  = get_Cmp_left(node);
155         ir_node *right = get_Cmp_right(node);
156         ir_node *conv  = NULL;
157
158         if(is_downconv
159 }
160 #endif
161
162 static char changed;
163
164 static
165 void conv_opt_walker(ir_node *node, void *data)
166 {
167         ir_node *transformed;
168         ir_node *pred;
169         ir_mode *pred_mode;
170         ir_mode *mode;
171         int costs;
172
173 #if 0
174         if(is_Cmp(node)) {
175                 try_optimize_cmp(node);
176                 return;
177         }
178 #endif
179
180         if(!is_Conv(node))
181                 return;
182
183         pred      = get_Conv_op(node);
184         mode      = get_irn_mode(node);
185         pred_mode = get_irn_mode(pred);
186
187         if (!is_Phi(pred) && !is_downconv(pred_mode, mode))
188                 return;
189
190         costs = get_conv_costs(pred, mode);
191         DB((dbg, LEVEL_2, "Costs for %+F -> %+F: %d\n", node, pred, costs));
192         if (costs > 0) return;
193
194         transformed = conv_transform(pred, mode);
195         exchange(node, transformed);
196         changed = 1;
197 }
198
199 void conv_opt(ir_graph *irg)
200 {
201         FIRM_DBG_REGISTER(dbg, "firm.opt.conv");
202
203         DB((dbg, LEVEL_1, "===> Performing conversion optimization on %+F\n", irg));
204
205         edges_assure(irg);
206         do {
207                 changed = 0;
208                 irg_walk_graph(irg, NULL, conv_opt_walker, NULL);
209                 local_optimize_graph(irg);
210         } while (changed);
211 }