a46b83dc23581e6f2c51120e91c6200ba6a83ffc
[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
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 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 # include "irnode_t.h"
18 # include "irgraph_t.h"
19 # include "irmode_t.h"
20 # include "iropt_t.h"
21 # include "ircons.h"
22 # include "irgmod.h"
23 # include "irvrfy.h"
24 # include "tv.h"
25 # include "dbginfo_t.h"
26 # include "iropt_dbg.h"
27 # include "irflag_t.h"
28 # include "firmstat.h"
29
30 /* Make types visible to allow most efficient access */
31 # include "entity_t.h"
32
33 /**
34  * Trivial INLINEable routine for copy propagation.
35  * Does follow Ids, needed to optimize INLINEd code.
36  */
37 static INLINE ir_node *
38 follow_Id (ir_node *n)
39 {
40   while (get_irn_op (n) == op_Id) n = get_Id_pred (n);
41   return n;
42 }
43
44 /**
45  * Returns the tarval of a Const node or tarval_bad for all other nodes.
46  */
47 static INLINE tarval *
48 value_of (ir_node *n)
49 {
50   if ((n != NULL) && (get_irn_op(n) == op_Const))
51     return get_Const_tarval(n); /* might return tarval_bad */
52   else
53     return tarval_bad;
54 }
55
56 static tarval *computed_value_Const(ir_node *n)
57 {
58     return get_Const_tarval(n);
59 }
60
61 static tarval *computed_value_SymConst(ir_node *n)
62 {
63   if ((get_SymConst_kind(n) == size) &&
64       (get_type_state(get_SymConst_type(n))) == layout_fixed)
65     return new_tarval_from_long (get_type_size(get_SymConst_type(n)), mode_Is);
66   return tarval_bad;
67 }
68
69 static tarval *computed_value_Add(ir_node *n)
70 {
71   ir_node *a = get_Add_left(n);
72   ir_node *b = get_Add_right(n);
73
74   tarval *ta = value_of(a);
75   tarval *tb = value_of(b);
76
77   if ((ta != tarval_bad) && (tb != tarval_bad)
78         && (get_irn_mode(a) == get_irn_mode(b))
79         && !(get_mode_sort(get_irn_mode(a)) == irms_reference)) {
80     return tarval_add(ta, tb);
81   }
82   return tarval_bad;
83 }
84
85 static tarval *computed_value_Sub(ir_node *n)
86 {
87   ir_node *a = get_Sub_left(n);
88   ir_node *b = get_Sub_right(n);
89
90   tarval *ta = value_of(a);
91   tarval *tb = value_of(b);
92
93   if ((ta != tarval_bad) && (tb != tarval_bad)
94         && (get_irn_mode(a) == get_irn_mode(b))
95         && !(get_mode_sort(get_irn_mode(a)) == irms_reference)) {
96     return tarval_sub(ta, tb);
97   }
98   return tarval_bad;
99 }
100
101 static tarval *computed_value_Minus(ir_node *n)
102 {
103   ir_node *a = get_Minus_op(n);
104   tarval *ta = value_of(a);
105
106   if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
107     return tarval_neg(ta);
108
109   return tarval_bad;
110 }
111
112 static tarval *computed_value_Mul(ir_node *n)
113 {
114   ir_node *a = get_Mul_left(n);
115   ir_node *b = get_Mul_right(n);
116
117   tarval *ta = value_of(a);
118   tarval *tb = value_of(b);
119
120   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
121     return tarval_mul(ta, tb);
122   } else {
123     /* a*0 = 0 or 0*b = 0:
124        calls computed_value recursive and returns the 0 with proper
125        mode. */
126     tarval *v;
127
128     if ( ( ((v = ta) != tarval_bad)
129              && (v == get_mode_null(get_tarval_mode(v))) )
130       || ( ((v = tb) != tarval_bad)
131              && (v == get_mode_null(get_tarval_mode(v))) )) {
132         return v;
133     }
134   }
135   return tarval_bad;
136 }
137
138 static tarval *computed_value_Quot(ir_node *n)
139 {
140   ir_node *a = get_Quot_left(n);
141   ir_node *b = get_Quot_right(n);
142
143   tarval *ta = value_of(a);
144   tarval *tb = value_of(b);
145
146   /* This was missing in original implementation. Why? */
147   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
148     if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
149       return tarval_quo(ta, tb);
150   }
151   return tarval_bad;
152 }
153
154 static tarval *computed_value_Div(ir_node *n)
155 {
156   ir_node *a = get_Div_left(n);
157   ir_node *b = get_Div_right(n);
158
159   tarval *ta = value_of(a);
160   tarval *tb = value_of(b);
161
162   /* This was missing in original implementation. Why? */
163   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
164     if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
165       return tarval_div(ta, tb);
166   }
167   return tarval_bad;
168 }
169
170 static tarval *computed_value_Mod(ir_node *n)
171 {
172   ir_node *a = get_Mod_left(n);
173   ir_node *b = get_Mod_right(n);
174
175   tarval *ta = value_of(a);
176   tarval *tb = value_of(b);
177
178   /* This was missing in original implementation. Why? */
179   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
180     if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
181       return tarval_mod(ta, tb);
182   }
183   return tarval_bad;
184 }
185
186 static tarval *computed_value_Abs(ir_node *n)
187 {
188   ir_node *a = get_Abs_op(n);
189   tarval *ta = value_of(a);
190
191   if (ta != tarval_bad)
192     return tarval_abs(ta);
193
194   return tarval_bad;
195 }
196
197 static tarval *computed_value_And(ir_node *n)
198 {
199   ir_node *a = get_And_left(n);
200   ir_node *b = get_And_right(n);
201
202   tarval *ta = value_of(a);
203   tarval *tb = value_of(b);
204
205   if ((ta != tarval_bad) && (tb != tarval_bad)) {
206     return tarval_and (ta, tb);
207   } else {
208     tarval *v;
209
210     if (   (tarval_classify ((v = computed_value (a))) == TV_CLASSIFY_NULL)
211         || (tarval_classify ((v = computed_value (b))) == TV_CLASSIFY_NULL)) {
212       return v;
213     }
214   }
215   return tarval_bad;
216 }
217
218 static tarval *computed_value_Or(ir_node *n)
219 {
220   ir_node *a = get_Or_left(n);
221   ir_node *b = get_Or_right(n);
222
223   tarval *ta = value_of(a);
224   tarval *tb = value_of(b);
225
226   if ((ta != tarval_bad) && (tb != tarval_bad)) {
227     return tarval_or (ta, tb);
228   } else {
229     tarval *v;
230     if (   (tarval_classify ((v = computed_value (a))) == TV_CLASSIFY_ALL_ONE)
231         || (tarval_classify ((v = computed_value (b))) == TV_CLASSIFY_ALL_ONE)) {
232       return v;
233     }
234   }
235   return tarval_bad;
236 }
237
238 static tarval *computed_value_Eor(ir_node *n)
239 {
240   ir_node *a = get_Eor_left(n);
241   ir_node *b = get_Eor_right(n);
242
243   tarval *ta = value_of(a);
244   tarval *tb = value_of(b);
245
246   if ((ta != tarval_bad) && (tb != tarval_bad)) {
247     return tarval_eor (ta, tb);
248   }
249   return tarval_bad;
250 }
251
252 static tarval *computed_value_Not(ir_node *n)
253 {
254   ir_node *a = get_Not_op(n);
255   tarval *ta = value_of(a);
256
257   if (ta != tarval_bad)
258     return tarval_not(ta);
259
260   return tarval_bad;
261 }
262
263 static tarval *computed_value_Shl(ir_node *n)
264 {
265   ir_node *a = get_Shl_left(n);
266   ir_node *b = get_Shl_right(n);
267
268   tarval *ta = value_of(a);
269   tarval *tb = value_of(b);
270
271   if ((ta != tarval_bad) && (tb != tarval_bad)) {
272     return tarval_shl (ta, tb);
273   }
274   return tarval_bad;
275 }
276
277 static tarval *computed_value_Shr(ir_node *n)
278 {
279   ir_node *a = get_Shr_left(n);
280   ir_node *b = get_Shr_right(n);
281
282   tarval *ta = value_of(a);
283   tarval *tb = value_of(b);
284
285   if ((ta != tarval_bad) && (tb != tarval_bad)) {
286     return tarval_shr (ta, tb);
287   }
288   return tarval_bad;
289 }
290
291 static tarval *computed_value_Shrs(ir_node *n)
292 {
293   ir_node *a = get_Shrs_left(n);
294   ir_node *b = get_Shrs_right(n);
295
296   tarval *ta = value_of(a);
297   tarval *tb = value_of(b);
298
299   if ((ta != tarval_bad) && (tb != tarval_bad)) {
300     return tarval_shrs (ta, tb);
301   }
302   return tarval_bad;
303 }
304
305 static tarval *computed_value_Rot(ir_node *n)
306 {
307   ir_node *a = get_Rot_left(n);
308   ir_node *b = get_Rot_right(n);
309
310   tarval *ta = value_of(a);
311   tarval *tb = value_of(b);
312
313   if ((ta != tarval_bad) && (tb != tarval_bad)) {
314     /* return tarval_rot (ta, tb); */
315   }
316   return tarval_bad;
317 }
318
319 static tarval *computed_value_Conv(ir_node *n)
320 {
321   ir_node *a = get_Conv_op(n);
322   tarval *ta = value_of(a);
323
324   if (ta != tarval_bad)
325     return tarval_convert_to(ta, get_irn_mode(n));
326
327   return tarval_bad;
328 }
329
330 static tarval *computed_value_Proj(ir_node *n)
331 {
332   ir_node *a = get_Proj_pred(n), *b;
333   ir_node *aa, *ab;
334
335   /* Optimize Cmp nodes.
336      This performs a first step of unreachable code elimination.
337      Proj can not be computed, but folding a Cmp above the Proj here is
338      not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
339      only 1 is used.
340      There are several case where we can evaluate a Cmp node:
341      1. The nodes compared are both the same.  If we compare for
342         equal, greater equal, ... this will return true, else it
343         will return false.  This step relies on cse.
344      2. The predecessors of Cmp are target values.  We can evaluate
345         the Cmp.
346      3. The predecessors are Allocs or void* constants.  Allocs never
347         return NULL, they raise an exception.   Therefore we can predict
348         the Cmp result. */
349   if (get_irn_op(a) == op_Cmp) {
350     aa = get_Cmp_left(a);
351     ab = get_Cmp_right(a);
352
353     if (aa == ab) { /* 1.: */
354       /* This is a tric with the bits used for encoding the Cmp
355          Proj numbers, the following statement is not the same:
356       return new_tarval_from_long ((get_Proj_proj(n) == Eq), mode_b) */
357       return new_tarval_from_long ((get_Proj_proj(n) & Eq), mode_b);
358     } else {
359       tarval *taa = computed_value (aa);
360       tarval *tab = computed_value (ab);
361
362       if ((taa != tarval_bad) && (tab != tarval_bad)) { /* 2.: */
363         /* strange checks... */
364         pnc_number flags = tarval_cmp (taa, tab);
365         if (flags != False) {
366           return new_tarval_from_long (get_Proj_proj(n) & flags, mode_b);
367         }
368       } else {  /* check for 3.: */
369         ir_node *aaa = skip_nop(skip_Proj(aa));
370         ir_node *aba = skip_nop(skip_Proj(ab));
371
372         if (   (   (/* aa is ProjP and aaa is Alloc */
373                        (get_irn_op(aa) == op_Proj)
374                     && (mode_is_reference(get_irn_mode(aa)))
375                     && (get_irn_op(aaa) == op_Alloc))
376                 && (   (/* ab is constant void */
377                            (get_irn_op(ab) == op_Const)
378                         && (mode_is_reference(get_irn_mode(ab)))
379                         && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
380                     || (/* ab is other Alloc */
381                            (get_irn_op(ab) == op_Proj)
382                         && (mode_is_reference(get_irn_mode(ab)))
383                         && (get_irn_op(aba) == op_Alloc)
384                         && (aaa != aba))))
385             || (/* aa is void and aba is Alloc */
386                    (get_irn_op(aa) == op_Const)
387                 && (mode_is_reference(get_irn_mode(aa)))
388                 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
389                 && (get_irn_op(ab) == op_Proj)
390                 && (mode_is_reference(get_irn_mode(ab)))
391                 && (get_irn_op(aba) == op_Alloc)))
392           /* 3.: */
393           return new_tarval_from_long (get_Proj_proj(n) & Ne, mode_b);
394       }
395     }
396   } else if (get_irn_op(a) == op_DivMod) {
397     tarval *tb = value_of(b = get_DivMod_right(a));
398     tarval *ta = value_of(a = get_DivMod_left(a));
399
400     if ((ta != tarval_bad)  && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
401       if (tb == get_mode_null(get_tarval_mode(tb)))  /* div by zero: return tarval_bad */
402         return tarval_bad;
403       if (get_Proj_proj(n)== 0) /* Div */
404         return tarval_div(ta, tb);
405       else /* Mod */
406         return tarval_mod(ta, tb);
407     }
408   }
409   return tarval_bad;
410 }
411
412 /**
413  * If the parameter n can be computed, return its value, else tarval_bad.
414  * Performs constant folding.
415  *
416  * GL: Only if n is arithmetic operator?
417  */
418 tarval *computed_value(ir_node *n)
419 {
420   if (n->op->computed_value)
421     return n->op->computed_value(n);
422   return tarval_bad;
423 }
424
425 /**
426  * set the default computed_value evaluator
427  */
428 static ir_op *firm_set_default_computed_value(ir_op *op)
429 {
430 #define CASE(a)                                 \
431   case iro_##a:                                 \
432     op->computed_value  = computed_value_##a;   \
433     break
434
435   switch (op->code) {
436   CASE(Const);
437   CASE(SymConst);
438   CASE(Add);
439   CASE(Sub);
440   CASE(Minus);
441   CASE(Mul);
442   CASE(Quot);
443   CASE(Div);
444   CASE(Mod);
445   CASE(Abs);
446   CASE(And);
447   CASE(Or);
448   CASE(Eor);
449   CASE(Not);
450   CASE(Shl);
451   CASE(Shr);
452   CASE(Shrs);
453   CASE(Rot);
454   CASE(Conv);
455   CASE(Proj);
456   default:
457     op->computed_value  = NULL;
458   }
459
460   return op;
461 #undef CASE
462 }
463
464 #if 0
465 /* returns 1 if the a and b are pointers to different locations. */
466 static bool
467 different_identity (ir_node *a, ir_node *b)
468 {
469   assert (mode_is_reference(get_irn_mode (a))
470           && mode_is_reference(get_irn_mode (b)));
471
472   if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
473     ir_node *a1 = get_Proj_pred (a);
474     ir_node *b1 = get_Proj_pred (b);
475     if (a1 != b1 && get_irn_op (a1) == op_Alloc
476                 && get_irn_op (b1) == op_Alloc)
477       return 1;
478   }
479   return 0;
480 }
481 #endif
482
483 static ir_node *equivalent_node_Block(ir_node *n)
484 {
485   ir_node *oldn = n;
486
487   /* The Block constructor does not call optimize, but mature_block
488      calls the optimization. */
489   assert(get_Block_matured(n));
490
491   /* Straightening: a single entry Block following a single exit Block
492      can be merged, if it is not the Start block. */
493   /* !!! Beware, all Phi-nodes of n must have been optimized away.
494      This should be true, as the block is matured before optimize is called.
495      But what about Phi-cycles with the Phi0/Id that could not be resolved?
496      Remaining Phi nodes are just Ids. */
497   if (get_Block_n_cfgpreds(n) == 1) {
498     ir_node *cfgpred = skip_Proj(get_Block_cfgpred(n, 0));
499     ir_node *predblock = get_nodes_Block(cfgpred);
500     if (get_irn_op(cfgpred) == op_Jmp) {
501       if (predblock == oldn) {
502         /* Jmp jumps into the block it is in -- deal self cycle. */
503         n = new_Bad();                                      DBG_OPT_DEAD;
504       } else if (get_opt_control_flow_straightening()) {
505         n = predblock;                                      DBG_OPT_STG;
506       }
507     } else if ((get_irn_op(cfgpred) == op_Cond) && (predblock == oldn)) {
508       /* Cond jumps into the block it is in -- deal self cycle. */
509       n = new_Bad();                                      DBG_OPT_DEAD;
510     }
511
512   } else if ((get_Block_n_cfgpreds(n) == 2) &&
513              (get_opt_control_flow_weak_simplification())) {
514     /* Test whether Cond jumps twice to this block
515        @@@ we could do this also with two loops finding two preds from several ones. */
516     ir_node *a = get_Block_cfgpred(n, 0);
517     ir_node *b = get_Block_cfgpred(n, 1);
518
519     if ((get_irn_op(a) == op_Proj) &&
520         (get_irn_op(b) == op_Proj) &&
521         (get_Proj_pred(a) == get_Proj_pred(b)) &&
522         (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
523         (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
524       /* Also a single entry Block following a single exit Block.  Phis have
525          twice the same operand and will be optimized away. */
526       n = get_nodes_Block(a);                                         DBG_OPT_IFSIM;
527     }
528   } else if (get_opt_unreachable_code() &&
529              (n != current_ir_graph->start_block) &&
530              (n != current_ir_graph->end_block)     ) {
531     int i;
532     /* If all inputs are dead, this block is dead too, except if it is
533        the start or end block.  This is a step of unreachable code
534        elimination */
535     for (i = 0; i < get_Block_n_cfgpreds(n); i++) {
536       if (!is_Bad(get_Block_cfgpred(n, i))) break;
537     }
538     if (i == get_Block_n_cfgpreds(n))
539       n = new_Bad();
540   }
541
542   return n;
543 }
544
545 static ir_node *equivalent_node_Jmp(ir_node *n)
546 {
547   /* GL: Why not same for op_Raise?? */
548   /* unreachable code elimination */
549   if (is_Bad(get_nodes_Block(n)))
550     n = new_Bad();
551
552   return n;
553 }
554
555 static ir_node *equivalent_node_Cond(ir_node *n)
556 {
557   /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
558      See cases for iro_Cond and iro_Proj in transform_node. */
559   return n;
560 }
561
562 static ir_node *equivalent_node_Or(ir_node *n)
563 {
564   ir_node *oldn = n;
565
566   ir_node *a = get_Or_left(n);
567   ir_node *b = get_Or_right(n);
568
569   /* remove a v a */
570   if (a == b) {
571     n = a;                                                             DBG_OPT_ALGSIM1;
572   }
573
574   return n;
575 }
576
577 /**
578  * optimize operations that are commutative and have neutral 0.
579  */
580 static ir_node *equivalent_node_neutral_zero(ir_node *n)
581 {
582   ir_node *oldn = n;
583
584   ir_node *a = get_binop_left(n);
585   ir_node *b = get_binop_right(n);
586
587   tarval *tv;
588   ir_node *on;
589
590   /* After running compute_node there is only one constant predecessor.
591      Find this predecessors value and remember the other node: */
592   if ((tv = computed_value (a)) != tarval_bad) {
593     on = b;
594   } else if ((tv = computed_value (b)) != tarval_bad) {
595     on = a;
596   } else
597     return n;
598
599   /* If this predecessors constant value is zero, the operation is
600      unnecessary. Remove it: */
601   if (tarval_classify (tv) == TV_CLASSIFY_NULL) {
602     n = on;                                                             DBG_OPT_ALGSIM1;
603   }
604
605   return n;
606 }
607
608 static ir_node *equivalent_node_Add(ir_node *n)
609 {
610   return equivalent_node_neutral_zero(n);
611 }
612
613 static ir_node *equivalent_node_Eor(ir_node *n)
614 {
615   return equivalent_node_neutral_zero(n);
616 }
617
618 /**
619  * optimize operations that are not commutative but have neutral 0 on left.
620  * Test only one predecessor.
621  */
622 static ir_node *equivalent_node_left_zero(ir_node *n)
623 {
624   ir_node *oldn = n;
625
626   ir_node *a = get_binop_left(n);
627   ir_node *b = get_binop_right(n);
628
629   if (tarval_classify (computed_value (b)) == TV_CLASSIFY_NULL) {
630     n = a;                                                              DBG_OPT_ALGSIM1;
631   }
632
633   return n;
634 }
635
636 static ir_node *equivalent_node_Sub(ir_node *n)
637 {
638   return equivalent_node_left_zero(n);
639 }
640
641 static ir_node *equivalent_node_Shl(ir_node *n)
642 {
643   return equivalent_node_left_zero(n);
644 }
645
646 static ir_node *equivalent_node_Shr(ir_node *n)
647 {
648   return equivalent_node_left_zero(n);
649 }
650
651 static ir_node *equivalent_node_Shrs(ir_node *n)
652 {
653   return equivalent_node_left_zero(n);
654 }
655
656 static ir_node *equivalent_node_Rot(ir_node *n)
657 {
658   return equivalent_node_left_zero(n);
659 }
660
661 static ir_node *equivalent_node_symmetric_unop(ir_node *n)
662 {
663   ir_node *oldn = n;
664
665   /* optimize symmetric unop */
666   if (get_irn_op(get_unop_op(n)) == get_irn_op(n)) {
667     n = get_unop_op(get_unop_op(n));                                    DBG_OPT_ALGSIM2;
668   }
669   return n;
670 }
671
672 static ir_node *equivalent_node_Not(ir_node *n)
673 {
674   /* NotNot x == x */
675   return equivalent_node_symmetric_unop(n);
676 }
677
678 static ir_node *equivalent_node_Minus(ir_node *n)
679 {
680   /* --x == x */  /* ??? Is this possible or can --x raise an
681                          out of bounds exception if min =! max? */
682   return equivalent_node_symmetric_unop(n);
683 }
684
685 static ir_node *equivalent_node_Mul(ir_node *n)
686 {
687   ir_node *oldn = n;
688
689   ir_node *a = get_Mul_left(n);
690   ir_node *b = get_Mul_right(n);
691
692   /* Mul is commutative and has again an other neutral element. */
693   if (tarval_classify (computed_value (a)) == TV_CLASSIFY_ONE) {
694     n = b;                                                              DBG_OPT_ALGSIM1;
695   } else if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE) {
696     n = a;                                                              DBG_OPT_ALGSIM1;
697   }
698   return n;
699 }
700
701 static ir_node *equivalent_node_Div(ir_node *n)
702 {
703   ir_node *a = get_Div_left(n);
704   ir_node *b = get_Div_right(n);
705
706   /* Div is not commutative. */
707   if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
708     /* Turn Div into a tuple (mem, bad, a) */
709     ir_node *mem = get_Div_mem(n);
710     turn_into_tuple(n, 3);
711     set_Tuple_pred(n, pn_Div_M,        mem);
712     set_Tuple_pred(n, pn_Div_X_except, new_Bad());      /* no exception */
713     set_Tuple_pred(n, pn_Div_res,      a);
714   }
715   return n;
716 }
717
718 static ir_node *equivalent_node_And(ir_node *n)
719 {
720   ir_node *oldn = n;
721
722   ir_node *a = get_And_left(n);
723   ir_node *b = get_And_right(n);
724
725   if (a == b) {
726     n = a;    /* And has it's own neutral element */
727   } else if (tarval_classify (computed_value (a)) == TV_CLASSIFY_ALL_ONE) {
728     n = b;
729   } else if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ALL_ONE) {
730     n = a;
731   }
732   if (n != oldn)                                                        DBG_OPT_ALGSIM1;
733   return n;
734 }
735
736 static ir_node *equivalent_node_Conv(ir_node *n)
737 {
738   ir_node *oldn = n;
739   ir_node *a = get_Conv_op(n);
740   ir_node *b;
741
742   ir_mode *n_mode = get_irn_mode(n);
743   ir_mode *a_mode = get_irn_mode(a);
744
745   if (n_mode == a_mode) { /* No Conv necessary */
746     n = a;                                                              DBG_OPT_ALGSIM3;
747   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
748     ir_mode *b_mode;
749
750     b = get_Conv_op(a);
751     n_mode = get_irn_mode(n);
752     b_mode = get_irn_mode(b);
753
754     if (n_mode == b_mode) {
755       if (n_mode == mode_b) {
756         n = b;  /* Convb(Conv*(xxxb(...))) == xxxb(...) */              DBG_OPT_ALGSIM1;
757       }
758       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
759         if (smaller_mode(b_mode, a_mode)){
760           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */      DBG_OPT_ALGSIM1;
761         }
762       }
763     }
764   }
765   return n;
766 }
767
768 static ir_node *equivalent_node_Phi(ir_node *n)
769 {
770   /* Several optimizations:
771      - no Phi in start block.
772      - remove Id operators that are inputs to Phi
773      - fold Phi-nodes, iff they have only one predecessor except
774              themselves.
775   */
776   int i, n_preds;
777
778   ir_node *oldn = n;
779   ir_node *block = NULL;     /* to shutup gcc */
780   ir_node *first_val = NULL; /* to shutup gcc */
781   ir_node *scnd_val = NULL;  /* to shutup gcc */
782
783   if (!get_opt_normalize()) return n;
784
785   n_preds = get_Phi_n_preds(n);
786
787   block = get_nodes_block(n);
788   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
789      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
790   if ((is_Bad(block)) ||                         /* Control dead */
791       (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
792     return new_Bad();                        /* in the Start Block. */
793
794   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
795
796 #if 0
797   /* first we test for a special case: */
798   /* Confirm is a special node fixing additional information for a
799      value that is known at a certain point.  This is useful for
800      dataflow analysis. */
801   if (n_preds == 2) {
802     ir_node *a = get_Phi_pred(n, 0);
803     ir_node *b = get_Phi_pred(n, 1);
804     if (   (get_irn_op(a) == op_Confirm)
805         && (get_irn_op(b) == op_Confirm)
806         && follow_Id (get_irn_n(a, 0) == get_irn_n(b, 0))
807         && (get_irn_n(a, 1) == get_irn_n (b, 1))
808         && (a->data.num == (~b->data.num & irpn_True) )) {
809       return get_irn_n(a, 0);
810     }
811   }
812 #endif
813
814   /* If the Block has a Bad pred, we also have one. */
815   for (i = 0;  i < n_preds;  ++i)
816     if (is_Bad (get_Block_cfgpred(block, i)))
817       set_Phi_pred(n, i, new_Bad());
818
819   /* Find first non-self-referencing input */
820   for (i = 0;  i < n_preds;  ++i) {
821     first_val = get_Phi_pred(n, i);
822     if (   (first_val != n)                            /* not self pointer */
823 #if 1
824         && (get_irn_op(first_val) != op_Bad)
825 #endif
826            ) {        /* value not dead */
827       break;          /* then found first value. */
828     }
829   }
830
831   /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
832   if (i >= n_preds) { return new_Bad(); }
833
834   scnd_val = NULL;
835
836   /* follow_Id () for rest of inputs, determine if any of these
837      are non-self-referencing */
838   while (++i < n_preds) {
839     scnd_val = get_Phi_pred(n, i);
840     if (   (scnd_val != n)
841         && (scnd_val != first_val)
842 #if 1
843         && (get_irn_op(scnd_val) != op_Bad)
844 #endif
845            ) {
846       break;
847     }
848   }
849
850   /* Fold, if no multiple distinct non-self-referencing inputs */
851   if (i >= n_preds) {
852     n = first_val;                                     DBG_OPT_PHI;
853   } else {
854     /* skip the remaining Ids (done in get_Phi_pred). */
855     /* superfluous, since we walk all to propagate Block's Bads.
856        while (++i < n_preds) get_Phi_pred(n, i);     */
857   }
858   return n;
859 }
860
861 static ir_node *equivalent_node_Load(ir_node *n)
862 {
863 #if 0  /* Is an illegal transformation: different nodes can
864           represent the same pointer value!! */
865  ir_node *a = skip_Proj(get_Load_mem(n));
866  ir_node *b = get_Load_ptr(n);
867
868  if (get_irn_op(a) == op_Store) {
869    if ( different_identity (b, get_Store_ptr(a))) {
870          /* load and store use different pointers, therefore load
871                 needs not take store's memory but the state before. */
872          set_Load_mem (n, get_Store_mem(a));
873    } else if (( 0 /* ???didn't get cryptic test that returns 0 */ )) {
874    }
875  }
876 #endif
877  return n;
878 }
879
880 static ir_node *equivalent_node_Store(ir_node *n)
881 {
882   ir_node *oldn = n;
883
884   /* remove unnecessary store. */
885   ir_node *a = skip_Proj(get_Store_mem(n));
886   ir_node *b = get_Store_ptr(n);
887   ir_node *c = skip_Proj(get_Store_value(n));
888
889   if (get_irn_op(a) == op_Store
890       && get_Store_ptr(a) == b
891       && skip_Proj(get_Store_value(a)) == c) {
892     /* We have twice exactly the same store -- a write after write. */
893     n = a;                                                         DBG_OPT_WAW;
894   } else if (get_irn_op(c) == op_Load
895              && (a == c || skip_Proj(get_Load_mem(c)) == a)
896              && get_Load_ptr(c) == b ) {
897     /* We just loaded the value from the same memory, i.e., the store
898        doesn't change the memory -- a write after read. */
899     a = get_Store_mem(n);
900     turn_into_tuple(n, 2);
901     set_Tuple_pred(n, pn_Store_M,        a);
902     set_Tuple_pred(n, pn_Store_X_except, new_Bad());               DBG_OPT_WAR;
903   }
904   return n;
905 }
906
907 static ir_node *equivalent_node_Proj(ir_node *n)
908 {
909   ir_node *oldn = n;
910
911   ir_node *a = get_Proj_pred(n);
912
913   if ( get_irn_op(a) == op_Tuple) {
914     /* Remove the Tuple/Proj combination. */
915     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
916       n = get_Tuple_pred(a, get_Proj_proj(n));                     DBG_OPT_TUPLE;
917     } else {
918       assert(0); /* This should not happen! */
919       n = new_Bad();
920     }
921   } else if (get_irn_mode(n) == mode_X &&
922              is_Bad(get_nodes_Block(n))) {
923     /* Remove dead control flow -- early gigo. */
924     n = new_Bad();
925   }
926   return n;
927 }
928
929 static ir_node *equivalent_node_Id(ir_node *n)
930 {
931   ir_node *oldn = n;
932
933   n = follow_Id (n);                                                 DBG_OPT_ID;
934   return n;
935 }
936
937 /*
938 case iro_Mod, Quot, DivMod
939   DivMod allocates new nodes --> it's treated in transform node.
940   What about Quot, DivMod?
941 */
942
943 /**
944  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
945  * perform no actual computation, as, e.g., the Id nodes.  It does not create
946  * new nodes.  It is therefore safe to free n if the node returned is not n.
947  * If a node returns a Tuple we can not just skip it.  If the size of the
948  * in array fits, we transform n into a tuple (e.g., Div).
949  */
950 ir_node *
951 equivalent_node (ir_node *n)
952 {
953   if (n->op->equivalent_node)
954     return n->op->equivalent_node(n);
955   return n;
956 }
957
958 /**
959  * set the default equivalent node operation
960  */
961 static ir_op *firm_set_default_equivalent_node(ir_op *op)
962 {
963 #define CASE(a)                                 \
964   case iro_##a:                                 \
965     op->equivalent_node  = equivalent_node_##a; \
966     break
967
968   switch (op->code) {
969   CASE(Block);
970   CASE(Jmp);
971   CASE(Cond);
972   CASE(Or);
973   CASE(Add);
974   CASE(Eor);
975   CASE(Sub);
976   CASE(Shl);
977   CASE(Shr);
978   CASE(Shrs);
979   CASE(Rot);
980   CASE(Not);
981   CASE(Minus);
982   CASE(Mul);
983   CASE(Div);
984   CASE(And);
985   CASE(Conv);
986   CASE(Phi);
987   CASE(Load);
988   CASE(Store);
989   CASE(Proj);
990   CASE(Id);
991   default:
992     op->equivalent_node  = NULL;
993   }
994
995   return op;
996 #undef CASE
997 }
998
999 /**
1000  * Do node specific optimizations of nodes predecessors.
1001  */
1002 static void
1003 optimize_preds(ir_node *n) {
1004   ir_node *a = NULL, *b = NULL;
1005
1006   /* get the operands we will work on for simple cases. */
1007   if (is_binop(n)) {
1008     a = get_binop_left(n);
1009     b = get_binop_right(n);
1010   } else if (is_unop(n)) {
1011     a = get_unop_op(n);
1012   }
1013
1014   switch (get_irn_opcode(n)) {
1015
1016   case iro_Cmp:
1017     /* We don't want Cast as input to Cmp. */
1018     if (get_irn_op(a) == op_Cast) {
1019       a = get_Cast_op(a);
1020       set_Cmp_left(n, a);
1021     }
1022     if (get_irn_op(b) == op_Cast) {
1023       b = get_Cast_op(b);
1024       set_Cmp_right(n, b);
1025     }
1026     break;
1027
1028   default: break;
1029   } /* end switch */
1030 }
1031
1032 static ir_node *transform_node_Div(ir_node *n)
1033 {
1034   tarval *ta = computed_value(n);
1035
1036   if (ta != tarval_bad) {
1037     /* Turn Div into a tuple (mem, bad, value) */
1038     ir_node *mem = get_Div_mem(n);
1039
1040     turn_into_tuple(n, 3);
1041     set_Tuple_pred(n, pn_Div_M, mem);
1042     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1043     set_Tuple_pred(n, pn_Div_res, new_Const(get_tarval_mode(ta), ta));
1044   }
1045   return n;
1046 }
1047
1048 static ir_node *transform_node_Mod(ir_node *n)
1049 {
1050   tarval *ta = computed_value(n);
1051
1052   if (ta != tarval_bad) {
1053     /* Turn Mod into a tuple (mem, bad, value) */
1054     ir_node *mem = get_Mod_mem(n);
1055     turn_into_tuple(n, 3);
1056     set_Tuple_pred(n, pn_Mod_M, mem);
1057     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1058     set_Tuple_pred(n, pn_Mod_res, new_Const(get_tarval_mode(ta), ta));
1059   }
1060   return n;
1061 }
1062
1063 static ir_node *transform_node_DivMod(ir_node *n)
1064 {
1065   int evaluated = 0;
1066
1067   ir_node *a = get_DivMod_left(n);
1068   ir_node *b = get_DivMod_right(n);
1069   ir_mode *mode = get_irn_mode(a);
1070
1071   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1072     return n;
1073
1074   if (a == b) {
1075     a = new_Const(mode, get_mode_one(mode));
1076     b = new_Const(mode, get_mode_null(mode));
1077     evaluated = 1;
1078   } else {
1079     tarval *ta = value_of(a);
1080     tarval *tb = value_of(b);
1081
1082     if (tb != tarval_bad) {
1083       if (tb == get_mode_one(get_tarval_mode(tb))) {
1084         b = new_Const (mode, get_mode_null(mode));
1085         evaluated = 1;
1086       } else if (ta != tarval_bad) {
1087         tarval *resa, *resb;
1088         resa = tarval_div (ta, tb);
1089         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1090                                           Jmp for X result!? */
1091         resb = tarval_mod (ta, tb);
1092         if (resb == tarval_bad) return n; /* Causes exception! */
1093         a = new_Const (mode, resa);
1094         b = new_Const (mode, resb);
1095         evaluated = 1;
1096       }
1097     } else if (ta == get_mode_null(mode)) {
1098       b = a;
1099       evaluated = 1;
1100     }
1101   }
1102   if (evaluated) { /* replace by tuple */
1103     ir_node *mem = get_DivMod_mem(n);
1104     turn_into_tuple(n, 4);
1105     set_Tuple_pred(n, pn_DivMod_M,        mem);
1106     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
1107     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1108     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
1109     assert(get_nodes_Block(n));
1110   }
1111
1112   return n;
1113 }
1114
1115 static ir_node *transform_node_Cond(ir_node *n)
1116 {
1117   /* Replace the Cond by a Jmp if it branches on a constant
1118      condition. */
1119   ir_node *jmp;
1120   ir_node *a = get_Cond_selector(n);
1121   tarval *ta = value_of(a);
1122
1123   if ((ta != tarval_bad) &&
1124       (get_irn_mode(a) == mode_b) &&
1125       (get_opt_unreachable_code())) {
1126     /* It's a boolean Cond, branching on a boolean constant.
1127                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1128     jmp = new_r_Jmp(current_ir_graph, get_nodes_Block(n));
1129     turn_into_tuple(n, 2);
1130     if (ta == tarval_b_true) {
1131       set_Tuple_pred(n, pn_Cond_false, new_Bad());
1132       set_Tuple_pred(n, pn_Cond_true, jmp);
1133     } else {
1134       set_Tuple_pred(n, pn_Cond_false, jmp);
1135       set_Tuple_pred(n, pn_Cond_true, new_Bad());
1136     }
1137     /* We might generate an endless loop, so keep it alive. */
1138     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
1139   } else if ((ta != tarval_bad) &&
1140              (get_irn_mode(a) == mode_Iu) &&
1141              (get_Cond_kind(n) == dense) &&
1142              (get_opt_unreachable_code())) {
1143     /* I don't want to allow Tuples smaller than the biggest Proj.
1144        Also this tuple might get really big...
1145        I generate the Jmp here, and remember it in link.  Link is used
1146        when optimizing Proj. */
1147     set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_Block(n)));
1148     /* We might generate an endless loop, so keep it alive. */
1149     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
1150   } else if ((get_irn_op(a) == op_Eor)
1151              && (get_irn_mode(a) == mode_b)
1152              && (tarval_classify(computed_value(get_Eor_right(a))) == TV_CLASSIFY_ONE)) {
1153     /* The Eor is a negate.  Generate a new Cond without the negate,
1154        simulate the negate by exchanging the results. */
1155     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
1156                                get_Eor_left(a)));
1157   } else if ((get_irn_op(a) == op_Not)
1158              && (get_irn_mode(a) == mode_b)) {
1159     /* A Not before the Cond.  Generate a new Cond without the Not,
1160        simulate the Not by exchanging the results. */
1161     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
1162                                get_Not_op(a)));
1163   }
1164   return n;
1165 }
1166
1167 static ir_node *transform_node_Eor(ir_node *n)
1168 {
1169   ir_node *a = get_Eor_left(n);
1170   ir_node *b = get_Eor_right(n);
1171
1172   if ((get_irn_mode(n) == mode_b)
1173       && (get_irn_op(a) == op_Proj)
1174       && (get_irn_mode(a) == mode_b)
1175       && (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE)
1176       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1177     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1178     n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
1179                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1180   else if ((get_irn_mode(n) == mode_b)
1181            && (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE))
1182     /* The Eor is a Not. Replace it by a Not. */
1183     /*   ????!!!Extend to bitfield 1111111. */
1184     n = new_r_Not(current_ir_graph, get_nodes_Block(n), a, mode_b);
1185
1186   return n;
1187 }
1188
1189 static ir_node *transform_node_Not(ir_node *n)
1190 {
1191   ir_node *a = get_Not_op(n);
1192
1193   if (   (get_irn_mode(n) == mode_b)
1194       && (get_irn_op(a) == op_Proj)
1195       && (get_irn_mode(a) == mode_b)
1196       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1197     /* We negate a Cmp. The Cmp has the negated result anyways! */
1198     n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
1199                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1200
1201   return n;
1202 }
1203
1204
1205 /**
1206  * Tries several [inplace] [optimizing] transformations and returns an
1207  * equivalent node.  The difference to equivalent_node() is that these
1208  * transformations _do_ generate new nodes, and thus the old node must
1209  * not be freed even if the equivalent node isn't the old one.
1210  */
1211 static ir_node *transform_node(ir_node *n)
1212 {
1213   if (n->op->transform_node)
1214     n = n->op->transform_node(n);
1215   return n;
1216 }
1217
1218 /**
1219  * set the default transform node operation
1220  */
1221 static ir_op *firm_set_default_transform_node(ir_op *op)
1222 {
1223 #define CASE(a)                                 \
1224   case iro_##a:                                 \
1225     op->transform_node  = transform_node_##a;   \
1226     break
1227
1228   switch (op->code) {
1229   CASE(Div);
1230   CASE(Mod);
1231   CASE(DivMod);
1232   CASE(Cond);
1233   CASE(Eor);
1234   CASE(Not);
1235   default:
1236     op->transform_node  = NULL;
1237   }
1238
1239   return op;
1240 #undef CASE
1241 }
1242
1243
1244 /* **************** Common Subexpression Elimination **************** */
1245
1246 /** The size of the hash table used, should estimate the number of nodes
1247     in a graph. */
1248 #define N_IR_NODES 512
1249
1250 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
1251 {
1252   return (get_Const_tarval(a) != get_Const_tarval(b))
1253       || (get_Const_type(a) != get_Const_type(b));
1254 }
1255
1256 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
1257 {
1258     return get_irn_proj_attr (a) != get_irn_proj_attr (b);
1259 }
1260
1261 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
1262 {
1263     return get_Filter_proj(a) != get_Filter_proj(b);
1264 }
1265
1266 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
1267 {
1268     return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
1269       || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
1270 }
1271
1272 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
1273 {
1274     return (get_irn_free_attr(a) != get_irn_free_attr(b));
1275 }
1276
1277 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
1278 {
1279     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
1280       || (get_irn_symconst_attr(a).tori.typ != get_irn_symconst_attr(b).tori.typ);
1281 }
1282
1283 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
1284 {
1285     return (get_irn_call_attr(a) != get_irn_call_attr(b));
1286 }
1287
1288 static int node_cmp_attr_FuncCall(ir_node *a, ir_node *b)
1289 {
1290     return (get_irn_funccall_attr(a) != get_irn_funccall_attr(b));
1291 }
1292
1293 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
1294 {
1295     return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
1296       || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
1297       || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
1298       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
1299       || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
1300 }
1301
1302 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
1303 {
1304     return get_irn_phi_attr (a) != get_irn_phi_attr (b);
1305 }
1306
1307 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
1308 {
1309     return get_Cast_type(a) != get_Cast_type(b);
1310 }
1311
1312 /**
1313  * set the default node attribute compare operation
1314  */
1315 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
1316 {
1317 #define CASE(a)                                 \
1318   case iro_##a:                                 \
1319     op->node_cmp_attr  = node_cmp_attr_##a;     \
1320     break
1321
1322   switch (op->code) {
1323   CASE(Const);
1324   CASE(Proj);
1325   CASE(Filter);
1326   CASE(Alloc);
1327   CASE(Free);
1328   CASE(SymConst);
1329   CASE(Call);
1330   CASE(FuncCall);
1331   CASE(Sel);
1332   CASE(Phi);
1333   CASE(Cast);
1334   default:
1335     op->node_cmp_attr  = NULL;
1336   }
1337
1338   return op;
1339 #undef CASE
1340 }
1341
1342 /**
1343  * Compare function for two nodes in the hash table. Gets two
1344  * nodes as parameters.  Returns 0 if the nodes are a cse.
1345  */
1346 static int
1347 vt_cmp (const void *elt, const void *key)
1348 {
1349   ir_node *a, *b;
1350   int i, irn_arity_a;
1351
1352   a = (void *)elt;
1353   b = (void *)key;
1354
1355   if (a == b) return 0;
1356
1357   if ((get_irn_op(a) != get_irn_op(b)) ||
1358       (get_irn_mode(a) != get_irn_mode(b))) return 1;
1359
1360   /* compare if a's in and b's in are equal */
1361   irn_arity_a = get_irn_arity (a);
1362   if (irn_arity_a != get_irn_arity(b))
1363     return 1;
1364
1365   /* for block-local cse and pinned nodes: */
1366   if (!get_opt_global_cse() || (get_op_pinned(get_irn_op(a)) == pinned)) {
1367     if (get_irn_n(a, -1) != get_irn_n(b, -1))
1368       return 1;
1369   }
1370
1371   /* compare a->in[0..ins] with b->in[0..ins] */
1372   for (i = 0; i < irn_arity_a; i++)
1373     if (get_irn_n(a, i) != get_irn_n(b, i))
1374       return 1;
1375
1376   /*
1377    * here, we already now that the nodes are identical except their
1378    * attributes
1379    */
1380   if (a->op->node_cmp_attr)
1381     return a->op->node_cmp_attr(a, b);
1382
1383   return 0;
1384 }
1385
1386 /**
1387  * Calculate a hash value of a node.
1388  */
1389 static unsigned
1390 ir_node_hash (ir_node *node)
1391 {
1392   unsigned h;
1393   int i, irn_arity;
1394
1395   /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
1396   h = irn_arity = get_irn_arity(node);
1397
1398   /* consider all in nodes... except the block. */
1399   for (i = 0;  i < irn_arity;  i++) {
1400     h = 9*h + (unsigned long)get_irn_n(node, i);
1401   }
1402
1403   /* ...mode,... */
1404   h = 9*h + (unsigned long) get_irn_mode (node);
1405   /* ...and code */
1406   h = 9*h + (unsigned long) get_irn_op (node);
1407
1408   return h;
1409 }
1410
1411 pset *
1412 new_identities (void)
1413 {
1414   return new_pset (vt_cmp, N_IR_NODES);
1415 }
1416
1417 void
1418 del_identities (pset *value_table)
1419 {
1420   del_pset (value_table);
1421 }
1422
1423 /**
1424  * Return the canonical node computing the same value as n.
1425  * Looks up the node in a hash table.
1426  */
1427 static INLINE ir_node *
1428 identify (pset *value_table, ir_node *n)
1429 {
1430   ir_node *o = NULL;
1431
1432   if (!value_table) return n;
1433
1434   /* TODO: use a generic commutative attribute */
1435   if (get_opt_reassociation()) {
1436     if (is_op_commutative(get_irn_op(n))) {
1437       /* for commutative operators perform  a OP b == b OP a */
1438       if (get_binop_left(n) > get_binop_right(n)) {
1439         ir_node *h = get_binop_left(n);
1440         set_binop_left(n, get_binop_right(n));
1441         set_binop_right(n, h);
1442       }
1443     }
1444   }
1445
1446   o = pset_find (value_table, n, ir_node_hash (n));
1447   if (!o) return n;
1448
1449   return o;
1450 }
1451
1452 /**
1453  * During construction we set the pinned flag in the graph right when the
1454  * optimizatin is performed.  The flag turning on procedure global cse could
1455  * be changed between two allocations.  This way we are safe.
1456  */
1457 static INLINE ir_node *
1458 identify_cons (pset *value_table, ir_node *n) {
1459   ir_node *old = n;
1460   n = identify(value_table, n);
1461   if (get_irn_n(old, -1) != get_irn_n(n, -1))
1462     set_irg_pinned(current_ir_graph, floats);
1463   return n;
1464 }
1465
1466 /**
1467  * Return the canonical node computing the same value as n.
1468  * Looks up the node in a hash table, enters it in the table
1469  * if it isn't there yet.
1470  */
1471 static ir_node *
1472 identify_remember (pset *value_table, ir_node *node)
1473 {
1474   ir_node *o = NULL;
1475
1476   if (!value_table) return node;
1477
1478   /* lookup or insert in hash table with given hash key. */
1479   o = pset_insert (value_table, node, ir_node_hash (node));
1480
1481   if (o == node) return node;
1482
1483   return o;
1484 }
1485
1486 void
1487 add_identities (pset *value_table, ir_node *node) {
1488   identify_remember (value_table, node);
1489 }
1490
1491 /**
1492  * garbage in, garbage out. If a node has a dead input, i.e., the
1493  * Bad node is input to the node, return the Bad node.
1494  */
1495 static INLINE ir_node *
1496 gigo (ir_node *node)
1497 {
1498   int i, irn_arity;
1499   ir_op* op = get_irn_op(node);
1500
1501   /* remove garbage blocks by looking at control flow that leaves the block
1502      and replacing the control flow by Bad. */
1503   if (get_irn_mode(node) == mode_X) {
1504     ir_node *block = get_nodes_block(node);
1505     if (op == op_End) return node;     /* Don't optimize End, may have Bads. */
1506     if (get_irn_op(block) == op_Block && get_Block_matured(block)) {
1507       irn_arity = get_irn_arity(block);
1508       for (i = 0; i < irn_arity; i++) {
1509         if (!is_Bad(get_irn_n(block, i))) break;
1510       }
1511       if (i == irn_arity) return new_Bad();
1512     }
1513   }
1514
1515   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
1516      blocks predecessors is dead. */
1517   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
1518     irn_arity = get_irn_arity(node);
1519     for (i = -1; i < irn_arity; i++) {
1520       if (is_Bad(get_irn_n(node, i))) {
1521         return new_Bad();
1522       }
1523     }
1524   }
1525 #if 0
1526   /* With this code we violate the agreement that local_optimize
1527      only leaves Bads in Block, Phi and Tuple nodes. */
1528   /* If Block has only Bads as predecessors it's garbage. */
1529   /* If Phi has only Bads as predecessors it's garbage. */
1530   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
1531     irn_arity = get_irn_arity(node);
1532     for (i = 0; i < irn_arity; i++) {
1533       if (!is_Bad(get_irn_n(node, i))) break;
1534     }
1535     if (i == irn_arity) node = new_Bad();
1536   }
1537 #endif
1538   return node;
1539 }
1540
1541
1542 /**
1543  * These optimizations deallocate nodes from the obstack.
1544  * It can only be called if it is guaranteed that no other nodes
1545  * reference this one, i.e., right after construction of a node.
1546  */
1547 ir_node *
1548 optimize_node (ir_node *n)
1549 {
1550   tarval *tv;
1551   ir_node *oldn = n;
1552   opcode iro = get_irn_opcode(n);
1553
1554   /* Allways optimize Phi nodes: part of the construction. */
1555   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
1556
1557   /* constant expression evaluation / constant folding */
1558   if (get_opt_constant_folding()) {
1559     /* constants can not be evaluated */
1560     if (iro != iro_Const) {
1561       /* try to evaluate */
1562       tv = computed_value (n);
1563       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
1564         /*
1565          * we MUST copy the node here temparary, because it's still needed
1566          * for DBG_OPT_ALGSIM0
1567          */
1568         ir_node x = *n;
1569         oldn = &x;
1570         /* evaluation was successful -- replace the node. */
1571         obstack_free (current_ir_graph->obst, n);
1572         n = new_Const (get_tarval_mode (tv), tv);
1573                                                         DBG_OPT_ALGSIM0;
1574         return n;
1575       }
1576     }
1577   }
1578
1579   /* remove unnecessary nodes */
1580   if (get_opt_constant_folding() ||
1581       (iro == iro_Phi)  ||   /* always optimize these nodes. */
1582       (iro == iro_Id)   ||
1583       (iro == iro_Proj) ||
1584       (iro == iro_Block)  )  /* Flags tested local. */
1585     n = equivalent_node (n);
1586
1587   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
1588
1589   /** common subexpression elimination **/
1590   /* Checks whether n is already available. */
1591   /* The block input is used to distinguish different subexpressions. Right
1592      now all nodes are pinned to blocks, i.e., the cse only finds common
1593      subexpressions within a block. */
1594   if (get_opt_cse())
1595     n = identify_cons (current_ir_graph->value_table, n);
1596
1597   if (n != oldn) {
1598     /* We found an existing, better node, so we can deallocate the old node. */
1599     obstack_free (current_ir_graph->obst, oldn);
1600
1601     return n;
1602   }
1603
1604   /* Some more constant expression evaluation that does not allow to
1605      free the node. */
1606   iro = get_irn_opcode(n);
1607   if (get_opt_constant_folding() ||
1608       (iro == iro_Cond) ||
1609       (iro == iro_Proj))     /* Flags tested local. */
1610     n = transform_node (n);
1611
1612   /* Remove nodes with dead (Bad) input.
1613      Run always for transformation induced Bads. */
1614   n = gigo (n);
1615
1616   /* Now we have a legal, useful node. Enter it in hash table for cse */
1617   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
1618     n = identify_remember (current_ir_graph->value_table, n);
1619   }
1620
1621   return n;
1622 }
1623
1624
1625 /**
1626  * These optimizations never deallocate nodes.  This can cause dead
1627  * nodes lying on the obstack.  Remove these by a dead node elimination,
1628  * i.e., a copying garbage collection.
1629  */
1630 ir_node *
1631 optimize_in_place_2 (ir_node *n)
1632 {
1633   tarval *tv;
1634   ir_node *oldn = n;
1635   opcode iro = get_irn_opcode(n);
1636
1637   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
1638
1639   /* if not optimize return n */
1640   if (n == NULL) {
1641     assert(0);
1642     /* Here this is possible.  Why? */
1643     return n;
1644   }
1645
1646
1647   /* constant expression evaluation / constant folding */
1648   if (get_opt_constant_folding()) {
1649     /* constants can not be evaluated */
1650     if (iro != iro_Const) {
1651       /* try to evaluate */
1652       tv = computed_value (n);
1653       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
1654         /* evaluation was successful -- replace the node. */
1655         n = new_Const (get_tarval_mode (tv), tv);
1656                                                 DBG_OPT_ALGSIM0;
1657         return n;
1658       }
1659     }
1660   }
1661
1662   /* remove unnecessary nodes */
1663   /*if (get_opt_constant_folding()) */
1664   if (get_opt_constant_folding() ||
1665       (iro == iro_Phi)  ||   /* always optimize these nodes. */
1666       (iro == iro_Id)   ||   /* ... */
1667       (iro == iro_Proj) ||   /* ... */
1668       (iro == iro_Block)  )  /* Flags tested local. */
1669     n = equivalent_node (n);
1670
1671   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
1672
1673   /** common subexpression elimination **/
1674   /* Checks whether n is already available. */
1675   /* The block input is used to distinguish different subexpressions.  Right
1676      now all nodes are pinned to blocks, i.e., the cse only finds common
1677      subexpressions within a block. */
1678   if (get_opt_cse()) {
1679     n = identify (current_ir_graph->value_table, n);
1680   }
1681
1682   /* Some more constant expression evaluation. */
1683   iro = get_irn_opcode(n);
1684   if (get_opt_constant_folding() ||
1685       (iro == iro_Cond) ||
1686       (iro == iro_Proj))     /* Flags tested local. */
1687     n = transform_node (n);
1688
1689   /* Remove nodes with dead (Bad) input.
1690      Run always for transformation induced Bads.  */
1691   n = gigo (n);
1692
1693   /* Now we can verify the node, as it has no dead inputs any more. */
1694   irn_vrfy(n);
1695
1696   /* Now we have a legal, useful node. Enter it in hash table for cse.
1697      Blocks should be unique anyways.  (Except the successor of start:
1698      is cse with the start block!) */
1699   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
1700     n = identify_remember (current_ir_graph->value_table, n);
1701
1702   return n;
1703 }
1704
1705 /**
1706  * Wrapper for external use, set proper status bits after optimization.
1707  */
1708 ir_node *
1709 optimize_in_place (ir_node *n)
1710 {
1711   /* Handle graph state */
1712   assert(get_irg_phase_state(current_ir_graph) != phase_building);
1713
1714   if (get_opt_global_cse())
1715     set_irg_pinned(current_ir_graph, floats);
1716   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1717     set_irg_outs_inconsistent(current_ir_graph);
1718   /* Maybe we could also test whether optimizing the node can
1719      change the control graph. */
1720   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1721     set_irg_dom_inconsistent(current_ir_graph);
1722   return optimize_in_place_2 (n);
1723 }
1724
1725 /**
1726  * set the default ir op operations
1727  */
1728 ir_op *firm_set_default_operations(ir_op *op)
1729 {
1730   op = firm_set_default_computed_value(op);
1731   op = firm_set_default_equivalent_node(op);
1732   op = firm_set_default_transform_node(op);
1733   op = firm_set_default_node_cmp_attr(op);
1734
1735   return op;
1736 }