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