- renamed variadicity to ir_variadicity
[libfirm] / ir / ir / iropt.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   iropt --- optimizations intertwined with IR construction.
23  * @author  Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "irnode_t.h"
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
33 #include "irmode_t.h"
34 #include "iropt_t.h"
35 #include "ircons_t.h"
36 #include "irgmod.h"
37 #include "irvrfy.h"
38 #include "tv_t.h"
39 #include "dbginfo_t.h"
40 #include "iropt_dbg.h"
41 #include "irflag_t.h"
42 #include "irhooks.h"
43 #include "irarch.h"
44 #include "hashptr.h"
45 #include "opt_confirms.h"
46 #include "opt_polymorphy.h"
47 #include "irtools.h"
48 #include "irhooks.h"
49 #include "array_t.h"
50
51 /* Make types visible to allow most efficient access */
52 #include "entity_t.h"
53
54 /**
55  * Returns the tarval of a Const node or tarval_bad for all other nodes.
56  */
57 static tarval *default_value_of(const ir_node *n) {
58         if (is_Const(n))
59                 return get_Const_tarval(n); /* might return tarval_bad */
60         else
61                 return tarval_bad;
62 }
63
64 value_of_func value_of_ptr = default_value_of;
65
66 /* * Set a new value_of function. */
67 void set_value_of_func(value_of_func func) {
68         if (func != NULL)
69                 value_of_ptr = func;
70         else
71                 value_of_ptr = default_value_of;
72 }
73
74 /**
75  * Return the value of a Constant.
76  */
77 static tarval *computed_value_Const(const ir_node *n) {
78         return get_Const_tarval(n);
79 }  /* computed_value_Const */
80
81 /**
82  * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
83  */
84 static tarval *computed_value_SymConst(const ir_node *n) {
85         ir_type   *type;
86         ir_entity *ent;
87
88         switch (get_SymConst_kind(n)) {
89         case symconst_type_size:
90                 type = get_SymConst_type(n);
91                 if (get_type_state(type) == layout_fixed)
92                         return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
93                 break;
94         case symconst_type_align:
95                 type = get_SymConst_type(n);
96                 if (get_type_state(type) == layout_fixed)
97                         return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
98                 break;
99         case symconst_ofs_ent:
100                 ent  = get_SymConst_entity(n);
101                 type = get_entity_owner(ent);
102                 if (get_type_state(type) == layout_fixed)
103                         return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
104                 break;
105         default:
106                 break;
107         }
108         return tarval_bad;
109 }  /* computed_value_SymConst */
110
111 /**
112  * Return the value of an Add.
113  */
114 static tarval *computed_value_Add(const ir_node *n) {
115         ir_node *a = get_Add_left(n);
116         ir_node *b = get_Add_right(n);
117
118         tarval *ta = value_of(a);
119         tarval *tb = value_of(b);
120
121         if ((ta != tarval_bad) && (tb != tarval_bad))
122                 return tarval_add(ta, tb);
123
124         return tarval_bad;
125 }  /* computed_value_Add */
126
127 /**
128  * Return the value of a Sub.
129  * Special case: a - a
130  */
131 static tarval *computed_value_Sub(const ir_node *n) {
132         ir_mode *mode = get_irn_mode(n);
133         ir_node *a    = get_Sub_left(n);
134         ir_node *b    = get_Sub_right(n);
135         tarval  *ta;
136         tarval  *tb;
137
138         /* NaN - NaN != 0 */
139         if (! mode_is_float(mode)) {
140                 /* a - a = 0 */
141                 if (a == b)
142                         return get_mode_null(mode);
143         }
144
145         ta = value_of(a);
146         tb = value_of(b);
147
148         if ((ta != tarval_bad) && (tb != tarval_bad))
149                 return tarval_sub(ta, tb, mode);
150
151         return tarval_bad;
152 }  /* computed_value_Sub */
153
154 /**
155  * Return the value of a Carry.
156  * Special : a op 0, 0 op b
157  */
158 static tarval *computed_value_Carry(const ir_node *n) {
159         ir_node *a = get_binop_left(n);
160         ir_node *b = get_binop_right(n);
161         ir_mode *m = get_irn_mode(n);
162
163         tarval *ta = value_of(a);
164         tarval *tb = value_of(b);
165
166         if ((ta != tarval_bad) && (tb != tarval_bad)) {
167                 tarval_add(ta, tb);
168                 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
169         } else {
170                 if (tarval_is_null(ta) || tarval_is_null(tb))
171                         return get_mode_null(m);
172         }
173         return tarval_bad;
174 }  /* computed_value_Carry */
175
176 /**
177  * Return the value of a Borrow.
178  * Special : a op 0
179  */
180 static tarval *computed_value_Borrow(const ir_node *n) {
181         ir_node *a = get_binop_left(n);
182         ir_node *b = get_binop_right(n);
183         ir_mode *m = get_irn_mode(n);
184
185         tarval *ta = value_of(a);
186         tarval *tb = value_of(b);
187
188         if ((ta != tarval_bad) && (tb != tarval_bad)) {
189                 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
190         } else if (tarval_is_null(ta)) {
191                 return get_mode_null(m);
192         }
193         return tarval_bad;
194 }  /* computed_value_Borrow */
195
196 /**
197  * Return the value of an unary Minus.
198  */
199 static tarval *computed_value_Minus(const ir_node *n) {
200         ir_node *a = get_Minus_op(n);
201         tarval *ta = value_of(a);
202
203         if (ta != tarval_bad)
204                 return tarval_neg(ta);
205
206         return tarval_bad;
207 }  /* computed_value_Minus */
208
209 /**
210  * Return the value of a Mul.
211  */
212 static tarval *computed_value_Mul(const ir_node *n) {
213         ir_node *a = get_Mul_left(n);
214         ir_node *b = get_Mul_right(n);
215         ir_mode *mode;
216
217         tarval *ta = value_of(a);
218         tarval *tb = value_of(b);
219
220         mode = get_irn_mode(n);
221         if (mode != get_irn_mode(a)) {
222                 /* n * n = 2n bit multiplication */
223                 ta = tarval_convert_to(ta, mode);
224                 tb = tarval_convert_to(tb, mode);
225         }
226
227         if (ta != tarval_bad && tb != tarval_bad) {
228                 return tarval_mul(ta, tb);
229         } else {
230                 /* a * 0 != 0 if a == NaN or a == Inf */
231                 if (!mode_is_float(mode)) {
232                         /* a*0 = 0 or 0*b = 0 */
233                         if (ta == get_mode_null(mode))
234                                 return ta;
235                         if (tb == get_mode_null(mode))
236                                 return tb;
237                 }
238         }
239         return tarval_bad;
240 }  /* computed_value_Mul */
241
242 /**
243  * Return the value of an Abs.
244  */
245 static tarval *computed_value_Abs(const ir_node *n) {
246         ir_node *a = get_Abs_op(n);
247         tarval *ta = value_of(a);
248
249         if (ta != tarval_bad)
250                 return tarval_abs(ta);
251
252         return tarval_bad;
253 }  /* computed_value_Abs */
254
255 /**
256  * Return the value of an And.
257  * Special case: a & 0, 0 & b
258  */
259 static tarval *computed_value_And(const ir_node *n) {
260         ir_node *a = get_And_left(n);
261         ir_node *b = get_And_right(n);
262
263         tarval *ta = value_of(a);
264         tarval *tb = value_of(b);
265
266         if ((ta != tarval_bad) && (tb != tarval_bad)) {
267                 return tarval_and (ta, tb);
268         } else {
269                 if (tarval_is_null(ta)) return ta;
270                 if (tarval_is_null(tb)) return tb;
271         }
272         return tarval_bad;
273 }  /* computed_value_And */
274
275 /**
276  * Return the value of an Or.
277  * Special case: a | 1...1, 1...1 | b
278  */
279 static tarval *computed_value_Or(const ir_node *n) {
280         ir_node *a = get_Or_left(n);
281         ir_node *b = get_Or_right(n);
282
283         tarval *ta = value_of(a);
284         tarval *tb = value_of(b);
285
286         if ((ta != tarval_bad) && (tb != tarval_bad)) {
287                 return tarval_or (ta, tb);
288         } else {
289                 if (tarval_is_all_one(ta)) return ta;
290                 if (tarval_is_all_one(tb)) return tb;
291         }
292         return tarval_bad;
293 }  /* computed_value_Or */
294
295 /**
296  * Return the value of an Eor.
297  */
298 static tarval *computed_value_Eor(const ir_node *n) {
299         ir_node *a = get_Eor_left(n);
300         ir_node *b = get_Eor_right(n);
301
302         tarval *ta, *tb;
303
304         if (a == b)
305                 return get_mode_null(get_irn_mode(n));
306
307         ta = value_of(a);
308         tb = value_of(b);
309
310         if ((ta != tarval_bad) && (tb != tarval_bad)) {
311                 return tarval_eor (ta, tb);
312         }
313         return tarval_bad;
314 }  /* computed_value_Eor */
315
316 /**
317  * Return the value of a Not.
318  */
319 static tarval *computed_value_Not(const ir_node *n) {
320         ir_node *a = get_Not_op(n);
321         tarval *ta = value_of(a);
322
323         if (ta != tarval_bad)
324                 return tarval_not(ta);
325
326         return tarval_bad;
327 }  /* computed_value_Not */
328
329 /**
330  * Return the value of a Shl.
331  */
332 static tarval *computed_value_Shl(const ir_node *n) {
333         ir_node *a = get_Shl_left(n);
334         ir_node *b = get_Shl_right(n);
335
336         tarval *ta = value_of(a);
337         tarval *tb = value_of(b);
338
339         if ((ta != tarval_bad) && (tb != tarval_bad)) {
340                 return tarval_shl (ta, tb);
341         }
342         return tarval_bad;
343 }  /* computed_value_Shl */
344
345 /**
346  * Return the value of a Shr.
347  */
348 static tarval *computed_value_Shr(const ir_node *n) {
349         ir_node *a = get_Shr_left(n);
350         ir_node *b = get_Shr_right(n);
351
352         tarval *ta = value_of(a);
353         tarval *tb = value_of(b);
354
355         if ((ta != tarval_bad) && (tb != tarval_bad)) {
356                 return tarval_shr (ta, tb);
357         }
358         return tarval_bad;
359 }  /* computed_value_Shr */
360
361 /**
362  * Return the value of a Shrs.
363  */
364 static tarval *computed_value_Shrs(const ir_node *n) {
365         ir_node *a = get_Shrs_left(n);
366         ir_node *b = get_Shrs_right(n);
367
368         tarval *ta = value_of(a);
369         tarval *tb = value_of(b);
370
371         if ((ta != tarval_bad) && (tb != tarval_bad)) {
372                 return tarval_shrs (ta, tb);
373         }
374         return tarval_bad;
375 }  /* computed_value_Shrs */
376
377 /**
378  * Return the value of a Rotl.
379  */
380 static tarval *computed_value_Rotl(const ir_node *n) {
381         ir_node *a = get_Rotl_left(n);
382         ir_node *b = get_Rotl_right(n);
383
384         tarval *ta = value_of(a);
385         tarval *tb = value_of(b);
386
387         if ((ta != tarval_bad) && (tb != tarval_bad)) {
388                 return tarval_rotl(ta, tb);
389         }
390         return tarval_bad;
391 }  /* computed_value_Rotl */
392
393 /**
394  * Return the value of a Conv.
395  */
396 static tarval *computed_value_Conv(const ir_node *n) {
397         ir_node *a = get_Conv_op(n);
398         tarval *ta = value_of(a);
399
400         if (ta != tarval_bad)
401                 return tarval_convert_to(ta, get_irn_mode(n));
402
403         return tarval_bad;
404 }  /* computed_value_Conv */
405
406 /**
407  * Calculate the value of a Mux: can be evaluated, if the
408  * sel and the right input are known.
409  */
410 static tarval *computed_value_Mux(const ir_node *n) {
411         ir_node *sel = get_Mux_sel(n);
412         tarval *ts = value_of(sel);
413
414         if (ts == get_tarval_b_true()) {
415                 ir_node *v = get_Mux_true(n);
416                 return value_of(v);
417         }
418         else if (ts == get_tarval_b_false()) {
419                 ir_node *v = get_Mux_false(n);
420                 return value_of(v);
421         }
422         return tarval_bad;
423 }  /* computed_value_Mux */
424
425 /**
426  * Calculate the value of a Confirm: can be evaluated,
427  * if it has the form Confirm(x, '=', Const).
428  */
429 static tarval *computed_value_Confirm(const ir_node *n) {
430         /*
431          * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
432          * Do NOT optimize them away (CondEval wants them), so wait until
433          * remove_confirm is activated.
434          */
435         if (get_opt_remove_confirm()) {
436                 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
437                         tarval *tv = value_of(get_Confirm_bound(n));
438                         if (tv != tarval_bad)
439                                 return tv;
440                 }
441         }
442         return value_of(get_Confirm_value(n));
443 }  /* computed_value_Confirm */
444
445 /**
446  * Return the value of a Proj(Cmp).
447  *
448  * This performs a first step of unreachable code elimination.
449  * Proj can not be computed, but folding a Cmp above the Proj here is
450  * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
451  * only 1 is used.
452  * There are several case where we can evaluate a Cmp node, see later.
453  */
454 static tarval *computed_value_Proj_Cmp(const ir_node *n) {
455         ir_node *a   = get_Proj_pred(n);
456         ir_node *aa  = get_Cmp_left(a);
457         ir_node *ab  = get_Cmp_right(a);
458         long proj_nr = get_Proj_proj(n);
459
460         /*
461          * BEWARE: a == a is NOT always True for floating Point values, as
462          * NaN != NaN is defined, so we must check this here.
463          */
464         if (aa == ab && (
465                 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt ||  proj_nr == pn_Cmp_Gt)
466                 ) { /* 1.: */
467
468                 /* This is a trick with the bits used for encoding the Cmp
469                    Proj numbers, the following statement is not the same:
470                 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
471                 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
472         }
473         else {
474                 tarval *taa = value_of(aa);
475                 tarval *tab = value_of(ab);
476                 ir_mode *mode = get_irn_mode(aa);
477
478                 /*
479                  * The predecessors of Cmp are target values.  We can evaluate
480                  * the Cmp.
481                  */
482                 if ((taa != tarval_bad) && (tab != tarval_bad)) {
483                         /* strange checks... */
484                         pn_Cmp flags = tarval_cmp(taa, tab);
485                         if (flags != pn_Cmp_False) {
486                                 return new_tarval_from_long (proj_nr & flags, mode_b);
487                         }
488                 }
489                 /* for integer values, we can check against MIN/MAX */
490                 else if (mode_is_int(mode)) {
491                         /* MIN <=/> x.  This results in true/false. */
492                         if (taa == get_mode_min(mode)) {
493                                 /* a compare with the MIN value */
494                                 if (proj_nr == pn_Cmp_Le)
495                                         return get_tarval_b_true();
496                                 else if (proj_nr == pn_Cmp_Gt)
497                                         return get_tarval_b_false();
498                         }
499                         /* x >=/< MIN.  This results in true/false. */
500                         else
501                                 if (tab == get_mode_min(mode)) {
502                                         /* a compare with the MIN value */
503                                         if (proj_nr == pn_Cmp_Ge)
504                                                 return get_tarval_b_true();
505                                         else if (proj_nr == pn_Cmp_Lt)
506                                                 return get_tarval_b_false();
507                                 }
508                                 /* MAX >=/< x.  This results in true/false. */
509                                 else if (taa == get_mode_max(mode)) {
510                                         if (proj_nr == pn_Cmp_Ge)
511                                                 return get_tarval_b_true();
512                                         else if (proj_nr == pn_Cmp_Lt)
513                                                 return get_tarval_b_false();
514                                 }
515                                 /* x <=/> MAX.  This results in true/false. */
516                                 else if (tab == get_mode_max(mode)) {
517                                         if (proj_nr == pn_Cmp_Le)
518                                                 return get_tarval_b_true();
519                                         else if (proj_nr == pn_Cmp_Gt)
520                                                 return get_tarval_b_false();
521                                 }
522                 }
523                 /*
524                  * The predecessors are Allocs or (void*)(0) constants.  Allocs never
525                  * return NULL, they raise an exception.   Therefore we can predict
526                  * the Cmp result.
527                  */
528                 else {
529                         ir_node *aaa = skip_Proj(aa);
530                         ir_node *aba = skip_Proj(ab);
531
532                         if (   (   (/* aa is ProjP and aaa is Alloc */
533                                        is_Proj(aa)
534                                     && mode_is_reference(get_irn_mode(aa))
535                                     && is_Alloc(aaa))
536                                 && (   (/* ab is NULL */
537                                         mode_is_reference(get_irn_mode(ab))
538                                         && tarval_is_null(tab))
539                                     || (/* ab is other Alloc */
540                                            is_Proj(ab)
541                                         && mode_is_reference(get_irn_mode(ab))
542                                         && is_Alloc(aba)
543                                         && (aaa != aba))))
544                             || (/* aa is NULL and aba is Alloc */
545                                 mode_is_reference(get_irn_mode(aa))
546                                 && tarval_is_null(taa)
547                                 && is_Proj(ab)
548                                 && mode_is_reference(get_irn_mode(ab))
549                                 && is_Alloc(aba)))
550                                 /* 3.: */
551                         return new_tarval_from_long(proj_nr & pn_Cmp_Lg, mode_b);
552                 }
553         }
554         return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
555 }  /* computed_value_Proj_Cmp */
556
557 /**
558  * Return the value of a floating point Quot.
559  */
560 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b) {
561         tarval  *ta = value_of(a);
562         tarval  *tb = value_of(b);
563
564         /* cannot optimize 0 / b = 0 because of NaN */
565         if (ta != tarval_bad && tb != tarval_bad)
566                 return tarval_quo(ta, tb);
567         return tarval_bad;
568 }  /* do_computed_value_Quot */
569
570 /**
571  * Calculate the value of an integer Div of two nodes.
572  * Special case: 0 / b
573  */
574 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b) {
575         tarval        *ta = value_of(a);
576         tarval        *tb;
577         const ir_node *dummy;
578
579         /* Compute c1 / c2 or 0 / a, a != 0 */
580         if (tarval_is_null(ta) && value_not_zero(b, &dummy))
581                 return ta;  /* 0 / b == 0 */
582         tb = value_of(b);
583         if (ta != tarval_bad && tb != tarval_bad)
584                 return tarval_div(ta, tb);
585         return tarval_bad;
586 }  /* do_computed_value_Div */
587
588 /**
589  * Calculate the value of an integer Mod of two nodes.
590  * Special case: a % 1
591  */
592 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b) {
593         tarval *ta = value_of(a);
594         tarval *tb = value_of(b);
595
596         /* Compute a % 1 or c1 % c2 */
597         if (tarval_is_one(tb))
598                 return get_mode_null(get_irn_mode(a));
599         if (ta != tarval_bad && tb != tarval_bad)
600                 return tarval_mod(ta, tb);
601         return tarval_bad;
602 }  /* do_computed_value_Mod */
603
604 /**
605  * Return the value of a Proj(DivMod).
606  */
607 static tarval *computed_value_Proj_DivMod(const ir_node *n) {
608         long proj_nr = get_Proj_proj(n);
609
610         /* compute either the Div or the Mod part */
611         if (proj_nr == pn_DivMod_res_div) {
612                 const ir_node *a = get_Proj_pred(n);
613                 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
614         } else if (proj_nr == pn_DivMod_res_mod) {
615                 const ir_node *a = get_Proj_pred(n);
616                 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
617         }
618         return tarval_bad;
619 }  /* computed_value_Proj_DivMod */
620
621 /**
622  * Return the value of a Proj(Div).
623  */
624 static tarval *computed_value_Proj_Div(const ir_node *n) {
625         long proj_nr = get_Proj_proj(n);
626
627         if (proj_nr == pn_Div_res) {
628                 const ir_node *a = get_Proj_pred(n);
629                 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
630         }
631         return tarval_bad;
632 }  /* computed_value_Proj_Div */
633
634 /**
635  * Return the value of a Proj(Mod).
636  */
637 static tarval *computed_value_Proj_Mod(const ir_node *n) {
638         long proj_nr = get_Proj_proj(n);
639
640         if (proj_nr == pn_Mod_res) {
641                 const ir_node *a = get_Proj_pred(n);
642                 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
643         }
644         return tarval_bad;
645 }  /* computed_value_Proj_Mod */
646
647 /**
648  * Return the value of a Proj(Quot).
649  */
650 static tarval *computed_value_Proj_Quot(const ir_node *n) {
651         long proj_nr = get_Proj_proj(n);
652
653         if (proj_nr == pn_Quot_res) {
654                 const ir_node *a = get_Proj_pred(n);
655                 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
656         }
657         return tarval_bad;
658 }  /* computed_value_Proj_Quot */
659
660 /**
661  * Return the value of a Proj.
662  */
663 static tarval *computed_value_Proj(const ir_node *proj) {
664         ir_node *n = get_Proj_pred(proj);
665
666         if (n->op->ops.computed_value_Proj != NULL)
667                 return n->op->ops.computed_value_Proj(proj);
668         return tarval_bad;
669 }  /* computed_value_Proj */
670
671 /**
672  * If the parameter n can be computed, return its value, else tarval_bad.
673  * Performs constant folding.
674  *
675  * @param n  The node this should be evaluated
676  */
677 tarval *computed_value(const ir_node *n) {
678         if (n->op->ops.computed_value)
679                 return n->op->ops.computed_value(n);
680         return tarval_bad;
681 }  /* computed_value */
682
683 /**
684  * Set the default computed_value evaluator in an ir_op_ops.
685  *
686  * @param code   the opcode for the default operation
687  * @param ops    the operations initialized
688  *
689  * @return
690  *    The operations.
691  */
692 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
693 {
694 #define CASE(a)                                        \
695         case iro_##a:                                      \
696                 ops->computed_value      = computed_value_##a; \
697                 break
698 #define CASE_PROJ(a)                                        \
699         case iro_##a:                                           \
700                 ops->computed_value_Proj = computed_value_Proj_##a; \
701                 break
702
703         switch (code) {
704         CASE(Const);
705         CASE(SymConst);
706         CASE(Add);
707         CASE(Sub);
708         CASE(Carry);
709         CASE(Borrow);
710         CASE(Minus);
711         CASE(Mul);
712         CASE(Abs);
713         CASE(And);
714         CASE(Or);
715         CASE(Eor);
716         CASE(Not);
717         CASE(Shl);
718         CASE(Shr);
719         CASE(Shrs);
720         CASE(Rotl);
721         CASE(Conv);
722         CASE(Mux);
723         CASE(Confirm);
724         CASE_PROJ(Cmp);
725         CASE_PROJ(DivMod);
726         CASE_PROJ(Div);
727         CASE_PROJ(Mod);
728         CASE_PROJ(Quot);
729         CASE(Proj);
730         default:
731                 /* leave NULL */;
732         }
733
734         return ops;
735 #undef CASE_PROJ
736 #undef CASE
737 }  /* firm_set_default_computed_value */
738
739 /**
740  * Returns a equivalent block for another block.
741  * If the block has only one predecessor, this is
742  * the equivalent one. If the only predecessor of a block is
743  * the block itself, this is a dead block.
744  *
745  * If both predecessors of a block are the branches of a binary
746  * Cond, the equivalent block is Cond's block.
747  *
748  * If all predecessors of a block are bad or lies in a dead
749  * block, the current block is dead as well.
750  *
751  * Note, that blocks are NEVER turned into Bad's, instead
752  * the dead_block flag is set. So, never test for is_Bad(block),
753  * always use is_dead_Block(block).
754  */
755 static ir_node *equivalent_node_Block(ir_node *n)
756 {
757         ir_node *oldn = n;
758         int     n_preds;
759
760         /* don't optimize dead or labeled blocks */
761         if (is_Block_dead(n) || has_Block_label(n))
762                 return n;
763
764         n_preds = get_Block_n_cfgpreds(n);
765
766         /* The Block constructor does not call optimize, but mature_immBlock()
767            calls the optimization. */
768         assert(get_Block_matured(n));
769
770         /* Straightening: a single entry Block following a single exit Block
771            can be merged, if it is not the Start block. */
772         /* !!! Beware, all Phi-nodes of n must have been optimized away.
773            This should be true, as the block is matured before optimize is called.
774            But what about Phi-cycles with the Phi0/Id that could not be resolved?
775            Remaining Phi nodes are just Ids. */
776         if (n_preds == 1) {
777                 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
778
779                 if (is_Jmp(pred)) {
780                         ir_node *predblock = get_nodes_block(pred);
781                         if (predblock == oldn) {
782                                 /* Jmp jumps into the block it is in -- deal self cycle. */
783                                 n = set_Block_dead(n);
784                                 DBG_OPT_DEAD_BLOCK(oldn, n);
785                         } else if (get_opt_control_flow_straightening()) {
786                                 n = predblock;
787                                 DBG_OPT_STG(oldn, n);
788                         }
789                 } else if (is_Cond(pred)) {
790                         ir_node *predblock = get_nodes_block(pred);
791                         if (predblock == oldn) {
792                                 /* Jmp jumps into the block it is in -- deal self cycle. */
793                                 n = set_Block_dead(n);
794                                 DBG_OPT_DEAD_BLOCK(oldn, n);
795                         }
796                 }
797         } else if ((n_preds == 2) &&
798                    (get_opt_control_flow_weak_simplification())) {
799                 /* Test whether Cond jumps twice to this block
800                  * The more general case which more than 2 predecessors is handles
801                  * in optimize_cf(), we handle only this special case for speed here.
802                  */
803                 ir_node *a = get_Block_cfgpred(n, 0);
804                 ir_node *b = get_Block_cfgpred(n, 1);
805
806                 if (is_Proj(a) && is_Proj(b)) {
807                         ir_node *cond = get_Proj_pred(a);
808
809                     if (cond == get_Proj_pred(b) && is_Cond(cond) &&
810                         get_irn_mode(get_Cond_selector(cond)) == mode_b) {
811                                 /* Also a single entry Block following a single exit Block.  Phis have
812                                    twice the same operand and will be optimized away. */
813                                 n = get_nodes_block(cond);
814                                 DBG_OPT_IFSIM1(oldn, a, b, n);
815                         }
816                 }
817         } else if (get_opt_unreachable_code() &&
818                    (n != get_irg_start_block(current_ir_graph)) &&
819                    (n != get_irg_end_block(current_ir_graph))    ) {
820                 int i;
821
822                 /* If all inputs are dead, this block is dead too, except if it is
823                    the start or end block.  This is one step of unreachable code
824                    elimination */
825                 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
826                         ir_node *pred = get_Block_cfgpred(n, i);
827                         ir_node *pred_blk;
828
829                         if (is_Bad(pred)) continue;
830                         pred_blk = get_nodes_block(skip_Proj(pred));
831
832                         if (is_Block_dead(pred_blk)) continue;
833
834                         if (pred_blk != n) {
835                                 /* really found a living input */
836                                 break;
837                         }
838                 }
839                 if (i < 0) {
840                         n = set_Block_dead(n);
841                         DBG_OPT_DEAD_BLOCK(oldn, n);
842                 }
843         }
844
845         return n;
846 }  /* equivalent_node_Block */
847
848 /**
849  * Returns a equivalent node for a Jmp, a Bad :-)
850  * Of course this only happens if the Block of the Jmp is dead.
851  */
852 static ir_node *equivalent_node_Jmp(ir_node *n) {
853         ir_node *oldn = n;
854
855         /* unreachable code elimination */
856         if (is_Block_dead(get_nodes_block(n))) {
857                 n = get_irg_bad(current_ir_graph);
858                 DBG_OPT_DEAD_BLOCK(oldn, n);
859         }
860         return n;
861 }  /* equivalent_node_Jmp */
862
863 /** Raise is handled in the same way as Jmp. */
864 #define equivalent_node_Raise   equivalent_node_Jmp
865
866
867 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
868    See transform_node_Proj_Cond(). */
869
870 /**
871  * Optimize operations that are commutative and have neutral 0,
872  * so a op 0 = 0 op a = a.
873  */
874 static ir_node *equivalent_node_neutral_zero(ir_node *n) {
875         ir_node *oldn = n;
876
877         ir_node *a = get_binop_left(n);
878         ir_node *b = get_binop_right(n);
879
880         tarval *tv;
881         ir_node *on;
882
883         /* After running compute_node there is only one constant predecessor.
884            Find this predecessors value and remember the other node: */
885         if ((tv = value_of(a)) != tarval_bad) {
886                 on = b;
887         } else if ((tv = value_of(b)) != tarval_bad) {
888                 on = a;
889         } else
890                 return n;
891
892         /* If this predecessors constant value is zero, the operation is
893          * unnecessary. Remove it.
894          *
895          * Beware: If n is a Add, the mode of on and n might be different
896          * which happens in this rare construction: NULL + 3.
897          * Then, a Conv would be needed which we cannot include here.
898          */
899         if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
900                 n = on;
901
902                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
903         }
904
905         return n;
906 }  /* equivalent_node_neutral_zero */
907
908 /**
909  * Eor is commutative and has neutral 0.
910  */
911 static ir_node *equivalent_node_Eor(ir_node *n) {
912         ir_node *oldn = n;
913         ir_node *a;
914         ir_node *b;
915
916         n = equivalent_node_neutral_zero(n);
917         if (n != oldn) return n;
918
919         a = get_Eor_left(n);
920         b = get_Eor_right(n);
921
922         if (is_Eor(a)) {
923                 ir_node *aa = get_Eor_left(a);
924                 ir_node *ab = get_Eor_right(a);
925
926                 if (aa == b) {
927                         /* (a ^ b) ^ a -> b */
928                         n = ab;
929                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
930                         return n;
931                 } else if (ab == b) {
932                         /* (a ^ b) ^ b -> a */
933                         n = aa;
934                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
935                         return n;
936                 }
937         }
938         if (is_Eor(b)) {
939                 ir_node *ba = get_Eor_left(b);
940                 ir_node *bb = get_Eor_right(b);
941
942                 if (ba == a) {
943                         /* a ^ (a ^ b) -> b */
944                         n = bb;
945                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
946                         return n;
947                 } else if (bb == a) {
948                         /* a ^ (b ^ a) -> b */
949                         n = ba;
950                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
951                         return n;
952                 }
953         }
954         return n;
955 }
956
957 /*
958  * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
959  *
960  * The second one looks strange, but this construct
961  * is used heavily in the LCC sources :-).
962  *
963  * Beware: The Mode of an Add may be different than the mode of its
964  * predecessors, so we could not return a predecessors in all cases.
965  */
966 static ir_node *equivalent_node_Add(ir_node *n) {
967         ir_node *oldn = n;
968         ir_node *left, *right;
969         ir_mode *mode = get_irn_mode(n);
970
971         n = equivalent_node_neutral_zero(n);
972         if (n != oldn)
973                 return n;
974
975         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
976         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
977                 return n;
978
979         left  = get_Add_left(n);
980         right = get_Add_right(n);
981
982         if (is_Sub(left)) {
983                 if (get_Sub_right(left) == right) {
984                         /* (a - x) + x */
985
986                         n = get_Sub_left(left);
987                         if (mode == get_irn_mode(n)) {
988                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
989                                 return n;
990                         }
991                 }
992         }
993         if (is_Sub(right)) {
994                 if (get_Sub_right(right) == left) {
995                         /* x + (a - x) */
996
997                         n = get_Sub_left(right);
998                         if (mode == get_irn_mode(n)) {
999                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1000                                 return n;
1001                         }
1002                 }
1003         }
1004         return n;
1005 }  /* equivalent_node_Add */
1006
1007 /**
1008  * optimize operations that are not commutative but have neutral 0 on left,
1009  * so a op 0 = a.
1010  */
1011 static ir_node *equivalent_node_left_zero(ir_node *n) {
1012         ir_node *oldn = n;
1013
1014         ir_node *a  = get_binop_left(n);
1015         ir_node *b  = get_binop_right(n);
1016         tarval  *tb = value_of(b);
1017
1018         if (tarval_is_null(tb)) {
1019                 n = a;
1020
1021                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1022         }
1023         return n;
1024 }  /* equivalent_node_left_zero */
1025
1026 #define equivalent_node_Shl   equivalent_node_left_zero
1027 #define equivalent_node_Shr   equivalent_node_left_zero
1028 #define equivalent_node_Shrs  equivalent_node_left_zero
1029 #define equivalent_node_Rotl  equivalent_node_left_zero
1030
1031 /**
1032  * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1033  *
1034  * The second one looks strange, but this construct
1035  * is used heavily in the LCC sources :-).
1036  *
1037  * Beware: The Mode of a Sub may be different than the mode of its
1038  * predecessors, so we could not return a predecessors in all cases.
1039  */
1040 static ir_node *equivalent_node_Sub(ir_node *n) {
1041         ir_node *oldn = n;
1042         ir_node *b;
1043         ir_mode *mode = get_irn_mode(n);
1044         tarval  *tb;
1045
1046         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1047         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1048                 return n;
1049
1050         b  = get_Sub_right(n);
1051         tb = value_of(b);
1052
1053         /* Beware: modes might be different */
1054         if (tarval_is_null(tb)) {
1055                 ir_node *a = get_Sub_left(n);
1056                 if (mode == get_irn_mode(a)) {
1057                         n = a;
1058
1059                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1060                 }
1061         }
1062         return n;
1063 }  /* equivalent_node_Sub */
1064
1065
1066 /**
1067  * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1068  *
1069  * @todo
1070  *   -(-a) == a, but might overflow two times.
1071  *   We handle it anyway here but the better way would be a
1072  *   flag. This would be needed for Pascal for instance.
1073  */
1074 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1075         ir_node *oldn = n;
1076         ir_node *pred = get_unop_op(n);
1077
1078         /* optimize symmetric unop */
1079         if (get_irn_op(pred) == get_irn_op(n)) {
1080                 n = get_unop_op(pred);
1081                 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1082         }
1083         return n;
1084 }  /* equivalent_node_idempotent_unop */
1085
1086 /** Optimize Not(Not(x)) == x. */
1087 #define equivalent_node_Not    equivalent_node_idempotent_unop
1088
1089 /** -(-x) == x       ??? Is this possible or can --x raise an
1090                        out of bounds exception if min =! max? */
1091 #define equivalent_node_Minus  equivalent_node_idempotent_unop
1092
1093 /**
1094  * Optimize a * 1 = 1 * a = a.
1095  */
1096 static ir_node *equivalent_node_Mul(ir_node *n) {
1097         ir_node *oldn = n;
1098         ir_node *a = get_Mul_left(n);
1099
1100         /* we can handle here only the n * n = n bit cases */
1101         if (get_irn_mode(n) == get_irn_mode(a)) {
1102                 ir_node *b = get_Mul_right(n);
1103                 tarval  *tv;
1104
1105                 /*
1106                  * Mul is commutative and has again an other neutral element.
1107                  * Constants are place right, so check this case first.
1108                  */
1109                 tv = value_of(b);
1110                 if (tarval_is_one(tv)) {
1111                         n = a;
1112                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1113                 } else {
1114                         tv = value_of(a);
1115                         if (tarval_is_one(tv)) {
1116                                 n = b;
1117                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1118                         }
1119                 }
1120         }
1121         return n;
1122 }  /* equivalent_node_Mul */
1123
1124 /**
1125  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1126  */
1127 static ir_node *equivalent_node_Or(ir_node *n) {
1128         ir_node *oldn = n;
1129
1130         ir_node *a = get_Or_left(n);
1131         ir_node *b = get_Or_right(n);
1132         tarval *tv;
1133
1134         if (a == b) {
1135                 n = a;    /* Or has it's own neutral element */
1136                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1137                 return n;
1138         }
1139         /* constants are cormalized to right, check this site first */
1140         tv = value_of(b);
1141         if (tarval_is_null(tv)) {
1142                 n = a;
1143                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1144                 return n;
1145         }
1146         tv = value_of(a);
1147         if (tarval_is_null(tv)) {
1148                 n = b;
1149                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1150                 return n;
1151         }
1152
1153         return n;
1154 }  /* equivalent_node_Or */
1155
1156 /**
1157  * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1158  */
1159 static ir_node *equivalent_node_And(ir_node *n) {
1160         ir_node *oldn = n;
1161
1162         ir_node *a = get_And_left(n);
1163         ir_node *b = get_And_right(n);
1164         tarval *tv;
1165
1166         if (a == b) {
1167                 n = a;    /* And has it's own neutral element */
1168                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1169                 return n;
1170         }
1171         /* constants are normalized to right, check this site first */
1172         tv = value_of(b);
1173         if (tarval_is_all_one(tv)) {
1174                 n = a;
1175                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1176                 return n;
1177         }
1178         if (tv != get_tarval_bad()) {
1179                 ir_mode *mode = get_irn_mode(n);
1180                 if (!mode_is_signed(mode) && is_Conv(a)) {
1181                         ir_node *convop     = get_Conv_op(a);
1182                         ir_mode *convopmode = get_irn_mode(convop);
1183                         if (!mode_is_signed(convopmode)) {
1184                                 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1185                                         /* Conv(X) & all_one(mode(X)) = Conv(X) */
1186                                         n = a;
1187                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1188                                         return n;
1189                                 }
1190                         }
1191                 }
1192         }
1193         tv = value_of(a);
1194         if (tarval_is_all_one(tv)) {
1195                 n = b;
1196                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1197                 return n;
1198         }
1199         if (is_Or(a)) {
1200                 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1201                         /* (a|X) & a */
1202                         n = b;
1203                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1204                         return n;
1205                 }
1206         }
1207         if (is_Or(b)) {
1208                 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1209                         /* a & (a|X) */
1210                         n = a;
1211                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1212                         return n;
1213                 }
1214         }
1215         return n;
1216 }  /* equivalent_node_And */
1217
1218 /**
1219  * Try to remove useless Conv's:
1220  */
1221 static ir_node *equivalent_node_Conv(ir_node *n) {
1222         ir_node *oldn = n;
1223         ir_node *a = get_Conv_op(n);
1224
1225         ir_mode *n_mode = get_irn_mode(n);
1226         ir_mode *a_mode = get_irn_mode(a);
1227
1228 restart:
1229         if (n_mode == a_mode) { /* No Conv necessary */
1230                 if (get_Conv_strict(n)) {
1231                         ir_node *p = a;
1232
1233                         /* neither Minus nor Abs nor Confirm change the precision,
1234                            so we can "look-through" */
1235                         for (;;) {
1236                                 if (is_Minus(p)) {
1237                                         p = get_Minus_op(p);
1238                                 } else if (is_Abs(p)) {
1239                                         p = get_Abs_op(p);
1240                                 } else if (is_Confirm(p)) {
1241                                         p = get_Confirm_value(p);
1242                                 } else {
1243                                         /* stop here */
1244                                         break;
1245                                 }
1246                         }
1247                         if (is_Conv(p) && get_Conv_strict(p)) {
1248                                 /* we known already, that a_mode == n_mode, and neither
1249                                    Abs nor Minus change the mode, so the second Conv
1250                                    can be kicked */
1251                                 assert(get_irn_mode(p) == n_mode);
1252                                 n = a;
1253                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1254                                 return n;
1255                         }
1256                         if (is_Proj(p)) {
1257                                 ir_node *pred = get_Proj_pred(p);
1258                                 if (is_Load(pred)) {
1259                                         /* Loads always return with the exact precision of n_mode */
1260                                         assert(get_Load_mode(pred) == n_mode);
1261                                         n = a;
1262                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1263                                         return n;
1264                                 }
1265                                 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1266                                         pred = get_Proj_pred(pred);
1267                                         if (is_Start(pred)) {
1268                                                 /* Arguments always return with the exact precision,
1269                                                    as strictConv's are place before Call -- if the
1270                                                    caller was compiled with the same setting.
1271                                                    Otherwise, the semantics is probably still right. */
1272                                                 assert(get_irn_mode(p) == n_mode);
1273                                                 n = a;
1274                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1275                                                 return n;
1276                                         }
1277                                 }
1278                         }
1279                         if (is_Conv(a)) {
1280                                 /* special case: the immediate predecessor is also a Conv */
1281                                 if (! get_Conv_strict(a)) {
1282                                         /* first one is not strict, kick it */
1283                                         a = get_Conv_op(a);
1284                                         a_mode = get_irn_mode(a);
1285                                         set_Conv_op(n, a);
1286                                         goto restart;
1287                                 }
1288                                 /* else both are strict conv, second is superfluous */
1289                                 n = a;
1290                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1291                                 return n;
1292                         }
1293                 } else {
1294                         n = a;
1295                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1296                         return n;
1297                 }
1298         } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1299                 ir_node *b      = get_Conv_op(a);
1300                 ir_mode *b_mode = get_irn_mode(b);
1301
1302                 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1303                         /* both are strict conv */
1304                         if (smaller_mode(a_mode, n_mode)) {
1305                                 /* both are strict, but the first is smaller, so
1306                                    the second cannot remove more precision, remove the
1307                                    strict bit */
1308                                 set_Conv_strict(n, 0);
1309                         }
1310                 }
1311                 if (n_mode == b_mode) {
1312                         if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1313                                 if (n_mode == mode_b) {
1314                                         n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1315                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1316                                         return n;
1317                                 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1318                                         if (values_in_mode(b_mode, a_mode)) {
1319                                                 n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1320                                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1321                                                 return n;
1322                                         }
1323                                 }
1324                         }
1325                         if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1326                                 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1327                                 unsigned int_mantissa   = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1328                                 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1329
1330                                 if (float_mantissa >= int_mantissa) {
1331                                         n = b;
1332                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1333                                         return n;
1334                                 }
1335                         }
1336                         if (is_Conv(b)) {
1337                                 if (smaller_mode(b_mode, a_mode)) {
1338                                         if (get_Conv_strict(n))
1339                                                 set_Conv_strict(b, 1);
1340                                         n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1341                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1342                                         return n;
1343                                 }
1344                         }
1345                 }
1346         }
1347         return n;
1348 }  /* equivalent_node_Conv */
1349
1350 /**
1351  * A Cast may be removed if the type of the previous node
1352  * is already the type of the Cast.
1353  */
1354 static ir_node *equivalent_node_Cast(ir_node *n) {
1355         ir_node *oldn = n;
1356         ir_node *pred = get_Cast_op(n);
1357
1358         if (get_irn_type(pred) == get_Cast_type(n)) {
1359                 n = pred;
1360                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1361         }
1362         return n;
1363 }  /* equivalent_node_Cast */
1364
1365 /**
1366  * - fold Phi-nodes, iff they have only one predecessor except
1367  *   themselves.
1368  */
1369 static ir_node *equivalent_node_Phi(ir_node *n) {
1370         int i, n_preds;
1371
1372         ir_node *oldn = n;
1373         ir_node *block;
1374         ir_node *first_val = NULL; /* to shutup gcc */
1375
1376         if (!get_opt_normalize()) return n;
1377
1378         n_preds = get_Phi_n_preds(n);
1379
1380         block = get_nodes_block(n);
1381         if (is_Block_dead(block))                  /* Control dead */
1382                 return get_irg_bad(current_ir_graph);
1383
1384         if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1385
1386         /* Find first non-self-referencing input */
1387         for (i = 0; i < n_preds; ++i) {
1388                 first_val = get_Phi_pred(n, i);
1389                 if (   (first_val != n)                            /* not self pointer */
1390 #if 0
1391                     /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1392                      * predecessors. Then, Phi nodes in dead code might be removed, causing
1393                      * nodes pointing to themself (Add's for instance).
1394                      * This is really bad and causes endless recursions in several
1395                      * code pathes, so we do NOT optimize such a code.
1396                      * This is not that bad as it sounds, optimize_cf() removes bad control flow
1397                      * (and bad Phi predecessors), so live code is optimized later.
1398                      */
1399                         && (! is_Bad(get_Block_cfgpred(block, i)))
1400 #endif
1401                    ) {        /* value not dead */
1402                         break;          /* then found first value. */
1403                 }
1404         }
1405
1406         if (i >= n_preds) {
1407                 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1408                 return get_irg_bad(current_ir_graph);
1409         }
1410
1411         /* search for rest of inputs, determine if any of these
1412         are non-self-referencing */
1413         while (++i < n_preds) {
1414                 ir_node *scnd_val = get_Phi_pred(n, i);
1415                 if (   (scnd_val != n)
1416                     && (scnd_val != first_val)
1417 #if 0
1418                     /* see above */
1419                     && (! is_Bad(get_Block_cfgpred(block, i)))
1420 #endif
1421                         ) {
1422                         break;
1423                 }
1424         }
1425
1426         if (i >= n_preds) {
1427                 /* Fold, if no multiple distinct non-self-referencing inputs */
1428                 n = first_val;
1429                 DBG_OPT_PHI(oldn, n);
1430         }
1431         return n;
1432 }  /* equivalent_node_Phi */
1433
1434 /**
1435  * Several optimizations:
1436  * - fold Sync-nodes, iff they have only one predecessor except
1437  *   themselves.
1438  */
1439 static ir_node *equivalent_node_Sync(ir_node *n) {
1440         int arity = get_Sync_n_preds(n);
1441         int i;
1442
1443         for (i = 0; i < arity;) {
1444                 ir_node *pred = get_Sync_pred(n, i);
1445                 int      j;
1446
1447                 /* Remove Bad predecessors */
1448                 if (is_Bad(pred)) {
1449                         del_Sync_n(n, i);
1450                         --arity;
1451                         continue;
1452                 }
1453
1454                 /* Remove duplicate predecessors */
1455                 for (j = 0;; ++j) {
1456                         if (j >= i) {
1457                                 ++i;
1458                                 break;
1459                         }
1460                         if (get_Sync_pred(n, j) == pred) {
1461                                 del_Sync_n(n, i);
1462                                 --arity;
1463                                 break;
1464                         }
1465                 }
1466         }
1467
1468         if (arity == 0) return get_irg_bad(current_ir_graph);
1469         if (arity == 1) return get_Sync_pred(n, 0);
1470         return n;
1471 }  /* equivalent_node_Sync */
1472
1473 /**
1474  * Optimize Proj(Tuple).
1475  */
1476 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1477         ir_node *oldn  = proj;
1478         ir_node *tuple = get_Proj_pred(proj);
1479
1480         /* Remove the Tuple/Proj combination. */
1481         proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1482         DBG_OPT_TUPLE(oldn, tuple, proj);
1483
1484         return proj;
1485 }  /* equivalent_node_Proj_Tuple */
1486
1487 /**
1488  * Optimize a / 1 = a.
1489  */
1490 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1491         ir_node *oldn = proj;
1492         ir_node *div  = get_Proj_pred(proj);
1493         ir_node *b    = get_Div_right(div);
1494         tarval  *tb   = value_of(b);
1495
1496         /* Div is not commutative. */
1497         if (tarval_is_one(tb)) { /* div(x, 1) == x */
1498                 switch (get_Proj_proj(proj)) {
1499                 case pn_Div_M:
1500                         proj = get_Div_mem(div);
1501                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1502                         return proj;
1503
1504                 case pn_Div_res:
1505                         proj = get_Div_left(div);
1506                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1507                         return proj;
1508
1509                 default:
1510                         /* we cannot replace the exception Proj's here, this is done in
1511                            transform_node_Proj_Div() */
1512                         return proj;
1513                 }
1514         }
1515         return proj;
1516 }  /* equivalent_node_Proj_Div */
1517
1518 /**
1519  * Optimize a / 1.0 = a.
1520  */
1521 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1522         ir_node *oldn = proj;
1523         ir_node *quot = get_Proj_pred(proj);
1524         ir_node *b    = get_Quot_right(quot);
1525         tarval  *tb   = value_of(b);
1526
1527         /* Div is not commutative. */
1528         if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1529                 switch (get_Proj_proj(proj)) {
1530                 case pn_Quot_M:
1531                         proj = get_Quot_mem(quot);
1532                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1533                         return proj;
1534
1535                 case pn_Quot_res:
1536                         proj = get_Quot_left(quot);
1537                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1538                         return proj;
1539
1540                 default:
1541                         /* we cannot replace the exception Proj's here, this is done in
1542                            transform_node_Proj_Quot() */
1543                         return proj;
1544                 }
1545         }
1546         return proj;
1547 }  /* equivalent_node_Proj_Quot */
1548
1549 /**
1550  * Optimize a / 1 = a.
1551  */
1552 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1553         ir_node *oldn   = proj;
1554         ir_node *divmod = get_Proj_pred(proj);
1555         ir_node *b      = get_DivMod_right(divmod);
1556         tarval  *tb     = value_of(b);
1557
1558         /* Div is not commutative. */
1559         if (tarval_is_one(tb)) { /* div(x, 1) == x */
1560                 switch (get_Proj_proj(proj)) {
1561                 case pn_DivMod_M:
1562                         proj = get_DivMod_mem(divmod);
1563                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1564                         return proj;
1565
1566                 case pn_DivMod_res_div:
1567                         proj = get_DivMod_left(divmod);
1568                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1569                         return proj;
1570
1571                 default:
1572                         /* we cannot replace the exception Proj's here, this is done in
1573                            transform_node_Proj_DivMod().
1574                            Note further that the pn_DivMod_res_div case is handled in
1575                            computed_value_Proj(). */
1576                         return proj;
1577                 }
1578         }
1579         return proj;
1580 }  /* equivalent_node_Proj_DivMod */
1581
1582 /**
1583  * Optimize CopyB(mem, x, x) into a Nop.
1584  */
1585 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1586         ir_node *oldn  = proj;
1587         ir_node *copyb = get_Proj_pred(proj);
1588         ir_node *a     = get_CopyB_dst(copyb);
1589         ir_node *b     = get_CopyB_src(copyb);
1590
1591         if (a == b) {
1592                 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1593                 switch (get_Proj_proj(proj)) {
1594                 case pn_CopyB_M_regular:
1595                         proj = get_CopyB_mem(copyb);
1596                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1597                         break;
1598
1599                 case pn_CopyB_M_except:
1600                 case pn_CopyB_X_except:
1601                         DBG_OPT_EXC_REM(proj);
1602                         proj = get_irg_bad(current_ir_graph);
1603                         break;
1604                 }
1605         }
1606         return proj;
1607 }  /* equivalent_node_Proj_CopyB */
1608
1609 /**
1610  * Optimize Bounds(idx, idx, upper) into idx.
1611  */
1612 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1613         ir_node *oldn  = proj;
1614         ir_node *bound = get_Proj_pred(proj);
1615         ir_node *idx   = get_Bound_index(bound);
1616         ir_node *pred  = skip_Proj(idx);
1617         int ret_tuple  = 0;
1618
1619         if (idx == get_Bound_lower(bound))
1620                 ret_tuple = 1;
1621         else if (is_Bound(pred)) {
1622                 /*
1623                  * idx was Bounds checked in the same MacroBlock previously,
1624                  * it is still valid if lower <= pred_lower && pred_upper <= upper.
1625                  */
1626                 ir_node *lower = get_Bound_lower(bound);
1627                 ir_node *upper = get_Bound_upper(bound);
1628                 if (get_Bound_lower(pred) == lower &&
1629                         get_Bound_upper(pred) == upper &&
1630                         get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1631                         /*
1632                          * One could expect that we simply return the previous
1633                          * Bound here. However, this would be wrong, as we could
1634                          * add an exception Proj to a new location then.
1635                          * So, we must turn in into a tuple.
1636                          */
1637                         ret_tuple = 1;
1638                 }
1639         }
1640         if (ret_tuple) {
1641                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1642                 switch (get_Proj_proj(proj)) {
1643                 case pn_Bound_M:
1644                         DBG_OPT_EXC_REM(proj);
1645                         proj = get_Bound_mem(bound);
1646                         break;
1647                 case pn_Bound_X_except:
1648                         DBG_OPT_EXC_REM(proj);
1649                         proj = get_irg_bad(current_ir_graph);
1650                         break;
1651                 case pn_Bound_res:
1652                         proj = idx;
1653                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1654                         break;
1655                 default:
1656                         /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1657                         ;
1658                 }
1659         }
1660         return proj;
1661 }  /* equivalent_node_Proj_Bound */
1662
1663 /**
1664  * Optimize an Exception Proj(Load) with a non-null address.
1665  */
1666 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1667         if (get_opt_ldst_only_null_ptr_exceptions()) {
1668                 if (get_irn_mode(proj) == mode_X) {
1669                         ir_node *load = get_Proj_pred(proj);
1670
1671                         /* get the Load address */
1672                         const ir_node *addr = get_Load_ptr(load);
1673                         const ir_node *confirm;
1674
1675                         if (value_not_null(addr, &confirm)) {
1676                                 if (get_Proj_proj(proj) == pn_Load_X_except) {
1677                                         DBG_OPT_EXC_REM(proj);
1678                                         return get_irg_bad(current_ir_graph);
1679                                 }
1680                         }
1681                 }
1682         }
1683         return proj;
1684 }  /* equivalent_node_Proj_Load */
1685
1686 /**
1687  * Optimize an Exception Proj(Store) with a non-null address.
1688  */
1689 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1690         if (get_opt_ldst_only_null_ptr_exceptions()) {
1691                 if (get_irn_mode(proj) == mode_X) {
1692                         ir_node *store = get_Proj_pred(proj);
1693
1694                         /* get the load/store address */
1695                         const ir_node *addr = get_Store_ptr(store);
1696                         const ir_node *confirm;
1697
1698                         if (value_not_null(addr, &confirm)) {
1699                                 if (get_Proj_proj(proj) == pn_Store_X_except) {
1700                                         DBG_OPT_EXC_REM(proj);
1701                                         return get_irg_bad(current_ir_graph);
1702                                 }
1703                         }
1704                 }
1705         }
1706         return proj;
1707 }  /* equivalent_node_Proj_Store */
1708
1709 /**
1710  * Does all optimizations on nodes that must be done on it's Proj's
1711  * because of creating new nodes.
1712  */
1713 static ir_node *equivalent_node_Proj(ir_node *proj) {
1714         ir_node *n = get_Proj_pred(proj);
1715
1716         if (get_irn_mode(proj) == mode_X) {
1717                 if (is_Block_dead(get_nodes_block(n))) {
1718                         /* Remove dead control flow -- early gigo(). */
1719                         return get_irg_bad(current_ir_graph);
1720                 }
1721         }
1722         if (n->op->ops.equivalent_node_Proj)
1723                 return n->op->ops.equivalent_node_Proj(proj);
1724         return proj;
1725 }  /* equivalent_node_Proj */
1726
1727 /**
1728  * Remove Id's.
1729  */
1730 static ir_node *equivalent_node_Id(ir_node *n) {
1731         ir_node *oldn = n;
1732
1733         do {
1734                 n = get_Id_pred(n);
1735         } while (is_Id(n));
1736
1737         DBG_OPT_ID(oldn, n);
1738         return n;
1739 }  /* equivalent_node_Id */
1740
1741 /**
1742  * Optimize a Mux.
1743  */
1744 static ir_node *equivalent_node_Mux(ir_node *n)
1745 {
1746         ir_node *oldn = n, *sel = get_Mux_sel(n);
1747         tarval *ts = value_of(sel);
1748
1749         /* Mux(true, f, t) == t */
1750         if (ts == tarval_b_true) {
1751                 n = get_Mux_true(n);
1752                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1753         }
1754         /* Mux(false, f, t) == f */
1755         else if (ts == tarval_b_false) {
1756                 n = get_Mux_false(n);
1757                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1758         }
1759         /* Mux(v, x, x) == x */
1760         else if (get_Mux_false(n) == get_Mux_true(n)) {
1761                 n = get_Mux_true(n);
1762                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1763         }
1764         else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1765                 ir_node *cmp = get_Proj_pred(sel);
1766                 long proj_nr = get_Proj_proj(sel);
1767                 ir_node *f   = get_Mux_false(n);
1768                 ir_node *t   = get_Mux_true(n);
1769
1770                 /*
1771                  * Note further that these optimization work even for floating point
1772                  * with NaN's because -NaN == NaN.
1773                  * However, if +0 and -0 is handled differently, we cannot use the first one.
1774                  */
1775                 if (is_Cmp(cmp)) {
1776                         ir_node *const cmp_l = get_Cmp_left(cmp);
1777                         ir_node *const cmp_r = get_Cmp_right(cmp);
1778
1779                         switch (proj_nr) {
1780                                 case pn_Cmp_Eq:
1781                                         if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1782                                                         (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1783                                                 n = f;
1784                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1785                                                 return n;
1786                                         }
1787                                         break;
1788
1789                                 case pn_Cmp_Lg:
1790                                 case pn_Cmp_Ne:
1791                                         if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1792                                                         (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1793                                                 n = t;
1794                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1795                                                 return n;
1796                                         }
1797                                         break;
1798                         }
1799
1800                         /*
1801                          * Note: normalization puts the constant on the right side,
1802                          * so we check only one case.
1803                          */
1804                         if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1805                                 /* Mux(t CMP 0, X, t) */
1806                                 if (is_Minus(f) && get_Minus_op(f) == t) {
1807                                         /* Mux(t CMP 0, -t, t) */
1808                                         if (proj_nr == pn_Cmp_Eq) {
1809                                                 /* Mux(t == 0, -t, t)  ==>  -t */
1810                                                 n = f;
1811                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1812                                         } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1813                                                 /* Mux(t != 0, -t, t)  ==> t */
1814                                                 n = t;
1815                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1816                                         }
1817                                 }
1818                         }
1819                 }
1820         }
1821         return n;
1822 }  /* equivalent_node_Mux */
1823
1824 /**
1825  * Remove Confirm nodes if setting is on.
1826  * Replace Confirms(x, '=', Constlike) by Constlike.
1827  */
1828 static ir_node *equivalent_node_Confirm(ir_node *n) {
1829         ir_node *pred = get_Confirm_value(n);
1830         pn_Cmp  pnc   = get_Confirm_cmp(n);
1831
1832         while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1833                 /*
1834                  * rare case: two identical Confirms one after another,
1835                  * replace the second one with the first.
1836                  */
1837                 n    = pred;
1838                 pred = get_Confirm_value(n);
1839                 pnc  = get_Confirm_cmp(n);
1840         }
1841         if (get_opt_remove_confirm())
1842                 return get_Confirm_value(n);
1843         return n;
1844 }
1845
1846 /**
1847  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1848  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1849  * new nodes.  It is therefore safe to free n if the node returned is not n.
1850  * If a node returns a Tuple we can not just skip it.  If the size of the
1851  * in array fits, we transform n into a tuple (e.g., Div).
1852  */
1853 ir_node *equivalent_node(ir_node *n) {
1854         if (n->op->ops.equivalent_node)
1855                 return n->op->ops.equivalent_node(n);
1856         return n;
1857 }  /* equivalent_node */
1858
1859 /**
1860  * Sets the default equivalent node operation for an ir_op_ops.
1861  *
1862  * @param code   the opcode for the default operation
1863  * @param ops    the operations initialized
1864  *
1865  * @return
1866  *    The operations.
1867  */
1868 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1869 {
1870 #define CASE(a)                                      \
1871         case iro_##a:                                    \
1872                 ops->equivalent_node  = equivalent_node_##a; \
1873                 break
1874 #define CASE_PROJ(a)                                          \
1875         case iro_##a:                                             \
1876                 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1877                 break
1878
1879         switch (code) {
1880         CASE(Block);
1881         CASE(Jmp);
1882         CASE(Raise);
1883         CASE(Eor);
1884         CASE(Add);
1885         CASE(Shl);
1886         CASE(Shr);
1887         CASE(Shrs);
1888         CASE(Rotl);
1889         CASE(Sub);
1890         CASE(Not);
1891         CASE(Minus);
1892         CASE(Mul);
1893         CASE(Or);
1894         CASE(And);
1895         CASE(Conv);
1896         CASE(Cast);
1897         CASE(Phi);
1898         CASE(Sync);
1899         CASE_PROJ(Tuple);
1900         CASE_PROJ(Div);
1901         CASE_PROJ(Quot);
1902         CASE_PROJ(DivMod);
1903         CASE_PROJ(CopyB);
1904         CASE_PROJ(Bound);
1905         CASE_PROJ(Load);
1906         CASE_PROJ(Store);
1907         CASE(Proj);
1908         CASE(Id);
1909         CASE(Mux);
1910         CASE(Confirm);
1911         default:
1912                 /* leave NULL */;
1913         }
1914
1915         return ops;
1916 #undef CASE
1917 #undef CASE_PROJ
1918 }  /* firm_set_default_equivalent_node */
1919
1920 /**
1921  * Returns non-zero if a node is a Phi node
1922  * with all predecessors constant.
1923  */
1924 static int is_const_Phi(ir_node *n) {
1925         int i;
1926
1927         if (! is_Phi(n) || get_irn_arity(n) == 0)
1928                 return 0;
1929         for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1930                 if (! is_Const(get_irn_n(n, i)))
1931                         return 0;
1932         }
1933         return 1;
1934 }  /* is_const_Phi */
1935
1936 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1937 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1938
1939 /**
1940  * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1941  */
1942 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1943         if (eval == tarval_sub) {
1944                 tarval_sub_type func = (tarval_sub_type)eval;
1945
1946                 return func(a, b, mode);
1947         } else {
1948                 tarval_binop_type func = (tarval_binop_type)eval;
1949
1950                 return func(a, b);
1951         }
1952 }
1953
1954 /**
1955  * Apply an evaluator on a binop with a constant operators (and one Phi).
1956  *
1957  * @param phi    the Phi node
1958  * @param other  the other operand
1959  * @param eval   an evaluator function
1960  * @param mode   the mode of the result, may be different from the mode of the Phi!
1961  * @param left   if non-zero, other is the left operand, else the right
1962  *
1963  * @return a new Phi node if the conversion was successful, NULL else
1964  */
1965 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1966         tarval   *tv;
1967         void     **res;
1968         ir_node  *pred;
1969         ir_graph *irg;
1970         int      i, n = get_irn_arity(phi);
1971
1972         NEW_ARR_A(void *, res, n);
1973         if (left) {
1974                 for (i = 0; i < n; ++i) {
1975                         pred = get_irn_n(phi, i);
1976                         tv   = get_Const_tarval(pred);
1977                         tv   = do_eval(eval, other, tv, mode);
1978
1979                         if (tv == tarval_bad) {
1980                                 /* folding failed, bad */
1981                                 return NULL;
1982                         }
1983                         res[i] = tv;
1984                 }
1985         } else {
1986                 for (i = 0; i < n; ++i) {
1987                         pred = get_irn_n(phi, i);
1988                         tv   = get_Const_tarval(pred);
1989                         tv   = do_eval(eval, tv, other, mode);
1990
1991                         if (tv == tarval_bad) {
1992                                 /* folding failed, bad */
1993                                 return 0;
1994                         }
1995                         res[i] = tv;
1996                 }
1997         }
1998         irg  = current_ir_graph;
1999         for (i = 0; i < n; ++i) {
2000                 pred = get_irn_n(phi, i);
2001                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2002         }
2003         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2004 }  /* apply_binop_on_phi */
2005
2006 /**
2007  * Apply an evaluator on a binop with two constant Phi.
2008  *
2009  * @param a      the left Phi node
2010  * @param b      the right Phi node
2011  * @param eval   an evaluator function
2012  * @param mode   the mode of the result, may be different from the mode of the Phi!
2013  *
2014  * @return a new Phi node if the conversion was successful, NULL else
2015  */
2016 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
2017         tarval   *tv_l, *tv_r, *tv;
2018         void     **res;
2019         ir_node  *pred;
2020         ir_graph *irg;
2021         int      i, n;
2022
2023         if (get_nodes_block(a) != get_nodes_block(b))
2024                 return NULL;
2025
2026         n = get_irn_arity(a);
2027         NEW_ARR_A(void *, res, n);
2028
2029         for (i = 0; i < n; ++i) {
2030                 pred = get_irn_n(a, i);
2031                 tv_l = get_Const_tarval(pred);
2032                 pred = get_irn_n(b, i);
2033                 tv_r = get_Const_tarval(pred);
2034                 tv   = do_eval(eval, tv_l, tv_r, mode);
2035
2036                 if (tv == tarval_bad) {
2037                         /* folding failed, bad */
2038                         return NULL;
2039                 }
2040                 res[i] = tv;
2041         }
2042         irg  = current_ir_graph;
2043         for (i = 0; i < n; ++i) {
2044                 pred = get_irn_n(a, i);
2045                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2046         }
2047         return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
2048 }  /* apply_binop_on_2_phis */
2049
2050 /**
2051  * Apply an evaluator on a unop with a constant operator (a Phi).
2052  *
2053  * @param phi    the Phi node
2054  * @param eval   an evaluator function
2055  *
2056  * @return a new Phi node if the conversion was successful, NULL else
2057  */
2058 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2059         tarval   *tv;
2060         void     **res;
2061         ir_node  *pred;
2062         ir_mode  *mode;
2063         ir_graph *irg;
2064         int      i, n = get_irn_arity(phi);
2065
2066         NEW_ARR_A(void *, res, n);
2067         for (i = 0; i < n; ++i) {
2068                 pred = get_irn_n(phi, i);
2069                 tv   = get_Const_tarval(pred);
2070                 tv   = eval(tv);
2071
2072                 if (tv == tarval_bad) {
2073                         /* folding failed, bad */
2074                         return 0;
2075                 }
2076                 res[i] = tv;
2077         }
2078         mode = get_irn_mode(phi);
2079         irg  = current_ir_graph;
2080         for (i = 0; i < n; ++i) {
2081                 pred = get_irn_n(phi, i);
2082                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2083         }
2084         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2085 }  /* apply_unop_on_phi */
2086
2087 /**
2088  * Apply a conversion on a constant operator (a Phi).
2089  *
2090  * @param phi    the Phi node
2091  *
2092  * @return a new Phi node if the conversion was successful, NULL else
2093  */
2094 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2095         tarval   *tv;
2096         void     **res;
2097         ir_node  *pred;
2098         ir_graph *irg;
2099         int      i, n = get_irn_arity(phi);
2100
2101         NEW_ARR_A(void *, res, n);
2102         for (i = 0; i < n; ++i) {
2103                 pred = get_irn_n(phi, i);
2104                 tv   = get_Const_tarval(pred);
2105                 tv   = tarval_convert_to(tv, mode);
2106
2107                 if (tv == tarval_bad) {
2108                         /* folding failed, bad */
2109                         return 0;
2110                 }
2111                 res[i] = tv;
2112         }
2113         irg  = current_ir_graph;
2114         for (i = 0; i < n; ++i) {
2115                 pred = get_irn_n(phi, i);
2116                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2117         }
2118         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2119 }  /* apply_conv_on_phi */
2120
2121 /**
2122  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2123  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2124  * If possible, remove the Conv's.
2125  */
2126 static ir_node *transform_node_AddSub(ir_node *n) {
2127         ir_mode *mode = get_irn_mode(n);
2128
2129         if (mode_is_reference(mode)) {
2130                 ir_node *left     = get_binop_left(n);
2131                 ir_node *right    = get_binop_right(n);
2132                 unsigned ref_bits = get_mode_size_bits(mode);
2133
2134                 if (is_Conv(left)) {
2135                         ir_mode *lmode = get_irn_mode(left);
2136                         unsigned bits = get_mode_size_bits(lmode);
2137
2138                         if (ref_bits == bits &&
2139                             mode_is_int(lmode) &&
2140                             get_mode_arithmetic(lmode) == irma_twos_complement) {
2141                                 ir_node *pre      = get_Conv_op(left);
2142                                 ir_mode *pre_mode = get_irn_mode(pre);
2143
2144                                 if (mode_is_int(pre_mode) &&
2145                                     get_mode_size_bits(pre_mode) == bits &&
2146                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2147                                         /* ok, this conv just changes to sign, moreover the calculation
2148                                          * is done with same number of bits as our address mode, so
2149                                          * we can ignore the conv as address calculation can be viewed
2150                                          * as either signed or unsigned
2151                                          */
2152                                         set_binop_left(n, pre);
2153                                 }
2154                         }
2155                 }
2156
2157                 if (is_Conv(right)) {
2158                         ir_mode *rmode = get_irn_mode(right);
2159                         unsigned bits = get_mode_size_bits(rmode);
2160
2161                         if (ref_bits == bits &&
2162                             mode_is_int(rmode) &&
2163                             get_mode_arithmetic(rmode) == irma_twos_complement) {
2164                                 ir_node *pre      = get_Conv_op(right);
2165                                 ir_mode *pre_mode = get_irn_mode(pre);
2166
2167                                 if (mode_is_int(pre_mode) &&
2168                                     get_mode_size_bits(pre_mode) == bits &&
2169                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2170                                         /* ok, this conv just changes to sign, moreover the calculation
2171                                          * is done with same number of bits as our address mode, so
2172                                          * we can ignore the conv as address calculation can be viewed
2173                                          * as either signed or unsigned
2174                                          */
2175                                         set_binop_right(n, pre);
2176                                 }
2177                         }
2178                 }
2179
2180                 /* let address arithmetic use unsigned modes */
2181                 if (is_Const(right)) {
2182                         ir_mode *rmode = get_irn_mode(right);
2183
2184                         if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2185                                 /* convert a AddP(P, *s) into AddP(P, *u) */
2186                                 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2187
2188                                 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2189                                 set_binop_right(n, pre);
2190                         }
2191                 }
2192         }
2193
2194         return n;
2195 }  /* transform_node_AddSub */
2196
2197 #define HANDLE_BINOP_PHI(eval, a, b, c, mode)                     \
2198   c = NULL;                                                       \
2199   if (is_Const(b) && is_const_Phi(a)) {                           \
2200     /* check for Op(Phi, Const) */                                \
2201     c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2202   }                                                               \
2203   else if (is_Const(a) && is_const_Phi(b)) {                      \
2204     /* check for Op(Const, Phi) */                                \
2205     c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2206   }                                                               \
2207   else if (is_const_Phi(a) && is_const_Phi(b)) {                  \
2208     /* check for Op(Phi, Phi) */                                  \
2209     c = apply_binop_on_2_phis(a, b, eval, mode);                  \
2210   }                                                               \
2211   if (c) {                                                        \
2212     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);                   \
2213     return c;                                                     \
2214   }
2215
2216 #define HANDLE_UNOP_PHI(eval, a, c)               \
2217   c = NULL;                                       \
2218   if (is_const_Phi(a)) {                          \
2219     /* check for Op(Phi) */                       \
2220     c = apply_unop_on_phi(a, eval);               \
2221     if (c) {                                      \
2222       DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2223       return c;                                   \
2224     }                                             \
2225   }
2226
2227 /**
2228  * Do the AddSub optimization, then Transform
2229  *   Constant folding on Phi
2230  *   Add(a,a)          -> Mul(a, 2)
2231  *   Add(Mul(a, x), a) -> Mul(a, x+1)
2232  * if the mode is integer or float.
2233  * Transform Add(a,-b) into Sub(a,b).
2234  * Reassociation might fold this further.
2235  */
2236 static ir_node *transform_node_Add(ir_node *n) {
2237         ir_mode *mode;
2238         ir_node *a, *b, *c, *oldn = n;
2239
2240         n = transform_node_AddSub(n);
2241
2242         a = get_Add_left(n);
2243         b = get_Add_right(n);
2244
2245         mode = get_irn_mode(n);
2246
2247         if (mode_is_reference(mode)) {
2248                 ir_mode *lmode = get_irn_mode(a);
2249
2250                 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2251                         /* an Add(a, NULL) is a hidden Conv */
2252                         dbg_info *dbg = get_irn_dbg_info(n);
2253                         return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2254                 }
2255         }
2256
2257         HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2258
2259         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2260         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2261                 return n;
2262
2263         if (mode_is_num(mode)) {
2264                 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2265                 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2266                         ir_node *block = get_nodes_block(n);
2267
2268                         n = new_rd_Mul(
2269                                 get_irn_dbg_info(n),
2270                                 current_ir_graph,
2271                                 block,
2272                                 a,
2273                                 new_Const_long(mode, 2),
2274                                 mode);
2275                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2276                         return n;
2277                 }
2278                 if (is_Minus(a)) {
2279                         n = new_rd_Sub(
2280                                         get_irn_dbg_info(n),
2281                                         current_ir_graph,
2282                                         get_nodes_block(n),
2283                                         b,
2284                                         get_Minus_op(a),
2285                                         mode);
2286                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2287                         return n;
2288                 }
2289                 if (is_Minus(b)) {
2290                         n = new_rd_Sub(
2291                                         get_irn_dbg_info(n),
2292                                         current_ir_graph,
2293                                         get_nodes_block(n),
2294                                         a,
2295                                         get_Minus_op(b),
2296                                         mode);
2297                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2298                         return n;
2299                 }
2300                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2301                         /* Here we rely on constants be on the RIGHT side */
2302                         if (is_Not(a)) {
2303                                 ir_node *op = get_Not_op(a);
2304
2305                                 if (is_Const(b) && is_Const_one(b)) {
2306                                         /* ~x + 1 = -x */
2307                                         ir_node *blk = get_nodes_block(n);
2308                                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2309                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2310                                         return n;
2311                                 }
2312                                 if (op == b) {
2313                                         /* ~x + x = -1 */
2314                                         n = new_Const(get_mode_minus_one(mode));
2315                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2316                                         return n;
2317                                 }
2318                         }
2319                         if (is_Not(b)) {
2320                                 ir_node *op = get_Not_op(b);
2321
2322                                 if (op == a) {
2323                                         /* x + ~x = -1 */
2324                                         n = new_Const(get_mode_minus_one(mode));
2325                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2326                                         return n;
2327                                 }
2328                         }
2329                 }
2330         }
2331         return n;
2332 }  /* transform_node_Add */
2333
2334 /**
2335  * returns -cnst or NULL if impossible
2336  */
2337 static ir_node *const_negate(ir_node *cnst) {
2338         tarval   *tv    = tarval_neg(get_Const_tarval(cnst));
2339         dbg_info *dbgi  = get_irn_dbg_info(cnst);
2340         ir_graph *irg   = get_irn_irg(cnst);
2341         if (tv == tarval_bad) return NULL;
2342         return new_rd_Const(dbgi, irg, tv);
2343 }
2344
2345 /**
2346  * Do the AddSub optimization, then Transform
2347  *   Constant folding on Phi
2348  *   Sub(0,a)          -> Minus(a)
2349  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2350  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2351  *   Sub(Add(a, x), x) -> a
2352  *   Sub(x, Add(x, a)) -> -a
2353  *   Sub(x, Const)     -> Add(x, -Const)
2354  */
2355 static ir_node *transform_node_Sub(ir_node *n) {
2356         ir_mode *mode;
2357         ir_node *oldn = n;
2358         ir_node *a, *b, *c;
2359
2360         n = transform_node_AddSub(n);
2361
2362         a = get_Sub_left(n);
2363         b = get_Sub_right(n);
2364
2365         mode = get_irn_mode(n);
2366
2367         if (mode_is_int(mode)) {
2368                 ir_mode *lmode = get_irn_mode(a);
2369
2370                 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2371                         /* a Sub(a, NULL) is a hidden Conv */
2372                         dbg_info *dbg = get_irn_dbg_info(n);
2373                         n = new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2374                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2375                         return n;
2376                 }
2377
2378                 if (mode == lmode                                     &&
2379                     get_mode_arithmetic(mode) == irma_twos_complement &&
2380                     is_Const(a)                                       &&
2381                     get_Const_tarval(a) == get_mode_minus_one(mode)) {
2382                         /* -1 - x -> ~x */
2383                         dbg_info *dbg = get_irn_dbg_info(n);
2384                         n = new_rd_Not(dbg, current_ir_graph, get_nodes_block(n), b, mode);
2385                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2386                         return n;
2387                 }
2388         }
2389
2390 restart:
2391         HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2392
2393         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2394         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2395                 return n;
2396
2397         if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2398                 /* a - C -> a + (-C) */
2399                 ir_node *cnst = const_negate(b);
2400                 if (cnst != NULL) {
2401                         ir_node  *block = get_nodes_block(n);
2402                         dbg_info *dbgi  = get_irn_dbg_info(n);
2403                         ir_graph *irg   = get_irn_irg(n);
2404
2405                         n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2406                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2407                         return n;
2408                 }
2409         }
2410
2411         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2412                 ir_graph *irg   = current_ir_graph;
2413                 dbg_info *dbg   = get_irn_dbg_info(n);
2414                 ir_node  *block = get_nodes_block(n);
2415                 ir_node  *left  = get_Minus_op(a);
2416                 ir_node  *add   = new_rd_Add(dbg, irg, block, left, b, mode);
2417
2418                 n = new_rd_Minus(dbg, irg, block, add, mode);
2419                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2420                 return n;
2421         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2422                 ir_graph *irg   = current_ir_graph;
2423                 dbg_info *dbg   = get_irn_dbg_info(n);
2424                 ir_node  *block = get_nodes_block(n);
2425                 ir_node  *right = get_Minus_op(b);
2426
2427                 n = new_rd_Add(dbg, irg, block, a, right, mode);
2428                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2429                 return n;
2430         } else if (is_Sub(b)) {
2431                 /* a - (b - c) -> a + (c - b)
2432                  *             -> (a - b) + c iff (b - c) is a pointer */
2433                 ir_graph *irg     = current_ir_graph;
2434                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2435                 ir_node  *s_block = get_nodes_block(b);
2436                 ir_node  *s_left  = get_Sub_left(b);
2437                 ir_node  *s_right = get_Sub_right(b);
2438                 ir_mode  *s_mode  = get_irn_mode(b);
2439                 if (mode_is_reference(s_mode)) {
2440                         ir_node  *sub     = new_rd_Sub(s_dbg, irg, s_block, a, s_left, mode);
2441                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2442                         ir_node  *a_block = get_nodes_block(n);
2443
2444                         if (s_mode != mode)
2445                                 s_right = new_r_Conv(irg, a_block, s_right, mode);
2446                         n = new_rd_Add(a_dbg, irg, a_block, sub, s_right, mode);
2447                 } else {
2448                         ir_node  *sub     = new_rd_Sub(s_dbg, irg, s_block, s_right, s_left, s_mode);
2449                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2450                         ir_node  *a_block = get_nodes_block(n);
2451
2452                         n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2453                 }
2454                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2455                 return n;
2456         } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2457                 ir_node *m_right = get_Mul_right(b);
2458                 if (is_Const(m_right)) {
2459                         ir_node *cnst2 = const_negate(m_right);
2460                         if (cnst2 != NULL) {
2461                                 ir_graph *irg     = current_ir_graph;
2462                                 dbg_info *m_dbg   = get_irn_dbg_info(b);
2463                                 ir_node  *m_block = get_nodes_block(b);
2464                                 ir_node  *m_left  = get_Mul_left(b);
2465                                 ir_mode  *m_mode  = get_irn_mode(b);
2466                                 ir_node  *mul     = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2467                                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2468                                 ir_node  *a_block = get_nodes_block(n);
2469
2470                                 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2471                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2472                                 return n;
2473                         }
2474                 }
2475         }
2476
2477         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2478         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2479                 n = new_rd_Minus(
2480                                 get_irn_dbg_info(n),
2481                                 current_ir_graph,
2482                                 get_nodes_block(n),
2483                                 b,
2484                                 mode);
2485                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2486                 return n;
2487         }
2488         if (is_Add(a)) {
2489                 if (mode_wrap_around(mode)) {
2490                         ir_node *left  = get_Add_left(a);
2491                         ir_node *right = get_Add_right(a);
2492
2493                         /* FIXME: Does the Conv's work only for two complement or generally? */
2494                         if (left == b) {
2495                                 if (mode != get_irn_mode(right)) {
2496                                         /* This Sub is an effective Cast */
2497                                         right = new_r_Conv(get_irn_irg(n), get_nodes_block(n), right, mode);
2498                                 }
2499                                 n = right;
2500                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2501                                 return n;
2502                         } else if (right == b) {
2503                                 if (mode != get_irn_mode(left)) {
2504                                         /* This Sub is an effective Cast */
2505                                         left = new_r_Conv(get_irn_irg(n), get_nodes_block(n), left, mode);
2506                                 }
2507                                 n = left;
2508                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2509                                 return n;
2510                         }
2511                 }
2512         }
2513         if (is_Add(b)) {
2514                 if (mode_wrap_around(mode)) {
2515                         ir_node *left  = get_Add_left(b);
2516                         ir_node *right = get_Add_right(b);
2517
2518                         /* FIXME: Does the Conv's work only for two complement or generally? */
2519                         if (left == a) {
2520                                 ir_mode *r_mode = get_irn_mode(right);
2521
2522                                 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), right, r_mode);
2523                                 if (mode != r_mode) {
2524                                         /* This Sub is an effective Cast */
2525                                         n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2526                                 }
2527                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2528                                 return n;
2529                         } else if (right == a) {
2530                                 ir_mode *l_mode = get_irn_mode(left);
2531
2532                                 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), left, l_mode);
2533                                 if (mode != l_mode) {
2534                                         /* This Sub is an effective Cast */
2535                                         n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2536                                 }
2537                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2538                                 return n;
2539                         }
2540                 }
2541         }
2542         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2543                 ir_mode *mode = get_irn_mode(a);
2544
2545                 if (mode == get_irn_mode(b)) {
2546                         ir_mode *ma, *mb;
2547                         ir_node *op_a = get_Conv_op(a);
2548                         ir_node *op_b = get_Conv_op(b);
2549
2550                         /* check if it's allowed to skip the conv */
2551                         ma = get_irn_mode(op_a);
2552                         mb = get_irn_mode(op_b);
2553
2554                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2555                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2556                                 a = op_a; b = op_b;
2557                                 set_Sub_left(n, a);
2558                                 set_Sub_right(n, b);
2559
2560                                 goto restart;
2561                         }
2562                 }
2563         }
2564         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2565         if (!is_reassoc_running() && is_Mul(a)) {
2566                 ir_node *ma = get_Mul_left(a);
2567                 ir_node *mb = get_Mul_right(a);
2568
2569                 if (ma == b) {
2570                         ir_node *blk = get_nodes_block(n);
2571                         n = new_rd_Mul(
2572                                         get_irn_dbg_info(n),
2573                                         current_ir_graph, blk,
2574                                         ma,
2575                                         new_rd_Sub(
2576                                                 get_irn_dbg_info(n),
2577                                                 current_ir_graph, blk,
2578                                                 mb,
2579                                                 new_Const_long(mode, 1),
2580                                                 mode),
2581                                         mode);
2582                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2583                         return n;
2584                 } else if (mb == b) {
2585                         ir_node *blk = get_nodes_block(n);
2586                         n = new_rd_Mul(
2587                                         get_irn_dbg_info(n),
2588                                         current_ir_graph, blk,
2589                                         mb,
2590                                         new_rd_Sub(
2591                                                 get_irn_dbg_info(n),
2592                                                 current_ir_graph, blk,
2593                                                 ma,
2594                                                 new_Const_long(mode, 1),
2595                                                 mode),
2596                                         mode);
2597                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2598                         return n;
2599                 }
2600         }
2601         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2602                 ir_node *x   =      get_Sub_left(a);
2603                 ir_node *y        = get_Sub_right(a);
2604                 ir_node *blk      = get_nodes_block(n);
2605                 ir_mode *m_b      = get_irn_mode(b);
2606                 ir_mode *m_y      = get_irn_mode(y);
2607                 ir_mode *add_mode;
2608                 ir_node *add;
2609
2610                 /* Determine the right mode for the Add. */
2611                 if (m_b == m_y)
2612                         add_mode = m_b;
2613                 else if (mode_is_reference(m_b))
2614                         add_mode = m_b;
2615                 else if (mode_is_reference(m_y))
2616                         add_mode = m_y;
2617                 else {
2618                         /*
2619                          * Both modes are different but none is reference,
2620                          * happens for instance in SubP(SubP(P, Iu), Is).
2621                          * We have two possibilities here: Cast or ignore.
2622                          * Currently we ignore this case.
2623                          */
2624                         return n;
2625                 }
2626
2627                 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2628
2629                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2630                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2631                 return n;
2632         }
2633
2634         if (get_mode_arithmetic(mode) == irma_twos_complement) {
2635                 if (is_Const(a) && is_Not(b)) {
2636                         /* c - ~X = X + (c+1) */
2637                         tarval *tv = get_Const_tarval(a);
2638
2639                         tv = tarval_add(tv, get_mode_one(mode));
2640                         if (tv != tarval_bad) {
2641                                 ir_node *blk = get_nodes_block(n);
2642                                 ir_node *c = new_Const(tv);
2643                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2644                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2645                                 return n;
2646                         }
2647                 }
2648         }
2649         return n;
2650 }  /* transform_node_Sub */
2651
2652 /**
2653  * Several transformation done on n*n=2n bits mul.
2654  * These transformations must be done here because new nodes may be produced.
2655  */
2656 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2657         ir_node *oldn = n;
2658         ir_node *a = get_Mul_left(n);
2659         ir_node *b = get_Mul_right(n);
2660         tarval *ta = value_of(a);
2661         tarval *tb = value_of(b);
2662         ir_mode *smode = get_irn_mode(a);
2663
2664         if (ta == get_mode_one(smode)) {
2665                 /* (L)1 * (L)b = (L)b */
2666                 ir_node *blk = get_nodes_block(n);
2667                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2668                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2669                 return n;
2670         }
2671         else if (ta == get_mode_minus_one(smode)) {
2672                 /* (L)-1 * (L)b = (L)b */
2673                 ir_node *blk = get_nodes_block(n);
2674                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2675                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2676                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2677                 return n;
2678         }
2679         if (tb == get_mode_one(smode)) {
2680                 /* (L)a * (L)1 = (L)a */
2681                 ir_node *blk = get_irn_n(a, -1);
2682                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2683                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2684                 return n;
2685         }
2686         else if (tb == get_mode_minus_one(smode)) {
2687                 /* (L)a * (L)-1 = (L)-a */
2688                 ir_node *blk = get_nodes_block(n);
2689                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2690                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2691                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2692                 return n;
2693         }
2694         return n;
2695 }
2696
2697 /**
2698  * Transform Mul(a,-1) into -a.
2699  * Do constant evaluation of Phi nodes.
2700  * Do architecture dependent optimizations on Mul nodes
2701  */
2702 static ir_node *transform_node_Mul(ir_node *n) {
2703         ir_node *c, *oldn = n;
2704         ir_mode *mode = get_irn_mode(n);
2705         ir_node *a = get_Mul_left(n);
2706         ir_node *b = get_Mul_right(n);
2707
2708         if (is_Bad(a) || is_Bad(b))
2709                 return n;
2710
2711         if (mode != get_irn_mode(a))
2712                 return transform_node_Mul2n(n, mode);
2713
2714         HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2715
2716         if (mode_is_signed(mode)) {
2717                 ir_node *r = NULL;
2718
2719                 if (value_of(a) == get_mode_minus_one(mode))
2720                         r = b;
2721                 else if (value_of(b) == get_mode_minus_one(mode))
2722                         r = a;
2723                 if (r) {
2724                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
2725                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2726                         return n;
2727                 }
2728         }
2729         if (is_Minus(a)) {
2730                 if (is_Const(b)) { /* (-a) * const -> a * -const */
2731                         ir_node *cnst = const_negate(b);
2732                         if (cnst != NULL) {
2733                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2734                                 ir_node  *block = get_nodes_block(n);
2735                                 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2736                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2737                                 return n;
2738                         }
2739                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2740                         dbg_info *dbgi  = get_irn_dbg_info(n);
2741                         ir_node  *block = get_nodes_block(n);
2742                         n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2743                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2744                         return n;
2745                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2746                         ir_node  *sub_l = get_Sub_left(b);
2747                         ir_node  *sub_r = get_Sub_right(b);
2748                         dbg_info *dbgi  = get_irn_dbg_info(n);
2749                         ir_graph *irg   = current_ir_graph;
2750                         ir_node  *block = get_nodes_block(n);
2751                         ir_node  *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2752                         n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2753                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2754                         return n;
2755                 }
2756         } else if (is_Minus(b)) {
2757                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2758                         ir_node  *sub_l = get_Sub_left(a);
2759                         ir_node  *sub_r = get_Sub_right(a);
2760                         dbg_info *dbgi  = get_irn_dbg_info(n);
2761                         ir_graph *irg   = current_ir_graph;
2762                         ir_node  *block = get_nodes_block(n);
2763                         ir_node  *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2764                         n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2765                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2766                         return n;
2767                 }
2768         }
2769         if (get_mode_arithmetic(mode) == irma_ieee754) {
2770                 if (is_Const(a)) {
2771                         tarval *tv = get_Const_tarval(a);
2772                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2773                                         && !tarval_is_negative(tv)) {
2774                                 /* 2.0 * b = b + b */
2775                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2776                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2777                                 return n;
2778                         }
2779                 }
2780                 else if (is_Const(b)) {
2781                         tarval *tv = get_Const_tarval(b);
2782                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2783                                         && !tarval_is_negative(tv)) {
2784                                 /* a * 2.0 = a + a */
2785                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2786                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2787                                 return n;
2788                         }
2789                 }
2790         }
2791         return arch_dep_replace_mul_with_shifts(n);
2792 }  /* transform_node_Mul */
2793
2794 /**
2795  * Transform a Div Node.
2796  */
2797 static ir_node *transform_node_Div(ir_node *n) {
2798         ir_mode *mode = get_Div_resmode(n);
2799         ir_node *a = get_Div_left(n);
2800         ir_node *b = get_Div_right(n);
2801         ir_node *value;
2802         tarval  *tv;
2803
2804         if (is_Const(b) && is_const_Phi(a)) {
2805                 /* check for Div(Phi, Const) */
2806                 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2807                 if (value) {
2808                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2809                         goto make_tuple;
2810                 }
2811         }
2812         else if (is_Const(a) && is_const_Phi(b)) {
2813                 /* check for Div(Const, Phi) */
2814                 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2815                 if (value) {
2816                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2817                         goto make_tuple;
2818                 }
2819         }
2820         else if (is_const_Phi(a) && is_const_Phi(b)) {
2821                 /* check for Div(Phi, Phi) */
2822                 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2823                 if (value) {
2824                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2825                         goto make_tuple;
2826                 }
2827         }
2828
2829         value = n;
2830         tv = value_of(n);
2831         if (tv != tarval_bad) {
2832                 value = new_Const(tv);
2833
2834                 DBG_OPT_CSTEVAL(n, value);
2835                 goto make_tuple;
2836         } else {
2837                 ir_node       *a = get_Div_left(n);
2838                 ir_node       *b = get_Div_right(n);
2839                 const ir_node *dummy;
2840
2841                 if (a == b && value_not_zero(a, &dummy)) {
2842                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2843                         value = new_Const(get_mode_one(mode));
2844                         DBG_OPT_CSTEVAL(n, value);
2845                         goto make_tuple;
2846                 } else {
2847                         if (mode_is_signed(mode) && is_Const(b)) {
2848                                 tarval *tv = get_Const_tarval(b);
2849
2850                                 if (tv == get_mode_minus_one(mode)) {
2851                                         /* a / -1 */
2852                                         value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2853                                         DBG_OPT_CSTEVAL(n, value);
2854                                         goto make_tuple;
2855                                 }
2856                         }
2857                         /* Try architecture dependent optimization */
2858                         value = arch_dep_replace_div_by_const(n);
2859                 }
2860         }
2861
2862         if (value != n) {
2863                 ir_node *mem, *blk;
2864
2865 make_tuple:
2866                 /* Turn Div into a tuple (mem, jmp, bad, value) */
2867                 mem = get_Div_mem(n);
2868                 blk = get_nodes_block(n);
2869
2870                 /* skip a potential Pin */
2871                 mem = skip_Pin(mem);
2872                 turn_into_tuple(n, pn_Div_max);
2873                 set_Tuple_pred(n, pn_Div_M,         mem);
2874                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2875                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());
2876                 set_Tuple_pred(n, pn_Div_res,       value);
2877         }
2878         return n;
2879 }  /* transform_node_Div */
2880
2881 /**
2882  * Transform a Mod node.
2883  */
2884 static ir_node *transform_node_Mod(ir_node *n) {
2885         ir_mode *mode = get_Mod_resmode(n);
2886         ir_node *a = get_Mod_left(n);
2887         ir_node *b = get_Mod_right(n);
2888         ir_node *value;
2889         tarval  *tv;
2890
2891         if (is_Const(b) && is_const_Phi(a)) {
2892                 /* check for Div(Phi, Const) */
2893                 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2894                 if (value) {
2895                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2896                         goto make_tuple;
2897                 }
2898         }
2899         else if (is_Const(a) && is_const_Phi(b)) {
2900                 /* check for Div(Const, Phi) */
2901                 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2902                 if (value) {
2903                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2904                         goto make_tuple;
2905                 }
2906         }
2907         else if (is_const_Phi(a) && is_const_Phi(b)) {
2908                 /* check for Div(Phi, Phi) */
2909                 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2910                 if (value) {
2911                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2912                         goto make_tuple;
2913                 }
2914         }
2915
2916         value = n;
2917         tv = value_of(n);
2918         if (tv != tarval_bad) {
2919                 value = new_Const(tv);
2920
2921                 DBG_OPT_CSTEVAL(n, value);
2922                 goto make_tuple;
2923         } else {
2924                 ir_node       *a = get_Mod_left(n);
2925                 ir_node       *b = get_Mod_right(n);
2926                 const ir_node *dummy;
2927
2928                 if (a == b && value_not_zero(a, &dummy)) {
2929                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2930                         value = new_Const(get_mode_null(mode));
2931                         DBG_OPT_CSTEVAL(n, value);
2932                         goto make_tuple;
2933                 } else {
2934                         if (mode_is_signed(mode) && is_Const(b)) {
2935                                 tarval *tv = get_Const_tarval(b);
2936
2937                                 if (tv == get_mode_minus_one(mode)) {
2938                                         /* a % -1 = 0 */
2939                                         value = new_Const(get_mode_null(mode));
2940                                         DBG_OPT_CSTEVAL(n, value);
2941                                         goto make_tuple;
2942                                 }
2943                         }
2944                         /* Try architecture dependent optimization */
2945                         value = arch_dep_replace_mod_by_const(n);
2946                 }
2947         }
2948
2949         if (value != n) {
2950                 ir_node *mem, *blk;
2951
2952 make_tuple:
2953                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2954                 mem = get_Mod_mem(n);
2955                 blk = get_nodes_block(n);
2956
2957                 /* skip a potential Pin */
2958                 mem = skip_Pin(mem);
2959                 turn_into_tuple(n, pn_Mod_max);
2960                 set_Tuple_pred(n, pn_Mod_M,         mem);
2961                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2962                 set_Tuple_pred(n, pn_Mod_X_except,  new_Bad());
2963                 set_Tuple_pred(n, pn_Mod_res,       value);
2964         }
2965         return n;
2966 }  /* transform_node_Mod */
2967
2968 /**
2969  * Transform a DivMod node.
2970  */
2971 static ir_node *transform_node_DivMod(ir_node *n) {
2972         const ir_node *dummy;
2973         ir_node       *a = get_DivMod_left(n);
2974         ir_node       *b = get_DivMod_right(n);
2975         ir_mode       *mode = get_DivMod_resmode(n);
2976         ir_node       *va, *vb;
2977         tarval        *ta, *tb;
2978         int           evaluated = 0;
2979
2980         if (is_Const(b) && is_const_Phi(a)) {
2981                 /* check for Div(Phi, Const) */
2982                 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2983                 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2984                 if (va && vb) {
2985                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2986                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2987                         goto make_tuple;
2988                 }
2989         }
2990         else if (is_Const(a) && is_const_Phi(b)) {
2991                 /* check for Div(Const, Phi) */
2992                 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2993                 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2994                 if (va && vb) {
2995                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2996                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2997                         goto make_tuple;
2998                 }
2999         }
3000         else if (is_const_Phi(a) && is_const_Phi(b)) {
3001                 /* check for Div(Phi, Phi) */
3002                 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
3003                 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
3004                 if (va && vb) {
3005                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3006                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3007                         goto make_tuple;
3008                 }
3009         }
3010
3011         ta = value_of(a);
3012         tb = value_of(b);
3013         if (tb != tarval_bad) {
3014                 if (tb == get_mode_one(get_tarval_mode(tb))) {
3015                         va = a;
3016                         vb = new_Const(get_mode_null(mode));
3017                         DBG_OPT_CSTEVAL(n, vb);
3018                         goto make_tuple;
3019                 } else if (ta != tarval_bad) {
3020                         tarval *resa, *resb;
3021                         resa = tarval_div(ta, tb);
3022                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3023                                                              Jmp for X result!? */
3024                         resb = tarval_mod(ta, tb);
3025                         if (resb == tarval_bad) return n; /* Causes exception! */
3026                         va = new_Const(resa);
3027                         vb = new_Const(resb);
3028                         DBG_OPT_CSTEVAL(n, va);
3029                         DBG_OPT_CSTEVAL(n, vb);
3030                         goto make_tuple;
3031                 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3032                         va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
3033                         vb = new_Const(get_mode_null(mode));
3034                         DBG_OPT_CSTEVAL(n, va);
3035                         DBG_OPT_CSTEVAL(n, vb);
3036                         goto make_tuple;
3037                 } else { /* Try architecture dependent optimization */
3038                         va = a;
3039                         vb = b;
3040                         arch_dep_replace_divmod_by_const(&va, &vb, n);
3041                         evaluated = va != NULL;
3042                 }
3043         } else if (a == b) {
3044                 if (value_not_zero(a, &dummy)) {
3045                         /* a/a && a != 0 */
3046                         va = new_Const(get_mode_one(mode));
3047                         vb = new_Const(get_mode_null(mode));
3048                         DBG_OPT_CSTEVAL(n, va);
3049                         DBG_OPT_CSTEVAL(n, vb);
3050                         goto make_tuple;
3051                 } else {
3052                         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3053                         return n;
3054                 }
3055         } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3056                 /* 0 / non-Const = 0 */
3057                 vb = va = a;
3058                 goto make_tuple;
3059         }
3060
3061         if (evaluated) { /* replace by tuple */
3062                 ir_node *mem, *blk;
3063
3064 make_tuple:
3065                 mem = get_DivMod_mem(n);
3066                 /* skip a potential Pin */
3067                 mem = skip_Pin(mem);
3068
3069                 blk = get_nodes_block(n);
3070                 turn_into_tuple(n, pn_DivMod_max);
3071                 set_Tuple_pred(n, pn_DivMod_M,         mem);
3072                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
3073                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());  /* no exception */
3074                 set_Tuple_pred(n, pn_DivMod_res_div,   va);
3075                 set_Tuple_pred(n, pn_DivMod_res_mod,   vb);
3076         }
3077
3078         return n;
3079 }  /* transform_node_DivMod */
3080
3081 /**
3082  * Optimize x / c to x * (1/c)
3083  */
3084 static ir_node *transform_node_Quot(ir_node *n) {
3085         ir_mode *mode = get_Quot_resmode(n);
3086         ir_node *oldn = n;
3087
3088         if (get_mode_arithmetic(mode) == irma_ieee754) {
3089                 ir_node *b = get_Quot_right(n);
3090                 tarval *tv = value_of(b);
3091
3092                 if (tv != tarval_bad) {
3093                         int rem = tarval_fp_ops_enabled();
3094
3095                         /*
3096                          * Floating point constant folding might be disabled here to
3097                          * prevent rounding.
3098                          * However, as we check for exact result, doing it is safe.
3099                          * Switch it on.
3100                          */
3101                         tarval_enable_fp_ops(1);
3102                         tv = tarval_quo(get_mode_one(mode), tv);
3103                         tarval_enable_fp_ops(rem);
3104
3105                         /* Do the transformation if the result is either exact or we are not
3106                            using strict rules. */
3107                         if (tv != tarval_bad &&
3108                             (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3109                                 ir_node *blk = get_nodes_block(n);
3110                                 ir_node *c = new_Const(tv);
3111                                 ir_node *a = get_Quot_left(n);
3112                                 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3113                                 ir_node *mem = get_Quot_mem(n);
3114
3115                                 /* skip a potential Pin */
3116                                 mem = skip_Pin(mem);
3117                                 turn_into_tuple(n, pn_Quot_max);
3118                                 set_Tuple_pred(n, pn_Quot_M, mem);
3119                                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3120                                 set_Tuple_pred(n, pn_Quot_X_except,  new_r_Bad(current_ir_graph));
3121                                 set_Tuple_pred(n, pn_Quot_res, m);
3122                                 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3123                         }
3124                 }
3125         }
3126         return n;
3127 }  /* transform_node_Quot */
3128
3129 /**
3130  * Optimize Abs(x) into  x if x is Confirmed >= 0
3131  * Optimize Abs(x) into -x if x is Confirmed <= 0
3132  * Optimize Abs(-x) int Abs(x)
3133  */
3134 static ir_node *transform_node_Abs(ir_node *n) {
3135         ir_node *c, *oldn = n;
3136         ir_node *a = get_Abs_op(n);
3137         ir_mode *mode;
3138
3139         HANDLE_UNOP_PHI(tarval_abs, a, c);
3140
3141         switch (classify_value_sign(a)) {
3142         case value_classified_negative:
3143                 mode = get_irn_mode(n);
3144
3145                 /*
3146                  * We can replace the Abs by -x here.
3147                  * We even could add a new Confirm here
3148                  * (if not twos complement)
3149                  *
3150                  * Note that -x would create a new node, so we could
3151                  * not run it in the equivalent_node() context.
3152                  */
3153                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3154                                 get_nodes_block(n), a, mode);
3155
3156                 DBG_OPT_CONFIRM(oldn, n);
3157                 return n;
3158         case value_classified_positive:
3159                 /* n is positive, Abs is not needed */
3160                 n = a;
3161
3162                 DBG_OPT_CONFIRM(oldn, n);
3163                 return n;
3164         default:
3165                 break;
3166         }
3167         if (is_Minus(a)) {
3168                 /* Abs(-x) = Abs(x) */
3169                 mode = get_irn_mode(n);
3170                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3171                                 get_nodes_block(n), get_Minus_op(a), mode);
3172                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3173                 return n;
3174         }
3175         return n;
3176 }  /* transform_node_Abs */
3177
3178 /**
3179  * Optimize -a CMP -b into b CMP a.
3180  * This works only for for modes where unary Minus
3181  * cannot Overflow.
3182  * Note that two-complement integers can Overflow
3183  * so it will NOT work.
3184  *
3185  * For == and != can be handled in Proj(Cmp)
3186  */
3187 static ir_node *transform_node_Cmp(ir_node *n) {
3188         ir_node *oldn = n;
3189         ir_node *left  = get_Cmp_left(n);
3190         ir_node *right = get_Cmp_right(n);
3191
3192         if (is_Minus(left) && is_Minus(right) &&
3193                 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3194                 ir_node *const new_left  = get_Minus_op(right);
3195                 ir_node *const new_right = get_Minus_op(left);
3196                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3197                         get_nodes_block(n), new_left, new_right);
3198                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3199         }
3200         return n;
3201 }  /* transform_node_Cmp */
3202
3203
3204 /**
3205  * Transform a Cond node.
3206  *
3207  * Replace the Cond by a Jmp if it branches on a constant
3208  * condition.
3209  */
3210 static ir_node *transform_node_Cond(ir_node *n) {
3211
3212         ir_node *jmp;
3213         ir_node *a = get_Cond_selector(n);
3214         tarval *ta = value_of(a);
3215
3216         /* we need block info which is not available in floating irgs */
3217         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3218                 return n;
3219
3220         if ((ta != tarval_bad) &&
3221             (get_irn_mode(a) == mode_b) &&
3222             (get_opt_unreachable_code())) {
3223                 /* It's a boolean Cond, branching on a boolean constant.
3224                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3225                 ir_node *blk = get_nodes_block(n);
3226                 jmp = new_r_Jmp(current_ir_graph, blk);
3227                 turn_into_tuple(n, pn_Cond_max);
3228                 if (ta == tarval_b_true) {
3229                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
3230                         set_Tuple_pred(n, pn_Cond_true, jmp);
3231                 } else {
3232                         set_Tuple_pred(n, pn_Cond_false, jmp);
3233                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
3234                 }
3235                 /* We might generate an endless loop, so keep it alive. */
3236                 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3237         }
3238         return n;
3239 }  /* transform_node_Cond */
3240
3241 /**
3242  * Prototype of a recursive transform function
3243  * for bitwise distributive transformations.
3244  */
3245 typedef ir_node* (*recursive_transform)(ir_node *n);
3246
3247 /**
3248  * makes use of distributive laws for and, or, eor
3249  *     and(a OP c, b OP c) -> and(a, b) OP c
3250  * note, might return a different op than n
3251  */
3252 static ir_node *transform_bitwise_distributive(ir_node *n,
3253                                                recursive_transform trans_func)
3254 {
3255         ir_node *oldn    = n;
3256         ir_node *a       = get_binop_left(n);
3257         ir_node *b       = get_binop_right(n);
3258         ir_op   *op      = get_irn_op(a);
3259         ir_op   *op_root = get_irn_op(n);
3260
3261         if(op != get_irn_op(b))
3262                 return n;
3263
3264         if (op == op_Conv) {
3265                 ir_node *a_op   = get_Conv_op(a);
3266                 ir_node *b_op   = get_Conv_op(b);
3267                 ir_mode *a_mode = get_irn_mode(a_op);
3268                 ir_mode *b_mode = get_irn_mode(b_op);
3269                 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3270                         ir_node *blk = get_nodes_block(n);
3271
3272                         n = exact_copy(n);
3273                         set_binop_left(n, a_op);
3274                         set_binop_right(n, b_op);
3275                         set_irn_mode(n, a_mode);
3276                         n = trans_func(n);
3277                         n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3278
3279                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3280                         return n;
3281                 }
3282         }
3283
3284         if (op == op_Eor) {
3285                 /* nothing to gain here */
3286                 return n;
3287         }
3288
3289         if (op == op_Shrs || op == op_Shr || op == op_Shl
3290                         || op == op_And || op == op_Or || op == op_Eor) {
3291                 ir_node *a_left  = get_binop_left(a);
3292                 ir_node *a_right = get_binop_right(a);
3293                 ir_node *b_left  = get_binop_left(b);
3294                 ir_node *b_right = get_binop_right(b);
3295                 ir_node *c       = NULL;
3296                 ir_node *op1     = NULL;
3297                 ir_node *op2     = NULL;
3298
3299                 if (is_op_commutative(op)) {
3300                         if (a_left == b_left) {
3301                                 c   = a_left;
3302                                 op1 = a_right;
3303                                 op2 = b_right;
3304                         } else if(a_left == b_right) {
3305                                 c   = a_left;
3306                                 op1 = a_right;
3307                                 op2 = b_left;
3308                         } else if(a_right == b_left) {
3309                                 c   = a_right;
3310                                 op1 = a_left;
3311                                 op2 = b_right;
3312                         }
3313                 }
3314                 if(a_right == b_right) {
3315                         c   = a_right;
3316                         op1 = a_left;
3317                         op2 = b_left;
3318                 }
3319
3320                 if (c != NULL) {
3321                         /* (a sop c) & (b sop c) => (a & b) sop c */
3322                         ir_node *blk = get_nodes_block(n);
3323
3324                         ir_node *new_n = exact_copy(n);
3325                         set_binop_left(new_n, op1);
3326                         set_binop_right(new_n, op2);
3327                         new_n = trans_func(new_n);
3328
3329                         if(op_root == op_Eor && op == op_Or) {
3330                                 dbg_info  *dbgi = get_irn_dbg_info(n);
3331                                 ir_graph  *irg  = current_ir_graph;
3332                                 ir_mode   *mode = get_irn_mode(c);
3333
3334                                 c = new_rd_Not(dbgi, irg, blk, c, mode);
3335                                 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3336                         } else {
3337                                 n = exact_copy(a);
3338                                 set_nodes_block(n, blk);
3339                                 set_binop_left(n, new_n);
3340                                 set_binop_right(n, c);
3341                                 add_identities(current_ir_graph->value_table, n);
3342                         }
3343
3344                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3345                         return n;
3346                 }
3347         }
3348
3349         return n;
3350 }
3351
3352 /**
3353  * Transform an And.
3354  */
3355 static ir_node *transform_node_And(ir_node *n) {
3356         ir_node *c, *oldn = n;
3357         ir_node *a = get_And_left(n);
3358         ir_node *b = get_And_right(n);
3359         ir_mode *mode;
3360
3361         mode = get_irn_mode(n);
3362         HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3363
3364         /* we can evaluate 2 Projs of the same Cmp */
3365         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3366                 ir_node *pred_a = get_Proj_pred(a);
3367                 ir_node *pred_b = get_Proj_pred(b);
3368                 if (pred_a == pred_b) {
3369                         dbg_info *dbgi  = get_irn_dbg_info(n);
3370                         ir_node  *block = get_nodes_block(pred_a);
3371                         pn_Cmp pn_a     = get_Proj_proj(a);
3372                         pn_Cmp pn_b     = get_Proj_proj(b);
3373                         /* yes, we can simply calculate with pncs */
3374                         pn_Cmp new_pnc  = pn_a & pn_b;
3375
3376                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3377                 }
3378         }
3379         if (is_Or(a)) {
3380                 if (is_Not(b)) {
3381                         ir_node *op = get_Not_op(b);
3382                         if (is_And(op)) {
3383                                 ir_node *ba = get_And_left(op);
3384                                 ir_node *bb = get_And_right(op);
3385
3386                                 /* it's enough to test the following cases due to normalization! */
3387                                 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3388                                         /* (a|b) & ~(a&b) = a^b */
3389                                         ir_node *block = get_nodes_block(n);
3390
3391                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3392                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3393                                         return n;
3394                                 }
3395                         }
3396                 }
3397         }
3398         if (is_Or(b)) {
3399                 if (is_Not(a)) {
3400                         ir_node *op = get_Not_op(a);
3401                         if (is_And(op)) {
3402                                 ir_node *aa = get_And_left(op);
3403                                 ir_node *ab = get_And_right(op);
3404
3405                                 /* it's enough to test the following cases due to normalization! */
3406                                 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3407                                         /* (a|b) & ~(a&b) = a^b */
3408                                         ir_node *block = get_nodes_block(n);
3409
3410                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3411                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3412                                         return n;
3413                                 }
3414                         }
3415                 }
3416         }
3417         if (is_Eor(a)) {
3418                 ir_node *al = get_Eor_left(a);
3419                 ir_node *ar = get_Eor_right(a);
3420
3421                 if (al == b) {
3422                         /* (b ^ a) & b -> ~a & b */
3423                         dbg_info *dbg  = get_irn_dbg_info(n);
3424                         ir_node *block = get_nodes_block(n);
3425
3426                         ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3427                         n  = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3428                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3429                         return n;
3430                 }
3431                 if (ar == b) {
3432                         /* (a ^ b) & b -> ~a & b */
3433                         dbg_info *dbg  = get_irn_dbg_info(n);
3434                         ir_node *block = get_nodes_block(n);
3435
3436                         al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3437                         n  = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3438                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3439                         return n;
3440                 }
3441         }
3442         if (is_Eor(b)) {
3443                 ir_node *bl = get_Eor_left(b);
3444                 ir_node *br = get_Eor_right(b);
3445
3446                 if (bl == a) {
3447                         /* a & (a ^ b) -> a & ~b */
3448                         dbg_info *dbg  = get_irn_dbg_info(n);
3449                         ir_node *block = get_nodes_block(n);
3450
3451                         br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3452                         n  = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3453                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3454                         return n;
3455                 }
3456                 if (br == a) {
3457                         /* a & (b ^ a) -> a & ~b */
3458                         dbg_info *dbg  = get_irn_dbg_info(n);
3459                         ir_node *block = get_nodes_block(n);
3460
3461                         bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3462                         n  = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3463                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3464                         return n;
3465                 }
3466         }
3467         if (is_Not(a) && is_Not(b)) {
3468                 /* ~a & ~b = ~(a|b) */
3469                 ir_node *block = get_nodes_block(n);
3470                 ir_mode *mode = get_irn_mode(n);
3471
3472                 a = get_Not_op(a);
3473                 b = get_Not_op(b);
3474                 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3475                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3476                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3477                 return n;
3478         }
3479
3480         n = transform_bitwise_distributive(n, transform_node_And);
3481
3482         return n;
3483 }  /* transform_node_And */
3484
3485 /**
3486  * Transform an Eor.
3487  */
3488 static ir_node *transform_node_Eor(ir_node *n) {
3489         ir_node *c, *oldn = n;
3490         ir_node *a = get_Eor_left(n);
3491         ir_node *b = get_Eor_right(n);
3492         ir_mode *mode = get_irn_mode(n);
3493
3494         HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3495
3496         /* we can evaluate 2 Projs of the same Cmp */
3497         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3498                 ir_node *pred_a = get_Proj_pred(a);
3499                 ir_node *pred_b = get_Proj_pred(b);
3500                 if(pred_a == pred_b) {
3501                         dbg_info *dbgi  = get_irn_dbg_info(n);
3502                         ir_node  *block = get_nodes_block(pred_a);
3503                         pn_Cmp pn_a     = get_Proj_proj(a);
3504                         pn_Cmp pn_b     = get_Proj_proj(b);
3505                         /* yes, we can simply calculate with pncs */
3506                         pn_Cmp new_pnc  = pn_a ^ pn_b;
3507
3508                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3509                                            new_pnc);
3510                 }
3511         }
3512
3513         if (a == b) {
3514                 /* a ^ a = 0 */
3515                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph,
3516                                  get_mode_null(mode));
3517                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3518         } else if (mode == mode_b &&
3519                         is_Proj(a) &&
3520                         is_Const(b) && is_Const_one(b) &&
3521                         is_Cmp(get_Proj_pred(a))) {
3522                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3523                 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3524                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3525
3526                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3527         } else if (is_Const(b)) {
3528                 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3529                         ir_node  *cnst   = new_Const(tarval_not(get_Const_tarval(b)));
3530                         ir_node  *not_op = get_Not_op(a);
3531                         dbg_info *dbg    = get_irn_dbg_info(n);
3532                         ir_graph *irg    = current_ir_graph;
3533                         ir_node  *block  = get_nodes_block(n);
3534                         ir_mode  *mode   = get_irn_mode(n);
3535                         n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3536                         return n;
3537                 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3538                         n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3539                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3540                 }
3541         } else {
3542                 n = transform_bitwise_distributive(n, transform_node_Eor);
3543         }
3544
3545         return n;
3546 }  /* transform_node_Eor */
3547
3548 /**
3549  * Transform a Not.
3550  */
3551 static ir_node *transform_node_Not(ir_node *n) {
3552         ir_node *c, *oldn = n;
3553         ir_node *a    = get_Not_op(n);
3554         ir_mode *mode = get_irn_mode(n);
3555
3556         HANDLE_UNOP_PHI(tarval_not,a,c);
3557
3558         /* check for a boolean Not */
3559         if (mode == mode_b &&
3560                         is_Proj(a) &&
3561                         is_Cmp(get_Proj_pred(a))) {
3562                 /* We negate a Cmp. The Cmp has the negated result anyways! */
3563                 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3564                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3565                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3566                 return n;
3567         }
3568         if  (is_Eor(a)) {
3569                 ir_node *eor_b = get_Eor_right(a);
3570                 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3571                         ir_node  *cnst  = new_Const(tarval_not(get_Const_tarval(eor_b)));
3572                         ir_node  *eor_a = get_Eor_left(a);
3573                         dbg_info *dbg   = get_irn_dbg_info(n);
3574                         ir_graph *irg   = current_ir_graph;
3575                         ir_node  *block = get_nodes_block(n);
3576                         ir_mode  *mode  = get_irn_mode(n);
3577                         n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3578                         return n;
3579                 }
3580         }
3581         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3582                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3583                         dbg_info *dbg   = get_irn_dbg_info(n);
3584                         ir_graph *irg   = current_ir_graph;
3585                         ir_node  *block = get_nodes_block(n);
3586                         ir_node  *add_l = get_Minus_op(a);
3587                         ir_node  *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3588                         n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3589                 } else if (is_Add(a)) {
3590                         ir_node *add_r = get_Add_right(a);
3591                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3592                                 /* ~(x + -1) = -x */
3593                                 ir_node *op = get_Add_left(a);
3594                                 ir_node *blk = get_nodes_block(n);
3595                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3596                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3597                         }
3598                 }
3599         }
3600         return n;
3601 }  /* transform_node_Not */
3602
3603 /**
3604  * Transform a Minus.
3605  * Optimize:
3606  *   -(~x) = x + 1
3607  *   -(a-b) = b - a
3608  *   -(a >>u (size-1)) = a >>s (size-1)
3609  *   -(a >>s (size-1)) = a >>u (size-1)
3610  *   -(a * const) -> a * -const
3611  */
3612 static ir_node *transform_node_Minus(ir_node *n) {
3613         ir_node *c, *oldn = n;
3614         ir_node *a = get_Minus_op(n);
3615         ir_mode *mode;
3616
3617         HANDLE_UNOP_PHI(tarval_neg,a,c);
3618
3619         mode = get_irn_mode(a);
3620         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3621                 /* the following rules are only to twos-complement */
3622                 if (is_Not(a)) {
3623                         /* -(~x) = x + 1 */
3624                         ir_node *op   = get_Not_op(a);
3625                         tarval *tv    = get_mode_one(mode);
3626                         ir_node *blk  = get_nodes_block(n);
3627                         ir_node *c    = new_Const(tv);
3628                         n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3629                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3630                         return n;
3631                 }
3632                 if (is_Shr(a)) {
3633                         ir_node *c = get_Shr_right(a);
3634
3635                         if (is_Const(c)) {
3636                                 tarval *tv = get_Const_tarval(c);
3637
3638                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3639                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3640                                         ir_node *v = get_Shr_left(a);
3641
3642                                         n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3643                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3644                                         return n;
3645                                 }
3646                         }
3647                 }
3648                 if (is_Shrs(a)) {
3649                         ir_node *c = get_Shrs_right(a);
3650
3651                         if (is_Const(c)) {
3652                                 tarval *tv = get_Const_tarval(c);
3653
3654                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3655                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3656                                         ir_node *v = get_Shrs_left(a);
3657
3658                                         n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3659                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3660                                         return n;
3661                                 }
3662                         }
3663                 }
3664         }
3665         if (is_Sub(a)) {
3666                 /* - (a-b) = b - a */
3667                 ir_node *la  = get_Sub_left(a);
3668                 ir_node *ra  = get_Sub_right(a);
3669                 ir_node *blk = get_nodes_block(n);
3670
3671                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3672                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3673                 return n;
3674         }
3675
3676         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3677                 ir_node *mul_l = get_Mul_left(a);
3678                 ir_node *mul_r = get_Mul_right(a);
3679                 tarval  *tv    = value_of(mul_r);
3680                 if (tv != tarval_bad) {
3681                         tv = tarval_neg(tv);
3682                         if (tv != tarval_bad) {
3683                                 ir_node  *cnst  = new_Const(tv);
3684                                 dbg_info *dbg   = get_irn_dbg_info(a);
3685                                 ir_graph *irg   = current_ir_graph;
3686                                 ir_node  *block = get_nodes_block(a);
3687                                 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3688                                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3689                                 return n;
3690                         }
3691                 }
3692         }
3693
3694         return n;
3695 }  /* transform_node_Minus */
3696
3697 /**
3698  * Transform a Cast_type(Const) into a new Const_type
3699  */
3700 static ir_node *transform_node_Cast(ir_node *n) {
3701         ir_node *oldn = n;
3702         ir_node *pred = get_Cast_op(n);
3703         ir_type *tp = get_irn_type(n);
3704
3705         if (is_Const(pred) && get_Const_type(pred) != tp) {
3706                 n = new_rd_Const_type(NULL, current_ir_graph, get_Const_tarval(pred), tp);
3707                 DBG_OPT_CSTEVAL(oldn, n);
3708         } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3709                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3710                         get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3711                 DBG_OPT_CSTEVAL(oldn, n);
3712         }
3713
3714         return n;
3715 }  /* transform_node_Cast */
3716
3717 /**
3718  * Transform a Proj(Load) with a non-null address.
3719  */
3720 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3721         if (get_opt_ldst_only_null_ptr_exceptions()) {
3722                 if (get_irn_mode(proj) == mode_X) {
3723                         ir_node *load = get_Proj_pred(proj);
3724
3725                         /* get the Load address */
3726                         const ir_node *addr = get_Load_ptr(load);
3727                         const ir_node *confirm;
3728
3729                         if (value_not_null(addr, &confirm)) {
3730                                 if (confirm == NULL) {
3731                                         /* this node may float if it did not depend on a Confirm */
3732                                         set_irn_pinned(load, op_pin_state_floats);
3733                                 }
3734                                 if (get_Proj_proj(proj) == pn_Load_X_except) {
3735                                         DBG_OPT_EXC_REM(proj);
3736                                         return get_irg_bad(current_ir_graph);
3737                                 } else {
3738                                         ir_node *blk = get_nodes_block(load);
3739                                         return new_r_Jmp(current_ir_graph, blk);
3740                                 }
3741                         }
3742                 }
3743         }
3744         return proj;
3745 }  /* transform_node_Proj_Load */
3746
3747 /**
3748  * Transform a Proj(Store) with a non-null address.
3749  */
3750 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3751         if (get_opt_ldst_only_null_ptr_exceptions()) {
3752                 if (get_irn_mode(proj) == mode_X) {
3753                         ir_node *store = get_Proj_pred(proj);
3754
3755                         /* get the load/store address */
3756                         const ir_node *addr = get_Store_ptr(store);
3757                         const ir_node *confirm;
3758
3759                         if (value_not_null(addr, &confirm)) {
3760                                 if (confirm == NULL) {
3761                                         /* this node may float if it did not depend on a Confirm */
3762                                         set_irn_pinned(store, op_pin_state_floats);
3763                                 }
3764                                 if (get_Proj_proj(proj) == pn_Store_X_except) {
3765                                         DBG_OPT_EXC_REM(proj);
3766                                         return get_irg_bad(current_ir_graph);
3767                                 } else {
3768                                         ir_node *blk = get_nodes_block(store);
3769                                         return new_r_Jmp(current_ir_graph, blk);
3770                                 }
3771                         }
3772                 }
3773         }
3774         return proj;
3775 }  /* transform_node_Proj_Store */
3776
3777 /**
3778  * Transform a Proj(Div) with a non-zero value.
3779  * Removes the exceptions and routes the memory to the NoMem node.
3780  */
3781 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3782         ir_node *div = get_Proj_pred(proj);
3783         ir_node *b   = get_Div_right(div);
3784         ir_node *res, *new_mem;
3785         const ir_node *confirm;
3786         long proj_nr;
3787
3788         if (value_not_zero(b, &confirm)) {
3789                 /* div(x, y) && y != 0 */
3790                 if (confirm == NULL) {
3791                         /* we are sure we have a Const != 0 */
3792                         new_mem = get_Div_mem(div);
3793                         new_mem = skip_Pin(new_mem);
3794                         set_Div_mem(div, new_mem);
3795                         set_irn_pinned(div, op_pin_state_floats);
3796                 }
3797
3798                 proj_nr = get_Proj_proj(proj);
3799                 switch (proj_nr) {
3800                 case pn_Div_X_regular:
3801                         return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3802
3803                 case pn_Div_X_except:
3804                         /* we found an exception handler, remove it */
3805                         DBG_OPT_EXC_REM(proj);
3806                         return new_Bad();
3807
3808                 case pn_Div_M:
3809                         res = get_Div_mem(div);
3810                         new_mem = get_irg_no_mem(current_ir_graph);
3811
3812                         if (confirm) {
3813                                 /* This node can only float up to the Confirm block */
3814                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3815                         }
3816                         set_irn_pinned(div, op_pin_state_floats);
3817                         /* this is a Div without exception, we can remove the memory edge */
3818                         set_Div_mem(div, new_mem);
3819                         return res;
3820                 }
3821         }
3822         return proj;
3823 }  /* transform_node_Proj_Div */
3824
3825 /**
3826  * Transform a Proj(Mod) with a non-zero value.
3827  * Removes the exceptions and routes the memory to the NoMem node.
3828  */
3829 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3830         ir_node *mod = get_Proj_pred(proj);
3831         ir_node *b   = get_Mod_right(mod);
3832         ir_node *res, *new_mem;
3833         const ir_node *confirm;
3834         long proj_nr;
3835
3836         if (value_not_zero(b, &confirm)) {
3837                 /* mod(x, y) && y != 0 */
3838                 proj_nr = get_Proj_proj(proj);
3839
3840                 if (confirm == NULL) {
3841                         /* we are sure we have a Const != 0 */
3842                         new_mem = get_Mod_mem(mod);
3843                         new_mem = skip_Pin(new_mem);
3844                         set_Mod_mem(mod, new_mem);
3845                         set_irn_pinned(mod, op_pin_state_floats);
3846                 }
3847
3848                 switch (proj_nr) {
3849
3850                 case pn_Mod_X_regular:
3851                         return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3852
3853                 case pn_Mod_X_except:
3854                         /* we found an exception handler, remove it */
3855                         DBG_OPT_EXC_REM(proj);
3856                         return new_Bad();
3857
3858                 case pn_Mod_M:
3859                         res = get_Mod_mem(mod);
3860                         new_mem = get_irg_no_mem(current_ir_graph);
3861
3862                         if (confirm) {
3863                                 /* This node can only float up to the Confirm block */
3864                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3865                         }
3866                         /* this is a Mod without exception, we can remove the memory edge */
3867                         set_Mod_mem(mod, new_mem);
3868                         return res;
3869                 case pn_Mod_res:
3870                         if (get_Mod_left(mod) == b) {
3871                                 /* a % a = 0 if a != 0 */
3872                                 ir_mode *mode = get_irn_mode(proj);
3873                                 ir_node *res  = new_Const(get_mode_null(mode));
3874
3875                                 DBG_OPT_CSTEVAL(mod, res);
3876                                 return res;
3877                         }
3878                 }
3879         }
3880         return proj;
3881 }  /* transform_node_Proj_Mod */
3882
3883 /**
3884  * Transform a Proj(DivMod) with a non-zero value.
3885  * Removes the exceptions and routes the memory to the NoMem node.
3886  */
3887 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3888         ir_node *divmod = get_Proj_pred(proj);
3889         ir_node *b      = get_DivMod_right(divmod);
3890         ir_node *res, *new_mem;
3891         const ir_node *confirm;
3892         long proj_nr;
3893
3894         if (value_not_zero(b, &confirm)) {
3895                 /* DivMod(x, y) && y != 0 */
3896                 proj_nr = get_Proj_proj(proj);
3897
3898                 if (confirm == NULL) {
3899                         /* we are sure we have a Const != 0 */
3900                         new_mem = get_DivMod_mem(divmod);
3901                         new_mem = skip_Pin(new_mem);
3902                         set_DivMod_mem(divmod, new_mem);
3903                         set_irn_pinned(divmod, op_pin_state_floats);
3904                 }
3905
3906                 switch (proj_nr) {
3907
3908                 case pn_DivMod_X_regular:
3909                         return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3910
3911                 case pn_DivMod_X_except:
3912                         /* we found an exception handler, remove it */
3913                         DBG_OPT_EXC_REM(proj);
3914                         return new_Bad();
3915
3916                 case pn_DivMod_M:
3917                         res = get_DivMod_mem(divmod);
3918                         new_mem = get_irg_no_mem(current_ir_graph);
3919
3920                         if (confirm) {
3921                                 /* This node can only float up to the Confirm block */
3922                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3923                         }
3924                         /* this is a DivMod without exception, we can remove the memory edge */
3925                         set_DivMod_mem(divmod, new_mem);
3926                         return res;
3927
3928                 case pn_DivMod_res_mod:
3929                         if (get_DivMod_left(divmod) == b) {
3930                                 /* a % a = 0 if a != 0 */
3931                                 ir_mode *mode = get_irn_mode(proj);
3932                                 ir_node *res  = new_Const(get_mode_null(mode));
3933
3934                                 DBG_OPT_CSTEVAL(divmod, res);
3935                                 return res;
3936                         }
3937                 }
3938         }
3939         return proj;
3940 }  /* transform_node_Proj_DivMod */
3941
3942 /**
3943  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3944  */
3945 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3946         if (get_opt_unreachable_code()) {
3947                 ir_node *n = get_Proj_pred(proj);
3948                 ir_node *b = get_Cond_selector(n);
3949
3950                 if (mode_is_int(get_irn_mode(b))) {
3951                         tarval *tb = value_of(b);
3952
3953                         if (tb != tarval_bad) {
3954                                 /* we have a constant switch */
3955                                 long num = get_Proj_proj(proj);
3956
3957                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3958                                         if (get_tarval_long(tb) == num) {
3959                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
3960                                                  * in a block. This case is optimized away in optimize_cf(). */
3961                                                 return proj;
3962                                         } else {
3963                                                 /* this case will NEVER be taken, kill it */
3964                                                 return get_irg_bad(current_ir_graph);
3965                                         }
3966                                 }
3967                         }
3968                 }
3969         }
3970         return proj;
3971 }  /* transform_node_Proj_Cond */
3972
3973 /**
3974  * Create a 0 constant of given mode.
3975  */
3976 static ir_node *create_zero_const(ir_mode *mode) {
3977         tarval   *tv    = get_mode_null(mode);
3978         ir_node  *cnst  = new_Const(tv);
3979
3980         return cnst;
3981 }
3982
3983 /* the order of the values is important! */
3984 typedef enum const_class {
3985         const_const = 0,
3986         const_like  = 1,
3987         const_other = 2
3988 } const_class;
3989
3990 static const_class classify_const(const ir_node* n)
3991 {
3992         if (is_Const(n))         return const_const;
3993         if (is_irn_constlike(n)) return const_like;
3994         return const_other;
3995 }
3996
3997 /**
3998  * Determines whether r is more constlike or has a larger index (in that order)
3999  * than l.
4000  */
4001 static int operands_are_normalized(const ir_node *l, const ir_node *r)
4002 {
4003         const const_class l_order = classify_const(l);
4004         const const_class r_order = classify_const(r);
4005         return
4006                 l_order > r_order ||
4007                 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4008 }
4009
4010 /**
4011  * Normalizes and optimizes Cmp nodes.
4012  */
4013 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
4014         ir_node      *n      = get_Proj_pred(proj);
4015         ir_node      *left   = get_Cmp_left(n);
4016         ir_node      *right  = get_Cmp_right(n);
4017         tarval      *tv      = NULL;
4018         int          changed = 0;
4019         ir_mode     *mode    = NULL;
4020         long         proj_nr = get_Proj_proj(proj);
4021
4022         /* we can evaluate some cases directly */
4023         switch (proj_nr) {
4024         case pn_Cmp_False:
4025                 return new_Const(get_tarval_b_false());
4026         case pn_Cmp_True:
4027                 return new_Const(get_tarval_b_true());
4028         case pn_Cmp_Leg:
4029                 if (!mode_is_float(get_irn_mode(left)))
4030                         return new_Const(get_tarval_b_true());
4031                 break;
4032         default:
4033                 break;
4034         }
4035
4036         /* remove Casts of both sides */
4037         left  = skip_Cast(left);
4038         right = skip_Cast(right);
4039
4040         /* Remove unnecessary conversions */
4041         /* TODO handle constants */
4042         if (is_Conv(left) && is_Conv(right)) {
4043                 ir_mode *mode        = get_irn_mode(left);
4044                 ir_node *op_left     = get_Conv_op(left);
4045                 ir_node *op_right    = get_Conv_op(right);
4046                 ir_mode *mode_left   = get_irn_mode(op_left);
4047                 ir_mode *mode_right  = get_irn_mode(op_right);
4048
4049                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4050                                 && mode_left != mode_b && mode_right != mode_b) {
4051                         ir_graph *irg   = current_ir_graph;
4052                         ir_node  *block = get_nodes_block(n);
4053
4054                         if (mode_left == mode_right) {
4055                                 left  = op_left;
4056                                 right = op_right;
4057                                 changed |= 1;
4058                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4059                         } else if (smaller_mode(mode_left, mode_right)) {
4060                                 left  = new_r_Conv(irg, block, op_left, mode_right);
4061                                 right = op_right;
4062                                 changed |= 1;
4063                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4064                         } else if (smaller_mode(mode_right, mode_left)) {
4065                                 left  = op_left;
4066                                 right = new_r_Conv(irg, block, op_right, mode_left);
4067                                 changed |= 1;
4068                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4069                         }
4070                 }
4071         }
4072
4073         /* remove operation on both sides if possible */
4074         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4075                 /*
4076                  * The following operations are NOT safe for floating point operations, for instance
4077                  * 1.0 + inf == 2.0 + inf, =/=> x == y
4078                  */
4079                 if (mode_is_int(get_irn_mode(left))) {
4080                         unsigned lop = get_irn_opcode(left);
4081
4082                         if (lop == get_irn_opcode(right)) {
4083                                 ir_node *ll, *lr, *rl, *rr;
4084
4085                                 /* same operation on both sides, try to remove */
4086                                 switch (lop) {
4087                                 case iro_Not:
4088                                 case iro_Minus:
4089                                         /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4090                                         left  = get_unop_op(left);
4091                                         right = get_unop_op(right);
4092                                         changed |= 1;
4093                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4094                                         break;
4095                                 case iro_Add:
4096                                         ll = get_Add_left(left);
4097                                         lr = get_Add_right(left);
4098                                         rl = get_Add_left(right);
4099                                         rr = get_Add_right(right);
4100
4101                                         if (ll == rl) {
4102                                                 /* X + a CMP X + b ==> a CMP b */
4103                                                 left  = lr;
4104                                                 right = rr;
4105                                                 changed |= 1;
4106                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4107                                         } else if (ll == rr) {
4108                                                 /* X + a CMP b + X ==> a CMP b */
4109                                                 left  = lr;
4110                                                 right = rl;
4111                                                 changed |= 1;
4112                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4113                                         } else if (lr == rl) {
4114                                                 /* a + X CMP X + b ==> a CMP b */
4115                                                 left  = ll;
4116                                                 right = rr;
4117                                                 changed |= 1;
4118                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4119                                         } else if (lr == rr) {
4120                                                 /* a + X CMP b + X ==> a CMP b */
4121                                                 left  = ll;
4122                                                 right = rl;
4123                                                 changed |= 1;
4124                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4125                                         }
4126                                         break;
4127                                 case iro_Sub:
4128                                         ll = get_Sub_left(left);
4129                                         lr = get_Sub_right(left);
4130                                         rl = get_Sub_left(right);
4131                                         rr = get_Sub_right(right);
4132
4133                                         if (ll == rl) {
4134                                                 /* X - a CMP X - b ==> a CMP b */
4135                                                 left  = lr;
4136                                                 right = rr;
4137                                                 changed |= 1;
4138                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4139                                         } else if (lr == rr) {
4140                                                 /* a - X CMP b - X ==> a CMP b */
4141                                                 left  = ll;
4142                                                 right = rl;
4143                                                 changed |= 1;
4144                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4145                                         }
4146                                         break;
4147                                 case iro_Rotl:
4148                                         if (get_Rotl_right(left) == get_Rotl_right(right)) {
4149                                                 /* a ROTL X CMP b ROTL X ==> a CMP b */
4150                                                 left  = get_Rotl_left(left);
4151                                                 right = get_Rotl_left(right);
4152                                                 changed |= 1;
4153                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4154                                         }
4155                                         break;
4156                                 default:
4157                                         break;
4158                                 }
4159                         }
4160
4161                         /* X+A == A, A+X == A, A-X == A -> X == 0 */
4162                         if (is_Add(left) || is_Sub(left)) {
4163                                 ir_node *ll = get_binop_left(left);
4164                                 ir_node *lr = get_binop_right(left);
4165
4166                                 if (lr == right && is_Add(left)) {
4167                                         ir_node *tmp = ll;
4168                                         ll = lr;
4169                                         lr = tmp;
4170                                 }
4171                                 if (ll == right) {
4172                                         left     = lr;
4173                                         right    = create_zero_const(get_irn_mode(left));
4174                                         changed |= 1;
4175                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4176                                 }
4177                         }
4178                         if (is_Add(right) || is_Sub(right)) {
4179                                 ir_node *rl = get_binop_left(right);
4180                                 ir_node *rr = get_binop_right(right);
4181
4182                                 if (rr == left && is_Add(right)) {
4183                                         ir_node *tmp = rl;
4184                                         rl = rr;
4185                                         rr = tmp;
4186                                 }
4187                                 if (rl == left) {
4188                                         left     = rr;
4189                                         right    = create_zero_const(get_irn_mode(left));
4190                                         changed |= 1;
4191                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4192                                 }
4193                         }
4194                         if (is_And(left) && is_Const(right)) {
4195                                 ir_node *ll = get_binop_left(left);
4196                                 ir_node *lr = get_binop_right(left);
4197                                 if (is_Shr(ll) && is_Const(lr)) {
4198                                         /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4199                                         ir_node *block = get_nodes_block(n);
4200                                         ir_mode *mode = get_irn_mode(left);
4201
4202                                         ir_node *llr = get_Shr_right(ll);
4203                                         if (is_Const(llr)) {
4204                                                 ir_graph *irg = current_ir_graph;
4205                                                 dbg_info *dbg = get_irn_dbg_info(left);
4206
4207                                                 tarval *c1    = get_Const_tarval(llr);
4208                                                 tarval *c2    = get_Const_tarval(lr);
4209                                                 tarval *c3    = get_Const_tarval(right);
4210                                                 tarval *mask  = tarval_shl(c2, c1);
4211                                                 tarval *value = tarval_shl(c3, c1);
4212
4213                                                 left  = new_rd_And(dbg, irg, block, get_Shr_left(ll), new_Const(mask), mode);
4214                                                 right = new_Const(value);
4215                                                 changed |= 1;
4216                                         }
4217                                 }
4218                         }
4219                 }  /* mode_is_int(...) */
4220         }  /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4221
4222         /* replace mode_b compares with ands/ors */
4223         if (get_irn_mode(left) == mode_b) {
4224                 ir_graph *irg   = current_ir_graph;
4225                 ir_node  *block = get_nodes_block(n);
4226                 ir_node  *bres;
4227
4228                 switch (proj_nr) {
4229                         case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4230                         case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4231                         case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4232                         case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4233                         case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4234                         case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4235                         default: bres = NULL;
4236                 }
4237                 if (bres) {
4238                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4239                         return bres;
4240                 }
4241         }
4242
4243         /*
4244          * First step: normalize the compare op
4245          * by placing the constant on the right side
4246          * or moving the lower address node to the left.
4247          */
4248         if (!operands_are_normalized(left, right)) {
4249                 ir_node *t = left;
4250
4251                 left  = right;
4252                 right = t;
4253
4254                 proj_nr = get_inversed_pnc(proj_nr);
4255                 changed |= 1;
4256         }
4257
4258         /*
4259          * Second step: Try to reduce the magnitude
4260          * of a constant. This may help to generate better code
4261          * later and may help to normalize more compares.
4262          * Of course this is only possible for integer values.
4263          */
4264         tv = value_of(right);
4265         if (tv != tarval_bad) {
4266                 mode = get_irn_mode(right);
4267
4268                 /* TODO extend to arbitrary constants */
4269                 if (is_Conv(left) && tarval_is_null(tv)) {
4270                         ir_node *op      = get_Conv_op(left);
4271                         ir_mode *op_mode = get_irn_mode(op);
4272
4273                         /*
4274                          * UpConv(x) REL 0  ==> x REL 0
4275                          */
4276                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4277                             ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4278                                  mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4279                                 tv   = get_mode_null(op_mode);
4280                                 left = op;
4281                                 mode = op_mode;
4282                                 changed |= 2;
4283                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4284                         }
4285                 }
4286
4287                 if (tv != tarval_bad) {
4288                         /* the following optimization is possible on modes without Overflow
4289                          * on Unary Minus or on == and !=:
4290                          * -a CMP c  ==>  a swap(CMP) -c
4291                          *
4292                          * Beware: for two-complement Overflow may occur, so only == and != can
4293                          * be optimized, see this:
4294                          * -MININT < 0 =/=> MININT > 0 !!!
4295                          */
4296                         if (is_Minus(left) &&
4297                                 (!mode_overflow_on_unary_Minus(mode) ||
4298                                 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4299                                 tv = tarval_neg(tv);
4300
4301                                 if (tv != tarval_bad) {
4302                                         left = get_Minus_op(left);
4303                                         proj_nr = get_inversed_pnc(proj_nr);
4304                                         changed |= 2;
4305                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4306                                 }
4307                         } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4308                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
4309                                 tv = tarval_not(tv);
4310
4311                                 if (tv != tarval_bad) {
4312                                         left = get_Not_op(left);
4313                                         changed |= 2;
4314                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4315                                 }
4316                         }
4317
4318                         /* for integer modes, we have more */
4319                         if (mode_is_int(mode)) {
4320                                 /* Ne includes Unordered which is not possible on integers.
4321                                  * However, frontends often use this wrong, so fix it here */
4322                                 if (proj_nr & pn_Cmp_Uo) {
4323                                         proj_nr &= ~pn_Cmp_Uo;
4324                                         set_Proj_proj(proj, proj_nr);
4325                                 }
4326
4327                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
4328                                 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4329                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4330                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4331
4332                                         if (tv != tarval_bad) {
4333                                                 proj_nr ^= pn_Cmp_Eq;
4334                                                 changed |= 2;
4335                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4336                                         }
4337                                 }
4338                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
4339                                 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4340                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4341                                         tv = tarval_add(tv, get_mode_one(mode));
4342
4343                                         if (tv != tarval_bad) {
4344                                                 proj_nr ^= pn_Cmp_Eq;
4345                                                 changed |= 2;
4346                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4347                                         }
4348                                 }
4349
4350                                 /* the following reassociations work only for == and != */
4351                                 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4352
4353 #if 0 /* Might be not that good in general */
4354                                         /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
4355                                         if (tarval_is_null(tv) && is_Sub(left)) {
4356                                                 right = get_Sub_right(left);
4357                                                 left  = get_Sub_left(left);
4358
4359                                                 tv = value_of(right);
4360                                                 changed = 1;
4361                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4362                                         }
4363 #endif
4364
4365                                         if (tv != tarval_bad) {
4366                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
4367                                                 if (is_Sub(left)) {
4368                                                         ir_node *c1 = get_Sub_right(left);
4369                                                         tarval *tv2 = value_of(c1);
4370
4371                                                         if (tv2 != tarval_bad) {
4372                                                                 tv2 = tarval_add(tv, value_of(c1));
4373
4374                                                                 if (tv2 != tarval_bad) {
4375                                                                         left    = get_Sub_left(left);
4376                                                                         tv      = tv2;
4377                                                                         changed |= 2;
4378                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4379                                                                 }
4380                                                         }
4381                                                 }
4382                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
4383                                                 else if (is_Add(left)) {
4384                                                         ir_node *a_l = get_Add_left(left);
4385                                                         ir_node *a_r = get_Add_right(left);
4386                                                         ir_node *a;
4387                                                         tarval *tv2;
4388
4389                                                         if (is_Const(a_l)) {
4390                                                                 a = a_r;
4391                                                                 tv2 = value_of(a_l);
4392                                                         } else {
4393                                                                 a = a_l;
4394                                                                 tv2 = value_of(a_r);
4395                                                         }
4396
4397                                                         if (tv2 != tarval_bad) {
4398                                                                 tv2 = tarval_sub(tv, tv2, NULL);
4399
4400                                                                 if (tv2 != tarval_bad) {
4401                                                                         left    = a;
4402                                                                         tv      = tv2;
4403                                                                         changed |= 2;
4404                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4405                                                                 }
4406                                                         }
4407                                                 }
4408                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
4409                                                 else if (is_Minus(left)) {
4410                                                         tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4411
4412                                                         if (tv2 != tarval_bad) {
4413                                                                 left    = get_Minus_op(left);
4414                                                                 tv      = tv2;
4415                                                                 changed |= 2;
4416                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4417                                                         }
4418                                                 }
4419                                         }
4420                                 } /* == or != */
4421                                 /* the following reassociations work only for <= */
4422                                 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4423                                         if (tv != tarval_bad) {
4424                                                 /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
4425                                                 if (is_Abs(left)) { // TODO something is missing here
4426                                                 }
4427                                         }
4428                                 }
4429                         } /* mode_is_int */
4430
4431                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4432                                 switch (get_irn_opcode(left)) {
4433                                         ir_node *c1;
4434
4435                                 case iro_And:
4436                                         c1 = get_And_right(left);
4437                                         if (is_Const(c1)) {
4438                                                 /*
4439                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4440                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4441                                                  */
4442                                                 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4443                                                 if (mask != tv) {
4444                                                         /* TODO: move to constant evaluation */
4445                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4446                                                         c1 = new_Const(tv);
4447                                                         DBG_OPT_CSTEVAL(proj, c1);
4448                                                         return c1;
4449                                                 }
4450
4451                                                 if (tarval_is_single_bit(tv)) {
4452                                                         /*
4453                                                          * optimization for AND:
4454                                                          * Optimize:
4455                                                          *   And(x, C) == C  ==>  And(x, C) != 0
4456                                                          *   And(x, C) != C  ==>  And(X, C) == 0
4457                                                          *
4458                                                          * if C is a single Bit constant.
4459                                                          */
4460
4461                                                         /* check for Constant's match. We have check hare the tarvals,
4462                                                            because our const might be changed */
4463                                                         if (get_Const_tarval(c1) == tv) {
4464                                                                 /* fine: do the transformation */
4465                                                                 tv = get_mode_null(get_tarval_mode(tv));
4466                                                                 proj_nr ^= pn_Cmp_Leg;
4467                                                                 changed |= 2;
4468                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4469                                                         }
4470                                                 }
4471                                         }
4472                                         break;
4473                                 case iro_Or:
4474                                         c1 = get_Or_right(left);
4475                                         if (is_Const(c1) && tarval_is_null(tv)) {
4476                                                 /*
4477                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
4478                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
4479                                                  */
4480                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
4481                                                         /* TODO: move to constant evaluation */
4482                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4483                                                         c1 = new_Const(tv);
4484                                                         DBG_OPT_CSTEVAL(proj, c1);
4485                                                         return c1;
4486                                                 }
4487                                         }
4488                                         break;
4489                                 case iro_Shl:
4490                                         /*
4491                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
4492                                          *                             FALSE                       else
4493                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
4494                                          *                             TRUE                        else
4495                                          */
4496                                         c1 = get_Shl_right(left);
4497                                         if (is_Const(c1)) {
4498                                                 tarval  *tv1    = get_Const_tarval(c1);
4499                                                 ir_mode *mode   = get_irn_mode(left);
4500                                                 tarval  *minus1 = get_mode_all_one(mode);
4501                                                 tarval  *amask  = tarval_shr(minus1, tv1);
4502                                                 tarval  *cmask  = tarval_shl(minus1, tv1);
4503                                                 ir_node *sl, *blk;
4504
4505                                                 if (tarval_and(tv, cmask) != tv) {
4506                                                         /* condition not met */
4507                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4508                                                         c1 = new_Const(tv);
4509                                                         DBG_OPT_CSTEVAL(proj, c1);
4510                                                         return c1;
4511                                                 }
4512                                                 sl   = get_Shl_left(left);
4513                                                 blk  = get_nodes_block(n);
4514                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4515                                                 tv   = tarval_shr(tv, tv1);
4516                                                 changed |= 2;
4517                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4518                                         }
4519                                         break;
4520                                 case iro_Shr:
4521                                         /*
4522                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
4523                                          *                             FALSE                       else
4524                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
4525                                          *                             TRUE                        else
4526                                          */
4527                                         c1 = get_Shr_right(left);
4528                                         if (is_Const(c1)) {
4529                                                 tarval  *tv1    = get_Const_tarval(c1);
4530                                                 ir_mode *mode   = get_irn_mode(left);
4531                                                 tarval  *minus1 = get_mode_all_one(mode);
4532                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4533                                                 tarval  *cmask  = tarval_shr(minus1, tv1);
4534                                                 ir_node *sl, *blk;
4535
4536                                                 if (tarval_and(tv, cmask) != tv) {
4537                                                         /* condition not met */
4538                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4539                                                         c1 = new_Const(tv);
4540                                                         DBG_OPT_CSTEVAL(proj, c1);
4541                                                         return c1;
4542                                                 }
4543                                                 sl   = get_Shr_left(left);
4544                                                 blk  = get_nodes_block(n);
4545                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4546                                                 tv   = tarval_shl(tv, tv1);
4547                                                 changed |= 2;
4548                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4549                                         }
4550                                         break;
4551                                 case iro_Shrs:
4552                                         /*
4553                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4554                                          *                             FALSE                       else
4555                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4556                                          *                             TRUE                        else
4557                                          */
4558                                         c1 = get_Shrs_right(left);
4559                                         if (is_Const(c1)) {
4560                                                 tarval  *tv1    = get_Const_tarval(c1);
4561                                                 ir_mode *mode   = get_irn_mode(left);
4562                                                 tarval  *minus1 = get_mode_all_one(mode);
4563                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4564                                                 tarval  *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4565                                                 ir_node *sl, *blk;
4566
4567                                                 cond = tarval_sub(cond, tv1, NULL);
4568                                                 cond = tarval_shrs(tv, cond);
4569
4570                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4571                                                         /* condition not met */
4572                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4573                                                         c1 = new_Const(tv);
4574                                                         DBG_OPT_CSTEVAL(proj, c1);
4575                                                         return c1;
4576                                                 }
4577                                                 sl   = get_Shrs_left(left);
4578                                                 blk  = get_nodes_block(n);
4579                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4580                                                 tv   = tarval_shl(tv, tv1);
4581                                                 changed |= 2;
4582                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4583                                         }
4584                                         break;
4585                                 }  /* switch */
4586                         }
4587                 } /* tarval != bad */
4588         }
4589
4590         if (changed & 2)      /* need a new Const */
4591                 right = new_Const(tv);
4592
4593         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4594                 ir_node *op = get_Proj_pred(left);
4595
4596                 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4597                     (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4598                         ir_node *c = get_binop_right(op);
4599
4600                         if (is_Const(c)) {
4601                                 tarval *tv = get_Const_tarval(c);
4602
4603                                 if (tarval_is_single_bit(tv)) {
4604                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4605                                         ir_node *v    = get_binop_left(op);
4606                                         ir_node *blk  = get_irn_n(op, -1);
4607                                         ir_mode *mode = get_irn_mode(v);
4608
4609                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4610                                         left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(tv), mode);
4611                                         changed |= 1;
4612                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4613                                 }
4614                         }
4615                 }
4616         }
4617
4618         if (changed) {
4619                 ir_node *block = get_nodes_block(n);
4620
4621                 /* create a new compare */
4622                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4623                 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4624         }
4625
4626         return proj;
4627 }  /* transform_node_Proj_Cmp */
4628
4629 /**
4630  * Optimize CopyB(mem, x, x) into a Nop.
4631  */
4632 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4633         ir_node *copyb = get_Proj_pred(proj);
4634         ir_node *a     = get_CopyB_dst(copyb);
4635         ir_node *b     = get_CopyB_src(copyb);
4636
4637         if (a == b) {
4638                 switch (get_Proj_proj(proj)) {
4639                 case pn_CopyB_X_regular:
4640                         /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4641                         DBG_OPT_EXC_REM(proj);
4642                         proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4643                         break;
4644                 case pn_CopyB_M_except:
4645                 case pn_CopyB_X_except:
4646                         DBG_OPT_EXC_REM(proj);
4647                         proj = get_irg_bad(current_ir_graph);
4648                         break;
4649                 default:
4650                         break;
4651                 }
4652         }
4653         return proj;
4654 }  /* transform_node_Proj_CopyB */
4655
4656 /**
4657  * Optimize Bounds(idx, idx, upper) into idx.
4658  */
4659 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4660         ir_node *oldn  = proj;
4661         ir_node *bound = get_Proj_pred(proj);
4662         ir_node *idx   = get_Bound_index(bound);
4663         ir_node *pred  = skip_Proj(idx);
4664         int ret_tuple  = 0;
4665
4666         if (idx == get_Bound_lower(bound))
4667                 ret_tuple = 1;
4668         else if (is_Bound(pred)) {
4669                 /*
4670                 * idx was Bounds checked in the same MacroBlock previously,
4671                 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4672                 */
4673                 ir_node *lower = get_Bound_lower(bound);
4674                 ir_node *upper = get_Bound_upper(bound);
4675                 if (get_Bound_lower(pred) == lower &&
4676                         get_Bound_upper(pred) == upper &&
4677                         get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4678                         /*
4679                          * One could expect that we simply return the previous
4680                          * Bound here. However, this would be wrong, as we could
4681                          * add an exception Proj to a new location then.
4682                          * So, we must turn in into a tuple.
4683                          */
4684                         ret_tuple = 1;
4685                 }
4686         }
4687         if (ret_tuple) {
4688                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4689                 switch (get_Proj_proj(proj)) {
4690                 case pn_Bound_M:
4691                         DBG_OPT_EXC_REM(proj);
4692                         proj = get_Bound_mem(bound);
4693                         break;
4694                 case pn_Bound_X_except:
4695                         DBG_OPT_EXC_REM(proj);
4696                         proj = get_irg_bad(current_ir_graph);
4697                         break;
4698                 case pn_Bound_res:
4699                         proj = idx;
4700                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4701                         break;
4702                 case pn_Bound_X_regular:
4703                         DBG_OPT_EXC_REM(proj);
4704                         proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4705                         break;
4706                 default:
4707                         break;
4708                 }
4709         }
4710         return proj;
4711 }  /* transform_node_Proj_Bound */
4712
4713 /**
4714  * Does all optimizations on nodes that must be done on it's Proj's
4715  * because of creating new nodes.
4716  */
4717 static ir_node *transform_node_Proj(ir_node *proj) {
4718         ir_node *n = get_Proj_pred(proj);
4719
4720         if (n->op->ops.transform_node_Proj)
4721                 return n->op->ops.transform_node_Proj(proj);
4722         return proj;
4723 }  /* transform_node_Proj */
4724
4725 /**
4726  * Move Confirms down through Phi nodes.
4727  */
4728 static ir_node *transform_node_Phi(ir_node *phi) {
4729         int i, n;
4730         ir_mode *mode = get_irn_mode(phi);
4731
4732         if (mode_is_reference(mode)) {
4733                 n = get_irn_arity(phi);
4734
4735                 /* Beware of Phi0 */
4736                 if (n > 0) {
4737                         ir_node *pred = get_irn_n(phi, 0);
4738                         ir_node *bound, *new_Phi, *block, **in;
4739                         pn_Cmp  pnc;
4740
4741                         if (! is_Confirm(pred))
4742                                 return phi;
4743
4744                         bound = get_Confirm_bound(pred);
4745                         pnc   = get_Confirm_cmp(pred);
4746
4747                         NEW_ARR_A(ir_node *, in, n);
4748                         in[0] = get_Confirm_value(pred);
4749
4750                         for (i = 1; i < n; ++i) {
4751                                 pred = get_irn_n(phi, i);
4752
4753                                 if (! is_Confirm(pred) ||
4754                                         get_Confirm_bound(pred) != bound ||
4755                                         get_Confirm_cmp(pred) != pnc)
4756                                         return phi;
4757                                 in[i] = get_Confirm_value(pred);
4758                         }
4759                         /* move the Confirm nodes "behind" the Phi */
4760                         block = get_irn_n(phi, -1);
4761                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4762                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4763                 }
4764         }
4765         return phi;
4766 }  /* transform_node_Phi */
4767
4768 /**
4769  * Returns the operands of a commutative bin-op, if one operand is
4770  * a const, it is returned as the second one.
4771  */
4772 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4773         ir_node *op_a = get_binop_left(binop);
4774         ir_node *op_b = get_binop_right(binop);
4775
4776         assert(is_op_commutative(get_irn_op(binop)));
4777
4778         if (is_Const(op_a)) {
4779                 *a = op_b;
4780                 *c = op_a;
4781         } else {
4782                 *a = op_a;
4783                 *c = op_b;
4784         }
4785 }  /* get_comm_Binop_Ops */
4786
4787 /**
4788  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4789  * Such pattern may arise in bitfield stores.
4790  *
4791  * value  c4                  value      c4 & c2
4792  *    AND     c3                    AND           c1 | c3
4793  *        OR     c2      ===>               OR
4794  *           AND    c1
4795  *               OR
4796  *
4797  *
4798  * value  c2                 value  c1
4799  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
4800  *        OR
4801  */
4802 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4803         ir_node *and, *c1;
4804         ir_node *or_l, *c2;
4805         ir_node *and_l, *c3;
4806         ir_node *value, *c4;
4807         ir_node *new_and, *new_const, *block;
4808         ir_mode *mode = get_irn_mode(or);
4809
4810         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4811
4812         while (1) {
4813                 get_comm_Binop_Ops(or, &and, &c1);
4814                 if (!is_Const(c1) || !is_And(and))
4815                         return or;
4816
4817                 get_comm_Binop_Ops(and, &or_l, &c2);
4818                 if (!is_Const(c2))
4819                         return or;
4820
4821                 tv1 = get_Const_tarval(c1);
4822                 tv2 = get_Const_tarval(c2);
4823
4824                 tv = tarval_or(tv1, tv2);
4825                 if (tarval_is_all_one(tv)) {
4826                         /* the AND does NOT clear a bit with isn't set by the OR */
4827                         set_Or_left(or, or_l);
4828                         set_Or_right(or, c1);
4829
4830                         /* check for more */
4831                         continue;
4832                 }
4833
4834                 if (!is_Or(or_l))
4835                         return or;
4836
4837                 get_comm_Binop_Ops(or_l, &and_l, &c3);
4838                 if (!is_Const(c3) || !is_And(and_l))
4839                         return or;
4840
4841                 get_comm_Binop_Ops(and_l, &value, &c4);
4842                 if (!is_Const(c4))
4843                         return or;
4844
4845                 /* ok, found the pattern, check for conditions */
4846                 assert(mode == get_irn_mode(and));
4847                 assert(mode == get_irn_mode(or_l));
4848                 assert(mode == get_irn_mode(and_l));
4849
4850                 tv3 = get_Const_tarval(c3);
4851                 tv4 = get_Const_tarval(c4);
4852
4853                 tv = tarval_or(tv4, tv2);
4854                 if (!tarval_is_all_one(tv)) {
4855                         /* have at least one 0 at the same bit position */
4856                         return or;
4857                 }
4858
4859                 n_tv4 = tarval_not(tv4);
4860                 if (tv3 != tarval_and(tv3, n_tv4)) {
4861                         /* bit in the or_mask is outside the and_mask */
4862                         return or;
4863                 }
4864
4865                 n_tv2 = tarval_not(tv2);
4866                 if (tv1 != tarval_and(tv1, n_tv2)) {
4867                         /* bit in the or_mask is outside the and_mask */
4868                         return or;
4869                 }
4870
4871                 /* ok, all conditions met */
4872                 block = get_irn_n(or, -1);
4873
4874                 new_and = new_r_And(current_ir_graph, block,
4875                         value, new_Const(tarval_and(tv4, tv2)), mode);
4876
4877                 new_const = new_Const(tarval_or(tv3, tv1));
4878
4879                 set_Or_left(or, new_and);
4880                 set_Or_right(or, new_const);
4881
4882                 /* check for more */
4883         }
4884 }  /* transform_node_Or_bf_store */
4885
4886 /**
4887  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4888  */
4889 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4890         ir_mode *mode = get_irn_mode(or);
4891         ir_node *shl, *shr, *block;
4892         ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4893         tarval *tv1, *tv2;
4894
4895         if (! mode_is_int(mode))
4896                 return or;
4897
4898         shl = get_binop_left(or);
4899         shr = get_binop_right(or);
4900
4901         if (is_Shr(shl)) {
4902                 if (!is_Shl(shr))
4903                         return or;
4904
4905                 irn = shl;
4906                 shl = shr;
4907                 shr = irn;
4908         } else if (!is_Shl(shl)) {
4909                 return or;
4910         } else if (!is_Shr(shr)) {
4911                 return or;
4912         }
4913         x = get_Shl_left(shl);
4914         if (x != get_Shr_left(shr))
4915                 return or;
4916
4917         c1 = get_Shl_right(shl);
4918         c2 = get_Shr_right(shr);
4919         if (is_Const(c1) && is_Const(c2)) {
4920                 tv1 = get_Const_tarval(c1);
4921                 if (! tarval_is_long(tv1))
4922                         return or;
4923
4924                 tv2 = get_Const_tarval(c2);
4925                 if (! tarval_is_long(tv2))
4926                         return or;
4927
4928                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4929                                 != (int) get_mode_size_bits(mode))
4930                         return or;
4931
4932                 /* yet, condition met */
4933                 block = get_nodes_block(or);
4934
4935                 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4936
4937                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4938                 return n;
4939         }
4940
4941     if (is_Sub(c1)) {
4942             v      = c2;
4943                 sub    = c1;
4944         rotval = sub; /* a Rot right is not supported, so use a rot left */
4945     } else if (is_Sub(c2)) {
4946                 v      = c1;
4947         sub    = c2;
4948         rotval = v;
4949     } else return or;
4950
4951         if (get_Sub_right(sub) != v)
4952                 return or;
4953
4954         c1 = get_Sub_left(sub);
4955         if (!is_Const(c1))
4956                 return or;
4957
4958         tv1 = get_Const_tarval(c1);
4959         if (! tarval_is_long(tv1))
4960                 return or;
4961
4962         if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4963                 return or;
4964
4965         /* yet, condition met */
4966         block = get_nodes_block(or);
4967
4968         n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4969
4970         DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4971         return n;
4972 }  /* transform_node_Or_Rotl */
4973
4974 /**
4975  * Transform an Or.
4976  */
4977 static ir_node *transform_node_Or(ir_node *n) {
4978         ir_node *c, *oldn = n;
4979         ir_node *a = get_Or_left(n);
4980         ir_node *b = get_Or_right(n);
4981         ir_mode *mode;
4982
4983         if (is_Not(a) && is_Not(b)) {
4984                 /* ~a | ~b = ~(a&b) */
4985                 ir_node *block = get_nodes_block(n);
4986
4987                 mode = get_irn_mode(n);
4988                 a = get_Not_op(a);
4989                 b = get_Not_op(b);
4990                 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4991                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4992                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4993                 return n;
4994         }
4995
4996         /* we can evaluate 2 Projs of the same Cmp */
4997         if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4998                 ir_node *pred_a = get_Proj_pred(a);
4999                 ir_node *pred_b = get_Proj_pred(b);
5000                 if (pred_a == pred_b) {
5001                         dbg_info *dbgi  = get_irn_dbg_info(n);
5002                         ir_node  *block = get_nodes_block(pred_a);
5003                         pn_Cmp pn_a     = get_Proj_proj(a);
5004                         pn_Cmp pn_b     = get_Proj_proj(b);
5005                         /* yes, we can simply calculate with pncs */
5006                         pn_Cmp new_pnc  = pn_a | pn_b;
5007
5008                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
5009                                            new_pnc);
5010                 }
5011         }
5012
5013         mode = get_irn_mode(n);
5014         HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
5015
5016         n = transform_node_Or_bf_store(n);
5017         n = transform_node_Or_Rotl(n);
5018         if (n != oldn)
5019                 return n;
5020
5021         n = transform_bitwise_distributive(n, transform_node_Or);
5022
5023         return n;
5024 }  /* transform_node_Or */
5025
5026
5027 /* forward */
5028 static ir_node *transform_node(ir_node *n);
5029
5030 /**
5031  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5032  *
5033  * Should be moved to reassociation?
5034  */
5035 static ir_node *transform_node_shift(ir_node *n) {
5036         ir_node *left, *right;
5037         ir_mode *mode;
5038         tarval *tv1, *tv2, *res;
5039         ir_node *in[2], *irn, *block;
5040
5041         left = get_binop_left(n);
5042
5043         /* different operations */
5044         if (get_irn_op(left) != get_irn_op(n))
5045                 return n;
5046
5047         right = get_binop_right(n);
5048         tv1 = value_of(right);
5049         if (tv1 == tarval_bad)
5050                 return n;
5051
5052         tv2 = value_of(get_binop_right(left));
5053         if (tv2 == tarval_bad)
5054                 return n;
5055
5056         res  = tarval_add(tv1, tv2);
5057         mode = get_irn_mode(n);
5058
5059         /* beware: a simple replacement works only, if res < modulo shift */
5060         if (!is_Rotl(n)) {
5061                 int modulo_shf = get_mode_modulo_shift(mode);
5062                 if (modulo_shf > 0) {
5063                         tarval *modulo = new_tarval_from_long(modulo_shf,
5064                                                               get_tarval_mode(res));
5065
5066                         assert(modulo_shf >= (int) get_mode_size_bits(mode));
5067
5068                         /* shifting too much */
5069                         if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5070                                 if (is_Shrs(n)) {
5071                                         ir_graph *irg   = get_irn_irg(n);
5072                                         ir_node  *block = get_nodes_block(n);
5073                                         dbg_info *dbgi  = get_irn_dbg_info(n);
5074                                         ir_mode  *smode  = get_irn_mode(right);
5075                                         ir_node  *cnst  = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5076                                         return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
5077                                                            cnst, mode);
5078                                 }
5079
5080                                 return new_Const(get_mode_null(mode));
5081                         }
5082                 }
5083         } else {
5084                 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5085         }
5086
5087         /* ok, we can replace it */
5088         block = get_nodes_block(n);
5089
5090         in[0] = get_binop_left(left);
5091         in[1] = new_Const(res);
5092
5093         irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5094
5095         DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5096
5097         return transform_node(irn);
5098 }  /* transform_node_shift */
5099
5100 /**
5101  * normalisation: (x & c1) >> c2   to   (x >> c2) & (c1 >> c2)
5102  *  (we can use:
5103  *    - and, or, xor          instead of &
5104  *    - Shl, Shr, Shrs, rotl  instead of >>
5105  *    (with a special case for Or/Xor + Shrs)
5106  */
5107 static ir_node *transform_node_bitop_shift(ir_node *n) {
5108         ir_node  *left;
5109         ir_node  *right = get_binop_right(n);
5110         ir_mode  *mode  = get_irn_mode(n);
5111         ir_node  *bitop_left;
5112         ir_node  *bitop_right;
5113         ir_op    *op_left;
5114         ir_graph *irg;
5115         ir_node  *block;
5116         dbg_info *dbgi;
5117         ir_node  *new_shift;
5118         ir_node  *new_bitop;
5119         ir_node  *new_const;
5120         tarval   *tv1;
5121         tarval   *tv2;
5122         tarval   *tv_shift;
5123
5124         assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5125
5126         if (!is_Const(right))
5127                 return n;
5128
5129         left    = get_binop_left(n);
5130         op_left = get_irn_op(left);
5131         if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5132                 return n;
5133
5134         /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5135         if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5136                 /* TODO: test if sign bit is affectes */
5137                 return n;
5138         }
5139
5140         bitop_right = get_binop_right(left);
5141         if (!is_Const(bitop_right))
5142                 return n;
5143
5144         bitop_left = get_binop_left(left);
5145
5146         irg   = get_irn_irg(n);
5147         block = get_nodes_block(n);
5148         dbgi  = get_irn_dbg_info(n);
5149         tv1   = get_Const_tarval(bitop_right);
5150         tv2   = get_Const_tarval(right);
5151
5152         assert(get_tarval_mode(tv1) == mode);
5153
5154         if (is_Shl(n)) {
5155                 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5156                 tv_shift  = tarval_shl(tv1, tv2);
5157         } else if(is_Shr(n)) {
5158                 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5159                 tv_shift  = tarval_shr(tv1, tv2);
5160         } else if(is_Shrs(n)) {
5161                 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5162                 tv_shift  = tarval_shrs(tv1, tv2);
5163         } else {
5164                 assert(is_Rotl(n));
5165                 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5166                 tv_shift  = tarval_rotl(tv1, tv2);
5167         }
5168
5169         assert(get_tarval_mode(tv_shift) == mode);
5170         new_const = new_Const(tv_shift);
5171
5172         if (op_left == op_And) {
5173                 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5174         } else if(op_left == op_Or) {
5175                 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5176         } else {
5177                 assert(op_left == op_Eor);
5178                 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5179         }
5180
5181         return new_bitop;
5182 }
5183
5184 /**
5185  * normalisation:
5186  *    (x << c1) >> c2  <=>  x OP (c2-c1) & ((-1 << c1) >> c2)
5187  *    also:
5188  *    (x >> c1) << c2  <=>  x OP (c2-c1) & ((-1 >> c1) << c2)
5189  *      (also with x >>s c1  when c1>=c2)
5190  */
5191 static ir_node *transform_node_shl_shr(ir_node *n) {
5192         ir_node  *left;
5193         ir_node  *right = get_binop_right(n);
5194         ir_node  *x;
5195         ir_graph *irg;
5196         ir_node  *block;
5197         ir_mode  *mode;
5198         dbg_info *dbgi;
5199         ir_node  *new_const;
5200         ir_node  *new_shift;
5201         ir_node  *new_and;
5202         tarval   *tv_shl;
5203         tarval   *tv_shr;
5204         tarval   *tv_shift;
5205         tarval   *tv_mask;
5206         pn_Cmp    pnc;
5207         int       need_shrs = 0;
5208
5209         assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5210
5211         if (!is_Const(right))
5212                 return n;
5213
5214         left = get_binop_left(n);
5215         mode = get_irn_mode(n);
5216         if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5217                 ir_node *shr_right = get_binop_right(left);
5218
5219                 if (!is_Const(shr_right))
5220                         return n;
5221
5222                 x      = get_binop_left(left);
5223                 tv_shr = get_Const_tarval(shr_right);
5224                 tv_shl = get_Const_tarval(right);
5225
5226                 if (is_Shrs(left)) {
5227                         /* shrs variant only allowed if c1 >= c2 */
5228                         if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5229                                 return n;
5230
5231                         tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5232                         need_shrs = 1;
5233                 } else {
5234                         tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5235                 }
5236                 tv_mask = tarval_shl(tv_mask, tv_shl);
5237         } else if(is_Shr(n) && is_Shl(left)) {
5238                 ir_node *shl_right = get_Shl_right(left);
5239
5240                 if (!is_Const(shl_right))
5241                         return n;
5242
5243                 x      = get_Shl_left(left);
5244                 tv_shr = get_Const_tarval(right);
5245                 tv_shl = get_Const_tarval(shl_right);
5246
5247                 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5248                 tv_mask = tarval_shr(tv_mask, tv_shr);
5249         } else {
5250                 return n;
5251         }
5252
5253         if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5254                 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5255         }
5256
5257         assert(tv_mask != tarval_bad);
5258         assert(get_tarval_mode(tv_mask) == mode);
5259
5260         irg   = get_irn_irg(n);
5261         block = get_nodes_block(n);
5262         dbgi  = get_irn_dbg_info(n);
5263
5264         pnc = tarval_cmp(tv_shl, tv_shr);
5265         if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5266                 tv_shift  = tarval_sub(tv_shr, tv_shl, NULL);
5267                 new_const = new_Const(tv_shift);
5268                 if (need_shrs) {
5269                         new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5270                 } else {
5271                         new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5272                 }
5273         } else {
5274                 assert(pnc == pn_Cmp_Gt);
5275                 tv_shift  = tarval_sub(tv_shl, tv_shr, NULL);
5276                 new_const = new_Const(tv_shift);
5277                 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5278         }
5279
5280         new_const = new_Const(tv_mask);
5281         new_and   = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5282
5283         return new_and;
5284 }
5285
5286 /**
5287  * Transform a Shr.
5288  */
5289 static ir_node *transform_node_Shr(ir_node *n) {
5290         ir_node *c, *oldn = n;
5291         ir_node *left  = get_Shr_left(n);
5292         ir_node *right = get_Shr_right(n);
5293         ir_mode *mode  = get_irn_mode(n);
5294
5295         HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5296         n = transform_node_shift(n);
5297
5298         if (is_Shr(n))
5299                 n = transform_node_shl_shr(n);
5300         if (is_Shr(n))
5301                 n = transform_node_bitop_shift(n);
5302
5303         return n;
5304 }  /* transform_node_Shr */
5305
5306 /**
5307  * Transform a Shrs.
5308  */
5309 static ir_node *transform_node_Shrs(ir_node *n) {
5310         ir_node *c, *oldn = n;
5311         ir_node *a    = get_Shrs_left(n);
5312         ir_node *b    = get_Shrs_right(n);
5313         ir_mode *mode = get_irn_mode(n);
5314
5315         HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5316         n = transform_node_shift(n);
5317
5318         if (is_Shrs(n))
5319                 n = transform_node_bitop_shift(n);
5320
5321         return n;
5322 }  /* transform_node_Shrs */
5323
5324 /**
5325  * Transform a Shl.
5326  */
5327 static ir_node *transform_node_Shl(ir_node *n) {
5328         ir_node *c, *oldn = n;
5329         ir_node *a    = get_Shl_left(n);
5330         ir_node *b    = get_Shl_right(n);
5331         ir_mode *mode = get_irn_mode(n);
5332
5333         HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5334         n = transform_node_shift(n);
5335
5336         if (is_Shl(n))
5337                 n = transform_node_shl_shr(n);
5338         if (is_Shl(n))
5339                 n = transform_node_bitop_shift(n);
5340
5341         return n;
5342 }  /* transform_node_Shl */
5343
5344 /**
5345  * Transform a Rotl.
5346  */
5347 static ir_node *transform_node_Rotl(ir_node *n) {
5348         ir_node *c, *oldn = n;
5349         ir_node *a    = get_Rotl_left(n);
5350         ir_node *b    = get_Rotl_right(n);
5351         ir_mode *mode = get_irn_mode(n);
5352
5353         HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5354         n = transform_node_shift(n);
5355
5356         if (is_Rotl(n))
5357                 n = transform_node_bitop_shift(n);
5358
5359         return n;
5360 }  /* transform_node_Rotl */
5361
5362 /**
5363  * Transform a Conv.
5364  */
5365 static ir_node *transform_node_Conv(ir_node *n) {
5366         ir_node *c, *oldn = n;
5367         ir_mode *mode = get_irn_mode(n);
5368         ir_node *a    = get_Conv_op(n);
5369
5370         if (mode != mode_b && is_const_Phi(a)) {
5371                 /* Do NOT optimize mode_b Conv's, this leads to remaining
5372                  * Phib nodes later, because the conv_b_lower operation
5373                  * is instantly reverted, when it tries to insert a Convb.
5374                  */
5375                 c = apply_conv_on_phi(a, mode);
5376                 if (c) {
5377                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5378                         return c;
5379                 }
5380         }
5381
5382         if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5383                 return new_r_Unknown(current_ir_graph, mode);
5384         }
5385
5386         if (mode_is_reference(mode) &&
5387                 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5388                 is_Add(a)) {
5389                 ir_node *l = get_Add_left(a);
5390                 ir_node *r = get_Add_right(a);
5391                 dbg_info *dbgi = get_irn_dbg_info(a);
5392                 ir_node *block = get_nodes_block(n);
5393                 if(is_Conv(l)) {
5394                         ir_node *lop = get_Conv_op(l);
5395                         if(get_irn_mode(lop) == mode) {
5396                                 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5397                                 n = new_rd_Add(dbgi, current_ir_graph, block, lop, r, mode);
5398                                 return n;
5399                         }
5400                 }
5401                 if(is_Conv(r)) {
5402                         ir_node *rop = get_Conv_op(r);
5403                         if(get_irn_mode(rop) == mode) {
5404                                 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5405                                 n = new_rd_Add(dbgi, current_ir_graph, block, l, rop, mode);
5406                                 return n;
5407                         }
5408                 }
5409         }
5410
5411         return n;
5412 }  /* transform_node_Conv */
5413
5414 /**
5415  * Remove dead blocks and nodes in dead blocks
5416  * in keep alive list.  We do not generate a new End node.
5417  */
5418 static ir_node *transform_node_End(ir_node *n) {
5419         int i, j, n_keepalives = get_End_n_keepalives(n);
5420         ir_node **in;
5421
5422         NEW_ARR_A(ir_node *, in, n_keepalives);
5423
5424         for (i = j = 0; i < n_keepalives; ++i) {
5425                 ir_node *ka = get_End_keepalive(n, i);
5426                 if (is_Block(ka)) {
5427                         if (! is_Block_dead(ka)) {
5428                                 in[j++] = ka;
5429                         }
5430                         continue;
5431                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5432                         continue;
5433                 } else if (is_Bad(ka)) {
5434                         /* no need to keep Bad */
5435                         continue;
5436                 }
5437                 in[j++] = ka;
5438         }
5439         if (j != n_keepalives)
5440                 set_End_keepalives(n, j, in);
5441         return n;
5442 }  /* transform_node_End */
5443
5444 /** returns 1 if a == -b */
5445 static int is_negated_value(ir_node *a, ir_node *b) {
5446         if (is_Minus(a) && get_Minus_op(a) == b)
5447                 return 1;
5448         if (is_Minus(b) && get_Minus_op(b) == a)
5449                 return 1;
5450         if (is_Sub(a) && is_Sub(b)) {
5451                 ir_node *a_left  = get_Sub_left(a);
5452                 ir_node *a_right = get_Sub_right(a);
5453                 ir_node *b_left  = get_Sub_left(b);
5454                 ir_node *b_right = get_Sub_right(b);
5455
5456                 if (a_left == b_right && a_right == b_left)
5457                         return 1;
5458         }
5459
5460         return 0;
5461 }
5462
5463 /**
5464  * Optimize a Mux into some simpler cases.
5465  */
5466 static ir_node *transform_node_Mux(ir_node *n) {
5467         ir_node *oldn = n, *sel = get_Mux_sel(n);
5468         ir_mode *mode = get_irn_mode(n);
5469         ir_node  *t   = get_Mux_true(n);
5470         ir_node  *f   = get_Mux_false(n);
5471         ir_graph *irg = current_ir_graph;
5472
5473         /* first normalization step: move a possible zero to the false case */
5474         if (is_Proj(sel)) {
5475                 ir_node *cmp = get_Proj_pred(sel);
5476
5477                 if (is_Cmp(cmp)) {
5478                         if (is_Const(t) && is_Const_null(t)) {
5479                                 ir_node *tmp;
5480
5481                                 /* Mux(x, 0, y) => Mux(x, y, 0) */
5482                                 pn_Cmp pnc = get_Proj_proj(sel);
5483                                 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5484                                         get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5485                                 n        = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5486                                 tmp = t;
5487                                 t = f;
5488                                 f = tmp;
5489                         }
5490                 }
5491         }
5492
5493         /* note: after normalization, false can only happen on default */
5494         if (mode == mode_b) {
5495                 dbg_info *dbg   = get_irn_dbg_info(n);
5496                 ir_node  *block = get_nodes_block(n);
5497                 ir_graph *irg   = current_ir_graph;
5498
5499                 if (is_Const(t)) {
5500                         tarval *tv_t = get_Const_tarval(t);
5501                         if (tv_t == tarval_b_true) {
5502                                 if (is_Const(f)) {
5503                                         /* Muxb(sel, true, false) = sel */
5504                                         assert(get_Const_tarval(f) == tarval_b_false);
5505                                         DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5506                                         return sel;
5507                                 } else {
5508                                         /* Muxb(sel, true, x) = Or(sel, x) */
5509                                         n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5510                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5511                                         return n;
5512                                 }
5513                         }
5514                 } else if (is_Const(f)) {
5515                         tarval *tv_f = get_Const_tarval(f);
5516                         if (tv_f == tarval_b_true) {
5517                                 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5518                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5519                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5520                                 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5521                                 return n;
5522                         } else {
5523                                 /* Muxb(sel, x, false) = And(sel, x) */
5524                                 assert(tv_f == tarval_b_false);
5525                                 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5526                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5527                                 return n;
5528                         }
5529                 }
5530         }
5531
5532         /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5533         if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5534                 tarval *a = get_Const_tarval(t);
5535                 tarval *b = get_Const_tarval(f);
5536                 tarval *null = get_tarval_null(mode);
5537                 tarval *diff, *min;
5538
5539                 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5540                         diff = tarval_sub(a, b, NULL);
5541                         min  = b;
5542                 } else {
5543                         diff = tarval_sub(b, a, NULL);
5544                         min  = a;
5545                 }
5546
5547                 if (diff == get_tarval_one(mode) && min != null) {
5548                         dbg_info *dbg   = get_irn_dbg_info(n);
5549                         ir_node  *block = get_nodes_block(n);
5550                         ir_graph *irg   = current_ir_graph;
5551                         ir_node  *t     = new_Const(tarval_sub(a, min, NULL));
5552                         ir_node  *f     = new_Const(tarval_sub(b, min, NULL));
5553                         n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5554                         n = new_rd_Add(dbg, irg, block, n, new_Const(min), mode);
5555                         return n;
5556                 }
5557         }
5558
5559         if (is_Proj(sel)) {
5560                 ir_node *cmp = get_Proj_pred(sel);
5561                 long     pn  = get_Proj_proj(sel);
5562
5563                 /*
5564                  * Note: normalization puts the constant on the right side,
5565                  * so we check only one case.
5566                  *
5567                  * Note further that these optimization work even for floating point
5568                  * with NaN's because -NaN == NaN.
5569                  * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5570                  * transformations.
5571                  */
5572                 if (is_Cmp(cmp)) {
5573                         ir_node *cmp_r = get_Cmp_right(cmp);
5574                         if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5575                                 ir_node *block = get_nodes_block(n);
5576                                 ir_node *cmp_l = get_Cmp_left(cmp);
5577
5578                                 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5579                                         /* f = -t */
5580
5581                                         if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5582                                                 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5583                                         {
5584                                                 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5585                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5586                                                                                  cmp_l, mode);
5587                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5588                                                 return n;
5589                                         } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5590                                                 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5591                                         {
5592                                                 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5593                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5594                                                                                  cmp_l, mode);
5595                                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5596                                                                                                                  block, n, mode);
5597                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5598                                                 return n;
5599                                         }
5600                                 }
5601
5602                                 if (mode_is_int(mode)) {
5603                                         /* integer only */
5604                                         if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5605                                                 /* Mux((a & b) != 0, c, 0) */
5606                                                 ir_node *and_r = get_And_right(cmp_l);
5607                                                 ir_node *and_l;
5608
5609                                                 if (and_r == t && f == cmp_r) {
5610                                                         if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5611                                                                 if (pn == pn_Cmp_Lg) {
5612                                                                         /* Mux((a & 2^C) != 0, 2^C, 0) */
5613                                                                         n = cmp_l;
5614                                                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5615                                                                 } else {
5616                                                                         /* Mux((a & 2^C) == 0, 2^C, 0) */
5617                                                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5618                                                                                 block, cmp_l, t, mode);
5619                                                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5620                                                                 }
5621                                                                 return n;
5622                                                         }
5623                                                 }
5624                                                 if (is_Shl(and_r)) {
5625                                                         ir_node *shl_l = get_Shl_left(and_r);
5626                                                         if (is_Const(shl_l) && is_Const_one(shl_l)) {
5627                                                                 if (and_r == t && f == cmp_r) {
5628                                                                         if (pn == pn_Cmp_Lg) {
5629                                                                                 /* (a & (1 << n)) != 0, (1 << n), 0) */
5630                                                                                 n = cmp_l;
5631                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5632                                                                         } else {
5633                                                                                 /* (a & (1 << n)) == 0, (1 << n), 0) */
5634                                                                                 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5635                                                                                         block, cmp_l, t, mode);
5636                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5637                                                                         }
5638                                                                         return n;
5639                                                                 }
5640                                                         }
5641                                                 }
5642                                                 and_l = get_And_left(cmp_l);
5643                                                 if (is_Shl(and_l)) {
5644                                                         ir_node *shl_l = get_Shl_left(and_l);
5645                                                         if (is_Const(shl_l) && is_Const_one(shl_l)) {
5646                                                                 if (and_l == t && f == cmp_r) {
5647                                                                         if (pn == pn_Cmp_Lg) {
5648                                                                                 /* ((1 << n) & a) != 0, (1 << n), 0) */
5649                                                                                 n = cmp_l;
5650                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5651                                                                         } else {
5652                                                                                 /* ((1 << n) & a) == 0, (1 << n), 0) */
5653                                                                                 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5654                                                                                         block, cmp_l, t, mode);
5655                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5656                                                                         }
5657                                                                         return n;
5658                                                                 }
5659                                                         }
5660                                                 }
5661                                         }
5662                                 }
5663                         }
5664                 }
5665         }
5666
5667         return n;
5668 }  /* transform_node_Mux */
5669
5670 /**
5671  * optimize Sync nodes that have other syncs as input we simply add the inputs
5672  * of the other sync to our own inputs
5673  */
5674 static ir_node *transform_node_Sync(ir_node *n) {
5675         int arity = get_Sync_n_preds(n);
5676         int i;
5677
5678         for (i = 0; i < arity;) {
5679                 ir_node *pred = get_Sync_pred(n, i);
5680                 int      pred_arity;
5681                 int      j;
5682
5683                 if (!is_Sync(pred)) {
5684                         ++i;
5685                         continue;
5686                 }
5687
5688                 del_Sync_n(n, i);
5689                 --arity;
5690
5691                 pred_arity = get_Sync_n_preds(pred);
5692                 for (j = 0; j < pred_arity; ++j) {
5693                         ir_node *pred_pred = get_Sync_pred(pred, j);
5694                         int      k;
5695
5696                         for (k = 0;; ++k) {
5697                                 if (k >= arity) {
5698                                         add_irn_n(n, pred_pred);
5699                                         ++arity;
5700                                         break;
5701                                 }
5702                                 if (get_Sync_pred(n, k) == pred_pred) break;
5703                         }
5704                 }
5705         }
5706
5707         /* rehash the sync node */
5708         add_identities(current_ir_graph->value_table, n);
5709
5710         return n;
5711 }  /* transform_node_Sync */
5712
5713 /**
5714  * optimize a trampoline Call into a direct Call
5715  */
5716 static ir_node *transform_node_Call(ir_node *call) {
5717         ir_node  *callee = get_Call_ptr(call);
5718         ir_node  *adr, *mem, *res, *bl, **in;
5719         ir_type  *ctp, *mtp, *tp;
5720         ident    *id;
5721         dbg_info *db;
5722         int      i, n_res, n_param;
5723         ir_variadicity var;
5724
5725         if (! is_Proj(callee))
5726                 return call;
5727         callee = get_Proj_pred(callee);
5728         if (! is_Builtin(callee))
5729                 return call;
5730         if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5731                 return call;
5732
5733         mem = get_Call_mem(call);
5734
5735         if (skip_Proj(mem) == callee) {
5736                 /* memory is routed to the trampoline, skip */
5737                 mem = get_Builtin_mem(callee);
5738         }
5739
5740         /* build a new call type */
5741         mtp = get_Call_type(call);
5742         id  = get_type_ident(mtp);
5743         id  = id_mangle(new_id_from_chars("T_", 2), id);
5744         db  = get_type_dbg_info(mtp);
5745
5746         n_res   = get_method_n_ress(mtp);
5747         n_param = get_method_n_params(mtp);
5748         ctp     = new_d_type_method(id, n_param + 1, n_res, db);
5749
5750         for (i = 0; i < n_res; ++i)
5751                 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5752
5753         NEW_ARR_A(ir_node *, in, n_param + 1);
5754
5755         /* FIXME: we don't need a new pointer type in every step */
5756         tp = get_irg_frame_type(current_ir_graph);
5757         id = id_mangle(get_type_ident(tp), new_id_from_chars("_ptr", 4));
5758         tp = new_type_pointer(id, tp, mode_P_data);
5759         set_method_param_type(ctp, 0, tp);
5760
5761         in[0] = get_Builtin_param(callee, 2);
5762         for (i = 0; i < n_param; ++i) {
5763                 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5764                 in[i + 1] = get_Call_param(call, i);
5765         }
5766         var = get_method_variadicity(mtp);
5767         set_method_variadicity(ctp, var);
5768         if (var == variadicity_variadic) {
5769                 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
5770         }
5771         set_method_calling_convention(ctp, get_method_calling_convention(mtp));
5772         set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5773
5774         adr = get_Builtin_param(callee, 1);
5775
5776         db  = get_irn_dbg_info(call);
5777         bl  = get_nodes_block(call);
5778
5779         res = new_rd_Call(db, current_ir_graph, bl, mem, adr, n_param + 1, in, ctp);
5780         if (get_irn_pinned(call) == op_pin_state_floats)
5781                 set_irn_pinned(res, op_pin_state_floats);
5782         return res;
5783 }  /* transform_node_Call */
5784
5785 /**
5786  * Tries several [inplace] [optimizing] transformations and returns an
5787  * equivalent node.  The difference to equivalent_node() is that these
5788  * transformations _do_ generate new nodes, and thus the old node must
5789  * not be freed even if the equivalent node isn't the old one.
5790  */
5791 static ir_node *transform_node(ir_node *n) {
5792         ir_node *oldn;
5793
5794         /*
5795          * Transform_node is the only "optimizing transformation" that might
5796          * return a node with a different opcode. We iterate HERE until fixpoint
5797          * to get the final result.
5798          */
5799         do {
5800                 oldn = n;
5801                 if (n->op->ops.transform_node)
5802                         n = n->op->ops.transform_node(n);
5803         } while (oldn != n);
5804
5805         return n;
5806 }  /* transform_node */
5807
5808 /**
5809  * Sets the default transform node operation for an ir_op_ops.
5810  *
5811  * @param code   the opcode for the default operation
5812  * @param ops    the operations initialized
5813  *
5814  * @return
5815  *    The operations.
5816  */
5817 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5818 {
5819 #define CASE(a)                                         \
5820         case iro_##a:                                       \
5821                 ops->transform_node      = transform_node_##a;  \
5822                 break
5823 #define CASE_PROJ(a)                                         \
5824         case iro_##a:                                            \
5825                 ops->transform_node_Proj = transform_node_Proj_##a;  \
5826                 break
5827 #define CASE_PROJ_EX(a)                                      \
5828         case iro_##a:                                            \
5829                 ops->transform_node      = transform_node_##a;       \
5830                 ops->transform_node_Proj = transform_node_Proj_##a;  \
5831                 break
5832
5833         switch (code) {
5834         CASE(Add);
5835         CASE(Sub);
5836         CASE(Mul);
5837         CASE_PROJ_EX(Div);
5838         CASE_PROJ_EX(Mod);
5839         CASE_PROJ_EX(DivMod);
5840         CASE(Quot);
5841         CASE(Abs);
5842         CASE_PROJ_EX(Cmp);
5843         CASE_PROJ_EX(Cond);
5844         CASE(And);
5845         CASE(Eor);
5846         CASE(Not);
5847         CASE(Minus);
5848         CASE(Cast);
5849         CASE_PROJ(Load);
5850         CASE_PROJ(Store);
5851         CASE_PROJ(Bound);
5852         CASE_PROJ(CopyB);
5853         CASE(Proj);
5854         CASE(Phi);
5855         CASE(Or);
5856         CASE(Sel);
5857         CASE(Shr);
5858         CASE(Shrs);
5859         CASE(Shl);
5860         CASE(Rotl);
5861         CASE(Conv);
5862         CASE(End);
5863         CASE(Mux);
5864         CASE(Sync);
5865         CASE(Call);
5866         default:
5867           /* leave NULL */;
5868         }
5869
5870         return ops;
5871 #undef CASE_PROJ_EX
5872 #undef CASE_PROJ
5873 #undef CASE
5874 }  /* firm_set_default_transform_node */
5875
5876
5877 /* **************** Common Subexpression Elimination **************** */
5878
5879 /** The size of the hash table used, should estimate the number of nodes
5880     in a graph. */
5881 #define N_IR_NODES 512
5882
5883 /** Compares the attributes of two Const nodes. */
5884 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5885         return (get_Const_tarval(a) != get_Const_tarval(b))
5886             || (get_Const_type(a) != get_Const_type(b));
5887 }  /* node_cmp_attr_Const */
5888
5889 /** Compares the attributes of two Proj nodes. */
5890 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5891         return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5892 }  /* node_cmp_attr_Proj */
5893
5894 /** Compares the attributes of two Filter nodes. */
5895 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5896         return get_Filter_proj(a) != get_Filter_proj(b);
5897 }  /* node_cmp_attr_Filter */
5898
5899 /** Compares the attributes of two Alloc nodes. */
5900 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5901         const alloc_attr *pa = get_irn_alloc_attr(a);
5902         const alloc_attr *pb = get_irn_alloc_attr(b);
5903         return (pa->where != pb->where) || (pa->type != pb->type);
5904 }  /* node_cmp_attr_Alloc */
5905
5906 /** Compares the attributes of two Free nodes. */
5907 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5908         const free_attr *pa = get_irn_free_attr(a);
5909         const free_attr *pb = get_irn_free_attr(b);
5910         return (pa->where != pb->where) || (pa->type != pb->type);
5911 }  /* node_cmp_attr_Free */
5912
5913 /** Compares the attributes of two SymConst nodes. */
5914 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5915         const symconst_attr *pa = get_irn_symconst_attr(a);
5916         const symconst_attr *pb = get_irn_symconst_attr(b);
5917         return (pa->kind       != pb->kind)
5918             || (pa->sym.type_p != pb->sym.type_p)
5919             || (pa->tp         != pb->tp);
5920 }  /* node_cmp_attr_SymConst */
5921
5922 /** Compares the attributes of two Call nodes. */
5923 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5924         return get_irn_call_attr(a) != get_irn_call_attr(b);
5925 }  /* node_cmp_attr_Call */
5926
5927 /** Compares the attributes of two Sel nodes. */
5928 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5929         const ir_entity *a_ent = get_Sel_entity(a);
5930         const ir_entity *b_ent = get_Sel_entity(b);
5931 #if 0
5932         return
5933                 (a_ent->kind    != b_ent->kind)    ||
5934                 (a_ent->name    != b_ent->name)    ||
5935                 (a_ent->owner   != b_ent->owner)   ||
5936                 (a_ent->ld_name != b_ent->ld_name) ||
5937                 (a_ent->type    != b_ent->type);
5938 #endif
5939         /* Matze: inlining of functions can produce 2 entities with same type,
5940          * name, etc. */
5941         return a_ent != b_ent;
5942 }  /* node_cmp_attr_Sel */
5943
5944 /** Compares the attributes of two Phi nodes. */
5945 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5946         /* we can only enter this function if both nodes have the same number of inputs,
5947            hence it is enough to check if one of them is a Phi0 */
5948         if (is_Phi0(a)) {
5949                 /* check the Phi0 pos attribute */
5950                 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5951         }
5952         return 0;
5953 }  /* node_cmp_attr_Phi */
5954
5955 /** Compares the attributes of two Conv nodes. */
5956 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5957         return get_Conv_strict(a) != get_Conv_strict(b);
5958 }  /* node_cmp_attr_Conv */
5959
5960 /** Compares the attributes of two Cast nodes. */
5961 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5962         return get_Cast_type(a) != get_Cast_type(b);
5963 }  /* node_cmp_attr_Cast */
5964
5965 /** Compares the attributes of two Load nodes. */
5966 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5967         if (get_Load_volatility(a) == volatility_is_volatile ||
5968             get_Load_volatility(b) == volatility_is_volatile)
5969                 /* NEVER do CSE on volatile Loads */
5970                 return 1;
5971         /* do not CSE Loads with different alignment. Be conservative. */
5972         if (get_Load_align(a) != get_Load_align(b))
5973                 return 1;
5974
5975         return get_Load_mode(a) != get_Load_mode(b);
5976 }  /* node_cmp_attr_Load */
5977
5978 /** Compares the attributes of two Store nodes. */
5979 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5980         /* do not CSE Stores with different alignment. Be conservative. */
5981         if (get_Store_align(a) != get_Store_align(b))
5982                 return 1;
5983
5984         /* NEVER do CSE on volatile Stores */
5985         return (get_Store_volatility(a) == volatility_is_volatile ||
5986                 get_Store_volatility(b) == volatility_is_volatile);
5987 }  /* node_cmp_attr_Store */
5988
5989 /** Compares two exception attributes */
5990 static int node_cmp_exception(ir_node *a, ir_node *b) {
5991         const except_attr *ea = get_irn_except_attr(a);
5992         const except_attr *eb = get_irn_except_attr(b);
5993
5994         return ea->pin_state != eb->pin_state;
5995 }
5996
5997 #define node_cmp_attr_Bound  node_cmp_exception
5998
5999 /** Compares the attributes of two Div nodes. */
6000 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
6001         const divmod_attr *ma = get_irn_divmod_attr(a);
6002         const divmod_attr *mb = get_irn_divmod_attr(b);
6003         return ma->exc.pin_state != mb->exc.pin_state ||
6004                    ma->res_mode      != mb->res_mode ||
6005                    ma->no_remainder  != mb->no_remainder;
6006 }  /* node_cmp_attr_Div */
6007
6008 /** Compares the attributes of two DivMod nodes. */
6009 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
6010         const divmod_attr *ma = get_irn_divmod_attr(a);
6011         const divmod_attr *mb = get_irn_divmod_attr(b);
6012         return ma->exc.pin_state != mb->exc.pin_state ||
6013                    ma->res_mode      != mb->res_mode;
6014 }  /* node_cmp_attr_DivMod */
6015
6016 /** Compares the attributes of two Mod nodes. */
6017 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
6018         const divmod_attr *ma = get_irn_divmod_attr(a);
6019         const divmod_attr *mb = get_irn_divmod_attr(b);
6020         return ma->exc.pin_state != mb->exc.pin_state ||
6021                    ma->res_mode      != mb->res_mode;
6022 }  /* node_cmp_attr_Mod */
6023
6024 /** Compares the attributes of two Quot nodes. */
6025 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
6026         const divmod_attr *ma = get_irn_divmod_attr(a);
6027         const divmod_attr *mb = get_irn_divmod_attr(b);
6028         return ma->exc.pin_state != mb->exc.pin_state ||
6029                    ma->res_mode      != mb->res_mode;
6030 }  /* node_cmp_attr_Quot */
6031
6032 /** Compares the attributes of two Confirm nodes. */
6033 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
6034         /* no need to compare the bound, as this is a input */
6035         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6036 }  /* node_cmp_attr_Confirm */
6037
6038 /** Compares the attributes of two Builtin nodes. */
6039 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b) {
6040         const builtin_attr *ma = get_irn_builtin_attr(a);
6041         const builtin_attr *mb = get_irn_builtin_attr(b);
6042
6043         /* no need to compare the type, equal kind means equal type */
6044         return ma->kind != mb->kind;
6045 }  /* node_cmp_attr_Builtin */
6046
6047 /** Compares the attributes of two ASM nodes. */
6048 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
6049         int i, n;
6050         const ir_asm_constraint *ca;
6051         const ir_asm_constraint *cb;
6052         ident **cla, **clb;
6053
6054         if (get_ASM_text(a) != get_ASM_text(b))
6055                 return 1;
6056
6057         /* Should we really check the constraints here? Should be better, but is strange. */
6058         n = get_ASM_n_input_constraints(a);
6059         if (n != get_ASM_n_input_constraints(b))
6060                 return 0;
6061
6062         ca = get_ASM_input_constraints(a);
6063         cb = get_ASM_input_constraints(b);
6064         for (i = 0; i < n; ++i) {
6065                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6066                         return 1;
6067         }
6068
6069         n = get_ASM_n_output_constraints(a);
6070         if (n != get_ASM_n_output_constraints(b))
6071                 return 0;
6072
6073         ca = get_ASM_output_constraints(a);
6074         cb = get_ASM_output_constraints(b);
6075         for (i = 0; i < n; ++i) {
6076                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6077                         return 1;
6078         }
6079
6080         n = get_ASM_n_clobbers(a);
6081         if (n != get_ASM_n_clobbers(b))
6082                 return 0;
6083
6084         cla = get_ASM_clobbers(a);
6085         clb = get_ASM_clobbers(b);
6086         for (i = 0; i < n; ++i) {
6087                 if (cla[i] != clb[i])
6088                         return 1;
6089         }
6090         return 0;
6091 }  /* node_cmp_attr_ASM */
6092
6093 /**
6094  * Set the default node attribute compare operation for an ir_op_ops.
6095  *
6096  * @param code   the opcode for the default operation
6097  * @param ops    the operations initialized
6098  *
6099  * @return
6100  *    The operations.
6101  */
6102 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6103 {
6104 #define CASE(a)                              \
6105         case iro_##a:                              \
6106                 ops->node_cmp_attr  = node_cmp_attr_##a; \
6107                 break
6108
6109         switch (code) {
6110         CASE(Const);
6111         CASE(Proj);
6112         CASE(Filter);
6113         CASE(Alloc);
6114         CASE(Free);
6115         CASE(SymConst);
6116         CASE(Call);
6117         CASE(Sel);
6118         CASE(Phi);
6119         CASE(Conv);
6120         CASE(Cast);
6121         CASE(Load);
6122         CASE(Store);
6123         CASE(Confirm);
6124         CASE(ASM);
6125         CASE(Div);
6126         CASE(DivMod);
6127         CASE(Mod);
6128         CASE(Quot);
6129         CASE(Bound);
6130         CASE(Builtin);
6131         /* FIXME CopyB */
6132         default:
6133           /* leave NULL */;
6134         }
6135
6136         return ops;
6137 #undef CASE
6138 }  /* firm_set_default_node_cmp_attr */
6139
6140 /*
6141  * Compare function for two nodes in the value table. Gets two
6142  * nodes as parameters.  Returns 0 if the nodes are a Common Sub Expression.
6143  */
6144 int identities_cmp(const void *elt, const void *key) {
6145         ir_node *a = (ir_node *)elt;
6146         ir_node *b = (ir_node *)key;
6147         int i, irn_arity_a;
6148
6149         if (a == b) return 0;
6150
6151         if ((get_irn_op(a) != get_irn_op(b)) ||
6152             (get_irn_mode(a) != get_irn_mode(b))) return 1;
6153
6154         /* compare if a's in and b's in are of equal length */
6155         irn_arity_a = get_irn_intra_arity(a);
6156         if (irn_arity_a != get_irn_intra_arity(b))
6157                 return 1;
6158
6159         if (get_irn_pinned(a) == op_pin_state_pinned) {
6160                 /* for pinned nodes, the block inputs must be equal */
6161                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6162                         return 1;
6163         } else if (! get_opt_global_cse()) {
6164                 /* for block-local CSE both nodes must be in the same MacroBlock */
6165                 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6166                         return 1;
6167         }
6168
6169         /* compare a->in[0..ins] with b->in[0..ins] */
6170         for (i = 0; i < irn_arity_a; i++)
6171                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
6172                         return 1;
6173
6174         /*
6175          * here, we already now that the nodes are identical except their
6176          * attributes
6177          */
6178         if (a->op->ops.node_cmp_attr)
6179                 return a->op->ops.node_cmp_attr(a, b);
6180
6181         return 0;
6182 }  /* identities_cmp */
6183
6184 /*
6185  * Calculate a hash value of a node.
6186  *
6187  * @param node  The IR-node
6188  */
6189 unsigned ir_node_hash(const ir_node *node) {
6190         return node->op->ops.hash(node);
6191 }  /* ir_node_hash */
6192
6193
6194 pset *new_identities(void) {
6195         return new_pset(identities_cmp, N_IR_NODES);
6196 }  /* new_identities */
6197
6198 void del_identities(pset *value_table) {
6199         del_pset(value_table);
6200 }  /* del_identities */
6201
6202 /* Normalize a node by putting constants (and operands with larger
6203  * node index) on the right (operator side). */
6204 void ir_normalize_node(ir_node *n) {
6205         if (is_op_commutative(get_irn_op(n))) {
6206                 ir_node *l = get_binop_left(n);
6207                 ir_node *r = get_binop_right(n);
6208
6209                 /* For commutative operators perform  a OP b == b OP a but keep
6210                  * constants on the RIGHT side. This helps greatly in some
6211                  * optimizations.  Moreover we use the idx number to make the form
6212                  * deterministic. */
6213                 if (!operands_are_normalized(l, r)) {
6214                         set_binop_left(n, r);
6215                         set_binop_right(n, l);
6216                         hook_normalize(n);
6217                 }
6218         }
6219 }  /* ir_normalize_node */
6220
6221 /**
6222  * Update the nodes after a match in the value table. If both nodes have
6223  * the same MacroBlock but different Blocks, we must ensure that the node
6224  * with the dominating Block (the node that is near to the MacroBlock header
6225  * is stored in the table.
6226  * Because a MacroBlock has only one "non-exception" flow, we don't need
6227  * dominance info here: We known, that one block must dominate the other and
6228  * following the only block input will allow to find it.
6229  */
6230 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6231         ir_node *known_blk, *new_block, *block, *mbh;
6232
6233         if (get_opt_global_cse()) {
6234                 /* Block inputs are meaning less */
6235                 return;
6236         }
6237         known_blk = get_irn_n(known_irn, -1);
6238         new_block = get_irn_n(new_ir_node, -1);
6239         if (known_blk == new_block) {
6240                 /* already in the same block */
6241                 return;
6242         }
6243         /*
6244          * We expect the typical case when we built the graph. In that case, the
6245          * known_irn is already the upper one, so checking this should be faster.
6246          */
6247         block = new_block;
6248         mbh   = get_Block_MacroBlock(new_block);
6249         for (;;) {
6250                 if (block == known_blk) {
6251                         /* ok, we have found it: known_block dominates new_block as expected */
6252                         return;
6253                 }
6254                 if (block == mbh) {
6255                         /*
6256                          * We have reached the MacroBlock header NOT founding
6257                          * the known_block. new_block must dominate known_block.
6258                          * Update known_irn.
6259                          */
6260                         set_irn_n(known_irn, -1, new_block);
6261                         return;
6262                 }
6263                 assert(get_Block_n_cfgpreds(block) == 1);
6264                 block = get_Block_cfgpred_block(block, 0);
6265         }
6266 }  /* update_value_table */
6267
6268 /*
6269  * Return the canonical node computing the same value as n.
6270  * Looks up the node in a hash table, enters it in the table
6271  * if it isn't there yet.
6272  *
6273  * @param value_table  the HashSet containing all nodes in the
6274  *                     current IR graph
6275  * @param n            the node to look up
6276  *
6277  * @return a node that computes the same value as n or n if no such
6278  *         node could be found
6279  */
6280 ir_node *identify_remember(pset *value_table, ir_node *n) {
6281         ir_node *o = NULL;
6282
6283         if (!value_table) return n;
6284
6285         ir_normalize_node(n);
6286         /* lookup or insert in hash table with given hash key. */
6287         o = pset_insert(value_table, n, ir_node_hash(n));
6288
6289         if (o != n) {
6290                 update_known_irn(o, n);
6291         }
6292
6293         return o;
6294 }  /* identify_remember */
6295
6296 /**
6297  * During construction we set the op_pin_state_pinned flag in the graph right when the
6298  * optimization is performed.  The flag turning on procedure global cse could
6299  * be changed between two allocations.  This way we are safe.
6300  *
6301  * @param value_table  The value table
6302  * @param n            The node to lookup
6303  */
6304 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6305         ir_node *old = n;
6306
6307         n = identify_remember(value_table, n);
6308         if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6309                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6310         return n;
6311 }  /* identify_cons */
6312
6313 /* Add a node to the identities value table. */
6314 void add_identities(pset *value_table, ir_node *node) {
6315         if (get_opt_cse() && is_no_Block(node))
6316                 identify_remember(value_table, node);
6317 }  /* add_identities */
6318
6319 /* Visit each node in the value table of a graph. */
6320 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6321         ir_node *node;
6322         ir_graph *rem = current_ir_graph;
6323
6324         current_ir_graph = irg;
6325         foreach_pset(irg->value_table, node)
6326                 visit(node, env);
6327         current_ir_graph = rem;
6328 }  /* visit_all_identities */
6329
6330 /**
6331  * Garbage in, garbage out. If a node has a dead input, i.e., the
6332  * Bad node is input to the node, return the Bad node.
6333  */
6334 static ir_node *gigo(ir_node *node) {
6335         int i, irn_arity;
6336         ir_op *op = get_irn_op(node);
6337
6338         /* remove garbage blocks by looking at control flow that leaves the block
6339            and replacing the control flow by Bad. */
6340         if (get_irn_mode(node) == mode_X) {
6341                 ir_node *block = get_nodes_block(skip_Proj(node));
6342
6343                 /* Don't optimize nodes in immature blocks. */
6344                 if (!get_Block_matured(block))
6345                         return node;
6346                 /* Don't optimize End, may have Bads. */
6347                 if (op == op_End) return node;
6348
6349                 if (is_Block(block)) {
6350                         if (is_Block_dead(block)) {
6351                                 /* control flow from dead block is dead */
6352                                 return new_Bad();
6353                         }
6354
6355                         for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6356                                 if (!is_Bad(get_irn_n(block, i)))
6357                                         break;
6358                         }
6359                         if (i < 0) {
6360                                 ir_graph *irg = get_irn_irg(block);
6361                                 /* the start block is never dead */
6362                                 if (block != get_irg_start_block(irg)
6363                                         && block != get_irg_end_block(irg)) {
6364                                         /*
6365                                          * Do NOT kill control flow without setting
6366                                          * the block to dead of bad things can happen:
6367                                          * We get a Block that is not reachable be irg_block_walk()
6368                                          * but can be found by irg_walk()!
6369                                          */
6370                                         set_Block_dead(block);
6371                                         return new_Bad();
6372                                 }
6373                         }
6374                 }
6375         }
6376
6377         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6378            blocks predecessors is dead. */
6379         if (op != op_Block && op != op_Phi && op != op_Tuple) {
6380                 irn_arity = get_irn_arity(node);
6381
6382                 /*
6383                  * Beware: we can only read the block of a non-floating node.
6384                  */
6385                 if (is_irn_pinned_in_irg(node) &&
6386                         is_Block_dead(get_nodes_block(skip_Proj(node))))
6387                         return new_Bad();
6388
6389                 for (i = 0; i < irn_arity; i++) {
6390                         ir_node *pred = get_irn_n(node, i);
6391
6392                         if (is_Bad(pred))
6393                                 return new_Bad();
6394 #if 0
6395                         /* Propagating Unknowns here seems to be a bad idea, because
6396                            sometimes we need a node as a input and did not want that
6397                            it kills it's user.
6398                            However, it might be useful to move this into a later phase
6399                            (if you think that optimizing such code is useful). */
6400                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6401                                 return new_Unknown(get_irn_mode(node));
6402 #endif
6403                 }
6404         }
6405 #if 0
6406         /* With this code we violate the agreement that local_optimize
6407            only leaves Bads in Block, Phi and Tuple nodes. */
6408         /* If Block has only Bads as predecessors it's garbage. */
6409         /* If Phi has only Bads as predecessors it's garbage. */
6410         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
6411                 irn_arity = get_irn_arity(node);
6412                 for (i = 0; i < irn_arity; i++) {
6413                         if (!is_Bad(get_irn_n(node, i))) break;
6414                 }
6415                 if (i == irn_arity) node = new_Bad();
6416         }
6417 #endif
6418         return node;
6419 }  /* gigo */
6420
6421 /**
6422  * These optimizations deallocate nodes from the obstack.
6423  * It can only be called if it is guaranteed that no other nodes
6424  * reference this one, i.e., right after construction of a node.
6425  *
6426  * @param n   The node to optimize
6427  *
6428  * current_ir_graph must be set to the graph of the node!
6429  */
6430 ir_node *optimize_node(ir_node *n) {
6431         tarval *tv;
6432         ir_node *oldn = n;
6433         ir_opcode iro = get_irn_opcode(n);
6434
6435         /* Always optimize Phi nodes: part of the construction. */
6436         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6437
6438         /* constant expression evaluation / constant folding */
6439         if (get_opt_constant_folding()) {
6440                 /* neither constants nor Tuple values can be evaluated */
6441                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6442                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
6443                         int old_fp_mode = tarval_fp_ops_enabled();
6444
6445                         tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6446
6447                         /* try to evaluate */
6448                         tv = computed_value(n);
6449                         if (tv != tarval_bad) {
6450                                 ir_node *nw;
6451                                 ir_type *old_tp = get_irn_type(n);
6452                                 int i, arity = get_irn_arity(n);
6453                                 int node_size;
6454
6455                                 /*
6456                                  * Try to recover the type of the new expression.
6457                                  */
6458                                 for (i = 0; i < arity && !old_tp; ++i)
6459                                         old_tp = get_irn_type(get_irn_n(n, i));
6460
6461                                 /*
6462                                  * we MUST copy the node here temporary, because it's still needed
6463                                  * for DBG_OPT_CSTEVAL
6464                                  */
6465                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
6466                                 oldn = alloca(node_size);
6467
6468                                 memcpy(oldn, n, node_size);
6469                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6470
6471                                 /* ARG, copy the in array, we need it for statistics */
6472                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6473
6474                                 /* note the inplace edges module */
6475                                 edges_node_deleted(n, current_ir_graph);
6476
6477                                 /* evaluation was successful -- replace the node. */
6478                                 irg_kill_node(current_ir_graph, n);
6479                                 nw = new_Const(tv);
6480
6481                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6482                                         set_Const_type(nw, old_tp);
6483                                 DBG_OPT_CSTEVAL(oldn, nw);
6484                                 tarval_enable_fp_ops(old_fp_mode);
6485                                 return nw;
6486                         }
6487                         tarval_enable_fp_ops(old_fp_mode);
6488                 }
6489         }
6490
6491         /* remove unnecessary nodes */
6492         if (get_opt_algebraic_simplification() ||
6493             (iro == iro_Phi)  ||   /* always optimize these nodes. */
6494             (iro == iro_Id)   ||
6495             (iro == iro_Proj) ||
6496             (iro == iro_Block)  )  /* Flags tested local. */
6497                 n = equivalent_node(n);
6498
6499         /* Common Subexpression Elimination.
6500          *
6501          * Checks whether n is already available.
6502          * The block input is used to distinguish different subexpressions. Right
6503          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6504          * subexpressions within a block.
6505          */
6506         if (get_opt_cse())
6507                 n = identify_cons(current_ir_graph->value_table, n);
6508
6509         if (n != oldn) {
6510                 edges_node_deleted(oldn, current_ir_graph);
6511
6512                 /* We found an existing, better node, so we can deallocate the old node. */
6513                 irg_kill_node(current_ir_graph, oldn);
6514                 return n;
6515         }
6516
6517         /* Some more constant expression evaluation that does not allow to
6518            free the node. */
6519         iro = get_irn_opcode(n);
6520         if (get_opt_algebraic_simplification() ||
6521             (iro == iro_Cond) ||
6522             (iro == iro_Proj))     /* Flags tested local. */
6523                 n = transform_node(n);
6524
6525         /* Remove nodes with dead (Bad) input.
6526            Run always for transformation induced Bads. */
6527         n = gigo(n);
6528
6529         /* Now we have a legal, useful node. Enter it in hash table for CSE */
6530         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6531                 ir_node *o = n;
6532                 n = identify_remember(current_ir_graph->value_table, o);
6533                 if (o != n)
6534                         DBG_OPT_CSE(o, n);
6535         }
6536
6537         return n;
6538 }  /* optimize_node */
6539
6540
6541 /**
6542  * These optimizations never deallocate nodes (in place).  This can cause dead
6543  * nodes lying on the obstack.  Remove these by a dead node elimination,
6544  * i.e., a copying garbage collection.
6545  */
6546 ir_node *optimize_in_place_2(ir_node *n) {
6547         tarval *tv;
6548         ir_node *oldn = n;
6549         ir_opcode iro = get_irn_opcode(n);
6550
6551         if (!get_opt_optimize() && !is_Phi(n)) return n;
6552
6553         /* constant expression evaluation / constant folding */
6554         if (get_opt_constant_folding()) {
6555                 /* neither constants nor Tuple values can be evaluated */
6556                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6557                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
6558                         int old_fp_mode = tarval_fp_ops_enabled();
6559
6560                         tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6561                         /* try to evaluate */
6562                         tv = computed_value(n);
6563                         if (tv != tarval_bad) {
6564                                 /* evaluation was successful -- replace the node. */
6565                                 ir_type *old_tp = get_irn_type(n);
6566                                 int i, arity = get_irn_arity(n);
6567
6568                                 /*
6569                                  * Try to recover the type of the new expression.
6570                                  */
6571                                 for (i = 0; i < arity && !old_tp; ++i)
6572                                         old_tp = get_irn_type(get_irn_n(n, i));
6573
6574                                 n = new_Const(tv);
6575
6576                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6577                                         set_Const_type(n, old_tp);
6578
6579                                 DBG_OPT_CSTEVAL(oldn, n);
6580                                 tarval_enable_fp_ops(old_fp_mode);
6581                                 return n;
6582                         }
6583                         tarval_enable_fp_ops(old_fp_mode);
6584                 }
6585         }
6586
6587         /* remove unnecessary nodes */
6588         if (get_opt_constant_folding() ||
6589             (iro == iro_Phi)  ||   /* always optimize these nodes. */
6590             (iro == iro_Id)   ||   /* ... */
6591             (iro == iro_Proj) ||   /* ... */
6592             (iro == iro_Block)  )  /* Flags tested local. */
6593                 n = equivalent_node(n);
6594
6595         /** common subexpression elimination **/
6596         /* Checks whether n is already available. */
6597         /* The block input is used to distinguish different subexpressions.  Right
6598            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6599            subexpressions within a block. */
6600         if (get_opt_cse()) {
6601                 ir_node *o = n;
6602                 n = identify_remember(current_ir_graph->value_table, o);
6603                 if (o != n)
6604                         DBG_OPT_CSE(o, n);
6605         }
6606
6607         /* Some more constant expression evaluation. */
6608         iro = get_irn_opcode(n);
6609         if (get_opt_constant_folding() ||
6610                 (iro == iro_Cond) ||
6611                 (iro == iro_Proj))     /* Flags tested local. */
6612                 n = transform_node(n);
6613
6614         /* Remove nodes with dead (Bad) input.
6615            Run always for transformation induced Bads.  */
6616         n = gigo(n);
6617
6618         /* Now we can verify the node, as it has no dead inputs any more. */
6619         irn_vrfy(n);
6620
6621         /* Now we have a legal, useful node. Enter it in hash table for cse.
6622            Blocks should be unique anyways.  (Except the successor of start:
6623            is cse with the start block!) */
6624         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6625                 ir_node *o = n;
6626                 n = identify_remember(current_ir_graph->value_table, o);
6627                 if (o != n)
6628                         DBG_OPT_CSE(o, n);
6629         }
6630
6631         return n;
6632 }  /* optimize_in_place_2 */
6633
6634 /**
6635  * Wrapper for external use, set proper status bits after optimization.
6636  */
6637 ir_node *optimize_in_place(ir_node *n) {
6638         /* Handle graph state */
6639         assert(get_irg_phase_state(current_ir_graph) != phase_building);
6640
6641         if (get_opt_global_cse())
6642                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6643         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6644                 set_irg_outs_inconsistent(current_ir_graph);
6645
6646         /* FIXME: Maybe we could also test whether optimizing the node can
6647            change the control graph. */
6648         set_irg_doms_inconsistent(current_ir_graph);
6649         return optimize_in_place_2(n);
6650 }  /* optimize_in_place */
6651
6652 /**
6653  * Calculate a hash value of a Const node.
6654  */
6655 static unsigned hash_Const(const ir_node *node) {
6656         unsigned h;
6657
6658         /* special value for const, as they only differ in their tarval. */
6659         h = HASH_PTR(node->attr.con.tv);
6660         h = 9*h + HASH_PTR(get_irn_mode(node));
6661
6662         return h;
6663 }  /* hash_Const */
6664
6665 /**
6666  * Calculate a hash value of a SymConst node.
6667  */
6668 static unsigned hash_SymConst(const ir_node *node) {
6669         unsigned h;
6670
6671         /* special value for const, as they only differ in their symbol. */
6672         h = HASH_PTR(node->attr.symc.sym.type_p);
6673         h = 9*h + HASH_PTR(get_irn_mode(node));
6674
6675         return h;
6676 }  /* hash_SymConst */
6677
6678 /**
6679  * Set the default hash operation in an ir_op_ops.
6680  *
6681  * @param code   the opcode for the default operation
6682  * @param ops    the operations initialized
6683  *
6684  * @return
6685  *    The operations.
6686  */
6687 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6688 {
6689 #define CASE(a)                                    \
6690         case iro_##a:                                  \
6691                 ops->hash  = hash_##a; \
6692                 break
6693
6694         /* hash function already set */
6695         if (ops->hash != NULL)
6696                 return ops;
6697
6698         switch (code) {
6699         CASE(Const);
6700         CASE(SymConst);
6701         default:
6702                 /* use input/mode default hash if no function was given */
6703                 ops->hash = firm_default_hash;
6704         }
6705
6706         return ops;
6707 #undef CASE
6708 }
6709
6710 /*
6711  * Sets the default operation for an ir_ops.
6712  */
6713 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6714         ops = firm_set_default_hash(code, ops);
6715         ops = firm_set_default_computed_value(code, ops);
6716         ops = firm_set_default_equivalent_node(code, ops);
6717         ops = firm_set_default_transform_node(code, ops);
6718         ops = firm_set_default_node_cmp_attr(code, ops);
6719         ops = firm_set_default_get_type(code, ops);
6720         ops = firm_set_default_get_type_attr(code, ops);
6721         ops = firm_set_default_get_entity_attr(code, ops);
6722
6723         return ops;
6724 }  /* firm_set_default_operations */