2 * Copyright (C) 1995-2007 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 dependand IR operations
34 #include "irgraph_t.h"
39 #include "firm_common_t.h"
41 #include "iropt_dbg.h"
44 /* when we need verifying */
46 # define IRN_VRFY_IRG(res, irg)
48 # define IRN_VRFY_IRG(res, irg) irn_vrfy_irg(res, irg)
51 /** current settings */
52 static arch_ops_info settings;
54 /** default settings */
55 static const arch_ops_info default_settings = {
60 /** The Min operation */
63 /** The Max operation */
66 ir_op *get_op_Min(void) { return op_Min; }
67 ir_op *get_op_Max(void) { return op_Max; }
70 * construct a Min: Min(a,b) = a < b ? a : b
73 new_rd_Min(dbg_info *db, ir_graph *irg, ir_node *block,
74 ir_node *op1, ir_node *op2, ir_mode *mode)
86 res = new_ir_node(db, irg, block, op_Min, mode, 2, in);
87 res = optimize_node(res);
88 IRN_VRFY_IRG(res, irg);
93 * construct a Max: Max(a,b) = a > b ? a : b
96 new_rd_Max(dbg_info *db, ir_graph *irg, ir_node *block,
97 ir_node *op1, ir_node *op2, ir_mode *mode)
109 res = new_ir_node(db, irg, block, op_Max, mode, 2, in);
110 res = optimize_node(res);
111 IRN_VRFY_IRG(res, irg);
116 new_r_Min(ir_graph *irg, ir_node *block,
117 ir_node *op1, ir_node *op2, ir_mode *mode) {
118 return new_rd_Min(NULL, irg, block, op1, op2, mode);
122 new_r_Max(ir_graph *irg, ir_node *block,
123 ir_node *op1, ir_node *op2, ir_mode *mode) {
124 return new_rd_Max(NULL, irg, block, op1, op2, mode);
128 new_d_Min(dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
129 return new_rd_Min(db, current_ir_graph, current_ir_graph->current_block,
134 new_d_Max(dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
135 return new_rd_Max(db, current_ir_graph, current_ir_graph->current_block,
140 new_Min(ir_node *op1, ir_node *op2, ir_mode *mode) {
141 return new_d_Min(NULL, op1, op2, mode);
145 new_Max(ir_node *op1, ir_node *op2, ir_mode *mode) {
146 return new_d_Max(NULL, op1, op2, mode);
152 * return the value of a Min
154 static tarval *computed_value_Min(ir_node *n)
156 ir_node *a = get_binop_left(n);
157 ir_node *b = get_binop_right(n);
159 tarval *ta = value_of(a);
160 tarval *tb = value_of(b);
162 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
163 pn_Cmp res = tarval_cmp(ta, tb);
165 /* beware: there might be Unordered tarvals here, in that
166 * case let the backend decide, do NOT optimize */
167 if (res == pn_Cmp_Lt)
169 if (res == pn_Cmp_Gt || res == pn_Cmp_Eq)
176 * return the value of a Max
178 static tarval *computed_value_Max(ir_node *n)
180 ir_node *a = get_binop_left(n);
181 ir_node *b = get_binop_right(n);
183 tarval *ta = value_of(a);
184 tarval *tb = value_of(b);
186 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
187 pn_Cmp res = tarval_cmp(ta, tb);
189 /* beware: there might be Unordered tarvals here, in that
190 * case let the backend decide, do NOT optimize */
191 if (res == pn_Cmp_Gt)
193 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
200 * Returns an equivalent node for a Min/Max node.
201 * We do not allow Exceptions in our Min/Max, so there will be always
203 * The problem is Min(NaN, NaN) == NaN ???.
205 static ir_node *equivalent_node_MinMax(ir_node *n)
209 if (settings.minmax_handle_NaN == 0 && mode_is_float(get_irn_mode(n)))
212 a = get_binop_left(n);
213 b = get_binop_right(n);
216 DBG_OPT_ALGSIM0(n, a, FS_OPT_MIN_MAX_EQ);
223 #define equivalent_node_Min equivalent_node_MinMax
224 #define equivalent_node_Max equivalent_node_MinMax
227 * Create Min and Max from Mux nodes
229 ir_node *arch_transform_node_Mux(ir_node *n)
231 if (settings.enabled_ops & ARCH_OPS_MINMAX) {
232 ir_node *oldn = n, *cmp, *proj = get_Mux_sel(n);
235 if (get_irn_op(proj) != op_Proj)
238 cmp = get_Proj_pred(proj);
239 if (get_irn_op(cmp) == op_Cmp) {
240 ir_node *a = get_Cmp_left(cmp);
241 ir_node *b = get_Cmp_right(cmp);
242 ir_node *t = get_Mux_true(n);
243 ir_node *f = get_Mux_false(n);
245 proj_nr = get_Proj_proj(proj);
247 if (proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) {
248 if (a == t && b == f) {
249 /* a </<= b ? a : b ==> Min(a,b) */
250 n = new_rd_Min(get_irn_dbg_info(n),
256 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MIN);
259 else if (a == f && b == t) {
260 /* a </<= b ? b : a ==> Max(a,b) */
261 n = new_rd_Max(get_irn_dbg_info(n),
267 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MAX);
271 else if (proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) {
272 if (a == t && b == f) {
273 /* a >/>= b ? a : b ==> Max(a,b) */
274 n = new_rd_Max(get_irn_dbg_info(n),
280 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MAX);
283 else if (a == f && b == t) {
284 /* a >/>= b ? b : a ==> Min(a,b) */
285 n = new_rd_Min(get_irn_dbg_info(n),
291 DBG_OPT_ALGSIM1(oldn, cmp, proj, n, FS_OPT_MUX_TO_MIN);
301 * verify a MinMax node
303 static int verify_node_MinMax(ir_node *n, ir_graph *irg) {
304 ir_mode *mymode = get_irn_mode(n);
305 ir_mode *op1mode = get_irn_mode(get_binop_left(n));
306 ir_mode *op2mode = get_irn_mode(get_binop_right(n));
309 /* MinMax: BB x numP x numP --> numP */
312 mode_is_numP(mymode),
319 * initialize the ops.
321 void firm_archops_init(const arch_ops_info *info)
326 info = &default_settings;
328 memcpy(&settings, info, sizeof(settings));
330 if (info->enabled_ops & ARCH_OPS_MINMAX) {
331 memset(&ops, 0, sizeof(ops));
333 ops.computed_value = computed_value_Min;
334 ops.equivalent_node = equivalent_node_Min;
335 ops.verify_node = verify_node_MinMax;
337 op_Min = new_ir_op(get_next_ir_opcode(), "Min", op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0, &ops);
339 ops.computed_value = computed_value_Max;
340 ops.equivalent_node = equivalent_node_Max;
341 ops.verify_node = verify_node_MinMax;
343 op_Max = new_ir_op(get_next_ir_opcode(), "Max", op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0, &ops);