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