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