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