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