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