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