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