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