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