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