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