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