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