- Introduce the concept of the graph state bitset: This is a set of flags that indica...
[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         ir_node *n_t, *n_f;
1748         tarval *ts = value_of(sel);
1749
1750         /* Mux(true, f, t) == t */
1751         if (ts == tarval_b_true) {
1752                 n = get_Mux_true(n);
1753                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1754                 return n;
1755         }
1756         /* Mux(false, f, t) == f */
1757         if (ts == tarval_b_false) {
1758                 n = get_Mux_false(n);
1759                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1760                 return n;
1761         }
1762         n_t = get_Mux_true(n);
1763         n_f = get_Mux_false(n);
1764
1765         /* Mux(v, x, T) == x */
1766         if (is_Unknown(n_f)) {
1767                 n = n_t;
1768                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1769                 return n;
1770         }
1771         /* Mux(v, T, x) == x */
1772         if (is_Unknown(n_t)) {
1773                 n = n_f;
1774                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1775                 return n;
1776         }
1777
1778         /* Mux(v, x, x) == x */
1779         if (n_t == n_f) {
1780                 n = n_t;
1781                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1782                 return n;
1783         }
1784         if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1785                 ir_node *cmp = get_Proj_pred(sel);
1786                 long proj_nr = get_Proj_proj(sel);
1787                 ir_node *f   = get_Mux_false(n);
1788                 ir_node *t   = get_Mux_true(n);
1789
1790                 /*
1791                  * Note further that these optimization work even for floating point
1792                  * with NaN's because -NaN == NaN.
1793                  * However, if +0 and -0 is handled differently, we cannot use the first one.
1794                  */
1795                 if (is_Cmp(cmp)) {
1796                         ir_node *const cmp_l = get_Cmp_left(cmp);
1797                         ir_node *const cmp_r = get_Cmp_right(cmp);
1798
1799                         switch (proj_nr) {
1800                                 case pn_Cmp_Eq:
1801                                         if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1802                                                         (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1803                                                 n = f;
1804                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1805                                                 return n;
1806                                         }
1807                                         break;
1808
1809                                 case pn_Cmp_Lg:
1810                                 case pn_Cmp_Ne:
1811                                         if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1812                                                         (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1813                                                 n = t;
1814                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1815                                                 return n;
1816                                         }
1817                                         break;
1818                         }
1819
1820                         /*
1821                          * Note: normalization puts the constant on the right side,
1822                          * so we check only one case.
1823                          */
1824                         if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1825                                 /* Mux(t CMP 0, X, t) */
1826                                 if (is_Minus(f) && get_Minus_op(f) == t) {
1827                                         /* Mux(t CMP 0, -t, t) */
1828                                         if (proj_nr == pn_Cmp_Eq) {
1829                                                 /* Mux(t == 0, -t, t)  ==>  -t */
1830                                                 n = f;
1831                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1832                                         } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1833                                                 /* Mux(t != 0, -t, t)  ==> t */
1834                                                 n = t;
1835                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1836                                         }
1837                                 }
1838                         }
1839                 }
1840         }
1841         return n;
1842 }  /* equivalent_node_Mux */
1843
1844 /**
1845  * Remove Confirm nodes if setting is on.
1846  * Replace Confirms(x, '=', Constlike) by Constlike.
1847  */
1848 static ir_node *equivalent_node_Confirm(ir_node *n) {
1849         ir_node *pred = get_Confirm_value(n);
1850         pn_Cmp  pnc   = get_Confirm_cmp(n);
1851
1852         while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1853                 /*
1854                  * rare case: two identical Confirms one after another,
1855                  * replace the second one with the first.
1856                  */
1857                 n    = pred;
1858                 pred = get_Confirm_value(n);
1859                 pnc  = get_Confirm_cmp(n);
1860         }
1861         if (get_opt_remove_confirm())
1862                 return get_Confirm_value(n);
1863         return n;
1864 }
1865
1866 /**
1867  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1868  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1869  * new nodes.  It is therefore safe to free n if the node returned is not n.
1870  * If a node returns a Tuple we can not just skip it.  If the size of the
1871  * in array fits, we transform n into a tuple (e.g., Div).
1872  */
1873 ir_node *equivalent_node(ir_node *n) {
1874         if (n->op->ops.equivalent_node)
1875                 return n->op->ops.equivalent_node(n);
1876         return n;
1877 }  /* equivalent_node */
1878
1879 /**
1880  * Sets the default equivalent node operation for an ir_op_ops.
1881  *
1882  * @param code   the opcode for the default operation
1883  * @param ops    the operations initialized
1884  *
1885  * @return
1886  *    The operations.
1887  */
1888 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1889 {
1890 #define CASE(a)                                      \
1891         case iro_##a:                                    \
1892                 ops->equivalent_node  = equivalent_node_##a; \
1893                 break
1894 #define CASE_PROJ(a)                                          \
1895         case iro_##a:                                             \
1896                 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1897                 break
1898
1899         switch (code) {
1900         CASE(Block);
1901         CASE(Jmp);
1902         CASE(Raise);
1903         CASE(Eor);
1904         CASE(Add);
1905         CASE(Shl);
1906         CASE(Shr);
1907         CASE(Shrs);
1908         CASE(Rotl);
1909         CASE(Sub);
1910         CASE(Not);
1911         CASE(Minus);
1912         CASE(Mul);
1913         CASE(Or);
1914         CASE(And);
1915         CASE(Conv);
1916         CASE(Cast);
1917         CASE(Phi);
1918         CASE(Sync);
1919         CASE_PROJ(Tuple);
1920         CASE_PROJ(Div);
1921         CASE_PROJ(Quot);
1922         CASE_PROJ(DivMod);
1923         CASE_PROJ(CopyB);
1924         CASE_PROJ(Bound);
1925         CASE_PROJ(Load);
1926         CASE_PROJ(Store);
1927         CASE(Proj);
1928         CASE(Id);
1929         CASE(Mux);
1930         CASE(Confirm);
1931         default:
1932                 /* leave NULL */;
1933         }
1934
1935         return ops;
1936 #undef CASE
1937 #undef CASE_PROJ
1938 }  /* firm_set_default_equivalent_node */
1939
1940 /**
1941  * Returns non-zero if a node is a Phi node
1942  * with all predecessors constant.
1943  */
1944 static int is_const_Phi(ir_node *n) {
1945         int i;
1946
1947         if (! is_Phi(n) || get_irn_arity(n) == 0)
1948                 return 0;
1949         for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1950                 if (! is_Const(get_irn_n(n, i)))
1951                         return 0;
1952         }
1953         return 1;
1954 }  /* is_const_Phi */
1955
1956 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1957 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1958
1959 /**
1960  * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1961  */
1962 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1963         if (eval == tarval_sub) {
1964                 tarval_sub_type func = (tarval_sub_type)eval;
1965
1966                 return func(a, b, mode);
1967         } else {
1968                 tarval_binop_type func = (tarval_binop_type)eval;
1969
1970                 return func(a, b);
1971         }
1972 }
1973
1974 /**
1975  * Apply an evaluator on a binop with a constant operators (and one Phi).
1976  *
1977  * @param phi    the Phi node
1978  * @param other  the other operand
1979  * @param eval   an evaluator function
1980  * @param mode   the mode of the result, may be different from the mode of the Phi!
1981  * @param left   if non-zero, other is the left operand, else the right
1982  *
1983  * @return a new Phi node if the conversion was successful, NULL else
1984  */
1985 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1986         tarval   *tv;
1987         void     **res;
1988         ir_node  *pred;
1989         ir_graph *irg;
1990         int      i, n = get_irn_arity(phi);
1991
1992         NEW_ARR_A(void *, res, n);
1993         if (left) {
1994                 for (i = 0; i < n; ++i) {
1995                         pred = get_irn_n(phi, i);
1996                         tv   = get_Const_tarval(pred);
1997                         tv   = do_eval(eval, other, tv, mode);
1998
1999                         if (tv == tarval_bad) {
2000                                 /* folding failed, bad */
2001                                 return NULL;
2002                         }
2003                         res[i] = tv;
2004                 }
2005         } else {
2006                 for (i = 0; i < n; ++i) {
2007                         pred = get_irn_n(phi, i);
2008                         tv   = get_Const_tarval(pred);
2009                         tv   = do_eval(eval, tv, other, mode);
2010
2011                         if (tv == tarval_bad) {
2012                                 /* folding failed, bad */
2013                                 return 0;
2014                         }
2015                         res[i] = tv;
2016                 }
2017         }
2018         irg  = current_ir_graph;
2019         for (i = 0; i < n; ++i) {
2020                 pred = get_irn_n(phi, i);
2021                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2022         }
2023         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2024 }  /* apply_binop_on_phi */
2025
2026 /**
2027  * Apply an evaluator on a binop with two constant Phi.
2028  *
2029  * @param a      the left Phi node
2030  * @param b      the right Phi node
2031  * @param eval   an evaluator function
2032  * @param mode   the mode of the result, may be different from the mode of the Phi!
2033  *
2034  * @return a new Phi node if the conversion was successful, NULL else
2035  */
2036 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
2037         tarval   *tv_l, *tv_r, *tv;
2038         void     **res;
2039         ir_node  *pred;
2040         ir_graph *irg;
2041         int      i, n;
2042
2043         if (get_nodes_block(a) != get_nodes_block(b))
2044                 return NULL;
2045
2046         n = get_irn_arity(a);
2047         NEW_ARR_A(void *, res, n);
2048
2049         for (i = 0; i < n; ++i) {
2050                 pred = get_irn_n(a, i);
2051                 tv_l = get_Const_tarval(pred);
2052                 pred = get_irn_n(b, i);
2053                 tv_r = get_Const_tarval(pred);
2054                 tv   = do_eval(eval, tv_l, tv_r, mode);
2055
2056                 if (tv == tarval_bad) {
2057                         /* folding failed, bad */
2058                         return NULL;
2059                 }
2060                 res[i] = tv;
2061         }
2062         irg  = current_ir_graph;
2063         for (i = 0; i < n; ++i) {
2064                 pred = get_irn_n(a, i);
2065                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2066         }
2067         return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
2068 }  /* apply_binop_on_2_phis */
2069
2070 /**
2071  * Apply an evaluator on a unop with a constant operator (a Phi).
2072  *
2073  * @param phi    the Phi node
2074  * @param eval   an evaluator function
2075  *
2076  * @return a new Phi node if the conversion was successful, NULL else
2077  */
2078 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2079         tarval   *tv;
2080         void     **res;
2081         ir_node  *pred;
2082         ir_mode  *mode;
2083         ir_graph *irg;
2084         int      i, n = get_irn_arity(phi);
2085
2086         NEW_ARR_A(void *, res, n);
2087         for (i = 0; i < n; ++i) {
2088                 pred = get_irn_n(phi, i);
2089                 tv   = get_Const_tarval(pred);
2090                 tv   = eval(tv);
2091
2092                 if (tv == tarval_bad) {
2093                         /* folding failed, bad */
2094                         return 0;
2095                 }
2096                 res[i] = tv;
2097         }
2098         mode = get_irn_mode(phi);
2099         irg  = current_ir_graph;
2100         for (i = 0; i < n; ++i) {
2101                 pred = get_irn_n(phi, i);
2102                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2103         }
2104         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2105 }  /* apply_unop_on_phi */
2106
2107 /**
2108  * Apply a conversion on a constant operator (a Phi).
2109  *
2110  * @param phi    the Phi node
2111  *
2112  * @return a new Phi node if the conversion was successful, NULL else
2113  */
2114 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2115         tarval   *tv;
2116         void     **res;
2117         ir_node  *pred;
2118         ir_graph *irg;
2119         int      i, n = get_irn_arity(phi);
2120
2121         NEW_ARR_A(void *, res, n);
2122         for (i = 0; i < n; ++i) {
2123                 pred = get_irn_n(phi, i);
2124                 tv   = get_Const_tarval(pred);
2125                 tv   = tarval_convert_to(tv, mode);
2126
2127                 if (tv == tarval_bad) {
2128                         /* folding failed, bad */
2129                         return 0;
2130                 }
2131                 res[i] = tv;
2132         }
2133         irg  = current_ir_graph;
2134         for (i = 0; i < n; ++i) {
2135                 pred = get_irn_n(phi, i);
2136                 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2137         }
2138         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2139 }  /* apply_conv_on_phi */
2140
2141 /**
2142  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2143  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2144  * If possible, remove the Conv's.
2145  */
2146 static ir_node *transform_node_AddSub(ir_node *n) {
2147         ir_mode *mode = get_irn_mode(n);
2148
2149         if (mode_is_reference(mode)) {
2150                 ir_node *left     = get_binop_left(n);
2151                 ir_node *right    = get_binop_right(n);
2152                 unsigned ref_bits = get_mode_size_bits(mode);
2153
2154                 if (is_Conv(left)) {
2155                         ir_mode *lmode = get_irn_mode(left);
2156                         unsigned bits = get_mode_size_bits(lmode);
2157
2158                         if (ref_bits == bits &&
2159                             mode_is_int(lmode) &&
2160                             get_mode_arithmetic(lmode) == irma_twos_complement) {
2161                                 ir_node *pre      = get_Conv_op(left);
2162                                 ir_mode *pre_mode = get_irn_mode(pre);
2163
2164                                 if (mode_is_int(pre_mode) &&
2165                                     get_mode_size_bits(pre_mode) == bits &&
2166                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2167                                         /* ok, this conv just changes to sign, moreover the calculation
2168                                          * is done with same number of bits as our address mode, so
2169                                          * we can ignore the conv as address calculation can be viewed
2170                                          * as either signed or unsigned
2171                                          */
2172                                         set_binop_left(n, pre);
2173                                 }
2174                         }
2175                 }
2176
2177                 if (is_Conv(right)) {
2178                         ir_mode *rmode = get_irn_mode(right);
2179                         unsigned bits = get_mode_size_bits(rmode);
2180
2181                         if (ref_bits == bits &&
2182                             mode_is_int(rmode) &&
2183                             get_mode_arithmetic(rmode) == irma_twos_complement) {
2184                                 ir_node *pre      = get_Conv_op(right);
2185                                 ir_mode *pre_mode = get_irn_mode(pre);
2186
2187                                 if (mode_is_int(pre_mode) &&
2188                                     get_mode_size_bits(pre_mode) == bits &&
2189                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2190                                         /* ok, this conv just changes to sign, moreover the calculation
2191                                          * is done with same number of bits as our address mode, so
2192                                          * we can ignore the conv as address calculation can be viewed
2193                                          * as either signed or unsigned
2194                                          */
2195                                         set_binop_right(n, pre);
2196                                 }
2197                         }
2198                 }
2199
2200                 /* let address arithmetic use unsigned modes */
2201                 if (is_Const(right)) {
2202                         ir_mode *rmode = get_irn_mode(right);
2203
2204                         if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2205                                 /* convert a AddP(P, *s) into AddP(P, *u) */
2206                                 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2207
2208                                 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2209                                 set_binop_right(n, pre);
2210                         }
2211                 }
2212         }
2213
2214         return n;
2215 }  /* transform_node_AddSub */
2216
2217 #define HANDLE_BINOP_PHI(eval, a, b, c, mode)                     \
2218   c = NULL;                                                       \
2219   if (is_Const(b) && is_const_Phi(a)) {                           \
2220     /* check for Op(Phi, Const) */                                \
2221     c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2222   }                                                               \
2223   else if (is_Const(a) && is_const_Phi(b)) {                      \
2224     /* check for Op(Const, Phi) */                                \
2225     c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2226   }                                                               \
2227   else if (is_const_Phi(a) && is_const_Phi(b)) {                  \
2228     /* check for Op(Phi, Phi) */                                  \
2229     c = apply_binop_on_2_phis(a, b, eval, mode);                  \
2230   }                                                               \
2231   if (c) {                                                        \
2232     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);                   \
2233     return c;                                                     \
2234   }
2235
2236 #define HANDLE_UNOP_PHI(eval, a, c)               \
2237   c = NULL;                                       \
2238   if (is_const_Phi(a)) {                          \
2239     /* check for Op(Phi) */                       \
2240     c = apply_unop_on_phi(a, eval);               \
2241     if (c) {                                      \
2242       DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2243       return c;                                   \
2244     }                                             \
2245   }
2246
2247 /**
2248  * Do the AddSub optimization, then Transform
2249  *   Constant folding on Phi
2250  *   Add(a,a)          -> Mul(a, 2)
2251  *   Add(Mul(a, x), a) -> Mul(a, x+1)
2252  * if the mode is integer or float.
2253  * Transform Add(a,-b) into Sub(a,b).
2254  * Reassociation might fold this further.
2255  */
2256 static ir_node *transform_node_Add(ir_node *n) {
2257         ir_mode *mode;
2258         ir_node *a, *b, *c, *oldn = n;
2259
2260         n = transform_node_AddSub(n);
2261
2262         a = get_Add_left(n);
2263         b = get_Add_right(n);
2264
2265         mode = get_irn_mode(n);
2266
2267         if (mode_is_reference(mode)) {
2268                 ir_mode *lmode = get_irn_mode(a);
2269
2270                 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2271                         /* an Add(a, NULL) is a hidden Conv */
2272                         dbg_info *dbg = get_irn_dbg_info(n);
2273                         return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2274                 }
2275         }
2276
2277         HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2278
2279         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2280         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2281                 return n;
2282
2283         if (mode_is_num(mode)) {
2284                 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2285                 if (!is_irg_state(current_ir_graph, IR_GRAPH_STATE_ARCH_DEP)
2286                                 && a == b && mode_is_int(mode)) {
2287                         ir_node *block = get_nodes_block(n);
2288
2289                         n = new_rd_Mul(
2290                                 get_irn_dbg_info(n),
2291                                 current_ir_graph,
2292                                 block,
2293                                 a,
2294                                 new_Const_long(mode, 2),
2295                                 mode);
2296                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2297                         return n;
2298                 }
2299                 if (is_Minus(a)) {
2300                         n = new_rd_Sub(
2301                                         get_irn_dbg_info(n),
2302                                         current_ir_graph,
2303                                         get_nodes_block(n),
2304                                         b,
2305                                         get_Minus_op(a),
2306                                         mode);
2307                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2308                         return n;
2309                 }
2310                 if (is_Minus(b)) {
2311                         n = new_rd_Sub(
2312                                         get_irn_dbg_info(n),
2313                                         current_ir_graph,
2314                                         get_nodes_block(n),
2315                                         a,
2316                                         get_Minus_op(b),
2317                                         mode);
2318                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2319                         return n;
2320                 }
2321                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2322                         /* Here we rely on constants be on the RIGHT side */
2323                         if (is_Not(a)) {
2324                                 ir_node *op = get_Not_op(a);
2325
2326                                 if (is_Const(b) && is_Const_one(b)) {
2327                                         /* ~x + 1 = -x */
2328                                         ir_node *blk = get_nodes_block(n);
2329                                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2330                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2331                                         return n;
2332                                 }
2333                                 if (op == b) {
2334                                         /* ~x + x = -1 */
2335                                         n = new_Const(get_mode_minus_one(mode));
2336                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2337                                         return n;
2338                                 }
2339                         }
2340                         if (is_Not(b)) {
2341                                 ir_node *op = get_Not_op(b);
2342
2343                                 if (op == a) {
2344                                         /* x + ~x = -1 */
2345                                         n = new_Const(get_mode_minus_one(mode));
2346                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2347                                         return n;
2348                                 }
2349                         }
2350                 }
2351         }
2352         return n;
2353 }  /* transform_node_Add */
2354
2355 /**
2356  * returns -cnst or NULL if impossible
2357  */
2358 static ir_node *const_negate(ir_node *cnst) {
2359         tarval   *tv    = tarval_neg(get_Const_tarval(cnst));
2360         dbg_info *dbgi  = get_irn_dbg_info(cnst);
2361         ir_graph *irg   = get_irn_irg(cnst);
2362         if (tv == tarval_bad) return NULL;
2363         return new_rd_Const(dbgi, irg, tv);
2364 }
2365
2366 /**
2367  * Do the AddSub optimization, then Transform
2368  *   Constant folding on Phi
2369  *   Sub(0,a)          -> Minus(a)
2370  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2371  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2372  *   Sub(Add(a, x), x) -> a
2373  *   Sub(x, Add(x, a)) -> -a
2374  *   Sub(x, Const)     -> Add(x, -Const)
2375  */
2376 static ir_node *transform_node_Sub(ir_node *n) {
2377         ir_mode *mode;
2378         ir_node *oldn = n;
2379         ir_node *a, *b, *c;
2380
2381         n = transform_node_AddSub(n);
2382
2383         a = get_Sub_left(n);
2384         b = get_Sub_right(n);
2385
2386         mode = get_irn_mode(n);
2387
2388         if (mode_is_int(mode)) {
2389                 ir_mode *lmode = get_irn_mode(a);
2390
2391                 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2392                         /* a Sub(a, NULL) is a hidden Conv */
2393                         dbg_info *dbg = get_irn_dbg_info(n);
2394                         n = new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2395                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2396                         return n;
2397                 }
2398
2399                 if (mode == lmode                                     &&
2400                     get_mode_arithmetic(mode) == irma_twos_complement &&
2401                     is_Const(a)                                       &&
2402                     get_Const_tarval(a) == get_mode_minus_one(mode)) {
2403                         /* -1 - x -> ~x */
2404                         dbg_info *dbg = get_irn_dbg_info(n);
2405                         n = new_rd_Not(dbg, current_ir_graph, get_nodes_block(n), b, mode);
2406                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2407                         return n;
2408                 }
2409         }
2410
2411 restart:
2412         HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2413
2414         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2415         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2416                 return n;
2417
2418         if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2419                 /* a - C -> a + (-C) */
2420                 ir_node *cnst = const_negate(b);
2421                 if (cnst != NULL) {
2422                         ir_node  *block = get_nodes_block(n);
2423                         dbg_info *dbgi  = get_irn_dbg_info(n);
2424                         ir_graph *irg   = get_irn_irg(n);
2425
2426                         n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2427                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2428                         return n;
2429                 }
2430         }
2431
2432         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2433                 ir_graph *irg   = current_ir_graph;
2434                 dbg_info *dbg   = get_irn_dbg_info(n);
2435                 ir_node  *block = get_nodes_block(n);
2436                 ir_node  *left  = get_Minus_op(a);
2437                 ir_node  *add   = new_rd_Add(dbg, irg, block, left, b, mode);
2438
2439                 n = new_rd_Minus(dbg, irg, block, add, mode);
2440                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2441                 return n;
2442         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2443                 ir_graph *irg   = current_ir_graph;
2444                 dbg_info *dbg   = get_irn_dbg_info(n);
2445                 ir_node  *block = get_nodes_block(n);
2446                 ir_node  *right = get_Minus_op(b);
2447
2448                 n = new_rd_Add(dbg, irg, block, a, right, mode);
2449                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2450                 return n;
2451         } else if (is_Sub(b)) {
2452                 /* a - (b - c) -> a + (c - b)
2453                  *             -> (a - b) + c iff (b - c) is a pointer */
2454                 ir_graph *irg     = current_ir_graph;
2455                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2456                 ir_node  *s_block = get_nodes_block(b);
2457                 ir_node  *s_left  = get_Sub_left(b);
2458                 ir_node  *s_right = get_Sub_right(b);
2459                 ir_mode  *s_mode  = get_irn_mode(b);
2460                 if (mode_is_reference(s_mode)) {
2461                         ir_node  *sub     = new_rd_Sub(s_dbg, irg, s_block, a, s_left, mode);
2462                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2463                         ir_node  *a_block = get_nodes_block(n);
2464
2465                         if (s_mode != mode)
2466                                 s_right = new_r_Conv(irg, a_block, s_right, mode);
2467                         n = new_rd_Add(a_dbg, irg, a_block, sub, s_right, mode);
2468                 } else {
2469                         ir_node  *sub     = new_rd_Sub(s_dbg, irg, s_block, s_right, s_left, s_mode);
2470                         dbg_info *a_dbg   = get_irn_dbg_info(n);
2471                         ir_node  *a_block = get_nodes_block(n);
2472
2473                         n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2474                 }
2475                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2476                 return n;
2477         } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2478                 ir_node *m_right = get_Mul_right(b);
2479                 if (is_Const(m_right)) {
2480                         ir_node *cnst2 = const_negate(m_right);
2481                         if (cnst2 != NULL) {
2482                                 ir_graph *irg     = current_ir_graph;
2483                                 dbg_info *m_dbg   = get_irn_dbg_info(b);
2484                                 ir_node  *m_block = get_nodes_block(b);
2485                                 ir_node  *m_left  = get_Mul_left(b);
2486                                 ir_mode  *m_mode  = get_irn_mode(b);
2487                                 ir_node  *mul     = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2488                                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2489                                 ir_node  *a_block = get_nodes_block(n);
2490
2491                                 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2492                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2493                                 return n;
2494                         }
2495                 }
2496         }
2497
2498         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2499         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2500                 n = new_rd_Minus(
2501                                 get_irn_dbg_info(n),
2502                                 current_ir_graph,
2503                                 get_nodes_block(n),
2504                                 b,
2505                                 mode);
2506                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2507                 return n;
2508         }
2509         if (is_Add(a)) {
2510                 if (mode_wrap_around(mode)) {
2511                         ir_node *left  = get_Add_left(a);
2512                         ir_node *right = get_Add_right(a);
2513
2514                         /* FIXME: Does the Conv's work only for two complement or generally? */
2515                         if (left == b) {
2516                                 if (mode != get_irn_mode(right)) {
2517                                         /* This Sub is an effective Cast */
2518                                         right = new_r_Conv(get_irn_irg(n), get_nodes_block(n), right, mode);
2519                                 }
2520                                 n = right;
2521                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2522                                 return n;
2523                         } else if (right == b) {
2524                                 if (mode != get_irn_mode(left)) {
2525                                         /* This Sub is an effective Cast */
2526                                         left = new_r_Conv(get_irn_irg(n), get_nodes_block(n), left, mode);
2527                                 }
2528                                 n = left;
2529                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2530                                 return n;
2531                         }
2532                 }
2533         }
2534         if (is_Add(b)) {
2535                 if (mode_wrap_around(mode)) {
2536                         ir_node *left  = get_Add_left(b);
2537                         ir_node *right = get_Add_right(b);
2538
2539                         /* FIXME: Does the Conv's work only for two complement or generally? */
2540                         if (left == a) {
2541                                 ir_mode *r_mode = get_irn_mode(right);
2542
2543                                 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), right, r_mode);
2544                                 if (mode != r_mode) {
2545                                         /* This Sub is an effective Cast */
2546                                         n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2547                                 }
2548                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2549                                 return n;
2550                         } else if (right == a) {
2551                                 ir_mode *l_mode = get_irn_mode(left);
2552
2553                                 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), left, l_mode);
2554                                 if (mode != l_mode) {
2555                                         /* This Sub is an effective Cast */
2556                                         n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2557                                 }
2558                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2559                                 return n;
2560                         }
2561                 }
2562         }
2563         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2564                 ir_mode *mode = get_irn_mode(a);
2565
2566                 if (mode == get_irn_mode(b)) {
2567                         ir_mode *ma, *mb;
2568                         ir_node *op_a = get_Conv_op(a);
2569                         ir_node *op_b = get_Conv_op(b);
2570
2571                         /* check if it's allowed to skip the conv */
2572                         ma = get_irn_mode(op_a);
2573                         mb = get_irn_mode(op_b);
2574
2575                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2576                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2577                                 a = op_a; b = op_b;
2578                                 set_Sub_left(n, a);
2579                                 set_Sub_right(n, b);
2580
2581                                 goto restart;
2582                         }
2583                 }
2584         }
2585         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2586         if (!is_reassoc_running() && is_Mul(a)) {
2587                 ir_node *ma = get_Mul_left(a);
2588                 ir_node *mb = get_Mul_right(a);
2589
2590                 if (ma == b) {
2591                         ir_node *blk = get_nodes_block(n);
2592                         n = new_rd_Mul(
2593                                         get_irn_dbg_info(n),
2594                                         current_ir_graph, blk,
2595                                         ma,
2596                                         new_rd_Sub(
2597                                                 get_irn_dbg_info(n),
2598                                                 current_ir_graph, blk,
2599                                                 mb,
2600                                                 new_Const_long(mode, 1),
2601                                                 mode),
2602                                         mode);
2603                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2604                         return n;
2605                 } else if (mb == b) {
2606                         ir_node *blk = get_nodes_block(n);
2607                         n = new_rd_Mul(
2608                                         get_irn_dbg_info(n),
2609                                         current_ir_graph, blk,
2610                                         mb,
2611                                         new_rd_Sub(
2612                                                 get_irn_dbg_info(n),
2613                                                 current_ir_graph, blk,
2614                                                 ma,
2615                                                 new_Const_long(mode, 1),
2616                                                 mode),
2617                                         mode);
2618                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2619                         return n;
2620                 }
2621         }
2622         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2623                 ir_node *x   =      get_Sub_left(a);
2624                 ir_node *y        = get_Sub_right(a);
2625                 ir_node *blk      = get_nodes_block(n);
2626                 ir_mode *m_b      = get_irn_mode(b);
2627                 ir_mode *m_y      = get_irn_mode(y);
2628                 ir_mode *add_mode;
2629                 ir_node *add;
2630
2631                 /* Determine the right mode for the Add. */
2632                 if (m_b == m_y)
2633                         add_mode = m_b;
2634                 else if (mode_is_reference(m_b))
2635                         add_mode = m_b;
2636                 else if (mode_is_reference(m_y))
2637                         add_mode = m_y;
2638                 else {
2639                         /*
2640                          * Both modes are different but none is reference,
2641                          * happens for instance in SubP(SubP(P, Iu), Is).
2642                          * We have two possibilities here: Cast or ignore.
2643                          * Currently we ignore this case.
2644                          */
2645                         return n;
2646                 }
2647
2648                 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2649
2650                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2651                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2652                 return n;
2653         }
2654
2655         if (get_mode_arithmetic(mode) == irma_twos_complement) {
2656                 if (is_Const(a) && is_Not(b)) {
2657                         /* c - ~X = X + (c+1) */
2658                         tarval *tv = get_Const_tarval(a);
2659
2660                         tv = tarval_add(tv, get_mode_one(mode));
2661                         if (tv != tarval_bad) {
2662                                 ir_node *blk = get_nodes_block(n);
2663                                 ir_node *c = new_Const(tv);
2664                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2665                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2666                                 return n;
2667                         }
2668                 }
2669         }
2670         return n;
2671 }  /* transform_node_Sub */
2672
2673 /**
2674  * Several transformation done on n*n=2n bits mul.
2675  * These transformations must be done here because new nodes may be produced.
2676  */
2677 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2678         ir_node *oldn = n;
2679         ir_node *a = get_Mul_left(n);
2680         ir_node *b = get_Mul_right(n);
2681         tarval *ta = value_of(a);
2682         tarval *tb = value_of(b);
2683         ir_mode *smode = get_irn_mode(a);
2684
2685         if (ta == get_mode_one(smode)) {
2686                 /* (L)1 * (L)b = (L)b */
2687                 ir_node *blk = get_nodes_block(n);
2688                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2689                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2690                 return n;
2691         }
2692         else if (ta == get_mode_minus_one(smode)) {
2693                 /* (L)-1 * (L)b = (L)b */
2694                 ir_node *blk = get_nodes_block(n);
2695                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2696                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2697                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2698                 return n;
2699         }
2700         if (tb == get_mode_one(smode)) {
2701                 /* (L)a * (L)1 = (L)a */
2702                 ir_node *blk = get_irn_n(a, -1);
2703                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2704                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2705                 return n;
2706         }
2707         else if (tb == get_mode_minus_one(smode)) {
2708                 /* (L)a * (L)-1 = (L)-a */
2709                 ir_node *blk = get_nodes_block(n);
2710                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2711                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2712                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2713                 return n;
2714         }
2715         return n;
2716 }
2717
2718 /**
2719  * Transform Mul(a,-1) into -a.
2720  * Do constant evaluation of Phi nodes.
2721  * Do architecture dependent optimizations on Mul nodes
2722  */
2723 static ir_node *transform_node_Mul(ir_node *n) {
2724         ir_node *c, *oldn = n;
2725         ir_mode *mode = get_irn_mode(n);
2726         ir_node *a = get_Mul_left(n);
2727         ir_node *b = get_Mul_right(n);
2728
2729         if (is_Bad(a) || is_Bad(b))
2730                 return n;
2731
2732         if (mode != get_irn_mode(a))
2733                 return transform_node_Mul2n(n, mode);
2734
2735         HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2736
2737         if (mode_is_signed(mode)) {
2738                 ir_node *r = NULL;
2739
2740                 if (value_of(a) == get_mode_minus_one(mode))
2741                         r = b;
2742                 else if (value_of(b) == get_mode_minus_one(mode))
2743                         r = a;
2744                 if (r) {
2745                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
2746                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2747                         return n;
2748                 }
2749         }
2750         if (is_Minus(a)) {
2751                 if (is_Const(b)) { /* (-a) * const -> a * -const */
2752                         ir_node *cnst = const_negate(b);
2753                         if (cnst != NULL) {
2754                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2755                                 ir_node  *block = get_nodes_block(n);
2756                                 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2757                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2758                                 return n;
2759                         }
2760                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2761                         dbg_info *dbgi  = get_irn_dbg_info(n);
2762                         ir_node  *block = get_nodes_block(n);
2763                         n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2764                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2765                         return n;
2766                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2767                         ir_node  *sub_l = get_Sub_left(b);
2768                         ir_node  *sub_r = get_Sub_right(b);
2769                         dbg_info *dbgi  = get_irn_dbg_info(n);
2770                         ir_graph *irg   = current_ir_graph;
2771                         ir_node  *block = get_nodes_block(n);
2772                         ir_node  *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2773                         n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2774                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2775                         return n;
2776                 }
2777         } else if (is_Minus(b)) {
2778                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2779                         ir_node  *sub_l = get_Sub_left(a);
2780                         ir_node  *sub_r = get_Sub_right(a);
2781                         dbg_info *dbgi  = get_irn_dbg_info(n);
2782                         ir_graph *irg   = current_ir_graph;
2783                         ir_node  *block = get_nodes_block(n);
2784                         ir_node  *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2785                         n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2786                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2787                         return n;
2788                 }
2789         }
2790         if (get_mode_arithmetic(mode) == irma_ieee754) {
2791                 if (is_Const(a)) {
2792                         tarval *tv = get_Const_tarval(a);
2793                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2794                                         && !tarval_is_negative(tv)) {
2795                                 /* 2.0 * b = b + b */
2796                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2797                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2798                                 return n;
2799                         }
2800                 }
2801                 else if (is_Const(b)) {
2802                         tarval *tv = get_Const_tarval(b);
2803                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2804                                         && !tarval_is_negative(tv)) {
2805                                 /* a * 2.0 = a + a */
2806                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2807                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2808                                 return n;
2809                         }
2810                 }
2811         }
2812         return arch_dep_replace_mul_with_shifts(n);
2813 }  /* transform_node_Mul */
2814
2815 /**
2816  * Transform a Div Node.
2817  */
2818 static ir_node *transform_node_Div(ir_node *n) {
2819         ir_mode *mode = get_Div_resmode(n);
2820         ir_node *a = get_Div_left(n);
2821         ir_node *b = get_Div_right(n);
2822         ir_node *value;
2823         tarval  *tv;
2824
2825         if (is_Const(b) && is_const_Phi(a)) {
2826                 /* check for Div(Phi, Const) */
2827                 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2828                 if (value) {
2829                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2830                         goto make_tuple;
2831                 }
2832         }
2833         else if (is_Const(a) && is_const_Phi(b)) {
2834                 /* check for Div(Const, Phi) */
2835                 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2836                 if (value) {
2837                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2838                         goto make_tuple;
2839                 }
2840         }
2841         else if (is_const_Phi(a) && is_const_Phi(b)) {
2842                 /* check for Div(Phi, Phi) */
2843                 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2844                 if (value) {
2845                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2846                         goto make_tuple;
2847                 }
2848         }
2849
2850         value = n;
2851         tv = value_of(n);
2852         if (tv != tarval_bad) {
2853                 value = new_Const(tv);
2854
2855                 DBG_OPT_CSTEVAL(n, value);
2856                 goto make_tuple;
2857         } else {
2858                 ir_node       *a = get_Div_left(n);
2859                 ir_node       *b = get_Div_right(n);
2860                 const ir_node *dummy;
2861
2862                 if (a == b && value_not_zero(a, &dummy)) {
2863                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2864                         value = new_Const(get_mode_one(mode));
2865                         DBG_OPT_CSTEVAL(n, value);
2866                         goto make_tuple;
2867                 } else {
2868                         if (mode_is_signed(mode) && is_Const(b)) {
2869                                 tarval *tv = get_Const_tarval(b);
2870
2871                                 if (tv == get_mode_minus_one(mode)) {
2872                                         /* a / -1 */
2873                                         value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2874                                         DBG_OPT_CSTEVAL(n, value);
2875                                         goto make_tuple;
2876                                 }
2877                         }
2878                         /* Try architecture dependent optimization */
2879                         value = arch_dep_replace_div_by_const(n);
2880                 }
2881         }
2882
2883         if (value != n) {
2884                 ir_node *mem, *blk;
2885
2886 make_tuple:
2887                 /* Turn Div into a tuple (mem, jmp, bad, value) */
2888                 mem = get_Div_mem(n);
2889                 blk = get_nodes_block(n);
2890
2891                 /* skip a potential Pin */
2892                 mem = skip_Pin(mem);
2893                 turn_into_tuple(n, pn_Div_max);
2894                 set_Tuple_pred(n, pn_Div_M,         mem);
2895                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2896                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());
2897                 set_Tuple_pred(n, pn_Div_res,       value);
2898         }
2899         return n;
2900 }  /* transform_node_Div */
2901
2902 /**
2903  * Transform a Mod node.
2904  */
2905 static ir_node *transform_node_Mod(ir_node *n) {
2906         ir_mode *mode = get_Mod_resmode(n);
2907         ir_node *a = get_Mod_left(n);
2908         ir_node *b = get_Mod_right(n);
2909         ir_node *value;
2910         tarval  *tv;
2911
2912         if (is_Const(b) && is_const_Phi(a)) {
2913                 /* check for Div(Phi, Const) */
2914                 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2915                 if (value) {
2916                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2917                         goto make_tuple;
2918                 }
2919         }
2920         else if (is_Const(a) && is_const_Phi(b)) {
2921                 /* check for Div(Const, Phi) */
2922                 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2923                 if (value) {
2924                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2925                         goto make_tuple;
2926                 }
2927         }
2928         else if (is_const_Phi(a) && is_const_Phi(b)) {
2929                 /* check for Div(Phi, Phi) */
2930                 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2931                 if (value) {
2932                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2933                         goto make_tuple;
2934                 }
2935         }
2936
2937         value = n;
2938         tv = value_of(n);
2939         if (tv != tarval_bad) {
2940                 value = new_Const(tv);
2941
2942                 DBG_OPT_CSTEVAL(n, value);
2943                 goto make_tuple;
2944         } else {
2945                 ir_node       *a = get_Mod_left(n);
2946                 ir_node       *b = get_Mod_right(n);
2947                 const ir_node *dummy;
2948
2949                 if (a == b && value_not_zero(a, &dummy)) {
2950                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2951                         value = new_Const(get_mode_null(mode));
2952                         DBG_OPT_CSTEVAL(n, value);
2953                         goto make_tuple;
2954                 } else {
2955                         if (mode_is_signed(mode) && is_Const(b)) {
2956                                 tarval *tv = get_Const_tarval(b);
2957
2958                                 if (tv == get_mode_minus_one(mode)) {
2959                                         /* a % -1 = 0 */
2960                                         value = new_Const(get_mode_null(mode));
2961                                         DBG_OPT_CSTEVAL(n, value);
2962                                         goto make_tuple;
2963                                 }
2964                         }
2965                         /* Try architecture dependent optimization */
2966                         value = arch_dep_replace_mod_by_const(n);
2967                 }
2968         }
2969
2970         if (value != n) {
2971                 ir_node *mem, *blk;
2972
2973 make_tuple:
2974                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2975                 mem = get_Mod_mem(n);
2976                 blk = get_nodes_block(n);
2977
2978                 /* skip a potential Pin */
2979                 mem = skip_Pin(mem);
2980                 turn_into_tuple(n, pn_Mod_max);
2981                 set_Tuple_pred(n, pn_Mod_M,         mem);
2982                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2983                 set_Tuple_pred(n, pn_Mod_X_except,  new_Bad());
2984                 set_Tuple_pred(n, pn_Mod_res,       value);
2985         }
2986         return n;
2987 }  /* transform_node_Mod */
2988
2989 /**
2990  * Transform a DivMod node.
2991  */
2992 static ir_node *transform_node_DivMod(ir_node *n) {
2993         const ir_node *dummy;
2994         ir_node       *a = get_DivMod_left(n);
2995         ir_node       *b = get_DivMod_right(n);
2996         ir_mode       *mode = get_DivMod_resmode(n);
2997         ir_node       *va, *vb;
2998         tarval        *ta, *tb;
2999         int           evaluated = 0;
3000
3001         if (is_Const(b) && is_const_Phi(a)) {
3002                 /* check for Div(Phi, Const) */
3003                 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
3004                 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
3005                 if (va && vb) {
3006                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3007                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3008                         goto make_tuple;
3009                 }
3010         }
3011         else if (is_Const(a) && is_const_Phi(b)) {
3012                 /* check for Div(Const, Phi) */
3013                 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
3014                 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
3015                 if (va && vb) {
3016                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3017                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3018                         goto make_tuple;
3019                 }
3020         }
3021         else if (is_const_Phi(a) && is_const_Phi(b)) {
3022                 /* check for Div(Phi, Phi) */
3023                 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
3024                 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
3025                 if (va && vb) {
3026                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3027                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3028                         goto make_tuple;
3029                 }
3030         }
3031
3032         ta = value_of(a);
3033         tb = value_of(b);
3034         if (tb != tarval_bad) {
3035                 if (tb == get_mode_one(get_tarval_mode(tb))) {
3036                         va = a;
3037                         vb = new_Const(get_mode_null(mode));
3038                         DBG_OPT_CSTEVAL(n, vb);
3039                         goto make_tuple;
3040                 } else if (ta != tarval_bad) {
3041                         tarval *resa, *resb;
3042                         resa = tarval_div(ta, tb);
3043                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3044                                                              Jmp for X result!? */
3045                         resb = tarval_mod(ta, tb);
3046                         if (resb == tarval_bad) return n; /* Causes exception! */
3047                         va = new_Const(resa);
3048                         vb = new_Const(resb);
3049                         DBG_OPT_CSTEVAL(n, va);
3050                         DBG_OPT_CSTEVAL(n, vb);
3051                         goto make_tuple;
3052                 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3053                         va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
3054                         vb = new_Const(get_mode_null(mode));
3055                         DBG_OPT_CSTEVAL(n, va);
3056                         DBG_OPT_CSTEVAL(n, vb);
3057                         goto make_tuple;
3058                 } else { /* Try architecture dependent optimization */
3059                         va = a;
3060                         vb = b;
3061                         arch_dep_replace_divmod_by_const(&va, &vb, n);
3062                         evaluated = va != NULL;
3063                 }
3064         } else if (a == b) {
3065                 if (value_not_zero(a, &dummy)) {
3066                         /* a/a && a != 0 */
3067                         va = new_Const(get_mode_one(mode));
3068                         vb = new_Const(get_mode_null(mode));
3069                         DBG_OPT_CSTEVAL(n, va);
3070                         DBG_OPT_CSTEVAL(n, vb);
3071                         goto make_tuple;
3072                 } else {
3073                         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3074                         return n;
3075                 }
3076         } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3077                 /* 0 / non-Const = 0 */
3078                 vb = va = a;
3079                 goto make_tuple;
3080         }
3081
3082         if (evaluated) { /* replace by tuple */
3083                 ir_node *mem, *blk;
3084
3085 make_tuple:
3086                 mem = get_DivMod_mem(n);
3087                 /* skip a potential Pin */
3088                 mem = skip_Pin(mem);
3089
3090                 blk = get_nodes_block(n);
3091                 turn_into_tuple(n, pn_DivMod_max);
3092                 set_Tuple_pred(n, pn_DivMod_M,         mem);
3093                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
3094                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());  /* no exception */
3095                 set_Tuple_pred(n, pn_DivMod_res_div,   va);
3096                 set_Tuple_pred(n, pn_DivMod_res_mod,   vb);
3097         }
3098
3099         return n;
3100 }  /* transform_node_DivMod */
3101
3102 /**
3103  * Optimize x / c to x * (1/c)
3104  */
3105 static ir_node *transform_node_Quot(ir_node *n) {
3106         ir_mode *mode = get_Quot_resmode(n);
3107         ir_node *oldn = n;
3108
3109         if (get_mode_arithmetic(mode) == irma_ieee754) {
3110                 ir_node *b = get_Quot_right(n);
3111                 tarval *tv = value_of(b);
3112
3113                 if (tv != tarval_bad) {
3114                         int rem = tarval_fp_ops_enabled();
3115
3116                         /*
3117                          * Floating point constant folding might be disabled here to
3118                          * prevent rounding.
3119                          * However, as we check for exact result, doing it is safe.
3120                          * Switch it on.
3121                          */
3122                         tarval_enable_fp_ops(1);
3123                         tv = tarval_quo(get_mode_one(mode), tv);
3124                         tarval_enable_fp_ops(rem);
3125
3126                         /* Do the transformation if the result is either exact or we are not
3127                            using strict rules. */
3128                         if (tv != tarval_bad &&
3129                             (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3130                                 ir_node *blk = get_nodes_block(n);
3131                                 ir_node *c = new_Const(tv);
3132                                 ir_node *a = get_Quot_left(n);
3133                                 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3134                                 ir_node *mem = get_Quot_mem(n);
3135
3136                                 /* skip a potential Pin */
3137                                 mem = skip_Pin(mem);
3138                                 turn_into_tuple(n, pn_Quot_max);
3139                                 set_Tuple_pred(n, pn_Quot_M, mem);
3140                                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3141                                 set_Tuple_pred(n, pn_Quot_X_except,  new_r_Bad(current_ir_graph));
3142                                 set_Tuple_pred(n, pn_Quot_res, m);
3143                                 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3144                         }
3145                 }
3146         }
3147         return n;
3148 }  /* transform_node_Quot */
3149
3150 /**
3151  * Optimize Abs(x) into  x if x is Confirmed >= 0
3152  * Optimize Abs(x) into -x if x is Confirmed <= 0
3153  * Optimize Abs(-x) int Abs(x)
3154  */
3155 static ir_node *transform_node_Abs(ir_node *n) {
3156         ir_node *c, *oldn = n;
3157         ir_node *a = get_Abs_op(n);
3158         ir_mode *mode;
3159
3160         HANDLE_UNOP_PHI(tarval_abs, a, c);
3161
3162         switch (classify_value_sign(a)) {
3163         case value_classified_negative:
3164                 mode = get_irn_mode(n);
3165
3166                 /*
3167                  * We can replace the Abs by -x here.
3168                  * We even could add a new Confirm here
3169                  * (if not twos complement)
3170                  *
3171                  * Note that -x would create a new node, so we could
3172                  * not run it in the equivalent_node() context.
3173                  */
3174                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3175                                 get_nodes_block(n), a, mode);
3176
3177                 DBG_OPT_CONFIRM(oldn, n);
3178                 return n;
3179         case value_classified_positive:
3180                 /* n is positive, Abs is not needed */
3181                 n = a;
3182
3183                 DBG_OPT_CONFIRM(oldn, n);
3184                 return n;
3185         default:
3186                 break;
3187         }
3188         if (is_Minus(a)) {
3189                 /* Abs(-x) = Abs(x) */
3190                 mode = get_irn_mode(n);
3191                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3192                                 get_nodes_block(n), get_Minus_op(a), mode);
3193                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3194                 return n;
3195         }
3196         return n;
3197 }  /* transform_node_Abs */
3198
3199 /**
3200  * Optimize -a CMP -b into b CMP a.
3201  * This works only for for modes where unary Minus
3202  * cannot Overflow.
3203  * Note that two-complement integers can Overflow
3204  * so it will NOT work.
3205  *
3206  * For == and != can be handled in Proj(Cmp)
3207  */
3208 static ir_node *transform_node_Cmp(ir_node *n) {
3209         ir_node *oldn = n;
3210         ir_node *left  = get_Cmp_left(n);
3211         ir_node *right = get_Cmp_right(n);
3212
3213         if (is_Minus(left) && is_Minus(right) &&
3214                 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3215                 ir_node *const new_left  = get_Minus_op(right);
3216                 ir_node *const new_right = get_Minus_op(left);
3217                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3218                         get_nodes_block(n), new_left, new_right);
3219                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3220         }
3221         return n;
3222 }  /* transform_node_Cmp */
3223
3224
3225 /**
3226  * Transform a Cond node.
3227  *
3228  * Replace the Cond by a Jmp if it branches on a constant
3229  * condition.
3230  */
3231 static ir_node *transform_node_Cond(ir_node *n) {
3232
3233         ir_node *jmp;
3234         ir_node *a = get_Cond_selector(n);
3235         tarval *ta = value_of(a);
3236
3237         /* we need block info which is not available in floating irgs */
3238         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3239                 return n;
3240
3241         if ((ta != tarval_bad) &&
3242             (get_irn_mode(a) == mode_b) &&
3243             (get_opt_unreachable_code())) {
3244                 /* It's a boolean Cond, branching on a boolean constant.
3245                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3246                 ir_node *blk = get_nodes_block(n);
3247                 jmp = new_r_Jmp(current_ir_graph, blk);
3248                 turn_into_tuple(n, pn_Cond_max);
3249                 if (ta == tarval_b_true) {
3250                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
3251                         set_Tuple_pred(n, pn_Cond_true, jmp);
3252                 } else {
3253                         set_Tuple_pred(n, pn_Cond_false, jmp);
3254                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
3255                 }
3256                 /* We might generate an endless loop, so keep it alive. */
3257                 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3258         }
3259         return n;
3260 }  /* transform_node_Cond */
3261
3262 /**
3263  * Prototype of a recursive transform function
3264  * for bitwise distributive transformations.
3265  */
3266 typedef ir_node* (*recursive_transform)(ir_node *n);
3267
3268 /**
3269  * makes use of distributive laws for and, or, eor
3270  *     and(a OP c, b OP c) -> and(a, b) OP c
3271  * note, might return a different op than n
3272  */
3273 static ir_node *transform_bitwise_distributive(ir_node *n,
3274                                                recursive_transform trans_func)
3275 {
3276         ir_node *oldn    = n;
3277         ir_node *a       = get_binop_left(n);
3278         ir_node *b       = get_binop_right(n);
3279         ir_op   *op      = get_irn_op(a);
3280         ir_op   *op_root = get_irn_op(n);
3281
3282         if(op != get_irn_op(b))
3283                 return n;
3284
3285         /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3286         if (op == op_Conv) {
3287                 ir_node *a_op   = get_Conv_op(a);
3288                 ir_node *b_op   = get_Conv_op(b);
3289                 ir_mode *a_mode = get_irn_mode(a_op);
3290                 ir_mode *b_mode = get_irn_mode(b_op);
3291                 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3292                         ir_node *blk = get_nodes_block(n);
3293
3294                         n = exact_copy(n);
3295                         set_binop_left(n, a_op);
3296                         set_binop_right(n, b_op);
3297                         set_irn_mode(n, a_mode);
3298                         n = trans_func(n);
3299                         n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3300
3301                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3302                         return n;
3303                 }
3304         }
3305
3306         if (op == op_Eor) {
3307                 /* nothing to gain here */
3308                 return n;
3309         }
3310
3311         if (op == op_Shrs || op == op_Shr || op == op_Shl
3312                         || op == op_And || op == op_Or || op == op_Eor) {
3313                 ir_node *a_left  = get_binop_left(a);
3314                 ir_node *a_right = get_binop_right(a);
3315                 ir_node *b_left  = get_binop_left(b);
3316                 ir_node *b_right = get_binop_right(b);
3317                 ir_node *c       = NULL;
3318                 ir_node *op1     = NULL;
3319                 ir_node *op2     = NULL;
3320
3321                 if (is_op_commutative(op)) {
3322                         if (a_left == b_left) {
3323                                 c   = a_left;
3324                                 op1 = a_right;
3325                                 op2 = b_right;
3326                         } else if(a_left == b_right) {
3327                                 c   = a_left;
3328                                 op1 = a_right;
3329                                 op2 = b_left;
3330                         } else if(a_right == b_left) {
3331                                 c   = a_right;
3332                                 op1 = a_left;
3333                                 op2 = b_right;
3334                         }
3335                 }
3336                 if(a_right == b_right) {
3337                         c   = a_right;
3338                         op1 = a_left;
3339                         op2 = b_left;
3340                 }
3341
3342                 if (c != NULL) {
3343                         /* (a sop c) & (b sop c) => (a & b) sop c */
3344                         ir_node *blk = get_nodes_block(n);
3345
3346                         ir_node *new_n = exact_copy(n);
3347                         set_binop_left(new_n, op1);
3348                         set_binop_right(new_n, op2);
3349                         new_n = trans_func(new_n);
3350
3351                         if(op_root == op_Eor && op == op_Or) {
3352                                 dbg_info  *dbgi = get_irn_dbg_info(n);
3353                                 ir_graph  *irg  = current_ir_graph;
3354                                 ir_mode   *mode = get_irn_mode(c);
3355
3356                                 c = new_rd_Not(dbgi, irg, blk, c, mode);
3357                                 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3358                         } else {
3359                                 n = exact_copy(a);
3360                                 set_nodes_block(n, blk);
3361                                 set_binop_left(n, new_n);
3362                                 set_binop_right(n, c);
3363                                 add_identities(current_ir_graph->value_table, n);
3364                         }
3365
3366                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3367                         return n;
3368                 }
3369         }
3370
3371         return n;
3372 }
3373
3374 /**
3375  * Transform an And.
3376  */
3377 static ir_node *transform_node_And(ir_node *n) {
3378         ir_node *c, *oldn = n;
3379         ir_node *a = get_And_left(n);
3380         ir_node *b = get_And_right(n);
3381         ir_mode *mode;
3382
3383         mode = get_irn_mode(n);
3384         HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3385
3386         /* we can evaluate 2 Projs of the same Cmp */
3387         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3388                 ir_node *pred_a = get_Proj_pred(a);
3389                 ir_node *pred_b = get_Proj_pred(b);
3390                 if (pred_a == pred_b) {
3391                         dbg_info *dbgi  = get_irn_dbg_info(n);
3392                         ir_node  *block = get_nodes_block(pred_a);
3393                         pn_Cmp pn_a     = get_Proj_proj(a);
3394                         pn_Cmp pn_b     = get_Proj_proj(b);
3395                         /* yes, we can simply calculate with pncs */
3396                         pn_Cmp new_pnc  = pn_a & pn_b;
3397
3398                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3399                 }
3400         }
3401         if (is_Or(a)) {
3402                 if (is_Not(b)) {
3403                         ir_node *op = get_Not_op(b);
3404                         if (is_And(op)) {
3405                                 ir_node *ba = get_And_left(op);
3406                                 ir_node *bb = get_And_right(op);
3407
3408                                 /* it's enough to test the following cases due to normalization! */
3409                                 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3410                                         /* (a|b) & ~(a&b) = a^b */
3411                                         ir_node *block = get_nodes_block(n);
3412
3413                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3414                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3415                                         return n;
3416                                 }
3417                         }
3418                 }
3419         }
3420         if (is_Or(b)) {
3421                 if (is_Not(a)) {
3422                         ir_node *op = get_Not_op(a);
3423                         if (is_And(op)) {
3424                                 ir_node *aa = get_And_left(op);
3425                                 ir_node *ab = get_And_right(op);
3426
3427                                 /* it's enough to test the following cases due to normalization! */
3428                                 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3429                                         /* (a|b) & ~(a&b) = a^b */
3430                                         ir_node *block = get_nodes_block(n);
3431
3432                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3433                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3434                                         return n;
3435                                 }
3436                         }
3437                 }
3438         }
3439         if (is_Eor(a)) {
3440                 ir_node *al = get_Eor_left(a);
3441                 ir_node *ar = get_Eor_right(a);
3442
3443                 if (al == b) {
3444                         /* (b ^ a) & b -> ~a & b */
3445                         dbg_info *dbg  = get_irn_dbg_info(n);
3446                         ir_node *block = get_nodes_block(n);
3447
3448                         ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3449                         n  = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3450                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3451                         return n;
3452                 }
3453                 if (ar == b) {
3454                         /* (a ^ b) & b -> ~a & b */
3455                         dbg_info *dbg  = get_irn_dbg_info(n);
3456                         ir_node *block = get_nodes_block(n);
3457
3458                         al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3459                         n  = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3460                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3461                         return n;
3462                 }
3463         }
3464         if (is_Eor(b)) {
3465                 ir_node *bl = get_Eor_left(b);
3466                 ir_node *br = get_Eor_right(b);
3467
3468                 if (bl == a) {
3469                         /* a & (a ^ b) -> a & ~b */
3470                         dbg_info *dbg  = get_irn_dbg_info(n);
3471                         ir_node *block = get_nodes_block(n);
3472
3473                         br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3474                         n  = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3475                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3476                         return n;
3477                 }
3478                 if (br == a) {
3479                         /* a & (b ^ a) -> a & ~b */
3480                         dbg_info *dbg  = get_irn_dbg_info(n);
3481                         ir_node *block = get_nodes_block(n);
3482
3483                         bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3484                         n  = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3485                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3486                         return n;
3487                 }
3488         }
3489         if (is_Not(a) && is_Not(b)) {
3490                 /* ~a & ~b = ~(a|b) */
3491                 ir_node *block = get_nodes_block(n);
3492                 ir_mode *mode = get_irn_mode(n);
3493
3494                 a = get_Not_op(a);
3495                 b = get_Not_op(b);
3496                 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3497                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3498                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3499                 return n;
3500         }
3501
3502         n = transform_bitwise_distributive(n, transform_node_And);
3503
3504         return n;
3505 }  /* transform_node_And */
3506
3507 /**
3508  * Transform an Eor.
3509  */
3510 static ir_node *transform_node_Eor(ir_node *n) {
3511         ir_node *c, *oldn = n;
3512         ir_node *a = get_Eor_left(n);
3513         ir_node *b = get_Eor_right(n);
3514         ir_mode *mode = get_irn_mode(n);
3515
3516         HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3517
3518         /* we can evaluate 2 Projs of the same Cmp */
3519         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3520                 ir_node *pred_a = get_Proj_pred(a);
3521                 ir_node *pred_b = get_Proj_pred(b);
3522                 if(pred_a == pred_b) {
3523                         dbg_info *dbgi  = get_irn_dbg_info(n);
3524                         ir_node  *block = get_nodes_block(pred_a);
3525                         pn_Cmp pn_a     = get_Proj_proj(a);
3526                         pn_Cmp pn_b     = get_Proj_proj(b);
3527                         /* yes, we can simply calculate with pncs */
3528                         pn_Cmp new_pnc  = pn_a ^ pn_b;
3529
3530                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3531                                            new_pnc);
3532                 }
3533         }
3534
3535         if (a == b) {
3536                 /* a ^ a = 0 */
3537                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph,
3538                                  get_mode_null(mode));
3539                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3540         } else if (mode == mode_b &&
3541                         is_Proj(a) &&
3542                         is_Const(b) && is_Const_one(b) &&
3543                         is_Cmp(get_Proj_pred(a))) {
3544                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3545                 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3546                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3547
3548                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3549         } else if (is_Const(b)) {
3550                 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3551                         ir_node  *cnst   = new_Const(tarval_not(get_Const_tarval(b)));
3552                         ir_node  *not_op = get_Not_op(a);
3553                         dbg_info *dbg    = get_irn_dbg_info(n);
3554                         ir_graph *irg    = current_ir_graph;
3555                         ir_node  *block  = get_nodes_block(n);
3556                         ir_mode  *mode   = get_irn_mode(n);
3557                         n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3558                         return n;
3559                 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3560                         n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3561                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3562                 }
3563         } else {
3564                 n = transform_bitwise_distributive(n, transform_node_Eor);
3565         }
3566
3567         return n;
3568 }  /* transform_node_Eor */
3569
3570 /**
3571  * Transform a Not.
3572  */
3573 static ir_node *transform_node_Not(ir_node *n) {
3574         ir_node *c, *oldn = n;
3575         ir_node *a    = get_Not_op(n);
3576         ir_mode *mode = get_irn_mode(n);
3577
3578         HANDLE_UNOP_PHI(tarval_not,a,c);
3579
3580         /* check for a boolean Not */
3581         if (mode == mode_b &&
3582                         is_Proj(a) &&
3583                         is_Cmp(get_Proj_pred(a))) {
3584                 /* We negate a Cmp. The Cmp has the negated result anyways! */
3585                 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3586                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3587                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3588                 return n;
3589         }
3590         if  (is_Eor(a)) {
3591                 ir_node *eor_b = get_Eor_right(a);
3592                 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3593                         ir_node  *cnst  = new_Const(tarval_not(get_Const_tarval(eor_b)));
3594                         ir_node  *eor_a = get_Eor_left(a);
3595                         dbg_info *dbg   = get_irn_dbg_info(n);
3596                         ir_graph *irg   = current_ir_graph;
3597                         ir_node  *block = get_nodes_block(n);
3598                         ir_mode  *mode  = get_irn_mode(n);
3599                         n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3600                         return n;
3601                 }
3602         }
3603         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3604                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3605                         dbg_info *dbg   = get_irn_dbg_info(n);
3606                         ir_graph *irg   = current_ir_graph;
3607                         ir_node  *block = get_nodes_block(n);
3608                         ir_node  *add_l = get_Minus_op(a);
3609                         ir_node  *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3610                         n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3611                 } else if (is_Add(a)) {
3612                         ir_node *add_r = get_Add_right(a);
3613                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3614                                 /* ~(x + -1) = -x */
3615                                 ir_node *op = get_Add_left(a);
3616                                 ir_node *blk = get_nodes_block(n);
3617                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3618                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3619                         }
3620                 }
3621         }
3622         return n;
3623 }  /* transform_node_Not */
3624
3625 /**
3626  * Transform a Minus.
3627  * Optimize:
3628  *   -(~x) = x + 1
3629  *   -(a-b) = b - a
3630  *   -(a >>u (size-1)) = a >>s (size-1)
3631  *   -(a >>s (size-1)) = a >>u (size-1)
3632  *   -(a * const) -> a * -const
3633  */
3634 static ir_node *transform_node_Minus(ir_node *n) {
3635         ir_node *c, *oldn = n;
3636         ir_node *a = get_Minus_op(n);
3637         ir_mode *mode;
3638
3639         HANDLE_UNOP_PHI(tarval_neg,a,c);
3640
3641         mode = get_irn_mode(a);
3642         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3643                 /* the following rules are only to twos-complement */
3644                 if (is_Not(a)) {
3645                         /* -(~x) = x + 1 */
3646                         ir_node *op   = get_Not_op(a);
3647                         tarval *tv    = get_mode_one(mode);
3648                         ir_node *blk  = get_nodes_block(n);
3649                         ir_node *c    = new_Const(tv);
3650                         n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3651                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3652                         return n;
3653                 }
3654                 if (is_Shr(a)) {
3655                         ir_node *c = get_Shr_right(a);
3656
3657                         if (is_Const(c)) {
3658                                 tarval *tv = get_Const_tarval(c);
3659
3660                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3661                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3662                                         ir_node *v = get_Shr_left(a);
3663
3664                                         n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3665                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3666                                         return n;
3667                                 }
3668                         }
3669                 }
3670                 if (is_Shrs(a)) {
3671                         ir_node *c = get_Shrs_right(a);
3672
3673                         if (is_Const(c)) {
3674                                 tarval *tv = get_Const_tarval(c);
3675
3676                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3677                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3678                                         ir_node *v = get_Shrs_left(a);
3679
3680                                         n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3681                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3682                                         return n;
3683                                 }
3684                         }
3685                 }
3686         }
3687         if (is_Sub(a)) {
3688                 /* - (a-b) = b - a */
3689                 ir_node *la  = get_Sub_left(a);
3690                 ir_node *ra  = get_Sub_right(a);
3691                 ir_node *blk = get_nodes_block(n);
3692
3693                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3694                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3695                 return n;
3696         }
3697
3698         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3699                 ir_node *mul_l = get_Mul_left(a);
3700                 ir_node *mul_r = get_Mul_right(a);
3701                 tarval  *tv    = value_of(mul_r);
3702                 if (tv != tarval_bad) {
3703                         tv = tarval_neg(tv);
3704                         if (tv != tarval_bad) {
3705                                 ir_node  *cnst  = new_Const(tv);
3706                                 dbg_info *dbg   = get_irn_dbg_info(a);
3707                                 ir_graph *irg   = current_ir_graph;
3708                                 ir_node  *block = get_nodes_block(a);
3709                                 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3710                                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3711                                 return n;
3712                         }
3713                 }
3714         }
3715
3716         return n;
3717 }  /* transform_node_Minus */
3718
3719 /**
3720  * Transform a Cast_type(Const) into a new Const_type
3721  */
3722 static ir_node *transform_node_Cast(ir_node *n) {
3723         ir_node *oldn = n;
3724         ir_node *pred = get_Cast_op(n);
3725         ir_type *tp = get_irn_type(n);
3726
3727         if (is_Const(pred) && get_Const_type(pred) != tp) {
3728                 n = new_rd_Const_type(NULL, current_ir_graph, get_Const_tarval(pred), tp);
3729                 DBG_OPT_CSTEVAL(oldn, n);
3730         } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3731                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3732                         get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3733                 DBG_OPT_CSTEVAL(oldn, n);
3734         }
3735
3736         return n;
3737 }  /* transform_node_Cast */
3738
3739 /**
3740  * Transform a Proj(Load) with a non-null address.
3741  */
3742 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3743         if (get_opt_ldst_only_null_ptr_exceptions()) {
3744                 if (get_irn_mode(proj) == mode_X) {
3745                         ir_node *load = get_Proj_pred(proj);
3746
3747                         /* get the Load address */
3748                         const ir_node *addr = get_Load_ptr(load);
3749                         const ir_node *confirm;
3750
3751                         if (value_not_null(addr, &confirm)) {
3752                                 if (confirm == NULL) {
3753                                         /* this node may float if it did not depend on a Confirm */
3754                                         set_irn_pinned(load, op_pin_state_floats);
3755                                 }
3756                                 if (get_Proj_proj(proj) == pn_Load_X_except) {
3757                                         DBG_OPT_EXC_REM(proj);
3758                                         return get_irg_bad(current_ir_graph);
3759                                 } else {
3760                                         ir_node *blk = get_nodes_block(load);
3761                                         return new_r_Jmp(current_ir_graph, blk);
3762                                 }
3763                         }
3764                 }
3765         }
3766         return proj;
3767 }  /* transform_node_Proj_Load */
3768
3769 /**
3770  * Transform a Proj(Store) with a non-null address.
3771  */
3772 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3773         if (get_opt_ldst_only_null_ptr_exceptions()) {
3774                 if (get_irn_mode(proj) == mode_X) {
3775                         ir_node *store = get_Proj_pred(proj);
3776
3777                         /* get the load/store address */
3778                         const ir_node *addr = get_Store_ptr(store);
3779                         const ir_node *confirm;
3780
3781                         if (value_not_null(addr, &confirm)) {
3782                                 if (confirm == NULL) {
3783                                         /* this node may float if it did not depend on a Confirm */
3784                                         set_irn_pinned(store, op_pin_state_floats);
3785                                 }
3786                                 if (get_Proj_proj(proj) == pn_Store_X_except) {
3787                                         DBG_OPT_EXC_REM(proj);
3788                                         return get_irg_bad(current_ir_graph);
3789                                 } else {
3790                                         ir_node *blk = get_nodes_block(store);
3791                                         return new_r_Jmp(current_ir_graph, blk);
3792                                 }
3793                         }
3794                 }
3795         }
3796         return proj;
3797 }  /* transform_node_Proj_Store */
3798
3799 /**
3800  * Transform a Proj(Div) with a non-zero value.
3801  * Removes the exceptions and routes the memory to the NoMem node.
3802  */
3803 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3804         ir_node *div = get_Proj_pred(proj);
3805         ir_node *b   = get_Div_right(div);
3806         ir_node *res, *new_mem;
3807         const ir_node *confirm;
3808         long proj_nr;
3809
3810         if (value_not_zero(b, &confirm)) {
3811                 /* div(x, y) && y != 0 */
3812                 if (confirm == NULL) {
3813                         /* we are sure we have a Const != 0 */
3814                         new_mem = get_Div_mem(div);
3815                         new_mem = skip_Pin(new_mem);
3816                         set_Div_mem(div, new_mem);
3817                         set_irn_pinned(div, op_pin_state_floats);
3818                 }
3819
3820                 proj_nr = get_Proj_proj(proj);
3821                 switch (proj_nr) {
3822                 case pn_Div_X_regular:
3823                         return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3824
3825                 case pn_Div_X_except:
3826                         /* we found an exception handler, remove it */
3827                         DBG_OPT_EXC_REM(proj);
3828                         return new_Bad();
3829
3830                 case pn_Div_M:
3831                         res = get_Div_mem(div);
3832                         new_mem = get_irg_no_mem(current_ir_graph);
3833
3834                         if (confirm) {
3835                                 /* This node can only float up to the Confirm block */
3836                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3837                         }
3838                         set_irn_pinned(div, op_pin_state_floats);
3839                         /* this is a Div without exception, we can remove the memory edge */
3840                         set_Div_mem(div, new_mem);
3841                         return res;
3842                 }
3843         }
3844         return proj;
3845 }  /* transform_node_Proj_Div */
3846
3847 /**
3848  * Transform a Proj(Mod) with a non-zero value.
3849  * Removes the exceptions and routes the memory to the NoMem node.
3850  */
3851 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3852         ir_node *mod = get_Proj_pred(proj);
3853         ir_node *b   = get_Mod_right(mod);
3854         ir_node *res, *new_mem;
3855         const ir_node *confirm;
3856         long proj_nr;
3857
3858         if (value_not_zero(b, &confirm)) {
3859                 /* mod(x, y) && y != 0 */
3860                 proj_nr = get_Proj_proj(proj);
3861
3862                 if (confirm == NULL) {
3863                         /* we are sure we have a Const != 0 */
3864                         new_mem = get_Mod_mem(mod);
3865                         new_mem = skip_Pin(new_mem);
3866                         set_Mod_mem(mod, new_mem);
3867                         set_irn_pinned(mod, op_pin_state_floats);
3868                 }
3869
3870                 switch (proj_nr) {
3871
3872                 case pn_Mod_X_regular:
3873                         return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3874
3875                 case pn_Mod_X_except:
3876                         /* we found an exception handler, remove it */
3877                         DBG_OPT_EXC_REM(proj);
3878                         return new_Bad();
3879
3880                 case pn_Mod_M:
3881                         res = get_Mod_mem(mod);
3882                         new_mem = get_irg_no_mem(current_ir_graph);
3883
3884                         if (confirm) {
3885                                 /* This node can only float up to the Confirm block */
3886                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3887                         }
3888                         /* this is a Mod without exception, we can remove the memory edge */
3889                         set_Mod_mem(mod, new_mem);
3890                         return res;
3891                 case pn_Mod_res:
3892                         if (get_Mod_left(mod) == b) {
3893                                 /* a % a = 0 if a != 0 */
3894                                 ir_mode *mode = get_irn_mode(proj);
3895                                 ir_node *res  = new_Const(get_mode_null(mode));
3896
3897                                 DBG_OPT_CSTEVAL(mod, res);
3898                                 return res;
3899                         }
3900                 }
3901         }
3902         return proj;
3903 }  /* transform_node_Proj_Mod */
3904
3905 /**
3906  * Transform a Proj(DivMod) with a non-zero value.
3907  * Removes the exceptions and routes the memory to the NoMem node.
3908  */
3909 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3910         ir_node *divmod = get_Proj_pred(proj);
3911         ir_node *b      = get_DivMod_right(divmod);
3912         ir_node *res, *new_mem;
3913         const ir_node *confirm;
3914         long proj_nr;
3915
3916         if (value_not_zero(b, &confirm)) {
3917                 /* DivMod(x, y) && y != 0 */
3918                 proj_nr = get_Proj_proj(proj);
3919
3920                 if (confirm == NULL) {
3921                         /* we are sure we have a Const != 0 */
3922                         new_mem = get_DivMod_mem(divmod);
3923                         new_mem = skip_Pin(new_mem);
3924                         set_DivMod_mem(divmod, new_mem);
3925                         set_irn_pinned(divmod, op_pin_state_floats);
3926                 }
3927
3928                 switch (proj_nr) {
3929
3930                 case pn_DivMod_X_regular:
3931                         return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3932
3933                 case pn_DivMod_X_except:
3934                         /* we found an exception handler, remove it */
3935                         DBG_OPT_EXC_REM(proj);
3936                         return new_Bad();
3937
3938                 case pn_DivMod_M:
3939                         res = get_DivMod_mem(divmod);
3940                         new_mem = get_irg_no_mem(current_ir_graph);
3941
3942                         if (confirm) {
3943                                 /* This node can only float up to the Confirm block */
3944                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3945                         }
3946                         /* this is a DivMod without exception, we can remove the memory edge */
3947                         set_DivMod_mem(divmod, new_mem);
3948                         return res;
3949
3950                 case pn_DivMod_res_mod:
3951                         if (get_DivMod_left(divmod) == b) {
3952                                 /* a % a = 0 if a != 0 */
3953                                 ir_mode *mode = get_irn_mode(proj);
3954                                 ir_node *res  = new_Const(get_mode_null(mode));
3955
3956                                 DBG_OPT_CSTEVAL(divmod, res);
3957                                 return res;
3958                         }
3959                 }
3960         }
3961         return proj;
3962 }  /* transform_node_Proj_DivMod */
3963
3964 /**
3965  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3966  */
3967 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3968         if (get_opt_unreachable_code()) {
3969                 ir_node *n = get_Proj_pred(proj);
3970                 ir_node *b = get_Cond_selector(n);
3971
3972                 if (mode_is_int(get_irn_mode(b))) {
3973                         tarval *tb = value_of(b);
3974
3975                         if (tb != tarval_bad) {
3976                                 /* we have a constant switch */
3977                                 long num = get_Proj_proj(proj);
3978
3979                                 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3980                                         if (get_tarval_long(tb) == num) {
3981                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
3982                                                  * in a block. This case is optimized away in optimize_cf(). */
3983                                                 return proj;
3984                                         } else {
3985                                                 /* this case will NEVER be taken, kill it */
3986                                                 return get_irg_bad(current_ir_graph);
3987                                         }
3988                                 }
3989                         }
3990                 }
3991         }
3992         return proj;
3993 }  /* transform_node_Proj_Cond */
3994
3995 /**
3996  * Create a 0 constant of given mode.
3997  */
3998 static ir_node *create_zero_const(ir_mode *mode) {
3999         tarval   *tv    = get_mode_null(mode);
4000         ir_node  *cnst  = new_Const(tv);
4001
4002         return cnst;
4003 }
4004
4005 /* the order of the values is important! */
4006 typedef enum const_class {
4007         const_const = 0,
4008         const_like  = 1,
4009         const_other = 2
4010 } const_class;
4011
4012 static const_class classify_const(const ir_node* n)
4013 {
4014         if (is_Const(n))         return const_const;
4015         if (is_irn_constlike(n)) return const_like;
4016         return const_other;
4017 }
4018
4019 /**
4020  * Determines whether r is more constlike or has a larger index (in that order)
4021  * than l.
4022  */
4023 static int operands_are_normalized(const ir_node *l, const ir_node *r)
4024 {
4025         const const_class l_order = classify_const(l);
4026         const const_class r_order = classify_const(r);
4027         return
4028                 l_order > r_order ||
4029                 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4030 }
4031
4032 /**
4033  * Normalizes and optimizes Cmp nodes.
4034  */
4035 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
4036         ir_node      *n      = get_Proj_pred(proj);
4037         ir_node      *left   = get_Cmp_left(n);
4038         ir_node      *right  = get_Cmp_right(n);
4039         tarval      *tv      = NULL;
4040         int          changed = 0;
4041         ir_mode     *mode    = NULL;
4042         long         proj_nr = get_Proj_proj(proj);
4043
4044         /* we can evaluate some cases directly */
4045         switch (proj_nr) {
4046         case pn_Cmp_False:
4047                 return new_Const(get_tarval_b_false());
4048         case pn_Cmp_True:
4049                 return new_Const(get_tarval_b_true());
4050         case pn_Cmp_Leg:
4051                 if (!mode_is_float(get_irn_mode(left)))
4052                         return new_Const(get_tarval_b_true());
4053                 break;
4054         default:
4055                 break;
4056         }
4057
4058         /* remove Casts of both sides */
4059         left  = skip_Cast(left);
4060         right = skip_Cast(right);
4061
4062         /* Remove unnecessary conversions */
4063         /* TODO handle constants */
4064         if (is_Conv(left) && is_Conv(right)) {
4065                 ir_mode *mode        = get_irn_mode(left);
4066                 ir_node *op_left     = get_Conv_op(left);
4067                 ir_node *op_right    = get_Conv_op(right);
4068                 ir_mode *mode_left   = get_irn_mode(op_left);
4069                 ir_mode *mode_right  = get_irn_mode(op_right);
4070
4071                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4072                                 && mode_left != mode_b && mode_right != mode_b) {
4073                         ir_graph *irg   = current_ir_graph;
4074                         ir_node  *block = get_nodes_block(n);
4075
4076                         if (mode_left == mode_right) {
4077                                 left  = op_left;
4078                                 right = op_right;
4079                                 changed |= 1;
4080                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4081                         } else if (smaller_mode(mode_left, mode_right)) {
4082                                 left  = new_r_Conv(irg, block, op_left, mode_right);
4083                                 right = op_right;
4084                                 changed |= 1;
4085                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4086                         } else if (smaller_mode(mode_right, mode_left)) {
4087                                 left  = op_left;
4088                                 right = new_r_Conv(irg, block, op_right, mode_left);
4089                                 changed |= 1;
4090                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4091                         }
4092                 }
4093         }
4094
4095         /* remove operation on both sides if possible */
4096         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4097                 /*
4098                  * The following operations are NOT safe for floating point operations, for instance
4099                  * 1.0 + inf == 2.0 + inf, =/=> x == y
4100                  */
4101                 if (mode_is_int(get_irn_mode(left))) {
4102                         unsigned lop = get_irn_opcode(left);
4103
4104                         if (lop == get_irn_opcode(right)) {
4105                                 ir_node *ll, *lr, *rl, *rr;
4106
4107                                 /* same operation on both sides, try to remove */
4108                                 switch (lop) {
4109                                 case iro_Not:
4110                                 case iro_Minus:
4111                                         /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4112                                         left  = get_unop_op(left);
4113                                         right = get_unop_op(right);
4114                                         changed |= 1;
4115                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4116                                         break;
4117                                 case iro_Add:
4118                                         ll = get_Add_left(left);
4119                                         lr = get_Add_right(left);
4120                                         rl = get_Add_left(right);
4121                                         rr = get_Add_right(right);
4122
4123                                         if (ll == rl) {
4124                                                 /* X + a CMP X + b ==> a CMP b */
4125                                                 left  = lr;
4126                                                 right = rr;
4127                                                 changed |= 1;
4128                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4129                                         } else if (ll == rr) {
4130                                                 /* X + a CMP b + X ==> a CMP b */
4131                                                 left  = lr;
4132                                                 right = rl;
4133                                                 changed |= 1;
4134                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4135                                         } else if (lr == rl) {
4136                                                 /* a + X CMP X + b ==> a CMP b */
4137                                                 left  = ll;
4138                                                 right = rr;
4139                                                 changed |= 1;
4140                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4141                                         } else if (lr == rr) {
4142                                                 /* a + X CMP b + X ==> a CMP b */
4143                                                 left  = ll;
4144                                                 right = rl;
4145                                                 changed |= 1;
4146                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4147                                         }
4148                                         break;
4149                                 case iro_Sub:
4150                                         ll = get_Sub_left(left);
4151                                         lr = get_Sub_right(left);
4152                                         rl = get_Sub_left(right);
4153                                         rr = get_Sub_right(right);
4154
4155                                         if (ll == rl) {
4156                                                 /* X - a CMP X - b ==> a CMP b */
4157                                                 left  = lr;
4158                                                 right = rr;
4159                                                 changed |= 1;
4160                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4161                                         } else if (lr == rr) {
4162                                                 /* a - X CMP b - X ==> a CMP b */
4163                                                 left  = ll;
4164                                                 right = rl;
4165                                                 changed |= 1;
4166                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4167                                         }
4168                                         break;
4169                                 case iro_Rotl:
4170                                         if (get_Rotl_right(left) == get_Rotl_right(right)) {
4171                                                 /* a ROTL X CMP b ROTL X ==> a CMP b */
4172                                                 left  = get_Rotl_left(left);
4173                                                 right = get_Rotl_left(right);
4174                                                 changed |= 1;
4175                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4176                                         }
4177                                         break;
4178                                 default:
4179                                         break;
4180                                 }
4181                         }
4182
4183                         /* X+A == A, A+X == A, A-X == A -> X == 0 */
4184                         if (is_Add(left) || is_Sub(left)) {
4185                                 ir_node *ll = get_binop_left(left);
4186                                 ir_node *lr = get_binop_right(left);
4187
4188                                 if (lr == right && is_Add(left)) {
4189                                         ir_node *tmp = ll;
4190                                         ll = lr;
4191                                         lr = tmp;
4192                                 }
4193                                 if (ll == right) {
4194                                         left     = lr;
4195                                         right    = create_zero_const(get_irn_mode(left));
4196                                         changed |= 1;
4197                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4198                                 }
4199                         }
4200                         if (is_Add(right) || is_Sub(right)) {
4201                                 ir_node *rl = get_binop_left(right);
4202                                 ir_node *rr = get_binop_right(right);
4203
4204                                 if (rr == left && is_Add(right)) {
4205                                         ir_node *tmp = rl;
4206                                         rl = rr;
4207                                         rr = tmp;
4208                                 }
4209                                 if (rl == left) {
4210                                         left     = rr;
4211                                         right    = create_zero_const(get_irn_mode(left));
4212                                         changed |= 1;
4213                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4214                                 }
4215                         }
4216                         if (is_And(left) && is_Const(right)) {
4217                                 ir_node *ll = get_binop_left(left);
4218                                 ir_node *lr = get_binop_right(left);
4219                                 if (is_Shr(ll) && is_Const(lr)) {
4220                                         /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4221                                         ir_node *block = get_nodes_block(n);
4222                                         ir_mode *mode = get_irn_mode(left);
4223
4224                                         ir_node *llr = get_Shr_right(ll);
4225                                         if (is_Const(llr)) {
4226                                                 ir_graph *irg = current_ir_graph;
4227                                                 dbg_info *dbg = get_irn_dbg_info(left);
4228
4229                                                 tarval *c1    = get_Const_tarval(llr);
4230                                                 tarval *c2    = get_Const_tarval(lr);
4231                                                 tarval *c3    = get_Const_tarval(right);
4232                                                 tarval *mask  = tarval_shl(c2, c1);
4233                                                 tarval *value = tarval_shl(c3, c1);
4234
4235                                                 left  = new_rd_And(dbg, irg, block, get_Shr_left(ll), new_Const(mask), mode);
4236                                                 right = new_Const(value);
4237                                                 changed |= 1;
4238                                         }
4239                                 }
4240                         }
4241                 }  /* mode_is_int(...) */
4242         }  /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4243
4244         /* replace mode_b compares with ands/ors */
4245         if (get_irn_mode(left) == mode_b) {
4246                 ir_graph *irg   = current_ir_graph;
4247                 ir_node  *block = get_nodes_block(n);
4248                 ir_node  *bres;
4249
4250                 switch (proj_nr) {
4251                         case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4252                         case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4253                         case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4254                         case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4255                         case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4256                         case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4257                         default: bres = NULL;
4258                 }
4259                 if (bres) {
4260                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4261                         return bres;
4262                 }
4263         }
4264
4265         /*
4266          * First step: normalize the compare op
4267          * by placing the constant on the right side
4268          * or moving the lower address node to the left.
4269          */
4270         if (!operands_are_normalized(left, right)) {
4271                 ir_node *t = left;
4272
4273                 left  = right;
4274                 right = t;
4275
4276                 proj_nr = get_inversed_pnc(proj_nr);
4277                 changed |= 1;
4278         }
4279
4280         /*
4281          * Second step: Try to reduce the magnitude
4282          * of a constant. This may help to generate better code
4283          * later and may help to normalize more compares.
4284          * Of course this is only possible for integer values.
4285          */
4286         tv = value_of(right);
4287         if (tv != tarval_bad) {
4288                 mode = get_irn_mode(right);
4289
4290                 /* TODO extend to arbitrary constants */
4291                 if (is_Conv(left) && tarval_is_null(tv)) {
4292                         ir_node *op      = get_Conv_op(left);
4293                         ir_mode *op_mode = get_irn_mode(op);
4294
4295                         /*
4296                          * UpConv(x) REL 0  ==> x REL 0
4297                          */
4298                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4299                             ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4300                                  mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4301                                 tv   = get_mode_null(op_mode);
4302                                 left = op;
4303                                 mode = op_mode;
4304                                 changed |= 2;
4305                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4306                         }
4307                 }
4308
4309                 if (tv != tarval_bad) {
4310                         /* the following optimization is possible on modes without Overflow
4311                          * on Unary Minus or on == and !=:
4312                          * -a CMP c  ==>  a swap(CMP) -c
4313                          *
4314                          * Beware: for two-complement Overflow may occur, so only == and != can
4315                          * be optimized, see this:
4316                          * -MININT < 0 =/=> MININT > 0 !!!
4317                          */
4318                         if (is_Minus(left) &&
4319                                 (!mode_overflow_on_unary_Minus(mode) ||
4320                                 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4321                                 tv = tarval_neg(tv);
4322
4323                                 if (tv != tarval_bad) {
4324                                         left = get_Minus_op(left);
4325                                         proj_nr = get_inversed_pnc(proj_nr);
4326                                         changed |= 2;
4327                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4328                                 }
4329                         } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4330                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
4331                                 tv = tarval_not(tv);
4332
4333                                 if (tv != tarval_bad) {
4334                                         left = get_Not_op(left);
4335                                         changed |= 2;
4336                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4337                                 }
4338                         }
4339
4340                         /* for integer modes, we have more */
4341                         if (mode_is_int(mode)) {
4342                                 /* Ne includes Unordered which is not possible on integers.
4343                                  * However, frontends often use this wrong, so fix it here */
4344                                 if (proj_nr & pn_Cmp_Uo) {
4345                                         proj_nr &= ~pn_Cmp_Uo;
4346                                         set_Proj_proj(proj, proj_nr);
4347                                 }
4348
4349                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
4350                                 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4351                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4352                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4353
4354                                         if (tv != tarval_bad) {
4355                                                 proj_nr ^= pn_Cmp_Eq;
4356                                                 changed |= 2;
4357                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4358                                         }
4359                                 }
4360                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
4361                                 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4362                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4363                                         tv = tarval_add(tv, get_mode_one(mode));
4364
4365                                         if (tv != tarval_bad) {
4366                                                 proj_nr ^= pn_Cmp_Eq;
4367                                                 changed |= 2;
4368                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4369                                         }
4370                                 }
4371
4372                                 /* the following reassociations work only for == and != */
4373                                 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4374
4375 #if 0 /* Might be not that good in general */
4376                                         /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
4377                                         if (tarval_is_null(tv) && is_Sub(left)) {
4378                                                 right = get_Sub_right(left);
4379                                                 left  = get_Sub_left(left);
4380
4381                                                 tv = value_of(right);
4382                                                 changed = 1;
4383                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4384                                         }
4385 #endif
4386
4387                                         if (tv != tarval_bad) {
4388                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
4389                                                 if (is_Sub(left)) {
4390                                                         ir_node *c1 = get_Sub_right(left);
4391                                                         tarval *tv2 = value_of(c1);
4392
4393                                                         if (tv2 != tarval_bad) {
4394                                                                 tv2 = tarval_add(tv, value_of(c1));
4395
4396                                                                 if (tv2 != tarval_bad) {
4397                                                                         left    = get_Sub_left(left);
4398                                                                         tv      = tv2;
4399                                                                         changed |= 2;
4400                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4401                                                                 }
4402                                                         }
4403                                                 }
4404                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
4405                                                 else if (is_Add(left)) {
4406                                                         ir_node *a_l = get_Add_left(left);
4407                                                         ir_node *a_r = get_Add_right(left);
4408                                                         ir_node *a;
4409                                                         tarval *tv2;
4410
4411                                                         if (is_Const(a_l)) {
4412                                                                 a = a_r;
4413                                                                 tv2 = value_of(a_l);
4414                                                         } else {
4415                                                                 a = a_l;
4416                                                                 tv2 = value_of(a_r);
4417                                                         }
4418
4419                                                         if (tv2 != tarval_bad) {
4420                                                                 tv2 = tarval_sub(tv, tv2, NULL);
4421
4422                                                                 if (tv2 != tarval_bad) {
4423                                                                         left    = a;
4424                                                                         tv      = tv2;
4425                                                                         changed |= 2;
4426                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4427                                                                 }
4428                                                         }
4429                                                 }
4430                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
4431                                                 else if (is_Minus(left)) {
4432                                                         tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4433
4434                                                         if (tv2 != tarval_bad) {
4435                                                                 left    = get_Minus_op(left);
4436                                                                 tv      = tv2;
4437                                                                 changed |= 2;
4438                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4439                                                         }
4440                                                 }
4441                                         }
4442                                 } /* == or != */
4443                                 /* the following reassociations work only for <= */
4444                                 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4445                                         if (tv != tarval_bad) {
4446                                                 /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
4447                                                 if (is_Abs(left)) { // TODO something is missing here
4448                                                 }
4449                                         }
4450                                 }
4451                         } /* mode_is_int */
4452
4453                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4454                                 switch (get_irn_opcode(left)) {
4455                                         ir_node *c1;
4456
4457                                 case iro_And:
4458                                         c1 = get_And_right(left);
4459                                         if (is_Const(c1)) {
4460                                                 /*
4461                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4462                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4463                                                  */
4464                                                 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4465                                                 if (mask != tv) {
4466                                                         /* TODO: move to constant evaluation */
4467                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4468                                                         c1 = new_Const(tv);
4469                                                         DBG_OPT_CSTEVAL(proj, c1);
4470                                                         return c1;
4471                                                 }
4472
4473                                                 if (tarval_is_single_bit(tv)) {
4474                                                         /*
4475                                                          * optimization for AND:
4476                                                          * Optimize:
4477                                                          *   And(x, C) == C  ==>  And(x, C) != 0
4478                                                          *   And(x, C) != C  ==>  And(X, C) == 0
4479                                                          *
4480                                                          * if C is a single Bit constant.
4481                                                          */
4482
4483                                                         /* check for Constant's match. We have check hare the tarvals,
4484                                                            because our const might be changed */
4485                                                         if (get_Const_tarval(c1) == tv) {
4486                                                                 /* fine: do the transformation */
4487                                                                 tv = get_mode_null(get_tarval_mode(tv));
4488                                                                 proj_nr ^= pn_Cmp_Leg;
4489                                                                 changed |= 2;
4490                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4491                                                         }
4492                                                 }
4493                                         }
4494                                         break;
4495                                 case iro_Or:
4496                                         c1 = get_Or_right(left);
4497                                         if (is_Const(c1) && tarval_is_null(tv)) {
4498                                                 /*
4499                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
4500                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
4501                                                  */
4502                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
4503                                                         /* TODO: move to constant evaluation */
4504                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4505                                                         c1 = new_Const(tv);
4506                                                         DBG_OPT_CSTEVAL(proj, c1);
4507                                                         return c1;
4508                                                 }
4509                                         }
4510                                         break;
4511                                 case iro_Shl:
4512                                         /*
4513                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
4514                                          *                             FALSE                       else
4515                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
4516                                          *                             TRUE                        else
4517                                          */
4518                                         c1 = get_Shl_right(left);
4519                                         if (is_Const(c1)) {
4520                                                 tarval  *tv1    = get_Const_tarval(c1);
4521                                                 ir_mode *mode   = get_irn_mode(left);
4522                                                 tarval  *minus1 = get_mode_all_one(mode);
4523                                                 tarval  *amask  = tarval_shr(minus1, tv1);
4524                                                 tarval  *cmask  = tarval_shl(minus1, tv1);
4525                                                 ir_node *sl, *blk;
4526
4527                                                 if (tarval_and(tv, cmask) != tv) {
4528                                                         /* condition not met */
4529                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4530                                                         c1 = new_Const(tv);
4531                                                         DBG_OPT_CSTEVAL(proj, c1);
4532                                                         return c1;
4533                                                 }
4534                                                 sl   = get_Shl_left(left);
4535                                                 blk  = get_nodes_block(n);
4536                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4537                                                 tv   = tarval_shr(tv, tv1);
4538                                                 changed |= 2;
4539                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4540                                         }
4541                                         break;
4542                                 case iro_Shr:
4543                                         /*
4544                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
4545                                          *                             FALSE                       else
4546                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
4547                                          *                             TRUE                        else
4548                                          */
4549                                         c1 = get_Shr_right(left);
4550                                         if (is_Const(c1)) {
4551                                                 tarval  *tv1    = get_Const_tarval(c1);
4552                                                 ir_mode *mode   = get_irn_mode(left);
4553                                                 tarval  *minus1 = get_mode_all_one(mode);
4554                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4555                                                 tarval  *cmask  = tarval_shr(minus1, tv1);
4556                                                 ir_node *sl, *blk;
4557
4558                                                 if (tarval_and(tv, cmask) != tv) {
4559                                                         /* condition not met */
4560                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4561                                                         c1 = new_Const(tv);
4562                                                         DBG_OPT_CSTEVAL(proj, c1);
4563                                                         return c1;
4564                                                 }
4565                                                 sl   = get_Shr_left(left);
4566                                                 blk  = get_nodes_block(n);
4567                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4568                                                 tv   = tarval_shl(tv, tv1);
4569                                                 changed |= 2;
4570                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4571                                         }
4572                                         break;
4573                                 case iro_Shrs:
4574                                         /*
4575                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4576                                          *                             FALSE                       else
4577                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4578                                          *                             TRUE                        else
4579                                          */
4580                                         c1 = get_Shrs_right(left);
4581                                         if (is_Const(c1)) {
4582                                                 tarval  *tv1    = get_Const_tarval(c1);
4583                                                 ir_mode *mode   = get_irn_mode(left);
4584                                                 tarval  *minus1 = get_mode_all_one(mode);
4585                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4586                                                 tarval  *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4587                                                 ir_node *sl, *blk;
4588
4589                                                 cond = tarval_sub(cond, tv1, NULL);
4590                                                 cond = tarval_shrs(tv, cond);
4591
4592                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4593                                                         /* condition not met */
4594                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4595                                                         c1 = new_Const(tv);
4596                                                         DBG_OPT_CSTEVAL(proj, c1);
4597                                                         return c1;
4598                                                 }
4599                                                 sl   = get_Shrs_left(left);
4600                                                 blk  = get_nodes_block(n);
4601                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4602                                                 tv   = tarval_shl(tv, tv1);
4603                                                 changed |= 2;
4604                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4605                                         }
4606                                         break;
4607                                 }  /* switch */
4608                         }
4609                 } /* tarval != bad */
4610         }
4611
4612         if (changed & 2)      /* need a new Const */
4613                 right = new_Const(tv);
4614
4615         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4616                 ir_node *op = get_Proj_pred(left);
4617
4618                 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4619                     (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4620                         ir_node *c = get_binop_right(op);
4621
4622                         if (is_Const(c)) {
4623                                 tarval *tv = get_Const_tarval(c);
4624
4625                                 if (tarval_is_single_bit(tv)) {
4626                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4627                                         ir_node *v    = get_binop_left(op);
4628                                         ir_node *blk  = get_irn_n(op, -1);
4629                                         ir_mode *mode = get_irn_mode(v);
4630
4631                                         tv = tarval_sub(tv, get_mode_one(mode), NULL);
4632                                         left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(tv), mode);
4633                                         changed |= 1;
4634                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4635                                 }
4636                         }
4637                 }
4638         }
4639
4640         if (changed) {
4641                 ir_node *block = get_nodes_block(n);
4642
4643                 /* create a new compare */
4644                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4645                 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4646         }
4647
4648         return proj;
4649 }  /* transform_node_Proj_Cmp */
4650
4651 /**
4652  * Optimize CopyB(mem, x, x) into a Nop.
4653  */
4654 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4655         ir_node *copyb = get_Proj_pred(proj);
4656         ir_node *a     = get_CopyB_dst(copyb);
4657         ir_node *b     = get_CopyB_src(copyb);
4658
4659         if (a == b) {
4660                 switch (get_Proj_proj(proj)) {
4661                 case pn_CopyB_X_regular:
4662                         /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4663                         DBG_OPT_EXC_REM(proj);
4664                         proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4665                         break;
4666                 case pn_CopyB_M_except:
4667                 case pn_CopyB_X_except:
4668                         DBG_OPT_EXC_REM(proj);
4669                         proj = get_irg_bad(current_ir_graph);
4670                         break;
4671                 default:
4672                         break;
4673                 }
4674         }
4675         return proj;
4676 }  /* transform_node_Proj_CopyB */
4677
4678 /**
4679  * Optimize Bounds(idx, idx, upper) into idx.
4680  */
4681 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4682         ir_node *oldn  = proj;
4683         ir_node *bound = get_Proj_pred(proj);
4684         ir_node *idx   = get_Bound_index(bound);
4685         ir_node *pred  = skip_Proj(idx);
4686         int ret_tuple  = 0;
4687
4688         if (idx == get_Bound_lower(bound))
4689                 ret_tuple = 1;
4690         else if (is_Bound(pred)) {
4691                 /*
4692                 * idx was Bounds checked in the same MacroBlock previously,
4693                 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4694                 */
4695                 ir_node *lower = get_Bound_lower(bound);
4696                 ir_node *upper = get_Bound_upper(bound);
4697                 if (get_Bound_lower(pred) == lower &&
4698                         get_Bound_upper(pred) == upper &&
4699                         get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4700                         /*
4701                          * One could expect that we simply return the previous
4702                          * Bound here. However, this would be wrong, as we could
4703                          * add an exception Proj to a new location then.
4704                          * So, we must turn in into a tuple.
4705                          */
4706                         ret_tuple = 1;
4707                 }
4708         }
4709         if (ret_tuple) {
4710                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4711                 switch (get_Proj_proj(proj)) {
4712                 case pn_Bound_M:
4713                         DBG_OPT_EXC_REM(proj);
4714                         proj = get_Bound_mem(bound);
4715                         break;
4716                 case pn_Bound_X_except:
4717                         DBG_OPT_EXC_REM(proj);
4718                         proj = get_irg_bad(current_ir_graph);
4719                         break;
4720                 case pn_Bound_res:
4721                         proj = idx;
4722                         DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4723                         break;
4724                 case pn_Bound_X_regular:
4725                         DBG_OPT_EXC_REM(proj);
4726                         proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4727                         break;
4728                 default:
4729                         break;
4730                 }
4731         }
4732         return proj;
4733 }  /* transform_node_Proj_Bound */
4734
4735 /**
4736  * Does all optimizations on nodes that must be done on it's Proj's
4737  * because of creating new nodes.
4738  */
4739 static ir_node *transform_node_Proj(ir_node *proj) {
4740         ir_node *n = get_Proj_pred(proj);
4741
4742         if (n->op->ops.transform_node_Proj)
4743                 return n->op->ops.transform_node_Proj(proj);
4744         return proj;
4745 }  /* transform_node_Proj */
4746
4747 /**
4748  * Move Confirms down through Phi nodes.
4749  */
4750 static ir_node *transform_node_Phi(ir_node *phi) {
4751         int i, n;
4752         ir_mode *mode = get_irn_mode(phi);
4753
4754         if (mode_is_reference(mode)) {
4755                 n = get_irn_arity(phi);
4756
4757                 /* Beware of Phi0 */
4758                 if (n > 0) {
4759                         ir_node *pred = get_irn_n(phi, 0);
4760                         ir_node *bound, *new_Phi, *block, **in;
4761                         pn_Cmp  pnc;
4762
4763                         if (! is_Confirm(pred))
4764                                 return phi;
4765
4766                         bound = get_Confirm_bound(pred);
4767                         pnc   = get_Confirm_cmp(pred);
4768
4769                         NEW_ARR_A(ir_node *, in, n);
4770                         in[0] = get_Confirm_value(pred);
4771
4772                         for (i = 1; i < n; ++i) {
4773                                 pred = get_irn_n(phi, i);
4774
4775                                 if (! is_Confirm(pred) ||
4776                                         get_Confirm_bound(pred) != bound ||
4777                                         get_Confirm_cmp(pred) != pnc)
4778                                         return phi;
4779                                 in[i] = get_Confirm_value(pred);
4780                         }
4781                         /* move the Confirm nodes "behind" the Phi */
4782                         block = get_irn_n(phi, -1);
4783                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4784                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4785                 }
4786         }
4787         return phi;
4788 }  /* transform_node_Phi */
4789
4790 /**
4791  * Returns the operands of a commutative bin-op, if one operand is
4792  * a const, it is returned as the second one.
4793  */
4794 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4795         ir_node *op_a = get_binop_left(binop);
4796         ir_node *op_b = get_binop_right(binop);
4797
4798         assert(is_op_commutative(get_irn_op(binop)));
4799
4800         if (is_Const(op_a)) {
4801                 *a = op_b;
4802                 *c = op_a;
4803         } else {
4804                 *a = op_a;
4805                 *c = op_b;
4806         }
4807 }  /* get_comm_Binop_Ops */
4808
4809 /**
4810  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4811  * Such pattern may arise in bitfield stores.
4812  *
4813  * value  c4                  value      c4 & c2
4814  *    AND     c3                    AND           c1 | c3
4815  *        OR     c2      ===>               OR
4816  *           AND    c1
4817  *               OR
4818  *
4819  *
4820  * value  c2                 value  c1
4821  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
4822  *        OR
4823  */
4824 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4825         ir_node *and, *c1;
4826         ir_node *or_l, *c2;
4827         ir_node *and_l, *c3;
4828         ir_node *value, *c4;
4829         ir_node *new_and, *new_const, *block;
4830         ir_mode *mode = get_irn_mode(or);
4831
4832         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4833
4834         while (1) {
4835                 get_comm_Binop_Ops(or, &and, &c1);
4836                 if (!is_Const(c1) || !is_And(and))
4837                         return or;
4838
4839                 get_comm_Binop_Ops(and, &or_l, &c2);
4840                 if (!is_Const(c2))
4841                         return or;
4842
4843                 tv1 = get_Const_tarval(c1);
4844                 tv2 = get_Const_tarval(c2);
4845
4846                 tv = tarval_or(tv1, tv2);
4847                 if (tarval_is_all_one(tv)) {
4848                         /* the AND does NOT clear a bit with isn't set by the OR */
4849                         set_Or_left(or, or_l);
4850                         set_Or_right(or, c1);
4851
4852                         /* check for more */
4853                         continue;
4854                 }
4855
4856                 if (!is_Or(or_l))
4857                         return or;
4858
4859                 get_comm_Binop_Ops(or_l, &and_l, &c3);
4860                 if (!is_Const(c3) || !is_And(and_l))
4861                         return or;
4862
4863                 get_comm_Binop_Ops(and_l, &value, &c4);
4864                 if (!is_Const(c4))
4865                         return or;
4866
4867                 /* ok, found the pattern, check for conditions */
4868                 assert(mode == get_irn_mode(and));
4869                 assert(mode == get_irn_mode(or_l));
4870                 assert(mode == get_irn_mode(and_l));
4871
4872                 tv3 = get_Const_tarval(c3);
4873                 tv4 = get_Const_tarval(c4);
4874
4875                 tv = tarval_or(tv4, tv2);
4876                 if (!tarval_is_all_one(tv)) {
4877                         /* have at least one 0 at the same bit position */
4878                         return or;
4879                 }
4880
4881                 n_tv4 = tarval_not(tv4);
4882                 if (tv3 != tarval_and(tv3, n_tv4)) {
4883                         /* bit in the or_mask is outside the and_mask */
4884                         return or;
4885                 }
4886
4887                 n_tv2 = tarval_not(tv2);
4888                 if (tv1 != tarval_and(tv1, n_tv2)) {
4889                         /* bit in the or_mask is outside the and_mask */
4890                         return or;
4891                 }
4892
4893                 /* ok, all conditions met */
4894                 block = get_irn_n(or, -1);
4895
4896                 new_and = new_r_And(current_ir_graph, block,
4897                         value, new_Const(tarval_and(tv4, tv2)), mode);
4898
4899                 new_const = new_Const(tarval_or(tv3, tv1));
4900
4901                 set_Or_left(or, new_and);
4902                 set_Or_right(or, new_const);
4903
4904                 /* check for more */
4905         }
4906 }  /* transform_node_Or_bf_store */
4907
4908 /**
4909  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4910  */
4911 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4912         ir_mode *mode = get_irn_mode(or);
4913         ir_node *shl, *shr, *block;
4914         ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4915         tarval *tv1, *tv2;
4916
4917         if (! mode_is_int(mode))
4918                 return or;
4919
4920         shl = get_binop_left(or);
4921         shr = get_binop_right(or);
4922
4923         if (is_Shr(shl)) {
4924                 if (!is_Shl(shr))
4925                         return or;
4926
4927                 irn = shl;
4928                 shl = shr;
4929                 shr = irn;
4930         } else if (!is_Shl(shl)) {
4931                 return or;
4932         } else if (!is_Shr(shr)) {
4933                 return or;
4934         }
4935         x = get_Shl_left(shl);
4936         if (x != get_Shr_left(shr))
4937                 return or;
4938
4939         c1 = get_Shl_right(shl);
4940         c2 = get_Shr_right(shr);
4941         if (is_Const(c1) && is_Const(c2)) {
4942                 tv1 = get_Const_tarval(c1);
4943                 if (! tarval_is_long(tv1))
4944                         return or;
4945
4946                 tv2 = get_Const_tarval(c2);
4947                 if (! tarval_is_long(tv2))
4948                         return or;
4949
4950                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4951                                 != (int) get_mode_size_bits(mode))
4952                         return or;
4953
4954                 /* yet, condition met */
4955                 block = get_nodes_block(or);
4956
4957                 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4958
4959                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4960                 return n;
4961         }
4962
4963     if (is_Sub(c1)) {
4964             v      = c2;
4965                 sub    = c1;
4966         rotval = sub; /* a Rot right is not supported, so use a rot left */
4967     } else if (is_Sub(c2)) {
4968                 v      = c1;
4969         sub    = c2;
4970         rotval = v;
4971     } else return or;
4972
4973         if (get_Sub_right(sub) != v)
4974                 return or;
4975
4976         c1 = get_Sub_left(sub);
4977         if (!is_Const(c1))
4978                 return or;
4979
4980         tv1 = get_Const_tarval(c1);
4981         if (! tarval_is_long(tv1))
4982                 return or;
4983
4984         if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4985                 return or;
4986
4987         /* yet, condition met */
4988         block = get_nodes_block(or);
4989
4990         n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4991
4992         DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4993         return n;
4994 }  /* transform_node_Or_Rotl */
4995
4996 /**
4997  * Transform an Or.
4998  */
4999 static ir_node *transform_node_Or(ir_node *n) {
5000         ir_node *c, *oldn = n;
5001         ir_node *a = get_Or_left(n);
5002         ir_node *b = get_Or_right(n);
5003         ir_mode *mode;
5004
5005         if (is_Not(a) && is_Not(b)) {
5006                 /* ~a | ~b = ~(a&b) */
5007                 ir_node *block = get_nodes_block(n);
5008
5009                 mode = get_irn_mode(n);
5010                 a = get_Not_op(a);
5011                 b = get_Not_op(b);
5012                 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
5013                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
5014                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5015                 return n;
5016         }
5017
5018         /* we can evaluate 2 Projs of the same Cmp */
5019         if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5020                 ir_node *pred_a = get_Proj_pred(a);
5021                 ir_node *pred_b = get_Proj_pred(b);
5022                 if (pred_a == pred_b) {
5023                         dbg_info *dbgi  = get_irn_dbg_info(n);
5024                         ir_node  *block = get_nodes_block(pred_a);
5025                         pn_Cmp pn_a     = get_Proj_proj(a);
5026                         pn_Cmp pn_b     = get_Proj_proj(b);
5027                         /* yes, we can simply calculate with pncs */
5028                         pn_Cmp new_pnc  = pn_a | pn_b;
5029
5030                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
5031                                            new_pnc);
5032                 }
5033         }
5034
5035         mode = get_irn_mode(n);
5036         HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
5037
5038         n = transform_node_Or_bf_store(n);
5039         n = transform_node_Or_Rotl(n);
5040         if (n != oldn)
5041                 return n;
5042
5043         n = transform_bitwise_distributive(n, transform_node_Or);
5044
5045         return n;
5046 }  /* transform_node_Or */
5047
5048
5049 /* forward */
5050 static ir_node *transform_node(ir_node *n);
5051
5052 /**
5053  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5054  *
5055  * Should be moved to reassociation?
5056  */
5057 static ir_node *transform_node_shift(ir_node *n) {
5058         ir_node *left, *right;
5059         ir_mode *mode;
5060         tarval *tv1, *tv2, *res;
5061         ir_node *in[2], *irn, *block;
5062
5063         left = get_binop_left(n);
5064
5065         /* different operations */
5066         if (get_irn_op(left) != get_irn_op(n))
5067                 return n;
5068
5069         right = get_binop_right(n);
5070         tv1 = value_of(right);
5071         if (tv1 == tarval_bad)
5072                 return n;
5073
5074         tv2 = value_of(get_binop_right(left));
5075         if (tv2 == tarval_bad)
5076                 return n;
5077
5078         res  = tarval_add(tv1, tv2);
5079         mode = get_irn_mode(n);
5080
5081         /* beware: a simple replacement works only, if res < modulo shift */
5082         if (!is_Rotl(n)) {
5083                 int modulo_shf = get_mode_modulo_shift(mode);
5084                 if (modulo_shf > 0) {
5085                         tarval *modulo = new_tarval_from_long(modulo_shf,
5086                                                               get_tarval_mode(res));
5087
5088                         assert(modulo_shf >= (int) get_mode_size_bits(mode));
5089
5090                         /* shifting too much */
5091                         if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5092                                 if (is_Shrs(n)) {
5093                                         ir_graph *irg   = get_irn_irg(n);
5094                                         ir_node  *block = get_nodes_block(n);
5095                                         dbg_info *dbgi  = get_irn_dbg_info(n);
5096                                         ir_mode  *smode  = get_irn_mode(right);
5097                                         ir_node  *cnst  = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5098                                         return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
5099                                                            cnst, mode);
5100                                 }
5101
5102                                 return new_Const(get_mode_null(mode));
5103                         }
5104                 }
5105         } else {
5106                 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5107         }
5108
5109         /* ok, we can replace it */
5110         block = get_nodes_block(n);
5111
5112         in[0] = get_binop_left(left);
5113         in[1] = new_Const(res);
5114
5115         irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5116
5117         DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5118
5119         return transform_node(irn);
5120 }  /* transform_node_shift */
5121
5122 /**
5123  * normalisation: (x & c1) >> c2   to   (x >> c2) & (c1 >> c2)
5124  *  (we can use:
5125  *    - and, or, xor          instead of &
5126  *    - Shl, Shr, Shrs, rotl  instead of >>
5127  *    (with a special case for Or/Xor + Shrs)
5128  */
5129 static ir_node *transform_node_bitop_shift(ir_node *n) {
5130         ir_node  *left;
5131         ir_node  *right = get_binop_right(n);
5132         ir_mode  *mode  = get_irn_mode(n);
5133         ir_node  *bitop_left;
5134         ir_node  *bitop_right;
5135         ir_op    *op_left;
5136         ir_graph *irg;
5137         ir_node  *block;
5138         dbg_info *dbgi;
5139         ir_node  *new_shift;
5140         ir_node  *new_bitop;
5141         ir_node  *new_const;
5142         tarval   *tv1;
5143         tarval   *tv2;
5144         tarval   *tv_shift;
5145
5146         assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5147
5148         if (!is_Const(right))
5149                 return n;
5150
5151         left    = get_binop_left(n);
5152         op_left = get_irn_op(left);
5153         if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5154                 return n;
5155
5156         /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5157         if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5158                 /* TODO: test if sign bit is affectes */
5159                 return n;
5160         }
5161
5162         bitop_right = get_binop_right(left);
5163         if (!is_Const(bitop_right))
5164                 return n;
5165
5166         bitop_left = get_binop_left(left);
5167
5168         irg   = get_irn_irg(n);
5169         block = get_nodes_block(n);
5170         dbgi  = get_irn_dbg_info(n);
5171         tv1   = get_Const_tarval(bitop_right);
5172         tv2   = get_Const_tarval(right);
5173
5174         assert(get_tarval_mode(tv1) == mode);
5175
5176         if (is_Shl(n)) {
5177                 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5178                 tv_shift  = tarval_shl(tv1, tv2);
5179         } else if(is_Shr(n)) {
5180                 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5181                 tv_shift  = tarval_shr(tv1, tv2);
5182         } else if(is_Shrs(n)) {
5183                 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5184                 tv_shift  = tarval_shrs(tv1, tv2);
5185         } else {
5186                 assert(is_Rotl(n));
5187                 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5188                 tv_shift  = tarval_rotl(tv1, tv2);
5189         }
5190
5191         assert(get_tarval_mode(tv_shift) == mode);
5192         new_const = new_Const(tv_shift);
5193
5194         if (op_left == op_And) {
5195                 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5196         } else if(op_left == op_Or) {
5197                 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5198         } else {
5199                 assert(op_left == op_Eor);
5200                 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5201         }
5202
5203         return new_bitop;
5204 }
5205
5206 /**
5207  * normalisation:
5208  *    (x << c1) >> c2  <=>  x OP (c2-c1) & ((-1 << c1) >> c2)
5209  *    also:
5210  *    (x >> c1) << c2  <=>  x OP (c2-c1) & ((-1 >> c1) << c2)
5211  *      (also with x >>s c1  when c1>=c2)
5212  */
5213 static ir_node *transform_node_shl_shr(ir_node *n) {
5214         ir_node  *left;
5215         ir_node  *right = get_binop_right(n);
5216         ir_node  *x;
5217         ir_graph *irg;
5218         ir_node  *block;
5219         ir_mode  *mode;
5220         dbg_info *dbgi;
5221         ir_node  *new_const;
5222         ir_node  *new_shift;
5223         ir_node  *new_and;
5224         tarval   *tv_shl;
5225         tarval   *tv_shr;
5226         tarval   *tv_shift;
5227         tarval   *tv_mask;
5228         pn_Cmp    pnc;
5229         int       need_shrs = 0;
5230
5231         assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5232
5233         if (!is_Const(right))
5234                 return n;
5235
5236         left = get_binop_left(n);
5237         mode = get_irn_mode(n);
5238         if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5239                 ir_node *shr_right = get_binop_right(left);
5240
5241                 if (!is_Const(shr_right))
5242                         return n;
5243
5244                 x      = get_binop_left(left);
5245                 tv_shr = get_Const_tarval(shr_right);
5246                 tv_shl = get_Const_tarval(right);
5247
5248                 if (is_Shrs(left)) {
5249                         /* shrs variant only allowed if c1 >= c2 */
5250                         if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5251                                 return n;
5252
5253                         tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5254                         need_shrs = 1;
5255                 } else {
5256                         tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5257                 }
5258                 tv_mask = tarval_shl(tv_mask, tv_shl);
5259         } else if(is_Shr(n) && is_Shl(left)) {
5260                 ir_node *shl_right = get_Shl_right(left);
5261
5262                 if (!is_Const(shl_right))
5263                         return n;
5264
5265                 x      = get_Shl_left(left);
5266                 tv_shr = get_Const_tarval(right);
5267                 tv_shl = get_Const_tarval(shl_right);
5268
5269                 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5270                 tv_mask = tarval_shr(tv_mask, tv_shr);
5271         } else {
5272                 return n;
5273         }
5274
5275         if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5276                 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5277         }
5278
5279         assert(tv_mask != tarval_bad);
5280         assert(get_tarval_mode(tv_mask) == mode);
5281
5282         irg   = get_irn_irg(n);
5283         block = get_nodes_block(n);
5284         dbgi  = get_irn_dbg_info(n);
5285
5286         pnc = tarval_cmp(tv_shl, tv_shr);
5287         if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5288                 tv_shift  = tarval_sub(tv_shr, tv_shl, NULL);
5289                 new_const = new_Const(tv_shift);
5290                 if (need_shrs) {
5291                         new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5292                 } else {
5293                         new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5294                 }
5295         } else {
5296                 assert(pnc == pn_Cmp_Gt);
5297                 tv_shift  = tarval_sub(tv_shl, tv_shr, NULL);
5298                 new_const = new_Const(tv_shift);
5299                 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5300         }
5301
5302         new_const = new_Const(tv_mask);
5303         new_and   = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5304
5305         return new_and;
5306 }
5307
5308 /**
5309  * Transform a Shr.
5310  */
5311 static ir_node *transform_node_Shr(ir_node *n) {
5312         ir_node *c, *oldn = n;
5313         ir_node *left  = get_Shr_left(n);
5314         ir_node *right = get_Shr_right(n);
5315         ir_mode *mode  = get_irn_mode(n);
5316
5317         HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5318         n = transform_node_shift(n);
5319
5320         if (is_Shr(n))
5321                 n = transform_node_shl_shr(n);
5322         if (is_Shr(n))
5323                 n = transform_node_bitop_shift(n);
5324
5325         return n;
5326 }  /* transform_node_Shr */
5327
5328 /**
5329  * Transform a Shrs.
5330  */
5331 static ir_node *transform_node_Shrs(ir_node *n) {
5332         ir_node *c, *oldn = n;
5333         ir_node *a    = get_Shrs_left(n);
5334         ir_node *b    = get_Shrs_right(n);
5335         ir_mode *mode = get_irn_mode(n);
5336
5337         HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5338         n = transform_node_shift(n);
5339
5340         if (is_Shrs(n))
5341                 n = transform_node_bitop_shift(n);
5342
5343         return n;
5344 }  /* transform_node_Shrs */
5345
5346 /**
5347  * Transform a Shl.
5348  */
5349 static ir_node *transform_node_Shl(ir_node *n) {
5350         ir_node *c, *oldn = n;
5351         ir_node *a    = get_Shl_left(n);
5352         ir_node *b    = get_Shl_right(n);
5353         ir_mode *mode = get_irn_mode(n);
5354
5355         HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5356         n = transform_node_shift(n);
5357
5358         if (is_Shl(n))
5359                 n = transform_node_shl_shr(n);
5360         if (is_Shl(n))
5361                 n = transform_node_bitop_shift(n);
5362
5363         return n;
5364 }  /* transform_node_Shl */
5365
5366 /**
5367  * Transform a Rotl.
5368  */
5369 static ir_node *transform_node_Rotl(ir_node *n) {
5370         ir_node *c, *oldn = n;
5371         ir_node *a    = get_Rotl_left(n);
5372         ir_node *b    = get_Rotl_right(n);
5373         ir_mode *mode = get_irn_mode(n);
5374
5375         HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5376         n = transform_node_shift(n);
5377
5378         if (is_Rotl(n))
5379                 n = transform_node_bitop_shift(n);
5380
5381         return n;
5382 }  /* transform_node_Rotl */
5383
5384 /**
5385  * Transform a Conv.
5386  */
5387 static ir_node *transform_node_Conv(ir_node *n) {
5388         ir_node *c, *oldn = n;
5389         ir_mode *mode = get_irn_mode(n);
5390         ir_node *a    = get_Conv_op(n);
5391
5392         if (mode != mode_b && is_const_Phi(a)) {
5393                 /* Do NOT optimize mode_b Conv's, this leads to remaining
5394                  * Phib nodes later, because the conv_b_lower operation
5395                  * is instantly reverted, when it tries to insert a Convb.
5396                  */
5397                 c = apply_conv_on_phi(a, mode);
5398                 if (c) {
5399                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5400                         return c;
5401                 }
5402         }
5403
5404         if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5405                 return new_r_Unknown(current_ir_graph, mode);
5406         }
5407
5408         if (mode_is_reference(mode) &&
5409                 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5410                 is_Add(a)) {
5411                 ir_node *l = get_Add_left(a);
5412                 ir_node *r = get_Add_right(a);
5413                 dbg_info *dbgi = get_irn_dbg_info(a);
5414                 ir_node *block = get_nodes_block(n);
5415                 if(is_Conv(l)) {
5416                         ir_node *lop = get_Conv_op(l);
5417                         if(get_irn_mode(lop) == mode) {
5418                                 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5419                                 n = new_rd_Add(dbgi, current_ir_graph, block, lop, r, mode);
5420                                 return n;
5421                         }
5422                 }
5423                 if(is_Conv(r)) {
5424                         ir_node *rop = get_Conv_op(r);
5425                         if(get_irn_mode(rop) == mode) {
5426                                 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5427                                 n = new_rd_Add(dbgi, current_ir_graph, block, l, rop, mode);
5428                                 return n;
5429                         }
5430                 }
5431         }
5432
5433         return n;
5434 }  /* transform_node_Conv */
5435
5436 /**
5437  * Remove dead blocks and nodes in dead blocks
5438  * in keep alive list.  We do not generate a new End node.
5439  */
5440 static ir_node *transform_node_End(ir_node *n) {
5441         int i, j, n_keepalives = get_End_n_keepalives(n);
5442         ir_node **in;
5443
5444         NEW_ARR_A(ir_node *, in, n_keepalives);
5445
5446         for (i = j = 0; i < n_keepalives; ++i) {
5447                 ir_node *ka = get_End_keepalive(n, i);
5448                 if (is_Block(ka)) {
5449                         if (! is_Block_dead(ka)) {
5450                                 in[j++] = ka;
5451                         }
5452                         continue;
5453                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5454                         continue;
5455                 } else if (is_Bad(ka)) {
5456                         /* no need to keep Bad */
5457                         continue;
5458                 }
5459                 in[j++] = ka;
5460         }
5461         if (j != n_keepalives)
5462                 set_End_keepalives(n, j, in);
5463         return n;
5464 }  /* transform_node_End */
5465
5466 /** returns 1 if a == -b */
5467 static int is_negated_value(ir_node *a, ir_node *b) {
5468         if (is_Minus(a) && get_Minus_op(a) == b)
5469                 return 1;
5470         if (is_Minus(b) && get_Minus_op(b) == a)
5471                 return 1;
5472         if (is_Sub(a) && is_Sub(b)) {
5473                 ir_node *a_left  = get_Sub_left(a);
5474                 ir_node *a_right = get_Sub_right(a);
5475                 ir_node *b_left  = get_Sub_left(b);
5476                 ir_node *b_right = get_Sub_right(b);
5477
5478                 if (a_left == b_right && a_right == b_left)
5479                         return 1;
5480         }
5481
5482         return 0;
5483 }
5484
5485 /**
5486  * Optimize a Mux into some simpler cases.
5487  */
5488 static ir_node *transform_node_Mux(ir_node *n) {
5489         ir_node *oldn = n, *sel = get_Mux_sel(n);
5490         ir_mode *mode = get_irn_mode(n);
5491         ir_node  *t   = get_Mux_true(n);
5492         ir_node  *f   = get_Mux_false(n);
5493         ir_graph *irg = current_ir_graph;
5494
5495         if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5496                 return n;
5497
5498         if (is_Mux(t)) {
5499                 ir_node*  block = get_nodes_block(n);
5500                 ir_node*  c0    = sel;
5501                 ir_node*  c1    = get_Mux_sel(t);
5502                 ir_node*  t1    = get_Mux_true(t);
5503                 ir_node*  f1    = get_Mux_false(t);
5504                 if (f == f1) {
5505                         /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5506                         ir_node* and_    = new_r_And(irg, block, c0, c1, mode_b);
5507                         ir_node* new_mux = new_r_Mux(irg, block, and_, f1, t1, mode);
5508                         n   = new_mux;
5509                         sel = and_;
5510                         f   = f1;
5511                         t   = t1;
5512                         DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5513                 } else if (f == t1) {
5514                         /* Mux(cond0, Mux(cond1, x, y), x) */
5515                         ir_node* not_c1  = new_r_Not(irg, block, c1, mode_b);
5516                         ir_node* and_    = new_r_And(irg, block, c0, not_c1, mode_b);
5517                         ir_node* new_mux = new_r_Mux(irg, block, and_, t1, f1, mode);
5518                         n   = new_mux;
5519                         sel = and_;
5520                         f   = t1;
5521                         t   = f1;
5522                         DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5523                 }
5524         } else if (is_Mux(f)) {
5525                 ir_node*  block = get_nodes_block(n);
5526                 ir_node*  c0    = sel;
5527                 ir_node*  c1    = get_Mux_sel(f);
5528                 ir_node*  t1    = get_Mux_true(f);
5529                 ir_node*  f1    = get_Mux_false(f);
5530                 if (t == t1) {
5531                         /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5532                         ir_node* or_     = new_r_Or(irg, block, c0, c1, mode_b);
5533                         ir_node* new_mux = new_r_Mux(irg, block, or_, f1, t1, mode);
5534                         n   = new_mux;
5535                         sel = or_;
5536                         f   = f1;
5537                         t   = t1;
5538                         DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5539                 } else if (t == f1) {
5540                         /* Mux(cond0, x, Mux(cond1, y, x)) */
5541                         ir_node* not_c1  = new_r_Not(irg, block, c1, mode_b);
5542                         ir_node* or_     = new_r_Or(irg, block, c0, not_c1, mode_b);
5543                         ir_node* new_mux = new_r_Mux(irg, block, or_, t1, f1, mode);
5544                         n   = new_mux;
5545                         sel = or_;
5546                         f   = t1;
5547                         t   = f1;
5548                         DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5549                 }
5550         }
5551
5552         /* first normalization step: move a possible zero to the false case */
5553         if (is_Proj(sel)) {
5554                 ir_node *cmp = get_Proj_pred(sel);
5555
5556                 if (is_Cmp(cmp)) {
5557                         if (is_Const(t) && is_Const_null(t)) {
5558                                 ir_node *tmp;
5559
5560                                 /* Mux(x, 0, y) => Mux(x, y, 0) */
5561                                 pn_Cmp pnc = get_Proj_proj(sel);
5562                                 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5563                                         get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5564                                 n        = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5565                                 tmp = t;
5566                                 t = f;
5567                                 f = tmp;
5568                         }
5569                 }
5570         }
5571
5572         /* note: after normalization, false can only happen on default */
5573         if (mode == mode_b) {
5574                 dbg_info *dbg   = get_irn_dbg_info(n);
5575                 ir_node  *block = get_nodes_block(n);
5576                 ir_graph *irg   = current_ir_graph;
5577
5578                 if (is_Const(t)) {
5579                         tarval *tv_t = get_Const_tarval(t);
5580                         if (tv_t == tarval_b_true) {
5581                                 if (is_Const(f)) {
5582                                         /* Muxb(sel, true, false) = sel */
5583                                         assert(get_Const_tarval(f) == tarval_b_false);
5584                                         DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5585                                         return sel;
5586                                 } else {
5587                                         /* Muxb(sel, true, x) = Or(sel, x) */
5588                                         n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5589                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5590                                         return n;
5591                                 }
5592                         }
5593                 } else if (is_Const(f)) {
5594                         tarval *tv_f = get_Const_tarval(f);
5595                         if (tv_f == tarval_b_true) {
5596                                 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5597                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5598                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5599                                 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5600                                 return n;
5601                         } else {
5602                                 /* Muxb(sel, x, false) = And(sel, x) */
5603                                 assert(tv_f == tarval_b_false);
5604                                 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5605                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5606                                 return n;
5607                         }
5608                 }
5609         }
5610
5611         /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5612         if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5613                 tarval *a = get_Const_tarval(t);
5614                 tarval *b = get_Const_tarval(f);
5615                 tarval *diff, *min;
5616
5617                 if (tarval_is_one(a) && tarval_is_null(b)) {
5618                         ir_node *block = get_nodes_block(n);
5619                         ir_node *conv  = new_r_Conv(current_ir_graph, block, sel, mode);
5620                         n = conv;
5621                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5622                         return n;
5623                 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5624                         ir_node *block = get_nodes_block(n);
5625                         ir_node *not_  = new_r_Not(current_ir_graph, block, sel, mode_b);
5626                         ir_node *conv  = new_r_Conv(current_ir_graph, block, not_, mode);
5627                         n = conv;
5628                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5629                         return n;
5630                 }
5631                 /* TODO: it's not really clear if that helps in general or should be moved
5632                  * to backend, especially with the MUX->Conv transformation above */
5633                 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5634                         diff = tarval_sub(a, b, NULL);
5635                         min  = b;
5636                 } else {
5637                         diff = tarval_sub(b, a, NULL);
5638                         min  = a;
5639                 }
5640
5641                 if (diff == get_tarval_one(mode)) {
5642                         dbg_info *dbg   = get_irn_dbg_info(n);
5643                         ir_node  *block = get_nodes_block(n);
5644                         ir_node  *t     = new_Const(tarval_sub(a, min, NULL));
5645                         ir_node  *f     = new_Const(tarval_sub(b, min, NULL));
5646                         n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5647                         n = new_rd_Add(dbg, irg, block, n, new_Const(min), mode);
5648                         return n;
5649                 }
5650         }
5651
5652         if (is_Proj(sel)) {
5653                 ir_node *cmp = get_Proj_pred(sel);
5654                 long     pn  = get_Proj_proj(sel);
5655
5656                 /*
5657                  * Note: normalization puts the constant on the right side,
5658                  * so we check only one case.
5659                  *
5660                  * Note further that these optimization work even for floating point
5661                  * with NaN's because -NaN == NaN.
5662                  * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5663                  * transformations.
5664                  */
5665                 if (is_Cmp(cmp)) {
5666                         ir_node *cmp_r = get_Cmp_right(cmp);
5667                         if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5668                                 ir_node *block = get_nodes_block(n);
5669                                 ir_node *cmp_l = get_Cmp_left(cmp);
5670
5671                                 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5672                                         /* f = -t */
5673
5674                                         if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5675                                                 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5676                                         {
5677                                                 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5678                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5679                                                                                  cmp_l, mode);
5680                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5681                                                 return n;
5682                                         } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5683                                                 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5684                                         {
5685                                                 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5686                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5687                                                                                  cmp_l, mode);
5688                                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5689                                                                                                                  block, n, mode);
5690                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5691                                                 return n;
5692                                         }
5693                                 }
5694
5695                                 if (mode_is_int(mode)) {
5696                                         /* integer only */
5697                                         if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5698                                                 /* Mux((a & b) != 0, c, 0) */
5699                                                 ir_node *and_r = get_And_right(cmp_l);
5700                                                 ir_node *and_l;
5701
5702                                                 if (and_r == t && f == cmp_r) {
5703                                                         if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5704                                                                 if (pn == pn_Cmp_Lg) {
5705                                                                         /* Mux((a & 2^C) != 0, 2^C, 0) */
5706                                                                         n = cmp_l;
5707                                                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5708                                                                 } else {
5709                                                                         /* Mux((a & 2^C) == 0, 2^C, 0) */
5710                                                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5711                                                                                 block, cmp_l, t, mode);
5712                                                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5713                                                                 }
5714                                                                 return n;
5715                                                         }
5716                                                 }
5717                                                 if (is_Shl(and_r)) {
5718                                                         ir_node *shl_l = get_Shl_left(and_r);
5719                                                         if (is_Const(shl_l) && is_Const_one(shl_l)) {
5720                                                                 if (and_r == t && f == cmp_r) {
5721                                                                         if (pn == pn_Cmp_Lg) {
5722                                                                                 /* (a & (1 << n)) != 0, (1 << n), 0) */
5723                                                                                 n = cmp_l;
5724                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5725                                                                         } else {
5726                                                                                 /* (a & (1 << n)) == 0, (1 << n), 0) */
5727                                                                                 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5728                                                                                         block, cmp_l, t, mode);
5729                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5730                                                                         }
5731                                                                         return n;
5732                                                                 }
5733                                                         }
5734                                                 }
5735                                                 and_l = get_And_left(cmp_l);
5736                                                 if (is_Shl(and_l)) {
5737                                                         ir_node *shl_l = get_Shl_left(and_l);
5738                                                         if (is_Const(shl_l) && is_Const_one(shl_l)) {
5739                                                                 if (and_l == t && f == cmp_r) {
5740                                                                         if (pn == pn_Cmp_Lg) {
5741                                                                                 /* ((1 << n) & a) != 0, (1 << n), 0) */
5742                                                                                 n = cmp_l;
5743                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5744                                                                         } else {
5745                                                                                 /* ((1 << n) & a) == 0, (1 << n), 0) */
5746                                                                                 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5747                                                                                         block, cmp_l, t, mode);
5748                                                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5749                                                                         }
5750                                                                         return n;
5751                                                                 }
5752                                                         }
5753                                                 }
5754                                         }
5755                                 }
5756                         }
5757                 }
5758         }
5759
5760         return n;
5761 }  /* transform_node_Mux */
5762
5763 /**
5764  * optimize Sync nodes that have other syncs as input we simply add the inputs
5765  * of the other sync to our own inputs
5766  */
5767 static ir_node *transform_node_Sync(ir_node *n) {
5768         int arity = get_Sync_n_preds(n);
5769         int i;
5770
5771         for (i = 0; i < arity;) {
5772                 ir_node *pred = get_Sync_pred(n, i);
5773                 int      pred_arity;
5774                 int      j;
5775
5776                 if (!is_Sync(pred)) {
5777                         ++i;
5778                         continue;
5779                 }
5780
5781                 del_Sync_n(n, i);
5782                 --arity;
5783
5784                 pred_arity = get_Sync_n_preds(pred);
5785                 for (j = 0; j < pred_arity; ++j) {
5786                         ir_node *pred_pred = get_Sync_pred(pred, j);
5787                         int      k;
5788
5789                         for (k = 0;; ++k) {
5790                                 if (k >= arity) {
5791                                         add_irn_n(n, pred_pred);
5792                                         ++arity;
5793                                         break;
5794                                 }
5795                                 if (get_Sync_pred(n, k) == pred_pred) break;
5796                         }
5797                 }
5798         }
5799
5800         /* rehash the sync node */
5801         add_identities(current_ir_graph->value_table, n);
5802
5803         return n;
5804 }  /* transform_node_Sync */
5805
5806 /**
5807  * optimize a trampoline Call into a direct Call
5808  */
5809 static ir_node *transform_node_Call(ir_node *call) {
5810         ir_node  *callee = get_Call_ptr(call);
5811         ir_node  *adr, *mem, *res, *bl, **in;
5812         ir_type  *ctp, *mtp, *tp;
5813         ident    *id;
5814         dbg_info *db;
5815         int      i, n_res, n_param;
5816         ir_variadicity var;
5817
5818         if (! is_Proj(callee))
5819                 return call;
5820         callee = get_Proj_pred(callee);
5821         if (! is_Builtin(callee))
5822                 return call;
5823         if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5824                 return call;
5825
5826         mem = get_Call_mem(call);
5827
5828         if (skip_Proj(mem) == callee) {
5829                 /* memory is routed to the trampoline, skip */
5830                 mem = get_Builtin_mem(callee);
5831         }
5832
5833         /* build a new call type */
5834         mtp = get_Call_type(call);
5835         id  = get_type_ident(mtp);
5836         id  = id_mangle(new_id_from_chars("T_", 2), id);
5837         db  = get_type_dbg_info(mtp);
5838
5839         n_res   = get_method_n_ress(mtp);
5840         n_param = get_method_n_params(mtp);
5841         ctp     = new_d_type_method(id, n_param + 1, n_res, db);
5842
5843         for (i = 0; i < n_res; ++i)
5844                 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5845
5846         NEW_ARR_A(ir_node *, in, n_param + 1);
5847
5848         /* FIXME: we don't need a new pointer type in every step */
5849         tp = get_irg_frame_type(current_ir_graph);
5850         id = id_mangle(get_type_ident(tp), new_id_from_chars("_ptr", 4));
5851         tp = new_type_pointer(id, tp, mode_P_data);
5852         set_method_param_type(ctp, 0, tp);
5853
5854         in[0] = get_Builtin_param(callee, 2);
5855         for (i = 0; i < n_param; ++i) {
5856                 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5857                 in[i + 1] = get_Call_param(call, i);
5858         }
5859         var = get_method_variadicity(mtp);
5860         set_method_variadicity(ctp, var);
5861         if (var == variadicity_variadic) {
5862                 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
5863         }
5864         /* When we resolve a trampoline, the function must be called by a this-call */
5865         set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5866         set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5867
5868         adr = get_Builtin_param(callee, 1);
5869
5870         db  = get_irn_dbg_info(call);
5871         bl  = get_nodes_block(call);
5872
5873         res = new_rd_Call(db, current_ir_graph, bl, mem, adr, n_param + 1, in, ctp);
5874         if (get_irn_pinned(call) == op_pin_state_floats)
5875                 set_irn_pinned(res, op_pin_state_floats);
5876         return res;
5877 }  /* transform_node_Call */
5878
5879 /**
5880  * Tries several [inplace] [optimizing] transformations and returns an
5881  * equivalent node.  The difference to equivalent_node() is that these
5882  * transformations _do_ generate new nodes, and thus the old node must
5883  * not be freed even if the equivalent node isn't the old one.
5884  */
5885 static ir_node *transform_node(ir_node *n) {
5886         ir_node *oldn;
5887
5888         /*
5889          * Transform_node is the only "optimizing transformation" that might
5890          * return a node with a different opcode. We iterate HERE until fixpoint
5891          * to get the final result.
5892          */
5893         do {
5894                 oldn = n;
5895                 if (n->op->ops.transform_node != NULL)
5896                         n = n->op->ops.transform_node(n);
5897         } while (oldn != n);
5898
5899         return n;
5900 }  /* transform_node */
5901
5902 /**
5903  * Sets the default transform node operation for an ir_op_ops.
5904  *
5905  * @param code   the opcode for the default operation
5906  * @param ops    the operations initialized
5907  *
5908  * @return
5909  *    The operations.
5910  */
5911 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5912 {
5913 #define CASE(a)                                         \
5914         case iro_##a:                                       \
5915                 ops->transform_node      = transform_node_##a;  \
5916                 break
5917 #define CASE_PROJ(a)                                         \
5918         case iro_##a:                                            \
5919                 ops->transform_node_Proj = transform_node_Proj_##a;  \
5920                 break
5921 #define CASE_PROJ_EX(a)                                      \
5922         case iro_##a:                                            \
5923                 ops->transform_node      = transform_node_##a;       \
5924                 ops->transform_node_Proj = transform_node_Proj_##a;  \
5925                 break
5926
5927         switch (code) {
5928         CASE(Add);
5929         CASE(Sub);
5930         CASE(Mul);
5931         CASE_PROJ_EX(Div);
5932         CASE_PROJ_EX(Mod);
5933         CASE_PROJ_EX(DivMod);
5934         CASE(Quot);
5935         CASE(Abs);
5936         CASE_PROJ_EX(Cmp);
5937         CASE_PROJ_EX(Cond);
5938         CASE(And);
5939         CASE(Eor);
5940         CASE(Not);
5941         CASE(Minus);
5942         CASE(Cast);
5943         CASE_PROJ(Load);
5944         CASE_PROJ(Store);
5945         CASE_PROJ(Bound);
5946         CASE_PROJ(CopyB);
5947         CASE(Proj);
5948         CASE(Phi);
5949         CASE(Or);
5950         CASE(Sel);
5951         CASE(Shr);
5952         CASE(Shrs);
5953         CASE(Shl);
5954         CASE(Rotl);
5955         CASE(Conv);
5956         CASE(End);
5957         CASE(Mux);
5958         CASE(Sync);
5959         CASE(Call);
5960         default:
5961           /* leave NULL */;
5962         }
5963
5964         return ops;
5965 #undef CASE_PROJ_EX
5966 #undef CASE_PROJ
5967 #undef CASE
5968 }  /* firm_set_default_transform_node */
5969
5970
5971 /* **************** Common Subexpression Elimination **************** */
5972
5973 /** The size of the hash table used, should estimate the number of nodes
5974     in a graph. */
5975 #define N_IR_NODES 512
5976
5977 /** Compares the attributes of two Const nodes. */
5978 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5979         return (get_Const_tarval(a) != get_Const_tarval(b))
5980             || (get_Const_type(a) != get_Const_type(b));
5981 }  /* node_cmp_attr_Const */
5982
5983 /** Compares the attributes of two Proj nodes. */
5984 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5985         return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5986 }  /* node_cmp_attr_Proj */
5987
5988 /** Compares the attributes of two Filter nodes. */
5989 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5990         return get_Filter_proj(a) != get_Filter_proj(b);
5991 }  /* node_cmp_attr_Filter */
5992
5993 /** Compares the attributes of two Alloc nodes. */
5994 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5995         const alloc_attr *pa = get_irn_alloc_attr(a);
5996         const alloc_attr *pb = get_irn_alloc_attr(b);
5997         return (pa->where != pb->where) || (pa->type != pb->type);
5998 }  /* node_cmp_attr_Alloc */
5999
6000 /** Compares the attributes of two Free nodes. */
6001 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
6002         const free_attr *pa = get_irn_free_attr(a);
6003         const free_attr *pb = get_irn_free_attr(b);
6004         return (pa->where != pb->where) || (pa->type != pb->type);
6005 }  /* node_cmp_attr_Free */
6006
6007 /** Compares the attributes of two SymConst nodes. */
6008 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
6009         const symconst_attr *pa = get_irn_symconst_attr(a);
6010         const symconst_attr *pb = get_irn_symconst_attr(b);
6011         return (pa->kind       != pb->kind)
6012             || (pa->sym.type_p != pb->sym.type_p)
6013             || (pa->tp         != pb->tp);
6014 }  /* node_cmp_attr_SymConst */
6015
6016 /** Compares the attributes of two Call nodes. */
6017 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
6018         return get_irn_call_attr(a) != get_irn_call_attr(b);
6019 }  /* node_cmp_attr_Call */
6020
6021 /** Compares the attributes of two Sel nodes. */
6022 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
6023         const ir_entity *a_ent = get_Sel_entity(a);
6024         const ir_entity *b_ent = get_Sel_entity(b);
6025 #if 0
6026         return
6027                 (a_ent->kind    != b_ent->kind)    ||
6028                 (a_ent->name    != b_ent->name)    ||
6029                 (a_ent->owner   != b_ent->owner)   ||
6030                 (a_ent->ld_name != b_ent->ld_name) ||
6031                 (a_ent->type    != b_ent->type);
6032 #endif
6033         /* Matze: inlining of functions can produce 2 entities with same type,
6034          * name, etc. */
6035         return a_ent != b_ent;
6036 }  /* node_cmp_attr_Sel */
6037
6038 /** Compares the attributes of two Phi nodes. */
6039 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
6040         /* we can only enter this function if both nodes have the same number of inputs,
6041            hence it is enough to check if one of them is a Phi0 */
6042         if (is_Phi0(a)) {
6043                 /* check the Phi0 pos attribute */
6044                 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
6045         }
6046         return 0;
6047 }  /* node_cmp_attr_Phi */
6048
6049 /** Compares the attributes of two Conv nodes. */
6050 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
6051         return get_Conv_strict(a) != get_Conv_strict(b);
6052 }  /* node_cmp_attr_Conv */
6053
6054 /** Compares the attributes of two Cast nodes. */
6055 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
6056         return get_Cast_type(a) != get_Cast_type(b);
6057 }  /* node_cmp_attr_Cast */
6058
6059 /** Compares the attributes of two Load nodes. */
6060 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
6061         if (get_Load_volatility(a) == volatility_is_volatile ||
6062             get_Load_volatility(b) == volatility_is_volatile)
6063                 /* NEVER do CSE on volatile Loads */
6064                 return 1;
6065         /* do not CSE Loads with different alignment. Be conservative. */
6066         if (get_Load_align(a) != get_Load_align(b))
6067                 return 1;
6068
6069         return get_Load_mode(a) != get_Load_mode(b);
6070 }  /* node_cmp_attr_Load */
6071
6072 /** Compares the attributes of two Store nodes. */
6073 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
6074         /* do not CSE Stores with different alignment. Be conservative. */
6075         if (get_Store_align(a) != get_Store_align(b))
6076                 return 1;
6077
6078         /* NEVER do CSE on volatile Stores */
6079         return (get_Store_volatility(a) == volatility_is_volatile ||
6080                 get_Store_volatility(b) == volatility_is_volatile);
6081 }  /* node_cmp_attr_Store */
6082
6083 /** Compares two exception attributes */
6084 static int node_cmp_exception(ir_node *a, ir_node *b) {
6085         const except_attr *ea = get_irn_except_attr(a);
6086         const except_attr *eb = get_irn_except_attr(b);
6087
6088         return ea->pin_state != eb->pin_state;
6089 }
6090
6091 #define node_cmp_attr_Bound  node_cmp_exception
6092
6093 /** Compares the attributes of two Div nodes. */
6094 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
6095         const divmod_attr *ma = get_irn_divmod_attr(a);
6096         const divmod_attr *mb = get_irn_divmod_attr(b);
6097         return ma->exc.pin_state != mb->exc.pin_state ||
6098                    ma->resmode       != mb->resmode ||
6099                    ma->no_remainder  != mb->no_remainder;
6100 }  /* node_cmp_attr_Div */
6101
6102 /** Compares the attributes of two DivMod nodes. */
6103 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
6104         const divmod_attr *ma = get_irn_divmod_attr(a);
6105         const divmod_attr *mb = get_irn_divmod_attr(b);
6106         return ma->exc.pin_state != mb->exc.pin_state ||
6107                    ma->resmode       != mb->resmode;
6108 }  /* node_cmp_attr_DivMod */
6109
6110 /** Compares the attributes of two Mod nodes. */
6111 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
6112         const divmod_attr *ma = get_irn_divmod_attr(a);
6113         const divmod_attr *mb = get_irn_divmod_attr(b);
6114         return ma->exc.pin_state != mb->exc.pin_state ||
6115                    ma->resmode       != mb->resmode;
6116 }  /* node_cmp_attr_Mod */
6117
6118 /** Compares the attributes of two Quot nodes. */
6119 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
6120         const divmod_attr *ma = get_irn_divmod_attr(a);
6121         const divmod_attr *mb = get_irn_divmod_attr(b);
6122         return ma->exc.pin_state != mb->exc.pin_state ||
6123                    ma->resmode       != mb->resmode;
6124 }  /* node_cmp_attr_Quot */
6125
6126 /** Compares the attributes of two Confirm nodes. */
6127 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
6128         /* no need to compare the bound, as this is a input */
6129         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6130 }  /* node_cmp_attr_Confirm */
6131
6132 /** Compares the attributes of two Builtin nodes. */
6133 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b) {
6134         const builtin_attr *ma = get_irn_builtin_attr(a);
6135         const builtin_attr *mb = get_irn_builtin_attr(b);
6136
6137         /* no need to compare the type, equal kind means equal type */
6138         return ma->kind != mb->kind;
6139 }  /* node_cmp_attr_Builtin */
6140
6141 /** Compares the attributes of two ASM nodes. */
6142 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
6143         int i, n;
6144         const ir_asm_constraint *ca;
6145         const ir_asm_constraint *cb;
6146         ident **cla, **clb;
6147
6148         if (get_ASM_text(a) != get_ASM_text(b))
6149                 return 1;
6150
6151         /* Should we really check the constraints here? Should be better, but is strange. */
6152         n = get_ASM_n_input_constraints(a);
6153         if (n != get_ASM_n_input_constraints(b))
6154                 return 0;
6155
6156         ca = get_ASM_input_constraints(a);
6157         cb = get_ASM_input_constraints(b);
6158         for (i = 0; i < n; ++i) {
6159                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6160                         return 1;
6161         }
6162
6163         n = get_ASM_n_output_constraints(a);
6164         if (n != get_ASM_n_output_constraints(b))
6165                 return 0;
6166
6167         ca = get_ASM_output_constraints(a);
6168         cb = get_ASM_output_constraints(b);
6169         for (i = 0; i < n; ++i) {
6170                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6171                         return 1;
6172         }
6173
6174         n = get_ASM_n_clobbers(a);
6175         if (n != get_ASM_n_clobbers(b))
6176                 return 0;
6177
6178         cla = get_ASM_clobbers(a);
6179         clb = get_ASM_clobbers(b);
6180         for (i = 0; i < n; ++i) {
6181                 if (cla[i] != clb[i])
6182                         return 1;
6183         }
6184         return 0;
6185 }  /* node_cmp_attr_ASM */
6186
6187 /** Compares the inexistent attributes of two Dummy nodes. */
6188 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6189 {
6190         (void) a;
6191         (void) b;
6192         return 1;
6193 }
6194
6195 /**
6196  * Set the default node attribute compare operation for an ir_op_ops.
6197  *
6198  * @param code   the opcode for the default operation
6199  * @param ops    the operations initialized
6200  *
6201  * @return
6202  *    The operations.
6203  */
6204 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6205 {
6206 #define CASE(a)                              \
6207         case iro_##a:                              \
6208                 ops->node_cmp_attr  = node_cmp_attr_##a; \
6209                 break
6210
6211         switch (code) {
6212         CASE(Const);
6213         CASE(Proj);
6214         CASE(Filter);
6215         CASE(Alloc);
6216         CASE(Free);
6217         CASE(SymConst);
6218         CASE(Call);
6219         CASE(Sel);
6220         CASE(Phi);
6221         CASE(Conv);
6222         CASE(Cast);
6223         CASE(Load);
6224         CASE(Store);
6225         CASE(Confirm);
6226         CASE(ASM);
6227         CASE(Div);
6228         CASE(DivMod);
6229         CASE(Mod);
6230         CASE(Quot);
6231         CASE(Bound);
6232         CASE(Builtin);
6233         CASE(Dummy);
6234         /* FIXME CopyB */
6235         default:
6236           /* leave NULL */;
6237         }
6238
6239         return ops;
6240 #undef CASE
6241 }  /* firm_set_default_node_cmp_attr */
6242
6243 /*
6244  * Compare function for two nodes in the value table. Gets two
6245  * nodes as parameters.  Returns 0 if the nodes are a Common Sub Expression.
6246  */
6247 int identities_cmp(const void *elt, const void *key) {
6248         ir_node *a = (ir_node *)elt;
6249         ir_node *b = (ir_node *)key;
6250         int i, irn_arity_a;
6251
6252         if (a == b) return 0;
6253
6254         if ((get_irn_op(a) != get_irn_op(b)) ||
6255             (get_irn_mode(a) != get_irn_mode(b))) return 1;
6256
6257         /* compare if a's in and b's in are of equal length */
6258         irn_arity_a = get_irn_intra_arity(a);
6259         if (irn_arity_a != get_irn_intra_arity(b))
6260                 return 1;
6261
6262         if (get_irn_pinned(a) == op_pin_state_pinned) {
6263                 /* for pinned nodes, the block inputs must be equal */
6264                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6265                         return 1;
6266         } else if (! get_opt_global_cse()) {
6267                 /* for block-local CSE both nodes must be in the same MacroBlock */
6268                 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6269                         return 1;
6270         }
6271
6272         /* compare a->in[0..ins] with b->in[0..ins] */
6273         for (i = 0; i < irn_arity_a; i++)
6274                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
6275                         return 1;
6276
6277         /*
6278          * here, we already now that the nodes are identical except their
6279          * attributes
6280          */
6281         if (a->op->ops.node_cmp_attr)
6282                 return a->op->ops.node_cmp_attr(a, b);
6283
6284         return 0;
6285 }  /* identities_cmp */
6286
6287 /*
6288  * Calculate a hash value of a node.
6289  *
6290  * @param node  The IR-node
6291  */
6292 unsigned ir_node_hash(const ir_node *node) {
6293         return node->op->ops.hash(node);
6294 }  /* ir_node_hash */
6295
6296
6297 pset *new_identities(void) {
6298         return new_pset(identities_cmp, N_IR_NODES);
6299 }  /* new_identities */
6300
6301 void del_identities(pset *value_table) {
6302         del_pset(value_table);
6303 }  /* del_identities */
6304
6305 /* Normalize a node by putting constants (and operands with larger
6306  * node index) on the right (operator side). */
6307 void ir_normalize_node(ir_node *n) {
6308         if (is_op_commutative(get_irn_op(n))) {
6309                 ir_node *l = get_binop_left(n);
6310                 ir_node *r = get_binop_right(n);
6311
6312                 /* For commutative operators perform  a OP b == b OP a but keep
6313                  * constants on the RIGHT side. This helps greatly in some
6314                  * optimizations.  Moreover we use the idx number to make the form
6315                  * deterministic. */
6316                 if (!operands_are_normalized(l, r)) {
6317                         set_binop_left(n, r);
6318                         set_binop_right(n, l);
6319                         hook_normalize(n);
6320                 }
6321         }
6322 }  /* ir_normalize_node */
6323
6324 /**
6325  * Update the nodes after a match in the value table. If both nodes have
6326  * the same MacroBlock but different Blocks, we must ensure that the node
6327  * with the dominating Block (the node that is near to the MacroBlock header
6328  * is stored in the table.
6329  * Because a MacroBlock has only one "non-exception" flow, we don't need
6330  * dominance info here: We known, that one block must dominate the other and
6331  * following the only block input will allow to find it.
6332  */
6333 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6334         ir_node *known_blk, *new_block, *block, *mbh;
6335
6336         if (get_opt_global_cse()) {
6337                 /* Block inputs are meaning less */
6338                 return;
6339         }
6340         known_blk = get_irn_n(known_irn, -1);
6341         new_block = get_irn_n(new_ir_node, -1);
6342         if (known_blk == new_block) {
6343                 /* already in the same block */
6344                 return;
6345         }
6346         /*
6347          * We expect the typical case when we built the graph. In that case, the
6348          * known_irn is already the upper one, so checking this should be faster.
6349          */
6350         block = new_block;
6351         mbh   = get_Block_MacroBlock(new_block);
6352         for (;;) {
6353                 if (block == known_blk) {
6354                         /* ok, we have found it: known_block dominates new_block as expected */
6355                         return;
6356                 }
6357                 if (block == mbh) {
6358                         /*
6359                          * We have reached the MacroBlock header NOT founding
6360                          * the known_block. new_block must dominate known_block.
6361                          * Update known_irn.
6362                          */
6363                         set_irn_n(known_irn, -1, new_block);
6364                         return;
6365                 }
6366                 assert(get_Block_n_cfgpreds(block) == 1);
6367                 block = get_Block_cfgpred_block(block, 0);
6368         }
6369 }  /* update_value_table */
6370
6371 /*
6372  * Return the canonical node computing the same value as n.
6373  * Looks up the node in a hash table, enters it in the table
6374  * if it isn't there yet.
6375  *
6376  * @param value_table  the HashSet containing all nodes in the
6377  *                     current IR graph
6378  * @param n            the node to look up
6379  *
6380  * @return a node that computes the same value as n or n if no such
6381  *         node could be found
6382  */
6383 ir_node *identify_remember(pset *value_table, ir_node *n) {
6384         ir_node *o = NULL;
6385
6386         if (!value_table) return n;
6387
6388         ir_normalize_node(n);
6389         /* lookup or insert in hash table with given hash key. */
6390         o = pset_insert(value_table, n, ir_node_hash(n));
6391
6392         if (o != n) {
6393                 update_known_irn(o, n);
6394         }
6395
6396         return o;
6397 }  /* identify_remember */
6398
6399 /**
6400  * During construction we set the op_pin_state_pinned flag in the graph right when the
6401  * optimization is performed.  The flag turning on procedure global cse could
6402  * be changed between two allocations.  This way we are safe.
6403  *
6404  * @param value_table  The value table
6405  * @param n            The node to lookup
6406  */
6407 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6408         ir_node *old = n;
6409
6410         n = identify_remember(value_table, n);
6411         if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6412                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6413         return n;
6414 }  /* identify_cons */
6415
6416 /* Add a node to the identities value table. */
6417 void add_identities(pset *value_table, ir_node *node) {
6418         if (get_opt_cse() && is_no_Block(node))
6419                 identify_remember(value_table, node);
6420 }  /* add_identities */
6421
6422 /* Visit each node in the value table of a graph. */
6423 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6424         ir_node *node;
6425         ir_graph *rem = current_ir_graph;
6426
6427         current_ir_graph = irg;
6428         foreach_pset(irg->value_table, node)
6429                 visit(node, env);
6430         current_ir_graph = rem;
6431 }  /* visit_all_identities */
6432
6433 /**
6434  * Garbage in, garbage out. If a node has a dead input, i.e., the
6435  * Bad node is input to the node, return the Bad node.
6436  */
6437 static ir_node *gigo(ir_node *node) {
6438         int i, irn_arity;
6439         ir_op *op = get_irn_op(node);
6440
6441         /* remove garbage blocks by looking at control flow that leaves the block
6442            and replacing the control flow by Bad. */
6443         if (get_irn_mode(node) == mode_X) {
6444                 ir_node *block = get_nodes_block(skip_Proj(node));
6445
6446                 /* Don't optimize nodes in immature blocks. */
6447                 if (!get_Block_matured(block))
6448                         return node;
6449                 /* Don't optimize End, may have Bads. */
6450                 if (op == op_End) return node;
6451
6452                 if (is_Block(block)) {
6453                         if (is_Block_dead(block)) {
6454                                 /* control flow from dead block is dead */
6455                                 return new_Bad();
6456                         }
6457
6458                         for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6459                                 if (!is_Bad(get_irn_n(block, i)))
6460                                         break;
6461                         }
6462                         if (i < 0) {
6463                                 ir_graph *irg = get_irn_irg(block);
6464                                 /* the start block is never dead */
6465                                 if (block != get_irg_start_block(irg)
6466                                         && block != get_irg_end_block(irg)) {
6467                                         /*
6468                                          * Do NOT kill control flow without setting
6469                                          * the block to dead of bad things can happen:
6470                                          * We get a Block that is not reachable be irg_block_walk()
6471                                          * but can be found by irg_walk()!
6472                                          */
6473                                         set_Block_dead(block);
6474                                         return new_Bad();
6475                                 }
6476                         }
6477                 }
6478         }
6479
6480         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6481            blocks predecessors is dead. */
6482         if (op != op_Block && op != op_Phi && op != op_Tuple) {
6483                 irn_arity = get_irn_arity(node);
6484
6485                 /*
6486                  * Beware: we can only read the block of a non-floating node.
6487                  */
6488                 if (is_irn_pinned_in_irg(node) &&
6489                         is_Block_dead(get_nodes_block(skip_Proj(node))))
6490                         return new_Bad();
6491
6492                 for (i = 0; i < irn_arity; i++) {
6493                         ir_node *pred = get_irn_n(node, i);
6494
6495                         if (is_Bad(pred))
6496                                 return new_Bad();
6497 #if 0
6498                         /* Propagating Unknowns here seems to be a bad idea, because
6499                            sometimes we need a node as a input and did not want that
6500                            it kills it's user.
6501                            However, it might be useful to move this into a later phase
6502                            (if you think that optimizing such code is useful). */
6503                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6504                                 return new_Unknown(get_irn_mode(node));
6505 #endif
6506                 }
6507         }
6508 #if 0
6509         /* With this code we violate the agreement that local_optimize
6510            only leaves Bads in Block, Phi and Tuple nodes. */
6511         /* If Block has only Bads as predecessors it's garbage. */
6512         /* If Phi has only Bads as predecessors it's garbage. */
6513         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
6514                 irn_arity = get_irn_arity(node);
6515                 for (i = 0; i < irn_arity; i++) {
6516                         if (!is_Bad(get_irn_n(node, i))) break;
6517                 }
6518                 if (i == irn_arity) node = new_Bad();
6519         }
6520 #endif
6521         return node;
6522 }  /* gigo */
6523
6524 /**
6525  * These optimizations deallocate nodes from the obstack.
6526  * It can only be called if it is guaranteed that no other nodes
6527  * reference this one, i.e., right after construction of a node.
6528  *
6529  * @param n   The node to optimize
6530  *
6531  * current_ir_graph must be set to the graph of the node!
6532  */
6533 ir_node *optimize_node(ir_node *n) {
6534         tarval *tv;
6535         ir_node *oldn = n;
6536         ir_opcode iro = get_irn_opcode(n);
6537
6538         /* Always optimize Phi nodes: part of the construction. */
6539         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6540
6541         /* constant expression evaluation / constant folding */
6542         if (get_opt_constant_folding()) {
6543                 /* neither constants nor Tuple values can be evaluated */
6544                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6545                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
6546                         int old_fp_mode = tarval_fp_ops_enabled();
6547
6548                         tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6549
6550                         /* try to evaluate */
6551                         tv = computed_value(n);
6552                         if (tv != tarval_bad) {
6553                                 ir_node *nw;
6554                                 ir_type *old_tp = get_irn_type(n);
6555                                 int i, arity = get_irn_arity(n);
6556                                 int node_size;
6557
6558                                 /*
6559                                  * Try to recover the type of the new expression.
6560                                  */
6561                                 for (i = 0; i < arity && !old_tp; ++i)
6562                                         old_tp = get_irn_type(get_irn_n(n, i));
6563
6564                                 /*
6565                                  * we MUST copy the node here temporary, because it's still needed
6566                                  * for DBG_OPT_CSTEVAL
6567                                  */
6568                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
6569                                 oldn = alloca(node_size);
6570
6571                                 memcpy(oldn, n, node_size);
6572                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6573
6574                                 /* ARG, copy the in array, we need it for statistics */
6575                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6576
6577                                 /* note the inplace edges module */
6578                                 edges_node_deleted(n, current_ir_graph);
6579
6580                                 /* evaluation was successful -- replace the node. */
6581                                 irg_kill_node(current_ir_graph, n);
6582                                 nw = new_Const(tv);
6583
6584                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6585                                         set_Const_type(nw, old_tp);
6586                                 DBG_OPT_CSTEVAL(oldn, nw);
6587                                 tarval_enable_fp_ops(old_fp_mode);
6588                                 return nw;
6589                         }
6590                         tarval_enable_fp_ops(old_fp_mode);
6591                 }
6592         }
6593
6594         /* remove unnecessary nodes */
6595         if (get_opt_algebraic_simplification() ||
6596             (iro == iro_Phi)  ||   /* always optimize these nodes. */
6597             (iro == iro_Id)   ||
6598             (iro == iro_Proj) ||
6599             (iro == iro_Block)  )  /* Flags tested local. */
6600                 n = equivalent_node(n);
6601
6602         /* Common Subexpression Elimination.
6603          *
6604          * Checks whether n is already available.
6605          * The block input is used to distinguish different subexpressions. Right
6606          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6607          * subexpressions within a block.
6608          */
6609         if (get_opt_cse())
6610                 n = identify_cons(current_ir_graph->value_table, n);
6611
6612         if (n != oldn) {
6613                 edges_node_deleted(oldn, current_ir_graph);
6614
6615                 /* We found an existing, better node, so we can deallocate the old node. */
6616                 irg_kill_node(current_ir_graph, oldn);
6617                 return n;
6618         }
6619
6620         /* Some more constant expression evaluation that does not allow to
6621            free the node. */
6622         iro = get_irn_opcode(n);
6623         if (get_opt_algebraic_simplification() ||
6624             (iro == iro_Cond) ||
6625             (iro == iro_Proj))     /* Flags tested local. */
6626                 n = transform_node(n);
6627
6628         /* Remove nodes with dead (Bad) input.
6629            Run always for transformation induced Bads. */
6630         n = gigo(n);
6631
6632         /* Now we have a legal, useful node. Enter it in hash table for CSE */
6633         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6634                 ir_node *o = n;
6635                 n = identify_remember(current_ir_graph->value_table, o);
6636                 if (o != n)
6637                         DBG_OPT_CSE(o, n);
6638         }
6639
6640         return n;
6641 }  /* optimize_node */
6642
6643
6644 /**
6645  * These optimizations never deallocate nodes (in place).  This can cause dead
6646  * nodes lying on the obstack.  Remove these by a dead node elimination,
6647  * i.e., a copying garbage collection.
6648  */
6649 ir_node *optimize_in_place_2(ir_node *n) {
6650         tarval *tv;
6651         ir_node *oldn = n;
6652         ir_opcode iro = get_irn_opcode(n);
6653
6654         if (!get_opt_optimize() && !is_Phi(n)) return n;
6655
6656         /* constant expression evaluation / constant folding */
6657         if (get_opt_constant_folding()) {
6658                 /* neither constants nor Tuple values can be evaluated */
6659                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6660                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
6661                         int old_fp_mode = tarval_fp_ops_enabled();
6662
6663                         tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6664                         /* try to evaluate */
6665                         tv = computed_value(n);
6666                         if (tv != tarval_bad) {
6667                                 /* evaluation was successful -- replace the node. */
6668                                 ir_type *old_tp = get_irn_type(n);
6669                                 int i, arity = get_irn_arity(n);
6670
6671                                 /*
6672                                  * Try to recover the type of the new expression.
6673                                  */
6674                                 for (i = 0; i < arity && !old_tp; ++i)
6675                                         old_tp = get_irn_type(get_irn_n(n, i));
6676
6677                                 n = new_Const(tv);
6678
6679                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6680                                         set_Const_type(n, old_tp);
6681
6682                                 DBG_OPT_CSTEVAL(oldn, n);
6683                                 tarval_enable_fp_ops(old_fp_mode);
6684                                 return n;
6685                         }
6686                         tarval_enable_fp_ops(old_fp_mode);
6687                 }
6688         }
6689
6690         /* remove unnecessary nodes */
6691         if (get_opt_constant_folding() ||
6692             (iro == iro_Phi)  ||   /* always optimize these nodes. */
6693             (iro == iro_Id)   ||   /* ... */
6694             (iro == iro_Proj) ||   /* ... */
6695             (iro == iro_Block)  )  /* Flags tested local. */
6696                 n = equivalent_node(n);
6697
6698         /** common subexpression elimination **/
6699         /* Checks whether n is already available. */
6700         /* The block input is used to distinguish different subexpressions.  Right
6701            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6702            subexpressions within a block. */
6703         if (get_opt_cse()) {
6704                 ir_node *o = n;
6705                 n = identify_remember(current_ir_graph->value_table, o);
6706                 if (o != n)
6707                         DBG_OPT_CSE(o, n);
6708         }
6709
6710         /* Some more constant expression evaluation. */
6711         iro = get_irn_opcode(n);
6712         if (get_opt_constant_folding() ||
6713                 (iro == iro_Cond) ||
6714                 (iro == iro_Proj))     /* Flags tested local. */
6715                 n = transform_node(n);
6716
6717         /* Remove nodes with dead (Bad) input.
6718            Run always for transformation induced Bads.  */
6719         n = gigo(n);
6720
6721         /* Now we can verify the node, as it has no dead inputs any more. */
6722         irn_vrfy(n);
6723
6724         /* Now we have a legal, useful node. Enter it in hash table for cse.
6725            Blocks should be unique anyways.  (Except the successor of start:
6726            is cse with the start block!) */
6727         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6728                 ir_node *o = n;
6729                 n = identify_remember(current_ir_graph->value_table, o);
6730                 if (o != n)
6731                         DBG_OPT_CSE(o, n);
6732         }
6733
6734         return n;
6735 }  /* optimize_in_place_2 */
6736
6737 /**
6738  * Wrapper for external use, set proper status bits after optimization.
6739  */
6740 ir_node *optimize_in_place(ir_node *n) {
6741         /* Handle graph state */
6742         assert(get_irg_phase_state(current_ir_graph) != phase_building);
6743
6744         if (get_opt_global_cse())
6745                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6746         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6747                 set_irg_outs_inconsistent(current_ir_graph);
6748
6749         /* FIXME: Maybe we could also test whether optimizing the node can
6750            change the control graph. */
6751         set_irg_doms_inconsistent(current_ir_graph);
6752         return optimize_in_place_2(n);
6753 }  /* optimize_in_place */
6754
6755 /**
6756  * Calculate a hash value of a Const node.
6757  */
6758 static unsigned hash_Const(const ir_node *node) {
6759         unsigned h;
6760
6761         /* special value for const, as they only differ in their tarval. */
6762         h = HASH_PTR(node->attr.con.tv);
6763
6764         return h;
6765 }  /* hash_Const */
6766
6767 /**
6768  * Calculate a hash value of a SymConst node.
6769  */
6770 static unsigned hash_SymConst(const ir_node *node) {
6771         unsigned h;
6772
6773         /* special value for const, as they only differ in their symbol. */
6774         if (node->attr.symc.kind == symconst_label)
6775                 h = (unsigned)node->attr.symc.sym.label;
6776         else {
6777                 /* all others are pointers */
6778                 h = HASH_PTR(node->attr.symc.sym.type_p);
6779         }
6780
6781         return h;
6782 }  /* hash_SymConst */
6783
6784 /**
6785  * Set the default hash operation in an ir_op_ops.
6786  *
6787  * @param code   the opcode for the default operation
6788  * @param ops    the operations initialized
6789  *
6790  * @return
6791  *    The operations.
6792  */
6793 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6794 {
6795 #define CASE(a)                                    \
6796         case iro_##a:                                  \
6797                 ops->hash  = hash_##a; \
6798                 break
6799
6800         /* hash function already set */
6801         if (ops->hash != NULL)
6802                 return ops;
6803
6804         switch (code) {
6805         CASE(Const);
6806         CASE(SymConst);
6807         default:
6808                 /* use input/mode default hash if no function was given */
6809                 ops->hash = firm_default_hash;
6810         }
6811
6812         return ops;
6813 #undef CASE
6814 }
6815
6816 /*
6817  * Sets the default operation for an ir_ops.
6818  */
6819 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6820         ops = firm_set_default_hash(code, ops);
6821         ops = firm_set_default_computed_value(code, ops);
6822         ops = firm_set_default_equivalent_node(code, ops);
6823         ops = firm_set_default_transform_node(code, ops);
6824         ops = firm_set_default_node_cmp_attr(code, ops);
6825         ops = firm_set_default_get_type(code, ops);
6826         ops = firm_set_default_get_type_attr(code, ops);
6827         ops = firm_set_default_get_entity_attr(code, ops);
6828
6829         return ops;
6830 }  /* firm_set_default_operations */