Added assertion to assure that we don't run into an endless loop.
[libfirm] / ir / arch / archop.c
1 /*
2  * Copyright (C) 1995-2008 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     architecture dependent IR operations
23  * @version   $Id$
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32
33 #include "irprog_t.h"
34 #include "irgraph_t.h"
35 #include "irnode_t.h"
36 #include "irmode_t.h"
37 #include "ircons_t.h"
38 #include "iropt_t.h"
39 #include "firm_common_t.h"
40 #include "irvrfy_t.h"
41 #include "iropt_dbg.h"
42 #include "archop.h"
43 #include "irop.h"
44
45 /* when we need verifying */
46 #ifdef NDEBUG
47 # define IRN_VRFY_IRG(res, irg)
48 #else
49 # define IRN_VRFY_IRG(res, irg)  irn_vrfy_irg(res, irg)
50 #endif
51
52 /** current settings */
53 static arch_ops_info settings;
54
55 /** default settings */
56 static const arch_ops_info default_settings = {
57         ARCH_OPS_NONE,
58         0
59 };
60
61 /*
62  * construct a Min: Min(a,b) = a < b ? a : b
63  */
64 ir_node *
65 new_rd_Min(dbg_info *db, ir_graph *irg, ir_node *block,
66            ir_node *op1, ir_node *op2, ir_mode *mode)
67 {
68         ir_node *in[2];
69         ir_node *res;
70
71         if (! op_Min) {
72                 assert(0);
73                 return NULL;
74         }
75
76         in[0] = op1;
77         in[1] = op2;
78         res = new_ir_node(db, irg, block, op_Min, mode, 2, in);
79         res = optimize_node(res);
80         IRN_VRFY_IRG(res, irg);
81         return res;
82 }
83
84 /*
85  * construct a Max: Max(a,b) = a > b ? a : b
86  */
87 ir_node *
88 new_rd_Max(dbg_info *db, ir_graph *irg, ir_node *block,
89            ir_node *op1, ir_node *op2, ir_mode *mode)
90 {
91         ir_node *in[2];
92         ir_node *res;
93
94         if (! op_Max) {
95                 assert(0);
96                 return NULL;
97         }
98
99         in[0] = op1;
100         in[1] = op2;
101         res = new_ir_node(db, irg, block, op_Max, mode, 2, in);
102         res = optimize_node(res);
103         IRN_VRFY_IRG(res, irg);
104         return res;
105 }
106
107 ir_node *
108 new_r_Min(ir_graph *irg, ir_node *block,
109           ir_node *op1, ir_node *op2, ir_mode *mode) {
110         return new_rd_Min(NULL, irg, block, op1, op2, mode);
111 }
112
113 ir_node *
114 new_r_Max(ir_graph *irg, ir_node *block,
115        ir_node *op1, ir_node *op2, ir_mode *mode) {
116   return new_rd_Max(NULL, irg, block, op1, op2, mode);
117 }
118
119 ir_node *
120 new_d_Min(dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
121         return new_rd_Min(db, current_ir_graph, current_ir_graph->current_block,
122                           op1, op2, mode);
123 }
124
125 ir_node *
126 new_d_Max(dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
127         return new_rd_Max(db, current_ir_graph, current_ir_graph->current_block,
128                           op1, op2, mode);
129 }
130
131 ir_node *
132 new_Min(ir_node *op1, ir_node *op2, ir_mode *mode) {
133         return new_d_Min(NULL, op1, op2, mode);
134 }
135
136 ir_node *
137 new_Max(ir_node *op1, ir_node *op2, ir_mode *mode) {
138   return new_d_Max(NULL, op1, op2, mode);
139 }
140
141 /* optimizations */
142
143 /**
144  * return the value of a Min
145  */
146 static tarval *computed_value_Min(const ir_node *n) {
147         ir_node *a = get_binop_left(n);
148         ir_node *b = get_binop_right(n);
149
150         tarval *ta = value_of(a);
151         tarval *tb = value_of(b);
152
153         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
154                 pn_Cmp res = tarval_cmp(ta, tb);
155
156                 /* beware: there might be Unordered tarvals here, in that
157                 * case let the backend decide, do NOT optimize */
158                 if (res == pn_Cmp_Lt)
159                         return ta;
160                 if (res == pn_Cmp_Gt || res == pn_Cmp_Eq)
161                         return tb;
162         }
163         return tarval_bad;
164 }
165
166 /**
167  * return the value of a Max
168  */
169 static tarval *computed_value_Max(const ir_node *n) {
170         ir_node *a      = get_binop_left(n);
171         ir_node *b      = get_binop_right(n);
172
173         tarval *tb = value_of(b);
174         tarval *ta = value_of(a);
175
176         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
177                 pn_Cmp res = tarval_cmp(ta, tb);
178
179                 /* beware: there might be Unordered tarvals here, in that
180                  * case let the backend decide, do NOT optimize */
181                 if (res == pn_Cmp_Gt)
182                         return ta;
183                 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
184                         return tb;
185         }
186         return tarval_bad;
187 }
188
189 /**
190  * Returns an equivalent node for a Min/Max node.
191  * We do not allow Exceptions in our Min/Max, so there will be always
192  * an result.
193  * The problem is Min(NaN, NaN) == NaN ???.
194  */
195 static ir_node *equivalent_node_MinMax(ir_node *n)
196 {
197         ir_node *a, *b;
198         ir_mode *mode;
199         tarval *tv;
200
201         if (settings.minmax_handle_NaN == 0 && mode_is_float(get_irn_mode(n)))
202                 return n;
203
204         a = get_binop_left(n);
205         b = get_binop_right(n);
206
207         if (a == b) {
208                 DBG_OPT_ALGSIM0(n, a, FS_OPT_MIN_MAX_EQ);
209                 return a;
210         }
211
212         mode = get_irn_mode(n);
213         tv   = n->op == op_Max ? get_mode_min(mode) : get_mode_max(mode);
214
215         if (value_of(b) == tv) {
216                 DBG_OPT_ALGSIM0(n, a, FS_OPT_MIN_MAX_EQ);
217                 return a;
218         }
219         if (value_of(a) == tv) {
220                 DBG_OPT_ALGSIM0(n, b, FS_OPT_MIN_MAX_EQ);
221                 return b;
222         }
223
224         return n;
225 }
226
227 #define equivalent_node_Min equivalent_node_MinMax
228 #define equivalent_node_Max equivalent_node_MinMax
229
230 /*
231  * Create Min and Max from Mux nodes
232  */
233 ir_node *arch_transform_node_Mux(ir_node *n)
234 {
235         if (settings.enabled_ops & ARCH_OPS_MINMAX) {
236                 ir_node *oldn = n, *cmp, *proj = get_Mux_sel(n);
237                 long proj_nr;
238
239                 if (get_irn_op(proj) != op_Proj)
240                         return n;
241
242                 cmp = get_Proj_pred(proj);
243                 if (is_Cmp(cmp)) {
244                         ir_node *a = get_Cmp_left(cmp);
245                         ir_node *b = get_Cmp_right(cmp);
246                         ir_node *t = get_Mux_true(n);
247                         ir_node *f = get_Mux_false(n);
248
249                         proj_nr = get_Proj_proj(proj);
250
251                         if (proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) {
252                                 if (a == t && b == f) {
253                                         /* a </<= b ? a : b  ==>  Min(a,b) */
254                                         n = new_rd_Min(get_irn_dbg_info(n),
255                                                 current_ir_graph,
256                                                 get_nodes_block(n),
257                                                 a, b,
258                                                 get_irn_mode(n));
259
260                                         DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MIN);
261                                         return n;
262                                 }
263                                 else if (a == f && b == t) {
264                                         /* a </<= b ? b : a  ==>  Max(a,b) */
265                                         n = new_rd_Max(get_irn_dbg_info(n),
266                                                 current_ir_graph,
267                                                 get_nodes_block(n),
268                                                 a, b,
269                                                 get_irn_mode(n));
270
271                                         DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MAX);
272                                         return n;
273                                 }
274                         }
275                         else if (proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) {
276                                 if (a == t && b == f) {
277                                         /* a >/>= b ? a : b  ==>  Max(a,b) */
278                                         n = new_rd_Max(get_irn_dbg_info(n),
279                                                 current_ir_graph,
280                                                 get_nodes_block(n),
281                                                 a, b,
282                                                 get_irn_mode(n));
283
284                                         DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MAX);
285                                         return n;
286                                 }
287                                 else if (a == f && b == t) {
288                                         /* a >/>= b ? b : a  ==>  Min(a,b) */
289                                         n = new_rd_Min(get_irn_dbg_info(n),
290                                                 current_ir_graph,
291                                                 get_nodes_block(n),
292                                                 a, b,
293                                                 get_irn_mode(n));
294
295                                         DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MIN);
296                                         return n;
297                                 }
298                         }
299                 }
300         }
301         return n;
302 }
303
304 /**
305  * verify a MinMax node
306  */
307 static int verify_node_MinMax(ir_node *n, ir_graph *irg) {
308         ir_mode *mymode  = get_irn_mode(n);
309         ir_mode *op1mode = get_irn_mode(get_binop_left(n));
310         ir_mode *op2mode = get_irn_mode(get_binop_right(n));
311         (void) irg;
312
313         ASSERT_AND_RET(
314                 /* MinMax: BB x numP x numP --> numP */
315                 op1mode == mymode &&
316                 op2mode == mymode &&
317                 mode_is_data(mymode),
318                 "Min or Max node", 0
319                 );
320         return 1;
321 }
322
323 /*
324  * initialize the ops.
325  */
326 void firm_archops_init(const arch_ops_info *info)
327 {
328         ir_op_ops ops;
329
330         if (! info)
331                 info = &default_settings;
332
333         memcpy(&settings, info, sizeof(settings));
334
335         if (info->enabled_ops & ARCH_OPS_MINMAX) {
336                 memset(&ops, 0, sizeof(ops));
337
338                 op_Min->ops.computed_value  = computed_value_Min;
339                 op_Min->ops.equivalent_node = equivalent_node_Min;
340                 op_Min->ops.verify_node     = verify_node_MinMax;
341
342                 op_Max->ops.computed_value  = computed_value_Max;
343                 op_Max->ops.equivalent_node = equivalent_node_Max;
344                 op_Max->ops.verify_node     = verify_node_MinMax;
345         }
346 }