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