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