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