3 * File name: ir/ir/iropt.c
4 * Purpose: iropt --- optimizations intertwined with IR construction.
5 * Author: Christian Schaefer
6 * Modified by: Goetz Lindenmaier, Michael Beck
9 * Copyright: (c) 1998-2005 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
28 #include "irgraph_t.h"
29 #include "iredges_t.h"
36 #include "dbginfo_t.h"
37 #include "iropt_dbg.h"
43 #include "opt_polymorphy.h"
44 #include "opt_confirms.h"
46 /* Make types visible to allow most efficient access */
47 # include "entity_t.h"
50 * return the value of a Constant
52 static tarval *computed_value_Const(ir_node *n)
54 return get_Const_tarval(n);
58 * return the value of a 'sizeof' SymConst
60 static tarval *computed_value_SymConst(ir_node *n)
62 if ((get_SymConst_kind(n) == symconst_size) &&
63 (get_type_state(get_SymConst_type(n))) == layout_fixed)
64 return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(n)), get_irn_mode(n));
69 * return the value of an Add
71 static tarval *computed_value_Add(ir_node *n)
73 ir_node *a = get_Add_left(n);
74 ir_node *b = get_Add_right(n);
76 tarval *ta = value_of(a);
77 tarval *tb = value_of(b);
79 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
80 return tarval_add(ta, tb);
86 * return the value of a Sub
89 static tarval *computed_value_Sub(ir_node *n)
91 ir_node *a = get_Sub_left(n);
92 ir_node *b = get_Sub_right(n);
97 if (a == b && !is_Bad(a))
98 return get_mode_null(get_irn_mode(n));
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_sub(ta, tb);
110 * return the value of an unary Minus
112 static tarval *computed_value_Minus(ir_node *n)
114 ir_node *a = get_Minus_op(n);
115 tarval *ta = value_of(a);
117 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
118 return tarval_neg(ta);
124 * return the value of a Mul
126 static tarval *computed_value_Mul(ir_node *n)
128 ir_node *a = get_Mul_left(n);
129 ir_node *b = get_Mul_right(n);
131 tarval *ta = value_of(a);
132 tarval *tb = value_of(b);
134 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
135 return tarval_mul(ta, tb);
137 /* a*0 = 0 or 0*b = 0:
138 calls computed_value recursive and returns the 0 with proper
140 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
142 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
149 * return the value of a floating point Quot
151 static tarval *computed_value_Quot(ir_node *n)
153 ir_node *a = get_Quot_left(n);
154 ir_node *b = get_Quot_right(n);
156 tarval *ta = value_of(a);
157 tarval *tb = value_of(b);
159 /* This was missing in original implementation. Why? */
160 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
161 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
162 return tarval_quo(ta, tb);
168 * calculate the value of an integer Div of two nodes
169 * Special case: 0 / b
171 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
173 tarval *ta = value_of(a);
174 tarval *tb = value_of(b);
176 /* Compute c1 / c2 or 0 / a, a != 0 */
177 if (ta != tarval_bad) {
178 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
179 return tarval_div(ta, tb);
180 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
187 * return the value of an integer Div
189 static tarval *computed_value_Div(ir_node *n)
191 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
195 * calculate the value of an integer Mod of two nodes
196 * Special case: a % 1
198 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
200 tarval *ta = value_of(a);
201 tarval *tb = value_of(b);
203 /* Compute c1 % c2 or a % 1 */
204 if (tb != tarval_bad) {
205 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
206 return tarval_mod(ta, tb);
207 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
208 return get_mode_null(get_irn_mode(a));
215 * return the value of an integer Mod
217 static tarval *computed_value_Mod(ir_node *n)
219 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
223 * return the value of an Abs
225 static tarval *computed_value_Abs(ir_node *n)
227 ir_node *a = get_Abs_op(n);
228 tarval *ta = value_of(a);
230 if (ta != tarval_bad)
231 return tarval_abs(ta);
237 * return the value of an And
238 * Special case: a & 0, 0 & b
240 static tarval *computed_value_And(ir_node *n)
242 ir_node *a = get_And_left(n);
243 ir_node *b = get_And_right(n);
245 tarval *ta = value_of(a);
246 tarval *tb = value_of(b);
248 if ((ta != tarval_bad) && (tb != tarval_bad)) {
249 return tarval_and (ta, tb);
253 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
254 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
262 * return the value of an Or
263 * Special case: a | 1...1, 1...1 | b
265 static tarval *computed_value_Or(ir_node *n)
267 ir_node *a = get_Or_left(n);
268 ir_node *b = get_Or_right(n);
270 tarval *ta = value_of(a);
271 tarval *tb = value_of(b);
273 if ((ta != tarval_bad) && (tb != tarval_bad)) {
274 return tarval_or (ta, tb);
277 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
278 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
286 * return the value of an Eor
288 static tarval *computed_value_Eor(ir_node *n)
290 ir_node *a = get_Eor_left(n);
291 ir_node *b = get_Eor_right(n);
296 return get_mode_null(get_irn_mode(n));
301 if ((ta != tarval_bad) && (tb != tarval_bad)) {
302 return tarval_eor (ta, tb);
308 * return the value of a Not
310 static tarval *computed_value_Not(ir_node *n)
312 ir_node *a = get_Not_op(n);
313 tarval *ta = value_of(a);
315 if (ta != tarval_bad)
316 return tarval_not(ta);
322 * return the value of a Shl
324 static tarval *computed_value_Shl(ir_node *n)
326 ir_node *a = get_Shl_left(n);
327 ir_node *b = get_Shl_right(n);
329 tarval *ta = value_of(a);
330 tarval *tb = value_of(b);
332 if ((ta != tarval_bad) && (tb != tarval_bad)) {
333 return tarval_shl (ta, tb);
339 * return the value of a Shr
341 static tarval *computed_value_Shr(ir_node *n)
343 ir_node *a = get_Shr_left(n);
344 ir_node *b = get_Shr_right(n);
346 tarval *ta = value_of(a);
347 tarval *tb = value_of(b);
349 if ((ta != tarval_bad) && (tb != tarval_bad)) {
350 return tarval_shr (ta, tb);
356 * return the value of a Shrs
358 static tarval *computed_value_Shrs(ir_node *n)
360 ir_node *a = get_Shrs_left(n);
361 ir_node *b = get_Shrs_right(n);
363 tarval *ta = value_of(a);
364 tarval *tb = value_of(b);
366 if ((ta != tarval_bad) && (tb != tarval_bad)) {
367 return tarval_shrs (ta, tb);
373 * return the value of a Rot
375 static tarval *computed_value_Rot(ir_node *n)
377 ir_node *a = get_Rot_left(n);
378 ir_node *b = get_Rot_right(n);
380 tarval *ta = value_of(a);
381 tarval *tb = value_of(b);
383 if ((ta != tarval_bad) && (tb != tarval_bad)) {
384 return tarval_rot (ta, tb);
390 * return the value of a Conv
392 static tarval *computed_value_Conv(ir_node *n)
394 ir_node *a = get_Conv_op(n);
395 tarval *ta = value_of(a);
397 if (ta != tarval_bad)
398 return tarval_convert_to(ta, get_irn_mode(n));
404 * return the value of a Proj(Cmp)
406 * This performs a first step of unreachable code elimination.
407 * Proj can not be computed, but folding a Cmp above the Proj here is
408 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
410 * There are several case where we can evaluate a Cmp node, see later.
412 static tarval *computed_value_Proj_Cmp(ir_node *n)
414 ir_node *a = get_Proj_pred(n);
415 ir_node *aa = get_Cmp_left(a);
416 ir_node *ab = get_Cmp_right(a);
417 long proj_nr = get_Proj_proj(n);
420 * BEWARE: a == a is NOT always True for floating Point values, as
421 * NaN != NaN is defined, so we must check this here.
424 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
427 /* This is a trick with the bits used for encoding the Cmp
428 Proj numbers, the following statement is not the same:
429 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
430 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
433 tarval *taa = value_of(aa);
434 tarval *tab = value_of(ab);
435 ir_mode *mode = get_irn_mode(aa);
438 * The predecessors of Cmp are target values. We can evaluate
441 if ((taa != tarval_bad) && (tab != tarval_bad)) {
442 /* strange checks... */
443 pn_Cmp flags = tarval_cmp(taa, tab);
444 if (flags != pn_Cmp_False) {
445 return new_tarval_from_long (proj_nr & flags, mode_b);
448 /* for integer values, we can check against MIN/MAX */
449 else if (mode_is_int(mode)) {
450 /* MIN <=/> x. This results in true/false. */
451 if (taa == get_mode_min(mode)) {
452 /* a compare with the MIN value */
453 if (proj_nr == pn_Cmp_Le)
454 return get_tarval_b_true();
455 else if (proj_nr == pn_Cmp_Gt)
456 return get_tarval_b_false();
458 /* x >=/< MIN. This results in true/false. */
460 if (tab == get_mode_min(mode)) {
461 /* a compare with the MIN value */
462 if (proj_nr == pn_Cmp_Ge)
463 return get_tarval_b_true();
464 else if (proj_nr == pn_Cmp_Lt)
465 return get_tarval_b_false();
467 /* MAX >=/< x. This results in true/false. */
468 else if (taa == get_mode_max(mode)) {
469 if (proj_nr == pn_Cmp_Ge)
470 return get_tarval_b_true();
471 else if (proj_nr == pn_Cmp_Lt)
472 return get_tarval_b_false();
474 /* x <=/> MAX. This results in true/false. */
475 else if (tab == get_mode_max(mode)) {
476 if (proj_nr == pn_Cmp_Le)
477 return get_tarval_b_true();
478 else if (proj_nr == pn_Cmp_Gt)
479 return get_tarval_b_false();
483 * The predecessors are Allocs or (void*)(0) constants. Allocs never
484 * return NULL, they raise an exception. Therefore we can predict
488 ir_node *aaa = skip_Id(skip_Proj(aa));
489 ir_node *aba = skip_Id(skip_Proj(ab));
491 if ( ( (/* aa is ProjP and aaa is Alloc */
492 (get_irn_op(aa) == op_Proj)
493 && (mode_is_reference(get_irn_mode(aa)))
494 && (get_irn_op(aaa) == op_Alloc))
495 && ( (/* ab is NULL */
496 (get_irn_op(ab) == op_Const)
497 && (mode_is_reference(get_irn_mode(ab)))
498 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
499 || (/* ab is other Alloc */
500 (get_irn_op(ab) == op_Proj)
501 && (mode_is_reference(get_irn_mode(ab)))
502 && (get_irn_op(aba) == op_Alloc)
504 || (/* aa is NULL and aba is Alloc */
505 (get_irn_op(aa) == op_Const)
506 && (mode_is_reference(get_irn_mode(aa)))
507 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
508 && (get_irn_op(ab) == op_Proj)
509 && (mode_is_reference(get_irn_mode(ab)))
510 && (get_irn_op(aba) == op_Alloc)))
512 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
516 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
520 * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
522 static tarval *computed_value_Proj(ir_node *n)
524 ir_node *a = get_Proj_pred(n);
527 switch (get_irn_opcode(a)) {
529 return computed_value_Proj_Cmp(n);
532 /* compute either the Div or the Mod part */
533 proj_nr = get_Proj_proj(n);
534 if (proj_nr == pn_DivMod_res_div)
535 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
536 else if (proj_nr == pn_DivMod_res_mod)
537 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
541 if (get_Proj_proj(n) == pn_Div_res)
542 return computed_value(a);
546 if (get_Proj_proj(n) == pn_Mod_res)
547 return computed_value(a);
557 * calculate the value of a Mux: can be evaluated, if the
558 * sel and the right input are known
560 static tarval *computed_value_Mux(ir_node *n)
562 ir_node *sel = get_Mux_sel(n);
563 tarval *ts = value_of(sel);
565 if (ts == get_tarval_b_true()) {
566 ir_node *v = get_Mux_true(n);
569 else if (ts == get_tarval_b_false()) {
570 ir_node *v = get_Mux_false(n);
577 * calculate the value of a Confirm: can be evaluated,
578 * if it has the form Confirm(x, '=', Const).
580 static tarval *computed_value_Confirm(ir_node *n)
582 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
583 value_of(get_Confirm_bound(n)) : tarval_bad;
587 * If the parameter n can be computed, return its value, else tarval_bad.
588 * Performs constant folding.
590 * @param n The node this should be evaluated
592 tarval *computed_value(ir_node *n)
594 if (n->op->ops.computed_value)
595 return n->op->ops.computed_value(n);
600 * set the default computed_value evaluator in an ir_op_ops.
602 * @param code the opcode for the default operation
603 * @param ops the operations initialized
608 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
612 ops->computed_value = computed_value_##a; \
647 /* returns 1 if the a and b are pointers to different locations. */
649 different_identity (ir_node *a, ir_node *b)
651 assert (mode_is_reference(get_irn_mode (a))
652 && mode_is_reference(get_irn_mode (b)));
654 if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
655 ir_node *a1 = get_Proj_pred (a);
656 ir_node *b1 = get_Proj_pred (b);
657 if (a1 != b1 && get_irn_op (a1) == op_Alloc
658 && get_irn_op (b1) == op_Alloc)
666 * Returns a equivalent block for another block.
667 * If the block has only one predecessor, this is
668 * the equivalent one. If the only predecessor of a block is
669 * the block itself, this is a dead block.
671 * If both predecessors of a block are the branches of a binary
672 * Cond, the equivalent block is Cond's block.
674 * If all predecessors of a block are bad or lies in a dead
675 * block, the current block is dead as well.
677 * Note, that blocks are NEVER turned into Bad's, instead
678 * the dead_block flag is set. So, never test for is_Bad(block),
679 * always use is_dead_Block(block).
681 static ir_node *equivalent_node_Block(ir_node *n)
684 int n_preds = get_Block_n_cfgpreds(n);
686 /* The Block constructor does not call optimize, but mature_immBlock
687 calls the optimization. */
688 assert(get_Block_matured(n));
690 /* Straightening: a single entry Block following a single exit Block
691 can be merged, if it is not the Start block. */
692 /* !!! Beware, all Phi-nodes of n must have been optimized away.
693 This should be true, as the block is matured before optimize is called.
694 But what about Phi-cycles with the Phi0/Id that could not be resolved?
695 Remaining Phi nodes are just Ids. */
696 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
697 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
698 if (predblock == oldn) {
699 /* Jmp jumps into the block it is in -- deal self cycle. */
700 n = set_Block_dead(n);
701 DBG_OPT_DEAD_BLOCK(oldn, n);
702 } else if (get_opt_control_flow_straightening()) {
704 DBG_OPT_STG(oldn, n);
707 else if ((n_preds == 1) &&
708 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
709 ir_node *predblock = get_Block_cfgpred_block(n, 0);
710 if (predblock == oldn) {
711 /* Jmp jumps into the block it is in -- deal self cycle. */
712 n = set_Block_dead(n);
713 DBG_OPT_DEAD_BLOCK(oldn, n);
716 else if ((n_preds == 2) &&
717 (get_opt_control_flow_weak_simplification())) {
718 /* Test whether Cond jumps twice to this block
719 * The more general case which more than 2 predecessors is handles
720 * in optimize_cf(), we handle only this special case for speed here.
722 ir_node *a = get_Block_cfgpred(n, 0);
723 ir_node *b = get_Block_cfgpred(n, 1);
725 if ((get_irn_op(a) == op_Proj) &&
726 (get_irn_op(b) == op_Proj) &&
727 (get_Proj_pred(a) == get_Proj_pred(b)) &&
728 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
729 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
730 /* Also a single entry Block following a single exit Block. Phis have
731 twice the same operand and will be optimized away. */
732 n = get_nodes_block(get_Proj_pred(a));
733 DBG_OPT_IFSIM1(oldn, a, b, n);
736 else if (get_opt_unreachable_code() &&
737 (n != current_ir_graph->start_block) &&
738 (n != current_ir_graph->end_block) ) {
741 /* If all inputs are dead, this block is dead too, except if it is
742 the start or end block. This is one step of unreachable code
744 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
745 ir_node *pred = get_Block_cfgpred(n, i);
748 if (is_Bad(pred)) continue;
749 pred_blk = get_nodes_block(skip_Proj(pred));
751 if (is_Block_dead(pred_blk)) continue;
754 /* really found a living input */
759 n = set_Block_dead(n);
760 DBG_OPT_DEAD_BLOCK(oldn, n);
768 * Returns a equivalent node for a Jmp, a Bad :-)
769 * Of course this only happens if the Block of the Jmp is Bad.
771 static ir_node *equivalent_node_Jmp(ir_node *n)
773 /* unreachable code elimination */
774 if (is_Block_dead(get_nodes_block(n)))
780 /* Same for op_Raise */
781 #define equivalent_node_Raise equivalent_node_Jmp
784 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
785 See transform_node_Proj_Cond(). */
788 * optimize operations that are commutative and have neutral 0,
789 * so a op 0 = 0 op a = a.
791 static ir_node *equivalent_node_neutral_zero(ir_node *n)
795 ir_node *a = get_binop_left(n);
796 ir_node *b = get_binop_right(n);
801 /* After running compute_node there is only one constant predecessor.
802 Find this predecessors value and remember the other node: */
803 if ((tv = value_of(a)) != tarval_bad) {
805 } else if ((tv = value_of(b)) != tarval_bad) {
810 /* If this predecessors constant value is zero, the operation is
811 * unnecessary. Remove it.
813 * Beware: If n is a Add, the mode of on and n might be different
814 * which happens in this rare construction: NULL + 3.
815 * Then, a Conv would be needed which we cannot include here.
817 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
818 if (get_irn_mode(on) == get_irn_mode(n)) {
821 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
828 #define equivalent_node_Eor equivalent_node_neutral_zero
831 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
833 * The second one looks strange, but this construct
834 * is used heavily in the LCC sources :-).
836 * Beware: The Mode of an Add may be different than the mode of its
837 * predecessors, so we could not return a predecessors in all cases.
839 static ir_node *equivalent_node_Add(ir_node *n)
842 ir_node *left, *right;
844 n = equivalent_node_neutral_zero(n);
848 left = get_Add_left(n);
849 right = get_Add_right(n);
851 if (get_irn_op(left) == op_Sub) {
852 if (get_Sub_right(left) == right) {
855 n = get_Sub_left(left);
856 if (get_irn_mode(oldn) == get_irn_mode(n)) {
857 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
862 if (get_irn_op(right) == op_Sub) {
863 if (get_Sub_right(right) == left) {
866 n = get_Sub_left(right);
867 if (get_irn_mode(oldn) == get_irn_mode(n)) {
868 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
877 * optimize operations that are not commutative but have neutral 0 on left,
880 static ir_node *equivalent_node_left_zero(ir_node *n)
884 ir_node *a = get_binop_left(n);
885 ir_node *b = get_binop_right(n);
887 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
890 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
896 #define equivalent_node_Shl equivalent_node_left_zero
897 #define equivalent_node_Shr equivalent_node_left_zero
898 #define equivalent_node_Shrs equivalent_node_left_zero
899 #define equivalent_node_Rot equivalent_node_left_zero
902 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
904 * The second one looks strange, but this construct
905 * is used heavily in the LCC sources :-).
907 * Beware: The Mode of a Sub may be different than the mode of its
908 * predecessors, so we could not return a predecessors in all cases.
910 static ir_node *equivalent_node_Sub(ir_node *n)
914 ir_node *a = get_Sub_left(n);
915 ir_node *b = get_Sub_right(n);
917 /* Beware: modes might be different */
918 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
919 if (get_irn_mode(n) == get_irn_mode(a)) {
922 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
925 else if (get_irn_op(a) == op_Add) {
926 ir_mode *mode = get_irn_mode(n);
928 if (mode_wrap_around(mode)) {
929 ir_node *left = get_Add_left(a);
930 ir_node *right = get_Add_right(a);
933 if (get_irn_mode(n) == get_irn_mode(right)) {
935 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
938 else if (right == b) {
939 if (get_irn_mode(n) == get_irn_mode(left)) {
941 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
952 * Optimize an "idempotent unary op", ie op(op(n)) = n.
955 * -(-a) == a, but might overflow two times.
956 * We handle it anyway here but the better way would be a
957 * flag. This would be needed for Pascal for instance.
959 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
962 ir_node *pred = get_unop_op(n);
964 /* optimize symmetric unop */
965 if (get_irn_op(pred) == get_irn_op(n)) {
966 n = get_unop_op(pred);
967 DBG_OPT_ALGSIM2(oldn, pred, n);
972 /* Not(Not(x)) == x */
973 #define equivalent_node_Not equivalent_node_idempotent_unop
975 /* --x == x */ /* ??? Is this possible or can --x raise an
976 out of bounds exception if min =! max? */
977 #define equivalent_node_Minus equivalent_node_idempotent_unop
980 * Optimize a * 1 = 1 * a = a.
982 static ir_node *equivalent_node_Mul(ir_node *n)
986 ir_node *a = get_Mul_left(n);
987 ir_node *b = get_Mul_right(n);
989 /* Mul is commutative and has again an other neutral element. */
990 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
992 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
993 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
995 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1001 * Optimize a / 1 = a.
1003 static ir_node *equivalent_node_Div(ir_node *n)
1005 ir_node *a = get_Div_left(n);
1006 ir_node *b = get_Div_right(n);
1008 /* Div is not commutative. */
1009 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1010 /* Turn Div into a tuple (mem, bad, a) */
1011 ir_node *mem = get_Div_mem(n);
1012 turn_into_tuple(n, pn_Div_max);
1013 set_Tuple_pred(n, pn_Div_M, mem);
1014 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1015 set_Tuple_pred(n, pn_Div_res, a);
1021 * Optimize a / 1 = a.
1023 static ir_node *equivalent_node_DivMod(ir_node *n)
1025 ir_node *a = get_DivMod_left(n);
1026 ir_node *b = get_DivMod_right(n);
1028 /* Div is not commutative. */
1029 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1030 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1031 ir_node *mem = get_Div_mem(n);
1032 ir_mode *mode = get_irn_mode(b);
1034 turn_into_tuple(n, pn_DivMod_max);
1035 set_Tuple_pred(n, pn_DivMod_M, mem);
1036 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1037 set_Tuple_pred(n, pn_DivMod_res_div, a);
1038 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1044 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1046 static ir_node *equivalent_node_Or(ir_node *n)
1050 ir_node *a = get_Or_left(n);
1051 ir_node *b = get_Or_right(n);
1054 n = a; /* Or has it's own neutral element */
1055 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1056 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1058 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1059 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1061 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1068 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1070 static ir_node *equivalent_node_And(ir_node *n)
1074 ir_node *a = get_And_left(n);
1075 ir_node *b = get_And_right(n);
1078 n = a; /* And has it's own neutral element */
1079 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1080 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1082 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1083 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1085 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1091 * Try to remove useless Conv's:
1093 static ir_node *equivalent_node_Conv(ir_node *n)
1096 ir_node *a = get_Conv_op(n);
1099 ir_mode *n_mode = get_irn_mode(n);
1100 ir_mode *a_mode = get_irn_mode(a);
1102 if (n_mode == a_mode) { /* No Conv necessary */
1104 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1105 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1109 n_mode = get_irn_mode(n);
1110 b_mode = get_irn_mode(b);
1112 if (n_mode == b_mode) {
1113 if (n_mode == mode_b) {
1114 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1115 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1117 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1118 if (smaller_mode(b_mode, a_mode)){
1119 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1120 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1129 * A Cast may be removed if the type of the previous node
1130 * is already the type of the Cast.
1132 static ir_node *equivalent_node_Cast(ir_node *n) {
1134 ir_node *pred = get_Cast_op(n);
1136 if (get_irn_type(pred) == get_Cast_type(n)) {
1138 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1143 /* Several optimizations:
1144 - no Phi in start block.
1145 - remove Id operators that are inputs to Phi
1146 - fold Phi-nodes, iff they have only one predecessor except
1149 static ir_node *equivalent_node_Phi(ir_node *n)
1154 ir_node *block = NULL; /* to shutup gcc */
1155 ir_node *first_val = NULL; /* to shutup gcc */
1156 ir_node *scnd_val = NULL; /* to shutup gcc */
1158 if (!get_opt_normalize()) return n;
1160 n_preds = get_Phi_n_preds(n);
1162 block = get_nodes_block(n);
1163 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1164 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1165 if ((is_Block_dead(block)) || /* Control dead */
1166 (block == current_ir_graph->start_block)) /* There should be no Phi nodes */
1167 return new_Bad(); /* in the Start Block. */
1169 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1171 /* If the Block has a Bad pred, we also have one. */
1172 for (i = 0; i < n_preds; ++i)
1173 if (is_Bad(get_Block_cfgpred(block, i)))
1174 set_Phi_pred(n, i, new_Bad());
1176 /* Find first non-self-referencing input */
1177 for (i = 0; i < n_preds; ++i) {
1178 first_val = get_Phi_pred(n, i);
1179 if ( (first_val != n) /* not self pointer */
1181 && (! is_Bad(first_val))
1183 ) { /* value not dead */
1184 break; /* then found first value. */
1189 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1195 /* follow_Id () for rest of inputs, determine if any of these
1196 are non-self-referencing */
1197 while (++i < n_preds) {
1198 scnd_val = get_Phi_pred(n, i);
1199 if ( (scnd_val != n)
1200 && (scnd_val != first_val)
1202 && (! is_Bad(scnd_val))
1210 /* Fold, if no multiple distinct non-self-referencing inputs */
1212 DBG_OPT_PHI(oldn, n);
1214 /* skip the remaining Ids (done in get_Phi_pred). */
1215 /* superfluous, since we walk all to propagate Block's Bads.
1216 while (++i < n_preds) get_Phi_pred(n, i); */
1222 * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1223 * ProjX(Load) and ProjX(Store)
1225 static ir_node *equivalent_node_Proj(ir_node *n)
1229 ir_node *a = get_Proj_pred(n);
1231 if ( get_irn_op(a) == op_Tuple) {
1232 /* Remove the Tuple/Proj combination. */
1233 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1234 n = get_Tuple_pred(a, get_Proj_proj(n));
1235 DBG_OPT_TUPLE(oldn, a, n);
1237 assert(0); /* This should not happen! */
1241 else if (get_irn_mode(n) == mode_X) {
1242 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1243 /* Remove dead control flow -- early gigo(). */
1246 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1247 ir_op *op = get_irn_op(a);
1249 if (op == op_Load || op == op_Store) {
1250 /* get the load/store address */
1251 ir_node *addr = get_irn_n(a, 1);
1252 if (value_not_null(addr)) {
1253 /* this node may float */
1254 set_irn_pinned(a, op_pin_state_floats);
1268 static ir_node *equivalent_node_Id(ir_node *n)
1274 } while (get_irn_op(n) == op_Id);
1276 DBG_OPT_ID(oldn, n);
1283 static ir_node *equivalent_node_Mux(ir_node *n)
1285 ir_node *oldn = n, *sel = get_Mux_sel(n);
1286 tarval *ts = value_of(sel);
1288 /* Mux(true, f, t) == t */
1289 if (ts == tarval_b_true) {
1290 n = get_Mux_true(n);
1291 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1293 /* Mux(false, f, t) == f */
1294 else if (ts == tarval_b_false) {
1295 n = get_Mux_false(n);
1296 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1298 /* Mux(v, x, x) == x */
1299 else if (get_Mux_false(n) == get_Mux_true(n)) {
1300 n = get_Mux_true(n);
1301 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1303 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1304 ir_node *cmp = get_Proj_pred(sel);
1305 long proj_nr = get_Proj_proj(sel);
1306 ir_node *b = get_Mux_false(n);
1307 ir_node *a = get_Mux_true(n);
1310 * Note: normalization puts the constant on the right site,
1311 * so we check only one case.
1313 * Note further that these optimization work even for floating point
1314 * with NaN's because -NaN == NaN.
1315 * However, if +0 and -0 is handled differently, we cannot use the first one.
1317 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1318 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1319 /* Mux(a CMP 0, X, a) */
1320 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1321 /* Mux(a CMP 0, -a, a) */
1322 if (proj_nr == pn_Cmp_Eq) {
1323 /* Mux(a == 0, -a, a) ==> -a */
1325 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1327 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1328 /* Mux(a != 0, -a, a) ==> a */
1330 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1333 else if (classify_Const(b) == CNST_NULL) {
1334 /* Mux(a CMP 0, 0, a) */
1335 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1336 /* Mux(a != 0, 0, a) ==> a */
1338 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1340 else if (proj_nr == pn_Cmp_Eq) {
1341 /* Mux(a == 0, 0, a) ==> 0 */
1343 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1353 * Optimize -a CMP -b into b CMP a.
1354 * This works only for for modes where unary Minus
1356 * Note that two-complement integers can Overflow
1357 * so it will NOT work.
1359 static ir_node *equivalent_node_Cmp(ir_node *n)
1361 ir_node *left = get_Cmp_left(n);
1362 ir_node *right = get_Cmp_right(n);
1364 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1365 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1366 left = get_Minus_op(left);
1367 right = get_Minus_op(right);
1368 set_Cmp_left(n, right);
1369 set_Cmp_right(n, left);
1375 * Remove Confirm nodes if setting is on.
1376 * Replace Confirms(x, '=', Constlike) by Constlike.
1378 static ir_node *equivalent_node_Confirm(ir_node *n)
1380 ir_node *pred = get_Confirm_value(n);
1381 pn_Cmp pnc = get_Confirm_cmp(n);
1383 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1385 * rare case: two identical Confirms one after another,
1386 * replace the second one with the first.
1390 if (pnc == pn_Cmp_Eq) {
1391 ir_node *bound = get_Confirm_bound(n);
1394 * Optimize a rare case:
1395 * Confirm(x, '=', Constlike) ==> Constlike
1397 if (is_irn_constlike(bound)) {
1398 DBG_OPT_CONFIRM(n, bound);
1402 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1406 * Optimize CopyB(mem, x, x) into a Nop
1408 static ir_node *equivalent_node_CopyB(ir_node *n)
1410 ir_node *a = get_CopyB_dst(n);
1411 ir_node *b = get_CopyB_src(n);
1414 /* Turn CopyB into a tuple (mem, bad, bad) */
1415 ir_node *mem = get_CopyB_mem(n);
1416 turn_into_tuple(n, pn_CopyB_max);
1417 set_Tuple_pred(n, pn_CopyB_M, mem);
1418 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1419 set_Tuple_pred(n, pn_Call_M_except, new_Bad());
1425 * Optimize Bounds(idx, idx, upper) into idx.
1427 static ir_node *equivalent_node_Bound(ir_node *n)
1429 ir_node *idx = get_Bound_index(n);
1430 ir_node *lower = get_Bound_lower(n);
1433 /* By definition lower < upper, so if idx == lower -->
1434 lower <= idx && idx < upper */
1436 /* Turn Bound into a tuple (mem, bad, idx) */
1440 ir_node *pred = skip_Proj(idx);
1442 if (get_irn_op(pred) == op_Bound) {
1444 * idx was Bounds_check previously, it is still valid if
1445 * lower <= pred_lower && pred_upper <= upper.
1447 ir_node *upper = get_Bound_upper(n);
1448 if (get_Bound_lower(pred) == lower &&
1449 get_Bound_upper(pred) == upper) {
1451 * One could expect that we simple return the previous
1452 * Bound here. However, this would be wrong, as we could
1453 * add an exception Proj to a new location than.
1454 * So, we must turn in into a tuple
1461 /* Turn Bound into a tuple (mem, bad, idx) */
1462 ir_node *mem = get_Bound_mem(n);
1463 turn_into_tuple(n, pn_Bound_max);
1464 set_Tuple_pred(n, pn_Bound_M_regular, mem);
1465 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1466 set_Tuple_pred(n, pn_Bound_res, idx);
1467 set_Tuple_pred(n, pn_Bound_M_except, mem);
1473 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1474 * perform no actual computation, as, e.g., the Id nodes. It does not create
1475 * new nodes. It is therefore safe to free n if the node returned is not n.
1476 * If a node returns a Tuple we can not just skip it. If the size of the
1477 * in array fits, we transform n into a tuple (e.g., Div).
1480 equivalent_node(ir_node *n)
1482 if (n->op->ops.equivalent_node)
1483 return n->op->ops.equivalent_node(n);
1488 * sets the default equivalent node operation for an ir_op_ops.
1490 * @param code the opcode for the default operation
1491 * @param ops the operations initialized
1496 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1500 ops->equivalent_node = equivalent_node_##a; \
1540 * Do node specific optimizations of nodes predecessors.
1543 optimize_preds(ir_node *n) {
1544 ir_node *a = NULL, *b = NULL;
1546 /* get the operands we will work on for simple cases. */
1548 a = get_binop_left(n);
1549 b = get_binop_right(n);
1550 } else if (is_unop(n)) {
1554 switch (get_irn_opcode(n)) {
1557 /* We don't want Cast as input to Cmp. */
1558 if (get_irn_op(a) == op_Cast) {
1562 if (get_irn_op(b) == op_Cast) {
1564 set_Cmp_right(n, b);
1573 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1574 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1575 * If possible, remove the Conv's.
1577 static ir_node *transform_node_AddSub(ir_node *n)
1579 ir_mode *mode = get_irn_mode(n);
1581 if (mode_is_reference(mode)) {
1582 ir_node *left = get_binop_left(n);
1583 ir_node *right = get_binop_right(n);
1584 int ref_bits = get_mode_size_bits(mode);
1586 if (get_irn_op(left) == op_Conv) {
1587 ir_mode *mode = get_irn_mode(left);
1588 int bits = get_mode_size_bits(mode);
1590 if (ref_bits == bits &&
1591 mode_is_int(mode) &&
1592 get_mode_arithmetic(mode) == irma_twos_complement) {
1593 ir_node *pre = get_Conv_op(left);
1594 ir_mode *pre_mode = get_irn_mode(pre);
1596 if (mode_is_int(pre_mode) &&
1597 get_mode_size_bits(pre_mode) == bits &&
1598 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1599 /* ok, this conv just changes to sign, moreover the calculation
1600 * is done with same number of bits as our address mode, so
1601 * we can ignore the conv as address calculation can be viewed
1602 * as either signed or unsigned
1604 set_binop_left(n, pre);
1609 if (get_irn_op(right) == op_Conv) {
1610 ir_mode *mode = get_irn_mode(right);
1611 int bits = get_mode_size_bits(mode);
1613 if (ref_bits == bits &&
1614 mode_is_int(mode) &&
1615 get_mode_arithmetic(mode) == irma_twos_complement) {
1616 ir_node *pre = get_Conv_op(right);
1617 ir_mode *pre_mode = get_irn_mode(pre);
1619 if (mode_is_int(pre_mode) &&
1620 get_mode_size_bits(pre_mode) == bits &&
1621 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1622 /* ok, this conv just changes to sign, moreover the calculation
1623 * is done with same number of bits as our address mode, so
1624 * we can ignore the conv as address calculation can be viewed
1625 * as either signed or unsigned
1627 set_binop_right(n, pre);
1636 * Do the AddSub optimization, then Transform
1637 * Add(a,a) -> Mul(a, 2)
1638 * Add(Mul(a, x), a) -> Mul(a, x+1)
1639 * if the mode is integer or float.
1640 * Transform Add(a,-b) into Sub(a,b).
1641 * Reassociation might fold this further.
1643 static ir_node *transform_node_Add(ir_node *n)
1648 n = transform_node_AddSub(n);
1650 mode = get_irn_mode(n);
1651 if (mode_is_num(mode)) {
1652 ir_node *a = get_Add_left(n);
1653 ir_node *b = get_Add_right(n);
1656 ir_node *block = get_irn_n(n, -1);
1659 get_irn_dbg_info(n),
1663 new_r_Const_long(current_ir_graph, block, mode, 2),
1665 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1667 else if (get_irn_op(a) == op_Minus) {
1669 get_irn_dbg_info(n),
1675 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1677 else if (get_irn_op(b) == op_Minus) {
1679 get_irn_dbg_info(n),
1685 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1687 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1688 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1689 ir_node *ma = get_Mul_left(a);
1690 ir_node *mb = get_Mul_right(a);
1693 ir_node *blk = get_irn_n(n, -1);
1695 get_irn_dbg_info(n), current_ir_graph, blk,
1698 get_irn_dbg_info(n), current_ir_graph, blk,
1700 new_r_Const_long(current_ir_graph, blk, mode, 1),
1703 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1706 ir_node *blk = get_irn_n(n, -1);
1708 get_irn_dbg_info(n), current_ir_graph, blk,
1711 get_irn_dbg_info(n), current_ir_graph, blk,
1713 new_r_Const_long(current_ir_graph, blk, mode, 1),
1716 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1719 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1720 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1721 ir_node *ma = get_Mul_left(b);
1722 ir_node *mb = get_Mul_right(b);
1725 ir_node *blk = get_irn_n(n, -1);
1727 get_irn_dbg_info(n), current_ir_graph, blk,
1730 get_irn_dbg_info(n), current_ir_graph, blk,
1732 new_r_Const_long(current_ir_graph, blk, mode, 1),
1735 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1738 ir_node *blk = get_irn_n(n, -1);
1740 get_irn_dbg_info(n), current_ir_graph, blk,
1743 get_irn_dbg_info(n), current_ir_graph, blk,
1745 new_r_Const_long(current_ir_graph, blk, mode, 1),
1748 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1756 * Do the AddSub optimization, then Transform
1757 * Sub(0,a) -> Minus(a)
1758 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1760 static ir_node *transform_node_Sub(ir_node *n)
1766 n = transform_node_AddSub(n);
1768 mode = get_irn_mode(n);
1769 a = get_Sub_left(n);
1770 b = get_Sub_right(n);
1771 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
1773 get_irn_dbg_info(n),
1778 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1780 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1781 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1782 ir_node *ma = get_Mul_left(a);
1783 ir_node *mb = get_Mul_right(a);
1786 ir_node *blk = get_irn_n(n, -1);
1788 get_irn_dbg_info(n),
1789 current_ir_graph, blk,
1792 get_irn_dbg_info(n),
1793 current_ir_graph, blk,
1795 new_r_Const_long(current_ir_graph, blk, mode, 1),
1798 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1801 ir_node *blk = get_irn_n(n, -1);
1803 get_irn_dbg_info(n),
1804 current_ir_graph, blk,
1807 get_irn_dbg_info(n),
1808 current_ir_graph, blk,
1810 new_r_Const_long(current_ir_graph, blk, mode, 1),
1813 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
1821 * Transform Mul(a,-1) into -a.
1822 * Do architecture dependent optimizations on Mul nodes
1824 static ir_node *transform_node_Mul(ir_node *n) {
1826 ir_mode *mode = get_irn_mode(n);
1828 if (mode_is_signed(mode)) {
1830 ir_node *a = get_Mul_left(n);
1831 ir_node *b = get_Mul_right(n);
1833 if (value_of(a) == get_mode_minus_one(mode))
1835 else if (value_of(b) == get_mode_minus_one(mode))
1838 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1839 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1843 return arch_dep_replace_mul_with_shifts(n);
1847 * transform a Div Node
1849 static ir_node *transform_node_Div(ir_node *n)
1851 tarval *tv = value_of(n);
1854 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1856 if (tv != tarval_bad) {
1857 value = new_Const(get_tarval_mode(tv), tv);
1859 DBG_OPT_CSTEVAL(n, value);
1861 else /* Try architecture dependent optimization */
1862 value = arch_dep_replace_div_by_const(n);
1865 /* Turn Div into a tuple (mem, bad, value) */
1866 ir_node *mem = get_Div_mem(n);
1868 turn_into_tuple(n, pn_Div_max);
1869 set_Tuple_pred(n, pn_Div_M, mem);
1870 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1871 set_Tuple_pred(n, pn_Div_res, value);
1877 * transform a Mod node
1879 static ir_node *transform_node_Mod(ir_node *n)
1881 tarval *tv = value_of(n);
1884 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1886 if (tv != tarval_bad) {
1887 value = new_Const(get_tarval_mode(tv), tv);
1889 DBG_OPT_CSTEVAL(n, value);
1891 else /* Try architecture dependent optimization */
1892 value = arch_dep_replace_mod_by_const(n);
1895 /* Turn Mod into a tuple (mem, bad, value) */
1896 ir_node *mem = get_Mod_mem(n);
1898 turn_into_tuple(n, pn_Mod_max);
1899 set_Tuple_pred(n, pn_Mod_M, mem);
1900 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1901 set_Tuple_pred(n, pn_Mod_res, value);
1907 * transform a DivMod node
1909 static ir_node *transform_node_DivMod(ir_node *n)
1913 ir_node *a = get_DivMod_left(n);
1914 ir_node *b = get_DivMod_right(n);
1915 ir_mode *mode = get_irn_mode(a);
1916 tarval *ta = value_of(a);
1917 tarval *tb = value_of(b);
1919 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1922 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1924 if (tb != tarval_bad) {
1925 if (tb == get_mode_one(get_tarval_mode(tb))) {
1926 b = new_Const (mode, get_mode_null(mode));
1929 DBG_OPT_CSTEVAL(n, b);
1931 else if (ta != tarval_bad) {
1932 tarval *resa, *resb;
1933 resa = tarval_div (ta, tb);
1934 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1935 Jmp for X result!? */
1936 resb = tarval_mod (ta, tb);
1937 if (resb == tarval_bad) return n; /* Causes exception! */
1938 a = new_Const (mode, resa);
1939 b = new_Const (mode, resb);
1942 DBG_OPT_CSTEVAL(n, a);
1943 DBG_OPT_CSTEVAL(n, b);
1945 else { /* Try architecture dependent optimization */
1946 arch_dep_replace_divmod_by_const(&a, &b, n);
1947 evaluated = a != NULL;
1949 } else if (ta == get_mode_null(mode)) {
1950 /* 0 / non-Const = 0 */
1955 if (evaluated) { /* replace by tuple */
1956 ir_node *mem = get_DivMod_mem(n);
1957 turn_into_tuple(n, pn_DivMod_max);
1958 set_Tuple_pred(n, pn_DivMod_M, mem);
1959 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1960 set_Tuple_pred(n, pn_DivMod_res_div, a);
1961 set_Tuple_pred(n, pn_DivMod_res_mod, b);
1968 * Optimize Abs(x) into x if x is Confirmed >= 0
1969 * Optimize Abs(x) into -x if x is Confirmed <= 0
1971 static ir_node *transform_node_Abs(ir_node *n)
1974 ir_node *a = get_Abs_op(n);
1975 value_classify sign = classify_value_sign(a);
1977 if (sign == VALUE_NEGATIVE) {
1978 ir_mode *mode = get_irn_mode(n);
1981 * We can replace the Abs by -x here.
1982 * We even could add a new Confirm here.
1984 * Note that -x would create a new node, so we could
1985 * not run it in the equivalent_node() context.
1987 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
1988 get_irn_n(n, -1), a, mode);
1990 DBG_OPT_CONFIRM(oldn, n);
1992 else if (sign == VALUE_POSITIVE) {
1993 /* n is positive, Abs is not needed */
1996 DBG_OPT_CONFIRM(oldn, n);
2003 * transform a Cond node
2005 static ir_node *transform_node_Cond(ir_node *n)
2007 /* Replace the Cond by a Jmp if it branches on a constant
2010 ir_node *a = get_Cond_selector(n);
2011 tarval *ta = value_of(a);
2013 /* we need block info which is not available in floating irgs */
2014 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2017 if ((ta != tarval_bad) &&
2018 (get_irn_mode(a) == mode_b) &&
2019 (get_opt_unreachable_code())) {
2020 /* It's a boolean Cond, branching on a boolean constant.
2021 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2022 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2023 turn_into_tuple(n, pn_Cond_max);
2024 if (ta == tarval_b_true) {
2025 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2026 set_Tuple_pred(n, pn_Cond_true, jmp);
2028 set_Tuple_pred(n, pn_Cond_false, jmp);
2029 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2031 /* We might generate an endless loop, so keep it alive. */
2032 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2040 static ir_node *transform_node_Eor(ir_node *n)
2043 ir_node *a = get_Eor_left(n);
2044 ir_node *b = get_Eor_right(n);
2045 ir_mode *mode = get_irn_mode(n);
2049 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2050 mode, get_mode_null(mode));
2051 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2053 else if ((mode == mode_b)
2054 && (get_irn_op(a) == op_Proj)
2055 && (get_irn_mode(a) == mode_b)
2056 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2057 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2058 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2059 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2060 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2062 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2064 else if ((mode == mode_b)
2065 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2066 /* The Eor is a Not. Replace it by a Not. */
2067 /* ????!!!Extend to bitfield 1111111. */
2068 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2070 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2077 * Transform a boolean Not.
2079 static ir_node *transform_node_Not(ir_node *n)
2082 ir_node *a = get_Not_op(n);
2084 if ( (get_irn_mode(n) == mode_b)
2085 && (get_irn_op(a) == op_Proj)
2086 && (get_irn_mode(a) == mode_b)
2087 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2088 /* We negate a Cmp. The Cmp has the negated result anyways! */
2089 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2090 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2091 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2098 * Transform a Cast_type(Const) into a new Const_type
2100 static ir_node *transform_node_Cast(ir_node *n) {
2102 ir_node *pred = get_Cast_op(n);
2103 ir_type *tp = get_irn_type(n);
2105 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2106 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2107 get_Const_tarval(pred), tp);
2108 DBG_OPT_CSTEVAL(oldn, n);
2109 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2110 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2111 get_SymConst_kind(pred), tp);
2112 DBG_OPT_CSTEVAL(oldn, n);
2119 * Transform a Proj(Div) with a non-zero value.
2120 * Removes the exceptions and routes the memory to the NoMem node.
2122 static ir_node *transform_node_Proj_Div(ir_node *proj)
2124 ir_node *n = get_Proj_pred(proj);
2125 ir_node *b = get_Div_right(n);
2128 if (value_not_zero(b)) {
2129 /* div(x, y) && y != 0 */
2130 proj_nr = get_Proj_proj(proj);
2132 /* this node may float */
2133 set_irn_pinned(n, op_pin_state_floats);
2135 if (proj_nr == pn_Div_X_except) {
2136 /* we found an exception handler, remove it */
2137 DBG_OPT_EXC_REM(proj);
2139 } else if (proj_nr == pn_Div_M) {
2140 /* the memory Proj can be removed */
2141 ir_node *res = get_Div_mem(n);
2142 set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2151 * Transform a Proj(Mod) with a non-zero value.
2152 * Removes the exceptions and routes the memory to the NoMem node.
2154 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2156 ir_node *n = get_Proj_pred(proj);
2157 ir_node *b = get_Mod_right(n);
2160 if (value_not_zero(b)) {
2161 /* mod(x, y) && y != 0 */
2162 proj_nr = get_Proj_proj(proj);
2164 /* this node may float */
2165 set_irn_pinned(n, op_pin_state_floats);
2167 if (proj_nr == pn_Mod_X_except) {
2168 /* we found an exception handler, remove it */
2169 DBG_OPT_EXC_REM(proj);
2171 } else if (proj_nr == pn_Mod_M) {
2172 /* the memory Proj can be removed */
2173 ir_node *res = get_Mod_mem(n);
2174 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2178 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2179 /* a % a = 0 if a != 0 */
2180 ir_mode *mode = get_irn_mode(proj);
2181 ir_node *res = new_Const(mode, get_mode_null(mode));
2183 DBG_OPT_CSTEVAL(n, res);
2191 * Transform a Proj(DivMod) with a non-zero value.
2192 * Removes the exceptions and routes the memory to the NoMem node.
2194 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2196 ir_node *n = get_Proj_pred(proj);
2197 ir_node *b = get_DivMod_right(n);
2200 if (value_not_zero(b)) {
2201 /* DivMod(x, y) && y != 0 */
2202 proj_nr = get_Proj_proj(proj);
2204 /* this node may float */
2205 set_irn_pinned(n, op_pin_state_floats);
2207 if (proj_nr == pn_DivMod_X_except) {
2208 /* we found an exception handler, remove it */
2209 DBG_OPT_EXC_REM(proj);
2212 else if (proj_nr == pn_DivMod_M) {
2213 /* the memory Proj can be removed */
2214 ir_node *res = get_DivMod_mem(n);
2215 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2219 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2220 /* a % a = 0 if a != 0 */
2221 ir_mode *mode = get_irn_mode(proj);
2222 ir_node *res = new_Const(mode, get_mode_null(mode));
2224 DBG_OPT_CSTEVAL(n, res);
2232 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2234 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2236 if (get_opt_unreachable_code()) {
2237 ir_node *n = get_Proj_pred(proj);
2238 ir_node *b = get_Cond_selector(n);
2240 if (mode_is_int(get_irn_mode(b))) {
2241 tarval *tb = value_of(b);
2243 if (tb != tarval_bad) {
2244 /* we have a constant switch */
2245 long num = get_Proj_proj(proj);
2247 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2248 if (get_tarval_long(tb) == num) {
2249 /* Do NOT create a jump here, or we will have 2 control flow ops
2250 * in a block. This case is optimized away in optimize_cf(). */
2254 /* this case will NEVER be taken, kill it */
2265 * Normalizes and optimizes Cmp nodes.
2267 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2269 if (get_opt_reassociation()) {
2270 ir_node *n = get_Proj_pred(proj);
2271 ir_node *left = get_Cmp_left(n);
2272 ir_node *right = get_Cmp_right(n);
2276 ir_mode *mode = NULL;
2277 long proj_nr = get_Proj_proj(proj);
2280 * First step: normalize the compare op
2281 * by placing the constant on the right site
2282 * or moving the lower address node to the left.
2283 * We ignore the case that both are constants
2284 * this case should be optimized away.
2286 if (get_irn_op(right) == op_Const)
2288 else if (get_irn_op(left) == op_Const) {
2293 proj_nr = get_inversed_pnc(proj_nr);
2296 else if (left > right) {
2302 proj_nr = get_inversed_pnc(proj_nr);
2307 * Second step: Try to reduce the magnitude
2308 * of a constant. This may help to generate better code
2309 * later and may help to normalize more compares.
2310 * Of course this is only possible for integer values.
2313 mode = get_irn_mode(c);
2314 tv = get_Const_tarval(c);
2316 if (tv != tarval_bad) {
2317 /* the following optimization is possible on modes without Overflow
2318 * on Unary Minus or on == and !=:
2319 * -a CMP c ==> a swap(CMP) -c
2321 * Beware: for two-complement Overflow may occur, so only == and != can
2322 * be optimized, see this:
2323 * -MININT < 0 =/=> MININT > 0 !!!
2325 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2326 (!mode_overflow_on_unary_Minus(mode) ||
2327 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2328 left = get_Minus_op(left);
2329 tv = tarval_sub(get_mode_null(mode), tv);
2331 proj_nr = get_inversed_pnc(proj_nr);
2335 /* for integer modes, we have more */
2336 if (mode_is_int(mode)) {
2337 /* Ne includes Unordered which is not possible on integers.
2338 * However, frontends often use this wrong, so fix it here */
2339 if (proj_nr & pn_Cmp_Uo) {
2340 proj_nr &= ~pn_Cmp_Uo;
2341 set_Proj_proj(proj, proj_nr);
2344 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2345 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2346 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2347 tv = tarval_sub(tv, get_mode_one(mode));
2349 proj_nr ^= pn_Cmp_Eq;
2352 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2353 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2354 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2355 tv = tarval_add(tv, get_mode_one(mode));
2357 proj_nr ^= pn_Cmp_Eq;
2361 /* the following reassociations work only for == and != */
2362 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2364 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2365 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2366 right = get_Sub_right(left);
2367 left = get_Sub_left(left);
2369 tv = value_of(right);
2373 if (tv != tarval_bad) {
2374 ir_op *op = get_irn_op(left);
2376 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2378 ir_node *c1 = get_Sub_right(left);
2379 tarval *tv2 = value_of(c1);
2381 if (tv2 != tarval_bad) {
2382 tv2 = tarval_add(tv, value_of(c1));
2384 if (tv2 != tarval_bad) {
2385 left = get_Sub_left(left);
2391 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2392 else if (op == op_Add) {
2393 ir_node *a_l = get_Add_left(left);
2394 ir_node *a_r = get_Add_right(left);
2398 if (get_irn_op(a_l) == op_Const) {
2400 tv2 = value_of(a_l);
2404 tv2 = value_of(a_r);
2407 if (tv2 != tarval_bad) {
2408 tv2 = tarval_sub(tv, tv2);
2410 if (tv2 != tarval_bad) {
2417 /* -a == c ==> a == -c, -a != c ==> a != -c */
2418 else if (op == op_Minus) {
2419 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2421 if (tv2 != tarval_bad) {
2422 left = get_Minus_op(left);
2429 /* the following reassociations work only for <= */
2430 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2431 if (tv != tarval_bad) {
2432 ir_op *op = get_irn_op(left);
2434 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2444 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2446 if (changed & 2) /* need a new Const */
2447 right = new_Const(mode, tv);
2449 /* create a new compare */
2450 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2453 set_Proj_pred(proj, n);
2454 set_Proj_proj(proj, proj_nr);
2461 * Does all optimizations on nodes that must be done on it's Proj's
2462 * because of creating new nodes.
2464 static ir_node *transform_node_Proj(ir_node *proj)
2466 ir_node *n = get_Proj_pred(proj);
2468 switch (get_irn_opcode(n)) {
2470 return transform_node_Proj_Div(proj);
2473 return transform_node_Proj_Mod(proj);
2476 return transform_node_Proj_DivMod(proj);
2479 return transform_node_Proj_Cond(proj);
2482 return transform_node_Proj_Cmp(proj);
2485 /* should not happen, but if it does will be optimized away */
2486 return equivalent_node_Proj(proj);
2495 * returns the operands of a commutative bin-op, if one operand is
2496 * a const, it is returned as the second one.
2498 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2500 ir_node *op_a = get_binop_left(binop);
2501 ir_node *op_b = get_binop_right(binop);
2503 assert(is_op_commutative(get_irn_op(binop)));
2505 if (get_irn_op(op_a) == op_Const) {
2516 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2517 * Such pattern may arise in bitfield stores.
2519 * value c4 value c4 & c2
2520 * AND c3 AND c1 | c3
2525 static ir_node *transform_node_Or_bf_store(ir_node *or)
2529 ir_node *and_l, *c3;
2530 ir_node *value, *c4;
2531 ir_node *new_and, *new_const, *block;
2532 ir_mode *mode = get_irn_mode(or);
2534 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2536 get_comm_Binop_Ops(or, &and, &c1);
2537 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2540 get_comm_Binop_Ops(and, &or_l, &c2);
2541 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2544 get_comm_Binop_Ops(or_l, &and_l, &c3);
2545 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2548 get_comm_Binop_Ops(and_l, &value, &c4);
2549 if (get_irn_op(c4) != op_Const)
2552 /* ok, found the pattern, check for conditions */
2553 assert(mode == get_irn_mode(and));
2554 assert(mode == get_irn_mode(or_l));
2555 assert(mode == get_irn_mode(and_l));
2557 tv1 = get_Const_tarval(c1);
2558 tv2 = get_Const_tarval(c2);
2559 tv3 = get_Const_tarval(c3);
2560 tv4 = get_Const_tarval(c4);
2562 tv = tarval_or(tv4, tv2);
2563 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2564 /* have at least one 0 at the same bit position */
2568 n_tv4 = tarval_not(tv4);
2569 if (tv3 != tarval_and(tv3, n_tv4)) {
2570 /* bit in the or_mask is outside the and_mask */
2574 n_tv2 = tarval_not(tv2);
2575 if (tv1 != tarval_and(tv1, n_tv2)) {
2576 /* bit in the or_mask is outside the and_mask */
2580 /* ok, all conditions met */
2581 block = get_irn_n(or, -1);
2583 new_and = new_r_And(current_ir_graph, block,
2584 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2586 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2588 set_Or_left(or, new_and);
2589 set_Or_right(or, new_const);
2591 /* check for more */
2592 return transform_node_Or_bf_store(or);
2596 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2598 static ir_node *transform_node_Or_Rot(ir_node *or)
2600 ir_mode *mode = get_irn_mode(or);
2601 ir_node *shl, *shr, *block;
2602 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2605 if (! mode_is_int(mode))
2608 shl = get_binop_left(or);
2609 shr = get_binop_right(or);
2611 if (get_irn_op(shl) == op_Shr) {
2612 if (get_irn_op(shr) != op_Shl)
2619 else if (get_irn_op(shl) != op_Shl)
2621 else if (get_irn_op(shr) != op_Shr)
2624 x = get_Shl_left(shl);
2625 if (x != get_Shr_left(shr))
2628 c1 = get_Shl_right(shl);
2629 c2 = get_Shr_right(shr);
2630 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2631 tv1 = get_Const_tarval(c1);
2632 if (! tarval_is_long(tv1))
2635 tv2 = get_Const_tarval(c2);
2636 if (! tarval_is_long(tv2))
2639 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2640 != get_mode_size_bits(mode))
2643 /* yet, condition met */
2644 block = get_irn_n(or, -1);
2646 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2648 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2651 else if (get_irn_op(c1) == op_Sub) {
2655 if (get_Sub_right(sub) != v)
2658 c1 = get_Sub_left(sub);
2659 if (get_irn_op(c1) != op_Const)
2662 tv1 = get_Const_tarval(c1);
2663 if (! tarval_is_long(tv1))
2666 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2669 /* yet, condition met */
2670 block = get_nodes_block(or);
2672 /* a Rot right is not supported, so use a rot left */
2673 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
2675 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2678 else if (get_irn_op(c2) == op_Sub) {
2682 c1 = get_Sub_left(sub);
2683 if (get_irn_op(c1) != op_Const)
2686 tv1 = get_Const_tarval(c1);
2687 if (! tarval_is_long(tv1))
2690 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2693 /* yet, condition met */
2694 block = get_irn_n(or, -1);
2697 n = new_r_Rot(current_ir_graph, block, x, v, mode);
2699 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2709 static ir_node *transform_node_Or(ir_node *or)
2711 or = transform_node_Or_bf_store(or);
2712 or = transform_node_Or_Rot(or);
2718 static ir_node *transform_node(ir_node *n);
2721 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2723 * Should be moved to reassociation?
2725 static ir_node *transform_node_shift(ir_node *n)
2727 ir_node *left, *right;
2728 tarval *tv1, *tv2, *res;
2730 int modulo_shf, flag;
2732 left = get_binop_left(n);
2734 /* different operations */
2735 if (get_irn_op(left) != get_irn_op(n))
2738 right = get_binop_right(n);
2739 tv1 = value_of(right);
2740 if (tv1 == tarval_bad)
2743 tv2 = value_of(get_binop_right(left));
2744 if (tv2 == tarval_bad)
2747 res = tarval_add(tv1, tv2);
2749 /* beware: a simple replacement works only, if res < modulo shift */
2750 mode = get_irn_mode(n);
2754 modulo_shf = get_mode_modulo_shift(mode);
2755 if (modulo_shf > 0) {
2756 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2758 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2765 /* ok, we can replace it */
2766 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2768 in[0] = get_binop_left(left);
2769 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2771 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2773 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2775 return transform_node(irn);
2780 #define transform_node_Shr transform_node_shift
2781 #define transform_node_Shrs transform_node_shift
2782 #define transform_node_Shl transform_node_shift
2785 * Remove dead blocks and nodes in dead blocks
2786 * in keep alive list. We do not generate a new End node.
2788 static ir_node *transform_node_End(ir_node *n) {
2789 int i, n_keepalives = get_End_n_keepalives(n);
2791 for (i = 0; i < n_keepalives; ++i) {
2792 ir_node *ka = get_End_keepalive(n, i);
2794 if (is_Block_dead(ka)) {
2795 set_End_keepalive(n, i, new_Bad());
2798 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2799 set_End_keepalive(n, i, new_Bad());
2805 * Optimize a Mux into some simpler cases.
2807 static ir_node *transform_node_Mux(ir_node *n)
2809 ir_node *oldn = n, *sel = get_Mux_sel(n);
2810 ir_mode *mode = get_irn_mode(n);
2812 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2813 ir_node *cmp = get_Proj_pred(sel);
2814 long proj_nr = get_Proj_proj(sel);
2815 ir_node *f = get_Mux_false(n);
2816 ir_node *t = get_Mux_true(n);
2818 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2819 ir_node *block = get_irn_n(n, -1);
2822 * Note: normalization puts the constant on the right site,
2823 * so we check only one case.
2825 * Note further that these optimization work even for floating point
2826 * with NaN's because -NaN == NaN.
2827 * However, if +0 and -0 is handled differently, we cannot use the first one.
2829 if (get_irn_op(f) == op_Minus &&
2830 get_Minus_op(f) == t &&
2831 get_Cmp_left(cmp) == t) {
2833 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2834 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
2835 n = new_rd_Abs(get_irn_dbg_info(n),
2839 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2842 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2843 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
2844 n = new_rd_Abs(get_irn_dbg_info(n),
2848 n = new_rd_Minus(get_irn_dbg_info(n),
2853 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2857 else if (get_irn_op(t) == op_Minus &&
2858 get_Minus_op(t) == f &&
2859 get_Cmp_left(cmp) == f) {
2861 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2862 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
2863 n = new_rd_Abs(get_irn_dbg_info(n),
2867 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2870 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2871 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
2872 n = new_rd_Abs(get_irn_dbg_info(n),
2876 n = new_rd_Minus(get_irn_dbg_info(n),
2881 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2886 if (mode_is_int(mode) && mode_is_signed(mode) &&
2887 get_mode_arithmetic(mode) == irma_twos_complement) {
2888 ir_node *x = get_Cmp_left(cmp);
2890 /* the following optimization works only with signed integer two-complement mode */
2892 if (mode == get_irn_mode(x)) {
2894 * FIXME: this restriction is two rigid, as it would still
2895 * work if mode(x) = Hs and mode == Is, but at least it removes
2898 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
2899 classify_Const(t) == CNST_ALL_ONE &&
2900 classify_Const(f) == CNST_NULL) {
2902 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
2906 n = new_rd_Shrs(get_irn_dbg_info(n),
2907 current_ir_graph, block, x,
2908 new_r_Const_long(current_ir_graph, block, mode_Iu,
2909 get_mode_size_bits(mode) - 1),
2911 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
2914 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
2915 classify_Const(t) == CNST_ONE &&
2916 classify_Const(f) == CNST_NULL) {
2918 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
2922 n = new_rd_Shr(get_irn_dbg_info(n),
2923 current_ir_graph, block,
2924 new_r_Minus(current_ir_graph, block, x, mode),
2925 new_r_Const_long(current_ir_graph, block, mode_Iu,
2926 get_mode_size_bits(mode) - 1),
2928 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
2935 return arch_transform_node_Mux(n);
2939 * Tries several [inplace] [optimizing] transformations and returns an
2940 * equivalent node. The difference to equivalent_node() is that these
2941 * transformations _do_ generate new nodes, and thus the old node must
2942 * not be freed even if the equivalent node isn't the old one.
2944 static ir_node *transform_node(ir_node *n)
2946 if (n->op->ops.transform_node)
2947 n = n->op->ops.transform_node(n);
2952 * sSets the default transform node operation for an ir_op_ops.
2954 * @param code the opcode for the default operation
2955 * @param ops the operations initialized
2960 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
2964 ops->transform_node = transform_node_##a; \
2996 /* **************** Common Subexpression Elimination **************** */
2998 /** The size of the hash table used, should estimate the number of nodes
3000 #define N_IR_NODES 512
3002 /** Compares the attributes of two Const nodes. */
3003 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3005 return (get_Const_tarval(a) != get_Const_tarval(b))
3006 || (get_Const_type(a) != get_Const_type(b));
3009 /** Compares the attributes of two Proj nodes. */
3010 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3012 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3015 /** Compares the attributes of two Filter nodes. */
3016 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3018 return get_Filter_proj(a) != get_Filter_proj(b);
3021 /** Compares the attributes of two Alloc nodes. */
3022 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3024 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3025 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3028 /** Compares the attributes of two Free nodes. */
3029 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3031 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3032 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3035 /** Compares the attributes of two SymConst nodes. */
3036 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3038 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3039 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3040 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3043 /** Compares the attributes of two Call nodes. */
3044 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3046 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3049 /** Compares the attributes of two Sel nodes. */
3050 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3052 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3053 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3054 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3055 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3056 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3059 /** Compares the attributes of two Phi nodes. */
3060 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3062 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3065 /** Compares the attributes of two Cast nodes. */
3066 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3068 return get_Cast_type(a) != get_Cast_type(b);
3071 /** Compares the attributes of two Load nodes. */
3072 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3074 if (get_Load_volatility(a) == volatility_is_volatile ||
3075 get_Load_volatility(b) == volatility_is_volatile)
3076 /* NEVER do CSE on volatile Loads */
3079 return get_Load_mode(a) != get_Load_mode(b);
3082 /** Compares the attributes of two Store nodes. */
3083 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3085 /* NEVER do CSE on volatile Stores */
3086 return (get_Store_volatility(a) == volatility_is_volatile ||
3087 get_Store_volatility(b) == volatility_is_volatile);
3090 /** Compares the attributes of two Confirm nodes. */
3091 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3093 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3097 * Set the default node attribute compare operation for an ir_op_ops.
3099 * @param code the opcode for the default operation
3100 * @param ops the operations initialized
3105 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3109 ops->node_cmp_attr = node_cmp_attr_##a; \
3135 * Compare function for two nodes in the hash table. Gets two
3136 * nodes as parameters. Returns 0 if the nodes are a cse.
3138 int identities_cmp(const void *elt, const void *key)
3146 if (a == b) return 0;
3148 if ((get_irn_op(a) != get_irn_op(b)) ||
3149 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3151 /* compare if a's in and b's in are of equal length */
3152 irn_arity_a = get_irn_intra_arity (a);
3153 if (irn_arity_a != get_irn_intra_arity(b))
3156 /* for block-local cse and op_pin_state_pinned nodes: */
3157 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3158 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3162 /* compare a->in[0..ins] with b->in[0..ins] */
3163 for (i = 0; i < irn_arity_a; i++)
3164 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3168 * here, we already now that the nodes are identical except their
3171 if (a->op->ops.node_cmp_attr)
3172 return a->op->ops.node_cmp_attr(a, b);
3178 * Calculate a hash value of a node.
3181 ir_node_hash (ir_node *node)
3186 if (node->op == op_Const) {
3187 /* special value for const, as they only differ in their tarval. */
3188 h = HASH_PTR(node->attr.con.tv);
3189 h = 9*h + HASH_PTR(get_irn_mode(node));
3190 } else if (node->op == op_SymConst) {
3191 /* special value for const, as they only differ in their symbol. */
3192 h = HASH_PTR(node->attr.i.sym.type_p);
3193 h = 9*h + HASH_PTR(get_irn_mode(node));
3196 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3197 h = irn_arity = get_irn_intra_arity(node);
3199 /* consider all in nodes... except the block if not a control flow. */
3200 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3201 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3205 h = 9*h + HASH_PTR(get_irn_mode(node));
3207 h = 9*h + HASH_PTR(get_irn_op(node));
3214 new_identities(void) {
3215 return new_pset(identities_cmp, N_IR_NODES);
3219 del_identities(pset *value_table) {
3220 del_pset(value_table);
3224 * Return the canonical node computing the same value as n.
3225 * Looks up the node in a hash table.
3227 * For Const nodes this is performed in the constructor, too. Const
3228 * nodes are extremely time critical because of their frequent use in
3229 * constant string arrays.
3231 static INLINE ir_node *
3232 identify (pset *value_table, ir_node *n)
3236 if (!value_table) return n;
3238 if (get_opt_reassociation()) {
3239 if (is_op_commutative(get_irn_op(n))) {
3240 ir_node *l = get_binop_left(n);
3241 ir_node *r = get_binop_right(n);
3243 /* for commutative operators perform a OP b == b OP a */
3245 set_binop_left(n, r);
3246 set_binop_right(n, l);
3251 o = pset_find (value_table, n, ir_node_hash (n));
3260 * During construction we set the op_pin_state_pinned flag in the graph right when the
3261 * optimization is performed. The flag turning on procedure global cse could
3262 * be changed between two allocations. This way we are safe.
3264 static INLINE ir_node *
3265 identify_cons (pset *value_table, ir_node *n) {
3268 n = identify(value_table, n);
3269 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3270 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3275 * Return the canonical node computing the same value as n.
3276 * Looks up the node in a hash table, enters it in the table
3277 * if it isn't there yet.
3280 identify_remember (pset *value_table, ir_node *n)
3284 if (!value_table) return n;
3286 if (get_opt_reassociation()) {
3287 if (is_op_commutative(get_irn_op(n))) {
3288 ir_node *l = get_binop_left(n);
3289 ir_node *r = get_binop_right(n);
3291 /* for commutative operators perform a OP b == b OP a */
3293 set_binop_left(n, r);
3294 set_binop_right(n, l);
3299 /* lookup or insert in hash table with given hash key. */
3300 o = pset_insert (value_table, n, ir_node_hash (n));
3310 add_identities (pset *value_table, ir_node *node) {
3311 if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
3312 identify_remember (value_table, node);
3316 * garbage in, garbage out. If a node has a dead input, i.e., the
3317 * Bad node is input to the node, return the Bad node.
3319 static INLINE ir_node *
3320 gigo (ir_node *node)
3323 ir_op *op = get_irn_op(node);
3325 /* remove garbage blocks by looking at control flow that leaves the block
3326 and replacing the control flow by Bad. */
3327 if (get_irn_mode(node) == mode_X) {
3328 ir_node *block = get_nodes_block(skip_Proj(node));
3330 /* Don't optimize nodes in immature blocks. */
3331 if (!get_Block_matured(block)) return node;
3332 /* Don't optimize End, may have Bads. */
3333 if (op == op_End) return node;
3335 if (is_Block(block)) {
3336 irn_arity = get_irn_arity(block);
3337 for (i = 0; i < irn_arity; i++) {
3338 if (!is_Bad(get_irn_n(block, i)))
3341 if (i == irn_arity) return new_Bad();
3345 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3346 blocks predecessors is dead. */
3347 if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3348 irn_arity = get_irn_arity(node);
3351 * Beware: we can only read the block of a non-floating node.
3353 if (is_irn_pinned_in_irg(node) &&
3354 is_Block_dead(get_nodes_block(node)))
3357 for (i = 0; i < irn_arity; i++) {
3358 ir_node *pred = get_irn_n(node, i);
3362 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3363 return new_Unknown(get_irn_mode(node));
3367 /* With this code we violate the agreement that local_optimize
3368 only leaves Bads in Block, Phi and Tuple nodes. */
3369 /* If Block has only Bads as predecessors it's garbage. */
3370 /* If Phi has only Bads as predecessors it's garbage. */
3371 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3372 irn_arity = get_irn_arity(node);
3373 for (i = 0; i < irn_arity; i++) {
3374 if (!is_Bad(get_irn_n(node, i))) break;
3376 if (i == irn_arity) node = new_Bad();
3384 * These optimizations deallocate nodes from the obstack.
3385 * It can only be called if it is guaranteed that no other nodes
3386 * reference this one, i.e., right after construction of a node.
3388 * current_ir_graph must be set to the graph of the node!
3391 optimize_node(ir_node *n)
3395 opcode iro = get_irn_opcode(n);
3397 /* Always optimize Phi nodes: part of the construction. */
3398 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3400 /* constant expression evaluation / constant folding */
3401 if (get_opt_constant_folding()) {
3402 /* neither constants nor Tuple values can be evaluated */
3403 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3404 /* try to evaluate */
3405 tv = computed_value(n);
3406 if (tv != tarval_bad) {
3408 ir_type *old_tp = get_irn_type(n);
3409 int i, arity = get_irn_arity(n);
3413 * Try to recover the type of the new expression.
3415 for (i = 0; i < arity && !old_tp; ++i)
3416 old_tp = get_irn_type(get_irn_n(n, i));
3419 * we MUST copy the node here temporary, because it's still needed
3420 * for DBG_OPT_CSTEVAL
3422 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3423 oldn = alloca(node_size);
3425 memcpy(oldn, n, node_size);
3426 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3428 /* ARG, copy the in array, we need it for statistics */
3429 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3431 /* note the inplace edges module */
3432 edges_node_deleted(n, current_ir_graph);
3434 /* evaluation was successful -- replace the node. */
3435 obstack_free(current_ir_graph->obst, n);
3436 nw = new_Const(get_tarval_mode (tv), tv);
3438 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3439 set_Const_type(nw, old_tp);
3440 DBG_OPT_CSTEVAL(oldn, nw);
3446 /* remove unnecessary nodes */
3447 if (get_opt_constant_folding() ||
3448 (iro == iro_Phi) || /* always optimize these nodes. */
3450 (iro == iro_Proj) ||
3451 (iro == iro_Block) ) /* Flags tested local. */
3452 n = equivalent_node (n);
3454 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3456 /* Common Subexpression Elimination.
3458 * Checks whether n is already available.
3459 * The block input is used to distinguish different subexpressions. Right
3460 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3461 * subexpressions within a block.
3464 n = identify_cons (current_ir_graph->value_table, n);
3467 edges_node_deleted(oldn, current_ir_graph);
3469 /* We found an existing, better node, so we can deallocate the old node. */
3470 obstack_free (current_ir_graph->obst, oldn);
3475 /* Some more constant expression evaluation that does not allow to
3477 iro = get_irn_opcode(n);
3478 if (get_opt_constant_folding() ||
3479 (iro == iro_Cond) ||
3480 (iro == iro_Proj) ||
3481 (iro == iro_Sel)) /* Flags tested local. */
3482 n = transform_node (n);
3484 /* Remove nodes with dead (Bad) input.
3485 Run always for transformation induced Bads. */
3488 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3489 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3490 n = identify_remember (current_ir_graph->value_table, n);
3498 * These optimizations never deallocate nodes (in place). This can cause dead
3499 * nodes lying on the obstack. Remove these by a dead node elimination,
3500 * i.e., a copying garbage collection.
3503 optimize_in_place_2 (ir_node *n)
3507 opcode iro = get_irn_opcode(n);
3509 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3511 /* constant expression evaluation / constant folding */
3512 if (get_opt_constant_folding()) {
3513 /* neither constants nor Tuple values can be evaluated */
3514 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3515 /* try to evaluate */
3516 tv = computed_value(n);
3517 if (tv != tarval_bad) {
3518 /* evaluation was successful -- replace the node. */
3519 ir_type *old_tp = get_irn_type(n);
3520 int i, arity = get_irn_arity(n);
3523 * Try to recover the type of the new expression.
3525 for (i = 0; i < arity && !old_tp; ++i)
3526 old_tp = get_irn_type(get_irn_n(n, i));
3528 n = new_Const(get_tarval_mode(tv), tv);
3530 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3531 set_Const_type(n, old_tp);
3533 DBG_OPT_CSTEVAL(oldn, n);
3539 /* remove unnecessary nodes */
3540 if (get_opt_constant_folding() ||
3541 (iro == iro_Phi) || /* always optimize these nodes. */
3542 (iro == iro_Id) || /* ... */
3543 (iro == iro_Proj) || /* ... */
3544 (iro == iro_Block) ) /* Flags tested local. */
3545 n = equivalent_node(n);
3547 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3549 /** common subexpression elimination **/
3550 /* Checks whether n is already available. */
3551 /* The block input is used to distinguish different subexpressions. Right
3552 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3553 subexpressions within a block. */
3554 if (get_opt_cse()) {
3555 n = identify(current_ir_graph->value_table, n);
3558 /* Some more constant expression evaluation. */
3559 iro = get_irn_opcode(n);
3560 if (get_opt_constant_folding() ||
3561 (iro == iro_Cond) ||
3562 (iro == iro_Proj) ||
3563 (iro == iro_Sel)) /* Flags tested local. */
3564 n = transform_node(n);
3566 /* Remove nodes with dead (Bad) input.
3567 Run always for transformation induced Bads. */
3570 /* Now we can verify the node, as it has no dead inputs any more. */
3573 /* Now we have a legal, useful node. Enter it in hash table for cse.
3574 Blocks should be unique anyways. (Except the successor of start:
3575 is cse with the start block!) */
3576 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3577 n = identify_remember(current_ir_graph->value_table, n);
3583 * Wrapper for external use, set proper status bits after optimization.
3586 optimize_in_place (ir_node *n)
3588 /* Handle graph state */
3589 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3591 if (get_opt_global_cse())
3592 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3593 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3594 set_irg_outs_inconsistent(current_ir_graph);
3596 /* FIXME: Maybe we could also test whether optimizing the node can
3597 change the control graph. */
3598 set_irg_doms_inconsistent(current_ir_graph);
3599 return optimize_in_place_2 (n);
3603 * Sets the default operation for an ir_ops.
3605 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3607 ops = firm_set_default_computed_value(code, ops);
3608 ops = firm_set_default_equivalent_node(code, ops);
3609 ops = firm_set_default_transform_node(code, ops);
3610 ops = firm_set_default_node_cmp_attr(code, ops);
3611 ops = firm_set_default_get_type(code, ops);
3612 ops = firm_set_default_get_type_attr(code, ops);
3613 ops = firm_set_default_get_entity_attr(code, ops);