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