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