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