55b8f6b0efdc107bf1d343f770abce663ec348d9
[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.
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         } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1136                 n = b;
1137                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138         } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1139                 n = a;
1140                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1141         }
1142         return n;
1143 }  /* equivalent_node_And */
1144
1145 /**
1146  * Try to remove useless Conv's:
1147  */
1148 static ir_node *equivalent_node_Conv(ir_node *n) {
1149         ir_node *oldn = n;
1150         ir_node *a = get_Conv_op(n);
1151         ir_node *b;
1152
1153         ir_mode *n_mode = get_irn_mode(n);
1154         ir_mode *a_mode = get_irn_mode(a);
1155
1156         if (n_mode == a_mode) { /* No Conv necessary */
1157                 if (get_Conv_strict(n)) {
1158                         /* special case: the predecessor might be a also a Conv */
1159                         if (is_Conv(a)) {
1160                                 if (! get_Conv_strict(a)) {
1161                                         /* first one is not strict, kick it */
1162                                         set_Conv_op(n, get_Conv_op(a));
1163                                         return n;
1164                                 }
1165                                 /* else both are strict conv, second is superflous */
1166                         } else {
1167                                 /* leave strict floating point Conv's */
1168                                 return n;
1169                         }
1170                 }
1171                 n = a;
1172                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1173         } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1174                 ir_mode *b_mode;
1175
1176                 b = get_Conv_op(a);
1177                 n_mode = get_irn_mode(n);
1178                 b_mode = get_irn_mode(b);
1179
1180                 if (n_mode == b_mode) {
1181                         if (n_mode == mode_b) {
1182                                 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1183                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1184                         } else if (mode_is_int(n_mode)) {
1185                                 if (smaller_mode(b_mode, a_mode)){
1186                                         n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1187                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1188                                 }
1189                         }
1190                 }
1191         }
1192         return n;
1193 }  /* equivalent_node_Conv */
1194
1195 /**
1196  * A Cast may be removed if the type of the previous node
1197  * is already the type of the Cast.
1198  */
1199 static ir_node *equivalent_node_Cast(ir_node *n) {
1200         ir_node *oldn = n;
1201         ir_node *pred = get_Cast_op(n);
1202
1203         if (get_irn_type(pred) == get_Cast_type(n)) {
1204                 n = pred;
1205                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1206         }
1207         return n;
1208 }  /* equivalent_node_Cast */
1209
1210 /**
1211  * Several optimizations:
1212  * - no Phi in start block.
1213  * - remove Id operators that are inputs to Phi
1214  * - fold Phi-nodes, iff they have only one predecessor except
1215  *   themselves.
1216  */
1217 static ir_node *equivalent_node_Phi(ir_node *n) {
1218         int i, n_preds;
1219
1220         ir_node *oldn = n;
1221         ir_node *block = NULL;     /* to shutup gcc */
1222         ir_node *first_val = NULL; /* to shutup gcc */
1223
1224         if (!get_opt_normalize()) return n;
1225
1226         n_preds = get_Phi_n_preds(n);
1227
1228         block = get_nodes_block(n);
1229         /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1230         assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1231         if ((is_Block_dead(block)) ||                  /* Control dead */
1232                 (block == get_irg_start_block(current_ir_graph)))  /* There should be no Phi nodes */
1233                 return new_Bad();                                    /* in the Start Block. */
1234
1235         if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1236
1237         /* If the Block has a Bad pred, we also have one. */
1238         for (i = 0;  i < n_preds;  ++i)
1239                 if (is_Bad(get_Block_cfgpred(block, i)))
1240                         set_Phi_pred(n, i, new_Bad());
1241
1242         /* Find first non-self-referencing input */
1243         for (i = 0; i < n_preds; ++i) {
1244                 first_val = get_Phi_pred(n, i);
1245                 if (   (first_val != n)                            /* not self pointer */
1246 #if 1
1247                     && (! is_Bad(first_val))
1248 #endif
1249                    ) {        /* value not dead */
1250                         break;          /* then found first value. */
1251                 }
1252         }
1253
1254         if (i >= n_preds) {
1255                 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1256                 return new_Bad();
1257         }
1258
1259         /* search for rest of inputs, determine if any of these
1260         are non-self-referencing */
1261         while (++i < n_preds) {
1262                 ir_node *scnd_val = get_Phi_pred(n, i);
1263                 if (   (scnd_val != n)
1264                     && (scnd_val != first_val)
1265 #if 1
1266                     && (! is_Bad(scnd_val))
1267 #endif
1268                         ) {
1269                         break;
1270                 }
1271         }
1272
1273         if (i >= n_preds) {
1274                 /* Fold, if no multiple distinct non-self-referencing inputs */
1275                 n = first_val;
1276                 DBG_OPT_PHI(oldn, n);
1277         }
1278         return n;
1279 }  /* equivalent_node_Phi */
1280
1281 /**
1282  * Several optimizations:
1283  * - no Sync in start block.
1284  * - fold Sync-nodes, iff they have only one predecessor except
1285  *   themselves.
1286  */
1287 static ir_node *equivalent_node_Sync(ir_node *n) {
1288         int i, n_preds;
1289
1290         ir_node *oldn = n;
1291         ir_node *first_val = NULL; /* to shutup gcc */
1292
1293         if (!get_opt_normalize()) return n;
1294
1295         n_preds = get_Sync_n_preds(n);
1296
1297         /* Find first non-self-referencing input */
1298         for (i = 0; i < n_preds; ++i) {
1299                 first_val = get_Sync_pred(n, i);
1300                 if ((first_val != n)  /* not self pointer */ &&
1301                     (! is_Bad(first_val))
1302                    ) {                /* value not dead */
1303                         break;            /* then found first value. */
1304                 }
1305         }
1306
1307         if (i >= n_preds)
1308                 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1309                 return new_Bad();
1310
1311         /* search the rest of inputs, determine if any of these
1312            are non-self-referencing */
1313         while (++i < n_preds) {
1314                 ir_node *scnd_val = get_Sync_pred(n, i);
1315                 if ((scnd_val != n) &&
1316                     (scnd_val != first_val) &&
1317                     (! is_Bad(scnd_val))
1318                    )
1319                         break;
1320         }
1321
1322         if (i >= n_preds) {
1323                 /* Fold, if no multiple distinct non-self-referencing inputs */
1324                 n = first_val;
1325                 DBG_OPT_SYNC(oldn, n);
1326         }
1327         return n;
1328 }  /* equivalent_node_Sync */
1329
1330 /**
1331  * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1332  * ProjX(Load) and ProjX(Store).
1333  */
1334 static ir_node *equivalent_node_Proj(ir_node *proj) {
1335         ir_node *oldn = proj;
1336         ir_node *a = get_Proj_pred(proj);
1337
1338         if (get_irn_op(a) == op_Tuple) {
1339                 /* Remove the Tuple/Proj combination. */
1340                 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1341                         proj = get_Tuple_pred(a, get_Proj_proj(proj));
1342                         DBG_OPT_TUPLE(oldn, a, proj);
1343                 } else {
1344                          /* This should not happen! */
1345                         assert(! "found a Proj with higher number than Tuple predecessors");
1346                         proj = new_Bad();
1347                 }
1348         } else if (get_irn_mode(proj) == mode_X) {
1349                 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1350                         /* Remove dead control flow -- early gigo(). */
1351                         proj = new_Bad();
1352                 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1353                         ir_op *op = get_irn_op(a);
1354
1355                         if (op == op_Load) {
1356                                 /* get the Load address */
1357                                 ir_node *addr = get_Load_ptr(a);
1358                                 ir_node *blk  = get_irn_n(a, -1);
1359                                 ir_node *confirm;
1360
1361                                 if (value_not_null(addr, &confirm)) {
1362                                         if (confirm == NULL) {
1363                                                 /* this node may float if it did not depend on a Confirm */
1364                                                 set_irn_pinned(a, op_pin_state_floats);
1365                                         }
1366                                         if (get_Proj_proj(proj) == pn_Load_X_except) {
1367                                                 DBG_OPT_EXC_REM(proj);
1368                                                 return new_Bad();
1369                                         } else
1370                                                 return new_r_Jmp(current_ir_graph, blk);
1371                                 }
1372                         } else if (op == op_Store) {
1373                                 /* get the load/store address */
1374                                 ir_node *addr = get_Store_ptr(a);
1375                                 ir_node *blk  = get_irn_n(a, -1);
1376                                 ir_node *confirm;
1377
1378                                 if (value_not_null(addr, &confirm)) {
1379                                         if (confirm == NULL) {
1380                                                 /* this node may float if it did not depend on a Confirm */
1381                                                 set_irn_pinned(a, op_pin_state_floats);
1382                                         }
1383                                         if (get_Proj_proj(proj) == pn_Store_X_except) {
1384                                                 DBG_OPT_EXC_REM(proj);
1385                                                 return new_Bad();
1386                                         } else
1387                                                 return new_r_Jmp(current_ir_graph, blk);
1388                                 }
1389                         }
1390                 }
1391         }
1392
1393         return proj;
1394 }  /* equivalent_node_Proj */
1395
1396 /**
1397  * Remove Id's.
1398  */
1399 static ir_node *equivalent_node_Id(ir_node *n) {
1400         ir_node *oldn = n;
1401
1402         do {
1403                 n = get_Id_pred(n);
1404         } while (get_irn_op(n) == op_Id);
1405
1406         DBG_OPT_ID(oldn, n);
1407         return n;
1408 }  /* equivalent_node_Id */
1409
1410 /**
1411  * Optimize a Mux.
1412  */
1413 static ir_node *equivalent_node_Mux(ir_node *n)
1414 {
1415         ir_node *oldn = n, *sel = get_Mux_sel(n);
1416         tarval *ts = value_of(sel);
1417
1418         /* Mux(true, f, t) == t */
1419         if (ts == tarval_b_true) {
1420                 n = get_Mux_true(n);
1421                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1422         }
1423         /* Mux(false, f, t) == f */
1424         else if (ts == tarval_b_false) {
1425                 n = get_Mux_false(n);
1426                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1427         }
1428         /* Mux(v, x, x) == x */
1429         else if (get_Mux_false(n) == get_Mux_true(n)) {
1430                 n = get_Mux_true(n);
1431                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1432         }
1433         else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1434                 ir_node *cmp = get_Proj_pred(sel);
1435                 long proj_nr = get_Proj_proj(sel);
1436                 ir_node *b   = get_Mux_false(n);
1437                 ir_node *a   = get_Mux_true(n);
1438
1439                 /*
1440                  * Note: normalization puts the constant on the right site,
1441                  * so we check only one case.
1442                  *
1443                  * Note further that these optimization work even for floating point
1444                  * with NaN's because -NaN == NaN.
1445                  * However, if +0 and -0 is handled differently, we cannot use the first one.
1446                  */
1447                 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1448                         if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1449                                 /* Mux(a CMP 0, X, a) */
1450                                 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1451                                         /* Mux(a CMP 0, -a, a) */
1452                                         if (proj_nr == pn_Cmp_Eq) {
1453                                                 /* Mux(a == 0, -a, a)  ==>  -a */
1454                                                 n = b;
1455                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1456                                         } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1457                                                 /* Mux(a != 0, -a, a)  ==> a */
1458                                                 n = a;
1459                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1460                                         }
1461                                 } else if (classify_Const(b) == CNST_NULL) {
1462                                         /* Mux(a CMP 0, 0, a) */
1463                                         if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1464                                                 /* Mux(a != 0, 0, a) ==> a */
1465                                                 n = a;
1466                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1467                                         } else if (proj_nr == pn_Cmp_Eq) {
1468                                                 /* Mux(a == 0, 0, a) ==> 0 */
1469                                                 n = b;
1470                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1471                                         }
1472                                 }
1473                         }
1474                 }
1475         }
1476         return n;
1477 }  /* equivalent_node_Mux */
1478
1479 /**
1480  * Returns a equivalent node of a Psi: if a condition is true
1481  * and all previous conditions are false we know its value.
1482  * If all conditions are false its value is the default one.
1483  */
1484 static ir_node *equivalent_node_Psi(ir_node *n) {
1485         if (is_Mux(n))
1486                 return equivalent_node_Mux(n);
1487         return n;
1488 }  /* equivalent_node_Psi */
1489
1490 /**
1491  * Optimize -a CMP -b into b CMP a.
1492  * This works only for for modes where unary Minus
1493  * cannot Overflow.
1494  * Note that two-complement integers can Overflow
1495  * so it will NOT work.
1496  *
1497  * For == and != can be handled in Proj(Cmp)
1498  */
1499 static ir_node *equivalent_node_Cmp(ir_node *n) {
1500         ir_node *left  = get_Cmp_left(n);
1501         ir_node *right = get_Cmp_right(n);
1502
1503         if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1504                 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1505                 left  = get_Minus_op(left);
1506                 right = get_Minus_op(right);
1507                 set_Cmp_left(n, right);
1508                 set_Cmp_right(n, left);
1509         }
1510         return n;
1511 }  /* equivalent_node_Cmp */
1512
1513 /**
1514  * Remove Confirm nodes if setting is on.
1515  * Replace Confirms(x, '=', Constlike) by Constlike.
1516  */
1517 static ir_node *equivalent_node_Confirm(ir_node *n) {
1518         ir_node *pred = get_Confirm_value(n);
1519         pn_Cmp  pnc   = get_Confirm_cmp(n);
1520
1521         if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1522                 /*
1523                  * rare case: two identical Confirms one after another,
1524                  * replace the second one with the first.
1525                  */
1526                 n = pred;
1527         }
1528         if (pnc == pn_Cmp_Eq) {
1529                 ir_node *bound = get_Confirm_bound(n);
1530
1531                 /*
1532                  * Optimize a rare case:
1533                  * Confirm(x, '=', Constlike) ==> Constlike
1534                  */
1535                 if (is_irn_constlike(bound)) {
1536                         DBG_OPT_CONFIRM(n, bound);
1537                         return bound;
1538                 }
1539         }
1540         return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1541 }
1542
1543 /**
1544  * Optimize CopyB(mem, x, x) into a Nop.
1545  */
1546 static ir_node *equivalent_node_CopyB(ir_node *n) {
1547         ir_node *a = get_CopyB_dst(n);
1548         ir_node *b = get_CopyB_src(n);
1549
1550         if (a == b) {
1551                 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1552                 ir_node *mem = get_CopyB_mem(n);
1553                 ir_node *blk = get_nodes_block(n);
1554                 turn_into_tuple(n, pn_CopyB_max);
1555                 set_Tuple_pred(n, pn_CopyB_M,         mem);
1556                 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1557                 set_Tuple_pred(n, pn_CopyB_X_except,  new_Bad());        /* no exception */
1558                 set_Tuple_pred(n, pn_CopyB_M_except,  new_Bad());
1559         }
1560         return n;
1561 }  /* equivalent_node_CopyB */
1562
1563 /**
1564  * Optimize Bounds(idx, idx, upper) into idx.
1565  */
1566 static ir_node *equivalent_node_Bound(ir_node *n) {
1567         ir_node *idx   = get_Bound_index(n);
1568         ir_node *lower = get_Bound_lower(n);
1569         int ret_tuple = 0;
1570
1571         /* By definition lower < upper, so if idx == lower -->
1572         lower <= idx && idx < upper */
1573         if (idx == lower) {
1574                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1575                 ret_tuple = 1;
1576         } else {
1577                 ir_node *pred = skip_Proj(idx);
1578
1579                 if (get_irn_op(pred) == op_Bound) {
1580                         /*
1581                          * idx was Bounds_check previously, it is still valid if
1582                          * lower <= pred_lower && pred_upper <= upper.
1583                          */
1584                         ir_node *upper = get_Bound_upper(n);
1585                         if (get_Bound_lower(pred) == lower &&
1586                                 get_Bound_upper(pred) == upper) {
1587                                 /*
1588                                  * One could expect that we simply return the previous
1589                                  * Bound here. However, this would be wrong, as we could
1590                                  * add an exception Proj to a new location then.
1591                                  * So, we must turn in into a tuple.
1592                                  */
1593                                 ret_tuple = 1;
1594                         }
1595                 }
1596         }
1597         if (ret_tuple) {
1598                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1599                 ir_node *mem = get_Bound_mem(n);
1600                 ir_node *blk = get_nodes_block(n);
1601                 turn_into_tuple(n, pn_Bound_max);
1602                 set_Tuple_pred(n, pn_Bound_M,         mem);
1603                 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk));       /* no exception */
1604                 set_Tuple_pred(n, pn_Bound_X_except,  new_Bad());       /* no exception */
1605                 set_Tuple_pred(n, pn_Bound_res,       idx);
1606         }
1607         return n;
1608 }  /* equivalent_node_Bound */
1609
1610 /**
1611  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1612  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1613  * new nodes.  It is therefore safe to free n if the node returned is not n.
1614  * If a node returns a Tuple we can not just skip it.  If the size of the
1615  * in array fits, we transform n into a tuple (e.g., Div).
1616  */
1617 ir_node *equivalent_node(ir_node *n) {
1618         if (n->op->ops.equivalent_node)
1619                 return n->op->ops.equivalent_node(n);
1620         return n;
1621 }  /* equivalent_node */
1622
1623 /**
1624  * Sets the default equivalent node operation for an ir_op_ops.
1625  *
1626  * @param code   the opcode for the default operation
1627  * @param ops    the operations initialized
1628  *
1629  * @return
1630  *    The operations.
1631  */
1632 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1633 {
1634 #define CASE(a)                                      \
1635         case iro_##a:                                    \
1636                 ops->equivalent_node  = equivalent_node_##a; \
1637                 break
1638
1639         switch (code) {
1640         CASE(Block);
1641         CASE(Jmp);
1642         CASE(Raise);
1643         CASE(Or);
1644         CASE(Add);
1645         CASE(Eor);
1646         CASE(Sub);
1647         CASE(Shl);
1648         CASE(Shr);
1649         CASE(Shrs);
1650         CASE(Rot);
1651         CASE(Not);
1652         CASE(Minus);
1653         CASE(Mul);
1654         CASE(Div);
1655         CASE(Quot);
1656         CASE(DivMod);
1657         CASE(And);
1658         CASE(Conv);
1659         CASE(Cast);
1660         CASE(Phi);
1661         CASE(Sync);
1662         CASE(Proj);
1663         CASE(Id);
1664         CASE(Mux);
1665         CASE(Psi);
1666         CASE(Cmp);
1667         CASE(Confirm);
1668         CASE(CopyB);
1669         CASE(Bound);
1670         default:
1671                 /* leave NULL */;
1672         }
1673
1674         return ops;
1675 #undef CASE
1676 }  /* firm_set_default_equivalent_node */
1677
1678 /**
1679  * Do node specific optimizations of nodes predecessors.
1680  */
1681 static void optimize_preds(ir_node *n) {
1682         switch (get_irn_opcode(n)) {
1683
1684         case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1685                 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1686
1687                 if (get_irn_op(a) == op_Cast) {
1688                         a = get_Cast_op(a);
1689                         set_Cmp_left(n, a);
1690                 }
1691                 if (get_irn_op(b) == op_Cast) {
1692                         b = get_Cast_op(b);
1693                         set_Cmp_right(n, b);
1694                 }
1695                 break;
1696         }
1697
1698         default: break;
1699         } /* end switch */
1700 }  /* optimize_preds */
1701
1702 /**
1703  * Returns non-zero if a node is a Phi node
1704  * with all predecessors constant.
1705  */
1706 static int is_const_Phi(ir_node *n) {
1707         int i;
1708
1709         if (! is_Phi(n))
1710                 return 0;
1711         for (i = get_irn_arity(n) - 1; i >= 0; --i)
1712                 if (! is_Const(get_irn_n(n, i)))
1713                         return 0;
1714                 return 1;
1715 }  /* is_const_Phi */
1716
1717 /**
1718  * Apply an evaluator on a binop with a constant operators (and one Phi).
1719  *
1720  * @param phi    the Phi node
1721  * @param other  the other operand
1722  * @param eval   an evaluator function
1723  * @param left   if non-zero, other is the left operand, else the right
1724  *
1725  * @return a new Phi node if the conversion was successful, NULL else
1726  */
1727 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1728         tarval   *tv;
1729         void     **res;
1730         ir_node  *pred;
1731         ir_mode  *mode;
1732         ir_graph *irg;
1733         int      i, n = get_irn_arity(phi);
1734
1735         NEW_ARR_A(void *, res, n);
1736         if (left) {
1737                 for (i = 0; i < n; ++i) {
1738                         pred = get_irn_n(phi, i);
1739                         tv   = get_Const_tarval(pred);
1740                         tv   = eval(other, tv);
1741
1742                         if (tv == tarval_bad) {
1743                                 /* folding failed, bad */
1744                                 return NULL;
1745                         }
1746                         res[i] = tv;
1747                 }
1748         } else {
1749                 for (i = 0; i < n; ++i) {
1750                         pred = get_irn_n(phi, i);
1751                         tv   = get_Const_tarval(pred);
1752                         tv   = eval(tv, other);
1753
1754                         if (tv == tarval_bad) {
1755                                 /* folding failed, bad */
1756                                 return 0;
1757                         }
1758                         res[i] = tv;
1759                 }
1760         }
1761         mode = get_irn_mode(phi);
1762         irg  = current_ir_graph;
1763         for (i = 0; i < n; ++i) {
1764                 pred = get_irn_n(phi, i);
1765                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1766                         mode, res[i], get_Const_type(pred));
1767         }
1768         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1769 }  /* apply_binop_on_phi */
1770
1771 /**
1772  * Apply an evaluator on a unop with a constant operator (a Phi).
1773  *
1774  * @param phi    the Phi node
1775  * @param eval   an evaluator function
1776  *
1777  * @return a new Phi node if the conversion was successful, NULL else
1778  */
1779 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1780         tarval   *tv;
1781         void     **res;
1782         ir_node  *pred;
1783         ir_mode  *mode;
1784         ir_graph *irg;
1785         int      i, n = get_irn_arity(phi);
1786
1787         NEW_ARR_A(void *, res, n);
1788         for (i = 0; i < n; ++i) {
1789                 pred = get_irn_n(phi, i);
1790                 tv   = get_Const_tarval(pred);
1791                 tv   = eval(tv);
1792
1793                 if (tv == tarval_bad) {
1794                         /* folding failed, bad */
1795                         return 0;
1796                 }
1797                 res[i] = tv;
1798         }
1799         mode = get_irn_mode(phi);
1800         irg  = current_ir_graph;
1801         for (i = 0; i < n; ++i) {
1802                 pred = get_irn_n(phi, i);
1803                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1804                         mode, res[i], get_Const_type(pred));
1805         }
1806         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1807 }  /* apply_unop_on_phi */
1808
1809 /**
1810  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1811  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1812  * If possible, remove the Conv's.
1813  */
1814 static ir_node *transform_node_AddSub(ir_node *n) {
1815         ir_mode *mode = get_irn_mode(n);
1816
1817         if (mode_is_reference(mode)) {
1818                 ir_node *left  = get_binop_left(n);
1819                 ir_node *right = get_binop_right(n);
1820                 int ref_bits   = get_mode_size_bits(mode);
1821
1822                 if (get_irn_op(left) == op_Conv) {
1823                         ir_mode *mode = get_irn_mode(left);
1824                         int bits      = get_mode_size_bits(mode);
1825
1826                         if (ref_bits == bits &&
1827                             mode_is_int(mode) &&
1828                             get_mode_arithmetic(mode) == irma_twos_complement) {
1829                                 ir_node *pre      = get_Conv_op(left);
1830                                 ir_mode *pre_mode = get_irn_mode(pre);
1831
1832                                 if (mode_is_int(pre_mode) &&
1833                                     get_mode_size_bits(pre_mode) == bits &&
1834                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1835                                         /* ok, this conv just changes to sign, moreover the calculation
1836                                          * is done with same number of bits as our address mode, so
1837                                          * we can ignore the conv as address calculation can be viewed
1838                                          * as either signed or unsigned
1839                                          */
1840                                         set_binop_left(n, pre);
1841                                 }
1842                         }
1843                 }
1844
1845                 if (get_irn_op(right) == op_Conv) {
1846                         ir_mode *mode = get_irn_mode(right);
1847                         int bits      = get_mode_size_bits(mode);
1848
1849                         if (ref_bits == bits &&
1850                                 mode_is_int(mode) &&
1851                                 get_mode_arithmetic(mode) == irma_twos_complement) {
1852                                 ir_node *pre      = get_Conv_op(right);
1853                                 ir_mode *pre_mode = get_irn_mode(pre);
1854
1855                                 if (mode_is_int(pre_mode) &&
1856                                     get_mode_size_bits(pre_mode) == bits &&
1857                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1858                                         /* ok, this conv just changes to sign, moreover the calculation
1859                                          * is done with same number of bits as our address mode, so
1860                                          * we can ignore the conv as address calculation can be viewed
1861                                          * as either signed or unsigned
1862                                          */
1863                                         set_binop_right(n, pre);
1864                                 }
1865                         }
1866                 }
1867         }
1868         return n;
1869 }  /* transform_node_AddSub */
1870
1871 #define HANDLE_BINOP_PHI(op,a,b,c)                          \
1872   c = NULL;                                                 \
1873   if (is_Const(b) && is_const_Phi(a)) {                     \
1874     /* check for Op(Phi, Const) */                          \
1875     c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0);  \
1876   }                                                         \
1877   else if (is_Const(a) && is_const_Phi(b)) {                \
1878     /* check for Op(Const, Phi) */                          \
1879     c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1);  \
1880   }                                                         \
1881   if (c) {                                                  \
1882     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);             \
1883     return c;                                               \
1884   }
1885
1886 #define HANDLE_UNOP_PHI(op,a,c)                   \
1887   c = NULL;                                       \
1888   if (is_const_Phi(a)) {                          \
1889     /* check for Op(Phi) */                       \
1890     c = apply_unop_on_phi(a, op);                 \
1891     if (c) {                                      \
1892       DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1893       return c;                                   \
1894     }                                             \
1895   }
1896
1897 /**
1898  * Do the AddSub optimization, then Transform
1899  *   Constant folding on Phi
1900  *   Add(a,a)          -> Mul(a, 2)
1901  *   Add(Mul(a, x), a) -> Mul(a, x+1)
1902  * if the mode is integer or float.
1903  * Transform Add(a,-b) into Sub(a,b).
1904  * Reassociation might fold this further.
1905  */
1906 static ir_node *transform_node_Add(ir_node *n) {
1907         ir_mode *mode;
1908         ir_node *a, *b, *c, *oldn = n;
1909
1910         n = transform_node_AddSub(n);
1911
1912         a = get_Add_left(n);
1913         b = get_Add_right(n);
1914
1915         HANDLE_BINOP_PHI(tarval_add, a,b,c);
1916
1917         mode = get_irn_mode(n);
1918
1919         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1920         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1921                 return n;
1922
1923         if (mode_is_num(mode)) {
1924                 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1925                 if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
1926                         ir_node *block = get_irn_n(n, -1);
1927
1928                         n = new_rd_Mul(
1929                                 get_irn_dbg_info(n),
1930                                 current_ir_graph,
1931                                 block,
1932                                 a,
1933                                 new_r_Const_long(current_ir_graph, block, mode, 2),
1934                                 mode);
1935                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1936                         return n;
1937                 }
1938                 if (is_Minus(a)) {
1939                         n = new_rd_Sub(
1940                                         get_irn_dbg_info(n),
1941                                         current_ir_graph,
1942                                         get_irn_n(n, -1),
1943                                         b,
1944                                         get_Minus_op(a),
1945                                         mode);
1946                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1947                         return n;
1948                 }
1949                 if (is_Minus(b)) {
1950                         n = new_rd_Sub(
1951                                         get_irn_dbg_info(n),
1952                                         current_ir_graph,
1953                                         get_irn_n(n, -1),
1954                                         a,
1955                                         get_Minus_op(b),
1956                                         mode);
1957                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1958                         return n;
1959                 }
1960                 if (! get_opt_reassociation()) {
1961                         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1962                         if (is_Mul(a)) {
1963                                 ir_node *ma = get_Mul_left(a);
1964                                 ir_node *mb = get_Mul_right(a);
1965
1966                                 if (b == ma) {
1967                                         ir_node *blk = get_irn_n(n, -1);
1968                                         n = new_rd_Mul(
1969                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1970                                                         ma,
1971                                                         new_rd_Add(
1972                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1973                                                                 mb,
1974                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
1975                                                                 mode),
1976                                                         mode);
1977                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1978                                         return n;
1979                                 } else if (b == mb) {
1980                                         ir_node *blk = get_irn_n(n, -1);
1981                                         n = new_rd_Mul(
1982                                                         get_irn_dbg_info(n), current_ir_graph, blk,
1983                                                         mb,
1984                                                         new_rd_Add(
1985                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
1986                                                                 ma,
1987                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
1988                                                                 mode),
1989                                                         mode);
1990                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1991                                         return n;
1992                                 }
1993                         }
1994                         if (is_Mul(b)) {
1995                                 ir_node *ma = get_Mul_left(b);
1996                                 ir_node *mb = get_Mul_right(b);
1997
1998                                 if (a == ma) {
1999                                         ir_node *blk = get_irn_n(n, -1);
2000                                         n = new_rd_Mul(
2001                                                         get_irn_dbg_info(n), current_ir_graph, blk,
2002                                                         ma,
2003                                                         new_rd_Add(
2004                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
2005                                                                 mb,
2006                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2007                                                                 mode),
2008                                                         mode);
2009                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2010                                         return n;
2011                                 }
2012                                 if (a == mb) {
2013                                         ir_node *blk = get_irn_n(n, -1);
2014                                         n = new_rd_Mul(
2015                                                         get_irn_dbg_info(n), current_ir_graph, blk,
2016                                                         mb,
2017                                                         new_rd_Add(
2018                                                                 get_irn_dbg_info(n), current_ir_graph, blk,
2019                                                                 ma,
2020                                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2021                                                                 mode),
2022                                                         mode);
2023                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2024                                         return n;
2025                                 }
2026                         }
2027                 }
2028                 /* Here we rely on constants be on the RIGHT side */
2029                 if (get_mode_arithmetic(mode) == irma_twos_complement &&
2030                          is_Not(a) && classify_Const(b) == CNST_ONE) {
2031                         /* ~x + 1 = -x */
2032                         ir_node *op = get_Not_op(a);
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         }
2039         return n;
2040 }  /* transform_node_Add */
2041
2042 /**
2043  * Do the AddSub optimization, then Transform
2044  *   Constant folding on Phi
2045  *   Sub(0,a)          -> Minus(a)
2046  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2047  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2048  *   Sub(Add(a, x), x) -> a
2049  *   Sub(x, Add(x, a)) -> -a
2050  *   Sub(x, Const)     -> Add(x, -Const)
2051  */
2052 static ir_node *transform_node_Sub(ir_node *n) {
2053         ir_mode *mode;
2054         ir_node *oldn = n;
2055         ir_node *a, *b, *c;
2056
2057         n = transform_node_AddSub(n);
2058
2059         a = get_Sub_left(n);
2060         b = get_Sub_right(n);
2061
2062         mode = get_irn_mode(n);
2063
2064 restart:
2065         HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2066
2067         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2068         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2069                 return n;
2070
2071         /* Sub(a, Const) -> Add(a, -Const) */
2072         if (is_Const(b) && get_irn_mode(b) != mode_P) {
2073                 tarval *tv = get_Const_tarval(b);
2074
2075                 tv = tarval_neg(tv);
2076                 if(tv != tarval_bad) {
2077                         ir_node  *cnst  = new_Const(get_irn_mode(b), tv);
2078                         ir_node  *block = get_nodes_block(n);
2079                         dbg_info *dbgi  = get_irn_dbg_info(n);
2080                         ir_graph *irg   = get_irn_irg(n);
2081                         ir_node  *add   = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2082
2083                         return add;
2084                 }
2085         }
2086
2087         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2088         if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2089                 n = new_rd_Minus(
2090                                 get_irn_dbg_info(n),
2091                                 current_ir_graph,
2092                                 get_irn_n(n, -1),
2093                                 b,
2094                                 mode);
2095                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2096                 return n;
2097         }
2098         if (is_Add(a)) {
2099                 if (mode_wrap_around(mode)) {
2100                         ir_node *left  = get_Add_left(a);
2101                         ir_node *right = get_Add_right(a);
2102
2103                         /* FIXME: Does the Conv's work only for two complement or generally? */
2104                         if (left == b) {
2105                                 if (mode != get_irn_mode(right)) {
2106                                         /* This Sub is an effective Cast */
2107                                         right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2108                                 }
2109                                 n = right;
2110                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2111                                 return n;
2112                         } else if (right == b) {
2113                                 if (mode != get_irn_mode(left)) {
2114                                         /* This Sub is an effective Cast */
2115                                         left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2116                                 }
2117                                 n = left;
2118                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2119                                 return n;
2120                         }
2121                 }
2122         }
2123         if (is_Add(b)) {
2124                 if (mode_wrap_around(mode)) {
2125                         ir_node *left  = get_Add_left(b);
2126                         ir_node *right = get_Add_right(b);
2127
2128                         /* FIXME: Does the Conv's work only for two complement or generally? */
2129                         if (left == a) {
2130                                 ir_mode *r_mode = get_irn_mode(right);
2131
2132                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2133                                 if (mode != r_mode) {
2134                                         /* This Sub is an effective Cast */
2135                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2136                                 }
2137                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2138                                 return n;
2139                         } else if (right == a) {
2140                                 ir_mode *l_mode = get_irn_mode(left);
2141
2142                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2143                                 if (mode != l_mode) {
2144                                         /* This Sub is an effective Cast */
2145                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2146                                 }
2147                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2148                                 return n;
2149                         }
2150                 }
2151         }
2152         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2153                 ir_mode *mode = get_irn_mode(a);
2154
2155                 if (mode == get_irn_mode(b)) {
2156                         ir_mode *ma, *mb;
2157
2158                         a = get_Conv_op(a);
2159                         b = get_Conv_op(b);
2160
2161                         /* check if it's allowed to skip the conv */
2162                         ma = get_irn_mode(a);
2163                         mb = get_irn_mode(b);
2164
2165                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2166                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2167                                 set_Sub_left(n, a);
2168                                 set_Sub_right(n, b);
2169
2170                                 goto restart;
2171                         }
2172                 }
2173         }
2174         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2175         if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2176                 ir_node *ma = get_Mul_left(a);
2177                 ir_node *mb = get_Mul_right(a);
2178
2179                 if (ma == b) {
2180                         ir_node *blk = get_irn_n(n, -1);
2181                         n = new_rd_Mul(
2182                                         get_irn_dbg_info(n),
2183                                         current_ir_graph, blk,
2184                                         ma,
2185                                         new_rd_Sub(
2186                                                 get_irn_dbg_info(n),
2187                                                 current_ir_graph, blk,
2188                                                 mb,
2189                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2190                                                 mode),
2191                                         mode);
2192                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2193                         return n;
2194                 } else if (mb == b) {
2195                         ir_node *blk = get_irn_n(n, -1);
2196                         n = new_rd_Mul(
2197                                         get_irn_dbg_info(n),
2198                                         current_ir_graph, blk,
2199                                         mb,
2200                                         new_rd_Sub(
2201                                                 get_irn_dbg_info(n),
2202                                                 current_ir_graph, blk,
2203                                                 ma,
2204                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2205                                                 mode),
2206                                         mode);
2207                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2208                         return n;
2209                 }
2210         }
2211         if (is_Sub(a)) {
2212                 ir_node *x   = get_Sub_left(a);
2213                 ir_node *y   = get_Sub_right(a);
2214                 ir_node *blk = get_irn_n(n, -1);
2215                 ir_mode *m_b = get_irn_mode(b);
2216                 ir_mode *m_y = get_irn_mode(y);
2217                 ir_node *add;
2218
2219                 /* Determine the right mode for the Add. */
2220                 if (m_b == m_y)
2221                         mode = m_b;
2222                 else if (mode_is_reference(m_b))
2223                         mode = m_b;
2224                 else if (mode_is_reference(m_y))
2225                         mode = m_y;
2226                 else {
2227                         /*
2228                          * Both modes are different but none is reference,
2229                          * happens for instance in SubP(SubP(P, Iu), Is).
2230                          * We have two possibilities here: Cast or ignore.
2231                          * Currently we ignore this case.
2232                          */
2233                         return n;
2234                 }
2235
2236                 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2237
2238                 set_Sub_left(n, x);
2239                 set_Sub_right(n, add);
2240                 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2241                 return n;
2242         }
2243         return n;
2244 }  /* transform_node_Sub */
2245
2246 /**
2247  * Several transformation done on n*n=2n bits mul.
2248  * These transformations must be done here because new nodes may be produced.
2249  */
2250 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2251         ir_node *oldn = n;
2252         ir_node *a = get_Mul_left(n);
2253         ir_node *b = get_Mul_right(n);
2254         tarval *ta = value_of(a);
2255         tarval *tb = value_of(b);
2256         ir_mode *smode = get_irn_mode(a);
2257
2258         if (ta == get_mode_one(smode)) {
2259                 ir_node *blk = get_irn_n(n, -1);
2260                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2261                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2262                 return n;
2263         }
2264         else if (ta == get_mode_minus_one(smode)) {
2265                 ir_node *blk = get_irn_n(n, -1);
2266                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2267                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2268                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2269                 return n;
2270         }
2271         if (tb == get_mode_one(smode)) {
2272                 ir_node *blk = get_irn_n(a, -1);
2273                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2274                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2275                 return n;
2276         }
2277         else if (tb == get_mode_minus_one(smode)) {
2278                 ir_node *blk = get_irn_n(n, -1);
2279                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2280                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2281                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2282                 return n;
2283         }
2284         return n;
2285 }
2286
2287 /**
2288  * Transform Mul(a,-1) into -a.
2289  * Do constant evaluation of Phi nodes.
2290  * Do architecture dependent optimizations on Mul nodes
2291  */
2292 static ir_node *transform_node_Mul(ir_node *n) {
2293         ir_node *c, *oldn = n;
2294         ir_mode *mode = get_irn_mode(n);
2295         ir_node *a = get_Mul_left(n);
2296         ir_node *b = get_Mul_right(n);
2297
2298         if (is_Bad(a) || is_Bad(b))
2299                 return n;
2300
2301         if (mode != get_irn_mode(a))
2302                 return transform_node_Mul2n(n, mode);
2303
2304         HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2305
2306         if (mode_is_signed(mode)) {
2307                 ir_node *r = NULL;
2308
2309                 if (value_of(a) == get_mode_minus_one(mode))
2310                         r = b;
2311                 else if (value_of(b) == get_mode_minus_one(mode))
2312                         r = a;
2313                 if (r) {
2314                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2315                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2316                         return n;
2317                 }
2318         }
2319         if (get_mode_arithmetic(mode) == irma_ieee754) {
2320                 if (is_Const(a)) {
2321                         tarval *tv = get_Const_tarval(a);
2322                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2323                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2324                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2325                                 return n;
2326                         }
2327                 }
2328                 else if (is_Const(b)) {
2329                         tarval *tv = get_Const_tarval(b);
2330                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2331                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2332                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2333                                 return n;
2334                         }
2335                 }
2336         }
2337         return arch_dep_replace_mul_with_shifts(n);
2338 }  /* transform_node_Mul */
2339
2340 /**
2341  * Transform a Div Node.
2342  */
2343 static ir_node *transform_node_Div(ir_node *n) {
2344         tarval *tv = value_of(n);
2345         ir_mode *mode = get_Div_resmode(n);
2346         ir_node *value = n;
2347
2348         if (tv != tarval_bad) {
2349                 value = new_Const(get_tarval_mode(tv), tv);
2350
2351                 DBG_OPT_CSTEVAL(n, value);
2352                 goto make_tuple;
2353         } else {
2354                 ir_node *a = get_Div_left(n);
2355                 ir_node *b = get_Div_right(n);
2356                 ir_node *dummy;
2357
2358                 if (a == b && value_not_zero(a, &dummy)) {
2359                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2360                         value = new_Const(mode, get_mode_one(mode));
2361                         DBG_OPT_CSTEVAL(n, value);
2362                         goto make_tuple;
2363                 } else {
2364                         if (mode_is_signed(mode) && is_Const(b)) {
2365                                 tarval *tv = get_Const_tarval(b);
2366
2367                                 if (tv == get_mode_minus_one(mode)) {
2368                                         /* a / -1 */
2369                                         value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2370                                         DBG_OPT_CSTEVAL(n, value);
2371                                         goto make_tuple;
2372                                 }
2373                         }
2374                         /* Try architecture dependent optimization */
2375                         value = arch_dep_replace_div_by_const(n);
2376                 }
2377         }
2378
2379         if (value != n) {
2380                 ir_node *mem, *blk;
2381
2382 make_tuple:
2383                 /* Turn Div into a tuple (mem, jmp, bad, value) */
2384                 mem = get_Div_mem(n);
2385                 blk = get_irn_n(n, -1);
2386
2387                 /* skip a potential Pin */
2388                 if (is_Pin(mem))
2389                         mem = get_Pin_op(mem);
2390                 turn_into_tuple(n, pn_Div_max);
2391                 set_Tuple_pred(n, pn_Div_M,         mem);
2392                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2393                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());
2394                 set_Tuple_pred(n, pn_Div_res,       value);
2395         }
2396         return n;
2397 }  /* transform_node_Div */
2398
2399 /**
2400  * Transform a Mod node.
2401  */
2402 static ir_node *transform_node_Mod(ir_node *n) {
2403         tarval *tv = value_of(n);
2404         ir_mode *mode = get_Mod_resmode(n);
2405         ir_node *value = n;
2406
2407         if (tv != tarval_bad) {
2408                 value = new_Const(get_tarval_mode(tv), tv);
2409
2410                 DBG_OPT_CSTEVAL(n, value);
2411                 goto make_tuple;
2412         } else {
2413                 ir_node *a = get_Mod_left(n);
2414                 ir_node *b = get_Mod_right(n);
2415                 ir_node *dummy;
2416
2417                 if (a == b && value_not_zero(a, &dummy)) {
2418                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2419                         value = new_Const(mode, get_mode_null(mode));
2420                         DBG_OPT_CSTEVAL(n, value);
2421                         goto make_tuple;
2422                 } else {
2423                         if (mode_is_signed(mode) && is_Const(b)) {
2424                                 tarval *tv = get_Const_tarval(b);
2425
2426                                 if (tv == get_mode_minus_one(mode)) {
2427                                         /* a % -1 = 0 */
2428                                         value = new_Const(mode, get_mode_null(mode));
2429                                         DBG_OPT_CSTEVAL(n, value);
2430                                         goto make_tuple;
2431                                 }
2432                         }
2433                         /* Try architecture dependent optimization */
2434                         value = arch_dep_replace_mod_by_const(n);
2435                 }
2436         }
2437
2438         if (value != n) {
2439                 ir_node *mem, *blk;
2440
2441 make_tuple:
2442                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2443                 mem = get_Mod_mem(n);
2444                 blk = get_irn_n(n, -1);
2445
2446                 /* skip a potential Pin */
2447                 if (is_Pin(mem))
2448                         mem = get_Pin_op(mem);
2449                 turn_into_tuple(n, pn_Mod_max);
2450                 set_Tuple_pred(n, pn_Mod_M,         mem);
2451                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2452                 set_Tuple_pred(n, pn_Mod_X_except,  new_Bad());
2453                 set_Tuple_pred(n, pn_Mod_res,       value);
2454         }
2455         return n;
2456 }  /* transform_node_Mod */
2457
2458 /**
2459  * Transform a DivMod node.
2460  */
2461 static ir_node *transform_node_DivMod(ir_node *n) {
2462         ir_node *dummy;
2463         ir_node *a = get_DivMod_left(n);
2464         ir_node *b = get_DivMod_right(n);
2465         ir_mode *mode = get_DivMod_resmode(n);
2466         tarval *ta = value_of(a);
2467         tarval *tb = value_of(b);
2468         int evaluated = 0;
2469
2470         if (tb != tarval_bad) {
2471                 if (tb == get_mode_one(get_tarval_mode(tb))) {
2472                         b = new_Const(mode, get_mode_null(mode));
2473                         DBG_OPT_CSTEVAL(n, b);
2474                         goto make_tuple;
2475                 } else if (ta != tarval_bad) {
2476                         tarval *resa, *resb;
2477                         resa = tarval_div(ta, tb);
2478                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2479                                                              Jmp for X result!? */
2480                         resb = tarval_mod(ta, tb);
2481                         if (resb == tarval_bad) return n; /* Causes exception! */
2482                         a = new_Const(mode, resa);
2483                         b = new_Const(mode, resb);
2484                         DBG_OPT_CSTEVAL(n, a);
2485                         DBG_OPT_CSTEVAL(n, b);
2486                         goto make_tuple;
2487                 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2488                         a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2489                         b = new_Const(mode, get_mode_null(mode));
2490                         DBG_OPT_CSTEVAL(n, a);
2491                         DBG_OPT_CSTEVAL(n, b);
2492                         goto make_tuple;
2493                 } else { /* Try architecture dependent optimization */
2494                         arch_dep_replace_divmod_by_const(&a, &b, n);
2495                         evaluated = a != NULL;
2496                 }
2497         } else if (a == b) {
2498                 if (value_not_zero(a, &dummy)) {
2499                         /* a/a && a != 0 */
2500                         a = new_Const(mode, get_mode_one(mode));
2501                         b = new_Const(mode, get_mode_null(mode));
2502                         DBG_OPT_CSTEVAL(n, a);
2503                         DBG_OPT_CSTEVAL(n, b);
2504                         goto make_tuple;
2505                 } else {
2506                         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2507                         return n;
2508                 }
2509         } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2510                 /* 0 / non-Const = 0 */
2511                 b = a;
2512                 goto make_tuple;
2513         }
2514
2515         if (evaluated) { /* replace by tuple */
2516                 ir_node *mem, *blk;
2517
2518 make_tuple:
2519                 mem = get_DivMod_mem(n);
2520                 /* skip a potential Pin */
2521                 if (is_Pin(mem))
2522                         mem = get_Pin_op(mem);
2523
2524                 blk = get_irn_n(n, -1);
2525                 turn_into_tuple(n, pn_DivMod_max);
2526                 set_Tuple_pred(n, pn_DivMod_M,         mem);
2527                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2528                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());  /* no exception */
2529                 set_Tuple_pred(n, pn_DivMod_res_div,   a);
2530                 set_Tuple_pred(n, pn_DivMod_res_mod,  b);
2531         }
2532
2533         return n;
2534 }  /* transform_node_DivMod */
2535
2536 /**
2537  * Optimize x / c to x * (1/c)
2538  */
2539 static ir_node *transform_node_Quot(ir_node *n) {
2540         ir_mode *mode = get_Quot_resmode(n);
2541         ir_node *oldn = n;
2542
2543         if (get_mode_arithmetic(mode) == irma_ieee754) {
2544                 ir_node *b = get_Quot_right(n);
2545
2546                 if (is_Const(b)) {
2547                         tarval *tv = get_Const_tarval(b);
2548
2549                         tv = tarval_quo(get_mode_one(mode), tv);
2550
2551                         /* Do the transformation if the result is either exact or we are not
2552                            using strict rules. */
2553                         if (tv != tarval_bad &&
2554                             (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2555                                 ir_node *blk = get_irn_n(n, -1);
2556                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2557                                 ir_node *a = get_Quot_left(n);
2558                                 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2559                                 ir_node *mem = get_Quot_mem(n);
2560
2561                                 /* skip a potential Pin */
2562                                 if (is_Pin(mem))
2563                                         mem = get_Pin_op(mem);
2564                                 turn_into_tuple(n, pn_Quot_max);
2565                                 set_Tuple_pred(n, pn_Quot_M, mem);
2566                                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2567                                 set_Tuple_pred(n, pn_Quot_X_except,  new_r_Bad(current_ir_graph));
2568                                 set_Tuple_pred(n, pn_Quot_res, m);
2569                                 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2570                         }
2571                 }
2572         }
2573         return n;
2574 }  /* transform_node_Quot */
2575
2576 /**
2577  * Optimize Abs(x) into  x if x is Confirmed >= 0
2578  * Optimize Abs(x) into -x if x is Confirmed <= 0
2579  */
2580 static ir_node *transform_node_Abs(ir_node *n) {
2581         ir_node        *oldn = n;
2582         ir_node        *a = get_Abs_op(n);
2583         value_classify_sign sign = classify_value_sign(a);
2584
2585         if (sign == value_classified_negative) {
2586                 ir_mode *mode = get_irn_mode(n);
2587
2588                 /*
2589                  * We can replace the Abs by -x here.
2590                  * We even could add a new Confirm here.
2591                  *
2592                  * Note that -x would create a new node, so we could
2593                  * not run it in the equivalent_node() context.
2594                  */
2595                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2596                                 get_irn_n(n, -1), a, mode);
2597
2598                 DBG_OPT_CONFIRM(oldn, n);
2599         } else if (sign == value_classified_positive) {
2600                 /* n is positive, Abs is not needed */
2601                 n = a;
2602
2603                 DBG_OPT_CONFIRM(oldn, n);
2604         }
2605
2606         return n;
2607 }  /* transform_node_Abs */
2608
2609 /**
2610  * Transform a Cond node.
2611  *
2612  * Replace the Cond by a Jmp if it branches on a constant
2613  * condition.
2614  */
2615 static ir_node *transform_node_Cond(ir_node *n) {
2616
2617         ir_node *jmp;
2618         ir_node *a = get_Cond_selector(n);
2619         tarval *ta = value_of(a);
2620
2621         /* we need block info which is not available in floating irgs */
2622         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2623                 return n;
2624
2625         if ((ta != tarval_bad) &&
2626             (get_irn_mode(a) == mode_b) &&
2627             (get_opt_unreachable_code())) {
2628                 /* It's a boolean Cond, branching on a boolean constant.
2629                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2630                 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2631                 turn_into_tuple(n, pn_Cond_max);
2632                 if (ta == tarval_b_true) {
2633                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
2634                         set_Tuple_pred(n, pn_Cond_true, jmp);
2635                 } else {
2636                         set_Tuple_pred(n, pn_Cond_false, jmp);
2637                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
2638                 }
2639                 /* We might generate an endless loop, so keep it alive. */
2640                 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2641         }
2642         return n;
2643 }  /* transform_node_Cond */
2644
2645 typedef ir_node* (*recursive_transform) (ir_node *n);
2646
2647 /**
2648  * makes use of distributive laws for and, or, eor
2649  *     and(a OP c, b OP c) -> and(a, b) OP c
2650  * note, might return a different op than n
2651  */
2652 static ir_node *transform_bitwise_distributive(ir_node *n,
2653                                                recursive_transform trans_func)
2654 {
2655         ir_node *oldn    = n;
2656         ir_node *a       = get_binop_left(n);
2657         ir_node *b       = get_binop_right(n);
2658         ir_op   *op      = get_irn_op(a);
2659         ir_op   *op_root = get_irn_op(n);
2660
2661         if(op != get_irn_op(b))
2662                 return n;
2663
2664         if (op == op_Conv) {
2665                 ir_node *a_op   = get_Conv_op(a);
2666                 ir_node *b_op   = get_Conv_op(b);
2667                 ir_mode *a_mode = get_irn_mode(a_op);
2668                 ir_mode *b_mode = get_irn_mode(b_op);
2669                 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2670                         ir_node *blk = get_irn_n(n, -1);
2671
2672                         n = exact_copy(n);
2673                         set_binop_left(n, a_op);
2674                         set_binop_right(n, b_op);
2675                         set_irn_mode(n, a_mode);
2676                         n = trans_func(n);
2677                         n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2678
2679                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2680                         return n;
2681                 }
2682         }
2683
2684         if (op == op_Eor) {
2685                 /* nothing to gain here */
2686                 return n;
2687         }
2688
2689         if (op == op_Shrs || op == op_Shr || op == op_Shl
2690                         || op == op_And || op == op_Or || op == op_Eor) {
2691                 ir_node *a_left  = get_binop_left(a);
2692                 ir_node *a_right = get_binop_right(a);
2693                 ir_node *b_left  = get_binop_left(b);
2694                 ir_node *b_right = get_binop_right(b);
2695                 ir_node *c       = NULL;
2696                 ir_node *op1, *op2;
2697
2698                 if (is_op_commutative(op)) {
2699                         if (a_left == b_left) {
2700                                 c   = a_left;
2701                                 op1 = a_right;
2702                                 op2 = b_right;
2703                         } else if(a_left == b_right) {
2704                                 c   = a_left;
2705                                 op1 = a_right;
2706                                 op2 = b_left;
2707                         } else if(a_right == b_left) {
2708                                 c   = a_right;
2709                                 op1 = a_left;
2710                                 op2 = b_right;
2711                         }
2712                 }
2713                 if(a_right == b_right) {
2714                         c   = a_right;
2715                         op1 = a_left;
2716                         op2 = b_left;
2717                 }
2718
2719                 if (c != NULL) {
2720                         /* (a sop c) & (b sop c) => (a & b) sop c */
2721                         ir_node *blk = get_irn_n(n, -1);
2722
2723                         ir_node *new_n = exact_copy(n);
2724                         set_binop_left(new_n, op1);
2725                         set_binop_right(new_n, op2);
2726                         new_n = trans_func(new_n);
2727
2728                         if(op_root == op_Eor && op == op_Or) {
2729                                 dbg_info  *dbgi = get_irn_dbg_info(n);
2730                                 ir_graph  *irg  = current_ir_graph;
2731                                 ir_mode   *mode = get_irn_mode(c);
2732
2733                                 c = new_rd_Not(dbgi, irg, blk, c, mode);
2734                                 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2735                         } else {
2736                                 n = exact_copy(a);
2737                                 set_irn_n(n, -1, blk);
2738                                 set_binop_left(n, new_n);
2739                                 set_binop_right(n, c);
2740                         }
2741
2742
2743                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2744                         return n;
2745                 }
2746         }
2747
2748         return n;
2749 }
2750
2751 /**
2752  * Transform an And.
2753  */
2754 static ir_node *transform_node_And(ir_node *n) {
2755         ir_node *c, *oldn;
2756         ir_node *a = get_And_left(n);
2757         ir_node *b = get_And_right(n);
2758
2759         HANDLE_BINOP_PHI(tarval_and, a,b,c);
2760
2761         /* we can evaluate 2 Projs of the same Cmp */
2762         if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2763                 ir_node *pred_a = get_Proj_pred(a);
2764                 ir_node *pred_b = get_Proj_pred(b);
2765                 if(pred_a == pred_b) {
2766                         dbg_info *dbgi  = get_irn_dbg_info(n);
2767                         ir_node  *block = get_nodes_block(pred_a);
2768                         pn_Cmp pn_a     = get_Proj_proj(a);
2769                         pn_Cmp pn_b     = get_Proj_proj(b);
2770                         /* yes, we can simply calculate with pncs */
2771                         pn_Cmp new_pnc  = pn_a & pn_b;
2772
2773                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2774                                            new_pnc);
2775                 }
2776         }
2777
2778         n = transform_bitwise_distributive(n, transform_node_And);
2779
2780         return n;
2781 }  /* transform_node_And */
2782
2783 /**
2784  * Transform an Eor.
2785  */
2786 static ir_node *transform_node_Eor(ir_node *n) {
2787         ir_node *c, *oldn = n;
2788         ir_node *a = get_Eor_left(n);
2789         ir_node *b = get_Eor_right(n);
2790         ir_mode *mode = get_irn_mode(n);
2791
2792         HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2793
2794         /* we can evaluate 2 Projs of the same Cmp */
2795         if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2796                 ir_node *pred_a = get_Proj_pred(a);
2797                 ir_node *pred_b = get_Proj_pred(b);
2798                 if(pred_a == pred_b) {
2799                         dbg_info *dbgi  = get_irn_dbg_info(n);
2800                         ir_node  *block = get_nodes_block(pred_a);
2801                         pn_Cmp pn_a     = get_Proj_proj(a);
2802                         pn_Cmp pn_b     = get_Proj_proj(b);
2803                         /* yes, we can simply calculate with pncs */
2804                         pn_Cmp new_pnc  = pn_a ^ pn_b;
2805
2806                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2807                                            new_pnc);
2808                 }
2809         }
2810
2811         if (a == b) {
2812                 /* a ^ a = 0 */
2813                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2814                                  mode, get_mode_null(mode));
2815                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2816         } else if ((mode == mode_b)
2817                    && (get_irn_op(a) == op_Proj)
2818                    && (get_irn_mode(a) == mode_b)
2819                    && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2820                    && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2821                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2822                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2823                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2824
2825                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2826         } else if ((mode == mode_b)
2827                && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2828                 /* The Eor is a Not. Replace it by a Not. */
2829                 /*   ????!!!Extend to bitfield 1111111. */
2830                 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2831
2832                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2833         } else {
2834                 n = transform_bitwise_distributive(n, transform_node_Eor);
2835         }
2836
2837         return n;
2838 }  /* transform_node_Eor */
2839
2840 /**
2841  * Transform a Not.
2842  */
2843 static ir_node *transform_node_Not(ir_node *n) {
2844         ir_node *c, *oldn = n;
2845         ir_node *a = get_Not_op(n);
2846         ir_op *op_a = get_irn_op(a);
2847
2848         HANDLE_UNOP_PHI(tarval_not,a,c);
2849
2850         /* check for a boolean Not */
2851         if (   (get_irn_mode(n) == mode_b)
2852             && (op_a == op_Proj)
2853             && (get_irn_mode(a) == mode_b)
2854             && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2855                 /* We negate a Cmp. The Cmp has the negated result anyways! */
2856                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2857                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2858                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2859                 return n;
2860         }
2861         if (op_a == op_Sub && classify_Const(get_Sub_right(a)) == CNST_ONE) {
2862                 /* ~(x-1) = -x */
2863                 ir_node *op = get_Sub_left(a);
2864                 ir_node *blk = get_irn_n(n, -1);
2865                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
2866                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
2867         }
2868         return n;
2869 }  /* transform_node_Not */
2870
2871 /**
2872  * Transform a Minus.
2873  * Optimize:
2874  *   -(~x) = x + 1
2875  */
2876 static ir_node *transform_node_Minus(ir_node *n) {
2877         ir_node *c, *oldn = n;
2878         ir_node *a = get_Minus_op(n);
2879         ir_mode *mode;
2880
2881         HANDLE_UNOP_PHI(tarval_neg,a,c);
2882
2883         mode = get_irn_mode(a);
2884         if (get_mode_arithmetic(mode) == irma_twos_complement && is_Not(a)) {
2885                 /* -(~x) = x + 1 */
2886                 ir_node *op   = get_Not_op(a);
2887                 tarval *tv    = get_mode_one(mode);
2888                 ir_node *blk  = get_irn_n(n, -1);
2889                 ir_node *c    = new_r_Const(current_ir_graph, blk, mode, tv);
2890                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
2891                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
2892         } else if (is_Sub(a)) {
2893                 /* - (a-b) = b - a */
2894                 ir_node *la  = get_Sub_left(a);
2895                 ir_node *ra  = get_Sub_right(a);
2896                 ir_node *blk = get_irn_n(n, -1);
2897
2898                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
2899                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
2900         }
2901
2902         return n;
2903 }  /* transform_node_Minus */
2904
2905 /**
2906  * Transform a Cast_type(Const) into a new Const_type
2907  */
2908 static ir_node *transform_node_Cast(ir_node *n) {
2909         ir_node *oldn = n;
2910         ir_node *pred = get_Cast_op(n);
2911         ir_type *tp = get_irn_type(n);
2912
2913         if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2914                 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2915                         get_Const_tarval(pred), tp);
2916                 DBG_OPT_CSTEVAL(oldn, n);
2917         } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2918                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2919                         get_SymConst_kind(pred), tp);
2920                 DBG_OPT_CSTEVAL(oldn, n);
2921         }
2922
2923         return n;
2924 }  /* transform_node_Cast */
2925
2926 /**
2927  * Transform a Proj(Div) with a non-zero value.
2928  * Removes the exceptions and routes the memory to the NoMem node.
2929  */
2930 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2931         ir_node *div = get_Proj_pred(proj);
2932         ir_node *b   = get_Div_right(div);
2933         ir_node *confirm, *res, *new_mem;
2934         long proj_nr;
2935
2936         if (value_not_zero(b, &confirm)) {
2937                 /* div(x, y) && y != 0 */
2938                 proj_nr = get_Proj_proj(proj);
2939                 switch (proj_nr) {
2940                 case pn_Div_X_regular:
2941                         return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
2942
2943                 case pn_Div_X_except:
2944                         /* we found an exception handler, remove it */
2945                         DBG_OPT_EXC_REM(proj);
2946                         return new_Bad();
2947
2948                 case pn_Div_M:
2949                         res = get_Div_mem(div);
2950                         new_mem = get_irg_no_mem(current_ir_graph);
2951
2952                         if (confirm) {
2953                                 /* This node can only float up to the Confirm block */
2954                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2955                         }
2956                         set_irn_pinned(div, op_pin_state_floats);
2957                         /* this is a Div without exception, we can remove the memory edge */
2958                         set_Div_mem(div, new_mem);
2959                         return res;
2960                 }
2961         }
2962         return proj;
2963 }  /* transform_node_Proj_Div */
2964
2965 /**
2966  * Transform a Proj(Mod) with a non-zero value.
2967  * Removes the exceptions and routes the memory to the NoMem node.
2968  */
2969 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2970         ir_node *mod = get_Proj_pred(proj);
2971         ir_node *b   = get_Mod_right(mod);
2972         ir_node *confirm, *res, *new_mem;
2973         long proj_nr;
2974
2975         if (value_not_zero(b, &confirm)) {
2976                 /* mod(x, y) && y != 0 */
2977                 proj_nr = get_Proj_proj(proj);
2978
2979                 switch (proj_nr) {
2980
2981                 case pn_Mod_X_regular:
2982                         return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
2983
2984                 case pn_Mod_X_except:
2985                         /* we found an exception handler, remove it */
2986                         DBG_OPT_EXC_REM(proj);
2987                         return new_Bad();
2988
2989                 case pn_Mod_M:
2990                         res = get_Mod_mem(mod);
2991                         new_mem = get_irg_no_mem(current_ir_graph);
2992
2993                         if (confirm) {
2994                                 /* This node can only float up to the Confirm block */
2995                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2996                         }
2997                         set_irn_pinned(mod, op_pin_state_floats);
2998                         /* this is a Mod without exception, we can remove the memory edge */
2999                         set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
3000                         return res;
3001                 case pn_Mod_res:
3002                         if (get_Mod_left(mod) == b) {
3003                                 /* a % a = 0 if a != 0 */
3004                                 ir_mode *mode = get_irn_mode(proj);
3005                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3006
3007                                 DBG_OPT_CSTEVAL(mod, res);
3008                                 return res;
3009                         }
3010                 }
3011         }
3012         return proj;
3013 }  /* transform_node_Proj_Mod */
3014
3015 /**
3016  * Transform a Proj(DivMod) with a non-zero value.
3017  * Removes the exceptions and routes the memory to the NoMem node.
3018  */
3019 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3020         ir_node *divmod = get_Proj_pred(proj);
3021         ir_node *b      = get_DivMod_right(divmod);
3022         ir_node *confirm, *res, *new_mem;
3023         long proj_nr;
3024
3025         if (value_not_zero(b, &confirm)) {
3026                 /* DivMod(x, y) && y != 0 */
3027                 proj_nr = get_Proj_proj(proj);
3028
3029                 switch (proj_nr) {
3030
3031                 case pn_DivMod_X_regular:
3032                         return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3033
3034                 case pn_DivMod_X_except:
3035                         /* we found an exception handler, remove it */
3036                         DBG_OPT_EXC_REM(proj);
3037                         return new_Bad();
3038
3039                 case pn_DivMod_M:
3040                         res = get_DivMod_mem(divmod);
3041                         new_mem = get_irg_no_mem(current_ir_graph);
3042
3043                         if (confirm) {
3044                                 /* This node can only float up to the Confirm block */
3045                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3046                         }
3047                         set_irn_pinned(divmod, op_pin_state_floats);
3048                         /* this is a DivMod without exception, we can remove the memory edge */
3049                         set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
3050                         return res;
3051
3052                 case pn_DivMod_res_mod:
3053                         if (get_DivMod_left(divmod) == b) {
3054                                 /* a % a = 0 if a != 0 */
3055                                 ir_mode *mode = get_irn_mode(proj);
3056                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3057
3058                                 DBG_OPT_CSTEVAL(divmod, res);
3059                                 return res;
3060                         }
3061                 }
3062         }
3063         return proj;
3064 }  /* transform_node_Proj_DivMod */
3065
3066 /**
3067  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3068  */
3069 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3070         if (get_opt_unreachable_code()) {
3071                 ir_node *n = get_Proj_pred(proj);
3072                 ir_node *b = get_Cond_selector(n);
3073
3074                 if (mode_is_int(get_irn_mode(b))) {
3075                         tarval *tb = value_of(b);
3076
3077                         if (tb != tarval_bad) {
3078                                 /* we have a constant switch */
3079                                 long num = get_Proj_proj(proj);
3080
3081                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3082                                         if (get_tarval_long(tb) == num) {
3083                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
3084                                                  * in a block. This case is optimized away in optimize_cf(). */
3085                                                 return proj;
3086                                         } else {
3087                                                 /* this case will NEVER be taken, kill it */
3088                                                 return new_Bad();
3089                                         }
3090                                 }
3091                         }
3092                 }
3093         }
3094         return proj;
3095 }  /* transform_node_Proj_Cond */
3096
3097 /**
3098  * Normalizes and optimizes Cmp nodes.
3099  */
3100 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3101         ir_node *n     = get_Proj_pred(proj);
3102         ir_node *left  = get_Cmp_left(n);
3103         ir_node *right = get_Cmp_right(n);
3104         ir_node *c     = NULL;
3105         tarval *tv     = NULL;
3106         int changed    = 0;
3107         ir_mode *mode  = NULL;
3108         long proj_nr   = get_Proj_proj(proj);
3109
3110         /* we can evaluate this direct */
3111         switch(proj_nr) {
3112         case pn_Cmp_False:
3113                 return new_Const(mode_b, get_tarval_b_false());
3114         case pn_Cmp_True:
3115                 return new_Const(mode_b, get_tarval_b_true());
3116         case pn_Cmp_Leg:
3117                 if(!mode_is_float(get_irn_mode(left)))
3118                         return new_Const(mode_b, get_tarval_b_true());
3119                 break;
3120         default:
3121                 break;
3122         }
3123
3124         /* Remove unnecessary conversions */
3125         /* TODO handle constants */
3126         if (is_Conv(left) && is_Conv(right)) {
3127                 ir_mode* mode        = get_irn_mode(left);
3128                 ir_node* op_left     = get_Conv_op(left);
3129                 ir_node* op_right    = get_Conv_op(right);
3130                 ir_mode* mode_left   = get_irn_mode(op_left);
3131                 ir_mode* mode_right  = get_irn_mode(op_right);
3132
3133                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3134                         ir_graph* irg   = current_ir_graph;
3135                         ir_node*  block = get_nodes_block(n);
3136                         ir_node*  new_left;
3137                         ir_node*  new_right;
3138
3139                         if (mode_left == mode_right) {
3140                                 new_left  = op_left;
3141                                 new_right = op_right;
3142                         } else if (smaller_mode(mode_left, mode_right)) {
3143                                 new_left  = new_r_Conv(irg, block, op_left, mode_right);
3144                                 new_right = op_right;
3145                         } else if (smaller_mode(mode_right, mode_left)) {
3146                                 new_left  = op_left;
3147                                 new_right = new_r_Conv(irg, block, op_right, mode_left);
3148                         } else {
3149                                 goto no_remove_conv;
3150                         }
3151                         left  = new_left;
3152                         right = new_right;
3153                         set_Cmp_left( n, left);
3154                         set_Cmp_right(n, right);
3155 no_remove_conv:;
3156                 }
3157         }
3158
3159         if (get_irn_mode(left) == mode_b) {
3160                 ir_graph* irg   = current_ir_graph;
3161                 ir_node*  block = get_nodes_block(n);
3162
3163                 switch (proj_nr) {
3164                         case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3165                         case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3166                         case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3167                         case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3168                         case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3169                         case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3170                 }
3171         }
3172
3173         if (!get_opt_reassociation())
3174                 return proj;
3175
3176         /*
3177          * First step: normalize the compare op
3178          * by placing the constant on the right site
3179          * or moving the lower address node to the left.
3180          * We ignore the case that both are constants
3181          * this case should be optimized away.
3182          */
3183         if (get_irn_op(right) == op_Const) {
3184                 c = right;
3185         } else if (get_irn_op(left) == op_Const) {
3186                 c     = left;
3187                 left  = right;
3188                 right = c;
3189
3190                 proj_nr = get_inversed_pnc(proj_nr);
3191                 changed |= 1;
3192         } else if (get_irn_idx(left) > get_irn_idx(right)) {
3193                 ir_node *t = left;
3194
3195                 left  = right;
3196                 right = t;
3197
3198                 proj_nr = get_inversed_pnc(proj_nr);
3199                 changed |= 1;
3200         }
3201
3202         /*
3203          * Second step: Try to reduce the magnitude
3204          * of a constant. This may help to generate better code
3205          * later and may help to normalize more compares.
3206          * Of course this is only possible for integer values.
3207          */
3208         if (c) {
3209                 mode = get_irn_mode(c);
3210                 tv = get_Const_tarval(c);
3211
3212                 if (tv != tarval_bad) {
3213                         /* the following optimization is possible on modes without Overflow
3214                          * on Unary Minus or on == and !=:
3215                          * -a CMP c  ==>  a swap(CMP) -c
3216                          *
3217                          * Beware: for two-complement Overflow may occur, so only == and != can
3218                          * be optimized, see this:
3219                          * -MININT < 0 =/=> MININT > 0 !!!
3220                          */
3221                         if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
3222                                 (!mode_overflow_on_unary_Minus(mode) ||
3223                                 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3224                                 left = get_Minus_op(left);
3225                                 tv = tarval_neg(tv);
3226
3227                                 if (tv != tarval_bad) {
3228                                         proj_nr = get_inversed_pnc(proj_nr);
3229                                         changed |= 2;
3230                                 }
3231                         }
3232
3233                         /* for integer modes, we have more */
3234                         if (mode_is_int(mode)) {
3235                                 /* Ne includes Unordered which is not possible on integers.
3236                                  * However, frontends often use this wrong, so fix it here */
3237                                 if (proj_nr & pn_Cmp_Uo) {
3238                                         proj_nr &= ~pn_Cmp_Uo;
3239                                         set_Proj_proj(proj, proj_nr);
3240                                 }
3241
3242                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
3243                                 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3244                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3245                                         tv = tarval_sub(tv, get_mode_one(mode));
3246
3247                                         if (tv != tarval_bad) {
3248                                                 proj_nr ^= pn_Cmp_Eq;
3249                                                 changed |= 2;
3250                                         }
3251                                 }
3252                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
3253                                 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3254                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3255                                         tv = tarval_add(tv, get_mode_one(mode));
3256
3257                                         if (tv != tarval_bad) {
3258                                                 proj_nr ^= pn_Cmp_Eq;
3259                                                 changed |= 2;
3260                                         }
3261                                 }
3262
3263                                 /* the following reassociations work only for == and != */
3264                                 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3265
3266                                         /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
3267                                         if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
3268                                                 right = get_Sub_right(left);
3269                                                 left  = get_Sub_left(left);
3270
3271                                                 tv = value_of(right);
3272                                                 if (tv != tarval_bad) {
3273                                                         changed = 1;
3274                                                 }
3275                                         }
3276
3277                                         if (tv != tarval_bad) {
3278                                                 ir_op *op = get_irn_op(left);
3279
3280                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
3281                                                 if (op == op_Sub) {
3282                                                         ir_node *c1 = get_Sub_right(left);
3283                                                         tarval *tv2 = value_of(c1);
3284
3285                                                         if (tv2 != tarval_bad) {
3286                                                                 tv2 = tarval_add(tv, value_of(c1));
3287
3288                                                                 if (tv2 != tarval_bad) {
3289                                                                         left    = get_Sub_left(left);
3290                                                                         tv      = tv2;
3291                                                                         changed |= 2;
3292                                                                 }
3293                                                         }
3294                                                 }
3295                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
3296                                                 else if (op == op_Add) {
3297                                                         ir_node *a_l = get_Add_left(left);
3298                                                         ir_node *a_r = get_Add_right(left);
3299                                                         ir_node *a;
3300                                                         tarval *tv2;
3301
3302                                                         if (get_irn_op(a_l) == op_Const) {
3303                                                                 a = a_r;
3304                                                                 tv2 = value_of(a_l);
3305                                                         } else {
3306                                                                 a = a_l;
3307                                                                 tv2 = value_of(a_r);
3308                                                         }
3309
3310                                                         if (tv2 != tarval_bad) {
3311                                                                 tv2 = tarval_sub(tv, tv2);
3312
3313                                                                 if (tv2 != tarval_bad) {
3314                                                                         left    = a;
3315                                                                         tv      = tv2;
3316                                                                         changed |= 2;
3317                                                                 }
3318                                                         }
3319                                                 }
3320                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
3321                                                 else if (op == op_Minus) {
3322                                                         tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3323
3324                                                         if (tv2 != tarval_bad) {
3325                                                                 left    = get_Minus_op(left);
3326                                                                 tv      = tv2;
3327                                                                 changed |= 2;
3328                                                         }
3329                                                 }
3330                                         }
3331                                 } /* == or != */
3332                                 /* the following reassociations work only for <= */
3333                                 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3334                                         if (tv != tarval_bad) {
3335                                                 ir_op *op = get_irn_op(left);
3336
3337                                                 /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
3338                                                 if (op == op_Abs) {
3339                                                 }
3340                                         }
3341                                 }
3342                         } /* mode_is_int */
3343
3344                         /*
3345                          * optimization for AND:
3346                          * Optimize:
3347                          *   And(x, C) == C  ==>  And(x, C) != 0
3348                          *   And(x, C) != C  ==>  And(X, C) == 0
3349                          *
3350                          * if C is a single Bit constant.
3351                          */
3352                         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
3353                                 (get_irn_op(left) == op_And)) {
3354                                 if (tarval_is_single_bit(tv)) {
3355                                         /* check for Constant's match. We have check hare the tarvals,
3356                                            because our const might be changed */
3357                                         ir_node *la = get_And_left(left);
3358                                         ir_node *ra = get_And_right(left);
3359                                         if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3360                                                 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3361                                                         /* fine: do the transformation */
3362                                                         tv = get_mode_null(get_tarval_mode(tv));
3363                                                         proj_nr ^= pn_Cmp_Leg;
3364                                                         changed |= 2;
3365                                         }
3366                                 }
3367                         }
3368                 } /* tarval != bad */
3369         }
3370
3371         if (changed) {
3372                 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3373
3374                 if (changed & 2)      /* need a new Const */
3375                         right = new_Const(mode, tv);
3376
3377                 /* create a new compare */
3378                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
3379                         left, right);
3380
3381                 set_Proj_pred(proj, n);
3382                 set_Proj_proj(proj, proj_nr);
3383         }
3384
3385         return proj;
3386 }  /* transform_node_Proj_Cmp */
3387
3388 /**
3389  * Does all optimizations on nodes that must be done on it's Proj's
3390  * because of creating new nodes.
3391  */
3392 static ir_node *transform_node_Proj(ir_node *proj) {
3393         ir_node *n = get_Proj_pred(proj);
3394
3395         switch (get_irn_opcode(n)) {
3396         case iro_Div:
3397                 return transform_node_Proj_Div(proj);
3398
3399         case iro_Mod:
3400                 return transform_node_Proj_Mod(proj);
3401
3402         case iro_DivMod:
3403                 return transform_node_Proj_DivMod(proj);
3404
3405         case iro_Cond:
3406                 return transform_node_Proj_Cond(proj);
3407
3408         case iro_Cmp:
3409                 return transform_node_Proj_Cmp(proj);
3410
3411         case iro_Tuple:
3412                 /* should not happen, but if it does will be optimized away */
3413                 return equivalent_node_Proj(proj);
3414
3415         default:
3416                 /* do nothing */
3417                 return proj;
3418         }
3419 }  /* transform_node_Proj */
3420
3421 /**
3422  * Move Confirms down through Phi nodes.
3423  */
3424 static ir_node *transform_node_Phi(ir_node *phi) {
3425         int i, n;
3426         ir_mode *mode = get_irn_mode(phi);
3427
3428         if (mode_is_reference(mode)) {
3429                 n = get_irn_arity(phi);
3430
3431                 /* Beware of Phi0 */
3432                 if (n > 0) {
3433                         ir_node *pred = get_irn_n(phi, 0);
3434                         ir_node *bound, *new_Phi, *block, **in;
3435                         pn_Cmp  pnc;
3436
3437                         if (! is_Confirm(pred))
3438                                 return phi;
3439
3440                         bound = get_Confirm_bound(pred);
3441                         pnc   = get_Confirm_cmp(pred);
3442
3443                         NEW_ARR_A(ir_node *, in, n);
3444                         in[0] = get_Confirm_value(pred);
3445
3446                         for (i = 1; i < n; ++i) {
3447                                 pred = get_irn_n(phi, i);
3448
3449                                 if (! is_Confirm(pred) ||
3450                                         get_Confirm_bound(pred) != bound ||
3451                                         get_Confirm_cmp(pred) != pnc)
3452                                         return phi;
3453                                 in[i] = get_Confirm_value(pred);
3454                         }
3455                         /* move the Confirm nodes "behind" the Phi */
3456                         block = get_irn_n(phi, -1);
3457                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3458                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3459                 }
3460         }
3461         return phi;
3462 }  /* transform_node_Phi */
3463
3464 /**
3465  * Returns the operands of a commutative bin-op, if one operand is
3466  * a const, it is returned as the second one.
3467  */
3468 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3469         ir_node *op_a = get_binop_left(binop);
3470         ir_node *op_b = get_binop_right(binop);
3471
3472         assert(is_op_commutative(get_irn_op(binop)));
3473
3474         if (get_irn_op(op_a) == op_Const) {
3475                 *a = op_b;
3476                 *c = op_a;
3477         } else {
3478                 *a = op_a;
3479                 *c = op_b;
3480         }
3481 }  /* get_comm_Binop_Ops */
3482
3483 /**
3484  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3485  * Such pattern may arise in bitfield stores.
3486  *
3487  * value  c4                  value      c4 & c2
3488  *    AND     c3                    AND           c1 | c3
3489  *        OR     c2      ===>               OR
3490  *           AND    c1
3491  *               OR
3492  *
3493  *
3494  * value  c2                 value  c1
3495  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
3496  *        OR
3497  */
3498 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3499         ir_node *and, *c1;
3500         ir_node *or_l, *c2;
3501         ir_node *and_l, *c3;
3502         ir_node *value, *c4;
3503         ir_node *new_and, *new_const, *block;
3504         ir_mode *mode = get_irn_mode(or);
3505
3506         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3507
3508         while (1) {
3509                 get_comm_Binop_Ops(or, &and, &c1);
3510                 if (!is_Const(c1) || !is_And(and))
3511                         return or;
3512
3513                 get_comm_Binop_Ops(and, &or_l, &c2);
3514                 if (!is_Const(c2))
3515                         return or;
3516
3517                 tv1 = get_Const_tarval(c1);
3518                 tv2 = get_Const_tarval(c2);
3519
3520                 tv = tarval_or(tv1, tv2);
3521                 if (classify_tarval(tv) == TV_CLASSIFY_ALL_ONE) {
3522                         /* the AND does NOT clear a bit with isn't set be the OR */
3523                         set_Or_left(or, or_l);
3524                         set_Or_right(or, c1);
3525
3526                         /* check for more */
3527                         continue;
3528                 }
3529
3530                 if (!is_Or(or_l))
3531                         return or;
3532
3533                 get_comm_Binop_Ops(or_l, &and_l, &c3);
3534                 if (!is_Const(c3) || !is_And(and_l))
3535                         return or;
3536
3537                 get_comm_Binop_Ops(and_l, &value, &c4);
3538                 if (!is_Const(c4))
3539                         return or;
3540
3541                 /* ok, found the pattern, check for conditions */
3542                 assert(mode == get_irn_mode(and));
3543                 assert(mode == get_irn_mode(or_l));
3544                 assert(mode == get_irn_mode(and_l));
3545
3546                 tv3 = get_Const_tarval(c3);
3547                 tv4 = get_Const_tarval(c4);
3548
3549                 tv = tarval_or(tv4, tv2);
3550                 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
3551                         /* have at least one 0 at the same bit position */
3552                         return or;
3553                 }
3554
3555                 n_tv4 = tarval_not(tv4);
3556                 if (tv3 != tarval_and(tv3, n_tv4)) {
3557                         /* bit in the or_mask is outside the and_mask */
3558                         return or;
3559                 }
3560
3561                 n_tv2 = tarval_not(tv2);
3562                 if (tv1 != tarval_and(tv1, n_tv2)) {
3563                         /* bit in the or_mask is outside the and_mask */
3564                         return or;
3565                 }
3566
3567                 /* ok, all conditions met */
3568                 block = get_irn_n(or, -1);
3569
3570                 new_and = new_r_And(current_ir_graph, block,
3571                         value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3572
3573                 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3574
3575                 set_Or_left(or, new_and);
3576                 set_Or_right(or, new_const);
3577
3578                 /* check for more */
3579         }
3580 }  /* transform_node_Or_bf_store */
3581
3582 /**
3583  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3584  */
3585 static ir_node *transform_node_Or_Rot(ir_node *or) {
3586         ir_mode *mode = get_irn_mode(or);
3587         ir_node *shl, *shr, *block;
3588         ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3589         tarval *tv1, *tv2;
3590
3591         if (! mode_is_int(mode))
3592                 return or;
3593
3594         shl = get_binop_left(or);
3595         shr = get_binop_right(or);
3596
3597         if (get_irn_op(shl) == op_Shr) {
3598                 if (get_irn_op(shr) != op_Shl)
3599                         return or;
3600
3601                 irn = shl;
3602                 shl = shr;
3603                 shr = irn;
3604         } else if (get_irn_op(shl) != op_Shl) {
3605                 return or;
3606         } else if (get_irn_op(shr) != op_Shr) {
3607                 return or;
3608         }
3609         x = get_Shl_left(shl);
3610         if (x != get_Shr_left(shr))
3611                 return or;
3612
3613         c1 = get_Shl_right(shl);
3614         c2 = get_Shr_right(shr);
3615         if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3616                 tv1 = get_Const_tarval(c1);
3617                 if (! tarval_is_long(tv1))
3618                         return or;
3619
3620                 tv2 = get_Const_tarval(c2);
3621                 if (! tarval_is_long(tv2))
3622                         return or;
3623
3624                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3625                         != get_mode_size_bits(mode))
3626                         return or;
3627
3628                 /* yet, condition met */
3629                 block = get_irn_n(or, -1);
3630
3631                 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3632
3633                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3634                 return n;
3635         } else if (get_irn_op(c1) == op_Sub) {
3636                 v   = c2;
3637                 sub = c1;
3638
3639                 if (get_Sub_right(sub) != v)
3640                         return or;
3641
3642                 c1 = get_Sub_left(sub);
3643                 if (get_irn_op(c1) != op_Const)
3644                         return or;
3645
3646                 tv1 = get_Const_tarval(c1);
3647                 if (! tarval_is_long(tv1))
3648                         return or;
3649
3650                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3651                         return or;
3652
3653                 /* yet, condition met */
3654                 block = get_nodes_block(or);
3655
3656                 /* a Rot right is not supported, so use a rot left */
3657                 n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
3658
3659                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3660                 return n;
3661         } else if (get_irn_op(c2) == op_Sub) {
3662                 v   = c1;
3663                 sub = c2;
3664
3665                 c1 = get_Sub_left(sub);
3666                 if (get_irn_op(c1) != op_Const)
3667                         return or;
3668
3669                 tv1 = get_Const_tarval(c1);
3670                 if (! tarval_is_long(tv1))
3671                         return or;
3672
3673                 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3674                         return or;
3675
3676                 /* yet, condition met */
3677                 block = get_irn_n(or, -1);
3678
3679                 /* a Rot Left */
3680                 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3681
3682                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3683                 return n;
3684         }
3685
3686         return or;
3687 }  /* transform_node_Or_Rot */
3688
3689 /**
3690  * Transform an Or.
3691  */
3692 static ir_node *transform_node_Or(ir_node *n) {
3693         ir_node *c, *oldn = n;
3694         ir_node *a = get_Or_left(n);
3695         ir_node *b = get_Or_right(n);
3696
3697         /* we can evaluate 2 Projs of the same Cmp */
3698         if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
3699                 ir_node *pred_a = get_Proj_pred(a);
3700                 ir_node *pred_b = get_Proj_pred(b);
3701                 if(pred_a == pred_b) {
3702                         dbg_info *dbgi  = get_irn_dbg_info(n);
3703                         ir_node  *block = get_nodes_block(pred_a);
3704                         pn_Cmp pn_a     = get_Proj_proj(a);
3705                         pn_Cmp pn_b     = get_Proj_proj(b);
3706                         /* yes, we can simply calculate with pncs */
3707                         pn_Cmp new_pnc  = pn_a | pn_b;
3708
3709                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3710                                            new_pnc);
3711                 }
3712         }
3713
3714         HANDLE_BINOP_PHI(tarval_or, a,b,c);
3715
3716         n = transform_node_Or_bf_store(n);
3717         n = transform_node_Or_Rot(n);
3718         if (n != oldn)
3719                 return n;
3720
3721         n = transform_bitwise_distributive(n, transform_node_Or);
3722
3723         return n;
3724 }  /* transform_node_Or */
3725
3726
3727 /* forward */
3728 static ir_node *transform_node(ir_node *n);
3729
3730 /**
3731  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3732  *
3733  * Should be moved to reassociation?
3734  */
3735 static ir_node *transform_node_shift(ir_node *n) {
3736         ir_node *left, *right;
3737         tarval *tv1, *tv2, *res;
3738         ir_mode *mode;
3739         int modulo_shf, flag;
3740
3741         left = get_binop_left(n);
3742
3743         /* different operations */
3744         if (get_irn_op(left) != get_irn_op(n))
3745                 return n;
3746
3747         right = get_binop_right(n);
3748         tv1 = value_of(right);
3749         if (tv1 == tarval_bad)
3750                 return n;
3751
3752         tv2 = value_of(get_binop_right(left));
3753         if (tv2 == tarval_bad)
3754                 return n;
3755
3756         res = tarval_add(tv1, tv2);
3757
3758         /* beware: a simple replacement works only, if res < modulo shift */
3759         mode = get_irn_mode(n);
3760
3761         flag = 0;
3762
3763         modulo_shf = get_mode_modulo_shift(mode);
3764         if (modulo_shf > 0) {
3765                 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3766
3767                 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3768                         flag = 1;
3769         } else
3770                 flag = 1;
3771
3772         if (flag) {
3773                 /* ok, we can replace it */
3774                 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3775
3776                 in[0] = get_binop_left(left);
3777                 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3778
3779                 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3780
3781                 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3782
3783                 return transform_node(irn);
3784         }
3785         return n;
3786 }  /* transform_node_shift */
3787
3788 /**
3789  * Transform a Shr.
3790  */
3791 static ir_node *transform_node_Shr(ir_node *n) {
3792         ir_node *c, *oldn = n;
3793         ir_node *a = get_Shr_left(n);
3794         ir_node *b = get_Shr_right(n);
3795
3796         HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3797         return transform_node_shift(n);
3798 }  /* transform_node_Shr */
3799
3800 /**
3801  * Transform a Shrs.
3802  */
3803 static ir_node *transform_node_Shrs(ir_node *n) {
3804         ir_node *c, *oldn = n;
3805         ir_node *a = get_Shrs_left(n);
3806         ir_node *b = get_Shrs_right(n);
3807
3808         HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3809         return transform_node_shift(n);
3810 }  /* transform_node_Shrs */
3811
3812 /**
3813  * Transform a Shl.
3814  */
3815 static ir_node *transform_node_Shl(ir_node *n) {
3816         ir_node *c, *oldn = n;
3817         ir_node *a = get_Shl_left(n);
3818         ir_node *b = get_Shl_right(n);
3819
3820         HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3821         return transform_node_shift(n);
3822 }  /* transform_node_Shl */
3823
3824 /**
3825  * Remove dead blocks and nodes in dead blocks
3826  * in keep alive list.  We do not generate a new End node.
3827  */
3828 static ir_node *transform_node_End(ir_node *n) {
3829         int i, j, n_keepalives = get_End_n_keepalives(n);
3830         ir_node **in;
3831
3832         NEW_ARR_A(ir_node *, in, n_keepalives);
3833
3834         for (i = j = 0; i < n_keepalives; ++i) {
3835                 ir_node *ka = get_End_keepalive(n, i);
3836                 if (is_Block(ka)) {
3837                         if (! is_Block_dead(ka)) {
3838                                 in[j++] = ka;
3839                         }
3840                         continue;
3841                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
3842                         continue;
3843                 }
3844                 /* FIXME: beabi need to keep a Proj(M) */
3845                 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
3846                         in[j++] = ka;
3847         }
3848         if (j != n_keepalives)
3849                 set_End_keepalives(n, j, in);
3850         return n;
3851 }  /* transform_node_End */
3852
3853 /** returns 1 if a == -b */
3854 static int is_negated_value(ir_node *a, ir_node *b) {
3855         if(is_Minus(a) && get_Minus_op(a) == b)
3856                 return 1;
3857         if(is_Minus(b) && get_Minus_op(b) == a)
3858                 return 1;
3859         if(is_Sub(a) && is_Sub(b)) {
3860                 ir_node *a_left  = get_Sub_left(a);
3861                 ir_node *a_right = get_Sub_right(a);
3862                 ir_node *b_left  = get_Sub_left(b);
3863                 ir_node *b_right = get_Sub_right(b);
3864
3865                 if(a_left == b_right && a_right == b_left)
3866                         return 1;
3867         }
3868
3869         return 0;
3870 }
3871
3872 /**
3873  * Optimize a Mux into some simpler cases.
3874  */
3875 static ir_node *transform_node_Mux(ir_node *n) {
3876         ir_node *oldn = n, *sel = get_Mux_sel(n);
3877         ir_mode *mode = get_irn_mode(n);
3878
3879         if (mode == mode_b) {
3880                 ir_node  *t     = get_Mux_true(n);
3881                 ir_node  *f     = get_Mux_false(n);
3882                 dbg_info *dbg   = get_irn_dbg_info(n);
3883                 ir_node  *block = get_irn_n(n, -1);
3884                 ir_graph *irg   = current_ir_graph;
3885
3886                 if (is_Const(t)) {
3887                         tarval *tv_t = get_Const_tarval(t);
3888                         if (tv_t == tarval_b_true) {
3889                                 if (is_Const(f)) {
3890                                         assert(get_Const_tarval(f) == tarval_b_false);
3891                                         return sel;
3892                                 } else {
3893                                         return new_rd_Or(dbg, irg, block, sel, f, mode_b);
3894                                 }
3895                         } else {
3896                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
3897                                 assert(tv_t == tarval_b_false);
3898                                 if (is_Const(f)) {
3899                                         assert(get_Const_tarval(f) == tarval_b_true);
3900                                         return not_sel;
3901                                 } else {
3902                                         return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
3903                                 }
3904                         }
3905                 } else if (is_Const(f)) {
3906                         tarval *tv_f = get_Const_tarval(f);
3907                         if (tv_f == tarval_b_true) {
3908                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
3909                                 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
3910                         } else {
3911                                 assert(tv_f == tarval_b_false);
3912                                 return new_rd_And(dbg, irg, block, sel, t, mode_b);
3913                         }
3914                 }
3915         }
3916
3917         if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3918                 ir_node *cmp = get_Proj_pred(sel);
3919                 long     pn  = get_Proj_proj(sel);
3920                 ir_node *f   = get_Mux_false(n);
3921                 ir_node *t   = get_Mux_true(n);
3922
3923                 /*
3924                  * Note: normalization puts the constant on the right site,
3925                  * so we check only one case.
3926                  *
3927                  * Note further that these optimization work even for floating point
3928                  * with NaN's because -NaN == NaN.
3929                  * However, if +0 and -0 is handled differently, we cannot use the first
3930                  * one.
3931                  */
3932                 if (get_irn_op(cmp) == op_Cmp
3933                                 && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3934                         ir_node *block    = get_irn_n(n, -1);
3935
3936                         if(is_negated_value(f, t)) {
3937                                 ir_node *cmp_left = get_Cmp_left(cmp);
3938
3939                                 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
3940                                 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
3941                                         || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
3942                                 {
3943                                         n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
3944                                                                    cmp_left, mode);
3945                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3946                                         return n;
3947                                 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
3948                                 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
3949                                         || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
3950                                 {
3951                                         n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
3952                                                                    cmp_left, mode);
3953                                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3954                                                          block, n, mode);
3955                                         DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3956                                         return n;
3957                                 }
3958                         }
3959
3960                         if (mode_is_int(mode) && mode_is_signed(mode) &&
3961                                 get_mode_arithmetic(mode) == irma_twos_complement) {
3962                                 ir_node *x = get_Cmp_left(cmp);
3963
3964                                 /* the following optimization works only with signed integer two-complement mode */
3965
3966                                 if (mode == get_irn_mode(x)) {
3967                                         /*
3968                                          * FIXME: this restriction is two rigid, as it would still
3969                                          * work if mode(x) = Hs and mode == Is, but at least it removes
3970                                          * all wrong cases.
3971                                          */
3972                                         if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
3973                                             classify_Const(t) == CNST_ALL_ONE &&
3974                                             classify_Const(f) == CNST_NULL) {
3975                                                 /*
3976                                                  * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3977                                                  * Conditions:
3978                                                  * T must be signed.
3979                                                  */
3980                                                 n = new_rd_Shrs(get_irn_dbg_info(n),
3981                                                                 current_ir_graph, block, x,
3982                                                                 new_r_Const_long(current_ir_graph, block, mode_Iu,
3983                                                                 get_mode_size_bits(mode) - 1),
3984                                                                 mode);
3985                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3986                                                 return n;
3987                                         } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
3988                                                    classify_Const(t) == CNST_ONE &&
3989                                                    classify_Const(f) == CNST_NULL) {
3990                                                 /*
3991                                                  * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3992                                                  * Conditions:
3993                                                  * T must be signed.
3994                                                  */
3995                                                 n = new_rd_Shr(get_irn_dbg_info(n),
3996                                                         current_ir_graph, block,
3997                                                         new_r_Minus(current_ir_graph, block, x, mode),
3998                                                         new_r_Const_long(current_ir_graph, block, mode_Iu,
3999                                                         get_mode_size_bits(mode) - 1),
4000                                                         mode);
4001                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4002                                                 return n;
4003                                         }
4004                                 }
4005                         }
4006                 }
4007         }
4008         return arch_transform_node_Mux(n);
4009 }  /* transform_node_Mux */
4010
4011 /**
4012  * Optimize a Psi into some simpler cases.
4013  */
4014 static ir_node *transform_node_Psi(ir_node *n) {
4015         if (is_Mux(n))
4016                 return transform_node_Mux(n);
4017
4018         return n;
4019 }  /* transform_node_Psi */
4020
4021 /**
4022  * Tries several [inplace] [optimizing] transformations and returns an
4023  * equivalent node.  The difference to equivalent_node() is that these
4024  * transformations _do_ generate new nodes, and thus the old node must
4025  * not be freed even if the equivalent node isn't the old one.
4026  */
4027 static ir_node *transform_node(ir_node *n) {
4028         ir_node *oldn;
4029
4030         /*
4031          * Transform_node is the only "optimizing transformation" that might
4032          * return a node with a different opcode. We iterate HERE until fixpoint
4033          * to get the final result.
4034          */
4035         do {
4036                 oldn = n;
4037                 if (n->op->ops.transform_node)
4038                         n = n->op->ops.transform_node(n);
4039         } while (oldn != n);
4040
4041         return n;
4042 }  /* transform_node */
4043
4044 /**
4045  * Sets the default transform node operation for an ir_op_ops.
4046  *
4047  * @param code   the opcode for the default operation
4048  * @param ops    the operations initialized
4049  *
4050  * @return
4051  *    The operations.
4052  */
4053 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4054 {
4055 #define CASE(a)                                 \
4056         case iro_##a:                                 \
4057                 ops->transform_node  = transform_node_##a;  \
4058                 break
4059
4060         switch (code) {
4061         CASE(Add);
4062         CASE(Sub);
4063         CASE(Mul);
4064         CASE(Div);
4065         CASE(Mod);
4066         CASE(DivMod);
4067         CASE(Quot);
4068         CASE(Abs);
4069         CASE(Cond);
4070         CASE(And);
4071         CASE(Or);
4072         CASE(Eor);
4073         CASE(Minus);
4074         CASE(Not);
4075         CASE(Cast);
4076         CASE(Proj);
4077         CASE(Phi);
4078         CASE(Sel);
4079         CASE(Shr);
4080         CASE(Shrs);
4081         CASE(Shl);
4082         CASE(End);
4083         CASE(Mux);
4084         CASE(Psi);
4085         default:
4086           /* leave NULL */;
4087         }
4088
4089         return ops;
4090 #undef CASE
4091 }  /* firm_set_default_transform_node */
4092
4093
4094 /* **************** Common Subexpression Elimination **************** */
4095
4096 /** The size of the hash table used, should estimate the number of nodes
4097     in a graph. */
4098 #define N_IR_NODES 512
4099
4100 /** Compares the attributes of two Const nodes. */
4101 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4102         return (get_Const_tarval(a) != get_Const_tarval(b))
4103             || (get_Const_type(a) != get_Const_type(b));
4104 }  /* node_cmp_attr_Const */
4105
4106 /** Compares the attributes of two Proj nodes. */
4107 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4108         return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4109 }  /* node_cmp_attr_Proj */
4110
4111 /** Compares the attributes of two Filter nodes. */
4112 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4113         return get_Filter_proj(a) != get_Filter_proj(b);
4114 }  /* node_cmp_attr_Filter */
4115
4116 /** Compares the attributes of two Alloc nodes. */
4117 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4118         const alloc_attr *pa = get_irn_alloc_attr(a);
4119         const alloc_attr *pb = get_irn_alloc_attr(b);
4120         return (pa->where != pb->where) || (pa->type != pb->type);
4121 }  /* node_cmp_attr_Alloc */
4122
4123 /** Compares the attributes of two Free nodes. */
4124 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4125         const free_attr *pa = get_irn_free_attr(a);
4126         const free_attr *pb = get_irn_free_attr(b);
4127         return (pa->where != pb->where) || (pa->type != pb->type);
4128 }  /* node_cmp_attr_Free */
4129
4130 /** Compares the attributes of two SymConst nodes. */
4131 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4132         const symconst_attr *pa = get_irn_symconst_attr(a);
4133         const symconst_attr *pb = get_irn_symconst_attr(b);
4134         return (pa->num        != pb->num)
4135             || (pa->sym.type_p != pb->sym.type_p)
4136             || (pa->tp         != pb->tp);
4137 }  /* node_cmp_attr_SymConst */
4138
4139 /** Compares the attributes of two Call nodes. */
4140 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4141         return (get_irn_call_attr(a) != get_irn_call_attr(b));
4142 }  /* node_cmp_attr_Call */
4143
4144 /** Compares the attributes of two Sel nodes. */
4145 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4146         const ir_entity *a_ent = get_Sel_entity(a);
4147         const ir_entity *b_ent = get_Sel_entity(b);
4148         return
4149                 (a_ent->kind    != b_ent->kind)    ||
4150                 (a_ent->name    != b_ent->name)    ||
4151                 (a_ent->owner   != b_ent->owner)   ||
4152                 (a_ent->ld_name != b_ent->ld_name) ||
4153                 (a_ent->type    != b_ent->type);
4154 }  /* node_cmp_attr_Sel */
4155
4156 /** Compares the attributes of two Phi nodes. */
4157 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4158         /* we can only enter this function if both nodes have the same number of inputs,
4159            hence it is enough to check if one of them is a Phi0 */
4160         if (is_Phi0(a)) {
4161                 /* check the Phi0 attribute */
4162                 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4163         }
4164         return 0;
4165 }  /* node_cmp_attr_Phi */
4166
4167 /** Compares the attributes of two Conv nodes. */
4168 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4169         return get_Conv_strict(a) != get_Conv_strict(b);
4170 }  /* node_cmp_attr_Conv */
4171
4172 /** Compares the attributes of two Cast nodes. */
4173 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4174         return get_Cast_type(a) != get_Cast_type(b);
4175 }  /* node_cmp_attr_Cast */
4176
4177 /** Compares the attributes of two Load nodes. */
4178 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4179         if (get_Load_volatility(a) == volatility_is_volatile ||
4180             get_Load_volatility(b) == volatility_is_volatile)
4181                 /* NEVER do CSE on volatile Loads */
4182                 return 1;
4183         /* do not CSE Loads with different alignment. Be conservative. */
4184         if (get_Load_align(a) != get_Load_align(b))
4185                 return 1;
4186
4187         return get_Load_mode(a) != get_Load_mode(b);
4188 }  /* node_cmp_attr_Load */
4189
4190 /** Compares the attributes of two Store nodes. */
4191 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4192         /* do not CSE Stores with different alignment. Be conservative. */
4193         if (get_Store_align(a) != get_Store_align(b))
4194                 return 1;
4195
4196         /* NEVER do CSE on volatile Stores */
4197         return (get_Store_volatility(a) == volatility_is_volatile ||
4198                 get_Store_volatility(b) == volatility_is_volatile);
4199 }  /* node_cmp_attr_Store */
4200
4201 /** Compares the attributes of two Confirm nodes. */
4202 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4203         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4204 }  /* node_cmp_attr_Confirm */
4205
4206 /** Compares the attributes of two ASM nodes. */
4207 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4208         int i, n;
4209         const ir_asm_constraint *ca;
4210         const ir_asm_constraint *cb;
4211         ident **cla, **clb;
4212
4213         if (get_ASM_text(a) != get_ASM_text(b))
4214                 return 1;
4215
4216         /* Should we really check the constraints here? Should be better, but is strange. */
4217         n = get_ASM_n_input_constraints(a);
4218         if (n != get_ASM_n_input_constraints(b))
4219                 return 0;
4220
4221         ca = get_ASM_input_constraints(a);
4222         cb = get_ASM_input_constraints(b);
4223         for (i = 0; i < n; ++i) {
4224                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4225                         return 1;
4226         }
4227
4228         n = get_ASM_n_output_constraints(a);
4229         if (n != get_ASM_n_output_constraints(b))
4230                 return 0;
4231
4232         ca = get_ASM_output_constraints(a);
4233         cb = get_ASM_output_constraints(b);
4234         for (i = 0; i < n; ++i) {
4235                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4236                         return 1;
4237         }
4238
4239         n = get_ASM_n_clobbers(a);
4240         if (n != get_ASM_n_clobbers(b))
4241                 return 0;
4242
4243         cla = get_ASM_clobbers(a);
4244         clb = get_ASM_clobbers(b);
4245         for (i = 0; i < n; ++i) {
4246                 if (cla[i] != clb[i])
4247                         return 1;
4248         }
4249         return 0;
4250 }  /* node_cmp_attr_ASM */
4251
4252 /**
4253  * Set the default node attribute compare operation for an ir_op_ops.
4254  *
4255  * @param code   the opcode for the default operation
4256  * @param ops    the operations initialized
4257  *
4258  * @return
4259  *    The operations.
4260  */
4261 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4262 {
4263 #define CASE(a)                              \
4264         case iro_##a:                              \
4265                 ops->node_cmp_attr  = node_cmp_attr_##a; \
4266                 break
4267
4268         switch (code) {
4269         CASE(Const);
4270         CASE(Proj);
4271         CASE(Filter);
4272         CASE(Alloc);
4273         CASE(Free);
4274         CASE(SymConst);
4275         CASE(Call);
4276         CASE(Sel);
4277         CASE(Phi);
4278         CASE(Conv);
4279         CASE(Cast);
4280         CASE(Load);
4281         CASE(Store);
4282         CASE(Confirm);
4283         CASE(ASM);
4284         default:
4285           /* leave NULL */;
4286         }
4287
4288         return ops;
4289 #undef CASE
4290 }  /* firm_set_default_node_cmp_attr */
4291
4292 /*
4293  * Compare function for two nodes in the hash table. Gets two
4294  * nodes as parameters.  Returns 0 if the nodes are a cse.
4295  */
4296 int identities_cmp(const void *elt, const void *key) {
4297         ir_node *a, *b;
4298         int i, irn_arity_a;
4299
4300         a = (void *)elt;
4301         b = (void *)key;
4302
4303         if (a == b) return 0;
4304
4305         if ((get_irn_op(a) != get_irn_op(b)) ||
4306             (get_irn_mode(a) != get_irn_mode(b))) return 1;
4307
4308         /* compare if a's in and b's in are of equal length */
4309         irn_arity_a = get_irn_intra_arity (a);
4310         if (irn_arity_a != get_irn_intra_arity(b))
4311                 return 1;
4312
4313         /* for block-local cse and op_pin_state_pinned nodes: */
4314         if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4315                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4316                         return 1;
4317         }
4318
4319         /* compare a->in[0..ins] with b->in[0..ins] */
4320         for (i = 0; i < irn_arity_a; i++)
4321                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4322                         return 1;
4323
4324         /*
4325          * here, we already now that the nodes are identical except their
4326          * attributes
4327          */
4328         if (a->op->ops.node_cmp_attr)
4329                 return a->op->ops.node_cmp_attr(a, b);
4330
4331         return 0;
4332 }  /* identities_cmp */
4333
4334 /*
4335  * Calculate a hash value of a node.
4336  */
4337 unsigned ir_node_hash(ir_node *node) {
4338         unsigned h;
4339         int i, irn_arity;
4340
4341         if (node->op == op_Const) {
4342                 /* special value for const, as they only differ in their tarval. */
4343                 h = HASH_PTR(node->attr.con.tv);
4344                 h = 9*h + HASH_PTR(get_irn_mode(node));
4345         } else if (node->op == op_SymConst) {
4346                 /* special value for const, as they only differ in their symbol. */
4347                 h = HASH_PTR(node->attr.symc.sym.type_p);
4348                 h = 9*h + HASH_PTR(get_irn_mode(node));
4349         } else {
4350
4351                 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4352                 h = irn_arity = get_irn_intra_arity(node);
4353
4354                 /* consider all in nodes... except the block if not a control flow. */
4355                 for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
4356                         h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4357                 }
4358
4359                 /* ...mode,... */
4360                 h = 9*h + HASH_PTR(get_irn_mode(node));
4361                 /* ...and code */
4362                 h = 9*h + HASH_PTR(get_irn_op(node));
4363         }
4364
4365         return h;
4366 }  /* ir_node_hash */
4367
4368 pset *new_identities(void) {
4369         return new_pset(identities_cmp, N_IR_NODES);
4370 }  /* new_identities */
4371
4372 void del_identities(pset *value_table) {
4373         del_pset(value_table);
4374 }  /* del_identities */
4375
4376 /**
4377  * Return the canonical node computing the same value as n.
4378  *
4379  * @param value_table  The value table
4380  * @param n            The node to lookup
4381  *
4382  * Looks up the node in a hash table.
4383  *
4384  * For Const nodes this is performed in the constructor, too.  Const
4385  * nodes are extremely time critical because of their frequent use in
4386  * constant string arrays.
4387  */
4388 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4389         ir_node *o = NULL;
4390
4391         if (!value_table) return n;
4392
4393         if (get_opt_reassociation()) {
4394                 if (is_op_commutative(get_irn_op(n))) {
4395                         ir_node *l = get_binop_left(n);
4396                         ir_node *r = get_binop_right(n);
4397
4398                         /* for commutative operators perform  a OP b == b OP a */
4399                         if (get_irn_idx(l) > get_irn_idx(r)) {
4400                                 set_binop_left(n, r);
4401                                 set_binop_right(n, l);
4402                         }
4403                 }
4404         }
4405
4406         o = pset_find(value_table, n, ir_node_hash(n));
4407         if (!o) return n;
4408
4409         DBG_OPT_CSE(n, o);
4410
4411         return o;
4412 }  /* identify */
4413
4414 /**
4415  * During construction we set the op_pin_state_pinned flag in the graph right when the
4416  * optimization is performed.  The flag turning on procedure global cse could
4417  * be changed between two allocations.  This way we are safe.
4418  */
4419 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4420         ir_node *old = n;
4421
4422         n = identify(value_table, n);
4423         if (get_irn_n(old, -1) != get_irn_n(n, -1))
4424                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4425         return n;
4426 }  /* identify_cons */
4427
4428 /*
4429  * Return the canonical node computing the same value as n.
4430  * Looks up the node in a hash table, enters it in the table
4431  * if it isn't there yet.
4432  */
4433 ir_node *identify_remember(pset *value_table, ir_node *n) {
4434         ir_node *o = NULL;
4435
4436         if (!value_table) return n;
4437
4438         if (get_opt_reassociation()) {
4439                 if (is_op_commutative(get_irn_op(n))) {
4440                         ir_node *l = get_binop_left(n);
4441                         ir_node *r = get_binop_right(n);
4442                         int l_idx = get_irn_idx(l);
4443                         int r_idx = get_irn_idx(r);
4444
4445                         /* For commutative operators perform  a OP b == b OP a but keep
4446                            constants on the RIGHT side. This helps greatly in some optimizations.
4447                            Moreover we use the idx number to make the form deterministic. */
4448                         if (is_irn_constlike(l))
4449                                 l_idx = -l_idx;
4450                         if (is_irn_constlike(r))
4451                                 r_idx = -r_idx;
4452                         if (l_idx < r_idx) {
4453                                 set_binop_left(n, r);
4454                                 set_binop_right(n, l);
4455                         }
4456                 }
4457         }
4458
4459         /* lookup or insert in hash table with given hash key. */
4460         o = pset_insert(value_table, n, ir_node_hash(n));
4461
4462         if (o != n) {
4463                 DBG_OPT_CSE(n, o);
4464         }
4465
4466         return o;
4467 }  /* identify_remember */
4468
4469 /* Add a node to the identities value table. */
4470 void add_identities(pset *value_table, ir_node *node) {
4471         if (get_opt_cse() && is_no_Block(node))
4472                 identify_remember(value_table, node);
4473 }  /* add_identities */
4474
4475 /* Visit each node in the value table of a graph. */
4476 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4477         ir_node *node;
4478         ir_graph *rem = current_ir_graph;
4479
4480         current_ir_graph = irg;
4481         foreach_pset(irg->value_table, node)
4482                 visit(node, env);
4483         current_ir_graph = rem;
4484 }  /* visit_all_identities */
4485
4486 /**
4487  * Garbage in, garbage out. If a node has a dead input, i.e., the
4488  * Bad node is input to the node, return the Bad node.
4489  */
4490 static INLINE ir_node *gigo(ir_node *node) {
4491         int i, irn_arity;
4492         ir_op *op = get_irn_op(node);
4493
4494         /* remove garbage blocks by looking at control flow that leaves the block
4495            and replacing the control flow by Bad. */
4496         if (get_irn_mode(node) == mode_X) {
4497                 ir_node *block = get_nodes_block(skip_Proj(node));
4498
4499                 /* Don't optimize nodes in immature blocks. */
4500                 if (!get_Block_matured(block)) return node;
4501                 /* Don't optimize End, may have Bads. */
4502                 if (op == op_End) return node;
4503
4504                 if (is_Block(block)) {
4505                         irn_arity = get_irn_arity(block);
4506                         for (i = 0; i < irn_arity; i++) {
4507                                 if (!is_Bad(get_irn_n(block, i)))
4508                                         break;
4509                         }
4510                         if (i == irn_arity) {
4511                                 ir_graph *irg = get_irn_irg(block);
4512                                 /* the start block is never dead */
4513                                 if (block != get_irg_start_block(irg)
4514                                         && block != get_irg_end_block(irg))
4515                                         return new_Bad();
4516                         }
4517                 }
4518         }
4519
4520         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4521            blocks predecessors is dead. */
4522         if (op != op_Block && op != op_Phi && op != op_Tuple) {
4523                 irn_arity = get_irn_arity(node);
4524
4525                 /*
4526                  * Beware: we can only read the block of a non-floating node.
4527                  */
4528                 if (is_irn_pinned_in_irg(node) &&
4529                         is_Block_dead(get_nodes_block(node)))
4530                         return new_Bad();
4531
4532                 for (i = 0; i < irn_arity; i++) {
4533                         ir_node *pred = get_irn_n(node, i);
4534
4535                         if (is_Bad(pred))
4536                                 return new_Bad();
4537 #if 0
4538                         /* Propagating Unknowns here seems to be a bad idea, because
4539                            sometimes we need a node as a input and did not want that
4540                            it kills it's user.
4541                            However, it might be useful to move this into a later phase
4542                            (if you think that optimizing such code is useful). */
4543                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4544                                 return new_Unknown(get_irn_mode(node));
4545 #endif
4546                 }
4547         }
4548 #if 0
4549         /* With this code we violate the agreement that local_optimize
4550            only leaves Bads in Block, Phi and Tuple nodes. */
4551         /* If Block has only Bads as predecessors it's garbage. */
4552         /* If Phi has only Bads as predecessors it's garbage. */
4553         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
4554                 irn_arity = get_irn_arity(node);
4555                 for (i = 0; i < irn_arity; i++) {
4556                         if (!is_Bad(get_irn_n(node, i))) break;
4557                 }
4558                 if (i == irn_arity) node = new_Bad();
4559         }
4560 #endif
4561         return node;
4562 }  /* gigo */
4563
4564 /**
4565  * These optimizations deallocate nodes from the obstack.
4566  * It can only be called if it is guaranteed that no other nodes
4567  * reference this one, i.e., right after construction of a node.
4568  *
4569  * @param n   The node to optimize
4570  *
4571  * current_ir_graph must be set to the graph of the node!
4572  */
4573 ir_node *optimize_node(ir_node *n) {
4574         tarval *tv;
4575         ir_node *oldn = n;
4576         ir_opcode iro = get_irn_opcode(n);
4577
4578         /* Always optimize Phi nodes: part of the construction. */
4579         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4580
4581         /* constant expression evaluation / constant folding */
4582         if (get_opt_constant_folding()) {
4583                 /* neither constants nor Tuple values can be evaluated */
4584                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4585                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
4586                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4587                         /* try to evaluate */
4588                         tv = computed_value(n);
4589                         if (tv != tarval_bad) {
4590                                 ir_node *nw;
4591                                 ir_type *old_tp = get_irn_type(n);
4592                                 int i, arity = get_irn_arity(n);
4593                                 int node_size;
4594
4595                                 /*
4596                                  * Try to recover the type of the new expression.
4597                                  */
4598                                 for (i = 0; i < arity && !old_tp; ++i)
4599                                         old_tp = get_irn_type(get_irn_n(n, i));
4600
4601                                 /*
4602                                  * we MUST copy the node here temporary, because it's still needed
4603                                  * for DBG_OPT_CSTEVAL
4604                                  */
4605                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
4606                                 oldn = alloca(node_size);
4607
4608                                 memcpy(oldn, n, node_size);
4609                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4610
4611                                 /* ARG, copy the in array, we need it for statistics */
4612                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4613
4614                                 /* note the inplace edges module */
4615                                 edges_node_deleted(n, current_ir_graph);
4616
4617                                 /* evaluation was successful -- replace the node. */
4618                                 irg_kill_node(current_ir_graph, n);
4619                                 nw = new_Const(get_tarval_mode (tv), tv);
4620
4621                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
4622                                         set_Const_type(nw, old_tp);
4623                                 DBG_OPT_CSTEVAL(oldn, nw);
4624                                 tarval_enable_fp_ops(old_fp_mode);
4625                                 return nw;
4626                         }
4627                         tarval_enable_fp_ops(old_fp_mode);
4628                 }
4629         }
4630
4631         /* remove unnecessary nodes */
4632         if (get_opt_constant_folding() ||
4633             (iro == iro_Phi)  ||   /* always optimize these nodes. */
4634             (iro == iro_Id)   ||
4635             (iro == iro_Proj) ||
4636             (iro == iro_Block)  )  /* Flags tested local. */
4637                 n = equivalent_node(n);
4638
4639         optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
4640
4641         /* Common Subexpression Elimination.
4642          *
4643          * Checks whether n is already available.
4644          * The block input is used to distinguish different subexpressions. Right
4645          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
4646          * subexpressions within a block.
4647          */
4648         if (get_opt_cse())
4649                 n = identify_cons(current_ir_graph->value_table, n);
4650
4651         if (n != oldn) {
4652                 edges_node_deleted(oldn, current_ir_graph);
4653
4654                 /* We found an existing, better node, so we can deallocate the old node. */
4655                 irg_kill_node(current_ir_graph, oldn);
4656                 return n;
4657         }
4658
4659         /* Some more constant expression evaluation that does not allow to
4660            free the node. */
4661         iro = get_irn_opcode(n);
4662         if (get_opt_constant_folding() ||
4663             (iro == iro_Cond) ||
4664             (iro == iro_Proj))     /* Flags tested local. */
4665                 n = transform_node(n);
4666
4667         /* Remove nodes with dead (Bad) input.
4668            Run always for transformation induced Bads. */
4669         n = gigo (n);
4670
4671         /* Now we have a legal, useful node. Enter it in hash table for CSE */
4672         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
4673                 n = identify_remember(current_ir_graph->value_table, n);
4674         }
4675
4676         return n;
4677 }  /* optimize_node */
4678
4679
4680 /**
4681  * These optimizations never deallocate nodes (in place).  This can cause dead
4682  * nodes lying on the obstack.  Remove these by a dead node elimination,
4683  * i.e., a copying garbage collection.
4684  */
4685 ir_node *optimize_in_place_2(ir_node *n) {
4686         tarval *tv;
4687         ir_node *oldn = n;
4688         ir_opcode iro = get_irn_opcode(n);
4689
4690         if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
4691
4692         /* constant expression evaluation / constant folding */
4693         if (get_opt_constant_folding()) {
4694                 /* neither constants nor Tuple values can be evaluated */
4695                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
4696                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
4697                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4698                         /* try to evaluate */
4699                         tv = computed_value(n);
4700                         if (tv != tarval_bad) {
4701                                 /* evaluation was successful -- replace the node. */
4702                                 ir_type *old_tp = get_irn_type(n);
4703                                 int i, arity = get_irn_arity(n);
4704
4705                                 /*
4706                                  * Try to recover the type of the new expression.
4707                                  */
4708                                 for (i = 0; i < arity && !old_tp; ++i)
4709                                         old_tp = get_irn_type(get_irn_n(n, i));
4710
4711                                 n = new_Const(get_tarval_mode(tv), tv);
4712
4713                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4714                                         set_Const_type(n, old_tp);
4715
4716                                 DBG_OPT_CSTEVAL(oldn, n);
4717                                 tarval_enable_fp_ops(old_fp_mode);
4718                                 return n;
4719                         }
4720                         tarval_enable_fp_ops(old_fp_mode);
4721                 }
4722         }
4723
4724         /* remove unnecessary nodes */
4725         if (get_opt_constant_folding() ||
4726             (iro == iro_Phi)  ||   /* always optimize these nodes. */
4727             (iro == iro_Id)   ||   /* ... */
4728             (iro == iro_Proj) ||   /* ... */
4729             (iro == iro_Block)  )  /* Flags tested local. */
4730                 n = equivalent_node(n);
4731
4732         optimize_preds(n);                  /* do node specific optimizations of nodes predecessors. */
4733
4734         /** common subexpression elimination **/
4735         /* Checks whether n is already available. */
4736         /* The block input is used to distinguish different subexpressions.  Right
4737            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4738            subexpressions within a block. */
4739         if (get_opt_cse()) {
4740                 n = identify(current_ir_graph->value_table, n);
4741         }
4742
4743         /* Some more constant expression evaluation. */
4744         iro = get_irn_opcode(n);
4745         if (get_opt_constant_folding() ||
4746                 (iro == iro_Cond) ||
4747                 (iro == iro_Proj))     /* Flags tested local. */
4748                 n = transform_node(n);
4749
4750         /* Remove nodes with dead (Bad) input.
4751            Run always for transformation induced Bads.  */
4752         n = gigo(n);
4753
4754         /* Now we can verify the node, as it has no dead inputs any more. */
4755         irn_vrfy(n);
4756
4757         /* Now we have a legal, useful node. Enter it in hash table for cse.
4758            Blocks should be unique anyways.  (Except the successor of start:
4759            is cse with the start block!) */
4760         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4761                 n = identify_remember(current_ir_graph->value_table, n);
4762
4763         return n;
4764 }  /* optimize_in_place_2 */
4765
4766 /**
4767  * Wrapper for external use, set proper status bits after optimization.
4768  */
4769 ir_node *optimize_in_place(ir_node *n) {
4770         /* Handle graph state */
4771         assert(get_irg_phase_state(current_ir_graph) != phase_building);
4772
4773         if (get_opt_global_cse())
4774                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4775         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4776                 set_irg_outs_inconsistent(current_ir_graph);
4777
4778         /* FIXME: Maybe we could also test whether optimizing the node can
4779            change the control graph. */
4780         set_irg_doms_inconsistent(current_ir_graph);
4781         return optimize_in_place_2(n);
4782 }  /* optimize_in_place */
4783
4784 /*
4785  * Sets the default operation for an ir_ops.
4786  */
4787 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4788         ops = firm_set_default_computed_value(code, ops);
4789         ops = firm_set_default_equivalent_node(code, ops);
4790         ops = firm_set_default_transform_node(code, ops);
4791         ops = firm_set_default_node_cmp_attr(code, ops);
4792         ops = firm_set_default_get_type(code, ops);
4793         ops = firm_set_default_get_type_attr(code, ops);
4794         ops = firm_set_default_get_entity_attr(code, ops);
4795
4796         return ops;
4797 }  /* firm_set_default_operations */