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