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