d29e52139d98aec4caafcd4495c032772eea7f5b
[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
901   n = equivalent_node_neutral_zero(n);
902   if (n != oldn)
903     return n;
904
905   left  = get_Add_left(n);
906   right = get_Add_right(n);
907
908   if (get_irn_op(left) == op_Sub) {
909     if (get_Sub_right(left) == right) {
910       /* (a - x) + x */
911
912       n = get_Sub_left(left);
913       if (get_irn_mode(oldn) == get_irn_mode(n)) {
914         DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
915         return n;
916       }
917     }
918   }
919   if (get_irn_op(right) == op_Sub) {
920     if (get_Sub_right(right) == left) {
921       /* x + (a - x) */
922
923       n = get_Sub_left(right);
924       if (get_irn_mode(oldn) == get_irn_mode(n)) {
925         DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
926         return n;
927       }
928     }
929   }
930   return n;
931 }
932
933 /**
934  * optimize operations that are not commutative but have neutral 0 on left,
935  * so a op 0 = a.
936  */
937 static ir_node *equivalent_node_left_zero(ir_node *n)
938 {
939   ir_node *oldn = n;
940
941   ir_node *a = get_binop_left(n);
942   ir_node *b = get_binop_right(n);
943
944   if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
945     n = a;
946
947     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
948   }
949
950   return n;
951 }
952
953 #define equivalent_node_Shl   equivalent_node_left_zero
954 #define equivalent_node_Shr   equivalent_node_left_zero
955 #define equivalent_node_Shrs  equivalent_node_left_zero
956 #define equivalent_node_Rot   equivalent_node_left_zero
957
958 /**
959  * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
960  *
961  * The second one looks strange, but this construct
962  * is used heavily in the LCC sources :-).
963  *
964  * Beware: The Mode of a Sub may be different than the mode of its
965  * predecessors, so we could not return a predecessors in all cases.
966  */
967 static ir_node *equivalent_node_Sub(ir_node *n)
968 {
969   ir_node *oldn = n;
970
971   ir_node *a = get_Sub_left(n);
972   ir_node *b = get_Sub_right(n);
973
974   /* Beware: modes might be different */
975   if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
976     if (get_irn_mode(n) == get_irn_mode(a)) {
977       n = a;
978
979       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
980     }
981   }
982   else if (get_irn_op(a) == op_Add) {
983     ir_mode *mode = get_irn_mode(n);
984
985     if (mode_wrap_around(mode)) {
986       ir_node *left  = get_Add_left(a);
987       ir_node *right = get_Add_right(a);
988
989       if (left == b) {
990         if (get_irn_mode(n) == get_irn_mode(right)) {
991           n = right;
992           DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
993         }
994       }
995       else if (right == b) {
996         if (get_irn_mode(n) == get_irn_mode(left)) {
997           n = left;
998           DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
999         }
1000       }
1001     }
1002   }
1003
1004   return n;
1005 }
1006
1007
1008 /**
1009  * Optimize an "idempotent unary op", ie op(op(n)) = n.
1010  *
1011  * @todo
1012  *   -(-a) == a, but might overflow two times.
1013  *   We handle it anyway here but the better way would be a
1014  *   flag. This would be needed for Pascal for instance.
1015  */
1016 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1017 {
1018   ir_node *oldn = n;
1019   ir_node *pred = get_unop_op(n);
1020
1021   /* optimize symmetric unop */
1022   if (get_irn_op(pred) == get_irn_op(n)) {
1023     n = get_unop_op(pred);
1024     DBG_OPT_ALGSIM2(oldn, pred, n);
1025   }
1026   return n;
1027 }
1028
1029 /** Not(Not(x)) == x */
1030 #define equivalent_node_Not    equivalent_node_idempotent_unop
1031
1032 /** --x == x       ??? Is this possible or can --x raise an
1033                        out of bounds exception if min =! max? */
1034 #define equivalent_node_Minus  equivalent_node_idempotent_unop
1035
1036 /**
1037  * Optimize a * 1 = 1 * a = a.
1038  */
1039 static ir_node *equivalent_node_Mul(ir_node *n)
1040 {
1041   ir_node *oldn = n;
1042
1043   ir_node *a = get_Mul_left(n);
1044   ir_node *b = get_Mul_right(n);
1045
1046   /* Mul is commutative and has again an other neutral element. */
1047   if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1048     n = b;
1049     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1050   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1051     n = a;
1052     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1053   }
1054   return n;
1055 }
1056
1057 /**
1058  * Optimize a / 1 = a.
1059  */
1060 static ir_node *equivalent_node_Div(ir_node *n)
1061 {
1062   ir_node *a = get_Div_left(n);
1063   ir_node *b = get_Div_right(n);
1064
1065   /* Div is not commutative. */
1066   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1067     /* Turn Div into a tuple (mem, bad, a) */
1068     ir_node *mem = get_Div_mem(n);
1069     turn_into_tuple(n, pn_Div_max);
1070     set_Tuple_pred(n, pn_Div_M,        mem);
1071     set_Tuple_pred(n, pn_Div_X_except, new_Bad());        /* no exception */
1072     set_Tuple_pred(n, pn_Div_res,      a);
1073   }
1074   return n;
1075 }
1076
1077 /**
1078  * Optimize a / 1 = a.
1079  */
1080 static ir_node *equivalent_node_DivMod(ir_node *n)
1081 {
1082   ir_node *a = get_DivMod_left(n);
1083   ir_node *b = get_DivMod_right(n);
1084
1085   /* Div is not commutative. */
1086   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1087     /* Turn DivMod into a tuple (mem, bad, a, 0) */
1088     ir_node *mem = get_Div_mem(n);
1089     ir_mode *mode = get_irn_mode(b);
1090
1091     turn_into_tuple(n, pn_DivMod_max);
1092     set_Tuple_pred(n, pn_DivMod_M,        mem);
1093     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());        /* no exception */
1094     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1095     set_Tuple_pred(n, pn_DivMod_res_mod,  new_Const(mode, get_mode_null(mode)));
1096   }
1097   return n;
1098 }
1099
1100 /**
1101  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1102  */
1103 static ir_node *equivalent_node_Or(ir_node *n)
1104 {
1105   ir_node *oldn = n;
1106
1107   ir_node *a = get_Or_left(n);
1108   ir_node *b = get_Or_right(n);
1109
1110   if (a == b) {
1111     n = a;    /* Or has it's own neutral element */
1112     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1113   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1114     n = b;
1115     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1116   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1117     n = a;
1118     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1119   }
1120
1121   return n;
1122 }
1123
1124 /**
1125  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
1126  */
1127 static ir_node *equivalent_node_And(ir_node *n)
1128 {
1129   ir_node *oldn = n;
1130
1131   ir_node *a = get_And_left(n);
1132   ir_node *b = get_And_right(n);
1133
1134   if (a == b) {
1135     n = a;    /* And has it's own neutral element */
1136     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1137   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1138     n = b;
1139     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1140   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1141     n = a;
1142     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1143   }
1144   return n;
1145 }
1146
1147 /**
1148  * Try to remove useless Conv's:
1149  */
1150 static ir_node *equivalent_node_Conv(ir_node *n)
1151 {
1152   ir_node *oldn = n;
1153   ir_node *a = get_Conv_op(n);
1154   ir_node *b;
1155
1156   ir_mode *n_mode = get_irn_mode(n);
1157   ir_mode *a_mode = get_irn_mode(a);
1158
1159   if (n_mode == a_mode) { /* No Conv necessary */
1160     n = a;
1161     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1162   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1163     ir_mode *b_mode;
1164
1165     b = get_Conv_op(a);
1166     n_mode = get_irn_mode(n);
1167     b_mode = get_irn_mode(b);
1168
1169     if (n_mode == b_mode) {
1170       if (n_mode == mode_b) {
1171         n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1172         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1173       }
1174       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1175         if (smaller_mode(b_mode, a_mode)){
1176           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1177           DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1178         }
1179       }
1180     }
1181   }
1182   return n;
1183 }
1184
1185 /**
1186  * A Cast may be removed if the type of the previous node
1187  * is already the type of the Cast.
1188  */
1189 static ir_node *equivalent_node_Cast(ir_node *n) {
1190   ir_node *oldn = n;
1191   ir_node *pred = get_Cast_op(n);
1192
1193   if (get_irn_type(pred) == get_Cast_type(n)) {
1194     n = pred;
1195     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1196   }
1197   return n;
1198 }
1199
1200 /**
1201   Several optimizations:
1202    - no Phi in start block.
1203    - remove Id operators that are inputs to Phi
1204    - fold Phi-nodes, iff they have only one predecessor except
1205            themselves.
1206  */
1207 static ir_node *equivalent_node_Phi(ir_node *n)
1208 {
1209   int i, n_preds;
1210
1211   ir_node *oldn = n;
1212   ir_node *block = NULL;     /* to shutup gcc */
1213   ir_node *first_val = NULL; /* to shutup gcc */
1214
1215   if (!get_opt_normalize()) return n;
1216
1217   n_preds = get_Phi_n_preds(n);
1218
1219   block = get_nodes_block(n);
1220   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1221      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1222   if ((is_Block_dead(block)) ||                  /* Control dead */
1223       (block == get_irg_start_block(current_ir_graph)))  /* There should be no Phi nodes */
1224     return new_Bad();                                    /* in the Start Block. */
1225
1226   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1227
1228   /* If the Block has a Bad pred, we also have one. */
1229   for (i = 0;  i < n_preds;  ++i)
1230     if (is_Bad(get_Block_cfgpred(block, i)))
1231       set_Phi_pred(n, i, new_Bad());
1232
1233   /* Find first non-self-referencing input */
1234   for (i = 0; i < n_preds; ++i) {
1235     first_val = get_Phi_pred(n, i);
1236     if (   (first_val != n)                            /* not self pointer */
1237 #if 1
1238         && (! is_Bad(first_val))
1239 #endif
1240            ) {        /* value not dead */
1241       break;          /* then found first value. */
1242     }
1243   }
1244
1245   if (i >= n_preds) {
1246     /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1247     return new_Bad();
1248   }
1249
1250   /* search for rest of inputs, determine if any of these
1251      are non-self-referencing */
1252   while (++i < n_preds) {
1253     ir_node *scnd_val = get_Phi_pred(n, i);
1254     if (   (scnd_val != n)
1255         && (scnd_val != first_val)
1256 #if 1
1257         && (! is_Bad(scnd_val))
1258 #endif
1259            ) {
1260       break;
1261     }
1262   }
1263
1264   if (i >= n_preds) {
1265     /* Fold, if no multiple distinct non-self-referencing inputs */
1266     n = first_val;
1267     DBG_OPT_PHI(oldn, n);
1268   }
1269   return n;
1270 }
1271
1272 /**
1273   Several optimizations:
1274    - no Sync in start block.
1275    - fold Sync-nodes, iff they have only one predecessor except
1276            themselves.
1277  */
1278 static ir_node *equivalent_node_Sync(ir_node *n)
1279 {
1280   int i, n_preds;
1281
1282   ir_node *oldn = n;
1283   ir_node *first_val = NULL; /* to shutup gcc */
1284
1285   if (!get_opt_normalize()) return n;
1286
1287   n_preds = get_Sync_n_preds(n);
1288
1289   /* Find first non-self-referencing input */
1290   for (i = 0; i < n_preds; ++i) {
1291     first_val = get_Sync_pred(n, i);
1292     if ((first_val != n)  /* not self pointer */ &&
1293         (! is_Bad(first_val))
1294        ) {            /* value not dead */
1295       break;          /* then found first value. */
1296     }
1297   }
1298
1299   if (i >= n_preds)
1300     /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1301     return new_Bad();
1302
1303   /* search the rest of inputs, determine if any of these
1304      are non-self-referencing */
1305   while (++i < n_preds) {
1306     ir_node *scnd_val = get_Sync_pred(n, i);
1307     if ((scnd_val != n) &&
1308         (scnd_val != first_val) &&
1309         (! is_Bad(scnd_val))
1310        )
1311       break;
1312   }
1313
1314   if (i >= n_preds) {
1315     /* Fold, if no multiple distinct non-self-referencing inputs */
1316     n = first_val;
1317     DBG_OPT_SYNC(oldn, n);
1318   }
1319   return n;
1320 }
1321
1322 /**
1323  * optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1324  * ProjX(Load) and ProjX(Store)
1325  */
1326 static ir_node *equivalent_node_Proj(ir_node *n)
1327 {
1328   ir_node *oldn = n;
1329
1330   ir_node *a = get_Proj_pred(n);
1331
1332   if ( get_irn_op(a) == op_Tuple) {
1333     /* Remove the Tuple/Proj combination. */
1334     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1335       n = get_Tuple_pred(a, get_Proj_proj(n));
1336       DBG_OPT_TUPLE(oldn, a, n);
1337     } else {
1338       assert(0); /* This should not happen! */
1339       n = new_Bad();
1340     }
1341   }
1342   else if (get_irn_mode(n) == mode_X) {
1343     if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1344       /* Remove dead control flow -- early gigo(). */
1345       n = new_Bad();
1346     }
1347     else if (get_opt_ldst_only_null_ptr_exceptions()) {
1348       ir_op *op = get_irn_op(a);
1349
1350       if (op == op_Load || op == op_Store) {
1351         /* get the load/store address */
1352         ir_node *addr = get_irn_n(a, 1);
1353         if (value_not_null(addr)) {
1354           /* this node may float if it did not depend on a Confirm */
1355           set_irn_pinned(a, op_pin_state_floats);
1356           DBG_OPT_EXC_REM(n);
1357           return new_Bad();
1358         }
1359       }
1360     }
1361   }
1362
1363   return n;
1364 }
1365
1366 /**
1367  * Remove Id's.
1368  */
1369 static ir_node *equivalent_node_Id(ir_node *n)
1370 {
1371   ir_node *oldn = n;
1372
1373   do {
1374     n = get_Id_pred(n);
1375   } while (get_irn_op(n) == op_Id);
1376
1377   DBG_OPT_ID(oldn, n);
1378   return n;
1379 }
1380
1381 /**
1382  * optimize a Mux
1383  */
1384 static ir_node *equivalent_node_Mux(ir_node *n)
1385 {
1386   ir_node *oldn = n, *sel = get_Mux_sel(n);
1387   tarval *ts = value_of(sel);
1388
1389   /* Mux(true, f, t) == t */
1390   if (ts == tarval_b_true) {
1391     n = get_Mux_true(n);
1392     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1393   }
1394   /* Mux(false, f, t) == f */
1395   else if (ts == tarval_b_false) {
1396     n = get_Mux_false(n);
1397     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1398   }
1399   /* Mux(v, x, x) == x */
1400   else if (get_Mux_false(n) == get_Mux_true(n)) {
1401     n = get_Mux_true(n);
1402     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1403   }
1404   else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1405     ir_node *cmp = get_Proj_pred(sel);
1406     long proj_nr = get_Proj_proj(sel);
1407     ir_node *b   = get_Mux_false(n);
1408     ir_node *a   = get_Mux_true(n);
1409
1410     /*
1411      * Note: normalization puts the constant on the right site,
1412      * so we check only one case.
1413      *
1414      * Note further that these optimization work even for floating point
1415      * with NaN's because -NaN == NaN.
1416      * However, if +0 and -0 is handled differently, we cannot use the first one.
1417      */
1418     if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1419       if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1420         /* Mux(a CMP 0, X, a) */
1421         if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1422           /* Mux(a CMP 0, -a, a) */
1423           if (proj_nr == pn_Cmp_Eq) {
1424             /* Mux(a == 0, -a, a)  ==>  -a */
1425             n = b;
1426             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1427           }
1428           else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1429             /* Mux(a != 0, -a, a)  ==> a */
1430             n = a;
1431             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1432           }
1433         }
1434         else if (classify_Const(b) == CNST_NULL) {
1435           /* Mux(a CMP 0, 0, a) */
1436           if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1437             /* Mux(a != 0, 0, a) ==> a */
1438             n = a;
1439             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1440           }
1441           else if (proj_nr == pn_Cmp_Eq) {
1442             /* Mux(a == 0, 0, a) ==> 0 */
1443             n = b;
1444             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1445           }
1446         }
1447       }
1448     }
1449   }
1450   return n;
1451 }
1452
1453 /**
1454  * Returns a equivalent node of a Psi: if a condition is true
1455  * and all previous conditions are false we know its value.
1456  * If all conditions are false its value is the default one.
1457  */
1458 static ir_node *equivalent_node_Psi(ir_node *n) {
1459   if (is_Mux(n))
1460     return equivalent_node_Mux(n);
1461   return n;
1462 }
1463
1464 /**
1465  * Optimize -a CMP -b into b CMP a.
1466  * This works only for for modes where unary Minus
1467  * cannot Overflow.
1468  * Note that two-complement integers can Overflow
1469  * so it will NOT work.
1470  */
1471 static ir_node *equivalent_node_Cmp(ir_node *n)
1472 {
1473   ir_node *left  = get_Cmp_left(n);
1474   ir_node *right = get_Cmp_right(n);
1475
1476   if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1477       !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1478     left  = get_Minus_op(left);
1479     right = get_Minus_op(right);
1480     set_Cmp_left(n, right);
1481     set_Cmp_right(n, left);
1482   }
1483   return n;
1484 }
1485
1486 /**
1487  * Remove Confirm nodes if setting is on.
1488  * Replace Confirms(x, '=', Constlike) by Constlike.
1489  */
1490 static ir_node *equivalent_node_Confirm(ir_node *n)
1491 {
1492   ir_node *pred = get_Confirm_value(n);
1493   pn_Cmp  pnc   = get_Confirm_cmp(n);
1494
1495   if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1496     /*
1497      * rare case: two identical Confirms one after another,
1498      * replace the second one with the first.
1499      */
1500     n = pred;
1501   }
1502   if (pnc == pn_Cmp_Eq) {
1503     ir_node *bound = get_Confirm_bound(n);
1504
1505     /*
1506      * Optimize a rare case:
1507      * Confirm(x, '=', Constlike) ==> Constlike
1508      */
1509     if (is_irn_constlike(bound)) {
1510       DBG_OPT_CONFIRM(n, bound);
1511       return bound;
1512     }
1513   }
1514   return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1515 }
1516
1517 /**
1518  * Optimize CopyB(mem, x, x) into a Nop
1519  */
1520 static ir_node *equivalent_node_CopyB(ir_node *n)
1521 {
1522   ir_node *a = get_CopyB_dst(n);
1523   ir_node *b = get_CopyB_src(n);
1524
1525   if (a == b) {
1526     /* Turn CopyB into a tuple (mem, bad, bad) */
1527     ir_node *mem = get_CopyB_mem(n);
1528     turn_into_tuple(n, pn_CopyB_max);
1529     set_Tuple_pred(n, pn_CopyB_M,        mem);
1530     set_Tuple_pred(n, pn_CopyB_X_except, new_Bad());        /* no exception */
1531     set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1532   }
1533   return n;
1534 }
1535
1536 /**
1537  * Optimize Bounds(idx, idx, upper) into idx.
1538  */
1539 static ir_node *equivalent_node_Bound(ir_node *n)
1540 {
1541   ir_node *idx   = get_Bound_index(n);
1542   ir_node *lower = get_Bound_lower(n);
1543   int ret_tuple = 0;
1544
1545   /* By definition lower < upper, so if idx == lower -->
1546      lower <= idx && idx < upper */
1547   if (idx == lower) {
1548     /* Turn Bound into a tuple (mem, bad, idx) */
1549     ret_tuple = 1;
1550   }
1551   else {
1552     ir_node *pred = skip_Proj(idx);
1553
1554     if (get_irn_op(pred) == op_Bound) {
1555       /*
1556        * idx was Bounds_check previously, it is still valid if
1557        * lower <= pred_lower && pred_upper <= upper.
1558        */
1559       ir_node *upper = get_Bound_upper(n);
1560       if (get_Bound_lower(pred) == lower &&
1561           get_Bound_upper(pred) == upper) {
1562         /*
1563          * One could expect that we simply return the previous
1564          * Bound here. However, this would be wrong, as we could
1565          * add an exception Proj to a new location than.
1566          * So, we must turn in into a tuple
1567          */
1568         ret_tuple = 1;
1569       }
1570     }
1571   }
1572   if (ret_tuple) {
1573     /* Turn Bound into a tuple (mem, bad, idx) */
1574     ir_node *mem = get_Bound_mem(n);
1575     turn_into_tuple(n, pn_Bound_max);
1576     set_Tuple_pred(n, pn_Bound_M,        mem);
1577     set_Tuple_pred(n, pn_Bound_X_except, new_Bad());       /* no exception */
1578     set_Tuple_pred(n, pn_Bound_res,      idx);
1579   }
1580   return n;
1581 }
1582
1583 /**
1584  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1585  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1586  * new nodes.  It is therefore safe to free n if the node returned is not n.
1587  * If a node returns a Tuple we can not just skip it.  If the size of the
1588  * in array fits, we transform n into a tuple (e.g., Div).
1589  */
1590 ir_node *
1591 equivalent_node(ir_node *n)
1592 {
1593   if (n->op->ops.equivalent_node)
1594     return n->op->ops.equivalent_node(n);
1595   return n;
1596 }
1597
1598 /**
1599  * sets the default equivalent node operation for an ir_op_ops.
1600  *
1601  * @param code   the opcode for the default operation
1602  * @param ops    the operations initialized
1603  *
1604  * @return
1605  *    The operations.
1606  */
1607 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1608 {
1609 #define CASE(a)                                 \
1610   case iro_##a:                                 \
1611     ops->equivalent_node  = equivalent_node_##a; \
1612     break
1613
1614   switch (code) {
1615   CASE(Block);
1616   CASE(Jmp);
1617   CASE(Raise);
1618   CASE(Or);
1619   CASE(Add);
1620   CASE(Eor);
1621   CASE(Sub);
1622   CASE(Shl);
1623   CASE(Shr);
1624   CASE(Shrs);
1625   CASE(Rot);
1626   CASE(Not);
1627   CASE(Minus);
1628   CASE(Mul);
1629   CASE(Div);
1630   CASE(DivMod);
1631   CASE(And);
1632   CASE(Conv);
1633   CASE(Cast);
1634   CASE(Phi);
1635   CASE(Sync);
1636   CASE(Proj);
1637   CASE(Id);
1638   CASE(Mux);
1639   CASE(Psi);
1640   CASE(Cmp);
1641   CASE(Confirm);
1642   CASE(CopyB);
1643   CASE(Bound);
1644   default:
1645     /* leave NULL */;
1646   }
1647
1648   return ops;
1649 #undef CASE
1650 }
1651
1652 /**
1653  * Do node specific optimizations of nodes predecessors.
1654  */
1655 static void
1656 optimize_preds(ir_node *n) {
1657   ir_node *a = NULL, *b = NULL;
1658
1659   /* get the operands we will work on for simple cases. */
1660   if (is_binop(n)) {
1661     a = get_binop_left(n);
1662     b = get_binop_right(n);
1663   } else if (is_unop(n)) {
1664     a = get_unop_op(n);
1665   }
1666
1667   switch (get_irn_opcode(n)) {
1668
1669   case iro_Cmp:
1670     /* We don't want Cast as input to Cmp. */
1671     if (get_irn_op(a) == op_Cast) {
1672       a = get_Cast_op(a);
1673       set_Cmp_left(n, a);
1674     }
1675     if (get_irn_op(b) == op_Cast) {
1676       b = get_Cast_op(b);
1677       set_Cmp_right(n, b);
1678     }
1679     break;
1680
1681   default: break;
1682   } /* end switch */
1683 }
1684
1685 /**
1686  * Returns non-zero if a node is a Phi node
1687  * with all predecessors constant.
1688  */
1689 static int is_const_Phi(ir_node *n) {
1690   int i;
1691
1692   if (! is_Phi(n))
1693     return 0;
1694   for (i = get_irn_arity(n) - 1; i >= 0; --i)
1695     if (! is_Const(get_irn_n(n, i)))
1696       return 0;
1697   return 1;
1698 }
1699
1700 /**
1701  * Apply an evaluator on a binop with a constant operators (and one Phi).
1702  *
1703  * @param phi    the Phi node
1704  * @param other  the other operand
1705  * @param eval   an evaluator function
1706  * @param left   if non-zero, other is the left operand, else the right
1707  *
1708  * @return a new Phi node if the conversion was successful, NULL else
1709  */
1710 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1711   tarval   *tv;
1712   void     **res;
1713   ir_node  *pred;
1714   ir_mode  *mode;
1715   ir_graph *irg;
1716   int      i, n = get_irn_arity(phi);
1717
1718   NEW_ARR_A(void *, res, n);
1719   if (left) {
1720     for (i = 0; i < n; ++i) {
1721       pred = get_irn_n(phi, i);
1722       tv   = get_Const_tarval(pred);
1723       tv   = eval(other, tv);
1724
1725       if (tv == tarval_bad) {
1726         /* folding failed, bad */
1727         return NULL;
1728       }
1729       res[i] = tv;
1730     }
1731   }
1732   else {
1733     for (i = 0; i < n; ++i) {
1734       pred = get_irn_n(phi, i);
1735       tv   = get_Const_tarval(pred);
1736       tv   = eval(tv, other);
1737
1738       if (tv == tarval_bad) {
1739         /* folding failed, bad */
1740         return 0;
1741       }
1742       res[i] = tv;
1743     }
1744   }
1745   mode = get_irn_mode(phi);
1746   irg  = current_ir_graph;
1747   for (i = 0; i < n; ++i) {
1748     pred = get_irn_n(phi, i);
1749     res[i] = new_r_Const_type(irg, get_nodes_block(pred),
1750                             mode, res[i], get_Const_type(pred));
1751   }
1752   return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1753 }
1754
1755 /**
1756  * Apply an evaluator on a unop with a constant operator (a Phi).
1757  *
1758  * @param phi    the Phi node
1759  * @param eval   an evaluator function
1760  *
1761  * @return a new Phi node if the conversion was successful, NULL else
1762  */
1763 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1764   tarval   *tv;
1765   void     **res;
1766   ir_node  *pred;
1767   ir_mode  *mode;
1768   ir_graph *irg;
1769   int      i, n = get_irn_arity(phi);
1770
1771   NEW_ARR_A(void *, res, n);
1772   for (i = 0; i < n; ++i) {
1773     pred = get_irn_n(phi, i);
1774     tv   = get_Const_tarval(pred);
1775     tv   = eval(tv);
1776
1777     if (tv == tarval_bad) {
1778       /* folding failed, bad */
1779       return 0;
1780     }
1781     res[i] = tv;
1782   }
1783   mode = get_irn_mode(phi);
1784   irg  = current_ir_graph;
1785   for (i = 0; i < n; ++i) {
1786     pred = get_irn_n(phi, i);
1787     res[i] = new_r_Const_type(irg, get_nodes_block(pred),
1788                             mode, res[i], get_Const_type(pred));
1789   }
1790   return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1791 }
1792
1793 /**
1794  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1795  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1796  * If possible, remove the Conv's.
1797  */
1798 static ir_node *transform_node_AddSub(ir_node *n)
1799 {
1800   ir_mode *mode = get_irn_mode(n);
1801
1802   if (mode_is_reference(mode)) {
1803     ir_node *left  = get_binop_left(n);
1804     ir_node *right = get_binop_right(n);
1805     int ref_bits   = get_mode_size_bits(mode);
1806
1807     if (get_irn_op(left) == op_Conv) {
1808       ir_mode *mode = get_irn_mode(left);
1809       int bits      = get_mode_size_bits(mode);
1810
1811       if (ref_bits == bits &&
1812           mode_is_int(mode) &&
1813           get_mode_arithmetic(mode) == irma_twos_complement) {
1814         ir_node *pre      = get_Conv_op(left);
1815         ir_mode *pre_mode = get_irn_mode(pre);
1816
1817         if (mode_is_int(pre_mode) &&
1818             get_mode_size_bits(pre_mode) == bits &&
1819             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1820           /* ok, this conv just changes to sign, moreover the calculation
1821            * is done with same number of bits as our address mode, so
1822            * we can ignore the conv as address calculation can be viewed
1823            * as either signed or unsigned
1824            */
1825           set_binop_left(n, pre);
1826         }
1827       }
1828     }
1829
1830     if (get_irn_op(right) == op_Conv) {
1831       ir_mode *mode = get_irn_mode(right);
1832       int bits      = get_mode_size_bits(mode);
1833
1834       if (ref_bits == bits &&
1835           mode_is_int(mode) &&
1836           get_mode_arithmetic(mode) == irma_twos_complement) {
1837         ir_node *pre      = get_Conv_op(right);
1838         ir_mode *pre_mode = get_irn_mode(pre);
1839
1840         if (mode_is_int(pre_mode) &&
1841             get_mode_size_bits(pre_mode) == bits &&
1842             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1843           /* ok, this conv just changes to sign, moreover the calculation
1844            * is done with same number of bits as our address mode, so
1845            * we can ignore the conv as address calculation can be viewed
1846            * as either signed or unsigned
1847            */
1848           set_binop_right(n, pre);
1849         }
1850       }
1851     }
1852   }
1853   return n;
1854 }
1855
1856 #define HANDLE_BINOP_PHI(op,a,b,c)                          \
1857   c = NULL;                                                 \
1858   if (is_Const(b) && is_const_Phi(a)) {                     \
1859     /* check for Op(Phi, Const) */                          \
1860     c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0);  \
1861   }                                                         \
1862   else if (is_Const(a) && is_const_Phi(b)) {                \
1863     /* check for Op(Const, Phi) */                          \
1864     c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1);  \
1865   }                                                         \
1866   if (c) {                                                  \
1867     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);             \
1868     return c;                                               \
1869   }
1870
1871 #define HANDLE_UNOP_PHI(op,a,c)                 \
1872   c = NULL;                                     \
1873   if (is_const_Phi(a)) {                        \
1874     /* check for Op(Phi) */                     \
1875     c = apply_unop_on_phi(a, op);               \
1876   }                                             \
1877   if (c) {                                      \
1878     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1879     return c;                                   \
1880   }
1881
1882
1883 /**
1884  * Do the AddSub optimization, then Transform
1885  *   Constant folding on Phi
1886  *   Add(a,a)          -> Mul(a, 2)
1887  *   Add(Mul(a, x), a) -> Mul(a, x+1)
1888  * if the mode is integer or float.
1889  * Transform Add(a,-b) into Sub(a,b).
1890  * Reassociation might fold this further.
1891  */
1892 static ir_node *transform_node_Add(ir_node *n)
1893 {
1894   ir_mode *mode;
1895   ir_node *a, *b, *c, *oldn = n;
1896
1897   n = transform_node_AddSub(n);
1898
1899   a = get_Add_left(n);
1900   b = get_Add_right(n);
1901
1902   HANDLE_BINOP_PHI(tarval_add, a,b,c);
1903
1904   mode = get_irn_mode(n);
1905   if (mode_is_num(mode)) {
1906     if (a == b) {
1907       ir_node *block = get_irn_n(n, -1);
1908
1909       n = new_rd_Mul(
1910             get_irn_dbg_info(n),
1911             current_ir_graph,
1912             block,
1913             a,
1914             new_r_Const_long(current_ir_graph, block, mode, 2),
1915             mode);
1916       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1917     }
1918     else if (get_irn_op(a) == op_Minus) {
1919       n = new_rd_Sub(
1920           get_irn_dbg_info(n),
1921           current_ir_graph,
1922           get_irn_n(n, -1),
1923           b,
1924           get_Minus_op(a),
1925           mode);
1926       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1927     }
1928     else if (get_irn_op(b) == op_Minus) {
1929       n = new_rd_Sub(
1930           get_irn_dbg_info(n),
1931           current_ir_graph,
1932           get_irn_n(n, -1),
1933           a,
1934           get_Minus_op(b),
1935           mode);
1936       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1937     }
1938     /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1939     else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1940       ir_node *ma = get_Mul_left(a);
1941       ir_node *mb = get_Mul_right(a);
1942
1943       if (b == ma) {
1944         ir_node *blk = get_irn_n(n, -1);
1945         n = new_rd_Mul(
1946           get_irn_dbg_info(n), current_ir_graph, blk,
1947           ma,
1948           new_rd_Add(
1949             get_irn_dbg_info(n), current_ir_graph, blk,
1950             mb,
1951             new_r_Const_long(current_ir_graph, blk, mode, 1),
1952             mode),
1953           mode);
1954         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1955       }
1956       else if (b == mb) {
1957         ir_node *blk = get_irn_n(n, -1);
1958         n = new_rd_Mul(
1959           get_irn_dbg_info(n), current_ir_graph, blk,
1960           mb,
1961           new_rd_Add(
1962             get_irn_dbg_info(n), current_ir_graph, blk,
1963             ma,
1964             new_r_Const_long(current_ir_graph, blk, mode, 1),
1965             mode),
1966           mode);
1967         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1968       }
1969     }
1970     /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1971     else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1972       ir_node *ma = get_Mul_left(b);
1973       ir_node *mb = get_Mul_right(b);
1974
1975       if (a == ma) {
1976         ir_node *blk = get_irn_n(n, -1);
1977         n = new_rd_Mul(
1978           get_irn_dbg_info(n), current_ir_graph, blk,
1979           ma,
1980           new_rd_Add(
1981             get_irn_dbg_info(n), current_ir_graph, blk,
1982             mb,
1983             new_r_Const_long(current_ir_graph, blk, mode, 1),
1984             mode),
1985           mode);
1986         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1987       }
1988       else if (a == mb) {
1989         ir_node *blk = get_irn_n(n, -1);
1990         n = new_rd_Mul(
1991           get_irn_dbg_info(n), current_ir_graph, blk,
1992           mb,
1993           new_rd_Add(
1994             get_irn_dbg_info(n), current_ir_graph, blk,
1995             ma,
1996             new_r_Const_long(current_ir_graph, blk, mode, 1),
1997             mode),
1998           mode);
1999         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2000       }
2001     }
2002   }
2003   return n;
2004 }
2005
2006 /**
2007  * Do the AddSub optimization, then Transform
2008  *   Constant folding on Phi
2009  *   Sub(0,a)          -> Minus(a)
2010  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2011  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2012  */
2013 static ir_node *transform_node_Sub(ir_node *n)
2014 {
2015   ir_mode *mode;
2016   ir_node *oldn = n;
2017   ir_node *a, *b, *c;
2018
2019   n = transform_node_AddSub(n);
2020
2021   a = get_Sub_left(n);
2022   b = get_Sub_right(n);
2023
2024   HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2025
2026   mode = get_irn_mode(n);
2027   if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
2028     n = new_rd_Minus(
2029           get_irn_dbg_info(n),
2030           current_ir_graph,
2031           get_irn_n(n, -1),
2032           b,
2033           mode);
2034     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2035   }
2036   /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2037   else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2038     ir_node *ma = get_Mul_left(a);
2039     ir_node *mb = get_Mul_right(a);
2040
2041     if (ma == b) {
2042       ir_node *blk = get_irn_n(n, -1);
2043       n = new_rd_Mul(
2044         get_irn_dbg_info(n),
2045         current_ir_graph, blk,
2046         ma,
2047         new_rd_Sub(
2048           get_irn_dbg_info(n),
2049           current_ir_graph, blk,
2050           mb,
2051           new_r_Const_long(current_ir_graph, blk, mode, 1),
2052           mode),
2053         mode);
2054       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2055     }
2056     else if (mb == b) {
2057       ir_node *blk = get_irn_n(n, -1);
2058       n = new_rd_Mul(
2059         get_irn_dbg_info(n),
2060         current_ir_graph, blk,
2061         mb,
2062         new_rd_Sub(
2063           get_irn_dbg_info(n),
2064           current_ir_graph, blk,
2065           ma,
2066           new_r_Const_long(current_ir_graph, blk, mode, 1),
2067           mode),
2068         mode);
2069       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2070     }
2071   }
2072   else if (get_irn_op(a) == op_Sub) {
2073     ir_node *x   = get_Sub_left(a);
2074     ir_node *y   = get_Sub_right(a);
2075     ir_node *blk = get_irn_n(n, -1);
2076     ir_mode *m_b = get_irn_mode(b);
2077     ir_mode *m_y = get_irn_mode(y);
2078     ir_node *add;
2079
2080     /* Determine the right mode for the Add. */
2081     if (m_b == m_y)
2082       mode = m_b;
2083     else if (mode_is_reference(m_b))
2084       mode = m_b;
2085     else if (mode_is_reference(m_y))
2086       mode = m_y;
2087     else {
2088       /*
2089        * Both modes are different but none is reference,
2090        * happens for instance in SubP(SubP(P, Iu), Is).
2091        * We have two possibilities here: Cast or ignore.
2092        * Currently we ignore this case.
2093        */
2094       return n;
2095     }
2096
2097     add = new_r_Add(current_ir_graph, blk, y, b, mode);
2098
2099     set_Sub_left(n, x);
2100     set_Sub_right(n, add);
2101     DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2102   }
2103
2104   return n;
2105 }
2106
2107 /**
2108  * Transform Mul(a,-1) into -a.
2109  * Do constant evaluation of Phi nodes.
2110  * Do architecture dependent optimizations on Mul nodes
2111  */
2112 static ir_node *transform_node_Mul(ir_node *n) {
2113   ir_node *c, *oldn = n;
2114   ir_node *a = get_Mul_left(n);
2115   ir_node *b = get_Mul_right(n);
2116   ir_mode *mode;
2117
2118   HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2119
2120   mode = get_irn_mode(n);
2121   if (mode_is_signed(mode)) {
2122     ir_node *r = NULL;
2123
2124     if (value_of(a) == get_mode_minus_one(mode))
2125       r = b;
2126     else if (value_of(b) == get_mode_minus_one(mode))
2127       r = a;
2128     if (r) {
2129       n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2130       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2131       return n;
2132     }
2133   }
2134   return arch_dep_replace_mul_with_shifts(n);
2135 }
2136
2137 /**
2138  * Transform a Div Node.
2139  */
2140 static ir_node *transform_node_Div(ir_node *n)
2141 {
2142   tarval *tv = value_of(n);
2143   ir_node *value = n;
2144
2145   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2146
2147   if (tv != tarval_bad) {
2148     value = new_Const(get_tarval_mode(tv), tv);
2149
2150     DBG_OPT_CSTEVAL(n, value);
2151   }
2152   else /* Try architecture dependent optimization */
2153     value = arch_dep_replace_div_by_const(n);
2154
2155   if (value != n) {
2156     /* Turn Div into a tuple (mem, bad, value) */
2157     ir_node *mem = get_Div_mem(n);
2158
2159     turn_into_tuple(n, pn_Div_max);
2160     set_Tuple_pred(n, pn_Div_M, mem);
2161     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2162     set_Tuple_pred(n, pn_Div_res, value);
2163   }
2164   return n;
2165 }
2166
2167 /**
2168  * Transform a Mod node.
2169  */
2170 static ir_node *transform_node_Mod(ir_node *n)
2171 {
2172   tarval *tv = value_of(n);
2173   ir_node *value = n;
2174
2175   /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2176
2177   if (tv != tarval_bad) {
2178     value = new_Const(get_tarval_mode(tv), tv);
2179
2180     DBG_OPT_CSTEVAL(n, value);
2181   }
2182   else /* Try architecture dependent optimization */
2183     value = arch_dep_replace_mod_by_const(n);
2184
2185   if (value != n) {
2186     /* Turn Mod into a tuple (mem, bad, value) */
2187     ir_node *mem = get_Mod_mem(n);
2188
2189     turn_into_tuple(n, pn_Mod_max);
2190     set_Tuple_pred(n, pn_Mod_M, mem);
2191     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2192     set_Tuple_pred(n, pn_Mod_res, value);
2193   }
2194   return n;
2195 }
2196
2197 /**
2198  * Transform a DivMod node.
2199  */
2200 static ir_node *transform_node_DivMod(ir_node *n)
2201 {
2202   int evaluated = 0;
2203
2204   ir_node *a = get_DivMod_left(n);
2205   ir_node *b = get_DivMod_right(n);
2206   ir_mode *mode = get_irn_mode(a);
2207   tarval *ta = value_of(a);
2208   tarval *tb = value_of(b);
2209
2210   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2211     return n;
2212
2213   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2214
2215   if (tb != tarval_bad) {
2216     if (tb == get_mode_one(get_tarval_mode(tb))) {
2217       b = new_Const (mode, get_mode_null(mode));
2218       evaluated = 1;
2219
2220       DBG_OPT_CSTEVAL(n, b);
2221     }
2222     else if (ta != tarval_bad) {
2223       tarval *resa, *resb;
2224       resa = tarval_div (ta, tb);
2225       if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2226                                         Jmp for X result!? */
2227       resb = tarval_mod (ta, tb);
2228       if (resb == tarval_bad) return n; /* Causes exception! */
2229       a = new_Const (mode, resa);
2230       b = new_Const (mode, resb);
2231       evaluated = 1;
2232
2233       DBG_OPT_CSTEVAL(n, a);
2234       DBG_OPT_CSTEVAL(n, b);
2235     }
2236     else { /* Try architecture dependent optimization */
2237       arch_dep_replace_divmod_by_const(&a, &b, n);
2238       evaluated = a != NULL;
2239     }
2240   } else if (ta == get_mode_null(mode)) {
2241     /* 0 / non-Const = 0 */
2242     b = a;
2243     evaluated = 1;
2244   }
2245
2246   if (evaluated) { /* replace by tuple */
2247     ir_node *mem = get_DivMod_mem(n);
2248     turn_into_tuple(n, pn_DivMod_max);
2249     set_Tuple_pred(n, pn_DivMod_M,        mem);
2250     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
2251     set_Tuple_pred(n, pn_DivMod_res_div,  a);
2252     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
2253   }
2254
2255   return n;
2256 }
2257
2258 /**
2259  * Optimize Abs(x) into  x if x is Confirmed >= 0
2260  * Optimize Abs(x) into -x if x is Confirmed <= 0
2261  */
2262 static ir_node *transform_node_Abs(ir_node *n)
2263 {
2264   ir_node        *oldn = n;
2265   ir_node        *a = get_Abs_op(n);
2266   value_classify sign = classify_value_sign(a);
2267
2268   if (sign == VALUE_NEGATIVE) {
2269     ir_mode *mode = get_irn_mode(n);
2270
2271     /*
2272      * We can replace the Abs by -x here.
2273      * We even could add a new Confirm here.
2274      *
2275      * Note that -x would create a new node, so we could
2276      * not run it in the equivalent_node() context.
2277      */
2278     n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2279                      get_irn_n(n, -1), a, mode);
2280
2281     DBG_OPT_CONFIRM(oldn, n);
2282   }
2283   else if (sign == VALUE_POSITIVE) {
2284     /* n is positive, Abs is not needed */
2285     n = a;
2286
2287     DBG_OPT_CONFIRM(oldn, n);
2288   }
2289
2290   return n;
2291 }
2292
2293 /**
2294  * Transform a Cond node.
2295  */
2296 static ir_node *transform_node_Cond(ir_node *n)
2297 {
2298   /* Replace the Cond by a Jmp if it branches on a constant
2299      condition. */
2300   ir_node *jmp;
2301   ir_node *a = get_Cond_selector(n);
2302   tarval *ta = value_of(a);
2303
2304   /* we need block info which is not available in floating irgs */
2305   if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2306      return n;
2307
2308   if ((ta != tarval_bad) &&
2309       (get_irn_mode(a) == mode_b) &&
2310       (get_opt_unreachable_code())) {
2311     /* It's a boolean Cond, branching on a boolean constant.
2312                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2313     jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2314     turn_into_tuple(n, pn_Cond_max);
2315     if (ta == tarval_b_true) {
2316       set_Tuple_pred(n, pn_Cond_false, new_Bad());
2317       set_Tuple_pred(n, pn_Cond_true, jmp);
2318     } else {
2319       set_Tuple_pred(n, pn_Cond_false, jmp);
2320       set_Tuple_pred(n, pn_Cond_true, new_Bad());
2321     }
2322     /* We might generate an endless loop, so keep it alive. */
2323     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2324   }
2325   return n;
2326 }
2327
2328 /**
2329  * Transform an And.
2330  */
2331 static ir_node *transform_node_And(ir_node *n)
2332 {
2333   ir_node *c, *oldn = n;
2334   ir_node *a = get_And_left(n);
2335   ir_node *b = get_And_right(n);
2336
2337   HANDLE_BINOP_PHI(tarval_and, a,b,c);
2338   return n;
2339 }
2340
2341 /**
2342  * Transform an Eor.
2343  */
2344 static ir_node *transform_node_Eor(ir_node *n)
2345 {
2346   ir_node *c, *oldn = n;
2347   ir_node *a = get_Eor_left(n);
2348   ir_node *b = get_Eor_right(n);
2349   ir_mode *mode = get_irn_mode(n);
2350
2351   HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2352
2353   if (a == b) {
2354     /* a ^ a = 0 */
2355     n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2356                      mode, get_mode_null(mode));
2357     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2358   }
2359   else if ((mode == mode_b)
2360       && (get_irn_op(a) == op_Proj)
2361       && (get_irn_mode(a) == mode_b)
2362       && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2363       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2364     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2365     n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2366                    mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2367
2368     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2369   }
2370   else if ((mode == mode_b)
2371         && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2372     /* The Eor is a Not. Replace it by a Not. */
2373     /*   ????!!!Extend to bitfield 1111111. */
2374     n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2375
2376     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2377   }
2378
2379   return n;
2380 }
2381
2382 /**
2383  * Transform a Not.
2384  */
2385 static ir_node *transform_node_Not(ir_node *n)
2386 {
2387   ir_node *c, *oldn = n;
2388   ir_node *a = get_Not_op(n);
2389
2390   HANDLE_UNOP_PHI(tarval_not,a,c);
2391
2392   /* check for a boolean Not */
2393   if (   (get_irn_mode(n) == mode_b)
2394       && (get_irn_op(a) == op_Proj)
2395       && (get_irn_mode(a) == mode_b)
2396       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2397     /* We negate a Cmp. The Cmp has the negated result anyways! */
2398     n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2399                    mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2400     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2401   }
2402   return n;
2403 }
2404
2405 /**
2406  * Transform a Minus.
2407  */
2408 static ir_node *transform_node_Minus(ir_node *n)
2409 {
2410   ir_node *c, *oldn = n;
2411   ir_node *a = get_Minus_op(n);
2412
2413   HANDLE_UNOP_PHI(tarval_neg,a,c);
2414   return n;
2415 }
2416
2417 /**
2418  * Transform a Cast_type(Const) into a new Const_type
2419  */
2420 static ir_node *transform_node_Cast(ir_node *n) {
2421   ir_node *oldn = n;
2422   ir_node *pred = get_Cast_op(n);
2423   ir_type *tp = get_irn_type(n);
2424
2425   if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2426     n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2427               get_Const_tarval(pred), tp);
2428     DBG_OPT_CSTEVAL(oldn, n);
2429   } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2430     n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2431                  get_SymConst_kind(pred), tp);
2432     DBG_OPT_CSTEVAL(oldn, n);
2433   }
2434
2435   return n;
2436 }
2437
2438 /**
2439  * Transform a Proj(Div) with a non-zero value.
2440  * Removes the exceptions and routes the memory to the NoMem node.
2441  */
2442 static ir_node *transform_node_Proj_Div(ir_node *proj)
2443 {
2444   ir_node *n = get_Proj_pred(proj);
2445   ir_node *b = get_Div_right(n);
2446   long proj_nr;
2447
2448   if (value_not_zero(b)) {
2449     /* div(x, y) && y != 0 */
2450     proj_nr = get_Proj_proj(proj);
2451
2452     /* this node may float if it did not depend on a Confirm */
2453     set_irn_pinned(n, op_pin_state_floats);
2454
2455     if (proj_nr == pn_Div_X_except) {
2456       /* we found an exception handler, remove it */
2457       DBG_OPT_EXC_REM(proj);
2458       return new_Bad();
2459     }
2460     else if (proj_nr == pn_Div_M) {
2461       ir_node *res = get_Div_mem(n);
2462       /* the memory Proj can only be removed if we divide by a
2463          real constant, but the node never produce a new memory */
2464       if (value_of(b) != tarval_bad) {
2465         /* this is a Div by a const, we can remove the memory edge */
2466         set_Div_mem(n, get_irg_no_mem(current_ir_graph));
2467       }
2468       return res;
2469     }
2470   }
2471   return proj;
2472 }
2473
2474 /**
2475  * Transform a Proj(Mod) with a non-zero value.
2476  * Removes the exceptions and routes the memory to the NoMem node.
2477  */
2478 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2479 {
2480   ir_node *n = get_Proj_pred(proj);
2481   ir_node *b = get_Mod_right(n);
2482   long proj_nr;
2483
2484   if (value_not_zero(b)) {
2485     /* mod(x, y) && y != 0 */
2486     proj_nr = get_Proj_proj(proj);
2487
2488     /* this node may float if it did not depend on a Confirm */
2489     set_irn_pinned(n, op_pin_state_floats);
2490
2491     if (proj_nr == pn_Mod_X_except) {
2492       /* we found an exception handler, remove it */
2493       DBG_OPT_EXC_REM(proj);
2494       return new_Bad();
2495     } else if (proj_nr == pn_Mod_M) {
2496       ir_node *res = get_Mod_mem(n);
2497       /* the memory Proj can only be removed if we divide by a
2498          real constant, but the node never produce a new memory */
2499       if (value_of(b) != tarval_bad) {
2500         /* this is a Mod by a const, we can remove the memory edge */
2501         set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2502       }
2503       return res;
2504     }
2505     else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2506       /* a % a = 0 if a != 0 */
2507       ir_mode *mode = get_irn_mode(proj);
2508       ir_node *res  = new_Const(mode, get_mode_null(mode));
2509
2510       DBG_OPT_CSTEVAL(n, res);
2511       return res;
2512     }
2513   }
2514   return proj;
2515 }
2516
2517 /**
2518  * Transform a Proj(DivMod) with a non-zero value.
2519  * Removes the exceptions and routes the memory to the NoMem node.
2520  */
2521 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2522 {
2523   ir_node *n = get_Proj_pred(proj);
2524   ir_node *b = get_DivMod_right(n);
2525   long proj_nr;
2526
2527   if (value_not_zero(b)) {
2528     /* DivMod(x, y) && y != 0 */
2529     proj_nr = get_Proj_proj(proj);
2530
2531     /* this node may float if it did not depend on a Confirm */
2532     set_irn_pinned(n, op_pin_state_floats);
2533
2534     if (proj_nr == pn_DivMod_X_except) {
2535       /* we found an exception handler, remove it */
2536       DBG_OPT_EXC_REM(proj);
2537       return new_Bad();
2538     }
2539     else if (proj_nr == pn_DivMod_M) {
2540       ir_node *res = get_DivMod_mem(n);
2541       /* the memory Proj can only be removed if we divide by a
2542          real constant, but the node never produce a new memory */
2543       if (value_of(b) != tarval_bad) {
2544         /* this is a DivMod by a const, we can remove the memory edge */
2545         set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2546       }
2547       return res;
2548     }
2549     else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2550       /* a % a = 0 if a != 0 */
2551       ir_mode *mode = get_irn_mode(proj);
2552       ir_node *res  = new_Const(mode, get_mode_null(mode));
2553
2554       DBG_OPT_CSTEVAL(n, res);
2555       return res;
2556     }
2557   }
2558   return proj;
2559 }
2560
2561 /**
2562  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2563  */
2564 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2565 {
2566   if (get_opt_unreachable_code()) {
2567     ir_node *n = get_Proj_pred(proj);
2568     ir_node *b = get_Cond_selector(n);
2569
2570     if (mode_is_int(get_irn_mode(b))) {
2571       tarval *tb = value_of(b);
2572
2573       if (tb != tarval_bad) {
2574         /* we have a constant switch */
2575         long num = get_Proj_proj(proj);
2576
2577         if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2578           if (get_tarval_long(tb) == num) {
2579             /* Do NOT create a jump here, or we will have 2 control flow ops
2580              * in a block. This case is optimized away in optimize_cf(). */
2581             return proj;
2582           }
2583           else {
2584             /* this case will NEVER be taken, kill it */
2585             return new_Bad();
2586           }
2587         }
2588       }
2589     }
2590   }
2591   return proj;
2592 }
2593
2594 /**
2595  * Normalizes and optimizes Cmp nodes.
2596  */
2597 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2598 {
2599   if (get_opt_reassociation()) {
2600     ir_node *n     = get_Proj_pred(proj);
2601     ir_node *left  = get_Cmp_left(n);
2602     ir_node *right = get_Cmp_right(n);
2603     ir_node *c     = NULL;
2604     tarval *tv     = NULL;
2605     int changed    = 0;
2606     ir_mode *mode  = NULL;
2607     long proj_nr   = get_Proj_proj(proj);
2608
2609     /*
2610      * First step: normalize the compare op
2611      * by placing the constant on the right site
2612      * or moving the lower address node to the left.
2613      * We ignore the case that both are constants
2614      * this case should be optimized away.
2615      */
2616     if (get_irn_op(right) == op_Const)
2617       c = right;
2618     else if (get_irn_op(left) == op_Const) {
2619       c     = left;
2620       left  = right;
2621       right = c;
2622
2623       proj_nr = get_inversed_pnc(proj_nr);
2624       changed |= 1;
2625     }
2626     else if (get_irn_idx(left) > get_irn_idx(right)) {
2627       ir_node *t = left;
2628
2629       left  = right;
2630       right = t;
2631
2632       proj_nr = get_inversed_pnc(proj_nr);
2633       changed |= 1;
2634     }
2635
2636     /*
2637      * Second step: Try to reduce the magnitude
2638      * of a constant. This may help to generate better code
2639      * later and may help to normalize more compares.
2640      * Of course this is only possible for integer values.
2641      */
2642     if (c) {
2643       mode = get_irn_mode(c);
2644       tv = get_Const_tarval(c);
2645
2646       if (tv != tarval_bad) {
2647         /* the following optimization is possible on modes without Overflow
2648          * on Unary Minus or on == and !=:
2649          * -a CMP c  ==>  a swap(CMP) -c
2650          *
2651          * Beware: for two-complement Overflow may occur, so only == and != can
2652          * be optimized, see this:
2653          * -MININT < 0 =/=> MININT > 0 !!!
2654          */
2655         if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2656             (!mode_overflow_on_unary_Minus(mode) ||
2657              (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2658           left = get_Minus_op(left);
2659           tv = tarval_sub(get_mode_null(mode), tv);
2660
2661           proj_nr = get_inversed_pnc(proj_nr);
2662           changed |= 2;
2663         }
2664
2665         /* for integer modes, we have more */
2666         if (mode_is_int(mode)) {
2667           /* Ne includes Unordered which is not possible on integers.
2668            * However, frontends often use this wrong, so fix it here */
2669           if (proj_nr & pn_Cmp_Uo) {
2670             proj_nr &= ~pn_Cmp_Uo;
2671             set_Proj_proj(proj, proj_nr);
2672           }
2673
2674           /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
2675           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2676               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2677             tv = tarval_sub(tv, get_mode_one(mode));
2678
2679             proj_nr ^= pn_Cmp_Eq;
2680             changed |= 2;
2681           }
2682           /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
2683           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2684               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2685             tv = tarval_add(tv, get_mode_one(mode));
2686
2687             proj_nr ^= pn_Cmp_Eq;
2688             changed |= 2;
2689           }
2690
2691           /* the following reassociations work only for == and != */
2692           if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2693
2694             /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
2695             if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2696               right = get_Sub_right(left);
2697               left  = get_Sub_left(left);
2698
2699               tv = value_of(right);
2700               changed = 1;
2701             }
2702
2703             if (tv != tarval_bad) {
2704               ir_op *op = get_irn_op(left);
2705
2706               /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
2707               if (op == op_Sub) {
2708                 ir_node *c1 = get_Sub_right(left);
2709                 tarval *tv2 = value_of(c1);
2710
2711                 if (tv2 != tarval_bad) {
2712                   tv2 = tarval_add(tv, value_of(c1));
2713
2714                   if (tv2 != tarval_bad) {
2715                     left    = get_Sub_left(left);
2716                     tv      = tv2;
2717                     changed |= 2;
2718                   }
2719                 }
2720               }
2721               /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
2722               else if (op == op_Add) {
2723                 ir_node *a_l = get_Add_left(left);
2724                 ir_node *a_r = get_Add_right(left);
2725                 ir_node *a;
2726                 tarval *tv2;
2727
2728                 if (get_irn_op(a_l) == op_Const) {
2729                   a = a_r;
2730                   tv2 = value_of(a_l);
2731                 }
2732                 else {
2733                   a = a_l;
2734                   tv2 = value_of(a_r);
2735                 }
2736
2737                 if (tv2 != tarval_bad) {
2738                   tv2 = tarval_sub(tv, tv2);
2739
2740                   if (tv2 != tarval_bad) {
2741                     left    = a;
2742                     tv      = tv2;
2743                     changed |= 2;
2744                   }
2745                 }
2746               }
2747               /* -a == c ==> a == -c, -a != c ==> a != -c */
2748               else if (op == op_Minus) {
2749                 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2750
2751                 if (tv2 != tarval_bad) {
2752                   left    = get_Minus_op(left);
2753                   tv      = tv2;
2754                   changed |= 2;
2755                 }
2756               }
2757             }
2758           } /* == or != */
2759           /* the following reassociations work only for <= */
2760           else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2761             if (tv != tarval_bad) {
2762               ir_op *op = get_irn_op(left);
2763
2764               /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
2765               if (op == op_Abs) {
2766               }
2767             }
2768           }
2769         } /* mode_is_int */
2770
2771         /*
2772          * optimization for AND:
2773          * Optimize:
2774          *   And(x, C) == C  ==>  And(x, C) != 0
2775          *   And(x, C) != C  ==>  And(X, C) == 0
2776          *
2777          * if C is a single Bit constant.
2778          */
2779         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2780             (get_irn_op(left) == op_And)) {
2781           if (is_single_bit_tarval(tv)) {
2782             /* check for Constant's match. We have check hare the tarvals,
2783                because our const might be changed */
2784             ir_node *la = get_And_left(left);
2785             ir_node *ra = get_And_right(left);
2786             if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2787                 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2788               /* fine: do the transformation */
2789               tv = get_mode_null(get_tarval_mode(tv));
2790               proj_nr ^= pn_Cmp_Leg;
2791               changed |= 2;
2792             }
2793           }
2794         }
2795       } /* tarval != bad */
2796     }
2797
2798     if (changed) {
2799       ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2800
2801       if (changed & 2)      /* need a new Const */
2802         right = new_Const(mode, tv);
2803
2804       /* create a new compare */
2805       n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2806             left, right);
2807
2808       set_Proj_pred(proj, n);
2809       set_Proj_proj(proj, proj_nr);
2810     }
2811   }
2812   return proj;
2813 }
2814
2815 /**
2816  * Does all optimizations on nodes that must be done on it's Proj's
2817  * because of creating new nodes.
2818  */
2819 static ir_node *transform_node_Proj(ir_node *proj)
2820 {
2821   ir_node *n = get_Proj_pred(proj);
2822
2823   switch (get_irn_opcode(n)) {
2824   case iro_Div:
2825     return transform_node_Proj_Div(proj);
2826
2827   case iro_Mod:
2828     return transform_node_Proj_Mod(proj);
2829
2830   case iro_DivMod:
2831     return transform_node_Proj_DivMod(proj);
2832
2833   case iro_Cond:
2834     return transform_node_Proj_Cond(proj);
2835
2836   case iro_Cmp:
2837     return transform_node_Proj_Cmp(proj);
2838
2839   case iro_Tuple:
2840     /* should not happen, but if it does will be optimized away */
2841     return equivalent_node_Proj(proj);
2842
2843   default:
2844     /* do nothing */
2845     return proj;
2846   }
2847 }
2848
2849 /**
2850  * Move Confirms down through Phi nodes.
2851  */
2852 static ir_node *transform_node_Phi(ir_node *phi) {
2853   int i, n;
2854   ir_mode *mode = get_irn_mode(phi);
2855
2856   if (mode_is_reference(mode)) {
2857     n = get_irn_arity(phi);
2858
2859     /* Beware of Phi0 */
2860     if (n > 0) {
2861       ir_node *pred = get_irn_n(phi, 0);
2862       ir_node *bound, *new_Phi, *block, **in;
2863       pn_Cmp  pnc;
2864
2865       if (! is_Confirm(pred))
2866         return phi;
2867
2868       bound = get_Confirm_bound(pred);
2869       pnc   = get_Confirm_cmp(pred);
2870
2871       NEW_ARR_A(ir_node *, in, n);
2872       in[0] = get_Confirm_value(pred);
2873
2874       for (i = 1; i < n; ++i) {
2875         pred = get_irn_n(phi, i);
2876
2877         if (! is_Confirm(pred) ||
2878             get_Confirm_bound(pred) != bound ||
2879             get_Confirm_cmp(pred) != pnc)
2880           return phi;
2881         in[i] = get_Confirm_value(pred);
2882       }
2883       /* move the Confirm nodes "behind" the Phi */
2884       block = get_irn_n(phi, -1);
2885       new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2886       return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2887     }
2888   }
2889   return phi;
2890 }
2891
2892 /**
2893  * returns the operands of a commutative bin-op, if one operand is
2894  * a const, it is returned as the second one.
2895  */
2896 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2897 {
2898   ir_node *op_a = get_binop_left(binop);
2899   ir_node *op_b = get_binop_right(binop);
2900
2901   assert(is_op_commutative(get_irn_op(binop)));
2902
2903   if (get_irn_op(op_a) == op_Const) {
2904     *a = op_b;
2905     *c = op_a;
2906   }
2907   else {
2908     *a = op_a;
2909     *c = op_b;
2910   }
2911 }
2912
2913 /**
2914  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2915  * Such pattern may arise in bitfield stores.
2916  *
2917  * value  c4                  value      c4 & c2
2918  *    AND     c3                    AND           c1 | c3
2919  *        OR     c2      ===>               OR
2920  *           AND    c1
2921  *               OR
2922  */
2923 static ir_node *transform_node_Or_bf_store(ir_node *or)
2924 {
2925   ir_node *and, *c1;
2926   ir_node *or_l, *c2;
2927   ir_node *and_l, *c3;
2928   ir_node *value, *c4;
2929   ir_node *new_and, *new_const, *block;
2930   ir_mode *mode = get_irn_mode(or);
2931
2932   tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2933
2934   get_comm_Binop_Ops(or, &and, &c1);
2935   if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2936     return or;
2937
2938   get_comm_Binop_Ops(and, &or_l, &c2);
2939   if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2940     return or;
2941
2942   get_comm_Binop_Ops(or_l, &and_l, &c3);
2943   if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2944     return or;
2945
2946   get_comm_Binop_Ops(and_l, &value, &c4);
2947   if (get_irn_op(c4) != op_Const)
2948     return or;
2949
2950   /* ok, found the pattern, check for conditions */
2951   assert(mode == get_irn_mode(and));
2952   assert(mode == get_irn_mode(or_l));
2953   assert(mode == get_irn_mode(and_l));
2954
2955   tv1 = get_Const_tarval(c1);
2956   tv2 = get_Const_tarval(c2);
2957   tv3 = get_Const_tarval(c3);
2958   tv4 = get_Const_tarval(c4);
2959
2960   tv = tarval_or(tv4, tv2);
2961   if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2962     /* have at least one 0 at the same bit position */
2963     return or;
2964   }
2965
2966   n_tv4 = tarval_not(tv4);
2967   if (tv3 != tarval_and(tv3, n_tv4)) {
2968     /* bit in the or_mask is outside the and_mask */
2969     return or;
2970   }
2971
2972   n_tv2 = tarval_not(tv2);
2973   if (tv1 != tarval_and(tv1, n_tv2)) {
2974     /* bit in the or_mask is outside the and_mask */
2975     return or;
2976   }
2977
2978   /* ok, all conditions met */
2979   block = get_irn_n(or, -1);
2980
2981   new_and = new_r_And(current_ir_graph, block,
2982       value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2983
2984   new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2985
2986   set_Or_left(or, new_and);
2987   set_Or_right(or, new_const);
2988
2989   /* check for more */
2990   return transform_node_Or_bf_store(or);
2991 }
2992
2993 /**
2994  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2995  */
2996 static ir_node *transform_node_Or_Rot(ir_node *or)
2997 {
2998   ir_mode *mode = get_irn_mode(or);
2999   ir_node *shl, *shr, *block;
3000   ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3001   tarval *tv1, *tv2;
3002
3003   if (! mode_is_int(mode))
3004     return or;
3005
3006   shl = get_binop_left(or);
3007   shr = get_binop_right(or);
3008
3009   if (get_irn_op(shl) == op_Shr) {
3010     if (get_irn_op(shr) != op_Shl)
3011       return or;
3012
3013     irn = shl;
3014     shl = shr;
3015     shr = irn;
3016   }
3017   else if (get_irn_op(shl) != op_Shl)
3018     return or;
3019   else if (get_irn_op(shr) != op_Shr)
3020     return or;
3021
3022   x = get_Shl_left(shl);
3023   if (x != get_Shr_left(shr))
3024     return or;
3025
3026   c1 = get_Shl_right(shl);
3027   c2 = get_Shr_right(shr);
3028   if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3029     tv1 = get_Const_tarval(c1);
3030     if (! tarval_is_long(tv1))
3031       return or;
3032
3033     tv2 = get_Const_tarval(c2);
3034     if (! tarval_is_long(tv2))
3035       return or;
3036
3037     if (get_tarval_long(tv1) + get_tarval_long(tv2)
3038         != get_mode_size_bits(mode))
3039       return or;
3040
3041     /* yet, condition met */
3042     block = get_irn_n(or, -1);
3043
3044     n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3045
3046     DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3047     return n;
3048   }
3049   else if (get_irn_op(c1) == op_Sub) {
3050     v   = c2;
3051     sub = c1;
3052
3053     if (get_Sub_right(sub) != v)
3054       return or;
3055
3056     c1 = get_Sub_left(sub);
3057     if (get_irn_op(c1) != op_Const)
3058       return or;
3059
3060     tv1 = get_Const_tarval(c1);
3061     if (! tarval_is_long(tv1))
3062       return or;
3063
3064     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3065       return or;
3066
3067     /* yet, condition met */
3068     block = get_nodes_block(or);
3069
3070     /* a Rot right is not supported, so use a rot left */
3071     n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
3072
3073     DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3074     return n;
3075   }
3076   else if (get_irn_op(c2) == op_Sub) {
3077     v   = c1;
3078     sub = c2;
3079
3080     c1 = get_Sub_left(sub);
3081     if (get_irn_op(c1) != op_Const)
3082       return or;
3083
3084     tv1 = get_Const_tarval(c1);
3085     if (! tarval_is_long(tv1))
3086       return or;
3087
3088     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3089       return or;
3090
3091     /* yet, condition met */
3092     block = get_irn_n(or, -1);
3093
3094     /* a Rot Left */
3095     n = new_r_Rot(current_ir_graph, block, x, v, mode);
3096
3097     DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3098     return n;
3099   }
3100
3101   return or;
3102 }
3103
3104 /**
3105  * Transform an Or.
3106  */
3107 static ir_node *transform_node_Or(ir_node *n)
3108 {
3109   ir_node *c, *oldn = n;
3110   ir_node *a = get_Or_left(n);
3111   ir_node *b = get_Or_right(n);
3112
3113   HANDLE_BINOP_PHI(tarval_or, a,b,c);
3114
3115   n = transform_node_Or_bf_store(n);
3116   n = transform_node_Or_Rot(n);
3117
3118   return n;
3119 }
3120
3121
3122 /* forward */
3123 static ir_node *transform_node(ir_node *n);
3124
3125 /**
3126  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3127  *
3128  * Should be moved to reassociation?
3129  */
3130 static ir_node *transform_node_shift(ir_node *n)
3131 {
3132   ir_node *left, *right;
3133   tarval *tv1, *tv2, *res;
3134   ir_mode *mode;
3135   int modulo_shf, flag;
3136
3137   left = get_binop_left(n);
3138
3139   /* different operations */
3140   if (get_irn_op(left) != get_irn_op(n))
3141     return n;
3142
3143   right = get_binop_right(n);
3144   tv1 = value_of(right);
3145   if (tv1 == tarval_bad)
3146     return n;
3147
3148   tv2 = value_of(get_binop_right(left));
3149   if (tv2 == tarval_bad)
3150     return n;
3151
3152   res = tarval_add(tv1, tv2);
3153
3154   /* beware: a simple replacement works only, if res < modulo shift */
3155   mode = get_irn_mode(n);
3156
3157   flag = 0;
3158
3159   modulo_shf = get_mode_modulo_shift(mode);
3160   if (modulo_shf > 0) {
3161     tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3162
3163     if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3164       flag = 1;
3165   }
3166   else
3167     flag = 1;
3168
3169   if (flag) {
3170     /* ok, we can replace it */
3171     ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3172
3173     in[0] = get_binop_left(left);
3174     in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3175
3176     irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3177
3178     DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3179
3180     return transform_node(irn);
3181   }
3182   return n;
3183 }
3184
3185 /**
3186  * Transform a Shr.
3187  */
3188 static ir_node *transform_node_Shr(ir_node *n)
3189 {
3190   ir_node *c, *oldn = n;
3191   ir_node *a = get_Shr_left(n);
3192   ir_node *b = get_Shr_right(n);
3193
3194   HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3195   return transform_node_shift(n);
3196 }
3197
3198 /**
3199  * Transform a Shrs.
3200  */
3201 static ir_node *transform_node_Shrs(ir_node *n)
3202 {
3203   ir_node *c, *oldn = n;
3204   ir_node *a = get_Shrs_left(n);
3205   ir_node *b = get_Shrs_right(n);
3206
3207   HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3208   return transform_node_shift(n);
3209 }
3210
3211 /**
3212  * Transform a Shl.
3213  */
3214 static ir_node *transform_node_Shl(ir_node *n)
3215 {
3216   ir_node *c, *oldn = n;
3217   ir_node *a = get_Shl_left(n);
3218   ir_node *b = get_Shl_right(n);
3219
3220   HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3221   return transform_node_shift(n);
3222 }
3223
3224 /**
3225  * Remove dead blocks and nodes in dead blocks
3226  * in keep alive list.  We do not generate a new End node.
3227  */
3228 static ir_node *transform_node_End(ir_node *n) {
3229   int i, n_keepalives = get_End_n_keepalives(n);
3230
3231   for (i = 0; i < n_keepalives; ++i) {
3232     ir_node *ka = get_End_keepalive(n, i);
3233     if (is_Block(ka)) {
3234       if (is_Block_dead(ka)) {
3235         set_End_keepalive(n, i, new_Bad());
3236       }
3237     }
3238     else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3239       set_End_keepalive(n, i, new_Bad());
3240   }
3241   return n;
3242 }
3243
3244 /**
3245  * Optimize a Mux into some simpler cases.
3246  */
3247 static ir_node *transform_node_Mux(ir_node *n)
3248 {
3249   ir_node *oldn = n, *sel = get_Mux_sel(n);
3250   ir_mode *mode = get_irn_mode(n);
3251
3252   if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3253     ir_node *cmp = get_Proj_pred(sel);
3254     long proj_nr = get_Proj_proj(sel);
3255     ir_node *f   =  get_Mux_false(n);
3256     ir_node *t   = get_Mux_true(n);
3257
3258     if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3259       ir_node *block = get_irn_n(n, -1);
3260
3261       /*
3262        * Note: normalization puts the constant on the right site,
3263        * so we check only one case.
3264        *
3265        * Note further that these optimization work even for floating point
3266        * with NaN's because -NaN == NaN.
3267        * However, if +0 and -0 is handled differently, we cannot use the first one.
3268        */
3269       if (get_irn_op(f) == op_Minus &&
3270           get_Minus_op(f)   == t &&
3271           get_Cmp_left(cmp) == t) {
3272
3273         if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3274           /* Mux(a >=/> 0, -a, a)  ==>  Abs(a) */
3275           n = new_rd_Abs(get_irn_dbg_info(n),
3276                 current_ir_graph,
3277                 block,
3278                 t, mode);
3279           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3280           return n;
3281         }
3282         else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3283           /* Mux(a <=/< 0, -a, a)  ==>  Minus(Abs(a)) */
3284           n = new_rd_Abs(get_irn_dbg_info(n),
3285                 current_ir_graph,
3286                 block,
3287                 t, mode);
3288           n = new_rd_Minus(get_irn_dbg_info(n),
3289                 current_ir_graph,
3290                 block,
3291                 n, mode);
3292
3293           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3294           return n;
3295         }
3296       }
3297       else if (get_irn_op(t) == op_Minus &&
3298           get_Minus_op(t)   == f &&
3299           get_Cmp_left(cmp) == f) {
3300
3301         if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3302           /* Mux(a <=/< 0, a, -a)  ==>  Abs(a) */
3303           n = new_rd_Abs(get_irn_dbg_info(n),
3304                 current_ir_graph,
3305                 block,
3306                 f, mode);
3307           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3308           return n;
3309         }
3310         else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3311           /* Mux(a >=/> 0, a, -a)  ==>  Minus(Abs(a)) */
3312           n = new_rd_Abs(get_irn_dbg_info(n),
3313                 current_ir_graph,
3314                 block,
3315                 f, mode);
3316           n = new_rd_Minus(get_irn_dbg_info(n),
3317                 current_ir_graph,
3318                 block,
3319                 n, mode);
3320
3321           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3322           return n;
3323         }
3324       }
3325
3326       if (mode_is_int(mode) && mode_is_signed(mode) &&
3327           get_mode_arithmetic(mode) == irma_twos_complement) {
3328         ir_node *x = get_Cmp_left(cmp);
3329
3330         /* the following optimization works only with signed integer two-complement mode */
3331
3332         if (mode == get_irn_mode(x)) {
3333           /*
3334            * FIXME: this restriction is two rigid, as it would still
3335            * work if mode(x) = Hs and mode == Is, but at least it removes
3336            * all wrong cases.
3337            */
3338           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3339               classify_Const(t) == CNST_ALL_ONE &&
3340               classify_Const(f) == CNST_NULL) {
3341             /*
3342              * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3343              * Conditions:
3344              * T must be signed.
3345              */
3346             n = new_rd_Shrs(get_irn_dbg_info(n),
3347                   current_ir_graph, block, x,
3348                   new_r_Const_long(current_ir_graph, block, mode_Iu,
3349                     get_mode_size_bits(mode) - 1),
3350                   mode);
3351             DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3352             return n;
3353           }
3354           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3355                    classify_Const(t) == CNST_ONE &&
3356                    classify_Const(f) == CNST_NULL) {
3357             /*
3358              * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3359              * Conditions:
3360              * T must be signed.
3361              */
3362             n = new_rd_Shr(get_irn_dbg_info(n),
3363                   current_ir_graph, block,
3364                   new_r_Minus(current_ir_graph, block, x, mode),
3365                   new_r_Const_long(current_ir_graph, block, mode_Iu,
3366                     get_mode_size_bits(mode) - 1),
3367                   mode);
3368             DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3369             return n;
3370           }
3371         }
3372       }
3373     }
3374   }
3375   return arch_transform_node_Mux(n);
3376 }
3377
3378 /**
3379  * Optimize a Psi into some simpler cases.
3380  */
3381 static ir_node *transform_node_Psi(ir_node *n) {
3382   if (is_Mux(n))
3383     return transform_node_Mux(n);
3384
3385   return n;
3386 }
3387
3388 /**
3389  * Tries several [inplace] [optimizing] transformations and returns an
3390  * equivalent node.  The difference to equivalent_node() is that these
3391  * transformations _do_ generate new nodes, and thus the old node must
3392  * not be freed even if the equivalent node isn't the old one.
3393  */
3394 static ir_node *transform_node(ir_node *n)
3395 {
3396   if (n->op->ops.transform_node)
3397     n = n->op->ops.transform_node(n);
3398   return n;
3399 }
3400
3401 /**
3402  * sSets the default transform node operation for an ir_op_ops.
3403  *
3404  * @param code   the opcode for the default operation
3405  * @param ops    the operations initialized
3406  *
3407  * @return
3408  *    The operations.
3409  */
3410 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3411 {
3412 #define CASE(a)                                 \
3413   case iro_##a:                                 \
3414     ops->transform_node  = transform_node_##a;   \
3415     break
3416
3417   switch (code) {
3418   CASE(Add);
3419   CASE(Sub);
3420   CASE(Mul);
3421   CASE(Div);
3422   CASE(Mod);
3423   CASE(DivMod);
3424   CASE(Abs);
3425   CASE(Cond);
3426   CASE(And);
3427   CASE(Or);
3428   CASE(Eor);
3429   CASE(Minus);
3430   CASE(Not);
3431   CASE(Cast);
3432   CASE(Proj);
3433   CASE(Phi);
3434   CASE(Sel);
3435   CASE(Shr);
3436   CASE(Shrs);
3437   CASE(Shl);
3438   CASE(End);
3439   CASE(Mux);
3440   CASE(Psi);
3441   default:
3442     /* leave NULL */;
3443   }
3444
3445   return ops;
3446 #undef CASE
3447 }
3448
3449
3450 /* **************** Common Subexpression Elimination **************** */
3451
3452 /** The size of the hash table used, should estimate the number of nodes
3453     in a graph. */
3454 #define N_IR_NODES 512
3455
3456 /** Compares the attributes of two Const nodes. */
3457 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
3458 {
3459   return (get_Const_tarval(a) != get_Const_tarval(b))
3460       || (get_Const_type(a) != get_Const_type(b));
3461 }
3462
3463 /** Compares the attributes of two Proj nodes. */
3464 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
3465 {
3466   return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3467 }
3468
3469 /** Compares the attributes of two Filter nodes. */
3470 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
3471 {
3472   return get_Filter_proj(a) != get_Filter_proj(b);
3473 }
3474
3475 /** Compares the attributes of two Alloc nodes. */
3476 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
3477 {
3478   return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3479       || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3480 }
3481
3482 /** Compares the attributes of two Free nodes. */
3483 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
3484 {
3485   return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3486       || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3487 }
3488
3489 /** Compares the attributes of two SymConst nodes. */
3490 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
3491 {
3492   return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3493       || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3494       || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3495 }
3496
3497 /** Compares the attributes of two Call nodes. */
3498 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
3499 {
3500   return (get_irn_call_attr(a) != get_irn_call_attr(b));
3501 }
3502
3503 /** Compares the attributes of two Sel nodes. */
3504 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
3505 {
3506   return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
3507       || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
3508       || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
3509       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3510       || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
3511 }
3512
3513 /** Compares the attributes of two Phi nodes. */
3514 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
3515 {
3516   return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3517 }
3518
3519 /** Compares the attributes of two Cast nodes. */
3520 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
3521 {
3522   return get_Cast_type(a) != get_Cast_type(b);
3523 }
3524
3525 /** Compares the attributes of two Load nodes. */
3526 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
3527 {
3528   if (get_Load_volatility(a) == volatility_is_volatile ||
3529       get_Load_volatility(b) == volatility_is_volatile)
3530     /* NEVER do CSE on volatile Loads */
3531     return 1;
3532
3533   return get_Load_mode(a) != get_Load_mode(b);
3534 }
3535
3536 /** Compares the attributes of two Store nodes. */
3537 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
3538 {
3539   /* NEVER do CSE on volatile Stores */
3540   return (get_Store_volatility(a) == volatility_is_volatile ||
3541           get_Store_volatility(b) == volatility_is_volatile);
3542 }
3543
3544 /** Compares the attributes of two Confirm nodes. */
3545 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
3546 {
3547   return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3548 }
3549
3550 /**
3551  * Set the default node attribute compare operation for an ir_op_ops.
3552  *
3553  * @param code   the opcode for the default operation
3554  * @param ops    the operations initialized
3555  *
3556  * @return
3557  *    The operations.
3558  */
3559 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3560 {
3561 #define CASE(a)                              \
3562   case iro_##a:                              \
3563     ops->node_cmp_attr  = node_cmp_attr_##a; \
3564     break
3565
3566   switch (code) {
3567   CASE(Const);
3568   CASE(Proj);
3569   CASE(Filter);
3570   CASE(Alloc);
3571   CASE(Free);
3572   CASE(SymConst);
3573   CASE(Call);
3574   CASE(Sel);
3575   CASE(Phi);
3576   CASE(Cast);
3577   CASE(Load);
3578   CASE(Store);
3579   CASE(Confirm);
3580   default:
3581     /* leave NULL */;
3582   }
3583
3584   return ops;
3585 #undef CASE
3586 }
3587
3588 /*
3589  * Compare function for two nodes in the hash table. Gets two
3590  * nodes as parameters.  Returns 0 if the nodes are a cse.
3591  */
3592 int identities_cmp(const void *elt, const void *key)
3593 {
3594   ir_node *a, *b;
3595   int i, irn_arity_a;
3596
3597   a = (void *)elt;
3598   b = (void *)key;
3599
3600   if (a == b) return 0;
3601
3602   if ((get_irn_op(a) != get_irn_op(b)) ||
3603       (get_irn_mode(a) != get_irn_mode(b))) return 1;
3604
3605   /* compare if a's in and b's in are of equal length */
3606   irn_arity_a = get_irn_intra_arity (a);
3607   if (irn_arity_a != get_irn_intra_arity(b))
3608     return 1;
3609
3610   /* for block-local cse and op_pin_state_pinned nodes: */
3611   if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3612     if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3613       return 1;
3614   }
3615
3616   /* compare a->in[0..ins] with b->in[0..ins] */
3617   for (i = 0; i < irn_arity_a; i++)
3618     if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3619       return 1;
3620
3621   /*
3622    * here, we already now that the nodes are identical except their
3623    * attributes
3624    */
3625   if (a->op->ops.node_cmp_attr)
3626     return a->op->ops.node_cmp_attr(a, b);
3627
3628   return 0;
3629 }
3630
3631 /*
3632  * Calculate a hash value of a node.
3633  */
3634 unsigned ir_node_hash(ir_node *node)
3635 {
3636   unsigned h;
3637   int i, irn_arity;
3638
3639   if (node->op == op_Const) {
3640     /* special value for const, as they only differ in their tarval. */
3641     h = HASH_PTR(node->attr.con.tv);
3642     h = 9*h + HASH_PTR(get_irn_mode(node));
3643   } else if (node->op == op_SymConst) {
3644     /* special value for const, as they only differ in their symbol. */
3645     h = HASH_PTR(node->attr.i.sym.type_p);
3646     h = 9*h + HASH_PTR(get_irn_mode(node));
3647   } else {
3648
3649     /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3650     h = irn_arity = get_irn_intra_arity(node);
3651
3652     /* consider all in nodes... except the block if not a control flow. */
3653     for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
3654       h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3655     }
3656
3657     /* ...mode,... */
3658     h = 9*h + HASH_PTR(get_irn_mode(node));
3659     /* ...and code */
3660     h = 9*h + HASH_PTR(get_irn_op(node));
3661   }
3662
3663   return h;
3664 }
3665
3666 pset *new_identities(void) {
3667   return new_pset(identities_cmp, N_IR_NODES);
3668 }
3669
3670 void del_identities(pset *value_table) {
3671   del_pset(value_table);
3672 }
3673
3674 /**
3675  * Return the canonical node computing the same value as n.
3676  * Looks up the node in a hash table.
3677  *
3678  * For Const nodes this is performed in the constructor, too.  Const
3679  * nodes are extremely time critical because of their frequent use in
3680  * constant string arrays.
3681  */
3682 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3683 {
3684   ir_node *o = NULL;
3685
3686   if (!value_table) return n;
3687
3688   if (get_opt_reassociation()) {
3689     if (is_op_commutative(get_irn_op(n))) {
3690       ir_node *l = get_binop_left(n);
3691       ir_node *r = get_binop_right(n);
3692
3693       /* for commutative operators perform  a OP b == b OP a */
3694       if (get_irn_idx(l) > get_irn_idx(r)) {
3695         set_binop_left(n, r);
3696         set_binop_right(n, l);
3697       }
3698     }
3699   }
3700
3701   o = pset_find(value_table, n, ir_node_hash(n));
3702   if (!o) return n;
3703
3704   DBG_OPT_CSE(n, o);
3705
3706   return o;
3707 }
3708
3709 /**
3710  * During construction we set the op_pin_state_pinned flag in the graph right when the
3711  * optimization is performed.  The flag turning on procedure global cse could
3712  * be changed between two allocations.  This way we are safe.
3713  */
3714 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3715   ir_node *old = n;
3716
3717   n = identify(value_table, n);
3718   if (get_irn_n(old, -1) != get_irn_n(n, -1))
3719     set_irg_pinned(current_ir_graph, op_pin_state_floats);
3720   return n;
3721 }
3722
3723 /*
3724  * Return the canonical node computing the same value as n.
3725  * Looks up the node in a hash table, enters it in the table
3726  * if it isn't there yet.
3727  */
3728 ir_node *identify_remember(pset *value_table, ir_node *n)
3729 {
3730   ir_node *o = NULL;
3731
3732   if (!value_table) return n;
3733
3734   if (get_opt_reassociation()) {
3735     if (is_op_commutative(get_irn_op(n))) {
3736       ir_node *l = get_binop_left(n);
3737       ir_node *r = get_binop_right(n);
3738
3739       /* for commutative operators perform  a OP b == b OP a */
3740       if (l > r) {
3741         set_binop_left(n, r);
3742         set_binop_right(n, l);
3743       }
3744     }
3745   }
3746
3747   /* lookup or insert in hash table with given hash key. */
3748   o = pset_insert (value_table, n, ir_node_hash (n));
3749
3750   if (o != n) {
3751     DBG_OPT_CSE(n, o);
3752   }
3753
3754   return o;
3755 }
3756
3757 /* Add a node to the identities value table. */
3758 void add_identities(pset *value_table, ir_node *node) {
3759   if (get_opt_cse() && is_no_Block(node))
3760     identify_remember(value_table, node);
3761 }
3762
3763 /* Visit each node in the value table of a graph. */
3764 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3765   ir_node *node;
3766   ir_graph *rem = current_ir_graph;
3767
3768   current_ir_graph = irg;
3769   foreach_pset(irg->value_table, node)
3770     visit(node, env);
3771   current_ir_graph = rem;
3772 }
3773
3774 /**
3775  * Garbage in, garbage out. If a node has a dead input, i.e., the
3776  * Bad node is input to the node, return the Bad node.
3777  */
3778 static INLINE ir_node *gigo(ir_node *node)
3779 {
3780   int i, irn_arity;
3781   ir_op *op = get_irn_op(node);
3782
3783   /* remove garbage blocks by looking at control flow that leaves the block
3784      and replacing the control flow by Bad. */
3785   if (get_irn_mode(node) == mode_X) {
3786     ir_node *block = get_nodes_block(skip_Proj(node));
3787
3788     /* Don't optimize nodes in immature blocks. */
3789     if (!get_Block_matured(block)) return node;
3790      /* Don't optimize End, may have Bads. */
3791     if (op == op_End) return node;
3792
3793     if (is_Block(block)) {
3794       irn_arity = get_irn_arity(block);
3795       for (i = 0; i < irn_arity; i++) {
3796         if (!is_Bad(get_irn_n(block, i)))
3797           break;
3798       }
3799       if (i == irn_arity) return new_Bad();
3800     }
3801   }
3802
3803   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3804      blocks predecessors is dead. */
3805   if (op != op_Block && op != op_Phi && op != op_Tuple) {
3806     irn_arity = get_irn_arity(node);
3807
3808     /*
3809      * Beware: we can only read the block of a non-floating node.
3810      */
3811     if (is_irn_pinned_in_irg(node) &&
3812         is_Block_dead(get_nodes_block(node)))
3813       return new_Bad();
3814
3815     for (i = 0; i < irn_arity; i++) {
3816       ir_node *pred = get_irn_n(node, i);
3817
3818       if (is_Bad(pred))
3819         return new_Bad();
3820 #if 0
3821       /* Propagating Unknowns here seems to be a bad idea, because
3822          sometimes we need a node as a input and did not want that
3823          it kills it's user.
3824          However, it might be useful to move this into a later phase
3825          (if you think that optimizing such code is useful). */
3826       if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3827         return new_Unknown(get_irn_mode(node));
3828 #endif
3829     }
3830   }
3831 #if 0
3832   /* With this code we violate the agreement that local_optimize
3833      only leaves Bads in Block, Phi and Tuple nodes. */
3834   /* If Block has only Bads as predecessors it's garbage. */
3835   /* If Phi has only Bads as predecessors it's garbage. */
3836   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
3837     irn_arity = get_irn_arity(node);
3838     for (i = 0; i < irn_arity; i++) {
3839       if (!is_Bad(get_irn_n(node, i))) break;
3840     }
3841     if (i == irn_arity) node = new_Bad();
3842   }
3843 #endif
3844   return node;
3845 }
3846
3847 /**
3848  * These optimizations deallocate nodes from the obstack.
3849  * It can only be called if it is guaranteed that no other nodes
3850  * reference this one, i.e., right after construction of a node.
3851  *
3852  * current_ir_graph must be set to the graph of the node!
3853  */
3854 ir_node *optimize_node(ir_node *n)
3855 {
3856   tarval *tv;
3857   ir_node *oldn = n;
3858   opcode iro = get_irn_opcode(n);
3859
3860   /* Always optimize Phi nodes: part of the construction. */
3861   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3862
3863   /* constant expression evaluation / constant folding */
3864   if (get_opt_constant_folding()) {
3865     /* neither constants nor Tuple values can be evaluated */
3866     if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3867       /* try to evaluate */
3868       tv = computed_value(n);
3869       if (tv != tarval_bad) {
3870         ir_node *nw;
3871         ir_type *old_tp = get_irn_type(n);
3872         int i, arity = get_irn_arity(n);
3873         int node_size;
3874
3875         /*
3876          * Try to recover the type of the new expression.
3877          */
3878         for (i = 0; i < arity && !old_tp; ++i)
3879           old_tp = get_irn_type(get_irn_n(n, i));
3880
3881         /*
3882          * we MUST copy the node here temporary, because it's still needed
3883          * for DBG_OPT_CSTEVAL
3884          */
3885         node_size = offsetof(ir_node, attr) +  n->op->attr_size;
3886         oldn = alloca(node_size);
3887
3888         memcpy(oldn, n, node_size);
3889         CLONE_ARR_A(ir_node *, oldn->in, n->in);
3890
3891         /* ARG, copy the in array, we need it for statistics */
3892         memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3893
3894         /* note the inplace edges module */
3895         edges_node_deleted(n, current_ir_graph);
3896
3897         /* evaluation was successful -- replace the node. */
3898         irg_kill_node(current_ir_graph, n);
3899         nw = new_Const(get_tarval_mode (tv), tv);
3900
3901         if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3902           set_Const_type(nw, old_tp);
3903         DBG_OPT_CSTEVAL(oldn, nw);
3904         return nw;
3905       }
3906     }
3907   }
3908
3909   /* remove unnecessary nodes */
3910   if (get_opt_constant_folding() ||
3911     (iro == iro_Phi)  ||   /* always optimize these nodes. */
3912     (iro == iro_Id)   ||
3913     (iro == iro_Proj) ||
3914     (iro == iro_Block)  )  /* Flags tested local. */
3915     n = equivalent_node (n);
3916
3917   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3918
3919   /* Common Subexpression Elimination.
3920    *
3921    * Checks whether n is already available.
3922    * The block input is used to distinguish different subexpressions. Right
3923    * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3924    * subexpressions within a block.
3925    */
3926   if (get_opt_cse())
3927     n = identify_cons (current_ir_graph->value_table, n);
3928
3929   if (n != oldn) {
3930     edges_node_deleted(oldn, current_ir_graph);
3931
3932     /* We found an existing, better node, so we can deallocate the old node. */
3933     irg_kill_node(current_ir_graph, oldn);
3934     return n;
3935   }
3936
3937   /* Some more constant expression evaluation that does not allow to
3938      free the node. */
3939   iro = get_irn_opcode(n);
3940   if (get_opt_constant_folding() ||
3941     (iro == iro_Cond) ||
3942     (iro == iro_Proj) ||
3943     (iro == iro_Sel))     /* Flags tested local. */
3944     n = transform_node (n);
3945
3946   /* Remove nodes with dead (Bad) input.
3947      Run always for transformation induced Bads. */
3948   n = gigo (n);
3949
3950   /* Now we have a legal, useful node. Enter it in hash table for CSE */
3951   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3952     n = identify_remember (current_ir_graph->value_table, n);
3953   }
3954
3955   return n;
3956 }
3957
3958
3959 /**
3960  * These optimizations never deallocate nodes (in place).  This can cause dead
3961  * nodes lying on the obstack.  Remove these by a dead node elimination,
3962  * i.e., a copying garbage collection.
3963  */
3964 ir_node *optimize_in_place_2(ir_node *n)
3965 {
3966   tarval *tv;
3967   ir_node *oldn = n;
3968   opcode iro = get_irn_opcode(n);
3969
3970   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3971
3972   /* constant expression evaluation / constant folding */
3973   if (get_opt_constant_folding()) {
3974     /* neither constants nor Tuple values can be evaluated */
3975     if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3976       /* try to evaluate */
3977       tv = computed_value(n);
3978       if (tv != tarval_bad) {
3979         /* evaluation was successful -- replace the node. */
3980         ir_type *old_tp = get_irn_type(n);
3981         int i, arity = get_irn_arity(n);
3982
3983         /*
3984          * Try to recover the type of the new expression.
3985          */
3986         for (i = 0; i < arity && !old_tp; ++i)
3987           old_tp = get_irn_type(get_irn_n(n, i));
3988
3989         n = new_Const(get_tarval_mode(tv), tv);
3990
3991         if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3992           set_Const_type(n, old_tp);
3993
3994         DBG_OPT_CSTEVAL(oldn, n);
3995         return n;
3996       }
3997     }
3998   }
3999
4000   /* remove unnecessary nodes */
4001   if (get_opt_constant_folding() ||
4002       (iro == iro_Phi)  ||   /* always optimize these nodes. */
4003       (iro == iro_Id)   ||   /* ... */
4004       (iro == iro_Proj) ||   /* ... */
4005       (iro == iro_Block)  )  /* Flags tested local. */
4006     n = equivalent_node(n);
4007
4008   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
4009
4010   /** common subexpression elimination **/
4011   /* Checks whether n is already available. */
4012   /* The block input is used to distinguish different subexpressions.  Right
4013      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4014      subexpressions within a block. */
4015   if (get_opt_cse()) {
4016     n = identify(current_ir_graph->value_table, n);
4017   }
4018
4019   /* Some more constant expression evaluation. */
4020   iro = get_irn_opcode(n);
4021   if (get_opt_constant_folding() ||
4022       (iro == iro_Cond) ||
4023       (iro == iro_Proj) ||
4024       (iro == iro_Sel))     /* Flags tested local. */
4025     n = transform_node(n);
4026
4027   /* Remove nodes with dead (Bad) input.
4028      Run always for transformation induced Bads.  */
4029   n = gigo(n);
4030
4031   /* Now we can verify the node, as it has no dead inputs any more. */
4032   irn_vrfy(n);
4033
4034   /* Now we have a legal, useful node. Enter it in hash table for cse.
4035      Blocks should be unique anyways.  (Except the successor of start:
4036      is cse with the start block!) */
4037   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4038     n = identify_remember(current_ir_graph->value_table, n);
4039
4040   return n;
4041 }
4042
4043 /**
4044  * Wrapper for external use, set proper status bits after optimization.
4045  */
4046 ir_node *optimize_in_place(ir_node *n)
4047 {
4048   /* Handle graph state */
4049   assert(get_irg_phase_state(current_ir_graph) != phase_building);
4050
4051   if (get_opt_global_cse())
4052     set_irg_pinned(current_ir_graph, op_pin_state_floats);
4053   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4054     set_irg_outs_inconsistent(current_ir_graph);
4055
4056   /* FIXME: Maybe we could also test whether optimizing the node can
4057      change the control graph. */
4058   set_irg_doms_inconsistent(current_ir_graph);
4059   return optimize_in_place_2 (n);
4060 }
4061
4062 /*
4063  * Sets the default operation for an ir_ops.
4064  */
4065 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
4066 {
4067   ops = firm_set_default_computed_value(code, ops);
4068   ops = firm_set_default_equivalent_node(code, ops);
4069   ops = firm_set_default_transform_node(code, ops);
4070   ops = firm_set_default_node_cmp_attr(code, ops);
4071   ops = firm_set_default_get_type(code, ops);
4072   ops = firm_set_default_get_type_attr(code, ops);
4073   ops = firm_set_default_get_entity_attr(code, ops);
4074
4075   return ops;
4076 }