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