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