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