01ca74b4587c9a248fcbead1055d1cd8edf05f30
[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       (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp) &&
499       (get_opt_control_flow_straightening())) {
500     n = get_nodes_Block(get_Block_cfgpred(n, 0));
501     if (n == oldn) {
502       /* Jmp jumps into the block it is in -- deal self cycle. */
503       n = new_Bad();
504     } else {
505                                                           DBG_OPT_STG;
506     }
507
508   } else if ((get_Block_n_cfgpreds(n) == 2) &&
509              (get_opt_control_flow_weak_simplification())) {
510     /* Test whether Cond jumps twice to this block
511        @@@ we could do this also with two loops finding two preds from several ones. */
512     ir_node *a = get_Block_cfgpred(n, 0);
513     ir_node *b = get_Block_cfgpred(n, 1);
514
515     if ((get_irn_op(a) == op_Proj) &&
516         (get_irn_op(b) == op_Proj) &&
517         (get_Proj_pred(a) == get_Proj_pred(b)) &&
518         (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
519         (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
520       /* Also a single entry Block following a single exit Block.  Phis have
521          twice the same operand and will be optimized away. */
522       n = get_nodes_Block(a);                                         DBG_OPT_IFSIM;
523     }
524   } else if (get_opt_unreachable_code() &&
525              (n != current_ir_graph->start_block) &&
526              (n != current_ir_graph->end_block)     ) {
527     int i;
528     /* If all inputs are dead, this block is dead too, except if it is
529        the start or end block.  This is a step of unreachable code
530        elimination */
531     for (i = 0; i < get_Block_n_cfgpreds(n); i++) {
532       if (!is_Bad(get_Block_cfgpred(n, i))) break;
533     }
534     if (i == get_Block_n_cfgpreds(n))
535       n = new_Bad();
536   }
537
538   return n;
539 }
540
541 static ir_node *equivalent_node_Jmp(ir_node *n)
542 {
543   /* GL: Why not same for op_Raise?? */
544   /* unreachable code elimination */
545   if (is_Bad(get_nodes_Block(n)))
546     n = new_Bad();
547
548   return n;
549 }
550
551 static ir_node *equivalent_node_Cond(ir_node *n)
552 {
553   /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
554      See cases for iro_Cond and iro_Proj in transform_node. */
555   return n;
556 }
557
558 static ir_node *equivalent_node_Or(ir_node *n)
559 {
560   ir_node *oldn = n;
561
562   ir_node *a = get_Or_left(n);
563   ir_node *b = get_Or_right(n);
564
565   /* remove a v a */
566   if (a == b) {
567     n = a;                                                             DBG_OPT_ALGSIM1;
568   }
569
570   return n;
571 }
572
573 /**
574  * optimize operations that are commutative and have neutral 0.
575  */
576 static ir_node *equivalent_node_neutral_zero(ir_node *n)
577 {
578   ir_node *oldn = n;
579
580   ir_node *a = get_binop_left(n);
581   ir_node *b = get_binop_right(n);
582
583   tarval *tv;
584   ir_node *on;
585
586   /* After running compute_node there is only one constant predecessor.
587      Find this predecessors value and remember the other node: */
588   if ((tv = computed_value (a)) != tarval_bad) {
589     on = b;
590   } else if ((tv = computed_value (b)) != tarval_bad) {
591     on = a;
592   } else
593     return n;
594
595   /* If this predecessors constant value is zero, the operation is
596      unnecessary. Remove it: */
597   if (tarval_classify (tv) == TV_CLASSIFY_NULL) {
598     n = on;                                                             DBG_OPT_ALGSIM1;
599   }
600
601   return n;
602 }
603
604 static ir_node *equivalent_node_Add(ir_node *n)
605 {
606   return equivalent_node_neutral_zero(n);
607 }
608
609 static ir_node *equivalent_node_Eor(ir_node *n)
610 {
611   return equivalent_node_neutral_zero(n);
612 }
613
614 /**
615  * optimize operations that are not commutative but have neutral 0 on left.
616  * Test only one predecessor.
617  */
618 static ir_node *equivalent_node_left_zero(ir_node *n)
619 {
620   ir_node *oldn = n;
621
622   ir_node *a = get_binop_left(n);
623   ir_node *b = get_binop_right(n);
624
625   if (tarval_classify (computed_value (b)) == TV_CLASSIFY_NULL) {
626     n = a;                                                              DBG_OPT_ALGSIM1;
627   }
628
629   return n;
630 }
631
632 static ir_node *equivalent_node_Sub(ir_node *n)
633 {
634   return equivalent_node_left_zero(n);
635 }
636
637 static ir_node *equivalent_node_Shl(ir_node *n)
638 {
639   return equivalent_node_left_zero(n);
640 }
641
642 static ir_node *equivalent_node_Shr(ir_node *n)
643 {
644   return equivalent_node_left_zero(n);
645 }
646
647 static ir_node *equivalent_node_Shrs(ir_node *n)
648 {
649   return equivalent_node_left_zero(n);
650 }
651
652 static ir_node *equivalent_node_Rot(ir_node *n)
653 {
654   return equivalent_node_left_zero(n);
655 }
656
657 static ir_node *equivalent_node_symmetric_unop(ir_node *n)
658 {
659   ir_node *oldn = n;
660
661   /* optimize symmetric unop */
662   if (get_irn_op(get_unop_op(n)) == get_irn_op(n)) {
663     n = get_unop_op(get_unop_op(n));                                    DBG_OPT_ALGSIM2;
664   }
665   return n;
666 }
667
668 static ir_node *equivalent_node_Not(ir_node *n)
669 {
670   /* NotNot x == x */
671   return equivalent_node_symmetric_unop(n);
672 }
673
674 static ir_node *equivalent_node_Minus(ir_node *n)
675 {
676   /* --x == x */  /* ??? Is this possible or can --x raise an
677                          out of bounds exception if min =! max? */
678   return equivalent_node_symmetric_unop(n);
679 }
680
681 static ir_node *equivalent_node_Mul(ir_node *n)
682 {
683   ir_node *oldn = n;
684
685   ir_node *a = get_Mul_left(n);
686   ir_node *b = get_Mul_right(n);
687
688   /* Mul is commutative and has again an other neutral element. */
689   if (tarval_classify (computed_value (a)) == TV_CLASSIFY_ONE) {
690     n = b;                                                              DBG_OPT_ALGSIM1;
691   } else if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE) {
692     n = a;                                                              DBG_OPT_ALGSIM1;
693   }
694   return n;
695 }
696
697 static ir_node *equivalent_node_Div(ir_node *n)
698 {
699   ir_node *a = get_Div_left(n);
700   ir_node *b = get_Div_right(n);
701
702   /* Div is not commutative. */
703   if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
704     /* Turn Div into a tuple (mem, bad, a) */
705     ir_node *mem = get_Div_mem(n);
706     turn_into_tuple(n, 3);
707     set_Tuple_pred(n, pn_Div_M,        mem);
708     set_Tuple_pred(n, pn_Div_X_except, new_Bad());      /* no exception */
709     set_Tuple_pred(n, pn_Div_res,      a);
710   }
711   return n;
712 }
713
714 static ir_node *equivalent_node_And(ir_node *n)
715 {
716   ir_node *oldn = n;
717
718   ir_node *a = get_And_left(n);
719   ir_node *b = get_And_right(n);
720
721   if (a == b) {
722     n = a;    /* And has it's own neutral element */
723   } else if (tarval_classify (computed_value (a)) == TV_CLASSIFY_ALL_ONE) {
724     n = b;
725   } else if (tarval_classify (computed_value (b)) == TV_CLASSIFY_ALL_ONE) {
726     n = a;
727   }
728   if (n != oldn)                                                        DBG_OPT_ALGSIM1;
729   return n;
730 }
731
732 static ir_node *equivalent_node_Conv(ir_node *n)
733 {
734   ir_node *oldn = n;
735   ir_node *a = get_Conv_op(n);
736   ir_node *b;
737
738   ir_mode *n_mode = get_irn_mode(n);
739   ir_mode *a_mode = get_irn_mode(a);
740
741   if (n_mode == a_mode) { /* No Conv necessary */
742     n = a;                                                              DBG_OPT_ALGSIM3;
743   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
744     ir_mode *b_mode;
745
746     b = get_Conv_op(a);
747     n_mode = get_irn_mode(n);
748     b_mode = get_irn_mode(b);
749
750     if (n_mode == b_mode) {
751       if (n_mode == mode_b) {
752         n = b;  /* Convb(Conv*(xxxb(...))) == xxxb(...) */              DBG_OPT_ALGSIM1;
753       }
754       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
755         if (smaller_mode(b_mode, a_mode)){
756           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */      DBG_OPT_ALGSIM1;
757         }
758       }
759     }
760   }
761   return n;
762 }
763
764 static ir_node *equivalent_node_Phi(ir_node *n)
765 {
766   /* Several optimizations:
767      - no Phi in start block.
768      - remove Id operators that are inputs to Phi
769      - fold Phi-nodes, iff they have only one predecessor except
770              themselves.
771   */
772   int i, n_preds;
773
774   ir_node *oldn = n;
775   ir_node *block = NULL;     /* to shutup gcc */
776   ir_node *first_val = NULL; /* to shutup gcc */
777   ir_node *scnd_val = NULL;  /* to shutup gcc */
778
779   if (!get_opt_normalize()) return n;
780
781   n_preds = get_Phi_n_preds(n);
782
783   block = get_nodes_Block(n);
784   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
785      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
786   if ((is_Bad(block)) ||                         /* Control dead */
787       (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
788     return new_Bad();                        /* in the Start Block. */
789
790   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
791
792 #if 0
793   /* first we test for a special case: */
794   /* Confirm is a special node fixing additional information for a
795      value that is known at a certain point.  This is useful for
796      dataflow analysis. */
797   if (n_preds == 2) {
798     ir_node *a = follow_Id (get_Phi_pred(n, 0));
799     ir_node *b = follow_Id (get_Phi_pred(n, 1));
800     if (   (get_irn_op(a) == op_Confirm)
801         && (get_irn_op(b) == op_Confirm)
802         && follow_Id (get_irn_n(a, 0) == get_irn_n(b, 0))
803         && (get_irn_n(a, 1) == get_irn_n (b, 1))
804         && (a->data.num == (~b->data.num & irpn_True) )) {
805       return get_irn_n(a, 0);
806     }
807   }
808 #endif
809
810   /* Find first non-self-referencing input */
811   for (i = 0;  i < n_preds;  ++i) {
812     first_val = follow_Id(get_Phi_pred(n, i));
813     /* skip Id's */
814     set_Phi_pred(n, i, first_val);
815     if (   (first_val != n)                            /* not self pointer */
816         && (get_irn_op(first_val) != op_Bad)           /* value not dead */
817         && !(is_Bad (get_Block_cfgpred(block, i))) ) { /* not dead control flow */
818       break;                         /* then found first value. */
819     }
820   }
821
822   /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
823   if (i >= n_preds) { return new_Bad(); }
824
825   scnd_val = NULL;
826
827   /* follow_Id () for rest of inputs, determine if any of these
828      are non-self-referencing */
829   while (++i < n_preds) {
830     scnd_val = follow_Id(get_Phi_pred(n, i));
831     /* skip Id's */
832     set_Phi_pred(n, i, scnd_val);
833     if (   (scnd_val != n)
834         && (scnd_val != first_val)
835         && (get_irn_op(scnd_val) != op_Bad)
836         && !(is_Bad (get_Block_cfgpred(block, i))) ) {
837       break;
838     }
839   }
840
841   /* Fold, if no multiple distinct non-self-referencing inputs */
842   if (i >= n_preds) {
843     n = first_val;                                     DBG_OPT_PHI;
844   } else {
845     /* skip the remaining Ids. */
846     while (++i < n_preds) {
847       set_Phi_pred(n, i, follow_Id(get_Phi_pred(n, i)));
848     }
849   }
850   return n;
851 }
852
853 static ir_node *equivalent_node_Load(ir_node *n)
854 {
855 #if 0  /* Is an illegal transformation: different nodes can
856           represent the same pointer value!! */
857  ir_node *a = skip_Proj(get_Load_mem(n));
858  ir_node *b = get_Load_ptr(n);
859
860  if (get_irn_op(a) == op_Store) {
861    if ( different_identity (b, get_Store_ptr(a))) {
862          /* load and store use different pointers, therefore load
863                 needs not take store's memory but the state before. */
864          set_Load_mem (n, get_Store_mem(a));
865    } else if (( 0 /* ???didn't get cryptic test that returns 0 */ )) {
866    }
867  }
868 #endif
869  return n;
870 }
871
872 static ir_node *equivalent_node_Store(ir_node *n)
873 {
874   ir_node *oldn = n;
875
876   /* remove unnecessary store. */
877   ir_node *a = skip_Proj(get_Store_mem(n));
878   ir_node *b = get_Store_ptr(n);
879   ir_node *c = skip_Proj(get_Store_value(n));
880
881   if (get_irn_op(a) == op_Store
882       && get_Store_ptr(a) == b
883       && skip_Proj(get_Store_value(a)) == c) {
884     /* We have twice exactly the same store -- a write after write. */
885     n = a;                                                         DBG_OPT_WAW;
886   } else if (get_irn_op(c) == op_Load
887              && (a == c || skip_Proj(get_Load_mem(c)) == a)
888              && get_Load_ptr(c) == b ) {
889     /* We just loaded the value from the same memory, i.e., the store
890        doesn't change the memory -- a write after read. */
891     a = get_Store_mem(n);
892     turn_into_tuple(n, 2);
893     set_Tuple_pred(n, pn_Store_M,        a);
894     set_Tuple_pred(n, pn_Store_X_except, new_Bad());               DBG_OPT_WAR;
895   }
896   return n;
897 }
898
899 static ir_node *equivalent_node_Proj(ir_node *n)
900 {
901   ir_node *oldn = n;
902
903   ir_node *a = get_Proj_pred(n);
904
905   if ( get_irn_op(a) == op_Tuple) {
906     /* Remove the Tuple/Proj combination. */
907     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
908       n = get_Tuple_pred(a, get_Proj_proj(n));                     DBG_OPT_TUPLE;
909     } else {
910       assert(0); /* This should not happen! */
911       n = new_Bad();
912     }
913   } else if (get_irn_mode(n) == mode_X &&
914              is_Bad(get_nodes_Block(n))) {
915     /* Remove dead control flow -- early gigo. */
916     n = new_Bad();
917   }
918   return n;
919 }
920
921 static ir_node *equivalent_node_Id(ir_node *n)
922 {
923   ir_node *oldn = n;
924
925   n = follow_Id (n);                                                 DBG_OPT_ID;
926   return n;
927 }
928
929 /*
930 case iro_Mod, Quot, DivMod
931   DivMod allocates new nodes --> it's treated in transform node.
932   What about Quot, DivMod?
933 */
934
935 /**
936  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
937  * perform no actual computation, as, e.g., the Id nodes.  It does not create
938  * new nodes.  It is therefore safe to free n if the node returned is not n.
939  * If a node returns a Tuple we can not just skip it.  If the size of the
940  * in array fits, we transform n into a tuple (e.g., Div).
941  */
942 ir_node *
943 equivalent_node (ir_node *n)
944 {
945   if (n->op->equivalent_node)
946     return n->op->equivalent_node(n);
947   return n;
948 }
949
950 /**
951  * set the default equivalent node operation
952  */
953 static ir_op *firm_set_default_equivalent_node(ir_op *op)
954 {
955 #define CASE(a)                                 \
956   case iro_##a:                                 \
957     op->equivalent_node  = equivalent_node_##a; \
958     break
959
960   switch (op->code) {
961   CASE(Block);
962   CASE(Jmp);
963   CASE(Cond);
964   CASE(Or);
965   CASE(Add);
966   CASE(Eor);
967   CASE(Sub);
968   CASE(Shl);
969   CASE(Shr);
970   CASE(Shrs);
971   CASE(Rot);
972   CASE(Not);
973   CASE(Minus);
974   CASE(Mul);
975   CASE(Div);
976   CASE(And);
977   CASE(Conv);
978   CASE(Phi);
979   CASE(Load);
980   CASE(Store);
981   CASE(Proj);
982   CASE(Id);
983   default:
984     op->equivalent_node  = NULL;
985   }
986
987   return op;
988 #undef CASE
989 }
990
991 /**
992  * Do node specific optimizations of nodes predecessors.
993  */
994 static void
995 optimize_preds(ir_node *n) {
996   ir_node *a = NULL, *b = NULL;
997
998   /* get the operands we will work on for simple cases. */
999   if (is_binop(n)) {
1000     a = get_binop_left(n);
1001     b = get_binop_right(n);
1002   } else if (is_unop(n)) {
1003     a = get_unop_op(n);
1004   }
1005
1006   switch (get_irn_opcode(n)) {
1007
1008   case iro_Cmp:
1009     /* We don't want Cast as input to Cmp. */
1010     if (get_irn_op(a) == op_Cast) {
1011       a = get_Cast_op(a);
1012       set_Cmp_left(n, a);
1013     }
1014     if (get_irn_op(b) == op_Cast) {
1015       b = get_Cast_op(b);
1016       set_Cmp_right(n, b);
1017     }
1018     break;
1019
1020   default: break;
1021   } /* end switch */
1022 }
1023
1024 static ir_node *transform_node_Div(ir_node *n)
1025 {
1026   tarval *ta = computed_value(n);
1027
1028   if (ta != tarval_bad) {
1029     /* Turn Div into a tuple (mem, bad, value) */
1030     ir_node *mem = get_Div_mem(n);
1031
1032     turn_into_tuple(n, 3);
1033     set_Tuple_pred(n, pn_Div_M, mem);
1034     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1035     set_Tuple_pred(n, pn_Div_res, new_Const(get_tarval_mode(ta), ta));
1036   }
1037   return n;
1038 }
1039
1040 static ir_node *transform_node_Mod(ir_node *n)
1041 {
1042   tarval *ta = computed_value(n);
1043
1044   if (ta != tarval_bad) {
1045     /* Turn Mod into a tuple (mem, bad, value) */
1046     ir_node *mem = get_Mod_mem(n);
1047     turn_into_tuple(n, 3);
1048     set_Tuple_pred(n, pn_Mod_M, mem);
1049     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1050     set_Tuple_pred(n, pn_Mod_res, new_Const(get_tarval_mode(ta), ta));
1051   }
1052   return n;
1053 }
1054
1055 static ir_node *transform_node_DivMod(ir_node *n)
1056 {
1057   int evaluated = 0;
1058
1059   ir_node *a = get_DivMod_left(n);
1060   ir_node *b = get_DivMod_right(n);
1061   ir_mode *mode = get_irn_mode(a);
1062
1063   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1064     return n;
1065
1066   if (a == b) {
1067     a = new_Const(mode, get_mode_one(mode));
1068     b = new_Const(mode, get_mode_null(mode));
1069     evaluated = 1;
1070   } else {
1071     tarval *ta = value_of(a);
1072     tarval *tb = value_of(b);
1073
1074     if (tb != tarval_bad) {
1075       if (tb == get_mode_one(get_tarval_mode(tb))) {
1076         b = new_Const (mode, get_mode_null(mode));
1077         evaluated = 1;
1078       } else if (ta != tarval_bad) {
1079         tarval *resa, *resb;
1080         resa = tarval_div (ta, tb);
1081         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1082                                           Jmp for X result!? */
1083         resb = tarval_mod (ta, tb);
1084         if (resb == tarval_bad) return n; /* Causes exception! */
1085         a = new_Const (mode, resa);
1086         b = new_Const (mode, resb);
1087         evaluated = 1;
1088       }
1089     } else if (ta == get_mode_null(mode)) {
1090       b = a;
1091       evaluated = 1;
1092     }
1093   }
1094   if (evaluated) { /* replace by tuple */
1095     ir_node *mem = get_DivMod_mem(n);
1096     turn_into_tuple(n, 4);
1097     set_Tuple_pred(n, pn_DivMod_M,        mem);
1098     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
1099     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1100     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
1101     assert(get_nodes_Block(n));
1102   }
1103
1104   return n;
1105 }
1106
1107 static ir_node *transform_node_Cond(ir_node *n)
1108 {
1109   /* Replace the Cond by a Jmp if it branches on a constant
1110      condition. */
1111   ir_node *jmp;
1112   ir_node *a = get_Cond_selector(n);
1113   tarval *ta = value_of(a);
1114
1115   if ((ta != tarval_bad) &&
1116       (get_irn_mode(a) == mode_b) &&
1117       (get_opt_unreachable_code())) {
1118     /* It's a boolean Cond, branching on a boolean constant.
1119                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1120     jmp = new_r_Jmp(current_ir_graph, get_nodes_Block(n));
1121     turn_into_tuple(n, 2);
1122     if (ta == tarval_b_true) {
1123       set_Tuple_pred(n, pn_Cond_false, new_Bad());
1124       set_Tuple_pred(n, pn_Cond_true, jmp);
1125     } else {
1126       set_Tuple_pred(n, pn_Cond_false, jmp);
1127       set_Tuple_pred(n, pn_Cond_true, new_Bad());
1128     }
1129     /* We might generate an endless loop, so keep it alive. */
1130     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
1131   } else if ((ta != tarval_bad) &&
1132              (get_irn_mode(a) == mode_Iu) &&
1133              (get_Cond_kind(n) == dense) &&
1134              (get_opt_unreachable_code())) {
1135     /* I don't want to allow Tuples smaller than the biggest Proj.
1136        Also this tuple might get really big...
1137        I generate the Jmp here, and remember it in link.  Link is used
1138        when optimizing Proj. */
1139     set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_Block(n)));
1140     /* We might generate an endless loop, so keep it alive. */
1141     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_Block(n));
1142   } else if ((get_irn_op(a) == op_Eor)
1143              && (get_irn_mode(a) == mode_b)
1144              && (tarval_classify(computed_value(get_Eor_right(a))) == TV_CLASSIFY_ONE)) {
1145     /* The Eor is a negate.  Generate a new Cond without the negate,
1146        simulate the negate by exchanging the results. */
1147     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
1148                                get_Eor_left(a)));
1149   } else if ((get_irn_op(a) == op_Not)
1150              && (get_irn_mode(a) == mode_b)) {
1151     /* A Not before the Cond.  Generate a new Cond without the Not,
1152        simulate the Not by exchanging the results. */
1153     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_Block(n),
1154                                get_Not_op(a)));
1155   }
1156   return n;
1157 }
1158
1159 static ir_node *transform_node_Eor(ir_node *n)
1160 {
1161   ir_node *a = get_Eor_left(n);
1162   ir_node *b = get_Eor_right(n);
1163
1164   if ((get_irn_mode(n) == mode_b)
1165       && (get_irn_op(a) == op_Proj)
1166       && (get_irn_mode(a) == mode_b)
1167       && (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE)
1168       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1169     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1170     n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
1171                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1172   else if ((get_irn_mode(n) == mode_b)
1173            && (tarval_classify (computed_value (b)) == TV_CLASSIFY_ONE))
1174     /* The Eor is a Not. Replace it by a Not. */
1175     /*   ????!!!Extend to bitfield 1111111. */
1176     n = new_r_Not(current_ir_graph, get_nodes_Block(n), a, mode_b);
1177
1178   return n;
1179 }
1180
1181 static ir_node *transform_node_Not(ir_node *n)
1182 {
1183   ir_node *a = get_Not_op(n);
1184
1185   if (   (get_irn_mode(n) == mode_b)
1186       && (get_irn_op(a) == op_Proj)
1187       && (get_irn_mode(a) == mode_b)
1188       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1189     /* We negate a Cmp. The Cmp has the negated result anyways! */
1190     n = new_r_Proj(current_ir_graph, get_nodes_Block(n), get_Proj_pred(a),
1191                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1192
1193   return n;
1194 }
1195
1196
1197 /**
1198  * Tries several [inplace] [optimizing] transformations and returns an
1199  * equivalent node.  The difference to equivalent_node() is that these
1200  * transformations _do_ generate new nodes, and thus the old node must
1201  * not be freed even if the equivalent node isn't the old one.
1202  */
1203 static ir_node *transform_node(ir_node *n)
1204 {
1205   if (n->op->transform_node)
1206     n = n->op->transform_node(n);
1207   return n;
1208 }
1209
1210 /**
1211  * set the default transform node operation
1212  */
1213 static ir_op *firm_set_default_transform_node(ir_op *op)
1214 {
1215 #define CASE(a)                                 \
1216   case iro_##a:                                 \
1217     op->transform_node  = transform_node_##a;   \
1218     break
1219
1220   switch (op->code) {
1221   CASE(Div);
1222   CASE(Mod);
1223   CASE(DivMod);
1224   CASE(Cond);
1225   CASE(Eor);
1226   CASE(Not);
1227   default:
1228     op->transform_node  = NULL;
1229   }
1230
1231   return op;
1232 #undef CASE
1233 }
1234
1235
1236 /* **************** Common Subexpression Elimination **************** */
1237
1238 /** The size of the hash table used, should estimate the number of nodes
1239     in a graph. */
1240 #define N_IR_NODES 512
1241
1242 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
1243 {
1244   return (get_Const_tarval(a) != get_Const_tarval(b))
1245       || (get_Const_type(a) != get_Const_type(b));
1246 }
1247
1248 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
1249 {
1250     return get_irn_proj_attr (a) != get_irn_proj_attr (b);
1251 }
1252
1253 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
1254 {
1255     return get_Filter_proj(a) != get_Filter_proj(b);
1256 }
1257
1258 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
1259 {
1260     return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
1261       || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
1262 }
1263
1264 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
1265 {
1266     return (get_irn_free_attr(a) != get_irn_free_attr(b));
1267 }
1268
1269 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
1270 {
1271     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
1272       || (get_irn_symconst_attr(a).tori.typ != get_irn_symconst_attr(b).tori.typ);
1273 }
1274
1275 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
1276 {
1277     return (get_irn_call_attr(a) != get_irn_call_attr(b));
1278 }
1279
1280 static int node_cmp_attr_FuncCall(ir_node *a, ir_node *b)
1281 {
1282     return (get_irn_funccall_attr(a) != get_irn_funccall_attr(b));
1283 }
1284
1285 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
1286 {
1287     return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
1288       || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
1289       || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
1290       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
1291       || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
1292 }
1293
1294 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
1295 {
1296     return get_irn_phi_attr (a) != get_irn_phi_attr (b);
1297 }
1298
1299 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
1300 {
1301     return get_Cast_type(a) != get_Cast_type(b);
1302 }
1303
1304 /**
1305  * set the default node attribute compare operation
1306  */
1307 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
1308 {
1309 #define CASE(a)                                 \
1310   case iro_##a:                                 \
1311     op->node_cmp_attr  = node_cmp_attr_##a;     \
1312     break
1313
1314   switch (op->code) {
1315   CASE(Const);
1316   CASE(Proj);
1317   CASE(Filter);
1318   CASE(Alloc);
1319   CASE(Free);
1320   CASE(SymConst);
1321   CASE(Call);
1322   CASE(FuncCall);
1323   CASE(Sel);
1324   CASE(Phi);
1325   CASE(Cast);
1326   default:
1327     op->node_cmp_attr  = NULL;
1328   }
1329
1330   return op;
1331 #undef CASE
1332 }
1333
1334 /**
1335  * Compare function for two nodes in the hash table. Gets two
1336  * nodes as parameters.  Returns 0 if the nodes are a cse.
1337  */
1338 static int
1339 vt_cmp (const void *elt, const void *key)
1340 {
1341   ir_node *a, *b;
1342   int i, irn_arity_a;
1343
1344   a = (void *)elt;
1345   b = (void *)key;
1346
1347   if (a == b) return 0;
1348
1349   if ((get_irn_op(a) != get_irn_op(b)) ||
1350       (get_irn_mode(a) != get_irn_mode(b))) return 1;
1351
1352   /* compare if a's in and b's in are equal */
1353   irn_arity_a = get_irn_arity (a);
1354   if (irn_arity_a != get_irn_arity(b))
1355     return 1;
1356
1357   /* for block-local cse and pinned nodes: */
1358   if (!get_opt_global_cse() || (get_op_pinned(get_irn_op(a)) == pinned)) {
1359     if (get_irn_n(a, -1) != get_irn_n(b, -1))
1360       return 1;
1361   }
1362
1363   /* compare a->in[0..ins] with b->in[0..ins] */
1364   for (i = 0; i < irn_arity_a; i++)
1365     if (get_irn_n(a, i) != get_irn_n(b, i))
1366       return 1;
1367
1368   /*
1369    * here, we already now that the nodes are identical except their
1370    * attributes
1371    */
1372   if (a->op->node_cmp_attr)
1373     return a->op->node_cmp_attr(a, b);
1374
1375   return 0;
1376 }
1377
1378 /**
1379  * Calculate a hash value of a node.
1380  */
1381 static unsigned
1382 ir_node_hash (ir_node *node)
1383 {
1384   unsigned h;
1385   int i, irn_arity;
1386
1387   /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
1388   h = irn_arity = get_irn_arity(node);
1389
1390   /* consider all in nodes... except the block. */
1391   for (i = 0;  i < irn_arity;  i++) {
1392     h = 9*h + (unsigned long)get_irn_n(node, i);
1393   }
1394
1395   /* ...mode,... */
1396   h = 9*h + (unsigned long) get_irn_mode (node);
1397   /* ...and code */
1398   h = 9*h + (unsigned long) get_irn_op (node);
1399
1400   return h;
1401 }
1402
1403 pset *
1404 new_identities (void)
1405 {
1406   return new_pset (vt_cmp, N_IR_NODES);
1407 }
1408
1409 void
1410 del_identities (pset *value_table)
1411 {
1412   del_pset (value_table);
1413 }
1414
1415 /**
1416  * Return the canonical node computing the same value as n.
1417  * Looks up the node in a hash table.
1418  */
1419 static INLINE ir_node *
1420 identify (pset *value_table, ir_node *n)
1421 {
1422   ir_node *o = NULL;
1423
1424   if (!value_table) return n;
1425
1426   /* TODO: use a generic commutative attribute */
1427   if (get_opt_reassociation()) {
1428     if (is_op_commutative(get_irn_op(n))) {
1429       /* for commutative operators perform  a OP b == b OP a */
1430       if (get_binop_left(n) > get_binop_right(n)) {
1431         ir_node *h = get_binop_left(n);
1432         set_binop_left(n, get_binop_right(n));
1433         set_binop_right(n, h);
1434       }
1435     }
1436   }
1437
1438   o = pset_find (value_table, n, ir_node_hash (n));
1439   if (!o) return n;
1440
1441   return o;
1442 }
1443
1444 /**
1445  * During construction we set the pinned flag in the graph right when the
1446  * optimizatin is performed.  The flag turning on procedure global cse could
1447  * be changed between two allocations.  This way we are safe.
1448  */
1449 static INLINE ir_node *
1450 identify_cons (pset *value_table, ir_node *n) {
1451   ir_node *old = n;
1452   n = identify(value_table, n);
1453   if (get_irn_n(old, -1) != get_irn_n(n, -1))
1454     set_irg_pinned(current_ir_graph, floats);
1455   return n;
1456 }
1457
1458 /**
1459  * Return the canonical node computing the same value as n.
1460  * Looks up the node in a hash table, enters it in the table
1461  * if it isn't there yet.
1462  */
1463 static ir_node *
1464 identify_remember (pset *value_table, ir_node *node)
1465 {
1466   ir_node *o = NULL;
1467
1468   if (!value_table) return node;
1469
1470   /* lookup or insert in hash table with given hash key. */
1471   o = pset_insert (value_table, node, ir_node_hash (node));
1472
1473   if (o == node) return node;
1474
1475   return o;
1476 }
1477
1478 void
1479 add_identities (pset *value_table, ir_node *node) {
1480   identify_remember (value_table, node);
1481 }
1482
1483 /**
1484  * garbage in, garbage out. If a node has a dead input, i.e., the
1485  * Bad node is input to the node, return the Bad node.
1486  */
1487 static INLINE ir_node *
1488 gigo (ir_node *node)
1489 {
1490   int i, irn_arity;
1491   ir_op* op = get_irn_op(node);
1492
1493   /* remove garbage blocks by looking at control flow that leaves the block
1494      and replacing the control flow by Bad. */
1495   if (get_irn_mode(node) == mode_X) {
1496     ir_node *block = get_nodes_block(node);
1497     if (op == op_End) return node;     /* Don't optimize End, may have Bads. */
1498     if (get_irn_op(block) == op_Block && get_Block_matured(block)) {
1499       irn_arity = get_irn_arity(block);
1500       for (i = 0; i < irn_arity; i++) {
1501         if (!is_Bad(get_irn_n(block, i))) break;
1502       }
1503       if (i == irn_arity) return new_Bad();
1504     }
1505   }
1506
1507   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
1508      blocks predecessors is dead. */
1509   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
1510     irn_arity = get_irn_arity(node);
1511     for (i = -1; i < irn_arity; i++) {
1512       if (is_Bad(get_irn_n(node, i))) {
1513         return new_Bad();
1514       }
1515     }
1516   }
1517 #if 0
1518   /* With this code we violate the agreement that local_optimize
1519      only leaves Bads in Block, Phi and Tuple nodes. */
1520   /* If Block has only Bads as predecessors it's garbage. */
1521   /* If Phi has only Bads as predecessors it's garbage. */
1522   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
1523     irn_arity = get_irn_arity(node);
1524     for (i = 0; i < irn_arity; i++) {
1525       if (!is_Bad(get_irn_n(node, i))) break;
1526     }
1527     if (i == irn_arity) node = new_Bad();
1528   }
1529 #endif
1530   return node;
1531 }
1532
1533
1534 /**
1535  * These optimizations deallocate nodes from the obstack.
1536  * It can only be called if it is guaranteed that no other nodes
1537  * reference this one, i.e., right after construction of a node.
1538  */
1539 ir_node *
1540 optimize_node (ir_node *n)
1541 {
1542   tarval *tv;
1543   ir_node *oldn = n;
1544   opcode iro = get_irn_opcode(n);
1545
1546   /* Allways optimize Phi nodes: part of the construction. */
1547   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
1548
1549   /* constant expression evaluation / constant folding */
1550   if (get_opt_constant_folding()) {
1551     /* constants can not be evaluated */
1552     if (iro != iro_Const) {
1553       /* try to evaluate */
1554       tv = computed_value (n);
1555       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
1556         /*
1557          * we MUST copy the node here temparary, because it's still needed
1558          * for DBG_OPT_ALGSIM0
1559          */
1560         ir_node x = *n;
1561         oldn = &x;
1562         /* evaluation was successful -- replace the node. */
1563         obstack_free (current_ir_graph->obst, n);
1564         n = new_Const (get_tarval_mode (tv), tv);
1565                                                         DBG_OPT_ALGSIM0;
1566         return n;
1567       }
1568     }
1569   }
1570
1571   /* remove unnecessary nodes */
1572   if (get_opt_constant_folding() ||
1573       (iro == iro_Phi)  ||   /* always optimize these nodes. */
1574       (iro == iro_Id)   ||
1575       (iro == iro_Proj) ||
1576       (iro == iro_Block)  )  /* Flags tested local. */
1577     n = equivalent_node (n);
1578
1579   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
1580
1581   /** common subexpression elimination **/
1582   /* Checks whether n is already available. */
1583   /* The block input is used to distinguish different subexpressions. Right
1584      now all nodes are pinned to blocks, i.e., the cse only finds common
1585      subexpressions within a block. */
1586   if (get_opt_cse())
1587     n = identify_cons (current_ir_graph->value_table, n);
1588
1589   if (n != oldn) {
1590     /* We found an existing, better node, so we can deallocate the old node. */
1591     obstack_free (current_ir_graph->obst, oldn);
1592
1593     return n;
1594   }
1595
1596   /* Some more constant expression evaluation that does not allow to
1597      free the node. */
1598   iro = get_irn_opcode(n);
1599   if (get_opt_constant_folding() ||
1600       (iro == iro_Cond) ||
1601       (iro == iro_Proj))     /* Flags tested local. */
1602     n = transform_node (n);
1603
1604   /* Remove nodes with dead (Bad) input.
1605      Run always for transformation induced Bads. */
1606   n = gigo (n);
1607
1608   /* Now we have a legal, useful node. Enter it in hash table for cse */
1609   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
1610     n = identify_remember (current_ir_graph->value_table, n);
1611   }
1612
1613   return n;
1614 }
1615
1616
1617 /**
1618  * These optimizations never deallocate nodes.  This can cause dead
1619  * nodes lying on the obstack.  Remove these by a dead node elimination,
1620  * i.e., a copying garbage collection.
1621  */
1622 ir_node *
1623 optimize_in_place_2 (ir_node *n)
1624 {
1625   tarval *tv;
1626   ir_node *oldn = n;
1627   opcode iro = get_irn_opcode(n);
1628
1629   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
1630
1631   /* if not optimize return n */
1632   if (n == NULL) {
1633     assert(0);
1634     /* Here this is possible.  Why? */
1635     return n;
1636   }
1637
1638
1639   /* constant expression evaluation / constant folding */
1640   if (get_opt_constant_folding()) {
1641     /* constants can not be evaluated */
1642     if (iro != iro_Const) {
1643       /* try to evaluate */
1644       tv = computed_value (n);
1645       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
1646         /* evaluation was successful -- replace the node. */
1647         n = new_Const (get_tarval_mode (tv), tv);
1648                                                 DBG_OPT_ALGSIM0;
1649         return n;
1650       }
1651     }
1652   }
1653
1654   /* remove unnecessary nodes */
1655   /*if (get_opt_constant_folding()) */
1656   if (get_opt_constant_folding() ||
1657       (iro == iro_Phi)  ||   /* always optimize these nodes. */
1658       (iro == iro_Id)   ||   /* ... */
1659       (iro == iro_Proj) ||   /* ... */
1660       (iro == iro_Block)  )  /* Flags tested local. */
1661     n = equivalent_node (n);
1662
1663   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
1664
1665   /** common subexpression elimination **/
1666   /* Checks whether n is already available. */
1667   /* The block input is used to distinguish different subexpressions.  Right
1668      now all nodes are pinned to blocks, i.e., the cse only finds common
1669      subexpressions within a block. */
1670   if (get_opt_cse()) {
1671     n = identify (current_ir_graph->value_table, n);
1672   }
1673
1674   /* Some more constant expression evaluation. */
1675   iro = get_irn_opcode(n);
1676   if (get_opt_constant_folding() ||
1677       (iro == iro_Cond) ||
1678       (iro == iro_Proj))     /* Flags tested local. */
1679     n = transform_node (n);
1680
1681   /* Remove nodes with dead (Bad) input.
1682      Run always for transformation induced Bads.  */
1683   n = gigo (n);
1684
1685   /* Now we can verify the node, as it has no dead inputs any more. */
1686   irn_vrfy(n);
1687
1688   /* Now we have a legal, useful node. Enter it in hash table for cse.
1689      Blocks should be unique anyways.  (Except the successor of start:
1690      is cse with the start block!) */
1691   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
1692     n = identify_remember (current_ir_graph->value_table, n);
1693
1694   return n;
1695 }
1696
1697 /**
1698  * Wrapper for external use, set proper status bits after optimization.
1699  */
1700 ir_node *
1701 optimize_in_place (ir_node *n)
1702 {
1703   /* Handle graph state */
1704   assert(get_irg_phase_state(current_ir_graph) != phase_building);
1705
1706   if (get_opt_global_cse())
1707     set_irg_pinned(current_ir_graph, floats);
1708   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
1709     set_irg_outs_inconsistent(current_ir_graph);
1710   /* Maybe we could also test whether optimizing the node can
1711      change the control graph. */
1712   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
1713     set_irg_dom_inconsistent(current_ir_graph);
1714   return optimize_in_place_2 (n);
1715 }
1716
1717 /**
1718  * set the default ir op operations
1719  */
1720 ir_op *firm_set_default_operations(ir_op *op)
1721 {
1722   op = firm_set_default_computed_value(op);
1723   op = firm_set_default_equivalent_node(op);
1724   op = firm_set_default_transform_node(op);
1725   op = firm_set_default_node_cmp_attr(op);
1726
1727   return op;
1728 }