added and fixed comments
[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 #ifdef HAVE_ALLOCA_H
18 #include <alloca.h>
19 #endif
20 #ifdef HAVE_MALLOC_H
21 #include <malloc.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26
27 # include "irnode_t.h"
28 # include "irgraph_t.h"
29 # include "irmode_t.h"
30 # include "iropt_t.h"
31 # include "ircons_t.h"
32 # include "irgmod.h"
33 # include "irvrfy.h"
34 # include "tv_t.h"
35 # include "dbginfo_t.h"
36 # include "iropt_dbg.h"
37 # include "irflag_t.h"
38 # include "irhooks.h"
39 # include "irarch.h"
40 # include "hashptr.h"
41
42 /* Make types visible to allow most efficient access */
43 # include "entity_t.h"
44
45 /**
46  * Trivial INLINEable routine for copy propagation.
47  * Does follow Ids, needed to optimize INLINEd code.
48  */
49 static INLINE ir_node *
50 follow_Id (ir_node *n)
51 {
52   while (get_irn_op (n) == op_Id) n = get_Id_pred (n);
53   return n;
54 }
55
56 /**
57  * return the value of a Constant
58  */
59 static tarval *computed_value_Const(ir_node *n)
60 {
61     return get_Const_tarval(n);
62 }
63
64 /**
65  * return the value of a 'sizeof' SymConst
66  */
67 static tarval *computed_value_SymConst(ir_node *n)
68 {
69   if ((get_SymConst_kind(n) == symconst_size) &&
70       (get_type_state(get_SymConst_type(n))) == layout_fixed)
71     return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(n)), get_irn_mode(n));
72   return tarval_bad;
73 }
74
75 /**
76  * return the value of an Add
77  */
78 static tarval *computed_value_Add(ir_node *n)
79 {
80   ir_node *a = get_Add_left(n);
81   ir_node *b = get_Add_right(n);
82
83   tarval *ta = value_of(a);
84   tarval *tb = value_of(b);
85
86   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
87     return tarval_add(ta, tb);
88
89   return tarval_bad;
90 }
91
92 /**
93  * return the value of a Sub
94  * Special case: a - a
95  */
96 static tarval *computed_value_Sub(ir_node *n)
97 {
98   ir_node *a = get_Sub_left(n);
99   ir_node *b = get_Sub_right(n);
100   tarval *ta;
101   tarval *tb;
102
103   /* a - a */
104   if (a == b && !is_Bad(a))
105     return get_tarval_null(get_irn_mode(n));
106
107   ta = value_of(a);
108   tb = value_of(b);
109
110   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
111     return tarval_sub(ta, tb);
112
113   return tarval_bad;
114 }
115
116 /**
117  * return the value of an unary Minus
118  */
119 static tarval *computed_value_Minus(ir_node *n)
120 {
121   ir_node *a = get_Minus_op(n);
122   tarval *ta = value_of(a);
123
124   if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
125     return tarval_neg(ta);
126
127   return tarval_bad;
128 }
129
130 /**
131  * return the value of a Mul
132  */
133 static tarval *computed_value_Mul(ir_node *n)
134 {
135   ir_node *a = get_Mul_left(n);
136   ir_node *b = get_Mul_right(n);
137
138   tarval *ta = value_of(a);
139   tarval *tb = value_of(b);
140
141   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
142     return tarval_mul(ta, tb);
143   } else {
144     /* a*0 = 0 or 0*b = 0:
145        calls computed_value recursive and returns the 0 with proper
146        mode. */
147     if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
148       return ta;
149     if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
150       return tb;
151   }
152   return tarval_bad;
153 }
154
155 /**
156  * return the value of a floating point Quot
157  */
158 static tarval *computed_value_Quot(ir_node *n)
159 {
160   ir_node *a = get_Quot_left(n);
161   ir_node *b = get_Quot_right(n);
162
163   tarval *ta = value_of(a);
164   tarval *tb = value_of(b);
165
166   /* This was missing in original implementation. Why? */
167   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
168     if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
169       return tarval_quo(ta, tb);
170   }
171   return tarval_bad;
172 }
173
174 /**
175  * calculate the value of an integer Div of two nodes
176  * Special case: 0 / b
177  */
178 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
179 {
180   tarval *ta = value_of(a);
181   tarval *tb = value_of(b);
182
183   /* Compute c1 / c2 or 0 / a, a != 0 */
184   if (ta != tarval_bad) {
185     if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b))))   /* div by zero: return tarval_bad */
186       return tarval_div(ta, tb);
187     else if (ta == get_mode_null(get_tarval_mode(ta)))  /* 0 / b == 0 */
188       return ta;
189   }
190   return tarval_bad;
191 }
192
193 /**
194  * return the value of an integer Div
195  */
196 static tarval *computed_value_Div(ir_node *n)
197 {
198   return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
199 }
200
201 /**
202  * calculate the value of an integer Mod of two nodes
203  * Special case: a % 1
204  */
205 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
206 {
207   tarval *ta = value_of(a);
208   tarval *tb = value_of(b);
209
210   /* Compute c1 % c2 or a % 1 */
211   if (tb != tarval_bad) {
212     if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb))))   /* div by zero: return tarval_bad */
213       return tarval_mod(ta, tb);
214     else if (tb == get_mode_one(get_tarval_mode(tb)))    /* x mod 1 == 0 */
215       return get_mode_null(get_irn_mode(a));
216   }
217
218   return tarval_bad;
219 }
220
221 /**
222  * return the value of an integer Mod
223  */
224 static tarval *computed_value_Mod(ir_node *n)
225 {
226   return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
227 }
228
229 /**
230  * return the value of an Abs
231  */
232 static tarval *computed_value_Abs(ir_node *n)
233 {
234   ir_node *a = get_Abs_op(n);
235   tarval *ta = value_of(a);
236
237   if (ta != tarval_bad)
238     return tarval_abs(ta);
239
240   return tarval_bad;
241 }
242
243 /**
244  * return the value of an And
245  * Special case: a & 0, 0 & b
246  */
247 static tarval *computed_value_And(ir_node *n)
248 {
249   ir_node *a = get_And_left(n);
250   ir_node *b = get_And_right(n);
251
252   tarval *ta = value_of(a);
253   tarval *tb = value_of(b);
254
255   if ((ta != tarval_bad) && (tb != tarval_bad)) {
256     return tarval_and (ta, tb);
257   } else {
258     tarval *v;
259
260     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
261         || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
262       return v;
263     }
264   }
265   return tarval_bad;
266 }
267
268 /**
269  * return the value of an Or
270  * Special case: a | 1...1, 1...1 | b
271  */
272 static tarval *computed_value_Or(ir_node *n)
273 {
274   ir_node *a = get_Or_left(n);
275   ir_node *b = get_Or_right(n);
276
277   tarval *ta = value_of(a);
278   tarval *tb = value_of(b);
279
280   if ((ta != tarval_bad) && (tb != tarval_bad)) {
281     return tarval_or (ta, tb);
282   } else {
283     tarval *v;
284     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
285         || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
286       return v;
287     }
288   }
289   return tarval_bad;
290 }
291
292 /**
293  * return the value of an Eor
294  */
295 static tarval *computed_value_Eor(ir_node *n)
296 {
297   ir_node *a = get_Eor_left(n);
298   ir_node *b = get_Eor_right(n);
299
300   tarval *ta, *tb;
301
302   if (a == b)
303     return get_tarval_null(get_irn_mode(n));
304
305   ta = value_of(a);
306   tb = value_of(b);
307
308   if ((ta != tarval_bad) && (tb != tarval_bad)) {
309     return tarval_eor (ta, tb);
310   }
311   return tarval_bad;
312 }
313
314 /**
315  * return the value of a Not
316  */
317 static tarval *computed_value_Not(ir_node *n)
318 {
319   ir_node *a = get_Not_op(n);
320   tarval *ta = value_of(a);
321
322   if (ta != tarval_bad)
323     return tarval_not(ta);
324
325   return tarval_bad;
326 }
327
328 /**
329  * return the value of a Shl
330  */
331 static tarval *computed_value_Shl(ir_node *n)
332 {
333   ir_node *a = get_Shl_left(n);
334   ir_node *b = get_Shl_right(n);
335
336   tarval *ta = value_of(a);
337   tarval *tb = value_of(b);
338
339   if ((ta != tarval_bad) && (tb != tarval_bad)) {
340     return tarval_shl (ta, tb);
341   }
342   return tarval_bad;
343 }
344
345 /**
346  * return the value of a Shr
347  */
348 static tarval *computed_value_Shr(ir_node *n)
349 {
350   ir_node *a = get_Shr_left(n);
351   ir_node *b = get_Shr_right(n);
352
353   tarval *ta = value_of(a);
354   tarval *tb = value_of(b);
355
356   if ((ta != tarval_bad) && (tb != tarval_bad)) {
357     return tarval_shr (ta, tb);
358   }
359   return tarval_bad;
360 }
361
362 /**
363  * return the value of a Shrs
364  */
365 static tarval *computed_value_Shrs(ir_node *n)
366 {
367   ir_node *a = get_Shrs_left(n);
368   ir_node *b = get_Shrs_right(n);
369
370   tarval *ta = value_of(a);
371   tarval *tb = value_of(b);
372
373   if ((ta != tarval_bad) && (tb != tarval_bad)) {
374     return tarval_shrs (ta, tb);
375   }
376   return tarval_bad;
377 }
378
379 /**
380  * return the value of a Rot
381  */
382 static tarval *computed_value_Rot(ir_node *n)
383 {
384   ir_node *a = get_Rot_left(n);
385   ir_node *b = get_Rot_right(n);
386
387   tarval *ta = value_of(a);
388   tarval *tb = value_of(b);
389
390   if ((ta != tarval_bad) && (tb != tarval_bad)) {
391     return tarval_rot (ta, tb);
392   }
393   return tarval_bad;
394 }
395
396 /**
397  * return the value of a Conv
398  */
399 static tarval *computed_value_Conv(ir_node *n)
400 {
401   ir_node *a = get_Conv_op(n);
402   tarval *ta = value_of(a);
403
404   if (ta != tarval_bad)
405     return tarval_convert_to(ta, get_irn_mode(n));
406
407   return tarval_bad;
408 }
409
410 /**
411  * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
412  */
413 static tarval *computed_value_Proj(ir_node *n)
414 {
415   ir_node *a = get_Proj_pred(n);
416   ir_node *aa, *ab;
417   long proj_nr;
418
419   /* Optimize Cmp nodes.
420      This performs a first step of unreachable code elimination.
421      Proj can not be computed, but folding a Cmp above the Proj here is
422      not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
423      only 1 is used.
424      There are several case where we can evaluate a Cmp node:
425      1. The nodes compared are both the same.  If we compare for
426         equal, greater equal, ... this will return true, else it
427         will return false.  This step relies on cse.
428      2. The predecessors of Cmp are target values.  We can evaluate
429         the Cmp.
430      3. The predecessors are Allocs or void* constants.  Allocs never
431         return NULL, they raise an exception.   Therefore we can predict
432         the Cmp result. */
433   switch (get_irn_opcode(a)) {
434   case iro_Cmp:
435     aa = get_Cmp_left(a);
436     ab = get_Cmp_right(a);
437     proj_nr = get_Proj_proj(n);
438
439     if (aa == ab && !mode_is_float(get_irn_mode(aa))) { /* 1.: */
440       /* BEWARE: a == a is NOT always True for floating Point!!! */
441       /* This is a trick with the bits used for encoding the Cmp
442          Proj numbers, the following statement is not the same:
443       return new_tarval_from_long (proj_nr == Eq, mode_b) */
444       return new_tarval_from_long (proj_nr & Eq, mode_b);
445     } else {
446       tarval *taa = value_of(aa);
447       tarval *tab = value_of(ab);
448
449       if ((taa != tarval_bad) && (tab != tarval_bad)) { /* 2.: */
450         /* strange checks... */
451         pnc_number flags = tarval_cmp (taa, tab);
452         if (flags != False) {
453           return new_tarval_from_long (proj_nr & flags, mode_b);
454         }
455       } else {  /* check for 3.: */
456         ir_node *aaa = skip_Id(skip_Proj(aa));
457         ir_node *aba = skip_Id(skip_Proj(ab));
458
459         if (   (   (/* aa is ProjP and aaa is Alloc */
460                        (get_irn_op(aa) == op_Proj)
461                     && (mode_is_reference(get_irn_mode(aa)))
462                     && (get_irn_op(aaa) == op_Alloc))
463                 && (   (/* ab is constant void */
464                            (get_irn_op(ab) == op_Const)
465                         && (mode_is_reference(get_irn_mode(ab)))
466                         && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
467                     || (/* ab is other Alloc */
468                            (get_irn_op(ab) == op_Proj)
469                         && (mode_is_reference(get_irn_mode(ab)))
470                         && (get_irn_op(aba) == op_Alloc)
471                         && (aaa != aba))))
472             || (/* aa is void and aba is Alloc */
473                    (get_irn_op(aa) == op_Const)
474                 && (mode_is_reference(get_irn_mode(aa)))
475                 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
476                 && (get_irn_op(ab) == op_Proj)
477                 && (mode_is_reference(get_irn_mode(ab)))
478                 && (get_irn_op(aba) == op_Alloc)))
479           /* 3.: */
480           return new_tarval_from_long (proj_nr & Ne, mode_b);
481       }
482     }
483     break;
484
485   case iro_DivMod:
486     /* compute either the Div or the Mod part */
487     proj_nr = get_Proj_proj(n);
488     if (proj_nr == pn_DivMod_res_div)
489       return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
490     else if (proj_nr == pn_DivMod_res_mod)
491       return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
492     break;
493
494   case iro_Div:
495     if (get_Proj_proj(n) == pn_Div_res)
496       return computed_value(a);
497     break;
498
499   case iro_Mod:
500     if (get_Proj_proj(n) == pn_Mod_res)
501       return computed_value(a);
502     break;
503
504   default:
505     return tarval_bad;
506   }
507   return tarval_bad;
508 }
509
510 /**
511  * calculate the value of a Mux: can be evaluated, if the
512  * sel and the right input are known
513  */
514 static tarval *computed_value_Mux(ir_node *n)
515 {
516   ir_node *sel = get_Mux_sel(n);
517   tarval *ts = value_of(sel);
518
519   if (ts == get_tarval_b_true()) {
520     ir_node *v = get_Mux_true(n);
521     return value_of(v);
522   }
523   else if (ts == get_tarval_b_false()) {
524     ir_node *v = get_Mux_false(n);
525     return value_of(v);
526   }
527   return tarval_bad;
528 }
529
530 /**
531  * If the parameter n can be computed, return its value, else tarval_bad.
532  * Performs constant folding.
533  *
534  * @param n  The node this should be evaluated
535  */
536 tarval *computed_value(ir_node *n)
537 {
538   if (n->op->computed_value)
539     return n->op->computed_value(n);
540   return tarval_bad;
541 }
542
543 /**
544  * set the default computed_value evaluator
545  */
546 static ir_op *firm_set_default_computed_value(ir_op *op)
547 {
548 #define CASE(a)                               \
549   case iro_##a:                               \
550     op->computed_value  = computed_value_##a; \
551     break
552
553   switch (op->code) {
554   CASE(Const);
555   CASE(SymConst);
556   CASE(Add);
557   CASE(Sub);
558   CASE(Minus);
559   CASE(Mul);
560   CASE(Quot);
561   CASE(Div);
562   CASE(Mod);
563   CASE(Abs);
564   CASE(And);
565   CASE(Or);
566   CASE(Eor);
567   CASE(Not);
568   CASE(Shl);
569   CASE(Shr);
570   CASE(Shrs);
571   CASE(Rot);
572   CASE(Conv);
573   CASE(Proj);
574   CASE(Mux);
575   default:
576     op->computed_value  = NULL;
577   }
578
579   return op;
580 #undef CASE
581 }
582
583 #if 0
584 /* returns 1 if the a and b are pointers to different locations. */
585 static bool
586 different_identity (ir_node *a, ir_node *b)
587 {
588   assert (mode_is_reference(get_irn_mode (a))
589           && mode_is_reference(get_irn_mode (b)));
590
591   if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
592     ir_node *a1 = get_Proj_pred (a);
593     ir_node *b1 = get_Proj_pred (b);
594     if (a1 != b1 && get_irn_op (a1) == op_Alloc
595                 && get_irn_op (b1) == op_Alloc)
596       return 1;
597   }
598   return 0;
599 }
600 #endif
601
602 static ir_node *equivalent_node_Block(ir_node *n)
603 {
604   ir_node *oldn = n;
605
606   /* The Block constructor does not call optimize, but mature_immBlock
607      calls the optimization. */
608   assert(get_Block_matured(n));
609
610   /* Straightening: a single entry Block following a single exit Block
611      can be merged, if it is not the Start block. */
612   /* !!! Beware, all Phi-nodes of n must have been optimized away.
613      This should be true, as the block is matured before optimize is called.
614      But what about Phi-cycles with the Phi0/Id that could not be resolved?
615      Remaining Phi nodes are just Ids. */
616    if ((get_Block_n_cfgpreds(n) == 1) &&
617        (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
618      ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
619      if (predblock == oldn) {
620        /* Jmp jumps into the block it is in -- deal self cycle. */
621        n = set_Block_dead(n);
622        DBG_OPT_DEAD(oldn, n);
623      } else if (get_opt_control_flow_straightening()) {
624        n = predblock;
625        DBG_OPT_STG(oldn, n);
626      }
627    }
628    else if ((get_Block_n_cfgpreds(n) == 1) &&
629             (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
630      ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
631      if (predblock == oldn) {
632        /* Jmp jumps into the block it is in -- deal self cycle. */
633        n = set_Block_dead(n);
634        DBG_OPT_DEAD(oldn, n);
635      }
636    }
637    else if ((get_Block_n_cfgpreds(n) == 2) &&
638             (get_opt_control_flow_weak_simplification())) {
639     /* Test whether Cond jumps twice to this block
640        @@@ we could do this also with two loops finding two preds from several ones. */
641     ir_node *a = get_Block_cfgpred(n, 0);
642     ir_node *b = get_Block_cfgpred(n, 1);
643
644     if ((get_irn_op(a) == op_Proj) &&
645         (get_irn_op(b) == op_Proj) &&
646         (get_Proj_pred(a) == get_Proj_pred(b)) &&
647         (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
648         (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
649       /* Also a single entry Block following a single exit Block.  Phis have
650          twice the same operand and will be optimized away. */
651       n = get_nodes_block(a);
652       DBG_OPT_IFSIM(oldn, a, b, n);
653     }
654   } else if (get_opt_unreachable_code() &&
655              (n != current_ir_graph->start_block) &&
656              (n != current_ir_graph->end_block)     ) {
657     int i, n_cfg = get_Block_n_cfgpreds(n);
658
659     /* If all inputs are dead, this block is dead too, except if it is
660        the start or end block.  This is a step of unreachable code
661        elimination */
662     for (i = 0; i < n_cfg; i++) {
663       ir_node *pred = get_Block_cfgpred(n, i);
664       ir_node *pred_blk;
665
666       if (is_Bad(pred)) continue;
667       pred_blk = get_nodes_block(pred);
668
669       if (is_Block_dead(pred_blk)) continue;
670
671       if (pred_blk != n) {
672         /* really found a living input */
673         break;
674       }
675     }
676     if (i == n_cfg)
677       n = set_Block_dead(n);
678   }
679
680   return n;
681 }
682
683 /**
684  * Returns a equivalent node for a Jmp, a Bad :-)
685  * Of course this only happens if the Block of the Jmp is Bad.
686  */
687 static ir_node *equivalent_node_Jmp(ir_node *n)
688 {
689   /* GL: Why not same for op_Raise?? */
690   /* unreachable code elimination */
691   if (is_Block_dead(get_nodes_block(n)))
692     n = new_Bad();
693
694   return n;
695 }
696
697 static ir_node *equivalent_node_Cond(ir_node *n)
698 {
699   /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
700      See cases for iro_Cond and iro_Proj in transform_node. */
701   return n;
702 }
703
704 /**
705  * optimize operations that are commutative and have neutral 0,
706  * so a op 0 = 0 op a = a.
707  */
708 static ir_node *equivalent_node_neutral_zero(ir_node *n)
709 {
710   ir_node *oldn = n;
711
712   ir_node *a = get_binop_left(n);
713   ir_node *b = get_binop_right(n);
714
715   tarval *tv;
716   ir_node *on;
717
718   /* After running compute_node there is only one constant predecessor.
719      Find this predecessors value and remember the other node: */
720   if ((tv = value_of(a)) != tarval_bad) {
721     on = b;
722   } else if ((tv = value_of(b)) != tarval_bad) {
723     on = a;
724   } else
725     return n;
726
727   /* If this predecessors constant value is zero, the operation is
728      unnecessary. Remove it: */
729   if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
730     n = on;
731
732     DBG_OPT_ALGSIM1(oldn, a, b, n);
733   }
734
735   return n;
736 }
737
738 #define equivalent_node_Add  equivalent_node_neutral_zero
739 #define equivalent_node_Eor  equivalent_node_neutral_zero
740
741 /**
742  * optimize operations that are not commutative but have neutral 0 on left,
743  * so a op 0 = a.
744  */
745 static ir_node *equivalent_node_left_zero(ir_node *n)
746 {
747   ir_node *oldn = n;
748
749   ir_node *a = get_binop_left(n);
750   ir_node *b = get_binop_right(n);
751
752   if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
753     n = a;
754
755     DBG_OPT_ALGSIM1(oldn, a, b, n);
756   }
757
758   return n;
759 }
760
761 #define equivalent_node_Sub   equivalent_node_left_zero
762 #define equivalent_node_Shl   equivalent_node_left_zero
763 #define equivalent_node_Shr   equivalent_node_left_zero
764 #define equivalent_node_Shrs  equivalent_node_left_zero
765 #define equivalent_node_Rot   equivalent_node_left_zero
766
767 /**
768  * Er, a "symmetic unop", ie op(op(n)) = n.
769  */
770 static ir_node *equivalent_node_symmetric_unop(ir_node *n)
771 {
772   ir_node *oldn = n;
773   ir_node *pred = get_unop_op(n);
774
775   /* optimize symmetric unop */
776   if (get_irn_op(pred) == get_irn_op(n)) {
777     n = get_unop_op(pred);
778     DBG_OPT_ALGSIM2(oldn, pred, n);
779   }
780   return n;
781 }
782
783 /* NotNot x == x */
784 #define equivalent_node_Not    equivalent_node_symmetric_unop
785
786 /* --x == x */  /* ??? Is this possible or can --x raise an
787                        out of bounds exception if min =! max? */
788 #define equivalent_node_Minus  equivalent_node_symmetric_unop
789
790 /**
791  * Optimize a * 1 = 1 * a = a.
792  */
793 static ir_node *equivalent_node_Mul(ir_node *n)
794 {
795   ir_node *oldn = n;
796
797   ir_node *a = get_Mul_left(n);
798   ir_node *b = get_Mul_right(n);
799
800   /* Mul is commutative and has again an other neutral element. */
801   if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
802     n = b;
803     DBG_OPT_ALGSIM1(oldn, a, b, n);
804   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
805     n = a;
806     DBG_OPT_ALGSIM1(oldn, a, b, n);
807   }
808   return n;
809 }
810
811 /**
812  * Optimize a / 1 = a.
813  */
814 static ir_node *equivalent_node_Div(ir_node *n)
815 {
816   ir_node *a = get_Div_left(n);
817   ir_node *b = get_Div_right(n);
818
819   /* Div is not commutative. */
820   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
821     /* Turn Div into a tuple (mem, bad, a) */
822     ir_node *mem = get_Div_mem(n);
823     turn_into_tuple(n, 3);
824     set_Tuple_pred(n, pn_Div_M,        mem);
825     set_Tuple_pred(n, pn_Div_X_except, new_Bad());        /* no exception */
826     set_Tuple_pred(n, pn_Div_res,      a);
827   }
828   return n;
829 }
830
831 /**
832  * Optimize a / 1 = a.
833  */
834 static ir_node *equivalent_node_DivMod(ir_node *n)
835 {
836   ir_node *a = get_DivMod_left(n);
837   ir_node *b = get_DivMod_right(n);
838
839   /* Div is not commutative. */
840   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
841     /* Turn DivMod into a tuple (mem, bad, a, 0) */
842     ir_node *mem = get_Div_mem(n);
843     ir_mode *mode = get_irn_mode(b);
844
845     turn_into_tuple(n, 4);
846     set_Tuple_pred(n, pn_DivMod_M,        mem);
847     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());        /* no exception */
848     set_Tuple_pred(n, pn_DivMod_res_div,  a);
849     set_Tuple_pred(n, pn_DivMod_res_mod,  new_Const(mode, get_mode_null(mode)));
850   }
851   return n;
852 }
853
854 /**
855  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
856  */
857 static ir_node *equivalent_node_Or(ir_node *n)
858 {
859   ir_node *oldn = n;
860
861   ir_node *a = get_Or_left(n);
862   ir_node *b = get_Or_right(n);
863
864   if (a == b) {
865     n = a;    /* Or has it's own neutral element */
866   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
867     n = b;
868     DBG_OPT_ALGSIM1(oldn, a, b, n);
869   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
870     n = a;
871     DBG_OPT_ALGSIM1(oldn, a, b, n);
872   }
873
874   return n;
875 }
876
877 /**
878  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
879  */
880 static ir_node *equivalent_node_And(ir_node *n)
881 {
882   ir_node *oldn = n;
883
884   ir_node *a = get_And_left(n);
885   ir_node *b = get_And_right(n);
886
887   if (a == b) {
888     n = a;    /* And has it's own neutral element */
889   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
890     n = b;
891     DBG_OPT_ALGSIM1(oldn, a, b, n);
892   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
893     n = a;
894     DBG_OPT_ALGSIM1(oldn, a, b, n);
895   }
896   return n;
897 }
898
899 /**
900  * Try to remove useless conv's:
901  */
902 static ir_node *equivalent_node_Conv(ir_node *n)
903 {
904   ir_node *oldn = n;
905   ir_node *a = get_Conv_op(n);
906   ir_node *b;
907
908   ir_mode *n_mode = get_irn_mode(n);
909   ir_mode *a_mode = get_irn_mode(a);
910
911   if (n_mode == a_mode) { /* No Conv necessary */
912     n = a;
913     DBG_OPT_ALGSIM3(oldn, a, n);
914   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
915     ir_mode *b_mode;
916
917     b = get_Conv_op(a);
918     n_mode = get_irn_mode(n);
919     b_mode = get_irn_mode(b);
920
921     if (n_mode == b_mode) {
922       if (n_mode == mode_b) {
923         n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
924     DBG_OPT_ALGSIM1(oldn, a, b, n);
925       }
926       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
927         if (smaller_mode(b_mode, a_mode)){
928           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
929       DBG_OPT_ALGSIM1(oldn, a, b, n);
930         }
931       }
932     }
933   }
934   return n;
935 }
936
937 /**
938  * A Cast may be removed if the type of the previous node
939  * is already to type of the Cast.
940  */
941 static ir_node *equivalent_node_Cast(ir_node *n) {
942   ir_node *pred = get_Cast_op(n);
943   if (get_irn_type(pred) == get_Cast_type(n))
944     n = pred;
945   return n;
946 }
947
948 /* Several optimizations:
949    - no Phi in start block.
950    - remove Id operators that are inputs to Phi
951    - fold Phi-nodes, iff they have only one predecessor except
952            themselves.
953 */
954 static ir_node *equivalent_node_Phi(ir_node *n)
955 {
956   int i, n_preds;
957
958   ir_node *oldn = n;
959   ir_node *block = NULL;     /* to shutup gcc */
960   ir_node *first_val = NULL; /* to shutup gcc */
961   ir_node *scnd_val = NULL;  /* to shutup gcc */
962
963   if (!get_opt_normalize()) return n;
964
965   n_preds = get_Phi_n_preds(n);
966
967   block = get_nodes_block(n);
968   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
969      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
970   if ((is_Block_dead(block)) ||                  /* Control dead */
971       (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
972     return new_Bad();                            /* in the Start Block. */
973
974   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
975
976 #if 0
977   /* first we test for a special case: */
978   /* Confirm is a special node fixing additional information for a
979      value that is known at a certain point.  This is useful for
980      dataflow analysis. */
981   if (n_preds == 2) {
982     ir_node *a = get_Phi_pred(n, 0);
983     ir_node *b = get_Phi_pred(n, 1);
984     if (   (get_irn_op(a) == op_Confirm)
985         && (get_irn_op(b) == op_Confirm)
986         && follow_Id (get_irn_n(a, 0) == get_irn_n(b, 0))
987         && (get_irn_n(a, 1) == get_irn_n (b, 1))
988         && (a->data.num == (~b->data.num & irpn_True) )) {
989       return get_irn_n(a, 0);
990     }
991   }
992 #endif
993
994   /* If the Block has a Bad pred, we also have one. */
995   for (i = 0;  i < n_preds;  ++i)
996     if (is_Bad (get_Block_cfgpred(block, i)))
997       set_Phi_pred(n, i, new_Bad());
998
999   /* Find first non-self-referencing input */
1000   for (i = 0;  i < n_preds;  ++i) {
1001     first_val = get_Phi_pred(n, i);
1002     if (   (first_val != n)                            /* not self pointer */
1003 #if 1
1004         && (get_irn_op(first_val) != op_Bad)
1005 #endif
1006            ) {        /* value not dead */
1007       break;          /* then found first value. */
1008     }
1009   }
1010
1011   /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1012   if (i >= n_preds) { return new_Bad(); }
1013
1014   scnd_val = NULL;
1015
1016   /* follow_Id () for rest of inputs, determine if any of these
1017      are non-self-referencing */
1018   while (++i < n_preds) {
1019     scnd_val = get_Phi_pred(n, i);
1020     if (   (scnd_val != n)
1021         && (scnd_val != first_val)
1022 #if 1
1023         && (get_irn_op(scnd_val) != op_Bad)
1024 #endif
1025            ) {
1026       break;
1027     }
1028   }
1029
1030   /* Fold, if no multiple distinct non-self-referencing inputs */
1031   if (i >= n_preds) {
1032     n = first_val;
1033     DBG_OPT_PHI(oldn, first_val, n);
1034   } else {
1035     /* skip the remaining Ids (done in get_Phi_pred). */
1036     /* superfluous, since we walk all to propagate Block's Bads.
1037        while (++i < n_preds) get_Phi_pred(n, i);     */
1038   }
1039   return n;
1040 }
1041
1042 /**
1043  * optimize Proj(Tuple) and gigo for ProjX in Bad block
1044  */
1045 static ir_node *equivalent_node_Proj(ir_node *n)
1046 {
1047   ir_node *oldn = n;
1048
1049   ir_node *a = get_Proj_pred(n);
1050
1051   if ( get_irn_op(a) == op_Tuple) {
1052     /* Remove the Tuple/Proj combination. */
1053     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1054       n = get_Tuple_pred(a, get_Proj_proj(n));
1055       DBG_OPT_TUPLE(oldn, a, n);
1056     } else {
1057       assert(0); /* This should not happen! */
1058       n = new_Bad();
1059     }
1060   } else if (get_irn_mode(n) == mode_X &&
1061              is_Block_dead(get_nodes_block(n))) {
1062     /* Remove dead control flow -- early gigo. */
1063     n = new_Bad();
1064   }
1065   return n;
1066 }
1067
1068 /**
1069  * Remove Id's.
1070  */
1071 static ir_node *equivalent_node_Id(ir_node *n)
1072 {
1073   ir_node *oldn = n;
1074
1075   n = follow_Id(n);
1076   DBG_OPT_ID(oldn, n);
1077   return n;
1078 }
1079
1080 /**
1081  * optimize a Mux
1082  */
1083 static ir_node *equivalent_node_Mux(ir_node *n)
1084 {
1085   ir_node *sel = get_Mux_sel(n);
1086   tarval *ts = value_of(sel);
1087
1088   if (ts == get_tarval_b_true())
1089     return get_Mux_true(n);
1090   else if (ts == get_tarval_b_false())
1091     return get_Mux_false(n);
1092
1093   return n;
1094 }
1095
1096 /**
1097  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1098  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1099  * new nodes.  It is therefore safe to free n if the node returned is not n.
1100  * If a node returns a Tuple we can not just skip it.  If the size of the
1101  * in array fits, we transform n into a tuple (e.g., Div).
1102  */
1103 ir_node *
1104 equivalent_node(ir_node *n)
1105 {
1106   if (n->op->equivalent_node)
1107     return n->op->equivalent_node(n);
1108   return n;
1109 }
1110
1111 /**
1112  * set the default equivalent node operation
1113  */
1114 static ir_op *firm_set_default_equivalent_node(ir_op *op)
1115 {
1116 #define CASE(a)                                 \
1117   case iro_##a:                                 \
1118     op->equivalent_node  = equivalent_node_##a; \
1119     break
1120
1121   switch (op->code) {
1122   CASE(Block);
1123   CASE(Jmp);
1124   CASE(Cond);
1125   CASE(Or);
1126   CASE(Add);
1127   CASE(Eor);
1128   CASE(Sub);
1129   CASE(Shl);
1130   CASE(Shr);
1131   CASE(Shrs);
1132   CASE(Rot);
1133   CASE(Not);
1134   CASE(Minus);
1135   CASE(Mul);
1136   CASE(Div);
1137   CASE(DivMod);
1138   CASE(And);
1139   CASE(Conv);
1140   CASE(Cast);
1141   CASE(Phi);
1142   CASE(Proj);
1143   CASE(Id);
1144   CASE(Mux);
1145   default:
1146     op->equivalent_node  = NULL;
1147   }
1148
1149   return op;
1150 #undef CASE
1151 }
1152
1153 /**
1154  * Do node specific optimizations of nodes predecessors.
1155  */
1156 static void
1157 optimize_preds(ir_node *n) {
1158   ir_node *a = NULL, *b = NULL;
1159
1160   /* get the operands we will work on for simple cases. */
1161   if (is_binop(n)) {
1162     a = get_binop_left(n);
1163     b = get_binop_right(n);
1164   } else if (is_unop(n)) {
1165     a = get_unop_op(n);
1166   }
1167
1168   switch (get_irn_opcode(n)) {
1169
1170   case iro_Cmp:
1171     /* We don't want Cast as input to Cmp. */
1172     if (get_irn_op(a) == op_Cast) {
1173       a = get_Cast_op(a);
1174       set_Cmp_left(n, a);
1175     }
1176     if (get_irn_op(b) == op_Cast) {
1177       b = get_Cast_op(b);
1178       set_Cmp_right(n, b);
1179     }
1180     break;
1181
1182   default: break;
1183   } /* end switch */
1184 }
1185
1186 /**
1187  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1188  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)) if possible.
1189  */
1190 static ir_node *transform_node_AddSub(ir_node *n)
1191 {
1192   ir_mode *mode = get_irn_mode(n);
1193
1194   if (mode_is_reference(mode)) {
1195     ir_node *left  = get_binop_left(n);
1196     ir_node *right = get_binop_right(n);
1197     int ref_bits   = get_mode_size_bits(mode);
1198
1199     if (get_irn_op(left) == op_Conv) {
1200       ir_mode *mode = get_irn_mode(left);
1201       int bits      = get_mode_size_bits(mode);
1202
1203       if (ref_bits == bits &&
1204           mode_is_int(mode) &&
1205           get_mode_arithmetic(mode) == irma_twos_complement) {
1206         ir_node *pre      = get_Conv_op(left);
1207         ir_mode *pre_mode = get_irn_mode(pre);
1208
1209         if (mode_is_int(pre_mode) &&
1210             get_mode_size_bits(pre_mode) == bits &&
1211             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1212           /* ok, this conv just changes to sign, moreover the calculation
1213            * is done with same number of bits as our address mode, so
1214            * we can ignore the conv as address calculation can be viewed
1215            * as either signed or unsigned
1216            */
1217           set_binop_left(n, pre);
1218         }
1219       }
1220     }
1221
1222     if (get_irn_op(right) == op_Conv) {
1223       ir_mode *mode = get_irn_mode(right);
1224       int bits      = get_mode_size_bits(mode);
1225
1226       if (ref_bits == bits &&
1227           mode_is_int(mode) &&
1228           get_mode_arithmetic(mode) == irma_twos_complement) {
1229         ir_node *pre      = get_Conv_op(right);
1230         ir_mode *pre_mode = get_irn_mode(pre);
1231
1232         if (mode_is_int(pre_mode) &&
1233             get_mode_size_bits(pre_mode) == bits &&
1234             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1235           /* ok, this conv just changes to sign, moreover the calculation
1236            * is done with same number of bits as our address mode, so
1237            * we can ignore the conv as address calculation can be viewed
1238            * as either signed or unsigned
1239            */
1240           set_binop_right(n, pre);
1241         }
1242       }
1243     }
1244   }
1245   return n;
1246 }
1247
1248 #define transform_node_Add      transform_node_AddSub
1249 #define transform_node_Sub      transform_node_AddSub
1250
1251 /** Do architecture dependend optimizations on Mul nodes */
1252 static ir_node *transform_node_Mul(ir_node *n) {
1253   return arch_dep_replace_mul_with_shifts(n);
1254 }
1255
1256 static ir_node *transform_node_Div(ir_node *n)
1257 {
1258   tarval *tv = value_of(n);
1259   ir_node *value = n;
1260
1261   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1262
1263   if (tv != tarval_bad)
1264     value = new_Const(get_tarval_mode(tv), tv);
1265   else /* Try architecture dependand optimization */
1266     value = arch_dep_replace_div_by_const(n);
1267
1268   if (value != n) {
1269     /* Turn Div into a tuple (mem, bad, value) */
1270     ir_node *mem = get_Div_mem(n);
1271
1272     turn_into_tuple(n, 3);
1273     set_Tuple_pred(n, pn_Div_M, mem);
1274     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1275     set_Tuple_pred(n, pn_Div_res, value);
1276   }
1277   return n;
1278 }
1279
1280 static ir_node *transform_node_Mod(ir_node *n)
1281 {
1282   tarval *tv = value_of(n);
1283   ir_node *value = n;
1284
1285   /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1286
1287   if (tv != tarval_bad)
1288     value = new_Const(get_tarval_mode(tv), tv);
1289   else /* Try architecture dependand optimization */
1290     value = arch_dep_replace_mod_by_const(n);
1291
1292   if (value != n) {
1293     /* Turn Mod into a tuple (mem, bad, value) */
1294     ir_node *mem = get_Mod_mem(n);
1295
1296     turn_into_tuple(n, 3);
1297     set_Tuple_pred(n, pn_Mod_M, mem);
1298     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1299     set_Tuple_pred(n, pn_Mod_res, value);
1300   }
1301   return n;
1302 }
1303
1304 static ir_node *transform_node_DivMod(ir_node *n)
1305 {
1306   int evaluated = 0;
1307
1308   ir_node *a = get_DivMod_left(n);
1309   ir_node *b = get_DivMod_right(n);
1310   ir_mode *mode = get_irn_mode(a);
1311   tarval *ta = value_of(a);
1312   tarval *tb = value_of(b);
1313
1314   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1315     return n;
1316
1317   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1318
1319   if (tb != tarval_bad) {
1320     if (tb == get_mode_one(get_tarval_mode(tb))) {
1321       b = new_Const (mode, get_mode_null(mode));
1322       evaluated = 1;
1323     } else if (ta != tarval_bad) {
1324       tarval *resa, *resb;
1325       resa = tarval_div (ta, tb);
1326       if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1327                                         Jmp for X result!? */
1328       resb = tarval_mod (ta, tb);
1329       if (resb == tarval_bad) return n; /* Causes exception! */
1330       a = new_Const (mode, resa);
1331       b = new_Const (mode, resb);
1332       evaluated = 1;
1333     }
1334     else { /* Try architecture dependand optimization */
1335       arch_dep_replace_divmod_by_const(&a, &b, n);
1336       evaluated = a != NULL;
1337     }
1338   } else if (ta == get_mode_null(mode)) {
1339     /* 0 / non-Const = 0 */
1340     b = a;
1341     evaluated = 1;
1342   }
1343
1344   if (evaluated) { /* replace by tuple */
1345     ir_node *mem = get_DivMod_mem(n);
1346     turn_into_tuple(n, 4);
1347     set_Tuple_pred(n, pn_DivMod_M,        mem);
1348     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
1349     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1350     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
1351     assert(get_nodes_block(n));
1352   }
1353
1354   return n;
1355 }
1356
1357 static ir_node *transform_node_Cond(ir_node *n)
1358 {
1359   /* Replace the Cond by a Jmp if it branches on a constant
1360      condition. */
1361   ir_node *jmp;
1362   ir_node *a = get_Cond_selector(n);
1363   tarval *ta = value_of(a);
1364
1365   if ((ta != tarval_bad) &&
1366       (get_irn_mode(a) == mode_b) &&
1367       (get_opt_unreachable_code())) {
1368     /* It's a boolean Cond, branching on a boolean constant.
1369                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1370     jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
1371     turn_into_tuple(n, 2);
1372     if (ta == tarval_b_true) {
1373       set_Tuple_pred(n, pn_Cond_false, new_Bad());
1374       set_Tuple_pred(n, pn_Cond_true, jmp);
1375     } else {
1376       set_Tuple_pred(n, pn_Cond_false, jmp);
1377       set_Tuple_pred(n, pn_Cond_true, new_Bad());
1378     }
1379     /* We might generate an endless loop, so keep it alive. */
1380     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1381   } else if ((ta != tarval_bad) &&
1382              (get_irn_mode(a) == mode_Iu) &&
1383              (get_Cond_kind(n) == dense) &&
1384              (get_opt_unreachable_code())) {
1385     /* I don't want to allow Tuples smaller than the biggest Proj.
1386        Also this tuple might get really big...
1387        I generate the Jmp here, and remember it in link.  Link is used
1388        when optimizing Proj. */
1389     set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_block(n)));
1390     /* We might generate an endless loop, so keep it alive. */
1391     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1392   } else if ((get_irn_op(a) == op_Eor)
1393              && (get_irn_mode(a) == mode_b)
1394              && (classify_tarval(value_of(get_Eor_right(a))) == TV_CLASSIFY_ONE)) {
1395     /* The Eor is a negate.  Generate a new Cond without the negate,
1396        simulate the negate by exchanging the results. */
1397     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1398                                get_Eor_left(a)));
1399   } else if ((get_irn_op(a) == op_Not)
1400              && (get_irn_mode(a) == mode_b)) {
1401     /* A Not before the Cond.  Generate a new Cond without the Not,
1402        simulate the Not by exchanging the results. */
1403     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1404                                get_Not_op(a)));
1405   }
1406   return n;
1407 }
1408
1409 /**
1410  * Transform an Eor.
1411  */
1412 static ir_node *transform_node_Eor(ir_node *n)
1413 {
1414   ir_node *a = get_Eor_left(n);
1415   ir_node *b = get_Eor_right(n);
1416
1417   if ((get_irn_mode(n) == mode_b)
1418       && (get_irn_op(a) == op_Proj)
1419       && (get_irn_mode(a) == mode_b)
1420       && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
1421       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1422     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1423     n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1424                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1425   else if ((get_irn_mode(n) == mode_b)
1426            && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE))
1427     /* The Eor is a Not. Replace it by a Not. */
1428     /*   ????!!!Extend to bitfield 1111111. */
1429     n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode_b);
1430
1431   return n;
1432 }
1433
1434 /**
1435  * Transform a boolean Not.
1436  */
1437 static ir_node *transform_node_Not(ir_node *n)
1438 {
1439   ir_node *a = get_Not_op(n);
1440
1441   if (   (get_irn_mode(n) == mode_b)
1442       && (get_irn_op(a) == op_Proj)
1443       && (get_irn_mode(a) == mode_b)
1444       && (get_irn_op(get_Proj_pred(a)) == op_Cmp))
1445     /* We negate a Cmp. The Cmp has the negated result anyways! */
1446     n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1447                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1448
1449   return n;
1450 }
1451
1452 /**
1453  * Transform a Cast of a Const into a new Const
1454  */
1455 static ir_node *transform_node_Cast(ir_node *n) {
1456   ir_node *pred = get_Cast_op(n);
1457   type *tp = get_irn_type(pred);
1458
1459   if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
1460     n = new_rd_Const_type(NULL, current_ir_graph, get_nodes_block(pred), get_irn_mode(pred),
1461               get_Const_tarval(pred), tp);
1462   } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
1463     n = new_rd_SymConst_type(NULL, current_ir_graph, get_nodes_block(pred), get_SymConst_symbol(pred),
1464                  get_SymConst_kind(pred), tp);
1465   }
1466   return n;
1467 }
1468
1469 /**
1470  * Transform a Div/Mod/DivMod with a non-zero constant. Must be
1471  * done here instead of equivalent node because it creates new
1472  * nodes.
1473  * Removes the exceptions and routes the memory to the NoMem node.
1474  *
1475  * Further, it optimizes jump tables by removing all impossible cases.
1476  */
1477 static ir_node *transform_node_Proj(ir_node *proj)
1478 {
1479   ir_node *n = get_Proj_pred(proj);
1480   ir_node *b;
1481   tarval *tb;
1482   long proj_nr;
1483
1484   switch (get_irn_opcode(n)) {
1485   case iro_Div:
1486     b  = get_Div_right(n);
1487     tb = value_of(b);
1488
1489     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* div(x, c) && c != 0 */
1490       proj_nr = get_Proj_proj(proj);
1491
1492       /* this node may float */
1493       set_irn_pinned(n, op_pin_state_floats);
1494
1495       if (proj_nr == pn_Div_X_except) {
1496         /* we found an exception handler, remove it */
1497         return new_Bad();
1498       } else {
1499         /* the memory Proj can be removed */
1500         ir_node *res = get_Div_mem(n);
1501         set_Div_mem(n, get_irg_no_mem(current_ir_graph));
1502         if (proj_nr == pn_Div_M)
1503           return res;
1504       }
1505     }
1506     break;
1507   case iro_Mod:
1508     b  = get_Mod_right(n);
1509     tb = value_of(b);
1510
1511     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* mod(x, c) && c != 0 */
1512       proj_nr = get_Proj_proj(proj);
1513
1514       /* this node may float */
1515       set_irn_pinned(n, op_pin_state_floats);
1516
1517       if (proj_nr == pn_Mod_X_except) {
1518         /* we found an exception handler, remove it */
1519         return new_Bad();
1520       } else {
1521         /* the memory Proj can be removed */
1522         ir_node *res = get_Mod_mem(n);
1523         set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
1524         if (proj_nr == pn_Mod_M)
1525           return res;
1526       }
1527     }
1528     break;
1529   case iro_DivMod:
1530     b  = get_DivMod_right(n);
1531     tb = value_of(b);
1532
1533     if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) { /* DivMod(x, c) && c != 0 */
1534       proj_nr = get_Proj_proj(proj);
1535
1536       /* this node may float */
1537       set_irn_pinned(n, op_pin_state_floats);
1538
1539       if (proj_nr == pn_DivMod_X_except) {
1540         /* we found an exception handler, remove it */
1541         return new_Bad();
1542       }
1543       else {
1544         /* the memory Proj can be removed */
1545         ir_node *res = get_DivMod_mem(n);
1546         set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
1547         if (proj_nr == pn_DivMod_M)
1548           return res;
1549       }
1550     }
1551     break;
1552
1553   case iro_Cond:
1554     if (get_opt_unreachable_code()) {
1555       b = get_Cond_selector(n);
1556       tb = value_of(b);
1557
1558       if (tb != tarval_bad && mode_is_int(get_tarval_mode(tb))) {
1559         /* we have a constant switch */
1560         long num = get_Proj_proj(proj);
1561
1562         if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
1563           if (get_tarval_long(tb) == num) {
1564             /* Do NOT create a jump here, or we will have 2 control flow ops
1565              * in a block. This case is optimized away in optimize_cf(). */
1566             return proj;
1567           }
1568           else
1569             return new_Bad();
1570         }
1571       }
1572     }
1573     return proj;
1574
1575   case iro_Tuple:
1576     /* should not happen, but if it does will be optimized away */
1577     break;
1578
1579   default:
1580     /* do nothing */
1581     return proj;
1582   }
1583
1584   /* we have added a Tuple, optimize it for the current Proj away */
1585   return equivalent_node_Proj(proj);
1586 }
1587
1588 /**
1589  * returns the operands of a commutative bin-op, if one operand is
1590  * a const, it is returned as the second one.
1591  */
1592 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
1593 {
1594   ir_node *op_a = get_binop_left(binop);
1595   ir_node *op_b = get_binop_right(binop);
1596
1597   assert(is_op_commutative(get_irn_op(binop)));
1598
1599   if (get_irn_op(op_a) == op_Const) {
1600     *a = op_b;
1601     *c = op_a;
1602   }
1603   else {
1604     *a = op_a;
1605     *c = op_b;
1606   }
1607 }
1608
1609 /**
1610  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
1611  * Such pattern may arise in bitfield stores.
1612  *
1613  * value  c4                  value      c4 & c2
1614  *    AND     c3                    AND           c1 | c3
1615  *        OR     c2      ===>               OR
1616  *           AND    c1
1617  *               OR
1618  */
1619 static ir_node *transform_node_Or(ir_node *or)
1620 {
1621   ir_node *and, *c1;
1622   ir_node *or_l, *c2;
1623   ir_node *and_l, *c3;
1624   ir_node *value, *c4;
1625   ir_node *new_and, *new_const, *block;
1626   ir_mode *mode = get_irn_mode(or);
1627
1628   tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
1629
1630   get_comm_Binop_Ops(or, &and, &c1);
1631   if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
1632     return or;
1633
1634   get_comm_Binop_Ops(and, &or_l, &c2);
1635   if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
1636     return or;
1637
1638   get_comm_Binop_Ops(or_l, &and_l, &c3);
1639   if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
1640     return or;
1641
1642   get_comm_Binop_Ops(and_l, &value, &c4);
1643   if (get_irn_op(c4) != op_Const)
1644     return or;
1645
1646   /* ok, found the pattern, check for conditions */
1647   assert(mode == get_irn_mode(and));
1648   assert(mode == get_irn_mode(or_l));
1649   assert(mode == get_irn_mode(and_l));
1650
1651   tv1 = get_Const_tarval(c1);
1652   tv2 = get_Const_tarval(c2);
1653   tv3 = get_Const_tarval(c3);
1654   tv4 = get_Const_tarval(c4);
1655
1656   tv = tarval_or(tv4, tv2);
1657   if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
1658     /* have at least one 0 at the same bit position */
1659     return or;
1660   }
1661
1662   n_tv4 = tarval_not(tv4);
1663   if (tv3 != tarval_and(tv3, n_tv4)) {
1664     /* bit in the or_mask is outside the and_mask */
1665     return or;
1666   }
1667
1668   n_tv2 = tarval_not(tv2);
1669   if (tv1 != tarval_and(tv1, n_tv2)) {
1670     /* bit in the or_mask is outside the and_mask */
1671     return or;
1672   }
1673
1674   /* ok, all conditions met */
1675   block = get_nodes_block(or);
1676
1677   new_and = new_r_And(current_ir_graph, block,
1678       value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
1679
1680   new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
1681
1682   set_Or_left(or, new_and);
1683   set_Or_right(or, new_const);
1684
1685   /* check for more */
1686   return transform_node_Or(or);
1687 }
1688
1689 /* forward */
1690 static ir_node *transform_node(ir_node *n);
1691
1692 /**
1693  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl
1694  */
1695 static ir_node * transform_node_shift(ir_node *n)
1696 {
1697   ir_node *left;
1698   tarval *tv1, *tv2, *res;
1699   ir_mode *mode;
1700   int modulo_shf, flag;
1701
1702   left = get_binop_left(n);
1703
1704   /* different operations */
1705   if (get_irn_op(left) != get_irn_op(n))
1706     return n;
1707
1708   tv1 = value_of(get_binop_right(n));
1709   if (tv1 == tarval_bad)
1710     return n;
1711
1712   tv2 = value_of(get_binop_right(left));
1713   if (tv2 == tarval_bad)
1714     return n;
1715
1716   res = tarval_add(tv1, tv2);
1717
1718   /* beware: a simple replacement works only, if res < modulo shift */
1719   mode = get_irn_mode(n);
1720
1721   flag = 0;
1722
1723   modulo_shf = get_mode_modulo_shift(mode);
1724   if (modulo_shf > 0) {
1725     tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
1726
1727     if (tarval_cmp(res, modulo) & Lt)
1728       flag = 1;
1729   }
1730   else
1731     flag = 1;
1732
1733   if (flag) {
1734     /* ok, we can replace it */
1735     ir_node *in[2], *irn, *block = get_nodes_block(n);
1736
1737     in[0] = get_binop_left(left);
1738     in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
1739
1740     irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
1741
1742     return transform_node(irn);
1743   }
1744   return n;
1745 }
1746
1747
1748 /**
1749  * Tries several [inplace] [optimizing] transformations and returns an
1750  * equivalent node.  The difference to equivalent_node() is that these
1751  * transformations _do_ generate new nodes, and thus the old node must
1752  * not be freed even if the equivalent node isn't the old one.
1753  */
1754 static ir_node *transform_node(ir_node *n)
1755 {
1756   if (n->op->transform_node)
1757     n = n->op->transform_node(n);
1758   return n;
1759 }
1760
1761 /**
1762  * set the default transform node operation
1763  */
1764 static ir_op *firm_set_default_transform_node(ir_op *op)
1765 {
1766 #define CASE(a)                                 \
1767   case iro_##a:                                 \
1768     op->transform_node  = transform_node_##a;   \
1769     break
1770
1771   switch (op->code) {
1772   CASE(Add);
1773   CASE(Sub);
1774   CASE(Mul);
1775   CASE(Div);
1776   CASE(Mod);
1777   CASE(DivMod);
1778   CASE(Cond);
1779   CASE(Eor);
1780   CASE(Not);
1781   CASE(Cast);
1782   CASE(Proj);
1783   CASE(Or);
1784   case iro_Shr:
1785   case iro_Shrs:
1786   case iro_Shl:
1787     op->transform_node  = transform_node_shift;
1788     break;
1789   default:
1790     op->transform_node  = NULL;
1791   }
1792
1793   return op;
1794 #undef CASE
1795 }
1796
1797
1798 /* **************** Common Subexpression Elimination **************** */
1799
1800 /** The size of the hash table used, should estimate the number of nodes
1801     in a graph. */
1802 #define N_IR_NODES 512
1803
1804 /** Compares the attributes of two Const nodes. */
1805 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
1806 {
1807   return (get_Const_tarval(a) != get_Const_tarval(b))
1808       || (get_Const_type(a) != get_Const_type(b));
1809 }
1810
1811 /** Compares the attributes of two Proj nodes. */
1812 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
1813 {
1814     return get_irn_proj_attr (a) != get_irn_proj_attr (b);
1815 }
1816
1817 /** Compares the attributes of two Filter nodes. */
1818 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
1819 {
1820     return get_Filter_proj(a) != get_Filter_proj(b);
1821 }
1822
1823 /** Compares the attributes of two Alloc nodes. */
1824 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
1825 {
1826     return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
1827         || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
1828 }
1829
1830 /** Compares the attributes of two Free nodes. */
1831 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
1832 {
1833     return (get_irn_free_attr(a) != get_irn_free_attr(b));
1834 }
1835
1836 /** Compares the attributes of two SymConst nodes. */
1837 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
1838 {
1839     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
1840       || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
1841       || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
1842 }
1843
1844 /** Compares the attributes of two Call nodes. */
1845 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
1846 {
1847     return (get_irn_call_attr(a) != get_irn_call_attr(b));
1848 }
1849
1850 /** Compares the attributes of two Sel nodes. */
1851 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
1852 {
1853     return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
1854       || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
1855       || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
1856       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
1857       || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
1858 }
1859
1860 /** Compares the attributes of two Phi nodes. */
1861 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
1862 {
1863     return get_irn_phi_attr (a) != get_irn_phi_attr (b);
1864 }
1865
1866 /** Compares the attributes of two Cast nodes. */
1867 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
1868 {
1869     return get_Cast_type(a) != get_Cast_type(b);
1870 }
1871
1872 /** Compares the attributes of two Load nodes. */
1873 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
1874 {
1875   if (get_Load_volatility(a) == volatility_is_volatile ||
1876       get_Load_volatility(b) == volatility_is_volatile)
1877     /* NEVER do CSE on volatile Loads */
1878     return 1;
1879
1880   return get_Load_mode(a) != get_Load_mode(b);
1881 }
1882
1883 /** Compares the attributes of two Store nodes. */
1884 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
1885 {
1886   /* NEVER do CSE on volatile Stores */
1887   return (get_Store_volatility(a) == volatility_is_volatile ||
1888       get_Store_volatility(b) == volatility_is_volatile);
1889 }
1890
1891 /**
1892  * set the default node attribute compare operation
1893  */
1894 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
1895 {
1896 #define CASE(a)                             \
1897   case iro_##a:                             \
1898     op->node_cmp_attr  = node_cmp_attr_##a; \
1899     break
1900
1901   switch (op->code) {
1902   CASE(Const);
1903   CASE(Proj);
1904   CASE(Filter);
1905   CASE(Alloc);
1906   CASE(Free);
1907   CASE(SymConst);
1908   CASE(Call);
1909   CASE(Sel);
1910   CASE(Phi);
1911   CASE(Cast);
1912   CASE(Load);
1913   CASE(Store);
1914   default:
1915     op->node_cmp_attr  = NULL;
1916   }
1917
1918   return op;
1919 #undef CASE
1920 }
1921
1922 /**
1923  * Compare function for two nodes in the hash table. Gets two
1924  * nodes as parameters.  Returns 0 if the nodes are a cse.
1925  */
1926 static int
1927 vt_cmp (const void *elt, const void *key)
1928 {
1929   ir_node *a, *b;
1930   int i, irn_arity_a;
1931
1932   a = (void *)elt;
1933   b = (void *)key;
1934
1935   if (a == b) return 0;
1936
1937   if ((get_irn_op(a) != get_irn_op(b)) ||
1938       (get_irn_mode(a) != get_irn_mode(b))) return 1;
1939
1940   /* compare if a's in and b's in are of equal length */
1941   irn_arity_a = get_irn_intra_arity (a);
1942   if (irn_arity_a != get_irn_intra_arity(b))
1943     return 1;
1944
1945   /* for block-local cse and op_pin_state_pinned nodes: */
1946   if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
1947     if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
1948       return 1;
1949   }
1950
1951   /* compare a->in[0..ins] with b->in[0..ins] */
1952   for (i = 0; i < irn_arity_a; i++)
1953     if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
1954       return 1;
1955
1956   /*
1957    * here, we already now that the nodes are identical except their
1958    * attributes
1959    */
1960   if (a->op->node_cmp_attr)
1961     return a->op->node_cmp_attr(a, b);
1962
1963   return 0;
1964 }
1965
1966 /*
1967  * Calculate a hash value of a node.
1968  */
1969 unsigned
1970 ir_node_hash (ir_node *node)
1971 {
1972   unsigned h;
1973   int i, irn_arity;
1974
1975   if (node->op == op_Const) {
1976     /* special value for const, as they only differ in their tarval. */
1977     h = HASH_PTR(node->attr.con.tv);
1978     h = 9*h + HASH_PTR(get_irn_mode(node));
1979   } else if (node->op == op_SymConst) {
1980     /* special value for const, as they only differ in their symbol. */
1981     h = HASH_PTR(node->attr.i.sym.type_p);
1982     h = 9*h + HASH_PTR(get_irn_mode(node));
1983   } else {
1984
1985     /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
1986     h = irn_arity = get_irn_intra_arity(node);
1987
1988     /* consider all in nodes... except the block if not a control flow. */
1989     for (i =  is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
1990       h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
1991     }
1992
1993     /* ...mode,... */
1994     h = 9*h + HASH_PTR(get_irn_mode(node));
1995     /* ...and code */
1996     h = 9*h + HASH_PTR(get_irn_op(node));
1997   }
1998
1999   return h;
2000 }
2001
2002 pset *
2003 new_identities(void) {
2004   return new_pset(vt_cmp, N_IR_NODES);
2005 }
2006
2007 void
2008 del_identities(pset *value_table) {
2009   del_pset(value_table);
2010 }
2011
2012 /**
2013  * Return the canonical node computing the same value as n.
2014  * Looks up the node in a hash table.
2015  *
2016  * For Const nodes this is performed in the constructor, too.  Const
2017  * nodes are extremely time critical because of their frequent use in
2018  * constant string arrays.
2019  */
2020 static INLINE ir_node *
2021 identify (pset *value_table, ir_node *n)
2022 {
2023   ir_node *o = NULL;
2024
2025   if (!value_table) return n;
2026
2027   if (get_opt_reassociation()) {
2028     if (is_op_commutative(get_irn_op(n))) {
2029       ir_node *l = get_binop_left(n);
2030       ir_node *r = get_binop_right(n);
2031
2032       /* for commutative operators perform  a OP b == b OP a */
2033       if (l > r) {
2034         set_binop_left(n, r);
2035         set_binop_right(n, l);
2036       }
2037     }
2038   }
2039
2040   o = pset_find (value_table, n, ir_node_hash (n));
2041   if (!o) return n;
2042
2043   DBG_OPT_CSE(n, o);
2044
2045   return o;
2046 }
2047
2048 /**
2049  * During construction we set the op_pin_state_pinned flag in the graph right when the
2050  * optimization is performed.  The flag turning on procedure global cse could
2051  * be changed between two allocations.  This way we are safe.
2052  */
2053 static INLINE ir_node *
2054 identify_cons (pset *value_table, ir_node *n) {
2055   ir_node *old = n;
2056
2057   n = identify(value_table, n);
2058   if (get_irn_n(old, -1) != get_irn_n(n, -1))
2059     set_irg_pinned(current_ir_graph, op_pin_state_floats);
2060   return n;
2061 }
2062
2063 /**
2064  * Return the canonical node computing the same value as n.
2065  * Looks up the node in a hash table, enters it in the table
2066  * if it isn't there yet.
2067  */
2068 static ir_node *
2069 identify_remember (pset *value_table, ir_node *n)
2070 {
2071   ir_node *o = NULL;
2072
2073   if (!value_table) return n;
2074
2075   if (get_opt_reassociation()) {
2076     if (is_op_commutative(get_irn_op(n))) {
2077       ir_node *l = get_binop_left(n);
2078       ir_node *r = get_binop_right(n);
2079
2080       /* for commutative operators perform  a OP b == b OP a */
2081       if (l > r) {
2082         set_binop_left(n, r);
2083         set_binop_right(n, l);
2084       }
2085     }
2086   }
2087
2088   /* lookup or insert in hash table with given hash key. */
2089   o = pset_insert (value_table, n, ir_node_hash (n));
2090
2091   if (o != n) {
2092     DBG_OPT_CSE(n, o);
2093   }
2094
2095   return o;
2096 }
2097
2098 void
2099 add_identities (pset *value_table, ir_node *node) {
2100   if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
2101     identify_remember (value_table, node);
2102 }
2103
2104 /**
2105  * garbage in, garbage out. If a node has a dead input, i.e., the
2106  * Bad node is input to the node, return the Bad node.
2107  */
2108 static INLINE ir_node *
2109 gigo (ir_node *node)
2110 {
2111   int i, irn_arity;
2112   ir_op* op = get_irn_op(node);
2113
2114   /* remove garbage blocks by looking at control flow that leaves the block
2115      and replacing the control flow by Bad. */
2116   if (get_irn_mode(node) == mode_X) {
2117     ir_node *block = get_nodes_block(node);
2118     if (!get_Block_matured(block)) return node;  /* Don't optimize nodes in immature blocks. */
2119     if (op == op_End) return node;     /* Don't optimize End, may have Bads. */
2120
2121     if (get_irn_op(block) == op_Block && get_Block_matured(block)) {
2122       irn_arity = get_irn_arity(block);
2123       for (i = 0; i < irn_arity; i++) {
2124         if (!is_Bad(get_irn_n(block, i))) break;
2125       }
2126       if (i == irn_arity) return new_Bad();
2127     }
2128   }
2129
2130   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
2131      blocks predecessors is dead. */
2132   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
2133     irn_arity = get_irn_arity(node);
2134
2135     if (is_Block_dead(get_nodes_block(node)))
2136       return new_Bad();
2137
2138     for (i = 0; i < irn_arity; i++) {
2139       if (is_Bad(get_irn_n(node, i))) {
2140         return new_Bad();
2141       }
2142     }
2143   }
2144 #if 0
2145   /* With this code we violate the agreement that local_optimize
2146      only leaves Bads in Block, Phi and Tuple nodes. */
2147   /* If Block has only Bads as predecessors it's garbage. */
2148   /* If Phi has only Bads as predecessors it's garbage. */
2149   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
2150     irn_arity = get_irn_arity(node);
2151     for (i = 0; i < irn_arity; i++) {
2152       if (!is_Bad(get_irn_n(node, i))) break;
2153     }
2154     if (i == irn_arity) node = new_Bad();
2155   }
2156 #endif
2157   return node;
2158 }
2159
2160
2161 /**
2162  * These optimizations deallocate nodes from the obstack.
2163  * It can only be called if it is guaranteed that no other nodes
2164  * reference this one, i.e., right after construction of a node.
2165  */
2166 ir_node *
2167 optimize_node (ir_node *n)
2168 {
2169   tarval *tv;
2170   ir_node *oldn = n;
2171   opcode iro = get_irn_opcode(n);
2172
2173   type *old_tp = get_irn_type(n);
2174   {
2175     int i, arity = get_irn_arity(n);
2176     for (i = 0; i < arity && !old_tp; ++i)
2177       old_tp = get_irn_type(get_irn_n(n, i));
2178   }
2179
2180   /* Allways optimize Phi nodes: part of the construction. */
2181   if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
2182
2183   /* constant expression evaluation / constant folding */
2184   if (get_opt_constant_folding()) {
2185     /* constants can not be evaluated */
2186     if (iro != iro_Const) {
2187       /* try to evaluate */
2188       tv = computed_value(n);
2189       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
2190         /*
2191          * we MUST copy the node here temporary, because it's still needed
2192          * for DBG_OPT_CSTEVAL
2193          */
2194         int node_size = offsetof(ir_node, attr) +  n->op->attr_size;
2195         oldn = alloca(node_size);
2196
2197         memcpy(oldn, n, node_size);
2198     CLONE_ARR_A(ir_node *, oldn->in, n->in);
2199
2200     /* ARG, copy the in array, we need it for statistics */
2201     memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
2202
2203         /* evaluation was successful -- replace the node. */
2204         obstack_free (current_ir_graph->obst, n);
2205         n = new_Const (get_tarval_mode (tv), tv);
2206     if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
2207       set_Const_type(n, old_tp);
2208                                                  DBG_OPT_CSTEVAL(oldn, n);
2209         return n;
2210       }
2211     }
2212   }
2213
2214   /* remove unnecessary nodes */
2215   if (get_opt_constant_folding() ||
2216       (iro == iro_Phi)  ||   /* always optimize these nodes. */
2217       (iro == iro_Id)   ||
2218       (iro == iro_Proj) ||
2219       (iro == iro_Block)  )  /* Flags tested local. */
2220     n = equivalent_node (n);
2221
2222   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
2223
2224   /** common subexpression elimination **/
2225   /* Checks whether n is already available. */
2226   /* The block input is used to distinguish different subexpressions. Right
2227      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
2228      subexpressions within a block. */
2229   if (get_opt_cse())
2230     n = identify_cons (current_ir_graph->value_table, n);
2231
2232   if (n != oldn) {
2233     /* We found an existing, better node, so we can deallocate the old node. */
2234     obstack_free (current_ir_graph->obst, oldn);
2235
2236     return n;
2237   }
2238
2239   /* Some more constant expression evaluation that does not allow to
2240      free the node. */
2241   iro = get_irn_opcode(n);
2242   if (get_opt_constant_folding() ||
2243       (iro == iro_Cond) ||
2244       (iro == iro_Proj))     /* Flags tested local. */
2245     n = transform_node (n);
2246
2247   /* Remove nodes with dead (Bad) input.
2248      Run always for transformation induced Bads. */
2249   n = gigo (n);
2250
2251   /* Now we have a legal, useful node. Enter it in hash table for cse */
2252   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
2253     n = identify_remember (current_ir_graph->value_table, n);
2254   }
2255
2256   return n;
2257 }
2258
2259
2260 /**
2261  * These optimizations never deallocate nodes.  This can cause dead
2262  * nodes lying on the obstack.  Remove these by a dead node elimination,
2263  * i.e., a copying garbage collection.
2264  */
2265 ir_node *
2266 optimize_in_place_2 (ir_node *n)
2267 {
2268   tarval *tv;
2269   ir_node *oldn = n;
2270   opcode iro = get_irn_opcode(n);
2271
2272   type *old_tp = get_irn_type(n);
2273   {
2274     int i, arity = get_irn_arity(n);
2275     for (i = 0; i < arity && !old_tp; ++i)
2276       old_tp = get_irn_type(get_irn_n(n, i));
2277   }
2278
2279   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
2280
2281   /* if not optimize return n */
2282   if (n == NULL) {
2283     assert(0);
2284     /* Here this is possible.  Why? */
2285     return n;
2286   }
2287
2288   /* constant expression evaluation / constant folding */
2289   if (get_opt_constant_folding()) {
2290     /* constants can not be evaluated */
2291     if (iro != iro_Const) {
2292       /* try to evaluate */
2293       tv = computed_value(n);
2294       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
2295         /* evaluation was successful -- replace the node. */
2296         n = new_Const (get_tarval_mode (tv), tv);
2297
2298     if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
2299       set_Const_type(n, old_tp);
2300
2301         DBG_OPT_CSTEVAL(oldn, n);
2302         return n;
2303       }
2304     }
2305   }
2306
2307   /* remove unnecessary nodes */
2308   if (get_opt_constant_folding() ||
2309       (iro == iro_Phi)  ||   /* always optimize these nodes. */
2310       (iro == iro_Id)   ||   /* ... */
2311       (iro == iro_Proj) ||   /* ... */
2312       (iro == iro_Block)  )  /* Flags tested local. */
2313     n = equivalent_node (n);
2314
2315   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
2316
2317   /** common subexpression elimination **/
2318   /* Checks whether n is already available. */
2319   /* The block input is used to distinguish different subexpressions.  Right
2320      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
2321      subexpressions within a block. */
2322   if (get_opt_cse()) {
2323     n = identify (current_ir_graph->value_table, n);
2324   }
2325
2326   /* Some more constant expression evaluation. */
2327   iro = get_irn_opcode(n);
2328   if (get_opt_constant_folding() ||
2329       (iro == iro_Cond) ||
2330       (iro == iro_Proj))     /* Flags tested local. */
2331     n = transform_node (n);
2332
2333   /* Remove nodes with dead (Bad) input.
2334      Run always for transformation induced Bads.  */
2335   n = gigo (n);
2336
2337   /* Now we can verify the node, as it has no dead inputs any more. */
2338   irn_vrfy(n);
2339
2340   /* Now we have a legal, useful node. Enter it in hash table for cse.
2341      Blocks should be unique anyways.  (Except the successor of start:
2342      is cse with the start block!) */
2343   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
2344     n = identify_remember (current_ir_graph->value_table, n);
2345
2346   return n;
2347 }
2348
2349 /**
2350  * Wrapper for external use, set proper status bits after optimization.
2351  */
2352 ir_node *
2353 optimize_in_place (ir_node *n)
2354 {
2355   /* Handle graph state */
2356   assert(get_irg_phase_state(current_ir_graph) != phase_building);
2357
2358   if (get_opt_global_cse())
2359     set_irg_pinned(current_ir_graph, op_pin_state_floats);
2360   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
2361     set_irg_outs_inconsistent(current_ir_graph);
2362
2363   /* Maybe we could also test whether optimizing the node can
2364      change the control graph. */
2365   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
2366     set_irg_dom_inconsistent(current_ir_graph);
2367   return optimize_in_place_2 (n);
2368 }
2369
2370 /**
2371  * set the default ir op operations
2372  */
2373 ir_op *firm_set_default_operations(ir_op *op)
2374 {
2375   op = firm_set_default_computed_value(op);
2376   op = firm_set_default_equivalent_node(op);
2377   op = firm_set_default_transform_node(op);
2378   op = firm_set_default_node_cmp_attr(op);
2379
2380   return op;
2381 }