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