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