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