dbc28a1364787036730274322a70066346eb1b6d
[libfirm] / ir / ir / iropt.c
1 /*
2  * Copyright (C) 1995-2007 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31
32 #include "irnode_t.h"
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
35 #include "irmode_t.h"
36 #include "iropt_t.h"
37 #include "ircons_t.h"
38 #include "irgmod.h"
39 #include "irvrfy.h"
40 #include "tv_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
43 #include "irflag_t.h"
44 #include "irhooks.h"
45 #include "irarch.h"
46 #include "hashptr.h"
47 #include "archop.h"
48 #include "opt_confirms.h"
49 #include "opt_polymorphy.h"
50 #include "irtools.h"
51 #include "xmalloc.h"
52
53 /* Make types visible to allow most efficient access */
54 #include "entity_t.h"
55
56 /**
57  * Return the value of a Constant.
58  */
59 static tarval *computed_value_Const(ir_node *n) {
60         return get_Const_tarval(n);
61 }  /* computed_value_Const */
62
63 /**
64  * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
65  */
66 static tarval *computed_value_SymConst(ir_node *n) {
67         ir_type   *type;
68         ir_entity *ent;
69
70         switch (get_SymConst_kind(n)) {
71         case symconst_type_size:
72                 type = get_SymConst_type(n);
73                 if (get_type_state(type) == layout_fixed)
74                         return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
75                 break;
76         case symconst_type_align:
77                 type = get_SymConst_type(n);
78                 if (get_type_state(type) == layout_fixed)
79                         return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
80                 break;
81         case symconst_ofs_ent:
82                 ent  = get_SymConst_entity(n);
83                 type = get_entity_owner(ent);
84                 if (get_type_state(type) == layout_fixed)
85                         return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
86                 break;
87         default:
88                 break;
89         }
90         return tarval_bad;
91 }  /* computed_value_SymConst */
92
93 /**
94  * Return the value of an Add.
95  */
96 static tarval *computed_value_Add(ir_node *n) {
97         ir_node *a = get_Add_left(n);
98         ir_node *b = get_Add_right(n);
99
100         tarval *ta = value_of(a);
101         tarval *tb = value_of(b);
102
103         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104                 return tarval_add(ta, tb);
105
106         return tarval_bad;
107 }  /* computed_value_Add */
108
109 /**
110  * Return the value of a Sub.
111  * Special case: a - a
112  */
113 static tarval *computed_value_Sub(ir_node *n) {
114         ir_node *a = get_Sub_left(n);
115         ir_node *b = get_Sub_right(n);
116         tarval *ta;
117         tarval *tb;
118
119         /* a - a */
120         if (a == b && !is_Bad(a))
121                 return get_mode_null(get_irn_mode(n));
122
123         ta = value_of(a);
124         tb = value_of(b);
125
126         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
127                 return tarval_sub(ta, tb);
128
129         return tarval_bad;
130 }  /* computed_value_Sub */
131
132 /**
133  * Return the value of a Carry.
134  * Special : a op 0, 0 op b
135  */
136 static tarval *computed_value_Carry(ir_node *n) {
137         ir_node *a = get_binop_left(n);
138         ir_node *b = get_binop_right(n);
139         ir_mode *m = get_irn_mode(n);
140
141         tarval *ta = value_of(a);
142         tarval *tb = value_of(b);
143
144         if ((ta != tarval_bad) && (tb != tarval_bad)) {
145                 tarval_add(ta, tb);
146                 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
147         } else {
148                 if (tarval_is_null(ta) || tarval_is_null(tb))
149                         return get_mode_null(m);
150         }
151         return tarval_bad;
152 }  /* computed_value_Carry */
153
154 /**
155  * Return the value of a Borrow.
156  * Special : a op 0
157  */
158 static tarval *computed_value_Borrow(ir_node *n) {
159         ir_node *a = get_binop_left(n);
160         ir_node *b = get_binop_right(n);
161         ir_mode *m = get_irn_mode(n);
162
163         tarval *ta = value_of(a);
164         tarval *tb = value_of(b);
165
166         if ((ta != tarval_bad) && (tb != tarval_bad)) {
167                 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
168         } else if (tarval_is_null(ta)) {
169                 return get_mode_null(m);
170         }
171         return tarval_bad;
172 }  /* computed_value_Borrow */
173
174 /**
175  * Return the value of an unary Minus.
176  */
177 static tarval *computed_value_Minus(ir_node *n) {
178         ir_node *a = get_Minus_op(n);
179         tarval *ta = value_of(a);
180
181         if (ta != tarval_bad)
182                 return tarval_neg(ta);
183
184         return tarval_bad;
185 }  /* computed_value_Minus */
186
187 /**
188  * Return the value of a Mul.
189  */
190 static tarval *computed_value_Mul(ir_node *n) {
191         ir_node *a = get_Mul_left(n);
192         ir_node *b = get_Mul_right(n);
193         ir_mode *mode;
194
195         tarval *ta = value_of(a);
196         tarval *tb = value_of(b);
197
198         mode = get_irn_mode(n);
199         if (mode != get_irn_mode(a)) {
200                 /* n * n = 2n bit multiplication */
201                 ta = tarval_convert_to(ta, mode);
202                 tb = tarval_convert_to(tb, mode);
203         }
204
205         if (ta != tarval_bad && tb != tarval_bad) {
206                 return tarval_mul(ta, tb);
207         } else {
208                 /* a*0 = 0 or 0*b = 0 */
209                 if (ta == get_mode_null(mode))
210                         return ta;
211                 if (tb == get_mode_null(mode))
212                         return tb;
213         }
214         return tarval_bad;
215 }  /* computed_value_Mul */
216
217 /**
218  * Return the value of a floating point Quot.
219  */
220 static tarval *computed_value_Quot(ir_node *n) {
221         ir_node *a = get_Quot_left(n);
222         ir_node *b = get_Quot_right(n);
223
224         tarval *ta = value_of(a);
225         tarval *tb = value_of(b);
226
227         /* This was missing in original implementation. Why? */
228         if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
229                 if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
230                         return tarval_quo(ta, tb);
231         }
232         return tarval_bad;
233 }  /* computed_value_Quot */
234
235 /**
236  * Calculate the value of an integer Div of two nodes.
237  * Special case: 0 / b
238  */
239 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
240         tarval *ta = value_of(a);
241         tarval *tb = value_of(b);
242
243         /* Compute c1 / c2 or 0 / a, a != 0 */
244         if (ta != tarval_bad) {
245                 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b))))   /* div by zero: return tarval_bad */
246                         return tarval_div(ta, tb);
247                 else if (ta == get_mode_null(get_tarval_mode(ta)))  /* 0 / b == 0 */
248                         return ta;
249         }
250         return tarval_bad;
251 }  /* do_computed_value_Div */
252
253 /**
254  * Return the value of an integer Div.
255  */
256 static tarval *computed_value_Div(ir_node *n) {
257         return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
258 }  /* computed_value_Div */
259
260 /**
261  * Calculate the value of an integer Mod of two nodes.
262  * Special case: a % 1
263  */
264 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
265         tarval *ta = value_of(a);
266         tarval *tb = value_of(b);
267
268         /* Compute c1 % c2 or a % 1 */
269         if (tb != tarval_bad) {
270                 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb))))   /* div by zero: return tarval_bad */
271                         return tarval_mod(ta, tb);
272                 else if (tb == get_mode_one(get_tarval_mode(tb)))    /* x mod 1 == 0 */
273                         return get_mode_null(get_irn_mode(a));
274         }
275         return tarval_bad;
276 }  /* do_computed_value_Mod */
277
278 /**
279  * Return the value of an integer Mod.
280  */
281 static tarval *computed_value_Mod(ir_node *n) {
282         return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
283 }  /* computed_value_Mod */
284
285 /**
286  * Return the value of an Abs.
287  */
288 static tarval *computed_value_Abs(ir_node *n) {
289         ir_node *a = get_Abs_op(n);
290         tarval *ta = value_of(a);
291
292         if (ta != tarval_bad)
293                 return tarval_abs(ta);
294
295         return tarval_bad;
296 }  /* computed_value_Abs */
297
298 /**
299  * Return the value of an And.
300  * Special case: a & 0, 0 & b
301  */
302 static tarval *computed_value_And(ir_node *n) {
303         ir_node *a = get_And_left(n);
304         ir_node *b = get_And_right(n);
305
306         tarval *ta = value_of(a);
307         tarval *tb = value_of(b);
308
309         if ((ta != tarval_bad) && (tb != tarval_bad)) {
310                 return tarval_and (ta, tb);
311         } else {
312                 if (tarval_is_null(ta)) return ta;
313                 if (tarval_is_null(tb)) return tb;
314         }
315         return tarval_bad;
316 }  /* computed_value_And */
317
318 /**
319  * Return the value of an Or.
320  * Special case: a | 1...1, 1...1 | b
321  */
322 static tarval *computed_value_Or(ir_node *n) {
323         ir_node *a = get_Or_left(n);
324         ir_node *b = get_Or_right(n);
325
326         tarval *ta = value_of(a);
327         tarval *tb = value_of(b);
328
329         if ((ta != tarval_bad) && (tb != tarval_bad)) {
330                 return tarval_or (ta, tb);
331         } else {
332                 if (tarval_is_all_one(ta)) return ta;
333                 if (tarval_is_all_one(tb)) return tb;
334         }
335         return tarval_bad;
336 }  /* computed_value_Or */
337
338 /**
339  * Return the value of an Eor.
340  */
341 static tarval *computed_value_Eor(ir_node *n) {
342         ir_node *a = get_Eor_left(n);
343         ir_node *b = get_Eor_right(n);
344
345         tarval *ta, *tb;
346
347         if (a == b)
348                 return get_mode_null(get_irn_mode(n));
349
350         ta = value_of(a);
351         tb = value_of(b);
352
353         if ((ta != tarval_bad) && (tb != tarval_bad)) {
354                 return tarval_eor (ta, tb);
355         }
356         return tarval_bad;
357 }  /* computed_value_Eor */
358
359 /**
360  * Return the value of a Not.
361  */
362 static tarval *computed_value_Not(ir_node *n) {
363         ir_node *a = get_Not_op(n);
364         tarval *ta = value_of(a);
365
366         if (ta != tarval_bad)
367                 return tarval_not(ta);
368
369         return tarval_bad;
370 }  /* computed_value_Not */
371
372 /**
373  * Return the value of a Shl.
374  */
375 static tarval *computed_value_Shl(ir_node *n) {
376         ir_node *a = get_Shl_left(n);
377         ir_node *b = get_Shl_right(n);
378
379         tarval *ta = value_of(a);
380         tarval *tb = value_of(b);
381
382         if ((ta != tarval_bad) && (tb != tarval_bad)) {
383                 return tarval_shl (ta, tb);
384         }
385         return tarval_bad;
386 }  /* computed_value_Shl */
387
388 /**
389  * Return the value of a Shr.
390  */
391 static tarval *computed_value_Shr(ir_node *n) {
392         ir_node *a = get_Shr_left(n);
393         ir_node *b = get_Shr_right(n);
394
395         tarval *ta = value_of(a);
396         tarval *tb = value_of(b);
397
398         if ((ta != tarval_bad) && (tb != tarval_bad)) {
399                 return tarval_shr (ta, tb);
400         }
401         return tarval_bad;
402 }  /* computed_value_Shr */
403
404 /**
405  * Return the value of a Shrs.
406  */
407 static tarval *computed_value_Shrs(ir_node *n) {
408         ir_node *a = get_Shrs_left(n);
409         ir_node *b = get_Shrs_right(n);
410
411         tarval *ta = value_of(a);
412         tarval *tb = value_of(b);
413
414         if ((ta != tarval_bad) && (tb != tarval_bad)) {
415                 return tarval_shrs (ta, tb);
416         }
417         return tarval_bad;
418 }  /* computed_value_Shrs */
419
420 /**
421  * Return the value of a Rot.
422  */
423 static tarval *computed_value_Rot(ir_node *n) {
424         ir_node *a = get_Rot_left(n);
425         ir_node *b = get_Rot_right(n);
426
427         tarval *ta = value_of(a);
428         tarval *tb = value_of(b);
429
430         if ((ta != tarval_bad) && (tb != tarval_bad)) {
431                 return tarval_rot (ta, tb);
432         }
433         return tarval_bad;
434 }  /* computed_value_Rot */
435
436 /**
437  * Return the value of a Conv.
438  */
439 static tarval *computed_value_Conv(ir_node *n) {
440         ir_node *a = get_Conv_op(n);
441         tarval *ta = value_of(a);
442
443         if (ta != tarval_bad)
444                 return tarval_convert_to(ta, get_irn_mode(n));
445
446         return tarval_bad;
447 }  /* computed_value_Conv */
448
449 /**
450  * Return the value of a Proj(Cmp).
451  *
452  * This performs a first step of unreachable code elimination.
453  * Proj can not be computed, but folding a Cmp above the Proj here is
454  * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
455  * only 1 is used.
456  * There are several case where we can evaluate a Cmp node, see later.
457  */
458 static tarval *computed_value_Proj_Cmp(ir_node *n) {
459         ir_node *a   = get_Proj_pred(n);
460         ir_node *aa  = get_Cmp_left(a);
461         ir_node *ab  = get_Cmp_right(a);
462         long proj_nr = get_Proj_proj(n);
463
464         /*
465          * BEWARE: a == a is NOT always True for floating Point values, as
466          * NaN != NaN is defined, so we must check this here.
467          */
468         if (aa == ab && (
469                 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt ||  proj_nr == pn_Cmp_Gt)
470                 ) { /* 1.: */
471
472                 /* This is a trick with the bits used for encoding the Cmp
473                    Proj numbers, the following statement is not the same:
474                 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
475                 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
476         }
477         else {
478                 tarval *taa = value_of(aa);
479                 tarval *tab = value_of(ab);
480                 ir_mode *mode = get_irn_mode(aa);
481
482                 /*
483                  * The predecessors of Cmp are target values.  We can evaluate
484                  * the Cmp.
485                  */
486                 if ((taa != tarval_bad) && (tab != tarval_bad)) {
487                         /* strange checks... */
488                         pn_Cmp flags = tarval_cmp(taa, tab);
489                         if (flags != pn_Cmp_False) {
490                                 return new_tarval_from_long (proj_nr & flags, mode_b);
491                         }
492                 }
493                 /* for integer values, we can check against MIN/MAX */
494                 else if (mode_is_int(mode)) {
495                         /* MIN <=/> x.  This results in true/false. */
496                         if (taa == get_mode_min(mode)) {
497                                 /* a compare with the MIN value */
498                                 if (proj_nr == pn_Cmp_Le)
499                                         return get_tarval_b_true();
500                                 else if (proj_nr == pn_Cmp_Gt)
501                                         return get_tarval_b_false();
502                         }
503                         /* x >=/< MIN.  This results in true/false. */
504                         else
505                                 if (tab == get_mode_min(mode)) {
506                                         /* a compare with the MIN value */
507                                         if (proj_nr == pn_Cmp_Ge)
508                                                 return get_tarval_b_true();
509                                         else if (proj_nr == pn_Cmp_Lt)
510                                                 return get_tarval_b_false();
511                                 }
512                                 /* MAX >=/< x.  This results in true/false. */
513                                 else if (taa == get_mode_max(mode)) {
514                                         if (proj_nr == pn_Cmp_Ge)
515                                                 return get_tarval_b_true();
516                                         else if (proj_nr == pn_Cmp_Lt)
517                                                 return get_tarval_b_false();
518                                 }
519                                 /* x <=/> MAX.  This results in true/false. */
520                                 else if (tab == get_mode_max(mode)) {
521                                         if (proj_nr == pn_Cmp_Le)
522                                                 return get_tarval_b_true();
523                                         else if (proj_nr == pn_Cmp_Gt)
524                                                 return get_tarval_b_false();
525                                 }
526                 }
527                 /*
528                  * The predecessors are Allocs or (void*)(0) constants.  Allocs never
529                  * return NULL, they raise an exception.   Therefore we can predict
530                  * the Cmp result.
531                  */
532                 else {
533                         ir_node *aaa = skip_Id(skip_Proj(aa));
534                         ir_node *aba = skip_Id(skip_Proj(ab));
535
536                         if (   (   (/* aa is ProjP and aaa is Alloc */
537                                        is_Proj(aa)
538                                     && mode_is_reference(get_irn_mode(aa))
539                                     && is_Alloc(aaa))
540                                 && (   (/* ab is NULL */
541                                            is_Const(ab)
542                                         && mode_is_reference(get_irn_mode(ab))
543                                         && is_Const_null(ab))
544                                     || (/* ab is other Alloc */
545                                            is_Proj(ab)
546                                         && mode_is_reference(get_irn_mode(ab))
547                                         && is_Alloc(aba)
548                                         && (aaa != aba))))
549                             || (/* aa is NULL and aba is Alloc */
550                                    is_Const(aa)
551                                 && mode_is_reference(get_irn_mode(aa))
552                                 && is_Const_null(aa)
553                                 && is_Proj(ab)
554                                 && mode_is_reference(get_irn_mode(ab))
555                                 && is_Alloc(aba)))
556                                 /* 3.: */
557                         return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
558                 }
559         }
560         return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
561 }  /* computed_value_Proj_Cmp */
562
563 /**
564  * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
565  * Proj(DivMod) and Proj(Quot).
566  */
567 static tarval *computed_value_Proj(ir_node *n) {
568         ir_node *a = get_Proj_pred(n);
569         long proj_nr;
570
571         switch (get_irn_opcode(a)) {
572         case iro_Cmp:
573                 return computed_value_Proj_Cmp(n);
574
575         case iro_DivMod:
576                 /* compute either the Div or the Mod part */
577                 proj_nr = get_Proj_proj(n);
578                 if (proj_nr == pn_DivMod_res_div)
579                         return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
580                 else if (proj_nr == pn_DivMod_res_mod)
581                         return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
582                 break;
583
584         case iro_Div:
585                 if (get_Proj_proj(n) == pn_Div_res)
586                         return computed_value(a);
587                 break;
588
589         case iro_Mod:
590                 if (get_Proj_proj(n) == pn_Mod_res)
591                         return computed_value(a);
592                 break;
593
594         case iro_Quot:
595                 if (get_Proj_proj(n) == pn_Quot_res)
596                         return computed_value(a);
597                 break;
598
599         default:
600                 return tarval_bad;
601         }
602         return tarval_bad;
603 }  /* computed_value_Proj */
604
605 /**
606  * Calculate the value of a Mux: can be evaluated, if the
607  * sel and the right input are known.
608  */
609 static tarval *computed_value_Mux(ir_node *n) {
610         ir_node *sel = get_Mux_sel(n);
611         tarval *ts = value_of(sel);
612
613         if (ts == get_tarval_b_true()) {
614                 ir_node *v = get_Mux_true(n);
615                 return value_of(v);
616         }
617         else if (ts == get_tarval_b_false()) {
618                 ir_node *v = get_Mux_false(n);
619                 return value_of(v);
620         }
621         return tarval_bad;
622 }  /* computed_value_Mux */
623
624 /**
625  * Calculate the value of a Psi: can be evaluated, if a condition is true
626  * and all previous conditions are false. If all conditions are false
627  * we evaluate to the default one.
628  */
629 static tarval *computed_value_Psi(ir_node *n) {
630         if (is_Mux(n))
631                 return computed_value_Mux(n);
632         return tarval_bad;
633 }  /* computed_value_Psi */
634
635 /**
636  * Calculate the value of a Confirm: can be evaluated,
637  * if it has the form Confirm(x, '=', Const).
638  */
639 static tarval *computed_value_Confirm(ir_node *n) {
640         return get_Confirm_cmp(n) == pn_Cmp_Eq ?
641                 value_of(get_Confirm_bound(n)) : tarval_bad;
642 }  /* computed_value_Confirm */
643
644 /**
645  * If the parameter n can be computed, return its value, else tarval_bad.
646  * Performs constant folding.
647  *
648  * @param n  The node this should be evaluated
649  */
650 tarval *computed_value(ir_node *n) {
651         if (n->op->ops.computed_value)
652                 return n->op->ops.computed_value(n);
653         return tarval_bad;
654 }  /* computed_value */
655
656 /**
657  * Set the default computed_value evaluator in an ir_op_ops.
658  *
659  * @param code   the opcode for the default operation
660  * @param ops    the operations initialized
661  *
662  * @return
663  *    The operations.
664  */
665 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
666 {
667 #define CASE(a)                                    \
668         case iro_##a:                                  \
669                 ops->computed_value  = computed_value_##a; \
670                 break
671
672         switch (code) {
673         CASE(Const);
674         CASE(SymConst);
675         CASE(Add);
676         CASE(Sub);
677         CASE(Minus);
678         CASE(Mul);
679         CASE(Quot);
680         CASE(Div);
681         CASE(Mod);
682         CASE(Abs);
683         CASE(And);
684         CASE(Or);
685         CASE(Eor);
686         CASE(Not);
687         CASE(Shl);
688         CASE(Shr);
689         CASE(Shrs);
690         CASE(Rot);
691         CASE(Carry);
692         CASE(Borrow);
693         CASE(Conv);
694         CASE(Proj);
695         CASE(Mux);
696         CASE(Psi);
697         CASE(Confirm);
698         default:
699                 /* leave NULL */;
700         }
701
702         return ops;
703 #undef CASE
704 }  /* firm_set_default_computed_value */
705
706 /**
707  * Returns a equivalent block for another block.
708  * If the block has only one predecessor, this is
709  * the equivalent one. If the only predecessor of a block is
710  * the block itself, this is a dead block.
711  *
712  * If both predecessors of a block are the branches of a binary
713  * Cond, the equivalent block is Cond's block.
714  *
715  * If all predecessors of a block are bad or lies in a dead
716  * block, the current block is dead as well.
717  *
718  * Note, that blocks are NEVER turned into Bad's, instead
719  * the dead_block flag is set. So, never test for is_Bad(block),
720  * always use is_dead_Block(block).
721  */
722 static ir_node *equivalent_node_Block(ir_node *n)
723 {
724         ir_node *oldn = n;
725         int n_preds   = get_Block_n_cfgpreds(n);
726
727         /* The Block constructor does not call optimize, but mature_immBlock
728         calls the optimization. */
729         assert(get_Block_matured(n));
730
731         /* Straightening: a single entry Block following a single exit Block
732            can be merged, if it is not the Start block. */
733         /* !!! Beware, all Phi-nodes of n must have been optimized away.
734            This should be true, as the block is matured before optimize is called.
735            But what about Phi-cycles with the Phi0/Id that could not be resolved?
736            Remaining Phi nodes are just Ids. */
737         if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
738                 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
739                 if (predblock == oldn) {
740                         /* Jmp jumps into the block it is in -- deal self cycle. */
741                         n = set_Block_dead(n);
742                         DBG_OPT_DEAD_BLOCK(oldn, n);
743                 } else if (get_opt_control_flow_straightening()) {
744                         n = predblock;
745                         DBG_OPT_STG(oldn, n);
746                 }
747         } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
748                 ir_node *predblock = get_Block_cfgpred_block(n, 0);
749                 if (predblock == oldn) {
750                         /* Jmp jumps into the block it is in -- deal self cycle. */
751                         n = set_Block_dead(n);
752                         DBG_OPT_DEAD_BLOCK(oldn, n);
753                 }
754         } else if ((n_preds == 2) &&
755                    (get_opt_control_flow_weak_simplification())) {
756                 /* Test whether Cond jumps twice to this block
757                  * The more general case which more than 2 predecessors is handles
758                  * in optimize_cf(), we handle only this special case for speed here.
759                  */
760                 ir_node *a = get_Block_cfgpred(n, 0);
761                 ir_node *b = get_Block_cfgpred(n, 1);
762
763                 if (is_Proj(a) &&
764                     is_Proj(b) &&
765                     (get_Proj_pred(a) == get_Proj_pred(b)) &&
766                     is_Cond(get_Proj_pred(a)) &&
767                     (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
768                         /* Also a single entry Block following a single exit Block.  Phis have
769                            twice the same operand and will be optimized away. */
770                         n = get_nodes_block(get_Proj_pred(a));
771                         DBG_OPT_IFSIM1(oldn, a, b, n);
772                 }
773         } else if (get_opt_unreachable_code() &&
774                    (n != get_irg_start_block(current_ir_graph)) &&
775                    (n != get_irg_end_block(current_ir_graph))    ) {
776                 int i;
777
778                 /* If all inputs are dead, this block is dead too, except if it is
779                    the start or end block.  This is one step of unreachable code
780                    elimination */
781                 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
782                         ir_node *pred = get_Block_cfgpred(n, i);
783                         ir_node *pred_blk;
784
785                         if (is_Bad(pred)) continue;
786                         pred_blk = get_nodes_block(skip_Proj(pred));
787
788                         if (is_Block_dead(pred_blk)) continue;
789
790                         if (pred_blk != n) {
791                                 /* really found a living input */
792                                 break;
793                         }
794                 }
795                 if (i < 0) {
796                         n = set_Block_dead(n);
797                         DBG_OPT_DEAD_BLOCK(oldn, n);
798                 }
799         }
800
801         return n;
802 }  /* equivalent_node_Block */
803
804 /**
805  * Returns a equivalent node for a Jmp, a Bad :-)
806  * Of course this only happens if the Block of the Jmp is dead.
807  */
808 static ir_node *equivalent_node_Jmp(ir_node *n) {
809         /* unreachable code elimination */
810         if (is_Block_dead(get_nodes_block(n)))
811                 n = new_Bad();
812
813         return n;
814 }  /* equivalent_node_Jmp */
815
816 /** Raise is handled in the same way as Jmp. */
817 #define equivalent_node_Raise   equivalent_node_Jmp
818
819
820 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
821    See transform_node_Proj_Cond(). */
822
823 /**
824  * Optimize operations that are commutative and have neutral 0,
825  * so a op 0 = 0 op a = a.
826  */
827 static ir_node *equivalent_node_neutral_zero(ir_node *n)
828 {
829         ir_node *oldn = n;
830
831         ir_node *a = get_binop_left(n);
832         ir_node *b = get_binop_right(n);
833
834         tarval *tv;
835         ir_node *on;
836
837         /* After running compute_node there is only one constant predecessor.
838            Find this predecessors value and remember the other node: */
839         if ((tv = value_of(a)) != tarval_bad) {
840                 on = b;
841         } else if ((tv = value_of(b)) != tarval_bad) {
842                 on = a;
843         } else
844                 return n;
845
846         /* If this predecessors constant value is zero, the operation is
847          * unnecessary. Remove it.
848          *
849          * Beware: If n is a Add, the mode of on and n might be different
850          * which happens in this rare construction: NULL + 3.
851          * Then, a Conv would be needed which we cannot include here.
852          */
853         if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
854                 n = on;
855
856                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
857         }
858
859         return n;
860 }  /* equivalent_node_neutral_zero */
861
862 /**
863  * Eor is commutative and has neutral 0.
864  */
865 #define equivalent_node_Eor  equivalent_node_neutral_zero
866
867 /*
868  * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
869  *
870  * The second one looks strange, but this construct
871  * is used heavily in the LCC sources :-).
872  *
873  * Beware: The Mode of an Add may be different than the mode of its
874  * predecessors, so we could not return a predecessors in all cases.
875  */
876 static ir_node *equivalent_node_Add(ir_node *n) {
877         ir_node *oldn = n;
878         ir_node *left, *right;
879         ir_mode *mode = get_irn_mode(n);
880
881         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
882         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
883                 return n;
884
885         n = equivalent_node_neutral_zero(n);
886         if (n != oldn)
887                 return n;
888
889         left  = get_Add_left(n);
890         right = get_Add_right(n);
891
892         if (is_Sub(left)) {
893                 if (get_Sub_right(left) == right) {
894                         /* (a - x) + x */
895
896                         n = get_Sub_left(left);
897                         if (mode == get_irn_mode(n)) {
898                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
899                                 return n;
900                         }
901                 }
902         }
903         if (is_Sub(right)) {
904                 if (get_Sub_right(right) == left) {
905                         /* x + (a - x) */
906
907                         n = get_Sub_left(right);
908                         if (mode == get_irn_mode(n)) {
909                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
910                                 return n;
911                         }
912                 }
913         }
914         return n;
915 }  /* equivalent_node_Add */
916
917 /**
918  * optimize operations that are not commutative but have neutral 0 on left,
919  * so a op 0 = a.
920  */
921 static ir_node *equivalent_node_left_zero(ir_node *n) {
922         ir_node *oldn = n;
923
924         ir_node *a = get_binop_left(n);
925         ir_node *b = get_binop_right(n);
926
927         if (is_Const(b) && is_Const_null(b)) {
928                 n = a;
929
930                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
931         }
932         return n;
933 }  /* equivalent_node_left_zero */
934
935 #define equivalent_node_Shl   equivalent_node_left_zero
936 #define equivalent_node_Shr   equivalent_node_left_zero
937 #define equivalent_node_Shrs  equivalent_node_left_zero
938 #define equivalent_node_Rot   equivalent_node_left_zero
939
940 /**
941  * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
942  *
943  * The second one looks strange, but this construct
944  * is used heavily in the LCC sources :-).
945  *
946  * Beware: The Mode of a Sub may be different than the mode of its
947  * predecessors, so we could not return a predecessors in all cases.
948  */
949 static ir_node *equivalent_node_Sub(ir_node *n) {
950         ir_node *oldn = n;
951         ir_node *b;
952         ir_mode *mode = get_irn_mode(n);
953
954         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
955         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
956                 return n;
957
958         b = get_Sub_right(n);
959
960         /* Beware: modes might be different */
961         if (is_Const(b) && is_Const_null(b)) {
962                 ir_node *a = get_Sub_left(n);
963                 if (mode == get_irn_mode(a)) {
964                         n = a;
965
966                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
967                 }
968         }
969         return n;
970 }  /* equivalent_node_Sub */
971
972
973 /**
974  * Optimize an "idempotent unary op", ie op(op(n)) = n.
975  *
976  * @todo
977  *   -(-a) == a, but might overflow two times.
978  *   We handle it anyway here but the better way would be a
979  *   flag. This would be needed for Pascal for instance.
980  */
981 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
982         ir_node *oldn = n;
983         ir_node *pred = get_unop_op(n);
984
985         /* optimize symmetric unop */
986         if (get_irn_op(pred) == get_irn_op(n)) {
987                 n = get_unop_op(pred);
988                 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
989         }
990         return n;
991 }  /* equivalent_node_idempotent_unop */
992
993 /** Optimize Not(Not(x)) == x. */
994 #define equivalent_node_Not    equivalent_node_idempotent_unop
995
996 /** -(-x) == x       ??? Is this possible or can --x raise an
997                        out of bounds exception if min =! max? */
998 #define equivalent_node_Minus  equivalent_node_idempotent_unop
999
1000 /**
1001  * Optimize a * 1 = 1 * a = a.
1002  */
1003 static ir_node *equivalent_node_Mul(ir_node *n) {
1004         ir_node *oldn = n;
1005         ir_node *a = get_Mul_left(n);
1006
1007         /* we can handle here only the n * n = n bit cases */
1008         if (get_irn_mode(n) == get_irn_mode(a)) {
1009                 ir_node *b = get_Mul_right(n);
1010
1011                 /* Mul is commutative and has again an other neutral element. */
1012                 if (is_Const(a) && is_Const_one(a)) {
1013                         n = b;
1014                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1015                 } else if (is_Const(b) && is_Const_one(b)) {
1016                         n = a;
1017                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1018                 }
1019         }
1020         return n;
1021 }  /* equivalent_node_Mul */
1022
1023 /**
1024  * Optimize a / 1 = a.
1025  */
1026 static ir_node *equivalent_node_Div(ir_node *n) {
1027         ir_node *a = get_Div_left(n);
1028         ir_node *b = get_Div_right(n);
1029
1030         /* Div is not commutative. */
1031         if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1032                 /* Turn Div into a tuple (mem, bad, a) */
1033                 ir_node *mem = get_Div_mem(n);
1034                 ir_node *blk = get_irn_n(n, -1);
1035                 turn_into_tuple(n, pn_Div_max);
1036                 set_Tuple_pred(n, pn_Div_M,         mem);
1037                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1038                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());        /* no exception */
1039                 set_Tuple_pred(n, pn_Div_res,       a);
1040         }
1041         return n;
1042 }  /* equivalent_node_Div */
1043
1044 /**
1045  * Optimize a / 1.0 = a.
1046  */
1047 static ir_node *equivalent_node_Quot(ir_node *n) {
1048         ir_node *a = get_Quot_left(n);
1049         ir_node *b = get_Quot_right(n);
1050
1051         /* Div is not commutative. */
1052         if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1053                 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1054                 ir_node *mem = get_Quot_mem(n);
1055                 ir_node *blk = get_irn_n(n, -1);
1056                 turn_into_tuple(n, pn_Quot_max);
1057                 set_Tuple_pred(n, pn_Quot_M,         mem);
1058                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1059                 set_Tuple_pred(n, pn_Quot_X_except,  new_Bad());        /* no exception */
1060                 set_Tuple_pred(n, pn_Quot_res,       a);
1061         }
1062         return n;
1063 }  /* equivalent_node_Quot */
1064
1065 /**
1066  * Optimize a / 1 = a.
1067  */
1068 static ir_node *equivalent_node_DivMod(ir_node *n) {
1069         ir_node *b = get_DivMod_right(n);
1070
1071         /* Div is not commutative. */
1072         if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1073                 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1074                 ir_node *a = get_DivMod_left(n);
1075                 ir_node *mem = get_Div_mem(n);
1076                 ir_node *blk = get_irn_n(n, -1);
1077                 ir_mode *mode = get_DivMod_resmode(n);
1078
1079                 turn_into_tuple(n, pn_DivMod_max);
1080                 set_Tuple_pred(n, pn_DivMod_M,         mem);
1081                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1082                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());        /* no exception */
1083                 set_Tuple_pred(n, pn_DivMod_res_div,   a);
1084                 set_Tuple_pred(n, pn_DivMod_res_mod,   new_Const(mode, get_mode_null(mode)));
1085         }
1086         return n;
1087 }  /* equivalent_node_DivMod */
1088
1089 /**
1090  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1091  */
1092 static ir_node *equivalent_node_Or(ir_node *n) {
1093         ir_node *oldn = n;
1094
1095         ir_node *a = get_Or_left(n);
1096         ir_node *b = get_Or_right(n);
1097
1098         if (a == b) {
1099                 n = a;    /* Or has it's own neutral element */
1100                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1101         } else if (is_Const(a) && is_Const_null(a)) {
1102                 n = b;
1103                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1104         } else if (is_Const(b) && is_Const_null(b)) {
1105                 n = a;
1106                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1107         }
1108
1109         return n;
1110 }  /* equivalent_node_Or */
1111
1112 /**
1113  * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1114  */
1115 static ir_node *equivalent_node_And(ir_node *n) {
1116         ir_node *oldn = n;
1117
1118         ir_node *a = get_And_left(n);
1119         ir_node *b = get_And_right(n);
1120
1121         if (a == b) {
1122                 n = a;    /* And has it's own neutral element */
1123                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1124                 return n;
1125         }
1126         if (is_Const(a) && is_Const_all_one(a)) {
1127                 n = b;
1128                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1129                 return n;
1130         }
1131         if (is_Const(b) && is_Const_all_one(b)) {
1132                 n = a;
1133                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1134                 return n;
1135         }
1136         if (is_Or(a)) {
1137                 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1138                         /* (a|X) & a */
1139                         n = b;
1140                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1141                         return n;
1142                 }
1143         }
1144         if (is_Or(b)) {
1145                 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1146                         /* a & (a|X) */
1147                         n = a;
1148                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1149                         return n;
1150                 }
1151         }
1152
1153         return n;
1154 }  /* equivalent_node_And */
1155
1156 /**
1157  * Try to remove useless Conv's:
1158  */
1159 static ir_node *equivalent_node_Conv(ir_node *n) {
1160         ir_node *oldn = n;
1161         ir_node *a = get_Conv_op(n);
1162         ir_node *b;
1163
1164         ir_mode *n_mode = get_irn_mode(n);
1165         ir_mode *a_mode = get_irn_mode(a);
1166
1167         if (n_mode == a_mode) { /* No Conv necessary */
1168                 if (get_Conv_strict(n)) {
1169                         /* special case: the predecessor might be a also a Conv */
1170                         if (is_Conv(a)) {
1171                                 if (! get_Conv_strict(a)) {
1172                                         /* first one is not strict, kick it */
1173                                         set_Conv_op(n, get_Conv_op(a));
1174                                         return n;
1175                                 }
1176                                 /* else both are strict conv, second is superflous */
1177                         } else if(is_Proj(a)) {
1178                                 ir_node *pred = get_Proj_pred(a);
1179                                 if(is_Load(pred)) {
1180                                         /* loads always return with the exact precision of n_mode */
1181                                         assert(get_Load_mode(pred) == n_mode);
1182                                         return a;
1183                                 }
1184                         }
1185
1186                         /* leave strict floating point Conv's */
1187                         return n;
1188                 }
1189                 n = a;
1190                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1191         } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1192                 ir_mode *b_mode;
1193
1194                 b = get_Conv_op(a);
1195                 n_mode = get_irn_mode(n);
1196                 b_mode = get_irn_mode(b);
1197
1198                 if (n_mode == b_mode) {
1199                         if (n_mode == mode_b) {
1200                                 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1201                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1202                         } else if (mode_is_int(n_mode)) {
1203                                 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1204                                         n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1205                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1206                                 }
1207                         }
1208                 }
1209         }
1210         return n;
1211 }  /* equivalent_node_Conv */
1212
1213 /**
1214  * A Cast may be removed if the type of the previous node
1215  * is already the type of the Cast.
1216  */
1217 static ir_node *equivalent_node_Cast(ir_node *n) {
1218         ir_node *oldn = n;
1219         ir_node *pred = get_Cast_op(n);
1220
1221         if (get_irn_type(pred) == get_Cast_type(n)) {
1222                 n = pred;
1223                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1224         }
1225         return n;
1226 }  /* equivalent_node_Cast */
1227
1228 /**
1229  * Several optimizations:
1230  * - no Phi in start block.
1231  * - remove Id operators that are inputs to Phi
1232  * - fold Phi-nodes, iff they have only one predecessor except
1233  *   themselves.
1234  */
1235 static ir_node *equivalent_node_Phi(ir_node *n) {
1236         int i, n_preds;
1237
1238         ir_node *oldn = n;
1239         ir_node *block = NULL;     /* to shutup gcc */
1240         ir_node *first_val = NULL; /* to shutup gcc */
1241
1242         if (!get_opt_normalize()) return n;
1243
1244         n_preds = get_Phi_n_preds(n);
1245
1246         block = get_nodes_block(n);
1247         /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1248         assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1249         if ((is_Block_dead(block)) ||                  /* Control dead */
1250                 (block == get_irg_start_block(current_ir_graph)))  /* There should be no Phi nodes */
1251                 return new_Bad();                                    /* in the Start Block. */
1252
1253         if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1254
1255         /* If the Block has a Bad pred, we also have one. */
1256         for (i = 0;  i < n_preds;  ++i)
1257                 if (is_Bad(get_Block_cfgpred(block, i)))
1258                         set_Phi_pred(n, i, new_Bad());
1259
1260         /* Find first non-self-referencing input */
1261         for (i = 0; i < n_preds; ++i) {
1262                 first_val = get_Phi_pred(n, i);
1263                 if (   (first_val != n)                            /* not self pointer */
1264 #if 1
1265                     && (! is_Bad(first_val))
1266 #endif
1267                    ) {        /* value not dead */
1268                         break;          /* then found first value. */
1269                 }
1270         }
1271
1272         if (i >= n_preds) {
1273                 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1274                 return new_Bad();
1275         }
1276
1277         /* search for rest of inputs, determine if any of these
1278         are non-self-referencing */
1279         while (++i < n_preds) {
1280                 ir_node *scnd_val = get_Phi_pred(n, i);
1281                 if (   (scnd_val != n)
1282                     && (scnd_val != first_val)
1283 #if 1
1284                     && (! is_Bad(scnd_val))
1285 #endif
1286                         ) {
1287                         break;
1288                 }
1289         }
1290
1291         if (i >= n_preds) {
1292                 /* Fold, if no multiple distinct non-self-referencing inputs */
1293                 n = first_val;
1294                 DBG_OPT_PHI(oldn, n);
1295         }
1296         return n;
1297 }  /* equivalent_node_Phi */
1298
1299 /**
1300  * Several optimizations:
1301  * - no Sync in start block.
1302  * - fold Sync-nodes, iff they have only one predecessor except
1303  *   themselves.
1304  */
1305 static ir_node *equivalent_node_Sync(ir_node *n) {
1306         int i, n_preds;
1307
1308         ir_node *oldn = n;
1309         ir_node *first_val = NULL; /* to shutup gcc */
1310
1311         if (!get_opt_normalize()) return n;
1312
1313         n_preds = get_Sync_n_preds(n);
1314
1315         /* Find first non-self-referencing input */
1316         for (i = 0; i < n_preds; ++i) {
1317                 first_val = get_Sync_pred(n, i);
1318                 if ((first_val != n)  /* not self pointer */ &&
1319                     (! is_Bad(first_val))
1320                    ) {                /* value not dead */
1321                         break;            /* then found first value. */
1322                 }
1323         }
1324
1325         if (i >= n_preds)
1326                 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1327                 return new_Bad();
1328
1329         /* search the rest of inputs, determine if any of these
1330            are non-self-referencing */
1331         while (++i < n_preds) {
1332                 ir_node *scnd_val = get_Sync_pred(n, i);
1333                 if ((scnd_val != n) &&
1334                     (scnd_val != first_val) &&
1335                     (! is_Bad(scnd_val))
1336                    )
1337                         break;
1338         }
1339
1340         if (i >= n_preds) {
1341                 /* Fold, if no multiple distinct non-self-referencing inputs */
1342                 n = first_val;
1343                 DBG_OPT_SYNC(oldn, n);
1344         }
1345         return n;
1346 }  /* equivalent_node_Sync */
1347
1348 /**
1349  * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1350  * ProjX(Load) and ProjX(Store).
1351  */
1352 static ir_node *equivalent_node_Proj(ir_node *proj) {
1353         ir_node *oldn = proj;
1354         ir_node *a = get_Proj_pred(proj);
1355
1356         if (is_Tuple(a)) {
1357                 /* Remove the Tuple/Proj combination. */
1358                 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1359                         proj = get_Tuple_pred(a, get_Proj_proj(proj));
1360                         DBG_OPT_TUPLE(oldn, a, proj);
1361                 } else {
1362                          /* This should not happen! */
1363                         assert(! "found a Proj with higher number than Tuple predecessors");
1364                         proj = new_Bad();
1365                 }
1366         } else if (get_irn_mode(proj) == mode_X) {
1367                 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1368                         /* Remove dead control flow -- early gigo(). */
1369                         proj = new_Bad();
1370                 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1371                         if (is_Load(a)) {
1372                                 /* get the Load address */
1373                                 ir_node *addr = get_Load_ptr(a);
1374                                 ir_node *blk  = get_irn_n(a, -1);
1375                                 ir_node *confirm;
1376
1377                                 if (value_not_null(addr, &confirm)) {
1378                                         if (confirm == NULL) {
1379                                                 /* this node may float if it did not depend on a Confirm */
1380                                                 set_irn_pinned(a, op_pin_state_floats);
1381                                         }
1382                                         if (get_Proj_proj(proj) == pn_Load_X_except) {
1383                                                 DBG_OPT_EXC_REM(proj);
1384                                                 return new_Bad();
1385                                         } else
1386                                                 return new_r_Jmp(current_ir_graph, blk);
1387                                 }
1388                         } else if (is_Store(a)) {
1389                                 /* get the load/store address */
1390                                 ir_node *addr = get_Store_ptr(a);
1391                                 ir_node *blk  = get_irn_n(a, -1);
1392                                 ir_node *confirm;
1393
1394                                 if (value_not_null(addr, &confirm)) {
1395                                         if (confirm == NULL) {
1396                                                 /* this node may float if it did not depend on a Confirm */
1397                                                 set_irn_pinned(a, op_pin_state_floats);
1398                                         }
1399                                         if (get_Proj_proj(proj) == pn_Store_X_except) {
1400                                                 DBG_OPT_EXC_REM(proj);
1401                                                 return new_Bad();
1402                                         } else
1403                                                 return new_r_Jmp(current_ir_graph, blk);
1404                                 }
1405                         }
1406                 }
1407         }
1408
1409         return proj;
1410 }  /* equivalent_node_Proj */
1411
1412 /**
1413  * Remove Id's.
1414  */
1415 static ir_node *equivalent_node_Id(ir_node *n) {
1416         ir_node *oldn = n;
1417
1418         do {
1419                 n = get_Id_pred(n);
1420         } while (get_irn_op(n) == op_Id);
1421
1422         DBG_OPT_ID(oldn, n);
1423         return n;
1424 }  /* equivalent_node_Id */
1425
1426 /**
1427  * Optimize a Mux.
1428  */
1429 static ir_node *equivalent_node_Mux(ir_node *n)
1430 {
1431         ir_node *oldn = n, *sel = get_Mux_sel(n);
1432         tarval *ts = value_of(sel);
1433
1434         /* Mux(true, f, t) == t */
1435         if (ts == tarval_b_true) {
1436                 n = get_Mux_true(n);
1437                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1438         }
1439         /* Mux(false, f, t) == f */
1440         else if (ts == tarval_b_false) {
1441                 n = get_Mux_false(n);
1442                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1443         }
1444         /* Mux(v, x, x) == x */
1445         else if (get_Mux_false(n) == get_Mux_true(n)) {
1446                 n = get_Mux_true(n);
1447                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1448         }
1449         else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1450                 ir_node *cmp = get_Proj_pred(sel);
1451                 long proj_nr = get_Proj_proj(sel);
1452                 ir_node *b   = get_Mux_false(n);
1453                 ir_node *a   = get_Mux_true(n);
1454
1455                 /*
1456                  * Note: normalization puts the constant on the right site,
1457                  * so we check only one case.
1458                  *
1459                  * Note further that these optimization work even for floating point
1460                  * with NaN's because -NaN == NaN.
1461                  * However, if +0 and -0 is handled differently, we cannot use the first one.
1462                  */
1463                 if (is_Cmp(cmp) && get_Cmp_left(cmp) == a) {
1464                         ir_node *cmp_r = get_Cmp_right(cmp);
1465                         if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
1466                                 /* Mux(a CMP 0, X, a) */
1467                                 if (is_Minus(b) && get_Minus_op(b) == a) {
1468                                         /* Mux(a CMP 0, -a, a) */
1469                                         if (proj_nr == pn_Cmp_Eq) {
1470                                                 /* Mux(a == 0, -a, a)  ==>  -a */
1471                                                 n = b;
1472                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1473                                         } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1474                                                 /* Mux(a != 0, -a, a)  ==> a */
1475                                                 n = a;
1476                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1477                                         }
1478                                 } else if (is_Const(b) && is_Const_null(b)) {
1479                                         /* Mux(a CMP 0, 0, a) */
1480                                         if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1481                                                 /* Mux(a != 0, 0, a) ==> a */
1482                                                 n = a;
1483                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1484                                         } else if (proj_nr == pn_Cmp_Eq) {
1485                                                 /* Mux(a == 0, 0, a) ==> 0 */
1486                                                 n = b;
1487                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1488                                         }
1489                                 }
1490                         }
1491                 }
1492         }
1493         return n;
1494 }  /* equivalent_node_Mux */
1495
1496 /**
1497  * Returns a equivalent node of a Psi: if a condition is true
1498  * and all previous conditions are false we know its value.
1499  * If all conditions are false its value is the default one.
1500  */
1501 static ir_node *equivalent_node_Psi(ir_node *n) {
1502         if (is_Mux(n))
1503                 return equivalent_node_Mux(n);
1504         return n;
1505 }  /* equivalent_node_Psi */
1506
1507 /**
1508  * Optimize -a CMP -b into b CMP a.
1509  * This works only for for modes where unary Minus
1510  * cannot Overflow.
1511  * Note that two-complement integers can Overflow
1512  * so it will NOT work.
1513  *
1514  * For == and != can be handled in Proj(Cmp)
1515  */
1516 static ir_node *equivalent_node_Cmp(ir_node *n) {
1517         ir_node *left  = get_Cmp_left(n);
1518         ir_node *right = get_Cmp_right(n);
1519
1520         if (is_Minus(left) && is_Minus(right) &&
1521                 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1522                 left  = get_Minus_op(left);
1523                 right = get_Minus_op(right);
1524                 set_Cmp_left(n, right);
1525                 set_Cmp_right(n, left);
1526         }
1527         return n;
1528 }  /* equivalent_node_Cmp */
1529
1530 /**
1531  * Remove Confirm nodes if setting is on.
1532  * Replace Confirms(x, '=', Constlike) by Constlike.
1533  */
1534 static ir_node *equivalent_node_Confirm(ir_node *n) {
1535         ir_node *pred = get_Confirm_value(n);
1536         pn_Cmp  pnc   = get_Confirm_cmp(n);
1537
1538         if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1539                 /*
1540                  * rare case: two identical Confirms one after another,
1541                  * replace the second one with the first.
1542                  */
1543                 n = pred;
1544         }
1545         if (pnc == pn_Cmp_Eq) {
1546                 ir_node *bound = get_Confirm_bound(n);
1547
1548                 /*
1549                  * Optimize a rare case:
1550                  * Confirm(x, '=', Constlike) ==> Constlike
1551                  */
1552                 if (is_irn_constlike(bound)) {
1553                         DBG_OPT_CONFIRM(n, bound);
1554                         return bound;
1555                 }
1556         }
1557         return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1558 }
1559
1560 /**
1561  * Optimize CopyB(mem, x, x) into a Nop.
1562  */
1563 static ir_node *equivalent_node_CopyB(ir_node *n) {
1564         ir_node *a = get_CopyB_dst(n);
1565         ir_node *b = get_CopyB_src(n);
1566
1567         if (a == b) {
1568                 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1569                 ir_node *mem = get_CopyB_mem(n);
1570                 ir_node *blk = get_nodes_block(n);
1571                 turn_into_tuple(n, pn_CopyB_max);
1572                 set_Tuple_pred(n, pn_CopyB_M,         mem);
1573                 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1574                 set_Tuple_pred(n, pn_CopyB_X_except,  new_Bad());        /* no exception */
1575                 set_Tuple_pred(n, pn_CopyB_M_except,  new_Bad());
1576         }
1577         return n;
1578 }  /* equivalent_node_CopyB */
1579
1580 /**
1581  * Optimize Bounds(idx, idx, upper) into idx.
1582  */
1583 static ir_node *equivalent_node_Bound(ir_node *n) {
1584         ir_node *idx   = get_Bound_index(n);
1585         ir_node *lower = get_Bound_lower(n);
1586         int ret_tuple = 0;
1587
1588         /* By definition lower < upper, so if idx == lower -->
1589         lower <= idx && idx < upper */
1590         if (idx == lower) {
1591                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1592                 ret_tuple = 1;
1593         } else {
1594                 ir_node *pred = skip_Proj(idx);
1595
1596                 if (get_irn_op(pred) == op_Bound) {
1597                         /*
1598                          * idx was Bounds_check previously, it is still valid if
1599                          * lower <= pred_lower && pred_upper <= upper.
1600                          */
1601                         ir_node *upper = get_Bound_upper(n);
1602                         if (get_Bound_lower(pred) == lower &&
1603                                 get_Bound_upper(pred) == upper) {
1604                                 /*
1605                                  * One could expect that we simply return the previous
1606                                  * Bound here. However, this would be wrong, as we could
1607                                  * add an exception Proj to a new location then.
1608                                  * So, we must turn in into a tuple.
1609                                  */
1610                                 ret_tuple = 1;
1611                         }
1612                 }
1613         }
1614         if (ret_tuple) {
1615                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1616                 ir_node *mem = get_Bound_mem(n);
1617                 ir_node *blk = get_nodes_block(n);
1618                 turn_into_tuple(n, pn_Bound_max);
1619                 set_Tuple_pred(n, pn_Bound_M,         mem);
1620                 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk));       /* no exception */
1621                 set_Tuple_pred(n, pn_Bound_X_except,  new_Bad());       /* no exception */
1622                 set_Tuple_pred(n, pn_Bound_res,       idx);
1623         }
1624         return n;
1625 }  /* equivalent_node_Bound */
1626
1627 /**
1628  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1629  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1630  * new nodes.  It is therefore safe to free n if the node returned is not n.
1631  * If a node returns a Tuple we can not just skip it.  If the size of the
1632  * in array fits, we transform n into a tuple (e.g., Div).
1633  */
1634 ir_node *equivalent_node(ir_node *n) {
1635         if (n->op->ops.equivalent_node)
1636                 return n->op->ops.equivalent_node(n);
1637         return n;
1638 }  /* equivalent_node */
1639
1640 /**
1641  * Sets the default equivalent node operation for an ir_op_ops.
1642  *
1643  * @param code   the opcode for the default operation
1644  * @param ops    the operations initialized
1645  *
1646  * @return
1647  *    The operations.
1648  */
1649 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1650 {
1651 #define CASE(a)                                      \
1652         case iro_##a:                                    \
1653                 ops->equivalent_node  = equivalent_node_##a; \
1654                 break
1655
1656         switch (code) {
1657         CASE(Block);
1658         CASE(Jmp);
1659         CASE(Raise);
1660         CASE(Or);
1661         CASE(Add);
1662         CASE(Eor);
1663         CASE(Sub);
1664         CASE(Shl);
1665         CASE(Shr);
1666         CASE(Shrs);
1667         CASE(Rot);
1668         CASE(Not);
1669         CASE(Minus);
1670         CASE(Mul);
1671         CASE(Div);
1672         CASE(Quot);
1673         CASE(DivMod);
1674         CASE(And);
1675         CASE(Conv);
1676         CASE(Cast);
1677         CASE(Phi);
1678         CASE(Sync);
1679         CASE(Proj);
1680         CASE(Id);
1681         CASE(Mux);
1682         CASE(Psi);
1683         CASE(Cmp);
1684         CASE(Confirm);
1685         CASE(CopyB);
1686         CASE(Bound);
1687         default:
1688                 /* leave NULL */;
1689         }
1690
1691         return ops;
1692 #undef CASE
1693 }  /* firm_set_default_equivalent_node */
1694
1695 /**
1696  * Returns non-zero if a node is a Phi node
1697  * with all predecessors constant.
1698  */
1699 static int is_const_Phi(ir_node *n) {
1700         int i;
1701
1702         if (! is_Phi(n))
1703                 return 0;
1704         for (i = get_irn_arity(n) - 1; i >= 0; --i)
1705                 if (! is_Const(get_irn_n(n, i)))
1706                         return 0;
1707                 return 1;
1708 }  /* is_const_Phi */
1709
1710 /**
1711  * Apply an evaluator on a binop with a constant operators (and one Phi).
1712  *
1713  * @param phi    the Phi node
1714  * @param other  the other operand
1715  * @param eval   an evaluator function
1716  * @param mode   the mode of the result, may be different from the mode of the Phi!
1717  * @param left   if non-zero, other is the left operand, else the right
1718  *
1719  * @return a new Phi node if the conversion was successful, NULL else
1720  */
1721 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1722         tarval   *tv;
1723         void     **res;
1724         ir_node  *pred;
1725         ir_graph *irg;
1726         int      i, n = get_irn_arity(phi);
1727
1728         NEW_ARR_A(void *, res, n);
1729         if (left) {
1730                 for (i = 0; i < n; ++i) {
1731                         pred = get_irn_n(phi, i);
1732                         tv   = get_Const_tarval(pred);
1733                         tv   = eval(other, tv);
1734
1735                         if (tv == tarval_bad) {
1736                                 /* folding failed, bad */
1737                                 return NULL;
1738                         }
1739                         res[i] = tv;
1740                 }
1741         } else {
1742                 for (i = 0; i < n; ++i) {
1743                         pred = get_irn_n(phi, i);
1744                         tv   = get_Const_tarval(pred);
1745                         tv   = eval(tv, other);
1746
1747                         if (tv == tarval_bad) {
1748                                 /* folding failed, bad */
1749                                 return 0;
1750                         }
1751                         res[i] = tv;
1752                 }
1753         }
1754         irg  = current_ir_graph;
1755         for (i = 0; i < n; ++i) {
1756                 pred = get_irn_n(phi, i);
1757                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1758                         mode, res[i], get_Const_type(pred));
1759         }
1760         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1761 }  /* apply_binop_on_phi */
1762
1763 /**
1764  * Apply an evaluator on a unop with a constant operator (a Phi).
1765  *
1766  * @param phi    the Phi node
1767  * @param eval   an evaluator function
1768  *
1769  * @return a new Phi node if the conversion was successful, NULL else
1770  */
1771 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1772         tarval   *tv;
1773         void     **res;
1774         ir_node  *pred;
1775         ir_mode  *mode;
1776         ir_graph *irg;
1777         int      i, n = get_irn_arity(phi);
1778
1779         NEW_ARR_A(void *, res, n);
1780         for (i = 0; i < n; ++i) {
1781                 pred = get_irn_n(phi, i);
1782                 tv   = get_Const_tarval(pred);
1783                 tv   = eval(tv);
1784
1785                 if (tv == tarval_bad) {
1786                         /* folding failed, bad */
1787                         return 0;
1788                 }
1789                 res[i] = tv;
1790         }
1791         mode = get_irn_mode(phi);
1792         irg  = current_ir_graph;
1793         for (i = 0; i < n; ++i) {
1794                 pred = get_irn_n(phi, i);
1795                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1796                         mode, res[i], get_Const_type(pred));
1797         }
1798         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1799 }  /* apply_unop_on_phi */
1800
1801 /**
1802  * Apply a conversion on a constant operator (a Phi).
1803  *
1804  * @param phi    the Phi node
1805  *
1806  * @return a new Phi node if the conversion was successful, NULL else
1807  */
1808 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1809         tarval   *tv;
1810         void     **res;
1811         ir_node  *pred;
1812         ir_graph *irg;
1813         int      i, n = get_irn_arity(phi);
1814
1815         NEW_ARR_A(void *, res, n);
1816         for (i = 0; i < n; ++i) {
1817                 pred = get_irn_n(phi, i);
1818                 tv   = get_Const_tarval(pred);
1819                 tv   = tarval_convert_to(tv, mode);
1820
1821                 if (tv == tarval_bad) {
1822                         /* folding failed, bad */
1823                         return 0;
1824                 }
1825                 res[i] = tv;
1826         }
1827         irg  = current_ir_graph;
1828         for (i = 0; i < n; ++i) {
1829                 pred = get_irn_n(phi, i);
1830                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1831                         mode, res[i], get_Const_type(pred));
1832         }
1833         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1834 }  /* apply_conv_on_phi */
1835
1836 /**
1837  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1838  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1839  * If possible, remove the Conv's.
1840  */
1841 static ir_node *transform_node_AddSub(ir_node *n) {
1842         ir_mode *mode = get_irn_mode(n);
1843
1844         if (mode_is_reference(mode)) {
1845                 ir_node *left  = get_binop_left(n);
1846                 ir_node *right = get_binop_right(n);
1847                 int ref_bits   = get_mode_size_bits(mode);
1848
1849                 if (is_Conv(left)) {
1850                         ir_mode *mode = get_irn_mode(left);
1851                         int bits      = get_mode_size_bits(mode);
1852
1853                         if (ref_bits == bits &&
1854                             mode_is_int(mode) &&
1855                             get_mode_arithmetic(mode) == irma_twos_complement) {
1856                                 ir_node *pre      = get_Conv_op(left);
1857                                 ir_mode *pre_mode = get_irn_mode(pre);
1858
1859                                 if (mode_is_int(pre_mode) &&
1860                                     get_mode_size_bits(pre_mode) == bits &&
1861                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1862                                         /* ok, this conv just changes to sign, moreover the calculation
1863                                          * is done with same number of bits as our address mode, so
1864                                          * we can ignore the conv as address calculation can be viewed
1865                                          * as either signed or unsigned
1866                                          */
1867                                         set_binop_left(n, pre);
1868                                 }
1869                         }
1870                 }
1871
1872                 if (is_Conv(right)) {
1873                         ir_mode *mode = get_irn_mode(right);
1874                         int bits      = get_mode_size_bits(mode);
1875
1876                         if (ref_bits == bits &&
1877                                 mode_is_int(mode) &&
1878                                 get_mode_arithmetic(mode) == irma_twos_complement) {
1879                                 ir_node *pre      = get_Conv_op(right);
1880                                 ir_mode *pre_mode = get_irn_mode(pre);
1881
1882                                 if (mode_is_int(pre_mode) &&
1883                                     get_mode_size_bits(pre_mode) == bits &&
1884                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1885                                         /* ok, this conv just changes to sign, moreover the calculation
1886                                          * is done with same number of bits as our address mode, so
1887                                          * we can ignore the conv as address calculation can be viewed
1888                                          * as either signed or unsigned
1889                                          */
1890                                         set_binop_right(n, pre);
1891                                 }
1892                         }
1893                 }
1894         }
1895         return n;
1896 }  /* transform_node_AddSub */
1897
1898 #define HANDLE_BINOP_PHI(eval, a, b, c, mode)                     \
1899   c = NULL;                                                       \
1900   if (is_Const(b) && is_const_Phi(a)) {                           \
1901     /* check for Op(Phi, Const) */                                \
1902     c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1903   }                                                               \
1904   else if (is_Const(a) && is_const_Phi(b)) {                      \
1905     /* check for Op(Const, Phi) */                                \
1906     c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1907   }                                                               \
1908   if (c) {                                                        \
1909     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);                   \
1910     return c;                                                     \
1911   }
1912
1913 #define HANDLE_UNOP_PHI(eval, a, c)               \
1914   c = NULL;                                       \
1915   if (is_const_Phi(a)) {                          \
1916     /* check for Op(Phi) */                       \
1917     c = apply_unop_on_phi(a, eval);               \
1918     if (c) {                                      \
1919       DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1920       return c;                                   \
1921     }                                             \
1922   }
1923
1924 /**
1925  * Do the AddSub optimization, then Transform
1926  *   Constant folding on Phi
1927  *   Add(a,a)          -> Mul(a, 2)
1928  *   Add(Mul(a, x), a) -> Mul(a, x+1)
1929  * if the mode is integer or float.
1930  * Transform Add(a,-b) into Sub(a,b).
1931  * Reassociation might fold this further.
1932  */
1933 static ir_node *transform_node_Add(ir_node *n) {
1934         ir_mode *mode;
1935         ir_node *a, *b, *c, *oldn = n;
1936
1937         n = transform_node_AddSub(n);
1938
1939         a = get_Add_left(n);
1940         b = get_Add_right(n);
1941
1942         mode = get_irn_mode(n);
1943         HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
1944
1945         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1946         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1947                 return n;
1948
1949         if (mode_is_num(mode)) {
1950                 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1951                 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
1952                         ir_node *block = get_irn_n(n, -1);
1953
1954                         n = new_rd_Mul(
1955                                 get_irn_dbg_info(n),
1956                                 current_ir_graph,
1957                                 block,
1958                                 a,
1959                                 new_r_Const_long(current_ir_graph, block, mode, 2),
1960                                 mode);
1961                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1962                         return n;
1963                 }
1964                 if (is_Minus(a)) {
1965                         n = new_rd_Sub(
1966                                         get_irn_dbg_info(n),
1967                                         current_ir_graph,
1968                                         get_irn_n(n, -1),
1969                                         b,
1970                                         get_Minus_op(a),
1971                                         mode);
1972                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1973                         return n;
1974                 }
1975                 if (is_Minus(b)) {
1976                         n = new_rd_Sub(
1977                                         get_irn_dbg_info(n),
1978                                         current_ir_graph,
1979                                         get_irn_n(n, -1),
1980                                         a,
1981                                         get_Minus_op(b),
1982                                         mode);
1983                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1984                         return n;
1985                 }
1986                 if (! is_reassoc_running()) {
1987                         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1988                         if (is_Mul(a)) {
1989                                 ir_node *ma = get_Mul_left(a);
1990                                 ir_node *mb = get_Mul_right(a);
1991
1992                                 if (b == ma) {
1993                                         ir_node *blk = get_irn_n(n, -1);
1994                                         n = new_rd_Mul(
1995                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1996                                                         ma,
1997                                                         new_rd_Add(
1998                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1999                                                                 mb,
2000                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2001                                                                 mode),
2002                                                         mode);
2003                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2004                                         return n;
2005                                 } else if (b == mb) {
2006                                         ir_node *blk = get_irn_n(n, -1);
2007                                         n = new_rd_Mul(
2008                                                         get_irn_dbg_info(n), current_ir_graph, blk,
2009                                                         mb,
2010                                                         new_rd_Add(
2011                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
2012                                                                 ma,
2013                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2014                                                                 mode),
2015                                                         mode);
2016                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2017                                         return n;
2018                                 }
2019                         }
2020                         if (is_Mul(b)) {
2021                                 ir_node *ma = get_Mul_left(b);
2022                                 ir_node *mb = get_Mul_right(b);
2023
2024                                 if (a == ma) {
2025                                         ir_node *blk = get_irn_n(n, -1);
2026                                         n = new_rd_Mul(
2027                                                         get_irn_dbg_info(n), current_ir_graph, blk,
2028                                                         ma,
2029                                                         new_rd_Add(
2030                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
2031                                                                 mb,
2032                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2033                                                                 mode),
2034                                                         mode);
2035                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2036                                         return n;
2037                                 }
2038                                 if (a == mb) {
2039                                         ir_node *blk = get_irn_n(n, -1);
2040                                         n = new_rd_Mul(
2041                                                         get_irn_dbg_info(n), current_ir_graph, blk,
2042                                                         mb,
2043                                                         new_rd_Add(
2044                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
2045                                                                 ma,
2046                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2047                                                                 mode),
2048                                                         mode);
2049                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2050                                         return n;
2051                                 }
2052                         }
2053                 }
2054                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2055                         /* Here we rely on constants be on the RIGHT side */
2056                         if (is_Not(a)) {
2057                                 ir_node *op = get_Not_op(a);
2058
2059                                 if (is_Const(b) && is_Const_one(b)) {
2060                                         /* ~x + 1 = -x */
2061                                         ir_node *blk = get_irn_n(n, -1);
2062                                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2063                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2064                                         return n;
2065                                 }
2066                                 if (op == b) {
2067                                         /* ~x + x = -1 */
2068                                         ir_node *blk = get_irn_n(n, -1);
2069                                         n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2070                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2071                                         return n;
2072                                 }
2073                         }
2074                         if (is_Not(b)) {
2075                                 ir_node *op = get_Not_op(b);
2076
2077                                 if (op == a) {
2078                                         /* x + ~x = -1 */
2079                                         ir_node *blk = get_irn_n(n, -1);
2080                                         n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2081                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2082                                         return n;
2083                                 }
2084                         }
2085                 }
2086         }
2087         return n;
2088 }  /* transform_node_Add */
2089
2090 /**
2091  * returns -cnst or NULL if impossible
2092  */
2093 static ir_node *const_negate(ir_node *cnst) {
2094         tarval   *tv    = tarval_neg(get_Const_tarval(cnst));
2095         dbg_info *dbgi  = get_irn_dbg_info(cnst);
2096         ir_graph *irg   = get_irn_irg(cnst);
2097         ir_node  *block = get_nodes_block(cnst);
2098         ir_mode  *mode  = get_irn_mode(cnst);
2099         if (tv == tarval_bad) return NULL;
2100         return new_rd_Const(dbgi, irg, block, mode, tv);
2101 }
2102
2103 /**
2104  * Do the AddSub optimization, then Transform
2105  *   Constant folding on Phi
2106  *   Sub(0,a)          -> Minus(a)
2107  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2108  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2109  *   Sub(Add(a, x), x) -> a
2110  *   Sub(x, Add(x, a)) -> -a
2111  *   Sub(x, Const)     -> Add(x, -Const)
2112  */
2113 static ir_node *transform_node_Sub(ir_node *n) {
2114         ir_mode *mode;
2115         ir_node *oldn = n;
2116         ir_node *a, *b, *c;
2117
2118         n = transform_node_AddSub(n);
2119
2120         a = get_Sub_left(n);
2121         b = get_Sub_right(n);
2122
2123         mode = get_irn_mode(n);
2124
2125 restart:
2126         HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2127
2128         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2129         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2130                 return n;
2131
2132         if (is_Const(b) && get_irn_mode(b) != mode_P) {
2133                 /* a - C -> a + (-C) */
2134                 ir_node *cnst = const_negate(b);
2135                 if (cnst != NULL) {
2136                         ir_node  *block = get_nodes_block(n);
2137                         dbg_info *dbgi  = get_irn_dbg_info(n);
2138                         ir_graph *irg   = get_irn_irg(n);
2139
2140                         n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2141                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2142                         return n;
2143                 }
2144         }
2145
2146         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2147                 ir_graph *irg   = current_ir_graph;
2148                 dbg_info *dbg   = get_irn_dbg_info(n);
2149                 ir_node  *block = get_nodes_block(n);
2150                 ir_node  *left  = get_Minus_op(a);
2151                 ir_node  *add   = new_rd_Add(dbg, irg, block, left, b, mode);
2152
2153                 n = new_rd_Minus(dbg, irg, block, add, mode);
2154                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2155                 return n;
2156         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2157                 ir_graph *irg   = current_ir_graph;
2158                 dbg_info *dbg   = get_irn_dbg_info(n);
2159                 ir_node  *block = get_nodes_block(n);
2160                 ir_node  *right = get_Minus_op(b);
2161
2162                 n = new_rd_Add(dbg, irg, block, a, right, mode);
2163                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2164                 return n;
2165         } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2166                 ir_graph *irg     = current_ir_graph;
2167                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2168                 ir_node  *s_block = get_nodes_block(b);
2169                 ir_node  *s_left  = get_Sub_right(b);
2170                 ir_node  *s_right = get_Sub_left(b);
2171                 ir_mode  *s_mode  = get_irn_mode(b);
2172                 ir_node  *sub     = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2173                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2174                 ir_node  *a_block = get_nodes_block(n);
2175
2176                 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2177                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2178                 return n;
2179         } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2180                 ir_node *m_right = get_Mul_right(b);
2181                 if (is_Const(m_right)) {
2182                         ir_node *cnst2 = const_negate(m_right);
2183                         if (cnst2 != NULL) {
2184                                 ir_graph *irg     = current_ir_graph;
2185                                 dbg_info *m_dbg   = get_irn_dbg_info(b);
2186                                 ir_node  *m_block = get_nodes_block(b);
2187                                 ir_node  *m_left  = get_Mul_left(b);
2188                                 ir_mode  *m_mode  = get_irn_mode(b);
2189                                 ir_node  *mul     = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2190                                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2191                                 ir_node  *a_block = get_nodes_block(n);
2192
2193                                 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2194                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2195                                 return n;
2196                         }
2197                 }
2198         }
2199
2200         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2201         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2202                 n = new_rd_Minus(
2203                                 get_irn_dbg_info(n),
2204                                 current_ir_graph,
2205                                 get_irn_n(n, -1),
2206                                 b,
2207                                 mode);
2208                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2209                 return n;
2210         }
2211         if (is_Add(a)) {
2212                 if (mode_wrap_around(mode)) {
2213                         ir_node *left  = get_Add_left(a);
2214                         ir_node *right = get_Add_right(a);
2215
2216                         /* FIXME: Does the Conv's work only for two complement or generally? */
2217                         if (left == b) {
2218                                 if (mode != get_irn_mode(right)) {
2219                                         /* This Sub is an effective Cast */
2220                                         right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2221                                 }
2222                                 n = right;
2223                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2224                                 return n;
2225                         } else if (right == b) {
2226                                 if (mode != get_irn_mode(left)) {
2227                                         /* This Sub is an effective Cast */
2228                                         left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2229                                 }
2230                                 n = left;
2231                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2232                                 return n;
2233                         }
2234                 }
2235         }
2236         if (is_Add(b)) {
2237                 if (mode_wrap_around(mode)) {
2238                         ir_node *left  = get_Add_left(b);
2239                         ir_node *right = get_Add_right(b);
2240
2241                         /* FIXME: Does the Conv's work only for two complement or generally? */
2242                         if (left == a) {
2243                                 ir_mode *r_mode = get_irn_mode(right);
2244
2245                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2246                                 if (mode != r_mode) {
2247                                         /* This Sub is an effective Cast */
2248                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2249                                 }
2250                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2251                                 return n;
2252                         } else if (right == a) {
2253                                 ir_mode *l_mode = get_irn_mode(left);
2254
2255                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2256                                 if (mode != l_mode) {
2257                                         /* This Sub is an effective Cast */
2258                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2259                                 }
2260                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2261                                 return n;
2262                         }
2263                 }
2264         }
2265         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2266                 ir_mode *mode = get_irn_mode(a);
2267
2268                 if (mode == get_irn_mode(b)) {
2269                         ir_mode *ma, *mb;
2270
2271                         a = get_Conv_op(a);
2272                         b = get_Conv_op(b);
2273
2274                         /* check if it's allowed to skip the conv */
2275                         ma = get_irn_mode(a);
2276                         mb = get_irn_mode(b);
2277
2278                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2279                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2280                                 set_Sub_left(n, a);
2281                                 set_Sub_right(n, b);
2282
2283                                 goto restart;
2284                         }
2285                 }
2286         }
2287         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2288         if (!is_reassoc_running() && is_Mul(a)) {
2289                 ir_node *ma = get_Mul_left(a);
2290                 ir_node *mb = get_Mul_right(a);
2291
2292                 if (ma == b) {
2293                         ir_node *blk = get_irn_n(n, -1);
2294                         n = new_rd_Mul(
2295                                         get_irn_dbg_info(n),
2296                                         current_ir_graph, blk,
2297                                         ma,
2298                                         new_rd_Sub(
2299                                                 get_irn_dbg_info(n),
2300                                                 current_ir_graph, blk,
2301                                                 mb,
2302                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2303                                                 mode),
2304                                         mode);
2305                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2306                         return n;
2307                 } else if (mb == b) {
2308                         ir_node *blk = get_irn_n(n, -1);
2309                         n = new_rd_Mul(
2310                                         get_irn_dbg_info(n),
2311                                         current_ir_graph, blk,
2312                                         mb,
2313                                         new_rd_Sub(
2314                                                 get_irn_dbg_info(n),
2315                                                 current_ir_graph, blk,
2316                                                 ma,
2317                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2318                                                 mode),
2319                                         mode);
2320                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2321                         return n;
2322                 }
2323         }
2324         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2325                 ir_node *x   =      get_Sub_left(a);
2326                 ir_node *y        = get_Sub_right(a);
2327                 ir_node *blk      = get_irn_n(n, -1);
2328                 ir_mode *m_b      = get_irn_mode(b);
2329                 ir_mode *m_y      = get_irn_mode(y);
2330                 ir_mode *add_mode;
2331                 ir_node *add;
2332
2333                 /* Determine the right mode for the Add. */
2334                 if (m_b == m_y)
2335                         add_mode = m_b;
2336                 else if (mode_is_reference(m_b))
2337                         add_mode = m_b;
2338                 else if (mode_is_reference(m_y))
2339                         add_mode = m_y;
2340                 else {
2341                         /*
2342                          * Both modes are different but none is reference,
2343                          * happens for instance in SubP(SubP(P, Iu), Is).
2344                          * We have two possibilities here: Cast or ignore.
2345                          * Currently we ignore this case.
2346                          */
2347                         return n;
2348                 }
2349
2350                 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2351
2352                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2353                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2354                 return n;
2355         }
2356
2357         if (get_mode_arithmetic(mode) == irma_twos_complement) {
2358                 if (is_Const(a) && is_Not(b)) {
2359                         /* c - ~X = X + (c+1) */
2360                         tarval *tv = get_Const_tarval(a);
2361
2362                         tv = tarval_add(tv, get_mode_one(mode));
2363                         if (tv != tarval_bad) {
2364                                 ir_node *blk = get_irn_n(n, -1);
2365                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2366                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2367                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2368                                 return n;
2369                         }
2370                 }
2371         }
2372         return n;
2373 }  /* transform_node_Sub */
2374
2375 /**
2376  * Several transformation done on n*n=2n bits mul.
2377  * These transformations must be done here because new nodes may be produced.
2378  */
2379 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2380         ir_node *oldn = n;
2381         ir_node *a = get_Mul_left(n);
2382         ir_node *b = get_Mul_right(n);
2383         tarval *ta = value_of(a);
2384         tarval *tb = value_of(b);
2385         ir_mode *smode = get_irn_mode(a);
2386
2387         if (ta == get_mode_one(smode)) {
2388                 ir_node *blk = get_irn_n(n, -1);
2389                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2390                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2391                 return n;
2392         }
2393         else if (ta == get_mode_minus_one(smode)) {
2394                 ir_node *blk = get_irn_n(n, -1);
2395                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2396                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2397                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2398                 return n;
2399         }
2400         if (tb == get_mode_one(smode)) {
2401                 ir_node *blk = get_irn_n(a, -1);
2402                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2403                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2404                 return n;
2405         }
2406         else if (tb == get_mode_minus_one(smode)) {
2407                 ir_node *blk = get_irn_n(n, -1);
2408                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2409                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2410                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2411                 return n;
2412         }
2413         return n;
2414 }
2415
2416 /**
2417  * Transform Mul(a,-1) into -a.
2418  * Do constant evaluation of Phi nodes.
2419  * Do architecture dependent optimizations on Mul nodes
2420  */
2421 static ir_node *transform_node_Mul(ir_node *n) {
2422         ir_node *c, *oldn = n;
2423         ir_mode *mode = get_irn_mode(n);
2424         ir_node *a = get_Mul_left(n);
2425         ir_node *b = get_Mul_right(n);
2426
2427         if (is_Bad(a) || is_Bad(b))
2428                 return n;
2429
2430         if (mode != get_irn_mode(a))
2431                 return transform_node_Mul2n(n, mode);
2432
2433         HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2434
2435         if (mode_is_signed(mode)) {
2436                 ir_node *r = NULL;
2437
2438                 if (value_of(a) == get_mode_minus_one(mode))
2439                         r = b;
2440                 else if (value_of(b) == get_mode_minus_one(mode))
2441                         r = a;
2442                 if (r) {
2443                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2444                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2445                         return n;
2446                 }
2447         }
2448         if (is_Minus(a)) {
2449                 if (is_Const(b)) { /* (-a) * const -> a * -const */
2450                         ir_node *cnst = const_negate(b);
2451                         if (cnst != NULL) {
2452                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2453                                 ir_node  *block = get_nodes_block(n);
2454                                 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2455                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2456                                 return n;
2457                         }
2458                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2459                         dbg_info *dbgi  = get_irn_dbg_info(n);
2460                         ir_node  *block = get_nodes_block(n);
2461                         n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2462                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2463                         return n;
2464                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2465                         ir_node  *sub_l = get_Sub_left(b);
2466                         ir_node  *sub_r = get_Sub_right(b);
2467                         dbg_info *dbgi  = get_irn_dbg_info(n);
2468                         ir_graph *irg   = current_ir_graph;
2469                         ir_node  *block = get_nodes_block(n);
2470                         ir_node  *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2471                         n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2472                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2473                         return n;
2474                 }
2475         } else if (is_Minus(b)) {
2476                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2477                         ir_node  *sub_l = get_Sub_left(a);
2478                         ir_node  *sub_r = get_Sub_right(a);
2479                         dbg_info *dbgi  = get_irn_dbg_info(n);
2480                         ir_graph *irg   = current_ir_graph;
2481                         ir_node  *block = get_nodes_block(n);
2482                         ir_node  *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2483                         n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2484                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2485                         return n;
2486                 }
2487         }
2488         if (get_mode_arithmetic(mode) == irma_ieee754) {
2489                 if (is_Const(a)) {
2490                         tarval *tv = get_Const_tarval(a);
2491                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2492                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2493                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2494                                 return n;
2495                         }
2496                 }
2497                 else if (is_Const(b)) {
2498                         tarval *tv = get_Const_tarval(b);
2499                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2500                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2501                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2502                                 return n;
2503                         }
2504                 }
2505         }
2506         return arch_dep_replace_mul_with_shifts(n);
2507 }  /* transform_node_Mul */
2508
2509 /**
2510  * Transform a Div Node.
2511  */
2512 static ir_node *transform_node_Div(ir_node *n) {
2513         tarval *tv = value_of(n);
2514         ir_mode *mode = get_Div_resmode(n);
2515         ir_node *value = n;
2516
2517         if (tv != tarval_bad) {
2518                 value = new_Const(get_tarval_mode(tv), tv);
2519
2520                 DBG_OPT_CSTEVAL(n, value);
2521                 goto make_tuple;
2522         } else {
2523                 ir_node *a = get_Div_left(n);
2524                 ir_node *b = get_Div_right(n);
2525                 ir_node *dummy;
2526
2527                 if (a == b && value_not_zero(a, &dummy)) {
2528                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2529                         value = new_Const(mode, get_mode_one(mode));
2530                         DBG_OPT_CSTEVAL(n, value);
2531                         goto make_tuple;
2532                 } else {
2533                         if (mode_is_signed(mode) && is_Const(b)) {
2534                                 tarval *tv = get_Const_tarval(b);
2535
2536                                 if (tv == get_mode_minus_one(mode)) {
2537                                         /* a / -1 */
2538                                         value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2539                                         DBG_OPT_CSTEVAL(n, value);
2540                                         goto make_tuple;
2541                                 }
2542                         }
2543                         /* Try architecture dependent optimization */
2544                         value = arch_dep_replace_div_by_const(n);
2545                 }
2546         }
2547
2548         if (value != n) {
2549                 ir_node *mem, *blk;
2550
2551 make_tuple:
2552                 /* Turn Div into a tuple (mem, jmp, bad, value) */
2553                 mem = get_Div_mem(n);
2554                 blk = get_irn_n(n, -1);
2555
2556                 /* skip a potential Pin */
2557                 if (is_Pin(mem))
2558                         mem = get_Pin_op(mem);
2559                 turn_into_tuple(n, pn_Div_max);
2560                 set_Tuple_pred(n, pn_Div_M,         mem);
2561                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2562                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());
2563                 set_Tuple_pred(n, pn_Div_res,       value);
2564         }
2565         return n;
2566 }  /* transform_node_Div */
2567
2568 /**
2569  * Transform a Mod node.
2570  */
2571 static ir_node *transform_node_Mod(ir_node *n) {
2572         tarval *tv = value_of(n);
2573         ir_mode *mode = get_Mod_resmode(n);
2574         ir_node *value = n;
2575
2576         if (tv != tarval_bad) {
2577                 value = new_Const(get_tarval_mode(tv), tv);
2578
2579                 DBG_OPT_CSTEVAL(n, value);
2580                 goto make_tuple;
2581         } else {
2582                 ir_node *a = get_Mod_left(n);
2583                 ir_node *b = get_Mod_right(n);
2584                 ir_node *dummy;
2585
2586                 if (a == b && value_not_zero(a, &dummy)) {
2587                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2588                         value = new_Const(mode, get_mode_null(mode));
2589                         DBG_OPT_CSTEVAL(n, value);
2590                         goto make_tuple;
2591                 } else {
2592                         if (mode_is_signed(mode) && is_Const(b)) {
2593                                 tarval *tv = get_Const_tarval(b);
2594
2595                                 if (tv == get_mode_minus_one(mode)) {
2596                                         /* a % -1 = 0 */
2597                                         value = new_Const(mode, get_mode_null(mode));
2598                                         DBG_OPT_CSTEVAL(n, value);
2599                                         goto make_tuple;
2600                                 }
2601                         }
2602                         /* Try architecture dependent optimization */
2603                         value = arch_dep_replace_mod_by_const(n);
2604                 }
2605         }
2606
2607         if (value != n) {
2608                 ir_node *mem, *blk;
2609
2610 make_tuple:
2611                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2612                 mem = get_Mod_mem(n);
2613                 blk = get_irn_n(n, -1);
2614
2615                 /* skip a potential Pin */
2616                 if (is_Pin(mem))
2617                         mem = get_Pin_op(mem);
2618                 turn_into_tuple(n, pn_Mod_max);
2619                 set_Tuple_pred(n, pn_Mod_M,         mem);
2620                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2621                 set_Tuple_pred(n, pn_Mod_X_except,  new_Bad());
2622                 set_Tuple_pred(n, pn_Mod_res,       value);
2623         }
2624         return n;
2625 }  /* transform_node_Mod */
2626
2627 /**
2628  * Transform a DivMod node.
2629  */
2630 static ir_node *transform_node_DivMod(ir_node *n) {
2631         ir_node *dummy;
2632         ir_node *a = get_DivMod_left(n);
2633         ir_node *b = get_DivMod_right(n);
2634         ir_mode *mode = get_DivMod_resmode(n);
2635         tarval *ta = value_of(a);
2636         tarval *tb = value_of(b);
2637         int evaluated = 0;
2638
2639         if (tb != tarval_bad) {
2640                 if (tb == get_mode_one(get_tarval_mode(tb))) {
2641                         b = new_Const(mode, get_mode_null(mode));
2642                         DBG_OPT_CSTEVAL(n, b);
2643                         goto make_tuple;
2644                 } else if (ta != tarval_bad) {
2645                         tarval *resa, *resb;
2646                         resa = tarval_div(ta, tb);
2647                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2648                                                              Jmp for X result!? */
2649                         resb = tarval_mod(ta, tb);
2650                         if (resb == tarval_bad) return n; /* Causes exception! */
2651                         a = new_Const(mode, resa);
2652                         b = new_Const(mode, resb);
2653                         DBG_OPT_CSTEVAL(n, a);
2654                         DBG_OPT_CSTEVAL(n, b);
2655                         goto make_tuple;
2656                 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2657                         a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2658                         b = new_Const(mode, get_mode_null(mode));
2659                         DBG_OPT_CSTEVAL(n, a);
2660                         DBG_OPT_CSTEVAL(n, b);
2661                         goto make_tuple;
2662                 } else { /* Try architecture dependent optimization */
2663                         arch_dep_replace_divmod_by_const(&a, &b, n);
2664                         evaluated = a != NULL;
2665                 }
2666         } else if (a == b) {
2667                 if (value_not_zero(a, &dummy)) {
2668                         /* a/a && a != 0 */
2669                         a = new_Const(mode, get_mode_one(mode));
2670                         b = new_Const(mode, get_mode_null(mode));
2671                         DBG_OPT_CSTEVAL(n, a);
2672                         DBG_OPT_CSTEVAL(n, b);
2673                         goto make_tuple;
2674                 } else {
2675                         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2676                         return n;
2677                 }
2678         } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2679                 /* 0 / non-Const = 0 */
2680                 b = a;
2681                 goto make_tuple;
2682         }
2683
2684         if (evaluated) { /* replace by tuple */
2685                 ir_node *mem, *blk;
2686
2687 make_tuple:
2688                 mem = get_DivMod_mem(n);
2689                 /* skip a potential Pin */
2690                 if (is_Pin(mem))
2691                         mem = get_Pin_op(mem);
2692
2693                 blk = get_irn_n(n, -1);
2694                 turn_into_tuple(n, pn_DivMod_max);
2695                 set_Tuple_pred(n, pn_DivMod_M,         mem);
2696                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2697                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());  /* no exception */
2698                 set_Tuple_pred(n, pn_DivMod_res_div,   a);
2699                 set_Tuple_pred(n, pn_DivMod_res_mod,  b);
2700         }
2701
2702         return n;
2703 }  /* transform_node_DivMod */
2704
2705 /**
2706  * Optimize x / c to x * (1/c)
2707  */
2708 static ir_node *transform_node_Quot(ir_node *n) {
2709         ir_mode *mode = get_Quot_resmode(n);
2710         ir_node *oldn = n;
2711
2712         if (get_mode_arithmetic(mode) == irma_ieee754) {
2713                 ir_node *b = get_Quot_right(n);
2714
2715                 if (is_Const(b)) {
2716                         tarval *tv = get_Const_tarval(b);
2717
2718                         tv = tarval_quo(get_mode_one(mode), tv);
2719
2720                         /* Do the transformation if the result is either exact or we are not
2721                            using strict rules. */
2722                         if (tv != tarval_bad &&
2723                             (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2724                                 ir_node *blk = get_irn_n(n, -1);
2725                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2726                                 ir_node *a = get_Quot_left(n);
2727                                 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2728                                 ir_node *mem = get_Quot_mem(n);
2729
2730                                 /* skip a potential Pin */
2731                                 if (is_Pin(mem))
2732                                         mem = get_Pin_op(mem);
2733                                 turn_into_tuple(n, pn_Quot_max);
2734                                 set_Tuple_pred(n, pn_Quot_M, mem);
2735                                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2736                                 set_Tuple_pred(n, pn_Quot_X_except,  new_r_Bad(current_ir_graph));
2737                                 set_Tuple_pred(n, pn_Quot_res, m);
2738                                 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2739                         }
2740                 }
2741         }
2742         return n;
2743 }  /* transform_node_Quot */
2744
2745 /**
2746  * Optimize Abs(x) into  x if x is Confirmed >= 0
2747  * Optimize Abs(x) into -x if x is Confirmed <= 0
2748  */
2749 static ir_node *transform_node_Abs(ir_node *n) {
2750         ir_node *c, *oldn = n;
2751         ir_node *a = get_Abs_op(n);
2752         ir_mode *mode;
2753
2754         HANDLE_UNOP_PHI(tarval_abs, a, c);
2755
2756         switch (classify_value_sign(a)) {
2757         case value_classified_negative:
2758                 mode = get_irn_mode(n);
2759
2760                 /*
2761                  * We can replace the Abs by -x here.
2762                  * We even could add a new Confirm here.
2763                  *
2764                  * Note that -x would create a new node, so we could
2765                  * not run it in the equivalent_node() context.
2766                  */
2767                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2768                                 get_irn_n(n, -1), a, mode);
2769
2770                 DBG_OPT_CONFIRM(oldn, n);
2771                 return n;
2772         case value_classified_positive:
2773                 /* n is positive, Abs is not needed */
2774                 n = a;
2775
2776                 DBG_OPT_CONFIRM(oldn, n);
2777                 return n;
2778         default:
2779                 return n;
2780         }
2781 }  /* transform_node_Abs */
2782
2783 /**
2784  * Transform a Cond node.
2785  *
2786  * Replace the Cond by a Jmp if it branches on a constant
2787  * condition.
2788  */
2789 static ir_node *transform_node_Cond(ir_node *n) {
2790
2791         ir_node *jmp;
2792         ir_node *a = get_Cond_selector(n);
2793         tarval *ta = value_of(a);
2794
2795         /* we need block info which is not available in floating irgs */
2796         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2797                 return n;
2798
2799         if ((ta != tarval_bad) &&
2800             (get_irn_mode(a) == mode_b) &&
2801             (get_opt_unreachable_code())) {
2802                 /* It's a boolean Cond, branching on a boolean constant.
2803                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2804                 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2805                 turn_into_tuple(n, pn_Cond_max);
2806                 if (ta == tarval_b_true) {
2807                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
2808                         set_Tuple_pred(n, pn_Cond_true, jmp);
2809                 } else {
2810                         set_Tuple_pred(n, pn_Cond_false, jmp);
2811                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
2812                 }
2813                 /* We might generate an endless loop, so keep it alive. */
2814                 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2815         }
2816         return n;
2817 }  /* transform_node_Cond */
2818
2819 typedef ir_node* (*recursive_transform) (ir_node *n);
2820
2821 /**
2822  * makes use of distributive laws for and, or, eor
2823  *     and(a OP c, b OP c) -> and(a, b) OP c
2824  * note, might return a different op than n
2825  */
2826 static ir_node *transform_bitwise_distributive(ir_node *n,
2827                                                recursive_transform trans_func)
2828 {
2829         ir_node *oldn    = n;
2830         ir_node *a       = get_binop_left(n);
2831         ir_node *b       = get_binop_right(n);
2832         ir_op   *op      = get_irn_op(a);
2833         ir_op   *op_root = get_irn_op(n);
2834
2835         if(op != get_irn_op(b))
2836                 return n;
2837
2838         if (op == op_Conv) {
2839                 ir_node *a_op   = get_Conv_op(a);
2840                 ir_node *b_op   = get_Conv_op(b);
2841                 ir_mode *a_mode = get_irn_mode(a_op);
2842                 ir_mode *b_mode = get_irn_mode(b_op);
2843                 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2844                         ir_node *blk = get_irn_n(n, -1);
2845
2846                         n = exact_copy(n);
2847                         set_binop_left(n, a_op);
2848                         set_binop_right(n, b_op);
2849                         set_irn_mode(n, a_mode);
2850                         n = trans_func(n);
2851                         n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2852
2853                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2854                         return n;
2855                 }
2856         }
2857
2858         if (op == op_Eor) {
2859                 /* nothing to gain here */
2860                 return n;
2861         }
2862
2863         if (op == op_Shrs || op == op_Shr || op == op_Shl
2864                         || op == op_And || op == op_Or || op == op_Eor) {
2865                 ir_node *a_left  = get_binop_left(a);
2866                 ir_node *a_right = get_binop_right(a);
2867                 ir_node *b_left  = get_binop_left(b);
2868                 ir_node *b_right = get_binop_right(b);
2869                 ir_node *c       = NULL;
2870                 ir_node *op1, *op2;
2871
2872                 if (is_op_commutative(op)) {
2873                         if (a_left == b_left) {
2874                                 c   = a_left;
2875                                 op1 = a_right;
2876                                 op2 = b_right;
2877                         } else if(a_left == b_right) {
2878                                 c   = a_left;
2879                                 op1 = a_right;
2880                                 op2 = b_left;
2881                         } else if(a_right == b_left) {
2882                                 c   = a_right;
2883                                 op1 = a_left;
2884                                 op2 = b_right;
2885                         }
2886                 }
2887                 if(a_right == b_right) {
2888                         c   = a_right;
2889                         op1 = a_left;
2890                         op2 = b_left;
2891                 }
2892
2893                 if (c != NULL) {
2894                         /* (a sop c) & (b sop c) => (a & b) sop c */
2895                         ir_node *blk = get_irn_n(n, -1);
2896
2897                         ir_node *new_n = exact_copy(n);
2898                         set_binop_left(new_n, op1);
2899                         set_binop_right(new_n, op2);
2900                         new_n = trans_func(new_n);
2901
2902                         if(op_root == op_Eor && op == op_Or) {
2903                                 dbg_info  *dbgi = get_irn_dbg_info(n);
2904                                 ir_graph  *irg  = current_ir_graph;
2905                                 ir_mode   *mode = get_irn_mode(c);
2906
2907                                 c = new_rd_Not(dbgi, irg, blk, c, mode);
2908                                 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2909                         } else {
2910                                 n = exact_copy(a);
2911                                 set_irn_n(n, -1, blk);
2912                                 set_binop_left(n, new_n);
2913                                 set_binop_right(n, c);
2914                                 add_identities(current_ir_graph->value_table, n);
2915                         }
2916
2917                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2918                         return n;
2919                 }
2920         }
2921
2922         return n;
2923 }
2924
2925 /**
2926  * Transform an And.
2927  */
2928 static ir_node *transform_node_And(ir_node *n) {
2929         ir_node *c, *oldn = n;
2930         ir_node *a = get_And_left(n);
2931         ir_node *b = get_And_right(n);
2932         ir_mode *mode;
2933
2934         mode = get_irn_mode(n);
2935         HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
2936
2937         /* we can evaluate 2 Projs of the same Cmp */
2938         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
2939                 ir_node *pred_a = get_Proj_pred(a);
2940                 ir_node *pred_b = get_Proj_pred(b);
2941                 if (pred_a == pred_b) {
2942                         dbg_info *dbgi  = get_irn_dbg_info(n);
2943                         ir_node  *block = get_nodes_block(pred_a);
2944                         pn_Cmp pn_a     = get_Proj_proj(a);
2945                         pn_Cmp pn_b     = get_Proj_proj(b);
2946                         /* yes, we can simply calculate with pncs */
2947                         pn_Cmp new_pnc  = pn_a & pn_b;
2948
2949                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
2950                 }
2951         }
2952         if (is_Or(a)) {
2953                 if (is_Not(b)) {
2954                         ir_node *op = get_Not_op(b);
2955                         if (is_And(op)) {
2956                                 ir_node *ba = get_And_left(op);
2957                                 ir_node *bb = get_And_right(op);
2958
2959                                 /* it's enough to test the following cases due to normalization! */
2960                                 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2961                                         /* (a|b) & ~(a&b) = a^b */
2962                                         ir_node *block = get_nodes_block(n);
2963
2964                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
2965                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2966                                         return n;
2967                                 }
2968                         }
2969                 }
2970         }
2971         if (is_Or(b)) {
2972                 if (is_Not(a)) {
2973                         ir_node *op = get_Not_op(a);
2974                         if (is_And(op)) {
2975                                 ir_node *aa = get_And_left(op);
2976                                 ir_node *ab = get_And_right(op);
2977
2978                                 /* it's enough to test the following cases due to normalization! */
2979                                 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2980                                         /* (a|b) & ~(a&b) = a^b */
2981                                         ir_node *block = get_nodes_block(n);
2982
2983                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
2984                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2985                                         return n;
2986                                 }
2987                         }
2988                 }
2989         }
2990         if (is_Eor(a)) {
2991                 ir_node *al = get_Eor_left(a);
2992                 ir_node *ar = get_Eor_right(a);
2993
2994                 if (al == b) {
2995                         /* (b ^ a) & b -> ~a & b */
2996                         dbg_info *dbg  = get_irn_dbg_info(n);
2997                         ir_node *block = get_nodes_block(n);
2998
2999                         ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3000                         n  = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3001                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3002                         return n;
3003                 }
3004                 if (ar == b) {
3005                         /* (a ^ b) & b -> ~a & b */
3006                         dbg_info *dbg  = get_irn_dbg_info(n);
3007                         ir_node *block = get_nodes_block(n);
3008
3009                         al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3010                         n  = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3011                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3012                         return n;
3013                 }
3014         }
3015         if (is_Eor(b)) {
3016                 ir_node *bl = get_Eor_left(b);
3017                 ir_node *br = get_Eor_right(b);
3018
3019                 if (bl == a) {
3020                         /* a & (a ^ b) -> a & ~b */
3021                         dbg_info *dbg  = get_irn_dbg_info(n);
3022                         ir_node *block = get_nodes_block(n);
3023
3024                         br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3025                         n  = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3026                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3027                         return n;
3028                 }
3029                 if (br == a) {
3030                         /* a & (b ^ a) -> a & ~b */
3031                         dbg_info *dbg  = get_irn_dbg_info(n);
3032                         ir_node *block = get_nodes_block(n);
3033
3034                         bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3035                         n  = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3036                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3037                         return n;
3038                 }
3039         }
3040         if (is_Not(a) && is_Not(b)) {
3041                 /* ~a & ~b = ~(a|b) */
3042                 ir_node *block = get_nodes_block(n);
3043                 ir_mode *mode = get_irn_mode(n);
3044
3045                 a = get_Not_op(a);
3046                 b = get_Not_op(b);
3047                 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3048                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3049                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3050                 return n;
3051         }
3052
3053         n = transform_bitwise_distributive(n, transform_node_And);
3054
3055         return n;
3056 }  /* transform_node_And */
3057
3058 /**
3059  * Transform an Eor.
3060  */
3061 static ir_node *transform_node_Eor(ir_node *n) {
3062         ir_node *c, *oldn = n;
3063         ir_node *a = get_Eor_left(n);
3064         ir_node *b = get_Eor_right(n);
3065         ir_mode *mode = get_irn_mode(n);
3066
3067         HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3068
3069         /* we can evaluate 2 Projs of the same Cmp */
3070         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3071                 ir_node *pred_a = get_Proj_pred(a);
3072                 ir_node *pred_b = get_Proj_pred(b);
3073                 if(pred_a == pred_b) {
3074                         dbg_info *dbgi  = get_irn_dbg_info(n);
3075                         ir_node  *block = get_nodes_block(pred_a);
3076                         pn_Cmp pn_a     = get_Proj_proj(a);
3077                         pn_Cmp pn_b     = get_Proj_proj(b);
3078                         /* yes, we can simply calculate with pncs */
3079                         pn_Cmp new_pnc  = pn_a ^ pn_b;
3080
3081                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3082                                            new_pnc);
3083                 }
3084         }
3085
3086         if (a == b) {
3087                 /* a ^ a = 0 */
3088                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3089                                  mode, get_mode_null(mode));
3090                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3091         } else if (mode == mode_b &&
3092                         is_Proj(a) &&
3093                         is_Const(b) && is_Const_one(b) &&
3094                         is_Cmp(get_Proj_pred(a))) {
3095                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3096                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3097                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3098
3099                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3100         } else if (is_Const(b)) {
3101                 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3102                         ir_node  *cnst   = new_Const(mode, tarval_not(get_Const_tarval(b)));
3103                         ir_node  *not_op = get_Not_op(a);
3104                         dbg_info *dbg    = get_irn_dbg_info(n);
3105                         ir_graph *irg    = current_ir_graph;
3106                         ir_node  *block  = get_nodes_block(n);
3107                         ir_mode  *mode   = get_irn_mode(n);
3108                         n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3109                         return n;
3110                 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3111                         n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3112                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3113                 }
3114         } else {
3115                 n = transform_bitwise_distributive(n, transform_node_Eor);
3116         }
3117
3118         return n;
3119 }  /* transform_node_Eor */
3120
3121 /**
3122  * Transform a Not.
3123  */
3124 static ir_node *transform_node_Not(ir_node *n) {
3125         ir_node *c, *oldn = n;
3126         ir_node *a    = get_Not_op(n);
3127         ir_mode *mode = get_irn_mode(n);
3128
3129         HANDLE_UNOP_PHI(tarval_not,a,c);
3130
3131         /* check for a boolean Not */
3132         if (mode == mode_b &&
3133                         is_Proj(a) &&
3134                         is_Cmp(get_Proj_pred(a))) {
3135                 /* We negate a Cmp. The Cmp has the negated result anyways! */
3136                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3137                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3138                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3139                 return n;
3140         }
3141         if  (is_Eor(a)) {
3142                 ir_node *eor_b = get_Eor_right(a);
3143                 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3144                         ir_node  *cnst  = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3145                         ir_node  *eor_a = get_Eor_left(a);
3146                         dbg_info *dbg   = get_irn_dbg_info(n);
3147                         ir_graph *irg   = current_ir_graph;
3148                         ir_node  *block = get_nodes_block(n);
3149                         ir_mode  *mode  = get_irn_mode(n);
3150                         n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3151                         return n;
3152                 }
3153         }
3154         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3155                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3156                         dbg_info *dbg   = get_irn_dbg_info(n);
3157                         ir_graph *irg   = current_ir_graph;
3158                         ir_node  *block = get_nodes_block(n);
3159                         ir_node  *add_l = get_Minus_op(a);
3160                         ir_node  *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3161                         n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3162                 } else if (is_Add(a)) {
3163                         ir_node *add_r = get_Add_right(a);
3164                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3165                                 /* ~(x + -1) = -x */
3166                                 ir_node *op = get_Add_left(a);
3167                                 ir_node *blk = get_irn_n(n, -1);
3168                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3169                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3170                         }
3171                 }
3172         }
3173         return n;
3174 }  /* transform_node_Not */
3175
3176 /**
3177  * Transform a Minus.
3178  * Optimize:
3179  *   -(~x) = x + 1
3180  *   -(a-b) = b - a
3181  *   -(a >>u (size-1)) = a >>s (size-1)
3182  *   -(a >>s (size-1)) = a >>u (size-1)
3183  *   -(a * const) -> a * -const
3184  */
3185 static ir_node *transform_node_Minus(ir_node *n) {
3186         ir_node *c, *oldn = n;
3187         ir_node *a = get_Minus_op(n);
3188         ir_mode *mode;
3189
3190         HANDLE_UNOP_PHI(tarval_neg,a,c);
3191
3192         mode = get_irn_mode(a);
3193         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3194                 /* the following rules are only to twos-complement */
3195                 if (is_Not(a)) {
3196                         /* -(~x) = x + 1 */
3197                         ir_node *op   = get_Not_op(a);
3198                         tarval *tv    = get_mode_one(mode);
3199                         ir_node *blk  = get_irn_n(n, -1);
3200                         ir_node *c    = new_r_Const(current_ir_graph, blk, mode, tv);
3201                         n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3202                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3203                         return n;
3204                 }
3205                 if (is_Shr(a)) {
3206                         ir_node *c = get_Shr_right(a);
3207
3208                         if (is_Const(c)) {
3209                                 tarval *tv = get_Const_tarval(c);
3210
3211                                 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3212                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3213                                         ir_node *v = get_Shr_left(a);
3214
3215                                         n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3216                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3217                                         return n;
3218                                 }
3219                         }
3220                 }
3221                 if (is_Shrs(a)) {
3222                         ir_node *c = get_Shrs_right(a);
3223
3224                         if (is_Const(c)) {
3225                                 tarval *tv = get_Const_tarval(c);
3226
3227                                 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3228                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3229                                         ir_node *v = get_Shrs_left(a);
3230
3231                                         n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3232                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3233                                         return n;
3234                                 }
3235                         }
3236                 }
3237         }
3238         if (is_Sub(a)) {
3239                 /* - (a-b) = b - a */
3240                 ir_node *la  = get_Sub_left(a);
3241                 ir_node *ra  = get_Sub_right(a);
3242                 ir_node *blk = get_irn_n(n, -1);
3243
3244                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3245                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3246                 return n;
3247         }
3248
3249         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3250                 ir_node *mul_l = get_Mul_left(a);
3251                 ir_node *mul_r = get_Mul_right(a);
3252                 if (is_Const(mul_r)) {
3253                         tarval   *tv    = tarval_neg(get_Const_tarval(mul_r));
3254                         ir_node  *cnst  = new_Const(mode, tv);
3255                         dbg_info *dbg   = get_irn_dbg_info(a);
3256                         ir_graph *irg   = current_ir_graph;
3257                         ir_node  *block = get_nodes_block(a);
3258                         n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3259                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3260                         return n;
3261                 }
3262         }
3263
3264         return n;
3265 }  /* transform_node_Minus */
3266
3267 /**
3268  * Transform a Cast_type(Const) into a new Const_type
3269  */
3270 static ir_node *transform_node_Cast(ir_node *n) {
3271         ir_node *oldn = n;
3272         ir_node *pred = get_Cast_op(n);
3273         ir_type *tp = get_irn_type(n);
3274
3275         if (is_Const(pred) && get_Const_type(pred) != tp) {
3276                 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3277                         get_Const_tarval(pred), tp);
3278                 DBG_OPT_CSTEVAL(oldn, n);
3279         } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3280                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3281                         get_SymConst_kind(pred), tp);
3282                 DBG_OPT_CSTEVAL(oldn, n);
3283         }
3284
3285         return n;
3286 }  /* transform_node_Cast */
3287
3288 /**
3289  * Transform a Proj(Div) with a non-zero value.
3290  * Removes the exceptions and routes the memory to the NoMem node.
3291  */
3292 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3293         ir_node *div = get_Proj_pred(proj);
3294         ir_node *b   = get_Div_right(div);
3295         ir_node *confirm, *res, *new_mem;
3296         long proj_nr;
3297
3298         if (value_not_zero(b, &confirm)) {
3299                 /* div(x, y) && y != 0 */
3300                 if (confirm == NULL) {
3301                         /* we are sure we have a Const != 0 */
3302                         new_mem = get_Div_mem(div);
3303                         if (is_Pin(new_mem))
3304                                 new_mem = get_Pin_op(new_mem);
3305                         set_Div_mem(div, new_mem);
3306                         set_irn_pinned(div, op_pin_state_floats);
3307                 }
3308
3309                 proj_nr = get_Proj_proj(proj);
3310                 switch (proj_nr) {
3311                 case pn_Div_X_regular:
3312                         return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3313
3314                 case pn_Div_X_except:
3315                         /* we found an exception handler, remove it */
3316                         DBG_OPT_EXC_REM(proj);
3317                         return new_Bad();
3318
3319                 case pn_Div_M:
3320                         res = get_Div_mem(div);
3321                         new_mem = get_irg_no_mem(current_ir_graph);
3322
3323                         if (confirm) {
3324                                 /* This node can only float up to the Confirm block */
3325                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3326                         }
3327                         set_irn_pinned(div, op_pin_state_floats);
3328                         /* this is a Div without exception, we can remove the memory edge */
3329                         set_Div_mem(div, new_mem);
3330                         return res;
3331                 }
3332         }
3333         return proj;
3334 }  /* transform_node_Proj_Div */
3335
3336 /**
3337  * Transform a Proj(Mod) with a non-zero value.
3338  * Removes the exceptions and routes the memory to the NoMem node.
3339  */
3340 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3341         ir_node *mod = get_Proj_pred(proj);
3342         ir_node *b   = get_Mod_right(mod);
3343         ir_node *confirm, *res, *new_mem;
3344         long proj_nr;
3345
3346         if (value_not_zero(b, &confirm)) {
3347                 /* mod(x, y) && y != 0 */
3348                 proj_nr = get_Proj_proj(proj);
3349
3350                 if (confirm == NULL) {
3351                         /* we are sure we have a Const != 0 */
3352                         new_mem = get_Mod_mem(mod);
3353                         if (is_Pin(new_mem))
3354                                 new_mem = get_Pin_op(new_mem);
3355                         set_Mod_mem(mod, new_mem);
3356                         set_irn_pinned(mod, op_pin_state_floats);
3357                 }
3358
3359                 switch (proj_nr) {
3360
3361                 case pn_Mod_X_regular:
3362                         return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3363
3364                 case pn_Mod_X_except:
3365                         /* we found an exception handler, remove it */
3366                         DBG_OPT_EXC_REM(proj);
3367                         return new_Bad();
3368
3369                 case pn_Mod_M:
3370                         res = get_Mod_mem(mod);
3371                         new_mem = get_irg_no_mem(current_ir_graph);
3372
3373                         if (confirm) {
3374                                 /* This node can only float up to the Confirm block */
3375                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3376                         }
3377                         /* this is a Mod without exception, we can remove the memory edge */
3378                         set_Mod_mem(mod, new_mem);
3379                         return res;
3380                 case pn_Mod_res:
3381                         if (get_Mod_left(mod) == b) {
3382                                 /* a % a = 0 if a != 0 */
3383                                 ir_mode *mode = get_irn_mode(proj);
3384                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3385
3386                                 DBG_OPT_CSTEVAL(mod, res);
3387                                 return res;
3388                         }
3389                 }
3390         }
3391         return proj;
3392 }  /* transform_node_Proj_Mod */
3393
3394 /**
3395  * Transform a Proj(DivMod) with a non-zero value.
3396  * Removes the exceptions and routes the memory to the NoMem node.
3397  */
3398 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3399         ir_node *divmod = get_Proj_pred(proj);
3400         ir_node *b      = get_DivMod_right(divmod);
3401         ir_node *confirm, *res, *new_mem;
3402         long proj_nr;
3403
3404         if (value_not_zero(b, &confirm)) {
3405                 /* DivMod(x, y) && y != 0 */
3406                 proj_nr = get_Proj_proj(proj);
3407
3408                 if (confirm == NULL) {
3409                         /* we are sure we have a Const != 0 */
3410                         new_mem = get_DivMod_mem(divmod);
3411                         if (is_Pin(new_mem))
3412                                 new_mem = get_Pin_op(new_mem);
3413                         set_DivMod_mem(divmod, new_mem);
3414                         set_irn_pinned(divmod, op_pin_state_floats);
3415                 }
3416
3417                 switch (proj_nr) {
3418
3419                 case pn_DivMod_X_regular:
3420                         return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3421
3422                 case pn_DivMod_X_except:
3423                         /* we found an exception handler, remove it */
3424                         DBG_OPT_EXC_REM(proj);
3425                         return new_Bad();
3426
3427                 case pn_DivMod_M:
3428                         res = get_DivMod_mem(divmod);
3429                         new_mem = get_irg_no_mem(current_ir_graph);
3430
3431                         if (confirm) {
3432                                 /* This node can only float up to the Confirm block */
3433                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3434                         }
3435                         /* this is a DivMod without exception, we can remove the memory edge */
3436                         set_DivMod_mem(divmod, new_mem);
3437                         return res;
3438
3439                 case pn_DivMod_res_mod:
3440                         if (get_DivMod_left(divmod) == b) {
3441                                 /* a % a = 0 if a != 0 */
3442                                 ir_mode *mode = get_irn_mode(proj);
3443                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3444
3445                                 DBG_OPT_CSTEVAL(divmod, res);
3446                                 return res;
3447                         }
3448                 }
3449         }
3450         return proj;
3451 }  /* transform_node_Proj_DivMod */
3452
3453 /**
3454  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3455  */
3456 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3457         if (get_opt_unreachable_code()) {
3458                 ir_node *n = get_Proj_pred(proj);
3459                 ir_node *b = get_Cond_selector(n);
3460
3461                 if (mode_is_int(get_irn_mode(b))) {
3462                         tarval *tb = value_of(b);
3463
3464                         if (tb != tarval_bad) {
3465                                 /* we have a constant switch */
3466                                 long num = get_Proj_proj(proj);
3467
3468                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3469                                         if (get_tarval_long(tb) == num) {
3470                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
3471                                                  * in a block. This case is optimized away in optimize_cf(). */
3472                                                 return proj;
3473                                         } else {
3474                                                 /* this case will NEVER be taken, kill it */
3475                                                 return new_Bad();
3476                                         }
3477                                 }
3478                         }
3479                 }
3480         }
3481         return proj;
3482 }  /* transform_node_Proj_Cond */
3483
3484 /**
3485  * Normalizes and optimizes Cmp nodes.
3486  */
3487 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3488         ir_node *n     = get_Proj_pred(proj);
3489         ir_node *left  = get_Cmp_left(n);
3490         ir_node *right = get_Cmp_right(n);
3491         ir_node *c     = NULL;
3492         tarval *tv     = NULL;
3493         int changed    = 0;
3494         ir_mode *mode  = NULL;
3495         long proj_nr   = get_Proj_proj(proj);
3496
3497         /* we can evaluate this direct */
3498         switch (proj_nr) {
3499         case pn_Cmp_False:
3500                 return new_Const(mode_b, get_tarval_b_false());
3501         case pn_Cmp_True:
3502                 return new_Const(mode_b, get_tarval_b_true());
3503         case pn_Cmp_Leg:
3504                 if(!mode_is_float(get_irn_mode(left)))
3505                         return new_Const(mode_b, get_tarval_b_true());
3506                 break;
3507         default:
3508                 break;
3509         }
3510
3511         /* remove Casts */
3512         if (is_Cast(left))
3513                 left = get_Cast_op(left);
3514         if (is_Cast(right))
3515                 right = get_Cast_op(right);
3516
3517         /* Remove unnecessary conversions */
3518         /* TODO handle constants */
3519         if (is_Conv(left) && is_Conv(right)) {
3520                 ir_mode *mode        = get_irn_mode(left);
3521                 ir_node *op_left     = get_Conv_op(left);
3522                 ir_node *op_right    = get_Conv_op(right);
3523                 ir_mode *mode_left   = get_irn_mode(op_left);
3524                 ir_mode *mode_right  = get_irn_mode(op_right);
3525
3526                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3527                         ir_graph *irg   = current_ir_graph;
3528                         ir_node  *block = get_nodes_block(n);
3529
3530                         if (mode_left == mode_right) {
3531                                 left  = op_left;
3532                                 right = op_right;
3533                                 changed |= 1;
3534                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3535                         } else if (smaller_mode(mode_left, mode_right)) {
3536                                 left  = new_r_Conv(irg, block, op_left, mode_right);
3537                                 right = op_right;
3538                                 changed |= 1;
3539                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3540                         } else if (smaller_mode(mode_right, mode_left)) {
3541                                 left  = op_left;
3542                                 right = new_r_Conv(irg, block, op_right, mode_left);
3543                                 changed |= 1;
3544                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3545                         }
3546                 }
3547         }
3548
3549         /* remove operation of both sides if possible */
3550         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3551                 ir_opcode lop = get_irn_opcode(left);
3552
3553                 if (lop == get_irn_opcode(right)) {
3554                         ir_node *ll, *lr, *rl, *rr;
3555
3556                         /* same operation on both sides, try to remove */
3557                         switch (lop) {
3558                         case iro_Not:
3559                         case iro_Minus:
3560                                 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3561                                 left  = get_unop_op(left);
3562                                 right = get_unop_op(right);
3563                                 changed |= 1;
3564                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3565                                 break;
3566                         case iro_Add:
3567                                 ll = get_Add_left(left);
3568                                 lr = get_Add_right(left);
3569                                 rl = get_Add_left(right);
3570                                 rr = get_Add_right(right);
3571
3572                                 if (ll == rl) {
3573                                         /* X + a CMP X + b ==> a CMP b */
3574                                         left  = lr;
3575                                         right = rr;
3576                                         changed |= 1;
3577                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3578                                 } else if (ll == rr) {
3579                                         /* X + a CMP b + X ==> a CMP b */
3580                                         left  = lr;
3581                                         right = rl;
3582                                         changed |= 1;
3583                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3584                                 } else if (lr == rl) {
3585                                         /* a + X CMP X + b ==> a CMP b */
3586                                         left  = ll;
3587                                         right = rr;
3588                                         changed |= 1;
3589                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3590                                 } else if (lr == rr) {
3591                                         /* a + X CMP b + X ==> a CMP b */
3592                                         left  = ll;
3593                                         right = rl;
3594                                         changed |= 1;
3595                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3596                                 }
3597                                 break;
3598                         case iro_Sub:
3599                                 ll = get_Sub_left(left);
3600                                 lr = get_Sub_right(left);
3601                                 rl = get_Sub_left(right);
3602                                 rr = get_Sub_right(right);
3603
3604                                 if (ll == rl) {
3605                                         /* X - a CMP X - b ==> a CMP b */
3606                                         left  = lr;
3607                                         right = rr;
3608                                         changed |= 1;
3609                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3610                                 } else if (lr == rr) {
3611                                         /* a - X CMP b - X ==> a CMP b */
3612                                         left  = ll;
3613                                         right = rl;
3614                                         changed |= 1;
3615                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3616                                 }
3617                                 break;
3618                         case iro_Rot:
3619                                 if (get_Rot_right(left) == get_Rot_right(right)) {
3620                                         /* a ROT X CMP b ROT X */
3621                                         left  = get_Rot_left(left);
3622                                         right = get_Rot_left(right);
3623                                         changed |= 1;
3624                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3625                                 }
3626                                 break;
3627                         default:
3628                                 break;
3629                         }
3630                 }
3631         }
3632
3633         if (get_irn_mode(left) == mode_b) {
3634                 ir_graph *irg   = current_ir_graph;
3635                 ir_node  *block = get_nodes_block(n);
3636                 ir_node  *bres;
3637
3638                 switch (proj_nr) {
3639                         case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3640                         case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3641                         case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3642                         case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3643                         case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3644                         case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3645                         default: bres = NULL;
3646                 }
3647                 if (bres) {
3648                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3649                         return bres;
3650                 }
3651         }
3652
3653         if (!get_opt_reassociation())
3654                 return proj;
3655
3656         /*
3657          * First step: normalize the compare op
3658          * by placing the constant on the right side
3659          * or moving the lower address node to the left.
3660          * We ignore the case that both are constants
3661          * this case should be optimized away.
3662          */
3663         if (is_Const(right)) {
3664                 c = right;
3665         } else if (is_irn_constlike(left)) {
3666                 c     = left;
3667                 left  = right;
3668                 right = c;
3669                 if(!is_Const(c))
3670                         c = NULL;
3671
3672                 proj_nr = get_inversed_pnc(proj_nr);
3673                 changed |= 1;
3674         } else if (get_irn_idx(left) > get_irn_idx(right)) {
3675                 ir_node *t = left;
3676
3677                 left  = right;
3678                 right = t;
3679
3680                 proj_nr = get_inversed_pnc(proj_nr);
3681                 changed |= 1;
3682         }
3683
3684         /*
3685          * Second step: Try to reduce the magnitude
3686          * of a constant. This may help to generate better code
3687          * later and may help to normalize more compares.
3688          * Of course this is only possible for integer values.
3689          */
3690         if (c) {
3691                 mode = get_irn_mode(c);
3692                 tv = get_Const_tarval(c);
3693
3694                 /* TODO extend to arbitrary constants */
3695                 if (is_Conv(left) && tarval_is_null(tv)) {
3696                         ir_node *op      = get_Conv_op(left);
3697                         ir_mode *op_mode = get_irn_mode(op);
3698
3699                         /*
3700                          * UpConv(x) REL 0  ==> x REL 0
3701                          */
3702                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3703                             ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3704                                  mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3705                                 tv   = get_mode_null(op_mode);
3706                                 left = op;
3707                                 mode = op_mode;
3708                                 changed |= 2;
3709                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3710                         }
3711                 }
3712
3713                 if (tv != tarval_bad) {
3714                         /* the following optimization is possible on modes without Overflow
3715                          * on Unary Minus or on == and !=:
3716                          * -a CMP c  ==>  a swap(CMP) -c
3717                          *
3718                          * Beware: for two-complement Overflow may occur, so only == and != can
3719                          * be optimized, see this:
3720                          * -MININT < 0 =/=> MININT > 0 !!!
3721                          */
3722                         if (is_Minus(left) &&
3723                                 (!mode_overflow_on_unary_Minus(mode) ||
3724                                 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3725                                 tv = tarval_neg(tv);
3726
3727                                 if (tv != tarval_bad) {
3728                                         left = get_Minus_op(left);
3729                                         proj_nr = get_inversed_pnc(proj_nr);
3730                                         changed |= 2;
3731                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3732                                 }
3733                         } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3734                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
3735                                 tv = tarval_not(tv);
3736
3737                                 if (tv != tarval_bad) {
3738                                         left = get_Not_op(left);
3739                                         changed |= 2;
3740                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3741                                 }
3742                         }
3743
3744                         /* for integer modes, we have more */
3745                         if (mode_is_int(mode)) {
3746                                 /* Ne includes Unordered which is not possible on integers.
3747                                  * However, frontends often use this wrong, so fix it here */
3748                                 if (proj_nr & pn_Cmp_Uo) {
3749                                         proj_nr &= ~pn_Cmp_Uo;
3750                                         set_Proj_proj(proj, proj_nr);
3751                                 }
3752
3753                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
3754                                 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3755                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3756                                         tv = tarval_sub(tv, get_mode_one(mode));
3757
3758                                         if (tv != tarval_bad) {
3759                                                 proj_nr ^= pn_Cmp_Eq;
3760                                                 changed |= 2;
3761                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3762                                         }
3763                                 }
3764                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
3765                                 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3766                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3767                                         tv = tarval_add(tv, get_mode_one(mode));
3768
3769                                         if (tv != tarval_bad) {
3770                                                 proj_nr ^= pn_Cmp_Eq;
3771                                                 changed |= 2;
3772                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3773                                         }
3774                                 }
3775
3776                                 /* the following reassociations work only for == and != */
3777                                 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3778
3779 #if 0 /* Might be not that good in general */
3780                                         /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
3781                                         if (tarval_is_null(tv) && is_Sub(left)) {
3782                                                 right = get_Sub_right(left);
3783                                                 left  = get_Sub_left(left);
3784
3785                                                 tv = value_of(right);
3786                                                 changed = 1;
3787                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3788                                         }
3789 #endif
3790
3791                                         if (tv != tarval_bad) {
3792                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
3793                                                 if (is_Sub(left)) {
3794                                                         ir_node *c1 = get_Sub_right(left);
3795                                                         tarval *tv2 = value_of(c1);
3796
3797                                                         if (tv2 != tarval_bad) {
3798                                                                 tv2 = tarval_add(tv, value_of(c1));
3799
3800                                                                 if (tv2 != tarval_bad) {
3801                                                                         left    = get_Sub_left(left);
3802                                                                         tv      = tv2;
3803                                                                         changed |= 2;
3804                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3805                                                                 }
3806                                                         }
3807                                                 }
3808                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
3809                                                 else if (is_Add(left)) {
3810                                                         ir_node *a_l = get_Add_left(left);
3811                                                         ir_node *a_r = get_Add_right(left);
3812                                                         ir_node *a;
3813                                                         tarval *tv2;
3814
3815                                                         if (is_Const(a_l)) {
3816                                                                 a = a_r;
3817                                                                 tv2 = value_of(a_l);
3818                                                         } else {
3819                                                                 a = a_l;
3820                                                                 tv2 = value_of(a_r);
3821                                                         }
3822
3823                                                         if (tv2 != tarval_bad) {
3824                                                                 tv2 = tarval_sub(tv, tv2);
3825
3826                                                                 if (tv2 != tarval_bad) {
3827                                                                         left    = a;
3828                                                                         tv      = tv2;
3829                                                                         changed |= 2;
3830                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3831                                                                 }
3832                                                         }
3833                                                 }
3834                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
3835                                                 else if (is_Minus(left)) {
3836                                                         tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3837
3838                                                         if (tv2 != tarval_bad) {
3839                                                                 left    = get_Minus_op(left);
3840                                                                 tv      = tv2;
3841                                                                 changed |= 2;
3842                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3843                                                         }
3844                                                 }
3845                                         }
3846                                 } /* == or != */
3847                                 /* the following reassociations work only for <= */
3848                                 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3849                                         if (tv != tarval_bad) {
3850                                                 /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
3851                                                 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
3852                                                 }
3853                                         }
3854                                 }
3855                         } /* mode_is_int */
3856
3857                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3858                                 switch (get_irn_opcode(left)) {
3859                                         ir_node *c1;
3860
3861                                 case iro_And:
3862                                         c1 = get_And_right(left);
3863                                         if (is_Const(c1)) {
3864                                                 /*
3865                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
3866                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
3867                                                  */
3868                                                 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
3869                                                 if (mask != tv) {
3870                                                         /* TODO: move to constant evaluation */
3871                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3872                                                         c1 = new_Const(mode_b, tv);
3873                                                         DBG_OPT_CSTEVAL(proj, c1);
3874                                                         return c1;
3875                                                 }
3876
3877                                                 if (tarval_is_single_bit(tv)) {
3878                                                         /*
3879                                                          * optimization for AND:
3880                                                          * Optimize:
3881                                                          *   And(x, C) == C  ==>  And(x, C) != 0
3882                                                          *   And(x, C) != C  ==>  And(X, C) == 0
3883                                                          *
3884                                                          * if C is a single Bit constant.
3885                                                          */
3886
3887                                                         /* check for Constant's match. We have check hare the tarvals,
3888                                                            because our const might be changed */
3889                                                         if (get_Const_tarval(c1) == tv) {
3890                                                                 /* fine: do the transformation */
3891                                                                 tv = get_mode_null(get_tarval_mode(tv));
3892                                                                 proj_nr ^= pn_Cmp_Leg;
3893                                                                 changed |= 2;
3894                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3895                                                         }
3896                                                 }
3897                                         }
3898                                         break;
3899                                 case iro_Or:
3900                                         c1 = get_Or_right(left);
3901                                         if (is_Const(c1) && tarval_is_null(tv)) {
3902                                                 /*
3903                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
3904                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
3905                                                  */
3906                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
3907                                                         /* TODO: move to constant evaluation */
3908                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3909                                                         c1 = new_Const(mode_b, tv);
3910                                                         DBG_OPT_CSTEVAL(proj, c1);
3911                                                         return c1;
3912                                                 }
3913                                         }
3914                                         break;
3915                                 case iro_Shl:
3916                                         /*
3917                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
3918                                          *                             FALSE                       else
3919                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
3920                                          *                             TRUE                        else
3921                                          */
3922                                         c1 = get_Shl_right(left);
3923                                         if (is_Const(c1)) {
3924                                                 tarval  *tv1    = get_Const_tarval(c1);
3925                                                 ir_mode *mode   = get_irn_mode(left);
3926                                                 tarval  *minus1 = get_mode_all_one(mode);
3927                                                 tarval  *amask  = tarval_shr(minus1, tv1);
3928                                                 tarval  *cmask  = tarval_shl(minus1, tv1);
3929                                                 ir_node *sl, *blk;
3930
3931                                                 if (tarval_and(tv, cmask) != tv) {
3932                                                         /* condition not met */
3933                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3934                                                         c1 = new_Const(mode_b, tv);
3935                                                         DBG_OPT_CSTEVAL(proj, c1);
3936                                                         return c1;
3937                                                 }
3938                                                 sl   = get_Shl_left(left);
3939                                                 blk  = get_nodes_block(n);
3940                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3941                                                 tv   = tarval_shr(tv, tv1);
3942                                                 changed |= 2;
3943                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
3944                                         }
3945                                         break;
3946                                 case iro_Shr:
3947                                         /*
3948                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
3949                                          *                             FALSE                       else
3950                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
3951                                          *                             TRUE                        else
3952                                          */
3953                                         c1 = get_Shr_right(left);
3954                                         if (is_Const(c1)) {
3955                                                 tarval  *tv1    = get_Const_tarval(c1);
3956                                                 ir_mode *mode   = get_irn_mode(left);
3957                                                 tarval  *minus1 = get_mode_all_one(mode);
3958                                                 tarval  *amask  = tarval_shl(minus1, tv1);
3959                                                 tarval  *cmask  = tarval_shr(minus1, tv1);
3960                                                 ir_node *sl, *blk;
3961
3962                                                 if (tarval_and(tv, cmask) != tv) {
3963                                                         /* condition not met */
3964                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3965                                                         c1 = new_Const(mode_b, tv);
3966                                                         DBG_OPT_CSTEVAL(proj, c1);
3967                                                         return c1;
3968                                                 }
3969                                                 sl   = get_Shr_left(left);
3970                                                 blk  = get_nodes_block(n);
3971                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3972                                                 tv   = tarval_shl(tv, tv1);
3973                                                 changed |= 2;
3974                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
3975                                         }
3976                                         break;
3977                                 case iro_Shrs:
3978                                         /*
3979                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
3980                                          *                             FALSE                       else
3981                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
3982                                          *                             TRUE                        else
3983                                          */
3984                                         c1 = get_Shrs_right(left);
3985                                         if (is_Const(c1)) {
3986                                                 tarval  *tv1    = get_Const_tarval(c1);
3987                                                 ir_mode *mode   = get_irn_mode(left);
3988                                                 tarval  *minus1 = get_mode_all_one(mode);
3989                                                 tarval  *amask  = tarval_shl(minus1, tv1);
3990                                                 tarval  *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
3991                                                 ir_node *sl, *blk;
3992
3993                                                 cond = tarval_sub(cond, tv1);
3994                                                 cond = tarval_shrs(tv, cond);
3995
3996                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
3997                                                         /* condition not met */
3998                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3999                                                         c1 = new_Const(mode_b, tv);
4000                                                         DBG_OPT_CSTEVAL(proj, c1);
4001                                                         return c1;
4002                                                 }
4003                                                 sl   = get_Shrs_left(left);
4004                                                 blk  = get_nodes_block(n);
4005                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4006                                                 tv   = tarval_shl(tv, tv1);
4007                                                 changed |= 2;
4008                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4009                                         }
4010                                         break;
4011                                 }  /* switch */
4012                         }
4013                 } /* tarval != bad */
4014         }
4015
4016         if (changed & 2)      /* need a new Const */
4017                 right = new_Const(mode, tv);
4018
4019         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4020                 ir_node *op = get_Proj_pred(left);
4021
4022                 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4023                     (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4024                         ir_node *c = get_binop_right(op);
4025
4026                         if (is_Const(c)) {
4027                                 tarval *tv = get_Const_tarval(c);
4028
4029                                 if (tarval_is_single_bit(tv)) {
4030                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4031                                         ir_node *v    = get_binop_left(op);
4032                                         ir_node *blk  = get_irn_n(op, -1);
4033                                         ir_mode *mode = get_irn_mode(v);
4034
4035                                         tv = tarval_sub(tv, get_mode_one(mode));
4036                                         left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4037                                         changed |= 1;
4038                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4039                                 }
4040                         }
4041                 }
4042         }
4043
4044         if (changed) {
4045                 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4046
4047                 /* create a new compare */
4048                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4049
4050                 set_Proj_pred(proj, n);
4051                 set_Proj_proj(proj, proj_nr);
4052         }
4053
4054         return proj;
4055 }  /* transform_node_Proj_Cmp */
4056
4057 /**
4058  * Does all optimizations on nodes that must be done on it's Proj's
4059  * because of creating new nodes.
4060  */
4061 static ir_node *transform_node_Proj(ir_node *proj) {
4062         ir_node *n = get_Proj_pred(proj);
4063
4064         switch (get_irn_opcode(n)) {
4065         case iro_Div:
4066                 return transform_node_Proj_Div(proj);
4067
4068         case iro_Mod:
4069                 return transform_node_Proj_Mod(proj);
4070
4071         case iro_DivMod:
4072                 return transform_node_Proj_DivMod(proj);
4073
4074         case iro_Cond:
4075                 return transform_node_Proj_Cond(proj);
4076
4077         case iro_Cmp:
4078                 return transform_node_Proj_Cmp(proj);
4079
4080         case iro_Tuple:
4081                 /* should not happen, but if it does will be optimized away */
4082                 return equivalent_node_Proj(proj);
4083
4084         default:
4085                 /* do nothing */
4086                 return proj;
4087         }
4088 }  /* transform_node_Proj */
4089
4090 /**
4091  * Move Confirms down through Phi nodes.
4092  */
4093 static ir_node *transform_node_Phi(ir_node *phi) {
4094         int i, n;
4095         ir_mode *mode = get_irn_mode(phi);
4096
4097         if (mode_is_reference(mode)) {
4098                 n = get_irn_arity(phi);
4099
4100                 /* Beware of Phi0 */
4101                 if (n > 0) {
4102                         ir_node *pred = get_irn_n(phi, 0);
4103                         ir_node *bound, *new_Phi, *block, **in;
4104                         pn_Cmp  pnc;
4105
4106                         if (! is_Confirm(pred))
4107                                 return phi;
4108
4109                         bound = get_Confirm_bound(pred);
4110                         pnc   = get_Confirm_cmp(pred);
4111
4112                         NEW_ARR_A(ir_node *, in, n);
4113                         in[0] = get_Confirm_value(pred);
4114
4115                         for (i = 1; i < n; ++i) {
4116                                 pred = get_irn_n(phi, i);
4117
4118                                 if (! is_Confirm(pred) ||
4119                                         get_Confirm_bound(pred) != bound ||
4120                                         get_Confirm_cmp(pred) != pnc)
4121                                         return phi;
4122                                 in[i] = get_Confirm_value(pred);
4123                         }
4124                         /* move the Confirm nodes "behind" the Phi */
4125                         block = get_irn_n(phi, -1);
4126                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4127                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4128                 }
4129         }
4130         return phi;
4131 }  /* transform_node_Phi */
4132
4133 /**
4134  * Returns the operands of a commutative bin-op, if one operand is
4135  * a const, it is returned as the second one.
4136  */
4137 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4138         ir_node *op_a = get_binop_left(binop);
4139         ir_node *op_b = get_binop_right(binop);
4140
4141         assert(is_op_commutative(get_irn_op(binop)));
4142
4143         if (is_Const(op_a)) {
4144                 *a = op_b;
4145                 *c = op_a;
4146         } else {
4147                 *a = op_a;
4148                 *c = op_b;
4149         }
4150 }  /* get_comm_Binop_Ops */
4151
4152 /**
4153  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4154  * Such pattern may arise in bitfield stores.
4155  *
4156  * value  c4                  value      c4 & c2
4157  *    AND     c3                    AND           c1 | c3
4158  *        OR     c2      ===>               OR
4159  *           AND    c1
4160  *               OR
4161  *
4162  *
4163  * value  c2                 value  c1
4164  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
4165  *        OR
4166  */
4167 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4168         ir_node *and, *c1;
4169         ir_node *or_l, *c2;
4170         ir_node *and_l, *c3;
4171         ir_node *value, *c4;
4172         ir_node *new_and, *new_const, *block;
4173         ir_mode *mode = get_irn_mode(or);
4174
4175         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4176
4177         while (1) {
4178                 get_comm_Binop_Ops(or, &and, &c1);
4179                 if (!is_Const(c1) || !is_And(and))
4180                         return or;
4181
4182                 get_comm_Binop_Ops(and, &or_l, &c2);
4183                 if (!is_Const(c2))
4184                         return or;
4185
4186                 tv1 = get_Const_tarval(c1);
4187                 tv2 = get_Const_tarval(c2);
4188
4189                 tv = tarval_or(tv1, tv2);
4190                 if (tarval_is_all_one(tv)) {
4191                         /* the AND does NOT clear a bit with isn't set by the OR */
4192                         set_Or_left(or, or_l);
4193                         set_Or_right(or, c1);
4194
4195                         /* check for more */
4196                         continue;
4197                 }
4198
4199                 if (!is_Or(or_l))
4200                         return or;
4201
4202                 get_comm_Binop_Ops(or_l, &and_l, &c3);
4203                 if (!is_Const(c3) || !is_And(and_l))
4204                         return or;
4205
4206                 get_comm_Binop_Ops(and_l, &value, &c4);
4207                 if (!is_Const(c4))
4208                         return or;
4209
4210                 /* ok, found the pattern, check for conditions */
4211                 assert(mode == get_irn_mode(and));
4212                 assert(mode == get_irn_mode(or_l));
4213                 assert(mode == get_irn_mode(and_l));
4214
4215                 tv3 = get_Const_tarval(c3);
4216                 tv4 = get_Const_tarval(c4);
4217
4218                 tv = tarval_or(tv4, tv2);
4219                 if (!tarval_is_all_one(tv)) {
4220                         /* have at least one 0 at the same bit position */
4221                         return or;
4222                 }
4223
4224                 n_tv4 = tarval_not(tv4);
4225                 if (tv3 != tarval_and(tv3, n_tv4)) {
4226                         /* bit in the or_mask is outside the and_mask */
4227                         return or;
4228                 }
4229
4230                 n_tv2 = tarval_not(tv2);
4231                 if (tv1 != tarval_and(tv1, n_tv2)) {
4232                         /* bit in the or_mask is outside the and_mask */
4233                         return or;
4234                 }
4235
4236                 /* ok, all conditions met */
4237                 block = get_irn_n(or, -1);
4238
4239                 new_and = new_r_And(current_ir_graph, block,
4240                         value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4241
4242                 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4243
4244                 set_Or_left(or, new_and);
4245                 set_Or_right(or, new_const);
4246
4247                 /* check for more */
4248         }
4249 }  /* transform_node_Or_bf_store */
4250
4251 /**
4252  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4253  */
4254 static ir_node *transform_node_Or_Rot(ir_node *or) {
4255         ir_mode *mode = get_irn_mode(or);
4256         ir_node *shl, *shr, *block;
4257         ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4258         tarval *tv1, *tv2;
4259
4260         if (! mode_is_int(mode))
4261                 return or;
4262
4263         shl = get_binop_left(or);
4264         shr = get_binop_right(or);
4265
4266         if (is_Shr(shl)) {
4267                 if (!is_Shl(shr))
4268                         return or;
4269
4270                 irn = shl;
4271                 shl = shr;
4272                 shr = irn;
4273         } else if (!is_Shl(shl)) {
4274                 return or;
4275         } else if (!is_Shr(shr)) {
4276                 return or;
4277         }
4278         x = get_Shl_left(shl);
4279         if (x != get_Shr_left(shr))
4280                 return or;
4281
4282         c1 = get_Shl_right(shl);
4283         c2 = get_Shr_right(shr);
4284         if (is_Const(c1) && is_Const(c2)) {
4285                 tv1 = get_Const_tarval(c1);
4286                 if (! tarval_is_long(tv1))
4287                         return or;
4288
4289                 tv2 = get_Const_tarval(c2);
4290                 if (! tarval_is_long(tv2))
4291                         return or;
4292
4293                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4294                         != get_mode_size_bits(mode))
4295                         return or;
4296
4297                 /* yet, condition met */
4298                 block = get_irn_n(or, -1);
4299
4300                 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4301
4302                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4303                 return n;
4304         } else if (is_Sub(c1)) {
4305                 v   = c2;
4306                 sub = c1;
4307
4308                 if (get_Sub_right(sub) != v)
4309                         return or;
4310
4311                 c1 = get_Sub_left(sub);
4312                 if (!is_Const(c1))
4313                         return or;
4314
4315                 tv1 = get_Const_tarval(c1);
4316                 if (! tarval_is_long(tv1))
4317                         return or;
4318
4319                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4320                         return or;
4321
4322                 /* yet, condition met */
4323                 block = get_nodes_block(or);
4324
4325                 /* a Rot right is not supported, so use a rot left */
4326                 n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
4327
4328                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4329                 return n;
4330         } else if (is_Sub(c2)) {
4331                 v   = c1;
4332                 sub = c2;
4333
4334                 c1 = get_Sub_left(sub);
4335                 if (!is_Const(c1))
4336                         return or;
4337
4338                 tv1 = get_Const_tarval(c1);
4339                 if (! tarval_is_long(tv1))
4340                         return or;
4341
4342                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4343                         return or;
4344
4345                 /* yet, condition met */
4346                 block = get_irn_n(or, -1);
4347
4348                 /* a Rot Left */
4349                 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4350
4351                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4352                 return n;
4353         }
4354
4355         return or;
4356 }  /* transform_node_Or_Rot */
4357
4358 /**
4359  * Transform an Or.
4360  */
4361 static ir_node *transform_node_Or(ir_node *n) {
4362         ir_node *c, *oldn = n;
4363         ir_node *a = get_Or_left(n);
4364         ir_node *b = get_Or_right(n);
4365         ir_mode *mode;
4366
4367         if (is_Not(a) && is_Not(b)) {
4368                 /* ~a | ~b = ~(a&b) */
4369                 ir_node *block = get_nodes_block(n);
4370
4371                 mode = get_irn_mode(n);
4372                 a = get_Not_op(a);
4373                 b = get_Not_op(b);
4374                 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4375                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4376                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4377                 return n;
4378         }
4379
4380         /* we can evaluate 2 Projs of the same Cmp */
4381         if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4382                 ir_node *pred_a = get_Proj_pred(a);
4383                 ir_node *pred_b = get_Proj_pred(b);
4384                 if (pred_a == pred_b) {
4385                         dbg_info *dbgi  = get_irn_dbg_info(n);
4386                         ir_node  *block = get_nodes_block(pred_a);
4387                         pn_Cmp pn_a     = get_Proj_proj(a);
4388                         pn_Cmp pn_b     = get_Proj_proj(b);
4389                         /* yes, we can simply calculate with pncs */
4390                         pn_Cmp new_pnc  = pn_a | pn_b;
4391
4392                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4393                                            new_pnc);
4394                 }
4395         }
4396
4397         mode = get_irn_mode(n);
4398         HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4399
4400         n = transform_node_Or_bf_store(n);
4401         n = transform_node_Or_Rot(n);
4402         if (n != oldn)
4403                 return n;
4404
4405         n = transform_bitwise_distributive(n, transform_node_Or);
4406
4407         return n;
4408 }  /* transform_node_Or */
4409
4410
4411 /* forward */
4412 static ir_node *transform_node(ir_node *n);
4413
4414 /**
4415  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4416  *
4417  * Should be moved to reassociation?
4418  */
4419 static ir_node *transform_node_shift(ir_node *n) {
4420         ir_node *left, *right;
4421         tarval *tv1, *tv2, *res;
4422         ir_mode *mode;
4423         int modulo_shf, flag;
4424
4425         left = get_binop_left(n);
4426
4427         /* different operations */
4428         if (get_irn_op(left) != get_irn_op(n))
4429                 return n;
4430
4431         right = get_binop_right(n);
4432         tv1 = value_of(right);
4433         if (tv1 == tarval_bad)
4434                 return n;
4435
4436         tv2 = value_of(get_binop_right(left));
4437         if (tv2 == tarval_bad)
4438                 return n;
4439
4440         res = tarval_add(tv1, tv2);
4441
4442         /* beware: a simple replacement works only, if res < modulo shift */
4443         mode = get_irn_mode(n);
4444
4445         flag = 0;
4446
4447         modulo_shf = get_mode_modulo_shift(mode);
4448         if (modulo_shf > 0) {
4449                 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4450
4451                 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4452                         flag = 1;
4453         } else
4454                 flag = 1;
4455
4456         if (flag) {
4457                 /* ok, we can replace it */
4458                 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4459
4460                 in[0] = get_binop_left(left);
4461                 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4462
4463                 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4464
4465                 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4466
4467                 return transform_node(irn);
4468         }
4469         return n;
4470 }  /* transform_node_shift */
4471
4472 /**
4473  * Transform a Shr.
4474  */
4475 static ir_node *transform_node_Shr(ir_node *n) {
4476         ir_node *c, *oldn = n;
4477         ir_node *a    = get_Shr_left(n);
4478         ir_node *b    = get_Shr_right(n);
4479         ir_mode *mode = get_irn_mode(n);
4480
4481         HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4482         return transform_node_shift(n);
4483 }  /* transform_node_Shr */
4484
4485 /**
4486  * Transform a Shrs.
4487  */
4488 static ir_node *transform_node_Shrs(ir_node *n) {
4489         ir_node *c, *oldn = n;
4490         ir_node *a    = get_Shrs_left(n);
4491         ir_node *b    = get_Shrs_right(n);
4492         ir_mode *mode = get_irn_mode(n);
4493
4494         HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4495         return transform_node_shift(n);
4496 }  /* transform_node_Shrs */
4497
4498 /**
4499  * Transform a Shl.
4500  */
4501 static ir_node *transform_node_Shl(ir_node *n) {
4502         ir_node *c, *oldn = n;
4503         ir_node *a    = get_Shl_left(n);
4504         ir_node *b    = get_Shl_right(n);
4505         ir_mode *mode = get_irn_mode(n);
4506
4507         HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4508         return transform_node_shift(n);
4509 }  /* transform_node_Shl */
4510
4511 /**
4512  * Transform a Rot.
4513  */
4514 static ir_node *transform_node_Rot(ir_node *n) {
4515         ir_node *c, *oldn = n;
4516         ir_node *a    = get_Rot_left(n);
4517         ir_node *b    = get_Rot_right(n);
4518         ir_mode *mode = get_irn_mode(n);
4519
4520         HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4521         return transform_node_shift(n);
4522 }  /* transform_node_Rot */
4523
4524 /**
4525  * Transform a Conv.
4526  */
4527 static ir_node *transform_node_Conv(ir_node *n) {
4528         ir_node *c, *oldn = n;
4529         ir_node *a = get_Conv_op(n);
4530
4531         if (is_const_Phi(a)) {
4532                 c = apply_conv_on_phi(a, get_irn_mode(n));
4533                 if (c) {
4534                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4535                         return c;
4536                 }
4537         }
4538         return n;
4539 }  /* transform_node_Conv */
4540
4541 /**
4542  * Remove dead blocks and nodes in dead blocks
4543  * in keep alive list.  We do not generate a new End node.
4544  */
4545 static ir_node *transform_node_End(ir_node *n) {
4546         int i, j, n_keepalives = get_End_n_keepalives(n);
4547         ir_node **in;
4548
4549         NEW_ARR_A(ir_node *, in, n_keepalives);
4550
4551         for (i = j = 0; i < n_keepalives; ++i) {
4552                 ir_node *ka = get_End_keepalive(n, i);
4553                 if (is_Block(ka)) {
4554                         if (! is_Block_dead(ka)) {
4555                                 in[j++] = ka;
4556                         }
4557                         continue;
4558                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4559                         continue;
4560                 }
4561                 /* FIXME: beabi need to keep a Proj(M) */
4562                 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4563                         in[j++] = ka;
4564         }
4565         if (j != n_keepalives)
4566                 set_End_keepalives(n, j, in);
4567         return n;
4568 }  /* transform_node_End */
4569
4570 /** returns 1 if a == -b */
4571 static int is_negated_value(ir_node *a, ir_node *b) {
4572         if(is_Minus(a) && get_Minus_op(a) == b)
4573                 return 1;
4574         if(is_Minus(b) && get_Minus_op(b) == a)
4575                 return 1;
4576         if(is_Sub(a) && is_Sub(b)) {
4577                 ir_node *a_left  = get_Sub_left(a);
4578                 ir_node *a_right = get_Sub_right(a);
4579                 ir_node *b_left  = get_Sub_left(b);
4580                 ir_node *b_right = get_Sub_right(b);
4581
4582                 if(a_left == b_right && a_right == b_left)
4583                         return 1;
4584         }
4585
4586         return 0;
4587 }
4588
4589 /**
4590  * Optimize a Mux into some simpler cases.
4591  */
4592 static ir_node *transform_node_Mux(ir_node *n) {
4593         ir_node *oldn = n, *sel = get_Mux_sel(n);
4594         ir_mode *mode = get_irn_mode(n);
4595
4596         if (mode == mode_b) {
4597                 ir_node  *t     = get_Mux_true(n);
4598                 ir_node  *f     = get_Mux_false(n);
4599                 dbg_info *dbg   = get_irn_dbg_info(n);
4600                 ir_node  *block = get_irn_n(n, -1);
4601                 ir_graph *irg   = current_ir_graph;
4602
4603                 if (is_Const(t)) {
4604                         tarval *tv_t = get_Const_tarval(t);
4605                         if (tv_t == tarval_b_true) {
4606                                 if (is_Const(f)) {
4607                                         assert(get_Const_tarval(f) == tarval_b_false);
4608                                         return sel;
4609                                 } else {
4610                                         return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4611                                 }
4612                         } else {
4613                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4614                                 assert(tv_t == tarval_b_false);
4615                                 if (is_Const(f)) {
4616                                         assert(get_Const_tarval(f) == tarval_b_true);
4617                                         return not_sel;
4618                                 } else {
4619                                         return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4620                                 }
4621                         }
4622                 } else if (is_Const(f)) {
4623                         tarval *tv_f = get_Const_tarval(f);
4624                         if (tv_f == tarval_b_true) {
4625                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4626                                 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4627                         } else {
4628                                 assert(tv_f == tarval_b_false);
4629                                 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4630                         }
4631                 }
4632         }
4633
4634         if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4635                 ir_node *cmp = get_Proj_pred(sel);
4636                 long     pn  = get_Proj_proj(sel);
4637                 ir_node *f   = get_Mux_false(n);
4638                 ir_node *t   = get_Mux_true(n);
4639
4640                 /*
4641                  * Note: normalization puts the constant on the right side,
4642                  * so we check only one case.
4643                  *
4644                  * Note further that these optimization work even for floating point
4645                  * with NaN's because -NaN == NaN.
4646                  * However, if +0 and -0 is handled differently, we cannot use the first
4647                  * one.
4648                  */
4649                 if (is_Cmp(cmp)) {
4650                         ir_node *cmp_r = get_Cmp_right(cmp);
4651                         if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4652                                 ir_node *block    = get_irn_n(n, -1);
4653
4654                                 if(is_negated_value(f, t)) {
4655                                         ir_node *cmp_left = get_Cmp_left(cmp);
4656
4657                                         /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4658                                         if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4659                                                 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4660                                         {
4661                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4662                                                                                  cmp_left, mode);
4663                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4664                                                 return n;
4665                                         /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4666                                         } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4667                                                 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4668                                         {
4669                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4670                                                                                  cmp_left, mode);
4671                                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4672                                                                                                                  block, n, mode);
4673                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4674                                                 return n;
4675                                         }
4676                                 }
4677
4678                         }
4679                 }
4680         }
4681         return arch_transform_node_Mux(n);
4682 }  /* transform_node_Mux */
4683
4684 /**
4685  * Optimize a Psi into some simpler cases.
4686  */
4687 static ir_node *transform_node_Psi(ir_node *n) {
4688         if (is_Mux(n))
4689                 return transform_node_Mux(n);
4690
4691         return n;
4692 }  /* transform_node_Psi */
4693
4694 /**
4695  * optimize sync nodes that have other syncs as input we simply add the inputs
4696  * of the other sync to our own inputs
4697  */
4698 static ir_node *transform_node_Sync(ir_node *n) {
4699         int i, arity;
4700
4701         arity = get_irn_arity(n);
4702         for(i = 0; i < get_irn_arity(n); /*empty*/) {
4703                 int i2, arity2;
4704                 ir_node *in = get_irn_n(n, i);
4705                 if(!is_Sync(in)) {
4706                         ++i;
4707                         continue;
4708                 }
4709
4710                 /* set sync input 0 instead of the sync */
4711                 set_irn_n(n, i, get_irn_n(in, 0));
4712                 /* so we check this input again for syncs */
4713
4714                 /* append all other inputs of the sync to our sync */
4715                 arity2 = get_irn_arity(in);
4716                 for(i2 = 1; i2 < arity2; ++i2) {
4717                         ir_node *in_in = get_irn_n(in, i2);
4718                         add_irn_n(n, in_in);
4719                         /* increase arity so we also check the new inputs for syncs */
4720                         arity++;
4721                 }
4722         }
4723
4724         /* rehash the sync node */
4725         add_identities(current_ir_graph->value_table, n);
4726
4727         return n;
4728 }
4729
4730 /**
4731  * Tries several [inplace] [optimizing] transformations and returns an
4732  * equivalent node.  The difference to equivalent_node() is that these
4733  * transformations _do_ generate new nodes, and thus the old node must
4734  * not be freed even if the equivalent node isn't the old one.
4735  */
4736 static ir_node *transform_node(ir_node *n) {
4737         ir_node *oldn;
4738
4739         /*
4740          * Transform_node is the only "optimizing transformation" that might
4741          * return a node with a different opcode. We iterate HERE until fixpoint
4742          * to get the final result.
4743          */
4744         do {
4745                 oldn = n;
4746                 if (n->op->ops.transform_node)
4747                         n = n->op->ops.transform_node(n);
4748         } while (oldn != n);
4749
4750         return n;
4751 }  /* transform_node */
4752
4753 /**
4754  * Sets the default transform node operation for an ir_op_ops.
4755  *
4756  * @param code   the opcode for the default operation
4757  * @param ops    the operations initialized
4758  *
4759  * @return
4760  *    The operations.
4761  */
4762 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4763 {
4764 #define CASE(a)                                 \
4765         case iro_##a:                                 \
4766                 ops->transform_node  = transform_node_##a;  \
4767                 break
4768
4769         switch (code) {
4770         CASE(Add);
4771         CASE(Sub);
4772         CASE(Mul);
4773         CASE(Div);
4774         CASE(Mod);
4775         CASE(DivMod);
4776         CASE(Quot);
4777         CASE(Abs);
4778         CASE(Cond);
4779         CASE(And);
4780         CASE(Or);
4781         CASE(Eor);
4782         CASE(Minus);
4783         CASE(Not);
4784         CASE(Cast);
4785         CASE(Proj);
4786         CASE(Phi);
4787         CASE(Sel);
4788         CASE(Shr);
4789         CASE(Shrs);
4790         CASE(Shl);
4791         CASE(Rot);
4792         CASE(Conv);
4793         CASE(End);
4794         CASE(Mux);
4795         CASE(Psi);
4796         CASE(Sync);
4797         default:
4798           /* leave NULL */;
4799         }
4800
4801         return ops;
4802 #undef CASE
4803 }  /* firm_set_default_transform_node */
4804
4805
4806 /* **************** Common Subexpression Elimination **************** */
4807
4808 /** The size of the hash table used, should estimate the number of nodes
4809     in a graph. */
4810 #define N_IR_NODES 512
4811
4812 /** Compares the attributes of two Const nodes. */
4813 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4814         return (get_Const_tarval(a) != get_Const_tarval(b))
4815             || (get_Const_type(a) != get_Const_type(b));
4816 }  /* node_cmp_attr_Const */
4817
4818 /** Compares the attributes of two Proj nodes. */
4819 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4820         return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4821 }  /* node_cmp_attr_Proj */
4822
4823 /** Compares the attributes of two Filter nodes. */
4824 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4825         return get_Filter_proj(a) != get_Filter_proj(b);
4826 }  /* node_cmp_attr_Filter */
4827
4828 /** Compares the attributes of two Alloc nodes. */
4829 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4830         const alloc_attr *pa = get_irn_alloc_attr(a);
4831         const alloc_attr *pb = get_irn_alloc_attr(b);
4832         return (pa->where != pb->where) || (pa->type != pb->type);
4833 }  /* node_cmp_attr_Alloc */
4834
4835 /** Compares the attributes of two Free nodes. */
4836 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4837         const free_attr *pa = get_irn_free_attr(a);
4838         const free_attr *pb = get_irn_free_attr(b);
4839         return (pa->where != pb->where) || (pa->type != pb->type);
4840 }  /* node_cmp_attr_Free */
4841
4842 /** Compares the attributes of two SymConst nodes. */
4843 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4844         const symconst_attr *pa = get_irn_symconst_attr(a);
4845         const symconst_attr *pb = get_irn_symconst_attr(b);
4846         return (pa->num        != pb->num)
4847             || (pa->sym.type_p != pb->sym.type_p)
4848             || (pa->tp         != pb->tp);
4849 }  /* node_cmp_attr_SymConst */
4850
4851 /** Compares the attributes of two Call nodes. */
4852 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4853         return (get_irn_call_attr(a) != get_irn_call_attr(b));
4854 }  /* node_cmp_attr_Call */
4855
4856 /** Compares the attributes of two Sel nodes. */
4857 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4858         const ir_entity *a_ent = get_Sel_entity(a);
4859         const ir_entity *b_ent = get_Sel_entity(b);
4860         return
4861                 (a_ent->kind    != b_ent->kind)    ||
4862                 (a_ent->name    != b_ent->name)    ||
4863                 (a_ent->owner   != b_ent->owner)   ||
4864                 (a_ent->ld_name != b_ent->ld_name) ||
4865                 (a_ent->type    != b_ent->type);
4866 }  /* node_cmp_attr_Sel */
4867
4868 /** Compares the attributes of two Phi nodes. */
4869 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4870         /* we can only enter this function if both nodes have the same number of inputs,
4871            hence it is enough to check if one of them is a Phi0 */
4872         if (is_Phi0(a)) {
4873                 /* check the Phi0 attribute */
4874                 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4875         }
4876         return 0;
4877 }  /* node_cmp_attr_Phi */
4878
4879 /** Compares the attributes of two Conv nodes. */
4880 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4881         return get_Conv_strict(a) != get_Conv_strict(b);
4882 }  /* node_cmp_attr_Conv */
4883
4884 /** Compares the attributes of two Cast nodes. */
4885 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4886         return get_Cast_type(a) != get_Cast_type(b);
4887 }  /* node_cmp_attr_Cast */
4888
4889 /** Compares the attributes of two Load nodes. */
4890 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4891         if (get_Load_volatility(a) == volatility_is_volatile ||
4892             get_Load_volatility(b) == volatility_is_volatile)
4893                 /* NEVER do CSE on volatile Loads */
4894                 return 1;
4895         /* do not CSE Loads with different alignment. Be conservative. */
4896         if (get_Load_align(a) != get_Load_align(b))
4897                 return 1;
4898
4899         return get_Load_mode(a) != get_Load_mode(b);
4900 }  /* node_cmp_attr_Load */
4901
4902 /** Compares the attributes of two Store nodes. */
4903 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4904         /* do not CSE Stores with different alignment. Be conservative. */
4905         if (get_Store_align(a) != get_Store_align(b))
4906                 return 1;
4907
4908         /* NEVER do CSE on volatile Stores */
4909         return (get_Store_volatility(a) == volatility_is_volatile ||
4910                 get_Store_volatility(b) == volatility_is_volatile);
4911 }  /* node_cmp_attr_Store */
4912
4913 /** Compares the attributes of two Confirm nodes. */
4914 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4915         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4916 }  /* node_cmp_attr_Confirm */
4917
4918 /** Compares the attributes of two ASM nodes. */
4919 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4920         int i, n;
4921         const ir_asm_constraint *ca;
4922         const ir_asm_constraint *cb;
4923         ident **cla, **clb;
4924
4925         if (get_ASM_text(a) != get_ASM_text(b))
4926                 return 1;
4927
4928         /* Should we really check the constraints here? Should be better, but is strange. */
4929         n = get_ASM_n_input_constraints(a);
4930         if (n != get_ASM_n_input_constraints(b))
4931                 return 0;
4932
4933         ca = get_ASM_input_constraints(a);
4934         cb = get_ASM_input_constraints(b);
4935         for (i = 0; i < n; ++i) {
4936                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4937                         return 1;
4938         }
4939
4940         n = get_ASM_n_output_constraints(a);
4941         if (n != get_ASM_n_output_constraints(b))
4942                 return 0;
4943
4944         ca = get_ASM_output_constraints(a);
4945         cb = get_ASM_output_constraints(b);
4946         for (i = 0; i < n; ++i) {
4947                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4948                         return 1;
4949         }
4950
4951         n = get_ASM_n_clobbers(a);
4952         if (n != get_ASM_n_clobbers(b))
4953                 return 0;
4954
4955         cla = get_ASM_clobbers(a);
4956         clb = get_ASM_clobbers(b);
4957         for (i = 0; i < n; ++i) {
4958                 if (cla[i] != clb[i])
4959                         return 1;
4960         }
4961         return 0;
4962 }  /* node_cmp_attr_ASM */
4963
4964 /**
4965  * Set the default node attribute compare operation for an ir_op_ops.
4966  *
4967  * @param code   the opcode for the default operation
4968  * @param ops    the operations initialized
4969  *
4970  * @return
4971  *    The operations.
4972  */
4973 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4974 {
4975 #define CASE(a)                              \
4976         case iro_##a:                              \
4977                 ops->node_cmp_attr  = node_cmp_attr_##a; \
4978                 break
4979
4980         switch (code) {
4981         CASE(Const);
4982         CASE(Proj);
4983         CASE(Filter);
4984         CASE(Alloc);
4985         CASE(Free);
4986         CASE(SymConst);
4987         CASE(Call);
4988         CASE(Sel);
4989         CASE(Phi);
4990         CASE(Conv);
4991         CASE(Cast);
4992         CASE(Load);
4993         CASE(Store);
4994         CASE(Confirm);
4995         CASE(ASM);
4996         default:
4997           /* leave NULL */;
4998         }
4999
5000         return ops;
5001 #undef CASE
5002 }  /* firm_set_default_node_cmp_attr */
5003
5004 /*
5005  * Compare function for two nodes in the hash table. Gets two
5006  * nodes as parameters.  Returns 0 if the nodes are a cse.
5007  */
5008 int identities_cmp(const void *elt, const void *key) {
5009         ir_node *a, *b;
5010         int i, irn_arity_a;
5011
5012         a = (void *)elt;
5013         b = (void *)key;
5014
5015         if (a == b) return 0;
5016
5017         if ((get_irn_op(a) != get_irn_op(b)) ||
5018             (get_irn_mode(a) != get_irn_mode(b))) return 1;
5019
5020         /* compare if a's in and b's in are of equal length */
5021         irn_arity_a = get_irn_intra_arity (a);
5022         if (irn_arity_a != get_irn_intra_arity(b))
5023                 return 1;
5024
5025         /* for block-local cse and op_pin_state_pinned nodes: */
5026         if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
5027                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5028                         return 1;
5029         }
5030
5031         /* compare a->in[0..ins] with b->in[0..ins] */
5032         for (i = 0; i < irn_arity_a; i++)
5033                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5034                         return 1;
5035
5036         /*
5037          * here, we already now that the nodes are identical except their
5038          * attributes
5039          */
5040         if (a->op->ops.node_cmp_attr)
5041                 return a->op->ops.node_cmp_attr(a, b);
5042
5043         return 0;
5044 }  /* identities_cmp */
5045
5046 /*
5047  * Calculate a hash value of a node.
5048  */
5049 unsigned ir_node_hash(ir_node *node) {
5050         unsigned h;
5051         int i, irn_arity;
5052
5053         if (node->op == op_Const) {
5054                 /* special value for const, as they only differ in their tarval. */
5055                 h = HASH_PTR(node->attr.con.tv);
5056                 h = 9*h + HASH_PTR(get_irn_mode(node));
5057         } else if (node->op == op_SymConst) {
5058                 /* special value for const, as they only differ in their symbol. */
5059                 h = HASH_PTR(node->attr.symc.sym.type_p);
5060                 h = 9*h + HASH_PTR(get_irn_mode(node));
5061         } else {
5062
5063                 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5064                 h = irn_arity = get_irn_intra_arity(node);
5065
5066                 /* consider all in nodes... except the block if not a control flow. */
5067                 for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
5068                         h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5069                 }
5070
5071                 /* ...mode,... */
5072                 h = 9*h + HASH_PTR(get_irn_mode(node));
5073                 /* ...and code */
5074                 h = 9*h + HASH_PTR(get_irn_op(node));
5075         }
5076
5077         return h;
5078 }  /* ir_node_hash */
5079
5080 pset *new_identities(void) {
5081         return new_pset(identities_cmp, N_IR_NODES);
5082 }  /* new_identities */
5083
5084 void del_identities(pset *value_table) {
5085         del_pset(value_table);
5086 }  /* del_identities */
5087
5088 /**
5089  * Normalize a node by putting constants (and operands with smaller
5090  * node index) on the right
5091  *
5092  * @param n   The node to normalize
5093  */
5094 static void normalize_node(ir_node *n) {
5095         if (get_opt_reassociation()) {
5096                 if (is_op_commutative(get_irn_op(n))) {
5097                         ir_node *l = get_binop_left(n);
5098                         ir_node *r = get_binop_right(n);
5099                         int l_idx = get_irn_idx(l);
5100                         int r_idx = get_irn_idx(r);
5101
5102                         /* For commutative operators perform  a OP b == b OP a but keep
5103                         constants on the RIGHT side. This helps greatly in some optimizations.
5104                         Moreover we use the idx number to make the form deterministic. */
5105                         if (is_irn_constlike(l))
5106                                 l_idx = -l_idx;
5107                         if (is_irn_constlike(r))
5108                                 r_idx = -r_idx;
5109                         if (l_idx < r_idx) {
5110                                 set_binop_left(n, r);
5111                                 set_binop_right(n, l);
5112                         }
5113                 }
5114         }
5115 }  /* normalize_node */
5116
5117 /**
5118  * Return the canonical node computing the same value as n.
5119  *
5120  * @param value_table  The value table
5121  * @param n            The node to lookup
5122  *
5123  * Looks up the node in a hash table.
5124  *
5125  * For Const nodes this is performed in the constructor, too.  Const
5126  * nodes are extremely time critical because of their frequent use in
5127  * constant string arrays.
5128  */
5129 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5130         ir_node *o = NULL;
5131
5132         if (!value_table) return n;
5133
5134         normalize_node(n);
5135
5136         o = pset_find(value_table, n, ir_node_hash(n));
5137         if (!o) return n;
5138
5139         DBG_OPT_CSE(n, o);
5140
5141         return o;
5142 }  /* identify */
5143
5144 /**
5145  * During construction we set the op_pin_state_pinned flag in the graph right when the
5146  * optimization is performed.  The flag turning on procedure global cse could
5147  * be changed between two allocations.  This way we are safe.
5148  */
5149 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5150         ir_node *old = n;
5151
5152         n = identify(value_table, n);
5153         if (get_irn_n(old, -1) != get_irn_n(n, -1))
5154                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5155         return n;
5156 }  /* identify_cons */
5157
5158 /*
5159  * Return the canonical node computing the same value as n.
5160  * Looks up the node in a hash table, enters it in the table
5161  * if it isn't there yet.
5162  */
5163 ir_node *identify_remember(pset *value_table, ir_node *n) {
5164         ir_node *o = NULL;
5165
5166         if (!value_table) return n;
5167
5168         normalize_node(n);
5169         /* lookup or insert in hash table with given hash key. */
5170         o = pset_insert(value_table, n, ir_node_hash(n));
5171
5172         if (o != n) {
5173                 DBG_OPT_CSE(n, o);
5174         }
5175
5176         return o;
5177 }  /* identify_remember */
5178
5179 /* Add a node to the identities value table. */
5180 void add_identities(pset *value_table, ir_node *node) {
5181         if (get_opt_cse() && is_no_Block(node))
5182                 identify_remember(value_table, node);
5183 }  /* add_identities */
5184
5185 /* Visit each node in the value table of a graph. */
5186 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5187         ir_node *node;
5188         ir_graph *rem = current_ir_graph;
5189
5190         current_ir_graph = irg;
5191         foreach_pset(irg->value_table, node)
5192                 visit(node, env);
5193         current_ir_graph = rem;
5194 }  /* visit_all_identities */
5195
5196 /**
5197  * Garbage in, garbage out. If a node has a dead input, i.e., the
5198  * Bad node is input to the node, return the Bad node.
5199  */
5200 static ir_node *gigo(ir_node *node) {
5201         int i, irn_arity;
5202         ir_op *op = get_irn_op(node);
5203
5204         /* remove garbage blocks by looking at control flow that leaves the block
5205            and replacing the control flow by Bad. */
5206         if (get_irn_mode(node) == mode_X) {
5207                 ir_node *block = get_nodes_block(skip_Proj(node));
5208
5209                 /* Don't optimize nodes in immature blocks. */
5210                 if (!get_Block_matured(block)) return node;
5211                 /* Don't optimize End, may have Bads. */
5212                 if (op == op_End) return node;
5213
5214                 if (is_Block(block)) {
5215                         irn_arity = get_irn_arity(block);
5216                         for (i = 0; i < irn_arity; i++) {
5217                                 if (!is_Bad(get_irn_n(block, i)))
5218                                         break;
5219                         }
5220                         if (i == irn_arity) {
5221                                 ir_graph *irg = get_irn_irg(block);
5222                                 /* the start block is never dead */
5223                                 if (block != get_irg_start_block(irg)
5224                                         && block != get_irg_end_block(irg))
5225                                         return new_Bad();
5226                         }
5227                 }
5228         }
5229
5230         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5231            blocks predecessors is dead. */
5232         if (op != op_Block && op != op_Phi && op != op_Tuple) {
5233                 irn_arity = get_irn_arity(node);
5234
5235                 /*
5236                  * Beware: we can only read the block of a non-floating node.
5237                  */
5238                 if (is_irn_pinned_in_irg(node) &&
5239                         is_Block_dead(get_nodes_block(node)))
5240                         return new_Bad();
5241
5242                 for (i = 0; i < irn_arity; i++) {
5243                         ir_node *pred = get_irn_n(node, i);
5244
5245                         if (is_Bad(pred))
5246                                 return new_Bad();
5247 #if 0
5248                         /* Propagating Unknowns here seems to be a bad idea, because
5249                            sometimes we need a node as a input and did not want that
5250                            it kills it's user.
5251                            However, it might be useful to move this into a later phase
5252                            (if you think that optimizing such code is useful). */
5253                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5254                                 return new_Unknown(get_irn_mode(node));
5255 #endif
5256                 }
5257         }
5258 #if 0
5259         /* With this code we violate the agreement that local_optimize
5260            only leaves Bads in Block, Phi and Tuple nodes. */
5261         /* If Block has only Bads as predecessors it's garbage. */
5262         /* If Phi has only Bads as predecessors it's garbage. */
5263         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
5264                 irn_arity = get_irn_arity(node);
5265                 for (i = 0; i < irn_arity; i++) {
5266                         if (!is_Bad(get_irn_n(node, i))) break;
5267                 }
5268                 if (i == irn_arity) node = new_Bad();
5269         }
5270 #endif
5271         return node;
5272 }  /* gigo */
5273
5274 /**
5275  * These optimizations deallocate nodes from the obstack.
5276  * It can only be called if it is guaranteed that no other nodes
5277  * reference this one, i.e., right after construction of a node.
5278  *
5279  * @param n   The node to optimize
5280  *
5281  * current_ir_graph must be set to the graph of the node!
5282  */
5283 ir_node *optimize_node(ir_node *n) {
5284         tarval *tv;
5285         ir_node *oldn = n;
5286         ir_opcode iro = get_irn_opcode(n);
5287
5288         /* Always optimize Phi nodes: part of the construction. */
5289         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5290
5291         /* constant expression evaluation / constant folding */
5292         if (get_opt_constant_folding()) {
5293                 /* neither constants nor Tuple values can be evaluated */
5294                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5295                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5296                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5297                         /* try to evaluate */
5298                         tv = computed_value(n);
5299                         if (tv != tarval_bad) {
5300                                 ir_node *nw;
5301                                 ir_type *old_tp = get_irn_type(n);
5302                                 int i, arity = get_irn_arity(n);
5303                                 int node_size;
5304
5305                                 /*
5306                                  * Try to recover the type of the new expression.
5307                                  */
5308                                 for (i = 0; i < arity && !old_tp; ++i)
5309                                         old_tp = get_irn_type(get_irn_n(n, i));
5310
5311                                 /*
5312                                  * we MUST copy the node here temporary, because it's still needed
5313                                  * for DBG_OPT_CSTEVAL
5314                                  */
5315                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
5316                                 oldn = alloca(node_size);
5317
5318                                 memcpy(oldn, n, node_size);
5319                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5320
5321                                 /* ARG, copy the in array, we need it for statistics */
5322                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5323
5324                                 /* note the inplace edges module */
5325                                 edges_node_deleted(n, current_ir_graph);
5326
5327                                 /* evaluation was successful -- replace the node. */
5328                                 irg_kill_node(current_ir_graph, n);
5329                                 nw = new_Const(get_tarval_mode(tv), tv);
5330
5331                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5332                                         set_Const_type(nw, old_tp);
5333                                 DBG_OPT_CSTEVAL(oldn, nw);
5334                                 tarval_enable_fp_ops(old_fp_mode);
5335                                 return nw;
5336                         }
5337                         tarval_enable_fp_ops(old_fp_mode);
5338                 }
5339         }
5340
5341         /* remove unnecessary nodes */
5342         if (get_opt_constant_folding() ||
5343             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5344             (iro == iro_Id)   ||
5345             (iro == iro_Proj) ||
5346             (iro == iro_Block)  )  /* Flags tested local. */
5347                 n = equivalent_node(n);
5348
5349         /* Common Subexpression Elimination.
5350          *
5351          * Checks whether n is already available.
5352          * The block input is used to distinguish different subexpressions. Right
5353          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5354          * subexpressions within a block.
5355          */
5356         if (get_opt_cse())
5357                 n = identify_cons(current_ir_graph->value_table, n);
5358
5359         if (n != oldn) {
5360                 edges_node_deleted(oldn, current_ir_graph);
5361
5362                 /* We found an existing, better node, so we can deallocate the old node. */
5363                 irg_kill_node(current_ir_graph, oldn);
5364                 return n;
5365         }
5366
5367         /* Some more constant expression evaluation that does not allow to
5368            free the node. */
5369         iro = get_irn_opcode(n);
5370         if (get_opt_constant_folding() ||
5371             (iro == iro_Cond) ||
5372             (iro == iro_Proj))     /* Flags tested local. */
5373                 n = transform_node(n);
5374
5375         /* Remove nodes with dead (Bad) input.
5376            Run always for transformation induced Bads. */
5377         n = gigo (n);
5378
5379         /* Now we have a legal, useful node. Enter it in hash table for CSE */
5380         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5381                 n = identify_remember(current_ir_graph->value_table, n);
5382         }
5383
5384         return n;
5385 }  /* optimize_node */
5386
5387
5388 /**
5389  * These optimizations never deallocate nodes (in place).  This can cause dead
5390  * nodes lying on the obstack.  Remove these by a dead node elimination,
5391  * i.e., a copying garbage collection.
5392  */
5393 ir_node *optimize_in_place_2(ir_node *n) {
5394         tarval *tv;
5395         ir_node *oldn = n;
5396         ir_opcode iro = get_irn_opcode(n);
5397
5398         if (!get_opt_optimize() && !is_Phi(n)) return n;
5399
5400         /* constant expression evaluation / constant folding */
5401         if (get_opt_constant_folding()) {
5402                 /* neither constants nor Tuple values can be evaluated */
5403                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5404                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5405                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5406                         /* try to evaluate */
5407                         tv = computed_value(n);
5408                         if (tv != tarval_bad) {
5409                                 /* evaluation was successful -- replace the node. */
5410                                 ir_type *old_tp = get_irn_type(n);
5411                                 int i, arity = get_irn_arity(n);
5412
5413                                 /*
5414                                  * Try to recover the type of the new expression.
5415                                  */
5416                                 for (i = 0; i < arity && !old_tp; ++i)
5417                                         old_tp = get_irn_type(get_irn_n(n, i));
5418
5419                                 n = new_Const(get_tarval_mode(tv), tv);
5420
5421                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5422                                         set_Const_type(n, old_tp);
5423
5424                                 DBG_OPT_CSTEVAL(oldn, n);
5425                                 tarval_enable_fp_ops(old_fp_mode);
5426                                 return n;
5427                         }
5428                         tarval_enable_fp_ops(old_fp_mode);
5429                 }
5430         }
5431
5432         /* remove unnecessary nodes */
5433         if (get_opt_constant_folding() ||
5434             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5435             (iro == iro_Id)   ||   /* ... */
5436             (iro == iro_Proj) ||   /* ... */
5437             (iro == iro_Block)  )  /* Flags tested local. */
5438                 n = equivalent_node(n);
5439
5440         /** common subexpression elimination **/
5441         /* Checks whether n is already available. */
5442         /* The block input is used to distinguish different subexpressions.  Right
5443            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5444            subexpressions within a block. */
5445         if (get_opt_cse()) {
5446                 n = identify(current_ir_graph->value_table, n);
5447         }
5448
5449         /* Some more constant expression evaluation. */
5450         iro = get_irn_opcode(n);
5451         if (get_opt_constant_folding() ||
5452                 (iro == iro_Cond) ||
5453                 (iro == iro_Proj))     /* Flags tested local. */
5454                 n = transform_node(n);
5455
5456         /* Remove nodes with dead (Bad) input.
5457            Run always for transformation induced Bads.  */
5458         n = gigo(n);
5459
5460         /* Now we can verify the node, as it has no dead inputs any more. */
5461         irn_vrfy(n);
5462
5463         /* Now we have a legal, useful node. Enter it in hash table for cse.
5464            Blocks should be unique anyways.  (Except the successor of start:
5465            is cse with the start block!) */
5466         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5467                 n = identify_remember(current_ir_graph->value_table, n);
5468
5469         return n;
5470 }  /* optimize_in_place_2 */
5471
5472 /**
5473  * Wrapper for external use, set proper status bits after optimization.
5474  */
5475 ir_node *optimize_in_place(ir_node *n) {
5476         /* Handle graph state */
5477         assert(get_irg_phase_state(current_ir_graph) != phase_building);
5478
5479         if (get_opt_global_cse())
5480                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5481         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5482                 set_irg_outs_inconsistent(current_ir_graph);
5483
5484         /* FIXME: Maybe we could also test whether optimizing the node can
5485            change the control graph. */
5486         set_irg_doms_inconsistent(current_ir_graph);
5487         return optimize_in_place_2(n);
5488 }  /* optimize_in_place */
5489
5490 /*
5491  * Sets the default operation for an ir_ops.
5492  */
5493 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5494         ops = firm_set_default_computed_value(code, ops);
5495         ops = firm_set_default_equivalent_node(code, ops);
5496         ops = firm_set_default_transform_node(code, ops);
5497         ops = firm_set_default_node_cmp_attr(code, ops);
5498         ops = firm_set_default_get_type(code, ops);
5499         ops = firm_set_default_get_type_attr(code, ops);
5500         ops = firm_set_default_get_entity_attr(code, ops);
5501
5502         return ops;
5503 }  /* firm_set_default_operations */