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