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