count executed optimization
[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) && mode_is_signed(get_irn_mode(a)))
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 (smaller_mode(b_mode, 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 left   if non-zero, other is the left operand, else the right
1717  *
1718  * @return a new Phi node if the conversion was successful, NULL else
1719  */
1720 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1721         tarval   *tv;
1722         void     **res;
1723         ir_node  *pred;
1724         ir_mode  *mode;
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         mode = get_irn_mode(phi);
1755         irg  = current_ir_graph;
1756         for (i = 0; i < n; ++i) {
1757                 pred = get_irn_n(phi, i);
1758                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1759                         mode, res[i], get_Const_type(pred));
1760         }
1761         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1762 }  /* apply_binop_on_phi */
1763
1764 /**
1765  * Apply an evaluator on a unop with a constant operator (a Phi).
1766  *
1767  * @param phi    the Phi node
1768  * @param eval   an evaluator function
1769  *
1770  * @return a new Phi node if the conversion was successful, NULL else
1771  */
1772 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1773         tarval   *tv;
1774         void     **res;
1775         ir_node  *pred;
1776         ir_mode  *mode;
1777         ir_graph *irg;
1778         int      i, n = get_irn_arity(phi);
1779
1780         NEW_ARR_A(void *, res, n);
1781         for (i = 0; i < n; ++i) {
1782                 pred = get_irn_n(phi, i);
1783                 tv   = get_Const_tarval(pred);
1784                 tv   = eval(tv);
1785
1786                 if (tv == tarval_bad) {
1787                         /* folding failed, bad */
1788                         return 0;
1789                 }
1790                 res[i] = tv;
1791         }
1792         mode = get_irn_mode(phi);
1793         irg  = current_ir_graph;
1794         for (i = 0; i < n; ++i) {
1795                 pred = get_irn_n(phi, i);
1796                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1797                         mode, res[i], get_Const_type(pred));
1798         }
1799         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1800 }  /* apply_unop_on_phi */
1801
1802 /**
1803  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1804  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1805  * If possible, remove the Conv's.
1806  */
1807 static ir_node *transform_node_AddSub(ir_node *n) {
1808         ir_mode *mode = get_irn_mode(n);
1809
1810         if (mode_is_reference(mode)) {
1811                 ir_node *left  = get_binop_left(n);
1812                 ir_node *right = get_binop_right(n);
1813                 int ref_bits   = get_mode_size_bits(mode);
1814
1815                 if (is_Conv(left)) {
1816                         ir_mode *mode = get_irn_mode(left);
1817                         int bits      = get_mode_size_bits(mode);
1818
1819                         if (ref_bits == bits &&
1820                             mode_is_int(mode) &&
1821                             get_mode_arithmetic(mode) == irma_twos_complement) {
1822                                 ir_node *pre      = get_Conv_op(left);
1823                                 ir_mode *pre_mode = get_irn_mode(pre);
1824
1825                                 if (mode_is_int(pre_mode) &&
1826                                     get_mode_size_bits(pre_mode) == bits &&
1827                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1828                                         /* ok, this conv just changes to sign, moreover the calculation
1829                                          * is done with same number of bits as our address mode, so
1830                                          * we can ignore the conv as address calculation can be viewed
1831                                          * as either signed or unsigned
1832                                          */
1833                                         set_binop_left(n, pre);
1834                                 }
1835                         }
1836                 }
1837
1838                 if (is_Conv(right)) {
1839                         ir_mode *mode = get_irn_mode(right);
1840                         int bits      = get_mode_size_bits(mode);
1841
1842                         if (ref_bits == bits &&
1843                                 mode_is_int(mode) &&
1844                                 get_mode_arithmetic(mode) == irma_twos_complement) {
1845                                 ir_node *pre      = get_Conv_op(right);
1846                                 ir_mode *pre_mode = get_irn_mode(pre);
1847
1848                                 if (mode_is_int(pre_mode) &&
1849                                     get_mode_size_bits(pre_mode) == bits &&
1850                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1851                                         /* ok, this conv just changes to sign, moreover the calculation
1852                                          * is done with same number of bits as our address mode, so
1853                                          * we can ignore the conv as address calculation can be viewed
1854                                          * as either signed or unsigned
1855                                          */
1856                                         set_binop_right(n, pre);
1857                                 }
1858                         }
1859                 }
1860         }
1861         return n;
1862 }  /* transform_node_AddSub */
1863
1864 #define HANDLE_BINOP_PHI(op,a,b,c)                          \
1865   c = NULL;                                                 \
1866   if (is_Const(b) && is_const_Phi(a)) {                     \
1867     /* check for Op(Phi, Const) */                          \
1868     c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0);  \
1869   }                                                         \
1870   else if (is_Const(a) && is_const_Phi(b)) {                \
1871     /* check for Op(Const, Phi) */                          \
1872     c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1);  \
1873   }                                                         \
1874   if (c) {                                                  \
1875     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);             \
1876     return c;                                               \
1877   }
1878
1879 #define HANDLE_UNOP_PHI(op,a,c)                   \
1880   c = NULL;                                       \
1881   if (is_const_Phi(a)) {                          \
1882     /* check for Op(Phi) */                       \
1883     c = apply_unop_on_phi(a, op);                 \
1884     if (c) {                                      \
1885       DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1886       return c;                                   \
1887     }                                             \
1888   }
1889
1890 /**
1891  * Do the AddSub optimization, then Transform
1892  *   Constant folding on Phi
1893  *   Add(a,a)          -> Mul(a, 2)
1894  *   Add(Mul(a, x), a) -> Mul(a, x+1)
1895  * if the mode is integer or float.
1896  * Transform Add(a,-b) into Sub(a,b).
1897  * Reassociation might fold this further.
1898  */
1899 static ir_node *transform_node_Add(ir_node *n) {
1900         ir_mode *mode;
1901         ir_node *a, *b, *c, *oldn = n;
1902
1903         n = transform_node_AddSub(n);
1904
1905         a = get_Add_left(n);
1906         b = get_Add_right(n);
1907
1908         HANDLE_BINOP_PHI(tarval_add, a,b,c);
1909
1910         mode = get_irn_mode(n);
1911
1912         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1913         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1914                 return n;
1915
1916         if (mode_is_num(mode)) {
1917                 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1918                 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
1919                         ir_node *block = get_irn_n(n, -1);
1920
1921                         n = new_rd_Mul(
1922                                 get_irn_dbg_info(n),
1923                                 current_ir_graph,
1924                                 block,
1925                                 a,
1926                                 new_r_Const_long(current_ir_graph, block, mode, 2),
1927                                 mode);
1928                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1929                         return n;
1930                 }
1931                 if (is_Minus(a)) {
1932                         n = new_rd_Sub(
1933                                         get_irn_dbg_info(n),
1934                                         current_ir_graph,
1935                                         get_irn_n(n, -1),
1936                                         b,
1937                                         get_Minus_op(a),
1938                                         mode);
1939                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1940                         return n;
1941                 }
1942                 if (is_Minus(b)) {
1943                         n = new_rd_Sub(
1944                                         get_irn_dbg_info(n),
1945                                         current_ir_graph,
1946                                         get_irn_n(n, -1),
1947                                         a,
1948                                         get_Minus_op(b),
1949                                         mode);
1950                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1951                         return n;
1952                 }
1953                 if (! is_reassoc_running()) {
1954                         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1955                         if (is_Mul(a)) {
1956                                 ir_node *ma = get_Mul_left(a);
1957                                 ir_node *mb = get_Mul_right(a);
1958
1959                                 if (b == ma) {
1960                                         ir_node *blk = get_irn_n(n, -1);
1961                                         n = new_rd_Mul(
1962                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1963                                                         ma,
1964                                                         new_rd_Add(
1965                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1966                                                                 mb,
1967                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
1968                                                                 mode),
1969                                                         mode);
1970                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1971                                         return n;
1972                                 } else if (b == mb) {
1973                                         ir_node *blk = get_irn_n(n, -1);
1974                                         n = new_rd_Mul(
1975                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1976                                                         mb,
1977                                                         new_rd_Add(
1978                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1979                                                                 ma,
1980                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
1981                                                                 mode),
1982                                                         mode);
1983                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1984                                         return n;
1985                                 }
1986                         }
1987                         if (is_Mul(b)) {
1988                                 ir_node *ma = get_Mul_left(b);
1989                                 ir_node *mb = get_Mul_right(b);
1990
1991                                 if (a == ma) {
1992                                         ir_node *blk = get_irn_n(n, -1);
1993                                         n = new_rd_Mul(
1994                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1995                                                         ma,
1996                                                         new_rd_Add(
1997                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1998                                                                 mb,
1999                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2000                                                                 mode),
2001                                                         mode);
2002                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2003                                         return n;
2004                                 }
2005                                 if (a == 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                 }
2021                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2022                         /* Here we rely on constants be on the RIGHT side */
2023                         if (is_Not(a)) {
2024                                 ir_node *op = get_Not_op(a);
2025
2026                                 if (is_Const(b) && is_Const_one(b)) {
2027                                         /* ~x + 1 = -x */
2028                                         ir_node *blk = get_irn_n(n, -1);
2029                                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2030                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2031                                         return n;
2032                                 }
2033                                 if (op == b) {
2034                                         /* ~x + x = -1 */
2035                                         ir_node *blk = get_irn_n(n, -1);
2036                                         n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2037                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2038                                         return n;
2039                                 }
2040                         }
2041                         if (is_Not(b)) {
2042                                 ir_node *op = get_Not_op(b);
2043
2044                                 if (op == a) {
2045                                         /* x + ~x = -1 */
2046                                         ir_node *blk = get_irn_n(n, -1);
2047                                         n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2048                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2049                                         return n;
2050                                 }
2051                         }
2052                 }
2053         }
2054         return n;
2055 }  /* transform_node_Add */
2056
2057 /**
2058  * returns -cnst or NULL if impossible
2059  */
2060 static ir_node *const_negate(ir_node *cnst) {
2061         tarval   *tv    = tarval_neg(get_Const_tarval(cnst));
2062         dbg_info *dbgi  = get_irn_dbg_info(cnst);
2063         ir_graph *irg   = get_irn_irg(cnst);
2064         ir_node  *block = get_nodes_block(cnst);
2065         ir_mode  *mode  = get_irn_mode(cnst);
2066         if (tv == tarval_bad) return NULL;
2067         return new_rd_Const(dbgi, irg, block, mode, tv);
2068 }
2069
2070 /**
2071  * Do the AddSub optimization, then Transform
2072  *   Constant folding on Phi
2073  *   Sub(0,a)          -> Minus(a)
2074  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2075  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2076  *   Sub(Add(a, x), x) -> a
2077  *   Sub(x, Add(x, a)) -> -a
2078  *   Sub(x, Const)     -> Add(x, -Const)
2079  */
2080 static ir_node *transform_node_Sub(ir_node *n) {
2081         ir_mode *mode;
2082         ir_node *oldn = n;
2083         ir_node *a, *b, *c;
2084
2085         n = transform_node_AddSub(n);
2086
2087         a = get_Sub_left(n);
2088         b = get_Sub_right(n);
2089
2090         mode = get_irn_mode(n);
2091
2092 restart:
2093         HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2094
2095         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2096         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2097                 return n;
2098
2099         if (is_Const(b) && get_irn_mode(b) != mode_P) {
2100                 /* a - C -> a + (-C) */
2101                 ir_node *cnst = const_negate(b);
2102                 if (cnst != NULL) {
2103                         ir_node  *block = get_nodes_block(n);
2104                         dbg_info *dbgi  = get_irn_dbg_info(n);
2105                         ir_graph *irg   = get_irn_irg(n);
2106
2107                         n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2108                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2109                         return n;
2110                 }
2111         }
2112
2113         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2114                 ir_graph *irg   = current_ir_graph;
2115                 dbg_info *dbg   = get_irn_dbg_info(n);
2116                 ir_node  *block = get_nodes_block(n);
2117                 ir_node  *left  = get_Minus_op(a);
2118                 ir_mode  *mode  = get_irn_mode(n);
2119                 ir_node  *add   = new_rd_Add(dbg, irg, block, left, b, mode);
2120
2121                 n = new_rd_Minus(dbg, irg, block, add, mode);
2122                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2123                 return n;
2124         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2125                 ir_graph *irg   = current_ir_graph;
2126                 dbg_info *dbg   = get_irn_dbg_info(n);
2127                 ir_node  *block = get_nodes_block(n);
2128                 ir_node  *right = get_Minus_op(b);
2129                 ir_mode  *mode  = get_irn_mode(n);
2130
2131                 n = new_rd_Add(dbg, irg, block, a, right, mode);
2132                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2133                 return n;
2134         } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2135                 ir_graph *irg     = current_ir_graph;
2136                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2137                 ir_node  *s_block = get_nodes_block(b);
2138                 ir_node  *s_left  = get_Sub_right(b);
2139                 ir_node  *s_right = get_Sub_left(b);
2140                 ir_mode  *s_mode  = get_irn_mode(b);
2141                 ir_node  *sub     = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2142                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2143                 ir_node  *a_block = get_nodes_block(n);
2144                 ir_mode  *a_mode  = get_irn_mode(n);
2145
2146                 n = new_rd_Add(a_dbg, irg, a_block, a, sub, a_mode);
2147                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2148                 return n;
2149         } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2150                 ir_node *m_right = get_Mul_right(b);
2151                 if (is_Const(m_right)) {
2152                         ir_node *cnst2 = const_negate(m_right);
2153                         if (cnst2 != NULL) {
2154                                 ir_graph *irg     = current_ir_graph;
2155                                 dbg_info *m_dbg   = get_irn_dbg_info(b);
2156                                 ir_node  *m_block = get_nodes_block(b);
2157                                 ir_node  *m_left  = get_Mul_left(b);
2158                                 ir_mode  *m_mode  = get_irn_mode(b);
2159                                 ir_node  *mul     = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2160                                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2161                                 ir_node  *a_block = get_nodes_block(n);
2162                                 ir_mode  *a_mode  = get_irn_mode(n);
2163
2164                                 n = new_rd_Add(a_dbg, irg, a_block, a, mul, a_mode);
2165                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2166                                 return n;
2167                         }
2168                 }
2169         }
2170
2171         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2172         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2173                 n = new_rd_Minus(
2174                                 get_irn_dbg_info(n),
2175                                 current_ir_graph,
2176                                 get_irn_n(n, -1),
2177                                 b,
2178                                 mode);
2179                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2180                 return n;
2181         }
2182         if (is_Add(a)) {
2183                 if (mode_wrap_around(mode)) {
2184                         ir_node *left  = get_Add_left(a);
2185                         ir_node *right = get_Add_right(a);
2186
2187                         /* FIXME: Does the Conv's work only for two complement or generally? */
2188                         if (left == b) {
2189                                 if (mode != get_irn_mode(right)) {
2190                                         /* This Sub is an effective Cast */
2191                                         right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2192                                 }
2193                                 n = right;
2194                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2195                                 return n;
2196                         } else if (right == b) {
2197                                 if (mode != get_irn_mode(left)) {
2198                                         /* This Sub is an effective Cast */
2199                                         left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2200                                 }
2201                                 n = left;
2202                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2203                                 return n;
2204                         }
2205                 }
2206         }
2207         if (is_Add(b)) {
2208                 if (mode_wrap_around(mode)) {
2209                         ir_node *left  = get_Add_left(b);
2210                         ir_node *right = get_Add_right(b);
2211
2212                         /* FIXME: Does the Conv's work only for two complement or generally? */
2213                         if (left == a) {
2214                                 ir_mode *r_mode = get_irn_mode(right);
2215
2216                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2217                                 if (mode != r_mode) {
2218                                         /* This Sub is an effective Cast */
2219                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2220                                 }
2221                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2222                                 return n;
2223                         } else if (right == a) {
2224                                 ir_mode *l_mode = get_irn_mode(left);
2225
2226                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2227                                 if (mode != l_mode) {
2228                                         /* This Sub is an effective Cast */
2229                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2230                                 }
2231                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2232                                 return n;
2233                         }
2234                 }
2235         }
2236         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2237                 ir_mode *mode = get_irn_mode(a);
2238
2239                 if (mode == get_irn_mode(b)) {
2240                         ir_mode *ma, *mb;
2241
2242                         a = get_Conv_op(a);
2243                         b = get_Conv_op(b);
2244
2245                         /* check if it's allowed to skip the conv */
2246                         ma = get_irn_mode(a);
2247                         mb = get_irn_mode(b);
2248
2249                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2250                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2251                                 set_Sub_left(n, a);
2252                                 set_Sub_right(n, b);
2253
2254                                 goto restart;
2255                         }
2256                 }
2257         }
2258         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2259         if (!is_reassoc_running() && is_Mul(a)) {
2260                 ir_node *ma = get_Mul_left(a);
2261                 ir_node *mb = get_Mul_right(a);
2262
2263                 if (ma == b) {
2264                         ir_node *blk = get_irn_n(n, -1);
2265                         n = new_rd_Mul(
2266                                         get_irn_dbg_info(n),
2267                                         current_ir_graph, blk,
2268                                         ma,
2269                                         new_rd_Sub(
2270                                                 get_irn_dbg_info(n),
2271                                                 current_ir_graph, blk,
2272                                                 mb,
2273                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2274                                                 mode),
2275                                         mode);
2276                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2277                         return n;
2278                 } else if (mb == b) {
2279                         ir_node *blk = get_irn_n(n, -1);
2280                         n = new_rd_Mul(
2281                                         get_irn_dbg_info(n),
2282                                         current_ir_graph, blk,
2283                                         mb,
2284                                         new_rd_Sub(
2285                                                 get_irn_dbg_info(n),
2286                                                 current_ir_graph, blk,
2287                                                 ma,
2288                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2289                                                 mode),
2290                                         mode);
2291                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2292                         return n;
2293                 }
2294         }
2295         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2296                 ir_node *x   =      get_Sub_left(a);
2297                 ir_node *y        = get_Sub_right(a);
2298                 ir_node *blk      = get_irn_n(n, -1);
2299                 ir_mode *m_b      = get_irn_mode(b);
2300                 ir_mode *m_y      = get_irn_mode(y);
2301                 ir_mode *add_mode;
2302                 ir_node *add;
2303
2304                 /* Determine the right mode for the Add. */
2305                 if (m_b == m_y)
2306                         add_mode = m_b;
2307                 else if (mode_is_reference(m_b))
2308                         add_mode = m_b;
2309                 else if (mode_is_reference(m_y))
2310                         add_mode = m_y;
2311                 else {
2312                         /*
2313                          * Both modes are different but none is reference,
2314                          * happens for instance in SubP(SubP(P, Iu), Is).
2315                          * We have two possibilities here: Cast or ignore.
2316                          * Currently we ignore this case.
2317                          */
2318                         return n;
2319                 }
2320
2321                 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2322
2323                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2324                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2325                 return n;
2326         }
2327
2328         if (get_mode_arithmetic(mode) == irma_twos_complement) {
2329                 if (is_Const(a) && is_Not(b)) {
2330                         /* c - ~X = X + (c+1) */
2331                         tarval *tv = get_Const_tarval(a);
2332
2333                         tv = tarval_add(tv, get_mode_one(mode));
2334                         if (tv != tarval_bad) {
2335                                 ir_node *blk = get_irn_n(n, -1);
2336                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2337                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2338                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2339                                 return n;
2340                         }
2341                 }
2342         }
2343         return n;
2344 }  /* transform_node_Sub */
2345
2346 /**
2347  * Several transformation done on n*n=2n bits mul.
2348  * These transformations must be done here because new nodes may be produced.
2349  */
2350 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2351         ir_node *oldn = n;
2352         ir_node *a = get_Mul_left(n);
2353         ir_node *b = get_Mul_right(n);
2354         tarval *ta = value_of(a);
2355         tarval *tb = value_of(b);
2356         ir_mode *smode = get_irn_mode(a);
2357
2358         if (ta == get_mode_one(smode)) {
2359                 ir_node *blk = get_irn_n(n, -1);
2360                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2361                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2362                 return n;
2363         }
2364         else if (ta == get_mode_minus_one(smode)) {
2365                 ir_node *blk = get_irn_n(n, -1);
2366                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2367                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2368                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2369                 return n;
2370         }
2371         if (tb == get_mode_one(smode)) {
2372                 ir_node *blk = get_irn_n(a, -1);
2373                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2374                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2375                 return n;
2376         }
2377         else if (tb == get_mode_minus_one(smode)) {
2378                 ir_node *blk = get_irn_n(n, -1);
2379                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2380                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2381                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2382                 return n;
2383         }
2384         return n;
2385 }
2386
2387 /**
2388  * Transform Mul(a,-1) into -a.
2389  * Do constant evaluation of Phi nodes.
2390  * Do architecture dependent optimizations on Mul nodes
2391  */
2392 static ir_node *transform_node_Mul(ir_node *n) {
2393         ir_node *c, *oldn = n;
2394         ir_mode *mode = get_irn_mode(n);
2395         ir_node *a = get_Mul_left(n);
2396         ir_node *b = get_Mul_right(n);
2397
2398         if (is_Bad(a) || is_Bad(b))
2399                 return n;
2400
2401         if (mode != get_irn_mode(a))
2402                 return transform_node_Mul2n(n, mode);
2403
2404         HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2405
2406         if (mode_is_signed(mode)) {
2407                 ir_node *r = NULL;
2408
2409                 if (value_of(a) == get_mode_minus_one(mode))
2410                         r = b;
2411                 else if (value_of(b) == get_mode_minus_one(mode))
2412                         r = a;
2413                 if (r) {
2414                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2415                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2416                         return n;
2417                 }
2418         }
2419         if (is_Minus(a)) {
2420                 if (is_Const(b)) { /* (-a) * const -> a * -const */
2421                         ir_node *cnst = const_negate(b);
2422                         if (cnst != NULL) {
2423                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2424                                 ir_node  *block = get_nodes_block(n);
2425                                 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2426                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2427                                 return n;
2428                         }
2429                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2430                         dbg_info *dbgi  = get_irn_dbg_info(n);
2431                         ir_node  *block = get_nodes_block(n);
2432                         n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2433                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2434                         return n;
2435                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2436                         ir_node  *sub_l = get_Sub_left(b);
2437                         ir_node  *sub_r = get_Sub_right(b);
2438                         dbg_info *dbgi  = get_irn_dbg_info(n);
2439                         ir_graph *irg   = current_ir_graph;
2440                         ir_node  *block = get_nodes_block(n);
2441                         ir_node  *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2442                         n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2443                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2444                         return n;
2445                 }
2446         } else if (is_Minus(b)) {
2447                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2448                         ir_node  *sub_l = get_Sub_left(a);
2449                         ir_node  *sub_r = get_Sub_right(a);
2450                         dbg_info *dbgi  = get_irn_dbg_info(n);
2451                         ir_graph *irg   = current_ir_graph;
2452                         ir_node  *block = get_nodes_block(n);
2453                         ir_node  *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2454                         n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2455                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2456                         return n;
2457                 }
2458         }
2459         if (get_mode_arithmetic(mode) == irma_ieee754) {
2460                 if (is_Const(a)) {
2461                         tarval *tv = get_Const_tarval(a);
2462                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2463                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2464                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2465                                 return n;
2466                         }
2467                 }
2468                 else if (is_Const(b)) {
2469                         tarval *tv = get_Const_tarval(b);
2470                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2471                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2472                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2473                                 return n;
2474                         }
2475                 }
2476         }
2477         return arch_dep_replace_mul_with_shifts(n);
2478 }  /* transform_node_Mul */
2479
2480 /**
2481  * Transform a Div Node.
2482  */
2483 static ir_node *transform_node_Div(ir_node *n) {
2484         tarval *tv = value_of(n);
2485         ir_mode *mode = get_Div_resmode(n);
2486         ir_node *value = n;
2487
2488         if (tv != tarval_bad) {
2489                 value = new_Const(get_tarval_mode(tv), tv);
2490
2491                 DBG_OPT_CSTEVAL(n, value);
2492                 goto make_tuple;
2493         } else {
2494                 ir_node *a = get_Div_left(n);
2495                 ir_node *b = get_Div_right(n);
2496                 ir_node *dummy;
2497
2498                 if (a == b && value_not_zero(a, &dummy)) {
2499                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2500                         value = new_Const(mode, get_mode_one(mode));
2501                         DBG_OPT_CSTEVAL(n, value);
2502                         goto make_tuple;
2503                 } else {
2504                         if (mode_is_signed(mode) && is_Const(b)) {
2505                                 tarval *tv = get_Const_tarval(b);
2506
2507                                 if (tv == get_mode_minus_one(mode)) {
2508                                         /* a / -1 */
2509                                         value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2510                                         DBG_OPT_CSTEVAL(n, value);
2511                                         goto make_tuple;
2512                                 }
2513                         }
2514                         /* Try architecture dependent optimization */
2515                         value = arch_dep_replace_div_by_const(n);
2516                 }
2517         }
2518
2519         if (value != n) {
2520                 ir_node *mem, *blk;
2521
2522 make_tuple:
2523                 /* Turn Div into a tuple (mem, jmp, bad, value) */
2524                 mem = get_Div_mem(n);
2525                 blk = get_irn_n(n, -1);
2526
2527                 /* skip a potential Pin */
2528                 if (is_Pin(mem))
2529                         mem = get_Pin_op(mem);
2530                 turn_into_tuple(n, pn_Div_max);
2531                 set_Tuple_pred(n, pn_Div_M,         mem);
2532                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2533                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());
2534                 set_Tuple_pred(n, pn_Div_res,       value);
2535         }
2536         return n;
2537 }  /* transform_node_Div */
2538
2539 /**
2540  * Transform a Mod node.
2541  */
2542 static ir_node *transform_node_Mod(ir_node *n) {
2543         tarval *tv = value_of(n);
2544         ir_mode *mode = get_Mod_resmode(n);
2545         ir_node *value = n;
2546
2547         if (tv != tarval_bad) {
2548                 value = new_Const(get_tarval_mode(tv), tv);
2549
2550                 DBG_OPT_CSTEVAL(n, value);
2551                 goto make_tuple;
2552         } else {
2553                 ir_node *a = get_Mod_left(n);
2554                 ir_node *b = get_Mod_right(n);
2555                 ir_node *dummy;
2556
2557                 if (a == b && value_not_zero(a, &dummy)) {
2558                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2559                         value = new_Const(mode, get_mode_null(mode));
2560                         DBG_OPT_CSTEVAL(n, value);
2561                         goto make_tuple;
2562                 } else {
2563                         if (mode_is_signed(mode) && is_Const(b)) {
2564                                 tarval *tv = get_Const_tarval(b);
2565
2566                                 if (tv == get_mode_minus_one(mode)) {
2567                                         /* a % -1 = 0 */
2568                                         value = new_Const(mode, get_mode_null(mode));
2569                                         DBG_OPT_CSTEVAL(n, value);
2570                                         goto make_tuple;
2571                                 }
2572                         }
2573                         /* Try architecture dependent optimization */
2574                         value = arch_dep_replace_mod_by_const(n);
2575                 }
2576         }
2577
2578         if (value != n) {
2579                 ir_node *mem, *blk;
2580
2581 make_tuple:
2582                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2583                 mem = get_Mod_mem(n);
2584                 blk = get_irn_n(n, -1);
2585
2586                 /* skip a potential Pin */
2587                 if (is_Pin(mem))
2588                         mem = get_Pin_op(mem);
2589                 turn_into_tuple(n, pn_Mod_max);
2590                 set_Tuple_pred(n, pn_Mod_M,         mem);
2591                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2592                 set_Tuple_pred(n, pn_Mod_X_except,  new_Bad());
2593                 set_Tuple_pred(n, pn_Mod_res,       value);
2594         }
2595         return n;
2596 }  /* transform_node_Mod */
2597
2598 /**
2599  * Transform a DivMod node.
2600  */
2601 static ir_node *transform_node_DivMod(ir_node *n) {
2602         ir_node *dummy;
2603         ir_node *a = get_DivMod_left(n);
2604         ir_node *b = get_DivMod_right(n);
2605         ir_mode *mode = get_DivMod_resmode(n);
2606         tarval *ta = value_of(a);
2607         tarval *tb = value_of(b);
2608         int evaluated = 0;
2609
2610         if (tb != tarval_bad) {
2611                 if (tb == get_mode_one(get_tarval_mode(tb))) {
2612                         b = new_Const(mode, get_mode_null(mode));
2613                         DBG_OPT_CSTEVAL(n, b);
2614                         goto make_tuple;
2615                 } else if (ta != tarval_bad) {
2616                         tarval *resa, *resb;
2617                         resa = tarval_div(ta, tb);
2618                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2619                                                              Jmp for X result!? */
2620                         resb = tarval_mod(ta, tb);
2621                         if (resb == tarval_bad) return n; /* Causes exception! */
2622                         a = new_Const(mode, resa);
2623                         b = new_Const(mode, resb);
2624                         DBG_OPT_CSTEVAL(n, a);
2625                         DBG_OPT_CSTEVAL(n, b);
2626                         goto make_tuple;
2627                 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2628                         a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2629                         b = new_Const(mode, get_mode_null(mode));
2630                         DBG_OPT_CSTEVAL(n, a);
2631                         DBG_OPT_CSTEVAL(n, b);
2632                         goto make_tuple;
2633                 } else { /* Try architecture dependent optimization */
2634                         arch_dep_replace_divmod_by_const(&a, &b, n);
2635                         evaluated = a != NULL;
2636                 }
2637         } else if (a == b) {
2638                 if (value_not_zero(a, &dummy)) {
2639                         /* a/a && a != 0 */
2640                         a = new_Const(mode, get_mode_one(mode));
2641                         b = new_Const(mode, get_mode_null(mode));
2642                         DBG_OPT_CSTEVAL(n, a);
2643                         DBG_OPT_CSTEVAL(n, b);
2644                         goto make_tuple;
2645                 } else {
2646                         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2647                         return n;
2648                 }
2649         } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2650                 /* 0 / non-Const = 0 */
2651                 b = a;
2652                 goto make_tuple;
2653         }
2654
2655         if (evaluated) { /* replace by tuple */
2656                 ir_node *mem, *blk;
2657
2658 make_tuple:
2659                 mem = get_DivMod_mem(n);
2660                 /* skip a potential Pin */
2661                 if (is_Pin(mem))
2662                         mem = get_Pin_op(mem);
2663
2664                 blk = get_irn_n(n, -1);
2665                 turn_into_tuple(n, pn_DivMod_max);
2666                 set_Tuple_pred(n, pn_DivMod_M,         mem);
2667                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2668                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());  /* no exception */
2669                 set_Tuple_pred(n, pn_DivMod_res_div,   a);
2670                 set_Tuple_pred(n, pn_DivMod_res_mod,  b);
2671         }
2672
2673         return n;
2674 }  /* transform_node_DivMod */
2675
2676 /**
2677  * Optimize x / c to x * (1/c)
2678  */
2679 static ir_node *transform_node_Quot(ir_node *n) {
2680         ir_mode *mode = get_Quot_resmode(n);
2681         ir_node *oldn = n;
2682
2683         if (get_mode_arithmetic(mode) == irma_ieee754) {
2684                 ir_node *b = get_Quot_right(n);
2685
2686                 if (is_Const(b)) {
2687                         tarval *tv = get_Const_tarval(b);
2688
2689                         tv = tarval_quo(get_mode_one(mode), tv);
2690
2691                         /* Do the transformation if the result is either exact or we are not
2692                            using strict rules. */
2693                         if (tv != tarval_bad &&
2694                             (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2695                                 ir_node *blk = get_irn_n(n, -1);
2696                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2697                                 ir_node *a = get_Quot_left(n);
2698                                 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2699                                 ir_node *mem = get_Quot_mem(n);
2700
2701                                 /* skip a potential Pin */
2702                                 if (is_Pin(mem))
2703                                         mem = get_Pin_op(mem);
2704                                 turn_into_tuple(n, pn_Quot_max);
2705                                 set_Tuple_pred(n, pn_Quot_M, mem);
2706                                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2707                                 set_Tuple_pred(n, pn_Quot_X_except,  new_r_Bad(current_ir_graph));
2708                                 set_Tuple_pred(n, pn_Quot_res, m);
2709                                 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2710                         }
2711                 }
2712         }
2713         return n;
2714 }  /* transform_node_Quot */
2715
2716 /**
2717  * Optimize Abs(x) into  x if x is Confirmed >= 0
2718  * Optimize Abs(x) into -x if x is Confirmed <= 0
2719  */
2720 static ir_node *transform_node_Abs(ir_node *n) {
2721         ir_node        *oldn = n;
2722         ir_node        *a = get_Abs_op(n);
2723         value_classify_sign sign = classify_value_sign(a);
2724
2725         if (sign == value_classified_negative) {
2726                 ir_mode *mode = get_irn_mode(n);
2727
2728                 /*
2729                  * We can replace the Abs by -x here.
2730                  * We even could add a new Confirm here.
2731                  *
2732                  * Note that -x would create a new node, so we could
2733                  * not run it in the equivalent_node() context.
2734                  */
2735                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2736                                 get_irn_n(n, -1), a, mode);
2737
2738                 DBG_OPT_CONFIRM(oldn, n);
2739         } else if (sign == value_classified_positive) {
2740                 /* n is positive, Abs is not needed */
2741                 n = a;
2742
2743                 DBG_OPT_CONFIRM(oldn, n);
2744         }
2745
2746         return n;
2747 }  /* transform_node_Abs */
2748
2749 /**
2750  * Transform a Cond node.
2751  *
2752  * Replace the Cond by a Jmp if it branches on a constant
2753  * condition.
2754  */
2755 static ir_node *transform_node_Cond(ir_node *n) {
2756
2757         ir_node *jmp;
2758         ir_node *a = get_Cond_selector(n);
2759         tarval *ta = value_of(a);
2760
2761         /* we need block info which is not available in floating irgs */
2762         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2763                 return n;
2764
2765         if ((ta != tarval_bad) &&
2766             (get_irn_mode(a) == mode_b) &&
2767             (get_opt_unreachable_code())) {
2768                 /* It's a boolean Cond, branching on a boolean constant.
2769                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2770                 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2771                 turn_into_tuple(n, pn_Cond_max);
2772                 if (ta == tarval_b_true) {
2773                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
2774                         set_Tuple_pred(n, pn_Cond_true, jmp);
2775                 } else {
2776                         set_Tuple_pred(n, pn_Cond_false, jmp);
2777                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
2778                 }
2779                 /* We might generate an endless loop, so keep it alive. */
2780                 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2781         }
2782         return n;
2783 }  /* transform_node_Cond */
2784
2785 typedef ir_node* (*recursive_transform) (ir_node *n);
2786
2787 /**
2788  * makes use of distributive laws for and, or, eor
2789  *     and(a OP c, b OP c) -> and(a, b) OP c
2790  * note, might return a different op than n
2791  */
2792 static ir_node *transform_bitwise_distributive(ir_node *n,
2793                                                recursive_transform trans_func)
2794 {
2795         ir_node *oldn    = n;
2796         ir_node *a       = get_binop_left(n);
2797         ir_node *b       = get_binop_right(n);
2798         ir_op   *op      = get_irn_op(a);
2799         ir_op   *op_root = get_irn_op(n);
2800
2801         if(op != get_irn_op(b))
2802                 return n;
2803
2804         if (op == op_Conv) {
2805                 ir_node *a_op   = get_Conv_op(a);
2806                 ir_node *b_op   = get_Conv_op(b);
2807                 ir_mode *a_mode = get_irn_mode(a_op);
2808                 ir_mode *b_mode = get_irn_mode(b_op);
2809                 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2810                         ir_node *blk = get_irn_n(n, -1);
2811
2812                         n = exact_copy(n);
2813                         set_binop_left(n, a_op);
2814                         set_binop_right(n, b_op);
2815                         set_irn_mode(n, a_mode);
2816                         n = trans_func(n);
2817                         n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2818
2819                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2820                         return n;
2821                 }
2822         }
2823
2824         if (op == op_Eor) {
2825                 /* nothing to gain here */
2826                 return n;
2827         }
2828
2829         if (op == op_Shrs || op == op_Shr || op == op_Shl
2830                         || op == op_And || op == op_Or || op == op_Eor) {
2831                 ir_node *a_left  = get_binop_left(a);
2832                 ir_node *a_right = get_binop_right(a);
2833                 ir_node *b_left  = get_binop_left(b);
2834                 ir_node *b_right = get_binop_right(b);
2835                 ir_node *c       = NULL;
2836                 ir_node *op1, *op2;
2837
2838                 if (is_op_commutative(op)) {
2839                         if (a_left == b_left) {
2840                                 c   = a_left;
2841                                 op1 = a_right;
2842                                 op2 = b_right;
2843                         } else if(a_left == b_right) {
2844                                 c   = a_left;
2845                                 op1 = a_right;
2846                                 op2 = b_left;
2847                         } else if(a_right == b_left) {
2848                                 c   = a_right;
2849                                 op1 = a_left;
2850                                 op2 = b_right;
2851                         }
2852                 }
2853                 if(a_right == b_right) {
2854                         c   = a_right;
2855                         op1 = a_left;
2856                         op2 = b_left;
2857                 }
2858
2859                 if (c != NULL) {
2860                         /* (a sop c) & (b sop c) => (a & b) sop c */
2861                         ir_node *blk = get_irn_n(n, -1);
2862
2863                         ir_node *new_n = exact_copy(n);
2864                         set_binop_left(new_n, op1);
2865                         set_binop_right(new_n, op2);
2866                         new_n = trans_func(new_n);
2867
2868                         if(op_root == op_Eor && op == op_Or) {
2869                                 dbg_info  *dbgi = get_irn_dbg_info(n);
2870                                 ir_graph  *irg  = current_ir_graph;
2871                                 ir_mode   *mode = get_irn_mode(c);
2872
2873                                 c = new_rd_Not(dbgi, irg, blk, c, mode);
2874                                 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2875                         } else {
2876                                 n = exact_copy(a);
2877                                 set_irn_n(n, -1, blk);
2878                                 set_binop_left(n, new_n);
2879                                 set_binop_right(n, c);
2880                         }
2881
2882                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2883                         return n;
2884                 }
2885         }
2886
2887         return n;
2888 }
2889
2890 /**
2891  * Transform an And.
2892  */
2893 static ir_node *transform_node_And(ir_node *n) {
2894         ir_node *c, *oldn = n;
2895         ir_node *a = get_And_left(n);
2896         ir_node *b = get_And_right(n);
2897         ir_mode *mode;
2898
2899         HANDLE_BINOP_PHI(tarval_and, a,b,c);
2900
2901         mode = get_irn_mode(n);
2902
2903         /* we can evaluate 2 Projs of the same Cmp */
2904         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
2905                 ir_node *pred_a = get_Proj_pred(a);
2906                 ir_node *pred_b = get_Proj_pred(b);
2907                 if (pred_a == pred_b) {
2908                         dbg_info *dbgi  = get_irn_dbg_info(n);
2909                         ir_node  *block = get_nodes_block(pred_a);
2910                         pn_Cmp pn_a     = get_Proj_proj(a);
2911                         pn_Cmp pn_b     = get_Proj_proj(b);
2912                         /* yes, we can simply calculate with pncs */
2913                         pn_Cmp new_pnc  = pn_a & pn_b;
2914
2915                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
2916                 }
2917         }
2918         if (is_Or(a)) {
2919                 if (is_Not(b)) {
2920                         ir_node *op = get_Not_op(b);
2921                         if (is_And(op)) {
2922                                 ir_node *ba = get_And_left(op);
2923                                 ir_node *bb = get_And_right(op);
2924
2925                                 /* it's enough to test the following cases due to normalization! */
2926                                 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2927                                         /* (a|b) & ~(a&b) = a^b */
2928                                         ir_node *block = get_nodes_block(n);
2929
2930                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
2931                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2932                                         return n;
2933                                 }
2934                         }
2935                 }
2936         }
2937         if (is_Or(b)) {
2938                 if (is_Not(a)) {
2939                         ir_node *op = get_Not_op(a);
2940                         if (is_And(op)) {
2941                                 ir_node *aa = get_And_left(op);
2942                                 ir_node *ab = get_And_right(op);
2943
2944                                 /* it's enough to test the following cases due to normalization! */
2945                                 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2946                                         /* (a|b) & ~(a&b) = a^b */
2947                                         ir_node *block = get_nodes_block(n);
2948
2949                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
2950                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2951                                         return n;
2952                                 }
2953                         }
2954                 }
2955         }
2956         if (is_Eor(a)) {
2957                 ir_node *al = get_Eor_left(a);
2958                 ir_node *ar = get_Eor_right(a);
2959
2960                 if (al == b) {
2961                         /* (b ^ a) & b -> ~a & b */
2962                         dbg_info *dbg  = get_irn_dbg_info(n);
2963                         ir_node *block = get_nodes_block(n);
2964
2965                         ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
2966                         n  = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
2967                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2968                         return n;
2969                 }
2970                 if (ar == b) {
2971                         /* (a ^ b) & b -> ~a & b */
2972                         dbg_info *dbg  = get_irn_dbg_info(n);
2973                         ir_node *block = get_nodes_block(n);
2974
2975                         al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
2976                         n  = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
2977                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2978                         return n;
2979                 }
2980         }
2981         if (is_Eor(b)) {
2982                 ir_node *bl = get_Eor_left(b);
2983                 ir_node *br = get_Eor_right(b);
2984
2985                 if (bl == a) {
2986                         /* a & (a ^ b) -> a & ~b */
2987                         dbg_info *dbg  = get_irn_dbg_info(n);
2988                         ir_node *block = get_nodes_block(n);
2989
2990                         br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
2991                         n  = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
2992                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2993                         return n;
2994                 }
2995                 if (br == a) {
2996                         /* a & (b ^ a) -> a & ~b */
2997                         dbg_info *dbg  = get_irn_dbg_info(n);
2998                         ir_node *block = get_nodes_block(n);
2999
3000                         bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3001                         n  = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3002                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3003                         return n;
3004                 }
3005         }
3006         if (is_Not(a) && is_Not(b)) {
3007                 /* ~a & ~b = ~(a|b) */
3008                 ir_node *block = get_nodes_block(n);
3009                 ir_mode *mode = get_irn_mode(n);
3010
3011                 a = get_Not_op(a);
3012                 b = get_Not_op(b);
3013                 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3014                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3015                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3016                 return n;
3017         }
3018
3019         n = transform_bitwise_distributive(n, transform_node_And);
3020
3021         return n;
3022 }  /* transform_node_And */
3023
3024 /**
3025  * Transform an Eor.
3026  */
3027 static ir_node *transform_node_Eor(ir_node *n) {
3028         ir_node *c, *oldn = n;
3029         ir_node *a = get_Eor_left(n);
3030         ir_node *b = get_Eor_right(n);
3031         ir_mode *mode = get_irn_mode(n);
3032
3033         HANDLE_BINOP_PHI(tarval_eor, a,b,c);
3034
3035         /* we can evaluate 2 Projs of the same Cmp */
3036         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3037                 ir_node *pred_a = get_Proj_pred(a);
3038                 ir_node *pred_b = get_Proj_pred(b);
3039                 if(pred_a == pred_b) {
3040                         dbg_info *dbgi  = get_irn_dbg_info(n);
3041                         ir_node  *block = get_nodes_block(pred_a);
3042                         pn_Cmp pn_a     = get_Proj_proj(a);
3043                         pn_Cmp pn_b     = get_Proj_proj(b);
3044                         /* yes, we can simply calculate with pncs */
3045                         pn_Cmp new_pnc  = pn_a ^ pn_b;
3046
3047                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3048                                            new_pnc);
3049                 }
3050         }
3051
3052         if (a == b) {
3053                 /* a ^ a = 0 */
3054                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3055                                  mode, get_mode_null(mode));
3056                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3057         } else if (mode == mode_b &&
3058                         is_Proj(a) &&
3059                         is_Const(b) && is_Const_one(b) &&
3060                         is_Cmp(get_Proj_pred(a))) {
3061                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3062                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3063                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3064
3065                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3066         } else if (is_Const(b)) {
3067                 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3068                         ir_node  *cnst   = new_Const(mode, tarval_not(get_Const_tarval(b)));
3069                         ir_node  *not_op = get_Not_op(a);
3070                         dbg_info *dbg    = get_irn_dbg_info(n);
3071                         ir_graph *irg    = current_ir_graph;
3072                         ir_node  *block  = get_nodes_block(n);
3073                         ir_mode  *mode   = get_irn_mode(n);
3074                         n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3075                         return n;
3076                 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3077                         n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3078                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3079                 }
3080         } else {
3081                 n = transform_bitwise_distributive(n, transform_node_Eor);
3082         }
3083
3084         return n;
3085 }  /* transform_node_Eor */
3086
3087 /**
3088  * Transform a Not.
3089  */
3090 static ir_node *transform_node_Not(ir_node *n) {
3091         ir_node *c, *oldn = n;
3092         ir_node *a    = get_Not_op(n);
3093         ir_mode *mode = get_irn_mode(n);
3094
3095         HANDLE_UNOP_PHI(tarval_not,a,c);
3096
3097         /* check for a boolean Not */
3098         if (mode == mode_b &&
3099                         is_Proj(a) &&
3100                         is_Cmp(get_Proj_pred(a))) {
3101                 /* We negate a Cmp. The Cmp has the negated result anyways! */
3102                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3103                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3104                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3105                 return n;
3106         }
3107         if  (is_Eor(a)) {
3108                 ir_node *eor_b = get_Eor_right(a);
3109                 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3110                         ir_node  *cnst  = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3111                         ir_node  *eor_a = get_Eor_left(a);
3112                         dbg_info *dbg   = get_irn_dbg_info(n);
3113                         ir_graph *irg   = current_ir_graph;
3114                         ir_node  *block = get_nodes_block(n);
3115                         ir_mode  *mode  = get_irn_mode(n);
3116                         n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3117                         return n;
3118                 }
3119         }
3120         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3121                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3122                         dbg_info *dbg   = get_irn_dbg_info(n);
3123                         ir_graph *irg   = current_ir_graph;
3124                         ir_node  *block = get_nodes_block(n);
3125                         ir_node  *add_l = get_Minus_op(a);
3126                         ir_node  *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3127                         n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3128                 } else if (is_Add(a)) {
3129                         ir_node *add_r = get_Add_right(a);
3130                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3131                                 /* ~(x + -1) = -x */
3132                                 ir_node *op = get_Add_left(a);
3133                                 ir_node *blk = get_irn_n(n, -1);
3134                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3135                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3136                         }
3137                 }
3138         }
3139         return n;
3140 }  /* transform_node_Not */
3141
3142 /**
3143  * Transform a Minus.
3144  * Optimize:
3145  *   -(~x) = x + 1
3146  *   -(a-b) = b - a
3147  */
3148 static ir_node *transform_node_Minus(ir_node *n) {
3149         ir_node *c, *oldn = n;
3150         ir_node *a = get_Minus_op(n);
3151         ir_mode *mode;
3152
3153         HANDLE_UNOP_PHI(tarval_neg,a,c);
3154
3155         mode = get_irn_mode(a);
3156         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3157                 /* the following rules are only to twos-complement */
3158                 if (is_Not(a)) {
3159                         /* -(~x) = x + 1 */
3160                         ir_node *op   = get_Not_op(a);
3161                         tarval *tv    = get_mode_one(mode);
3162                         ir_node *blk  = get_irn_n(n, -1);
3163                         ir_node *c    = new_r_Const(current_ir_graph, blk, mode, tv);
3164                         n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3165                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3166                         return n;
3167                 }
3168                 if (is_Shr(a)) {
3169                         ir_node *c = get_Shr_right(a);
3170
3171                         if (is_Const(c)) {
3172                                 tarval *tv = get_Const_tarval(c);
3173
3174                                 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3175                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3176                                         ir_node *v = get_Shr_left(a);
3177
3178                                         n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3179                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3180                                         return n;
3181                                 }
3182                         }
3183                 }
3184                 if (is_Shrs(a)) {
3185                         ir_node *c = get_Shrs_right(a);
3186
3187                         if (is_Const(c)) {
3188                                 tarval *tv = get_Const_tarval(c);
3189
3190                                 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3191                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3192                                         ir_node *v = get_Shrs_left(a);
3193
3194                                         n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3195                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3196                                         return n;
3197                                 }
3198                         }
3199                 }
3200         }
3201         if (is_Sub(a)) {
3202                 /* - (a-b) = b - a */
3203                 ir_node *la  = get_Sub_left(a);
3204                 ir_node *ra  = get_Sub_right(a);
3205                 ir_node *blk = get_irn_n(n, -1);
3206
3207                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3208                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3209                 return n;
3210         }
3211
3212         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3213                 ir_node *mul_l = get_Mul_left(a);
3214                 ir_node *mul_r = get_Mul_right(a);
3215                 if (is_Const(mul_r)) {
3216                         tarval   *tv    = tarval_neg(get_Const_tarval(mul_r));
3217                         ir_node  *cnst  = new_Const(mode, tv);
3218                         dbg_info *dbg   = get_irn_dbg_info(a);
3219                         ir_graph *irg   = current_ir_graph;
3220                         ir_node  *block = get_nodes_block(a);
3221                         n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3222                         return n;
3223                 }
3224         }
3225
3226         return n;
3227 }  /* transform_node_Minus */
3228
3229 /**
3230  * Transform a Cast_type(Const) into a new Const_type
3231  */
3232 static ir_node *transform_node_Cast(ir_node *n) {
3233         ir_node *oldn = n;
3234         ir_node *pred = get_Cast_op(n);
3235         ir_type *tp = get_irn_type(n);
3236
3237         if (is_Const(pred) && get_Const_type(pred) != tp) {
3238                 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3239                         get_Const_tarval(pred), tp);
3240                 DBG_OPT_CSTEVAL(oldn, n);
3241         } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3242                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3243                         get_SymConst_kind(pred), tp);
3244                 DBG_OPT_CSTEVAL(oldn, n);
3245         }
3246
3247         return n;
3248 }  /* transform_node_Cast */
3249
3250 /**
3251  * Transform a Proj(Div) with a non-zero value.
3252  * Removes the exceptions and routes the memory to the NoMem node.
3253  */
3254 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3255         ir_node *div = get_Proj_pred(proj);
3256         ir_node *b   = get_Div_right(div);
3257         ir_node *confirm, *res, *new_mem;
3258         long proj_nr;
3259
3260         if (value_not_zero(b, &confirm)) {
3261                 /* div(x, y) && y != 0 */
3262                 if (confirm == NULL) {
3263                         /* we are sure we have a Const != 0 */
3264                         new_mem = get_Div_mem(div);
3265                         if (is_Pin(new_mem))
3266                                 new_mem = get_Pin_op(new_mem);
3267                         set_Div_mem(div, new_mem);
3268                         set_irn_pinned(div, op_pin_state_floats);
3269                 }
3270
3271                 proj_nr = get_Proj_proj(proj);
3272                 switch (proj_nr) {
3273                 case pn_Div_X_regular:
3274                         return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3275
3276                 case pn_Div_X_except:
3277                         /* we found an exception handler, remove it */
3278                         DBG_OPT_EXC_REM(proj);
3279                         return new_Bad();
3280
3281                 case pn_Div_M:
3282                         res = get_Div_mem(div);
3283                         new_mem = get_irg_no_mem(current_ir_graph);
3284
3285                         if (confirm) {
3286                                 /* This node can only float up to the Confirm block */
3287                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3288                         }
3289                         set_irn_pinned(div, op_pin_state_floats);
3290                         /* this is a Div without exception, we can remove the memory edge */
3291                         set_Div_mem(div, new_mem);
3292                         return res;
3293                 }
3294         }
3295         return proj;
3296 }  /* transform_node_Proj_Div */
3297
3298 /**
3299  * Transform a Proj(Mod) with a non-zero value.
3300  * Removes the exceptions and routes the memory to the NoMem node.
3301  */
3302 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3303         ir_node *mod = get_Proj_pred(proj);
3304         ir_node *b   = get_Mod_right(mod);
3305         ir_node *confirm, *res, *new_mem;
3306         long proj_nr;
3307
3308         if (value_not_zero(b, &confirm)) {
3309                 /* mod(x, y) && y != 0 */
3310                 proj_nr = get_Proj_proj(proj);
3311
3312                 if (confirm == NULL) {
3313                         /* we are sure we have a Const != 0 */
3314                         new_mem = get_Mod_mem(mod);
3315                         if (is_Pin(new_mem))
3316                                 new_mem = get_Pin_op(new_mem);
3317                         set_Mod_mem(mod, new_mem);
3318                         set_irn_pinned(mod, op_pin_state_floats);
3319                 }
3320
3321                 switch (proj_nr) {
3322
3323                 case pn_Mod_X_regular:
3324                         return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3325
3326                 case pn_Mod_X_except:
3327                         /* we found an exception handler, remove it */
3328                         DBG_OPT_EXC_REM(proj);
3329                         return new_Bad();
3330
3331                 case pn_Mod_M:
3332                         res = get_Mod_mem(mod);
3333                         new_mem = get_irg_no_mem(current_ir_graph);
3334
3335                         if (confirm) {
3336                                 /* This node can only float up to the Confirm block */
3337                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3338                         }
3339                         /* this is a Mod without exception, we can remove the memory edge */
3340                         set_Mod_mem(mod, new_mem);
3341                         return res;
3342                 case pn_Mod_res:
3343                         if (get_Mod_left(mod) == b) {
3344                                 /* a % a = 0 if a != 0 */
3345                                 ir_mode *mode = get_irn_mode(proj);
3346                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3347
3348                                 DBG_OPT_CSTEVAL(mod, res);
3349                                 return res;
3350                         }
3351                 }
3352         }
3353         return proj;
3354 }  /* transform_node_Proj_Mod */
3355
3356 /**
3357  * Transform a Proj(DivMod) with a non-zero value.
3358  * Removes the exceptions and routes the memory to the NoMem node.
3359  */
3360 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3361         ir_node *divmod = get_Proj_pred(proj);
3362         ir_node *b      = get_DivMod_right(divmod);
3363         ir_node *confirm, *res, *new_mem;
3364         long proj_nr;
3365
3366         if (value_not_zero(b, &confirm)) {
3367                 /* DivMod(x, y) && y != 0 */
3368                 proj_nr = get_Proj_proj(proj);
3369
3370                 if (confirm == NULL) {
3371                         /* we are sure we have a Const != 0 */
3372                         new_mem = get_DivMod_mem(divmod);
3373                         if (is_Pin(new_mem))
3374                                 new_mem = get_Pin_op(new_mem);
3375                         set_DivMod_mem(divmod, new_mem);
3376                         set_irn_pinned(divmod, op_pin_state_floats);
3377                 }
3378
3379                 switch (proj_nr) {
3380
3381                 case pn_DivMod_X_regular:
3382                         return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3383
3384                 case pn_DivMod_X_except:
3385                         /* we found an exception handler, remove it */
3386                         DBG_OPT_EXC_REM(proj);
3387                         return new_Bad();
3388
3389                 case pn_DivMod_M:
3390                         res = get_DivMod_mem(divmod);
3391                         new_mem = get_irg_no_mem(current_ir_graph);
3392
3393                         if (confirm) {
3394                                 /* This node can only float up to the Confirm block */
3395                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3396                         }
3397                         /* this is a DivMod without exception, we can remove the memory edge */
3398                         set_DivMod_mem(divmod, new_mem);
3399                         return res;
3400
3401                 case pn_DivMod_res_mod:
3402                         if (get_DivMod_left(divmod) == b) {
3403                                 /* a % a = 0 if a != 0 */
3404                                 ir_mode *mode = get_irn_mode(proj);
3405                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3406
3407                                 DBG_OPT_CSTEVAL(divmod, res);
3408                                 return res;
3409                         }
3410                 }
3411         }
3412         return proj;
3413 }  /* transform_node_Proj_DivMod */
3414
3415 /**
3416  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3417  */
3418 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3419         if (get_opt_unreachable_code()) {
3420                 ir_node *n = get_Proj_pred(proj);
3421                 ir_node *b = get_Cond_selector(n);
3422
3423                 if (mode_is_int(get_irn_mode(b))) {
3424                         tarval *tb = value_of(b);
3425
3426                         if (tb != tarval_bad) {
3427                                 /* we have a constant switch */
3428                                 long num = get_Proj_proj(proj);
3429
3430                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3431                                         if (get_tarval_long(tb) == num) {
3432                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
3433                                                  * in a block. This case is optimized away in optimize_cf(). */
3434                                                 return proj;
3435                                         } else {
3436                                                 /* this case will NEVER be taken, kill it */
3437                                                 return new_Bad();
3438                                         }
3439                                 }
3440                         }
3441                 }
3442         }
3443         return proj;
3444 }  /* transform_node_Proj_Cond */
3445
3446 /**
3447  * Normalizes and optimizes Cmp nodes.
3448  */
3449 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3450         ir_node *n     = get_Proj_pred(proj);
3451         ir_node *left  = get_Cmp_left(n);
3452         ir_node *right = get_Cmp_right(n);
3453         ir_node *c     = NULL;
3454         tarval *tv     = NULL;
3455         int changed    = 0;
3456         ir_mode *mode  = NULL;
3457         long proj_nr   = get_Proj_proj(proj);
3458
3459         /* we can evaluate this direct */
3460         switch (proj_nr) {
3461         case pn_Cmp_False:
3462                 return new_Const(mode_b, get_tarval_b_false());
3463         case pn_Cmp_True:
3464                 return new_Const(mode_b, get_tarval_b_true());
3465         case pn_Cmp_Leg:
3466                 if(!mode_is_float(get_irn_mode(left)))
3467                         return new_Const(mode_b, get_tarval_b_true());
3468                 break;
3469         default:
3470                 break;
3471         }
3472
3473         /* remove Casts */
3474         if (is_Cast(left))
3475                 left = get_Cast_op(left);
3476         if (is_Cast(right))
3477                 right = get_Cast_op(right);
3478
3479         /* Remove unnecessary conversions */
3480         /* TODO handle constants */
3481         if (is_Conv(left) && is_Conv(right)) {
3482                 ir_mode *mode        = get_irn_mode(left);
3483                 ir_node *op_left     = get_Conv_op(left);
3484                 ir_node *op_right    = get_Conv_op(right);
3485                 ir_mode *mode_left   = get_irn_mode(op_left);
3486                 ir_mode *mode_right  = get_irn_mode(op_right);
3487
3488                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3489                         ir_graph *irg   = current_ir_graph;
3490                         ir_node  *block = get_nodes_block(n);
3491
3492                         if (mode_left == mode_right) {
3493                                 left  = op_left;
3494                                 right = op_right;
3495                                 changed |= 1;
3496                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3497                         } else if (smaller_mode(mode_left, mode_right)) {
3498                                 left  = new_r_Conv(irg, block, op_left, mode_right);
3499                                 right = op_right;
3500                                 changed |= 1;
3501                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3502                         } else if (smaller_mode(mode_right, mode_left)) {
3503                                 left  = op_left;
3504                                 right = new_r_Conv(irg, block, op_right, mode_left);
3505                                 changed |= 1;
3506                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3507                         }
3508                 }
3509         }
3510
3511         /* remove operation of both sides if possible */
3512         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3513                 ir_opcode lop = get_irn_opcode(left);
3514
3515                 if (lop == get_irn_opcode(right)) {
3516                         ir_node *ll, *lr, *rl, *rr;
3517
3518                         /* same operation on both sides, try to remove */
3519                         switch (lop) {
3520                         case iro_Not:
3521                         case iro_Minus:
3522                                 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3523                                 left  = get_unop_op(left);
3524                                 right = get_unop_op(right);
3525                                 changed |= 1;
3526                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3527                                 break;
3528                         case iro_Add:
3529                                 ll = get_Add_left(left);
3530                                 lr = get_Add_right(left);
3531                                 rl = get_Add_left(right);
3532                                 rr = get_Add_right(right);
3533
3534                                 if (ll == rl) {
3535                                         /* X + a CMP X + b ==> a CMP b */
3536                                         left  = lr;
3537                                         right = rr;
3538                                         changed |= 1;
3539                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3540                                 } else if (ll == rr) {
3541                                         /* X + a CMP b + X ==> a CMP b */
3542                                         left  = lr;
3543                                         right = rl;
3544                                         changed |= 1;
3545                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3546                                 } else if (lr == rl) {
3547                                         /* a + X CMP X + b ==> a CMP b */
3548                                         left  = ll;
3549                                         right = rr;
3550                                         changed |= 1;
3551                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3552                                 } else if (lr == rr) {
3553                                         /* a + X CMP b + X ==> a CMP b */
3554                                         left  = ll;
3555                                         right = rl;
3556                                         changed |= 1;
3557                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3558                                 }
3559                                 break;
3560                         case iro_Sub:
3561                                 ll = get_Sub_left(left);
3562                                 lr = get_Sub_right(left);
3563                                 rl = get_Sub_left(right);
3564                                 rr = get_Sub_right(right);
3565
3566                                 if (ll == rl) {
3567                                         /* X - a CMP X - b ==> a CMP b */
3568                                         left  = lr;
3569                                         right = rr;
3570                                         changed |= 1;
3571                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3572                                 } else if (lr == rr) {
3573                                         /* a - X CMP b - X ==> a CMP b */
3574                                         left  = ll;
3575                                         right = rl;
3576                                         changed |= 1;
3577                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3578                                 }
3579                                 break;
3580                         case iro_Rot:
3581                                 if (get_Rot_right(left) == get_Rot_right(right)) {
3582                                         /* a ROT X CMP b ROT X */
3583                                         left  = get_Rot_left(left);
3584                                         right = get_Rot_left(right);
3585                                         changed |= 1;
3586                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3587                                 }
3588                                 break;
3589                         default:
3590                                 break;
3591                         }
3592                 }
3593         }
3594
3595         if (get_irn_mode(left) == mode_b) {
3596                 ir_graph *irg   = current_ir_graph;
3597                 ir_node  *block = get_nodes_block(n);
3598                 ir_node  *bres;
3599
3600                 switch (proj_nr) {
3601                         case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3602                         case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3603                         case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3604                         case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3605                         case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3606                         case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3607                         default: bres = NULL;
3608                 }
3609                 if (bres) {
3610                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3611                         return bres;
3612                 }
3613         }
3614
3615         if (!get_opt_reassociation())
3616                 return proj;
3617
3618         /*
3619          * First step: normalize the compare op
3620          * by placing the constant on the right side
3621          * or moving the lower address node to the left.
3622          * We ignore the case that both are constants
3623          * this case should be optimized away.
3624          */
3625         if (is_Const(right)) {
3626                 c = right;
3627         } else if (is_Const(left)) {
3628                 c     = left;
3629                 left  = right;
3630                 right = c;
3631
3632                 proj_nr = get_inversed_pnc(proj_nr);
3633                 changed |= 1;
3634         } else if (get_irn_idx(left) > get_irn_idx(right)) {
3635                 ir_node *t = left;
3636
3637                 left  = right;
3638                 right = t;
3639
3640                 proj_nr = get_inversed_pnc(proj_nr);
3641                 changed |= 1;
3642         }
3643
3644         /*
3645          * Second step: Try to reduce the magnitude
3646          * of a constant. This may help to generate better code
3647          * later and may help to normalize more compares.
3648          * Of course this is only possible for integer values.
3649          */
3650         if (c) {
3651                 mode = get_irn_mode(c);
3652                 tv = get_Const_tarval(c);
3653
3654                 /* TODO extend to arbitrary constants */
3655                 if (is_Conv(left) && tarval_is_null(tv)) {
3656                         ir_node *op      = get_Conv_op(left);
3657                         ir_mode *op_mode = get_irn_mode(op);
3658
3659                         /*
3660                          * UpConv(x) REL 0  ==> x REL 0
3661                          */
3662                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3663                             ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3664                                  mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3665                                 tv   = get_mode_null(op_mode);
3666                                 left = op;
3667                                 mode = op_mode;
3668                                 changed |= 2;
3669                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3670                         }
3671                 }
3672
3673                 if (tv != tarval_bad) {
3674                         /* the following optimization is possible on modes without Overflow
3675                          * on Unary Minus or on == and !=:
3676                          * -a CMP c  ==>  a swap(CMP) -c
3677                          *
3678                          * Beware: for two-complement Overflow may occur, so only == and != can
3679                          * be optimized, see this:
3680                          * -MININT < 0 =/=> MININT > 0 !!!
3681                          */
3682                         if (is_Minus(left) &&
3683                                 (!mode_overflow_on_unary_Minus(mode) ||
3684                                 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3685                                 tv = tarval_neg(tv);
3686
3687                                 if (tv != tarval_bad) {
3688                                         left = get_Minus_op(left);
3689                                         proj_nr = get_inversed_pnc(proj_nr);
3690                                         changed |= 2;
3691                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3692                                 }
3693                         } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3694                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
3695                                 tv = tarval_not(tv);
3696
3697                                 if (tv != tarval_bad) {
3698                                         left = get_Not_op(left);
3699                                         changed |= 2;
3700                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3701                                 }
3702                         }
3703
3704                         /* for integer modes, we have more */
3705                         if (mode_is_int(mode)) {
3706                                 /* Ne includes Unordered which is not possible on integers.
3707                                  * However, frontends often use this wrong, so fix it here */
3708                                 if (proj_nr & pn_Cmp_Uo) {
3709                                         proj_nr &= ~pn_Cmp_Uo;
3710                                         set_Proj_proj(proj, proj_nr);
3711                                 }
3712
3713                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
3714                                 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3715                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3716                                         tv = tarval_sub(tv, get_mode_one(mode));
3717
3718                                         if (tv != tarval_bad) {
3719                                                 proj_nr ^= pn_Cmp_Eq;
3720                                                 changed |= 2;
3721                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3722                                         }
3723                                 }
3724                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
3725                                 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3726                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3727                                         tv = tarval_add(tv, get_mode_one(mode));
3728
3729                                         if (tv != tarval_bad) {
3730                                                 proj_nr ^= pn_Cmp_Eq;
3731                                                 changed |= 2;
3732                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3733                                         }
3734                                 }
3735
3736                                 /* the following reassociations work only for == and != */
3737                                 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3738
3739 #if 0 /* Might be not that good in general */
3740                                         /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
3741                                         if (tarval_is_null(tv) && is_Sub(left)) {
3742                                                 right = get_Sub_right(left);
3743                                                 left  = get_Sub_left(left);
3744
3745                                                 tv = value_of(right);
3746                                                 changed = 1;
3747                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3748                                         }
3749 #endif
3750
3751                                         if (tv != tarval_bad) {
3752                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
3753                                                 if (is_Sub(left)) {
3754                                                         ir_node *c1 = get_Sub_right(left);
3755                                                         tarval *tv2 = value_of(c1);
3756
3757                                                         if (tv2 != tarval_bad) {
3758                                                                 tv2 = tarval_add(tv, value_of(c1));
3759
3760                                                                 if (tv2 != tarval_bad) {
3761                                                                         left    = get_Sub_left(left);
3762                                                                         tv      = tv2;
3763                                                                         changed |= 2;
3764                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3765                                                                 }
3766                                                         }
3767                                                 }
3768                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
3769                                                 else if (is_Add(left)) {
3770                                                         ir_node *a_l = get_Add_left(left);
3771                                                         ir_node *a_r = get_Add_right(left);
3772                                                         ir_node *a;
3773                                                         tarval *tv2;
3774
3775                                                         if (is_Const(a_l)) {
3776                                                                 a = a_r;
3777                                                                 tv2 = value_of(a_l);
3778                                                         } else {
3779                                                                 a = a_l;
3780                                                                 tv2 = value_of(a_r);
3781                                                         }
3782
3783                                                         if (tv2 != tarval_bad) {
3784                                                                 tv2 = tarval_sub(tv, tv2);
3785
3786                                                                 if (tv2 != tarval_bad) {
3787                                                                         left    = a;
3788                                                                         tv      = tv2;
3789                                                                         changed |= 2;
3790                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3791                                                                 }
3792                                                         }
3793                                                 }
3794                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
3795                                                 else if (is_Minus(left)) {
3796                                                         tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3797
3798                                                         if (tv2 != tarval_bad) {
3799                                                                 left    = get_Minus_op(left);
3800                                                                 tv      = tv2;
3801                                                                 changed |= 2;
3802                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3803                                                         }
3804                                                 }
3805                                         }
3806                                 } /* == or != */
3807                                 /* the following reassociations work only for <= */
3808                                 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3809                                         if (tv != tarval_bad) {
3810                                                 /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
3811                                                 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
3812                                                 }
3813                                         }
3814                                 }
3815                         } /* mode_is_int */
3816
3817                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3818                                 switch (get_irn_opcode(left)) {
3819                                         ir_node *c1;
3820
3821                                 case iro_And:
3822                                         c1 = get_And_right(left);
3823                                         if (is_Const(c1)) {
3824                                                 /*
3825                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
3826                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
3827                                                  */
3828                                                 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
3829                                                 if (mask != tv) {
3830                                                         /* TODO: move to constant evaluation */
3831                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3832                                                         c1 = new_Const(mode_b, tv);
3833                                                         DBG_OPT_CSTEVAL(proj, c1);
3834                                                         return c1;
3835                                                 }
3836
3837                                                 if (tarval_is_single_bit(tv)) {
3838                                                         /*
3839                                                          * optimization for AND:
3840                                                          * Optimize:
3841                                                          *   And(x, C) == C  ==>  And(x, C) != 0
3842                                                          *   And(x, C) != C  ==>  And(X, C) == 0
3843                                                          *
3844                                                          * if C is a single Bit constant.
3845                                                          */
3846
3847                                                         /* check for Constant's match. We have check hare the tarvals,
3848                                                            because our const might be changed */
3849                                                         if (get_Const_tarval(c1) == tv) {
3850                                                                 /* fine: do the transformation */
3851                                                                 tv = get_mode_null(get_tarval_mode(tv));
3852                                                                 proj_nr ^= pn_Cmp_Leg;
3853                                                                 changed |= 2;
3854                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3855                                                         }
3856                                                 }
3857                                         }
3858                                         break;
3859                                 case iro_Or:
3860                                         c1 = get_Or_right(left);
3861                                         if (is_Const(c1) && tarval_is_null(tv)) {
3862                                                 /*
3863                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
3864                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
3865                                                  */
3866                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
3867                                                         /* TODO: move to constant evaluation */
3868                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3869                                                         c1 = new_Const(mode_b, tv);
3870                                                         DBG_OPT_CSTEVAL(proj, c1);
3871                                                         return c1;
3872                                                 }
3873                                         }
3874                                         break;
3875                                 case iro_Shl:
3876                                         /*
3877                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
3878                                          *                             FALSE                       else
3879                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
3880                                          *                             TRUE                        else
3881                                          */
3882                                         c1 = get_Shl_right(left);
3883                                         if (is_Const(c1)) {
3884                                                 tarval  *tv1    = get_Const_tarval(c1);
3885                                                 ir_mode *mode   = get_irn_mode(left);
3886                                                 tarval  *minus1 = get_mode_all_one(mode);
3887                                                 tarval  *amask  = tarval_shr(minus1, tv1);
3888                                                 tarval  *cmask  = tarval_shl(minus1, tv1);
3889                                                 ir_node *sl, *blk;
3890
3891                                                 if (tarval_and(tv, cmask) != tv) {
3892                                                         /* condition not met */
3893                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3894                                                         c1 = new_Const(mode_b, tv);
3895                                                         DBG_OPT_CSTEVAL(proj, c1);
3896                                                         return c1;
3897                                                 }
3898                                                 sl   = get_Shl_left(left);
3899                                                 blk  = get_nodes_block(n);
3900                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3901                                                 tv   = tarval_shr(tv, tv1);
3902                                                 changed |= 2;
3903                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
3904                                         }
3905                                         break;
3906                                 case iro_Shr:
3907                                         /*
3908                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
3909                                          *                             FALSE                       else
3910                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
3911                                          *                             TRUE                        else
3912                                          */
3913                                         c1 = get_Shr_right(left);
3914                                         if (is_Const(c1)) {
3915                                                 tarval  *tv1    = get_Const_tarval(c1);
3916                                                 ir_mode *mode   = get_irn_mode(left);
3917                                                 tarval  *minus1 = get_mode_all_one(mode);
3918                                                 tarval  *amask  = tarval_shl(minus1, tv1);
3919                                                 tarval  *cmask  = tarval_shr(minus1, tv1);
3920                                                 ir_node *sl, *blk;
3921
3922                                                 if (tarval_and(tv, cmask) != tv) {
3923                                                         /* condition not met */
3924                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3925                                                         c1 = new_Const(mode_b, tv);
3926                                                         DBG_OPT_CSTEVAL(proj, c1);
3927                                                         return c1;
3928                                                 }
3929                                                 sl   = get_Shr_left(left);
3930                                                 blk  = get_nodes_block(n);
3931                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3932                                                 tv   = tarval_shl(tv, tv1);
3933                                                 changed |= 2;
3934                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
3935                                         }
3936                                         break;
3937                                 case iro_Shrs:
3938                                         /*
3939                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
3940                                          *                             FALSE                       else
3941                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
3942                                          *                             TRUE                        else
3943                                          */
3944                                         c1 = get_Shrs_right(left);
3945                                         if (is_Const(c1)) {
3946                                                 tarval  *tv1    = get_Const_tarval(c1);
3947                                                 ir_mode *mode   = get_irn_mode(left);
3948                                                 tarval  *minus1 = get_mode_all_one(mode);
3949                                                 tarval  *amask  = tarval_shl(minus1, tv1);
3950                                                 tarval  *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
3951                                                 ir_node *sl, *blk;
3952
3953                                                 cond = tarval_sub(cond, tv1);
3954                                                 cond = tarval_shrs(tv, cond);
3955
3956                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
3957                                                         /* condition not met */
3958                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
3959                                                         c1 = new_Const(mode_b, tv);
3960                                                         DBG_OPT_CSTEVAL(proj, c1);
3961                                                         return c1;
3962                                                 }
3963                                                 sl   = get_Shrs_left(left);
3964                                                 blk  = get_nodes_block(n);
3965                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
3966                                                 tv   = tarval_shl(tv, tv1);
3967                                                 changed |= 2;
3968                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
3969                                         }
3970                                         break;
3971                                 }  /* switch */
3972                         }
3973                 } /* tarval != bad */
3974         }
3975
3976         if (changed & 2)      /* need a new Const */
3977                 right = new_Const(mode, tv);
3978
3979         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
3980                 ir_node *op = get_Proj_pred(left);
3981
3982                 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
3983                     (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
3984                         ir_node *c = get_binop_right(op);
3985
3986                         if (is_Const(c)) {
3987                                 tarval *tv = get_Const_tarval(c);
3988
3989                                 if (tarval_is_single_bit(tv)) {
3990                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
3991                                         ir_node *v    = get_binop_left(op);
3992                                         ir_node *blk  = get_irn_n(op, -1);
3993                                         ir_mode *mode = get_irn_mode(v);
3994
3995                                         tv = tarval_sub(tv, get_mode_one(mode));
3996                                         left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
3997                                         changed |= 1;
3998                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
3999                                 }
4000                         }
4001                 }
4002         }
4003
4004         if (changed) {
4005                 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4006
4007                 /* create a new compare */
4008                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4009
4010                 set_Proj_pred(proj, n);
4011                 set_Proj_proj(proj, proj_nr);
4012         }
4013
4014         return proj;
4015 }  /* transform_node_Proj_Cmp */
4016
4017 /**
4018  * Does all optimizations on nodes that must be done on it's Proj's
4019  * because of creating new nodes.
4020  */
4021 static ir_node *transform_node_Proj(ir_node *proj) {
4022         ir_node *n = get_Proj_pred(proj);
4023
4024         switch (get_irn_opcode(n)) {
4025         case iro_Div:
4026                 return transform_node_Proj_Div(proj);
4027
4028         case iro_Mod:
4029                 return transform_node_Proj_Mod(proj);
4030
4031         case iro_DivMod:
4032                 return transform_node_Proj_DivMod(proj);
4033
4034         case iro_Cond:
4035                 return transform_node_Proj_Cond(proj);
4036
4037         case iro_Cmp:
4038                 return transform_node_Proj_Cmp(proj);
4039
4040         case iro_Tuple:
4041                 /* should not happen, but if it does will be optimized away */
4042                 return equivalent_node_Proj(proj);
4043
4044         default:
4045                 /* do nothing */
4046                 return proj;
4047         }
4048 }  /* transform_node_Proj */
4049
4050 /**
4051  * Move Confirms down through Phi nodes.
4052  */
4053 static ir_node *transform_node_Phi(ir_node *phi) {
4054         int i, n;
4055         ir_mode *mode = get_irn_mode(phi);
4056
4057         if (mode_is_reference(mode)) {
4058                 n = get_irn_arity(phi);
4059
4060                 /* Beware of Phi0 */
4061                 if (n > 0) {
4062                         ir_node *pred = get_irn_n(phi, 0);
4063                         ir_node *bound, *new_Phi, *block, **in;
4064                         pn_Cmp  pnc;
4065
4066                         if (! is_Confirm(pred))
4067                                 return phi;
4068
4069                         bound = get_Confirm_bound(pred);
4070                         pnc   = get_Confirm_cmp(pred);
4071
4072                         NEW_ARR_A(ir_node *, in, n);
4073                         in[0] = get_Confirm_value(pred);
4074
4075                         for (i = 1; i < n; ++i) {
4076                                 pred = get_irn_n(phi, i);
4077
4078                                 if (! is_Confirm(pred) ||
4079                                         get_Confirm_bound(pred) != bound ||
4080                                         get_Confirm_cmp(pred) != pnc)
4081                                         return phi;
4082                                 in[i] = get_Confirm_value(pred);
4083                         }
4084                         /* move the Confirm nodes "behind" the Phi */
4085                         block = get_irn_n(phi, -1);
4086                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4087                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4088                 }
4089         }
4090         return phi;
4091 }  /* transform_node_Phi */
4092
4093 /**
4094  * Returns the operands of a commutative bin-op, if one operand is
4095  * a const, it is returned as the second one.
4096  */
4097 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4098         ir_node *op_a = get_binop_left(binop);
4099         ir_node *op_b = get_binop_right(binop);
4100
4101         assert(is_op_commutative(get_irn_op(binop)));
4102
4103         if (is_Const(op_a)) {
4104                 *a = op_b;
4105                 *c = op_a;
4106         } else {
4107                 *a = op_a;
4108                 *c = op_b;
4109         }
4110 }  /* get_comm_Binop_Ops */
4111
4112 /**
4113  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4114  * Such pattern may arise in bitfield stores.
4115  *
4116  * value  c4                  value      c4 & c2
4117  *    AND     c3                    AND           c1 | c3
4118  *        OR     c2      ===>               OR
4119  *           AND    c1
4120  *               OR
4121  *
4122  *
4123  * value  c2                 value  c1
4124  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
4125  *        OR
4126  */
4127 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4128         ir_node *and, *c1;
4129         ir_node *or_l, *c2;
4130         ir_node *and_l, *c3;
4131         ir_node *value, *c4;
4132         ir_node *new_and, *new_const, *block;
4133         ir_mode *mode = get_irn_mode(or);
4134
4135         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4136
4137         while (1) {
4138                 get_comm_Binop_Ops(or, &and, &c1);
4139                 if (!is_Const(c1) || !is_And(and))
4140                         return or;
4141
4142                 get_comm_Binop_Ops(and, &or_l, &c2);
4143                 if (!is_Const(c2))
4144                         return or;
4145
4146                 tv1 = get_Const_tarval(c1);
4147                 tv2 = get_Const_tarval(c2);
4148
4149                 tv = tarval_or(tv1, tv2);
4150                 if (tarval_is_all_one(tv)) {
4151                         /* the AND does NOT clear a bit with isn't set by the OR */
4152                         set_Or_left(or, or_l);
4153                         set_Or_right(or, c1);
4154
4155                         /* check for more */
4156                         continue;
4157                 }
4158
4159                 if (!is_Or(or_l))
4160                         return or;
4161
4162                 get_comm_Binop_Ops(or_l, &and_l, &c3);
4163                 if (!is_Const(c3) || !is_And(and_l))
4164                         return or;
4165
4166                 get_comm_Binop_Ops(and_l, &value, &c4);
4167                 if (!is_Const(c4))
4168                         return or;
4169
4170                 /* ok, found the pattern, check for conditions */
4171                 assert(mode == get_irn_mode(and));
4172                 assert(mode == get_irn_mode(or_l));
4173                 assert(mode == get_irn_mode(and_l));
4174
4175                 tv3 = get_Const_tarval(c3);
4176                 tv4 = get_Const_tarval(c4);
4177
4178                 tv = tarval_or(tv4, tv2);
4179                 if (!tarval_is_all_one(tv)) {
4180                         /* have at least one 0 at the same bit position */
4181                         return or;
4182                 }
4183
4184                 n_tv4 = tarval_not(tv4);
4185                 if (tv3 != tarval_and(tv3, n_tv4)) {
4186                         /* bit in the or_mask is outside the and_mask */
4187                         return or;
4188                 }
4189
4190                 n_tv2 = tarval_not(tv2);
4191                 if (tv1 != tarval_and(tv1, n_tv2)) {
4192                         /* bit in the or_mask is outside the and_mask */
4193                         return or;
4194                 }
4195
4196                 /* ok, all conditions met */
4197                 block = get_irn_n(or, -1);
4198
4199                 new_and = new_r_And(current_ir_graph, block,
4200                         value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4201
4202                 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4203
4204                 set_Or_left(or, new_and);
4205                 set_Or_right(or, new_const);
4206
4207                 /* check for more */
4208         }
4209 }  /* transform_node_Or_bf_store */
4210
4211 /**
4212  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4213  */
4214 static ir_node *transform_node_Or_Rot(ir_node *or) {
4215         ir_mode *mode = get_irn_mode(or);
4216         ir_node *shl, *shr, *block;
4217         ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4218         tarval *tv1, *tv2;
4219
4220         if (! mode_is_int(mode))
4221                 return or;
4222
4223         shl = get_binop_left(or);
4224         shr = get_binop_right(or);
4225
4226         if (is_Shr(shl)) {
4227                 if (!is_Shl(shr))
4228                         return or;
4229
4230                 irn = shl;
4231                 shl = shr;
4232                 shr = irn;
4233         } else if (!is_Shl(shl)) {
4234                 return or;
4235         } else if (!is_Shr(shr)) {
4236                 return or;
4237         }
4238         x = get_Shl_left(shl);
4239         if (x != get_Shr_left(shr))
4240                 return or;
4241
4242         c1 = get_Shl_right(shl);
4243         c2 = get_Shr_right(shr);
4244         if (is_Const(c1) && is_Const(c2)) {
4245                 tv1 = get_Const_tarval(c1);
4246                 if (! tarval_is_long(tv1))
4247                         return or;
4248
4249                 tv2 = get_Const_tarval(c2);
4250                 if (! tarval_is_long(tv2))
4251                         return or;
4252
4253                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4254                         != get_mode_size_bits(mode))
4255                         return or;
4256
4257                 /* yet, condition met */
4258                 block = get_irn_n(or, -1);
4259
4260                 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4261
4262                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4263                 return n;
4264         } else if (is_Sub(c1)) {
4265                 v   = c2;
4266                 sub = c1;
4267
4268                 if (get_Sub_right(sub) != v)
4269                         return or;
4270
4271                 c1 = get_Sub_left(sub);
4272                 if (!is_Const(c1))
4273                         return or;
4274
4275                 tv1 = get_Const_tarval(c1);
4276                 if (! tarval_is_long(tv1))
4277                         return or;
4278
4279                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4280                         return or;
4281
4282                 /* yet, condition met */
4283                 block = get_nodes_block(or);
4284
4285                 /* a Rot right is not supported, so use a rot left */
4286                 n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
4287
4288                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4289                 return n;
4290         } else if (is_Sub(c2)) {
4291                 v   = c1;
4292                 sub = c2;
4293
4294                 c1 = get_Sub_left(sub);
4295                 if (!is_Const(c1))
4296                         return or;
4297
4298                 tv1 = get_Const_tarval(c1);
4299                 if (! tarval_is_long(tv1))
4300                         return or;
4301
4302                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
4303                         return or;
4304
4305                 /* yet, condition met */
4306                 block = get_irn_n(or, -1);
4307
4308                 /* a Rot Left */
4309                 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4310
4311                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4312                 return n;
4313         }
4314
4315         return or;
4316 }  /* transform_node_Or_Rot */
4317
4318 /**
4319  * Transform an Or.
4320  */
4321 static ir_node *transform_node_Or(ir_node *n) {
4322         ir_node *c, *oldn = n;
4323         ir_node *a = get_Or_left(n);
4324         ir_node *b = get_Or_right(n);
4325
4326         if (is_Not(a) && is_Not(b)) {
4327                 /* ~a | ~b = ~(a&b) */
4328                 ir_node *block = get_nodes_block(n);
4329                 ir_mode *mode = get_irn_mode(n);
4330
4331                 a = get_Not_op(a);
4332                 b = get_Not_op(b);
4333                 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4334                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4335                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4336                 return n;
4337         }
4338
4339         /* we can evaluate 2 Projs of the same Cmp */
4340         if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4341                 ir_node *pred_a = get_Proj_pred(a);
4342                 ir_node *pred_b = get_Proj_pred(b);
4343                 if (pred_a == pred_b) {
4344                         dbg_info *dbgi  = get_irn_dbg_info(n);
4345                         ir_node  *block = get_nodes_block(pred_a);
4346                         pn_Cmp pn_a     = get_Proj_proj(a);
4347                         pn_Cmp pn_b     = get_Proj_proj(b);
4348                         /* yes, we can simply calculate with pncs */
4349                         pn_Cmp new_pnc  = pn_a | pn_b;
4350
4351                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4352                                            new_pnc);
4353                 }
4354         }
4355
4356         HANDLE_BINOP_PHI(tarval_or, a,b,c);
4357
4358         n = transform_node_Or_bf_store(n);
4359         n = transform_node_Or_Rot(n);
4360         if (n != oldn)
4361                 return n;
4362
4363         n = transform_bitwise_distributive(n, transform_node_Or);
4364
4365         return n;
4366 }  /* transform_node_Or */
4367
4368
4369 /* forward */
4370 static ir_node *transform_node(ir_node *n);
4371
4372 /**
4373  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
4374  *
4375  * Should be moved to reassociation?
4376  */
4377 static ir_node *transform_node_shift(ir_node *n) {
4378         ir_node *left, *right;
4379         tarval *tv1, *tv2, *res;
4380         ir_mode *mode;
4381         int modulo_shf, flag;
4382
4383         left = get_binop_left(n);
4384
4385         /* different operations */
4386         if (get_irn_op(left) != get_irn_op(n))
4387                 return n;
4388
4389         right = get_binop_right(n);
4390         tv1 = value_of(right);
4391         if (tv1 == tarval_bad)
4392                 return n;
4393
4394         tv2 = value_of(get_binop_right(left));
4395         if (tv2 == tarval_bad)
4396                 return n;
4397
4398         res = tarval_add(tv1, tv2);
4399
4400         /* beware: a simple replacement works only, if res < modulo shift */
4401         mode = get_irn_mode(n);
4402
4403         flag = 0;
4404
4405         modulo_shf = get_mode_modulo_shift(mode);
4406         if (modulo_shf > 0) {
4407                 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4408
4409                 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4410                         flag = 1;
4411         } else
4412                 flag = 1;
4413
4414         if (flag) {
4415                 /* ok, we can replace it */
4416                 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4417
4418                 in[0] = get_binop_left(left);
4419                 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4420
4421                 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4422
4423                 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4424
4425                 return transform_node(irn);
4426         }
4427         return n;
4428 }  /* transform_node_shift */
4429
4430 /**
4431  * Transform a Shr.
4432  */
4433 static ir_node *transform_node_Shr(ir_node *n) {
4434         ir_node *c, *oldn = n;
4435         ir_node *a = get_Shr_left(n);
4436         ir_node *b = get_Shr_right(n);
4437
4438         HANDLE_BINOP_PHI(tarval_shr, a, b, c);
4439         return transform_node_shift(n);
4440 }  /* transform_node_Shr */
4441
4442 /**
4443  * Transform a Shrs.
4444  */
4445 static ir_node *transform_node_Shrs(ir_node *n) {
4446         ir_node *c, *oldn = n;
4447         ir_node *a = get_Shrs_left(n);
4448         ir_node *b = get_Shrs_right(n);
4449
4450         HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
4451         return transform_node_shift(n);
4452 }  /* transform_node_Shrs */
4453
4454 /**
4455  * Transform a Shl.
4456  */
4457 static ir_node *transform_node_Shl(ir_node *n) {
4458         ir_node *c, *oldn = n;
4459         ir_node *a = get_Shl_left(n);
4460         ir_node *b = get_Shl_right(n);
4461
4462         HANDLE_BINOP_PHI(tarval_shl, a, b, c);
4463         return transform_node_shift(n);
4464 }  /* transform_node_Shl */
4465
4466 /**
4467  * Remove dead blocks and nodes in dead blocks
4468  * in keep alive list.  We do not generate a new End node.
4469  */
4470 static ir_node *transform_node_End(ir_node *n) {
4471         int i, j, n_keepalives = get_End_n_keepalives(n);
4472         ir_node **in;
4473
4474         NEW_ARR_A(ir_node *, in, n_keepalives);
4475
4476         for (i = j = 0; i < n_keepalives; ++i) {
4477                 ir_node *ka = get_End_keepalive(n, i);
4478                 if (is_Block(ka)) {
4479                         if (! is_Block_dead(ka)) {
4480                                 in[j++] = ka;
4481                         }
4482                         continue;
4483                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4484                         continue;
4485                 }
4486                 /* FIXME: beabi need to keep a Proj(M) */
4487                 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4488                         in[j++] = ka;
4489         }
4490         if (j != n_keepalives)
4491                 set_End_keepalives(n, j, in);
4492         return n;
4493 }  /* transform_node_End */
4494
4495 /** returns 1 if a == -b */
4496 static int is_negated_value(ir_node *a, ir_node *b) {
4497         if(is_Minus(a) && get_Minus_op(a) == b)
4498                 return 1;
4499         if(is_Minus(b) && get_Minus_op(b) == a)
4500                 return 1;
4501         if(is_Sub(a) && is_Sub(b)) {
4502                 ir_node *a_left  = get_Sub_left(a);
4503                 ir_node *a_right = get_Sub_right(a);
4504                 ir_node *b_left  = get_Sub_left(b);
4505                 ir_node *b_right = get_Sub_right(b);
4506
4507                 if(a_left == b_right && a_right == b_left)
4508                         return 1;
4509         }
4510
4511         return 0;
4512 }
4513
4514 /**
4515  * Optimize a Mux into some simpler cases.
4516  */
4517 static ir_node *transform_node_Mux(ir_node *n) {
4518         ir_node *oldn = n, *sel = get_Mux_sel(n);
4519         ir_mode *mode = get_irn_mode(n);
4520
4521         if (mode == mode_b) {
4522                 ir_node  *t     = get_Mux_true(n);
4523                 ir_node  *f     = get_Mux_false(n);
4524                 dbg_info *dbg   = get_irn_dbg_info(n);
4525                 ir_node  *block = get_irn_n(n, -1);
4526                 ir_graph *irg   = current_ir_graph;
4527
4528                 if (is_Const(t)) {
4529                         tarval *tv_t = get_Const_tarval(t);
4530                         if (tv_t == tarval_b_true) {
4531                                 if (is_Const(f)) {
4532                                         assert(get_Const_tarval(f) == tarval_b_false);
4533                                         return sel;
4534                                 } else {
4535                                         return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4536                                 }
4537                         } else {
4538                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4539                                 assert(tv_t == tarval_b_false);
4540                                 if (is_Const(f)) {
4541                                         assert(get_Const_tarval(f) == tarval_b_true);
4542                                         return not_sel;
4543                                 } else {
4544                                         return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4545                                 }
4546                         }
4547                 } else if (is_Const(f)) {
4548                         tarval *tv_f = get_Const_tarval(f);
4549                         if (tv_f == tarval_b_true) {
4550                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4551                                 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4552                         } else {
4553                                 assert(tv_f == tarval_b_false);
4554                                 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4555                         }
4556                 }
4557         }
4558
4559         if (is_Proj(sel) && !mode_honor_signed_zeros(mode)) {
4560                 ir_node *cmp = get_Proj_pred(sel);
4561                 long     pn  = get_Proj_proj(sel);
4562                 ir_node *f   = get_Mux_false(n);
4563                 ir_node *t   = get_Mux_true(n);
4564
4565                 /*
4566                  * Note: normalization puts the constant on the right side,
4567                  * so we check only one case.
4568                  *
4569                  * Note further that these optimization work even for floating point
4570                  * with NaN's because -NaN == NaN.
4571                  * However, if +0 and -0 is handled differently, we cannot use the first
4572                  * one.
4573                  */
4574                 if (is_Cmp(cmp)) {
4575                         ir_node *cmp_r = get_Cmp_right(cmp);
4576                         if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4577                                 ir_node *block    = get_irn_n(n, -1);
4578
4579                                 if(is_negated_value(f, t)) {
4580                                         ir_node *cmp_left = get_Cmp_left(cmp);
4581
4582                                         /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4583                                         if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4584                                                 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4585                                         {
4586                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4587                                                                                  cmp_left, mode);
4588                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4589                                                 return n;
4590                                         /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4591                                         } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4592                                                 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4593                                         {
4594                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4595                                                                                  cmp_left, mode);
4596                                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4597                                                                                                                  block, n, mode);
4598                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4599                                                 return n;
4600                                         }
4601                                 }
4602
4603                         }
4604                 }
4605         }
4606         return arch_transform_node_Mux(n);
4607 }  /* transform_node_Mux */
4608
4609 /**
4610  * Optimize a Psi into some simpler cases.
4611  */
4612 static ir_node *transform_node_Psi(ir_node *n) {
4613         if (is_Mux(n))
4614                 return transform_node_Mux(n);
4615
4616         return n;
4617 }  /* transform_node_Psi */
4618
4619 /**
4620  * optimize sync nodes that have other syncs as input we simply add the inputs
4621  * of the other sync to our own inputs
4622  */
4623 static ir_node *transform_node_Sync(ir_node *n) {
4624         int i, arity;
4625
4626         arity = get_irn_arity(n);
4627         for(i = 0; i < get_irn_arity(n); /*empty*/) {
4628                 int i2, arity2;
4629                 ir_node *in = get_irn_n(n, i);
4630                 if(!is_Sync(in)) {
4631                         ++i;
4632                         continue;
4633                 }
4634
4635                 /* set sync input 0 instead of the sync */
4636                 set_irn_n(n, i, get_irn_n(in, 0));
4637                 /* so we check this input again for syncs */
4638
4639                 /* append all other inputs of the sync to our sync */
4640                 arity2 = get_irn_arity(in);
4641                 for(i2 = 1; i2 < arity2; ++i2) {
4642                         ir_node *in_in = get_irn_n(in, i2);
4643                         add_irn_n(n, in_in);
4644                         /* increase arity so we also check the new inputs for syncs */
4645                         arity++;
4646                 }
4647         }
4648
4649         /* rehash the sync node */
4650         add_identities(current_ir_graph->value_table, n);
4651
4652         return n;
4653 }
4654
4655 /**
4656  * Tries several [inplace] [optimizing] transformations and returns an
4657  * equivalent node.  The difference to equivalent_node() is that these
4658  * transformations _do_ generate new nodes, and thus the old node must
4659  * not be freed even if the equivalent node isn't the old one.
4660  */
4661 static ir_node *transform_node(ir_node *n) {
4662         ir_node *oldn;
4663
4664         /*
4665          * Transform_node is the only "optimizing transformation" that might
4666          * return a node with a different opcode. We iterate HERE until fixpoint
4667          * to get the final result.
4668          */
4669         do {
4670                 oldn = n;
4671                 if (n->op->ops.transform_node)
4672                         n = n->op->ops.transform_node(n);
4673         } while (oldn != n);
4674
4675         return n;
4676 }  /* transform_node */
4677
4678 /**
4679  * Sets the default transform node operation for an ir_op_ops.
4680  *
4681  * @param code   the opcode for the default operation
4682  * @param ops    the operations initialized
4683  *
4684  * @return
4685  *    The operations.
4686  */
4687 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4688 {
4689 #define CASE(a)                                 \
4690         case iro_##a:                                 \
4691                 ops->transform_node  = transform_node_##a;  \
4692                 break
4693
4694         switch (code) {
4695         CASE(Add);
4696         CASE(Sub);
4697         CASE(Mul);
4698         CASE(Div);
4699         CASE(Mod);
4700         CASE(DivMod);
4701         CASE(Quot);
4702         CASE(Abs);
4703         CASE(Cond);
4704         CASE(And);
4705         CASE(Or);
4706         CASE(Eor);
4707         CASE(Minus);
4708         CASE(Not);
4709         CASE(Cast);
4710         CASE(Proj);
4711         CASE(Phi);
4712         CASE(Sel);
4713         CASE(Shr);
4714         CASE(Shrs);
4715         CASE(Shl);
4716         CASE(End);
4717         CASE(Mux);
4718         CASE(Psi);
4719         CASE(Sync);
4720         default:
4721           /* leave NULL */;
4722         }
4723
4724         return ops;
4725 #undef CASE
4726 }  /* firm_set_default_transform_node */
4727
4728
4729 /* **************** Common Subexpression Elimination **************** */
4730
4731 /** The size of the hash table used, should estimate the number of nodes
4732     in a graph. */
4733 #define N_IR_NODES 512
4734
4735 /** Compares the attributes of two Const nodes. */
4736 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4737         return (get_Const_tarval(a) != get_Const_tarval(b))
4738             || (get_Const_type(a) != get_Const_type(b));
4739 }  /* node_cmp_attr_Const */
4740
4741 /** Compares the attributes of two Proj nodes. */
4742 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4743         return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4744 }  /* node_cmp_attr_Proj */
4745
4746 /** Compares the attributes of two Filter nodes. */
4747 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4748         return get_Filter_proj(a) != get_Filter_proj(b);
4749 }  /* node_cmp_attr_Filter */
4750
4751 /** Compares the attributes of two Alloc nodes. */
4752 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4753         const alloc_attr *pa = get_irn_alloc_attr(a);
4754         const alloc_attr *pb = get_irn_alloc_attr(b);
4755         return (pa->where != pb->where) || (pa->type != pb->type);
4756 }  /* node_cmp_attr_Alloc */
4757
4758 /** Compares the attributes of two Free nodes. */
4759 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4760         const free_attr *pa = get_irn_free_attr(a);
4761         const free_attr *pb = get_irn_free_attr(b);
4762         return (pa->where != pb->where) || (pa->type != pb->type);
4763 }  /* node_cmp_attr_Free */
4764
4765 /** Compares the attributes of two SymConst nodes. */
4766 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4767         const symconst_attr *pa = get_irn_symconst_attr(a);
4768         const symconst_attr *pb = get_irn_symconst_attr(b);
4769         return (pa->num        != pb->num)
4770             || (pa->sym.type_p != pb->sym.type_p)
4771             || (pa->tp         != pb->tp);
4772 }  /* node_cmp_attr_SymConst */
4773
4774 /** Compares the attributes of two Call nodes. */
4775 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4776         return (get_irn_call_attr(a) != get_irn_call_attr(b));
4777 }  /* node_cmp_attr_Call */
4778
4779 /** Compares the attributes of two Sel nodes. */
4780 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4781         const ir_entity *a_ent = get_Sel_entity(a);
4782         const ir_entity *b_ent = get_Sel_entity(b);
4783         return
4784                 (a_ent->kind    != b_ent->kind)    ||
4785                 (a_ent->name    != b_ent->name)    ||
4786                 (a_ent->owner   != b_ent->owner)   ||
4787                 (a_ent->ld_name != b_ent->ld_name) ||
4788                 (a_ent->type    != b_ent->type);
4789 }  /* node_cmp_attr_Sel */
4790
4791 /** Compares the attributes of two Phi nodes. */
4792 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4793         /* we can only enter this function if both nodes have the same number of inputs,
4794            hence it is enough to check if one of them is a Phi0 */
4795         if (is_Phi0(a)) {
4796                 /* check the Phi0 attribute */
4797                 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4798         }
4799         return 0;
4800 }  /* node_cmp_attr_Phi */
4801
4802 /** Compares the attributes of two Conv nodes. */
4803 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4804         return get_Conv_strict(a) != get_Conv_strict(b);
4805 }  /* node_cmp_attr_Conv */
4806
4807 /** Compares the attributes of two Cast nodes. */
4808 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4809         return get_Cast_type(a) != get_Cast_type(b);
4810 }  /* node_cmp_attr_Cast */
4811
4812 /** Compares the attributes of two Load nodes. */
4813 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4814         if (get_Load_volatility(a) == volatility_is_volatile ||
4815             get_Load_volatility(b) == volatility_is_volatile)
4816                 /* NEVER do CSE on volatile Loads */
4817                 return 1;
4818         /* do not CSE Loads with different alignment. Be conservative. */
4819         if (get_Load_align(a) != get_Load_align(b))
4820                 return 1;
4821
4822         return get_Load_mode(a) != get_Load_mode(b);
4823 }  /* node_cmp_attr_Load */
4824
4825 /** Compares the attributes of two Store nodes. */
4826 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4827         /* do not CSE Stores with different alignment. Be conservative. */
4828         if (get_Store_align(a) != get_Store_align(b))
4829                 return 1;
4830
4831         /* NEVER do CSE on volatile Stores */
4832         return (get_Store_volatility(a) == volatility_is_volatile ||
4833                 get_Store_volatility(b) == volatility_is_volatile);
4834 }  /* node_cmp_attr_Store */
4835
4836 /** Compares the attributes of two Confirm nodes. */
4837 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4838         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4839 }  /* node_cmp_attr_Confirm */
4840
4841 /** Compares the attributes of two ASM nodes. */
4842 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4843         int i, n;
4844         const ir_asm_constraint *ca;
4845         const ir_asm_constraint *cb;
4846         ident **cla, **clb;
4847
4848         if (get_ASM_text(a) != get_ASM_text(b))
4849                 return 1;
4850
4851         /* Should we really check the constraints here? Should be better, but is strange. */
4852         n = get_ASM_n_input_constraints(a);
4853         if (n != get_ASM_n_input_constraints(b))
4854                 return 0;
4855
4856         ca = get_ASM_input_constraints(a);
4857         cb = get_ASM_input_constraints(b);
4858         for (i = 0; i < n; ++i) {
4859                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4860                         return 1;
4861         }
4862
4863         n = get_ASM_n_output_constraints(a);
4864         if (n != get_ASM_n_output_constraints(b))
4865                 return 0;
4866
4867         ca = get_ASM_output_constraints(a);
4868         cb = get_ASM_output_constraints(b);
4869         for (i = 0; i < n; ++i) {
4870                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4871                         return 1;
4872         }
4873
4874         n = get_ASM_n_clobbers(a);
4875         if (n != get_ASM_n_clobbers(b))
4876                 return 0;
4877
4878         cla = get_ASM_clobbers(a);
4879         clb = get_ASM_clobbers(b);
4880         for (i = 0; i < n; ++i) {
4881                 if (cla[i] != clb[i])
4882                         return 1;
4883         }
4884         return 0;
4885 }  /* node_cmp_attr_ASM */
4886
4887 /**
4888  * Set the default node attribute compare operation for an ir_op_ops.
4889  *
4890  * @param code   the opcode for the default operation
4891  * @param ops    the operations initialized
4892  *
4893  * @return
4894  *    The operations.
4895  */
4896 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4897 {
4898 #define CASE(a)                              \
4899         case iro_##a:                              \
4900                 ops->node_cmp_attr  = node_cmp_attr_##a; \
4901                 break
4902
4903         switch (code) {
4904         CASE(Const);
4905         CASE(Proj);
4906         CASE(Filter);
4907         CASE(Alloc);
4908         CASE(Free);
4909         CASE(SymConst);
4910         CASE(Call);
4911         CASE(Sel);
4912         CASE(Phi);
4913         CASE(Conv);
4914         CASE(Cast);
4915         CASE(Load);
4916         CASE(Store);
4917         CASE(Confirm);
4918         CASE(ASM);
4919         default:
4920           /* leave NULL */;
4921         }
4922
4923         return ops;
4924 #undef CASE
4925 }  /* firm_set_default_node_cmp_attr */
4926
4927 /*
4928  * Compare function for two nodes in the hash table. Gets two
4929  * nodes as parameters.  Returns 0 if the nodes are a cse.
4930  */
4931 int identities_cmp(const void *elt, const void *key) {
4932         ir_node *a, *b;
4933         int i, irn_arity_a;
4934
4935         a = (void *)elt;
4936         b = (void *)key;
4937
4938         if (a == b) return 0;
4939
4940         if ((get_irn_op(a) != get_irn_op(b)) ||
4941             (get_irn_mode(a) != get_irn_mode(b))) return 1;
4942
4943         /* compare if a's in and b's in are of equal length */
4944         irn_arity_a = get_irn_intra_arity (a);
4945         if (irn_arity_a != get_irn_intra_arity(b))
4946                 return 1;
4947
4948         /* for block-local cse and op_pin_state_pinned nodes: */
4949         if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4950                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4951                         return 1;
4952         }
4953
4954         /* compare a->in[0..ins] with b->in[0..ins] */
4955         for (i = 0; i < irn_arity_a; i++)
4956                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4957                         return 1;
4958
4959         /*
4960          * here, we already now that the nodes are identical except their
4961          * attributes
4962          */
4963         if (a->op->ops.node_cmp_attr)
4964                 return a->op->ops.node_cmp_attr(a, b);
4965
4966         return 0;
4967 }  /* identities_cmp */
4968
4969 /*
4970  * Calculate a hash value of a node.
4971  */
4972 unsigned ir_node_hash(ir_node *node) {
4973         unsigned h;
4974         int i, irn_arity;
4975
4976         if (node->op == op_Const) {
4977                 /* special value for const, as they only differ in their tarval. */
4978                 h = HASH_PTR(node->attr.con.tv);
4979                 h = 9*h + HASH_PTR(get_irn_mode(node));
4980         } else if (node->op == op_SymConst) {
4981                 /* special value for const, as they only differ in their symbol. */
4982                 h = HASH_PTR(node->attr.symc.sym.type_p);
4983                 h = 9*h + HASH_PTR(get_irn_mode(node));
4984         } else {
4985
4986                 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4987                 h = irn_arity = get_irn_intra_arity(node);
4988
4989                 /* consider all in nodes... except the block if not a control flow. */
4990                 for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
4991                         h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4992                 }
4993
4994                 /* ...mode,... */
4995                 h = 9*h + HASH_PTR(get_irn_mode(node));
4996                 /* ...and code */
4997                 h = 9*h + HASH_PTR(get_irn_op(node));
4998         }
4999
5000         return h;
5001 }  /* ir_node_hash */
5002
5003 pset *new_identities(void) {
5004         return new_pset(identities_cmp, N_IR_NODES);
5005 }  /* new_identities */
5006
5007 void del_identities(pset *value_table) {
5008         del_pset(value_table);
5009 }  /* del_identities */
5010
5011 /**
5012  * Normalize a node by putting constants (and operands with smaller
5013  * node index) on the right
5014  *
5015  * @param n   The node to normalize
5016  */
5017 static void normalize_node(ir_node *n) {
5018         if (get_opt_reassociation()) {
5019                 if (is_op_commutative(get_irn_op(n))) {
5020                         ir_node *l = get_binop_left(n);
5021                         ir_node *r = get_binop_right(n);
5022                         int l_idx = get_irn_idx(l);
5023                         int r_idx = get_irn_idx(r);
5024
5025                         /* For commutative operators perform  a OP b == b OP a but keep
5026                         constants on the RIGHT side. This helps greatly in some optimizations.
5027                         Moreover we use the idx number to make the form deterministic. */
5028                         if (is_irn_constlike(l))
5029                                 l_idx = -l_idx;
5030                         if (is_irn_constlike(r))
5031                                 r_idx = -r_idx;
5032                         if (l_idx < r_idx) {
5033                                 set_binop_left(n, r);
5034                                 set_binop_right(n, l);
5035                         }
5036                 }
5037         }
5038 }  /* normalize_node */
5039
5040 /**
5041  * Return the canonical node computing the same value as n.
5042  *
5043  * @param value_table  The value table
5044  * @param n            The node to lookup
5045  *
5046  * Looks up the node in a hash table.
5047  *
5048  * For Const nodes this is performed in the constructor, too.  Const
5049  * nodes are extremely time critical because of their frequent use in
5050  * constant string arrays.
5051  */
5052 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5053         ir_node *o = NULL;
5054
5055         if (!value_table) return n;
5056
5057         normalize_node(n);
5058
5059         o = pset_find(value_table, n, ir_node_hash(n));
5060         if (!o) return n;
5061
5062         DBG_OPT_CSE(n, o);
5063
5064         return o;
5065 }  /* identify */
5066
5067 /**
5068  * During construction we set the op_pin_state_pinned flag in the graph right when the
5069  * optimization is performed.  The flag turning on procedure global cse could
5070  * be changed between two allocations.  This way we are safe.
5071  */
5072 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5073         ir_node *old = n;
5074
5075         n = identify(value_table, n);
5076         if (get_irn_n(old, -1) != get_irn_n(n, -1))
5077                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5078         return n;
5079 }  /* identify_cons */
5080
5081 /*
5082  * Return the canonical node computing the same value as n.
5083  * Looks up the node in a hash table, enters it in the table
5084  * if it isn't there yet.
5085  */
5086 ir_node *identify_remember(pset *value_table, ir_node *n) {
5087         ir_node *o = NULL;
5088
5089         if (!value_table) return n;
5090
5091         normalize_node(n);
5092         /* lookup or insert in hash table with given hash key. */
5093         o = pset_insert(value_table, n, ir_node_hash(n));
5094
5095         if (o != n) {
5096                 DBG_OPT_CSE(n, o);
5097         }
5098
5099         return o;
5100 }  /* identify_remember */
5101
5102 /* Add a node to the identities value table. */
5103 void add_identities(pset *value_table, ir_node *node) {
5104         if (get_opt_cse() && is_no_Block(node))
5105                 identify_remember(value_table, node);
5106 }  /* add_identities */
5107
5108 /* Visit each node in the value table of a graph. */
5109 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5110         ir_node *node;
5111         ir_graph *rem = current_ir_graph;
5112
5113         current_ir_graph = irg;
5114         foreach_pset(irg->value_table, node)
5115                 visit(node, env);
5116         current_ir_graph = rem;
5117 }  /* visit_all_identities */
5118
5119 /**
5120  * Garbage in, garbage out. If a node has a dead input, i.e., the
5121  * Bad node is input to the node, return the Bad node.
5122  */
5123 static ir_node *gigo(ir_node *node) {
5124         int i, irn_arity;
5125         ir_op *op = get_irn_op(node);
5126
5127         /* remove garbage blocks by looking at control flow that leaves the block
5128            and replacing the control flow by Bad. */
5129         if (get_irn_mode(node) == mode_X) {
5130                 ir_node *block = get_nodes_block(skip_Proj(node));
5131
5132                 /* Don't optimize nodes in immature blocks. */
5133                 if (!get_Block_matured(block)) return node;
5134                 /* Don't optimize End, may have Bads. */
5135                 if (op == op_End) return node;
5136
5137                 if (is_Block(block)) {
5138                         irn_arity = get_irn_arity(block);
5139                         for (i = 0; i < irn_arity; i++) {
5140                                 if (!is_Bad(get_irn_n(block, i)))
5141                                         break;
5142                         }
5143                         if (i == irn_arity) {
5144                                 ir_graph *irg = get_irn_irg(block);
5145                                 /* the start block is never dead */
5146                                 if (block != get_irg_start_block(irg)
5147                                         && block != get_irg_end_block(irg))
5148                                         return new_Bad();
5149                         }
5150                 }
5151         }
5152
5153         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5154            blocks predecessors is dead. */
5155         if (op != op_Block && op != op_Phi && op != op_Tuple) {
5156                 irn_arity = get_irn_arity(node);
5157
5158                 /*
5159                  * Beware: we can only read the block of a non-floating node.
5160                  */
5161                 if (is_irn_pinned_in_irg(node) &&
5162                         is_Block_dead(get_nodes_block(node)))
5163                         return new_Bad();
5164
5165                 for (i = 0; i < irn_arity; i++) {
5166                         ir_node *pred = get_irn_n(node, i);
5167
5168                         if (is_Bad(pred))
5169                                 return new_Bad();
5170 #if 0
5171                         /* Propagating Unknowns here seems to be a bad idea, because
5172                            sometimes we need a node as a input and did not want that
5173                            it kills it's user.
5174                            However, it might be useful to move this into a later phase
5175                            (if you think that optimizing such code is useful). */
5176                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5177                                 return new_Unknown(get_irn_mode(node));
5178 #endif
5179                 }
5180         }
5181 #if 0
5182         /* With this code we violate the agreement that local_optimize
5183            only leaves Bads in Block, Phi and Tuple nodes. */
5184         /* If Block has only Bads as predecessors it's garbage. */
5185         /* If Phi has only Bads as predecessors it's garbage. */
5186         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
5187                 irn_arity = get_irn_arity(node);
5188                 for (i = 0; i < irn_arity; i++) {
5189                         if (!is_Bad(get_irn_n(node, i))) break;
5190                 }
5191                 if (i == irn_arity) node = new_Bad();
5192         }
5193 #endif
5194         return node;
5195 }  /* gigo */
5196
5197 /**
5198  * These optimizations deallocate nodes from the obstack.
5199  * It can only be called if it is guaranteed that no other nodes
5200  * reference this one, i.e., right after construction of a node.
5201  *
5202  * @param n   The node to optimize
5203  *
5204  * current_ir_graph must be set to the graph of the node!
5205  */
5206 ir_node *optimize_node(ir_node *n) {
5207         tarval *tv;
5208         ir_node *oldn = n;
5209         ir_opcode iro = get_irn_opcode(n);
5210
5211         /* Always optimize Phi nodes: part of the construction. */
5212         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5213
5214         /* constant expression evaluation / constant folding */
5215         if (get_opt_constant_folding()) {
5216                 /* neither constants nor Tuple values can be evaluated */
5217                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5218                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5219                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5220                         /* try to evaluate */
5221                         tv = computed_value(n);
5222                         if (tv != tarval_bad) {
5223                                 ir_node *nw;
5224                                 ir_type *old_tp = get_irn_type(n);
5225                                 int i, arity = get_irn_arity(n);
5226                                 int node_size;
5227
5228                                 /*
5229                                  * Try to recover the type of the new expression.
5230                                  */
5231                                 for (i = 0; i < arity && !old_tp; ++i)
5232                                         old_tp = get_irn_type(get_irn_n(n, i));
5233
5234                                 /*
5235                                  * we MUST copy the node here temporary, because it's still needed
5236                                  * for DBG_OPT_CSTEVAL
5237                                  */
5238                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
5239                                 oldn = alloca(node_size);
5240
5241                                 memcpy(oldn, n, node_size);
5242                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5243
5244                                 /* ARG, copy the in array, we need it for statistics */
5245                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5246
5247                                 /* note the inplace edges module */
5248                                 edges_node_deleted(n, current_ir_graph);
5249
5250                                 /* evaluation was successful -- replace the node. */
5251                                 irg_kill_node(current_ir_graph, n);
5252                                 nw = new_Const(get_tarval_mode(tv), tv);
5253
5254                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5255                                         set_Const_type(nw, old_tp);
5256                                 DBG_OPT_CSTEVAL(oldn, nw);
5257                                 tarval_enable_fp_ops(old_fp_mode);
5258                                 return nw;
5259                         }
5260                         tarval_enable_fp_ops(old_fp_mode);
5261                 }
5262         }
5263
5264         /* remove unnecessary nodes */
5265         if (get_opt_constant_folding() ||
5266             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5267             (iro == iro_Id)   ||
5268             (iro == iro_Proj) ||
5269             (iro == iro_Block)  )  /* Flags tested local. */
5270                 n = equivalent_node(n);
5271
5272         /* Common Subexpression Elimination.
5273          *
5274          * Checks whether n is already available.
5275          * The block input is used to distinguish different subexpressions. Right
5276          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5277          * subexpressions within a block.
5278          */
5279         if (get_opt_cse())
5280                 n = identify_cons(current_ir_graph->value_table, n);
5281
5282         if (n != oldn) {
5283                 edges_node_deleted(oldn, current_ir_graph);
5284
5285                 /* We found an existing, better node, so we can deallocate the old node. */
5286                 irg_kill_node(current_ir_graph, oldn);
5287                 return n;
5288         }
5289
5290         /* Some more constant expression evaluation that does not allow to
5291            free the node. */
5292         iro = get_irn_opcode(n);
5293         if (get_opt_constant_folding() ||
5294             (iro == iro_Cond) ||
5295             (iro == iro_Proj))     /* Flags tested local. */
5296                 n = transform_node(n);
5297
5298         /* Remove nodes with dead (Bad) input.
5299            Run always for transformation induced Bads. */
5300         n = gigo (n);
5301
5302         /* Now we have a legal, useful node. Enter it in hash table for CSE */
5303         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5304                 n = identify_remember(current_ir_graph->value_table, n);
5305         }
5306
5307         return n;
5308 }  /* optimize_node */
5309
5310
5311 /**
5312  * These optimizations never deallocate nodes (in place).  This can cause dead
5313  * nodes lying on the obstack.  Remove these by a dead node elimination,
5314  * i.e., a copying garbage collection.
5315  */
5316 ir_node *optimize_in_place_2(ir_node *n) {
5317         tarval *tv;
5318         ir_node *oldn = n;
5319         ir_opcode iro = get_irn_opcode(n);
5320
5321         if (!get_opt_optimize() && !is_Phi(n)) return n;
5322
5323         /* constant expression evaluation / constant folding */
5324         if (get_opt_constant_folding()) {
5325                 /* neither constants nor Tuple values can be evaluated */
5326                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5327                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5328                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5329                         /* try to evaluate */
5330                         tv = computed_value(n);
5331                         if (tv != tarval_bad) {
5332                                 /* evaluation was successful -- replace the node. */
5333                                 ir_type *old_tp = get_irn_type(n);
5334                                 int i, arity = get_irn_arity(n);
5335
5336                                 /*
5337                                  * Try to recover the type of the new expression.
5338                                  */
5339                                 for (i = 0; i < arity && !old_tp; ++i)
5340                                         old_tp = get_irn_type(get_irn_n(n, i));
5341
5342                                 n = new_Const(get_tarval_mode(tv), tv);
5343
5344                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5345                                         set_Const_type(n, old_tp);
5346
5347                                 DBG_OPT_CSTEVAL(oldn, n);
5348                                 tarval_enable_fp_ops(old_fp_mode);
5349                                 return n;
5350                         }
5351                         tarval_enable_fp_ops(old_fp_mode);
5352                 }
5353         }
5354
5355         /* remove unnecessary nodes */
5356         if (get_opt_constant_folding() ||
5357             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5358             (iro == iro_Id)   ||   /* ... */
5359             (iro == iro_Proj) ||   /* ... */
5360             (iro == iro_Block)  )  /* Flags tested local. */
5361                 n = equivalent_node(n);
5362
5363         /** common subexpression elimination **/
5364         /* Checks whether n is already available. */
5365         /* The block input is used to distinguish different subexpressions.  Right
5366            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5367            subexpressions within a block. */
5368         if (get_opt_cse()) {
5369                 n = identify(current_ir_graph->value_table, n);
5370         }
5371
5372         /* Some more constant expression evaluation. */
5373         iro = get_irn_opcode(n);
5374         if (get_opt_constant_folding() ||
5375                 (iro == iro_Cond) ||
5376                 (iro == iro_Proj))     /* Flags tested local. */
5377                 n = transform_node(n);
5378
5379         /* Remove nodes with dead (Bad) input.
5380            Run always for transformation induced Bads.  */
5381         n = gigo(n);
5382
5383         /* Now we can verify the node, as it has no dead inputs any more. */
5384         irn_vrfy(n);
5385
5386         /* Now we have a legal, useful node. Enter it in hash table for cse.
5387            Blocks should be unique anyways.  (Except the successor of start:
5388            is cse with the start block!) */
5389         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5390                 n = identify_remember(current_ir_graph->value_table, n);
5391
5392         return n;
5393 }  /* optimize_in_place_2 */
5394
5395 /**
5396  * Wrapper for external use, set proper status bits after optimization.
5397  */
5398 ir_node *optimize_in_place(ir_node *n) {
5399         /* Handle graph state */
5400         assert(get_irg_phase_state(current_ir_graph) != phase_building);
5401
5402         if (get_opt_global_cse())
5403                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5404         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5405                 set_irg_outs_inconsistent(current_ir_graph);
5406
5407         /* FIXME: Maybe we could also test whether optimizing the node can
5408            change the control graph. */
5409         set_irg_doms_inconsistent(current_ir_graph);
5410         return optimize_in_place_2(n);
5411 }  /* optimize_in_place */
5412
5413 /*
5414  * Sets the default operation for an ir_ops.
5415  */
5416 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5417         ops = firm_set_default_computed_value(code, ops);
5418         ops = firm_set_default_equivalent_node(code, ops);
5419         ops = firm_set_default_transform_node(code, ops);
5420         ops = firm_set_default_node_cmp_attr(code, ops);
5421         ops = firm_set_default_get_type(code, ops);
5422         ops = firm_set_default_get_type_attr(code, ops);
5423         ops = firm_set_default_get_entity_attr(code, ops);
5424
5425         return ops;
5426 }  /* firm_set_default_operations */