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