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