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