refactored ir_op_ops
[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  * @fixme -(-a) == a, but might overflow two times.
955  * We handle it anyway here but the better way would be a
956  * flag. This would be needed for Pascal for instance.
957  */
958 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
959 {
960   ir_node *oldn = n;
961   ir_node *pred = get_unop_op(n);
962
963   /* optimize symmetric unop */
964   if (get_irn_op(pred) == get_irn_op(n)) {
965     n = get_unop_op(pred);
966     DBG_OPT_ALGSIM2(oldn, pred, n);
967   }
968   return n;
969 }
970
971 /* Not(Not(x)) == x */
972 #define equivalent_node_Not    equivalent_node_idempotent_unop
973
974 /* --x == x */  /* ??? Is this possible or can --x raise an
975                        out of bounds exception if min =! max? */
976 #define equivalent_node_Minus  equivalent_node_idempotent_unop
977
978 /**
979  * Optimize a * 1 = 1 * a = a.
980  */
981 static ir_node *equivalent_node_Mul(ir_node *n)
982 {
983   ir_node *oldn = n;
984
985   ir_node *a = get_Mul_left(n);
986   ir_node *b = get_Mul_right(n);
987
988   /* Mul is commutative and has again an other neutral element. */
989   if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
990     n = b;
991     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
992   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
993     n = a;
994     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
995   }
996   return n;
997 }
998
999 /**
1000  * Optimize a / 1 = a.
1001  */
1002 static ir_node *equivalent_node_Div(ir_node *n)
1003 {
1004   ir_node *a = get_Div_left(n);
1005   ir_node *b = get_Div_right(n);
1006
1007   /* Div is not commutative. */
1008   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1009     /* Turn Div into a tuple (mem, bad, a) */
1010     ir_node *mem = get_Div_mem(n);
1011     turn_into_tuple(n, 3);
1012     set_Tuple_pred(n, pn_Div_M,        mem);
1013     set_Tuple_pred(n, pn_Div_X_except, new_Bad());        /* no exception */
1014     set_Tuple_pred(n, pn_Div_res,      a);
1015   }
1016   return n;
1017 }
1018
1019 /**
1020  * Optimize a / 1 = a.
1021  */
1022 static ir_node *equivalent_node_DivMod(ir_node *n)
1023 {
1024   ir_node *a = get_DivMod_left(n);
1025   ir_node *b = get_DivMod_right(n);
1026
1027   /* Div is not commutative. */
1028   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1029     /* Turn DivMod into a tuple (mem, bad, a, 0) */
1030     ir_node *mem = get_Div_mem(n);
1031     ir_mode *mode = get_irn_mode(b);
1032
1033     turn_into_tuple(n, 4);
1034     set_Tuple_pred(n, pn_DivMod_M,        mem);
1035     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());        /* no exception */
1036     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1037     set_Tuple_pred(n, pn_DivMod_res_mod,  new_Const(mode, get_mode_null(mode)));
1038   }
1039   return n;
1040 }
1041
1042 /**
1043  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1044  */
1045 static ir_node *equivalent_node_Or(ir_node *n)
1046 {
1047   ir_node *oldn = n;
1048
1049   ir_node *a = get_Or_left(n);
1050   ir_node *b = get_Or_right(n);
1051
1052   if (a == b) {
1053     n = a;    /* Or has it's own neutral element */
1054     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1055   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1056     n = b;
1057     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1058   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1059     n = a;
1060     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1061   }
1062
1063   return n;
1064 }
1065
1066 /**
1067  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
1068  */
1069 static ir_node *equivalent_node_And(ir_node *n)
1070 {
1071   ir_node *oldn = n;
1072
1073   ir_node *a = get_And_left(n);
1074   ir_node *b = get_And_right(n);
1075
1076   if (a == b) {
1077     n = a;    /* And has it's own neutral element */
1078     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1079   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1080     n = b;
1081     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1082   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1083     n = a;
1084     DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1085   }
1086   return n;
1087 }
1088
1089 /**
1090  * Try to remove useless Conv's:
1091  */
1092 static ir_node *equivalent_node_Conv(ir_node *n)
1093 {
1094   ir_node *oldn = n;
1095   ir_node *a = get_Conv_op(n);
1096   ir_node *b;
1097
1098   ir_mode *n_mode = get_irn_mode(n);
1099   ir_mode *a_mode = get_irn_mode(a);
1100
1101   if (n_mode == a_mode) { /* No Conv necessary */
1102     n = a;
1103     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1104   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1105     ir_mode *b_mode;
1106
1107     b = get_Conv_op(a);
1108     n_mode = get_irn_mode(n);
1109     b_mode = get_irn_mode(b);
1110
1111     if (n_mode == b_mode) {
1112       if (n_mode == mode_b) {
1113         n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1114         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1115       }
1116       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1117         if (smaller_mode(b_mode, a_mode)){
1118           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1119           DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1120         }
1121       }
1122     }
1123   }
1124   return n;
1125 }
1126
1127 /**
1128  * A Cast may be removed if the type of the previous node
1129  * is already the type of the Cast.
1130  */
1131 static ir_node *equivalent_node_Cast(ir_node *n) {
1132   ir_node *oldn = n;
1133   ir_node *pred = get_Cast_op(n);
1134
1135   if (get_irn_type(pred) == get_Cast_type(n)) {
1136     n = pred;
1137     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1138   }
1139   return n;
1140 }
1141
1142 /* Several optimizations:
1143    - no Phi in start block.
1144    - remove Id operators that are inputs to Phi
1145    - fold Phi-nodes, iff they have only one predecessor except
1146            themselves.
1147 */
1148 static ir_node *equivalent_node_Phi(ir_node *n)
1149 {
1150   int i, n_preds;
1151
1152   ir_node *oldn = n;
1153   ir_node *block = NULL;     /* to shutup gcc */
1154   ir_node *first_val = NULL; /* to shutup gcc */
1155   ir_node *scnd_val = NULL;  /* to shutup gcc */
1156
1157   if (!get_opt_normalize()) return n;
1158
1159   n_preds = get_Phi_n_preds(n);
1160
1161   block = get_nodes_block(n);
1162   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1163      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1164   if ((is_Block_dead(block)) ||                  /* Control dead */
1165       (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
1166     return new_Bad();                            /* in the Start Block. */
1167
1168   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1169
1170   /* If the Block has a Bad pred, we also have one. */
1171   for (i = 0;  i < n_preds;  ++i)
1172     if (is_Bad(get_Block_cfgpred(block, i)))
1173       set_Phi_pred(n, i, new_Bad());
1174
1175   /* Find first non-self-referencing input */
1176   for (i = 0; i < n_preds; ++i) {
1177     first_val = get_Phi_pred(n, i);
1178     if (   (first_val != n)                            /* not self pointer */
1179 #if 1
1180         && (! is_Bad(first_val))
1181 #endif
1182            ) {        /* value not dead */
1183       break;          /* then found first value. */
1184     }
1185   }
1186
1187   if (i >= n_preds) {
1188     /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1189     return new_Bad();
1190   }
1191
1192   scnd_val = NULL;
1193
1194   /* follow_Id () for rest of inputs, determine if any of these
1195      are non-self-referencing */
1196   while (++i < n_preds) {
1197     scnd_val = get_Phi_pred(n, i);
1198     if (   (scnd_val != n)
1199         && (scnd_val != first_val)
1200 #if 1
1201         && (! is_Bad(scnd_val))
1202 #endif
1203            ) {
1204       break;
1205     }
1206   }
1207
1208   if (i >= n_preds) {
1209     /* Fold, if no multiple distinct non-self-referencing inputs */
1210     n = first_val;
1211     DBG_OPT_PHI(oldn, n);
1212   } else {
1213     /* skip the remaining Ids (done in get_Phi_pred). */
1214     /* superfluous, since we walk all to propagate Block's Bads.
1215        while (++i < n_preds) get_Phi_pred(n, i);     */
1216   }
1217   return n;
1218 }
1219
1220 /**
1221  * optimize Proj(Tuple) and gigo() for ProjX in Bad block
1222  */
1223 static ir_node *equivalent_node_Proj(ir_node *n)
1224 {
1225   ir_node *oldn = n;
1226
1227   ir_node *a = get_Proj_pred(n);
1228
1229   if ( get_irn_op(a) == op_Tuple) {
1230     /* Remove the Tuple/Proj combination. */
1231     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1232       n = get_Tuple_pred(a, get_Proj_proj(n));
1233       DBG_OPT_TUPLE(oldn, a, n);
1234     } else {
1235       assert(0); /* This should not happen! */
1236       n = new_Bad();
1237     }
1238   }
1239   else if (get_irn_mode(n) == mode_X) {
1240     if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1241       /* Remove dead control flow -- early gigo(). */
1242       n = new_Bad();
1243     }
1244   }
1245
1246   return n;
1247 }
1248
1249 /**
1250  * Remove Id's.
1251  */
1252 static ir_node *equivalent_node_Id(ir_node *n)
1253 {
1254   ir_node *oldn = n;
1255
1256   do {
1257     n = get_Id_pred(n);
1258   } while (get_irn_op(n) == op_Id);
1259
1260   DBG_OPT_ID(oldn, n);
1261   return n;
1262 }
1263
1264 /**
1265  * optimize a Mux
1266  */
1267 static ir_node *equivalent_node_Mux(ir_node *n)
1268 {
1269   ir_node *oldn = n, *sel = get_Mux_sel(n);
1270   tarval *ts = value_of(sel);
1271
1272   /* Mux(true, f, t) == t */
1273   if (ts == tarval_b_true) {
1274     n = get_Mux_true(n);
1275     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1276   }
1277   /* Mux(false, f, t) == f */
1278   else if (ts == tarval_b_false) {
1279     n = get_Mux_false(n);
1280     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1281   }
1282   /* Mux(v, x, x) == x */
1283   else if (get_Mux_false(n) == get_Mux_true(n)) {
1284     n = get_Mux_true(n);
1285     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1286   }
1287   else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1288     ir_node *cmp = get_Proj_pred(sel);
1289     long proj_nr = get_Proj_proj(sel);
1290     ir_node *b   = get_Mux_false(n);
1291     ir_node *a   = get_Mux_true(n);
1292
1293     /*
1294      * Note: normalization puts the constant on the right site,
1295      * so we check only one case.
1296      *
1297      * Note further that these optimization work even for floating point
1298      * with NaN's because -NaN == NaN.
1299      * However, if +0 and -0 is handled differently, we cannot use the first one.
1300      */
1301     if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1302       if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1303         /* Mux(a CMP 0, X, a) */
1304         if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1305           /* Mux(a CMP 0, -a, a) */
1306           if (proj_nr == pn_Cmp_Eq) {
1307             /* Mux(a == 0, -a, a)  ==>  -a */
1308             n = b;
1309             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1310           }
1311           else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1312             /* Mux(a != 0, -a, a)  ==> a */
1313             n = a;
1314             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1315           }
1316         }
1317         else if (classify_Const(b) == CNST_NULL) {
1318           /* Mux(a CMP 0, 0, a) */
1319           if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1320             /* Mux(a != 0, 0, a) ==> a */
1321             n = a;
1322             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1323           }
1324           else if (proj_nr == pn_Cmp_Eq) {
1325             /* Mux(a == 0, 0, a) ==> 0 */
1326             n = b;
1327             DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1328           }
1329         }
1330       }
1331     }
1332   }
1333   return n;
1334 }
1335
1336 /**
1337  * Optimize -a CMP -b into b CMP a.
1338  * This works only for for modes where unary Minus
1339  * cannot Overflow.
1340  * Note that two-complement integers can Overflow
1341  * so it will NOT work.
1342  */
1343 static ir_node *equivalent_node_Cmp(ir_node *n)
1344 {
1345   ir_node *left  = get_Cmp_left(n);
1346   ir_node *right = get_Cmp_right(n);
1347
1348   if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1349       !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1350     left  = get_Minus_op(left);
1351     right = get_Minus_op(right);
1352     set_Cmp_left(n, right);
1353     set_Cmp_right(n, left);
1354   }
1355   return n;
1356 }
1357
1358 /**
1359  * Remove Confirm nodes if setting is on.
1360  * Replace Confirms(x, '=', Constlike) by Constlike.
1361  */
1362 static ir_node *equivalent_node_Confirm(ir_node *n)
1363 {
1364   ir_node *pred = get_Confirm_value(n);
1365   pn_Cmp  pnc   = get_Confirm_cmp(n);
1366
1367   if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1368     /*
1369      * rare case: two identical Confirms one after another,
1370      * replace the second one with the first.
1371      */
1372     return pred;
1373   }
1374   if (pnc == pn_Cmp_Eq) {
1375     ir_node *bound = get_Confirm_bound(n);
1376
1377     /*
1378      * Optimize a rare case:
1379      * Confirm(x, '=', Constlike) ==> Constlike
1380      */
1381     if (is_irn_constlike(bound)) {
1382       DBG_OPT_CONFIRM(n, bound);
1383       return bound;
1384     }
1385   }
1386   return get_opt_remove_Confirm() ? get_Confirm_value(n) : n;
1387 }
1388
1389 /**
1390  * Optimize CopyB(mem, x, x) into a Nop
1391  */
1392 static ir_node *equivalent_node_CopyB(ir_node *n)
1393 {
1394   ir_node *a = get_CopyB_dst(n);
1395   ir_node *b = get_CopyB_src(n);
1396
1397   if (a == b) {
1398     /* Turn CopyB into a tuple (mem, bad, bad) */
1399     ir_node *mem = get_CopyB_mem(n);
1400     turn_into_tuple(n, pn_CopyB_max);
1401     set_Tuple_pred(n, pn_CopyB_M,        mem);
1402     set_Tuple_pred(n, pn_CopyB_X_except, new_Bad());        /* no exception */
1403     set_Tuple_pred(n, pn_Call_M_except,  new_Bad());
1404   }
1405   return n;
1406 }
1407
1408 /**
1409  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1410  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1411  * new nodes.  It is therefore safe to free n if the node returned is not n.
1412  * If a node returns a Tuple we can not just skip it.  If the size of the
1413  * in array fits, we transform n into a tuple (e.g., Div).
1414  */
1415 ir_node *
1416 equivalent_node(ir_node *n)
1417 {
1418   if (n->op->ops.equivalent_node)
1419     return n->op->ops.equivalent_node(n);
1420   return n;
1421 }
1422
1423 /**
1424  * sets the default equivalent node operation for an ir_op_ops.
1425  *
1426  * @param code   the opcode for the default operation
1427  * @param ops    the operations initialized
1428  *
1429  * @return
1430  *    The operations.
1431  */
1432 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1433 {
1434 #define CASE(a)                                 \
1435   case iro_##a:                                 \
1436     ops->equivalent_node  = equivalent_node_##a; \
1437     break
1438
1439   switch (code) {
1440   CASE(Block);
1441   CASE(Jmp);
1442   CASE(Raise);
1443   CASE(Or);
1444   CASE(Add);
1445   CASE(Eor);
1446   CASE(Sub);
1447   CASE(Shl);
1448   CASE(Shr);
1449   CASE(Shrs);
1450   CASE(Rot);
1451   CASE(Not);
1452   CASE(Minus);
1453   CASE(Mul);
1454   CASE(Div);
1455   CASE(DivMod);
1456   CASE(And);
1457   CASE(Conv);
1458   CASE(Cast);
1459   CASE(Phi);
1460   CASE(Proj);
1461   CASE(Id);
1462   CASE(Mux);
1463   CASE(Cmp);
1464   CASE(Confirm);
1465   CASE(CopyB);
1466   default:
1467     /* leave NULL */;
1468   }
1469
1470   return ops;
1471 #undef CASE
1472 }
1473
1474 /**
1475  * Do node specific optimizations of nodes predecessors.
1476  */
1477 static void
1478 optimize_preds(ir_node *n) {
1479   ir_node *a = NULL, *b = NULL;
1480
1481   /* get the operands we will work on for simple cases. */
1482   if (is_binop(n)) {
1483     a = get_binop_left(n);
1484     b = get_binop_right(n);
1485   } else if (is_unop(n)) {
1486     a = get_unop_op(n);
1487   }
1488
1489   switch (get_irn_opcode(n)) {
1490
1491   case iro_Cmp:
1492     /* We don't want Cast as input to Cmp. */
1493     if (get_irn_op(a) == op_Cast) {
1494       a = get_Cast_op(a);
1495       set_Cmp_left(n, a);
1496     }
1497     if (get_irn_op(b) == op_Cast) {
1498       b = get_Cast_op(b);
1499       set_Cmp_right(n, b);
1500     }
1501     break;
1502
1503   default: break;
1504   } /* end switch */
1505 }
1506
1507 /**
1508  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1509  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1510  * If possible, remove the Conv's.
1511  */
1512 static ir_node *transform_node_AddSub(ir_node *n)
1513 {
1514   ir_mode *mode = get_irn_mode(n);
1515
1516   if (mode_is_reference(mode)) {
1517     ir_node *left  = get_binop_left(n);
1518     ir_node *right = get_binop_right(n);
1519     int ref_bits   = get_mode_size_bits(mode);
1520
1521     if (get_irn_op(left) == op_Conv) {
1522       ir_mode *mode = get_irn_mode(left);
1523       int bits      = get_mode_size_bits(mode);
1524
1525       if (ref_bits == bits &&
1526           mode_is_int(mode) &&
1527           get_mode_arithmetic(mode) == irma_twos_complement) {
1528         ir_node *pre      = get_Conv_op(left);
1529         ir_mode *pre_mode = get_irn_mode(pre);
1530
1531         if (mode_is_int(pre_mode) &&
1532             get_mode_size_bits(pre_mode) == bits &&
1533             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1534           /* ok, this conv just changes to sign, moreover the calculation
1535            * is done with same number of bits as our address mode, so
1536            * we can ignore the conv as address calculation can be viewed
1537            * as either signed or unsigned
1538            */
1539           set_binop_left(n, pre);
1540         }
1541       }
1542     }
1543
1544     if (get_irn_op(right) == op_Conv) {
1545       ir_mode *mode = get_irn_mode(right);
1546       int bits      = get_mode_size_bits(mode);
1547
1548       if (ref_bits == bits &&
1549           mode_is_int(mode) &&
1550           get_mode_arithmetic(mode) == irma_twos_complement) {
1551         ir_node *pre      = get_Conv_op(right);
1552         ir_mode *pre_mode = get_irn_mode(pre);
1553
1554         if (mode_is_int(pre_mode) &&
1555             get_mode_size_bits(pre_mode) == bits &&
1556             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1557           /* ok, this conv just changes to sign, moreover the calculation
1558            * is done with same number of bits as our address mode, so
1559            * we can ignore the conv as address calculation can be viewed
1560            * as either signed or unsigned
1561            */
1562           set_binop_right(n, pre);
1563         }
1564       }
1565     }
1566   }
1567   return n;
1568 }
1569
1570 /**
1571  * Do the AddSub optimization, then Transform Add(a,a) into Mul(a, 2)
1572  * if the mode is integer or float.
1573  * Transform Add(a,-b) into Sub(a,b).
1574  * Reassociation might fold this further.
1575  */
1576 static ir_node *transform_node_Add(ir_node *n)
1577 {
1578   ir_mode *mode;
1579   ir_node *oldn = n;
1580
1581   n = transform_node_AddSub(n);
1582
1583   mode = get_irn_mode(n);
1584   if (mode_is_num(mode)) {
1585     ir_node *a = get_Add_left(n);
1586
1587     if (a == get_Add_right(n)) {
1588       ir_node *block = get_irn_n(n, -1);
1589
1590       n = new_rd_Mul(
1591             get_irn_dbg_info(n),
1592             current_ir_graph,
1593             block,
1594             a,
1595             new_r_Const_long(current_ir_graph, block, mode, 2),
1596             mode);
1597       DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1598     }
1599     else {
1600       ir_node *b = get_Add_right(n);
1601
1602       if (get_irn_op(a) == op_Minus) {
1603         n = new_rd_Sub(
1604             get_irn_dbg_info(n),
1605             current_ir_graph,
1606             get_irn_n(n, -1),
1607             b,
1608             get_Minus_op(a),
1609             mode);
1610         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1611       }
1612       else if (get_irn_op(b) == op_Minus) {
1613         n = new_rd_Sub(
1614             get_irn_dbg_info(n),
1615             current_ir_graph,
1616             get_irn_n(n, -1),
1617             a,
1618             get_Minus_op(b),
1619             mode);
1620         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1621       }
1622     }
1623   }
1624   return n;
1625 }
1626
1627 /**
1628  * Do the AddSub optimization, then Transform Sub(0,a) into Minus(a).
1629  */
1630 static ir_node *transform_node_Sub(ir_node *n)
1631 {
1632   ir_mode *mode;
1633   ir_node *oldn = n;
1634
1635   n = transform_node_AddSub(n);
1636
1637   mode = get_irn_mode(n);
1638   if (mode_is_num(mode) && (classify_Const(get_Sub_left(n)) == CNST_NULL)) {
1639     n = new_rd_Minus(
1640           get_irn_dbg_info(n),
1641           current_ir_graph,
1642           get_irn_n(n, -1),
1643           get_Sub_right(n),
1644           mode);
1645     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
1646   }
1647
1648   return n;
1649 }
1650
1651 /**
1652  * Transform Mul(a,-1) into -a.
1653  * Do architecture dependent optimizations on Mul nodes
1654  */
1655 static ir_node *transform_node_Mul(ir_node *n) {
1656   ir_node *oldn = n;
1657   ir_mode *mode = get_irn_mode(n);
1658
1659   if (mode_is_signed(mode)) {
1660     ir_node *r = NULL;
1661     ir_node *a = get_Mul_left(n);
1662     ir_node *b = get_Mul_right(n);
1663
1664     if (value_of(a) == get_mode_minus_one(mode))
1665       r = b;
1666     else if (value_of(b) == get_mode_minus_one(mode))
1667       r = a;
1668     if (r) {
1669       n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
1670       DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
1671       return n;
1672     }
1673   }
1674   return arch_dep_replace_mul_with_shifts(n);
1675 }
1676
1677 /**
1678  * transform a Div Node
1679  */
1680 static ir_node *transform_node_Div(ir_node *n)
1681 {
1682   tarval *tv = value_of(n);
1683   ir_node *value = n;
1684
1685   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1686
1687   if (tv != tarval_bad) {
1688     value = new_Const(get_tarval_mode(tv), tv);
1689
1690     DBG_OPT_CSTEVAL(n, value);
1691   }
1692   else /* Try architecture dependent optimization */
1693     value = arch_dep_replace_div_by_const(n);
1694
1695   if (value != n) {
1696     /* Turn Div into a tuple (mem, bad, value) */
1697     ir_node *mem = get_Div_mem(n);
1698
1699     turn_into_tuple(n, pn_Div_max);
1700     set_Tuple_pred(n, pn_Div_M, mem);
1701     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1702     set_Tuple_pred(n, pn_Div_res, value);
1703   }
1704   return n;
1705 }
1706
1707 /**
1708  * transform a Mod node
1709  */
1710 static ir_node *transform_node_Mod(ir_node *n)
1711 {
1712   tarval *tv = value_of(n);
1713   ir_node *value = n;
1714
1715   /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1716
1717   if (tv != tarval_bad) {
1718     value = new_Const(get_tarval_mode(tv), tv);
1719
1720     DBG_OPT_CSTEVAL(n, value);
1721   }
1722   else /* Try architecture dependent optimization */
1723     value = arch_dep_replace_mod_by_const(n);
1724
1725   if (value != n) {
1726     /* Turn Mod into a tuple (mem, bad, value) */
1727     ir_node *mem = get_Mod_mem(n);
1728
1729     turn_into_tuple(n, 3);
1730     set_Tuple_pred(n, pn_Mod_M, mem);
1731     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1732     set_Tuple_pred(n, pn_Mod_res, value);
1733   }
1734   return n;
1735 }
1736
1737 /**
1738  * transform a DivMod node
1739  */
1740 static ir_node *transform_node_DivMod(ir_node *n)
1741 {
1742   int evaluated = 0;
1743
1744   ir_node *a = get_DivMod_left(n);
1745   ir_node *b = get_DivMod_right(n);
1746   ir_mode *mode = get_irn_mode(a);
1747   tarval *ta = value_of(a);
1748   tarval *tb = value_of(b);
1749
1750   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1751     return n;
1752
1753   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1754
1755   if (tb != tarval_bad) {
1756     if (tb == get_mode_one(get_tarval_mode(tb))) {
1757       b = new_Const (mode, get_mode_null(mode));
1758       evaluated = 1;
1759
1760       DBG_OPT_CSTEVAL(n, b);
1761     }
1762     else if (ta != tarval_bad) {
1763       tarval *resa, *resb;
1764       resa = tarval_div (ta, tb);
1765       if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1766                                         Jmp for X result!? */
1767       resb = tarval_mod (ta, tb);
1768       if (resb == tarval_bad) return n; /* Causes exception! */
1769       a = new_Const (mode, resa);
1770       b = new_Const (mode, resb);
1771       evaluated = 1;
1772
1773       DBG_OPT_CSTEVAL(n, a);
1774       DBG_OPT_CSTEVAL(n, b);
1775     }
1776     else { /* Try architecture dependent optimization */
1777       arch_dep_replace_divmod_by_const(&a, &b, n);
1778       evaluated = a != NULL;
1779     }
1780   } else if (ta == get_mode_null(mode)) {
1781     /* 0 / non-Const = 0 */
1782     b = a;
1783     evaluated = 1;
1784   }
1785
1786   if (evaluated) { /* replace by tuple */
1787     ir_node *mem = get_DivMod_mem(n);
1788     turn_into_tuple(n, 4);
1789     set_Tuple_pred(n, pn_DivMod_M,        mem);
1790     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
1791     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1792     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
1793   }
1794
1795   return n;
1796 }
1797
1798 /**
1799  * Optimize Abs(x) into  x if x is Confirmed >= 0
1800  * Optimize Abs(x) into -x if x is Confirmed <= 0
1801  */
1802 static ir_node *transform_node_Abs(ir_node *n)
1803 {
1804   ir_node        *oldn = n;
1805   ir_node        *a = get_Abs_op(n);
1806   value_classify sign = classify_value_sign(a);
1807
1808   if (sign == VALUE_NEGATIVE) {
1809     ir_mode *mode = get_irn_mode(n);
1810
1811     /*
1812      * We can replace the Abs by -x here.
1813      * We even could add a new Confirm here.
1814      *
1815      * Note that -x would create a new node, so we could
1816      * not run it in the equivalent_node() context.
1817      */
1818     n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
1819                      get_irn_n(n, -1), a, mode);
1820
1821     DBG_OPT_CONFIRM(oldn, n);
1822   }
1823   else if (sign == VALUE_POSITIVE) {
1824     /* n is positive, Abs is not needed */
1825     n = a;
1826
1827     DBG_OPT_CONFIRM(oldn, n);
1828   }
1829
1830   return n;
1831 }
1832
1833 /**
1834  * transform a Cond node
1835  */
1836 static ir_node *transform_node_Cond(ir_node *n)
1837 {
1838   /* Replace the Cond by a Jmp if it branches on a constant
1839      condition. */
1840   ir_node *jmp;
1841   ir_node *a = get_Cond_selector(n);
1842   tarval *ta = value_of(a);
1843
1844   /* we need block info which is not available in floating irgs */
1845   if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
1846      return n;
1847
1848   if ((ta != tarval_bad) &&
1849       (get_irn_mode(a) == mode_b) &&
1850       (get_opt_unreachable_code())) {
1851     /* It's a boolean Cond, branching on a boolean constant.
1852                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1853     jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
1854     turn_into_tuple(n, 2);
1855     if (ta == tarval_b_true) {
1856       set_Tuple_pred(n, pn_Cond_false, new_Bad());
1857       set_Tuple_pred(n, pn_Cond_true, jmp);
1858     } else {
1859       set_Tuple_pred(n, pn_Cond_false, jmp);
1860       set_Tuple_pred(n, pn_Cond_true, new_Bad());
1861     }
1862     /* We might generate an endless loop, so keep it alive. */
1863     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1864   }
1865   return n;
1866 }
1867
1868 /**
1869  * Transform an Eor.
1870  */
1871 static ir_node *transform_node_Eor(ir_node *n)
1872 {
1873   ir_node *oldn = n;
1874   ir_node *a = get_Eor_left(n);
1875   ir_node *b = get_Eor_right(n);
1876   ir_mode *mode = get_irn_mode(n);
1877
1878   if (a == b) {
1879     /* a ^ a = 0 */
1880     n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
1881                      mode, get_mode_null(mode));
1882     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
1883   }
1884   else if ((mode == mode_b)
1885       && (get_irn_op(a) == op_Proj)
1886       && (get_irn_mode(a) == mode_b)
1887       && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
1888       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1889     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1890     n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
1891                    mode_b, get_negated_pnc(get_Proj_proj(a), mode));
1892
1893     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
1894   }
1895   else if ((mode == mode_b)
1896         && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
1897     /* The Eor is a Not. Replace it by a Not. */
1898     /*   ????!!!Extend to bitfield 1111111. */
1899     n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
1900
1901     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
1902   }
1903
1904   return n;
1905 }
1906
1907 /**
1908  * Transform a boolean Not.
1909  */
1910 static ir_node *transform_node_Not(ir_node *n)
1911 {
1912   ir_node *oldn = n;
1913   ir_node *a = get_Not_op(n);
1914
1915   if (   (get_irn_mode(n) == mode_b)
1916       && (get_irn_op(a) == op_Proj)
1917       && (get_irn_mode(a) == mode_b)
1918       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1919     /* We negate a Cmp. The Cmp has the negated result anyways! */
1920     n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
1921                    mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
1922     DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
1923   }
1924
1925   return n;
1926 }
1927
1928 /**
1929  * Transform a Cast_type(Const) into a new Const_type
1930  */
1931 static ir_node *transform_node_Cast(ir_node *n) {
1932   ir_node *oldn = n;
1933   ir_node *pred = get_Cast_op(n);
1934   type *tp = get_irn_type(n);
1935
1936   if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
1937     n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
1938               get_Const_tarval(pred), tp);
1939     DBG_OPT_CSTEVAL(oldn, n);
1940   } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
1941     n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
1942                  get_SymConst_kind(pred), tp);
1943     DBG_OPT_CSTEVAL(oldn, n);
1944   }
1945
1946   return n;
1947 }
1948
1949 /**
1950  * Transform a Proj(Div) with a non-zero value.
1951  * Removes the exceptions and routes the memory to the NoMem node.
1952  */
1953 static ir_node *transform_node_Proj_Div(ir_node *proj)
1954 {
1955   ir_node *n = get_Proj_pred(proj);
1956   ir_node *b = get_Div_right(n);
1957   long proj_nr;
1958
1959   if (value_not_zero(b)) {
1960     /* div(x, y) && y != 0 */
1961     proj_nr = get_Proj_proj(proj);
1962
1963     /* this node may float */
1964     set_irn_pinned(n, op_pin_state_floats);
1965
1966     if (proj_nr == pn_Div_X_except) {
1967       /* we found an exception handler, remove it */
1968       return new_Bad();
1969     } else if (proj_nr == pn_Div_M) {
1970       /* the memory Proj can be removed */
1971       ir_node *res = get_Div_mem(n);
1972       set_Div_mem(n, get_irg_no_mem(current_ir_graph));
1973
1974       return res;
1975     }
1976   }
1977   return proj;
1978 }
1979
1980 /**
1981  * Transform a Proj(Mod) with a non-zero value.
1982  * Removes the exceptions and routes the memory to the NoMem node.
1983  */
1984 static ir_node *transform_node_Proj_Mod(ir_node *proj)
1985 {
1986   ir_node *n = get_Proj_pred(proj);
1987   ir_node *b = get_Mod_right(n);
1988   long proj_nr;
1989
1990   if (value_not_zero(b)) {
1991     /* mod(x, y) && y != 0 */
1992     proj_nr = get_Proj_proj(proj);
1993
1994     /* this node may float */
1995     set_irn_pinned(n, op_pin_state_floats);
1996
1997     if (proj_nr == pn_Mod_X_except) {
1998       /* we found an exception handler, remove it */
1999       return new_Bad();
2000     } else if (proj_nr == pn_Mod_M) {
2001       /* the memory Proj can be removed */
2002       ir_node *res = get_Mod_mem(n);
2003       set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2004
2005       return res;
2006     }
2007     else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2008       /* a % a = 0 if a != 0 */
2009       ir_mode *mode = get_irn_mode(proj);
2010       ir_node *res  = new_Const(mode, get_mode_null(mode));
2011
2012       DBG_OPT_CSTEVAL(n, res);
2013       return res;
2014     }
2015   }
2016   return proj;
2017 }
2018
2019 /**
2020  * Transform a Proj(DivMod) with a non-zero value.
2021  * Removes the exceptions and routes the memory to the NoMem node.
2022  */
2023 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2024 {
2025   ir_node *n = get_Proj_pred(proj);
2026   ir_node *b = get_DivMod_right(n);
2027   long proj_nr;
2028
2029   if (value_not_zero(b)) {
2030     /* DivMod(x, y) && y != 0 */
2031     proj_nr = get_Proj_proj(proj);
2032
2033     /* this node may float */
2034     set_irn_pinned(n, op_pin_state_floats);
2035
2036     if (proj_nr == pn_DivMod_X_except) {
2037       /* we found an exception handler, remove it */
2038       return new_Bad();
2039     }
2040     else if (proj_nr == pn_DivMod_M) {
2041       /* the memory Proj can be removed */
2042       ir_node *res = get_DivMod_mem(n);
2043       set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2044
2045       return res;
2046     }
2047     else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2048       /* a % a = 0 if a != 0 */
2049       ir_mode *mode = get_irn_mode(proj);
2050       ir_node *res  = new_Const(mode, get_mode_null(mode));
2051
2052       DBG_OPT_CSTEVAL(n, res);
2053       return res;
2054     }
2055   }
2056   return proj;
2057 }
2058
2059 /**
2060  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2061  */
2062 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2063 {
2064   if (get_opt_unreachable_code()) {
2065     ir_node *n = get_Proj_pred(proj);
2066     ir_node *b = get_Cond_selector(n);
2067
2068     if (mode_is_int(get_irn_mode(b))) {
2069       tarval *tb = value_of(b);
2070
2071       if (tb != tarval_bad) {
2072         /* we have a constant switch */
2073         long num = get_Proj_proj(proj);
2074
2075         if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2076           if (get_tarval_long(tb) == num) {
2077             /* Do NOT create a jump here, or we will have 2 control flow ops
2078              * in a block. This case is optimized away in optimize_cf(). */
2079             return proj;
2080           }
2081           else {
2082             /* this case will NEVER be taken, kill it */
2083             return new_Bad();
2084           }
2085         }
2086       }
2087     }
2088   }
2089   return proj;
2090 }
2091
2092 /**
2093  * Normalizes and optimizes Cmp nodes.
2094  */
2095 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2096 {
2097   if (get_opt_reassociation()) {
2098     ir_node *n     = get_Proj_pred(proj);
2099     ir_node *left  = get_Cmp_left(n);
2100     ir_node *right = get_Cmp_right(n);
2101     ir_node *c     = NULL;
2102     tarval *tv     = NULL;
2103     int changed    = 0;
2104     ir_mode *mode  = NULL;
2105     long proj_nr   = get_Proj_proj(proj);
2106
2107     /*
2108      * First step: normalize the compare op
2109      * by placing the constant on the right site
2110      * or moving the lower address node to the left.
2111      * We ignore the case that both are constants
2112      * this case should be optimized away.
2113      */
2114     if (get_irn_op(right) == op_Const)
2115       c = right;
2116     else if (get_irn_op(left) == op_Const) {
2117       c     = left;
2118       left  = right;
2119       right = c;
2120
2121       proj_nr = get_inversed_pnc(proj_nr);
2122       changed |= 1;
2123     }
2124     else if (left > right) {
2125       ir_node *t = left;
2126
2127       left  = right;
2128       right = t;
2129
2130       proj_nr = get_inversed_pnc(proj_nr);
2131       changed |= 1;
2132     }
2133
2134     /*
2135      * Second step: Try to reduce the magnitude
2136      * of a constant. This may help to generate better code
2137      * later and may help to normalize more compares.
2138      * Of course this is only possible for integer values.
2139      */
2140     if (c) {
2141       mode = get_irn_mode(c);
2142       tv = get_Const_tarval(c);
2143
2144       if (tv != tarval_bad) {
2145         /* the following optimization is possible on modes without Overflow
2146          * on Unary Minus or on == and !=:
2147          * -a CMP c  ==>  a swap(CMP) -c
2148          *
2149          * Beware: for two-complement Overflow may occur, so only == and != can
2150          * be optimized, see this:
2151          * -MININT < 0 =/=> MININT > 0 !!!
2152          */
2153         if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2154             (!mode_overflow_on_unary_Minus(mode) ||
2155              (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2156           left = get_Minus_op(left);
2157           tv = tarval_sub(get_mode_null(mode), tv);
2158
2159           proj_nr = get_inversed_pnc(proj_nr);
2160           changed |= 2;
2161         }
2162
2163         /* for integer modes, we have more */
2164         if (mode_is_int(mode)) {
2165           /* Ne includes Unordered which is not possible on integers.
2166            * However, frontends often use this wrong, so fix it here */
2167           if (proj_nr & pn_Cmp_Uo) {
2168             proj_nr &= ~pn_Cmp_Uo;
2169             set_Proj_proj(proj, proj_nr);
2170           }
2171
2172           /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
2173           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2174               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2175             tv = tarval_sub(tv, get_mode_one(mode));
2176
2177             proj_nr ^= pn_Cmp_Eq;
2178             changed |= 2;
2179           }
2180           /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
2181           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2182               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2183             tv = tarval_add(tv, get_mode_one(mode));
2184
2185             proj_nr ^= pn_Cmp_Eq;
2186             changed |= 2;
2187           }
2188
2189           /* the following reassociations work only for == and != */
2190           if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2191
2192             /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
2193             if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2194               right = get_Sub_right(left);
2195               left  = get_Sub_left(left);
2196
2197               tv = value_of(right);
2198               changed = 1;
2199             }
2200
2201             if (tv != tarval_bad) {
2202               ir_op *op = get_irn_op(left);
2203
2204               /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
2205               if (op == op_Sub) {
2206                 ir_node *c1 = get_Sub_right(left);
2207                 tarval *tv2 = value_of(c1);
2208
2209                 if (tv2 != tarval_bad) {
2210                   tv2 = tarval_add(tv, value_of(c1));
2211
2212                   if (tv2 != tarval_bad) {
2213                     left    = get_Sub_left(left);
2214                     tv      = tv2;
2215                     changed |= 2;
2216                   }
2217                 }
2218               }
2219               /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
2220               else if (op == op_Add) {
2221                 ir_node *a_l = get_Add_left(left);
2222                 ir_node *a_r = get_Add_right(left);
2223                 ir_node *a;
2224                 tarval *tv2;
2225
2226                 if (get_irn_op(a_l) == op_Const) {
2227                   a = a_r;
2228                   tv2 = value_of(a_l);
2229                 }
2230                 else {
2231                   a = a_l;
2232                   tv2 = value_of(a_r);
2233                 }
2234
2235                 if (tv2 != tarval_bad) {
2236                   tv2 = tarval_sub(tv, tv2);
2237
2238                   if (tv2 != tarval_bad) {
2239                     left    = a;
2240                     tv      = tv2;
2241                     changed |= 2;
2242                   }
2243                 }
2244               }
2245               /* -a == c ==> a == -c, -a != c ==> a != -c */
2246               else if (op == op_Minus) {
2247                 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2248
2249                 if (tv2 != tarval_bad) {
2250                   left    = get_Minus_op(left);
2251                   tv      = tv2;
2252                   changed |= 2;
2253                 }
2254               }
2255             }
2256           } /* == or != */
2257           /* the following reassociations work only for <= */
2258           else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2259             if (tv != tarval_bad) {
2260               ir_op *op = get_irn_op(left);
2261
2262               /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
2263               if (op == op_Abs) {
2264               }
2265             }
2266           }
2267         } /* mode_is_int */
2268       }
2269     }
2270
2271     if (changed) {
2272       ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2273
2274       if (changed & 2)      /* need a new Const */
2275         right = new_Const(mode, tv);
2276
2277       /* create a new compare */
2278       n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2279             left, right);
2280
2281       set_Proj_pred(proj, n);
2282       set_Proj_proj(proj, proj_nr);
2283     }
2284   }
2285   return proj;
2286 }
2287
2288 /**
2289  * Does all optimizations on nodes that must be done on it's Proj's
2290  * because of creating new nodes.
2291  */
2292 static ir_node *transform_node_Proj(ir_node *proj)
2293 {
2294   ir_node *n = get_Proj_pred(proj);
2295
2296   switch (get_irn_opcode(n)) {
2297   case iro_Div:
2298     return transform_node_Proj_Div(proj);
2299
2300   case iro_Mod:
2301     return transform_node_Proj_Mod(proj);
2302
2303   case iro_DivMod:
2304     return transform_node_Proj_DivMod(proj);
2305
2306   case iro_Cond:
2307     return transform_node_Proj_Cond(proj);
2308
2309   case iro_Cmp:
2310     return transform_node_Proj_Cmp(proj);
2311
2312   case iro_Tuple:
2313     /* should not happen, but if it does will be optimized away */
2314     return equivalent_node_Proj(proj);
2315
2316   default:
2317     /* do nothing */
2318     return proj;
2319   }
2320 }
2321
2322 /**
2323  * returns the operands of a commutative bin-op, if one operand is
2324  * a const, it is returned as the second one.
2325  */
2326 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2327 {
2328   ir_node *op_a = get_binop_left(binop);
2329   ir_node *op_b = get_binop_right(binop);
2330
2331   assert(is_op_commutative(get_irn_op(binop)));
2332
2333   if (get_irn_op(op_a) == op_Const) {
2334     *a = op_b;
2335     *c = op_a;
2336   }
2337   else {
2338     *a = op_a;
2339     *c = op_b;
2340   }
2341 }
2342
2343 /**
2344  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2345  * Such pattern may arise in bitfield stores.
2346  *
2347  * value  c4                  value      c4 & c2
2348  *    AND     c3                    AND           c1 | c3
2349  *        OR     c2      ===>               OR
2350  *           AND    c1
2351  *               OR
2352  */
2353 static ir_node *transform_node_Or_bf_store(ir_node *or)
2354 {
2355   ir_node *and, *c1;
2356   ir_node *or_l, *c2;
2357   ir_node *and_l, *c3;
2358   ir_node *value, *c4;
2359   ir_node *new_and, *new_const, *block;
2360   ir_mode *mode = get_irn_mode(or);
2361
2362   tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2363
2364   get_comm_Binop_Ops(or, &and, &c1);
2365   if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2366     return or;
2367
2368   get_comm_Binop_Ops(and, &or_l, &c2);
2369   if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2370     return or;
2371
2372   get_comm_Binop_Ops(or_l, &and_l, &c3);
2373   if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2374     return or;
2375
2376   get_comm_Binop_Ops(and_l, &value, &c4);
2377   if (get_irn_op(c4) != op_Const)
2378     return or;
2379
2380   /* ok, found the pattern, check for conditions */
2381   assert(mode == get_irn_mode(and));
2382   assert(mode == get_irn_mode(or_l));
2383   assert(mode == get_irn_mode(and_l));
2384
2385   tv1 = get_Const_tarval(c1);
2386   tv2 = get_Const_tarval(c2);
2387   tv3 = get_Const_tarval(c3);
2388   tv4 = get_Const_tarval(c4);
2389
2390   tv = tarval_or(tv4, tv2);
2391   if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2392     /* have at least one 0 at the same bit position */
2393     return or;
2394   }
2395
2396   n_tv4 = tarval_not(tv4);
2397   if (tv3 != tarval_and(tv3, n_tv4)) {
2398     /* bit in the or_mask is outside the and_mask */
2399     return or;
2400   }
2401
2402   n_tv2 = tarval_not(tv2);
2403   if (tv1 != tarval_and(tv1, n_tv2)) {
2404     /* bit in the or_mask is outside the and_mask */
2405     return or;
2406   }
2407
2408   /* ok, all conditions met */
2409   block = get_irn_n(or, -1);
2410
2411   new_and = new_r_And(current_ir_graph, block,
2412       value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2413
2414   new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2415
2416   set_Or_left(or, new_and);
2417   set_Or_right(or, new_const);
2418
2419   /* check for more */
2420   return transform_node_Or_bf_store(or);
2421 }
2422
2423 /**
2424  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2425  */
2426 static ir_node *transform_node_Or_Rot(ir_node *or)
2427 {
2428   ir_mode *mode = get_irn_mode(or);
2429   ir_node *shl, *shr, *block;
2430   ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2431   tarval *tv1, *tv2;
2432
2433   if (! mode_is_int(mode))
2434     return or;
2435
2436   shl = get_binop_left(or);
2437   shr = get_binop_right(or);
2438
2439   if (get_irn_op(shl) == op_Shr) {
2440     if (get_irn_op(shr) != op_Shl)
2441       return or;
2442
2443     irn = shl;
2444     shl = shr;
2445     shr = irn;
2446   }
2447   else if (get_irn_op(shl) != op_Shl)
2448     return or;
2449   else if (get_irn_op(shr) != op_Shr)
2450     return or;
2451
2452   x = get_Shl_left(shl);
2453   if (x != get_Shr_left(shr))
2454     return or;
2455
2456   c1 = get_Shl_right(shl);
2457   c2 = get_Shr_right(shr);
2458   if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2459     tv1 = get_Const_tarval(c1);
2460     if (! tarval_is_long(tv1))
2461       return or;
2462
2463     tv2 = get_Const_tarval(c2);
2464     if (! tarval_is_long(tv2))
2465       return or;
2466
2467     if (get_tarval_long(tv1) + get_tarval_long(tv2)
2468         != get_mode_size_bits(mode))
2469       return or;
2470
2471     /* yet, condition met */
2472     block = get_irn_n(or, -1);
2473
2474     n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2475
2476     DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2477     return n;
2478   }
2479   else if (get_irn_op(c1) == op_Sub) {
2480     v   = c2;
2481     sub = c1;
2482
2483     if (get_Sub_right(sub) != v)
2484       return or;
2485
2486     c1 = get_Sub_left(sub);
2487     if (get_irn_op(c1) != op_Const)
2488       return or;
2489
2490     tv1 = get_Const_tarval(c1);
2491     if (! tarval_is_long(tv1))
2492       return or;
2493
2494     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2495       return or;
2496
2497     /* yet, condition met */
2498     block = get_nodes_block(or);
2499
2500     /* a Rot right is not supported, so use a rot left */
2501     n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
2502
2503     DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2504     return n;
2505   }
2506   else if (get_irn_op(c2) == op_Sub) {
2507     v   = c1;
2508     sub = c2;
2509
2510     c1 = get_Sub_left(sub);
2511     if (get_irn_op(c1) != op_Const)
2512       return or;
2513
2514     tv1 = get_Const_tarval(c1);
2515     if (! tarval_is_long(tv1))
2516       return or;
2517
2518     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2519       return or;
2520
2521     /* yet, condition met */
2522     block = get_irn_n(or, -1);
2523
2524     /* a Rot Left */
2525     n = new_r_Rot(current_ir_graph, block, x, v, mode);
2526
2527     DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
2528     return n;
2529   }
2530
2531   return or;
2532 }
2533
2534 /**
2535  * Optimize an Or
2536  */
2537 static ir_node *transform_node_Or(ir_node *or)
2538 {
2539   or = transform_node_Or_bf_store(or);
2540   or = transform_node_Or_Rot(or);
2541
2542   return or;
2543 }
2544
2545 /* forward */
2546 static ir_node *transform_node(ir_node *n);
2547
2548 /**
2549  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
2550  *
2551  * Should be moved to reassociation?
2552  */
2553 static ir_node *transform_node_shift(ir_node *n)
2554 {
2555   ir_node *left, *right;
2556   tarval *tv1, *tv2, *res;
2557   ir_mode *mode;
2558   int modulo_shf, flag;
2559
2560   left = get_binop_left(n);
2561
2562   /* different operations */
2563   if (get_irn_op(left) != get_irn_op(n))
2564     return n;
2565
2566   right = get_binop_right(n);
2567   tv1 = value_of(right);
2568   if (tv1 == tarval_bad)
2569     return n;
2570
2571   tv2 = value_of(get_binop_right(left));
2572   if (tv2 == tarval_bad)
2573     return n;
2574
2575   res = tarval_add(tv1, tv2);
2576
2577   /* beware: a simple replacement works only, if res < modulo shift */
2578   mode = get_irn_mode(n);
2579
2580   flag = 0;
2581
2582   modulo_shf = get_mode_modulo_shift(mode);
2583   if (modulo_shf > 0) {
2584     tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2585
2586     if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2587       flag = 1;
2588   }
2589   else
2590     flag = 1;
2591
2592   if (flag) {
2593     /* ok, we can replace it */
2594     ir_node *in[2], *irn, *block = get_irn_n(n, -1);
2595
2596     in[0] = get_binop_left(left);
2597     in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2598
2599     irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2600
2601     DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
2602
2603     return transform_node(irn);
2604   }
2605   return n;
2606 }
2607
2608 #define transform_node_Shr  transform_node_shift
2609 #define transform_node_Shrs transform_node_shift
2610 #define transform_node_Shl  transform_node_shift
2611
2612 /**
2613  * Remove dead blocks and nodes in dead blocks
2614  * in keep alive list.  We do not generate a new End node.
2615  */
2616 static ir_node *transform_node_End(ir_node *n) {
2617   int i, n_keepalives = get_End_n_keepalives(n);
2618
2619   for (i = 0; i < n_keepalives; ++i) {
2620     ir_node *ka = get_End_keepalive(n, i);
2621     if (is_Block(ka)) {
2622       if (is_Block_dead(ka)) {
2623         set_End_keepalive(n, i, new_Bad());
2624       }
2625     }
2626     else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
2627       set_End_keepalive(n, i, new_Bad());
2628   }
2629   return n;
2630 }
2631
2632 /**
2633  * Optimize a Mux into some simpler cases.
2634  */
2635 static ir_node *transform_node_Mux(ir_node *n)
2636 {
2637   ir_node *oldn = n, *sel = get_Mux_sel(n);
2638   ir_mode *mode = get_irn_mode(n);
2639
2640   if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2641     ir_node *cmp = get_Proj_pred(sel);
2642     long proj_nr = get_Proj_proj(sel);
2643     ir_node *f   =  get_Mux_false(n);
2644     ir_node *t   = get_Mux_true(n);
2645
2646     if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2647       ir_node *block = get_irn_n(n, -1);
2648
2649       /*
2650        * Note: normalization puts the constant on the right site,
2651        * so we check only one case.
2652        *
2653        * Note further that these optimization work even for floating point
2654        * with NaN's because -NaN == NaN.
2655        * However, if +0 and -0 is handled differently, we cannot use the first one.
2656        */
2657       if (get_irn_op(f) == op_Minus &&
2658           get_Minus_op(f)   == t &&
2659           get_Cmp_left(cmp) == t) {
2660
2661         if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2662           /* Mux(a >=/> 0, -a, a)  ==>  Abs(a) */
2663           n = new_rd_Abs(get_irn_dbg_info(n),
2664                 current_ir_graph,
2665                 block,
2666                 t, mode);
2667           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2668           return n;
2669         }
2670         else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2671           /* Mux(a <=/< 0, -a, a)  ==>  Minus(Abs(a)) */
2672           n = new_rd_Abs(get_irn_dbg_info(n),
2673                 current_ir_graph,
2674                 block,
2675                 t, mode);
2676           n = new_rd_Minus(get_irn_dbg_info(n),
2677                 current_ir_graph,
2678                 block,
2679                 n, mode);
2680
2681           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2682           return n;
2683         }
2684       }
2685       else if (get_irn_op(t) == op_Minus &&
2686           get_Minus_op(t)   == f &&
2687           get_Cmp_left(cmp) == f) {
2688
2689         if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2690           /* Mux(a <=/< 0, a, -a)  ==>  Abs(a) */
2691           n = new_rd_Abs(get_irn_dbg_info(n),
2692                 current_ir_graph,
2693                 block,
2694                 f, mode);
2695           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2696           return n;
2697         }
2698         else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2699           /* Mux(a >=/> 0, a, -a)  ==>  Minus(Abs(a)) */
2700           n = new_rd_Abs(get_irn_dbg_info(n),
2701                 current_ir_graph,
2702                 block,
2703                 f, mode);
2704           n = new_rd_Minus(get_irn_dbg_info(n),
2705                 current_ir_graph,
2706                 block,
2707                 n, mode);
2708
2709           DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
2710           return n;
2711         }
2712       }
2713
2714       if (mode_is_int(mode) && mode_is_signed(mode) &&
2715           get_mode_arithmetic(mode) == irma_twos_complement) {
2716         ir_node *x = get_Cmp_left(cmp);
2717
2718         /* the following optimization works only with signed integer two-complement mode */
2719
2720         if (mode == get_irn_mode(x)) {
2721           /*
2722            * FIXME: this restriction is two rigid, as it would still
2723            * work if mode(x) = Hs and mode == Is, but at least it removes
2724            * all wrong cases.
2725            */
2726           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
2727               classify_Const(t) == CNST_ALL_ONE &&
2728               classify_Const(f) == CNST_NULL) {
2729             /*
2730              * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
2731              * Conditions:
2732              * T must be signed.
2733              */
2734             n = new_rd_Shrs(get_irn_dbg_info(n),
2735                   current_ir_graph, block, x,
2736                   new_r_Const_long(current_ir_graph, block, mode_Iu,
2737                     get_mode_size_bits(mode) - 1),
2738                   mode);
2739             DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
2740             return n;
2741           }
2742           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
2743                    classify_Const(t) == CNST_ONE &&
2744                    classify_Const(f) == CNST_NULL) {
2745             /*
2746              * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
2747              * Conditions:
2748              * T must be signed.
2749              */
2750             n = new_rd_Shr(get_irn_dbg_info(n),
2751                   current_ir_graph, block,
2752                   new_r_Minus(current_ir_graph, block, x, mode),
2753                   new_r_Const_long(current_ir_graph, block, mode_Iu,
2754                     get_mode_size_bits(mode) - 1),
2755                   mode);
2756             DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
2757             return n;
2758           }
2759         }
2760       }
2761     }
2762   }
2763   return arch_transform_node_Mux(n);
2764 }
2765
2766 /**
2767  * Tries several [inplace] [optimizing] transformations and returns an
2768  * equivalent node.  The difference to equivalent_node() is that these
2769  * transformations _do_ generate new nodes, and thus the old node must
2770  * not be freed even if the equivalent node isn't the old one.
2771  */
2772 static ir_node *transform_node(ir_node *n)
2773 {
2774   if (n->op->ops.transform_node)
2775     n = n->op->ops.transform_node(n);
2776   return n;
2777 }
2778
2779 /**
2780  * sSets the default transform node operation for an ir_op_ops.
2781  *
2782  * @param code   the opcode for the default operation
2783  * @param ops    the operations initialized
2784  *
2785  * @return
2786  *    The operations.
2787  */
2788 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
2789 {
2790 #define CASE(a)                                 \
2791   case iro_##a:                                 \
2792     ops->transform_node  = transform_node_##a;   \
2793     break
2794
2795   switch (code) {
2796   CASE(Add);
2797   CASE(Sub);
2798   CASE(Mul);
2799   CASE(Div);
2800   CASE(Mod);
2801   CASE(DivMod);
2802   CASE(Abs);
2803   CASE(Cond);
2804   CASE(Eor);
2805   CASE(Not);
2806   CASE(Cast);
2807   CASE(Proj);
2808   CASE(Sel);
2809   CASE(Or);
2810   CASE(Shr);
2811   CASE(Shrs);
2812   CASE(Shl);
2813   CASE(End);
2814   CASE(Mux);
2815   default:
2816     /* leave NULL */;
2817   }
2818
2819   return ops;
2820 #undef CASE
2821 }
2822
2823
2824 /* **************** Common Subexpression Elimination **************** */
2825
2826 /** The size of the hash table used, should estimate the number of nodes
2827     in a graph. */
2828 #define N_IR_NODES 512
2829
2830 /** Compares the attributes of two Const nodes. */
2831 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
2832 {
2833   return (get_Const_tarval(a) != get_Const_tarval(b))
2834       || (get_Const_type(a) != get_Const_type(b));
2835 }
2836
2837 /** Compares the attributes of two Proj nodes. */
2838 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
2839 {
2840   return get_irn_proj_attr (a) != get_irn_proj_attr (b);
2841 }
2842
2843 /** Compares the attributes of two Filter nodes. */
2844 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
2845 {
2846   return get_Filter_proj(a) != get_Filter_proj(b);
2847 }
2848
2849 /** Compares the attributes of two Alloc nodes. */
2850 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
2851 {
2852   return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
2853       || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
2854 }
2855
2856 /** Compares the attributes of two Free nodes. */
2857 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
2858 {
2859   return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
2860       || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
2861 }
2862
2863 /** Compares the attributes of two SymConst nodes. */
2864 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
2865 {
2866   return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
2867       || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
2868       || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
2869 }
2870
2871 /** Compares the attributes of two Call nodes. */
2872 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
2873 {
2874   return (get_irn_call_attr(a) != get_irn_call_attr(b));
2875 }
2876
2877 /** Compares the attributes of two Sel nodes. */
2878 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
2879 {
2880   return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
2881       || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
2882       || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
2883       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
2884       || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
2885 }
2886
2887 /** Compares the attributes of two Phi nodes. */
2888 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
2889 {
2890   return get_irn_phi_attr (a) != get_irn_phi_attr (b);
2891 }
2892
2893 /** Compares the attributes of two Cast nodes. */
2894 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
2895 {
2896   return get_Cast_type(a) != get_Cast_type(b);
2897 }
2898
2899 /** Compares the attributes of two Load nodes. */
2900 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
2901 {
2902   if (get_Load_volatility(a) == volatility_is_volatile ||
2903       get_Load_volatility(b) == volatility_is_volatile)
2904     /* NEVER do CSE on volatile Loads */
2905     return 1;
2906
2907   return get_Load_mode(a) != get_Load_mode(b);
2908 }
2909
2910 /** Compares the attributes of two Store nodes. */
2911 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
2912 {
2913   /* NEVER do CSE on volatile Stores */
2914   return (get_Store_volatility(a) == volatility_is_volatile ||
2915           get_Store_volatility(b) == volatility_is_volatile);
2916 }
2917
2918 /** Compares the attributes of two Confirm nodes. */
2919 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
2920 {
2921   return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
2922 }
2923
2924 /**
2925  * Set the default node attribute compare operation for an ir_op_ops.
2926  *
2927  * @param code   the opcode for the default operation
2928  * @param ops    the operations initialized
2929  *
2930  * @return
2931  *    The operations.
2932  */
2933 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
2934 {
2935 #define CASE(a)                              \
2936   case iro_##a:                              \
2937     ops->node_cmp_attr  = node_cmp_attr_##a; \
2938     break
2939
2940   switch (code) {
2941   CASE(Const);
2942   CASE(Proj);
2943   CASE(Filter);
2944   CASE(Alloc);
2945   CASE(Free);
2946   CASE(SymConst);
2947   CASE(Call);
2948   CASE(Sel);
2949   CASE(Phi);
2950   CASE(Cast);
2951   CASE(Load);
2952   CASE(Store);
2953   CASE(Confirm);
2954   default:
2955     /* leave NULL */;
2956   }
2957
2958   return ops;
2959 #undef CASE
2960 }
2961
2962 /**
2963  * Compare function for two nodes in the hash table. Gets two
2964  * nodes as parameters.  Returns 0 if the nodes are a cse.
2965  */
2966 static int
2967 vt_cmp (const void *elt, const void *key)
2968 {
2969   ir_node *a, *b;
2970   int i, irn_arity_a;
2971
2972   a = (void *)elt;
2973   b = (void *)key;
2974
2975   if (a == b) return 0;
2976
2977   if ((get_irn_op(a) != get_irn_op(b)) ||
2978       (get_irn_mode(a) != get_irn_mode(b))) return 1;
2979
2980   /* compare if a's in and b's in are of equal length */
2981   irn_arity_a = get_irn_intra_arity (a);
2982   if (irn_arity_a != get_irn_intra_arity(b))
2983     return 1;
2984
2985   /* for block-local cse and op_pin_state_pinned nodes: */
2986   if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
2987     if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
2988       return 1;
2989   }
2990
2991   /* compare a->in[0..ins] with b->in[0..ins] */
2992   for (i = 0; i < irn_arity_a; i++)
2993     if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
2994       return 1;
2995
2996   /*
2997    * here, we already now that the nodes are identical except their
2998    * attributes
2999    */
3000   if (a->op->ops.node_cmp_attr)
3001     return a->op->ops.node_cmp_attr(a, b);
3002
3003   return 0;
3004 }
3005
3006 /*
3007  * Calculate a hash value of a node.
3008  */
3009 unsigned
3010 ir_node_hash (ir_node *node)
3011 {
3012   unsigned h;
3013   int i, irn_arity;
3014
3015   if (node->op == op_Const) {
3016     /* special value for const, as they only differ in their tarval. */
3017     h = HASH_PTR(node->attr.con.tv);
3018     h = 9*h + HASH_PTR(get_irn_mode(node));
3019   } else if (node->op == op_SymConst) {
3020     /* special value for const, as they only differ in their symbol. */
3021     h = HASH_PTR(node->attr.i.sym.type_p);
3022     h = 9*h + HASH_PTR(get_irn_mode(node));
3023   } else {
3024
3025     /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3026     h = irn_arity = get_irn_intra_arity(node);
3027
3028     /* consider all in nodes... except the block if not a control flow. */
3029     for (i =  is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
3030       h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3031     }
3032
3033     /* ...mode,... */
3034     h = 9*h + HASH_PTR(get_irn_mode(node));
3035     /* ...and code */
3036     h = 9*h + HASH_PTR(get_irn_op(node));
3037   }
3038
3039   return h;
3040 }
3041
3042 pset *
3043 new_identities(void) {
3044   return new_pset(vt_cmp, N_IR_NODES);
3045 }
3046
3047 void
3048 del_identities(pset *value_table) {
3049   del_pset(value_table);
3050 }
3051
3052 /**
3053  * Return the canonical node computing the same value as n.
3054  * Looks up the node in a hash table.
3055  *
3056  * For Const nodes this is performed in the constructor, too.  Const
3057  * nodes are extremely time critical because of their frequent use in
3058  * constant string arrays.
3059  */
3060 static INLINE ir_node *
3061 identify (pset *value_table, ir_node *n)
3062 {
3063   ir_node *o = NULL;
3064
3065   if (!value_table) return n;
3066
3067   if (get_opt_reassociation()) {
3068     if (is_op_commutative(get_irn_op(n))) {
3069       ir_node *l = get_binop_left(n);
3070       ir_node *r = get_binop_right(n);
3071
3072       /* for commutative operators perform  a OP b == b OP a */
3073       if (l > r) {
3074         set_binop_left(n, r);
3075         set_binop_right(n, l);
3076       }
3077     }
3078   }
3079
3080   o = pset_find (value_table, n, ir_node_hash (n));
3081   if (!o) return n;
3082
3083   DBG_OPT_CSE(n, o);
3084
3085   return o;
3086 }
3087
3088 /**
3089  * During construction we set the op_pin_state_pinned flag in the graph right when the
3090  * optimization is performed.  The flag turning on procedure global cse could
3091  * be changed between two allocations.  This way we are safe.
3092  */
3093 static INLINE ir_node *
3094 identify_cons (pset *value_table, ir_node *n) {
3095   ir_node *old = n;
3096
3097   n = identify(value_table, n);
3098   if (get_irn_n(old, -1) != get_irn_n(n, -1))
3099     set_irg_pinned(current_ir_graph, op_pin_state_floats);
3100   return n;
3101 }
3102
3103 /**
3104  * Return the canonical node computing the same value as n.
3105  * Looks up the node in a hash table, enters it in the table
3106  * if it isn't there yet.
3107  */
3108 static ir_node *
3109 identify_remember (pset *value_table, ir_node *n)
3110 {
3111   ir_node *o = NULL;
3112
3113   if (!value_table) return n;
3114
3115   if (get_opt_reassociation()) {
3116     if (is_op_commutative(get_irn_op(n))) {
3117       ir_node *l = get_binop_left(n);
3118       ir_node *r = get_binop_right(n);
3119
3120       /* for commutative operators perform  a OP b == b OP a */
3121       if (l > r) {
3122         set_binop_left(n, r);
3123         set_binop_right(n, l);
3124       }
3125     }
3126   }
3127
3128   /* lookup or insert in hash table with given hash key. */
3129   o = pset_insert (value_table, n, ir_node_hash (n));
3130
3131   if (o != n) {
3132     DBG_OPT_CSE(n, o);
3133   }
3134
3135   return o;
3136 }
3137
3138 void
3139 add_identities (pset *value_table, ir_node *node) {
3140   if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
3141     identify_remember (value_table, node);
3142 }
3143
3144 /**
3145  * garbage in, garbage out. If a node has a dead input, i.e., the
3146  * Bad node is input to the node, return the Bad node.
3147  */
3148 static INLINE ir_node *
3149 gigo (ir_node *node)
3150 {
3151   int i, irn_arity;
3152   ir_op *op = get_irn_op(node);
3153
3154   /* remove garbage blocks by looking at control flow that leaves the block
3155      and replacing the control flow by Bad. */
3156   if (get_irn_mode(node) == mode_X) {
3157     ir_node *block = get_nodes_block(skip_Proj(node));
3158
3159     /* Don't optimize nodes in immature blocks. */
3160     if (!get_Block_matured(block)) return node;
3161      /* Don't optimize End, may have Bads. */
3162     if (op == op_End) return node;
3163
3164     if (is_Block(block)) {
3165       irn_arity = get_irn_arity(block);
3166       for (i = 0; i < irn_arity; i++) {
3167         if (!is_Bad(get_irn_n(block, i)))
3168           break;
3169       }
3170       if (i == irn_arity) return new_Bad();
3171     }
3172   }
3173
3174   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3175      blocks predecessors is dead. */
3176   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3177     irn_arity = get_irn_arity(node);
3178
3179     /*
3180      * Beware: we can only read the block of a non-floating node.
3181      */
3182     if (is_irn_pinned_in_irg(node) &&
3183         is_Block_dead(get_nodes_block(node)))
3184       return new_Bad();
3185
3186     for (i = 0; i < irn_arity; i++) {
3187       ir_node *pred = get_irn_n(node, i);
3188
3189       if (is_Bad(pred))
3190         return new_Bad();
3191       if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3192         return new_Unknown(get_irn_mode(node));
3193     }
3194   }
3195 #if 0
3196   /* With this code we violate the agreement that local_optimize
3197      only leaves Bads in Block, Phi and Tuple nodes. */
3198   /* If Block has only Bads as predecessors it's garbage. */
3199   /* If Phi has only Bads as predecessors it's garbage. */
3200   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
3201     irn_arity = get_irn_arity(node);
3202     for (i = 0; i < irn_arity; i++) {
3203       if (!is_Bad(get_irn_n(node, i))) break;
3204     }
3205     if (i == irn_arity) node = new_Bad();
3206   }
3207 #endif
3208   return node;
3209 }
3210
3211
3212 /**
3213  * These optimizations deallocate nodes from the obstack.
3214  * It can only be called if it is guaranteed that no other nodes
3215  * reference this one, i.e., right after construction of a node.
3216  *
3217  * current_ir_graph must be set to the graph of the node!
3218  */
3219 ir_node *
3220 optimize_node(ir_node *n)
3221 {
3222   tarval *tv;
3223   ir_node *oldn = n;
3224   opcode iro = get_irn_opcode(n);
3225
3226   /* Always optimize Phi nodes: part of the construction. */
3227   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3228
3229   /* constant expression evaluation / constant folding */
3230   if (get_opt_constant_folding()) {
3231     /* neither constants nor Tuple values can be evaluated */
3232     if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3233       /* try to evaluate */
3234       tv = computed_value(n);
3235       if (tv != tarval_bad) {
3236         ir_node *nw;
3237         type *old_tp = get_irn_type(n);
3238         int i, arity = get_irn_arity(n);
3239         int node_size;
3240
3241         /*
3242          * Try to recover the type of the new expression.
3243          */
3244         for (i = 0; i < arity && !old_tp; ++i)
3245           old_tp = get_irn_type(get_irn_n(n, i));
3246
3247         /*
3248          * we MUST copy the node here temporary, because it's still needed
3249          * for DBG_OPT_CSTEVAL
3250          */
3251         node_size = offsetof(ir_node, attr) +  n->op->attr_size;
3252         oldn = alloca(node_size);
3253
3254         memcpy(oldn, n, node_size);
3255         CLONE_ARR_A(ir_node *, oldn->in, n->in);
3256
3257         /* ARG, copy the in array, we need it for statistics */
3258         memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3259
3260         /* note the inplace edges module */
3261         edges_node_deleted(n, current_ir_graph);
3262
3263         /* evaluation was successful -- replace the node. */
3264         obstack_free(current_ir_graph->obst, n);
3265         nw = new_Const(get_tarval_mode (tv), tv);
3266
3267         if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3268           set_Const_type(nw, old_tp);
3269         DBG_OPT_CSTEVAL(oldn, nw);
3270         return nw;
3271       }
3272     }
3273   }
3274
3275   /* remove unnecessary nodes */
3276   if (get_opt_constant_folding() ||
3277     (iro == iro_Phi)  ||   /* always optimize these nodes. */
3278     (iro == iro_Id)   ||
3279     (iro == iro_Proj) ||
3280     (iro == iro_Block)  )  /* Flags tested local. */
3281     n = equivalent_node (n);
3282
3283   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3284
3285   /* Common Subexpression Elimination.
3286    *
3287    * Checks whether n is already available.
3288    * The block input is used to distinguish different subexpressions. Right
3289    * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3290    * subexpressions within a block.
3291    */
3292   if (get_opt_cse())
3293     n = identify_cons (current_ir_graph->value_table, n);
3294
3295   if (n != oldn) {
3296     edges_node_deleted(oldn, current_ir_graph);
3297
3298     /* We found an existing, better node, so we can deallocate the old node. */
3299     obstack_free (current_ir_graph->obst, oldn);
3300
3301     return n;
3302   }
3303
3304   /* Some more constant expression evaluation that does not allow to
3305      free the node. */
3306   iro = get_irn_opcode(n);
3307   if (get_opt_constant_folding() ||
3308     (iro == iro_Cond) ||
3309     (iro == iro_Proj) ||
3310     (iro == iro_Sel))     /* Flags tested local. */
3311     n = transform_node (n);
3312
3313   /* Remove nodes with dead (Bad) input.
3314      Run always for transformation induced Bads. */
3315   n = gigo (n);
3316
3317   /* Now we have a legal, useful node. Enter it in hash table for CSE */
3318   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3319     n = identify_remember (current_ir_graph->value_table, n);
3320   }
3321
3322   return n;
3323 }
3324
3325
3326 /**
3327  * These optimizations never deallocate nodes (in place).  This can cause dead
3328  * nodes lying on the obstack.  Remove these by a dead node elimination,
3329  * i.e., a copying garbage collection.
3330  */
3331 ir_node *
3332 optimize_in_place_2 (ir_node *n)
3333 {
3334   tarval *tv;
3335   ir_node *oldn = n;
3336   opcode iro = get_irn_opcode(n);
3337
3338   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3339
3340   /* constant expression evaluation / constant folding */
3341   if (get_opt_constant_folding()) {
3342     /* neither constants nor Tuple values can be evaluated */
3343     if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3344       /* try to evaluate */
3345       tv = computed_value(n);
3346       if (tv != tarval_bad) {
3347         /* evaluation was successful -- replace the node. */
3348         type *old_tp = get_irn_type(n);
3349         int i, arity = get_irn_arity(n);
3350
3351         /*
3352          * Try to recover the type of the new expression.
3353          */
3354         for (i = 0; i < arity && !old_tp; ++i)
3355           old_tp = get_irn_type(get_irn_n(n, i));
3356
3357         n = new_Const(get_tarval_mode(tv), tv);
3358
3359         if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3360           set_Const_type(n, old_tp);
3361
3362         DBG_OPT_CSTEVAL(oldn, n);
3363         return n;
3364       }
3365     }
3366   }
3367
3368   /* remove unnecessary nodes */
3369   if (get_opt_constant_folding() ||
3370       (iro == iro_Phi)  ||   /* always optimize these nodes. */
3371       (iro == iro_Id)   ||   /* ... */
3372       (iro == iro_Proj) ||   /* ... */
3373       (iro == iro_Block)  )  /* Flags tested local. */
3374     n = equivalent_node(n);
3375
3376   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3377
3378   /** common subexpression elimination **/
3379   /* Checks whether n is already available. */
3380   /* The block input is used to distinguish different subexpressions.  Right
3381      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3382      subexpressions within a block. */
3383   if (get_opt_cse()) {
3384     n = identify(current_ir_graph->value_table, n);
3385   }
3386
3387   /* Some more constant expression evaluation. */
3388   iro = get_irn_opcode(n);
3389   if (get_opt_constant_folding() ||
3390       (iro == iro_Cond) ||
3391       (iro == iro_Proj) ||
3392       (iro == iro_Sel))     /* Flags tested local. */
3393     n = transform_node(n);
3394
3395   /* Remove nodes with dead (Bad) input.
3396      Run always for transformation induced Bads.  */
3397   n = gigo(n);
3398
3399   /* Now we can verify the node, as it has no dead inputs any more. */
3400   irn_vrfy(n);
3401
3402   /* Now we have a legal, useful node. Enter it in hash table for cse.
3403      Blocks should be unique anyways.  (Except the successor of start:
3404      is cse with the start block!) */
3405   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3406     n = identify_remember(current_ir_graph->value_table, n);
3407
3408   return n;
3409 }
3410
3411 /**
3412  * Wrapper for external use, set proper status bits after optimization.
3413  */
3414 ir_node *
3415 optimize_in_place (ir_node *n)
3416 {
3417   /* Handle graph state */
3418   assert(get_irg_phase_state(current_ir_graph) != phase_building);
3419
3420   if (get_opt_global_cse())
3421     set_irg_pinned(current_ir_graph, op_pin_state_floats);
3422   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3423     set_irg_outs_inconsistent(current_ir_graph);
3424
3425   /* Maybe we could also test whether optimizing the node can
3426      change the control graph. */
3427   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
3428     set_irg_dom_inconsistent(current_ir_graph);
3429   return optimize_in_place_2 (n);
3430 }
3431
3432 /*
3433  * Sets the default operation for an ir_ops.
3434  */
3435 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
3436 {
3437   ops = firm_set_default_computed_value(code, ops);
3438   ops = firm_set_default_equivalent_node(code, ops);
3439   ops = firm_set_default_transform_node(code, ops);
3440   ops = firm_set_default_node_cmp_attr(code, ops);
3441   ops = firm_set_default_get_type(code, ops);
3442
3443   return ops;
3444 }