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