firmjni does not like two similar enums.
[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-2005 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 "iredges_t.h"
30 # include "irmode_t.h"
31 # include "iropt_t.h"
32 # include "ircons_t.h"
33 # include "irgmod.h"
34 # include "irvrfy.h"
35 # include "tv_t.h"
36 # include "dbginfo_t.h"
37 # include "iropt_dbg.h"
38 # include "irflag_t.h"
39 # include "irhooks.h"
40 # include "irarch.h"
41 # include "hashptr.h"
42 # include "archop.h"
43 # include "opt_polymorphy.h"
44
45 /* Make types visible to allow most efficient access */
46 # include "entity_t.h"
47
48 /**
49  * Trivial INLINEable routine for copy propagation.
50  * Does follow Ids, needed to optimize INLINEd code.
51  */
52 static INLINE ir_node *
53 follow_Id (ir_node *n)
54 {
55   while (get_irn_op (n) == op_Id) n = get_Id_pred (n);
56   return n;
57 }
58
59 /**
60  * return the value of a Constant
61  */
62 static tarval *computed_value_Const(ir_node *n)
63 {
64   return get_Const_tarval(n);
65 }
66
67 /**
68  * return the value of a 'sizeof' SymConst
69  */
70 static tarval *computed_value_SymConst(ir_node *n)
71 {
72   if ((get_SymConst_kind(n) == symconst_size) &&
73       (get_type_state(get_SymConst_type(n))) == layout_fixed)
74     return new_tarval_from_long(get_type_size_bytes(get_SymConst_type(n)), get_irn_mode(n));
75   return tarval_bad;
76 }
77
78 /**
79  * return the value of an Add
80  */
81 static tarval *computed_value_Add(ir_node *n)
82 {
83   ir_node *a = get_Add_left(n);
84   ir_node *b = get_Add_right(n);
85
86   tarval *ta = value_of(a);
87   tarval *tb = value_of(b);
88
89   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
90     return tarval_add(ta, tb);
91
92   return tarval_bad;
93 }
94
95 /**
96  * return the value of a Sub
97  * Special case: a - a
98  */
99 static tarval *computed_value_Sub(ir_node *n)
100 {
101   ir_node *a = get_Sub_left(n);
102   ir_node *b = get_Sub_right(n);
103   tarval *ta;
104   tarval *tb;
105
106   /* a - a */
107   if (a == b && !is_Bad(a))
108     return get_mode_null(get_irn_mode(n));
109
110   ta = value_of(a);
111   tb = value_of(b);
112
113   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
114     return tarval_sub(ta, tb);
115
116   return tarval_bad;
117 }
118
119 /**
120  * return the value of an unary Minus
121  */
122 static tarval *computed_value_Minus(ir_node *n)
123 {
124   ir_node *a = get_Minus_op(n);
125   tarval *ta = value_of(a);
126
127   if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
128     return tarval_neg(ta);
129
130   return tarval_bad;
131 }
132
133 /**
134  * return the value of a Mul
135  */
136 static tarval *computed_value_Mul(ir_node *n)
137 {
138   ir_node *a = get_Mul_left(n);
139   ir_node *b = get_Mul_right(n);
140
141   tarval *ta = value_of(a);
142   tarval *tb = value_of(b);
143
144   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
145     return tarval_mul(ta, tb);
146   } else {
147     /* a*0 = 0 or 0*b = 0:
148        calls computed_value recursive and returns the 0 with proper
149        mode. */
150     if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
151       return ta;
152     if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
153       return tb;
154   }
155   return tarval_bad;
156 }
157
158 /**
159  * return the value of a floating point Quot
160  */
161 static tarval *computed_value_Quot(ir_node *n)
162 {
163   ir_node *a = get_Quot_left(n);
164   ir_node *b = get_Quot_right(n);
165
166   tarval *ta = value_of(a);
167   tarval *tb = value_of(b);
168
169   /* This was missing in original implementation. Why? */
170   if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
171     if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
172       return tarval_quo(ta, tb);
173   }
174   return tarval_bad;
175 }
176
177 /**
178  * calculate the value of an integer Div of two nodes
179  * Special case: 0 / b
180  */
181 static tarval *do_computed_value_Div(ir_node *a, ir_node *b)
182 {
183   tarval *ta = value_of(a);
184   tarval *tb = value_of(b);
185
186   /* Compute c1 / c2 or 0 / a, a != 0 */
187   if (ta != tarval_bad) {
188     if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b))))   /* div by zero: return tarval_bad */
189       return tarval_div(ta, tb);
190     else if (ta == get_mode_null(get_tarval_mode(ta)))  /* 0 / b == 0 */
191       return ta;
192   }
193   return tarval_bad;
194 }
195
196 /**
197  * return the value of an integer Div
198  */
199 static tarval *computed_value_Div(ir_node *n)
200 {
201   return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
202 }
203
204 /**
205  * calculate the value of an integer Mod of two nodes
206  * Special case: a % 1
207  */
208 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b)
209 {
210   tarval *ta = value_of(a);
211   tarval *tb = value_of(b);
212
213   /* Compute c1 % c2 or a % 1 */
214   if (tb != tarval_bad) {
215     if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb))))   /* div by zero: return tarval_bad */
216       return tarval_mod(ta, tb);
217     else if (tb == get_mode_one(get_tarval_mode(tb)))    /* x mod 1 == 0 */
218       return get_mode_null(get_irn_mode(a));
219   }
220
221   return tarval_bad;
222 }
223
224 /**
225  * return the value of an integer Mod
226  */
227 static tarval *computed_value_Mod(ir_node *n)
228 {
229   return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
230 }
231
232 /**
233  * return the value of an Abs
234  */
235 static tarval *computed_value_Abs(ir_node *n)
236 {
237   ir_node *a = get_Abs_op(n);
238   tarval *ta = value_of(a);
239
240   if (ta != tarval_bad)
241     return tarval_abs(ta);
242
243   return tarval_bad;
244 }
245
246 /**
247  * return the value of an And
248  * Special case: a & 0, 0 & b
249  */
250 static tarval *computed_value_And(ir_node *n)
251 {
252   ir_node *a = get_And_left(n);
253   ir_node *b = get_And_right(n);
254
255   tarval *ta = value_of(a);
256   tarval *tb = value_of(b);
257
258   if ((ta != tarval_bad) && (tb != tarval_bad)) {
259     return tarval_and (ta, tb);
260   } else {
261     tarval *v;
262
263     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
264         || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
265       return v;
266     }
267   }
268   return tarval_bad;
269 }
270
271 /**
272  * return the value of an Or
273  * Special case: a | 1...1, 1...1 | b
274  */
275 static tarval *computed_value_Or(ir_node *n)
276 {
277   ir_node *a = get_Or_left(n);
278   ir_node *b = get_Or_right(n);
279
280   tarval *ta = value_of(a);
281   tarval *tb = value_of(b);
282
283   if ((ta != tarval_bad) && (tb != tarval_bad)) {
284     return tarval_or (ta, tb);
285   } else {
286     tarval *v;
287     if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
288         || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
289       return v;
290     }
291   }
292   return tarval_bad;
293 }
294
295 /**
296  * return the value of an Eor
297  */
298 static tarval *computed_value_Eor(ir_node *n)
299 {
300   ir_node *a = get_Eor_left(n);
301   ir_node *b = get_Eor_right(n);
302
303   tarval *ta, *tb;
304
305   if (a == b)
306     return get_mode_null(get_irn_mode(n));
307
308   ta = value_of(a);
309   tb = value_of(b);
310
311   if ((ta != tarval_bad) && (tb != tarval_bad)) {
312     return tarval_eor (ta, tb);
313   }
314   return tarval_bad;
315 }
316
317 /**
318  * return the value of a Not
319  */
320 static tarval *computed_value_Not(ir_node *n)
321 {
322   ir_node *a = get_Not_op(n);
323   tarval *ta = value_of(a);
324
325   if (ta != tarval_bad)
326     return tarval_not(ta);
327
328   return tarval_bad;
329 }
330
331 /**
332  * return the value of a Shl
333  */
334 static tarval *computed_value_Shl(ir_node *n)
335 {
336   ir_node *a = get_Shl_left(n);
337   ir_node *b = get_Shl_right(n);
338
339   tarval *ta = value_of(a);
340   tarval *tb = value_of(b);
341
342   if ((ta != tarval_bad) && (tb != tarval_bad)) {
343     return tarval_shl (ta, tb);
344   }
345   return tarval_bad;
346 }
347
348 /**
349  * return the value of a Shr
350  */
351 static tarval *computed_value_Shr(ir_node *n)
352 {
353   ir_node *a = get_Shr_left(n);
354   ir_node *b = get_Shr_right(n);
355
356   tarval *ta = value_of(a);
357   tarval *tb = value_of(b);
358
359   if ((ta != tarval_bad) && (tb != tarval_bad)) {
360     return tarval_shr (ta, tb);
361   }
362   return tarval_bad;
363 }
364
365 /**
366  * return the value of a Shrs
367  */
368 static tarval *computed_value_Shrs(ir_node *n)
369 {
370   ir_node *a = get_Shrs_left(n);
371   ir_node *b = get_Shrs_right(n);
372
373   tarval *ta = value_of(a);
374   tarval *tb = value_of(b);
375
376   if ((ta != tarval_bad) && (tb != tarval_bad)) {
377     return tarval_shrs (ta, tb);
378   }
379   return tarval_bad;
380 }
381
382 /**
383  * return the value of a Rot
384  */
385 static tarval *computed_value_Rot(ir_node *n)
386 {
387   ir_node *a = get_Rot_left(n);
388   ir_node *b = get_Rot_right(n);
389
390   tarval *ta = value_of(a);
391   tarval *tb = value_of(b);
392
393   if ((ta != tarval_bad) && (tb != tarval_bad)) {
394     return tarval_rot (ta, tb);
395   }
396   return tarval_bad;
397 }
398
399 /**
400  * return the value of a Conv
401  */
402 static tarval *computed_value_Conv(ir_node *n)
403 {
404   ir_node *a = get_Conv_op(n);
405   tarval *ta = value_of(a);
406
407   if (ta != tarval_bad)
408     return tarval_convert_to(ta, get_irn_mode(n));
409
410   return tarval_bad;
411 }
412
413 /**
414  * return the value of a Proj(Cmp)
415  *
416  * This performs a first step of unreachable code elimination.
417  * Proj can not be computed, but folding a Cmp above the Proj here is
418  * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
419  * only 1 is used.
420  * There are several case where we can evaluate a Cmp node, see later.
421  */
422 static tarval *computed_value_Proj_Cmp(ir_node *n)
423 {
424   ir_node *a   = get_Proj_pred(n);
425   ir_node *aa  = get_Cmp_left(a);
426   ir_node *ab  = get_Cmp_right(a);
427   long proj_nr = get_Proj_proj(n);
428
429   /*
430    * BEWARE: a == a is NOT always True for floating Point values, as
431    * NaN != NaN is defined, so we must check this here.
432    */
433   if (aa == ab && (
434       !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt ||  proj_nr == pn_Cmp_Gt)
435       ) { /* 1.: */
436
437     /* This is a trick with the bits used for encoding the Cmp
438        Proj numbers, the following statement is not the same:
439     return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
440     return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
441   }
442   else {
443     tarval *taa = value_of(aa);
444     tarval *tab = value_of(ab);
445     ir_mode *mode = get_irn_mode(aa);
446
447     /*
448      * The predecessors of Cmp are target values.  We can evaluate
449      * the Cmp.
450      */
451     if ((taa != tarval_bad) && (tab != tarval_bad)) {
452       /* strange checks... */
453       pn_Cmp flags = tarval_cmp(taa, tab);
454       if (flags != pn_Cmp_False) {
455         return new_tarval_from_long (proj_nr & flags, mode_b);
456       }
457     }
458     /* for integer values, we can check against MIN/MAX */
459     else if (mode_is_int(mode)) {
460       /* MIN <=/> x.  This results in true/false. */
461       if (taa == get_mode_min(mode)) {
462         /* a compare with the MIN value */
463         if (proj_nr == pn_Cmp_Le)
464           return get_tarval_b_true();
465         else if (proj_nr == pn_Cmp_Gt)
466           return get_tarval_b_false();
467       }
468       /* x >=/< MIN.  This results in true/false. */
469       else
470       if (tab == get_mode_min(mode)) {
471         /* a compare with the MIN value */
472         if (proj_nr == pn_Cmp_Ge)
473           return get_tarval_b_true();
474         else if (proj_nr == pn_Cmp_Lt)
475           return get_tarval_b_false();
476       }
477       /* MAX >=/< x.  This results in true/false. */
478       else if (taa == get_mode_max(mode)) {
479         if (proj_nr == pn_Cmp_Ge)
480           return get_tarval_b_true();
481         else if (proj_nr == pn_Cmp_Lt)
482           return get_tarval_b_false();
483       }
484       /* x <=/> MAX.  This results in true/false. */
485       else if (tab == get_mode_max(mode)) {
486         if (proj_nr == pn_Cmp_Le)
487           return get_tarval_b_true();
488         else if (proj_nr == pn_Cmp_Gt)
489           return get_tarval_b_false();
490       }
491     }
492     /*
493      * The predecessors are Allocs or (void*)(0) constants.  Allocs never
494      * return NULL, they raise an exception.   Therefore we can predict
495      * the Cmp result.
496      */
497     else {
498       ir_node *aaa = skip_Id(skip_Proj(aa));
499       ir_node *aba = skip_Id(skip_Proj(ab));
500
501       if (   (   (/* aa is ProjP and aaa is Alloc */
502                      (get_irn_op(aa) == op_Proj)
503                   && (mode_is_reference(get_irn_mode(aa)))
504                   && (get_irn_op(aaa) == op_Alloc))
505               && (   (/* ab is NULL */
506                          (get_irn_op(ab) == op_Const)
507                       && (mode_is_reference(get_irn_mode(ab)))
508                       && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
509                   || (/* ab is other Alloc */
510                          (get_irn_op(ab) == op_Proj)
511                       && (mode_is_reference(get_irn_mode(ab)))
512                       && (get_irn_op(aba) == op_Alloc)
513                       && (aaa != aba))))
514           || (/* aa is NULL and aba is Alloc */
515                  (get_irn_op(aa) == op_Const)
516               && (mode_is_reference(get_irn_mode(aa)))
517               && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
518               && (get_irn_op(ab) == op_Proj)
519               && (mode_is_reference(get_irn_mode(ab)))
520               && (get_irn_op(aba) == op_Alloc)))
521         /* 3.: */
522         return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
523     }
524   }
525   return tarval_bad;
526 }
527
528 /**
529  * return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod), Proj(DivMod)
530  */
531 static tarval *computed_value_Proj(ir_node *n)
532 {
533   ir_node *a = get_Proj_pred(n);
534   long proj_nr;
535
536   switch (get_irn_opcode(a)) {
537   case iro_Cmp:
538     return computed_value_Proj_Cmp(n);
539
540   case iro_DivMod:
541     /* compute either the Div or the Mod part */
542     proj_nr = get_Proj_proj(n);
543     if (proj_nr == pn_DivMod_res_div)
544       return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
545     else if (proj_nr == pn_DivMod_res_mod)
546       return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
547     break;
548
549   case iro_Div:
550     if (get_Proj_proj(n) == pn_Div_res)
551       return computed_value(a);
552     break;
553
554   case iro_Mod:
555     if (get_Proj_proj(n) == pn_Mod_res)
556       return computed_value(a);
557     break;
558
559   default:
560     return tarval_bad;
561   }
562   return tarval_bad;
563 }
564
565 /**
566  * calculate the value of a Mux: can be evaluated, if the
567  * sel and the right input are known
568  */
569 static tarval *computed_value_Mux(ir_node *n)
570 {
571   ir_node *sel = get_Mux_sel(n);
572   tarval *ts = value_of(sel);
573
574   if (ts == get_tarval_b_true()) {
575     ir_node *v = get_Mux_true(n);
576     return value_of(v);
577   }
578   else if (ts == get_tarval_b_false()) {
579     ir_node *v = get_Mux_false(n);
580     return value_of(v);
581   }
582   return tarval_bad;
583 }
584
585 /**
586  * If the parameter n can be computed, return its value, else tarval_bad.
587  * Performs constant folding.
588  *
589  * @param n  The node this should be evaluated
590  */
591 tarval *computed_value(ir_node *n)
592 {
593   if (n->op->computed_value)
594     return n->op->computed_value(n);
595   return tarval_bad;
596 }
597
598 /**
599  * set the default computed_value evaluator
600  */
601 static ir_op *firm_set_default_computed_value(ir_op *op)
602 {
603 #define CASE(a)                               \
604   case iro_##a:                               \
605     op->computed_value  = computed_value_##a; \
606     break
607
608   switch (op->code) {
609   CASE(Const);
610   CASE(SymConst);
611   CASE(Add);
612   CASE(Sub);
613   CASE(Minus);
614   CASE(Mul);
615   CASE(Quot);
616   CASE(Div);
617   CASE(Mod);
618   CASE(Abs);
619   CASE(And);
620   CASE(Or);
621   CASE(Eor);
622   CASE(Not);
623   CASE(Shl);
624   CASE(Shr);
625   CASE(Shrs);
626   CASE(Rot);
627   CASE(Conv);
628   CASE(Proj);
629   CASE(Mux);
630   default:
631     op->computed_value  = NULL;
632   }
633
634   return op;
635 #undef CASE
636 }
637
638 #if 0
639 /* returns 1 if the a and b are pointers to different locations. */
640 static bool
641 different_identity (ir_node *a, ir_node *b)
642 {
643   assert (mode_is_reference(get_irn_mode (a))
644           && mode_is_reference(get_irn_mode (b)));
645
646   if (get_irn_op (a) == op_Proj && get_irn_op(b) == op_Proj) {
647     ir_node *a1 = get_Proj_pred (a);
648     ir_node *b1 = get_Proj_pred (b);
649     if (a1 != b1 && get_irn_op (a1) == op_Alloc
650                 && get_irn_op (b1) == op_Alloc)
651       return 1;
652   }
653   return 0;
654 }
655 #endif
656
657 /**
658  * Returns a equivalent block for another block.
659  * If the block has only one predecessor, this is
660  * the equivalent one. If the only predecessor of a block is
661  * the block itself, this is a dead block.
662  *
663  * If both predecessors of a block are the branches of a binary
664  * Cond, the equivalent block is Cond's block.
665  *
666  * If all predecessors of a block are bad or lies in a dead
667  * block, the current block is dead as well.
668  *
669  * Note, that blocks are NEVER turned into Bad's, instead
670  * the dead_block flag is set. So, never test for is_Bad(block),
671  * always use is_dead_Block(block).
672  */
673 static ir_node *equivalent_node_Block(ir_node *n)
674 {
675   ir_node *oldn = n;
676   int n_preds   = get_Block_n_cfgpreds(n);
677
678   /* The Block constructor does not call optimize, but mature_immBlock
679      calls the optimization. */
680   assert(get_Block_matured(n));
681
682   /* Straightening: a single entry Block following a single exit Block
683      can be merged, if it is not the Start block. */
684   /* !!! Beware, all Phi-nodes of n must have been optimized away.
685      This should be true, as the block is matured before optimize is called.
686      But what about Phi-cycles with the Phi0/Id that could not be resolved?
687      Remaining Phi nodes are just Ids. */
688    if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
689      ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
690      if (predblock == oldn) {
691        /* Jmp jumps into the block it is in -- deal self cycle. */
692        n = set_Block_dead(n);
693        DBG_OPT_DEAD(oldn, n);
694      } else if (get_opt_control_flow_straightening()) {
695        n = predblock;
696        DBG_OPT_STG(oldn, n);
697      }
698    }
699    else if ((n_preds == 1) &&
700             (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
701      ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
702      if (predblock == oldn) {
703        /* Jmp jumps into the block it is in -- deal self cycle. */
704        n = set_Block_dead(n);
705        DBG_OPT_DEAD(oldn, n);
706      }
707    }
708    else if ((n_preds == 2) &&
709             (get_opt_control_flow_weak_simplification())) {
710     /* Test whether Cond jumps twice to this block
711        @@@ we could do this also with two loops finding two preds from several ones. */
712     ir_node *a = get_Block_cfgpred(n, 0);
713     ir_node *b = get_Block_cfgpred(n, 1);
714
715     if ((get_irn_op(a) == op_Proj) &&
716         (get_irn_op(b) == op_Proj) &&
717         (get_Proj_pred(a) == get_Proj_pred(b)) &&
718         (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
719         (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
720       /* Also a single entry Block following a single exit Block.  Phis have
721          twice the same operand and will be optimized away. */
722       n = get_nodes_block(a);
723       DBG_OPT_IFSIM(oldn, a, b, n);
724     }
725   } else if (get_opt_unreachable_code() &&
726              (n != current_ir_graph->start_block) &&
727              (n != current_ir_graph->end_block)     ) {
728     int i, n_cfg = get_Block_n_cfgpreds(n);
729
730     /* If all inputs are dead, this block is dead too, except if it is
731        the start or end block.  This is a step of unreachable code
732        elimination */
733     for (i = 0; i < n_cfg; i++) {
734       ir_node *pred = get_Block_cfgpred(n, i);
735       ir_node *pred_blk;
736
737       if (is_Bad(pred)) continue;
738       pred_blk = get_nodes_block(pred);
739
740       if (is_Block_dead(pred_blk)) continue;
741
742       if (pred_blk != n) {
743         /* really found a living input */
744         break;
745       }
746     }
747     if (i == n_cfg)
748       n = set_Block_dead(n);
749   }
750
751   return n;
752 }
753
754 /**
755  * Returns a equivalent node for a Jmp, a Bad :-)
756  * Of course this only happens if the Block of the Jmp is Bad.
757  */
758 static ir_node *equivalent_node_Jmp(ir_node *n)
759 {
760   /* GL: Why not same for op_Raise?? */
761   /* unreachable code elimination */
762   if (is_Block_dead(get_nodes_block(n)))
763     n = new_Bad();
764
765   return n;
766 }
767
768 static ir_node *equivalent_node_Cond(ir_node *n)
769 {
770   /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
771      See cases for iro_Cond and iro_Proj in transform_node(). */
772   return n;
773 }
774
775 /**
776  * optimize operations that are commutative and have neutral 0,
777  * so a op 0 = 0 op a = a.
778  */
779 static ir_node *equivalent_node_neutral_zero(ir_node *n)
780 {
781   ir_node *oldn = n;
782
783   ir_node *a = get_binop_left(n);
784   ir_node *b = get_binop_right(n);
785
786   tarval *tv;
787   ir_node *on;
788
789   /* After running compute_node there is only one constant predecessor.
790      Find this predecessors value and remember the other node: */
791   if ((tv = value_of(a)) != tarval_bad) {
792     on = b;
793   } else if ((tv = value_of(b)) != tarval_bad) {
794     on = a;
795   } else
796     return n;
797
798   /* If this predecessors constant value is zero, the operation is
799      unnecessary. Remove it: */
800   if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
801     n = on;
802
803     DBG_OPT_ALGSIM1(oldn, a, b, n);
804   }
805
806   return n;
807 }
808
809 #define equivalent_node_Add  equivalent_node_neutral_zero
810 #define equivalent_node_Eor  equivalent_node_neutral_zero
811
812 /**
813  * optimize operations that are not commutative but have neutral 0 on left,
814  * so a op 0 = a.
815  */
816 static ir_node *equivalent_node_left_zero(ir_node *n)
817 {
818   ir_node *oldn = n;
819
820   ir_node *a = get_binop_left(n);
821   ir_node *b = get_binop_right(n);
822
823   if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
824     n = a;
825
826     DBG_OPT_ALGSIM1(oldn, a, b, n);
827   }
828
829   return n;
830 }
831
832 #define equivalent_node_Sub   equivalent_node_left_zero
833 #define equivalent_node_Shl   equivalent_node_left_zero
834 #define equivalent_node_Shr   equivalent_node_left_zero
835 #define equivalent_node_Shrs  equivalent_node_left_zero
836 #define equivalent_node_Rot   equivalent_node_left_zero
837
838 /**
839  * Optimize an "idempotent unary op", ie op(op(n)) = n.
840  *
841  * @fixme -(-a) == a, but might overflow two times.
842  * We handle it anyway here but the better way would be a
843  * flag. This would be needed for Pascal for instance.
844  */
845 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
846 {
847   ir_node *oldn = n;
848   ir_node *pred = get_unop_op(n);
849
850   /* optimize symmetric unop */
851   if (get_irn_op(pred) == get_irn_op(n)) {
852     n = get_unop_op(pred);
853     DBG_OPT_ALGSIM2(oldn, pred, n);
854   }
855   return n;
856 }
857
858 /* Not(Not(x)) == x */
859 #define equivalent_node_Not    equivalent_node_idempotent_unop
860
861 /* --x == x */  /* ??? Is this possible or can --x raise an
862                        out of bounds exception if min =! max? */
863 #define equivalent_node_Minus  equivalent_node_idempotent_unop
864
865 /**
866  * Optimize a * 1 = 1 * a = a.
867  */
868 static ir_node *equivalent_node_Mul(ir_node *n)
869 {
870   ir_node *oldn = n;
871
872   ir_node *a = get_Mul_left(n);
873   ir_node *b = get_Mul_right(n);
874
875   /* Mul is commutative and has again an other neutral element. */
876   if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
877     n = b;
878     DBG_OPT_ALGSIM1(oldn, a, b, n);
879   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
880     n = a;
881     DBG_OPT_ALGSIM1(oldn, a, b, n);
882   }
883   return n;
884 }
885
886 /**
887  * Optimize a / 1 = a.
888  */
889 static ir_node *equivalent_node_Div(ir_node *n)
890 {
891   ir_node *a = get_Div_left(n);
892   ir_node *b = get_Div_right(n);
893
894   /* Div is not commutative. */
895   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
896     /* Turn Div into a tuple (mem, bad, a) */
897     ir_node *mem = get_Div_mem(n);
898     turn_into_tuple(n, 3);
899     set_Tuple_pred(n, pn_Div_M,        mem);
900     set_Tuple_pred(n, pn_Div_X_except, new_Bad());        /* no exception */
901     set_Tuple_pred(n, pn_Div_res,      a);
902   }
903   return n;
904 }
905
906 /**
907  * Optimize a / 1 = a.
908  */
909 static ir_node *equivalent_node_DivMod(ir_node *n)
910 {
911   ir_node *a = get_DivMod_left(n);
912   ir_node *b = get_DivMod_right(n);
913
914   /* Div is not commutative. */
915   if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
916     /* Turn DivMod into a tuple (mem, bad, a, 0) */
917     ir_node *mem = get_Div_mem(n);
918     ir_mode *mode = get_irn_mode(b);
919
920     turn_into_tuple(n, 4);
921     set_Tuple_pred(n, pn_DivMod_M,        mem);
922     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());        /* no exception */
923     set_Tuple_pred(n, pn_DivMod_res_div,  a);
924     set_Tuple_pred(n, pn_DivMod_res_mod,  new_Const(mode, get_mode_null(mode)));
925   }
926   return n;
927 }
928
929 /**
930  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
931  */
932 static ir_node *equivalent_node_Or(ir_node *n)
933 {
934   ir_node *oldn = n;
935
936   ir_node *a = get_Or_left(n);
937   ir_node *b = get_Or_right(n);
938
939   if (a == b) {
940     n = a;    /* Or has it's own neutral element */
941   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
942     n = b;
943     DBG_OPT_ALGSIM1(oldn, a, b, n);
944   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
945     n = a;
946     DBG_OPT_ALGSIM1(oldn, a, b, n);
947   }
948
949   return n;
950 }
951
952 /**
953  * Optimize a & 0b1...1 = 0b1...1 & a =  a & a = a.
954  */
955 static ir_node *equivalent_node_And(ir_node *n)
956 {
957   ir_node *oldn = n;
958
959   ir_node *a = get_And_left(n);
960   ir_node *b = get_And_right(n);
961
962   if (a == b) {
963     n = a;    /* And has it's own neutral element */
964   } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
965     n = b;
966     DBG_OPT_ALGSIM1(oldn, a, b, n);
967   } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
968     n = a;
969     DBG_OPT_ALGSIM1(oldn, a, b, n);
970   }
971   return n;
972 }
973
974 /**
975  * Try to remove useless Conv's:
976  */
977 static ir_node *equivalent_node_Conv(ir_node *n)
978 {
979   ir_node *oldn = n;
980   ir_node *a = get_Conv_op(n);
981   ir_node *b;
982
983   ir_mode *n_mode = get_irn_mode(n);
984   ir_mode *a_mode = get_irn_mode(a);
985
986   if (n_mode == a_mode) { /* No Conv necessary */
987     n = a;
988     DBG_OPT_ALGSIM3(oldn, a, n);
989   } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
990     ir_mode *b_mode;
991
992     b = get_Conv_op(a);
993     n_mode = get_irn_mode(n);
994     b_mode = get_irn_mode(b);
995
996     if (n_mode == b_mode) {
997       if (n_mode == mode_b) {
998         n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
999         DBG_OPT_ALGSIM1(oldn, a, b, n);
1000       }
1001       else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1002         if (smaller_mode(b_mode, a_mode)){
1003           n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1004           DBG_OPT_ALGSIM1(oldn, a, b, n);
1005         }
1006       }
1007     }
1008   }
1009   return n;
1010 }
1011
1012 /**
1013  * A Cast may be removed if the type of the previous node
1014  * is already the type of the Cast.
1015  */
1016 static ir_node *equivalent_node_Cast(ir_node *n) {
1017   ir_node *pred = get_Cast_op(n);
1018   if (get_irn_type(pred) == get_Cast_type(n))
1019     n = pred;
1020   return n;
1021 }
1022
1023 /* Several optimizations:
1024    - no Phi in start block.
1025    - remove Id operators that are inputs to Phi
1026    - fold Phi-nodes, iff they have only one predecessor except
1027            themselves.
1028 */
1029 static ir_node *equivalent_node_Phi(ir_node *n)
1030 {
1031   int i, n_preds;
1032
1033   ir_node *oldn = n;
1034   ir_node *block = NULL;     /* to shutup gcc */
1035   ir_node *first_val = NULL; /* to shutup gcc */
1036   ir_node *scnd_val = NULL;  /* to shutup gcc */
1037
1038   if (!get_opt_normalize()) return n;
1039
1040   n_preds = get_Phi_n_preds(n);
1041
1042   block = get_nodes_block(n);
1043   /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1044      assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1045   if ((is_Block_dead(block)) ||                  /* Control dead */
1046       (block == current_ir_graph->start_block))  /* There should be no Phi nodes */
1047     return new_Bad();                            /* in the Start Block. */
1048
1049   if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1050
1051 #if 0
1052   /* first we test for a special case: */
1053   /* Confirm is a special node fixing additional information for a
1054      value that is known at a certain point.  This is useful for
1055      dataflow analysis. */
1056   if (n_preds == 2) {
1057     ir_node *a = get_Phi_pred(n, 0);
1058     ir_node *b = get_Phi_pred(n, 1);
1059     if (   (get_irn_op(a) == op_Confirm)
1060         && (get_irn_op(b) == op_Confirm)
1061         && follow_Id (get_irn_n(a, 0) == get_irn_n(b, 0))
1062         && (get_irn_n(a, 1) == get_irn_n (b, 1))
1063         && (a->data.num == (~b->data.num & irpn_True) )) {
1064       return get_irn_n(a, 0);
1065     }
1066   }
1067 #endif
1068
1069   /* If the Block has a Bad pred, we also have one. */
1070   for (i = 0;  i < n_preds;  ++i)
1071     if (is_Bad (get_Block_cfgpred(block, i)))
1072       set_Phi_pred(n, i, new_Bad());
1073
1074   /* Find first non-self-referencing input */
1075   for (i = 0;  i < n_preds;  ++i) {
1076     first_val = get_Phi_pred(n, i);
1077     if (   (first_val != n)                            /* not self pointer */
1078 #if 1
1079         && (get_irn_op(first_val) != op_Bad)
1080 #endif
1081            ) {        /* value not dead */
1082       break;          /* then found first value. */
1083     }
1084   }
1085
1086   /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1087   if (i >= n_preds) { return new_Bad(); }
1088
1089   scnd_val = NULL;
1090
1091   /* follow_Id () for rest of inputs, determine if any of these
1092      are non-self-referencing */
1093   while (++i < n_preds) {
1094     scnd_val = get_Phi_pred(n, i);
1095     if (   (scnd_val != n)
1096         && (scnd_val != first_val)
1097 #if 1
1098         && (get_irn_op(scnd_val) != op_Bad)
1099 #endif
1100            ) {
1101       break;
1102     }
1103   }
1104
1105   /* Fold, if no multiple distinct non-self-referencing inputs */
1106   if (i >= n_preds) {
1107     n = first_val;
1108     DBG_OPT_PHI(oldn, first_val, n);
1109   } else {
1110     /* skip the remaining Ids (done in get_Phi_pred). */
1111     /* superfluous, since we walk all to propagate Block's Bads.
1112        while (++i < n_preds) get_Phi_pred(n, i);     */
1113   }
1114   return n;
1115 }
1116
1117 /**
1118  * optimize Proj(Tuple) and gigo for ProjX in Bad block
1119  */
1120 static ir_node *equivalent_node_Proj(ir_node *n)
1121 {
1122   ir_node *oldn = n;
1123
1124   ir_node *a = get_Proj_pred(n);
1125
1126   if ( get_irn_op(a) == op_Tuple) {
1127     /* Remove the Tuple/Proj combination. */
1128     if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1129       n = get_Tuple_pred(a, get_Proj_proj(n));
1130       DBG_OPT_TUPLE(oldn, a, n);
1131     } else {
1132       assert(0); /* This should not happen! */
1133       n = new_Bad();
1134     }
1135   } else if (get_irn_mode(n) == mode_X &&
1136              is_Block_dead(get_nodes_block(n))) {
1137     /* Remove dead control flow -- early gigo. */
1138     n = new_Bad();
1139   }
1140   return n;
1141 }
1142
1143 /**
1144  * Remove Id's.
1145  */
1146 static ir_node *equivalent_node_Id(ir_node *n)
1147 {
1148   ir_node *oldn = n;
1149
1150   n = follow_Id(n);
1151   DBG_OPT_ID(oldn, n);
1152   return n;
1153 }
1154
1155 /**
1156  * optimize a Mux
1157  */
1158 static ir_node *equivalent_node_Mux(ir_node *n)
1159 {
1160   ir_node *oldn = n, *sel = get_Mux_sel(n);
1161   tarval *ts = value_of(sel);
1162
1163   /* Mux(true, f, t) == t */
1164   if (ts == get_tarval_b_true()) {
1165     n = get_Mux_true(n);
1166     DBG_OPT_ALGSIM0(oldn, n);
1167   }
1168   /* Mux(false, f, t) == f */
1169   else if (ts == get_tarval_b_false()) {
1170     n = get_Mux_false(n);
1171     DBG_OPT_ALGSIM0(oldn, n);
1172   }
1173   /* Mux(v, x, x) == x */
1174   else if (get_Mux_false(n) == get_Mux_true(n)) {
1175     n = get_Mux_true(n);
1176     DBG_OPT_ALGSIM0(oldn, n);
1177   }
1178   else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1179     ir_node *cmp = get_Proj_pred(sel);
1180     long proj_nr = get_Proj_proj(sel);
1181     ir_node *b   = get_Mux_false(n);
1182     ir_node *a   = get_Mux_true(n);
1183
1184     /*
1185      * Note: normalization puts the constant on the right site,
1186      * so we check only one case.
1187      *
1188      * Note further that these optimization work even for floating point
1189      * with NaN's because -NaN == NaN.
1190      * However, if +0 and -0 is handled differently, we cannot use the first one.
1191      */
1192     if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1193       if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1194         /* Mux(a CMP 0, X, a) */
1195         if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1196           /* Mux(a CMP 0, -a, a) */
1197           if (proj_nr == pn_Cmp_Eq) {
1198             /* Mux(a == 0, -a, a)  ==>  -a */
1199             n = b;
1200             DBG_OPT_ALGSIM0(oldn, n);
1201           }
1202           else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1203             /* Mux(a != 0, -a, a)  ==> a */
1204             n = a;
1205             DBG_OPT_ALGSIM0(oldn, n);
1206           }
1207         }
1208         else if (classify_Const(b) == CNST_NULL) {
1209           /* Mux(a CMP 0, 0, a) */
1210           if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1211             /* Mux(a != 0, 0, a) ==> a */
1212             n = a;
1213             DBG_OPT_ALGSIM0(oldn, n);
1214           }
1215           else if (proj_nr == pn_Cmp_Eq) {
1216             /* Mux(a == 0, 0, a) ==> 0 */
1217             n = b;
1218             DBG_OPT_ALGSIM0(oldn, n);
1219           }
1220         }
1221       }
1222     }
1223   }
1224
1225   return n;
1226 }
1227
1228 /**
1229  * Optimize -a CMP -b into b CMP a.
1230  * This works only for for modes where unary Minus
1231  * cannot Overflow.
1232  * Note that two-complement integers can Overflow
1233  * so it will NOT work.
1234  */
1235 static ir_node *equivalent_node_Cmp(ir_node *n)
1236 {
1237   ir_node *left  = get_Cmp_left(n);
1238   ir_node *right = get_Cmp_right(n);
1239
1240   if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1241       !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1242     left  = get_Minus_op(left);
1243     right = get_Minus_op(right);
1244     set_Cmp_left(n, right);
1245     set_Cmp_right(n, left);
1246   }
1247   return n;
1248 }
1249
1250 /**
1251  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1252  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1253  * new nodes.  It is therefore safe to free n if the node returned is not n.
1254  * If a node returns a Tuple we can not just skip it.  If the size of the
1255  * in array fits, we transform n into a tuple (e.g., Div).
1256  */
1257 ir_node *
1258 equivalent_node(ir_node *n)
1259 {
1260   if (n->op->equivalent_node)
1261     return n->op->equivalent_node(n);
1262   return n;
1263 }
1264
1265 /**
1266  * set the default equivalent node operation
1267  */
1268 static ir_op *firm_set_default_equivalent_node(ir_op *op)
1269 {
1270 #define CASE(a)                                 \
1271   case iro_##a:                                 \
1272     op->equivalent_node  = equivalent_node_##a; \
1273     break
1274
1275   switch (op->code) {
1276   CASE(Block);
1277   CASE(Jmp);
1278   CASE(Cond);
1279   CASE(Or);
1280   CASE(Add);
1281   CASE(Eor);
1282   CASE(Sub);
1283   CASE(Shl);
1284   CASE(Shr);
1285   CASE(Shrs);
1286   CASE(Rot);
1287   CASE(Not);
1288   CASE(Minus);
1289   CASE(Mul);
1290   CASE(Div);
1291   CASE(DivMod);
1292   CASE(And);
1293   CASE(Conv);
1294   CASE(Cast);
1295   CASE(Phi);
1296   CASE(Proj);
1297   CASE(Id);
1298   CASE(Mux);
1299   CASE(Cmp);
1300   default:
1301     op->equivalent_node  = NULL;
1302   }
1303
1304   return op;
1305 #undef CASE
1306 }
1307
1308 /**
1309  * Do node specific optimizations of nodes predecessors.
1310  */
1311 static void
1312 optimize_preds(ir_node *n) {
1313   ir_node *a = NULL, *b = NULL;
1314
1315   /* get the operands we will work on for simple cases. */
1316   if (is_binop(n)) {
1317     a = get_binop_left(n);
1318     b = get_binop_right(n);
1319   } else if (is_unop(n)) {
1320     a = get_unop_op(n);
1321   }
1322
1323   switch (get_irn_opcode(n)) {
1324
1325   case iro_Cmp:
1326     /* We don't want Cast as input to Cmp. */
1327     if (get_irn_op(a) == op_Cast) {
1328       a = get_Cast_op(a);
1329       set_Cmp_left(n, a);
1330     }
1331     if (get_irn_op(b) == op_Cast) {
1332       b = get_Cast_op(b);
1333       set_Cmp_right(n, b);
1334     }
1335     break;
1336
1337   default: break;
1338   } /* end switch */
1339 }
1340
1341 /**
1342  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1343  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1344  * If possible, remove the Conv's.
1345  */
1346 static ir_node *transform_node_AddSub(ir_node *n)
1347 {
1348   ir_mode *mode = get_irn_mode(n);
1349
1350   if (mode_is_reference(mode)) {
1351     ir_node *left  = get_binop_left(n);
1352     ir_node *right = get_binop_right(n);
1353     int ref_bits   = get_mode_size_bits(mode);
1354
1355     if (get_irn_op(left) == op_Conv) {
1356       ir_mode *mode = get_irn_mode(left);
1357       int bits      = get_mode_size_bits(mode);
1358
1359       if (ref_bits == bits &&
1360           mode_is_int(mode) &&
1361           get_mode_arithmetic(mode) == irma_twos_complement) {
1362         ir_node *pre      = get_Conv_op(left);
1363         ir_mode *pre_mode = get_irn_mode(pre);
1364
1365         if (mode_is_int(pre_mode) &&
1366             get_mode_size_bits(pre_mode) == bits &&
1367             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1368           /* ok, this conv just changes to sign, moreover the calculation
1369            * is done with same number of bits as our address mode, so
1370            * we can ignore the conv as address calculation can be viewed
1371            * as either signed or unsigned
1372            */
1373           set_binop_left(n, pre);
1374         }
1375       }
1376     }
1377
1378     if (get_irn_op(right) == op_Conv) {
1379       ir_mode *mode = get_irn_mode(right);
1380       int bits      = get_mode_size_bits(mode);
1381
1382       if (ref_bits == bits &&
1383           mode_is_int(mode) &&
1384           get_mode_arithmetic(mode) == irma_twos_complement) {
1385         ir_node *pre      = get_Conv_op(right);
1386         ir_mode *pre_mode = get_irn_mode(pre);
1387
1388         if (mode_is_int(pre_mode) &&
1389             get_mode_size_bits(pre_mode) == bits &&
1390             get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1391           /* ok, this conv just changes to sign, moreover the calculation
1392            * is done with same number of bits as our address mode, so
1393            * we can ignore the conv as address calculation can be viewed
1394            * as either signed or unsigned
1395            */
1396           set_binop_right(n, pre);
1397         }
1398       }
1399     }
1400   }
1401   return n;
1402 }
1403
1404 /**
1405  * Do the AddSub optimization, then Transform Add(a,a) into Mul(a, 2)
1406  * if the mode is integer or float.
1407  * Transform Add(a,-b) into Sub(a,b).
1408  * Reassociation might fold this further.
1409  */
1410 static ir_node *transform_node_Add(ir_node *n)
1411 {
1412   ir_mode *mode;
1413   ir_node *oldn = n;
1414
1415   n = transform_node_AddSub(n);
1416
1417   mode = get_irn_mode(n);
1418   if (mode_is_num(mode)) {
1419     ir_node *a = get_Add_left(n);
1420
1421     if (a == get_Add_right(n)) {
1422       ir_node *block = get_nodes_block(n);
1423
1424       n = new_rd_Mul(
1425             get_irn_dbg_info(n),
1426             current_ir_graph,
1427             block,
1428             a,
1429             new_r_Const_long(current_ir_graph, block, mode, 2),
1430             mode);
1431       DBG_OPT_ALGSIM0(oldn, n);
1432     }
1433     else {
1434       ir_node *b = get_Add_right(n);
1435
1436       if (get_irn_op(a) == op_Minus) {
1437         n = new_rd_Sub(
1438             get_irn_dbg_info(n),
1439             current_ir_graph,
1440             get_nodes_block(n),
1441             b,
1442             get_Minus_op(a),
1443             mode);
1444         DBG_OPT_ALGSIM0(oldn, n);
1445       }
1446       else if (get_irn_op(b) == op_Minus) {
1447         n = new_rd_Sub(
1448             get_irn_dbg_info(n),
1449             current_ir_graph,
1450             get_nodes_block(n),
1451             a,
1452             get_Minus_op(b),
1453             mode);
1454         DBG_OPT_ALGSIM0(oldn, n);
1455       }
1456     }
1457   }
1458   return n;
1459 }
1460
1461 /**
1462  * Do the AddSub optimization, then Transform Sub(0,a) into Minus(a).
1463  */
1464 static ir_node *transform_node_Sub(ir_node *n)
1465 {
1466   ir_mode *mode;
1467   ir_node *oldn = n;
1468
1469   n = transform_node_AddSub(n);
1470
1471   mode = get_irn_mode(n);
1472   if (mode_is_num(mode) && (classify_Const(get_Sub_left(n)) == CNST_NULL)) {
1473     n = new_rd_Minus(
1474           get_irn_dbg_info(n),
1475           current_ir_graph,
1476           get_nodes_block(n),
1477           get_Sub_right(n),
1478           mode);
1479     DBG_OPT_ALGSIM0(oldn, n);
1480   }
1481
1482   return n;
1483 }
1484
1485 /**
1486   Transform Mul(a,-1) into -a.
1487  * Do architecture dependend optimizations on Mul nodes
1488  */
1489 static ir_node *transform_node_Mul(ir_node *n) {
1490   ir_node *oldn = n;
1491   ir_mode *mode = get_irn_mode(n);
1492
1493   if (mode_is_signed(mode)) {
1494     ir_node *r = NULL;
1495     ir_node *a = get_Mul_left(n);
1496     ir_node *b = get_Mul_right(n);
1497
1498     if (value_of(a) == get_mode_minus_one(mode))
1499       r = b;
1500     else if (value_of(b) == get_mode_minus_one(mode))
1501       r = a;
1502     if (r) {
1503       n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
1504       DBG_OPT_ALGSIM1(oldn, a, b, n);
1505       return n;
1506     }
1507   }
1508   return arch_dep_replace_mul_with_shifts(n);
1509 }
1510
1511 /**
1512  * transform a Div Node
1513  */
1514 static ir_node *transform_node_Div(ir_node *n)
1515 {
1516   tarval *tv = value_of(n);
1517   ir_node *value = n;
1518
1519   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1520
1521   if (tv != tarval_bad) {
1522     value = new_Const(get_tarval_mode(tv), tv);
1523
1524     DBG_OPT_CSTEVAL(n, value);
1525   }
1526   else /* Try architecture dependend optimization */
1527     value = arch_dep_replace_div_by_const(n);
1528
1529   if (value != n) {
1530     /* Turn Div into a tuple (mem, bad, value) */
1531     ir_node *mem = get_Div_mem(n);
1532
1533     turn_into_tuple(n, 3);
1534     set_Tuple_pred(n, pn_Div_M, mem);
1535     set_Tuple_pred(n, pn_Div_X_except, new_Bad());
1536     set_Tuple_pred(n, pn_Div_res, value);
1537   }
1538   return n;
1539 }
1540
1541 /**
1542  * transform a Mod node
1543  */
1544 static ir_node *transform_node_Mod(ir_node *n)
1545 {
1546   tarval *tv = value_of(n);
1547   ir_node *value = n;
1548
1549   /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
1550
1551   if (tv != tarval_bad) {
1552     value = new_Const(get_tarval_mode(tv), tv);
1553
1554     DBG_OPT_CSTEVAL(n, value);
1555   }
1556   else /* Try architecture dependend optimization */
1557     value = arch_dep_replace_mod_by_const(n);
1558
1559   if (value != n) {
1560     /* Turn Mod into a tuple (mem, bad, value) */
1561     ir_node *mem = get_Mod_mem(n);
1562
1563     turn_into_tuple(n, 3);
1564     set_Tuple_pred(n, pn_Mod_M, mem);
1565     set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
1566     set_Tuple_pred(n, pn_Mod_res, value);
1567   }
1568   return n;
1569 }
1570
1571 /**
1572  * transform a DivMod node
1573  */
1574 static ir_node *transform_node_DivMod(ir_node *n)
1575 {
1576   int evaluated = 0;
1577
1578   ir_node *a = get_DivMod_left(n);
1579   ir_node *b = get_DivMod_right(n);
1580   ir_mode *mode = get_irn_mode(a);
1581   tarval *ta = value_of(a);
1582   tarval *tb = value_of(b);
1583
1584   if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
1585     return n;
1586
1587   /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
1588
1589   if (tb != tarval_bad) {
1590     if (tb == get_mode_one(get_tarval_mode(tb))) {
1591       b = new_Const (mode, get_mode_null(mode));
1592       evaluated = 1;
1593
1594       DBG_OPT_CSTEVAL(n, b);
1595     }
1596     else if (ta != tarval_bad) {
1597       tarval *resa, *resb;
1598       resa = tarval_div (ta, tb);
1599       if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
1600                                         Jmp for X result!? */
1601       resb = tarval_mod (ta, tb);
1602       if (resb == tarval_bad) return n; /* Causes exception! */
1603       a = new_Const (mode, resa);
1604       b = new_Const (mode, resb);
1605       evaluated = 1;
1606
1607       DBG_OPT_CSTEVAL(n, a);
1608       DBG_OPT_CSTEVAL(n, b);
1609     }
1610     else { /* Try architecture dependend optimization */
1611       arch_dep_replace_divmod_by_const(&a, &b, n);
1612       evaluated = a != NULL;
1613     }
1614   } else if (ta == get_mode_null(mode)) {
1615     /* 0 / non-Const = 0 */
1616     b = a;
1617     evaluated = 1;
1618   }
1619
1620   if (evaluated) { /* replace by tuple */
1621     ir_node *mem = get_DivMod_mem(n);
1622     turn_into_tuple(n, 4);
1623     set_Tuple_pred(n, pn_DivMod_M,        mem);
1624     set_Tuple_pred(n, pn_DivMod_X_except, new_Bad());  /* no exception */
1625     set_Tuple_pred(n, pn_DivMod_res_div,  a);
1626     set_Tuple_pred(n, pn_DivMod_res_mod,  b);
1627     assert(get_nodes_block(n));
1628   }
1629
1630   return n;
1631 }
1632
1633
1634 /**
1635  * Optimize Abs(x) => x if x is Confirmed >= 0
1636  * Optimize Abs(x) => -x if x is Confirmed <= 0
1637  */
1638 static ir_node *transform_node_Abs(ir_node *n)
1639 {
1640   ir_node *oldn = n;
1641   ir_node *a = get_Abs_op(n);
1642   tarval *tv, *c;
1643   ir_mode *mode;
1644   pn_Cmp cmp, ncmp;
1645   int do_minus = 0;
1646
1647   if (get_irn_op(a) != op_Confirm)
1648     return n;
1649
1650   tv  = value_of(get_Confirm_bound(n));
1651   if (tv == tarval_bad)
1652     return n;
1653
1654   mode = get_irn_mode(n);
1655
1656   /*
1657    * We can handle only >=, >, <, <= cases.
1658    * We could handle == too, but this should not happen.
1659    *
1660    * Note that for integer modes we have a slightly better
1661    * optimization possibilities, so we handle this
1662    * different.
1663    */
1664   cmp = get_Confirm_cmp(n);
1665
1666   switch (cmp) {
1667   case pn_Cmp_Lt:
1668     /*
1669      * must be x < c <= 1 to be useful if integer mode and -0 = 0
1670      *         x < c <= 0 to be useful else
1671      */
1672   case pn_Cmp_Le:
1673     /*
1674      * must be x <= c < 1 to be useful if integer mode and -0 = 0
1675      *         x <= c < 0 to be useful else
1676      */
1677     c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
1678       get_mode_one(mode) : get_mode_null(mode);
1679
1680     ncmp = tarval_cmp(tv, c) ^ pn_Cmp_Eq;
1681     if (cmp != ncmp)
1682       return n;
1683
1684     /* yep, we can replace by -x */
1685     do_minus = 1;
1686     break;
1687
1688   case pn_Cmp_Ge:
1689     /*
1690      * must be x >= c > -1 to be useful if integer mode
1691      *         x >= c >= 0 to be useful else
1692      */
1693   case pn_Cmp_Gt:
1694     /*
1695      * must be x > c >= -1 to be useful if integer mode
1696      *         x > c >= 0 to be useful else
1697      */
1698     if (mode_is_int(mode)) {
1699       c = get_mode_minus_one(mode);
1700
1701       ncmp = tarval_cmp(tv, c) ^ pn_Cmp_Eq;
1702       if (cmp != ncmp)
1703         return n;
1704     }
1705     else {
1706       c = get_mode_minus_one(mode);
1707
1708       ncmp = tarval_cmp(tv, c);
1709
1710       if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
1711         return n;
1712     }
1713
1714     /* yep, we can replace by x */
1715     do_minus = 0;
1716     break;
1717
1718   default:
1719     return n;
1720   }
1721
1722   /*
1723    * Ok, when we get here, we can replace the Abs
1724    * by either x or -x. In the second case we eve could
1725    * add a new Confirm.
1726    *
1727    * Note that -x would create a new node, so we could
1728    * not run it in the equivalent_node() context.
1729    */
1730   if (do_minus) {
1731     ir_node *c;
1732
1733     n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
1734                      get_nodes_block(n), a, mode);
1735
1736     c  = new_Const(mode, tarval_neg(tv));
1737     n = new_r_Confirm(current_ir_graph, get_nodes_block(n), n, c, get_inversed_pnc(cmp));
1738   }
1739   else {
1740     n = a;
1741   }
1742
1743   return n;
1744 }
1745
1746 /**
1747  * transform a Cond node
1748  */
1749 static ir_node *transform_node_Cond(ir_node *n)
1750 {
1751   /* Replace the Cond by a Jmp if it branches on a constant
1752      condition. */
1753   ir_node *jmp;
1754   ir_node *a = get_Cond_selector(n);
1755   tarval *ta = value_of(a);
1756
1757   if ((ta != tarval_bad) &&
1758       (get_irn_mode(a) == mode_b) &&
1759       (get_opt_unreachable_code())) {
1760     /* It's a boolean Cond, branching on a boolean constant.
1761                Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
1762     jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
1763     turn_into_tuple(n, 2);
1764     if (ta == tarval_b_true) {
1765       set_Tuple_pred(n, pn_Cond_false, new_Bad());
1766       set_Tuple_pred(n, pn_Cond_true, jmp);
1767     } else {
1768       set_Tuple_pred(n, pn_Cond_false, jmp);
1769       set_Tuple_pred(n, pn_Cond_true, new_Bad());
1770     }
1771     /* We might generate an endless loop, so keep it alive. */
1772     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1773   } else if ((ta != tarval_bad) &&
1774              (get_irn_mode(a) == mode_Iu) &&
1775              (get_Cond_kind(n) == dense) &&
1776              (get_opt_unreachable_code())) {
1777     /* I don't want to allow Tuples smaller than the biggest Proj.
1778        Also this tuple might get really big...
1779        I generate the Jmp here, and remember it in link.  Link is used
1780        when optimizing Proj. */
1781     set_irn_link(n, new_r_Jmp(current_ir_graph, get_nodes_block(n)));
1782     /* We might generate an endless loop, so keep it alive. */
1783     add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
1784   } else if ((get_irn_op(a) == op_Eor)
1785              && (get_irn_mode(a) == mode_b)
1786              && (classify_tarval(value_of(get_Eor_right(a))) == TV_CLASSIFY_ONE)) {
1787     /* The Eor is a negate.  Generate a new Cond without the negate,
1788        simulate the negate by exchanging the results. */
1789     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1790                                get_Eor_left(a)));
1791   } else if ((get_irn_op(a) == op_Not)
1792              && (get_irn_mode(a) == mode_b)) {
1793     /* A Not before the Cond.  Generate a new Cond without the Not,
1794        simulate the Not by exchanging the results. */
1795     set_irn_link(n, new_r_Cond(current_ir_graph, get_nodes_block(n),
1796                                get_Not_op(a)));
1797   }
1798   return n;
1799 }
1800
1801 /**
1802  * Transform an Eor.
1803  */
1804 static ir_node *transform_node_Eor(ir_node *n)
1805 {
1806   ir_node *oldn = n;
1807   ir_node *a = get_Eor_left(n);
1808   ir_node *b = get_Eor_right(n);
1809   ir_mode *mode = get_irn_mode(n);
1810
1811   if (a == b) {
1812     /* a ^ a = 0 */
1813     n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n),
1814                      mode, get_mode_null(mode));
1815     DBG_OPT_ALGSIM0(oldn, n);
1816   }
1817   else if ((mode == mode_b)
1818       && (get_irn_op(a) == op_Proj)
1819       && (get_irn_mode(a) == mode_b)
1820       && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
1821       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1822     /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
1823     n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1824                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1825
1826     DBG_OPT_ALGSIM0(oldn, n);
1827   }
1828   else if ((mode == mode_b)
1829         && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
1830     /* The Eor is a Not. Replace it by a Not. */
1831     /*   ????!!!Extend to bitfield 1111111. */
1832     n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode_b);
1833
1834     DBG_OPT_ALGSIM0(oldn, n);
1835   }
1836
1837   return n;
1838 }
1839
1840 /**
1841  * Transform a boolean Not.
1842  */
1843 static ir_node *transform_node_Not(ir_node *n)
1844 {
1845   ir_node *oldn = n;
1846   ir_node *a = get_Not_op(n);
1847
1848   if (   (get_irn_mode(n) == mode_b)
1849       && (get_irn_op(a) == op_Proj)
1850       && (get_irn_mode(a) == mode_b)
1851       && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
1852     /* We negate a Cmp. The Cmp has the negated result anyways! */
1853     n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
1854                    mode_b, get_negated_pnc(get_Proj_proj(a)));
1855     DBG_OPT_ALGSIM0(oldn, n);
1856   }
1857
1858   return n;
1859 }
1860
1861 /**
1862  * Transform a Cast of a Const into a new Const
1863  */
1864 static ir_node *transform_node_Cast(ir_node *n) {
1865   ir_node *oldn = n;
1866   ir_node *pred = get_Cast_op(n);
1867   type *tp = get_irn_type(n);
1868
1869   if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
1870     n = new_rd_Const_type(NULL, current_ir_graph, get_nodes_block(pred), get_irn_mode(pred),
1871               get_Const_tarval(pred), tp);
1872     DBG_OPT_CSTEVAL(oldn, n);
1873   } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
1874     n = new_rd_SymConst_type(NULL, current_ir_graph, get_nodes_block(pred), get_SymConst_symbol(pred),
1875                  get_SymConst_kind(pred), tp);
1876     DBG_OPT_CSTEVAL(oldn, n);
1877   }
1878
1879   return n;
1880 }
1881
1882 /**
1883  * Transform a Proj(Div) with a non-zero constant.
1884  * Removes the exceptions and routes the memory to the NoMem node.
1885  */
1886 static ir_node *transform_node_Proj_Div(ir_node *proj)
1887 {
1888   ir_node *n = get_Proj_pred(proj);
1889   ir_node *b;
1890   tarval *tb;
1891   long proj_nr;
1892
1893   b  = get_Div_right(n);
1894   tb = value_of(b);
1895
1896   if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) {
1897     /* div(x, c) && c != 0 */
1898     proj_nr = get_Proj_proj(proj);
1899
1900     /* this node may float */
1901     set_irn_pinned(n, op_pin_state_floats);
1902
1903     if (proj_nr == pn_Div_X_except) {
1904       /* we found an exception handler, remove it */
1905       return new_Bad();
1906     } else {
1907       /* the memory Proj can be removed */
1908       ir_node *res = get_Div_mem(n);
1909       set_Div_mem(n, get_irg_no_mem(current_ir_graph));
1910       if (proj_nr == pn_Div_M)
1911         return res;
1912     }
1913   }
1914   return proj;
1915 }
1916
1917 /**
1918  * Transform a Proj(Mod) with a non-zero constant.
1919  * Removes the exceptions and routes the memory to the NoMem node.
1920  */
1921 static ir_node *transform_node_Proj_Mod(ir_node *proj)
1922 {
1923   ir_node *n = get_Proj_pred(proj);
1924   ir_node *b;
1925   tarval *tb;
1926   long proj_nr;
1927
1928   b  = get_Mod_right(n);
1929   tb = value_of(b);
1930
1931   if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) {
1932     /* mod(x, c) && c != 0 */
1933     proj_nr = get_Proj_proj(proj);
1934
1935     /* this node may float */
1936     set_irn_pinned(n, op_pin_state_floats);
1937
1938     if (proj_nr == pn_Mod_X_except) {
1939       /* we found an exception handler, remove it */
1940       return new_Bad();
1941     } else {
1942       /* the memory Proj can be removed */
1943       ir_node *res = get_Mod_mem(n);
1944       set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
1945       if (proj_nr == pn_Mod_M)
1946         return res;
1947     }
1948   }
1949   return proj;
1950 }
1951
1952 /**
1953  * Transform a Proj(DivMod) with a non-zero constant.
1954  * Removes the exceptions and routes the memory to the NoMem node.
1955  */
1956 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
1957 {
1958   ir_node *n = get_Proj_pred(proj);
1959   ir_node *b;
1960   tarval *tb;
1961   long proj_nr;
1962
1963   b  = get_DivMod_right(n);
1964   tb = value_of(b);
1965
1966   if (tb != tarval_bad && classify_tarval(tb) != TV_CLASSIFY_NULL) {
1967     /* DivMod(x, c) && c != 0 */
1968     proj_nr = get_Proj_proj(proj);
1969
1970     /* this node may float */
1971     set_irn_pinned(n, op_pin_state_floats);
1972
1973     if (proj_nr == pn_DivMod_X_except) {
1974       /* we found an exception handler, remove it */
1975       return new_Bad();
1976     }
1977     else {
1978       /* the memory Proj can be removed */
1979       ir_node *res = get_DivMod_mem(n);
1980       set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
1981       if (proj_nr == pn_DivMod_M)
1982         return res;
1983     }
1984   }
1985   return proj;
1986 }
1987
1988 /**
1989  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
1990  */
1991 static ir_node *transform_node_Proj_Cond(ir_node *proj)
1992 {
1993   if (get_opt_unreachable_code()) {
1994     ir_node *n = get_Proj_pred(proj);
1995     ir_node *b = get_Cond_selector(n);
1996     tarval *tb = value_of(b);
1997
1998     if (tb != tarval_bad && mode_is_int(get_tarval_mode(tb))) {
1999       /* we have a constant switch */
2000       long num = get_Proj_proj(proj);
2001
2002       if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2003         if (get_tarval_long(tb) == num) {
2004           /* Do NOT create a jump here, or we will have 2 control flow ops
2005            * in a block. This case is optimized away in optimize_cf(). */
2006           return proj;
2007         }
2008         else {
2009           /* this case will NEVER be taken, kill it */
2010           return new_Bad();
2011         }
2012       }
2013     }
2014   }
2015   return proj;
2016 }
2017
2018 /**
2019  * Normalizes and optimizes Cmp nodes.
2020  */
2021 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2022 {
2023   if (get_opt_reassociation()) {
2024     ir_node *n     = get_Proj_pred(proj);
2025     ir_node *left  = get_Cmp_left(n);
2026     ir_node *right = get_Cmp_right(n);
2027     ir_node *c     = NULL;
2028     tarval *tv     = NULL;
2029     int changed    = 0;
2030     ir_mode *mode  = NULL;
2031     long proj_nr   = get_Proj_proj(proj);
2032
2033     /*
2034      * First step: normalize the compare op
2035      * by placing the constant on the right site
2036      * or moving the lower address node to the left.
2037      * We ignore the case that both are constants
2038      * this case should be optimized away.
2039      */
2040     if (get_irn_op(right) == op_Const)
2041       c = right;
2042     else if (get_irn_op(left) == op_Const) {
2043       c     = left;
2044       left  = right;
2045       right = c;
2046
2047       proj_nr = get_inversed_pnc(proj_nr);
2048       changed |= 1;
2049     }
2050     else if (left > right) {
2051       ir_node *t = left;
2052
2053       left  = right;
2054       right = t;
2055
2056       proj_nr = get_inversed_pnc(proj_nr);
2057       changed |= 1;
2058     }
2059
2060     /*
2061      * Second step: Try to reduce the magnitude
2062      * of a constant. This may help to generate better code
2063      * later and may help to normalize more compares.
2064      * Of course this is only possible for integer values.
2065      */
2066     if (c) {
2067       mode = get_irn_mode(c);
2068       tv = get_Const_tarval(c);
2069
2070       if (tv != tarval_bad) {
2071         /* the following optimization is possible on modes without Overflow
2072          * on Unary Minus or on == and !=:
2073          * -a CMP c  ==>  a swap(CMP) -c
2074          *
2075          * Beware: for two-complement Overflow may occur, so only == and != can
2076          * be optimized, see this:
2077          * -MININT < 0 =/=> MININT > 0 !!!
2078          */
2079         if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2080             (!mode_overflow_on_unary_Minus(mode) ||
2081              (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2082           left = get_Minus_op(left);
2083           tv = tarval_sub(get_mode_null(mode), tv);
2084
2085           proj_nr = get_inversed_pnc(proj_nr);
2086           changed |= 2;
2087         }
2088
2089         /* for integer modes, we have more */
2090         if (mode_is_int(mode)) {
2091           /* Ne includes Unordered which is not possible on integers.
2092            * However, frontends often use this wrong, so fix it here */
2093           if (proj_nr == pn_Cmp_Ne) {
2094             proj_nr = pn_Cmp_Lg;
2095             set_Proj_proj(proj, proj_nr);
2096           }
2097
2098           /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
2099           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2100               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2101             tv = tarval_sub(tv, get_mode_one(mode));
2102
2103             proj_nr ^= pn_Cmp_Eq;
2104             changed |= 2;
2105           }
2106           /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
2107           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2108               tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2109             tv = tarval_add(tv, get_mode_one(mode));
2110
2111             proj_nr ^= pn_Cmp_Eq;
2112             changed |= 2;
2113           }
2114
2115           /* the following reassociations work only for == and != */
2116           if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2117
2118             /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
2119             if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2120               right = get_Sub_right(left);
2121               left  = get_Sub_left(left);
2122
2123               tv = value_of(right);
2124               changed = 1;
2125             }
2126
2127             if (tv != tarval_bad) {
2128               ir_op *op = get_irn_op(left);
2129
2130               /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
2131               if (op == op_Sub) {
2132                 ir_node *c1 = get_Sub_right(left);
2133                 tarval *tv2 = value_of(c1);
2134
2135                 if (tv2 != tarval_bad) {
2136                   tv2 = tarval_add(tv, value_of(c1));
2137
2138                   if (tv2 != tarval_bad) {
2139                     left    = get_Sub_left(left);
2140                     tv      = tv2;
2141                     changed |= 2;
2142                   }
2143                 }
2144               }
2145               /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
2146               else if (op == op_Add) {
2147                 ir_node *a_l = get_Add_left(left);
2148                 ir_node *a_r = get_Add_right(left);
2149                 ir_node *a;
2150                 tarval *tv2;
2151
2152                 if (get_irn_op(a_l) == op_Const) {
2153                   a = a_r;
2154                   tv2 = value_of(a_l);
2155                 }
2156                 else {
2157                   a = a_l;
2158                   tv2 = value_of(a_r);
2159                 }
2160
2161                 if (tv2 != tarval_bad) {
2162                   tv2 = tarval_sub(tv, tv2);
2163
2164                   if (tv2 != tarval_bad) {
2165                     left    = a;
2166                     tv      = tv2;
2167                     changed |= 2;
2168                   }
2169                 }
2170               }
2171               /* -a == c ==> a == -c, -a != c ==> a != -c */
2172               else if (op == op_Minus) {
2173                 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2174
2175                 if (tv2 != tarval_bad) {
2176                   left    = get_Minus_op(left);
2177                   tv      = tv2;
2178                   changed |= 2;
2179                 }
2180               }
2181             }
2182           } /* == or != */
2183           /* the following reassociations work only for <= */
2184           else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2185             if (tv != tarval_bad) {
2186               ir_op *op = get_irn_op(left);
2187
2188               /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
2189               if (op == op_Abs) {
2190               }
2191             }
2192           }
2193         } /* mode_is_int */
2194       }
2195     }
2196
2197     if (changed) {
2198       ir_node *block = get_nodes_block(n);
2199
2200       if (changed & 2)      /* need a new Const */
2201         right = new_Const(mode, tv);
2202
2203       /* create a new compare */
2204       n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2205             left, right);
2206
2207       set_Proj_pred(proj, n);
2208       set_Proj_proj(proj, proj_nr);
2209     }
2210   }
2211   return proj;
2212 }
2213
2214 /**
2215  * Does all optimizations on nodes that must be done on it's Proj's
2216  * because of creating new nodes.
2217  */
2218 static ir_node *transform_node_Proj(ir_node *proj)
2219 {
2220   ir_node *n = get_Proj_pred(proj);
2221
2222   switch (get_irn_opcode(n)) {
2223   case iro_Div:
2224     return transform_node_Proj_Div(proj);
2225
2226   case iro_Mod:
2227     return transform_node_Proj_Mod(proj);
2228
2229   case iro_DivMod:
2230     return transform_node_Proj_DivMod(proj);
2231
2232   case iro_Cond:
2233     return transform_node_Proj_Cond(proj);
2234
2235   case iro_Cmp:
2236     return transform_node_Proj_Cmp(proj);
2237
2238   case iro_Tuple:
2239     /* should not happen, but if it does will be optimized away */
2240     return equivalent_node_Proj(proj);
2241
2242   default:
2243     /* do nothing */
2244     return proj;
2245   }
2246 }
2247
2248 /**
2249  * returns the operands of a commutative bin-op, if one operand is
2250  * a const, it is returned as the second one.
2251  */
2252 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2253 {
2254   ir_node *op_a = get_binop_left(binop);
2255   ir_node *op_b = get_binop_right(binop);
2256
2257   assert(is_op_commutative(get_irn_op(binop)));
2258
2259   if (get_irn_op(op_a) == op_Const) {
2260     *a = op_b;
2261     *c = op_a;
2262   }
2263   else {
2264     *a = op_a;
2265     *c = op_b;
2266   }
2267 }
2268
2269 /**
2270  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2271  * Such pattern may arise in bitfield stores.
2272  *
2273  * value  c4                  value      c4 & c2
2274  *    AND     c3                    AND           c1 | c3
2275  *        OR     c2      ===>               OR
2276  *           AND    c1
2277  *               OR
2278  */
2279 static ir_node *transform_node_Or_bf_store(ir_node *or)
2280 {
2281   ir_node *and, *c1;
2282   ir_node *or_l, *c2;
2283   ir_node *and_l, *c3;
2284   ir_node *value, *c4;
2285   ir_node *new_and, *new_const, *block;
2286   ir_mode *mode = get_irn_mode(or);
2287
2288   tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2289
2290   get_comm_Binop_Ops(or, &and, &c1);
2291   if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2292     return or;
2293
2294   get_comm_Binop_Ops(and, &or_l, &c2);
2295   if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2296     return or;
2297
2298   get_comm_Binop_Ops(or_l, &and_l, &c3);
2299   if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2300     return or;
2301
2302   get_comm_Binop_Ops(and_l, &value, &c4);
2303   if (get_irn_op(c4) != op_Const)
2304     return or;
2305
2306   /* ok, found the pattern, check for conditions */
2307   assert(mode == get_irn_mode(and));
2308   assert(mode == get_irn_mode(or_l));
2309   assert(mode == get_irn_mode(and_l));
2310
2311   tv1 = get_Const_tarval(c1);
2312   tv2 = get_Const_tarval(c2);
2313   tv3 = get_Const_tarval(c3);
2314   tv4 = get_Const_tarval(c4);
2315
2316   tv = tarval_or(tv4, tv2);
2317   if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2318     /* have at least one 0 at the same bit position */
2319     return or;
2320   }
2321
2322   n_tv4 = tarval_not(tv4);
2323   if (tv3 != tarval_and(tv3, n_tv4)) {
2324     /* bit in the or_mask is outside the and_mask */
2325     return or;
2326   }
2327
2328   n_tv2 = tarval_not(tv2);
2329   if (tv1 != tarval_and(tv1, n_tv2)) {
2330     /* bit in the or_mask is outside the and_mask */
2331     return or;
2332   }
2333
2334   /* ok, all conditions met */
2335   block = get_nodes_block(or);
2336
2337   new_and = new_r_And(current_ir_graph, block,
2338       value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2339
2340   new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2341
2342   set_Or_left(or, new_and);
2343   set_Or_right(or, new_const);
2344
2345   /* check for more */
2346   return transform_node_Or_bf_store(or);
2347 }
2348
2349 /**
2350  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2351  */
2352 static ir_node *transform_node_Or_Rot(ir_node *or)
2353 {
2354   ir_mode *mode = get_irn_mode(or);
2355   ir_node *shl, *shr, *block;
2356   ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2357   tarval *tv1, *tv2;
2358
2359   if (! mode_is_int(mode))
2360     return or;
2361
2362   shl = get_binop_left(or);
2363   shr = get_binop_right(or);
2364
2365   if (get_irn_op(shl) == op_Shr) {
2366     if (get_irn_op(shr) != op_Shl)
2367       return or;
2368
2369     irn = shl;
2370     shl = shr;
2371     shr = irn;
2372   }
2373   else if (get_irn_op(shl) != op_Shl)
2374     return or;
2375   else if (get_irn_op(shr) != op_Shr)
2376     return or;
2377
2378   x = get_Shl_left(shl);
2379   if (x != get_Shr_left(shr))
2380     return or;
2381
2382   c1 = get_Shl_right(shl);
2383   c2 = get_Shr_right(shr);
2384   if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2385     tv1 = get_Const_tarval(c1);
2386     if (! tarval_is_long(tv1))
2387       return or;
2388
2389     tv2 = get_Const_tarval(c2);
2390     if (! tarval_is_long(tv2))
2391       return or;
2392
2393     if (get_tarval_long(tv1) + get_tarval_long(tv2)
2394         != get_mode_size_bits(mode))
2395       return or;
2396
2397     /* yet, condition met */
2398     block = get_nodes_block(or);
2399
2400     n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2401
2402     DBG_OPT_ALGSIM1(or, shl, shr, n);
2403     return n;
2404   }
2405   else if (get_irn_op(c1) == op_Sub) {
2406     v   = c2;
2407     sub = c1;
2408
2409     if (get_Sub_right(sub) != v)
2410       return or;
2411
2412     c1 = get_Sub_left(sub);
2413     if (get_irn_op(c1) != op_Const)
2414       return or;
2415
2416     tv1 = get_Const_tarval(c1);
2417     if (! tarval_is_long(tv1))
2418       return or;
2419
2420     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2421       return or;
2422
2423     /* yet, condition met */
2424     block = get_nodes_block(or);
2425
2426     /* a Rot right is not supported, so use a rot left */
2427     n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
2428
2429     DBG_OPT_ALGSIM0(or, n);
2430     return n;
2431   }
2432   else if (get_irn_op(c2) == op_Sub) {
2433     v   = c1;
2434     sub = c2;
2435
2436     c1 = get_Sub_left(sub);
2437     if (get_irn_op(c1) != op_Const)
2438       return or;
2439
2440     tv1 = get_Const_tarval(c1);
2441     if (! tarval_is_long(tv1))
2442       return or;
2443
2444     if (get_tarval_long(tv1) != get_mode_size_bits(mode))
2445       return or;
2446
2447     /* yet, condition met */
2448     block = get_nodes_block(or);
2449
2450     /* a Rot Left */
2451     n = new_r_Rot(current_ir_graph, block, x, v, mode);
2452
2453     DBG_OPT_ALGSIM0(or, n);
2454     return n;
2455   }
2456
2457   return or;
2458 }
2459
2460 /**
2461  * Optimize an Or
2462  */
2463 static ir_node *transform_node_Or(ir_node *or)
2464 {
2465   or = transform_node_Or_bf_store(or);
2466   or = transform_node_Or_Rot(or);
2467
2468   return or;
2469 }
2470
2471 /* forward */
2472 static ir_node *transform_node(ir_node *n);
2473
2474 /**
2475  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl
2476  */
2477 static ir_node *transform_node_shift(ir_node *n)
2478 {
2479   ir_node *left, *right;
2480   tarval *tv1, *tv2, *res;
2481   ir_mode *mode;
2482   int modulo_shf, flag;
2483
2484   left = get_binop_left(n);
2485
2486   /* different operations */
2487   if (get_irn_op(left) != get_irn_op(n))
2488     return n;
2489
2490   right = get_binop_right(n);
2491   tv1 = value_of(right);
2492   if (tv1 == tarval_bad)
2493     return n;
2494
2495   tv2 = value_of(get_binop_right(left));
2496   if (tv2 == tarval_bad)
2497     return n;
2498
2499   res = tarval_add(tv1, tv2);
2500
2501   /* beware: a simple replacement works only, if res < modulo shift */
2502   mode = get_irn_mode(n);
2503
2504   flag = 0;
2505
2506   modulo_shf = get_mode_modulo_shift(mode);
2507   if (modulo_shf > 0) {
2508     tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
2509
2510     if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
2511       flag = 1;
2512   }
2513   else
2514     flag = 1;
2515
2516   if (flag) {
2517     /* ok, we can replace it */
2518     ir_node *in[2], *irn, *block = get_nodes_block(n);
2519
2520     in[0] = get_binop_left(left);
2521     in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
2522
2523     irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
2524
2525     DBG_OPT_ALGSIM0(n, irn);
2526
2527     return transform_node(irn);
2528   }
2529   return n;
2530 }
2531
2532 #define transform_node_Shr  transform_node_shift
2533 #define transform_node_Shrs transform_node_shift
2534 #define transform_node_Shl  transform_node_shift
2535
2536 /**
2537  * Remove dead blocks in keepalive list.  We do not generate a new End node.
2538  */
2539 static ir_node *transform_node_End(ir_node *n) {
2540   int i, n_keepalives = get_End_n_keepalives(n);
2541
2542   for (i = 0; i < n_keepalives; ++i) {
2543     ir_node *ka = get_End_keepalive(n, i);
2544     if (is_Block(ka) && is_Block_dead(ka))
2545       set_End_keepalive(n, i, new_Bad());
2546   }
2547   return n;
2548 }
2549
2550 /**
2551  * Optimize a Mux into some simplier cases.
2552  */
2553 static ir_node *transform_node_Mux(ir_node *n)
2554 {
2555   ir_node *oldn = n, *sel = get_Mux_sel(n);
2556   ir_mode *mode = get_irn_mode(n);
2557
2558   if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
2559     ir_node *cmp = get_Proj_pred(sel);
2560     long proj_nr = get_Proj_proj(sel);
2561     ir_node *f   =  get_Mux_false(n);
2562     ir_node *t   = get_Mux_true(n);
2563
2564     if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
2565       ir_node *block = get_nodes_block(n);
2566
2567       /*
2568        * Note: normalization puts the constant on the right site,
2569        * so we check only one case.
2570        *
2571        * Note further that these optimization work even for floating point
2572        * with NaN's because -NaN == NaN.
2573        * However, if +0 and -0 is handled differently, we cannot use the first one.
2574        */
2575       if (get_irn_op(f) == op_Minus &&
2576           get_Minus_op(f)   == t &&
2577           get_Cmp_left(cmp) == t) {
2578
2579         if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2580           /* Mux(a >=/> 0, -a, a)  ==>  Abs(a) */
2581           n = new_rd_Abs(get_irn_dbg_info(n),
2582                 current_ir_graph,
2583                 block,
2584                 t, mode);
2585           DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2586           return n;
2587         }
2588         else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2589           /* Mux(a <=/< 0, -a, a)  ==>  Minus(Abs(a)) */
2590           n = new_rd_Abs(get_irn_dbg_info(n),
2591                 current_ir_graph,
2592                 block,
2593                 t, mode);
2594           n = new_rd_Minus(get_irn_dbg_info(n),
2595                 current_ir_graph,
2596                 block,
2597                 n, mode);
2598
2599           DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2600           return n;
2601         }
2602       }
2603       else if (get_irn_op(t) == op_Minus &&
2604           get_Minus_op(t)   == f &&
2605           get_Cmp_left(cmp) == f) {
2606
2607         if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2608           /* Mux(a <=/< 0, a, -a)  ==>  Abs(a) */
2609           n = new_rd_Abs(get_irn_dbg_info(n),
2610                 current_ir_graph,
2611                 block,
2612                 f, mode);
2613           DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2614           return n;
2615         }
2616         else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
2617           /* Mux(a >=/> 0, a, -a)  ==>  Minus(Abs(a)) */
2618           n = new_rd_Abs(get_irn_dbg_info(n),
2619                 current_ir_graph,
2620                 block,
2621                 f, mode);
2622           n = new_rd_Minus(get_irn_dbg_info(n),
2623                 current_ir_graph,
2624                 block,
2625                 n, mode);
2626
2627           DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2628           return n;
2629         }
2630       }
2631
2632       if (mode_is_int(mode) && mode_is_signed(mode) &&
2633           get_mode_arithmetic(mode) == irma_twos_complement) {
2634         ir_node *x = get_Cmp_left(cmp);
2635
2636         /* the following optimization works only with signed integer two-complement mode */
2637
2638         if (mode == get_irn_mode(x)) {
2639           /*
2640            * FIXME: this restriction is two rigid, as it would still
2641            * work if mode(x) = Hs and mode == Is, but at least it removes
2642            * all wrong cases.
2643            */
2644           if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
2645               classify_Const(t) == CNST_ALL_ONE &&
2646               classify_Const(f) == CNST_NULL) {
2647             /*
2648              * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
2649              * Conditions:
2650              * T must be signed.
2651              */
2652             n = new_rd_Shrs(get_irn_dbg_info(n),
2653                   current_ir_graph, block, x,
2654                   new_r_Const_long(current_ir_graph, block, mode_Iu,
2655                     get_mode_size_bits(mode) - 1),
2656                   mode);
2657             DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2658             return n;
2659           }
2660           else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
2661                    classify_Const(t) == CNST_ONE &&
2662                    classify_Const(f) == CNST_NULL) {
2663             /*
2664              * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
2665              * Conditions:
2666              * T must be signed.
2667              */
2668             n = new_rd_Shr(get_irn_dbg_info(n),
2669                   current_ir_graph, block,
2670                   new_r_Minus(current_ir_graph, block, x, mode),
2671                   new_r_Const_long(current_ir_graph, block, mode_Iu,
2672                     get_mode_size_bits(mode) - 1),
2673                   mode);
2674             DBG_OPT_ALGSIM1(oldn, cmp, sel, n);
2675             return n;
2676           }
2677         }
2678       }
2679     }
2680   }
2681   return arch_transform_node_Mux(n);
2682 }
2683
2684 /**
2685  * Tries several [inplace] [optimizing] transformations and returns an
2686  * equivalent node.  The difference to equivalent_node() is that these
2687  * transformations _do_ generate new nodes, and thus the old node must
2688  * not be freed even if the equivalent node isn't the old one.
2689  */
2690 static ir_node *transform_node(ir_node *n)
2691 {
2692   if (n->op->transform_node)
2693     n = n->op->transform_node(n);
2694   return n;
2695 }
2696
2697 /**
2698  * set the default transform node operation
2699  */
2700 static ir_op *firm_set_default_transform_node(ir_op *op)
2701 {
2702 #define CASE(a)                                 \
2703   case iro_##a:                                 \
2704     op->transform_node  = transform_node_##a;   \
2705     break
2706
2707   switch (op->code) {
2708   CASE(Add);
2709   CASE(Sub);
2710   CASE(Mul);
2711   CASE(Div);
2712   CASE(Mod);
2713   CASE(DivMod);
2714   CASE(Abs);
2715   CASE(Cond);
2716   CASE(Eor);
2717   CASE(Not);
2718   CASE(Cast);
2719   CASE(Proj);
2720   CASE(Sel);
2721   CASE(Or);
2722   CASE(Shr);
2723   CASE(Shrs);
2724   CASE(Shl);
2725   CASE(End);
2726   CASE(Mux);
2727   default:
2728     op->transform_node = NULL;
2729   }
2730
2731   return op;
2732 #undef CASE
2733 }
2734
2735
2736 /* **************** Common Subexpression Elimination **************** */
2737
2738 /** The size of the hash table used, should estimate the number of nodes
2739     in a graph. */
2740 #define N_IR_NODES 512
2741
2742 /** Compares the attributes of two Const nodes. */
2743 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
2744 {
2745   return (get_Const_tarval(a) != get_Const_tarval(b))
2746       || (get_Const_type(a) != get_Const_type(b));
2747 }
2748
2749 /** Compares the attributes of two Proj nodes. */
2750 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
2751 {
2752     return get_irn_proj_attr (a) != get_irn_proj_attr (b);
2753 }
2754
2755 /** Compares the attributes of two Filter nodes. */
2756 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
2757 {
2758     return get_Filter_proj(a) != get_Filter_proj(b);
2759 }
2760
2761 /** Compares the attributes of two Alloc nodes. */
2762 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
2763 {
2764     return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
2765         || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
2766 }
2767
2768 /** Compares the attributes of two Free nodes. */
2769 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
2770 {
2771     return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
2772         || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
2773 }
2774
2775 /** Compares the attributes of two SymConst nodes. */
2776 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
2777 {
2778     return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
2779       || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
2780       || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
2781 }
2782
2783 /** Compares the attributes of two Call nodes. */
2784 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
2785 {
2786     return (get_irn_call_attr(a) != get_irn_call_attr(b));
2787 }
2788
2789 /** Compares the attributes of two Sel nodes. */
2790 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
2791 {
2792     return (get_irn_sel_attr(a).ent->kind  != get_irn_sel_attr(b).ent->kind)
2793       || (get_irn_sel_attr(a).ent->name    != get_irn_sel_attr(b).ent->name)
2794       || (get_irn_sel_attr(a).ent->owner   != get_irn_sel_attr(b).ent->owner)
2795       || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
2796       || (get_irn_sel_attr(a).ent->type    != get_irn_sel_attr(b).ent->type);
2797 }
2798
2799 /** Compares the attributes of two Phi nodes. */
2800 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
2801 {
2802     return get_irn_phi_attr (a) != get_irn_phi_attr (b);
2803 }
2804
2805 /** Compares the attributes of two Cast nodes. */
2806 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
2807 {
2808     return get_Cast_type(a) != get_Cast_type(b);
2809 }
2810
2811 /** Compares the attributes of two Load nodes. */
2812 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
2813 {
2814   if (get_Load_volatility(a) == volatility_is_volatile ||
2815       get_Load_volatility(b) == volatility_is_volatile)
2816     /* NEVER do CSE on volatile Loads */
2817     return 1;
2818
2819   return get_Load_mode(a) != get_Load_mode(b);
2820 }
2821
2822 /** Compares the attributes of two Store nodes. */
2823 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
2824 {
2825   /* NEVER do CSE on volatile Stores */
2826   return (get_Store_volatility(a) == volatility_is_volatile ||
2827       get_Store_volatility(b) == volatility_is_volatile);
2828 }
2829
2830 /**
2831  * set the default node attribute compare operation
2832  */
2833 static ir_op *firm_set_default_node_cmp_attr(ir_op *op)
2834 {
2835 #define CASE(a)                             \
2836   case iro_##a:                             \
2837     op->node_cmp_attr  = node_cmp_attr_##a; \
2838     break
2839
2840   switch (op->code) {
2841   CASE(Const);
2842   CASE(Proj);
2843   CASE(Filter);
2844   CASE(Alloc);
2845   CASE(Free);
2846   CASE(SymConst);
2847   CASE(Call);
2848   CASE(Sel);
2849   CASE(Phi);
2850   CASE(Cast);
2851   CASE(Load);
2852   CASE(Store);
2853   default:
2854     op->node_cmp_attr  = NULL;
2855   }
2856
2857   return op;
2858 #undef CASE
2859 }
2860
2861 /**
2862  * Compare function for two nodes in the hash table. Gets two
2863  * nodes as parameters.  Returns 0 if the nodes are a cse.
2864  */
2865 static int
2866 vt_cmp (const void *elt, const void *key)
2867 {
2868   ir_node *a, *b;
2869   int i, irn_arity_a;
2870
2871   a = (void *)elt;
2872   b = (void *)key;
2873
2874   if (a == b) return 0;
2875
2876   if ((get_irn_op(a) != get_irn_op(b)) ||
2877       (get_irn_mode(a) != get_irn_mode(b))) return 1;
2878
2879   /* compare if a's in and b's in are of equal length */
2880   irn_arity_a = get_irn_intra_arity (a);
2881   if (irn_arity_a != get_irn_intra_arity(b))
2882     return 1;
2883
2884   /* for block-local cse and op_pin_state_pinned nodes: */
2885   if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
2886     if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
2887       return 1;
2888   }
2889
2890   /* compare a->in[0..ins] with b->in[0..ins] */
2891   for (i = 0; i < irn_arity_a; i++)
2892     if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
2893       return 1;
2894
2895   /*
2896    * here, we already now that the nodes are identical except their
2897    * attributes
2898    */
2899   if (a->op->node_cmp_attr)
2900     return a->op->node_cmp_attr(a, b);
2901
2902   return 0;
2903 }
2904
2905 /*
2906  * Calculate a hash value of a node.
2907  */
2908 unsigned
2909 ir_node_hash (ir_node *node)
2910 {
2911   unsigned h;
2912   int i, irn_arity;
2913
2914   if (node->op == op_Const) {
2915     /* special value for const, as they only differ in their tarval. */
2916     h = HASH_PTR(node->attr.con.tv);
2917     h = 9*h + HASH_PTR(get_irn_mode(node));
2918   } else if (node->op == op_SymConst) {
2919     /* special value for const, as they only differ in their symbol. */
2920     h = HASH_PTR(node->attr.i.sym.type_p);
2921     h = 9*h + HASH_PTR(get_irn_mode(node));
2922   } else {
2923
2924     /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
2925     h = irn_arity = get_irn_intra_arity(node);
2926
2927     /* consider all in nodes... except the block if not a control flow. */
2928     for (i =  is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
2929       h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
2930     }
2931
2932     /* ...mode,... */
2933     h = 9*h + HASH_PTR(get_irn_mode(node));
2934     /* ...and code */
2935     h = 9*h + HASH_PTR(get_irn_op(node));
2936   }
2937
2938   return h;
2939 }
2940
2941 pset *
2942 new_identities(void) {
2943   return new_pset(vt_cmp, N_IR_NODES);
2944 }
2945
2946 void
2947 del_identities(pset *value_table) {
2948   del_pset(value_table);
2949 }
2950
2951 /**
2952  * Return the canonical node computing the same value as n.
2953  * Looks up the node in a hash table.
2954  *
2955  * For Const nodes this is performed in the constructor, too.  Const
2956  * nodes are extremely time critical because of their frequent use in
2957  * constant string arrays.
2958  */
2959 static INLINE ir_node *
2960 identify (pset *value_table, ir_node *n)
2961 {
2962   ir_node *o = NULL;
2963
2964   if (!value_table) return n;
2965
2966   if (get_opt_reassociation()) {
2967     if (is_op_commutative(get_irn_op(n))) {
2968       ir_node *l = get_binop_left(n);
2969       ir_node *r = get_binop_right(n);
2970
2971       /* for commutative operators perform  a OP b == b OP a */
2972       if (l > r) {
2973         set_binop_left(n, r);
2974         set_binop_right(n, l);
2975       }
2976     }
2977   }
2978
2979   o = pset_find (value_table, n, ir_node_hash (n));
2980   if (!o) return n;
2981
2982   DBG_OPT_CSE(n, o);
2983
2984   return o;
2985 }
2986
2987 /**
2988  * During construction we set the op_pin_state_pinned flag in the graph right when the
2989  * optimization is performed.  The flag turning on procedure global cse could
2990  * be changed between two allocations.  This way we are safe.
2991  */
2992 static INLINE ir_node *
2993 identify_cons (pset *value_table, ir_node *n) {
2994   ir_node *old = n;
2995
2996   n = identify(value_table, n);
2997   if (get_irn_n(old, -1) != get_irn_n(n, -1))
2998     set_irg_pinned(current_ir_graph, op_pin_state_floats);
2999   return n;
3000 }
3001
3002 /**
3003  * Return the canonical node computing the same value as n.
3004  * Looks up the node in a hash table, enters it in the table
3005  * if it isn't there yet.
3006  */
3007 static ir_node *
3008 identify_remember (pset *value_table, ir_node *n)
3009 {
3010   ir_node *o = NULL;
3011
3012   if (!value_table) return n;
3013
3014   if (get_opt_reassociation()) {
3015     if (is_op_commutative(get_irn_op(n))) {
3016       ir_node *l = get_binop_left(n);
3017       ir_node *r = get_binop_right(n);
3018
3019       /* for commutative operators perform  a OP b == b OP a */
3020       if (l > r) {
3021         set_binop_left(n, r);
3022         set_binop_right(n, l);
3023       }
3024     }
3025   }
3026
3027   /* lookup or insert in hash table with given hash key. */
3028   o = pset_insert (value_table, n, ir_node_hash (n));
3029
3030   if (o != n) {
3031     DBG_OPT_CSE(n, o);
3032   }
3033
3034   return o;
3035 }
3036
3037 void
3038 add_identities (pset *value_table, ir_node *node) {
3039   if (get_opt_cse() && (get_irn_opcode(node) != iro_Block))
3040     identify_remember (value_table, node);
3041 }
3042
3043 /**
3044  * garbage in, garbage out. If a node has a dead input, i.e., the
3045  * Bad node is input to the node, return the Bad node.
3046  */
3047 static INLINE ir_node *
3048 gigo (ir_node *node)
3049 {
3050   int i, irn_arity;
3051   ir_op* op = get_irn_op(node);
3052
3053   /* remove garbage blocks by looking at control flow that leaves the block
3054      and replacing the control flow by Bad. */
3055   if (get_irn_mode(node) == mode_X) {
3056     ir_node *block = get_nodes_block(node);
3057     if (!get_Block_matured(block)) return node;  /* Don't optimize nodes in immature blocks. */
3058     if (op == op_End) return node;     /* Don't optimize End, may have Bads. */
3059
3060     if (get_irn_op(block) == op_Block && get_Block_matured(block)) {
3061       irn_arity = get_irn_arity(block);
3062       for (i = 0; i < irn_arity; i++) {
3063         if (!is_Bad(get_irn_n(block, i))) break;
3064       }
3065       if (i == irn_arity) return new_Bad();
3066     }
3067   }
3068
3069   /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3070      blocks predecessors is dead. */
3071   if ( op != op_Block && op != op_Phi && op != op_Tuple) {
3072     irn_arity = get_irn_arity(node);
3073
3074     if (is_Block_dead(get_nodes_block(node)))
3075       return new_Bad();
3076
3077     for (i = 0; i < irn_arity; i++) {
3078       if (is_Bad(get_irn_n(node, i))) {
3079         return new_Bad();
3080       }
3081     }
3082   }
3083 #if 0
3084   /* With this code we violate the agreement that local_optimize
3085      only leaves Bads in Block, Phi and Tuple nodes. */
3086   /* If Block has only Bads as predecessors it's garbage. */
3087   /* If Phi has only Bads as predecessors it's garbage. */
3088   if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
3089     irn_arity = get_irn_arity(node);
3090     for (i = 0; i < irn_arity; i++) {
3091       if (!is_Bad(get_irn_n(node, i))) break;
3092     }
3093     if (i == irn_arity) node = new_Bad();
3094   }
3095 #endif
3096   return node;
3097 }
3098
3099
3100 /**
3101  * These optimizations deallocate nodes from the obstack.
3102  * It can only be called if it is guaranteed that no other nodes
3103  * reference this one, i.e., right after construction of a node.
3104  */
3105 ir_node *
3106 optimize_node (ir_node *n)
3107 {
3108         tarval *tv;
3109         ir_node *oldn = n;
3110         opcode iro = get_irn_opcode(n);
3111
3112         type *old_tp = get_irn_type(n);
3113         {
3114                 int i, arity = get_irn_arity(n);
3115                 for (i = 0; i < arity && !old_tp; ++i)
3116                         old_tp = get_irn_type(get_irn_n(n, i));
3117         }
3118
3119         /* Always optimize Phi nodes: part of the construction. */
3120         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3121
3122         /* constant expression evaluation / constant folding */
3123         if (get_opt_constant_folding()) {
3124                 /* constants can not be evaluated */
3125                 if (iro != iro_Const) {
3126                         /* try to evaluate */
3127                         tv = computed_value(n);
3128                         if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
3129                                 ir_node *nw;
3130
3131                                 /*
3132                                  * we MUST copy the node here temporary, because it's still needed
3133                                  * for DBG_OPT_CSTEVAL
3134                                  */
3135                                 int node_size = offsetof(ir_node, attr) +  n->op->attr_size;
3136                                 oldn = alloca(node_size);
3137
3138                                 memcpy(oldn, n, node_size);
3139                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3140
3141                                 /* ARG, copy the in array, we need it for statistics */
3142                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3143
3144
3145                                 edges_node_deleted(n, current_ir_graph);
3146
3147                                 /* evaluation was successful -- replace the node. */
3148                                 obstack_free (current_ir_graph->obst, n);
3149                                 nw = new_Const (get_tarval_mode (tv), tv);
3150
3151                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3152                                         set_Const_type(nw, old_tp);
3153                                 DBG_OPT_CSTEVAL(oldn, nw);
3154                                 return nw;
3155                         }
3156                 }
3157         }
3158
3159         /* remove unnecessary nodes */
3160         if (get_opt_constant_folding() ||
3161                         (iro == iro_Phi)  ||   /* always optimize these nodes. */
3162                         (iro == iro_Id)   ||
3163                         (iro == iro_Proj) ||
3164                         (iro == iro_Block)  )  /* Flags tested local. */
3165                 n = equivalent_node (n);
3166
3167         optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3168
3169         /** common subexpression elimination **/
3170         /* Checks whether n is already available. */
3171         /* The block input is used to distinguish different subexpressions. Right
3172                  now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3173                  subexpressions within a block. */
3174         if (get_opt_cse())
3175                 n = identify_cons (current_ir_graph->value_table, n);
3176
3177         if (n != oldn) {
3178                 edges_node_deleted(oldn, current_ir_graph);
3179
3180                 /* We found an existing, better node, so we can deallocate the old node. */
3181                 obstack_free (current_ir_graph->obst, oldn);
3182
3183                 return n;
3184         }
3185
3186         /* Some more constant expression evaluation that does not allow to
3187                  free the node. */
3188         iro = get_irn_opcode(n);
3189         if (get_opt_constant_folding() ||
3190             (iro == iro_Cond) ||
3191             (iro == iro_Proj) ||
3192             (iro == iro_Sel))     /* Flags tested local. */
3193           n = transform_node (n);
3194
3195         /* Remove nodes with dead (Bad) input.
3196            Run always for transformation induced Bads. */
3197         n = gigo (n);
3198
3199         /* Now we have a legal, useful node. Enter it in hash table for cse */
3200         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3201           n = identify_remember (current_ir_graph->value_table, n);
3202         }
3203
3204         return n;
3205 }
3206
3207
3208 /**
3209  * These optimizations never deallocate nodes (in place).  This can cause dead
3210  * nodes lying on the obstack.  Remove these by a dead node elimination,
3211  * i.e., a copying garbage collection.
3212  */
3213 ir_node *
3214 optimize_in_place_2 (ir_node *n)
3215 {
3216   tarval *tv;
3217   ir_node *oldn = n;
3218   opcode iro = get_irn_opcode(n);
3219
3220   type *old_tp = get_irn_type(n);
3221   {
3222     int i, arity = get_irn_arity(n);
3223     for (i = 0; i < arity && !old_tp; ++i)
3224       old_tp = get_irn_type(get_irn_n(n, i));
3225   }
3226
3227   if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3228
3229   /* if not optimize return n */
3230   if (n == NULL) {
3231     assert(0);
3232     /* Here this is possible.  Why? */
3233     return n;
3234   }
3235
3236   /* constant expression evaluation / constant folding */
3237   if (get_opt_constant_folding()) {
3238     /* constants can not be evaluated */
3239     if (iro != iro_Const) {
3240       /* try to evaluate */
3241       tv = computed_value(n);
3242       if ((get_irn_mode(n) != mode_T) && (tv != tarval_bad)) {
3243         /* evaluation was successful -- replace the node. */
3244         n = new_Const (get_tarval_mode (tv), tv);
3245
3246     if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3247       set_Const_type(n, old_tp);
3248
3249         DBG_OPT_CSTEVAL(oldn, n);
3250         return n;
3251       }
3252     }
3253   }
3254
3255   /* remove unnecessary nodes */
3256   if (get_opt_constant_folding() ||
3257       (iro == iro_Phi)  ||   /* always optimize these nodes. */
3258       (iro == iro_Id)   ||   /* ... */
3259       (iro == iro_Proj) ||   /* ... */
3260       (iro == iro_Block)  )  /* Flags tested local. */
3261     n = equivalent_node (n);
3262
3263   optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
3264
3265   /** common subexpression elimination **/
3266   /* Checks whether n is already available. */
3267   /* The block input is used to distinguish different subexpressions.  Right
3268      now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3269      subexpressions within a block. */
3270   if (get_opt_cse()) {
3271     n = identify (current_ir_graph->value_table, n);
3272   }
3273
3274   /* Some more constant expression evaluation. */
3275   iro = get_irn_opcode(n);
3276   if (get_opt_constant_folding() ||
3277       (iro == iro_Cond) ||
3278       (iro == iro_Proj) ||
3279       (iro == iro_Sel))     /* Flags tested local. */
3280     n = transform_node (n);
3281
3282   /* Remove nodes with dead (Bad) input.
3283      Run always for transformation induced Bads.  */
3284   n = gigo (n);
3285
3286   /* Now we can verify the node, as it has no dead inputs any more. */
3287   irn_vrfy(n);
3288
3289   /* Now we have a legal, useful node. Enter it in hash table for cse.
3290      Blocks should be unique anyways.  (Except the successor of start:
3291      is cse with the start block!) */
3292   if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3293     n = identify_remember (current_ir_graph->value_table, n);
3294
3295   return n;
3296 }
3297
3298 /**
3299  * Wrapper for external use, set proper status bits after optimization.
3300  */
3301 ir_node *
3302 optimize_in_place (ir_node *n)
3303 {
3304   /* Handle graph state */
3305   assert(get_irg_phase_state(current_ir_graph) != phase_building);
3306
3307   if (get_opt_global_cse())
3308     set_irg_pinned(current_ir_graph, op_pin_state_floats);
3309   if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3310     set_irg_outs_inconsistent(current_ir_graph);
3311
3312   /* Maybe we could also test whether optimizing the node can
3313      change the control graph. */
3314   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
3315     set_irg_dom_inconsistent(current_ir_graph);
3316   return optimize_in_place_2 (n);
3317 }
3318
3319 /**
3320  * set the default ir op operations
3321  */
3322 ir_op *firm_set_default_operations(ir_op *op)
3323 {
3324   op = firm_set_default_computed_value(op);
3325   op = firm_set_default_equivalent_node(op);
3326   op = firm_set_default_transform_node(op);
3327   op = firm_set_default_node_cmp_attr(op);
3328   op = firm_set_default_get_type(op);
3329
3330   return op;
3331 }