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