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