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