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