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