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