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