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