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