2 * Copyright (C) 1995-2008 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 architecture dependent IR operations
34 #include "irgraph_t.h"
39 #include "firm_common_t.h"
41 #include "iropt_dbg.h"
45 /* when we need verifying */
47 # define IRN_VRFY_IRG(res, irg)
49 # define IRN_VRFY_IRG(res, irg) irn_vrfy_irg(res, irg)
52 /** current settings */
53 static arch_ops_info settings;
55 /** default settings */
56 static const arch_ops_info default_settings = {
62 * construct a Min: Min(a,b) = a < b ? a : b
65 new_rd_Min(dbg_info *db, ir_graph *irg, ir_node *block,
66 ir_node *op1, ir_node *op2, ir_mode *mode)
78 res = new_ir_node(db, irg, block, op_Min, mode, 2, in);
79 res = optimize_node(res);
80 IRN_VRFY_IRG(res, irg);
85 * construct a Max: Max(a,b) = a > b ? a : b
88 new_rd_Max(dbg_info *db, ir_graph *irg, ir_node *block,
89 ir_node *op1, ir_node *op2, ir_mode *mode)
101 res = new_ir_node(db, irg, block, op_Max, mode, 2, in);
102 res = optimize_node(res);
103 IRN_VRFY_IRG(res, irg);
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);
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);
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,
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,
132 new_Min(ir_node *op1, ir_node *op2, ir_mode *mode) {
133 return new_d_Min(NULL, op1, op2, mode);
137 new_Max(ir_node *op1, ir_node *op2, ir_mode *mode) {
138 return new_d_Max(NULL, op1, op2, mode);
144 * return the value of a Min
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);
150 tarval *ta = value_of(a);
151 tarval *tb = value_of(b);
153 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
154 pn_Cmp res = tarval_cmp(ta, tb);
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)
160 if (res == pn_Cmp_Gt || res == pn_Cmp_Eq)
167 * return the value of a Max
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);
173 tarval *tb = value_of(b);
174 tarval *ta = value_of(a);
176 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
177 pn_Cmp res = tarval_cmp(ta, tb);
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)
183 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
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
193 * The problem is Min(NaN, NaN) == NaN ???.
195 static ir_node *equivalent_node_MinMax(ir_node *n)
201 if (settings.minmax_handle_NaN == 0 && mode_is_float(get_irn_mode(n)))
204 a = get_binop_left(n);
205 b = get_binop_right(n);
208 DBG_OPT_ALGSIM0(n, a, FS_OPT_MIN_MAX_EQ);
212 mode = get_irn_mode(n);
213 tv = n->op == op_Max ? get_mode_min(mode) : get_mode_max(mode);
215 if (value_of(b) == tv) {
216 DBG_OPT_ALGSIM0(n, a, FS_OPT_MIN_MAX_EQ);
219 if (value_of(a) == tv) {
220 DBG_OPT_ALGSIM0(n, b, FS_OPT_MIN_MAX_EQ);
227 #define equivalent_node_Min equivalent_node_MinMax
228 #define equivalent_node_Max equivalent_node_MinMax
231 * Create Min and Max from Mux nodes
233 ir_node *arch_transform_node_Mux(ir_node *n)
235 if (settings.enabled_ops & ARCH_OPS_MINMAX) {
236 ir_node *oldn = n, *cmp, *proj = get_Mux_sel(n);
239 if (get_irn_op(proj) != op_Proj)
242 cmp = get_Proj_pred(proj);
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);
249 proj_nr = get_Proj_proj(proj);
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),
260 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MIN);
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),
271 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MAX);
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),
284 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MAX);
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),
295 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MIN);
305 * verify a MinMax node
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));
314 /* MinMax: BB x numP x numP --> numP */
317 mode_is_data(mymode),
324 * initialize the ops.
326 void firm_archops_init(const arch_ops_info *info)
331 info = &default_settings;
333 memcpy(&settings, info, sizeof(settings));
335 if (info->enabled_ops & ARCH_OPS_MINMAX) {
336 memset(&ops, 0, sizeof(ops));
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;
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;