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