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