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