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