Renamed attribute names to more understandable names.
[libfirm] / ir / ir / iropt.c
1 /*
2  * Project:     libFIRM
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
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2005 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_ALLOCA_H
18 #include <alloca.h>
19 #endif
20 #ifdef HAVE_MALLOC_H
21 #include <malloc.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26
27 #include "irnode_t.h"
28 #include "irgraph_t.h"
29 #include "iredges_t.h"
30 #include "irmode_t.h"
31 #include "iropt_t.h"
32 #include "ircons_t.h"
33 #include "irgmod.h"
34 #include "irvrfy.h"
35 #include "tv_t.h"
36 #include "dbginfo_t.h"
37 #include "iropt_dbg.h"
38 #include "irflag_t.h"
39 #include "irhooks.h"
40 #include "irarch.h"
41 #include "hashptr.h"
42 #include "archop.h"
43 #include "opt_polymorphy.h"
44 #include "opt_confirms.h"
45 #include "irtools.h"
46
47 /* Make types visible to allow most efficient access */
48 # include "entity_t.h"
49
50 /**
51  * return the value of a Constant
52  */
53 static tarval *computed_value_Const(ir_node *n)
54 {
55   return get_Const_tarval(n);
56 }
57
58 /**
59  * return the value of a 'sizeof' SymConst
60  */
61 static tarval *computed_value_SymConst(ir_node *n)
62 {
63   ir_type *type;
64
65   switch (get_SymConst_kind(n)) {
66   case symconst_type_size:
67     type = get_SymConst_type(n);
68     if (get_type_state(type) == layout_fixed)
69       return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
70     break;
71   case symconst_type_align:
72     type = get_SymConst_type(n);
73     if (get_type_state(type) == layout_fixed)
74       return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
75     break;
76   default:
77     break;
78   }
79   return tarval_bad;
80 }
81
82 /**
83  * return the value of an Add
84  */
85 static tarval *computed_value_Add(ir_node *n)
86 {
87   ir_node *a = get_Add_left(n);
88   ir_node *b = get_Add_right(n);
89
90   tarval *ta = value_of(a);
91   tarval *tb = value_of(b);
92
93   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
94     return tarval_add(ta, tb);
95
96   return tarval_bad;
97 }
98
99 /**
100  * return the value of a Sub
101  * Special case: a - a
102  */
103 static tarval *computed_value_Sub(ir_node *n)
104 {
105   ir_node *a = get_Sub_left(n);
106   ir_node *b = get_Sub_right(n);
107   tarval *ta;
108   tarval *tb;
109
110   /* a - a */
111   if (a == b && !is_Bad(a))
112     return get_mode_null(get_irn_mode(n));
113
114   ta = value_of(a);
115   tb = value_of(b);
116
117   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
118     return tarval_sub(ta, tb);
119
120   return tarval_bad;
121 }
122
123 /**
124  * return the value of a Carry
125  * Special : a op 0, 0 op b
126  */
127 static tarval *computed_value_Carry(ir_node *n)
128 {
129   ir_node *a = get_binop_left(n);
130   ir_node *b = get_binop_right(n);
131   ir_mode *m = get_irn_mode(n);
132
133   tarval *ta = value_of(a);
134   tarval *tb = value_of(b);
135
136   if ((ta != tarval_bad) && (tb != tarval_bad)) {
137     tarval_add(ta, tb);
138     return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
139   } else {
140     if (   (classify_tarval(ta) == TV_CLASSIFY_NULL)
141         || (classify_tarval(tb) == TV_CLASSIFY_NULL))
142       return get_mode_null(m);
143   }
144   return tarval_bad;
145 }
146
147 /**
148  * return the value of a Borrow
149  * Special : a op 0
150  */
151 static tarval *computed_value_Borrow(ir_node *n)
152 {
153   ir_node *a = get_binop_left(n);
154   ir_node *b = get_binop_right(n);
155   ir_mode *m = get_irn_mode(n);
156
157   tarval *ta = value_of(a);
158   tarval *tb = value_of(b);
159
160   if ((ta != tarval_bad) && (tb != tarval_bad)) {
161     return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
162   } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
163       return get_mode_null(m);
164   }
165   return tarval_bad;
166 }
167
168 /**
169  * return the value of an unary Minus
170  */
171 static tarval *computed_value_Minus(ir_node *n)
172 {
173   ir_node *a = get_Minus_op(n);
174   tarval *ta = value_of(a);
175
176   if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
177     return tarval_neg(ta);
178
179   return tarval_bad;
180 }
181
182 /**
183  * return the value of a Mul
184  */
185 static tarval *computed_value_Mul(ir_node *n)
186 {
187   ir_node *a = get_Mul_left(n);
188   ir_node *b = get_Mul_right(n);
189
190   tarval *ta = value_of(a);
191   tarval *tb = value_of(b);
192
193   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
194     return tarval_mul(ta, tb);
195   } else {
196     /* a*0 = 0 or 0*b = 0:
197        calls computed_value recursive and returns the 0 with proper
198        mode. */
199     if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
200       return ta;
201     if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
202       return tb;
203   }
204   return tarval_bad;
205 }
206
207 /**
208  * return the value of a floating point Quot
209  */
210 static tarval *computed_value_Quot(ir_node *n)
211 {
212   ir_node *a = get_Quot_left(n);
213   ir_node *b = get_Quot_right(n);
214
215   tarval *ta = value_of(a);
216   tarval *tb = value_of(b);
217
218   /* This was missing in original implementation. Why? */
219   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
220     if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
221       return tarval_quo(ta, tb);
222   }
223   return tarval_bad;
224 }
225
226 /**
227  * calculate the value of an integer Div of two nodes
228  * Special case: 0 / b
229  */
230 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
231 {
232   tarval *ta = value_of(a);
233   tarval *tb = value_of(b);
234
235   /* Compute c1 / c2 or 0 / a, a != 0 */
236   if (ta != tarval_bad) {
237     if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b))))   /* div by zero: return tarval_bad */
238       return tarval_div(ta, tb);
239     else if (ta == get_mode_null(get_tarval_mode(ta)))  /* 0 / b == 0 */
240       return ta;
241   }
242   return tarval_bad;
243 }
244
245 /**
246  * return the value of an integer Div
247  */
248 static tarval *computed_value_Div(ir_node *n)
249 {
250   return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
251 }
252
253 /**
254  * calculate the value of an integer Mod of two nodes
255  * Special case: a % 1
256  */
257 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
258 {
259   tarval *ta = value_of(a);
260   tarval *tb = value_of(b);
261
262   /* Compute c1 % c2 or a % 1 */
263   if (tb != tarval_bad) {
264     if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb))))   /* div by zero: return tarval_bad */
265       return tarval_mod(ta, tb);
266     else if (tb == get_mode_one(get_tarval_mode(tb)))    /* x mod 1 == 0 */
267       return get_mode_null(get_irn_mode(a));
268   }
269
270   return tarval_bad;
271 }
272
273 /**
274  * return the value of an integer Mod
275  */
276 static tarval *computed_value_Mod(ir_node *n)
277 {
278   return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
279 }
280
281 /**
282  * return the value of an Abs
283  */
284 static tarval *computed_value_Abs(ir_node *n)
285 {
286   ir_node *a = get_Abs_op(n);
287   tarval *ta = value_of(a);
288
289   if (ta != tarval_bad)
290     return tarval_abs(ta);
291
292   return tarval_bad;
293 }
294
295 /**
296  * return the value of an And
297  * Special case: a & 0, 0 & b
298  */
299 static tarval *computed_value_And(ir_node *n)
300 {
301   ir_node *a = get_And_left(n);
302   ir_node *b = get_And_right(n);
303
304   tarval *ta = value_of(a);
305   tarval *tb = value_of(b);
306
307   if ((ta != tarval_bad) && (tb != tarval_bad)) {
308     return tarval_and (ta, tb);
309   } else {
310     tarval *v;
311
312     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
313         || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
314       return v;
315     }
316   }
317   return tarval_bad;
318 }
319
320 /**
321  * return the value of an Or
322  * Special case: a | 1...1, 1...1 | b
323  */
324 static tarval *computed_value_Or(ir_node *n)
325 {
326   ir_node *a = get_Or_left(n);
327   ir_node *b = get_Or_right(n);
328
329   tarval *ta = value_of(a);
330   tarval *tb = value_of(b);
331
332   if ((ta != tarval_bad) && (tb != tarval_bad)) {
333     return tarval_or (ta, tb);
334   } else {
335     tarval *v;
336     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
337         || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
338       return v;
339     }
340   }
341   return tarval_bad;
342 }
343
344 /**
345  * return the value of an Eor
346  */
347 static tarval *computed_value_Eor(ir_node *n)
348 {
349   ir_node *a = get_Eor_left(n);
350   ir_node *b = get_Eor_right(n);
351
352   tarval *ta, *tb;
353
354   if (a == b)
355     return get_mode_null(get_irn_mode(n));
356
357   ta = value_of(a);
358   tb = value_of(b);
359
360   if ((ta != tarval_bad) && (tb != tarval_bad)) {
361     return tarval_eor (ta, tb);
362   }
363   return tarval_bad;
364 }
365
366 /**
367  * return the value of a Not
368  */
369 static tarval *computed_value_Not(ir_node *n)
370 {
371   ir_node *a = get_Not_op(n);
372   tarval *ta = value_of(a);
373
374   if (ta != tarval_bad)
375     return tarval_not(ta);
376
377   return tarval_bad;
378 }
379
380 /**
381  * return the value of a Shl
382  */
383 static tarval *computed_value_Shl(ir_node *n)
384 {
385   ir_node *a = get_Shl_left(n);
386   ir_node *b = get_Shl_right(n);
387
388   tarval *ta = value_of(a);
389   tarval *tb = value_of(b);
390
391   if ((ta != tarval_bad) && (tb != tarval_bad)) {
392     return tarval_shl (ta, tb);
393   }
394   return tarval_bad;
395 }
396
397 /**
398  * return the value of a Shr
399  */
400 static tarval *computed_value_Shr(ir_node *n)
401 {
402   ir_node *a = get_Shr_left(n);
403   ir_node *b = get_Shr_right(n);
404
405   tarval *ta = value_of(a);
406   tarval *tb = value_of(b);
407
408   if ((ta != tarval_bad) && (tb != tarval_bad)) {
409     return tarval_shr (ta, tb);
410   }
411   return tarval_bad;
412 }
413
414 /**
415  * return the value of a Shrs
416  */
417 static tarval *computed_value_Shrs(ir_node *n)
418 {
419   ir_node *a = get_Shrs_left(n);
420   ir_node *b = get_Shrs_right(n);
421
422   tarval *ta = value_of(a);
423   tarval *tb = value_of(b);
424
425   if ((ta != tarval_bad) && (tb != tarval_bad)) {
426     return tarval_shrs (ta, tb);
427   }
428   return tarval_bad;
429 }
430
431 /**
432  * return the value of a Rot
433  */
434 static tarval *computed_value_Rot(ir_node *n)
435 {
436   ir_node *a = get_Rot_left(n);
437   ir_node *b = get_Rot_right(n);
438
439   tarval *ta = value_of(a);
440   tarval *tb = value_of(b);
441
442   if ((ta != tarval_bad) && (tb != tarval_bad)) {
443     return tarval_rot (ta, tb);
444   }
445   return tarval_bad;
446 }
447
448 /**
449  * return the value of a Conv
450  */
451 static tarval *computed_value_Conv(ir_node *n)
452 {
453   ir_node *a = get_Conv_op(n);
454   tarval *ta = value_of(a);
455
456   if (ta != tarval_bad)
457     return tarval_convert_to(ta, get_irn_mode(n));
458
459   return tarval_bad;
460 }
461
462 /**
463  * return the value of a Proj(Cmp)
464  *
465  * This performs a first step of unreachable code elimination.
466  * Proj can not be computed, but folding a Cmp above the Proj here is
467  * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
468  * only 1 is used.
469  * There are several case where we can evaluate a Cmp node, see later.
470  */
471 static tarval *computed_value_Proj_Cmp(ir_node *n)
472 {
473   ir_node *a   = get_Proj_pred(n);
474   ir_node *aa  = get_Cmp_left(a);
475   ir_node *ab  = get_Cmp_right(a);
476   long proj_nr = get_Proj_proj(n);
477
478   /*
479    * BEWARE: a == a is NOT always True for floating Point values, as
480    * NaN != NaN is defined, so we must check this here.
481    */
482   if (aa == ab && (
483       !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt ||  proj_nr == pn_Cmp_Gt)
484       ) { /* 1.: */
485
486     /* This is a trick with the bits used for encoding the Cmp
487        Proj numbers, the following statement is not the same:
488     return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
489     return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
490   }
491   else {
492     tarval *taa = value_of(aa);
493     tarval *tab = value_of(ab);
494     ir_mode *mode = get_irn_mode(aa);
495
496     /*
497      * The predecessors of Cmp are target values.  We can evaluate
498      * the Cmp.
499      */
500     if ((taa != tarval_bad) && (tab != tarval_bad)) {
501       /* strange checks... */
502       pn_Cmp flags = tarval_cmp(taa, tab);
503       if (flags != pn_Cmp_False) {
504         return new_tarval_from_long (proj_nr & flags, mode_b);
505       }
506     }
507     /* for integer values, we can check against MIN/MAX */
508     else if (mode_is_int(mode)) {
509       /* MIN <=/> x.  This results in true/false. */
510       if (taa == get_mode_min(mode)) {
511         /* a compare with the MIN value */
512         if (proj_nr == pn_Cmp_Le)
513           return get_tarval_b_true();
514         else if (proj_nr == pn_Cmp_Gt)
515           return get_tarval_b_false();
516       }
517       /* x >=/< MIN.  This results in true/false. */
518       else
519       if (tab == get_mode_min(mode)) {
520         /* a compare with the MIN value */
521         if (proj_nr == pn_Cmp_Ge)
522           return get_tarval_b_true();
523         else if (proj_nr == pn_Cmp_Lt)
524           return get_tarval_b_false();
525       }
526       /* MAX >=/< x.  This results in true/false. */
527       else if (taa == get_mode_max(mode)) {
528         if (proj_nr == pn_Cmp_Ge)
529           return get_tarval_b_true();
530         else if (proj_nr == pn_Cmp_Lt)
531           return get_tarval_b_false();
532       }
533       /* x <=/> MAX.  This results in true/false. */
534       else if (tab == get_mode_max(mode)) {
535         if (proj_nr == pn_Cmp_Le)
536           return get_tarval_b_true();
537         else if (proj_nr == pn_Cmp_Gt)
538           return get_tarval_b_false();
539       }
540     }
541     /*
542      * The predecessors are Allocs or (void*)(0) constants.  Allocs never
543      * return NULL, they raise an exception.   Therefore we can predict
544      * the Cmp result.
545      */
546     else {
547       ir_node *aaa = skip_Id(skip_Proj(aa));
548       ir_node *aba = skip_Id(skip_Proj(ab));
549
550       if (   (   (/* aa is ProjP and aaa is Alloc */
551                      (get_irn_op(aa) == op_Proj)
552                   && (mode_is_reference(get_irn_mode(aa)))
553                   && (get_irn_op(aaa) == op_Alloc))
554               && (   (/* ab is NULL */
555                          (get_irn_op(ab) == op_Const)
556                       && (mode_is_reference(get_irn_mode(ab)))
557                       && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
558                   || (/* ab is other Alloc */
559                          (get_irn_op(ab) == op_Proj)
560                       && (mode_is_reference(get_irn_mode(ab)))
561                       && (get_irn_op(aba) == op_Alloc)
562                       && (aaa != aba))))
563           || (/* aa is NULL and aba is Alloc */
564                  (get_irn_op(aa) == op_Const)
565               && (mode_is_reference(get_irn_mode(aa)))
566               && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
567               && (get_irn_op(ab) == op_Proj)
568               && (mode_is_reference(get_irn_mode(ab)))
569               && (get_irn_op(aba) == op_Alloc)))
570         /* 3.: */
571         return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
572     }
573   }
574   return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
575 }
576
577 /**
578  * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
579  */
580 static tarval *computed_value_Proj(ir_node *n)
581 {
582   ir_node *a = get_Proj_pred(n);
583   long proj_nr;
584
585   switch (get_irn_opcode(a)) {
586   case iro_Cmp:
587     return computed_value_Proj_Cmp(n);
588
589   case iro_DivMod:
590     /* compute either the Div or the Mod part */
591     proj_nr = get_Proj_proj(n);
592     if (proj_nr == pn_DivMod_res_div)
593       return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
594     else if (proj_nr == pn_DivMod_res_mod)
595       return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
596     break;
597
598   case iro_Div:
599     if (get_Proj_proj(n) == pn_Div_res)
600       return computed_value(a);
601     break;
602
603   case iro_Mod:
604     if (get_Proj_proj(n) == pn_Mod_res)
605       return computed_value(a);
606     break;
607
608   default:
609     return tarval_bad;
610   }
611   return tarval_bad;
612 }
613
614 /**
615  * calculate the value of a Mux: can be evaluated, if the
616  * sel and the right input are known
617  */
618 static tarval *computed_value_Mux(ir_node *n)
619 {
620   ir_node *sel = get_Mux_sel(n);
621   tarval *ts = value_of(sel);
622
623   if (ts == get_tarval_b_true()) {
624     ir_node *v = get_Mux_true(n);
625     return value_of(v);
626   }
627   else if (ts == get_tarval_b_false()) {
628     ir_node *v = get_Mux_false(n);
629     return value_of(v);
630   }
631   return tarval_bad;
632 }
633
634 /**
635  * Calculate the value of a Psi: can be evaluated, if a condition is true
636  * and all previous conditions are false. If all conditions are false
637  * we evaluate to the default one.
638  */
639 static tarval *computed_value_Psi(ir_node *n)
640 {
641   if (is_Mux(n))
642     return computed_value_Mux(n);
643   return tarval_bad;
644 }
645
646 /**
647  * calculate the value of a Confirm: can be evaluated,
648  * if it has the form Confirm(x, '=', Const).
649  */
650 static tarval *computed_value_Confirm(ir_node *n)
651 {
652   return get_Confirm_cmp(n) == pn_Cmp_Eq ?
653     value_of(get_Confirm_bound(n)) : tarval_bad;
654 }
655
656 /**
657  * If the parameter n can be computed, return its value, else tarval_bad.
658  * Performs constant folding.
659  *
660  * @param n  The node this should be evaluated
661  */
662 tarval *computed_value(ir_node *n)
663 {
664   if (n->op->ops.computed_value)
665     return n->op->ops.computed_value(n);
666   return tarval_bad;
667 }
668
669 /**
670  * set the default computed_value evaluator in an ir_op_ops.
671  *
672  * @param code   the opcode for the default operation
673  * @param ops    the operations initialized
674  *
675  * @return
676  *    The operations.
677  */
678 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
679 {
680 #define CASE(a)                               \
681   case iro_##a:                               \
682     ops->computed_value  = computed_value_##a; \
683     break
684
685   switch (code) {
686   CASE(Const);
687   CASE(SymConst);
688   CASE(Add);
689   CASE(Sub);
690   CASE(Minus);
691   CASE(Mul);
692   CASE(Quot);
693   CASE(Div);
694   CASE(Mod);
695   CASE(Abs);
696   CASE(And);
697   CASE(Or);
698   CASE(Eor);
699   CASE(Not);
700   CASE(Shl);
701   CASE(Shr);
702   CASE(Shrs);
703   CASE(Rot);
704   CASE(Carry);
705   CASE(Borrow);
706   CASE(Conv);
707   CASE(Proj);
708   CASE(Mux);
709   CASE(Psi);
710   CASE(Confirm);
711   default:
712     /* leave NULL */;
713   }
714
715   return ops;
716 #undef CASE
717 }
718
719 /**
720  * Returns a equivalent block for another block.
721  * If the block has only one predecessor, this is
722  * the equivalent one. If the only predecessor of a block is
723  * the block itself, this is a dead block.
724  *
725  * If both predecessors of a block are the branches of a binary
726  * Cond, the equivalent block is Cond's block.
727  *
728  * If all predecessors of a block are bad or lies in a dead
729  * block, the current block is dead as well.
730  *
731  * Note, that blocks are NEVER turned into Bad's, instead
732  * the dead_block flag is set. So, never test for is_Bad(block),
733  * always use is_dead_Block(block).
734  */
735 static ir_node *equivalent_node_Block(ir_node *n)
736 {
737   ir_node *oldn = n;
738   int n_preds   = get_Block_n_cfgpreds(n);
739
740   /* The Block constructor does not call optimize, but mature_immBlock
741      calls the optimization. */
742   assert(get_Block_matured(n));
743
744   /* Straightening: a single entry Block following a single exit Block
745      can be merged, if it is not the Start block. */
746   /* !!! Beware, all Phi-nodes of n must have been optimized away.
747      This should be true, as the block is matured before optimize is called.
748      But what about Phi-cycles with the Phi0/Id that could not be resolved?
749      Remaining Phi nodes are just Ids. */
750    if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
751      ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
752      if (predblock == oldn) {
753        /* Jmp jumps into the block it is in -- deal self cycle. */
754        n = set_Block_dead(n);
755        DBG_OPT_DEAD_BLOCK(oldn, n);
756      } else if (get_opt_control_flow_straightening()) {
757        n = predblock;
758        DBG_OPT_STG(oldn, n);
759      }
760    }
761    else if ((n_preds == 1) &&
762             (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
763      ir_node *predblock = get_Block_cfgpred_block(n, 0);
764      if (predblock == oldn) {
765        /* Jmp jumps into the block it is in -- deal self cycle. */
766        n = set_Block_dead(n);
767        DBG_OPT_DEAD_BLOCK(oldn, n);
768      }
769    }
770    else if ((n_preds == 2) &&
771             (get_opt_control_flow_weak_simplification())) {
772     /* Test whether Cond jumps twice to this block
773      * The more general case which more than 2 predecessors is handles
774      * in optimize_cf(), we handle only this special case for speed here.
775      */
776     ir_node *a = get_Block_cfgpred(n, 0);
777     ir_node *b = get_Block_cfgpred(n, 1);
778
779     if ((get_irn_op(a) == op_Proj) &&
780         (get_irn_op(b) == op_Proj) &&
781         (get_Proj_pred(a) == get_Proj_pred(b)) &&
782         (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
783         (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
784       /* Also a single entry Block following a single exit Block.  Phis have
785          twice the same operand and will be optimized away. */
786       n = get_nodes_block(get_Proj_pred(a));
787       DBG_OPT_IFSIM1(oldn, a, b, n);
788     }
789   }
790   else if (get_opt_unreachable_code() &&
791            (n != get_irg_start_block(current_ir_graph)) &&
792            (n != get_irg_end_block(current_ir_graph))    ) {
793     int i;
794
795     /* If all inputs are dead, this block is dead too, except if it is
796        the start or end block.  This is one step of unreachable code
797        elimination */
798     for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
799       ir_node *pred = get_Block_cfgpred(n, i);
800       ir_node *pred_blk;
801
802       if (is_Bad(pred)) continue;
803       pred_blk = get_nodes_block(skip_Proj(pred));
804
805       if (is_Block_dead(pred_blk)) continue;
806
807       if (pred_blk != n) {
808         /* really found a living input */
809         break;
810       }
811     }
812     if (i < 0) {
813       n = set_Block_dead(n);
814       DBG_OPT_DEAD_BLOCK(oldn, n);
815     }
816   }
817
818   return n;
819 }
820
821 /**
822  * Returns a equivalent node for a Jmp, a Bad :-)
823  * Of course this only happens if the Block of the Jmp is dead.
824  */
825 static ir_node *equivalent_node_Jmp(ir_node *n)
826 {
827   /* unreachable code elimination */
828   if (is_Block_dead(get_nodes_block(n)))
829     n = new_Bad();
830
831   return n;
832 }
833
834 /** Raise is handled in the same way as Jmp. */
835 #define equivalent_node_Raise   equivalent_node_Jmp
836
837
838 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
839    See transform_node_Proj_Cond(). */
840
841 /**
842  * optimize operations that are commutative and have neutral 0,
843  * so a op 0 = 0 op a = a.
844  */
845 static ir_node *equivalent_node_neutral_zero(ir_node *n)
846 {
847   ir_node *oldn = n;
848
849   ir_node *a = get_binop_left(n);
850   ir_node *b = get_binop_right(n);
851
852   tarval *tv;
853   ir_node *on;
854
855   /* After running compute_node there is only one constant predecessor.
856      Find this predecessors value and remember the other node: */
857   if ((tv = value_of(a)) != tarval_bad) {
858     on = b;
859   } else if ((tv = value_of(b)) != tarval_bad) {
860     on = a;
861   } else
862     return n;
863
864   /* If this predecessors constant value is zero, the operation is
865    * unnecessary. Remove it.
866    *
867    * Beware: If n is a Add, the mode of on and n might be different
868    * which happens in this rare construction: NULL + 3.
869    * Then, a Conv would be needed which we cannot include here.
870    */
871   if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
872     if (get_irn_mode(on) == get_irn_mode(n)) {
873       n = on;
874
875       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
876     }
877   }
878
879   return n;
880 }
881
882 /**
883  * Eor is commutative and has neutral 0.
884  */
885 #define equivalent_node_Eor  equivalent_node_neutral_zero
886
887 /*
888  * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
889  *
890  * The second one looks strange, but this construct
891  * is used heavily in the LCC sources :-).
892  *
893  * Beware: The Mode of an Add may be different than the mode of its
894  * predecessors, so we could not return a predecessors in all cases.
895  */
896 static ir_node *equivalent_node_Add(ir_node *n)
897 {
898   ir_node *oldn = n;
899   ir_node *left, *right;
900   ir_mode *mode = get_irn_mode(n);
901
902   /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
903   if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
904     return n;
905
906   n = equivalent_node_neutral_zero(n);
907   if (n != oldn)
908     return n;
909
910   left  = get_Add_left(n);
911   right = get_Add_right(n);
912
913   if (get_irn_op(left) == op_Sub) {
914     if (get_Sub_right(left) == right) {
915       /* (a - x) + x */
916
917       n = get_Sub_left(left);
918       if (mode == get_irn_mode(n)) {
919         DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
920         return n;
921       }
922     }
923   }
924   if (get_irn_op(right) == op_Sub) {
925     if (get_Sub_right(right) == left) {
926       /* x + (a - x) */
927
928       n = get_Sub_left(right);
929       if (mode == get_irn_mode(n)) {
930         DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
931         return n;
932       }
933     }
934   }
935   return n;
936 }
937
938 /**
939  * optimize operations that are not commutative but have neutral 0 on left,
940  * so a op 0 = a.
941  */
942 static ir_node *equivalent_node_left_zero(ir_node *n)
943 {
944   ir_node *oldn = n;
945
946   ir_node *a = get_binop_left(n);
947   ir_node *b = get_binop_right(n);
948
949   if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
950     n = a;
951
952     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
953   }
954
955   return n;
956 }
957
958 #define equivalent_node_Shl   equivalent_node_left_zero
959 #define equivalent_node_Shr   equivalent_node_left_zero
960 #define equivalent_node_Shrs  equivalent_node_left_zero
961 #define equivalent_node_Rot   equivalent_node_left_zero
962
963 /**
964  * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
965  *
966  * The second one looks strange, but this construct
967  * is used heavily in the LCC sources :-).
968  *
969  * Beware: The Mode of a Sub may be different than the mode of its
970  * predecessors, so we could not return a predecessors in all cases.
971  */
972 static ir_node *equivalent_node_Sub(ir_node *n)
973 {
974   ir_node *oldn = n;
975   ir_node *a, *b;
976   ir_mode *mode = get_irn_mode(n);
977
978   /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
979   if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
980     return n;
981
982   a = get_Sub_left(n);
983   b = get_Sub_right(n);
984
985   /* Beware: modes might be different */
986   if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
987     if (mode == get_irn_mode(a)) {
988       n = a;
989
990       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
991     }
992   }
993   else if (get_irn_op(a) == op_Add) {
994     if (mode_wrap_around(mode)) {
995       ir_node *left  = get_Add_left(a);
996       ir_node *right = get_Add_right(a);
997
998       if (left == b) {
999         if (mode == get_irn_mode(right)) {
1000           n = right;
1001           DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
1002         }
1003       }
1004       else if (right == b) {
1005         if (mode == get_irn_mode(left)) {
1006           n = left;
1007           DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
1008         }
1009       }
1010     }
1011   }
1012   return n;
1013 }
1014
1015
1016 /**
1017  * Optimize an "idempotent unary op", ie op(op(n)) = n.
1018  *
1019  * @todo
1020  *   -(-a) == a, but might overflow two times.
1021  *   We handle it anyway here but the better way would be a
1022  *   flag. This would be needed for Pascal for instance.
1023  */
1024 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1025 {
1026   ir_node *oldn = n;
1027   ir_node *pred = get_unop_op(n);
1028
1029   /* optimize symmetric unop */
1030   if (get_irn_op(pred) == get_irn_op(n)) {
1031     n = get_unop_op(pred);
1032     DBG_OPT_ALGSIM2(oldn, pred, n);
1033   }
1034   return n;
1035 }
1036
1037 /** Not(Not(x)) == x */
1038 #define equivalent_node_Not    equivalent_node_idempotent_unop
1039
1040 /** --x == x       ??? Is this possible or can --x raise an
1041                        out of bounds exception if min =! max? */
1042 #define equivalent_node_Minus  equivalent_node_idempotent_unop
1043
1044 /**
1045  * Optimize a * 1 = 1 * a = a.
1046  */
1047 static ir_node *equivalent_node_Mul(ir_node *n)
1048 {
1049   ir_node *oldn = n;
1050   ir_node *a = get_Mul_left(n);
1051   ir_node *b = get_Mul_right(n);
1052
1053   /* Mul is commutative and has again an other neutral element. */
1054   if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1055     n = b;
1056     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1057   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1058     n = a;
1059     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1060   }
1061   return n;
1062 }
1063
1064 /**
1065  * Optimize a / 1 = a.
1066  */
1067 static ir_node *equivalent_node_Div(ir_node *n)
1068 {
1069   ir_node *a = get_Div_left(n);
1070   ir_node *b = get_Div_right(n);
1071
1072   /* Div is not commutative. */
1073   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1074     /* Turn Div into a tuple (mem, bad, a) */
1075     ir_node *mem = get_Div_mem(n);
1076     turn_into_tuple(n, pn_Div_max);
1077     set_Tuple_pred(n, pn_Div_M,        mem);
1078     set_Tuple_pred(n, pn_Div_X_except, new_Bad());        /* no exception */
1079     set_Tuple_pred(n, pn_Div_res,      a);
1080   }
1081   return n;
1082 }
1083
1084 /**
1085  * Optimize a / 1.0 = a.
1086  */
1087 static ir_node *equivalent_node_Quot(ir_node *n) {
1088   ir_node *a = get_Div_left(n);
1089   ir_node *b = get_Div_right(n);
1090
1091   /* Div is not commutative. */
1092   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1093     /* Turn Quot into a tuple (mem, bad, a) */
1094     ir_node *mem = get_Quot_mem(n);
1095     turn_into_tuple(n, pn_Quot_max);
1096     set_Tuple_pred(n, pn_Quot_M,        mem);
1097     set_Tuple_pred(n, pn_Quot_X_except, new_Bad());        /* no exception */
1098     set_Tuple_pred(n, pn_Quot_res,      a);
1099   }
1100   return n;
1101 }
1102
1103 /**
1104  * Optimize a / 1 = a.
1105  */
1106 static ir_node *equivalent_node_DivMod(ir_node *n)
1107 {
1108   ir_node *a = get_DivMod_left(n);
1109   ir_node *b = get_DivMod_right(n);
1110
1111   /* Div is not commutative. */
1112   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1113     /* Turn DivMod into a tuple (mem, bad, a, 0) */
1114     ir_node *mem = get_Div_mem(n);
1115     ir_mode *mode = get_irn_mode(b);
1116
1117     turn_into_tuple(n, pn_DivMod_max);
1118     set_Tuple_pred(n, pn_DivMod_M,        mem);
1119     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());        /* no exception */
1120     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1121     set_Tuple_pred(n, pn_DivMod_res_mod,  new_Const(mode, get_mode_null(mode)));
1122   }
1123   return n;
1124 }
1125
1126 /**
1127  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1128  */
1129 static ir_node *equivalent_node_Or(ir_node *n)
1130 {
1131   ir_node *oldn = n;
1132
1133   ir_node *a = get_Or_left(n);
1134   ir_node *b = get_Or_right(n);
1135
1136   if (a == b) {
1137     n = a;    /* Or has it's own neutral element */
1138     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1139   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1140     n = b;
1141     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1142   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1143     n = a;
1144     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1145   }
1146
1147   return n;
1148 }
1149
1150 /**
1151  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
1152  */
1153 static ir_node *equivalent_node_And(ir_node *n)
1154 {
1155   ir_node *oldn = n;
1156
1157   ir_node *a = get_And_left(n);
1158   ir_node *b = get_And_right(n);
1159
1160   if (a == b) {
1161     n = a;    /* And has it's own neutral element */
1162     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1163   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1164     n = b;
1165     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1166   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1167     n = a;
1168     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1169   }
1170   return n;
1171 }
1172
1173 /**
1174  * Try to remove useless Conv's:
1175  */
1176 static ir_node *equivalent_node_Conv(ir_node *n)
1177 {
1178   ir_node *oldn = n;
1179   ir_node *a = get_Conv_op(n);
1180   ir_node *b;
1181
1182   ir_mode *n_mode = get_irn_mode(n);
1183   ir_mode *a_mode = get_irn_mode(a);
1184
1185   if (n_mode == a_mode) { /* No Conv necessary */
1186     /* leave strict floating point Conv's */
1187     if (get_Conv_strict(n))
1188       return n;
1189     n = a;
1190     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1191   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1192     ir_mode *b_mode;
1193
1194     b = get_Conv_op(a);
1195     n_mode = get_irn_mode(n);
1196     b_mode = get_irn_mode(b);
1197
1198     if (n_mode == b_mode) {
1199       if (n_mode == mode_b) {
1200         n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1201         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1202       }
1203       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1204         if (smaller_mode(b_mode, a_mode)){
1205           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1206           DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1207         }
1208       }
1209     }
1210   }
1211   return n;
1212 }
1213
1214 /**
1215  * A Cast may be removed if the type of the previous node
1216  * is already the type of the Cast.
1217  */
1218 static ir_node *equivalent_node_Cast(ir_node *n) {
1219   ir_node *oldn = n;
1220   ir_node *pred = get_Cast_op(n);
1221
1222   if (get_irn_type(pred) == get_Cast_type(n)) {
1223     n = pred;
1224     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1225   }
1226   return n;
1227 }
1228
1229 /**
1230   Several optimizations:
1231    - no Phi in start block.
1232    - remove Id operators that are inputs to Phi
1233    - fold Phi-nodes, iff they have only one predecessor except
1234            themselves.
1235  */
1236 static ir_node *equivalent_node_Phi(ir_node *n)
1237 {
1238   int i, n_preds;
1239
1240   ir_node *oldn = n;
1241   ir_node *block = NULL;     /* to shutup gcc */
1242   ir_node *first_val = NULL; /* to shutup gcc */
1243
1244   if (!get_opt_normalize()) return n;
1245
1246   n_preds = get_Phi_n_preds(n);
1247
1248   block = get_nodes_block(n);
1249   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1250      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1251   if ((is_Block_dead(block)) ||                  /* Control dead */
1252       (block == get_irg_start_block(current_ir_graph)))  /* There should be no Phi nodes */
1253     return new_Bad();                                    /* in the Start Block. */
1254
1255   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1256
1257   /* If the Block has a Bad pred, we also have one. */
1258   for (i = 0;  i < n_preds;  ++i)
1259     if (is_Bad(get_Block_cfgpred(block, i)))
1260       set_Phi_pred(n, i, new_Bad());
1261
1262   /* Find first non-self-referencing input */
1263   for (i = 0; i < n_preds; ++i) {
1264     first_val = get_Phi_pred(n, i);
1265     if (   (first_val != n)                            /* not self pointer */
1266 #if 1
1267         && (! is_Bad(first_val))
1268 #endif
1269            ) {        /* value not dead */
1270       break;          /* then found first value. */
1271     }
1272   }
1273
1274   if (i >= n_preds) {
1275     /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1276     return new_Bad();
1277   }
1278
1279   /* search for rest of inputs, determine if any of these
1280      are non-self-referencing */
1281   while (++i < n_preds) {
1282     ir_node *scnd_val = get_Phi_pred(n, i);
1283     if (   (scnd_val != n)
1284         && (scnd_val != first_val)
1285 #if 1
1286         && (! is_Bad(scnd_val))
1287 #endif
1288            ) {
1289       break;
1290     }
1291   }
1292
1293   if (i >= n_preds) {
1294     /* Fold, if no multiple distinct non-self-referencing inputs */
1295     n = first_val;
1296     DBG_OPT_PHI(oldn, n);
1297   }
1298   return n;
1299 }
1300
1301 /**
1302   Several optimizations:
1303    - no Sync in start block.
1304    - fold Sync-nodes, iff they have only one predecessor except
1305            themselves.
1306  */
1307 static ir_node *equivalent_node_Sync(ir_node *n)
1308 {
1309   int i, n_preds;
1310
1311   ir_node *oldn = n;
1312   ir_node *first_val = NULL; /* to shutup gcc */
1313
1314   if (!get_opt_normalize()) return n;
1315
1316   n_preds = get_Sync_n_preds(n);
1317
1318   /* Find first non-self-referencing input */
1319   for (i = 0; i < n_preds; ++i) {
1320     first_val = get_Sync_pred(n, i);
1321     if ((first_val != n)  /* not self pointer */ &&
1322         (! is_Bad(first_val))
1323        ) {            /* value not dead */
1324       break;          /* then found first value. */
1325     }
1326   }
1327
1328   if (i >= n_preds)
1329     /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1330     return new_Bad();
1331
1332   /* search the rest of inputs, determine if any of these
1333      are non-self-referencing */
1334   while (++i < n_preds) {
1335     ir_node *scnd_val = get_Sync_pred(n, i);
1336     if ((scnd_val != n) &&
1337         (scnd_val != first_val) &&
1338         (! is_Bad(scnd_val))
1339        )
1340       break;
1341   }
1342
1343   if (i >= n_preds) {
1344     /* Fold, if no multiple distinct non-self-referencing inputs */
1345     n = first_val;
1346     DBG_OPT_SYNC(oldn, n);
1347   }
1348   return n;
1349 }
1350
1351 /**
1352  * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1353  * ProjX(Load) and ProjX(Store)
1354  */
1355 static ir_node *equivalent_node_Proj(ir_node *n)
1356 {
1357   ir_node *oldn = n;
1358
1359   ir_node *a = get_Proj_pred(n);
1360
1361   if ( get_irn_op(a) == op_Tuple) {
1362     /* Remove the Tuple/Proj combination. */
1363     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1364       n = get_Tuple_pred(a, get_Proj_proj(n));
1365       DBG_OPT_TUPLE(oldn, a, n);
1366     } else {
1367       assert(0); /* This should not happen! */
1368       n = new_Bad();
1369     }
1370   }
1371   else if (get_irn_mode(n) == mode_X) {
1372     if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1373       /* Remove dead control flow -- early gigo(). */
1374       n = new_Bad();
1375     }
1376     else if (get_opt_ldst_only_null_ptr_exceptions()) {
1377       ir_op *op = get_irn_op(a);
1378
1379       if (op == op_Load || op == op_Store) {
1380         /* get the load/store address */
1381         ir_node *addr = get_irn_n(a, 1);
1382         if (value_not_null(addr)) {
1383           /* this node may float if it did not depend on a Confirm */
1384           set_irn_pinned(a, op_pin_state_floats);
1385           DBG_OPT_EXC_REM(n);
1386           return new_Bad();
1387         }
1388       }
1389     }
1390   }
1391
1392   return n;
1393 }
1394
1395 /**
1396  * Remove Id's.
1397  */
1398 static ir_node *equivalent_node_Id(ir_node *n)
1399 {
1400   ir_node *oldn = n;
1401
1402   do {
1403     n = get_Id_pred(n);
1404   } while (get_irn_op(n) == op_Id);
1405
1406   DBG_OPT_ID(oldn, n);
1407   return n;
1408 }
1409
1410 /**
1411  * optimize a Mux
1412  */
1413 static ir_node *equivalent_node_Mux(ir_node *n)
1414 {
1415   ir_node *oldn = n, *sel = get_Mux_sel(n);
1416   tarval *ts = value_of(sel);
1417
1418   /* Mux(true, f, t) == t */
1419   if (ts == tarval_b_true) {
1420     n = get_Mux_true(n);
1421     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1422   }
1423   /* Mux(false, f, t) == f */
1424   else if (ts == tarval_b_false) {
1425     n = get_Mux_false(n);
1426     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1427   }
1428   /* Mux(v, x, x) == x */
1429   else if (get_Mux_false(n) == get_Mux_true(n)) {
1430     n = get_Mux_true(n);
1431     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1432   }
1433   else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1434     ir_node *cmp = get_Proj_pred(sel);
1435     long proj_nr = get_Proj_proj(sel);
1436     ir_node *b   = get_Mux_false(n);
1437     ir_node *a   = get_Mux_true(n);
1438
1439     /*
1440      * Note: normalization puts the constant on the right site,
1441      * so we check only one case.
1442      *
1443      * Note further that these optimization work even for floating point
1444      * with NaN's because -NaN == NaN.
1445      * However, if +0 and -0 is handled differently, we cannot use the first one.
1446      */
1447     if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1448       if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1449         /* Mux(a CMP 0, X, a) */
1450         if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1451           /* Mux(a CMP 0, -a, a) */
1452           if (proj_nr == pn_Cmp_Eq) {
1453             /* Mux(a == 0, -a, a)  ==>  -a */
1454             n = b;
1455             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1456           }
1457           else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1458             /* Mux(a != 0, -a, a)  ==> a */
1459             n = a;
1460             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1461           }
1462         }
1463         else if (classify_Const(b) == CNST_NULL) {
1464           /* Mux(a CMP 0, 0, a) */
1465           if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1466             /* Mux(a != 0, 0, a) ==> a */
1467             n = a;
1468             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1469           }
1470           else if (proj_nr == pn_Cmp_Eq) {
1471             /* Mux(a == 0, 0, a) ==> 0 */
1472             n = b;
1473             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1474           }
1475         }
1476       }
1477     }
1478   }
1479   return n;
1480 }
1481
1482 /**
1483  * Returns a equivalent node of a Psi: if a condition is true
1484  * and all previous conditions are false we know its value.
1485  * If all conditions are false its value is the default one.
1486  */
1487 static ir_node *equivalent_node_Psi(ir_node *n) {
1488   if (is_Mux(n))
1489     return equivalent_node_Mux(n);
1490   return n;
1491 }
1492
1493 /**
1494  * Optimize -a CMP -b into b CMP a.
1495  * This works only for for modes where unary Minus
1496  * cannot Overflow.
1497  * Note that two-complement integers can Overflow
1498  * so it will NOT work.
1499  */
1500 static ir_node *equivalent_node_Cmp(ir_node *n)
1501 {
1502   ir_node *left  = get_Cmp_left(n);
1503   ir_node *right = get_Cmp_right(n);
1504
1505   if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1506       !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1507     left  = get_Minus_op(left);
1508     right = get_Minus_op(right);
1509     set_Cmp_left(n, right);
1510     set_Cmp_right(n, left);
1511   }
1512   return n;
1513 }
1514
1515 /**
1516  * Remove Confirm nodes if setting is on.
1517  * Replace Confirms(x, '=', Constlike) by Constlike.
1518  */
1519 static ir_node *equivalent_node_Confirm(ir_node *n)
1520 {
1521   ir_node *pred = get_Confirm_value(n);
1522   pn_Cmp  pnc   = get_Confirm_cmp(n);
1523
1524   if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1525     /*
1526      * rare case: two identical Confirms one after another,
1527      * replace the second one with the first.
1528      */
1529     n = pred;
1530   }
1531   if (pnc == pn_Cmp_Eq) {
1532     ir_node *bound = get_Confirm_bound(n);
1533
1534     /*
1535      * Optimize a rare case:
1536      * Confirm(x, '=', Constlike) ==> Constlike
1537      */
1538     if (is_irn_constlike(bound)) {
1539       DBG_OPT_CONFIRM(n, bound);
1540       return bound;
1541     }
1542   }
1543   return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1544 }
1545
1546 /**
1547  * Optimize CopyB(mem, x, x) into a Nop
1548  */
1549 static ir_node *equivalent_node_CopyB(ir_node *n)
1550 {
1551   ir_node *a = get_CopyB_dst(n);
1552   ir_node *b = get_CopyB_src(n);
1553
1554   if (a == b) {
1555     /* Turn CopyB into a tuple (mem, bad, bad) */
1556     ir_node *mem = get_CopyB_mem(n);
1557     turn_into_tuple(n, pn_CopyB_max);
1558     set_Tuple_pred(n, pn_CopyB_M,        mem);
1559     set_Tuple_pred(n, pn_CopyB_X_except, new_Bad());        /* no exception */
1560     set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1561   }
1562   return n;
1563 }
1564
1565 /**
1566  * Optimize Bounds(idx, idx, upper) into idx.
1567  */
1568 static ir_node *equivalent_node_Bound(ir_node *n)
1569 {
1570   ir_node *idx   = get_Bound_index(n);
1571   ir_node *lower = get_Bound_lower(n);
1572   int ret_tuple = 0;
1573
1574   /* By definition lower < upper, so if idx == lower -->
1575      lower <= idx && idx < upper */
1576   if (idx == lower) {
1577     /* Turn Bound into a tuple (mem, bad, idx) */
1578     ret_tuple = 1;
1579   }
1580   else {
1581     ir_node *pred = skip_Proj(idx);
1582
1583     if (get_irn_op(pred) == op_Bound) {
1584       /*
1585        * idx was Bounds_check previously, it is still valid if
1586        * lower <= pred_lower && pred_upper <= upper.
1587        */
1588       ir_node *upper = get_Bound_upper(n);
1589       if (get_Bound_lower(pred) == lower &&
1590           get_Bound_upper(pred) == upper) {
1591         /*
1592          * One could expect that we simply return the previous
1593          * Bound here. However, this would be wrong, as we could
1594          * add an exception Proj to a new location than.
1595          * So, we must turn in into a tuple
1596          */
1597         ret_tuple = 1;
1598       }
1599     }
1600   }
1601   if (ret_tuple) {
1602     /* Turn Bound into a tuple (mem, bad, idx) */
1603     ir_node *mem = get_Bound_mem(n);
1604     turn_into_tuple(n, pn_Bound_max);
1605     set_Tuple_pred(n, pn_Bound_M,        mem);
1606     set_Tuple_pred(n, pn_Bound_X_except, new_Bad());       /* no exception */
1607     set_Tuple_pred(n, pn_Bound_res,      idx);
1608   }
1609   return n;
1610 }
1611
1612 /**
1613  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1614  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1615  * new nodes.  It is therefore safe to free n if the node returned is not n.
1616  * If a node returns a Tuple we can not just skip it.  If the size of the
1617  * in array fits, we transform n into a tuple (e.g., Div).
1618  */
1619 ir_node *
1620 equivalent_node(ir_node *n)
1621 {
1622   if (n->op->ops.equivalent_node)
1623     return n->op->ops.equivalent_node(n);
1624   return n;
1625 }
1626
1627 /**
1628  * sets the default equivalent node operation for an ir_op_ops.
1629  *
1630  * @param code   the opcode for the default operation
1631  * @param ops    the operations initialized
1632  *
1633  * @return
1634  *    The operations.
1635  */
1636 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1637 {
1638 #define CASE(a)                                 \
1639   case iro_##a:                                 \
1640     ops->equivalent_node  = equivalent_node_##a; \
1641     break
1642
1643   switch (code) {
1644   CASE(Block);
1645   CASE(Jmp);
1646   CASE(Raise);
1647   CASE(Or);
1648   CASE(Add);
1649   CASE(Eor);
1650   CASE(Sub);
1651   CASE(Shl);
1652   CASE(Shr);
1653   CASE(Shrs);
1654   CASE(Rot);
1655   CASE(Not);
1656   CASE(Minus);
1657   CASE(Mul);
1658   CASE(Div);
1659   CASE(Quot);
1660   CASE(DivMod);
1661   CASE(And);
1662   CASE(Conv);
1663   CASE(Cast);
1664   CASE(Phi);
1665   CASE(Sync);
1666   CASE(Proj);
1667   CASE(Id);
1668   CASE(Mux);
1669   CASE(Psi);
1670   CASE(Cmp);
1671   CASE(Confirm);
1672   CASE(CopyB);
1673   CASE(Bound);
1674   default:
1675     /* leave NULL */;
1676   }
1677
1678   return ops;
1679 #undef CASE
1680 }
1681
1682 /**
1683  * Do node specific optimizations of nodes predecessors.
1684  */
1685 static void
1686 optimize_preds(ir_node *n) {
1687   ir_node *a = NULL, *b = NULL;
1688
1689   /* get the operands we will work on for simple cases. */
1690   if (is_binop(n)) {
1691     a = get_binop_left(n);
1692     b = get_binop_right(n);
1693   } else if (is_unop(n)) {
1694     a = get_unop_op(n);
1695   }
1696
1697   switch (get_irn_opcode(n)) {
1698
1699   case iro_Cmp:
1700     /* We don't want Cast as input to Cmp. */
1701     if (get_irn_op(a) == op_Cast) {
1702       a = get_Cast_op(a);
1703       set_Cmp_left(n, a);
1704     }
1705     if (get_irn_op(b) == op_Cast) {
1706       b = get_Cast_op(b);
1707       set_Cmp_right(n, b);
1708     }
1709     break;
1710
1711   default: break;
1712   } /* end switch */
1713 }
1714
1715 /**
1716  * Returns non-zero if a node is a Phi node
1717  * with all predecessors constant.
1718  */
1719 static int is_const_Phi(ir_node *n) {
1720   int i;
1721
1722   if (! is_Phi(n))
1723     return 0;
1724   for (i = get_irn_arity(n) - 1; i >= 0; --i)
1725     if (! is_Const(get_irn_n(n, i)))
1726       return 0;
1727   return 1;
1728 }
1729
1730 /**
1731  * Apply an evaluator on a binop with a constant operators (and one Phi).
1732  *
1733  * @param phi    the Phi node
1734  * @param other  the other operand
1735  * @param eval   an evaluator function
1736  * @param left   if non-zero, other is the left operand, else the right
1737  *
1738  * @return a new Phi node if the conversion was successful, NULL else
1739  */
1740 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1741   tarval   *tv;
1742   void     **res;
1743   ir_node  *pred;
1744   ir_mode  *mode;
1745   ir_graph *irg;
1746   int      i, n = get_irn_arity(phi);
1747
1748   NEW_ARR_A(void *, res, n);
1749   if (left) {
1750     for (i = 0; i < n; ++i) {
1751       pred = get_irn_n(phi, i);
1752       tv   = get_Const_tarval(pred);
1753       tv   = eval(other, tv);
1754
1755       if (tv == tarval_bad) {
1756         /* folding failed, bad */
1757         return NULL;
1758       }
1759       res[i] = tv;
1760     }
1761   }
1762   else {
1763     for (i = 0; i < n; ++i) {
1764       pred = get_irn_n(phi, i);
1765       tv   = get_Const_tarval(pred);
1766       tv   = eval(tv, other);
1767
1768       if (tv == tarval_bad) {
1769         /* folding failed, bad */
1770         return 0;
1771       }
1772       res[i] = tv;
1773     }
1774   }
1775   mode = get_irn_mode(phi);
1776   irg  = current_ir_graph;
1777   for (i = 0; i < n; ++i) {
1778     pred = get_irn_n(phi, i);
1779     res[i] = new_r_Const_type(irg, get_nodes_block(pred),
1780                             mode, res[i], get_Const_type(pred));
1781   }
1782   return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1783 }
1784
1785 /**
1786  * Apply an evaluator on a unop with a constant operator (a Phi).
1787  *
1788  * @param phi    the Phi node
1789  * @param eval   an evaluator function
1790  *
1791  * @return a new Phi node if the conversion was successful, NULL else
1792  */
1793 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1794   tarval   *tv;
1795   void     **res;
1796   ir_node  *pred;
1797   ir_mode  *mode;
1798   ir_graph *irg;
1799   int      i, n = get_irn_arity(phi);
1800
1801   NEW_ARR_A(void *, res, n);
1802   for (i = 0; i < n; ++i) {
1803     pred = get_irn_n(phi, i);
1804     tv   = get_Const_tarval(pred);
1805     tv   = eval(tv);
1806
1807     if (tv == tarval_bad) {
1808       /* folding failed, bad */
1809       return 0;
1810     }
1811     res[i] = tv;
1812   }
1813   mode = get_irn_mode(phi);
1814   irg  = current_ir_graph;
1815   for (i = 0; i < n; ++i) {
1816     pred = get_irn_n(phi, i);
1817     res[i] = new_r_Const_type(irg, get_nodes_block(pred),
1818                             mode, res[i], get_Const_type(pred));
1819   }
1820   return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1821 }
1822
1823 /**
1824  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1825  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1826  * If possible, remove the Conv's.
1827  */
1828 static ir_node *transform_node_AddSub(ir_node *n)
1829 {
1830   ir_mode *mode = get_irn_mode(n);
1831
1832   if (mode_is_reference(mode)) {
1833     ir_node *left  = get_binop_left(n);
1834     ir_node *right = get_binop_right(n);
1835     int ref_bits   = get_mode_size_bits(mode);
1836
1837     if (get_irn_op(left) == op_Conv) {
1838       ir_mode *mode = get_irn_mode(left);
1839       int bits      = get_mode_size_bits(mode);
1840
1841       if (ref_bits == bits &&
1842           mode_is_int(mode) &&
1843           get_mode_arithmetic(mode) == irma_twos_complement) {
1844         ir_node *pre      = get_Conv_op(left);
1845         ir_mode *pre_mode = get_irn_mode(pre);
1846
1847         if (mode_is_int(pre_mode) &&
1848             get_mode_size_bits(pre_mode) == bits &&
1849             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1850           /* ok, this conv just changes to sign, moreover the calculation
1851            * is done with same number of bits as our address mode, so
1852            * we can ignore the conv as address calculation can be viewed
1853            * as either signed or unsigned
1854            */
1855           set_binop_left(n, pre);
1856         }
1857       }
1858     }
1859
1860     if (get_irn_op(right) == op_Conv) {
1861       ir_mode *mode = get_irn_mode(right);
1862       int bits      = get_mode_size_bits(mode);
1863
1864       if (ref_bits == bits &&
1865           mode_is_int(mode) &&
1866           get_mode_arithmetic(mode) == irma_twos_complement) {
1867         ir_node *pre      = get_Conv_op(right);
1868         ir_mode *pre_mode = get_irn_mode(pre);
1869
1870         if (mode_is_int(pre_mode) &&
1871             get_mode_size_bits(pre_mode) == bits &&
1872             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1873           /* ok, this conv just changes to sign, moreover the calculation
1874            * is done with same number of bits as our address mode, so
1875            * we can ignore the conv as address calculation can be viewed
1876            * as either signed or unsigned
1877            */
1878           set_binop_right(n, pre);
1879         }
1880       }
1881     }
1882   }
1883   return n;
1884 }
1885
1886 #define HANDLE_BINOP_PHI(op,a,b,c)                          \
1887   c = NULL;                                                 \
1888   if (is_Const(b) && is_const_Phi(a)) {                     \
1889     /* check for Op(Phi, Const) */                          \
1890     c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0);  \
1891   }                                                         \
1892   else if (is_Const(a) && is_const_Phi(b)) {                \
1893     /* check for Op(Const, Phi) */                          \
1894     c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1);  \
1895   }                                                         \
1896   if (c) {                                                  \
1897     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);             \
1898     return c;                                               \
1899   }
1900
1901 #define HANDLE_UNOP_PHI(op,a,c)                 \
1902   c = NULL;                                     \
1903   if (is_const_Phi(a)) {                        \
1904     /* check for Op(Phi) */                     \
1905     c = apply_unop_on_phi(a, op);               \
1906   }                                             \
1907   if (c) {                                      \
1908     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1909     return c;                                   \
1910   }
1911
1912
1913 /**
1914  * Do the AddSub optimization, then Transform
1915  *   Constant folding on Phi
1916  *   Add(a,a)          -> Mul(a, 2)
1917  *   Add(Mul(a, x), a) -> Mul(a, x+1)
1918  * if the mode is integer or float.
1919  * Transform Add(a,-b) into Sub(a,b).
1920  * Reassociation might fold this further.
1921  */
1922 static ir_node *transform_node_Add(ir_node *n)
1923 {
1924   ir_mode *mode;
1925   ir_node *a, *b, *c, *oldn = n;
1926
1927   n = transform_node_AddSub(n);
1928
1929   a = get_Add_left(n);
1930   b = get_Add_right(n);
1931
1932   HANDLE_BINOP_PHI(tarval_add, a,b,c);
1933
1934   mode = get_irn_mode(n);
1935
1936   /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1937   if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1938     return n;
1939
1940   if (mode_is_num(mode)) {
1941     if (a == b) {
1942       ir_node *block = get_irn_n(n, -1);
1943
1944       n = new_rd_Mul(
1945             get_irn_dbg_info(n),
1946             current_ir_graph,
1947             block,
1948             a,
1949             new_r_Const_long(current_ir_graph, block, mode, 2),
1950             mode);
1951       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1952     }
1953     else if (get_irn_op(a) == op_Minus) {
1954       n = new_rd_Sub(
1955           get_irn_dbg_info(n),
1956           current_ir_graph,
1957           get_irn_n(n, -1),
1958           b,
1959           get_Minus_op(a),
1960           mode);
1961       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1962     }
1963     else if (get_irn_op(b) == op_Minus) {
1964       n = new_rd_Sub(
1965           get_irn_dbg_info(n),
1966           current_ir_graph,
1967           get_irn_n(n, -1),
1968           a,
1969           get_Minus_op(b),
1970           mode);
1971       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1972     }
1973     /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1974     else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1975       ir_node *ma = get_Mul_left(a);
1976       ir_node *mb = get_Mul_right(a);
1977
1978       if (b == ma) {
1979         ir_node *blk = get_irn_n(n, -1);
1980         n = new_rd_Mul(
1981           get_irn_dbg_info(n), current_ir_graph, blk,
1982           ma,
1983           new_rd_Add(
1984             get_irn_dbg_info(n), current_ir_graph, blk,
1985             mb,
1986             new_r_Const_long(current_ir_graph, blk, mode, 1),
1987             mode),
1988           mode);
1989         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1990       }
1991       else if (b == mb) {
1992         ir_node *blk = get_irn_n(n, -1);
1993         n = new_rd_Mul(
1994           get_irn_dbg_info(n), current_ir_graph, blk,
1995           mb,
1996           new_rd_Add(
1997             get_irn_dbg_info(n), current_ir_graph, blk,
1998             ma,
1999             new_r_Const_long(current_ir_graph, blk, mode, 1),
2000             mode),
2001           mode);
2002         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2003       }
2004     }
2005     /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2006     else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
2007       ir_node *ma = get_Mul_left(b);
2008       ir_node *mb = get_Mul_right(b);
2009
2010       if (a == ma) {
2011         ir_node *blk = get_irn_n(n, -1);
2012         n = new_rd_Mul(
2013           get_irn_dbg_info(n), current_ir_graph, blk,
2014           ma,
2015           new_rd_Add(
2016             get_irn_dbg_info(n), current_ir_graph, blk,
2017             mb,
2018             new_r_Const_long(current_ir_graph, blk, mode, 1),
2019             mode),
2020           mode);
2021         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2022       }
2023       else if (a == mb) {
2024         ir_node *blk = get_irn_n(n, -1);
2025         n = new_rd_Mul(
2026           get_irn_dbg_info(n), current_ir_graph, blk,
2027           mb,
2028           new_rd_Add(
2029             get_irn_dbg_info(n), current_ir_graph, blk,
2030             ma,
2031             new_r_Const_long(current_ir_graph, blk, mode, 1),
2032             mode),
2033           mode);
2034         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2035       }
2036     }
2037   }
2038   return n;
2039 }
2040
2041 /**
2042  * Do the AddSub optimization, then Transform
2043  *   Constant folding on Phi
2044  *   Sub(0,a)          -> Minus(a)
2045  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2046  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2047  */
2048 static ir_node *transform_node_Sub(ir_node *n)
2049 {
2050   ir_mode *mode;
2051   ir_node *oldn = n;
2052   ir_node *a, *b, *c;
2053
2054   n = transform_node_AddSub(n);
2055
2056   a = get_Sub_left(n);
2057   b = get_Sub_right(n);
2058
2059   HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2060
2061   mode = get_irn_mode(n);
2062
2063   /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2064   if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2065     return n;
2066
2067   if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
2068     n = new_rd_Minus(
2069           get_irn_dbg_info(n),
2070           current_ir_graph,
2071           get_irn_n(n, -1),
2072           b,
2073           mode);
2074     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2075   }
2076   /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2077   else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2078     ir_node *ma = get_Mul_left(a);
2079     ir_node *mb = get_Mul_right(a);
2080
2081     if (ma == b) {
2082       ir_node *blk = get_irn_n(n, -1);
2083       n = new_rd_Mul(
2084         get_irn_dbg_info(n),
2085         current_ir_graph, blk,
2086         ma,
2087         new_rd_Sub(
2088           get_irn_dbg_info(n),
2089           current_ir_graph, blk,
2090           mb,
2091           new_r_Const_long(current_ir_graph, blk, mode, 1),
2092           mode),
2093         mode);
2094       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2095     }
2096     else if (mb == b) {
2097       ir_node *blk = get_irn_n(n, -1);
2098       n = new_rd_Mul(
2099         get_irn_dbg_info(n),
2100         current_ir_graph, blk,
2101         mb,
2102         new_rd_Sub(
2103           get_irn_dbg_info(n),
2104           current_ir_graph, blk,
2105           ma,
2106           new_r_Const_long(current_ir_graph, blk, mode, 1),
2107           mode),
2108         mode);
2109       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2110     }
2111   }
2112   else if (get_irn_op(a) == op_Sub) {
2113     ir_node *x   = get_Sub_left(a);
2114     ir_node *y   = get_Sub_right(a);
2115     ir_node *blk = get_irn_n(n, -1);
2116     ir_mode *m_b = get_irn_mode(b);
2117     ir_mode *m_y = get_irn_mode(y);
2118     ir_node *add;
2119
2120     /* Determine the right mode for the Add. */
2121     if (m_b == m_y)
2122       mode = m_b;
2123     else if (mode_is_reference(m_b))
2124       mode = m_b;
2125     else if (mode_is_reference(m_y))
2126       mode = m_y;
2127     else {
2128       /*
2129        * Both modes are different but none is reference,
2130        * happens for instance in SubP(SubP(P, Iu), Is).
2131        * We have two possibilities here: Cast or ignore.
2132        * Currently we ignore this case.
2133        */
2134       return n;
2135     }
2136
2137     add = new_r_Add(current_ir_graph, blk, y, b, mode);
2138
2139     set_Sub_left(n, x);
2140     set_Sub_right(n, add);
2141     DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2142   }
2143
2144   return n;
2145 }
2146
2147 /**
2148  * Transform Mul(a,-1) into -a.
2149  * Do constant evaluation of Phi nodes.
2150  * Do architecture dependent optimizations on Mul nodes
2151  */
2152 static ir_node *transform_node_Mul(ir_node *n) {
2153   ir_node *c, *oldn = n;
2154   ir_node *a = get_Mul_left(n);
2155   ir_node *b = get_Mul_right(n);
2156   ir_mode *mode;
2157
2158   HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2159
2160   mode = get_irn_mode(n);
2161   if (mode_is_signed(mode)) {
2162     ir_node *r = NULL;
2163
2164     if (value_of(a) == get_mode_minus_one(mode))
2165       r = b;
2166     else if (value_of(b) == get_mode_minus_one(mode))
2167       r = a;
2168     if (r) {
2169       n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2170       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2171       return n;
2172     }
2173   }
2174   return arch_dep_replace_mul_with_shifts(n);
2175 }
2176
2177 /**
2178  * Transform a Div Node.
2179  */
2180 static ir_node *transform_node_Div(ir_node *n)
2181 {
2182   tarval *tv = value_of(n);
2183   ir_node *value = n;
2184
2185   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2186
2187   if (tv != tarval_bad) {
2188     value = new_Const(get_tarval_mode(tv), tv);
2189
2190     DBG_OPT_CSTEVAL(n, value);
2191   }
2192   else /* Try architecture dependent optimization */
2193     value = arch_dep_replace_div_by_const(n);
2194
2195   if (value != n) {
2196     /* Turn Div into a tuple (mem, bad, value) */
2197     ir_node *mem = get_Div_mem(n);
2198
2199     turn_into_tuple(n, pn_Div_max);
2200     set_Tuple_pred(n, pn_Div_M, mem);
2201     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2202     set_Tuple_pred(n, pn_Div_res, value);
2203   }
2204   return n;
2205 }
2206
2207 /**
2208  * Transform a Mod node.
2209  */
2210 static ir_node *transform_node_Mod(ir_node *n)
2211 {
2212   tarval *tv = value_of(n);
2213   ir_node *value = n;
2214
2215   /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2216
2217   if (tv != tarval_bad) {
2218     value = new_Const(get_tarval_mode(tv), tv);
2219
2220     DBG_OPT_CSTEVAL(n, value);
2221   }
2222   else /* Try architecture dependent optimization */
2223     value = arch_dep_replace_mod_by_const(n);
2224
2225   if (value != n) {
2226     /* Turn Mod into a tuple (mem, bad, value) */
2227     ir_node *mem = get_Mod_mem(n);
2228
2229     turn_into_tuple(n, pn_Mod_max);
2230     set_Tuple_pred(n, pn_Mod_M, mem);
2231     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2232     set_Tuple_pred(n, pn_Mod_res, value);
2233   }
2234   return n;
2235 }
2236
2237 /**
2238  * Transform a DivMod node.
2239  */
2240 static ir_node *transform_node_DivMod(ir_node *n)
2241 {
2242   int evaluated = 0;
2243
2244   ir_node *a = get_DivMod_left(n);
2245   ir_node *b = get_DivMod_right(n);
2246   ir_mode *mode = get_irn_mode(a);
2247   tarval *ta = value_of(a);
2248   tarval *tb = value_of(b);
2249
2250   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2251     return n;
2252
2253   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2254
2255   if (tb != tarval_bad) {
2256     if (tb == get_mode_one(get_tarval_mode(tb))) {
2257       b = new_Const (mode, get_mode_null(mode));
2258       evaluated = 1;
2259
2260       DBG_OPT_CSTEVAL(n, b);
2261     }
2262     else if (ta != tarval_bad) {
2263       tarval *resa, *resb;
2264       resa = tarval_div (ta, tb);
2265       if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2266                                         Jmp for X result!? */
2267       resb = tarval_mod (ta, tb);
2268       if (resb == tarval_bad) return n; /* Causes exception! */
2269       a = new_Const (mode, resa);
2270       b = new_Const (mode, resb);
2271       evaluated = 1;
2272
2273       DBG_OPT_CSTEVAL(n, a);
2274       DBG_OPT_CSTEVAL(n, b);
2275     }
2276     else { /* Try architecture dependent optimization */
2277       arch_dep_replace_divmod_by_const(&a, &b, n);
2278       evaluated = a != NULL;
2279     }
2280   } else if (ta == get_mode_null(mode)) {
2281     /* 0 / non-Const = 0 */
2282     b = a;
2283     evaluated = 1;
2284   }
2285
2286   if (evaluated) { /* replace by tuple */
2287     ir_node *mem = get_DivMod_mem(n);
2288     turn_into_tuple(n, pn_DivMod_max);
2289     set_Tuple_pred(n, pn_DivMod_M,        mem);
2290     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
2291     set_Tuple_pred(n, pn_DivMod_res_div,  a);
2292     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
2293   }
2294
2295   return n;
2296 }
2297
2298 /**
2299  * Optimize Abs(x) into  x if x is Confirmed >= 0
2300  * Optimize Abs(x) into -x if x is Confirmed <= 0
2301  */
2302 static ir_node *transform_node_Abs(ir_node *n)
2303 {
2304   ir_node        *oldn = n;
2305   ir_node        *a = get_Abs_op(n);
2306   value_classify sign = classify_value_sign(a);
2307
2308   if (sign == VALUE_NEGATIVE) {
2309     ir_mode *mode = get_irn_mode(n);
2310
2311     /*
2312      * We can replace the Abs by -x here.
2313      * We even could add a new Confirm here.
2314      *
2315      * Note that -x would create a new node, so we could
2316      * not run it in the equivalent_node() context.
2317      */
2318     n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2319                      get_irn_n(n, -1), a, mode);
2320
2321     DBG_OPT_CONFIRM(oldn, n);
2322   }
2323   else if (sign == VALUE_POSITIVE) {
2324     /* n is positive, Abs is not needed */
2325     n = a;
2326
2327     DBG_OPT_CONFIRM(oldn, n);
2328   }
2329
2330   return n;
2331 }
2332
2333 /**
2334  * Transform a Cond node.
2335  */
2336 static ir_node *transform_node_Cond(ir_node *n)
2337 {
2338   /* Replace the Cond by a Jmp if it branches on a constant
2339      condition. */
2340   ir_node *jmp;
2341   ir_node *a = get_Cond_selector(n);
2342   tarval *ta = value_of(a);
2343
2344   /* we need block info which is not available in floating irgs */
2345   if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2346      return n;
2347
2348   if ((ta != tarval_bad) &&
2349       (get_irn_mode(a) == mode_b) &&
2350       (get_opt_unreachable_code())) {
2351     /* It's a boolean Cond, branching on a boolean constant.
2352                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2353     jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2354     turn_into_tuple(n, pn_Cond_max);
2355     if (ta == tarval_b_true) {
2356       set_Tuple_pred(n, pn_Cond_false, new_Bad());
2357       set_Tuple_pred(n, pn_Cond_true, jmp);
2358     } else {
2359       set_Tuple_pred(n, pn_Cond_false, jmp);
2360       set_Tuple_pred(n, pn_Cond_true, new_Bad());
2361     }
2362     /* We might generate an endless loop, so keep it alive. */
2363     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2364   }
2365   return n;
2366 }
2367
2368 /**
2369  * Transform an And.
2370  */
2371 static ir_node *transform_node_And(ir_node *n)
2372 {
2373   ir_node *c, *oldn = n;
2374   ir_node *a = get_And_left(n);
2375   ir_node *b = get_And_right(n);
2376
2377   HANDLE_BINOP_PHI(tarval_and, a,b,c);
2378   return n;
2379 }
2380
2381 /**
2382  * Transform an Eor.
2383  */
2384 static ir_node *transform_node_Eor(ir_node *n)
2385 {
2386   ir_node *c, *oldn = n;
2387   ir_node *a = get_Eor_left(n);
2388   ir_node *b = get_Eor_right(n);
2389   ir_mode *mode = get_irn_mode(n);
2390
2391   HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2392
2393   if (a == b) {
2394     /* a ^ a = 0 */
2395     n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2396                      mode, get_mode_null(mode));
2397     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2398   }
2399   else if ((mode == mode_b)
2400       && (get_irn_op(a) == op_Proj)
2401       && (get_irn_mode(a) == mode_b)
2402       && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2403       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2404     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2405     n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2406                    mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2407
2408     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2409   }
2410   else if ((mode == mode_b)
2411         && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2412     /* The Eor is a Not. Replace it by a Not. */
2413     /*   ????!!!Extend to bitfield 1111111. */
2414     n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2415
2416     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2417   }
2418
2419   return n;
2420 }
2421
2422 /**
2423  * Transform a Not.
2424  */
2425 static ir_node *transform_node_Not(ir_node *n)
2426 {
2427   ir_node *c, *oldn = n;
2428   ir_node *a = get_Not_op(n);
2429
2430   HANDLE_UNOP_PHI(tarval_not,a,c);
2431
2432   /* check for a boolean Not */
2433   if (   (get_irn_mode(n) == mode_b)
2434       && (get_irn_op(a) == op_Proj)
2435       && (get_irn_mode(a) == mode_b)
2436       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2437     /* We negate a Cmp. The Cmp has the negated result anyways! */
2438     n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2439                    mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2440     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2441   }
2442   return n;
2443 }
2444
2445 /**
2446  * Transform a Minus.
2447  */
2448 static ir_node *transform_node_Minus(ir_node *n)
2449 {
2450   ir_node *c, *oldn = n;
2451   ir_node *a = get_Minus_op(n);
2452
2453   HANDLE_UNOP_PHI(tarval_neg,a,c);
2454   return n;
2455 }
2456
2457 /**
2458  * Transform a Cast_type(Const) into a new Const_type
2459  */
2460 static ir_node *transform_node_Cast(ir_node *n) {
2461   ir_node *oldn = n;
2462   ir_node *pred = get_Cast_op(n);
2463   ir_type *tp = get_irn_type(n);
2464
2465   if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2466     n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2467               get_Const_tarval(pred), tp);
2468     DBG_OPT_CSTEVAL(oldn, n);
2469   } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2470     n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2471                  get_SymConst_kind(pred), tp);
2472     DBG_OPT_CSTEVAL(oldn, n);
2473   }
2474
2475   return n;
2476 }
2477
2478 /**
2479  * Transform a Proj(Div) with a non-zero value.
2480  * Removes the exceptions and routes the memory to the NoMem node.
2481  */
2482 static ir_node *transform_node_Proj_Div(ir_node *proj)
2483 {
2484   ir_node *n = get_Proj_pred(proj);
2485   ir_node *b = get_Div_right(n);
2486   long proj_nr;
2487
2488   if (value_not_zero(b)) {
2489     /* div(x, y) && y != 0 */
2490     proj_nr = get_Proj_proj(proj);
2491
2492     /* this node may float if it did not depend on a Confirm */
2493     set_irn_pinned(n, op_pin_state_floats);
2494
2495     if (proj_nr == pn_Div_X_except) {
2496       /* we found an exception handler, remove it */
2497       DBG_OPT_EXC_REM(proj);
2498       return new_Bad();
2499     }
2500     else if (proj_nr == pn_Div_M) {
2501       ir_node *res = get_Div_mem(n);
2502       /* the memory Proj can only be removed if we divide by a
2503          real constant, but the node never produce a new memory */
2504       if (value_of(b) != tarval_bad) {
2505         /* this is a Div by a const, we can remove the memory edge */
2506         set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2507       }
2508       return res;
2509     }
2510   }
2511   return proj;
2512 }
2513
2514 /**
2515  * Transform a Proj(Mod) with a non-zero value.
2516  * Removes the exceptions and routes the memory to the NoMem node.
2517  */
2518 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2519 {
2520   ir_node *n = get_Proj_pred(proj);
2521   ir_node *b = get_Mod_right(n);
2522   long proj_nr;
2523
2524   if (value_not_zero(b)) {
2525     /* mod(x, y) && y != 0 */
2526     proj_nr = get_Proj_proj(proj);
2527
2528     /* this node may float if it did not depend on a Confirm */
2529     set_irn_pinned(n, op_pin_state_floats);
2530
2531     if (proj_nr == pn_Mod_X_except) {
2532       /* we found an exception handler, remove it */
2533       DBG_OPT_EXC_REM(proj);
2534       return new_Bad();
2535     } else if (proj_nr == pn_Mod_M) {
2536       ir_node *res = get_Mod_mem(n);
2537       /* the memory Proj can only be removed if we divide by a
2538          real constant, but the node never produce a new memory */
2539       if (value_of(b) != tarval_bad) {
2540         /* this is a Mod by a const, we can remove the memory edge */
2541         set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2542       }
2543       return res;
2544     }
2545     else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2546       /* a % a = 0 if a != 0 */
2547       ir_mode *mode = get_irn_mode(proj);
2548       ir_node *res  = new_Const(mode, get_mode_null(mode));
2549
2550       DBG_OPT_CSTEVAL(n, res);
2551       return res;
2552     }
2553   }
2554   return proj;
2555 }
2556
2557 /**
2558  * Transform a Proj(DivMod) with a non-zero value.
2559  * Removes the exceptions and routes the memory to the NoMem node.
2560  */
2561 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2562 {
2563   ir_node *n = get_Proj_pred(proj);
2564   ir_node *b = get_DivMod_right(n);
2565   long proj_nr;
2566
2567   if (value_not_zero(b)) {
2568     /* DivMod(x, y) && y != 0 */
2569     proj_nr = get_Proj_proj(proj);
2570
2571     /* this node may float if it did not depend on a Confirm */
2572     set_irn_pinned(n, op_pin_state_floats);
2573
2574     if (proj_nr == pn_DivMod_X_except) {
2575       /* we found an exception handler, remove it */
2576       DBG_OPT_EXC_REM(proj);
2577       return new_Bad();
2578     }
2579     else if (proj_nr == pn_DivMod_M) {
2580       ir_node *res = get_DivMod_mem(n);
2581       /* the memory Proj can only be removed if we divide by a
2582          real constant, but the node never produce a new memory */
2583       if (value_of(b) != tarval_bad) {
2584         /* this is a DivMod by a const, we can remove the memory edge */
2585         set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2586       }
2587       return res;
2588     }
2589     else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2590       /* a % a = 0 if a != 0 */
2591       ir_mode *mode = get_irn_mode(proj);
2592       ir_node *res  = new_Const(mode, get_mode_null(mode));
2593
2594       DBG_OPT_CSTEVAL(n, res);
2595       return res;
2596     }
2597   }
2598   return proj;
2599 }
2600
2601 /**
2602  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2603  */
2604 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2605 {
2606   if (get_opt_unreachable_code()) {
2607     ir_node *n = get_Proj_pred(proj);
2608     ir_node *b = get_Cond_selector(n);
2609
2610     if (mode_is_int(get_irn_mode(b))) {
2611       tarval *tb = value_of(b);
2612
2613       if (tb != tarval_bad) {
2614         /* we have a constant switch */
2615         long num = get_Proj_proj(proj);
2616
2617         if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2618           if (get_tarval_long(tb) == num) {
2619             /* Do NOT create a jump here, or we will have 2 control flow ops
2620              * in a block. This case is optimized away in optimize_cf(). */
2621             return proj;
2622           }
2623           else {
2624             /* this case will NEVER be taken, kill it */
2625             return new_Bad();
2626           }
2627         }
2628       }
2629     }
2630   }
2631   return proj;
2632 }
2633
2634 /**
2635  * Normalizes and optimizes Cmp nodes.
2636  */
2637 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2638 {
2639   if (get_opt_reassociation()) {
2640     ir_node *n     = get_Proj_pred(proj);
2641     ir_node *left  = get_Cmp_left(n);
2642     ir_node *right = get_Cmp_right(n);
2643     ir_node *c     = NULL;
2644     tarval *tv     = NULL;
2645     int changed    = 0;
2646     ir_mode *mode  = NULL;
2647     long proj_nr   = get_Proj_proj(proj);
2648
2649     /*
2650      * First step: normalize the compare op
2651      * by placing the constant on the right site
2652      * or moving the lower address node to the left.
2653      * We ignore the case that both are constants
2654      * this case should be optimized away.
2655      */
2656     if (get_irn_op(right) == op_Const)
2657       c = right;
2658     else if (get_irn_op(left) == op_Const) {
2659       c     = left;
2660       left  = right;
2661       right = c;
2662
2663       proj_nr = get_inversed_pnc(proj_nr);
2664       changed |= 1;
2665     }
2666     else if (get_irn_idx(left) > get_irn_idx(right)) {
2667       ir_node *t = left;
2668
2669       left  = right;
2670       right = t;
2671
2672       proj_nr = get_inversed_pnc(proj_nr);
2673       changed |= 1;
2674     }
2675
2676     /*
2677      * Second step: Try to reduce the magnitude
2678      * of a constant. This may help to generate better code
2679      * later and may help to normalize more compares.
2680      * Of course this is only possible for integer values.
2681      */
2682     if (c) {
2683       mode = get_irn_mode(c);
2684       tv = get_Const_tarval(c);
2685
2686       if (tv != tarval_bad) {
2687         /* the following optimization is possible on modes without Overflow
2688          * on Unary Minus or on == and !=:
2689          * -a CMP c  ==>  a swap(CMP) -c
2690          *
2691          * Beware: for two-complement Overflow may occur, so only == and != can
2692          * be optimized, see this:
2693          * -MININT < 0 =/=> MININT > 0 !!!
2694          */
2695         if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2696             (!mode_overflow_on_unary_Minus(mode) ||
2697              (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2698           left = get_Minus_op(left);
2699           tv = tarval_sub(get_mode_null(mode), tv);
2700
2701           proj_nr = get_inversed_pnc(proj_nr);
2702           changed |= 2;
2703         }
2704
2705         /* for integer modes, we have more */
2706         if (mode_is_int(mode)) {
2707           /* Ne includes Unordered which is not possible on integers.
2708            * However, frontends often use this wrong, so fix it here */
2709           if (proj_nr & pn_Cmp_Uo) {
2710             proj_nr &= ~pn_Cmp_Uo;
2711             set_Proj_proj(proj, proj_nr);
2712           }
2713
2714           /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
2715           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2716               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2717             tv = tarval_sub(tv, get_mode_one(mode));
2718
2719             proj_nr ^= pn_Cmp_Eq;
2720             changed |= 2;
2721           }
2722           /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
2723           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2724               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2725             tv = tarval_add(tv, get_mode_one(mode));
2726
2727             proj_nr ^= pn_Cmp_Eq;
2728             changed |= 2;
2729           }
2730
2731           /* the following reassociations work only for == and != */
2732           if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2733
2734             /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
2735             if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2736               right = get_Sub_right(left);
2737               left  = get_Sub_left(left);
2738
2739               tv = value_of(right);
2740               changed = 1;
2741             }
2742
2743             if (tv != tarval_bad) {
2744               ir_op *op = get_irn_op(left);
2745
2746               /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
2747               if (op == op_Sub) {
2748                 ir_node *c1 = get_Sub_right(left);
2749                 tarval *tv2 = value_of(c1);
2750
2751                 if (tv2 != tarval_bad) {
2752                   tv2 = tarval_add(tv, value_of(c1));
2753
2754                   if (tv2 != tarval_bad) {
2755                     left    = get_Sub_left(left);
2756                     tv      = tv2;
2757                     changed |= 2;
2758                   }
2759                 }
2760               }
2761               /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
2762               else if (op == op_Add) {
2763                 ir_node *a_l = get_Add_left(left);
2764                 ir_node *a_r = get_Add_right(left);
2765                 ir_node *a;
2766                 tarval *tv2;
2767
2768                 if (get_irn_op(a_l) == op_Const) {
2769                   a = a_r;
2770                   tv2 = value_of(a_l);
2771                 }
2772                 else {
2773                   a = a_l;
2774                   tv2 = value_of(a_r);
2775                 }
2776
2777                 if (tv2 != tarval_bad) {
2778                   tv2 = tarval_sub(tv, tv2);
2779
2780                   if (tv2 != tarval_bad) {
2781                     left    = a;
2782                     tv      = tv2;
2783                     changed |= 2;
2784                   }
2785                 }
2786               }
2787               /* -a == c ==> a == -c, -a != c ==> a != -c */
2788               else if (op == op_Minus) {
2789                 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2790
2791                 if (tv2 != tarval_bad) {
2792                   left    = get_Minus_op(left);
2793                   tv      = tv2;
2794                   changed |= 2;
2795                 }
2796               }
2797             }
2798           } /* == or != */
2799           /* the following reassociations work only for <= */
2800           else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2801             if (tv != tarval_bad) {
2802               ir_op *op = get_irn_op(left);
2803
2804               /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
2805               if (op == op_Abs) {
2806               }
2807             }
2808           }
2809         } /* mode_is_int */
2810
2811         /*
2812          * optimization for AND:
2813          * Optimize:
2814          *   And(x, C) == C  ==>  And(x, C) != 0
2815          *   And(x, C) != C  ==>  And(X, C) == 0
2816          *
2817          * if C is a single Bit constant.
2818          */
2819         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2820             (get_irn_op(left) == op_And)) {
2821           if (is_single_bit_tarval(tv)) {
2822             /* check for Constant's match. We have check hare the tarvals,
2823                because our const might be changed */
2824             ir_node *la = get_And_left(left);
2825             ir_node *ra = get_And_right(left);
2826             if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2827                 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2828               /* fine: do the transformation */
2829               tv = get_mode_null(get_tarval_mode(tv));
2830               proj_nr ^= pn_Cmp_Leg;
2831               changed |= 2;
2832             }
2833           }
2834         }
2835       } /* tarval != bad */
2836     }
2837
2838     if (changed) {
2839       ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2840
2841       if (changed & 2)      /* need a new Const */
2842         right = new_Const(mode, tv);
2843
2844       /* create a new compare */
2845       n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2846             left, right);
2847
2848       set_Proj_pred(proj, n);
2849       set_Proj_proj(proj, proj_nr);
2850     }
2851   }
2852   return proj;
2853 }
2854
2855 /**
2856  * Does all optimizations on nodes that must be done on it's Proj's
2857  * because of creating new nodes.
2858  */
2859 static ir_node *transform_node_Proj(ir_node *proj)
2860 {
2861   ir_node *n = get_Proj_pred(proj);
2862
2863   switch (get_irn_opcode(n)) {
2864   case iro_Div:
2865     return transform_node_Proj_Div(proj);
2866
2867   case iro_Mod:
2868     return transform_node_Proj_Mod(proj);
2869
2870   case iro_DivMod:
2871     return transform_node_Proj_DivMod(proj);
2872
2873   case iro_Cond:
2874     return transform_node_Proj_Cond(proj);
2875
2876   case iro_Cmp:
2877     return transform_node_Proj_Cmp(proj);
2878
2879   case iro_Tuple:
2880     /* should not happen, but if it does will be optimized away */
2881     return equivalent_node_Proj(proj);
2882
2883   default:
2884     /* do nothing */
2885     return proj;
2886   }
2887 }
2888
2889 /**
2890  * Move Confirms down through Phi nodes.
2891  */
2892 static ir_node *transform_node_Phi(ir_node *phi) {
2893   int i, n;
2894   ir_mode *mode = get_irn_mode(phi);
2895
2896   if (mode_is_reference(mode)) {
2897     n = get_irn_arity(phi);
2898
2899     /* Beware of Phi0 */
2900     if (n > 0) {
2901       ir_node *pred = get_irn_n(phi, 0);
2902       ir_node *bound, *new_Phi, *block, **in;
2903       pn_Cmp  pnc;
2904
2905       if (! is_Confirm(pred))
2906         return phi;
2907
2908       bound = get_Confirm_bound(pred);
2909       pnc   = get_Confirm_cmp(pred);
2910
2911       NEW_ARR_A(ir_node *, in, n);
2912       in[0] = get_Confirm_value(pred);
2913
2914       for (i = 1; i < n; ++i) {
2915         pred = get_irn_n(phi, i);
2916
2917         if (! is_Confirm(pred) ||
2918             get_Confirm_bound(pred) != bound ||
2919             get_Confirm_cmp(pred) != pnc)
2920           return phi;
2921         in[i] = get_Confirm_value(pred);
2922       }
2923       /* move the Confirm nodes "behind" the Phi */
2924       block = get_irn_n(phi, -1);
2925       new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2926       return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2927     }
2928   }
2929   return phi;
2930 }
2931
2932 /**
2933  * returns the operands of a commutative bin-op, if one operand is
2934  * a const, it is returned as the second one.
2935  */
2936 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2937 {
2938   ir_node *op_a = get_binop_left(binop);
2939   ir_node *op_b = get_binop_right(binop);
2940
2941   assert(is_op_commutative(get_irn_op(binop)));
2942
2943   if (get_irn_op(op_a) == op_Const) {
2944     *a = op_b;
2945     *c = op_a;
2946   }
2947   else {
2948     *a = op_a;
2949     *c = op_b;
2950   }
2951 }
2952
2953 /**
2954  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2955  * Such pattern may arise in bitfield stores.
2956  *
2957  * value  c4                  value      c4 & c2
2958  *    AND     c3                    AND           c1 | c3
2959  *        OR     c2      ===>               OR
2960  *           AND    c1
2961  *               OR
2962  */
2963 static ir_node *transform_node_Or_bf_store(ir_node *or)
2964 {
2965   ir_node *and, *c1;
2966   ir_node *or_l, *c2;
2967   ir_node *and_l, *c3;
2968   ir_node *value, *c4;
2969   ir_node *new_and, *new_const, *block;
2970   ir_mode *mode = get_irn_mode(or);
2971
2972   tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2973
2974   get_comm_Binop_Ops(or, &and, &c1);
2975   if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2976     return or;
2977
2978   get_comm_Binop_Ops(and, &or_l, &c2);
2979   if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2980     return or;
2981
2982   get_comm_Binop_Ops(or_l, &and_l, &c3);
2983   if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2984     return or;
2985
2986   get_comm_Binop_Ops(and_l, &value, &c4);
2987   if (get_irn_op(c4) != op_Const)
2988     return or;
2989
2990   /* ok, found the pattern, check for conditions */
2991   assert(mode == get_irn_mode(and));
2992   assert(mode == get_irn_mode(or_l));
2993   assert(mode == get_irn_mode(and_l));
2994
2995   tv1 = get_Const_tarval(c1);
2996   tv2 = get_Const_tarval(c2);
2997   tv3 = get_Const_tarval(c3);
2998   tv4 = get_Const_tarval(c4);
2999
3000   tv = tarval_or(tv4, tv2);
3001   if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
3002     /* have at least one 0 at the same bit position */
3003     return or;
3004   }
3005
3006   n_tv4 = tarval_not(tv4);
3007   if (tv3 != tarval_and(tv3, n_tv4)) {
3008     /* bit in the or_mask is outside the and_mask */
3009     return or;
3010   }
3011
3012   n_tv2 = tarval_not(tv2);
3013   if (tv1 != tarval_and(tv1, n_tv2)) {
3014     /* bit in the or_mask is outside the and_mask */
3015     return or;
3016   }
3017
3018   /* ok, all conditions met */
3019   block = get_irn_n(or, -1);
3020
3021   new_and = new_r_And(current_ir_graph, block,
3022       value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3023
3024   new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3025
3026   set_Or_left(or, new_and);
3027   set_Or_right(or, new_const);
3028
3029   /* check for more */
3030   return transform_node_Or_bf_store(or);
3031 }
3032
3033 /**
3034  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3035  */
3036 static ir_node *transform_node_Or_Rot(ir_node *or)
3037 {
3038   ir_mode *mode = get_irn_mode(or);
3039   ir_node *shl, *shr, *block;
3040   ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3041   tarval *tv1, *tv2;
3042
3043   if (! mode_is_int(mode))
3044     return or;
3045
3046   shl = get_binop_left(or);
3047   shr = get_binop_right(or);
3048
3049   if (get_irn_op(shl) == op_Shr) {
3050     if (get_irn_op(shr) != op_Shl)
3051       return or;
3052
3053     irn = shl;
3054     shl = shr;
3055     shr = irn;
3056   }
3057   else if (get_irn_op(shl) != op_Shl)
3058     return or;
3059   else if (get_irn_op(shr) != op_Shr)
3060     return or;
3061
3062   x = get_Shl_left(shl);
3063   if (x != get_Shr_left(shr))
3064     return or;
3065
3066   c1 = get_Shl_right(shl);
3067   c2 = get_Shr_right(shr);
3068   if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3069     tv1 = get_Const_tarval(c1);
3070     if (! tarval_is_long(tv1))
3071       return or;
3072
3073     tv2 = get_Const_tarval(c2);
3074     if (! tarval_is_long(tv2))
3075       return or;
3076
3077     if (get_tarval_long(tv1) + get_tarval_long(tv2)
3078         != get_mode_size_bits(mode))
3079       return or;
3080
3081     /* yet, condition met */
3082     block = get_irn_n(or, -1);
3083
3084     n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3085
3086     DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3087     return n;
3088   }
3089   else if (get_irn_op(c1) == op_Sub) {
3090     v   = c2;
3091     sub = c1;
3092
3093     if (get_Sub_right(sub) != v)
3094       return or;
3095
3096     c1 = get_Sub_left(sub);
3097     if (get_irn_op(c1) != op_Const)
3098       return or;
3099
3100     tv1 = get_Const_tarval(c1);
3101     if (! tarval_is_long(tv1))
3102       return or;
3103
3104     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3105       return or;
3106
3107     /* yet, condition met */
3108     block = get_nodes_block(or);
3109
3110     /* a Rot right is not supported, so use a rot left */
3111     n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
3112
3113     DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3114     return n;
3115   }
3116   else if (get_irn_op(c2) == op_Sub) {
3117     v   = c1;
3118     sub = c2;
3119
3120     c1 = get_Sub_left(sub);
3121     if (get_irn_op(c1) != op_Const)
3122       return or;
3123
3124     tv1 = get_Const_tarval(c1);
3125     if (! tarval_is_long(tv1))
3126       return or;
3127
3128     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3129       return or;
3130
3131     /* yet, condition met */
3132     block = get_irn_n(or, -1);
3133
3134     /* a Rot Left */
3135     n = new_r_Rot(current_ir_graph, block, x, v, mode);
3136
3137     DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3138     return n;
3139   }
3140
3141   return or;
3142 }
3143
3144 /**
3145  * Transform an Or.
3146  */
3147 static ir_node *transform_node_Or(ir_node *n)
3148 {
3149   ir_node *c, *oldn = n;
3150   ir_node *a = get_Or_left(n);
3151   ir_node *b = get_Or_right(n);
3152
3153   HANDLE_BINOP_PHI(tarval_or, a,b,c);
3154
3155   n = transform_node_Or_bf_store(n);
3156   n = transform_node_Or_Rot(n);
3157
3158   return n;
3159 }
3160
3161
3162 /* forward */
3163 static ir_node *transform_node(ir_node *n);
3164
3165 /**
3166  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3167  *
3168  * Should be moved to reassociation?
3169  */
3170 static ir_node *transform_node_shift(ir_node *n)
3171 {
3172   ir_node *left, *right;
3173   tarval *tv1, *tv2, *res;
3174   ir_mode *mode;
3175   int modulo_shf, flag;
3176
3177   left = get_binop_left(n);
3178
3179   /* different operations */
3180   if (get_irn_op(left) != get_irn_op(n))
3181     return n;
3182
3183   right = get_binop_right(n);
3184   tv1 = value_of(right);
3185   if (tv1 == tarval_bad)
3186     return n;
3187
3188   tv2 = value_of(get_binop_right(left));
3189   if (tv2 == tarval_bad)
3190     return n;
3191
3192   res = tarval_add(tv1, tv2);
3193
3194   /* beware: a simple replacement works only, if res < modulo shift */
3195   mode = get_irn_mode(n);
3196
3197   flag = 0;
3198
3199   modulo_shf = get_mode_modulo_shift(mode);
3200   if (modulo_shf > 0) {
3201     tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3202
3203     if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3204       flag = 1;
3205   }
3206   else
3207     flag = 1;
3208
3209   if (flag) {
3210     /* ok, we can replace it */
3211     ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3212
3213     in[0] = get_binop_left(left);
3214     in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3215
3216     irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3217
3218     DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3219
3220     return transform_node(irn);
3221   }
3222   return n;
3223 }
3224
3225 /**
3226  * Transform a Shr.
3227  */
3228 static ir_node *transform_node_Shr(ir_node *n)
3229 {
3230   ir_node *c, *oldn = n;
3231   ir_node *a = get_Shr_left(n);
3232   ir_node *b = get_Shr_right(n);
3233
3234   HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3235   return transform_node_shift(n);
3236 }
3237
3238 /**
3239  * Transform a Shrs.
3240  */
3241 static ir_node *transform_node_Shrs(ir_node *n)
3242 {
3243   ir_node *c, *oldn = n;
3244   ir_node *a = get_Shrs_left(n);
3245   ir_node *b = get_Shrs_right(n);
3246
3247   HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3248   return transform_node_shift(n);
3249 }
3250
3251 /**
3252  * Transform a Shl.
3253  */
3254 static ir_node *transform_node_Shl(ir_node *n)
3255 {
3256   ir_node *c, *oldn = n;
3257   ir_node *a = get_Shl_left(n);
3258   ir_node *b = get_Shl_right(n);
3259
3260   HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3261   return transform_node_shift(n);
3262 }
3263
3264 /**
3265  * Remove dead blocks and nodes in dead blocks
3266  * in keep alive list.  We do not generate a new End node.
3267  */
3268 static ir_node *transform_node_End(ir_node *n) {
3269   int i, n_keepalives = get_End_n_keepalives(n);
3270
3271   for (i = 0; i < n_keepalives; ++i) {
3272     ir_node *ka = get_End_keepalive(n, i);
3273     if (is_Block(ka)) {
3274       if (is_Block_dead(ka)) {
3275         set_End_keepalive(n, i, new_Bad());
3276       }
3277     }
3278     else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3279       set_End_keepalive(n, i, new_Bad());
3280   }
3281   return n;
3282 }
3283
3284 /**
3285  * Optimize a Mux into some simpler cases.
3286  */
3287 static ir_node *transform_node_Mux(ir_node *n)
3288 {
3289   ir_node *oldn = n, *sel = get_Mux_sel(n);
3290   ir_mode *mode = get_irn_mode(n);
3291
3292   if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3293     ir_node *cmp = get_Proj_pred(sel);
3294     long proj_nr = get_Proj_proj(sel);
3295     ir_node *f   =  get_Mux_false(n);
3296     ir_node *t   = get_Mux_true(n);
3297
3298     if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3299       ir_node *block = get_irn_n(n, -1);
3300
3301       /*
3302        * Note: normalization puts the constant on the right site,
3303        * so we check only one case.
3304        *
3305        * Note further that these optimization work even for floating point
3306        * with NaN's because -NaN == NaN.
3307        * However, if +0 and -0 is handled differently, we cannot use the first one.
3308        */
3309       if (get_irn_op(f) == op_Minus &&
3310           get_Minus_op(f)   == t &&
3311           get_Cmp_left(cmp) == t) {
3312
3313         if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3314           /* Mux(a >=/> 0, -a, a)  ==>  Abs(a) */
3315           n = new_rd_Abs(get_irn_dbg_info(n),
3316                 current_ir_graph,
3317                 block,
3318                 t, mode);
3319           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3320           return n;
3321         }
3322         else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3323           /* Mux(a <=/< 0, -a, a)  ==>  Minus(Abs(a)) */
3324           n = new_rd_Abs(get_irn_dbg_info(n),
3325                 current_ir_graph,
3326                 block,
3327                 t, mode);
3328           n = new_rd_Minus(get_irn_dbg_info(n),
3329                 current_ir_graph,
3330                 block,
3331                 n, mode);
3332
3333           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3334           return n;
3335         }
3336       }
3337       else if (get_irn_op(t) == op_Minus &&
3338           get_Minus_op(t)   == f &&
3339           get_Cmp_left(cmp) == f) {
3340
3341         if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3342           /* Mux(a <=/< 0, a, -a)  ==>  Abs(a) */
3343           n = new_rd_Abs(get_irn_dbg_info(n),
3344                 current_ir_graph,
3345                 block,
3346                 f, mode);
3347           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3348           return n;
3349         }
3350         else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3351           /* Mux(a >=/> 0, a, -a)  ==>  Minus(Abs(a)) */
3352           n = new_rd_Abs(get_irn_dbg_info(n),
3353                 current_ir_graph,
3354                 block,
3355                 f, mode);
3356           n = new_rd_Minus(get_irn_dbg_info(n),
3357                 current_ir_graph,
3358                 block,
3359                 n, mode);
3360
3361           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3362           return n;
3363         }
3364       }
3365
3366       if (mode_is_int(mode) && mode_is_signed(mode) &&
3367           get_mode_arithmetic(mode) == irma_twos_complement) {
3368         ir_node *x = get_Cmp_left(cmp);
3369
3370         /* the following optimization works only with signed integer two-complement mode */
3371
3372         if (mode == get_irn_mode(x)) {
3373           /*
3374            * FIXME: this restriction is two rigid, as it would still
3375            * work if mode(x) = Hs and mode == Is, but at least it removes
3376            * all wrong cases.
3377            */
3378           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3379               classify_Const(t) == CNST_ALL_ONE &&
3380               classify_Const(f) == CNST_NULL) {
3381             /*
3382              * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3383              * Conditions:
3384              * T must be signed.
3385              */
3386             n = new_rd_Shrs(get_irn_dbg_info(n),
3387                   current_ir_graph, block, x,
3388                   new_r_Const_long(current_ir_graph, block, mode_Iu,
3389                     get_mode_size_bits(mode) - 1),
3390                   mode);
3391             DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3392             return n;
3393           }
3394           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3395                    classify_Const(t) == CNST_ONE &&
3396                    classify_Const(f) == CNST_NULL) {
3397             /*
3398              * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3399              * Conditions:
3400              * T must be signed.
3401              */
3402             n = new_rd_Shr(get_irn_dbg_info(n),
3403                   current_ir_graph, block,
3404                   new_r_Minus(current_ir_graph, block, x, mode),
3405                   new_r_Const_long(current_ir_graph, block, mode_Iu,
3406                     get_mode_size_bits(mode) - 1),
3407                   mode);
3408             DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3409             return n;
3410           }
3411         }
3412       }
3413     }
3414   }
3415   return arch_transform_node_Mux(n);
3416 }
3417
3418 /**
3419  * Optimize a Psi into some simpler cases.
3420  */
3421 static ir_node *transform_node_Psi(ir_node *n) {
3422   if (is_Mux(n))
3423     return transform_node_Mux(n);
3424
3425   return n;
3426 }
3427
3428 /**
3429  * Tries several [inplace] [optimizing] transformations and returns an
3430  * equivalent node.  The difference to equivalent_node() is that these
3431  * transformations _do_ generate new nodes, and thus the old node must
3432  * not be freed even if the equivalent node isn't the old one.
3433  */
3434 static ir_node *transform_node(ir_node *n)
3435 {
3436   if (n->op->ops.transform_node)
3437     n = n->op->ops.transform_node(n);
3438   return n;
3439 }
3440
3441 /**
3442  * sSets the default transform node operation for an ir_op_ops.
3443  *
3444  * @param code   the opcode for the default operation
3445  * @param ops    the operations initialized
3446  *
3447  * @return
3448  *    The operations.
3449  */
3450 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3451 {
3452 #define CASE(a)                                 \
3453   case iro_##a:                                 \
3454     ops->transform_node  = transform_node_##a;   \
3455     break
3456
3457   switch (code) {
3458   CASE(Add);
3459   CASE(Sub);
3460   CASE(Mul);
3461   CASE(Div);
3462   CASE(Mod);
3463   CASE(DivMod);
3464   CASE(Abs);
3465   CASE(Cond);
3466   CASE(And);
3467   CASE(Or);
3468   CASE(Eor);
3469   CASE(Minus);
3470   CASE(Not);
3471   CASE(Cast);
3472   CASE(Proj);
3473   CASE(Phi);
3474   CASE(Sel);
3475   CASE(Shr);
3476   CASE(Shrs);
3477   CASE(Shl);
3478   CASE(End);
3479   CASE(Mux);
3480   CASE(Psi);
3481   default:
3482     /* leave NULL */;
3483   }
3484
3485   return ops;
3486 #undef CASE
3487 }
3488
3489
3490 /* **************** Common Subexpression Elimination **************** */
3491
3492 /** The size of the hash table used, should estimate the number of nodes
3493     in a graph. */
3494 #define N_IR_NODES 512
3495
3496 /** Compares the attributes of two Const nodes. */
3497 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3498 {
3499   return (get_Const_tarval(a) != get_Const_tarval(b))
3500       || (get_Const_type(a) != get_Const_type(b));
3501 }
3502
3503 /** Compares the attributes of two Proj nodes. */
3504 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3505   return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3506 }
3507
3508 /** Compares the attributes of two Filter nodes. */
3509 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3510 {
3511   return get_Filter_proj(a) != get_Filter_proj(b);
3512 }
3513
3514 /** Compares the attributes of two Alloc nodes. */
3515 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3516 {
3517   return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3518       || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3519 }
3520
3521 /** Compares the attributes of two Free nodes. */
3522 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3523 {
3524   return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3525       || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3526 }
3527
3528 /** Compares the attributes of two SymConst nodes. */
3529 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3530 {
3531   return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3532       || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3533       || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3534 }
3535
3536 /** Compares the attributes of two Call nodes. */
3537 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3538   return (get_irn_call_attr(a) != get_irn_call_attr(b));
3539 }
3540
3541 /** Compares the attributes of two Sel nodes. */
3542 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3543 {
3544   return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
3545       || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
3546       || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
3547       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3548       || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
3549 }
3550
3551 /** Compares the attributes of two Phi nodes. */
3552 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3553   return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3554 }
3555
3556 /** Compares the attributes of two Conv nodes. */
3557 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3558   return get_Conv_strict(a) != get_Conv_strict(b);
3559 }
3560
3561 /** Compares the attributes of two Cast nodes. */
3562 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3563   return get_Cast_type(a) != get_Cast_type(b);
3564 }
3565
3566 /** Compares the attributes of two Load nodes. */
3567 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3568 {
3569   if (get_Load_volatility(a) == volatility_is_volatile ||
3570       get_Load_volatility(b) == volatility_is_volatile)
3571     /* NEVER do CSE on volatile Loads */
3572     return 1;
3573
3574   return get_Load_mode(a) != get_Load_mode(b);
3575 }
3576
3577 /** Compares the attributes of two Store nodes. */
3578 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3579 {
3580   /* NEVER do CSE on volatile Stores */
3581   return (get_Store_volatility(a) == volatility_is_volatile ||
3582           get_Store_volatility(b) == volatility_is_volatile);
3583 }
3584
3585 /** Compares the attributes of two Confirm nodes. */
3586 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3587 {
3588   return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3589 }
3590
3591 /**
3592  * Set the default node attribute compare operation for an ir_op_ops.
3593  *
3594  * @param code   the opcode for the default operation
3595  * @param ops    the operations initialized
3596  *
3597  * @return
3598  *    The operations.
3599  */
3600 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3601 {
3602 #define CASE(a)                              \
3603   case iro_##a:                              \
3604     ops->node_cmp_attr  = node_cmp_attr_##a; \
3605     break
3606
3607   switch (code) {
3608   CASE(Const);
3609   CASE(Proj);
3610   CASE(Filter);
3611   CASE(Alloc);
3612   CASE(Free);
3613   CASE(SymConst);
3614   CASE(Call);
3615   CASE(Sel);
3616   CASE(Phi);
3617   CASE(Conv);
3618   CASE(Cast);
3619   CASE(Load);
3620   CASE(Store);
3621   CASE(Confirm);
3622   default:
3623     /* leave NULL */;
3624   }
3625
3626   return ops;
3627 #undef CASE
3628 }
3629
3630 /*
3631  * Compare function for two nodes in the hash table. Gets two
3632  * nodes as parameters.  Returns 0 if the nodes are a cse.
3633  */
3634 int identities_cmp(const void *elt, const void *key)
3635 {
3636   ir_node *a, *b;
3637   int i, irn_arity_a;
3638
3639   a = (void *)elt;
3640   b = (void *)key;
3641
3642   if (a == b) return 0;
3643
3644   if ((get_irn_op(a) != get_irn_op(b)) ||
3645       (get_irn_mode(a) != get_irn_mode(b))) return 1;
3646
3647   /* compare if a's in and b's in are of equal length */
3648   irn_arity_a = get_irn_intra_arity (a);
3649   if (irn_arity_a != get_irn_intra_arity(b))
3650     return 1;
3651
3652   /* for block-local cse and op_pin_state_pinned nodes: */
3653   if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3654     if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3655       return 1;
3656   }
3657
3658   /* compare a->in[0..ins] with b->in[0..ins] */
3659   for (i = 0; i < irn_arity_a; i++)
3660     if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3661       return 1;
3662
3663   /*
3664    * here, we already now that the nodes are identical except their
3665    * attributes
3666    */
3667   if (a->op->ops.node_cmp_attr)
3668     return a->op->ops.node_cmp_attr(a, b);
3669
3670   return 0;
3671 }
3672
3673 /*
3674  * Calculate a hash value of a node.
3675  */
3676 unsigned ir_node_hash(ir_node *node)
3677 {
3678   unsigned h;
3679   int i, irn_arity;
3680
3681   if (node->op == op_Const) {
3682     /* special value for const, as they only differ in their tarval. */
3683     h = HASH_PTR(node->attr.con.tv);
3684     h = 9*h + HASH_PTR(get_irn_mode(node));
3685   } else if (node->op == op_SymConst) {
3686     /* special value for const, as they only differ in their symbol. */
3687     h = HASH_PTR(node->attr.symc.sym.type_p);
3688     h = 9*h + HASH_PTR(get_irn_mode(node));
3689   } else {
3690
3691     /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3692     h = irn_arity = get_irn_intra_arity(node);
3693
3694     /* consider all in nodes... except the block if not a control flow. */
3695     for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
3696       h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3697     }
3698
3699     /* ...mode,... */
3700     h = 9*h + HASH_PTR(get_irn_mode(node));
3701     /* ...and code */
3702     h = 9*h + HASH_PTR(get_irn_op(node));
3703   }
3704
3705   return h;
3706 }
3707
3708 pset *new_identities(void) {
3709   return new_pset(identities_cmp, N_IR_NODES);
3710 }
3711
3712 void del_identities(pset *value_table) {
3713   del_pset(value_table);
3714 }
3715
3716 /**
3717  * Return the canonical node computing the same value as n.
3718  * Looks up the node in a hash table.
3719  *
3720  * For Const nodes this is performed in the constructor, too.  Const
3721  * nodes are extremely time critical because of their frequent use in
3722  * constant string arrays.
3723  */
3724 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3725 {
3726   ir_node *o = NULL;
3727
3728   if (!value_table) return n;
3729
3730   if (get_opt_reassociation()) {
3731     if (is_op_commutative(get_irn_op(n))) {
3732       ir_node *l = get_binop_left(n);
3733       ir_node *r = get_binop_right(n);
3734
3735       /* for commutative operators perform  a OP b == b OP a */
3736       if (get_irn_idx(l) > get_irn_idx(r)) {
3737         set_binop_left(n, r);
3738         set_binop_right(n, l);
3739       }
3740     }
3741   }
3742
3743   o = pset_find(value_table, n, ir_node_hash(n));
3744   if (!o) return n;
3745
3746   DBG_OPT_CSE(n, o);
3747
3748   return o;
3749 }
3750
3751 /**
3752  * During construction we set the op_pin_state_pinned flag in the graph right when the
3753  * optimization is performed.  The flag turning on procedure global cse could
3754  * be changed between two allocations.  This way we are safe.
3755  */
3756 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3757   ir_node *old = n;
3758
3759   n = identify(value_table, n);
3760   if (get_irn_n(old, -1) != get_irn_n(n, -1))
3761     set_irg_pinned(current_ir_graph, op_pin_state_floats);
3762   return n;
3763 }
3764
3765 /*
3766  * Return the canonical node computing the same value as n.
3767  * Looks up the node in a hash table, enters it in the table
3768  * if it isn't there yet.
3769  */
3770 ir_node *identify_remember(pset *value_table, ir_node *n)
3771 {
3772   ir_node *o = NULL;
3773
3774   if (!value_table) return n;
3775
3776   if (get_opt_reassociation()) {
3777     if (is_op_commutative(get_irn_op(n))) {
3778       ir_node *l = get_binop_left(n);
3779       ir_node *r = get_binop_right(n);
3780
3781       /* for commutative operators perform  a OP b == b OP a */
3782       if (l > r) {
3783         set_binop_left(n, r);
3784         set_binop_right(n, l);
3785       }
3786     }
3787   }
3788
3789   /* lookup or insert in hash table with given hash key. */
3790   o = pset_insert (value_table, n, ir_node_hash (n));
3791
3792   if (o != n) {
3793     DBG_OPT_CSE(n, o);
3794   }
3795
3796   return o;
3797 }
3798
3799 /* Add a node to the identities value table. */
3800 void add_identities(pset *value_table, ir_node *node) {
3801   if (get_opt_cse() && is_no_Block(node))
3802     identify_remember(value_table, node);
3803 }
3804
3805 /* Visit each node in the value table of a graph. */
3806 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3807   ir_node *node;
3808   ir_graph *rem = current_ir_graph;
3809
3810   current_ir_graph = irg;
3811   foreach_pset(irg->value_table, node)
3812     visit(node, env);
3813   current_ir_graph = rem;
3814 }
3815
3816 /**
3817  * Garbage in, garbage out. If a node has a dead input, i.e., the
3818  * Bad node is input to the node, return the Bad node.
3819  */
3820 static INLINE ir_node *gigo(ir_node *node)
3821 {
3822   int i, irn_arity;
3823   ir_op *op = get_irn_op(node);
3824
3825   /* remove garbage blocks by looking at control flow that leaves the block
3826      and replacing the control flow by Bad. */
3827   if (get_irn_mode(node) == mode_X) {
3828     ir_node *block = get_nodes_block(skip_Proj(node));
3829
3830     /* Don't optimize nodes in immature blocks. */
3831     if (!get_Block_matured(block)) return node;
3832      /* Don't optimize End, may have Bads. */
3833     if (op == op_End) return node;
3834
3835     if (is_Block(block)) {
3836       irn_arity = get_irn_arity(block);
3837       for (i = 0; i < irn_arity; i++) {
3838         if (!is_Bad(get_irn_n(block, i)))
3839           break;
3840       }
3841       if (i == irn_arity) return new_Bad();
3842     }
3843   }
3844
3845   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3846      blocks predecessors is dead. */
3847   if (op != op_Block && op != op_Phi && op != op_Tuple) {
3848     irn_arity = get_irn_arity(node);
3849
3850     /*
3851      * Beware: we can only read the block of a non-floating node.
3852      */
3853     if (is_irn_pinned_in_irg(node) &&
3854         is_Block_dead(get_nodes_block(node)))
3855       return new_Bad();
3856
3857     for (i = 0; i < irn_arity; i++) {
3858       ir_node *pred = get_irn_n(node, i);
3859
3860       if (is_Bad(pred))
3861         return new_Bad();
3862 #if 0
3863       /* Propagating Unknowns here seems to be a bad idea, because
3864          sometimes we need a node as a input and did not want that
3865          it kills it's user.
3866          However, it might be useful to move this into a later phase
3867          (if you think that optimizing such code is useful). */
3868       if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3869         return new_Unknown(get_irn_mode(node));
3870 #endif
3871     }
3872   }
3873 #if 0
3874   /* With this code we violate the agreement that local_optimize
3875      only leaves Bads in Block, Phi and Tuple nodes. */
3876   /* If Block has only Bads as predecessors it's garbage. */
3877   /* If Phi has only Bads as predecessors it's garbage. */
3878   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
3879     irn_arity = get_irn_arity(node);
3880     for (i = 0; i < irn_arity; i++) {
3881       if (!is_Bad(get_irn_n(node, i))) break;
3882     }
3883     if (i == irn_arity) node = new_Bad();
3884   }
3885 #endif
3886   return node;
3887 }
3888
3889 /**
3890  * These optimizations deallocate nodes from the obstack.
3891  * It can only be called if it is guaranteed that no other nodes
3892  * reference this one, i.e., right after construction of a node.
3893  *
3894  * current_ir_graph must be set to the graph of the node!
3895  */
3896 ir_node *optimize_node(ir_node *n)
3897 {
3898   tarval *tv;
3899   ir_node *oldn = n;
3900   opcode iro = get_irn_opcode(n);
3901
3902   /* Always optimize Phi nodes: part of the construction. */
3903   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3904
3905   /* constant expression evaluation / constant folding */
3906   if (get_opt_constant_folding()) {
3907     /* neither constants nor Tuple values can be evaluated */
3908     if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3909       /* try to evaluate */
3910       tv = computed_value(n);
3911       if (tv != tarval_bad) {
3912         ir_node *nw;
3913         ir_type *old_tp = get_irn_type(n);
3914         int i, arity = get_irn_arity(n);
3915         int node_size;
3916
3917         /*
3918          * Try to recover the type of the new expression.
3919          */
3920         for (i = 0; i < arity && !old_tp; ++i)
3921           old_tp = get_irn_type(get_irn_n(n, i));
3922
3923         /*
3924          * we MUST copy the node here temporary, because it's still needed
3925          * for DBG_OPT_CSTEVAL
3926          */
3927         node_size = offsetof(ir_node, attr) +  n->op->attr_size;
3928         oldn = alloca(node_size);
3929
3930         memcpy(oldn, n, node_size);
3931         CLONE_ARR_A(ir_node *, oldn->in, n->in);
3932
3933         /* ARG, copy the in array, we need it for statistics */
3934         memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3935
3936         /* note the inplace edges module */
3937         edges_node_deleted(n, current_ir_graph);
3938
3939         /* evaluation was successful -- replace the node. */
3940         irg_kill_node(current_ir_graph, n);
3941         nw = new_Const(get_tarval_mode (tv), tv);
3942
3943         if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3944           set_Const_type(nw, old_tp);
3945         DBG_OPT_CSTEVAL(oldn, nw);
3946         return nw;
3947       }
3948     }
3949   }
3950
3951   /* remove unnecessary nodes */
3952   if (get_opt_constant_folding() ||
3953     (iro == iro_Phi)  ||   /* always optimize these nodes. */
3954     (iro == iro_Id)   ||
3955     (iro == iro_Proj) ||
3956     (iro == iro_Block)  )  /* Flags tested local. */
3957     n = equivalent_node (n);
3958
3959   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3960
3961   /* Common Subexpression Elimination.
3962    *
3963    * Checks whether n is already available.
3964    * The block input is used to distinguish different subexpressions. Right
3965    * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3966    * subexpressions within a block.
3967    */
3968   if (get_opt_cse())
3969     n = identify_cons (current_ir_graph->value_table, n);
3970
3971   if (n != oldn) {
3972     edges_node_deleted(oldn, current_ir_graph);
3973
3974     /* We found an existing, better node, so we can deallocate the old node. */
3975     irg_kill_node(current_ir_graph, oldn);
3976     return n;
3977   }
3978
3979   /* Some more constant expression evaluation that does not allow to
3980      free the node. */
3981   iro = get_irn_opcode(n);
3982   if (get_opt_constant_folding() ||
3983     (iro == iro_Cond) ||
3984     (iro == iro_Proj) ||
3985     (iro == iro_Sel))     /* Flags tested local. */
3986     n = transform_node (n);
3987
3988   /* Remove nodes with dead (Bad) input.
3989      Run always for transformation induced Bads. */
3990   n = gigo (n);
3991
3992   /* Now we have a legal, useful node. Enter it in hash table for CSE */
3993   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3994     n = identify_remember (current_ir_graph->value_table, n);
3995   }
3996
3997   return n;
3998 }
3999
4000
4001 /**
4002  * These optimizations never deallocate nodes (in place).  This can cause dead
4003  * nodes lying on the obstack.  Remove these by a dead node elimination,
4004  * i.e., a copying garbage collection.
4005  */
4006 ir_node *optimize_in_place_2(ir_node *n)
4007 {
4008   tarval *tv;
4009   ir_node *oldn = n;
4010   opcode iro = get_irn_opcode(n);
4011
4012   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
4013
4014   /* constant expression evaluation / constant folding */
4015   if (get_opt_constant_folding()) {
4016     /* neither constants nor Tuple values can be evaluated */
4017     if (iro != iro_Const && get_irn_mode(n) != mode_T) {
4018       /* try to evaluate */
4019       tv = computed_value(n);
4020       if (tv != tarval_bad) {
4021         /* evaluation was successful -- replace the node. */
4022         ir_type *old_tp = get_irn_type(n);
4023         int i, arity = get_irn_arity(n);
4024
4025         /*
4026          * Try to recover the type of the new expression.
4027          */
4028         for (i = 0; i < arity && !old_tp; ++i)
4029           old_tp = get_irn_type(get_irn_n(n, i));
4030
4031         n = new_Const(get_tarval_mode(tv), tv);
4032
4033         if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4034           set_Const_type(n, old_tp);
4035
4036         DBG_OPT_CSTEVAL(oldn, n);
4037         return n;
4038       }
4039     }
4040   }
4041
4042   /* remove unnecessary nodes */
4043   if (get_opt_constant_folding() ||
4044       (iro == iro_Phi)  ||   /* always optimize these nodes. */
4045       (iro == iro_Id)   ||   /* ... */
4046       (iro == iro_Proj) ||   /* ... */
4047       (iro == iro_Block)  )  /* Flags tested local. */
4048     n = equivalent_node(n);
4049
4050   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
4051
4052   /** common subexpression elimination **/
4053   /* Checks whether n is already available. */
4054   /* The block input is used to distinguish different subexpressions.  Right
4055      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4056      subexpressions within a block. */
4057   if (get_opt_cse()) {
4058     n = identify(current_ir_graph->value_table, n);
4059   }
4060
4061   /* Some more constant expression evaluation. */
4062   iro = get_irn_opcode(n);
4063   if (get_opt_constant_folding() ||
4064       (iro == iro_Cond) ||
4065       (iro == iro_Proj) ||
4066       (iro == iro_Sel))     /* Flags tested local. */
4067     n = transform_node(n);
4068
4069   /* Remove nodes with dead (Bad) input.
4070      Run always for transformation induced Bads.  */
4071   n = gigo(n);
4072
4073   /* Now we can verify the node, as it has no dead inputs any more. */
4074   irn_vrfy(n);
4075
4076   /* Now we have a legal, useful node. Enter it in hash table for cse.
4077      Blocks should be unique anyways.  (Except the successor of start:
4078      is cse with the start block!) */
4079   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4080     n = identify_remember(current_ir_graph->value_table, n);
4081
4082   return n;
4083 }
4084
4085 /**
4086  * Wrapper for external use, set proper status bits after optimization.
4087  */
4088 ir_node *optimize_in_place(ir_node *n)
4089 {
4090   /* Handle graph state */
4091   assert(get_irg_phase_state(current_ir_graph) != phase_building);
4092
4093   if (get_opt_global_cse())
4094     set_irg_pinned(current_ir_graph, op_pin_state_floats);
4095   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4096     set_irg_outs_inconsistent(current_ir_graph);
4097
4098   /* FIXME: Maybe we could also test whether optimizing the node can
4099      change the control graph. */
4100   set_irg_doms_inconsistent(current_ir_graph);
4101   return optimize_in_place_2 (n);
4102 }
4103
4104 /*
4105  * Sets the default operation for an ir_ops.
4106  */
4107 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
4108 {
4109   ops = firm_set_default_computed_value(code, ops);
4110   ops = firm_set_default_equivalent_node(code, ops);
4111   ops = firm_set_default_transform_node(code, ops);
4112   ops = firm_set_default_node_cmp_attr(code, ops);
4113   ops = firm_set_default_get_type(code, ops);
4114   ops = firm_set_default_get_type_attr(code, ops);
4115   ops = firm_set_default_get_entity_attr(code, ops);
4116
4117   return ops;
4118 }