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