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