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