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