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