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