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