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 iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
39 #include "dbginfo_t.h"
40 #include "iropt_dbg.h"
45 #include "opt_confirms.h"
46 #include "opt_polymorphy.h"
51 #include "firm_types.h"
53 /* Make types visible to allow most efficient access */
57 * Returns the tarval of a Const node or tarval_bad for all other nodes.
59 static tarval *default_value_of(const ir_node *n) {
61 return get_Const_tarval(n); /* might return tarval_bad */
66 value_of_func value_of_ptr = default_value_of;
68 /* * Set a new value_of function. */
69 void set_value_of_func(value_of_func func) {
73 value_of_ptr = default_value_of;
77 * Return the value of a Constant.
79 static tarval *computed_value_Const(const ir_node *n) {
80 return get_Const_tarval(n);
81 } /* computed_value_Const */
84 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
86 static tarval *computed_value_SymConst(const ir_node *n) {
90 switch (get_SymConst_kind(n)) {
91 case symconst_type_size:
92 type = get_SymConst_type(n);
93 if (get_type_state(type) == layout_fixed)
94 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
96 case symconst_type_align:
97 type = get_SymConst_type(n);
98 if (get_type_state(type) == layout_fixed)
99 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
101 case symconst_ofs_ent:
102 ent = get_SymConst_entity(n);
103 type = get_entity_owner(ent);
104 if (get_type_state(type) == layout_fixed)
105 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
111 } /* computed_value_SymConst */
114 * Return the value of an Add.
116 static tarval *computed_value_Add(const ir_node *n) {
117 ir_node *a = get_Add_left(n);
118 ir_node *b = get_Add_right(n);
120 tarval *ta = value_of(a);
121 tarval *tb = value_of(b);
123 if ((ta != tarval_bad) && (tb != tarval_bad))
124 return tarval_add(ta, tb);
127 } /* computed_value_Add */
130 * Return the value of a Sub.
131 * Special case: a - a
133 static tarval *computed_value_Sub(const ir_node *n) {
134 ir_mode *mode = get_irn_mode(n);
135 ir_node *a = get_Sub_left(n);
136 ir_node *b = get_Sub_right(n);
141 if (! mode_is_float(mode)) {
144 return get_mode_null(mode);
150 if ((ta != tarval_bad) && (tb != tarval_bad))
151 return tarval_sub(ta, tb, mode);
154 } /* computed_value_Sub */
157 * Return the value of a Carry.
158 * Special : a op 0, 0 op b
160 static tarval *computed_value_Carry(const ir_node *n) {
161 ir_node *a = get_binop_left(n);
162 ir_node *b = get_binop_right(n);
163 ir_mode *m = get_irn_mode(n);
165 tarval *ta = value_of(a);
166 tarval *tb = value_of(b);
168 if ((ta != tarval_bad) && (tb != tarval_bad)) {
170 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
172 if (tarval_is_null(ta) || tarval_is_null(tb))
173 return get_mode_null(m);
176 } /* computed_value_Carry */
179 * Return the value of a Borrow.
182 static tarval *computed_value_Borrow(const ir_node *n) {
183 ir_node *a = get_binop_left(n);
184 ir_node *b = get_binop_right(n);
185 ir_mode *m = get_irn_mode(n);
187 tarval *ta = value_of(a);
188 tarval *tb = value_of(b);
190 if ((ta != tarval_bad) && (tb != tarval_bad)) {
191 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
192 } else if (tarval_is_null(ta)) {
193 return get_mode_null(m);
196 } /* computed_value_Borrow */
199 * Return the value of an unary Minus.
201 static tarval *computed_value_Minus(const ir_node *n) {
202 ir_node *a = get_Minus_op(n);
203 tarval *ta = value_of(a);
205 if (ta != tarval_bad)
206 return tarval_neg(ta);
209 } /* computed_value_Minus */
212 * Return the value of a Mul.
214 static tarval *computed_value_Mul(const ir_node *n) {
215 ir_node *a = get_Mul_left(n);
216 ir_node *b = get_Mul_right(n);
219 tarval *ta = value_of(a);
220 tarval *tb = value_of(b);
222 mode = get_irn_mode(n);
223 if (mode != get_irn_mode(a)) {
224 /* n * n = 2n bit multiplication */
225 ta = tarval_convert_to(ta, mode);
226 tb = tarval_convert_to(tb, mode);
229 if (ta != tarval_bad && tb != tarval_bad) {
230 return tarval_mul(ta, tb);
232 /* a * 0 != 0 if a == NaN or a == Inf */
233 if (!mode_is_float(mode)) {
234 /* a*0 = 0 or 0*b = 0 */
235 if (ta == get_mode_null(mode))
237 if (tb == get_mode_null(mode))
242 } /* computed_value_Mul */
245 * Return the value of an Abs.
247 static tarval *computed_value_Abs(const ir_node *n) {
248 ir_node *a = get_Abs_op(n);
249 tarval *ta = value_of(a);
251 if (ta != tarval_bad)
252 return tarval_abs(ta);
255 } /* computed_value_Abs */
258 * Return the value of an And.
259 * Special case: a & 0, 0 & b
261 static tarval *computed_value_And(const ir_node *n) {
262 ir_node *a = get_And_left(n);
263 ir_node *b = get_And_right(n);
265 tarval *ta = value_of(a);
266 tarval *tb = value_of(b);
268 if ((ta != tarval_bad) && (tb != tarval_bad)) {
269 return tarval_and (ta, tb);
271 if (tarval_is_null(ta)) return ta;
272 if (tarval_is_null(tb)) return tb;
275 } /* computed_value_And */
278 * Return the value of an Or.
279 * Special case: a | 1...1, 1...1 | b
281 static tarval *computed_value_Or(const ir_node *n) {
282 ir_node *a = get_Or_left(n);
283 ir_node *b = get_Or_right(n);
285 tarval *ta = value_of(a);
286 tarval *tb = value_of(b);
288 if ((ta != tarval_bad) && (tb != tarval_bad)) {
289 return tarval_or (ta, tb);
291 if (tarval_is_all_one(ta)) return ta;
292 if (tarval_is_all_one(tb)) return tb;
295 } /* computed_value_Or */
298 * Return the value of an Eor.
300 static tarval *computed_value_Eor(const ir_node *n) {
301 ir_node *a = get_Eor_left(n);
302 ir_node *b = get_Eor_right(n);
307 return get_mode_null(get_irn_mode(n));
312 if ((ta != tarval_bad) && (tb != tarval_bad)) {
313 return tarval_eor(ta, tb);
316 } /* computed_value_Eor */
319 * Return the value of a Not.
321 static tarval *computed_value_Not(const ir_node *n) {
322 ir_node *a = get_Not_op(n);
323 tarval *ta = value_of(a);
325 if (ta != tarval_bad)
326 return tarval_not(ta);
329 } /* computed_value_Not */
332 * Return the value of a Shl.
334 static tarval *computed_value_Shl(const ir_node *n) {
335 ir_node *a = get_Shl_left(n);
336 ir_node *b = get_Shl_right(n);
338 tarval *ta = value_of(a);
339 tarval *tb = value_of(b);
341 if ((ta != tarval_bad) && (tb != tarval_bad)) {
342 return tarval_shl(ta, tb);
345 } /* computed_value_Shl */
348 * Return the value of a Shr.
350 static tarval *computed_value_Shr(const ir_node *n) {
351 ir_node *a = get_Shr_left(n);
352 ir_node *b = get_Shr_right(n);
354 tarval *ta = value_of(a);
355 tarval *tb = value_of(b);
357 if ((ta != tarval_bad) && (tb != tarval_bad)) {
358 return tarval_shr(ta, tb);
361 } /* computed_value_Shr */
364 * Return the value of a Shrs.
366 static tarval *computed_value_Shrs(const ir_node *n) {
367 ir_node *a = get_Shrs_left(n);
368 ir_node *b = get_Shrs_right(n);
370 tarval *ta = value_of(a);
371 tarval *tb = value_of(b);
373 if ((ta != tarval_bad) && (tb != tarval_bad)) {
374 return tarval_shrs(ta, tb);
377 } /* computed_value_Shrs */
380 * Return the value of a Rotl.
382 static tarval *computed_value_Rotl(const ir_node *n) {
383 ir_node *a = get_Rotl_left(n);
384 ir_node *b = get_Rotl_right(n);
386 tarval *ta = value_of(a);
387 tarval *tb = value_of(b);
389 if ((ta != tarval_bad) && (tb != tarval_bad)) {
390 return tarval_rotl(ta, tb);
393 } /* computed_value_Rotl */
396 * Return the value of a Conv.
398 static tarval *computed_value_Conv(const ir_node *n) {
399 ir_node *a = get_Conv_op(n);
400 tarval *ta = value_of(a);
402 if (ta != tarval_bad)
403 return tarval_convert_to(ta, get_irn_mode(n));
406 } /* computed_value_Conv */
409 * Calculate the value of a Mux: can be evaluated, if the
410 * sel and the right input are known.
412 static tarval *computed_value_Mux(const ir_node *n) {
413 ir_node *sel = get_Mux_sel(n);
414 tarval *ts = value_of(sel);
416 if (ts == get_tarval_b_true()) {
417 ir_node *v = get_Mux_true(n);
420 else if (ts == get_tarval_b_false()) {
421 ir_node *v = get_Mux_false(n);
425 } /* computed_value_Mux */
428 * Calculate the value of a Confirm: can be evaluated,
429 * if it has the form Confirm(x, '=', Const).
431 static tarval *computed_value_Confirm(const ir_node *n) {
433 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
434 * Do NOT optimize them away (jump threading wants them), so wait until
435 * remove_confirm is activated.
437 if (get_opt_remove_confirm()) {
438 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
439 tarval *tv = value_of(get_Confirm_bound(n));
440 if (tv != tarval_bad)
444 return value_of(get_Confirm_value(n));
445 } /* computed_value_Confirm */
448 * Return the value of a Proj(Cmp).
450 * This performs a first step of unreachable code elimination.
451 * Proj can not be computed, but folding a Cmp above the Proj here is
452 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
454 * There are several case where we can evaluate a Cmp node, see later.
456 static tarval *computed_value_Proj_Cmp(const ir_node *n) {
457 ir_node *cmp = get_Proj_pred(n);
458 ir_node *left = get_Cmp_left(cmp);
459 ir_node *right = get_Cmp_right(cmp);
460 long pn_cmp = get_Proj_proj(n);
461 ir_mode *mode = get_irn_mode(left);
465 * BEWARE: a == a is NOT always True for floating Point values, as
466 * NaN != NaN is defined, so we must check this here.
468 if (left == right && (!mode_is_float(mode) || pn_cmp == pn_Cmp_Lt || pn_cmp == pn_Cmp_Gt)) {
469 /* This is a trick with the bits used for encoding the Cmp
470 Proj numbers, the following statement is not the same:
471 return new_tarval_from_long(pn_cmp == pn_Cmp_Eq, mode_b) */
472 return new_tarval_from_long(pn_cmp & pn_Cmp_Eq, mode_b);
474 tv_l = value_of(left);
475 tv_r = value_of(right);
477 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
479 * The predecessors of Cmp are target values. We can evaluate
482 pn_Cmp flags = tarval_cmp(tv_l, tv_r);
483 if (flags != pn_Cmp_False) {
484 return new_tarval_from_long (pn_cmp & flags, mode_b);
486 } else if (mode_is_int(mode)) {
487 /* for integer values, we can check against MIN/MAX */
490 if (tv_l == get_mode_min(mode)) {
491 /* MIN <=/> x. This results in true/false. */
492 if (pn_cmp == pn_Cmp_Le)
493 return tarval_b_true;
494 else if (pn_cmp == pn_Cmp_Gt)
495 return tarval_b_false;
496 } else if (tv_r == get_mode_min(mode)) {
497 /* x >=/< MIN. This results in true/false. */
498 if (pn_cmp == pn_Cmp_Ge)
499 return tarval_b_true;
500 else if (pn_cmp == pn_Cmp_Lt)
501 return tarval_b_false;
502 } else if (tv_l == get_mode_max(mode)) {
503 /* MAX >=/< x. This results in true/false. */
504 if (pn_cmp == pn_Cmp_Ge)
505 return tarval_b_true;
506 else if (pn_cmp == pn_Cmp_Lt)
507 return tarval_b_false;
508 } else if (tv_r == get_mode_max(mode)) {
509 /* x <=/> MAX. This results in true/false. */
510 if (pn_cmp == pn_Cmp_Le)
511 return tarval_b_true;
512 else if (pn_cmp == pn_Cmp_Gt)
513 return tarval_b_false;
516 cmp_result = vrp_cmp(left, right);
517 if (cmp_result != pn_Cmp_False) {
518 if (cmp_result == pn_Cmp_Lg) {
519 if (pn_cmp == pn_Cmp_Eq) {
520 return tarval_b_false;
521 } else if (pn_cmp == pn_Cmp_Lg) {
522 return tarval_b_true;
525 return new_tarval_from_long(cmp_result & pn_cmp, mode_b);
528 } else if (mode_is_reference(mode)) {
529 /* pointer compare */
530 ir_node *s_l = skip_Proj(left);
531 ir_node *s_r = skip_Proj(right);
533 if ((is_Alloc(s_l) && tarval_is_null(tv_r)) ||
534 (tarval_is_null(tv_l) && is_Alloc(s_r))) {
536 * The predecessors are Allocs and (void*)(0) constants. In Firm Allocs never
537 * return NULL, they raise an exception. Therefore we can predict
540 return new_tarval_from_long(pn_cmp & pn_Cmp_Lg, mode_b);
543 return computed_value_Cmp_Confirm(cmp, left, right, pn_cmp);
544 } /* computed_value_Proj_Cmp */
547 * Return the value of a floating point Quot.
549 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b) {
550 tarval *ta = value_of(a);
551 tarval *tb = value_of(b);
553 /* cannot optimize 0 / b = 0 because of NaN */
554 if (ta != tarval_bad && tb != tarval_bad)
555 return tarval_quo(ta, tb);
557 } /* do_computed_value_Quot */
560 * Calculate the value of an integer Div of two nodes.
561 * Special case: 0 / b
563 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b) {
564 tarval *ta = value_of(a);
566 const ir_node *dummy;
568 /* Compute c1 / c2 or 0 / a, a != 0 */
569 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
570 return ta; /* 0 / b == 0 */
572 if (ta != tarval_bad && tb != tarval_bad)
573 return tarval_div(ta, tb);
575 } /* do_computed_value_Div */
578 * Calculate the value of an integer Mod of two nodes.
579 * Special case: a % 1
581 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b) {
582 tarval *ta = value_of(a);
583 tarval *tb = value_of(b);
585 /* Compute a % 1 or c1 % c2 */
586 if (tarval_is_one(tb))
587 return get_mode_null(get_irn_mode(a));
588 if (ta != tarval_bad && tb != tarval_bad)
589 return tarval_mod(ta, tb);
591 } /* do_computed_value_Mod */
594 * Return the value of a Proj(DivMod).
596 static tarval *computed_value_Proj_DivMod(const ir_node *n) {
597 long proj_nr = get_Proj_proj(n);
599 /* compute either the Div or the Mod part */
600 if (proj_nr == pn_DivMod_res_div) {
601 const ir_node *a = get_Proj_pred(n);
602 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
603 } else if (proj_nr == pn_DivMod_res_mod) {
604 const ir_node *a = get_Proj_pred(n);
605 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
608 } /* computed_value_Proj_DivMod */
611 * Return the value of a Proj(Div).
613 static tarval *computed_value_Proj_Div(const ir_node *n) {
614 long proj_nr = get_Proj_proj(n);
616 if (proj_nr == pn_Div_res) {
617 const ir_node *a = get_Proj_pred(n);
618 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
621 } /* computed_value_Proj_Div */
624 * Return the value of a Proj(Mod).
626 static tarval *computed_value_Proj_Mod(const ir_node *n) {
627 long proj_nr = get_Proj_proj(n);
629 if (proj_nr == pn_Mod_res) {
630 const ir_node *a = get_Proj_pred(n);
631 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
634 } /* computed_value_Proj_Mod */
637 * Return the value of a Proj(Quot).
639 static tarval *computed_value_Proj_Quot(const ir_node *n) {
640 long proj_nr = get_Proj_proj(n);
642 if (proj_nr == pn_Quot_res) {
643 const ir_node *a = get_Proj_pred(n);
644 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
647 } /* computed_value_Proj_Quot */
650 * Return the value of a Proj.
652 static tarval *computed_value_Proj(const ir_node *proj) {
653 ir_node *n = get_Proj_pred(proj);
655 if (n->op->ops.computed_value_Proj != NULL)
656 return n->op->ops.computed_value_Proj(proj);
658 } /* computed_value_Proj */
661 * If the parameter n can be computed, return its value, else tarval_bad.
662 * Performs constant folding.
664 * @param n The node this should be evaluated
666 tarval *computed_value(const ir_node *n) {
667 if(mode_is_int(get_irn_mode(n)) && n->vrp.valid && tarval_is_all_one(
668 tarval_or(n->vrp.bits_set, n->vrp.bits_not_set))) {
669 return n->vrp.bits_set;
671 if (n->op->ops.computed_value)
672 return n->op->ops.computed_value(n);
674 } /* computed_value */
677 * Set the default computed_value evaluator in an ir_op_ops.
679 * @param code the opcode for the default operation
680 * @param ops the operations initialized
685 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
689 ops->computed_value = computed_value_##a; \
691 #define CASE_PROJ(a) \
693 ops->computed_value_Proj = computed_value_Proj_##a; \
730 } /* firm_set_default_computed_value */
733 * Returns a equivalent block for another block.
734 * If the block has only one predecessor, this is
735 * the equivalent one. If the only predecessor of a block is
736 * the block itself, this is a dead block.
738 * If both predecessors of a block are the branches of a binary
739 * Cond, the equivalent block is Cond's block.
741 * If all predecessors of a block are bad or lies in a dead
742 * block, the current block is dead as well.
744 * Note, that blocks are NEVER turned into Bad's, instead
745 * the dead_block flag is set. So, never test for is_Bad(block),
746 * always use is_dead_Block(block).
748 static ir_node *equivalent_node_Block(ir_node *n)
753 /* don't optimize dead or labeled blocks */
754 if (is_Block_dead(n) || has_Block_entity(n))
757 n_preds = get_Block_n_cfgpreds(n);
759 /* The Block constructor does not call optimize, but mature_immBlock()
760 calls the optimization. */
761 assert(get_Block_matured(n));
763 /* Straightening: a single entry Block following a single exit Block
764 can be merged, if it is not the Start block. */
765 /* !!! Beware, all Phi-nodes of n must have been optimized away.
766 This should be true, as the block is matured before optimize is called.
767 But what about Phi-cycles with the Phi0/Id that could not be resolved?
768 Remaining Phi nodes are just Ids. */
770 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
773 ir_node *predblock = get_nodes_block(pred);
774 if (predblock == oldn) {
775 /* Jmp jumps into the block it is in -- deal self cycle. */
776 n = set_Block_dead(n);
777 DBG_OPT_DEAD_BLOCK(oldn, n);
778 } else if (get_opt_control_flow_straightening()) {
780 DBG_OPT_STG(oldn, n);
782 } else if (is_Cond(pred)) {
783 ir_node *predblock = get_nodes_block(pred);
784 if (predblock == oldn) {
785 /* Jmp jumps into the block it is in -- deal self cycle. */
786 n = set_Block_dead(n);
787 DBG_OPT_DEAD_BLOCK(oldn, n);
790 } else if ((n_preds == 2) &&
791 (get_opt_control_flow_weak_simplification())) {
792 /* Test whether Cond jumps twice to this block
793 * The more general case which more than 2 predecessors is handles
794 * in optimize_cf(), we handle only this special case for speed here.
796 ir_node *a = get_Block_cfgpred(n, 0);
797 ir_node *b = get_Block_cfgpred(n, 1);
799 if (is_Proj(a) && is_Proj(b)) {
800 ir_node *cond = get_Proj_pred(a);
802 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
803 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
804 /* Also a single entry Block following a single exit Block. Phis have
805 twice the same operand and will be optimized away. */
806 n = get_nodes_block(cond);
807 DBG_OPT_IFSIM1(oldn, a, b, n);
810 } else if (get_opt_unreachable_code() &&
811 (n != get_irg_start_block(current_ir_graph)) &&
812 (n != get_irg_end_block(current_ir_graph)) ) {
815 /* If all inputs are dead, this block is dead too, except if it is
816 the start or end block. This is one step of unreachable code
818 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
819 ir_node *pred = get_Block_cfgpred(n, i);
822 if (is_Bad(pred)) continue;
823 pred_blk = get_nodes_block(skip_Proj(pred));
825 if (is_Block_dead(pred_blk)) continue;
828 /* really found a living input */
833 n = set_Block_dead(n);
834 DBG_OPT_DEAD_BLOCK(oldn, n);
839 } /* equivalent_node_Block */
842 * Returns a equivalent node for a Jmp, a Bad :-)
843 * Of course this only happens if the Block of the Jmp is dead.
845 static ir_node *equivalent_node_Jmp(ir_node *n) {
848 /* unreachable code elimination */
849 if (is_Block_dead(get_nodes_block(n))) {
850 n = get_irg_bad(current_ir_graph);
851 DBG_OPT_DEAD_BLOCK(oldn, n);
854 } /* equivalent_node_Jmp */
856 /** Raise is handled in the same way as Jmp. */
857 #define equivalent_node_Raise equivalent_node_Jmp
860 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
861 See transform_node_Proj_Cond(). */
864 * Optimize operations that are commutative and have neutral 0,
865 * so a op 0 = 0 op a = a.
867 static ir_node *equivalent_node_neutral_zero(ir_node *n) {
870 ir_node *a = get_binop_left(n);
871 ir_node *b = get_binop_right(n);
876 /* After running compute_node there is only one constant predecessor.
877 Find this predecessors value and remember the other node: */
878 if ((tv = value_of(a)) != tarval_bad) {
880 } else if ((tv = value_of(b)) != tarval_bad) {
885 /* If this predecessors constant value is zero, the operation is
886 * unnecessary. Remove it.
888 * Beware: If n is a Add, the mode of on and n might be different
889 * which happens in this rare construction: NULL + 3.
890 * Then, a Conv would be needed which we cannot include here.
892 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
895 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
899 } /* equivalent_node_neutral_zero */
902 * Eor is commutative and has neutral 0.
904 static ir_node *equivalent_node_Eor(ir_node *n) {
909 n = equivalent_node_neutral_zero(n);
910 if (n != oldn) return n;
913 b = get_Eor_right(n);
916 ir_node *aa = get_Eor_left(a);
917 ir_node *ab = get_Eor_right(a);
920 /* (a ^ b) ^ a -> b */
922 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
924 } else if (ab == b) {
925 /* (a ^ b) ^ b -> a */
927 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
932 ir_node *ba = get_Eor_left(b);
933 ir_node *bb = get_Eor_right(b);
936 /* a ^ (a ^ b) -> b */
938 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
940 } else if (bb == a) {
941 /* a ^ (b ^ a) -> b */
943 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
951 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
953 * The second one looks strange, but this construct
954 * is used heavily in the LCC sources :-).
956 * Beware: The Mode of an Add may be different than the mode of its
957 * predecessors, so we could not return a predecessors in all cases.
959 static ir_node *equivalent_node_Add(ir_node *n) {
961 ir_node *left, *right;
962 ir_mode *mode = get_irn_mode(n);
964 n = equivalent_node_neutral_zero(n);
968 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
969 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
972 left = get_Add_left(n);
973 right = get_Add_right(n);
976 if (get_Sub_right(left) == right) {
979 n = get_Sub_left(left);
980 if (mode == get_irn_mode(n)) {
981 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
987 if (get_Sub_right(right) == left) {
990 n = get_Sub_left(right);
991 if (mode == get_irn_mode(n)) {
992 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
998 } /* equivalent_node_Add */
1001 * optimize operations that are not commutative but have neutral 0 on left,
1004 static ir_node *equivalent_node_left_zero(ir_node *n) {
1007 ir_node *a = get_binop_left(n);
1008 ir_node *b = get_binop_right(n);
1009 tarval *tb = value_of(b);
1011 if (tarval_is_null(tb)) {
1014 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1017 } /* equivalent_node_left_zero */
1019 #define equivalent_node_Shl equivalent_node_left_zero
1020 #define equivalent_node_Shr equivalent_node_left_zero
1021 #define equivalent_node_Shrs equivalent_node_left_zero
1022 #define equivalent_node_Rotl equivalent_node_left_zero
1025 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1027 * The second one looks strange, but this construct
1028 * is used heavily in the LCC sources :-).
1030 * Beware: The Mode of a Sub may be different than the mode of its
1031 * predecessors, so we could not return a predecessors in all cases.
1033 static ir_node *equivalent_node_Sub(ir_node *n) {
1036 ir_mode *mode = get_irn_mode(n);
1039 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1040 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1043 b = get_Sub_right(n);
1046 /* Beware: modes might be different */
1047 if (tarval_is_null(tb)) {
1048 ir_node *a = get_Sub_left(n);
1049 if (mode == get_irn_mode(a)) {
1052 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1056 } /* equivalent_node_Sub */
1060 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1063 * -(-a) == a, but might overflow two times.
1064 * We handle it anyway here but the better way would be a
1065 * flag. This would be needed for Pascal for instance.
1067 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1069 ir_node *pred = get_unop_op(n);
1071 /* optimize symmetric unop */
1072 if (get_irn_op(pred) == get_irn_op(n)) {
1073 n = get_unop_op(pred);
1074 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1077 } /* equivalent_node_idempotent_unop */
1079 /** Optimize Not(Not(x)) == x. */
1080 #define equivalent_node_Not equivalent_node_idempotent_unop
1082 /** -(-x) == x ??? Is this possible or can --x raise an
1083 out of bounds exception if min =! max? */
1084 #define equivalent_node_Minus equivalent_node_idempotent_unop
1087 * Optimize a * 1 = 1 * a = a.
1089 static ir_node *equivalent_node_Mul(ir_node *n) {
1091 ir_node *a = get_Mul_left(n);
1093 /* we can handle here only the n * n = n bit cases */
1094 if (get_irn_mode(n) == get_irn_mode(a)) {
1095 ir_node *b = get_Mul_right(n);
1099 * Mul is commutative and has again an other neutral element.
1100 * Constants are place right, so check this case first.
1103 if (tarval_is_one(tv)) {
1105 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1108 if (tarval_is_one(tv)) {
1110 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1115 } /* equivalent_node_Mul */
1118 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1120 static ir_node *equivalent_node_Or(ir_node *n) {
1123 ir_node *a = get_Or_left(n);
1124 ir_node *b = get_Or_right(n);
1128 n = a; /* Or has it's own neutral element */
1129 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1132 /* constants are cormalized to right, check this site first */
1134 if (tarval_is_null(tv)) {
1136 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1140 if (tarval_is_null(tv)) {
1142 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1147 } /* equivalent_node_Or */
1150 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1152 static ir_node *equivalent_node_And(ir_node *n) {
1155 ir_node *a = get_And_left(n);
1156 ir_node *b = get_And_right(n);
1160 n = a; /* And has it's own neutral element */
1161 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1164 /* constants are normalized to right, check this site first */
1166 if (tarval_is_all_one(tv)) {
1168 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1171 if (tv != get_tarval_bad()) {
1172 ir_mode *mode = get_irn_mode(n);
1173 if (!mode_is_signed(mode) && is_Conv(a)) {
1174 ir_node *convop = get_Conv_op(a);
1175 ir_mode *convopmode = get_irn_mode(convop);
1176 if (!mode_is_signed(convopmode)) {
1177 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1178 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1180 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1187 if (tarval_is_all_one(tv)) {
1189 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1193 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1196 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1201 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1204 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1209 } /* equivalent_node_And */
1212 * Try to remove useless Conv's:
1214 static ir_node *equivalent_node_Conv(ir_node *n) {
1216 ir_node *a = get_Conv_op(n);
1218 ir_mode *n_mode = get_irn_mode(n);
1219 ir_mode *a_mode = get_irn_mode(a);
1222 if (n_mode == a_mode) { /* No Conv necessary */
1223 if (get_Conv_strict(n)) {
1226 /* neither Minus nor Abs nor Confirm change the precision,
1227 so we can "look-through" */
1230 p = get_Minus_op(p);
1231 } else if (is_Abs(p)) {
1233 } else if (is_Confirm(p)) {
1234 p = get_Confirm_value(p);
1240 if (is_Conv(p) && get_Conv_strict(p)) {
1241 /* we known already, that a_mode == n_mode, and neither
1242 Abs nor Minus change the mode, so the second Conv
1244 assert(get_irn_mode(p) == n_mode);
1246 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1250 ir_node *pred = get_Proj_pred(p);
1251 if (is_Load(pred)) {
1252 /* Loads always return with the exact precision of n_mode */
1253 assert(get_Load_mode(pred) == n_mode);
1255 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1258 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1259 pred = get_Proj_pred(pred);
1260 if (is_Start(pred)) {
1261 /* Arguments always return with the exact precision,
1262 as strictConv's are place before Call -- if the
1263 caller was compiled with the same setting.
1264 Otherwise, the semantics is probably still right. */
1265 assert(get_irn_mode(p) == n_mode);
1267 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1273 /* special case: the immediate predecessor is also a Conv */
1274 if (! get_Conv_strict(a)) {
1275 /* first one is not strict, kick it */
1277 a_mode = get_irn_mode(a);
1281 /* else both are strict conv, second is superfluous */
1283 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1288 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1291 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1292 ir_node *b = get_Conv_op(a);
1293 ir_mode *b_mode = get_irn_mode(b);
1295 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1296 /* both are strict conv */
1297 if (smaller_mode(a_mode, n_mode)) {
1298 /* both are strict, but the first is smaller, so
1299 the second cannot remove more precision, remove the
1301 set_Conv_strict(n, 0);
1304 if (n_mode == b_mode) {
1305 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1306 if (n_mode == mode_b) {
1307 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1308 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1310 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1311 if (values_in_mode(b_mode, a_mode)) {
1312 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1313 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1318 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1319 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1320 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1321 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1323 if (float_mantissa >= int_mantissa) {
1325 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1330 if (smaller_mode(b_mode, a_mode)) {
1331 if (get_Conv_strict(n))
1332 set_Conv_strict(b, 1);
1333 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1334 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1341 } /* equivalent_node_Conv */
1344 * A Cast may be removed if the type of the previous node
1345 * is already the type of the Cast.
1347 static ir_node *equivalent_node_Cast(ir_node *n) {
1349 ir_node *pred = get_Cast_op(n);
1351 if (get_irn_type(pred) == get_Cast_type(n)) {
1353 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1356 } /* equivalent_node_Cast */
1359 * - fold Phi-nodes, iff they have only one predecessor except
1362 static ir_node *equivalent_node_Phi(ir_node *n) {
1367 ir_node *first_val = NULL; /* to shutup gcc */
1369 if (!get_opt_normalize()) return n;
1371 n_preds = get_Phi_n_preds(n);
1373 block = get_nodes_block(n);
1374 if (is_Block_dead(block)) /* Control dead */
1375 return get_irg_bad(current_ir_graph);
1377 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1379 /* Find first non-self-referencing input */
1380 for (i = 0; i < n_preds; ++i) {
1381 first_val = get_Phi_pred(n, i);
1382 if ( (first_val != n) /* not self pointer */
1384 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1385 * predecessors. Then, Phi nodes in dead code might be removed, causing
1386 * nodes pointing to themself (Add's for instance).
1387 * This is really bad and causes endless recursions in several
1388 * code pathes, so we do NOT optimize such a code.
1389 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1390 * (and bad Phi predecessors), so live code is optimized later.
1392 && (! is_Bad(get_Block_cfgpred(block, i)))
1394 ) { /* value not dead */
1395 break; /* then found first value. */
1400 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1401 return get_irg_bad(current_ir_graph);
1404 /* search for rest of inputs, determine if any of these
1405 are non-self-referencing */
1406 while (++i < n_preds) {
1407 ir_node *scnd_val = get_Phi_pred(n, i);
1408 if ( (scnd_val != n)
1409 && (scnd_val != first_val)
1412 && (! is_Bad(get_Block_cfgpred(block, i)))
1420 /* Fold, if no multiple distinct non-self-referencing inputs */
1422 DBG_OPT_PHI(oldn, n);
1425 } /* equivalent_node_Phi */
1428 * Several optimizations:
1429 * - fold Sync-nodes, iff they have only one predecessor except
1432 static ir_node *equivalent_node_Sync(ir_node *n) {
1433 int arity = get_Sync_n_preds(n);
1436 for (i = 0; i < arity;) {
1437 ir_node *pred = get_Sync_pred(n, i);
1440 /* Remove Bad predecessors */
1447 /* Remove duplicate predecessors */
1453 if (get_Sync_pred(n, j) == pred) {
1461 if (arity == 0) return get_irg_bad(current_ir_graph);
1462 if (arity == 1) return get_Sync_pred(n, 0);
1464 } /* equivalent_node_Sync */
1467 * Optimize Proj(Tuple).
1469 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1470 ir_node *oldn = proj;
1471 ir_node *tuple = get_Proj_pred(proj);
1473 /* Remove the Tuple/Proj combination. */
1474 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1475 DBG_OPT_TUPLE(oldn, tuple, proj);
1478 } /* equivalent_node_Proj_Tuple */
1481 * Optimize a / 1 = a.
1483 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1484 ir_node *oldn = proj;
1485 ir_node *div = get_Proj_pred(proj);
1486 ir_node *b = get_Div_right(div);
1487 tarval *tb = value_of(b);
1489 /* Div is not commutative. */
1490 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1491 switch (get_Proj_proj(proj)) {
1493 proj = get_Div_mem(div);
1494 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1498 proj = get_Div_left(div);
1499 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1503 /* we cannot replace the exception Proj's here, this is done in
1504 transform_node_Proj_Div() */
1509 } /* equivalent_node_Proj_Div */
1512 * Optimize a / 1.0 = a.
1514 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1515 ir_node *oldn = proj;
1516 ir_node *quot = get_Proj_pred(proj);
1517 ir_node *b = get_Quot_right(quot);
1518 tarval *tb = value_of(b);
1520 /* Div is not commutative. */
1521 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1522 switch (get_Proj_proj(proj)) {
1524 proj = get_Quot_mem(quot);
1525 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1529 proj = get_Quot_left(quot);
1530 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1534 /* we cannot replace the exception Proj's here, this is done in
1535 transform_node_Proj_Quot() */
1540 } /* equivalent_node_Proj_Quot */
1543 * Optimize a / 1 = a.
1545 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1546 ir_node *oldn = proj;
1547 ir_node *divmod = get_Proj_pred(proj);
1548 ir_node *b = get_DivMod_right(divmod);
1549 tarval *tb = value_of(b);
1551 /* Div is not commutative. */
1552 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1553 switch (get_Proj_proj(proj)) {
1555 proj = get_DivMod_mem(divmod);
1556 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1559 case pn_DivMod_res_div:
1560 proj = get_DivMod_left(divmod);
1561 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1565 /* we cannot replace the exception Proj's here, this is done in
1566 transform_node_Proj_DivMod().
1567 Note further that the pn_DivMod_res_div case is handled in
1568 computed_value_Proj(). */
1573 } /* equivalent_node_Proj_DivMod */
1576 * Optimize CopyB(mem, x, x) into a Nop.
1578 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1579 ir_node *oldn = proj;
1580 ir_node *copyb = get_Proj_pred(proj);
1581 ir_node *a = get_CopyB_dst(copyb);
1582 ir_node *b = get_CopyB_src(copyb);
1585 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1586 switch (get_Proj_proj(proj)) {
1587 case pn_CopyB_M_regular:
1588 proj = get_CopyB_mem(copyb);
1589 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1592 case pn_CopyB_X_except:
1593 DBG_OPT_EXC_REM(proj);
1594 proj = get_irg_bad(current_ir_graph);
1599 } /* equivalent_node_Proj_CopyB */
1602 * Optimize Bounds(idx, idx, upper) into idx.
1604 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1605 ir_node *oldn = proj;
1606 ir_node *bound = get_Proj_pred(proj);
1607 ir_node *idx = get_Bound_index(bound);
1608 ir_node *pred = skip_Proj(idx);
1611 if (idx == get_Bound_lower(bound))
1613 else if (is_Bound(pred)) {
1615 * idx was Bounds checked in the same MacroBlock previously,
1616 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1618 ir_node *lower = get_Bound_lower(bound);
1619 ir_node *upper = get_Bound_upper(bound);
1620 if (get_Bound_lower(pred) == lower &&
1621 get_Bound_upper(pred) == upper &&
1622 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1624 * One could expect that we simply return the previous
1625 * Bound here. However, this would be wrong, as we could
1626 * add an exception Proj to a new location then.
1627 * So, we must turn in into a tuple.
1633 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1634 switch (get_Proj_proj(proj)) {
1636 DBG_OPT_EXC_REM(proj);
1637 proj = get_Bound_mem(bound);
1639 case pn_Bound_X_except:
1640 DBG_OPT_EXC_REM(proj);
1641 proj = get_irg_bad(current_ir_graph);
1645 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1648 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1653 } /* equivalent_node_Proj_Bound */
1656 * Optimize an Exception Proj(Load) with a non-null address.
1658 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1659 if (get_opt_ldst_only_null_ptr_exceptions()) {
1660 if (get_irn_mode(proj) == mode_X) {
1661 ir_node *load = get_Proj_pred(proj);
1663 /* get the Load address */
1664 const ir_node *addr = get_Load_ptr(load);
1665 const ir_node *confirm;
1667 if (value_not_null(addr, &confirm)) {
1668 if (get_Proj_proj(proj) == pn_Load_X_except) {
1669 DBG_OPT_EXC_REM(proj);
1670 return get_irg_bad(current_ir_graph);
1676 } /* equivalent_node_Proj_Load */
1679 * Optimize an Exception Proj(Store) with a non-null address.
1681 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1682 if (get_opt_ldst_only_null_ptr_exceptions()) {
1683 if (get_irn_mode(proj) == mode_X) {
1684 ir_node *store = get_Proj_pred(proj);
1686 /* get the load/store address */
1687 const ir_node *addr = get_Store_ptr(store);
1688 const ir_node *confirm;
1690 if (value_not_null(addr, &confirm)) {
1691 if (get_Proj_proj(proj) == pn_Store_X_except) {
1692 DBG_OPT_EXC_REM(proj);
1693 return get_irg_bad(current_ir_graph);
1699 } /* equivalent_node_Proj_Store */
1702 * Does all optimizations on nodes that must be done on it's Proj's
1703 * because of creating new nodes.
1705 static ir_node *equivalent_node_Proj(ir_node *proj) {
1706 ir_node *n = get_Proj_pred(proj);
1708 if (get_irn_mode(proj) == mode_X) {
1709 if (is_Block_dead(get_nodes_block(n))) {
1710 /* Remove dead control flow -- early gigo(). */
1711 return get_irg_bad(current_ir_graph);
1714 if (n->op->ops.equivalent_node_Proj)
1715 return n->op->ops.equivalent_node_Proj(proj);
1717 } /* equivalent_node_Proj */
1722 static ir_node *equivalent_node_Id(ir_node *n) {
1729 DBG_OPT_ID(oldn, n);
1731 } /* equivalent_node_Id */
1736 static ir_node *equivalent_node_Mux(ir_node *n)
1738 ir_node *oldn = n, *sel = get_Mux_sel(n);
1740 tarval *ts = value_of(sel);
1742 /* Mux(true, f, t) == t */
1743 if (ts == tarval_b_true) {
1744 n = get_Mux_true(n);
1745 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1748 /* Mux(false, f, t) == f */
1749 if (ts == tarval_b_false) {
1750 n = get_Mux_false(n);
1751 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1754 n_t = get_Mux_true(n);
1755 n_f = get_Mux_false(n);
1757 /* Mux(v, x, T) == x */
1758 if (is_Unknown(n_f)) {
1760 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1763 /* Mux(v, T, x) == x */
1764 if (is_Unknown(n_t)) {
1766 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1770 /* Mux(v, x, x) == x */
1773 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1776 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1777 ir_node *cmp = get_Proj_pred(sel);
1778 long proj_nr = get_Proj_proj(sel);
1779 ir_node *f = get_Mux_false(n);
1780 ir_node *t = get_Mux_true(n);
1783 * Note further that these optimization work even for floating point
1784 * with NaN's because -NaN == NaN.
1785 * However, if +0 and -0 is handled differently, we cannot use the first one.
1788 ir_node *const cmp_l = get_Cmp_left(cmp);
1789 ir_node *const cmp_r = get_Cmp_right(cmp);
1793 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1794 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1796 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1803 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1804 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1806 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1813 * Note: normalization puts the constant on the right side,
1814 * so we check only one case.
1816 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1817 /* Mux(t CMP 0, X, t) */
1818 if (is_Minus(f) && get_Minus_op(f) == t) {
1819 /* Mux(t CMP 0, -t, t) */
1820 if (proj_nr == pn_Cmp_Eq) {
1821 /* Mux(t == 0, -t, t) ==> -t */
1823 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1824 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1825 /* Mux(t != 0, -t, t) ==> t */
1827 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1834 } /* equivalent_node_Mux */
1837 * Remove Confirm nodes if setting is on.
1838 * Replace Confirms(x, '=', Constlike) by Constlike.
1840 static ir_node *equivalent_node_Confirm(ir_node *n) {
1841 ir_node *pred = get_Confirm_value(n);
1842 pn_Cmp pnc = get_Confirm_cmp(n);
1844 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1846 * rare case: two identical Confirms one after another,
1847 * replace the second one with the first.
1850 pred = get_Confirm_value(n);
1851 pnc = get_Confirm_cmp(n);
1853 if (get_opt_remove_confirm())
1854 return get_Confirm_value(n);
1859 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1860 * perform no actual computation, as, e.g., the Id nodes. It does not create
1861 * new nodes. It is therefore safe to free n if the node returned is not n.
1862 * If a node returns a Tuple we can not just skip it. If the size of the
1863 * in array fits, we transform n into a tuple (e.g., Div).
1865 ir_node *equivalent_node(ir_node *n) {
1866 if (n->op->ops.equivalent_node)
1867 return n->op->ops.equivalent_node(n);
1869 } /* equivalent_node */
1872 * Sets the default equivalent node operation for an ir_op_ops.
1874 * @param code the opcode for the default operation
1875 * @param ops the operations initialized
1880 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1884 ops->equivalent_node = equivalent_node_##a; \
1886 #define CASE_PROJ(a) \
1888 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1930 } /* firm_set_default_equivalent_node */
1933 * Returns non-zero if a node is a Phi node
1934 * with all predecessors constant.
1936 static int is_const_Phi(ir_node *n) {
1939 if (! is_Phi(n) || get_irn_arity(n) == 0)
1941 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1942 if (! is_Const(get_irn_n(n, i)))
1946 } /* is_const_Phi */
1948 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1949 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1952 * in reality eval_func should be tarval (*eval_func)() but incomplete
1953 * declarations are bad style and generate noisy warnings
1955 typedef void (*eval_func)(void);
1958 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1960 static tarval *do_eval(eval_func eval, tarval *a, tarval *b, ir_mode *mode)
1962 if (eval == (eval_func) tarval_sub) {
1963 tarval_sub_type func = (tarval_sub_type)eval;
1965 return func(a, b, mode);
1967 tarval_binop_type func = (tarval_binop_type)eval;
1974 * Apply an evaluator on a binop with a constant operators (and one Phi).
1976 * @param phi the Phi node
1977 * @param other the other operand
1978 * @param eval an evaluator function
1979 * @param mode the mode of the result, may be different from the mode of the Phi!
1980 * @param left if non-zero, other is the left operand, else the right
1982 * @return a new Phi node if the conversion was successful, NULL else
1984 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, eval_func eval, ir_mode *mode, int left) {
1989 int i, n = get_irn_arity(phi);
1991 NEW_ARR_A(void *, res, n);
1993 for (i = 0; i < n; ++i) {
1994 pred = get_irn_n(phi, i);
1995 tv = get_Const_tarval(pred);
1996 tv = do_eval(eval, other, tv, mode);
1998 if (tv == tarval_bad) {
1999 /* folding failed, bad */
2005 for (i = 0; i < n; ++i) {
2006 pred = get_irn_n(phi, i);
2007 tv = get_Const_tarval(pred);
2008 tv = do_eval(eval, tv, other, mode);
2010 if (tv == tarval_bad) {
2011 /* folding failed, bad */
2017 irg = current_ir_graph;
2018 for (i = 0; i < n; ++i) {
2019 pred = get_irn_n(phi, i);
2020 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2022 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2023 } /* apply_binop_on_phi */
2026 * Apply an evaluator on a binop with two constant Phi.
2028 * @param a the left Phi node
2029 * @param b the right Phi node
2030 * @param eval an evaluator function
2031 * @param mode the mode of the result, may be different from the mode of the Phi!
2033 * @return a new Phi node if the conversion was successful, NULL else
2035 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode) {
2036 tarval *tv_l, *tv_r, *tv;
2042 if (get_nodes_block(a) != get_nodes_block(b))
2045 n = get_irn_arity(a);
2046 NEW_ARR_A(void *, res, n);
2048 for (i = 0; i < n; ++i) {
2049 pred = get_irn_n(a, i);
2050 tv_l = get_Const_tarval(pred);
2051 pred = get_irn_n(b, i);
2052 tv_r = get_Const_tarval(pred);
2053 tv = do_eval(eval, tv_l, tv_r, mode);
2055 if (tv == tarval_bad) {
2056 /* folding failed, bad */
2061 irg = current_ir_graph;
2062 for (i = 0; i < n; ++i) {
2063 pred = get_irn_n(a, i);
2064 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2066 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2067 } /* apply_binop_on_2_phis */
2070 * Apply an evaluator on a unop with a constant operator (a Phi).
2072 * @param phi the Phi node
2073 * @param eval an evaluator function
2075 * @return a new Phi node if the conversion was successful, NULL else
2077 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2083 int i, n = get_irn_arity(phi);
2085 NEW_ARR_A(void *, res, n);
2086 for (i = 0; i < n; ++i) {
2087 pred = get_irn_n(phi, i);
2088 tv = get_Const_tarval(pred);
2091 if (tv == tarval_bad) {
2092 /* folding failed, bad */
2097 mode = get_irn_mode(phi);
2098 irg = current_ir_graph;
2099 for (i = 0; i < n; ++i) {
2100 pred = get_irn_n(phi, i);
2101 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2103 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2104 } /* apply_unop_on_phi */
2107 * Apply a conversion on a constant operator (a Phi).
2109 * @param phi the Phi node
2111 * @return a new Phi node if the conversion was successful, NULL else
2113 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2118 int i, n = get_irn_arity(phi);
2120 NEW_ARR_A(void *, res, n);
2121 for (i = 0; i < n; ++i) {
2122 pred = get_irn_n(phi, i);
2123 tv = get_Const_tarval(pred);
2124 tv = tarval_convert_to(tv, mode);
2126 if (tv == tarval_bad) {
2127 /* folding failed, bad */
2132 irg = current_ir_graph;
2133 for (i = 0; i < n; ++i) {
2134 pred = get_irn_n(phi, i);
2135 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2137 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2138 } /* apply_conv_on_phi */
2141 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2142 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2143 * If possible, remove the Conv's.
2145 static ir_node *transform_node_AddSub(ir_node *n) {
2146 ir_mode *mode = get_irn_mode(n);
2148 if (mode_is_reference(mode)) {
2149 ir_node *left = get_binop_left(n);
2150 ir_node *right = get_binop_right(n);
2151 unsigned ref_bits = get_mode_size_bits(mode);
2153 if (is_Conv(left)) {
2154 ir_mode *lmode = get_irn_mode(left);
2155 unsigned bits = get_mode_size_bits(lmode);
2157 if (ref_bits == bits &&
2158 mode_is_int(lmode) &&
2159 get_mode_arithmetic(lmode) == irma_twos_complement) {
2160 ir_node *pre = get_Conv_op(left);
2161 ir_mode *pre_mode = get_irn_mode(pre);
2163 if (mode_is_int(pre_mode) &&
2164 get_mode_size_bits(pre_mode) == bits &&
2165 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2166 /* ok, this conv just changes to sign, moreover the calculation
2167 * is done with same number of bits as our address mode, so
2168 * we can ignore the conv as address calculation can be viewed
2169 * as either signed or unsigned
2171 set_binop_left(n, pre);
2176 if (is_Conv(right)) {
2177 ir_mode *rmode = get_irn_mode(right);
2178 unsigned bits = get_mode_size_bits(rmode);
2180 if (ref_bits == bits &&
2181 mode_is_int(rmode) &&
2182 get_mode_arithmetic(rmode) == irma_twos_complement) {
2183 ir_node *pre = get_Conv_op(right);
2184 ir_mode *pre_mode = get_irn_mode(pre);
2186 if (mode_is_int(pre_mode) &&
2187 get_mode_size_bits(pre_mode) == bits &&
2188 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2189 /* ok, this conv just changes to sign, moreover the calculation
2190 * is done with same number of bits as our address mode, so
2191 * we can ignore the conv as address calculation can be viewed
2192 * as either signed or unsigned
2194 set_binop_right(n, pre);
2199 /* let address arithmetic use unsigned modes */
2200 if (is_Const(right)) {
2201 ir_mode *rmode = get_irn_mode(right);
2203 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2204 /* convert a AddP(P, *s) into AddP(P, *u) */
2205 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2207 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2208 set_binop_right(n, pre);
2214 } /* transform_node_AddSub */
2216 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2218 if (is_Const(b) && is_const_Phi(a)) { \
2219 /* check for Op(Phi, Const) */ \
2220 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2222 else if (is_Const(a) && is_const_Phi(b)) { \
2223 /* check for Op(Const, Phi) */ \
2224 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2226 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2227 /* check for Op(Phi, Phi) */ \
2228 c = apply_binop_on_2_phis(a, b, eval, mode); \
2231 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2235 #define HANDLE_UNOP_PHI(eval, a, c) \
2237 if (is_const_Phi(a)) { \
2238 /* check for Op(Phi) */ \
2239 c = apply_unop_on_phi(a, eval); \
2241 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2247 * Do the AddSub optimization, then Transform
2248 * Constant folding on Phi
2249 * Add(a,a) -> Mul(a, 2)
2250 * Add(Mul(a, x), a) -> Mul(a, x+1)
2251 * if the mode is integer or float.
2252 * Transform Add(a,-b) into Sub(a,b).
2253 * Reassociation might fold this further.
2255 static ir_node *transform_node_Add(ir_node *n) {
2257 ir_node *a, *b, *c, *oldn = n;
2259 n = transform_node_AddSub(n);
2261 a = get_Add_left(n);
2262 b = get_Add_right(n);
2264 mode = get_irn_mode(n);
2266 if (mode_is_reference(mode)) {
2267 ir_mode *lmode = get_irn_mode(a);
2269 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2270 /* an Add(a, NULL) is a hidden Conv */
2271 dbg_info *dbg = get_irn_dbg_info(n);
2272 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2276 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2278 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2279 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2282 if (mode_is_num(mode)) {
2283 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2284 if (!is_irg_state(current_ir_graph, IR_GRAPH_STATE_ARCH_DEP)
2285 && a == b && mode_is_int(mode)) {
2286 ir_node *block = get_nodes_block(n);
2289 get_irn_dbg_info(n),
2292 new_Const_long(mode, 2),
2294 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2299 get_irn_dbg_info(n),
2304 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2309 get_irn_dbg_info(n),
2314 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2317 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2318 /* Here we rely on constants be on the RIGHT side */
2320 ir_node *op = get_Not_op(a);
2322 if (is_Const(b) && is_Const_one(b)) {
2324 ir_node *blk = get_nodes_block(n);
2325 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2326 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2331 n = new_Const(get_mode_minus_one(mode));
2332 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2337 ir_node *op = get_Not_op(b);
2341 n = new_Const(get_mode_minus_one(mode));
2342 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2348 if (mode_is_int(mode) && a->vrp.valid && b->vrp.valid) {
2349 tarval *c = tarval_and(
2350 tarval_not(a->vrp.bits_not_set),
2351 tarval_not(b->vrp.bits_not_set)
2354 if(tarval_is_null(c)) {
2355 dbg_info *dbgi = get_irn_dbg_info(n);
2356 return new_rd_Or(dbgi, get_nodes_block(n),
2361 } /* transform_node_Add */
2364 * returns -cnst or NULL if impossible
2366 static ir_node *const_negate(ir_node *cnst) {
2367 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2368 dbg_info *dbgi = get_irn_dbg_info(cnst);
2369 ir_graph *irg = get_irn_irg(cnst);
2370 if (tv == tarval_bad) return NULL;
2371 return new_rd_Const(dbgi, irg, tv);
2375 * Do the AddSub optimization, then Transform
2376 * Constant folding on Phi
2377 * Sub(0,a) -> Minus(a)
2378 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2379 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2380 * Sub(Add(a, x), x) -> a
2381 * Sub(x, Add(x, a)) -> -a
2382 * Sub(x, Const) -> Add(x, -Const)
2384 static ir_node *transform_node_Sub(ir_node *n) {
2389 n = transform_node_AddSub(n);
2391 a = get_Sub_left(n);
2392 b = get_Sub_right(n);
2394 mode = get_irn_mode(n);
2396 if (mode_is_int(mode)) {
2397 ir_mode *lmode = get_irn_mode(a);
2399 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2400 /* a Sub(a, NULL) is a hidden Conv */
2401 dbg_info *dbg = get_irn_dbg_info(n);
2402 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2403 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2407 if (mode == lmode &&
2408 get_mode_arithmetic(mode) == irma_twos_complement &&
2410 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2412 dbg_info *dbg = get_irn_dbg_info(n);
2413 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2414 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2420 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2422 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2423 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2426 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2427 /* a - C -> a + (-C) */
2428 ir_node *cnst = const_negate(b);
2430 ir_node *block = get_nodes_block(n);
2431 dbg_info *dbgi = get_irn_dbg_info(n);
2433 n = new_rd_Add(dbgi, block, a, cnst, mode);
2434 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2439 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2440 dbg_info *dbg = get_irn_dbg_info(n);
2441 ir_node *block = get_nodes_block(n);
2442 ir_node *left = get_Minus_op(a);
2443 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2445 n = new_rd_Minus(dbg, block, add, mode);
2446 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2448 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2449 dbg_info *dbg = get_irn_dbg_info(n);
2450 ir_node *block = get_nodes_block(n);
2451 ir_node *right = get_Minus_op(b);
2453 n = new_rd_Add(dbg, block, a, right, mode);
2454 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2456 } else if (is_Sub(b)) {
2457 /* a - (b - c) -> a + (c - b)
2458 * -> (a - b) + c iff (b - c) is a pointer */
2459 dbg_info *s_dbg = get_irn_dbg_info(b);
2460 ir_node *s_block = get_nodes_block(b);
2461 ir_node *s_left = get_Sub_left(b);
2462 ir_node *s_right = get_Sub_right(b);
2463 ir_mode *s_mode = get_irn_mode(b);
2464 if (mode_is_reference(s_mode)) {
2465 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2466 dbg_info *a_dbg = get_irn_dbg_info(n);
2467 ir_node *a_block = get_nodes_block(n);
2470 s_right = new_r_Conv(a_block, s_right, mode);
2471 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2473 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2474 dbg_info *a_dbg = get_irn_dbg_info(n);
2475 ir_node *a_block = get_nodes_block(n);
2477 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2479 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2481 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2482 ir_node *m_right = get_Mul_right(b);
2483 if (is_Const(m_right)) {
2484 ir_node *cnst2 = const_negate(m_right);
2485 if (cnst2 != NULL) {
2486 dbg_info *m_dbg = get_irn_dbg_info(b);
2487 ir_node *m_block = get_nodes_block(b);
2488 ir_node *m_left = get_Mul_left(b);
2489 ir_mode *m_mode = get_irn_mode(b);
2490 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2491 dbg_info *a_dbg = get_irn_dbg_info(n);
2492 ir_node *a_block = get_nodes_block(n);
2494 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2495 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2501 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2502 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2504 get_irn_dbg_info(n),
2508 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2512 if (mode_wrap_around(mode)) {
2513 ir_node *left = get_Add_left(a);
2514 ir_node *right = get_Add_right(a);
2516 /* FIXME: Does the Conv's work only for two complement or generally? */
2518 if (mode != get_irn_mode(right)) {
2519 /* This Sub is an effective Cast */
2520 right = new_r_Conv(get_nodes_block(n), right, mode);
2523 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2525 } else if (right == b) {
2526 if (mode != get_irn_mode(left)) {
2527 /* This Sub is an effective Cast */
2528 left = new_r_Conv(get_nodes_block(n), left, mode);
2531 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2537 if (mode_wrap_around(mode)) {
2538 ir_node *left = get_Add_left(b);
2539 ir_node *right = get_Add_right(b);
2541 /* FIXME: Does the Conv's work only for two complement or generally? */
2543 ir_mode *r_mode = get_irn_mode(right);
2545 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2546 if (mode != r_mode) {
2547 /* This Sub is an effective Cast */
2548 n = new_r_Conv(get_nodes_block(n), n, mode);
2550 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2552 } else if (right == a) {
2553 ir_mode *l_mode = get_irn_mode(left);
2555 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2556 if (mode != l_mode) {
2557 /* This Sub is an effective Cast */
2558 n = new_r_Conv(get_nodes_block(n), n, mode);
2560 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2565 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2566 ir_mode *mode = get_irn_mode(a);
2568 if (mode == get_irn_mode(b)) {
2570 ir_node *op_a = get_Conv_op(a);
2571 ir_node *op_b = get_Conv_op(b);
2573 /* check if it's allowed to skip the conv */
2574 ma = get_irn_mode(op_a);
2575 mb = get_irn_mode(op_b);
2577 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2578 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2581 set_Sub_right(n, b);
2587 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2588 if (!is_reassoc_running() && is_Mul(a)) {
2589 ir_node *ma = get_Mul_left(a);
2590 ir_node *mb = get_Mul_right(a);
2593 ir_node *blk = get_nodes_block(n);
2595 get_irn_dbg_info(n),
2599 get_irn_dbg_info(n),
2602 new_Const_long(mode, 1),
2605 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2607 } else if (mb == b) {
2608 ir_node *blk = get_nodes_block(n);
2610 get_irn_dbg_info(n),
2614 get_irn_dbg_info(n),
2617 new_Const_long(mode, 1),
2620 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2624 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2625 ir_node *x = get_Sub_left(a);
2626 ir_node *y = get_Sub_right(a);
2627 ir_node *blk = get_nodes_block(n);
2628 ir_mode *m_b = get_irn_mode(b);
2629 ir_mode *m_y = get_irn_mode(y);
2633 /* Determine the right mode for the Add. */
2636 else if (mode_is_reference(m_b))
2638 else if (mode_is_reference(m_y))
2642 * Both modes are different but none is reference,
2643 * happens for instance in SubP(SubP(P, Iu), Is).
2644 * We have two possibilities here: Cast or ignore.
2645 * Currently we ignore this case.
2650 add = new_r_Add(blk, y, b, add_mode);
2652 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2653 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2657 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2658 if (is_Const(a) && is_Not(b)) {
2659 /* c - ~X = X + (c+1) */
2660 tarval *tv = get_Const_tarval(a);
2662 tv = tarval_add(tv, get_mode_one(mode));
2663 if (tv != tarval_bad) {
2664 ir_node *blk = get_nodes_block(n);
2665 ir_node *c = new_Const(tv);
2666 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2667 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2673 } /* transform_node_Sub */
2676 * Several transformation done on n*n=2n bits mul.
2677 * These transformations must be done here because new nodes may be produced.
2679 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2681 ir_node *a = get_Mul_left(n);
2682 ir_node *b = get_Mul_right(n);
2683 tarval *ta = value_of(a);
2684 tarval *tb = value_of(b);
2685 ir_mode *smode = get_irn_mode(a);
2687 if (ta == get_mode_one(smode)) {
2688 /* (L)1 * (L)b = (L)b */
2689 ir_node *blk = get_nodes_block(n);
2690 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2691 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2694 else if (ta == get_mode_minus_one(smode)) {
2695 /* (L)-1 * (L)b = (L)b */
2696 ir_node *blk = get_nodes_block(n);
2697 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2698 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2699 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2702 if (tb == get_mode_one(smode)) {
2703 /* (L)a * (L)1 = (L)a */
2704 ir_node *blk = get_irn_n(a, -1);
2705 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2706 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2709 else if (tb == get_mode_minus_one(smode)) {
2710 /* (L)a * (L)-1 = (L)-a */
2711 ir_node *blk = get_nodes_block(n);
2712 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2713 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2714 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2721 * Transform Mul(a,-1) into -a.
2722 * Do constant evaluation of Phi nodes.
2723 * Do architecture dependent optimizations on Mul nodes
2725 static ir_node *transform_node_Mul(ir_node *n) {
2726 ir_node *c, *oldn = n;
2727 ir_mode *mode = get_irn_mode(n);
2728 ir_node *a = get_Mul_left(n);
2729 ir_node *b = get_Mul_right(n);
2731 if (is_Bad(a) || is_Bad(b))
2734 if (mode != get_irn_mode(a))
2735 return transform_node_Mul2n(n, mode);
2737 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2739 if (mode_is_signed(mode)) {
2742 if (value_of(a) == get_mode_minus_one(mode))
2744 else if (value_of(b) == get_mode_minus_one(mode))
2747 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2748 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2753 if (is_Const(b)) { /* (-a) * const -> a * -const */
2754 ir_node *cnst = const_negate(b);
2756 dbg_info *dbgi = get_irn_dbg_info(n);
2757 ir_node *block = get_nodes_block(n);
2758 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2759 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2762 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2763 dbg_info *dbgi = get_irn_dbg_info(n);
2764 ir_node *block = get_nodes_block(n);
2765 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2766 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2768 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2769 ir_node *sub_l = get_Sub_left(b);
2770 ir_node *sub_r = get_Sub_right(b);
2771 dbg_info *dbgi = get_irn_dbg_info(n);
2772 ir_node *block = get_nodes_block(n);
2773 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2774 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2775 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2778 } else if (is_Minus(b)) {
2779 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2780 ir_node *sub_l = get_Sub_left(a);
2781 ir_node *sub_r = get_Sub_right(a);
2782 dbg_info *dbgi = get_irn_dbg_info(n);
2783 ir_node *block = get_nodes_block(n);
2784 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2785 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2786 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2789 } else if (is_Shl(a)) {
2790 ir_node *const shl_l = get_Shl_left(a);
2791 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2792 /* (1 << x) * b -> b << x */
2793 dbg_info *const dbgi = get_irn_dbg_info(n);
2794 ir_node *const block = get_nodes_block(n);
2795 ir_node *const shl_r = get_Shl_right(a);
2796 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2797 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2800 } else if (is_Shl(b)) {
2801 ir_node *const shl_l = get_Shl_left(b);
2802 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2803 /* a * (1 << x) -> a << x */
2804 dbg_info *const dbgi = get_irn_dbg_info(n);
2805 ir_node *const block = get_nodes_block(n);
2806 ir_node *const shl_r = get_Shl_right(b);
2807 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2808 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2812 if (get_mode_arithmetic(mode) == irma_ieee754) {
2814 tarval *tv = get_Const_tarval(a);
2815 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2816 && !tarval_is_negative(tv)) {
2817 /* 2.0 * b = b + b */
2818 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2819 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2823 else if (is_Const(b)) {
2824 tarval *tv = get_Const_tarval(b);
2825 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2826 && !tarval_is_negative(tv)) {
2827 /* a * 2.0 = a + a */
2828 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2829 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2834 return arch_dep_replace_mul_with_shifts(n);
2835 } /* transform_node_Mul */
2838 * Transform a Div Node.
2840 static ir_node *transform_node_Div(ir_node *n) {
2841 ir_mode *mode = get_Div_resmode(n);
2842 ir_node *a = get_Div_left(n);
2843 ir_node *b = get_Div_right(n);
2845 const ir_node *dummy;
2847 if (is_Const(b) && is_const_Phi(a)) {
2848 /* check for Div(Phi, Const) */
2849 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2851 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2855 else if (is_Const(a) && is_const_Phi(b)) {
2856 /* check for Div(Const, Phi) */
2857 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2859 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2863 else if (is_const_Phi(a) && is_const_Phi(b)) {
2864 /* check for Div(Phi, Phi) */
2865 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2867 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2874 if (a == b && value_not_zero(a, &dummy)) {
2875 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2876 value = new_Const(get_mode_one(mode));
2877 DBG_OPT_CSTEVAL(n, value);
2880 if (mode_is_signed(mode) && is_Const(b)) {
2881 tarval *tv = get_Const_tarval(b);
2883 if (tv == get_mode_minus_one(mode)) {
2885 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2886 DBG_OPT_CSTEVAL(n, value);
2890 /* Try architecture dependent optimization */
2891 value = arch_dep_replace_div_by_const(n);
2898 /* Turn Div into a tuple (mem, jmp, bad, value) */
2899 mem = get_Div_mem(n);
2900 blk = get_nodes_block(n);
2902 /* skip a potential Pin */
2903 mem = skip_Pin(mem);
2904 turn_into_tuple(n, pn_Div_max);
2905 set_Tuple_pred(n, pn_Div_M, mem);
2906 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2907 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2908 set_Tuple_pred(n, pn_Div_res, value);
2911 } /* transform_node_Div */
2914 * Transform a Mod node.
2916 static ir_node *transform_node_Mod(ir_node *n) {
2917 ir_mode *mode = get_Mod_resmode(n);
2918 ir_node *a = get_Mod_left(n);
2919 ir_node *b = get_Mod_right(n);
2923 if (is_Const(b) && is_const_Phi(a)) {
2924 /* check for Div(Phi, Const) */
2925 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2927 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2931 else if (is_Const(a) && is_const_Phi(b)) {
2932 /* check for Div(Const, Phi) */
2933 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2935 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2939 else if (is_const_Phi(a) && is_const_Phi(b)) {
2940 /* check for Div(Phi, Phi) */
2941 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2943 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2950 if (tv != tarval_bad) {
2951 value = new_Const(tv);
2953 DBG_OPT_CSTEVAL(n, value);
2956 ir_node *a = get_Mod_left(n);
2957 ir_node *b = get_Mod_right(n);
2958 const ir_node *dummy;
2960 if (a == b && value_not_zero(a, &dummy)) {
2961 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2962 value = new_Const(get_mode_null(mode));
2963 DBG_OPT_CSTEVAL(n, value);
2966 if (mode_is_signed(mode) && is_Const(b)) {
2967 tarval *tv = get_Const_tarval(b);
2969 if (tv == get_mode_minus_one(mode)) {
2971 value = new_Const(get_mode_null(mode));
2972 DBG_OPT_CSTEVAL(n, value);
2976 /* Try architecture dependent optimization */
2977 value = arch_dep_replace_mod_by_const(n);
2985 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2986 mem = get_Mod_mem(n);
2987 blk = get_nodes_block(n);
2989 /* skip a potential Pin */
2990 mem = skip_Pin(mem);
2991 turn_into_tuple(n, pn_Mod_max);
2992 set_Tuple_pred(n, pn_Mod_M, mem);
2993 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2994 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2995 set_Tuple_pred(n, pn_Mod_res, value);
2998 } /* transform_node_Mod */
3001 * Transform a DivMod node.
3003 static ir_node *transform_node_DivMod(ir_node *n) {
3004 const ir_node *dummy;
3005 ir_node *a = get_DivMod_left(n);
3006 ir_node *b = get_DivMod_right(n);
3007 ir_mode *mode = get_DivMod_resmode(n);
3012 if (is_Const(b) && is_const_Phi(a)) {
3013 /* check for Div(Phi, Const) */
3014 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3015 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3017 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3018 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3022 else if (is_Const(a) && is_const_Phi(b)) {
3023 /* check for Div(Const, Phi) */
3024 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3025 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3027 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3028 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3032 else if (is_const_Phi(a) && is_const_Phi(b)) {
3033 /* check for Div(Phi, Phi) */
3034 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3035 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3037 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3038 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3045 if (tb != tarval_bad) {
3046 if (tb == get_mode_one(get_tarval_mode(tb))) {
3048 vb = new_Const(get_mode_null(mode));
3049 DBG_OPT_CSTEVAL(n, vb);
3051 } else if (ta != tarval_bad) {
3052 tarval *resa, *resb;
3053 resa = tarval_div(ta, tb);
3054 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3055 Jmp for X result!? */
3056 resb = tarval_mod(ta, tb);
3057 if (resb == tarval_bad) return n; /* Causes exception! */
3058 va = new_Const(resa);
3059 vb = new_Const(resb);
3060 DBG_OPT_CSTEVAL(n, va);
3061 DBG_OPT_CSTEVAL(n, vb);
3063 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3064 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3065 vb = new_Const(get_mode_null(mode));
3066 DBG_OPT_CSTEVAL(n, va);
3067 DBG_OPT_CSTEVAL(n, vb);
3069 } else { /* Try architecture dependent optimization */
3072 arch_dep_replace_divmod_by_const(&va, &vb, n);
3073 evaluated = va != NULL;
3075 } else if (a == b) {
3076 if (value_not_zero(a, &dummy)) {
3078 va = new_Const(get_mode_one(mode));
3079 vb = new_Const(get_mode_null(mode));
3080 DBG_OPT_CSTEVAL(n, va);
3081 DBG_OPT_CSTEVAL(n, vb);
3084 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3087 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3088 /* 0 / non-Const = 0 */
3093 if (evaluated) { /* replace by tuple */
3097 mem = get_DivMod_mem(n);
3098 /* skip a potential Pin */
3099 mem = skip_Pin(mem);
3101 blk = get_nodes_block(n);
3102 turn_into_tuple(n, pn_DivMod_max);
3103 set_Tuple_pred(n, pn_DivMod_M, mem);
3104 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3105 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3106 set_Tuple_pred(n, pn_DivMod_res_div, va);
3107 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3111 } /* transform_node_DivMod */
3114 * Optimize x / c to x * (1/c)
3116 static ir_node *transform_node_Quot(ir_node *n) {
3117 ir_mode *mode = get_Quot_resmode(n);
3120 if (get_mode_arithmetic(mode) == irma_ieee754) {
3121 ir_node *b = get_Quot_right(n);
3122 tarval *tv = value_of(b);
3124 if (tv != tarval_bad) {
3125 int rem = tarval_fp_ops_enabled();
3128 * Floating point constant folding might be disabled here to
3130 * However, as we check for exact result, doing it is safe.
3133 tarval_enable_fp_ops(1);
3134 tv = tarval_quo(get_mode_one(mode), tv);
3135 tarval_enable_fp_ops(rem);
3137 /* Do the transformation if the result is either exact or we are not
3138 using strict rules. */
3139 if (tv != tarval_bad &&
3140 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3141 ir_node *blk = get_nodes_block(n);
3142 ir_node *c = new_Const(tv);
3143 ir_node *a = get_Quot_left(n);
3144 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3145 ir_node *mem = get_Quot_mem(n);
3147 /* skip a potential Pin */
3148 mem = skip_Pin(mem);
3149 turn_into_tuple(n, pn_Quot_max);
3150 set_Tuple_pred(n, pn_Quot_M, mem);
3151 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3152 set_Tuple_pred(n, pn_Quot_X_except, new_Bad());
3153 set_Tuple_pred(n, pn_Quot_res, m);
3154 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3159 } /* transform_node_Quot */
3162 * Optimize Abs(x) into x if x is Confirmed >= 0
3163 * Optimize Abs(x) into -x if x is Confirmed <= 0
3164 * Optimize Abs(-x) int Abs(x)
3166 static ir_node *transform_node_Abs(ir_node *n) {
3167 ir_node *c, *oldn = n;
3168 ir_node *a = get_Abs_op(n);
3171 HANDLE_UNOP_PHI(tarval_abs, a, c);
3173 switch (classify_value_sign(a)) {
3174 case value_classified_negative:
3175 mode = get_irn_mode(n);
3178 * We can replace the Abs by -x here.
3179 * We even could add a new Confirm here
3180 * (if not twos complement)
3182 * Note that -x would create a new node, so we could
3183 * not run it in the equivalent_node() context.
3185 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3187 DBG_OPT_CONFIRM(oldn, n);
3189 case value_classified_positive:
3190 /* n is positive, Abs is not needed */
3193 DBG_OPT_CONFIRM(oldn, n);
3199 /* Abs(-x) = Abs(x) */
3200 mode = get_irn_mode(n);
3201 n = new_rd_Abs(get_irn_dbg_info(n), get_nodes_block(n), get_Minus_op(a), mode);
3202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3206 } /* transform_node_Abs */
3209 * Optimize -a CMP -b into b CMP a.
3210 * This works only for for modes where unary Minus
3212 * Note that two-complement integers can Overflow
3213 * so it will NOT work.
3215 * For == and != can be handled in Proj(Cmp)
3217 static ir_node *transform_node_Cmp(ir_node *n) {
3219 ir_node *left = get_Cmp_left(n);
3220 ir_node *right = get_Cmp_right(n);
3222 if (is_Minus(left) && is_Minus(right) &&
3223 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3224 ir_node *const new_left = get_Minus_op(right);
3225 ir_node *const new_right = get_Minus_op(left);
3226 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3227 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3230 } /* transform_node_Cmp */
3234 * Transform a Cond node.
3236 * Replace the Cond by a Jmp if it branches on a constant
3239 static ir_node *transform_node_Cond(ir_node *n) {
3242 ir_node *a = get_Cond_selector(n);
3243 tarval *ta = value_of(a);
3245 /* we need block info which is not available in floating irgs */
3246 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3249 if ((ta != tarval_bad) &&
3250 (get_irn_mode(a) == mode_b) &&
3251 (get_opt_unreachable_code())) {
3252 /* It's a boolean Cond, branching on a boolean constant.
3253 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3254 ir_node *blk = get_nodes_block(n);
3255 jmp = new_r_Jmp(blk);
3256 turn_into_tuple(n, pn_Cond_max);
3257 if (ta == tarval_b_true) {
3258 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3259 set_Tuple_pred(n, pn_Cond_true, jmp);
3261 set_Tuple_pred(n, pn_Cond_false, jmp);
3262 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3264 /* We might generate an endless loop, so keep it alive. */
3265 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3268 } /* transform_node_Cond */
3271 * Prototype of a recursive transform function
3272 * for bitwise distributive transformations.
3274 typedef ir_node* (*recursive_transform)(ir_node *n);
3277 * makes use of distributive laws for and, or, eor
3278 * and(a OP c, b OP c) -> and(a, b) OP c
3279 * note, might return a different op than n
3281 static ir_node *transform_bitwise_distributive(ir_node *n,
3282 recursive_transform trans_func)
3285 ir_node *a = get_binop_left(n);
3286 ir_node *b = get_binop_right(n);
3287 ir_op *op = get_irn_op(a);
3288 ir_op *op_root = get_irn_op(n);
3290 if(op != get_irn_op(b))
3293 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3294 if (op == op_Conv) {
3295 ir_node *a_op = get_Conv_op(a);
3296 ir_node *b_op = get_Conv_op(b);
3297 ir_mode *a_mode = get_irn_mode(a_op);
3298 ir_mode *b_mode = get_irn_mode(b_op);
3299 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3300 ir_node *blk = get_nodes_block(n);
3303 set_binop_left(n, a_op);
3304 set_binop_right(n, b_op);
3305 set_irn_mode(n, a_mode);
3307 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3309 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3315 /* nothing to gain here */
3319 if (op == op_Shrs || op == op_Shr || op == op_Shl
3320 || op == op_And || op == op_Or || op == op_Eor) {
3321 ir_node *a_left = get_binop_left(a);
3322 ir_node *a_right = get_binop_right(a);
3323 ir_node *b_left = get_binop_left(b);
3324 ir_node *b_right = get_binop_right(b);
3326 ir_node *op1 = NULL;
3327 ir_node *op2 = NULL;
3329 if (is_op_commutative(op)) {
3330 if (a_left == b_left) {
3334 } else if(a_left == b_right) {
3338 } else if(a_right == b_left) {
3344 if(a_right == b_right) {
3351 /* (a sop c) & (b sop c) => (a & b) sop c */
3352 ir_node *blk = get_nodes_block(n);
3354 ir_node *new_n = exact_copy(n);
3355 set_binop_left(new_n, op1);
3356 set_binop_right(new_n, op2);
3357 new_n = trans_func(new_n);
3359 if(op_root == op_Eor && op == op_Or) {
3360 dbg_info *dbgi = get_irn_dbg_info(n);
3361 ir_mode *mode = get_irn_mode(c);
3363 c = new_rd_Not(dbgi, blk, c, mode);
3364 n = new_rd_And(dbgi, blk, new_n, c, mode);
3367 set_nodes_block(n, blk);
3368 set_binop_left(n, new_n);
3369 set_binop_right(n, c);
3370 add_identities(current_ir_graph->value_table, n);
3373 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3384 static ir_node *transform_node_And(ir_node *n) {
3385 ir_node *c, *oldn = n;
3386 ir_node *a = get_And_left(n);
3387 ir_node *b = get_And_right(n);
3390 mode = get_irn_mode(n);
3391 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3393 /* we can evaluate 2 Projs of the same Cmp */
3394 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3395 ir_node *pred_a = get_Proj_pred(a);
3396 ir_node *pred_b = get_Proj_pred(b);
3397 if (pred_a == pred_b) {
3398 dbg_info *dbgi = get_irn_dbg_info(n);
3399 ir_node *block = get_nodes_block(pred_a);
3400 pn_Cmp pn_a = get_Proj_proj(a);
3401 pn_Cmp pn_b = get_Proj_proj(b);
3402 /* yes, we can simply calculate with pncs */
3403 pn_Cmp new_pnc = pn_a & pn_b;
3405 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3410 ir_node *op = get_Not_op(b);
3412 ir_node *ba = get_And_left(op);
3413 ir_node *bb = get_And_right(op);
3415 /* it's enough to test the following cases due to normalization! */
3416 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3417 /* (a|b) & ~(a&b) = a^b */
3418 ir_node *block = get_nodes_block(n);
3420 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3421 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3429 ir_node *op = get_Not_op(a);
3431 ir_node *aa = get_And_left(op);
3432 ir_node *ab = get_And_right(op);
3434 /* it's enough to test the following cases due to normalization! */
3435 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3436 /* (a|b) & ~(a&b) = a^b */
3437 ir_node *block = get_nodes_block(n);
3439 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3440 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3447 ir_node *al = get_Eor_left(a);
3448 ir_node *ar = get_Eor_right(a);
3451 /* (b ^ a) & b -> ~a & b */
3452 dbg_info *dbg = get_irn_dbg_info(n);
3453 ir_node *block = get_nodes_block(n);
3455 ar = new_rd_Not(dbg, block, ar, mode);
3456 n = new_rd_And(dbg, block, ar, b, mode);
3457 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3461 /* (a ^ b) & b -> ~a & b */
3462 dbg_info *dbg = get_irn_dbg_info(n);
3463 ir_node *block = get_nodes_block(n);
3465 al = new_rd_Not(dbg, block, al, mode);
3466 n = new_rd_And(dbg, block, al, b, mode);
3467 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3472 ir_node *bl = get_Eor_left(b);
3473 ir_node *br = get_Eor_right(b);
3476 /* a & (a ^ b) -> a & ~b */
3477 dbg_info *dbg = get_irn_dbg_info(n);
3478 ir_node *block = get_nodes_block(n);
3480 br = new_rd_Not(dbg, block, br, mode);
3481 n = new_rd_And(dbg, block, br, a, mode);
3482 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3486 /* a & (b ^ a) -> a & ~b */
3487 dbg_info *dbg = get_irn_dbg_info(n);
3488 ir_node *block = get_nodes_block(n);
3490 bl = new_rd_Not(dbg, block, bl, mode);
3491 n = new_rd_And(dbg, block, bl, a, mode);
3492 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3496 if (is_Not(a) && is_Not(b)) {
3497 /* ~a & ~b = ~(a|b) */
3498 ir_node *block = get_nodes_block(n);
3499 ir_mode *mode = get_irn_mode(n);
3503 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3504 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3505 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3509 if (is_Const(a) && b->vrp.valid && (tarval_is_all_one(tarval_or(get_Const_tarval(a),
3510 b->vrp.bits_not_set)))) {
3511 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3512 b, get_irn_mode(n));
3516 if (is_Const(b) && a->vrp.valid && (tarval_is_all_one(tarval_or(get_Const_tarval(b),
3517 a->vrp.bits_not_set)))) {
3518 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3519 a, get_irn_mode(n));
3523 n = transform_bitwise_distributive(n, transform_node_And);
3526 } /* transform_node_And */
3531 static ir_node *transform_node_Eor(ir_node *n) {
3532 ir_node *c, *oldn = n;
3533 ir_node *a = get_Eor_left(n);
3534 ir_node *b = get_Eor_right(n);
3535 ir_mode *mode = get_irn_mode(n);
3537 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3539 /* we can evaluate 2 Projs of the same Cmp */
3540 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3541 ir_node *pred_a = get_Proj_pred(a);
3542 ir_node *pred_b = get_Proj_pred(b);
3543 if(pred_a == pred_b) {
3544 dbg_info *dbgi = get_irn_dbg_info(n);
3545 ir_node *block = get_nodes_block(pred_a);
3546 pn_Cmp pn_a = get_Proj_proj(a);
3547 pn_Cmp pn_b = get_Proj_proj(b);
3548 /* yes, we can simply calculate with pncs */
3549 pn_Cmp new_pnc = pn_a ^ pn_b;
3551 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3557 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph,
3558 get_mode_null(mode));
3559 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3560 } else if (is_Const(b)) {
3561 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3562 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(b)));
3563 ir_node *not_op = get_Not_op(a);
3564 dbg_info *dbg = get_irn_dbg_info(n);
3565 ir_node *block = get_nodes_block(n);
3566 ir_mode *mode = get_irn_mode(n);
3567 n = new_rd_Eor(dbg, block, not_op, cnst, mode);
3569 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3570 n = new_r_Not(get_nodes_block(n), a, mode);
3571 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3574 n = transform_bitwise_distributive(n, transform_node_Eor);
3578 } /* transform_node_Eor */
3583 static ir_node *transform_node_Not(ir_node *n) {
3584 ir_node *c, *oldn = n;
3585 ir_node *a = get_Not_op(n);
3586 ir_mode *mode = get_irn_mode(n);
3588 HANDLE_UNOP_PHI(tarval_not,a,c);
3590 /* check for a boolean Not */
3591 if (mode == mode_b && is_Proj(a)) {
3592 ir_node *a_pred = get_Proj_pred(a);
3593 if (is_Cmp(a_pred)) {
3594 ir_node *cmp_block = get_nodes_block(a_pred);
3595 /* We negate a Cmp. The Cmp has the negated result anyways! */
3596 n = new_r_Proj(cmp_block, get_Proj_pred(a),
3597 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3598 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3603 ir_node *eor_b = get_Eor_right(a);
3604 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3605 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(eor_b)));
3606 ir_node *eor_a = get_Eor_left(a);
3607 dbg_info *dbg = get_irn_dbg_info(n);
3608 ir_node *block = get_nodes_block(n);
3609 ir_mode *mode = get_irn_mode(n);
3610 n = new_rd_Eor(dbg, block, eor_a, cnst, mode);
3614 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3615 if (is_Minus(a)) { /* ~-x -> x + -1 */
3616 dbg_info *dbg = get_irn_dbg_info(n);
3617 ir_graph *irg = current_ir_graph;
3618 ir_node *block = get_nodes_block(n);
3619 ir_node *add_l = get_Minus_op(a);
3620 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3621 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3622 } else if (is_Add(a)) {
3623 ir_node *add_r = get_Add_right(a);
3624 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3625 /* ~(x + -1) = -x */
3626 ir_node *op = get_Add_left(a);
3627 ir_node *blk = get_nodes_block(n);
3628 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3629 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3634 } /* transform_node_Not */
3637 * Transform a Minus.
3641 * -(a >>u (size-1)) = a >>s (size-1)
3642 * -(a >>s (size-1)) = a >>u (size-1)
3643 * -(a * const) -> a * -const
3645 static ir_node *transform_node_Minus(ir_node *n) {
3646 ir_node *c, *oldn = n;
3647 ir_node *a = get_Minus_op(n);
3650 HANDLE_UNOP_PHI(tarval_neg,a,c);
3652 mode = get_irn_mode(a);
3653 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3654 /* the following rules are only to twos-complement */
3657 ir_node *op = get_Not_op(a);
3658 tarval *tv = get_mode_one(mode);
3659 ir_node *blk = get_nodes_block(n);
3660 ir_node *c = new_Const(tv);
3661 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3662 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3666 ir_node *c = get_Shr_right(a);
3669 tarval *tv = get_Const_tarval(c);
3671 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3672 /* -(a >>u (size-1)) = a >>s (size-1) */
3673 ir_node *v = get_Shr_left(a);
3675 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3676 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3682 ir_node *c = get_Shrs_right(a);
3685 tarval *tv = get_Const_tarval(c);
3687 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3688 /* -(a >>s (size-1)) = a >>u (size-1) */
3689 ir_node *v = get_Shrs_left(a);
3691 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3692 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3699 /* - (a-b) = b - a */
3700 ir_node *la = get_Sub_left(a);
3701 ir_node *ra = get_Sub_right(a);
3702 ir_node *blk = get_nodes_block(n);
3704 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3705 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3709 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3710 ir_node *mul_l = get_Mul_left(a);
3711 ir_node *mul_r = get_Mul_right(a);
3712 tarval *tv = value_of(mul_r);
3713 if (tv != tarval_bad) {
3714 tv = tarval_neg(tv);
3715 if (tv != tarval_bad) {
3716 ir_node *cnst = new_Const(tv);
3717 dbg_info *dbg = get_irn_dbg_info(a);
3718 ir_node *block = get_nodes_block(a);
3719 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3720 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3727 } /* transform_node_Minus */
3730 * Transform a Cast_type(Const) into a new Const_type
3732 static ir_node *transform_node_Cast(ir_node *n) {
3734 ir_node *pred = get_Cast_op(n);
3735 ir_type *tp = get_irn_type(n);
3737 if (is_Const(pred) && get_Const_type(pred) != tp) {
3738 n = new_rd_Const_type(NULL, current_ir_graph, get_Const_tarval(pred), tp);
3739 DBG_OPT_CSTEVAL(oldn, n);
3740 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3741 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_mode(pred),
3742 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3743 DBG_OPT_CSTEVAL(oldn, n);
3747 } /* transform_node_Cast */
3750 * Transform a Proj(Load) with a non-null address.
3752 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3753 if (get_opt_ldst_only_null_ptr_exceptions()) {
3754 if (get_irn_mode(proj) == mode_X) {
3755 ir_node *load = get_Proj_pred(proj);
3757 /* get the Load address */
3758 const ir_node *addr = get_Load_ptr(load);
3759 const ir_node *confirm;
3761 if (value_not_null(addr, &confirm)) {
3762 if (confirm == NULL) {
3763 /* this node may float if it did not depend on a Confirm */
3764 set_irn_pinned(load, op_pin_state_floats);
3766 if (get_Proj_proj(proj) == pn_Load_X_except) {
3767 DBG_OPT_EXC_REM(proj);
3768 return get_irg_bad(current_ir_graph);
3770 ir_node *blk = get_nodes_block(load);
3771 return new_r_Jmp(blk);
3777 } /* transform_node_Proj_Load */
3780 * Transform a Proj(Store) with a non-null address.
3782 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3783 if (get_opt_ldst_only_null_ptr_exceptions()) {
3784 if (get_irn_mode(proj) == mode_X) {
3785 ir_node *store = get_Proj_pred(proj);
3787 /* get the load/store address */
3788 const ir_node *addr = get_Store_ptr(store);
3789 const ir_node *confirm;
3791 if (value_not_null(addr, &confirm)) {
3792 if (confirm == NULL) {
3793 /* this node may float if it did not depend on a Confirm */
3794 set_irn_pinned(store, op_pin_state_floats);
3796 if (get_Proj_proj(proj) == pn_Store_X_except) {
3797 DBG_OPT_EXC_REM(proj);
3798 return get_irg_bad(current_ir_graph);
3800 ir_node *blk = get_nodes_block(store);
3801 return new_r_Jmp(blk);
3807 } /* transform_node_Proj_Store */
3810 * Transform a Proj(Div) with a non-zero value.
3811 * Removes the exceptions and routes the memory to the NoMem node.
3813 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3814 ir_node *div = get_Proj_pred(proj);
3815 ir_node *b = get_Div_right(div);
3816 ir_node *res, *new_mem;
3817 const ir_node *confirm;
3820 if (value_not_zero(b, &confirm)) {
3821 /* div(x, y) && y != 0 */
3822 if (confirm == NULL) {
3823 /* we are sure we have a Const != 0 */
3824 new_mem = get_Div_mem(div);
3825 new_mem = skip_Pin(new_mem);
3826 set_Div_mem(div, new_mem);
3827 set_irn_pinned(div, op_pin_state_floats);
3830 proj_nr = get_Proj_proj(proj);
3832 case pn_Div_X_regular:
3833 return new_r_Jmp(get_nodes_block(div));
3835 case pn_Div_X_except:
3836 /* we found an exception handler, remove it */
3837 DBG_OPT_EXC_REM(proj);
3841 res = get_Div_mem(div);
3842 new_mem = get_irg_no_mem(current_ir_graph);
3845 /* This node can only float up to the Confirm block */
3846 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3848 set_irn_pinned(div, op_pin_state_floats);
3849 /* this is a Div without exception, we can remove the memory edge */
3850 set_Div_mem(div, new_mem);
3855 } /* transform_node_Proj_Div */
3858 * Transform a Proj(Mod) with a non-zero value.
3859 * Removes the exceptions and routes the memory to the NoMem node.
3861 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3862 ir_node *mod = get_Proj_pred(proj);
3863 ir_node *b = get_Mod_right(mod);
3864 ir_node *res, *new_mem;
3865 const ir_node *confirm;
3868 if (value_not_zero(b, &confirm)) {
3869 /* mod(x, y) && y != 0 */
3870 proj_nr = get_Proj_proj(proj);
3872 if (confirm == NULL) {
3873 /* we are sure we have a Const != 0 */
3874 new_mem = get_Mod_mem(mod);
3875 new_mem = skip_Pin(new_mem);
3876 set_Mod_mem(mod, new_mem);
3877 set_irn_pinned(mod, op_pin_state_floats);
3882 case pn_Mod_X_regular:
3883 return new_r_Jmp(get_irn_n(mod, -1));
3885 case pn_Mod_X_except:
3886 /* we found an exception handler, remove it */
3887 DBG_OPT_EXC_REM(proj);
3891 res = get_Mod_mem(mod);
3892 new_mem = get_irg_no_mem(current_ir_graph);
3895 /* This node can only float up to the Confirm block */
3896 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3898 /* this is a Mod without exception, we can remove the memory edge */
3899 set_Mod_mem(mod, new_mem);
3902 if (get_Mod_left(mod) == b) {
3903 /* a % a = 0 if a != 0 */
3904 ir_mode *mode = get_irn_mode(proj);
3905 ir_node *res = new_Const(get_mode_null(mode));
3907 DBG_OPT_CSTEVAL(mod, res);
3913 } /* transform_node_Proj_Mod */
3916 * Transform a Proj(DivMod) with a non-zero value.
3917 * Removes the exceptions and routes the memory to the NoMem node.
3919 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3920 ir_node *divmod = get_Proj_pred(proj);
3921 ir_node *b = get_DivMod_right(divmod);
3922 ir_node *res, *new_mem;
3923 const ir_node *confirm;
3926 if (value_not_zero(b, &confirm)) {
3927 /* DivMod(x, y) && y != 0 */
3928 proj_nr = get_Proj_proj(proj);
3930 if (confirm == NULL) {
3931 /* we are sure we have a Const != 0 */
3932 new_mem = get_DivMod_mem(divmod);
3933 new_mem = skip_Pin(new_mem);
3934 set_DivMod_mem(divmod, new_mem);
3935 set_irn_pinned(divmod, op_pin_state_floats);
3940 case pn_DivMod_X_regular:
3941 return new_r_Jmp(get_nodes_block(divmod));
3943 case pn_DivMod_X_except:
3944 /* we found an exception handler, remove it */
3945 DBG_OPT_EXC_REM(proj);
3949 res = get_DivMod_mem(divmod);
3950 new_mem = get_irg_no_mem(current_ir_graph);
3953 /* This node can only float up to the Confirm block */
3954 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3956 /* this is a DivMod without exception, we can remove the memory edge */
3957 set_DivMod_mem(divmod, new_mem);
3960 case pn_DivMod_res_mod:
3961 if (get_DivMod_left(divmod) == b) {
3962 /* a % a = 0 if a != 0 */
3963 ir_mode *mode = get_irn_mode(proj);
3964 ir_node *res = new_Const(get_mode_null(mode));
3966 DBG_OPT_CSTEVAL(divmod, res);
3972 } /* transform_node_Proj_DivMod */
3975 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3977 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3978 if (get_opt_unreachable_code()) {
3979 ir_node *n = get_Proj_pred(proj);
3980 ir_node *b = get_Cond_selector(n);
3982 if (mode_is_int(get_irn_mode(b))) {
3983 tarval *tb = value_of(b);
3985 if (tb != tarval_bad) {
3986 /* we have a constant switch */
3987 long num = get_Proj_proj(proj);
3989 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3990 if (get_tarval_long(tb) == num) {
3991 /* Do NOT create a jump here, or we will have 2 control flow ops
3992 * in a block. This case is optimized away in optimize_cf(). */
3995 /* this case will NEVER be taken, kill it */
3996 return get_irg_bad(current_ir_graph);
4000 long num = get_Proj_proj(proj);
4001 if (num != get_Cond_default_proj(n) && b->vrp.valid) {
4002 /* Try handling with vrp data. We only remove dead parts. */
4003 tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4005 if (b->vrp.range_type == VRP_RANGE) {
4006 pn_Cmp cmp_result = tarval_cmp(b->vrp.range_bottom, tp);
4007 pn_Cmp cmp_result2 = tarval_cmp(b->vrp.range_top, tp);
4009 if ((cmp_result & pn_Cmp_Lt) == cmp_result && (cmp_result2
4010 & pn_Cmp_Gt) == cmp_result2) {
4011 return get_irg_bad(current_ir_graph);
4013 } else if (b->vrp.range_type == VRP_ANTIRANGE) {
4014 pn_Cmp cmp_result = tarval_cmp(b->vrp.range_bottom, tp);
4015 pn_Cmp cmp_result2 = tarval_cmp(b->vrp.range_top, tp);
4017 if ((cmp_result & pn_Cmp_Ge) == cmp_result && (cmp_result2
4018 & pn_Cmp_Le) == cmp_result2) {
4019 return get_irg_bad(current_ir_graph);
4024 tarval_and( b->vrp.bits_set, tp),
4028 return get_irg_bad(current_ir_graph);
4034 b->vrp.bits_not_set),
4035 b->vrp.bits_not_set)
4038 return get_irg_bad(current_ir_graph);
4047 } /* transform_node_Proj_Cond */
4050 * Create a 0 constant of given mode.
4052 static ir_node *create_zero_const(ir_mode *mode) {
4053 tarval *tv = get_mode_null(mode);
4054 ir_node *cnst = new_Const(tv);
4059 /* the order of the values is important! */
4060 typedef enum const_class {
4066 static const_class classify_const(const ir_node* n)
4068 if (is_Const(n)) return const_const;
4069 if (is_irn_constlike(n)) return const_like;
4074 * Determines whether r is more constlike or has a larger index (in that order)
4077 static int operands_are_normalized(const ir_node *l, const ir_node *r)
4079 const const_class l_order = classify_const(l);
4080 const const_class r_order = classify_const(r);
4082 l_order > r_order ||
4083 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4087 * Normalizes and optimizes Cmp nodes.
4089 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
4090 ir_node *n = get_Proj_pred(proj);
4091 ir_node *left = get_Cmp_left(n);
4092 ir_node *right = get_Cmp_right(n);
4095 ir_mode *mode = NULL;
4096 long proj_nr = get_Proj_proj(proj);
4098 /* we can evaluate some cases directly */
4101 return new_Const(get_tarval_b_false());
4103 return new_Const(get_tarval_b_true());
4105 if (!mode_is_float(get_irn_mode(left)))
4106 return new_Const(get_tarval_b_true());
4112 /* remove Casts of both sides */
4113 left = skip_Cast(left);
4114 right = skip_Cast(right);
4116 /* Remove unnecessary conversions */
4117 /* TODO handle constants */
4118 if (is_Conv(left) && is_Conv(right)) {
4119 ir_mode *mode = get_irn_mode(left);
4120 ir_node *op_left = get_Conv_op(left);
4121 ir_node *op_right = get_Conv_op(right);
4122 ir_mode *mode_left = get_irn_mode(op_left);
4123 ir_mode *mode_right = get_irn_mode(op_right);
4125 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4126 && mode_left != mode_b && mode_right != mode_b) {
4127 ir_node *block = get_nodes_block(n);
4129 if (mode_left == mode_right) {
4133 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4134 } else if (smaller_mode(mode_left, mode_right)) {
4135 left = new_r_Conv(block, op_left, mode_right);
4138 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4139 } else if (smaller_mode(mode_right, mode_left)) {
4141 right = new_r_Conv(block, op_right, mode_left);
4143 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4148 /* remove operation on both sides if possible */
4149 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4151 * The following operations are NOT safe for floating point operations, for instance
4152 * 1.0 + inf == 2.0 + inf, =/=> x == y
4154 if (mode_is_int(get_irn_mode(left))) {
4155 unsigned lop = get_irn_opcode(left);
4157 if (lop == get_irn_opcode(right)) {
4158 ir_node *ll, *lr, *rl, *rr;
4160 /* same operation on both sides, try to remove */
4164 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4165 left = get_unop_op(left);
4166 right = get_unop_op(right);
4168 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4171 ll = get_Add_left(left);
4172 lr = get_Add_right(left);
4173 rl = get_Add_left(right);
4174 rr = get_Add_right(right);
4177 /* X + a CMP X + b ==> a CMP b */
4181 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4182 } else if (ll == rr) {
4183 /* X + a CMP b + X ==> a CMP b */
4187 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4188 } else if (lr == rl) {
4189 /* a + X CMP X + b ==> a CMP b */
4193 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4194 } else if (lr == rr) {
4195 /* a + X CMP b + X ==> a CMP b */
4199 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4203 ll = get_Sub_left(left);
4204 lr = get_Sub_right(left);
4205 rl = get_Sub_left(right);
4206 rr = get_Sub_right(right);
4209 /* X - a CMP X - b ==> a CMP b */
4213 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4214 } else if (lr == rr) {
4215 /* a - X CMP b - X ==> a CMP b */
4219 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4223 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4224 /* a ROTL X CMP b ROTL X ==> a CMP b */
4225 left = get_Rotl_left(left);
4226 right = get_Rotl_left(right);
4228 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4236 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4237 if (is_Add(left) || is_Sub(left)) {
4238 ir_node *ll = get_binop_left(left);
4239 ir_node *lr = get_binop_right(left);
4241 if (lr == right && is_Add(left)) {
4248 right = create_zero_const(get_irn_mode(left));
4250 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4253 if (is_Add(right) || is_Sub(right)) {
4254 ir_node *rl = get_binop_left(right);
4255 ir_node *rr = get_binop_right(right);
4257 if (rr == left && is_Add(right)) {
4264 right = create_zero_const(get_irn_mode(left));
4266 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4269 if (is_And(left) && is_Const(right)) {
4270 ir_node *ll = get_binop_left(left);
4271 ir_node *lr = get_binop_right(left);
4272 if (is_Shr(ll) && is_Const(lr)) {
4273 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4274 ir_node *block = get_nodes_block(n);
4275 ir_mode *mode = get_irn_mode(left);
4277 ir_node *llr = get_Shr_right(ll);
4278 if (is_Const(llr)) {
4279 dbg_info *dbg = get_irn_dbg_info(left);
4281 tarval *c1 = get_Const_tarval(llr);
4282 tarval *c2 = get_Const_tarval(lr);
4283 tarval *c3 = get_Const_tarval(right);
4284 tarval *mask = tarval_shl(c2, c1);
4285 tarval *value = tarval_shl(c3, c1);
4287 left = new_rd_And(dbg, block, get_Shr_left(ll), new_Const(mask), mode);
4288 right = new_Const(value);
4293 } /* mode_is_int(...) */
4294 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4296 /* replace mode_b compares with ands/ors */
4297 if (get_irn_mode(left) == mode_b) {
4298 ir_node *block = get_nodes_block(n);
4302 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4303 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4304 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4305 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4306 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4307 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4308 default: bres = NULL;
4311 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4317 * First step: normalize the compare op
4318 * by placing the constant on the right side
4319 * or moving the lower address node to the left.
4321 if (!operands_are_normalized(left, right)) {
4327 proj_nr = get_inversed_pnc(proj_nr);
4332 * Second step: Try to reduce the magnitude
4333 * of a constant. This may help to generate better code
4334 * later and may help to normalize more compares.
4335 * Of course this is only possible for integer values.
4337 tv = value_of(right);
4338 if (tv != tarval_bad) {
4339 mode = get_irn_mode(right);
4341 /* TODO extend to arbitrary constants */
4342 if (is_Conv(left) && tarval_is_null(tv)) {
4343 ir_node *op = get_Conv_op(left);
4344 ir_mode *op_mode = get_irn_mode(op);
4347 * UpConv(x) REL 0 ==> x REL 0
4348 * Don't do this for float values as it's unclear whether it is a
4349 * win. (on the other side it makes detection/creation of fabs hard)
4351 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4352 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4353 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4354 !mode_is_float(mode)) {
4355 tv = get_mode_null(op_mode);
4359 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4363 if (tv != tarval_bad) {
4364 /* the following optimization is possible on modes without Overflow
4365 * on Unary Minus or on == and !=:
4366 * -a CMP c ==> a swap(CMP) -c
4368 * Beware: for two-complement Overflow may occur, so only == and != can
4369 * be optimized, see this:
4370 * -MININT < 0 =/=> MININT > 0 !!!
4372 if (is_Minus(left) &&
4373 (!mode_overflow_on_unary_Minus(mode) ||
4374 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4375 tv = tarval_neg(tv);
4377 if (tv != tarval_bad) {
4378 left = get_Minus_op(left);
4379 proj_nr = get_inversed_pnc(proj_nr);
4381 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4383 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4384 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4385 tv = tarval_not(tv);
4387 if (tv != tarval_bad) {
4388 left = get_Not_op(left);
4390 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4394 /* for integer modes, we have more */
4395 if (mode_is_int(mode)) {
4396 /* Ne includes Unordered which is not possible on integers.
4397 * However, frontends often use this wrong, so fix it here */
4398 if (proj_nr & pn_Cmp_Uo) {
4399 proj_nr &= ~pn_Cmp_Uo;
4400 set_Proj_proj(proj, proj_nr);
4403 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4404 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4405 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4406 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4408 if (tv != tarval_bad) {
4409 proj_nr ^= pn_Cmp_Eq;
4411 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4414 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4415 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4416 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4417 tv = tarval_add(tv, get_mode_one(mode));
4419 if (tv != tarval_bad) {
4420 proj_nr ^= pn_Cmp_Eq;
4422 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4426 /* the following reassociations work only for == and != */
4427 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4429 #if 0 /* Might be not that good in general */
4430 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4431 if (tarval_is_null(tv) && is_Sub(left)) {
4432 right = get_Sub_right(left);
4433 left = get_Sub_left(left);
4435 tv = value_of(right);
4437 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4441 if (tv != tarval_bad) {
4442 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4444 ir_node *c1 = get_Sub_right(left);
4445 tarval *tv2 = value_of(c1);
4447 if (tv2 != tarval_bad) {
4448 tv2 = tarval_add(tv, value_of(c1));
4450 if (tv2 != tarval_bad) {
4451 left = get_Sub_left(left);
4454 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4458 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4459 else if (is_Add(left)) {
4460 ir_node *a_l = get_Add_left(left);
4461 ir_node *a_r = get_Add_right(left);
4465 if (is_Const(a_l)) {
4467 tv2 = value_of(a_l);
4470 tv2 = value_of(a_r);
4473 if (tv2 != tarval_bad) {
4474 tv2 = tarval_sub(tv, tv2, NULL);
4476 if (tv2 != tarval_bad) {
4480 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4484 /* -a == c ==> a == -c, -a != c ==> a != -c */
4485 else if (is_Minus(left)) {
4486 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4488 if (tv2 != tarval_bad) {
4489 left = get_Minus_op(left);
4492 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4497 /* the following reassociations work only for <= */
4498 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4499 if (tv != tarval_bad) {
4500 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4501 if (is_Abs(left)) { // TODO something is missing here
4507 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4508 switch (get_irn_opcode(left)) {
4512 c1 = get_And_right(left);
4515 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4516 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4518 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4520 /* TODO: move to constant evaluation */
4521 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4523 DBG_OPT_CSTEVAL(proj, c1);
4527 if (tarval_is_single_bit(tv)) {
4529 * optimization for AND:
4531 * And(x, C) == C ==> And(x, C) != 0
4532 * And(x, C) != C ==> And(X, C) == 0
4534 * if C is a single Bit constant.
4537 /* check for Constant's match. We have check hare the tarvals,
4538 because our const might be changed */
4539 if (get_Const_tarval(c1) == tv) {
4540 /* fine: do the transformation */
4541 tv = get_mode_null(get_tarval_mode(tv));
4542 proj_nr ^= pn_Cmp_Leg;
4544 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4550 c1 = get_Or_right(left);
4551 if (is_Const(c1) && tarval_is_null(tv)) {
4553 * Or(x, C) == 0 && C != 0 ==> FALSE
4554 * Or(x, C) != 0 && C != 0 ==> TRUE
4556 if (! tarval_is_null(get_Const_tarval(c1))) {
4557 /* TODO: move to constant evaluation */
4558 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4560 DBG_OPT_CSTEVAL(proj, c1);
4567 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4569 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4572 c1 = get_Shl_right(left);
4574 tarval *tv1 = get_Const_tarval(c1);
4575 ir_mode *mode = get_irn_mode(left);
4576 tarval *minus1 = get_mode_all_one(mode);
4577 tarval *amask = tarval_shr(minus1, tv1);
4578 tarval *cmask = tarval_shl(minus1, tv1);
4581 if (tarval_and(tv, cmask) != tv) {
4582 /* condition not met */
4583 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4585 DBG_OPT_CSTEVAL(proj, c1);
4588 sl = get_Shl_left(left);
4589 blk = get_nodes_block(n);
4590 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4591 tv = tarval_shr(tv, tv1);
4593 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4598 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4600 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4603 c1 = get_Shr_right(left);
4605 tarval *tv1 = get_Const_tarval(c1);
4606 ir_mode *mode = get_irn_mode(left);
4607 tarval *minus1 = get_mode_all_one(mode);
4608 tarval *amask = tarval_shl(minus1, tv1);
4609 tarval *cmask = tarval_shr(minus1, tv1);
4612 if (tarval_and(tv, cmask) != tv) {
4613 /* condition not met */
4614 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4616 DBG_OPT_CSTEVAL(proj, c1);
4619 sl = get_Shr_left(left);
4620 blk = get_nodes_block(n);
4621 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4622 tv = tarval_shl(tv, tv1);
4624 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4629 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4631 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4634 c1 = get_Shrs_right(left);
4636 tarval *tv1 = get_Const_tarval(c1);
4637 ir_mode *mode = get_irn_mode(left);
4638 tarval *minus1 = get_mode_all_one(mode);
4639 tarval *amask = tarval_shl(minus1, tv1);
4640 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4643 cond = tarval_sub(cond, tv1, NULL);
4644 cond = tarval_shrs(tv, cond);
4646 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4647 /* condition not met */
4648 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4650 DBG_OPT_CSTEVAL(proj, c1);
4653 sl = get_Shrs_left(left);
4654 blk = get_nodes_block(n);
4655 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4656 tv = tarval_shl(tv, tv1);
4658 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4663 } /* tarval != bad */
4666 if (changed & 2) /* need a new Const */
4667 right = new_Const(tv);
4669 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4670 ir_node *op = get_Proj_pred(left);
4672 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4673 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4674 ir_node *c = get_binop_right(op);
4677 tarval *tv = get_Const_tarval(c);
4679 if (tarval_is_single_bit(tv)) {
4680 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4681 ir_node *v = get_binop_left(op);
4682 ir_node *blk = get_irn_n(op, -1);
4683 ir_mode *mode = get_irn_mode(v);
4685 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4686 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_Const(tv), mode);
4688 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4695 ir_node *block = get_nodes_block(n);
4697 /* create a new compare */
4698 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4699 proj = new_rd_Proj(get_irn_dbg_info(proj), block, n, get_irn_mode(proj), proj_nr);
4703 } /* transform_node_Proj_Cmp */
4706 * Optimize CopyB(mem, x, x) into a Nop.
4708 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4709 ir_node *copyb = get_Proj_pred(proj);
4710 ir_node *a = get_CopyB_dst(copyb);
4711 ir_node *b = get_CopyB_src(copyb);
4714 switch (get_Proj_proj(proj)) {
4715 case pn_CopyB_X_regular:
4716 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4717 DBG_OPT_EXC_REM(proj);
4718 proj = new_r_Jmp(get_nodes_block(copyb));
4720 case pn_CopyB_X_except:
4721 DBG_OPT_EXC_REM(proj);
4722 proj = get_irg_bad(get_irn_irg(proj));
4729 } /* transform_node_Proj_CopyB */
4732 * Optimize Bounds(idx, idx, upper) into idx.
4734 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4735 ir_node *oldn = proj;
4736 ir_node *bound = get_Proj_pred(proj);
4737 ir_node *idx = get_Bound_index(bound);
4738 ir_node *pred = skip_Proj(idx);
4741 if (idx == get_Bound_lower(bound))
4743 else if (is_Bound(pred)) {
4745 * idx was Bounds checked in the same MacroBlock previously,
4746 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4748 ir_node *lower = get_Bound_lower(bound);
4749 ir_node *upper = get_Bound_upper(bound);
4750 if (get_Bound_lower(pred) == lower &&
4751 get_Bound_upper(pred) == upper &&
4752 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4754 * One could expect that we simply return the previous
4755 * Bound here. However, this would be wrong, as we could
4756 * add an exception Proj to a new location then.
4757 * So, we must turn in into a tuple.
4763 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4764 switch (get_Proj_proj(proj)) {
4766 DBG_OPT_EXC_REM(proj);
4767 proj = get_Bound_mem(bound);
4769 case pn_Bound_X_except:
4770 DBG_OPT_EXC_REM(proj);
4771 proj = get_irg_bad(get_irn_irg(proj));
4775 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4777 case pn_Bound_X_regular:
4778 DBG_OPT_EXC_REM(proj);
4779 proj = new_r_Jmp(get_nodes_block(bound));
4786 } /* transform_node_Proj_Bound */
4789 * Does all optimizations on nodes that must be done on it's Proj's
4790 * because of creating new nodes.
4792 static ir_node *transform_node_Proj(ir_node *proj) {
4793 ir_node *n = get_Proj_pred(proj);
4795 if (n->op->ops.transform_node_Proj)
4796 return n->op->ops.transform_node_Proj(proj);
4798 } /* transform_node_Proj */
4801 * Move Confirms down through Phi nodes.
4803 static ir_node *transform_node_Phi(ir_node *phi) {
4805 ir_mode *mode = get_irn_mode(phi);
4807 if (mode_is_reference(mode)) {
4808 n = get_irn_arity(phi);
4810 /* Beware of Phi0 */
4812 ir_node *pred = get_irn_n(phi, 0);
4813 ir_node *bound, *new_Phi, *block, **in;
4816 if (! is_Confirm(pred))
4819 bound = get_Confirm_bound(pred);
4820 pnc = get_Confirm_cmp(pred);
4822 NEW_ARR_A(ir_node *, in, n);
4823 in[0] = get_Confirm_value(pred);
4825 for (i = 1; i < n; ++i) {
4826 pred = get_irn_n(phi, i);
4828 if (! is_Confirm(pred) ||
4829 get_Confirm_bound(pred) != bound ||
4830 get_Confirm_cmp(pred) != pnc)
4832 in[i] = get_Confirm_value(pred);
4834 /* move the Confirm nodes "behind" the Phi */
4835 block = get_irn_n(phi, -1);
4836 new_Phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4837 return new_r_Confirm(block, new_Phi, bound, pnc);
4841 } /* transform_node_Phi */
4844 * Returns the operands of a commutative bin-op, if one operand is
4845 * a const, it is returned as the second one.
4847 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4848 ir_node *op_a = get_binop_left(binop);
4849 ir_node *op_b = get_binop_right(binop);
4851 assert(is_op_commutative(get_irn_op(binop)));
4853 if (is_Const(op_a)) {
4860 } /* get_comm_Binop_Ops */
4863 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4864 * Such pattern may arise in bitfield stores.
4866 * value c4 value c4 & c2
4867 * AND c3 AND c1 | c3
4874 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4877 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4880 ir_node *and_l, *c3;
4881 ir_node *value, *c4;
4882 ir_node *new_and, *new_const, *block;
4883 ir_mode *mode = get_irn_mode(or);
4885 tarval *tv1, *tv2, *tv3, *tv4, *tv;
4888 get_comm_Binop_Ops(or, &and, &c1);
4889 if (!is_Const(c1) || !is_And(and))
4892 get_comm_Binop_Ops(and, &or_l, &c2);
4896 tv1 = get_Const_tarval(c1);
4897 tv2 = get_Const_tarval(c2);
4899 tv = tarval_or(tv1, tv2);
4900 if (tarval_is_all_one(tv)) {
4901 /* the AND does NOT clear a bit with isn't set by the OR */
4902 set_Or_left(or, or_l);
4903 set_Or_right(or, c1);
4905 /* check for more */
4912 get_comm_Binop_Ops(or_l, &and_l, &c3);
4913 if (!is_Const(c3) || !is_And(and_l))
4916 get_comm_Binop_Ops(and_l, &value, &c4);
4920 /* ok, found the pattern, check for conditions */
4921 assert(mode == get_irn_mode(and));
4922 assert(mode == get_irn_mode(or_l));
4923 assert(mode == get_irn_mode(and_l));
4925 tv3 = get_Const_tarval(c3);
4926 tv4 = get_Const_tarval(c4);
4928 tv = tarval_or(tv4, tv2);
4929 if (!tarval_is_all_one(tv)) {
4930 /* have at least one 0 at the same bit position */
4934 if (tv3 != tarval_andnot(tv3, tv4)) {
4935 /* bit in the or_mask is outside the and_mask */
4939 if (tv1 != tarval_andnot(tv1, tv2)) {
4940 /* bit in the or_mask is outside the and_mask */
4944 /* ok, all conditions met */
4945 block = get_irn_n(or, -1);
4947 new_and = new_r_And(block, value, new_Const(tarval_and(tv4, tv2)), mode);
4949 new_const = new_Const(tarval_or(tv3, tv1));
4951 set_Or_left(or, new_and);
4952 set_Or_right(or, new_const);
4954 /* check for more */
4956 } /* transform_node_Or_bf_store */
4959 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4961 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4962 ir_mode *mode = get_irn_mode(or);
4963 ir_node *shl, *shr, *block;
4964 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4967 if (! mode_is_int(mode))
4970 shl = get_binop_left(or);
4971 shr = get_binop_right(or);
4980 } else if (!is_Shl(shl)) {
4982 } else if (!is_Shr(shr)) {
4985 x = get_Shl_left(shl);
4986 if (x != get_Shr_left(shr))
4989 c1 = get_Shl_right(shl);
4990 c2 = get_Shr_right(shr);
4991 if (is_Const(c1) && is_Const(c2)) {
4992 tv1 = get_Const_tarval(c1);
4993 if (! tarval_is_long(tv1))
4996 tv2 = get_Const_tarval(c2);
4997 if (! tarval_is_long(tv2))
5000 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5001 != (int) get_mode_size_bits(mode))
5004 /* yet, condition met */
5005 block = get_nodes_block(or);
5007 n = new_r_Rotl(block, x, c1, mode);
5009 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5016 rotval = sub; /* a Rot right is not supported, so use a rot left */
5017 } else if (is_Sub(c2)) {
5023 if (get_Sub_right(sub) != v)
5026 c1 = get_Sub_left(sub);
5030 tv1 = get_Const_tarval(c1);
5031 if (! tarval_is_long(tv1))
5034 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
5037 /* yet, condition met */
5038 block = get_nodes_block(or);
5040 n = new_r_Rotl(block, x, rotval, mode);
5042 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
5044 } /* transform_node_Or_Rotl */
5049 static ir_node *transform_node_Or(ir_node *n) {
5050 ir_node *c, *oldn = n;
5051 ir_node *a = get_Or_left(n);
5052 ir_node *b = get_Or_right(n);
5055 if (is_Not(a) && is_Not(b)) {
5056 /* ~a | ~b = ~(a&b) */
5057 ir_node *block = get_nodes_block(n);
5059 mode = get_irn_mode(n);
5062 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5063 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5064 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5068 /* we can evaluate 2 Projs of the same Cmp */
5069 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5070 ir_node *pred_a = get_Proj_pred(a);
5071 ir_node *pred_b = get_Proj_pred(b);
5072 if (pred_a == pred_b) {
5073 dbg_info *dbgi = get_irn_dbg_info(n);
5074 ir_node *block = get_nodes_block(pred_a);
5075 pn_Cmp pn_a = get_Proj_proj(a);
5076 pn_Cmp pn_b = get_Proj_proj(b);
5077 /* yes, we can simply calculate with pncs */
5078 pn_Cmp new_pnc = pn_a | pn_b;
5080 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
5084 mode = get_irn_mode(n);
5085 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5087 n = transform_node_Or_bf_store(n);
5088 n = transform_node_Or_Rotl(n);
5092 n = transform_bitwise_distributive(n, transform_node_Or);
5095 } /* transform_node_Or */
5099 static ir_node *transform_node(ir_node *n);
5102 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5104 * Should be moved to reassociation?
5106 static ir_node *transform_node_shift(ir_node *n) {
5107 ir_node *left, *right;
5109 tarval *tv1, *tv2, *res;
5110 ir_node *in[2], *irn, *block;
5112 left = get_binop_left(n);
5114 /* different operations */
5115 if (get_irn_op(left) != get_irn_op(n))
5118 right = get_binop_right(n);
5119 tv1 = value_of(right);
5120 if (tv1 == tarval_bad)
5123 tv2 = value_of(get_binop_right(left));
5124 if (tv2 == tarval_bad)
5127 res = tarval_add(tv1, tv2);
5128 mode = get_irn_mode(n);
5130 /* beware: a simple replacement works only, if res < modulo shift */
5132 int modulo_shf = get_mode_modulo_shift(mode);
5133 if (modulo_shf > 0) {
5134 tarval *modulo = new_tarval_from_long(modulo_shf,
5135 get_tarval_mode(res));
5137 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5139 /* shifting too much */
5140 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5142 ir_node *block = get_nodes_block(n);
5143 dbg_info *dbgi = get_irn_dbg_info(n);
5144 ir_mode *smode = get_irn_mode(right);
5145 ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5146 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5149 return new_Const(get_mode_null(mode));
5153 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5156 /* ok, we can replace it */
5157 block = get_nodes_block(n);
5159 in[0] = get_binop_left(left);
5160 in[1] = new_Const(res);
5162 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5164 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5166 return transform_node(irn);
5167 } /* transform_node_shift */
5170 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5172 * - and, or, xor instead of &
5173 * - Shl, Shr, Shrs, rotl instead of >>
5174 * (with a special case for Or/Xor + Shrs)
5176 static ir_node *transform_node_bitop_shift(ir_node *n) {
5178 ir_node *right = get_binop_right(n);
5179 ir_mode *mode = get_irn_mode(n);
5180 ir_node *bitop_left;
5181 ir_node *bitop_right;
5192 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5194 if (!is_Const(right))
5197 left = get_binop_left(n);
5198 op_left = get_irn_op(left);
5199 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5202 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5203 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5204 /* TODO: test if sign bit is affectes */
5208 bitop_right = get_binop_right(left);
5209 if (!is_Const(bitop_right))
5212 bitop_left = get_binop_left(left);
5214 block = get_nodes_block(n);
5215 dbgi = get_irn_dbg_info(n);
5216 tv1 = get_Const_tarval(bitop_right);
5217 tv2 = get_Const_tarval(right);
5219 assert(get_tarval_mode(tv1) == mode);
5222 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5223 tv_shift = tarval_shl(tv1, tv2);
5224 } else if(is_Shr(n)) {
5225 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5226 tv_shift = tarval_shr(tv1, tv2);
5227 } else if(is_Shrs(n)) {
5228 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5229 tv_shift = tarval_shrs(tv1, tv2);
5232 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5233 tv_shift = tarval_rotl(tv1, tv2);
5236 assert(get_tarval_mode(tv_shift) == mode);
5237 new_const = new_Const(tv_shift);
5239 if (op_left == op_And) {
5240 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5241 } else if(op_left == op_Or) {
5242 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5244 assert(op_left == op_Eor);
5245 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5253 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5255 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5256 * (also with x >>s c1 when c1>=c2)
5258 static ir_node *transform_node_shl_shr(ir_node *n) {
5260 ir_node *right = get_binop_right(n);
5276 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5278 if (!is_Const(right))
5281 left = get_binop_left(n);
5282 mode = get_irn_mode(n);
5283 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5284 ir_node *shr_right = get_binop_right(left);
5286 if (!is_Const(shr_right))
5289 x = get_binop_left(left);
5290 tv_shr = get_Const_tarval(shr_right);
5291 tv_shl = get_Const_tarval(right);
5293 if (is_Shrs(left)) {
5294 /* shrs variant only allowed if c1 >= c2 */
5295 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5298 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5301 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5303 tv_mask = tarval_shl(tv_mask, tv_shl);
5304 } else if(is_Shr(n) && is_Shl(left)) {
5305 ir_node *shl_right = get_Shl_right(left);
5307 if (!is_Const(shl_right))
5310 x = get_Shl_left(left);
5311 tv_shr = get_Const_tarval(right);
5312 tv_shl = get_Const_tarval(shl_right);
5314 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5315 tv_mask = tarval_shr(tv_mask, tv_shr);
5320 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5321 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5324 assert(tv_mask != tarval_bad);
5325 assert(get_tarval_mode(tv_mask) == mode);
5327 irg = get_irn_irg(n);
5328 block = get_nodes_block(n);
5329 dbgi = get_irn_dbg_info(n);
5331 pnc = tarval_cmp(tv_shl, tv_shr);
5332 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5333 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5334 new_const = new_Const(tv_shift);
5336 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5338 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5341 assert(pnc == pn_Cmp_Gt);
5342 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5343 new_const = new_Const(tv_shift);
5344 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5347 new_const = new_Const(tv_mask);
5348 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5356 static ir_node *transform_node_Shr(ir_node *n) {
5357 ir_node *c, *oldn = n;
5358 ir_node *left = get_Shr_left(n);
5359 ir_node *right = get_Shr_right(n);
5360 ir_mode *mode = get_irn_mode(n);
5362 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5363 n = transform_node_shift(n);
5366 n = transform_node_shl_shr(n);
5368 n = transform_node_bitop_shift(n);
5371 } /* transform_node_Shr */
5376 static ir_node *transform_node_Shrs(ir_node *n) {
5377 ir_node *c, *oldn = n;
5378 ir_node *a = get_Shrs_left(n);
5379 ir_node *b = get_Shrs_right(n);
5380 ir_mode *mode = get_irn_mode(n);
5382 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5383 n = transform_node_shift(n);
5386 n = transform_node_bitop_shift(n);
5389 } /* transform_node_Shrs */
5394 static ir_node *transform_node_Shl(ir_node *n) {
5395 ir_node *c, *oldn = n;
5396 ir_node *a = get_Shl_left(n);
5397 ir_node *b = get_Shl_right(n);
5398 ir_mode *mode = get_irn_mode(n);
5400 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5401 n = transform_node_shift(n);
5404 n = transform_node_shl_shr(n);
5406 n = transform_node_bitop_shift(n);
5409 } /* transform_node_Shl */
5414 static ir_node *transform_node_Rotl(ir_node *n) {
5415 ir_node *c, *oldn = n;
5416 ir_node *a = get_Rotl_left(n);
5417 ir_node *b = get_Rotl_right(n);
5418 ir_mode *mode = get_irn_mode(n);
5420 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5421 n = transform_node_shift(n);
5424 n = transform_node_bitop_shift(n);
5427 } /* transform_node_Rotl */
5432 static ir_node *transform_node_Conv(ir_node *n) {
5433 ir_node *c, *oldn = n;
5434 ir_mode *mode = get_irn_mode(n);
5435 ir_node *a = get_Conv_op(n);
5437 if (mode != mode_b && is_const_Phi(a)) {
5438 /* Do NOT optimize mode_b Conv's, this leads to remaining
5439 * Phib nodes later, because the conv_b_lower operation
5440 * is instantly reverted, when it tries to insert a Convb.
5442 c = apply_conv_on_phi(a, mode);
5444 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5449 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5450 return new_r_Unknown(current_ir_graph, mode);
5453 if (mode_is_reference(mode) &&
5454 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5456 ir_node *l = get_Add_left(a);
5457 ir_node *r = get_Add_right(a);
5458 dbg_info *dbgi = get_irn_dbg_info(a);
5459 ir_node *block = get_nodes_block(n);
5461 ir_node *lop = get_Conv_op(l);
5462 if(get_irn_mode(lop) == mode) {
5463 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5464 n = new_rd_Add(dbgi, block, lop, r, mode);
5469 ir_node *rop = get_Conv_op(r);
5470 if(get_irn_mode(rop) == mode) {
5471 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5472 n = new_rd_Add(dbgi, block, l, rop, mode);
5479 } /* transform_node_Conv */
5482 * Remove dead blocks and nodes in dead blocks
5483 * in keep alive list. We do not generate a new End node.
5485 static ir_node *transform_node_End(ir_node *n) {
5486 int i, j, n_keepalives = get_End_n_keepalives(n);
5489 NEW_ARR_A(ir_node *, in, n_keepalives);
5491 for (i = j = 0; i < n_keepalives; ++i) {
5492 ir_node *ka = get_End_keepalive(n, i);
5494 if (! is_Block_dead(ka)) {
5498 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5500 } else if (is_Bad(ka)) {
5501 /* no need to keep Bad */
5506 if (j != n_keepalives)
5507 set_End_keepalives(n, j, in);
5509 } /* transform_node_End */
5511 bool is_negated_value(ir_node *a, ir_node *b)
5513 if (is_Minus(a) && get_Minus_op(a) == b)
5515 if (is_Minus(b) && get_Minus_op(b) == a)
5517 if (is_Sub(a) && is_Sub(b)) {
5518 ir_node *a_left = get_Sub_left(a);
5519 ir_node *a_right = get_Sub_right(a);
5520 ir_node *b_left = get_Sub_left(b);
5521 ir_node *b_right = get_Sub_right(b);
5523 if (a_left == b_right && a_right == b_left)
5531 * Optimize a Mux into some simpler cases.
5533 static ir_node *transform_node_Mux(ir_node *n) {
5534 ir_node *oldn = n, *sel = get_Mux_sel(n);
5535 ir_mode *mode = get_irn_mode(n);
5536 ir_node *t = get_Mux_true(n);
5537 ir_node *f = get_Mux_false(n);
5538 ir_graph *irg = current_ir_graph;
5540 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5544 ir_node* block = get_nodes_block(n);
5546 ir_node* c1 = get_Mux_sel(t);
5547 ir_node* t1 = get_Mux_true(t);
5548 ir_node* f1 = get_Mux_false(t);
5550 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5551 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5552 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5557 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5558 } else if (f == t1) {
5559 /* Mux(cond0, Mux(cond1, x, y), x) */
5560 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5561 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5562 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5567 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5569 } else if (is_Mux(f)) {
5570 ir_node* block = get_nodes_block(n);
5572 ir_node* c1 = get_Mux_sel(f);
5573 ir_node* t1 = get_Mux_true(f);
5574 ir_node* f1 = get_Mux_false(f);
5576 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5577 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5578 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5583 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5584 } else if (t == f1) {
5585 /* Mux(cond0, x, Mux(cond1, y, x)) */
5586 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5587 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5588 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5593 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5597 /* first normalization step: move a possible zero to the false case */
5599 ir_node *cmp = get_Proj_pred(sel);
5602 if (is_Const(t) && is_Const_null(t)) {
5605 /* Mux(x, 0, y) => Mux(x, y, 0) */
5606 pn_Cmp pnc = get_Proj_proj(sel);
5607 sel = new_r_Proj(get_nodes_block(cmp), cmp, mode_b,
5608 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5609 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, t, f, mode);
5617 /* note: after normalization, false can only happen on default */
5618 if (mode == mode_b) {
5619 dbg_info *dbg = get_irn_dbg_info(n);
5620 ir_node *block = get_nodes_block(n);
5623 tarval *tv_t = get_Const_tarval(t);
5624 if (tv_t == tarval_b_true) {
5626 /* Muxb(sel, true, false) = sel */
5627 assert(get_Const_tarval(f) == tarval_b_false);
5628 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5631 /* Muxb(sel, true, x) = Or(sel, x) */
5632 n = new_rd_Or(dbg, block, sel, f, mode_b);
5633 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5637 } else if (is_Const(f)) {
5638 tarval *tv_f = get_Const_tarval(f);
5639 if (tv_f == tarval_b_true) {
5640 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5641 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5642 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5643 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5646 /* Muxb(sel, x, false) = And(sel, x) */
5647 assert(tv_f == tarval_b_false);
5648 n = new_rd_And(dbg, block, sel, t, mode_b);
5649 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5655 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5656 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5657 tarval *a = get_Const_tarval(t);
5658 tarval *b = get_Const_tarval(f);
5661 if (tarval_is_one(a) && tarval_is_null(b)) {
5662 ir_node *block = get_nodes_block(n);
5663 ir_node *conv = new_r_Conv(block, sel, mode);
5665 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5667 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5668 ir_node *block = get_nodes_block(n);
5669 ir_node *not_ = new_r_Not(block, sel, mode_b);
5670 ir_node *conv = new_r_Conv(block, not_, mode);
5672 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5675 /* TODO: it's not really clear if that helps in general or should be moved
5676 * to backend, especially with the MUX->Conv transformation above */
5677 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5678 diff = tarval_sub(a, b, NULL);
5681 diff = tarval_sub(b, a, NULL);
5685 if (diff == get_tarval_one(mode)) {
5686 dbg_info *dbg = get_irn_dbg_info(n);
5687 ir_node *block = get_nodes_block(n);
5688 ir_node *t = new_Const(tarval_sub(a, min, NULL));
5689 ir_node *f = new_Const(tarval_sub(b, min, NULL));
5690 n = new_rd_Mux(dbg, block, sel, f, t, mode);
5691 n = new_rd_Add(dbg, block, n, new_Const(min), mode);
5697 ir_node *cmp = get_Proj_pred(sel);
5698 long pn = get_Proj_proj(sel);
5701 * Note: normalization puts the constant on the right side,
5702 * so we check only one case.
5704 * Note further that these optimization work even for floating point
5705 * with NaN's because -NaN == NaN.
5706 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5710 ir_node *cmp_r = get_Cmp_right(cmp);
5711 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5712 ir_node *block = get_nodes_block(n);
5713 ir_node *cmp_l = get_Cmp_left(cmp);
5715 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5718 /* NaN's work fine with abs, so it is ok to remove Uo */
5719 long pnc = pn & ~pn_Cmp_Uo;
5721 if ( (cmp_l == t && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt))
5722 || (cmp_l == f && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt)))
5724 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5725 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5726 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5728 } else if ((cmp_l == t && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt))
5729 || (cmp_l == f && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt)))
5731 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5732 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5733 n = new_rd_Minus(get_irn_dbg_info(n), block, n, mode);
5734 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5739 if (mode_is_int(mode)) {
5741 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5742 /* Mux((a & b) != 0, c, 0) */
5743 ir_node *and_r = get_And_right(cmp_l);
5746 if (and_r == t && f == cmp_r) {
5747 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5748 if (pn == pn_Cmp_Lg) {
5749 /* Mux((a & 2^C) != 0, 2^C, 0) */
5751 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5753 /* Mux((a & 2^C) == 0, 2^C, 0) */
5754 n = new_rd_Eor(get_irn_dbg_info(n),
5755 block, cmp_l, t, mode);
5756 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5761 if (is_Shl(and_r)) {
5762 ir_node *shl_l = get_Shl_left(and_r);
5763 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5764 if (and_r == t && f == cmp_r) {
5765 if (pn == pn_Cmp_Lg) {
5766 /* (a & (1 << n)) != 0, (1 << n), 0) */
5768 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5770 /* (a & (1 << n)) == 0, (1 << n), 0) */
5771 n = new_rd_Eor(get_irn_dbg_info(n),
5772 block, cmp_l, t, mode);
5773 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5779 and_l = get_And_left(cmp_l);
5780 if (is_Shl(and_l)) {
5781 ir_node *shl_l = get_Shl_left(and_l);
5782 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5783 if (and_l == t && f == cmp_r) {
5784 if (pn == pn_Cmp_Lg) {
5785 /* ((1 << n) & a) != 0, (1 << n), 0) */
5787 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5789 /* ((1 << n) & a) == 0, (1 << n), 0) */
5790 n = new_rd_Eor(get_irn_dbg_info(n),
5791 block, cmp_l, t, mode);
5792 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5805 } /* transform_node_Mux */
5808 * optimize Sync nodes that have other syncs as input we simply add the inputs
5809 * of the other sync to our own inputs
5811 static ir_node *transform_node_Sync(ir_node *n) {
5812 int arity = get_Sync_n_preds(n);
5815 for (i = 0; i < arity;) {
5816 ir_node *pred = get_Sync_pred(n, i);
5820 if (!is_Sync(pred)) {
5828 pred_arity = get_Sync_n_preds(pred);
5829 for (j = 0; j < pred_arity; ++j) {
5830 ir_node *pred_pred = get_Sync_pred(pred, j);
5835 add_irn_n(n, pred_pred);
5839 if (get_Sync_pred(n, k) == pred_pred) break;
5844 /* rehash the sync node */
5845 add_identities(current_ir_graph->value_table, n);
5848 } /* transform_node_Sync */
5851 * optimize a trampoline Call into a direct Call
5853 static ir_node *transform_node_Call(ir_node *call) {
5854 ir_node *callee = get_Call_ptr(call);
5855 ir_node *adr, *mem, *res, *bl, **in;
5856 ir_type *ctp, *mtp, *tp;
5859 int i, n_res, n_param;
5862 if (! is_Proj(callee))
5864 callee = get_Proj_pred(callee);
5865 if (! is_Builtin(callee))
5867 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5870 mem = get_Call_mem(call);
5872 if (skip_Proj(mem) == callee) {
5873 /* memory is routed to the trampoline, skip */
5874 mem = get_Builtin_mem(callee);
5877 /* build a new call type */
5878 mtp = get_Call_type(call);
5879 tdb = get_type_dbg_info(mtp);
5881 n_res = get_method_n_ress(mtp);
5882 n_param = get_method_n_params(mtp);
5883 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5885 for (i = 0; i < n_res; ++i)
5886 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5888 NEW_ARR_A(ir_node *, in, n_param + 1);
5890 /* FIXME: we don't need a new pointer type in every step */
5891 tp = get_irg_frame_type(current_ir_graph);
5892 tp = new_type_pointer(tp);
5893 set_method_param_type(ctp, 0, tp);
5895 in[0] = get_Builtin_param(callee, 2);
5896 for (i = 0; i < n_param; ++i) {
5897 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5898 in[i + 1] = get_Call_param(call, i);
5900 var = get_method_variadicity(mtp);
5901 set_method_variadicity(ctp, var);
5902 if (var == variadicity_variadic) {
5903 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
5905 /* When we resolve a trampoline, the function must be called by a this-call */
5906 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5907 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5909 adr = get_Builtin_param(callee, 1);
5911 db = get_irn_dbg_info(call);
5912 bl = get_nodes_block(call);
5914 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5915 if (get_irn_pinned(call) == op_pin_state_floats)
5916 set_irn_pinned(res, op_pin_state_floats);
5918 } /* transform_node_Call */
5921 * Tries several [inplace] [optimizing] transformations and returns an
5922 * equivalent node. The difference to equivalent_node() is that these
5923 * transformations _do_ generate new nodes, and thus the old node must
5924 * not be freed even if the equivalent node isn't the old one.
5926 static ir_node *transform_node(ir_node *n) {
5930 * Transform_node is the only "optimizing transformation" that might
5931 * return a node with a different opcode. We iterate HERE until fixpoint
5932 * to get the final result.
5936 if (n->op->ops.transform_node != NULL)
5937 n = n->op->ops.transform_node(n);
5938 } while (oldn != n);
5941 } /* transform_node */
5944 * Sets the default transform node operation for an ir_op_ops.
5946 * @param code the opcode for the default operation
5947 * @param ops the operations initialized
5952 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5956 ops->transform_node = transform_node_##a; \
5958 #define CASE_PROJ(a) \
5960 ops->transform_node_Proj = transform_node_Proj_##a; \
5962 #define CASE_PROJ_EX(a) \
5964 ops->transform_node = transform_node_##a; \
5965 ops->transform_node_Proj = transform_node_Proj_##a; \
5974 CASE_PROJ_EX(DivMod);
6009 } /* firm_set_default_transform_node */
6012 /* **************** Common Subexpression Elimination **************** */
6014 /** The size of the hash table used, should estimate the number of nodes
6016 #define N_IR_NODES 512
6018 /** Compares the attributes of two Const nodes. */
6019 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
6020 return (get_Const_tarval(a) != get_Const_tarval(b))
6021 || (get_Const_type(a) != get_Const_type(b));
6022 } /* node_cmp_attr_Const */
6024 /** Compares the attributes of two Proj nodes. */
6025 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
6026 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
6027 } /* node_cmp_attr_Proj */
6029 /** Compares the attributes of two Filter nodes. */
6030 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
6031 return get_Filter_proj(a) != get_Filter_proj(b);
6032 } /* node_cmp_attr_Filter */
6034 /** Compares the attributes of two Alloc nodes. */
6035 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
6036 const alloc_attr *pa = get_irn_alloc_attr(a);
6037 const alloc_attr *pb = get_irn_alloc_attr(b);
6038 return (pa->where != pb->where) || (pa->type != pb->type);
6039 } /* node_cmp_attr_Alloc */
6041 /** Compares the attributes of two Free nodes. */
6042 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
6043 const free_attr *pa = get_irn_free_attr(a);
6044 const free_attr *pb = get_irn_free_attr(b);
6045 return (pa->where != pb->where) || (pa->type != pb->type);
6046 } /* node_cmp_attr_Free */
6048 /** Compares the attributes of two SymConst nodes. */
6049 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
6050 const symconst_attr *pa = get_irn_symconst_attr(a);
6051 const symconst_attr *pb = get_irn_symconst_attr(b);
6052 return (pa->kind != pb->kind)
6053 || (pa->sym.type_p != pb->sym.type_p)
6054 || (pa->tp != pb->tp);
6055 } /* node_cmp_attr_SymConst */
6057 /** Compares the attributes of two Call nodes. */
6058 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
6059 const call_attr *pa = get_irn_call_attr(a);
6060 const call_attr *pb = get_irn_call_attr(b);
6061 return (pa->type != pb->type)
6062 || (pa->tail_call != pb->tail_call);
6063 } /* node_cmp_attr_Call */
6065 /** Compares the attributes of two Sel nodes. */
6066 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
6067 const ir_entity *a_ent = get_Sel_entity(a);
6068 const ir_entity *b_ent = get_Sel_entity(b);
6071 (a_ent->kind != b_ent->kind) ||
6072 (a_ent->name != b_ent->name) ||
6073 (a_ent->owner != b_ent->owner) ||
6074 (a_ent->ld_name != b_ent->ld_name) ||
6075 (a_ent->type != b_ent->type);
6077 /* Matze: inlining of functions can produce 2 entities with same type,
6079 return a_ent != b_ent;
6080 } /* node_cmp_attr_Sel */
6082 /** Compares the attributes of two Phi nodes. */
6083 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
6084 /* we can only enter this function if both nodes have the same number of inputs,
6085 hence it is enough to check if one of them is a Phi0 */
6087 /* check the Phi0 pos attribute */
6088 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
6091 } /* node_cmp_attr_Phi */
6093 /** Compares the attributes of two Conv nodes. */
6094 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
6095 return get_Conv_strict(a) != get_Conv_strict(b);
6096 } /* node_cmp_attr_Conv */
6098 /** Compares the attributes of two Cast nodes. */
6099 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
6100 return get_Cast_type(a) != get_Cast_type(b);
6101 } /* node_cmp_attr_Cast */
6103 /** Compares the attributes of two Load nodes. */
6104 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
6105 if (get_Load_volatility(a) == volatility_is_volatile ||
6106 get_Load_volatility(b) == volatility_is_volatile)
6107 /* NEVER do CSE on volatile Loads */
6109 /* do not CSE Loads with different alignment. Be conservative. */
6110 if (get_Load_align(a) != get_Load_align(b))
6113 return get_Load_mode(a) != get_Load_mode(b);
6114 } /* node_cmp_attr_Load */
6116 /** Compares the attributes of two Store nodes. */
6117 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
6118 /* do not CSE Stores with different alignment. Be conservative. */
6119 if (get_Store_align(a) != get_Store_align(b))
6122 /* NEVER do CSE on volatile Stores */
6123 return (get_Store_volatility(a) == volatility_is_volatile ||
6124 get_Store_volatility(b) == volatility_is_volatile);
6125 } /* node_cmp_attr_Store */
6127 /** Compares two exception attributes */
6128 static int node_cmp_exception(ir_node *a, ir_node *b) {
6129 const except_attr *ea = get_irn_except_attr(a);
6130 const except_attr *eb = get_irn_except_attr(b);
6132 return ea->pin_state != eb->pin_state;
6135 #define node_cmp_attr_Bound node_cmp_exception
6137 /** Compares the attributes of two Div nodes. */
6138 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
6139 const divmod_attr *ma = get_irn_divmod_attr(a);
6140 const divmod_attr *mb = get_irn_divmod_attr(b);
6141 return ma->exc.pin_state != mb->exc.pin_state ||
6142 ma->resmode != mb->resmode ||
6143 ma->no_remainder != mb->no_remainder;
6144 } /* node_cmp_attr_Div */
6146 /** Compares the attributes of two DivMod nodes. */
6147 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
6148 const divmod_attr *ma = get_irn_divmod_attr(a);
6149 const divmod_attr *mb = get_irn_divmod_attr(b);
6150 return ma->exc.pin_state != mb->exc.pin_state ||
6151 ma->resmode != mb->resmode;
6152 } /* node_cmp_attr_DivMod */
6154 /** Compares the attributes of two Mod nodes. */
6155 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
6156 const divmod_attr *ma = get_irn_divmod_attr(a);
6157 const divmod_attr *mb = get_irn_divmod_attr(b);
6158 return ma->exc.pin_state != mb->exc.pin_state ||
6159 ma->resmode != mb->resmode;
6160 } /* node_cmp_attr_Mod */
6162 /** Compares the attributes of two Quot nodes. */
6163 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
6164 const divmod_attr *ma = get_irn_divmod_attr(a);
6165 const divmod_attr *mb = get_irn_divmod_attr(b);
6166 return ma->exc.pin_state != mb->exc.pin_state ||
6167 ma->resmode != mb->resmode;
6168 } /* node_cmp_attr_Quot */
6170 /** Compares the attributes of two Confirm nodes. */
6171 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
6172 /* no need to compare the bound, as this is a input */
6173 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6174 } /* node_cmp_attr_Confirm */
6176 /** Compares the attributes of two Builtin nodes. */
6177 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b) {
6178 const builtin_attr *ma = get_irn_builtin_attr(a);
6179 const builtin_attr *mb = get_irn_builtin_attr(b);
6181 /* no need to compare the type, equal kind means equal type */
6182 return ma->kind != mb->kind;
6183 } /* node_cmp_attr_Builtin */
6185 /** Compares the attributes of two ASM nodes. */
6186 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
6188 const ir_asm_constraint *ca;
6189 const ir_asm_constraint *cb;
6192 if (get_ASM_text(a) != get_ASM_text(b))
6195 /* Should we really check the constraints here? Should be better, but is strange. */
6196 n = get_ASM_n_input_constraints(a);
6197 if (n != get_ASM_n_input_constraints(b))
6200 ca = get_ASM_input_constraints(a);
6201 cb = get_ASM_input_constraints(b);
6202 for (i = 0; i < n; ++i) {
6203 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6207 n = get_ASM_n_output_constraints(a);
6208 if (n != get_ASM_n_output_constraints(b))
6211 ca = get_ASM_output_constraints(a);
6212 cb = get_ASM_output_constraints(b);
6213 for (i = 0; i < n; ++i) {
6214 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6218 n = get_ASM_n_clobbers(a);
6219 if (n != get_ASM_n_clobbers(b))
6222 cla = get_ASM_clobbers(a);
6223 clb = get_ASM_clobbers(b);
6224 for (i = 0; i < n; ++i) {
6225 if (cla[i] != clb[i])
6229 } /* node_cmp_attr_ASM */
6231 /** Compares the inexistent attributes of two Dummy nodes. */
6232 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6240 * Set the default node attribute compare operation for an ir_op_ops.
6242 * @param code the opcode for the default operation
6243 * @param ops the operations initialized
6248 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6252 ops->node_cmp_attr = node_cmp_attr_##a; \
6285 } /* firm_set_default_node_cmp_attr */
6288 * Compare function for two nodes in the value table. Gets two
6289 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6291 int identities_cmp(const void *elt, const void *key) {
6292 ir_node *a = (ir_node *)elt;
6293 ir_node *b = (ir_node *)key;
6296 if (a == b) return 0;
6298 if ((get_irn_op(a) != get_irn_op(b)) ||
6299 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6301 /* compare if a's in and b's in are of equal length */
6302 irn_arity_a = get_irn_intra_arity(a);
6303 if (irn_arity_a != get_irn_intra_arity(b))
6306 if (get_irn_pinned(a) == op_pin_state_pinned) {
6307 /* for pinned nodes, the block inputs must be equal */
6308 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6310 } else if (! get_opt_global_cse()) {
6311 /* for block-local CSE both nodes must be in the same MacroBlock */
6312 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6316 /* compare a->in[0..ins] with b->in[0..ins] */
6317 for (i = 0; i < irn_arity_a; ++i) {
6318 ir_node *pred_a = get_irn_intra_n(a, i);
6319 ir_node *pred_b = get_irn_intra_n(b, i);
6320 if (pred_a != pred_b) {
6321 /* if both predecessors are CSE neutral they might be different */
6322 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6328 * here, we already now that the nodes are identical except their
6331 if (a->op->ops.node_cmp_attr)
6332 return a->op->ops.node_cmp_attr(a, b);
6335 } /* identities_cmp */
6338 * Calculate a hash value of a node.
6340 * @param node The IR-node
6342 unsigned ir_node_hash(const ir_node *node) {
6343 return node->op->ops.hash(node);
6344 } /* ir_node_hash */
6347 pset *new_identities(void) {
6348 return new_pset(identities_cmp, N_IR_NODES);
6349 } /* new_identities */
6351 void del_identities(pset *value_table) {
6352 del_pset(value_table);
6353 } /* del_identities */
6355 /* Normalize a node by putting constants (and operands with larger
6356 * node index) on the right (operator side). */
6357 void ir_normalize_node(ir_node *n) {
6358 if (is_op_commutative(get_irn_op(n))) {
6359 ir_node *l = get_binop_left(n);
6360 ir_node *r = get_binop_right(n);
6362 /* For commutative operators perform a OP b == b OP a but keep
6363 * constants on the RIGHT side. This helps greatly in some
6364 * optimizations. Moreover we use the idx number to make the form
6366 if (!operands_are_normalized(l, r)) {
6367 set_binop_left(n, r);
6368 set_binop_right(n, l);
6372 } /* ir_normalize_node */
6375 * Update the nodes after a match in the value table. If both nodes have
6376 * the same MacroBlock but different Blocks, we must ensure that the node
6377 * with the dominating Block (the node that is near to the MacroBlock header
6378 * is stored in the table.
6379 * Because a MacroBlock has only one "non-exception" flow, we don't need
6380 * dominance info here: We known, that one block must dominate the other and
6381 * following the only block input will allow to find it.
6383 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6384 ir_node *known_blk, *new_block, *block, *mbh;
6386 if (get_opt_global_cse()) {
6387 /* Block inputs are meaning less */
6390 known_blk = get_irn_n(known_irn, -1);
6391 new_block = get_irn_n(new_ir_node, -1);
6392 if (known_blk == new_block) {
6393 /* already in the same block */
6397 * We expect the typical case when we built the graph. In that case, the
6398 * known_irn is already the upper one, so checking this should be faster.
6401 mbh = get_Block_MacroBlock(new_block);
6403 if (block == known_blk) {
6404 /* ok, we have found it: known_block dominates new_block as expected */
6409 * We have reached the MacroBlock header NOT founding
6410 * the known_block. new_block must dominate known_block.
6413 set_irn_n(known_irn, -1, new_block);
6416 assert(get_Block_n_cfgpreds(block) == 1);
6417 block = get_Block_cfgpred_block(block, 0);
6419 } /* update_value_table */
6422 * Return the canonical node computing the same value as n.
6423 * Looks up the node in a hash table, enters it in the table
6424 * if it isn't there yet.
6426 * @param value_table the HashSet containing all nodes in the
6428 * @param n the node to look up
6430 * @return a node that computes the same value as n or n if no such
6431 * node could be found
6433 ir_node *identify_remember(pset *value_table, ir_node *n) {
6436 if (!value_table) return n;
6438 ir_normalize_node(n);
6439 /* lookup or insert in hash table with given hash key. */
6440 nn = pset_insert(value_table, n, ir_node_hash(n));
6443 update_known_irn(nn, n);
6445 /* n is reachable again */
6446 edges_node_revival(nn, get_irn_irg(nn));
6450 } /* identify_remember */
6453 * During construction we set the op_pin_state_pinned flag in the graph right when the
6454 * optimization is performed. The flag turning on procedure global cse could
6455 * be changed between two allocations. This way we are safe.
6457 * @param value_table The value table
6458 * @param n The node to lookup
6460 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6463 n = identify_remember(value_table, n);
6464 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6465 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6467 } /* identify_cons */
6469 /* Add a node to the identities value table. */
6470 void add_identities(pset *value_table, ir_node *node) {
6471 if (get_opt_cse() && is_no_Block(node))
6472 identify_remember(value_table, node);
6473 } /* add_identities */
6475 /* Visit each node in the value table of a graph. */
6476 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6478 ir_graph *rem = current_ir_graph;
6480 current_ir_graph = irg;
6481 foreach_pset(irg->value_table, node)
6483 current_ir_graph = rem;
6484 } /* visit_all_identities */
6487 * Garbage in, garbage out. If a node has a dead input, i.e., the
6488 * Bad node is input to the node, return the Bad node.
6490 static ir_node *gigo(ir_node *node) {
6492 ir_op *op = get_irn_op(node);
6494 /* remove garbage blocks by looking at control flow that leaves the block
6495 and replacing the control flow by Bad. */
6496 if (get_irn_mode(node) == mode_X) {
6497 ir_node *block = get_nodes_block(skip_Proj(node));
6499 /* Don't optimize nodes in immature blocks. */
6500 if (!get_Block_matured(block))
6502 /* Don't optimize End, may have Bads. */
6503 if (op == op_End) return node;
6505 if (is_Block(block)) {
6506 if (is_Block_dead(block)) {
6507 /* control flow from dead block is dead */
6511 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6512 if (!is_Bad(get_irn_n(block, i)))
6516 ir_graph *irg = get_irn_irg(block);
6517 /* the start block is never dead */
6518 if (block != get_irg_start_block(irg)
6519 && block != get_irg_end_block(irg)) {
6521 * Do NOT kill control flow without setting
6522 * the block to dead of bad things can happen:
6523 * We get a Block that is not reachable be irg_block_walk()
6524 * but can be found by irg_walk()!
6526 set_Block_dead(block);
6533 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6534 blocks predecessors is dead. */
6535 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6536 irn_arity = get_irn_arity(node);
6539 * Beware: we can only read the block of a non-floating node.
6541 if (is_irn_pinned_in_irg(node) &&
6542 is_Block_dead(get_nodes_block(skip_Proj(node))))
6545 for (i = 0; i < irn_arity; i++) {
6546 ir_node *pred = get_irn_n(node, i);
6551 /* Propagating Unknowns here seems to be a bad idea, because
6552 sometimes we need a node as a input and did not want that
6554 However, it might be useful to move this into a later phase
6555 (if you think that optimizing such code is useful). */
6556 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6557 return new_Unknown(get_irn_mode(node));
6562 /* With this code we violate the agreement that local_optimize
6563 only leaves Bads in Block, Phi and Tuple nodes. */
6564 /* If Block has only Bads as predecessors it's garbage. */
6565 /* If Phi has only Bads as predecessors it's garbage. */
6566 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6567 irn_arity = get_irn_arity(node);
6568 for (i = 0; i < irn_arity; i++) {
6569 if (!is_Bad(get_irn_n(node, i))) break;
6571 if (i == irn_arity) node = new_Bad();
6578 * These optimizations deallocate nodes from the obstack.
6579 * It can only be called if it is guaranteed that no other nodes
6580 * reference this one, i.e., right after construction of a node.
6582 * @param n The node to optimize
6584 * current_ir_graph must be set to the graph of the node!
6586 ir_node *optimize_node(ir_node *n) {
6589 ir_opcode iro = get_irn_opcode(n);
6591 /* Always optimize Phi nodes: part of the construction. */
6592 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6594 /* constant expression evaluation / constant folding */
6595 if (get_opt_constant_folding()) {
6596 /* neither constants nor Tuple values can be evaluated */
6597 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6598 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6599 int old_fp_mode = tarval_fp_ops_enabled();
6601 tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6603 /* try to evaluate */
6604 tv = computed_value(n);
6605 if (tv != tarval_bad) {
6607 ir_type *old_tp = get_irn_type(n);
6608 int i, arity = get_irn_arity(n);
6612 * Try to recover the type of the new expression.
6614 for (i = 0; i < arity && !old_tp; ++i)
6615 old_tp = get_irn_type(get_irn_n(n, i));
6618 * we MUST copy the node here temporary, because it's still needed
6619 * for DBG_OPT_CSTEVAL
6621 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6622 oldn = alloca(node_size);
6624 memcpy(oldn, n, node_size);
6625 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6627 /* ARG, copy the in array, we need it for statistics */
6628 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6630 /* note the inplace edges module */
6631 edges_node_deleted(n, current_ir_graph);
6633 /* evaluation was successful -- replace the node. */
6634 irg_kill_node(current_ir_graph, n);
6637 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6638 set_Const_type(nw, old_tp);
6639 DBG_OPT_CSTEVAL(oldn, nw);
6640 tarval_enable_fp_ops(old_fp_mode);
6643 tarval_enable_fp_ops(old_fp_mode);
6647 /* remove unnecessary nodes */
6648 if (get_opt_algebraic_simplification() ||
6649 (iro == iro_Phi) || /* always optimize these nodes. */
6651 (iro == iro_Proj) ||
6652 (iro == iro_Block) ) /* Flags tested local. */
6653 n = equivalent_node(n);
6655 /* Common Subexpression Elimination.
6657 * Checks whether n is already available.
6658 * The block input is used to distinguish different subexpressions. Right
6659 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6660 * subexpressions within a block.
6663 n = identify_cons(current_ir_graph->value_table, n);
6666 edges_node_deleted(oldn, current_ir_graph);
6668 /* We found an existing, better node, so we can deallocate the old node. */
6669 irg_kill_node(current_ir_graph, oldn);
6673 /* Some more constant expression evaluation that does not allow to
6675 iro = get_irn_opcode(n);
6676 if (get_opt_algebraic_simplification() ||
6677 (iro == iro_Cond) ||
6678 (iro == iro_Proj)) /* Flags tested local. */
6679 n = transform_node(n);
6681 /* Remove nodes with dead (Bad) input.
6682 Run always for transformation induced Bads. */
6685 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6686 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6688 n = identify_remember(current_ir_graph->value_table, o);
6694 } /* optimize_node */
6698 * These optimizations never deallocate nodes (in place). This can cause dead
6699 * nodes lying on the obstack. Remove these by a dead node elimination,
6700 * i.e., a copying garbage collection.
6702 ir_node *optimize_in_place_2(ir_node *n) {
6705 ir_opcode iro = get_irn_opcode(n);
6707 if (!get_opt_optimize() && !is_Phi(n)) return n;
6709 /* constant expression evaluation / constant folding */
6710 if (get_opt_constant_folding()) {
6711 /* neither constants nor Tuple values can be evaluated */
6712 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6713 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6714 int old_fp_mode = tarval_fp_ops_enabled();
6716 tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6717 /* try to evaluate */
6718 tv = computed_value(n);
6719 if (tv != tarval_bad) {
6720 /* evaluation was successful -- replace the node. */
6721 ir_type *old_tp = get_irn_type(n);
6722 int i, arity = get_irn_arity(n);
6725 * Try to recover the type of the new expression.
6727 for (i = 0; i < arity && !old_tp; ++i)
6728 old_tp = get_irn_type(get_irn_n(n, i));
6732 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6733 set_Const_type(n, old_tp);
6735 DBG_OPT_CSTEVAL(oldn, n);
6736 tarval_enable_fp_ops(old_fp_mode);
6739 tarval_enable_fp_ops(old_fp_mode);
6743 /* remove unnecessary nodes */
6744 if (get_opt_constant_folding() ||
6745 (iro == iro_Phi) || /* always optimize these nodes. */
6746 (iro == iro_Id) || /* ... */
6747 (iro == iro_Proj) || /* ... */
6748 (iro == iro_Block) ) /* Flags tested local. */
6749 n = equivalent_node(n);
6751 /** common subexpression elimination **/
6752 /* Checks whether n is already available. */
6753 /* The block input is used to distinguish different subexpressions. Right
6754 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6755 subexpressions within a block. */
6756 if (get_opt_cse()) {
6758 n = identify_remember(current_ir_graph->value_table, o);
6763 /* Some more constant expression evaluation. */
6764 iro = get_irn_opcode(n);
6765 if (get_opt_constant_folding() ||
6766 (iro == iro_Cond) ||
6767 (iro == iro_Proj)) /* Flags tested local. */
6768 n = transform_node(n);
6770 /* Remove nodes with dead (Bad) input.
6771 Run always for transformation induced Bads. */
6774 /* Now we can verify the node, as it has no dead inputs any more. */
6777 /* Now we have a legal, useful node. Enter it in hash table for cse.
6778 Blocks should be unique anyways. (Except the successor of start:
6779 is cse with the start block!) */
6780 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6782 n = identify_remember(current_ir_graph->value_table, o);
6788 } /* optimize_in_place_2 */
6791 * Wrapper for external use, set proper status bits after optimization.
6793 ir_node *optimize_in_place(ir_node *n) {
6794 /* Handle graph state */
6795 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6797 if (get_opt_global_cse())
6798 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6799 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6800 set_irg_outs_inconsistent(current_ir_graph);
6802 /* FIXME: Maybe we could also test whether optimizing the node can
6803 change the control graph. */
6804 set_irg_doms_inconsistent(current_ir_graph);
6805 return optimize_in_place_2(n);
6806 } /* optimize_in_place */
6809 * Calculate a hash value of a Const node.
6811 static unsigned hash_Const(const ir_node *node) {
6814 /* special value for const, as they only differ in their tarval. */
6815 h = HASH_PTR(node->attr.con.tv);
6821 * Calculate a hash value of a SymConst node.
6823 static unsigned hash_SymConst(const ir_node *node) {
6826 /* all others are pointers */
6827 h = HASH_PTR(node->attr.symc.sym.type_p);
6830 } /* hash_SymConst */
6833 * Set the default hash operation in an ir_op_ops.
6835 * @param code the opcode for the default operation
6836 * @param ops the operations initialized
6841 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6845 ops->hash = hash_##a; \
6848 /* hash function already set */
6849 if (ops->hash != NULL)
6856 /* use input/mode default hash if no function was given */
6857 ops->hash = firm_default_hash;
6865 * Sets the default operation for an ir_ops.
6867 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6868 ops = firm_set_default_hash(code, ops);
6869 ops = firm_set_default_computed_value(code, ops);
6870 ops = firm_set_default_equivalent_node(code, ops);
6871 ops = firm_set_default_transform_node(code, ops);
6872 ops = firm_set_default_node_cmp_attr(code, ops);
6873 ops = firm_set_default_get_type(code, ops);
6874 ops = firm_set_default_get_type_attr(code, ops);
6875 ops = firm_set_default_get_entity_attr(code, ops);
6878 } /* firm_set_default_operations */