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