- added is_Id, is_CallBegin, is_Free
[libfirm] / ir / ir / iropt.c
1 /*
2  * Copyright (C) 1995-2008 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)
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                                        is_Proj(aa)
538                                     && mode_is_reference(get_irn_mode(aa))
539                                     && is_Alloc(aaa))
540                                 && (   (/* ab is NULL */
541                                            is_Const(ab)
542                                         && mode_is_reference(get_irn_mode(ab))
543                                         && is_Const_null(ab))
544                                     || (/* ab is other Alloc */
545                                            is_Proj(ab)
546                                         && mode_is_reference(get_irn_mode(ab))
547                                         && is_Alloc(aba)
548                                         && (aaa != aba))))
549                             || (/* aa is NULL and aba is Alloc */
550                                    is_Const(aa)
551                                 && mode_is_reference(get_irn_mode(aa))
552                                 && is_Const_null(aa)
553                                 && is_Proj(ab)
554                                 && mode_is_reference(get_irn_mode(ab))
555                                 && is_Alloc(aba)))
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         /*
641          * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
642          * Do NOT optimize them away (CondEval wants them), so wait until
643          * remove_confirm is activated.
644          */
645         if (get_opt_remove_confirm()) {
646                 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
647                         value_of(get_Confirm_bound(n)) : tarval_bad;
648         }
649         return tarval_bad;
650 }  /* computed_value_Confirm */
651
652 /**
653  * If the parameter n can be computed, return its value, else tarval_bad.
654  * Performs constant folding.
655  *
656  * @param n  The node this should be evaluated
657  */
658 tarval *computed_value(ir_node *n) {
659         if (n->op->ops.computed_value)
660                 return n->op->ops.computed_value(n);
661         return tarval_bad;
662 }  /* computed_value */
663
664 /**
665  * Set the default computed_value evaluator in an ir_op_ops.
666  *
667  * @param code   the opcode for the default operation
668  * @param ops    the operations initialized
669  *
670  * @return
671  *    The operations.
672  */
673 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
674 {
675 #define CASE(a)                                    \
676         case iro_##a:                                  \
677                 ops->computed_value  = computed_value_##a; \
678                 break
679
680         switch (code) {
681         CASE(Const);
682         CASE(SymConst);
683         CASE(Add);
684         CASE(Sub);
685         CASE(Minus);
686         CASE(Mul);
687         CASE(Quot);
688         CASE(Div);
689         CASE(Mod);
690         CASE(Abs);
691         CASE(And);
692         CASE(Or);
693         CASE(Eor);
694         CASE(Not);
695         CASE(Shl);
696         CASE(Shr);
697         CASE(Shrs);
698         CASE(Rot);
699         CASE(Carry);
700         CASE(Borrow);
701         CASE(Conv);
702         CASE(Proj);
703         CASE(Mux);
704         CASE(Psi);
705         CASE(Confirm);
706         default:
707                 /* leave NULL */;
708         }
709
710         return ops;
711 #undef CASE
712 }  /* firm_set_default_computed_value */
713
714 /**
715  * Returns a equivalent block for another block.
716  * If the block has only one predecessor, this is
717  * the equivalent one. If the only predecessor of a block is
718  * the block itself, this is a dead block.
719  *
720  * If both predecessors of a block are the branches of a binary
721  * Cond, the equivalent block is Cond's block.
722  *
723  * If all predecessors of a block are bad or lies in a dead
724  * block, the current block is dead as well.
725  *
726  * Note, that blocks are NEVER turned into Bad's, instead
727  * the dead_block flag is set. So, never test for is_Bad(block),
728  * always use is_dead_Block(block).
729  */
730 static ir_node *equivalent_node_Block(ir_node *n)
731 {
732         ir_node *oldn = n;
733         int     n_preds;
734
735         /* don't optimize dead blocks */
736         if (is_Block_dead(n))
737                 return n;
738
739         n_preds = get_Block_n_cfgpreds(n);
740
741         /* The Block constructor does not call optimize, but mature_immBlock()
742            calls the optimization. */
743         assert(get_Block_matured(n));
744
745         /* Straightening: a single entry Block following a single exit Block
746            can be merged, if it is not the Start block. */
747         /* !!! Beware, all Phi-nodes of n must have been optimized away.
748            This should be true, as the block is matured before optimize is called.
749            But what about Phi-cycles with the Phi0/Id that could not be resolved?
750            Remaining Phi nodes are just Ids. */
751         if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
752                 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
753                 if (predblock == oldn) {
754                         /* Jmp jumps into the block it is in -- deal self cycle. */
755                         n = set_Block_dead(n);
756                         DBG_OPT_DEAD_BLOCK(oldn, n);
757                 } else if (get_opt_control_flow_straightening()) {
758                         n = predblock;
759                         DBG_OPT_STG(oldn, n);
760                 }
761         } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
762                 ir_node *predblock = get_Block_cfgpred_block(n, 0);
763                 if (predblock == oldn) {
764                         /* Jmp jumps into the block it is in -- deal self cycle. */
765                         n = set_Block_dead(n);
766                         DBG_OPT_DEAD_BLOCK(oldn, n);
767                 }
768         } else if ((n_preds == 2) &&
769                    (get_opt_control_flow_weak_simplification())) {
770                 /* Test whether Cond jumps twice to this block
771                  * The more general case which more than 2 predecessors is handles
772                  * in optimize_cf(), we handle only this special case for speed here.
773                  */
774                 ir_node *a = get_Block_cfgpred(n, 0);
775                 ir_node *b = get_Block_cfgpred(n, 1);
776
777                 if (is_Proj(a) &&
778                     is_Proj(b) &&
779                     (get_Proj_pred(a) == get_Proj_pred(b)) &&
780                     is_Cond(get_Proj_pred(a)) &&
781                     (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
782                         /* Also a single entry Block following a single exit Block.  Phis have
783                            twice the same operand and will be optimized away. */
784                         n = get_nodes_block(get_Proj_pred(a));
785                         DBG_OPT_IFSIM1(oldn, a, b, n);
786                 }
787         } else if (get_opt_unreachable_code() &&
788                    (n != get_irg_start_block(current_ir_graph)) &&
789                    (n != get_irg_end_block(current_ir_graph))    ) {
790                 int i;
791
792                 /* If all inputs are dead, this block is dead too, except if it is
793                    the start or end block.  This is one step of unreachable code
794                    elimination */
795                 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
796                         ir_node *pred = get_Block_cfgpred(n, i);
797                         ir_node *pred_blk;
798
799                         if (is_Bad(pred)) continue;
800                         pred_blk = get_nodes_block(skip_Proj(pred));
801
802                         if (is_Block_dead(pred_blk)) continue;
803
804                         if (pred_blk != n) {
805                                 /* really found a living input */
806                                 break;
807                         }
808                 }
809                 if (i < 0) {
810                         n = set_Block_dead(n);
811                         DBG_OPT_DEAD_BLOCK(oldn, n);
812                 }
813         }
814
815         return n;
816 }  /* equivalent_node_Block */
817
818 /**
819  * Returns a equivalent node for a Jmp, a Bad :-)
820  * Of course this only happens if the Block of the Jmp is dead.
821  */
822 static ir_node *equivalent_node_Jmp(ir_node *n) {
823         /* unreachable code elimination */
824         if (is_Block_dead(get_nodes_block(n)))
825                 n = new_Bad();
826
827         return n;
828 }  /* equivalent_node_Jmp */
829
830 /** Raise is handled in the same way as Jmp. */
831 #define equivalent_node_Raise   equivalent_node_Jmp
832
833
834 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
835    See transform_node_Proj_Cond(). */
836
837 /**
838  * Optimize operations that are commutative and have neutral 0,
839  * so a op 0 = 0 op a = a.
840  */
841 static ir_node *equivalent_node_neutral_zero(ir_node *n)
842 {
843         ir_node *oldn = n;
844
845         ir_node *a = get_binop_left(n);
846         ir_node *b = get_binop_right(n);
847
848         tarval *tv;
849         ir_node *on;
850
851         /* After running compute_node there is only one constant predecessor.
852            Find this predecessors value and remember the other node: */
853         if ((tv = value_of(a)) != tarval_bad) {
854                 on = b;
855         } else if ((tv = value_of(b)) != tarval_bad) {
856                 on = a;
857         } else
858                 return n;
859
860         /* If this predecessors constant value is zero, the operation is
861          * unnecessary. Remove it.
862          *
863          * Beware: If n is a Add, the mode of on and n might be different
864          * which happens in this rare construction: NULL + 3.
865          * Then, a Conv would be needed which we cannot include here.
866          */
867         if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
868                 n = on;
869
870                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
871         }
872
873         return n;
874 }  /* equivalent_node_neutral_zero */
875
876 /**
877  * Eor is commutative and has neutral 0.
878  */
879 static ir_node *equivalent_node_Eor(ir_node *n)
880 {
881         ir_node *oldn = n;
882         ir_node *a;
883         ir_node *b;
884
885         n = equivalent_node_neutral_zero(n);
886         if (n != oldn) return n;
887
888         a = get_Eor_left(n);
889         b = get_Eor_right(n);
890
891         if (is_Eor(a)) {
892                 ir_node *aa = get_Eor_left(a);
893                 ir_node *ab = get_Eor_right(a);
894
895                 if (aa == b) {
896                         /* (a ^ b) ^ a -> b */
897                         n = ab;
898                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
899                         return n;
900                 } else if (ab == b) {
901                         /* (a ^ b) ^ b -> a */
902                         n = aa;
903                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
904                         return n;
905                 }
906         }
907         if (is_Eor(b)) {
908                 ir_node *ba = get_Eor_left(b);
909                 ir_node *bb = get_Eor_right(b);
910
911                 if (ba == a) {
912                         /* a ^ (a ^ b) -> b */
913                         n = bb;
914                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
915                         return n;
916                 } else if (bb == a) {
917                         /* a ^ (b ^ a) -> b */
918                         n = ba;
919                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
920                         return n;
921                 }
922         }
923
924         return n;
925 }
926
927 /*
928  * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
929  *
930  * The second one looks strange, but this construct
931  * is used heavily in the LCC sources :-).
932  *
933  * Beware: The Mode of an Add may be different than the mode of its
934  * predecessors, so we could not return a predecessors in all cases.
935  */
936 static ir_node *equivalent_node_Add(ir_node *n) {
937         ir_node *oldn = n;
938         ir_node *left, *right;
939         ir_mode *mode = get_irn_mode(n);
940
941         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
942         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
943                 return n;
944
945         n = equivalent_node_neutral_zero(n);
946         if (n != oldn)
947                 return n;
948
949         left  = get_Add_left(n);
950         right = get_Add_right(n);
951
952         if (is_Sub(left)) {
953                 if (get_Sub_right(left) == right) {
954                         /* (a - x) + x */
955
956                         n = get_Sub_left(left);
957                         if (mode == get_irn_mode(n)) {
958                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
959                                 return n;
960                         }
961                 }
962         }
963         if (is_Sub(right)) {
964                 if (get_Sub_right(right) == left) {
965                         /* x + (a - x) */
966
967                         n = get_Sub_left(right);
968                         if (mode == get_irn_mode(n)) {
969                                 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
970                                 return n;
971                         }
972                 }
973         }
974         return n;
975 }  /* equivalent_node_Add */
976
977 /**
978  * optimize operations that are not commutative but have neutral 0 on left,
979  * so a op 0 = a.
980  */
981 static ir_node *equivalent_node_left_zero(ir_node *n) {
982         ir_node *oldn = n;
983
984         ir_node *a = get_binop_left(n);
985         ir_node *b = get_binop_right(n);
986
987         if (is_Const(b) && is_Const_null(b)) {
988                 n = a;
989
990                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
991         }
992         return n;
993 }  /* equivalent_node_left_zero */
994
995 #define equivalent_node_Shl   equivalent_node_left_zero
996 #define equivalent_node_Shr   equivalent_node_left_zero
997 #define equivalent_node_Shrs  equivalent_node_left_zero
998 #define equivalent_node_Rot   equivalent_node_left_zero
999
1000 /**
1001  * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1002  *
1003  * The second one looks strange, but this construct
1004  * is used heavily in the LCC sources :-).
1005  *
1006  * Beware: The Mode of a Sub may be different than the mode of its
1007  * predecessors, so we could not return a predecessors in all cases.
1008  */
1009 static ir_node *equivalent_node_Sub(ir_node *n) {
1010         ir_node *oldn = n;
1011         ir_node *b;
1012         ir_mode *mode = get_irn_mode(n);
1013
1014         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1015         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1016                 return n;
1017
1018         b = get_Sub_right(n);
1019
1020         /* Beware: modes might be different */
1021         if (is_Const(b) && is_Const_null(b)) {
1022                 ir_node *a = get_Sub_left(n);
1023                 if (mode == get_irn_mode(a)) {
1024                         n = a;
1025
1026                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1027                 }
1028         }
1029         return n;
1030 }  /* equivalent_node_Sub */
1031
1032
1033 /**
1034  * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1035  *
1036  * @todo
1037  *   -(-a) == a, but might overflow two times.
1038  *   We handle it anyway here but the better way would be a
1039  *   flag. This would be needed for Pascal for instance.
1040  */
1041 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1042         ir_node *oldn = n;
1043         ir_node *pred = get_unop_op(n);
1044
1045         /* optimize symmetric unop */
1046         if (get_irn_op(pred) == get_irn_op(n)) {
1047                 n = get_unop_op(pred);
1048                 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1049         }
1050         return n;
1051 }  /* equivalent_node_idempotent_unop */
1052
1053 /** Optimize Not(Not(x)) == x. */
1054 #define equivalent_node_Not    equivalent_node_idempotent_unop
1055
1056 /** -(-x) == x       ??? Is this possible or can --x raise an
1057                        out of bounds exception if min =! max? */
1058 #define equivalent_node_Minus  equivalent_node_idempotent_unop
1059
1060 /**
1061  * Optimize a * 1 = 1 * a = a.
1062  */
1063 static ir_node *equivalent_node_Mul(ir_node *n) {
1064         ir_node *oldn = n;
1065         ir_node *a = get_Mul_left(n);
1066
1067         /* we can handle here only the n * n = n bit cases */
1068         if (get_irn_mode(n) == get_irn_mode(a)) {
1069                 ir_node *b = get_Mul_right(n);
1070
1071                 /* Mul is commutative and has again an other neutral element. */
1072                 if (is_Const(a) && is_Const_one(a)) {
1073                         n = b;
1074                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1075                 } else if (is_Const(b) && is_Const_one(b)) {
1076                         n = a;
1077                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1078                 }
1079         }
1080         return n;
1081 }  /* equivalent_node_Mul */
1082
1083 /**
1084  * Optimize a / 1 = a.
1085  */
1086 static ir_node *equivalent_node_Div(ir_node *n) {
1087         ir_node *a = get_Div_left(n);
1088         ir_node *b = get_Div_right(n);
1089
1090         /* Div is not commutative. */
1091         if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1092                 /* Turn Div into a tuple (mem, bad, a) */
1093                 ir_node *mem = get_Div_mem(n);
1094                 ir_node *blk = get_irn_n(n, -1);
1095                 turn_into_tuple(n, pn_Div_max);
1096                 set_Tuple_pred(n, pn_Div_M,         mem);
1097                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1098                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());        /* no exception */
1099                 set_Tuple_pred(n, pn_Div_res,       a);
1100         }
1101         return n;
1102 }  /* equivalent_node_Div */
1103
1104 /**
1105  * Optimize a / 1.0 = a.
1106  */
1107 static ir_node *equivalent_node_Quot(ir_node *n) {
1108         ir_node *a = get_Quot_left(n);
1109         ir_node *b = get_Quot_right(n);
1110
1111         /* Div is not commutative. */
1112         if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1113                 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1114                 ir_node *mem = get_Quot_mem(n);
1115                 ir_node *blk = get_irn_n(n, -1);
1116                 turn_into_tuple(n, pn_Quot_max);
1117                 set_Tuple_pred(n, pn_Quot_M,         mem);
1118                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1119                 set_Tuple_pred(n, pn_Quot_X_except,  new_Bad());        /* no exception */
1120                 set_Tuple_pred(n, pn_Quot_res,       a);
1121         }
1122         return n;
1123 }  /* equivalent_node_Quot */
1124
1125 /**
1126  * Optimize a / 1 = a.
1127  */
1128 static ir_node *equivalent_node_DivMod(ir_node *n) {
1129         ir_node *b = get_DivMod_right(n);
1130
1131         /* Div is not commutative. */
1132         if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1133                 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1134                 ir_node *a = get_DivMod_left(n);
1135                 ir_node *mem = get_Div_mem(n);
1136                 ir_node *blk = get_irn_n(n, -1);
1137                 ir_mode *mode = get_DivMod_resmode(n);
1138
1139                 turn_into_tuple(n, pn_DivMod_max);
1140                 set_Tuple_pred(n, pn_DivMod_M,         mem);
1141                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1142                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());        /* no exception */
1143                 set_Tuple_pred(n, pn_DivMod_res_div,   a);
1144                 set_Tuple_pred(n, pn_DivMod_res_mod,   new_Const(mode, get_mode_null(mode)));
1145         }
1146         return n;
1147 }  /* equivalent_node_DivMod */
1148
1149 /**
1150  * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1151  */
1152 static ir_node *equivalent_node_Or(ir_node *n) {
1153         ir_node *oldn = n;
1154
1155         ir_node *a = get_Or_left(n);
1156         ir_node *b = get_Or_right(n);
1157
1158         if (a == b) {
1159                 n = a;    /* Or has it's own neutral element */
1160                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1161         } else if (is_Const(a) && is_Const_null(a)) {
1162                 n = b;
1163                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1164         } else if (is_Const(b) && is_Const_null(b)) {
1165                 n = a;
1166                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1167         }
1168
1169         return n;
1170 }  /* equivalent_node_Or */
1171
1172 /**
1173  * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1174  */
1175 static ir_node *equivalent_node_And(ir_node *n) {
1176         ir_node *oldn = n;
1177
1178         ir_node *a = get_And_left(n);
1179         ir_node *b = get_And_right(n);
1180
1181         if (a == b) {
1182                 n = a;    /* And has it's own neutral element */
1183                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1184                 return n;
1185         }
1186         if (is_Const(a) && is_Const_all_one(a)) {
1187                 n = b;
1188                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1189                 return n;
1190         }
1191         if (is_Const(b) && is_Const_all_one(b)) {
1192                 n = a;
1193                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1194                 return n;
1195         }
1196         if (is_Or(a)) {
1197                 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1198                         /* (a|X) & a */
1199                         n = b;
1200                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1201                         return n;
1202                 }
1203         }
1204         if (is_Or(b)) {
1205                 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1206                         /* a & (a|X) */
1207                         n = a;
1208                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1209                         return n;
1210                 }
1211         }
1212
1213         return n;
1214 }  /* equivalent_node_And */
1215
1216 /**
1217  * Try to remove useless Conv's:
1218  */
1219 static ir_node *equivalent_node_Conv(ir_node *n) {
1220         ir_node *oldn = n;
1221         ir_node *a = get_Conv_op(n);
1222
1223         ir_mode *n_mode = get_irn_mode(n);
1224         ir_mode *a_mode = get_irn_mode(a);
1225
1226         if (n_mode == a_mode) { /* No Conv necessary */
1227                 if (get_Conv_strict(n)) {
1228                         /* special case: the predecessor might be a also a Conv */
1229                         if (is_Conv(a)) {
1230                                 if (! get_Conv_strict(a)) {
1231                                         /* first one is not strict, kick it */
1232                                         set_Conv_op(n, get_Conv_op(a));
1233                                         return n;
1234                                 }
1235                                 /* else both are strict conv, second is superfluous */
1236                         } else if(is_Proj(a)) {
1237                                 ir_node *pred = get_Proj_pred(a);
1238                                 if(is_Load(pred)) {
1239                                         /* loads always return with the exact precision of n_mode */
1240                                         assert(get_Load_mode(pred) == n_mode);
1241                                         return a;
1242                                 }
1243                         }
1244
1245                         /* leave strict floating point Conv's */
1246                         return n;
1247                 }
1248                 n = a;
1249                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1250         } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1251                 ir_node *b      = get_Conv_op(a);
1252                 ir_mode *b_mode = get_irn_mode(b);
1253
1254                 if (n_mode == b_mode) {
1255                         if (n_mode == mode_b) {
1256                                 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1257                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1258                         } else if (mode_is_int(n_mode)) {
1259                                 if (get_mode_size_bits(b_mode) <= get_mode_size_bits(a_mode)) {
1260                                         n = b;        /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1261                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1262                                 }
1263                         }
1264                 }
1265         }
1266         return n;
1267 }  /* equivalent_node_Conv */
1268
1269 /**
1270  * A Cast may be removed if the type of the previous node
1271  * is already the type of the Cast.
1272  */
1273 static ir_node *equivalent_node_Cast(ir_node *n) {
1274         ir_node *oldn = n;
1275         ir_node *pred = get_Cast_op(n);
1276
1277         if (get_irn_type(pred) == get_Cast_type(n)) {
1278                 n = pred;
1279                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1280         }
1281         return n;
1282 }  /* equivalent_node_Cast */
1283
1284 /**
1285  * Several optimizations:
1286  * - no Phi in start block.
1287  * - remove Id operators that are inputs to Phi
1288  * - fold Phi-nodes, iff they have only one predecessor except
1289  *   themselves.
1290  */
1291 static ir_node *equivalent_node_Phi(ir_node *n) {
1292         int i, n_preds;
1293
1294         ir_node *oldn = n;
1295         ir_node *block;
1296         ir_node *first_val = NULL; /* to shutup gcc */
1297
1298         if (!get_opt_normalize()) return n;
1299
1300         n_preds = get_Phi_n_preds(n);
1301
1302         block = get_nodes_block(n);
1303         if ((is_Block_dead(block)) ||                  /* Control dead */
1304                 (block == get_irg_start_block(current_ir_graph)))  /* There should be no Phi nodes */
1305                 return new_Bad();                                    /* in the Start Block. */
1306
1307         if (n_preds == 0) return n;           /* Phi of dead Region without predecessors. */
1308
1309         /* If the Block has a Bad pred, we also have one. */
1310         for (i = 0;  i < n_preds;  ++i)
1311                 if (is_Bad(get_Block_cfgpred(block, i)))
1312                         set_Phi_pred(n, i, new_Bad());
1313
1314         /* Find first non-self-referencing input */
1315         for (i = 0; i < n_preds; ++i) {
1316                 first_val = get_Phi_pred(n, i);
1317                 if (   (first_val != n)                            /* not self pointer */
1318 #if 1
1319                     && (! is_Bad(first_val))
1320 #endif
1321                    ) {        /* value not dead */
1322                         break;          /* then found first value. */
1323                 }
1324         }
1325
1326         if (i >= n_preds) {
1327                 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1328                 return new_Bad();
1329         }
1330
1331         /* search for rest of inputs, determine if any of these
1332         are non-self-referencing */
1333         while (++i < n_preds) {
1334                 ir_node *scnd_val = get_Phi_pred(n, i);
1335                 if (   (scnd_val != n)
1336                     && (scnd_val != first_val)
1337 #if 1
1338                     && (! is_Bad(scnd_val))
1339 #endif
1340                         ) {
1341                         break;
1342                 }
1343         }
1344
1345         if (i >= n_preds) {
1346                 /* Fold, if no multiple distinct non-self-referencing inputs */
1347                 n = first_val;
1348                 DBG_OPT_PHI(oldn, n);
1349         }
1350         return n;
1351 }  /* equivalent_node_Phi */
1352
1353 /**
1354  * Several optimizations:
1355  * - no Sync in start block.
1356  * - fold Sync-nodes, iff they have only one predecessor except
1357  *   themselves.
1358  */
1359 static ir_node *equivalent_node_Sync(ir_node *n) {
1360         int arity = get_Sync_n_preds(n);
1361         int i;
1362
1363         for (i = 0; i < arity;) {
1364                 ir_node *pred = get_Sync_pred(n, i);
1365                 int      j;
1366
1367                 /* Remove Bad predecessors */
1368                 if (is_Bad(pred)) {
1369                         del_Sync_n(n, i);
1370                         --arity;
1371                         continue;
1372                 }
1373
1374                 /* Remove duplicate predecessors */
1375                 for (j = 0;; ++j) {
1376                         if (j >= i) {
1377                                 ++i;
1378                                 break;
1379                         }
1380                         if (get_Sync_pred(n, j) == pred) {
1381                                 del_Sync_n(n, i);
1382                                 --arity;
1383                                 break;
1384                         }
1385                 }
1386         }
1387
1388         if (arity == 0) return new_Bad();
1389         if (arity == 1) return get_Sync_pred(n, 0);
1390         return n;
1391 }  /* equivalent_node_Sync */
1392
1393 /**
1394  * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1395  * ProjX(Load) and ProjX(Store).
1396  */
1397 static ir_node *equivalent_node_Proj(ir_node *proj) {
1398         ir_node *oldn = proj;
1399         ir_node *a = get_Proj_pred(proj);
1400
1401         if (is_Tuple(a)) {
1402                 /* Remove the Tuple/Proj combination. */
1403                 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1404                         proj = get_Tuple_pred(a, get_Proj_proj(proj));
1405                         DBG_OPT_TUPLE(oldn, a, proj);
1406                 } else {
1407                          /* This should not happen! */
1408                         assert(! "found a Proj with higher number than Tuple predecessors");
1409                         proj = new_Bad();
1410                 }
1411         } else if (get_irn_mode(proj) == mode_X) {
1412                 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1413                         /* Remove dead control flow -- early gigo(). */
1414                         proj = new_Bad();
1415                 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1416                         if (is_Load(a)) {
1417                                 /* get the Load address */
1418                                 ir_node *addr = get_Load_ptr(a);
1419                                 ir_node *blk  = get_irn_n(a, -1);
1420                                 ir_node *confirm;
1421
1422                                 if (value_not_null(addr, &confirm)) {
1423                                         if (confirm == NULL) {
1424                                                 /* this node may float if it did not depend on a Confirm */
1425                                                 set_irn_pinned(a, op_pin_state_floats);
1426                                         }
1427                                         if (get_Proj_proj(proj) == pn_Load_X_except) {
1428                                                 DBG_OPT_EXC_REM(proj);
1429                                                 return new_Bad();
1430                                         } else
1431                                                 return new_r_Jmp(current_ir_graph, blk);
1432                                 }
1433                         } else if (is_Store(a)) {
1434                                 /* get the load/store address */
1435                                 ir_node *addr = get_Store_ptr(a);
1436                                 ir_node *blk  = get_irn_n(a, -1);
1437                                 ir_node *confirm;
1438
1439                                 if (value_not_null(addr, &confirm)) {
1440                                         if (confirm == NULL) {
1441                                                 /* this node may float if it did not depend on a Confirm */
1442                                                 set_irn_pinned(a, op_pin_state_floats);
1443                                         }
1444                                         if (get_Proj_proj(proj) == pn_Store_X_except) {
1445                                                 DBG_OPT_EXC_REM(proj);
1446                                                 return new_Bad();
1447                                         } else
1448                                                 return new_r_Jmp(current_ir_graph, blk);
1449                                 }
1450                         }
1451                 }
1452         }
1453
1454         return proj;
1455 }  /* equivalent_node_Proj */
1456
1457 /**
1458  * Remove Id's.
1459  */
1460 static ir_node *equivalent_node_Id(ir_node *n) {
1461         ir_node *oldn = n;
1462
1463         do {
1464                 n = get_Id_pred(n);
1465         } while (get_irn_op(n) == op_Id);
1466
1467         DBG_OPT_ID(oldn, n);
1468         return n;
1469 }  /* equivalent_node_Id */
1470
1471 /**
1472  * Optimize a Mux.
1473  */
1474 static ir_node *equivalent_node_Mux(ir_node *n)
1475 {
1476         ir_node *oldn = n, *sel = get_Mux_sel(n);
1477         tarval *ts = value_of(sel);
1478
1479         /* Mux(true, f, t) == t */
1480         if (ts == tarval_b_true) {
1481                 n = get_Mux_true(n);
1482                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1483         }
1484         /* Mux(false, f, t) == f */
1485         else if (ts == tarval_b_false) {
1486                 n = get_Mux_false(n);
1487                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1488         }
1489         /* Mux(v, x, x) == x */
1490         else if (get_Mux_false(n) == get_Mux_true(n)) {
1491                 n = get_Mux_true(n);
1492                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1493         }
1494         else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1495                 ir_node *cmp = get_Proj_pred(sel);
1496                 long proj_nr = get_Proj_proj(sel);
1497                 ir_node *f   = get_Mux_false(n);
1498                 ir_node *t   = get_Mux_true(n);
1499
1500                 /*
1501                  * Note further that these optimization work even for floating point
1502                  * with NaN's because -NaN == NaN.
1503                  * However, if +0 and -0 is handled differently, we cannot use the first one.
1504                  */
1505                 if (is_Cmp(cmp)) {
1506                         ir_node *const cmp_l = get_Cmp_left(cmp);
1507                         ir_node *const cmp_r = get_Cmp_right(cmp);
1508
1509                         switch (proj_nr) {
1510                                 case pn_Cmp_Eq:
1511                                         if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1512                                                         (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1513                                                 n = f;
1514                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1515                                                 return n;
1516                                         }
1517                                         break;
1518
1519                                 case pn_Cmp_Lg:
1520                                 case pn_Cmp_Ne:
1521                                         if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1522                                                         (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1523                                                 n = t;
1524                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1525                                                 return n;
1526                                         }
1527                                         break;
1528                         }
1529
1530                         /*
1531                          * Note: normalization puts the constant on the right side,
1532                          * so we check only one case.
1533                          */
1534                         if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1535                                 /* Mux(t CMP 0, X, t) */
1536                                 if (is_Minus(f) && get_Minus_op(f) == t) {
1537                                         /* Mux(t CMP 0, -t, t) */
1538                                         if (proj_nr == pn_Cmp_Eq) {
1539                                                 /* Mux(t == 0, -t, t)  ==>  -t */
1540                                                 n = f;
1541                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1542                                         } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1543                                                 /* Mux(t != 0, -t, t)  ==> t */
1544                                                 n = t;
1545                                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1546                                         }
1547                                 }
1548                         }
1549                 }
1550         }
1551         return n;
1552 }  /* equivalent_node_Mux */
1553
1554 /**
1555  * Returns a equivalent node of a Psi: if a condition is true
1556  * and all previous conditions are false we know its value.
1557  * If all conditions are false its value is the default one.
1558  */
1559 static ir_node *equivalent_node_Psi(ir_node *n) {
1560         if (is_Mux(n))
1561                 return equivalent_node_Mux(n);
1562         return n;
1563 }  /* equivalent_node_Psi */
1564
1565 /**
1566  * Optimize -a CMP -b into b CMP a.
1567  * This works only for for modes where unary Minus
1568  * cannot Overflow.
1569  * Note that two-complement integers can Overflow
1570  * so it will NOT work.
1571  *
1572  * For == and != can be handled in Proj(Cmp)
1573  */
1574 static ir_node *equivalent_node_Cmp(ir_node *n) {
1575         ir_node *left  = get_Cmp_left(n);
1576         ir_node *right = get_Cmp_right(n);
1577
1578         if (is_Minus(left) && is_Minus(right) &&
1579                 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1580                 left  = get_Minus_op(left);
1581                 right = get_Minus_op(right);
1582                 set_Cmp_left(n, right);
1583                 set_Cmp_right(n, left);
1584         }
1585         return n;
1586 }  /* equivalent_node_Cmp */
1587
1588 /**
1589  * Remove Confirm nodes if setting is on.
1590  * Replace Confirms(x, '=', Constlike) by Constlike.
1591  */
1592 static ir_node *equivalent_node_Confirm(ir_node *n) {
1593         ir_node *pred = get_Confirm_value(n);
1594         pn_Cmp  pnc   = get_Confirm_cmp(n);
1595
1596         if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1597                 /*
1598                  * rare case: two identical Confirms one after another,
1599                  * replace the second one with the first.
1600                  */
1601                 n = pred;
1602         }
1603         if (get_opt_remove_confirm())
1604                 return get_Confirm_value(n);
1605         return n;
1606 }
1607
1608 /**
1609  * Optimize CopyB(mem, x, x) into a Nop.
1610  */
1611 static ir_node *equivalent_node_CopyB(ir_node *n) {
1612         ir_node *a = get_CopyB_dst(n);
1613         ir_node *b = get_CopyB_src(n);
1614
1615         if (a == b) {
1616                 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1617                 ir_node *mem = get_CopyB_mem(n);
1618                 ir_node *blk = get_nodes_block(n);
1619                 turn_into_tuple(n, pn_CopyB_max);
1620                 set_Tuple_pred(n, pn_CopyB_M,         mem);
1621                 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1622                 set_Tuple_pred(n, pn_CopyB_X_except,  new_Bad());        /* no exception */
1623                 set_Tuple_pred(n, pn_CopyB_M_except,  new_Bad());
1624         }
1625         return n;
1626 }  /* equivalent_node_CopyB */
1627
1628 /**
1629  * Optimize Bounds(idx, idx, upper) into idx.
1630  */
1631 static ir_node *equivalent_node_Bound(ir_node *n) {
1632         ir_node *idx  = get_Bound_index(n);
1633         ir_node *pred = skip_Proj(idx);
1634         int ret_tuple = 0;
1635
1636         if (is_Bound(pred)) {
1637                 /*
1638                  * idx was Bounds checked in the same MacroBlock previously,
1639                  * it is still valid if lower <= pred_lower && pred_upper <= upper.
1640                  */
1641                 ir_node *lower = get_Bound_lower(n);
1642                 ir_node *upper = get_Bound_upper(n);
1643                 if (get_Bound_lower(pred) == lower &&
1644                         get_Bound_upper(pred) == upper &&
1645                         get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1646                         /*
1647                          * One could expect that we simply return the previous
1648                          * Bound here. However, this would be wrong, as we could
1649                          * add an exception Proj to a new location then.
1650                          * So, we must turn in into a tuple.
1651                          */
1652                         ret_tuple = 1;
1653                 }
1654         }
1655         if (ret_tuple) {
1656                 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1657                 ir_node *mem = get_Bound_mem(n);
1658                 ir_node *blk = get_nodes_block(n);
1659                 turn_into_tuple(n, pn_Bound_max);
1660                 set_Tuple_pred(n, pn_Bound_M,         mem);
1661                 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk));       /* no exception */
1662                 set_Tuple_pred(n, pn_Bound_X_except,  new_Bad());       /* no exception */
1663                 set_Tuple_pred(n, pn_Bound_res,       idx);
1664         }
1665         return n;
1666 }  /* equivalent_node_Bound */
1667
1668 /**
1669  * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1670  * perform no actual computation, as, e.g., the Id nodes.  It does not create
1671  * new nodes.  It is therefore safe to free n if the node returned is not n.
1672  * If a node returns a Tuple we can not just skip it.  If the size of the
1673  * in array fits, we transform n into a tuple (e.g., Div).
1674  */
1675 ir_node *equivalent_node(ir_node *n) {
1676         if (n->op->ops.equivalent_node)
1677                 return n->op->ops.equivalent_node(n);
1678         return n;
1679 }  /* equivalent_node */
1680
1681 /**
1682  * Sets the default equivalent node operation for an ir_op_ops.
1683  *
1684  * @param code   the opcode for the default operation
1685  * @param ops    the operations initialized
1686  *
1687  * @return
1688  *    The operations.
1689  */
1690 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1691 {
1692 #define CASE(a)                                      \
1693         case iro_##a:                                    \
1694                 ops->equivalent_node  = equivalent_node_##a; \
1695                 break
1696
1697         switch (code) {
1698         CASE(Block);
1699         CASE(Jmp);
1700         CASE(Raise);
1701         CASE(Or);
1702         CASE(Add);
1703         CASE(Eor);
1704         CASE(Sub);
1705         CASE(Shl);
1706         CASE(Shr);
1707         CASE(Shrs);
1708         CASE(Rot);
1709         CASE(Not);
1710         CASE(Minus);
1711         CASE(Mul);
1712         CASE(Div);
1713         CASE(Quot);
1714         CASE(DivMod);
1715         CASE(And);
1716         CASE(Conv);
1717         CASE(Cast);
1718         CASE(Phi);
1719         CASE(Sync);
1720         CASE(Proj);
1721         CASE(Id);
1722         CASE(Mux);
1723         CASE(Psi);
1724         CASE(Cmp);
1725         CASE(Confirm);
1726         CASE(CopyB);
1727         CASE(Bound);
1728         default:
1729                 /* leave NULL */;
1730         }
1731
1732         return ops;
1733 #undef CASE
1734 }  /* firm_set_default_equivalent_node */
1735
1736 /**
1737  * Returns non-zero if a node is a Phi node
1738  * with all predecessors constant.
1739  */
1740 static int is_const_Phi(ir_node *n) {
1741         int i;
1742
1743         if (! is_Phi(n) || get_irn_arity(n) == 0)
1744                 return 0;
1745         for (i = get_irn_arity(n) - 1; i >= 0; --i)
1746                 if (! is_Const(get_irn_n(n, i)))
1747                         return 0;
1748                 return 1;
1749 }  /* is_const_Phi */
1750
1751 /**
1752  * Apply an evaluator on a binop with a constant operators (and one Phi).
1753  *
1754  * @param phi    the Phi node
1755  * @param other  the other operand
1756  * @param eval   an evaluator function
1757  * @param mode   the mode of the result, may be different from the mode of the Phi!
1758  * @param left   if non-zero, other is the left operand, else the right
1759  *
1760  * @return a new Phi node if the conversion was successful, NULL else
1761  */
1762 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1763         tarval   *tv;
1764         void     **res;
1765         ir_node  *pred;
1766         ir_graph *irg;
1767         int      i, n = get_irn_arity(phi);
1768
1769         NEW_ARR_A(void *, res, n);
1770         if (left) {
1771                 for (i = 0; i < n; ++i) {
1772                         pred = get_irn_n(phi, i);
1773                         tv   = get_Const_tarval(pred);
1774                         tv   = eval(other, tv);
1775
1776                         if (tv == tarval_bad) {
1777                                 /* folding failed, bad */
1778                                 return NULL;
1779                         }
1780                         res[i] = tv;
1781                 }
1782         } else {
1783                 for (i = 0; i < n; ++i) {
1784                         pred = get_irn_n(phi, i);
1785                         tv   = get_Const_tarval(pred);
1786                         tv   = eval(tv, other);
1787
1788                         if (tv == tarval_bad) {
1789                                 /* folding failed, bad */
1790                                 return 0;
1791                         }
1792                         res[i] = tv;
1793                 }
1794         }
1795         irg  = current_ir_graph;
1796         for (i = 0; i < n; ++i) {
1797                 pred = get_irn_n(phi, i);
1798                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1799                         mode, res[i], get_Const_type(pred));
1800         }
1801         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1802 }  /* apply_binop_on_phi */
1803
1804 /**
1805  * Apply an evaluator on a binop with two constant Phi.
1806  *
1807  * @param a      the left Phi node
1808  * @param b      the right Phi node
1809  * @param eval   an evaluator function
1810  * @param mode   the mode of the result, may be different from the mode of the Phi!
1811  *
1812  * @return a new Phi node if the conversion was successful, NULL else
1813  */
1814 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1815         tarval   *tv_l, *tv_r, *tv;
1816         void     **res;
1817         ir_node  *pred;
1818         ir_graph *irg;
1819         int      i, n;
1820
1821         if (get_nodes_block(a) != get_nodes_block(b))
1822                 return NULL;
1823
1824         n = get_irn_arity(a);
1825         NEW_ARR_A(void *, res, n);
1826
1827         for (i = 0; i < n; ++i) {
1828                 pred = get_irn_n(a, i);
1829                 tv_l = get_Const_tarval(pred);
1830                 pred = get_irn_n(b, i);
1831                 tv_r = get_Const_tarval(pred);
1832                 tv   = eval(tv_l, tv_r);
1833
1834                 if (tv == tarval_bad) {
1835                         /* folding failed, bad */
1836                         return NULL;
1837                 }
1838                 res[i] = tv;
1839         }
1840         irg  = current_ir_graph;
1841         for (i = 0; i < n; ++i) {
1842                 pred = get_irn_n(a, i);
1843                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1844         }
1845         return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1846 }  /* apply_binop_on_2_phis */
1847
1848 /**
1849  * Apply an evaluator on a unop with a constant operator (a Phi).
1850  *
1851  * @param phi    the Phi node
1852  * @param eval   an evaluator function
1853  *
1854  * @return a new Phi node if the conversion was successful, NULL else
1855  */
1856 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1857         tarval   *tv;
1858         void     **res;
1859         ir_node  *pred;
1860         ir_mode  *mode;
1861         ir_graph *irg;
1862         int      i, n = get_irn_arity(phi);
1863
1864         NEW_ARR_A(void *, res, n);
1865         for (i = 0; i < n; ++i) {
1866                 pred = get_irn_n(phi, i);
1867                 tv   = get_Const_tarval(pred);
1868                 tv   = eval(tv);
1869
1870                 if (tv == tarval_bad) {
1871                         /* folding failed, bad */
1872                         return 0;
1873                 }
1874                 res[i] = tv;
1875         }
1876         mode = get_irn_mode(phi);
1877         irg  = current_ir_graph;
1878         for (i = 0; i < n; ++i) {
1879                 pred = get_irn_n(phi, i);
1880                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1881                         mode, res[i], get_Const_type(pred));
1882         }
1883         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1884 }  /* apply_unop_on_phi */
1885
1886 /**
1887  * Apply a conversion on a constant operator (a Phi).
1888  *
1889  * @param phi    the Phi node
1890  *
1891  * @return a new Phi node if the conversion was successful, NULL else
1892  */
1893 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1894         tarval   *tv;
1895         void     **res;
1896         ir_node  *pred;
1897         ir_graph *irg;
1898         int      i, n = get_irn_arity(phi);
1899
1900         NEW_ARR_A(void *, res, n);
1901         for (i = 0; i < n; ++i) {
1902                 pred = get_irn_n(phi, i);
1903                 tv   = get_Const_tarval(pred);
1904                 tv   = tarval_convert_to(tv, mode);
1905
1906                 if (tv == tarval_bad) {
1907                         /* folding failed, bad */
1908                         return 0;
1909                 }
1910                 res[i] = tv;
1911         }
1912         irg  = current_ir_graph;
1913         for (i = 0; i < n; ++i) {
1914                 pred = get_irn_n(phi, i);
1915                 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1916                         mode, res[i], get_Const_type(pred));
1917         }
1918         return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1919 }  /* apply_conv_on_phi */
1920
1921 /**
1922  * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1923  * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1924  * If possible, remove the Conv's.
1925  */
1926 static ir_node *transform_node_AddSub(ir_node *n) {
1927         ir_mode *mode = get_irn_mode(n);
1928
1929         if (mode_is_reference(mode)) {
1930                 ir_node *left     = get_binop_left(n);
1931                 ir_node *right    = get_binop_right(n);
1932                 unsigned ref_bits = get_mode_size_bits(mode);
1933
1934                 if (is_Conv(left)) {
1935                         ir_mode *lmode = get_irn_mode(left);
1936                         unsigned bits = get_mode_size_bits(lmode);
1937
1938                         if (ref_bits == bits &&
1939                             mode_is_int(lmode) &&
1940                             get_mode_arithmetic(lmode) == irma_twos_complement) {
1941                                 ir_node *pre      = get_Conv_op(left);
1942                                 ir_mode *pre_mode = get_irn_mode(pre);
1943
1944                                 if (mode_is_int(pre_mode) &&
1945                                     get_mode_size_bits(pre_mode) == bits &&
1946                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1947                                         /* ok, this conv just changes to sign, moreover the calculation
1948                                          * is done with same number of bits as our address mode, so
1949                                          * we can ignore the conv as address calculation can be viewed
1950                                          * as either signed or unsigned
1951                                          */
1952                                         set_binop_left(n, pre);
1953                                 }
1954                         }
1955                 }
1956
1957                 if (is_Conv(right)) {
1958                         ir_mode *rmode = get_irn_mode(right);
1959                         unsigned bits = get_mode_size_bits(rmode);
1960
1961                         if (ref_bits == bits &&
1962                             mode_is_int(rmode) &&
1963                             get_mode_arithmetic(rmode) == irma_twos_complement) {
1964                                 ir_node *pre      = get_Conv_op(right);
1965                                 ir_mode *pre_mode = get_irn_mode(pre);
1966
1967                                 if (mode_is_int(pre_mode) &&
1968                                     get_mode_size_bits(pre_mode) == bits &&
1969                                     get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1970                                         /* ok, this conv just changes to sign, moreover the calculation
1971                                          * is done with same number of bits as our address mode, so
1972                                          * we can ignore the conv as address calculation can be viewed
1973                                          * as either signed or unsigned
1974                                          */
1975                                         set_binop_right(n, pre);
1976                                 }
1977                         }
1978                 }
1979
1980                 /* let address arithmetic use unsigned modes */
1981                 if (is_Const(right)) {
1982                         ir_mode *rmode = get_irn_mode(right);
1983
1984                         if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1985                                 /* convert a AddP(P, *s) into AddP(P, *u) */
1986                                 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1987
1988                                 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
1989                                 set_binop_right(n, pre);
1990                         }
1991                 }
1992         }
1993
1994         return n;
1995 }  /* transform_node_AddSub */
1996
1997 #define HANDLE_BINOP_PHI(eval, a, b, c, mode)                     \
1998   c = NULL;                                                       \
1999   if (is_Const(b) && is_const_Phi(a)) {                           \
2000     /* check for Op(Phi, Const) */                                \
2001     c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2002   }                                                               \
2003   else if (is_Const(a) && is_const_Phi(b)) {                      \
2004     /* check for Op(Const, Phi) */                                \
2005     c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2006   }                                                               \
2007   else if (is_const_Phi(a) && is_const_Phi(b)) {                  \
2008     /* check for Op(Phi, Phi) */                                  \
2009     c = apply_binop_on_2_phis(a, b, eval, mode);                  \
2010   }                                                               \
2011   if (c) {                                                        \
2012     DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);                   \
2013     return c;                                                     \
2014   }
2015
2016 #define HANDLE_UNOP_PHI(eval, a, c)               \
2017   c = NULL;                                       \
2018   if (is_const_Phi(a)) {                          \
2019     /* check for Op(Phi) */                       \
2020     c = apply_unop_on_phi(a, eval);               \
2021     if (c) {                                      \
2022       DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2023       return c;                                   \
2024     }                                             \
2025   }
2026
2027 /**
2028  * Do the AddSub optimization, then Transform
2029  *   Constant folding on Phi
2030  *   Add(a,a)          -> Mul(a, 2)
2031  *   Add(Mul(a, x), a) -> Mul(a, x+1)
2032  * if the mode is integer or float.
2033  * Transform Add(a,-b) into Sub(a,b).
2034  * Reassociation might fold this further.
2035  */
2036 static ir_node *transform_node_Add(ir_node *n) {
2037         ir_mode *mode;
2038         ir_node *a, *b, *c, *oldn = n;
2039
2040         n = transform_node_AddSub(n);
2041
2042         a = get_Add_left(n);
2043         b = get_Add_right(n);
2044
2045         mode = get_irn_mode(n);
2046
2047         if (mode_is_reference(mode)) {
2048                 ir_mode *lmode = get_irn_mode(a);
2049
2050                 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2051                         /* an Add(a, NULL) is a hidden Conv */
2052                         dbg_info *dbg = get_irn_dbg_info(n);
2053                         return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2054                 }
2055         }
2056
2057         HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2058
2059         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2060         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2061                 return n;
2062
2063         if (mode_is_num(mode)) {
2064                 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2065                 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2066                         ir_node *block = get_nodes_block(n);
2067
2068                         n = new_rd_Mul(
2069                                 get_irn_dbg_info(n),
2070                                 current_ir_graph,
2071                                 block,
2072                                 a,
2073                                 new_r_Const_long(current_ir_graph, block, mode, 2),
2074                                 mode);
2075                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2076                         return n;
2077                 }
2078                 if (is_Minus(a)) {
2079                         n = new_rd_Sub(
2080                                         get_irn_dbg_info(n),
2081                                         current_ir_graph,
2082                                         get_irn_n(n, -1),
2083                                         b,
2084                                         get_Minus_op(a),
2085                                         mode);
2086                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2087                         return n;
2088                 }
2089                 if (is_Minus(b)) {
2090                         n = new_rd_Sub(
2091                                         get_irn_dbg_info(n),
2092                                         current_ir_graph,
2093                                         get_irn_n(n, -1),
2094                                         a,
2095                                         get_Minus_op(b),
2096                                         mode);
2097                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2098                         return n;
2099                 }
2100                 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2101                         /* Here we rely on constants be on the RIGHT side */
2102                         if (is_Not(a)) {
2103                                 ir_node *op = get_Not_op(a);
2104
2105                                 if (is_Const(b) && is_Const_one(b)) {
2106                                         /* ~x + 1 = -x */
2107                                         ir_node *blk = get_irn_n(n, -1);
2108                                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2109                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2110                                         return n;
2111                                 }
2112                                 if (op == b) {
2113                                         /* ~x + x = -1 */
2114                                         ir_node *blk = get_irn_n(n, -1);
2115                                         n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2116                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2117                                         return n;
2118                                 }
2119                         }
2120                         if (is_Not(b)) {
2121                                 ir_node *op = get_Not_op(b);
2122
2123                                 if (op == a) {
2124                                         /* x + ~x = -1 */
2125                                         ir_node *blk = get_irn_n(n, -1);
2126                                         n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2127                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2128                                         return n;
2129                                 }
2130                         }
2131                 }
2132         }
2133         return n;
2134 }  /* transform_node_Add */
2135
2136 /**
2137  * returns -cnst or NULL if impossible
2138  */
2139 static ir_node *const_negate(ir_node *cnst) {
2140         tarval   *tv    = tarval_neg(get_Const_tarval(cnst));
2141         dbg_info *dbgi  = get_irn_dbg_info(cnst);
2142         ir_graph *irg   = get_irn_irg(cnst);
2143         ir_node  *block = get_nodes_block(cnst);
2144         ir_mode  *mode  = get_irn_mode(cnst);
2145         if (tv == tarval_bad) return NULL;
2146         return new_rd_Const(dbgi, irg, block, mode, tv);
2147 }
2148
2149 /**
2150  * Do the AddSub optimization, then Transform
2151  *   Constant folding on Phi
2152  *   Sub(0,a)          -> Minus(a)
2153  *   Sub(Mul(a, x), a) -> Mul(a, x-1)
2154  *   Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2155  *   Sub(Add(a, x), x) -> a
2156  *   Sub(x, Add(x, a)) -> -a
2157  *   Sub(x, Const)     -> Add(x, -Const)
2158  */
2159 static ir_node *transform_node_Sub(ir_node *n) {
2160         ir_mode *mode;
2161         ir_node *oldn = n;
2162         ir_node *a, *b, *c;
2163
2164         n = transform_node_AddSub(n);
2165
2166         a = get_Sub_left(n);
2167         b = get_Sub_right(n);
2168
2169         mode = get_irn_mode(n);
2170
2171         if (mode_is_int(mode)) {
2172                 ir_mode *lmode = get_irn_mode(a);
2173
2174                 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2175                         /* a Sub(a, NULL) is a hidden Conv */
2176                         dbg_info *dbg = get_irn_dbg_info(n);
2177                         return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2178                 }
2179         }
2180
2181 restart:
2182         HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2183
2184         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2185         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2186                 return n;
2187
2188         if (is_Const(b) && get_irn_mode(b) != mode_P) {
2189                 /* a - C -> a + (-C) */
2190                 ir_node *cnst = const_negate(b);
2191                 if (cnst != NULL) {
2192                         ir_node  *block = get_nodes_block(n);
2193                         dbg_info *dbgi  = get_irn_dbg_info(n);
2194                         ir_graph *irg   = get_irn_irg(n);
2195
2196                         n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2197                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2198                         return n;
2199                 }
2200         }
2201
2202         if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2203                 ir_graph *irg   = current_ir_graph;
2204                 dbg_info *dbg   = get_irn_dbg_info(n);
2205                 ir_node  *block = get_nodes_block(n);
2206                 ir_node  *left  = get_Minus_op(a);
2207                 ir_node  *add   = new_rd_Add(dbg, irg, block, left, b, mode);
2208
2209                 n = new_rd_Minus(dbg, irg, block, add, mode);
2210                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2211                 return n;
2212         } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2213                 ir_graph *irg   = current_ir_graph;
2214                 dbg_info *dbg   = get_irn_dbg_info(n);
2215                 ir_node  *block = get_nodes_block(n);
2216                 ir_node  *right = get_Minus_op(b);
2217
2218                 n = new_rd_Add(dbg, irg, block, a, right, mode);
2219                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2220                 return n;
2221         } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2222                 ir_graph *irg     = current_ir_graph;
2223                 dbg_info *s_dbg   = get_irn_dbg_info(b);
2224                 ir_node  *s_block = get_nodes_block(b);
2225                 ir_node  *s_left  = get_Sub_right(b);
2226                 ir_node  *s_right = get_Sub_left(b);
2227                 ir_mode  *s_mode  = get_irn_mode(b);
2228                 ir_node  *sub     = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2229                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2230                 ir_node  *a_block = get_nodes_block(n);
2231
2232                 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2233                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2234                 return n;
2235         } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2236                 ir_node *m_right = get_Mul_right(b);
2237                 if (is_Const(m_right)) {
2238                         ir_node *cnst2 = const_negate(m_right);
2239                         if (cnst2 != NULL) {
2240                                 ir_graph *irg     = current_ir_graph;
2241                                 dbg_info *m_dbg   = get_irn_dbg_info(b);
2242                                 ir_node  *m_block = get_nodes_block(b);
2243                                 ir_node  *m_left  = get_Mul_left(b);
2244                                 ir_mode  *m_mode  = get_irn_mode(b);
2245                                 ir_node  *mul     = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2246                                 dbg_info *a_dbg   = get_irn_dbg_info(n);
2247                                 ir_node  *a_block = get_nodes_block(n);
2248
2249                                 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2250                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2251                                 return n;
2252                         }
2253                 }
2254         }
2255
2256         /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2257         if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2258                 n = new_rd_Minus(
2259                                 get_irn_dbg_info(n),
2260                                 current_ir_graph,
2261                                 get_irn_n(n, -1),
2262                                 b,
2263                                 mode);
2264                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2265                 return n;
2266         }
2267         if (is_Add(a)) {
2268                 if (mode_wrap_around(mode)) {
2269                         ir_node *left  = get_Add_left(a);
2270                         ir_node *right = get_Add_right(a);
2271
2272                         /* FIXME: Does the Conv's work only for two complement or generally? */
2273                         if (left == b) {
2274                                 if (mode != get_irn_mode(right)) {
2275                                         /* This Sub is an effective Cast */
2276                                         right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2277                                 }
2278                                 n = right;
2279                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2280                                 return n;
2281                         } else if (right == b) {
2282                                 if (mode != get_irn_mode(left)) {
2283                                         /* This Sub is an effective Cast */
2284                                         left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2285                                 }
2286                                 n = left;
2287                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2288                                 return n;
2289                         }
2290                 }
2291         }
2292         if (is_Add(b)) {
2293                 if (mode_wrap_around(mode)) {
2294                         ir_node *left  = get_Add_left(b);
2295                         ir_node *right = get_Add_right(b);
2296
2297                         /* FIXME: Does the Conv's work only for two complement or generally? */
2298                         if (left == a) {
2299                                 ir_mode *r_mode = get_irn_mode(right);
2300
2301                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2302                                 if (mode != r_mode) {
2303                                         /* This Sub is an effective Cast */
2304                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2305                                 }
2306                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2307                                 return n;
2308                         } else if (right == a) {
2309                                 ir_mode *l_mode = get_irn_mode(left);
2310
2311                                 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2312                                 if (mode != l_mode) {
2313                                         /* This Sub is an effective Cast */
2314                                         n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2315                                 }
2316                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2317                                 return n;
2318                         }
2319                 }
2320         }
2321         if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2322                 ir_mode *mode = get_irn_mode(a);
2323
2324                 if (mode == get_irn_mode(b)) {
2325                         ir_mode *ma, *mb;
2326                         ir_node *op_a = get_Conv_op(a);
2327                         ir_node *op_b = get_Conv_op(b);
2328
2329                         /* check if it's allowed to skip the conv */
2330                         ma = get_irn_mode(op_a);
2331                         mb = get_irn_mode(op_b);
2332
2333                         if (mode_is_reference(ma) && mode_is_reference(mb)) {
2334                                 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2335                                 a = op_a; b = op_b;
2336                                 set_Sub_left(n, a);
2337                                 set_Sub_right(n, b);
2338
2339                                 goto restart;
2340                         }
2341                 }
2342         }
2343         /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2344         if (!is_reassoc_running() && is_Mul(a)) {
2345                 ir_node *ma = get_Mul_left(a);
2346                 ir_node *mb = get_Mul_right(a);
2347
2348                 if (ma == b) {
2349                         ir_node *blk = get_irn_n(n, -1);
2350                         n = new_rd_Mul(
2351                                         get_irn_dbg_info(n),
2352                                         current_ir_graph, blk,
2353                                         ma,
2354                                         new_rd_Sub(
2355                                                 get_irn_dbg_info(n),
2356                                                 current_ir_graph, blk,
2357                                                 mb,
2358                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2359                                                 mode),
2360                                         mode);
2361                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2362                         return n;
2363                 } else if (mb == b) {
2364                         ir_node *blk = get_irn_n(n, -1);
2365                         n = new_rd_Mul(
2366                                         get_irn_dbg_info(n),
2367                                         current_ir_graph, blk,
2368                                         mb,
2369                                         new_rd_Sub(
2370                                                 get_irn_dbg_info(n),
2371                                                 current_ir_graph, blk,
2372                                                 ma,
2373                                                 new_r_Const_long(current_ir_graph, blk, mode, 1),
2374                                                 mode),
2375                                         mode);
2376                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2377                         return n;
2378                 }
2379         }
2380         if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2381                 ir_node *x   =      get_Sub_left(a);
2382                 ir_node *y        = get_Sub_right(a);
2383                 ir_node *blk      = get_irn_n(n, -1);
2384                 ir_mode *m_b      = get_irn_mode(b);
2385                 ir_mode *m_y      = get_irn_mode(y);
2386                 ir_mode *add_mode;
2387                 ir_node *add;
2388
2389                 /* Determine the right mode for the Add. */
2390                 if (m_b == m_y)
2391                         add_mode = m_b;
2392                 else if (mode_is_reference(m_b))
2393                         add_mode = m_b;
2394                 else if (mode_is_reference(m_y))
2395                         add_mode = m_y;
2396                 else {
2397                         /*
2398                          * Both modes are different but none is reference,
2399                          * happens for instance in SubP(SubP(P, Iu), Is).
2400                          * We have two possibilities here: Cast or ignore.
2401                          * Currently we ignore this case.
2402                          */
2403                         return n;
2404                 }
2405
2406                 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2407
2408                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2409                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2410                 return n;
2411         }
2412
2413         if (get_mode_arithmetic(mode) == irma_twos_complement) {
2414                 if (is_Const(a) && is_Not(b)) {
2415                         /* c - ~X = X + (c+1) */
2416                         tarval *tv = get_Const_tarval(a);
2417
2418                         tv = tarval_add(tv, get_mode_one(mode));
2419                         if (tv != tarval_bad) {
2420                                 ir_node *blk = get_irn_n(n, -1);
2421                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2422                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2423                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2424                                 return n;
2425                         }
2426                 }
2427         }
2428         return n;
2429 }  /* transform_node_Sub */
2430
2431 /**
2432  * Several transformation done on n*n=2n bits mul.
2433  * These transformations must be done here because new nodes may be produced.
2434  */
2435 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2436         ir_node *oldn = n;
2437         ir_node *a = get_Mul_left(n);
2438         ir_node *b = get_Mul_right(n);
2439         tarval *ta = value_of(a);
2440         tarval *tb = value_of(b);
2441         ir_mode *smode = get_irn_mode(a);
2442
2443         if (ta == get_mode_one(smode)) {
2444                 /* (L)1 * (L)b = (L)b */
2445                 ir_node *blk = get_irn_n(n, -1);
2446                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2447                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2448                 return n;
2449         }
2450         else if (ta == get_mode_minus_one(smode)) {
2451                 /* (L)-1 * (L)b = (L)b */
2452                 ir_node *blk = get_irn_n(n, -1);
2453                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2454                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2455                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2456                 return n;
2457         }
2458         if (tb == get_mode_one(smode)) {
2459                 /* (L)a * (L)1 = (L)a */
2460                 ir_node *blk = get_irn_n(a, -1);
2461                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2462                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2463                 return n;
2464         }
2465         else if (tb == get_mode_minus_one(smode)) {
2466                 /* (L)a * (L)-1 = (L)-a */
2467                 ir_node *blk = get_irn_n(n, -1);
2468                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2469                 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2470                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2471                 return n;
2472         }
2473         return n;
2474 }
2475
2476 /**
2477  * Transform Mul(a,-1) into -a.
2478  * Do constant evaluation of Phi nodes.
2479  * Do architecture dependent optimizations on Mul nodes
2480  */
2481 static ir_node *transform_node_Mul(ir_node *n) {
2482         ir_node *c, *oldn = n;
2483         ir_mode *mode = get_irn_mode(n);
2484         ir_node *a = get_Mul_left(n);
2485         ir_node *b = get_Mul_right(n);
2486
2487         if (is_Bad(a) || is_Bad(b))
2488                 return n;
2489
2490         if (mode != get_irn_mode(a))
2491                 return transform_node_Mul2n(n, mode);
2492
2493         HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2494
2495         if (mode_is_signed(mode)) {
2496                 ir_node *r = NULL;
2497
2498                 if (value_of(a) == get_mode_minus_one(mode))
2499                         r = b;
2500                 else if (value_of(b) == get_mode_minus_one(mode))
2501                         r = a;
2502                 if (r) {
2503                         n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2504                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2505                         return n;
2506                 }
2507         }
2508         if (is_Minus(a)) {
2509                 if (is_Const(b)) { /* (-a) * const -> a * -const */
2510                         ir_node *cnst = const_negate(b);
2511                         if (cnst != NULL) {
2512                                 dbg_info *dbgi  = get_irn_dbg_info(n);
2513                                 ir_node  *block = get_nodes_block(n);
2514                                 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2515                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2516                                 return n;
2517                         }
2518                 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2519                         dbg_info *dbgi  = get_irn_dbg_info(n);
2520                         ir_node  *block = get_nodes_block(n);
2521                         n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2522                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2523                         return n;
2524                 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2525                         ir_node  *sub_l = get_Sub_left(b);
2526                         ir_node  *sub_r = get_Sub_right(b);
2527                         dbg_info *dbgi  = get_irn_dbg_info(n);
2528                         ir_graph *irg   = current_ir_graph;
2529                         ir_node  *block = get_nodes_block(n);
2530                         ir_node  *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2531                         n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2532                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2533                         return n;
2534                 }
2535         } else if (is_Minus(b)) {
2536                 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2537                         ir_node  *sub_l = get_Sub_left(a);
2538                         ir_node  *sub_r = get_Sub_right(a);
2539                         dbg_info *dbgi  = get_irn_dbg_info(n);
2540                         ir_graph *irg   = current_ir_graph;
2541                         ir_node  *block = get_nodes_block(n);
2542                         ir_node  *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2543                         n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2544                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2545                         return n;
2546                 }
2547         }
2548         if (get_mode_arithmetic(mode) == irma_ieee754) {
2549                 if (is_Const(a)) {
2550                         tarval *tv = get_Const_tarval(a);
2551                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2552                                 /* 2.0 * b = b + b */
2553                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2554                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2555                                 return n;
2556                         }
2557                 }
2558                 else if (is_Const(b)) {
2559                         tarval *tv = get_Const_tarval(b);
2560                         if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2561                                 /* a * 2.0 = a + a */
2562                                 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2563                                 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2564                                 return n;
2565                         }
2566                 }
2567         }
2568         return arch_dep_replace_mul_with_shifts(n);
2569 }  /* transform_node_Mul */
2570
2571 /**
2572  * Transform a Div Node.
2573  */
2574 static ir_node *transform_node_Div(ir_node *n) {
2575         ir_mode *mode = get_Div_resmode(n);
2576         ir_node *a = get_Div_left(n);
2577         ir_node *b = get_Div_right(n);
2578         ir_node *value;
2579         tarval  *tv;
2580
2581         if (is_Const(b) && is_const_Phi(a)) {
2582                 /* check for Div(Phi, Const) */
2583                 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2584                 if (value) {
2585                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2586                         goto make_tuple;
2587                 }
2588         }
2589         else if (is_Const(a) && is_const_Phi(b)) {
2590                 /* check for Div(Const, Phi) */
2591                 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2592                 if (value) {
2593                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2594                         goto make_tuple;
2595                 }
2596         }
2597         else if (is_const_Phi(a) && is_const_Phi(b)) {
2598                 /* check for Div(Phi, Phi) */
2599                 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2600                 if (value) {
2601                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2602                         goto make_tuple;
2603                 }
2604         }
2605
2606         value = n;
2607         tv = value_of(n);
2608         if (tv != tarval_bad) {
2609                 value = new_Const(get_tarval_mode(tv), tv);
2610
2611                 DBG_OPT_CSTEVAL(n, value);
2612                 goto make_tuple;
2613         } else {
2614                 ir_node *a = get_Div_left(n);
2615                 ir_node *b = get_Div_right(n);
2616                 ir_node *dummy;
2617
2618                 if (a == b && value_not_zero(a, &dummy)) {
2619                         /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2620                         value = new_Const(mode, get_mode_one(mode));
2621                         DBG_OPT_CSTEVAL(n, value);
2622                         goto make_tuple;
2623                 } else {
2624                         if (mode_is_signed(mode) && is_Const(b)) {
2625                                 tarval *tv = get_Const_tarval(b);
2626
2627                                 if (tv == get_mode_minus_one(mode)) {
2628                                         /* a / -1 */
2629                                         value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2630                                         DBG_OPT_CSTEVAL(n, value);
2631                                         goto make_tuple;
2632                                 }
2633                         }
2634                         /* Try architecture dependent optimization */
2635                         value = arch_dep_replace_div_by_const(n);
2636                 }
2637         }
2638
2639         if (value != n) {
2640                 ir_node *mem, *blk;
2641
2642 make_tuple:
2643                 /* Turn Div into a tuple (mem, jmp, bad, value) */
2644                 mem = get_Div_mem(n);
2645                 blk = get_irn_n(n, -1);
2646
2647                 /* skip a potential Pin */
2648                 if (is_Pin(mem))
2649                         mem = get_Pin_op(mem);
2650                 turn_into_tuple(n, pn_Div_max);
2651                 set_Tuple_pred(n, pn_Div_M,         mem);
2652                 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2653                 set_Tuple_pred(n, pn_Div_X_except,  new_Bad());
2654                 set_Tuple_pred(n, pn_Div_res,       value);
2655         }
2656         return n;
2657 }  /* transform_node_Div */
2658
2659 /**
2660  * Transform a Mod node.
2661  */
2662 static ir_node *transform_node_Mod(ir_node *n) {
2663         ir_mode *mode = get_Mod_resmode(n);
2664         ir_node *a = get_Mod_left(n);
2665         ir_node *b = get_Mod_right(n);
2666         ir_node *value;
2667         tarval  *tv;
2668
2669         if (is_Const(b) && is_const_Phi(a)) {
2670                 /* check for Div(Phi, Const) */
2671                 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2672                 if (value) {
2673                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2674                         goto make_tuple;
2675                 }
2676         }
2677         else if (is_Const(a) && is_const_Phi(b)) {
2678                 /* check for Div(Const, Phi) */
2679                 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2680                 if (value) {
2681                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2682                         goto make_tuple;
2683                 }
2684         }
2685         else if (is_const_Phi(a) && is_const_Phi(b)) {
2686                 /* check for Div(Phi, Phi) */
2687                 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2688                 if (value) {
2689                         DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2690                         goto make_tuple;
2691                 }
2692         }
2693
2694         value = n;
2695         tv = value_of(n);
2696         if (tv != tarval_bad) {
2697                 value = new_Const(get_tarval_mode(tv), tv);
2698
2699                 DBG_OPT_CSTEVAL(n, value);
2700                 goto make_tuple;
2701         } else {
2702                 ir_node *a = get_Mod_left(n);
2703                 ir_node *b = get_Mod_right(n);
2704                 ir_node *dummy;
2705
2706                 if (a == b && value_not_zero(a, &dummy)) {
2707                         /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2708                         value = new_Const(mode, get_mode_null(mode));
2709                         DBG_OPT_CSTEVAL(n, value);
2710                         goto make_tuple;
2711                 } else {
2712                         if (mode_is_signed(mode) && is_Const(b)) {
2713                                 tarval *tv = get_Const_tarval(b);
2714
2715                                 if (tv == get_mode_minus_one(mode)) {
2716                                         /* a % -1 = 0 */
2717                                         value = new_Const(mode, get_mode_null(mode));
2718                                         DBG_OPT_CSTEVAL(n, value);
2719                                         goto make_tuple;
2720                                 }
2721                         }
2722                         /* Try architecture dependent optimization */
2723                         value = arch_dep_replace_mod_by_const(n);
2724                 }
2725         }
2726
2727         if (value != n) {
2728                 ir_node *mem, *blk;
2729
2730 make_tuple:
2731                 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2732                 mem = get_Mod_mem(n);
2733                 blk = get_irn_n(n, -1);
2734
2735                 /* skip a potential Pin */
2736                 if (is_Pin(mem))
2737                         mem = get_Pin_op(mem);
2738                 turn_into_tuple(n, pn_Mod_max);
2739                 set_Tuple_pred(n, pn_Mod_M,         mem);
2740                 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2741                 set_Tuple_pred(n, pn_Mod_X_except,  new_Bad());
2742                 set_Tuple_pred(n, pn_Mod_res,       value);
2743         }
2744         return n;
2745 }  /* transform_node_Mod */
2746
2747 /**
2748  * Transform a DivMod node.
2749  */
2750 static ir_node *transform_node_DivMod(ir_node *n) {
2751         ir_node *dummy;
2752         ir_node *a = get_DivMod_left(n);
2753         ir_node *b = get_DivMod_right(n);
2754         ir_mode *mode = get_DivMod_resmode(n);
2755         tarval *ta, *tb;
2756         int evaluated = 0;
2757         ir_node *va, *vb;
2758
2759         if (is_Const(b) && is_const_Phi(a)) {
2760                 /* check for Div(Phi, Const) */
2761                 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2762                 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2763                 if (va && vb) {
2764                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2765                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2766                         goto make_tuple;
2767                 }
2768         }
2769         else if (is_Const(a) && is_const_Phi(b)) {
2770                 /* check for Div(Const, Phi) */
2771                 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2772                 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2773                 if (va && vb) {
2774                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2775                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2776                         goto make_tuple;
2777                 }
2778         }
2779         else if (is_const_Phi(a) && is_const_Phi(b)) {
2780                 /* check for Div(Phi, Phi) */
2781                 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2782                 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2783                 if (va && vb) {
2784                         DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2785                         DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2786                         goto make_tuple;
2787                 }
2788         }
2789
2790         ta = value_of(a);
2791         tb = value_of(b);
2792         if (tb != tarval_bad) {
2793                 if (tb == get_mode_one(get_tarval_mode(tb))) {
2794                         va = a;
2795                         vb = new_Const(mode, get_mode_null(mode));
2796                         DBG_OPT_CSTEVAL(n, vb);
2797                         goto make_tuple;
2798                 } else if (ta != tarval_bad) {
2799                         tarval *resa, *resb;
2800                         resa = tarval_div(ta, tb);
2801                         if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2802                                                              Jmp for X result!? */
2803                         resb = tarval_mod(ta, tb);
2804                         if (resb == tarval_bad) return n; /* Causes exception! */
2805                         va = new_Const(mode, resa);
2806                         vb = new_Const(mode, resb);
2807                         DBG_OPT_CSTEVAL(n, va);
2808                         DBG_OPT_CSTEVAL(n, vb);
2809                         goto make_tuple;
2810                 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2811                         va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2812                         vb = new_Const(mode, get_mode_null(mode));
2813                         DBG_OPT_CSTEVAL(n, va);
2814                         DBG_OPT_CSTEVAL(n, vb);
2815                         goto make_tuple;
2816                 } else { /* Try architecture dependent optimization */
2817                         va = a;
2818                         vb = b;
2819                         arch_dep_replace_divmod_by_const(&va, &vb, n);
2820                         evaluated = va != NULL;
2821                 }
2822         } else if (a == b) {
2823                 if (value_not_zero(a, &dummy)) {
2824                         /* a/a && a != 0 */
2825                         va = new_Const(mode, get_mode_one(mode));
2826                         vb = new_Const(mode, get_mode_null(mode));
2827                         DBG_OPT_CSTEVAL(n, va);
2828                         DBG_OPT_CSTEVAL(n, vb);
2829                         goto make_tuple;
2830                 } else {
2831                         /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2832                         return n;
2833                 }
2834         } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2835                 /* 0 / non-Const = 0 */
2836                 vb = va = a;
2837                 goto make_tuple;
2838         }
2839
2840         if (evaluated) { /* replace by tuple */
2841                 ir_node *mem, *blk;
2842
2843 make_tuple:
2844                 mem = get_DivMod_mem(n);
2845                 /* skip a potential Pin */
2846                 if (is_Pin(mem))
2847                         mem = get_Pin_op(mem);
2848
2849                 blk = get_irn_n(n, -1);
2850                 turn_into_tuple(n, pn_DivMod_max);
2851                 set_Tuple_pred(n, pn_DivMod_M,         mem);
2852                 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2853                 set_Tuple_pred(n, pn_DivMod_X_except,  new_Bad());  /* no exception */
2854                 set_Tuple_pred(n, pn_DivMod_res_div,   va);
2855                 set_Tuple_pred(n, pn_DivMod_res_mod,   vb);
2856         }
2857
2858         return n;
2859 }  /* transform_node_DivMod */
2860
2861 /**
2862  * Optimize x / c to x * (1/c)
2863  */
2864 static ir_node *transform_node_Quot(ir_node *n) {
2865         ir_mode *mode = get_Quot_resmode(n);
2866         ir_node *oldn = n;
2867
2868         if (get_mode_arithmetic(mode) == irma_ieee754) {
2869                 ir_node *b = get_Quot_right(n);
2870
2871                 if (is_Const(b)) {
2872                         tarval *tv = get_Const_tarval(b);
2873                         int rem;
2874
2875                         /*
2876                          * Floating point constant folding might be disabled here to
2877                          * prevent rounding.
2878                          * However, as we check for exact result, doing it is safe.
2879                          * Switch it on.
2880                          */
2881                         rem = tarval_enable_fp_ops(1);
2882                         tv = tarval_quo(get_mode_one(mode), tv);
2883                         (void)tarval_enable_fp_ops(rem);
2884
2885                         /* Do the transformation if the result is either exact or we are not
2886                            using strict rules. */
2887                         if (tv != tarval_bad &&
2888                             (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2889                                 ir_node *blk = get_irn_n(n, -1);
2890                                 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2891                                 ir_node *a = get_Quot_left(n);
2892                                 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2893                                 ir_node *mem = get_Quot_mem(n);
2894
2895                                 /* skip a potential Pin */
2896                                 if (is_Pin(mem))
2897                                         mem = get_Pin_op(mem);
2898                                 turn_into_tuple(n, pn_Quot_max);
2899                                 set_Tuple_pred(n, pn_Quot_M, mem);
2900                                 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2901                                 set_Tuple_pred(n, pn_Quot_X_except,  new_r_Bad(current_ir_graph));
2902                                 set_Tuple_pred(n, pn_Quot_res, m);
2903                                 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2904                         }
2905                 }
2906         }
2907         return n;
2908 }  /* transform_node_Quot */
2909
2910 /**
2911  * Optimize Abs(x) into  x if x is Confirmed >= 0
2912  * Optimize Abs(x) into -x if x is Confirmed <= 0
2913  * Optimize Abs(-x) int Abs(x)
2914  */
2915 static ir_node *transform_node_Abs(ir_node *n) {
2916         ir_node *c, *oldn = n;
2917         ir_node *a = get_Abs_op(n);
2918         ir_mode *mode;
2919
2920         HANDLE_UNOP_PHI(tarval_abs, a, c);
2921
2922         switch (classify_value_sign(a)) {
2923         case value_classified_negative:
2924                 mode = get_irn_mode(n);
2925
2926                 /*
2927                  * We can replace the Abs by -x here.
2928                  * We even could add a new Confirm here
2929                  * (if not twos complement)
2930                  *
2931                  * Note that -x would create a new node, so we could
2932                  * not run it in the equivalent_node() context.
2933                  */
2934                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2935                                 get_nodes_block(n), a, mode);
2936
2937                 DBG_OPT_CONFIRM(oldn, n);
2938                 return n;
2939         case value_classified_positive:
2940                 /* n is positive, Abs is not needed */
2941                 n = a;
2942
2943                 DBG_OPT_CONFIRM(oldn, n);
2944                 return n;
2945         default:
2946                 break;
2947         }
2948         if (is_Minus(a)) {
2949                 /* Abs(-x) = Abs(x) */
2950                 mode = get_irn_mode(n);
2951                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
2952                                 get_nodes_block(n), get_Minus_op(a), mode);
2953                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
2954                 return n;
2955         }
2956         return n;
2957 }  /* transform_node_Abs */
2958
2959 /**
2960  * Transform a Cond node.
2961  *
2962  * Replace the Cond by a Jmp if it branches on a constant
2963  * condition.
2964  */
2965 static ir_node *transform_node_Cond(ir_node *n) {
2966
2967         ir_node *jmp;
2968         ir_node *a = get_Cond_selector(n);
2969         tarval *ta = value_of(a);
2970
2971         /* we need block info which is not available in floating irgs */
2972         if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2973                 return n;
2974
2975         if ((ta != tarval_bad) &&
2976             (get_irn_mode(a) == mode_b) &&
2977             (get_opt_unreachable_code())) {
2978                 /* It's a boolean Cond, branching on a boolean constant.
2979                    Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2980                 ir_node *blk = get_nodes_block(n);
2981                 jmp = new_r_Jmp(current_ir_graph, blk);
2982                 turn_into_tuple(n, pn_Cond_max);
2983                 if (ta == tarval_b_true) {
2984                         set_Tuple_pred(n, pn_Cond_false, new_Bad());
2985                         set_Tuple_pred(n, pn_Cond_true, jmp);
2986                 } else {
2987                         set_Tuple_pred(n, pn_Cond_false, jmp);
2988                         set_Tuple_pred(n, pn_Cond_true, new_Bad());
2989                 }
2990                 /* We might generate an endless loop, so keep it alive. */
2991                 add_End_keepalive(get_irg_end(current_ir_graph), blk);
2992         }
2993         return n;
2994 }  /* transform_node_Cond */
2995
2996 /**
2997  * Prototype of a recursive transform function
2998  * for bitwise distributive transformations.
2999  */
3000 typedef ir_node* (*recursive_transform)(ir_node *n);
3001
3002 /**
3003  * makes use of distributive laws for and, or, eor
3004  *     and(a OP c, b OP c) -> and(a, b) OP c
3005  * note, might return a different op than n
3006  */
3007 static ir_node *transform_bitwise_distributive(ir_node *n,
3008                                                recursive_transform trans_func)
3009 {
3010         ir_node *oldn    = n;
3011         ir_node *a       = get_binop_left(n);
3012         ir_node *b       = get_binop_right(n);
3013         ir_op   *op      = get_irn_op(a);
3014         ir_op   *op_root = get_irn_op(n);
3015
3016         if(op != get_irn_op(b))
3017                 return n;
3018
3019         if (op == op_Conv) {
3020                 ir_node *a_op   = get_Conv_op(a);
3021                 ir_node *b_op   = get_Conv_op(b);
3022                 ir_mode *a_mode = get_irn_mode(a_op);
3023                 ir_mode *b_mode = get_irn_mode(b_op);
3024                 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3025                         ir_node *blk = get_irn_n(n, -1);
3026
3027                         n = exact_copy(n);
3028                         set_binop_left(n, a_op);
3029                         set_binop_right(n, b_op);
3030                         set_irn_mode(n, a_mode);
3031                         n = trans_func(n);
3032                         n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3033
3034                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3035                         return n;
3036                 }
3037         }
3038
3039         if (op == op_Eor) {
3040                 /* nothing to gain here */
3041                 return n;
3042         }
3043
3044         if (op == op_Shrs || op == op_Shr || op == op_Shl
3045                         || op == op_And || op == op_Or || op == op_Eor) {
3046                 ir_node *a_left  = get_binop_left(a);
3047                 ir_node *a_right = get_binop_right(a);
3048                 ir_node *b_left  = get_binop_left(b);
3049                 ir_node *b_right = get_binop_right(b);
3050                 ir_node *c       = NULL;
3051                 ir_node *op1     = NULL;
3052                 ir_node *op2     = NULL;
3053
3054                 if (is_op_commutative(op)) {
3055                         if (a_left == b_left) {
3056                                 c   = a_left;
3057                                 op1 = a_right;
3058                                 op2 = b_right;
3059                         } else if(a_left == b_right) {
3060                                 c   = a_left;
3061                                 op1 = a_right;
3062                                 op2 = b_left;
3063                         } else if(a_right == b_left) {
3064                                 c   = a_right;
3065                                 op1 = a_left;
3066                                 op2 = b_right;
3067                         }
3068                 }
3069                 if(a_right == b_right) {
3070                         c   = a_right;
3071                         op1 = a_left;
3072                         op2 = b_left;
3073                 }
3074
3075                 if (c != NULL) {
3076                         /* (a sop c) & (b sop c) => (a & b) sop c */
3077                         ir_node *blk = get_irn_n(n, -1);
3078
3079                         ir_node *new_n = exact_copy(n);
3080                         set_binop_left(new_n, op1);
3081                         set_binop_right(new_n, op2);
3082                         new_n = trans_func(new_n);
3083
3084                         if(op_root == op_Eor && op == op_Or) {
3085                                 dbg_info  *dbgi = get_irn_dbg_info(n);
3086                                 ir_graph  *irg  = current_ir_graph;
3087                                 ir_mode   *mode = get_irn_mode(c);
3088
3089                                 c = new_rd_Not(dbgi, irg, blk, c, mode);
3090                                 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3091                         } else {
3092                                 n = exact_copy(a);
3093                                 set_nodes_block(n, blk);
3094                                 set_binop_left(n, new_n);
3095                                 set_binop_right(n, c);
3096                                 add_identities(current_ir_graph->value_table, n);
3097                         }
3098
3099                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3100                         return n;
3101                 }
3102         }
3103
3104         return n;
3105 }
3106
3107 /**
3108  * Transform an And.
3109  */
3110 static ir_node *transform_node_And(ir_node *n) {
3111         ir_node *c, *oldn = n;
3112         ir_node *a = get_And_left(n);
3113         ir_node *b = get_And_right(n);
3114         ir_mode *mode;
3115
3116         mode = get_irn_mode(n);
3117         HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3118
3119         /* we can evaluate 2 Projs of the same Cmp */
3120         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3121                 ir_node *pred_a = get_Proj_pred(a);
3122                 ir_node *pred_b = get_Proj_pred(b);
3123                 if (pred_a == pred_b) {
3124                         dbg_info *dbgi  = get_irn_dbg_info(n);
3125                         ir_node  *block = get_nodes_block(pred_a);
3126                         pn_Cmp pn_a     = get_Proj_proj(a);
3127                         pn_Cmp pn_b     = get_Proj_proj(b);
3128                         /* yes, we can simply calculate with pncs */
3129                         pn_Cmp new_pnc  = pn_a & pn_b;
3130
3131                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3132                 }
3133         }
3134         if (is_Or(a)) {
3135                 if (is_Not(b)) {
3136                         ir_node *op = get_Not_op(b);
3137                         if (is_And(op)) {
3138                                 ir_node *ba = get_And_left(op);
3139                                 ir_node *bb = get_And_right(op);
3140
3141                                 /* it's enough to test the following cases due to normalization! */
3142                                 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3143                                         /* (a|b) & ~(a&b) = a^b */
3144                                         ir_node *block = get_nodes_block(n);
3145
3146                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3147                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3148                                         return n;
3149                                 }
3150                         }
3151                 }
3152         }
3153         if (is_Or(b)) {
3154                 if (is_Not(a)) {
3155                         ir_node *op = get_Not_op(a);
3156                         if (is_And(op)) {
3157                                 ir_node *aa = get_And_left(op);
3158                                 ir_node *ab = get_And_right(op);
3159
3160                                 /* it's enough to test the following cases due to normalization! */
3161                                 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3162                                         /* (a|b) & ~(a&b) = a^b */
3163                                         ir_node *block = get_nodes_block(n);
3164
3165                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3166                                         DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3167                                         return n;
3168                                 }
3169                         }
3170                 }
3171         }
3172         if (is_Eor(a)) {
3173                 ir_node *al = get_Eor_left(a);
3174                 ir_node *ar = get_Eor_right(a);
3175
3176                 if (al == b) {
3177                         /* (b ^ a) & b -> ~a & b */
3178                         dbg_info *dbg  = get_irn_dbg_info(n);
3179                         ir_node *block = get_nodes_block(n);
3180
3181                         ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3182                         n  = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3183                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3184                         return n;
3185                 }
3186                 if (ar == b) {
3187                         /* (a ^ b) & b -> ~a & b */
3188                         dbg_info *dbg  = get_irn_dbg_info(n);
3189                         ir_node *block = get_nodes_block(n);
3190
3191                         al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3192                         n  = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3193                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3194                         return n;
3195                 }
3196         }
3197         if (is_Eor(b)) {
3198                 ir_node *bl = get_Eor_left(b);
3199                 ir_node *br = get_Eor_right(b);
3200
3201                 if (bl == a) {
3202                         /* a & (a ^ b) -> a & ~b */
3203                         dbg_info *dbg  = get_irn_dbg_info(n);
3204                         ir_node *block = get_nodes_block(n);
3205
3206                         br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3207                         n  = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3208                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3209                         return n;
3210                 }
3211                 if (br == a) {
3212                         /* a & (b ^ a) -> a & ~b */
3213                         dbg_info *dbg  = get_irn_dbg_info(n);
3214                         ir_node *block = get_nodes_block(n);
3215
3216                         bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3217                         n  = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3218                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3219                         return n;
3220                 }
3221         }
3222         if (is_Not(a) && is_Not(b)) {
3223                 /* ~a & ~b = ~(a|b) */
3224                 ir_node *block = get_nodes_block(n);
3225                 ir_mode *mode = get_irn_mode(n);
3226
3227                 a = get_Not_op(a);
3228                 b = get_Not_op(b);
3229                 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3230                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3231                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3232                 return n;
3233         }
3234
3235         n = transform_bitwise_distributive(n, transform_node_And);
3236
3237         return n;
3238 }  /* transform_node_And */
3239
3240 /**
3241  * Transform an Eor.
3242  */
3243 static ir_node *transform_node_Eor(ir_node *n) {
3244         ir_node *c, *oldn = n;
3245         ir_node *a = get_Eor_left(n);
3246         ir_node *b = get_Eor_right(n);
3247         ir_mode *mode = get_irn_mode(n);
3248
3249         HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3250
3251         /* we can evaluate 2 Projs of the same Cmp */
3252         if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3253                 ir_node *pred_a = get_Proj_pred(a);
3254                 ir_node *pred_b = get_Proj_pred(b);
3255                 if(pred_a == pred_b) {
3256                         dbg_info *dbgi  = get_irn_dbg_info(n);
3257                         ir_node  *block = get_nodes_block(pred_a);
3258                         pn_Cmp pn_a     = get_Proj_proj(a);
3259                         pn_Cmp pn_b     = get_Proj_proj(b);
3260                         /* yes, we can simply calculate with pncs */
3261                         pn_Cmp new_pnc  = pn_a ^ pn_b;
3262
3263                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3264                                            new_pnc);
3265                 }
3266         }
3267
3268         if (a == b) {
3269                 /* a ^ a = 0 */
3270                 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3271                                  mode, get_mode_null(mode));
3272                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3273         } else if (mode == mode_b &&
3274                         is_Proj(a) &&
3275                         is_Const(b) && is_Const_one(b) &&
3276                         is_Cmp(get_Proj_pred(a))) {
3277                 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3278                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3279                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3280
3281                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3282         } else if (is_Const(b)) {
3283                 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3284                         ir_node  *cnst   = new_Const(mode, tarval_not(get_Const_tarval(b)));
3285                         ir_node  *not_op = get_Not_op(a);
3286                         dbg_info *dbg    = get_irn_dbg_info(n);
3287                         ir_graph *irg    = current_ir_graph;
3288                         ir_node  *block  = get_nodes_block(n);
3289                         ir_mode  *mode   = get_irn_mode(n);
3290                         n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3291                         return n;
3292                 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3293                         n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3294                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3295                 }
3296         } else {
3297                 n = transform_bitwise_distributive(n, transform_node_Eor);
3298         }
3299
3300         return n;
3301 }  /* transform_node_Eor */
3302
3303 /**
3304  * Transform a Not.
3305  */
3306 static ir_node *transform_node_Not(ir_node *n) {
3307         ir_node *c, *oldn = n;
3308         ir_node *a    = get_Not_op(n);
3309         ir_mode *mode = get_irn_mode(n);
3310
3311         HANDLE_UNOP_PHI(tarval_not,a,c);
3312
3313         /* check for a boolean Not */
3314         if (mode == mode_b &&
3315                         is_Proj(a) &&
3316                         is_Cmp(get_Proj_pred(a))) {
3317                 /* We negate a Cmp. The Cmp has the negated result anyways! */
3318                 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3319                                 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3320                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3321                 return n;
3322         }
3323         if  (is_Eor(a)) {
3324                 ir_node *eor_b = get_Eor_right(a);
3325                 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3326                         ir_node  *cnst  = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3327                         ir_node  *eor_a = get_Eor_left(a);
3328                         dbg_info *dbg   = get_irn_dbg_info(n);
3329                         ir_graph *irg   = current_ir_graph;
3330                         ir_node  *block = get_nodes_block(n);
3331                         ir_mode  *mode  = get_irn_mode(n);
3332                         n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3333                         return n;
3334                 }
3335         }
3336         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3337                 if (is_Minus(a)) { /* ~-x -> x + -1 */
3338                         dbg_info *dbg   = get_irn_dbg_info(n);
3339                         ir_graph *irg   = current_ir_graph;
3340                         ir_node  *block = get_nodes_block(n);
3341                         ir_node  *add_l = get_Minus_op(a);
3342                         ir_node  *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3343                         n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3344                 } else if (is_Add(a)) {
3345                         ir_node *add_r = get_Add_right(a);
3346                         if (is_Const(add_r) && is_Const_all_one(add_r)) {
3347                                 /* ~(x + -1) = -x */
3348                                 ir_node *op = get_Add_left(a);
3349                                 ir_node *blk = get_irn_n(n, -1);
3350                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3351                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3352                         }
3353                 }
3354         }
3355         return n;
3356 }  /* transform_node_Not */
3357
3358 /**
3359  * Transform a Minus.
3360  * Optimize:
3361  *   -(~x) = x + 1
3362  *   -(a-b) = b - a
3363  *   -(a >>u (size-1)) = a >>s (size-1)
3364  *   -(a >>s (size-1)) = a >>u (size-1)
3365  *   -(a * const) -> a * -const
3366  */
3367 static ir_node *transform_node_Minus(ir_node *n) {
3368         ir_node *c, *oldn = n;
3369         ir_node *a = get_Minus_op(n);
3370         ir_mode *mode;
3371
3372         HANDLE_UNOP_PHI(tarval_neg,a,c);
3373
3374         mode = get_irn_mode(a);
3375         if (get_mode_arithmetic(mode) == irma_twos_complement) {
3376                 /* the following rules are only to twos-complement */
3377                 if (is_Not(a)) {
3378                         /* -(~x) = x + 1 */
3379                         ir_node *op   = get_Not_op(a);
3380                         tarval *tv    = get_mode_one(mode);
3381                         ir_node *blk  = get_irn_n(n, -1);
3382                         ir_node *c    = new_r_Const(current_ir_graph, blk, mode, tv);
3383                         n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3384                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3385                         return n;
3386                 }
3387                 if (is_Shr(a)) {
3388                         ir_node *c = get_Shr_right(a);
3389
3390                         if (is_Const(c)) {
3391                                 tarval *tv = get_Const_tarval(c);
3392
3393                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3394                                         /* -(a >>u (size-1)) = a >>s (size-1) */
3395                                         ir_node *v = get_Shr_left(a);
3396
3397                                         n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3398                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3399                                         return n;
3400                                 }
3401                         }
3402                 }
3403                 if (is_Shrs(a)) {
3404                         ir_node *c = get_Shrs_right(a);
3405
3406                         if (is_Const(c)) {
3407                                 tarval *tv = get_Const_tarval(c);
3408
3409                                 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3410                                         /* -(a >>s (size-1)) = a >>u (size-1) */
3411                                         ir_node *v = get_Shrs_left(a);
3412
3413                                         n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3414                                         DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3415                                         return n;
3416                                 }
3417                         }
3418                 }
3419         }
3420         if (is_Sub(a)) {
3421                 /* - (a-b) = b - a */
3422                 ir_node *la  = get_Sub_left(a);
3423                 ir_node *ra  = get_Sub_right(a);
3424                 ir_node *blk = get_irn_n(n, -1);
3425
3426                 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3427                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3428                 return n;
3429         }
3430
3431         if (is_Mul(a)) { /* -(a * const) -> a * -const */
3432                 ir_node *mul_l = get_Mul_left(a);
3433                 ir_node *mul_r = get_Mul_right(a);
3434                 if (is_Const(mul_r)) {
3435                         tarval   *tv    = tarval_neg(get_Const_tarval(mul_r));
3436                         if(tv != tarval_bad) {
3437                                 ir_node  *cnst  = new_Const(mode, tv);
3438                                 dbg_info *dbg   = get_irn_dbg_info(a);
3439                                 ir_graph *irg   = current_ir_graph;
3440                                 ir_node  *block = get_nodes_block(a);
3441                                 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3442                                 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3443                                 return n;
3444                         }
3445                 }
3446         }
3447
3448         return n;
3449 }  /* transform_node_Minus */
3450
3451 /**
3452  * Transform a Cast_type(Const) into a new Const_type
3453  */
3454 static ir_node *transform_node_Cast(ir_node *n) {
3455         ir_node *oldn = n;
3456         ir_node *pred = get_Cast_op(n);
3457         ir_type *tp = get_irn_type(n);
3458
3459         if (is_Const(pred) && get_Const_type(pred) != tp) {
3460                 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3461                         get_Const_tarval(pred), tp);
3462                 DBG_OPT_CSTEVAL(oldn, n);
3463         } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3464                 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3465                         get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3466                 DBG_OPT_CSTEVAL(oldn, n);
3467         }
3468
3469         return n;
3470 }  /* transform_node_Cast */
3471
3472 /**
3473  * Transform a Proj(Div) with a non-zero value.
3474  * Removes the exceptions and routes the memory to the NoMem node.
3475  */
3476 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3477         ir_node *div = get_Proj_pred(proj);
3478         ir_node *b   = get_Div_right(div);
3479         ir_node *confirm, *res, *new_mem;
3480         long proj_nr;
3481
3482         if (value_not_zero(b, &confirm)) {
3483                 /* div(x, y) && y != 0 */
3484                 if (confirm == NULL) {
3485                         /* we are sure we have a Const != 0 */
3486                         new_mem = get_Div_mem(div);
3487                         if (is_Pin(new_mem))
3488                                 new_mem = get_Pin_op(new_mem);
3489                         set_Div_mem(div, new_mem);
3490                         set_irn_pinned(div, op_pin_state_floats);
3491                 }
3492
3493                 proj_nr = get_Proj_proj(proj);
3494                 switch (proj_nr) {
3495                 case pn_Div_X_regular:
3496                         return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3497
3498                 case pn_Div_X_except:
3499                         /* we found an exception handler, remove it */
3500                         DBG_OPT_EXC_REM(proj);
3501                         return new_Bad();
3502
3503                 case pn_Div_M:
3504                         res = get_Div_mem(div);
3505                         new_mem = get_irg_no_mem(current_ir_graph);
3506
3507                         if (confirm) {
3508                                 /* This node can only float up to the Confirm block */
3509                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3510                         }
3511                         set_irn_pinned(div, op_pin_state_floats);
3512                         /* this is a Div without exception, we can remove the memory edge */
3513                         set_Div_mem(div, new_mem);
3514                         return res;
3515                 }
3516         }
3517         return proj;
3518 }  /* transform_node_Proj_Div */
3519
3520 /**
3521  * Transform a Proj(Mod) with a non-zero value.
3522  * Removes the exceptions and routes the memory to the NoMem node.
3523  */
3524 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3525         ir_node *mod = get_Proj_pred(proj);
3526         ir_node *b   = get_Mod_right(mod);
3527         ir_node *confirm, *res, *new_mem;
3528         long proj_nr;
3529
3530         if (value_not_zero(b, &confirm)) {
3531                 /* mod(x, y) && y != 0 */
3532                 proj_nr = get_Proj_proj(proj);
3533
3534                 if (confirm == NULL) {
3535                         /* we are sure we have a Const != 0 */
3536                         new_mem = get_Mod_mem(mod);
3537                         if (is_Pin(new_mem))
3538                                 new_mem = get_Pin_op(new_mem);
3539                         set_Mod_mem(mod, new_mem);
3540                         set_irn_pinned(mod, op_pin_state_floats);
3541                 }
3542
3543                 switch (proj_nr) {
3544
3545                 case pn_Mod_X_regular:
3546                         return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3547
3548                 case pn_Mod_X_except:
3549                         /* we found an exception handler, remove it */
3550                         DBG_OPT_EXC_REM(proj);
3551                         return new_Bad();
3552
3553                 case pn_Mod_M:
3554                         res = get_Mod_mem(mod);
3555                         new_mem = get_irg_no_mem(current_ir_graph);
3556
3557                         if (confirm) {
3558                                 /* This node can only float up to the Confirm block */
3559                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3560                         }
3561                         /* this is a Mod without exception, we can remove the memory edge */
3562                         set_Mod_mem(mod, new_mem);
3563                         return res;
3564                 case pn_Mod_res:
3565                         if (get_Mod_left(mod) == b) {
3566                                 /* a % a = 0 if a != 0 */
3567                                 ir_mode *mode = get_irn_mode(proj);
3568                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3569
3570                                 DBG_OPT_CSTEVAL(mod, res);
3571                                 return res;
3572                         }
3573                 }
3574         }
3575         return proj;
3576 }  /* transform_node_Proj_Mod */
3577
3578 /**
3579  * Transform a Proj(DivMod) with a non-zero value.
3580  * Removes the exceptions and routes the memory to the NoMem node.
3581  */
3582 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3583         ir_node *divmod = get_Proj_pred(proj);
3584         ir_node *b      = get_DivMod_right(divmod);
3585         ir_node *confirm, *res, *new_mem;
3586         long proj_nr;
3587
3588         if (value_not_zero(b, &confirm)) {
3589                 /* DivMod(x, y) && y != 0 */
3590                 proj_nr = get_Proj_proj(proj);
3591
3592                 if (confirm == NULL) {
3593                         /* we are sure we have a Const != 0 */
3594                         new_mem = get_DivMod_mem(divmod);
3595                         if (is_Pin(new_mem))
3596                                 new_mem = get_Pin_op(new_mem);
3597                         set_DivMod_mem(divmod, new_mem);
3598                         set_irn_pinned(divmod, op_pin_state_floats);
3599                 }
3600
3601                 switch (proj_nr) {
3602
3603                 case pn_DivMod_X_regular:
3604                         return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3605
3606                 case pn_DivMod_X_except:
3607                         /* we found an exception handler, remove it */
3608                         DBG_OPT_EXC_REM(proj);
3609                         return new_Bad();
3610
3611                 case pn_DivMod_M:
3612                         res = get_DivMod_mem(divmod);
3613                         new_mem = get_irg_no_mem(current_ir_graph);
3614
3615                         if (confirm) {
3616                                 /* This node can only float up to the Confirm block */
3617                                 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3618                         }
3619                         /* this is a DivMod without exception, we can remove the memory edge */
3620                         set_DivMod_mem(divmod, new_mem);
3621                         return res;
3622
3623                 case pn_DivMod_res_mod:
3624                         if (get_DivMod_left(divmod) == b) {
3625                                 /* a % a = 0 if a != 0 */
3626                                 ir_mode *mode = get_irn_mode(proj);
3627                                 ir_node *res  = new_Const(mode, get_mode_null(mode));
3628
3629                                 DBG_OPT_CSTEVAL(divmod, res);
3630                                 return res;
3631                         }
3632                 }
3633         }
3634         return proj;
3635 }  /* transform_node_Proj_DivMod */
3636
3637 /**
3638  * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3639  */
3640 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3641         if (get_opt_unreachable_code()) {
3642                 ir_node *n = get_Proj_pred(proj);
3643                 ir_node *b = get_Cond_selector(n);
3644
3645                 if (mode_is_int(get_irn_mode(b))) {
3646                         tarval *tb = value_of(b);
3647
3648                         if (tb != tarval_bad) {
3649                                 /* we have a constant switch */
3650                                 long num = get_Proj_proj(proj);
3651
3652                                 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3653                                         if (get_tarval_long(tb) == num) {
3654                                                 /* Do NOT create a jump here, or we will have 2 control flow ops
3655                                                  * in a block. This case is optimized away in optimize_cf(). */
3656                                                 return proj;
3657                                         } else {
3658                                                 /* this case will NEVER be taken, kill it */
3659                                                 return new_Bad();
3660                                         }
3661                                 }
3662                         }
3663                 }
3664         }
3665         return proj;
3666 }  /* transform_node_Proj_Cond */
3667
3668 /**
3669  * Create a 0 constant of given mode.
3670  */
3671 static ir_node *create_zero_const(ir_mode *mode) {
3672         tarval   *tv    = get_mode_null(mode);
3673         ir_node  *cnst  = new_Const(mode, tv);
3674
3675         return cnst;
3676 }
3677
3678 /* the order of the values is important! */
3679 typedef enum const_class {
3680         const_const = 0,
3681         const_like  = 1,
3682         const_other = 2
3683 } const_class;
3684
3685 static const_class classify_const(const ir_node* n)
3686 {
3687         if (is_Const(n))         return const_const;
3688         if (is_irn_constlike(n)) return const_like;
3689         return const_other;
3690 }
3691
3692 /**
3693  * Determines whether r is more constlike or has a larger index (in that order)
3694  * than l.
3695  */
3696 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3697 {
3698         const const_class l_order = classify_const(l);
3699         const const_class r_order = classify_const(r);
3700         return
3701                 l_order > r_order ||
3702                 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3703 }
3704
3705 /**
3706  * Normalizes and optimizes Cmp nodes.
3707  */
3708 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3709         ir_node      *n      = get_Proj_pred(proj);
3710         ir_node      *left   = get_Cmp_left(n);
3711         ir_node      *right  = get_Cmp_right(n);
3712         tarval      *tv      = NULL;
3713         int          changed = 0;
3714         ir_mode     *mode    = NULL;
3715         long         proj_nr = get_Proj_proj(proj);
3716
3717         /* we can evaluate some cases directly */
3718         switch (proj_nr) {
3719         case pn_Cmp_False:
3720                 return new_Const(mode_b, get_tarval_b_false());
3721         case pn_Cmp_True:
3722                 return new_Const(mode_b, get_tarval_b_true());
3723         case pn_Cmp_Leg:
3724                 if (!mode_is_float(get_irn_mode(left)))
3725                         return new_Const(mode_b, get_tarval_b_true());
3726                 break;
3727         default:
3728                 break;
3729         }
3730
3731         /* remove Casts of both sides */
3732         left  = skip_Cast(left);
3733         right = skip_Cast(right);
3734
3735         /* Remove unnecessary conversions */
3736         /* TODO handle constants */
3737         if (is_Conv(left) && is_Conv(right)) {
3738                 ir_mode *mode        = get_irn_mode(left);
3739                 ir_node *op_left     = get_Conv_op(left);
3740                 ir_node *op_right    = get_Conv_op(right);
3741                 ir_mode *mode_left   = get_irn_mode(op_left);
3742                 ir_mode *mode_right  = get_irn_mode(op_right);
3743
3744                 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3745                                 && mode_left != mode_b && mode_right != mode_b) {
3746                         ir_graph *irg   = current_ir_graph;
3747                         ir_node  *block = get_nodes_block(n);
3748
3749                         if (mode_left == mode_right) {
3750                                 left  = op_left;
3751                                 right = op_right;
3752                                 changed |= 1;
3753                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3754                         } else if (smaller_mode(mode_left, mode_right)) {
3755                                 left  = new_r_Conv(irg, block, op_left, mode_right);
3756                                 right = op_right;
3757                                 changed |= 1;
3758                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3759                         } else if (smaller_mode(mode_right, mode_left)) {
3760                                 left  = op_left;
3761                                 right = new_r_Conv(irg, block, op_right, mode_left);
3762                                 changed |= 1;
3763                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3764                         }
3765                 }
3766         }
3767
3768         /* remove operation on both sides if possible */
3769         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3770                 /*
3771                  * The following operations are NOT safe for floating point operations, for instance
3772                  * 1.0 + inf == 2.0 + inf, =/=> x == y
3773                  */
3774                 if (mode_is_int(get_irn_mode(left))) {
3775                         unsigned lop = get_irn_opcode(left);
3776
3777                         if (lop == get_irn_opcode(right)) {
3778                                 ir_node *ll, *lr, *rl, *rr;
3779
3780                                 /* same operation on both sides, try to remove */
3781                                 switch (lop) {
3782                                 case iro_Not:
3783                                 case iro_Minus:
3784                                         /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3785                                         left  = get_unop_op(left);
3786                                         right = get_unop_op(right);
3787                                         changed |= 1;
3788                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3789                                         break;
3790                                 case iro_Add:
3791                                         ll = get_Add_left(left);
3792                                         lr = get_Add_right(left);
3793                                         rl = get_Add_left(right);
3794                                         rr = get_Add_right(right);
3795
3796                                         if (ll == rl) {
3797                                                 /* X + a CMP X + b ==> a CMP b */
3798                                                 left  = lr;
3799                                                 right = rr;
3800                                                 changed |= 1;
3801                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3802                                         } else if (ll == rr) {
3803                                                 /* X + a CMP b + X ==> a CMP b */
3804                                                 left  = lr;
3805                                                 right = rl;
3806                                                 changed |= 1;
3807                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3808                                         } else if (lr == rl) {
3809                                                 /* a + X CMP X + b ==> a CMP b */
3810                                                 left  = ll;
3811                                                 right = rr;
3812                                                 changed |= 1;
3813                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3814                                         } else if (lr == rr) {
3815                                                 /* a + X CMP b + X ==> a CMP b */
3816                                                 left  = ll;
3817                                                 right = rl;
3818                                                 changed |= 1;
3819                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3820                                         }
3821                                         break;
3822                                 case iro_Sub:
3823                                         ll = get_Sub_left(left);
3824                                         lr = get_Sub_right(left);
3825                                         rl = get_Sub_left(right);
3826                                         rr = get_Sub_right(right);
3827
3828                                         if (ll == rl) {
3829                                                 /* X - a CMP X - b ==> a CMP b */
3830                                                 left  = lr;
3831                                                 right = rr;
3832                                                 changed |= 1;
3833                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3834                                         } else if (lr == rr) {
3835                                                 /* a - X CMP b - X ==> a CMP b */
3836                                                 left  = ll;
3837                                                 right = rl;
3838                                                 changed |= 1;
3839                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3840                                         }
3841                                         break;
3842                                 case iro_Rot:
3843                                         if (get_Rot_right(left) == get_Rot_right(right)) {
3844                                                 /* a ROT X CMP b ROT X ==> a CMP b */
3845                                                 left  = get_Rot_left(left);
3846                                                 right = get_Rot_left(right);
3847                                                 changed |= 1;
3848                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3849                                         }
3850                                         break;
3851                                 default:
3852                                         break;
3853                                 }
3854                         }
3855
3856                         /* X+A == A, A+X == A, A-X == A -> X == 0 */
3857                         if (is_Add(left) || is_Sub(left)) {
3858                                 ir_node *ll = get_binop_left(left);
3859                                 ir_node *lr = get_binop_right(left);
3860
3861                                 if (lr == right && is_Add(left)) {
3862                                         ir_node *tmp = ll;
3863                                         ll = lr;
3864                                         lr = tmp;
3865                                 }
3866                                 if (ll == right) {
3867                                         left     = lr;
3868                                         right    = create_zero_const(get_irn_mode(left));
3869                                         changed |= 1;
3870                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3871                                 }
3872                         }
3873                         if (is_Add(right) || is_Sub(right)) {
3874                                 ir_node *rl = get_binop_left(right);
3875                                 ir_node *rr = get_binop_right(right);
3876
3877                                 if (rr == left && is_Add(right)) {
3878                                         ir_node *tmp = rl;
3879                                         rl = rr;
3880                                         rr = tmp;
3881                                 }
3882                                 if (rl == left) {
3883                                         left     = rr;
3884                                         right    = create_zero_const(get_irn_mode(left));
3885                                         changed |= 1;
3886                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3887                                 }
3888                         }
3889                 }  /* mode_is_int(...) */
3890         }  /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3891
3892         /* replace mode_b compares with ands/ors */
3893         if (get_irn_mode(left) == mode_b) {
3894                 ir_graph *irg   = current_ir_graph;
3895                 ir_node  *block = get_nodes_block(n);
3896                 ir_node  *bres;
3897
3898                 switch (proj_nr) {
3899                         case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3900                         case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3901                         case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3902                         case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3903                         case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3904                         case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3905                         default: bres = NULL;
3906                 }
3907                 if (bres) {
3908                         DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3909                         return bres;
3910                 }
3911         }
3912
3913         /*
3914          * First step: normalize the compare op
3915          * by placing the constant on the right side
3916          * or moving the lower address node to the left.
3917          */
3918         if (!operands_are_normalized(left, right)) {
3919                 ir_node *t = left;
3920
3921                 left  = right;
3922                 right = t;
3923
3924                 proj_nr = get_inversed_pnc(proj_nr);
3925                 changed |= 1;
3926         }
3927
3928         /*
3929          * Second step: Try to reduce the magnitude
3930          * of a constant. This may help to generate better code
3931          * later and may help to normalize more compares.
3932          * Of course this is only possible for integer values.
3933          */
3934         if (is_Const(right)) {
3935                 mode = get_irn_mode(right);
3936                 tv = get_Const_tarval(right);
3937
3938                 /* TODO extend to arbitrary constants */
3939                 if (is_Conv(left) && tarval_is_null(tv)) {
3940                         ir_node *op      = get_Conv_op(left);
3941                         ir_mode *op_mode = get_irn_mode(op);
3942
3943                         /*
3944                          * UpConv(x) REL 0  ==> x REL 0
3945                          */
3946                         if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3947                             ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3948                                  mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3949                                 tv   = get_mode_null(op_mode);
3950                                 left = op;
3951                                 mode = op_mode;
3952                                 changed |= 2;
3953                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3954                         }
3955                 }
3956
3957                 if (tv != tarval_bad) {
3958                         /* the following optimization is possible on modes without Overflow
3959                          * on Unary Minus or on == and !=:
3960                          * -a CMP c  ==>  a swap(CMP) -c
3961                          *
3962                          * Beware: for two-complement Overflow may occur, so only == and != can
3963                          * be optimized, see this:
3964                          * -MININT < 0 =/=> MININT > 0 !!!
3965                          */
3966                         if (is_Minus(left) &&
3967                                 (!mode_overflow_on_unary_Minus(mode) ||
3968                                 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3969                                 tv = tarval_neg(tv);
3970
3971                                 if (tv != tarval_bad) {
3972                                         left = get_Minus_op(left);
3973                                         proj_nr = get_inversed_pnc(proj_nr);
3974                                         changed |= 2;
3975                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3976                                 }
3977                         } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3978                                 /* Not(a) ==/!= c  ==>  a ==/!= Not(c) */
3979                                 tv = tarval_not(tv);
3980
3981                                 if (tv != tarval_bad) {
3982                                         left = get_Not_op(left);
3983                                         changed |= 2;
3984                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3985                                 }
3986                         }
3987
3988                         /* for integer modes, we have more */
3989                         if (mode_is_int(mode)) {
3990                                 /* Ne includes Unordered which is not possible on integers.
3991                                  * However, frontends often use this wrong, so fix it here */
3992                                 if (proj_nr & pn_Cmp_Uo) {
3993                                         proj_nr &= ~pn_Cmp_Uo;
3994                                         set_Proj_proj(proj, proj_nr);
3995                                 }
3996
3997                                 /* c > 0 : a < c  ==>  a <= (c-1)    a >= c  ==>  a > (c-1) */
3998                                 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3999                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4000                                         tv = tarval_sub(tv, get_mode_one(mode));
4001
4002                                         if (tv != tarval_bad) {
4003                                                 proj_nr ^= pn_Cmp_Eq;
4004                                                 changed |= 2;
4005                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4006                                         }
4007                                 }
4008                                 /* c < 0 : a > c  ==>  a >= (c+1)    a <= c  ==>  a < (c+1) */
4009                                 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4010                                         tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4011                                         tv = tarval_add(tv, get_mode_one(mode));
4012
4013                                         if (tv != tarval_bad) {
4014                                                 proj_nr ^= pn_Cmp_Eq;
4015                                                 changed |= 2;
4016                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4017                                         }
4018                                 }
4019
4020                                 /* the following reassociations work only for == and != */
4021                                 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4022
4023 #if 0 /* Might be not that good in general */
4024                                         /* a-b == 0  ==>  a == b,  a-b != 0  ==>  a != b */
4025                                         if (tarval_is_null(tv) && is_Sub(left)) {
4026                                                 right = get_Sub_right(left);
4027                                                 left  = get_Sub_left(left);
4028
4029                                                 tv = value_of(right);
4030                                                 changed = 1;
4031                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4032                                         }
4033 #endif
4034
4035                                         if (tv != tarval_bad) {
4036                                                 /* a-c1 == c2  ==>  a == c2+c1,  a-c1 != c2  ==>  a != c2+c1 */
4037                                                 if (is_Sub(left)) {
4038                                                         ir_node *c1 = get_Sub_right(left);
4039                                                         tarval *tv2 = value_of(c1);
4040
4041                                                         if (tv2 != tarval_bad) {
4042                                                                 tv2 = tarval_add(tv, value_of(c1));
4043
4044                                                                 if (tv2 != tarval_bad) {
4045                                                                         left    = get_Sub_left(left);
4046                                                                         tv      = tv2;
4047                                                                         changed |= 2;
4048                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4049                                                                 }
4050                                                         }
4051                                                 }
4052                                                 /* a+c1 == c2  ==>  a == c2-c1,  a+c1 != c2  ==>  a != c2-c1 */
4053                                                 else if (is_Add(left)) {
4054                                                         ir_node *a_l = get_Add_left(left);
4055                                                         ir_node *a_r = get_Add_right(left);
4056                                                         ir_node *a;
4057                                                         tarval *tv2;
4058
4059                                                         if (is_Const(a_l)) {
4060                                                                 a = a_r;
4061                                                                 tv2 = value_of(a_l);
4062                                                         } else {
4063                                                                 a = a_l;
4064                                                                 tv2 = value_of(a_r);
4065                                                         }
4066
4067                                                         if (tv2 != tarval_bad) {
4068                                                                 tv2 = tarval_sub(tv, tv2);
4069
4070                                                                 if (tv2 != tarval_bad) {
4071                                                                         left    = a;
4072                                                                         tv      = tv2;
4073                                                                         changed |= 2;
4074                                                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4075                                                                 }
4076                                                         }
4077                                                 }
4078                                                 /* -a == c ==> a == -c, -a != c ==> a != -c */
4079                                                 else if (is_Minus(left)) {
4080                                                         tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4081
4082                                                         if (tv2 != tarval_bad) {
4083                                                                 left    = get_Minus_op(left);
4084                                                                 tv      = tv2;
4085                                                                 changed |= 2;
4086                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4087                                                         }
4088                                                 }
4089                                         }
4090                                 } /* == or != */
4091                                 /* the following reassociations work only for <= */
4092                                 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4093                                         if (tv != tarval_bad) {
4094                                                 /* c >= 0 : Abs(a) <= c  ==>  (unsigned)(a + c) <= 2*c */
4095                                                 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4096                                                 }
4097                                         }
4098                                 }
4099                         } /* mode_is_int */
4100
4101                         if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4102                                 switch (get_irn_opcode(left)) {
4103                                         ir_node *c1;
4104
4105                                 case iro_And:
4106                                         c1 = get_And_right(left);
4107                                         if (is_Const(c1)) {
4108                                                 /*
4109                                                  * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4110                                                  * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4111                                                  */
4112                                                 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4113                                                 if (mask != tv) {
4114                                                         /* TODO: move to constant evaluation */
4115                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4116                                                         c1 = new_Const(mode_b, tv);
4117                                                         DBG_OPT_CSTEVAL(proj, c1);
4118                                                         return c1;
4119                                                 }
4120
4121                                                 if (tarval_is_single_bit(tv)) {
4122                                                         /*
4123                                                          * optimization for AND:
4124                                                          * Optimize:
4125                                                          *   And(x, C) == C  ==>  And(x, C) != 0
4126                                                          *   And(x, C) != C  ==>  And(X, C) == 0
4127                                                          *
4128                                                          * if C is a single Bit constant.
4129                                                          */
4130
4131                                                         /* check for Constant's match. We have check hare the tarvals,
4132                                                            because our const might be changed */
4133                                                         if (get_Const_tarval(c1) == tv) {
4134                                                                 /* fine: do the transformation */
4135                                                                 tv = get_mode_null(get_tarval_mode(tv));
4136                                                                 proj_nr ^= pn_Cmp_Leg;
4137                                                                 changed |= 2;
4138                                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4139                                                         }
4140                                                 }
4141                                         }
4142                                         break;
4143                                 case iro_Or:
4144                                         c1 = get_Or_right(left);
4145                                         if (is_Const(c1) && tarval_is_null(tv)) {
4146                                                 /*
4147                                                  * Or(x, C) == 0  && C != 0 ==> FALSE
4148                                                  * Or(x, C) != 0  && C != 0 ==> TRUE
4149                                                  */
4150                                                 if (! tarval_is_null(get_Const_tarval(c1))) {
4151                                                         /* TODO: move to constant evaluation */
4152                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4153                                                         c1 = new_Const(mode_b, tv);
4154                                                         DBG_OPT_CSTEVAL(proj, c1);
4155                                                         return c1;
4156                                                 }
4157                                         }
4158                                         break;
4159                                 case iro_Shl:
4160                                         /*
4161                                          * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1  if  c & (-1 << c1) == c
4162                                          *                             FALSE                       else
4163                                          * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1  if  c & (-1 << c1) == c
4164                                          *                             TRUE                        else
4165                                          */
4166                                         c1 = get_Shl_right(left);
4167                                         if (is_Const(c1)) {
4168                                                 tarval  *tv1    = get_Const_tarval(c1);
4169                                                 ir_mode *mode   = get_irn_mode(left);
4170                                                 tarval  *minus1 = get_mode_all_one(mode);
4171                                                 tarval  *amask  = tarval_shr(minus1, tv1);
4172                                                 tarval  *cmask  = tarval_shl(minus1, tv1);
4173                                                 ir_node *sl, *blk;
4174
4175                                                 if (tarval_and(tv, cmask) != tv) {
4176                                                         /* condition not met */
4177                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4178                                                         c1 = new_Const(mode_b, tv);
4179                                                         DBG_OPT_CSTEVAL(proj, c1);
4180                                                         return c1;
4181                                                 }
4182                                                 sl   = get_Shl_left(left);
4183                                                 blk  = get_nodes_block(n);
4184                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4185                                                 tv   = tarval_shr(tv, tv1);
4186                                                 changed |= 2;
4187                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4188                                         }
4189                                         break;
4190                                 case iro_Shr:
4191                                         /*
4192                                          * optimize x >>u c1 == c into x & (-1 << c1) == c << c1  if  c & (-1 >>u c1) == c
4193                                          *                             FALSE                       else
4194                                          * optimize x >>u c1 != c into x & (-1 << c1) != c << c1  if  c & (-1 >>u c1) == c
4195                                          *                             TRUE                        else
4196                                          */
4197                                         c1 = get_Shr_right(left);
4198                                         if (is_Const(c1)) {
4199                                                 tarval  *tv1    = get_Const_tarval(c1);
4200                                                 ir_mode *mode   = get_irn_mode(left);
4201                                                 tarval  *minus1 = get_mode_all_one(mode);
4202                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4203                                                 tarval  *cmask  = tarval_shr(minus1, tv1);
4204                                                 ir_node *sl, *blk;
4205
4206                                                 if (tarval_and(tv, cmask) != tv) {
4207                                                         /* condition not met */
4208                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4209                                                         c1 = new_Const(mode_b, tv);
4210                                                         DBG_OPT_CSTEVAL(proj, c1);
4211                                                         return c1;
4212                                                 }
4213                                                 sl   = get_Shr_left(left);
4214                                                 blk  = get_nodes_block(n);
4215                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4216                                                 tv   = tarval_shl(tv, tv1);
4217                                                 changed |= 2;
4218                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4219                                         }
4220                                         break;
4221                                 case iro_Shrs:
4222                                         /*
4223                                          * optimize x >>s c1 == c into x & (-1 << c1) == c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4224                                          *                             FALSE                       else
4225                                          * optimize x >>s c1 != c into x & (-1 << c1) != c << c1  if  (c >>s (BITS - c1)) \in {0,-1}
4226                                          *                             TRUE                        else
4227                                          */
4228                                         c1 = get_Shrs_right(left);
4229                                         if (is_Const(c1)) {
4230                                                 tarval  *tv1    = get_Const_tarval(c1);
4231                                                 ir_mode *mode   = get_irn_mode(left);
4232                                                 tarval  *minus1 = get_mode_all_one(mode);
4233                                                 tarval  *amask  = tarval_shl(minus1, tv1);
4234                                                 tarval  *cond   = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4235                                                 ir_node *sl, *blk;
4236
4237                                                 cond = tarval_sub(cond, tv1);
4238                                                 cond = tarval_shrs(tv, cond);
4239
4240                                                 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4241                                                         /* condition not met */
4242                                                         tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4243                                                         c1 = new_Const(mode_b, tv);
4244                                                         DBG_OPT_CSTEVAL(proj, c1);
4245                                                         return c1;
4246                                                 }
4247                                                 sl   = get_Shrs_left(left);
4248                                                 blk  = get_nodes_block(n);
4249                                                 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4250                                                 tv   = tarval_shl(tv, tv1);
4251                                                 changed |= 2;
4252                                                 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4253                                         }
4254                                         break;
4255                                 }  /* switch */
4256                         }
4257                 } /* tarval != bad */
4258         }
4259
4260         if (changed & 2)      /* need a new Const */
4261                 right = new_Const(mode, tv);
4262
4263         if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4264                 ir_node *op = get_Proj_pred(left);
4265
4266                 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4267                     (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4268                         ir_node *c = get_binop_right(op);
4269
4270                         if (is_Const(c)) {
4271                                 tarval *tv = get_Const_tarval(c);
4272
4273                                 if (tarval_is_single_bit(tv)) {
4274                                         /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4275                                         ir_node *v    = get_binop_left(op);
4276                                         ir_node *blk  = get_irn_n(op, -1);
4277                                         ir_mode *mode = get_irn_mode(v);
4278
4279                                         tv = tarval_sub(tv, get_mode_one(mode));
4280                                         left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4281                                         changed |= 1;
4282                                         DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4283                                 }
4284                         }
4285                 }
4286         }
4287
4288         if (changed) {
4289                 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4290
4291                 /* create a new compare */
4292                 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4293                 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4294         }
4295
4296         return proj;
4297 }  /* transform_node_Proj_Cmp */
4298
4299 /**
4300  * Does all optimizations on nodes that must be done on it's Proj's
4301  * because of creating new nodes.
4302  */
4303 static ir_node *transform_node_Proj(ir_node *proj) {
4304         ir_node *n = get_Proj_pred(proj);
4305
4306         switch (get_irn_opcode(n)) {
4307         case iro_Div:
4308                 return transform_node_Proj_Div(proj);
4309
4310         case iro_Mod:
4311                 return transform_node_Proj_Mod(proj);
4312
4313         case iro_DivMod:
4314                 return transform_node_Proj_DivMod(proj);
4315
4316         case iro_Cond:
4317                 return transform_node_Proj_Cond(proj);
4318
4319         case iro_Cmp:
4320                 return transform_node_Proj_Cmp(proj);
4321
4322         case iro_Tuple:
4323                 /* should not happen, but if it does will be optimized away */
4324                 return equivalent_node_Proj(proj);
4325
4326         default:
4327                 /* do nothing */
4328                 return proj;
4329         }
4330 }  /* transform_node_Proj */
4331
4332 /**
4333  * Move Confirms down through Phi nodes.
4334  */
4335 static ir_node *transform_node_Phi(ir_node *phi) {
4336         int i, n;
4337         ir_mode *mode = get_irn_mode(phi);
4338
4339         if (mode_is_reference(mode)) {
4340                 n = get_irn_arity(phi);
4341
4342                 /* Beware of Phi0 */
4343                 if (n > 0) {
4344                         ir_node *pred = get_irn_n(phi, 0);
4345                         ir_node *bound, *new_Phi, *block, **in;
4346                         pn_Cmp  pnc;
4347
4348                         if (! is_Confirm(pred))
4349                                 return phi;
4350
4351                         bound = get_Confirm_bound(pred);
4352                         pnc   = get_Confirm_cmp(pred);
4353
4354                         NEW_ARR_A(ir_node *, in, n);
4355                         in[0] = get_Confirm_value(pred);
4356
4357                         for (i = 1; i < n; ++i) {
4358                                 pred = get_irn_n(phi, i);
4359
4360                                 if (! is_Confirm(pred) ||
4361                                         get_Confirm_bound(pred) != bound ||
4362                                         get_Confirm_cmp(pred) != pnc)
4363                                         return phi;
4364                                 in[i] = get_Confirm_value(pred);
4365                         }
4366                         /* move the Confirm nodes "behind" the Phi */
4367                         block = get_irn_n(phi, -1);
4368                         new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4369                         return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4370                 }
4371         }
4372         return phi;
4373 }  /* transform_node_Phi */
4374
4375 /**
4376  * Returns the operands of a commutative bin-op, if one operand is
4377  * a const, it is returned as the second one.
4378  */
4379 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4380         ir_node *op_a = get_binop_left(binop);
4381         ir_node *op_b = get_binop_right(binop);
4382
4383         assert(is_op_commutative(get_irn_op(binop)));
4384
4385         if (is_Const(op_a)) {
4386                 *a = op_b;
4387                 *c = op_a;
4388         } else {
4389                 *a = op_a;
4390                 *c = op_b;
4391         }
4392 }  /* get_comm_Binop_Ops */
4393
4394 /**
4395  * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4396  * Such pattern may arise in bitfield stores.
4397  *
4398  * value  c4                  value      c4 & c2
4399  *    AND     c3                    AND           c1 | c3
4400  *        OR     c2      ===>               OR
4401  *           AND    c1
4402  *               OR
4403  *
4404  *
4405  * value  c2                 value  c1
4406  *     AND   c1    ===>           OR     if (c1 | c2) == 0x111..11
4407  *        OR
4408  */
4409 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4410         ir_node *and, *c1;
4411         ir_node *or_l, *c2;
4412         ir_node *and_l, *c3;
4413         ir_node *value, *c4;
4414         ir_node *new_and, *new_const, *block;
4415         ir_mode *mode = get_irn_mode(or);
4416
4417         tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4418
4419         while (1) {
4420                 get_comm_Binop_Ops(or, &and, &c1);
4421                 if (!is_Const(c1) || !is_And(and))
4422                         return or;
4423
4424                 get_comm_Binop_Ops(and, &or_l, &c2);
4425                 if (!is_Const(c2))
4426                         return or;
4427
4428                 tv1 = get_Const_tarval(c1);
4429                 tv2 = get_Const_tarval(c2);
4430
4431                 tv = tarval_or(tv1, tv2);
4432                 if (tarval_is_all_one(tv)) {
4433                         /* the AND does NOT clear a bit with isn't set by the OR */
4434                         set_Or_left(or, or_l);
4435                         set_Or_right(or, c1);
4436
4437                         /* check for more */
4438                         continue;
4439                 }
4440
4441                 if (!is_Or(or_l))
4442                         return or;
4443
4444                 get_comm_Binop_Ops(or_l, &and_l, &c3);
4445                 if (!is_Const(c3) || !is_And(and_l))
4446                         return or;
4447
4448                 get_comm_Binop_Ops(and_l, &value, &c4);
4449                 if (!is_Const(c4))
4450                         return or;
4451
4452                 /* ok, found the pattern, check for conditions */
4453                 assert(mode == get_irn_mode(and));
4454                 assert(mode == get_irn_mode(or_l));
4455                 assert(mode == get_irn_mode(and_l));
4456
4457                 tv3 = get_Const_tarval(c3);
4458                 tv4 = get_Const_tarval(c4);
4459
4460                 tv = tarval_or(tv4, tv2);
4461                 if (!tarval_is_all_one(tv)) {
4462                         /* have at least one 0 at the same bit position */
4463                         return or;
4464                 }
4465
4466                 n_tv4 = tarval_not(tv4);
4467                 if (tv3 != tarval_and(tv3, n_tv4)) {
4468                         /* bit in the or_mask is outside the and_mask */
4469                         return or;
4470                 }
4471
4472                 n_tv2 = tarval_not(tv2);
4473                 if (tv1 != tarval_and(tv1, n_tv2)) {
4474                         /* bit in the or_mask is outside the and_mask */
4475                         return or;
4476                 }
4477
4478                 /* ok, all conditions met */
4479                 block = get_irn_n(or, -1);
4480
4481                 new_and = new_r_And(current_ir_graph, block,
4482                         value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4483
4484                 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4485
4486                 set_Or_left(or, new_and);
4487                 set_Or_right(or, new_const);
4488
4489                 /* check for more */
4490         }
4491 }  /* transform_node_Or_bf_store */
4492
4493 /**
4494  * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
4495  */
4496 static ir_node *transform_node_Or_Rot(ir_node *or) {
4497         ir_mode *mode = get_irn_mode(or);
4498         ir_node *shl, *shr, *block;
4499         ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
4500         tarval *tv1, *tv2;
4501
4502         if (! mode_is_int(mode))
4503                 return or;
4504
4505         shl = get_binop_left(or);
4506         shr = get_binop_right(or);
4507
4508         if (is_Shr(shl)) {
4509                 if (!is_Shl(shr))
4510                         return or;
4511
4512                 irn = shl;
4513                 shl = shr;
4514                 shr = irn;
4515         } else if (!is_Shl(shl)) {
4516                 return or;
4517         } else if (!is_Shr(shr)) {
4518                 return or;
4519         }
4520         x = get_Shl_left(shl);
4521         if (x != get_Shr_left(shr))
4522                 return or;
4523
4524         c1 = get_Shl_right(shl);
4525         c2 = get_Shr_right(shr);
4526         if (is_Const(c1) && is_Const(c2)) {
4527                 tv1 = get_Const_tarval(c1);
4528                 if (! tarval_is_long(tv1))
4529                         return or;
4530
4531                 tv2 = get_Const_tarval(c2);
4532                 if (! tarval_is_long(tv2))
4533                         return or;
4534
4535                 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4536                                 != (int) get_mode_size_bits(mode))
4537                         return or;
4538
4539                 /* yet, condition met */
4540                 block = get_irn_n(or, -1);
4541
4542                 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
4543
4544                 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
4545                 return n;
4546         } else if (is_Sub(c1)) {
4547                 v   = c2;
4548                 sub = c1;
4549
4550                 if (get_Sub_right(sub) != v)
4551                         return or;
4552
4553                 c1 = get_Sub_left(sub);
4554                 if (!is_Const(c1))
4555                         return or;
4556
4557                 tv1 = get_Const_tarval(c1);
4558                 if (! tarval_is_long(tv1))
4559                         return or;
4560
4561                 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4562                         return or;
4563
4564                 /* yet, condition met */
4565                 block = get_nodes_block(or);
4566
4567                 /* a Rot right is not supported, so use a rot left */
4568                 n =  new_r_Rot(current_ir_graph, block, x, sub, mode);
4569
4570                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4571                 return n;
4572         } else if (is_Sub(c2)) {
4573                 v   = c1;
4574                 sub = c2;
4575
4576                 c1 = get_Sub_left(sub);
4577                 if (!is_Const(c1))
4578                         return or;
4579
4580                 tv1 = get_Const_tarval(c1);
4581                 if (! tarval_is_long(tv1))
4582                         return or;
4583
4584                 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4585                         return or;
4586
4587                 /* yet, condition met */
4588                 block = get_irn_n(or, -1);
4589
4590                 /* a Rot Left */
4591                 n = new_r_Rot(current_ir_graph, block, x, v, mode);
4592
4593                 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
4594                 return n;
4595         }
4596
4597         return or;
4598 }  /* transform_node_Or_Rot */
4599
4600 /**
4601  * Transform an Or.
4602  */
4603 static ir_node *transform_node_Or(ir_node *n) {
4604         ir_node *c, *oldn = n;
4605         ir_node *a = get_Or_left(n);
4606         ir_node *b = get_Or_right(n);
4607         ir_mode *mode;
4608
4609         if (is_Not(a) && is_Not(b)) {
4610                 /* ~a | ~b = ~(a&b) */
4611                 ir_node *block = get_nodes_block(n);
4612
4613                 mode = get_irn_mode(n);
4614                 a = get_Not_op(a);
4615                 b = get_Not_op(b);
4616                 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4617                 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4618                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4619                 return n;
4620         }
4621
4622         /* we can evaluate 2 Projs of the same Cmp */
4623         if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4624                 ir_node *pred_a = get_Proj_pred(a);
4625                 ir_node *pred_b = get_Proj_pred(b);
4626                 if (pred_a == pred_b) {
4627                         dbg_info *dbgi  = get_irn_dbg_info(n);
4628                         ir_node  *block = get_nodes_block(pred_a);
4629                         pn_Cmp pn_a     = get_Proj_proj(a);
4630                         pn_Cmp pn_b     = get_Proj_proj(b);
4631                         /* yes, we can simply calculate with pncs */
4632                         pn_Cmp new_pnc  = pn_a | pn_b;
4633
4634                         return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4635                                            new_pnc);
4636                 }
4637         }
4638
4639         mode = get_irn_mode(n);
4640         HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4641
4642         n = transform_node_Or_bf_store(n);
4643         n = transform_node_Or_Rot(n);
4644         if (n != oldn)
4645                 return n;
4646
4647         n = transform_bitwise_distributive(n, transform_node_Or);
4648
4649         return n;
4650 }  /* transform_node_Or */
4651
4652
4653 /* forward */
4654 static ir_node *transform_node(ir_node *n);
4655
4656 /**
4657  * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rot.
4658  *
4659  * Should be moved to reassociation?
4660  */
4661 static ir_node *transform_node_shift(ir_node *n) {
4662         ir_node *left, *right;
4663         tarval *tv1, *tv2, *res;
4664         ir_mode *mode;
4665         int modulo_shf, flag;
4666
4667         left = get_binop_left(n);
4668
4669         /* different operations */
4670         if (get_irn_op(left) != get_irn_op(n))
4671                 return n;
4672
4673         right = get_binop_right(n);
4674         tv1 = value_of(right);
4675         if (tv1 == tarval_bad)
4676                 return n;
4677
4678         tv2 = value_of(get_binop_right(left));
4679         if (tv2 == tarval_bad)
4680                 return n;
4681
4682         res = tarval_add(tv1, tv2);
4683
4684         /* beware: a simple replacement works only, if res < modulo shift */
4685         mode = get_irn_mode(n);
4686
4687         flag = 0;
4688
4689         modulo_shf = get_mode_modulo_shift(mode);
4690         if (modulo_shf > 0) {
4691                 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4692
4693                 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4694                         flag = 1;
4695         } else
4696                 flag = 1;
4697
4698         if (flag) {
4699                 /* ok, we can replace it */
4700                 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4701
4702                 in[0] = get_binop_left(left);
4703                 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4704
4705                 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4706
4707                 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4708
4709                 return transform_node(irn);
4710         }
4711         return n;
4712 }  /* transform_node_shift */
4713
4714 /**
4715  * Transform a Shr.
4716  */
4717 static ir_node *transform_node_Shr(ir_node *n) {
4718         ir_node *c, *oldn = n;
4719         ir_node *a    = get_Shr_left(n);
4720         ir_node *b    = get_Shr_right(n);
4721         ir_mode *mode = get_irn_mode(n);
4722
4723         HANDLE_BINOP_PHI(tarval_shr, a, b, c, mode);
4724         return transform_node_shift(n);
4725 }  /* transform_node_Shr */
4726
4727 /**
4728  * Transform a Shrs.
4729  */
4730 static ir_node *transform_node_Shrs(ir_node *n) {
4731         ir_node *c, *oldn = n;
4732         ir_node *a    = get_Shrs_left(n);
4733         ir_node *b    = get_Shrs_right(n);
4734         ir_mode *mode = get_irn_mode(n);
4735
4736         HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4737         return transform_node_shift(n);
4738 }  /* transform_node_Shrs */
4739
4740 /**
4741  * Transform a Shl.
4742  */
4743 static ir_node *transform_node_Shl(ir_node *n) {
4744         ir_node *c, *oldn = n;
4745         ir_node *a    = get_Shl_left(n);
4746         ir_node *b    = get_Shl_right(n);
4747         ir_mode *mode = get_irn_mode(n);
4748
4749         HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4750         return transform_node_shift(n);
4751 }  /* transform_node_Shl */
4752
4753 /**
4754  * Transform a Rot.
4755  */
4756 static ir_node *transform_node_Rot(ir_node *n) {
4757         ir_node *c, *oldn = n;
4758         ir_node *a    = get_Rot_left(n);
4759         ir_node *b    = get_Rot_right(n);
4760         ir_mode *mode = get_irn_mode(n);
4761
4762         HANDLE_BINOP_PHI(tarval_rot, a, b, c, mode);
4763         return transform_node_shift(n);
4764 }  /* transform_node_Rot */
4765
4766 /**
4767  * Transform a Conv.
4768  */
4769 static ir_node *transform_node_Conv(ir_node *n) {
4770         ir_node *c, *oldn = n;
4771         ir_node *a = get_Conv_op(n);
4772
4773         if (is_const_Phi(a)) {
4774                 c = apply_conv_on_phi(a, get_irn_mode(n));
4775                 if (c) {
4776                         DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
4777                         return c;
4778                 }
4779         }
4780
4781         if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
4782                 ir_mode *mode = get_irn_mode(n);
4783                 return new_r_Unknown(current_ir_graph, mode);
4784         }
4785
4786         return n;
4787 }  /* transform_node_Conv */
4788
4789 /**
4790  * Remove dead blocks and nodes in dead blocks
4791  * in keep alive list.  We do not generate a new End node.
4792  */
4793 static ir_node *transform_node_End(ir_node *n) {
4794         int i, j, n_keepalives = get_End_n_keepalives(n);
4795         ir_node **in;
4796
4797         NEW_ARR_A(ir_node *, in, n_keepalives);
4798
4799         for (i = j = 0; i < n_keepalives; ++i) {
4800                 ir_node *ka = get_End_keepalive(n, i);
4801                 if (is_Block(ka)) {
4802                         if (! is_Block_dead(ka)) {
4803                                 in[j++] = ka;
4804                         }
4805                         continue;
4806                 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4807                         continue;
4808                 }
4809                 /* FIXME: beabi need to keep a Proj(M) */
4810                 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4811                         in[j++] = ka;
4812         }
4813         if (j != n_keepalives)
4814                 set_End_keepalives(n, j, in);
4815         return n;
4816 }  /* transform_node_End */
4817
4818 /** returns 1 if a == -b */
4819 static int is_negated_value(ir_node *a, ir_node *b) {
4820         if (is_Minus(a) && get_Minus_op(a) == b)
4821                 return 1;
4822         if (is_Minus(b) && get_Minus_op(b) == a)
4823                 return 1;
4824         if (is_Sub(a) && is_Sub(b)) {
4825                 ir_node *a_left  = get_Sub_left(a);
4826                 ir_node *a_right = get_Sub_right(a);
4827                 ir_node *b_left  = get_Sub_left(b);
4828                 ir_node *b_right = get_Sub_right(b);
4829
4830                 if (a_left == b_right && a_right == b_left)
4831                         return 1;
4832         }
4833
4834         return 0;
4835 }
4836
4837 /**
4838  * Optimize a Mux into some simpler cases.
4839  */
4840 static ir_node *transform_node_Mux(ir_node *n) {
4841         ir_node *oldn = n, *sel = get_Mux_sel(n);
4842         ir_mode *mode = get_irn_mode(n);
4843         ir_node  *t   = get_Mux_true(n);
4844         ir_node  *f   = get_Mux_false(n);
4845         ir_graph *irg = current_ir_graph;
4846         ir_node  *conds[1], *vals[2];
4847
4848         /* first normalization step: move a possible zero to the false case */
4849         if (is_Proj(sel)) {
4850                 ir_node *cmp = get_Proj_pred(sel);
4851
4852                 if (is_Cmp(cmp)) {
4853                         if (is_Const(t) && is_Const_null(t)) {
4854                                 /* Psi(x, 0, y) => Psi(x, y, 0) */
4855                                 pn_Cmp pnc = get_Proj_proj(sel);
4856                                 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
4857                                         get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
4858                                 conds[0] = sel;
4859                                 vals[0]  = f;
4860                                 vals[1]  = t;
4861                                 n        = new_rd_Psi(get_irn_dbg_info(n), irg, get_nodes_block(n), 1, conds, vals, mode);
4862                                 t = vals[0];
4863                                 f = vals[1];
4864                         }
4865                 }
4866         }
4867
4868         /* note: after normalization, false can only happen on default */
4869         if (mode == mode_b) {
4870                 dbg_info *dbg   = get_irn_dbg_info(n);
4871                 ir_node  *block = get_nodes_block(n);
4872                 ir_graph *irg   = current_ir_graph;
4873
4874                 if (is_Const(t)) {
4875                         tarval *tv_t = get_Const_tarval(t);
4876                         if (tv_t == tarval_b_true) {
4877                                 if (is_Const(f)) {
4878                                         /* Muxb(sel, true, false) = sel */
4879                                         assert(get_Const_tarval(f) == tarval_b_false);
4880                                         DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
4881                                         return sel;
4882                                 } else {
4883                                         /* Muxb(sel, true, x) = Or(sel, x) */
4884                                         n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
4885                                         DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
4886                                         return n;
4887                                 }
4888                         }
4889                 } else if (is_Const(f)) {
4890                         tarval *tv_f = get_Const_tarval(f);
4891                         if (tv_f == tarval_b_true) {
4892                                 /* Muxb(sel, x, true) = Or(Not(sel), x) */
4893                                 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4894                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
4895                                 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4896                                 return n;
4897                         } else {
4898                                 /* Muxb(sel, x, false) = And(sel, x) */
4899                                 assert(tv_f == tarval_b_false);
4900                                 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
4901                                 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
4902                                 return n;
4903                         }
4904                 }
4905         }
4906
4907         /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
4908         if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
4909                 tarval *a = get_Const_tarval(t);
4910                 tarval *b = get_Const_tarval(f);
4911                 tarval *null = get_tarval_null(mode);
4912                 tarval *diff, *min;
4913
4914                 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
4915                         diff = tarval_sub(a, b);
4916                         min  = b;
4917                 } else {
4918                         diff = tarval_sub(b, a);
4919                         min  = a;
4920                 }
4921
4922                 if (diff == get_tarval_one(mode) && min != null) {
4923                         dbg_info *dbg   = get_irn_dbg_info(n);
4924                         ir_node  *block = get_nodes_block(n);
4925                         ir_graph *irg   = current_ir_graph;
4926
4927
4928                         conds[0] = sel;
4929                         vals[0] = new_Const(mode, tarval_sub(a, min));
4930                         vals[1] = new_Const(mode, tarval_sub(b, min));
4931                         n = new_rd_Psi(dbg, irg, block, 1, conds, vals, mode);
4932                         n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
4933                         return n;
4934                 }
4935         }
4936
4937         if (is_Proj(sel)) {
4938                 ir_node *cmp = get_Proj_pred(sel);
4939                 long     pn  = get_Proj_proj(sel);
4940
4941                 /*
4942                  * Note: normalization puts the constant on the right side,
4943                  * so we check only one case.
4944                  *
4945                  * Note further that these optimization work even for floating point
4946                  * with NaN's because -NaN == NaN.
4947                  * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
4948                  * transformations.
4949                  */
4950                 if (is_Cmp(cmp)) {
4951                         ir_node *cmp_r = get_Cmp_right(cmp);
4952                         if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
4953                                 ir_node *block = get_nodes_block(n);
4954                                 ir_node *cmp_l = get_Cmp_left(cmp);
4955
4956                                 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
4957                                         /* f = -t */
4958
4959                                         if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4960                                                 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4961                                         {
4962                                                 /* Psi(a >/>= 0, a, -a) = Psi(a </<= 0, -a, a) ==> Abs(a) */
4963                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4964                                                                                  cmp_l, mode);
4965                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4966                                                 return n;
4967                                         } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4968                                                 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4969                                         {
4970                                                 /* Psi(a </<= 0, a, -a) = Psi(a >/>= 0, -a, a) ==> -Abs(a) */
4971                                                 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4972                                                                                  cmp_l, mode);
4973                                                 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4974                                                                                                                  block, n, mode);
4975                                                 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4976                                                 return n;
4977                                         }
4978                                 }
4979
4980                                 if (mode_is_int(mode)) {
4981                                         /* integer only */
4982                                         if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
4983                                                 /* Psi((a & b) != 0, c, 0) */
4984                                                 ir_node *and_r = get_And_right(cmp_l);
4985                                                 ir_node *and_l;
4986
4987                                                 if (and_r == t && f == cmp_r) {
4988                                                         if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
4989                                                                 if (pn == pn_Cmp_Lg) {
4990                                                                         /* Psi((a & 2^C) != 0, 2^C, 0) */
4991                                                                         n = cmp_l;
4992                                                                 } else {
4993                                                                         /* Psi((a & 2^C) == 0, 2^C, 0) */
4994                                                                         n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
4995                                                                                 block, cmp_l, t, mode);
4996                                                                 }
4997                                                                 return n;
4998                                                         }
4999                                                 }
5000                                                 if (is_Shl(and_r)) {
5001                                                         ir_node *shl_l = get_Shl_left(and_r);
5002                                                         if (is_Const(shl_l) && is_Const_one(shl_l)) {
5003                                                                 if (and_r == t && f == cmp_r) {
5004                                                                         if (pn == pn_Cmp_Lg) {
5005                                                                                 /* (a & (1 << n)) != 0, (1 << n), 0) */
5006                                                                                 n = cmp_l;
5007                                                                         } else {
5008                                                                                 /* (a & (1 << n)) == 0, (1 << n), 0) */
5009                                                                                 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5010                                                                                         block, cmp_l, t, mode);
5011                                                                         }
5012                                                                         return n;
5013                                                                 }
5014                                                         }
5015                                                 }
5016                                                 and_l = get_And_left(cmp_l);
5017                                                 if (is_Shl(and_l)) {
5018                                                         ir_node *shl_l = get_Shl_left(and_l);
5019                                                         if (is_Const(shl_l) && is_Const_one(shl_l)) {
5020                                                                 if (and_l == t && f == cmp_r) {
5021                                                                         if (pn == pn_Cmp_Lg) {
5022                                                                                 /* ((1 << n) & a) != 0, (1 << n), 0) */
5023                                                                                 n = cmp_l;
5024                                                                         } else {
5025                                                                                 /* ((1 << n) & a) == 0, (1 << n), 0) */
5026                                                                                 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5027                                                                                         block, cmp_l, t, mode);
5028                                                                         }
5029                                                                         return n;
5030                                                                 }
5031                                                         }
5032                                                 }
5033                                         }
5034                                 }
5035                         }
5036                 }
5037         }
5038         return arch_transform_node_Mux(n);
5039 }  /* transform_node_Mux */
5040
5041 /**
5042  * Optimize a Psi into some simpler cases.
5043  */
5044 static ir_node *transform_node_Psi(ir_node *n) {
5045         if (is_Mux(n))
5046                 return transform_node_Mux(n);
5047
5048         return n;
5049 }  /* transform_node_Psi */
5050
5051 /**
5052  * optimize sync nodes that have other syncs as input we simply add the inputs
5053  * of the other sync to our own inputs
5054  */
5055 static ir_node *transform_node_Sync(ir_node *n) {
5056         int arity = get_Sync_n_preds(n);
5057         int i;
5058
5059         for (i = 0; i < arity;) {
5060                 ir_node *pred = get_Sync_pred(n, i);
5061                 int      pred_arity;
5062                 int      j;
5063
5064                 if (!is_Sync(pred)) {
5065                         ++i;
5066                         continue;
5067                 }
5068
5069                 del_Sync_n(n, i);
5070                 --arity;
5071
5072                 pred_arity = get_Sync_n_preds(pred);
5073                 for (j = 0; j < pred_arity; ++j) {
5074                         ir_node *pred_pred = get_Sync_pred(pred, j);
5075                         int      k;
5076
5077                         for (k = 0;; ++k) {
5078                                 if (k >= arity) {
5079                                         add_irn_n(n, pred_pred);
5080                                         ++arity;
5081                                         break;
5082                                 }
5083                                 if (get_Sync_pred(n, k) == pred_pred) break;
5084                         }
5085                 }
5086         }
5087
5088         /* rehash the sync node */
5089         add_identities(current_ir_graph->value_table, n);
5090
5091         return n;
5092 }
5093
5094 /**
5095  * Tries several [inplace] [optimizing] transformations and returns an
5096  * equivalent node.  The difference to equivalent_node() is that these
5097  * transformations _do_ generate new nodes, and thus the old node must
5098  * not be freed even if the equivalent node isn't the old one.
5099  */
5100 static ir_node *transform_node(ir_node *n) {
5101         ir_node *oldn;
5102
5103         /*
5104          * Transform_node is the only "optimizing transformation" that might
5105          * return a node with a different opcode. We iterate HERE until fixpoint
5106          * to get the final result.
5107          */
5108         do {
5109                 oldn = n;
5110                 if (n->op->ops.transform_node)
5111                         n = n->op->ops.transform_node(n);
5112         } while (oldn != n);
5113
5114         return n;
5115 }  /* transform_node */
5116
5117 /**
5118  * Sets the default transform node operation for an ir_op_ops.
5119  *
5120  * @param code   the opcode for the default operation
5121  * @param ops    the operations initialized
5122  *
5123  * @return
5124  *    The operations.
5125  */
5126 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5127 {
5128 #define CASE(a)                                 \
5129         case iro_##a:                                 \
5130                 ops->transform_node  = transform_node_##a;  \
5131                 break
5132
5133         switch (code) {
5134         CASE(Add);
5135         CASE(Sub);
5136         CASE(Mul);
5137         CASE(Div);
5138         CASE(Mod);
5139         CASE(DivMod);
5140         CASE(Quot);
5141         CASE(Abs);
5142         CASE(Cond);
5143         CASE(And);
5144         CASE(Or);
5145         CASE(Eor);
5146         CASE(Minus);
5147         CASE(Not);
5148         CASE(Cast);
5149         CASE(Proj);
5150         CASE(Phi);
5151         CASE(Sel);
5152         CASE(Shr);
5153         CASE(Shrs);
5154         CASE(Shl);
5155         CASE(Rot);
5156         CASE(Conv);
5157         CASE(End);
5158         CASE(Mux);
5159         CASE(Psi);
5160         CASE(Sync);
5161         default:
5162           /* leave NULL */;
5163         }
5164
5165         return ops;
5166 #undef CASE
5167 }  /* firm_set_default_transform_node */
5168
5169
5170 /* **************** Common Subexpression Elimination **************** */
5171
5172 /** The size of the hash table used, should estimate the number of nodes
5173     in a graph. */
5174 #define N_IR_NODES 512
5175
5176 /** Compares the attributes of two Const nodes. */
5177 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5178         return (get_Const_tarval(a) != get_Const_tarval(b))
5179             || (get_Const_type(a) != get_Const_type(b));
5180 }  /* node_cmp_attr_Const */
5181
5182 /** Compares the attributes of two Proj nodes. */
5183 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5184         return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5185 }  /* node_cmp_attr_Proj */
5186
5187 /** Compares the attributes of two Filter nodes. */
5188 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5189         return get_Filter_proj(a) != get_Filter_proj(b);
5190 }  /* node_cmp_attr_Filter */
5191
5192 /** Compares the attributes of two Alloc nodes. */
5193 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5194         const alloc_attr *pa = get_irn_alloc_attr(a);
5195         const alloc_attr *pb = get_irn_alloc_attr(b);
5196         return (pa->where != pb->where) || (pa->type != pb->type);
5197 }  /* node_cmp_attr_Alloc */
5198
5199 /** Compares the attributes of two Free nodes. */
5200 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5201         const free_attr *pa = get_irn_free_attr(a);
5202         const free_attr *pb = get_irn_free_attr(b);
5203         return (pa->where != pb->where) || (pa->type != pb->type);
5204 }  /* node_cmp_attr_Free */
5205
5206 /** Compares the attributes of two SymConst nodes. */
5207 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5208         const symconst_attr *pa = get_irn_symconst_attr(a);
5209         const symconst_attr *pb = get_irn_symconst_attr(b);
5210         return (pa->kind       != pb->kind)
5211             || (pa->sym.type_p != pb->sym.type_p)
5212             || (pa->tp         != pb->tp);
5213 }  /* node_cmp_attr_SymConst */
5214
5215 /** Compares the attributes of two Call nodes. */
5216 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5217         return get_irn_call_attr(a) != get_irn_call_attr(b);
5218 }  /* node_cmp_attr_Call */
5219
5220 /** Compares the attributes of two Sel nodes. */
5221 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5222         const ir_entity *a_ent = get_Sel_entity(a);
5223         const ir_entity *b_ent = get_Sel_entity(b);
5224         return
5225                 (a_ent->kind    != b_ent->kind)    ||
5226                 (a_ent->name    != b_ent->name)    ||
5227                 (a_ent->owner   != b_ent->owner)   ||
5228                 (a_ent->ld_name != b_ent->ld_name) ||
5229                 (a_ent->type    != b_ent->type);
5230 }  /* node_cmp_attr_Sel */
5231
5232 /** Compares the attributes of two Phi nodes. */
5233 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5234         /* we can only enter this function if both nodes have the same number of inputs,
5235            hence it is enough to check if one of them is a Phi0 */
5236         if (is_Phi0(a)) {
5237                 /* check the Phi0 pos attribute */
5238                 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5239         }
5240         return 0;
5241 }  /* node_cmp_attr_Phi */
5242
5243 /** Compares the attributes of two Conv nodes. */
5244 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5245         return get_Conv_strict(a) != get_Conv_strict(b);
5246 }  /* node_cmp_attr_Conv */
5247
5248 /** Compares the attributes of two Cast nodes. */
5249 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5250         return get_Cast_type(a) != get_Cast_type(b);
5251 }  /* node_cmp_attr_Cast */
5252
5253 /** Compares the attributes of two Load nodes. */
5254 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5255         if (get_Load_volatility(a) == volatility_is_volatile ||
5256             get_Load_volatility(b) == volatility_is_volatile)
5257                 /* NEVER do CSE on volatile Loads */
5258                 return 1;
5259         /* do not CSE Loads with different alignment. Be conservative. */
5260         if (get_Load_align(a) != get_Load_align(b))
5261                 return 1;
5262
5263         return get_Load_mode(a) != get_Load_mode(b);
5264 }  /* node_cmp_attr_Load */
5265
5266 /** Compares the attributes of two Store nodes. */
5267 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5268         /* do not CSE Stores with different alignment. Be conservative. */
5269         if (get_Store_align(a) != get_Store_align(b))
5270                 return 1;
5271
5272         /* NEVER do CSE on volatile Stores */
5273         return (get_Store_volatility(a) == volatility_is_volatile ||
5274                 get_Store_volatility(b) == volatility_is_volatile);
5275 }  /* node_cmp_attr_Store */
5276
5277 /** Compares two exception attributes */
5278 static int node_cmp_exception(ir_node *a, ir_node *b) {
5279         const except_attr *ea = get_irn_except_attr(a);
5280         const except_attr *eb = get_irn_except_attr(b);
5281
5282         return ea->pin_state != eb->pin_state;
5283 }
5284
5285 #define node_cmp_attr_Bound  node_cmp_exception
5286
5287 /** Compares the attributes of two Div nodes. */
5288 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5289         const divmod_attr *ma = get_irn_divmod_attr(a);
5290         const divmod_attr *mb = get_irn_divmod_attr(b);
5291         return ma->exc.pin_state != mb->exc.pin_state ||
5292                    ma->res_mode      != mb->res_mode ||
5293                    ma->no_remainder  != mb->no_remainder;
5294 }  /* node_cmp_attr_Div */
5295
5296 /** Compares the attributes of two DivMod nodes. */
5297 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5298         const divmod_attr *ma = get_irn_divmod_attr(a);
5299         const divmod_attr *mb = get_irn_divmod_attr(b);
5300         return ma->exc.pin_state != mb->exc.pin_state ||
5301                    ma->res_mode      != mb->res_mode;
5302 }  /* node_cmp_attr_DivMod */
5303
5304 /** Compares the attributes of two Mod nodes. */
5305 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5306         const divmod_attr *ma = get_irn_divmod_attr(a);
5307         const divmod_attr *mb = get_irn_divmod_attr(b);
5308         return ma->exc.pin_state != mb->exc.pin_state ||
5309                    ma->res_mode      != mb->res_mode;
5310 }  /* node_cmp_attr_Mod */
5311
5312 /** Compares the attributes of two Quot nodes. */
5313 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5314         const divmod_attr *ma = get_irn_divmod_attr(a);
5315         const divmod_attr *mb = get_irn_divmod_attr(b);
5316         return ma->exc.pin_state != mb->exc.pin_state ||
5317                    ma->res_mode      != mb->res_mode;
5318 }  /* node_cmp_attr_Quot */
5319
5320 /** Compares the attributes of two Confirm nodes. */
5321 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5322         return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5323 }  /* node_cmp_attr_Confirm */
5324
5325 /** Compares the attributes of two ASM nodes. */
5326 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5327         int i, n;
5328         const ir_asm_constraint *ca;
5329         const ir_asm_constraint *cb;
5330         ident **cla, **clb;
5331
5332         if (get_ASM_text(a) != get_ASM_text(b))
5333                 return 1;
5334
5335         /* Should we really check the constraints here? Should be better, but is strange. */
5336         n = get_ASM_n_input_constraints(a);
5337         if (n != get_ASM_n_input_constraints(b))
5338                 return 0;
5339
5340         ca = get_ASM_input_constraints(a);
5341         cb = get_ASM_input_constraints(b);
5342         for (i = 0; i < n; ++i) {
5343                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5344                         return 1;
5345         }
5346
5347         n = get_ASM_n_output_constraints(a);
5348         if (n != get_ASM_n_output_constraints(b))
5349                 return 0;
5350
5351         ca = get_ASM_output_constraints(a);
5352         cb = get_ASM_output_constraints(b);
5353         for (i = 0; i < n; ++i) {
5354                 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5355                         return 1;
5356         }
5357
5358         n = get_ASM_n_clobbers(a);
5359         if (n != get_ASM_n_clobbers(b))
5360                 return 0;
5361
5362         cla = get_ASM_clobbers(a);
5363         clb = get_ASM_clobbers(b);
5364         for (i = 0; i < n; ++i) {
5365                 if (cla[i] != clb[i])
5366                         return 1;
5367         }
5368         return 0;
5369 }  /* node_cmp_attr_ASM */
5370
5371 /**
5372  * Set the default node attribute compare operation for an ir_op_ops.
5373  *
5374  * @param code   the opcode for the default operation
5375  * @param ops    the operations initialized
5376  *
5377  * @return
5378  *    The operations.
5379  */
5380 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5381 {
5382 #define CASE(a)                              \
5383         case iro_##a:                              \
5384                 ops->node_cmp_attr  = node_cmp_attr_##a; \
5385                 break
5386
5387         switch (code) {
5388         CASE(Const);
5389         CASE(Proj);
5390         CASE(Filter);
5391         CASE(Alloc);
5392         CASE(Free);
5393         CASE(SymConst);
5394         CASE(Call);
5395         CASE(Sel);
5396         CASE(Phi);
5397         CASE(Conv);
5398         CASE(Cast);
5399         CASE(Load);
5400         CASE(Store);
5401         CASE(Confirm);
5402         CASE(ASM);
5403         CASE(Div);
5404         CASE(DivMod);
5405         CASE(Mod);
5406         CASE(Quot);
5407         CASE(Bound);
5408         /* FIXME CopyB */
5409         default:
5410           /* leave NULL */;
5411         }
5412
5413         return ops;
5414 #undef CASE
5415 }  /* firm_set_default_node_cmp_attr */
5416
5417 /*
5418  * Compare function for two nodes in the value table. Gets two
5419  * nodes as parameters.  Returns 0 if the nodes are a Common Sub Expression.
5420  */
5421 int identities_cmp(const void *elt, const void *key) {
5422         ir_node *a = (ir_node *)elt;
5423         ir_node *b = (ir_node *)key;
5424         int i, irn_arity_a;
5425
5426         if (a == b) return 0;
5427
5428         if ((get_irn_op(a) != get_irn_op(b)) ||
5429             (get_irn_mode(a) != get_irn_mode(b))) return 1;
5430
5431         /* compare if a's in and b's in are of equal length */
5432         irn_arity_a = get_irn_intra_arity(a);
5433         if (irn_arity_a != get_irn_intra_arity(b))
5434                 return 1;
5435
5436         if (get_irn_pinned(a) == op_pin_state_pinned) {
5437                 /* for pinned nodes, the block inputs must be equal */
5438                 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5439                         return 1;
5440         } else if (! get_opt_global_cse()) {
5441                 /* for block-local CSE both nodes must be in the same MacroBlock */
5442                 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5443                         return 1;
5444         }
5445
5446         /* compare a->in[0..ins] with b->in[0..ins] */
5447         for (i = 0; i < irn_arity_a; i++)
5448                 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5449                         return 1;
5450
5451         /*
5452          * here, we already now that the nodes are identical except their
5453          * attributes
5454          */
5455         if (a->op->ops.node_cmp_attr)
5456                 return a->op->ops.node_cmp_attr(a, b);
5457
5458         return 0;
5459 }  /* identities_cmp */
5460
5461 /*
5462  * Calculate a hash value of a node.
5463  */
5464 unsigned ir_node_hash(ir_node *node) {
5465         unsigned h;
5466         int i, irn_arity;
5467
5468         if (node->op == op_Const) {
5469                 /* special value for const, as they only differ in their tarval. */
5470                 h = HASH_PTR(node->attr.con.tv);
5471                 h = 9*h + HASH_PTR(get_irn_mode(node));
5472         } else if (node->op == op_SymConst) {
5473                 /* special value for const, as they only differ in their symbol. */
5474                 h = HASH_PTR(node->attr.symc.sym.type_p);
5475                 h = 9*h + HASH_PTR(get_irn_mode(node));
5476         } else {
5477
5478                 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5479                 h = irn_arity = get_irn_intra_arity(node);
5480
5481                 /* consider all in nodes... except the block if not a control flow. */
5482                 for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  i++) {
5483                         h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5484                 }
5485
5486                 /* ...mode,... */
5487                 h = 9*h + HASH_PTR(get_irn_mode(node));
5488                 /* ...and code */
5489                 h = 9*h + HASH_PTR(get_irn_op(node));
5490         }
5491
5492         return h;
5493 }  /* ir_node_hash */
5494
5495 pset *new_identities(void) {
5496         return new_pset(identities_cmp, N_IR_NODES);
5497 }  /* new_identities */
5498
5499 void del_identities(pset *value_table) {
5500         del_pset(value_table);
5501 }  /* del_identities */
5502
5503 /**
5504  * Normalize a node by putting constants (and operands with larger
5505  * node index) on the right (operator side).
5506  *
5507  * @param n   The node to normalize
5508  */
5509 static void normalize_node(ir_node *n) {
5510         if (is_op_commutative(get_irn_op(n))) {
5511                 ir_node *l = get_binop_left(n);
5512                 ir_node *r = get_binop_right(n);
5513
5514                 /* For commutative operators perform  a OP b == b OP a but keep
5515                  * constants on the RIGHT side. This helps greatly in some
5516                  * optimizations.  Moreover we use the idx number to make the form
5517                  * deterministic. */
5518                 if (!operands_are_normalized(l, r)) {
5519                         set_binop_left(n, r);
5520                         set_binop_right(n, l);
5521                 }
5522         }
5523 }  /* normalize_node */
5524
5525 /**
5526  * Update the nodes after a match in the value table. If both nodes have
5527  * the same MacroBlock but different Blocks, we must ensure that the node
5528  * with the dominating Block (the node that is near to the MacroBlock header
5529  * is stored in the table.
5530  * Because a MacroBlock has only one "non-exception" flow, we don't need
5531  * dominance info here: We known, that one block must dominate the other and
5532  * following the only block input will allow to find it.
5533  */
5534 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5535         ir_node *known_blk, *new_block, *block, *mbh;
5536
5537         if (get_opt_global_cse()) {
5538                 /* Block inputs are meaning less */
5539                 return;
5540         }
5541         known_blk = get_irn_n(known_irn, -1);
5542         new_block = get_irn_n(new_ir_node, -1);
5543         if (known_blk == new_block) {
5544                 /* already in the same block */
5545                 return;
5546         }
5547         /*
5548          * We expect the typical case when we built the graph. In that case, the
5549          * known_irn is already the upper one, so checking this should be faster.
5550          */
5551         block = new_block;
5552         mbh   = get_Block_MacroBlock(new_block);
5553         for (;;) {
5554                 if (block == known_blk) {
5555                         /* ok, we have found it: known_block dominates new_block as expected */
5556                         return;
5557                 }
5558                 if (block == mbh) {
5559                         /*
5560                          * We have reached the MacroBlock header NOT founding
5561                          * the known_block. new_block must dominate known_block.
5562                          * Update known_irn.
5563                          */
5564                         set_irn_n(known_irn, -1, new_block);
5565                         return;
5566                 }
5567                 assert(get_Block_n_cfgpreds(block) == 1);
5568                 block = get_Block_cfgpred_block(block, 0);
5569         }
5570 }  /* update_value_table */
5571
5572 /**
5573  * Return the canonical node computing the same value as n.
5574  *
5575  * @param value_table  The value table
5576  * @param n            The node to lookup
5577  *
5578  * Looks up the node in a hash table.
5579  *
5580  * For Const nodes this is performed in the constructor, too.  Const
5581  * nodes are extremely time critical because of their frequent use in
5582  * constant string arrays.
5583  */
5584 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
5585         ir_node *o = NULL;
5586
5587         if (!value_table) return n;
5588
5589         normalize_node(n);
5590
5591         o = pset_find(value_table, n, ir_node_hash(n));
5592         if (o == NULL)
5593                 return n;
5594
5595         update_known_irn(o, n);
5596         DBG_OPT_CSE(n, o);
5597
5598         return o;
5599 }  /* identify */
5600
5601 /**
5602  * During construction we set the op_pin_state_pinned flag in the graph right when the
5603  * optimization is performed.  The flag turning on procedure global cse could
5604  * be changed between two allocations.  This way we are safe.
5605  *
5606  * @param value_table  The value table
5607  * @param n            The node to lookup
5608  */
5609 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5610         ir_node *old = n;
5611
5612         n = identify(value_table, n);
5613         if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5614                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5615         return n;
5616 }  /* identify_cons */
5617
5618 /*
5619  * Return the canonical node computing the same value as n.
5620  * Looks up the node in a hash table, enters it in the table
5621  * if it isn't there yet.
5622  *
5623  * @param value_table  the HashSet containing all nodes in the
5624  *                     current IR graph
5625  * @param n            the node to look up
5626  *
5627  * @return a node that computes the same value as n or n if no such
5628  *         node could be found
5629  */
5630 ir_node *identify_remember(pset *value_table, ir_node *n) {
5631         ir_node *o = NULL;
5632
5633         if (!value_table) return n;
5634
5635         normalize_node(n);
5636         /* lookup or insert in hash table with given hash key. */
5637         o = pset_insert(value_table, n, ir_node_hash(n));
5638
5639         if (o != n) {
5640                 update_known_irn(o, n);
5641                 DBG_OPT_CSE(n, o);
5642         }
5643
5644         return o;
5645 }  /* identify_remember */
5646
5647 /* Add a node to the identities value table. */
5648 void add_identities(pset *value_table, ir_node *node) {
5649         if (get_opt_cse() && is_no_Block(node))
5650                 identify_remember(value_table, node);
5651 }  /* add_identities */
5652
5653 /* Visit each node in the value table of a graph. */
5654 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5655         ir_node *node;
5656         ir_graph *rem = current_ir_graph;
5657
5658         current_ir_graph = irg;
5659         foreach_pset(irg->value_table, node)
5660                 visit(node, env);
5661         current_ir_graph = rem;
5662 }  /* visit_all_identities */
5663
5664 /**
5665  * Garbage in, garbage out. If a node has a dead input, i.e., the
5666  * Bad node is input to the node, return the Bad node.
5667  */
5668 static ir_node *gigo(ir_node *node) {
5669         int i, irn_arity;
5670         ir_op *op = get_irn_op(node);
5671
5672         /* remove garbage blocks by looking at control flow that leaves the block
5673            and replacing the control flow by Bad. */
5674         if (get_irn_mode(node) == mode_X) {
5675                 ir_node *block = get_nodes_block(skip_Proj(node));
5676
5677                 /* Don't optimize nodes in immature blocks. */
5678                 if (!get_Block_matured(block))
5679                         return node;
5680                 /* Don't optimize End, may have Bads. */
5681                 if (op == op_End) return node;
5682
5683                 if (is_Block(block)) {
5684                         if (is_Block_dead(block)) {
5685                                 /* control flow from dead block is dead */
5686                                 return new_Bad();
5687                         }
5688
5689                         for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5690                                 if (!is_Bad(get_irn_n(block, i)))
5691                                         break;
5692                         }
5693                         if (i < 0) {
5694                                 ir_graph *irg = get_irn_irg(block);
5695                                 /* the start block is never dead */
5696                                 if (block != get_irg_start_block(irg)
5697                                         && block != get_irg_end_block(irg)) {
5698                                         /*
5699                                          * Do NOT kill control flow without setting
5700                                          * the block to dead of bad things can happen:
5701                                          * We get a Block that is not reachable be irg_block_walk()
5702                                          * but can be found by irg_walk()!
5703                                          */
5704                                         set_Block_dead(block);
5705                                         return new_Bad();
5706                                 }
5707                         }
5708                 }
5709         }
5710
5711         /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5712            blocks predecessors is dead. */
5713         if (op != op_Block && op != op_Phi && op != op_Tuple) {
5714                 irn_arity = get_irn_arity(node);
5715
5716                 /*
5717                  * Beware: we can only read the block of a non-floating node.
5718                  */
5719                 if (is_irn_pinned_in_irg(node) &&
5720                         is_Block_dead(get_nodes_block(skip_Proj(node))))
5721                         return new_Bad();
5722
5723                 for (i = 0; i < irn_arity; i++) {
5724                         ir_node *pred = get_irn_n(node, i);
5725
5726                         if (is_Bad(pred))
5727                                 return new_Bad();
5728 #if 0
5729                         /* Propagating Unknowns here seems to be a bad idea, because
5730                            sometimes we need a node as a input and did not want that
5731                            it kills it's user.
5732                            However, it might be useful to move this into a later phase
5733                            (if you think that optimizing such code is useful). */
5734                         if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5735                                 return new_Unknown(get_irn_mode(node));
5736 #endif
5737                 }
5738         }
5739 #if 0
5740         /* With this code we violate the agreement that local_optimize
5741            only leaves Bads in Block, Phi and Tuple nodes. */
5742         /* If Block has only Bads as predecessors it's garbage. */
5743         /* If Phi has only Bads as predecessors it's garbage. */
5744         if ((op == op_Block && get_Block_matured(node)) || op == op_Phi)  {
5745                 irn_arity = get_irn_arity(node);
5746                 for (i = 0; i < irn_arity; i++) {
5747                         if (!is_Bad(get_irn_n(node, i))) break;
5748                 }
5749                 if (i == irn_arity) node = new_Bad();
5750         }
5751 #endif
5752         return node;
5753 }  /* gigo */
5754
5755 /**
5756  * These optimizations deallocate nodes from the obstack.
5757  * It can only be called if it is guaranteed that no other nodes
5758  * reference this one, i.e., right after construction of a node.
5759  *
5760  * @param n   The node to optimize
5761  *
5762  * current_ir_graph must be set to the graph of the node!
5763  */
5764 ir_node *optimize_node(ir_node *n) {
5765         tarval *tv;
5766         ir_node *oldn = n;
5767         ir_opcode iro = get_irn_opcode(n);
5768
5769         /* Always optimize Phi nodes: part of the construction. */
5770         if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5771
5772         /* constant expression evaluation / constant folding */
5773         if (get_opt_constant_folding()) {
5774                 /* neither constants nor Tuple values can be evaluated */
5775                 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5776                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5777                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5778                         /* try to evaluate */
5779                         tv = computed_value(n);
5780                         if (tv != tarval_bad) {
5781                                 ir_node *nw;
5782                                 ir_type *old_tp = get_irn_type(n);
5783                                 int i, arity = get_irn_arity(n);
5784                                 int node_size;
5785
5786                                 /*
5787                                  * Try to recover the type of the new expression.
5788                                  */
5789                                 for (i = 0; i < arity && !old_tp; ++i)
5790                                         old_tp = get_irn_type(get_irn_n(n, i));
5791
5792                                 /*
5793                                  * we MUST copy the node here temporary, because it's still needed
5794                                  * for DBG_OPT_CSTEVAL
5795                                  */
5796                                 node_size = offsetof(ir_node, attr) +  n->op->attr_size;
5797                                 oldn = alloca(node_size);
5798
5799                                 memcpy(oldn, n, node_size);
5800                                 CLONE_ARR_A(ir_node *, oldn->in, n->in);
5801
5802                                 /* ARG, copy the in array, we need it for statistics */
5803                                 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
5804
5805                                 /* note the inplace edges module */
5806                                 edges_node_deleted(n, current_ir_graph);
5807
5808                                 /* evaluation was successful -- replace the node. */
5809                                 irg_kill_node(current_ir_graph, n);
5810                                 nw = new_Const(get_tarval_mode(tv), tv);
5811
5812                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5813                                         set_Const_type(nw, old_tp);
5814                                 DBG_OPT_CSTEVAL(oldn, nw);
5815                                 tarval_enable_fp_ops(old_fp_mode);
5816                                 return nw;
5817                         }
5818                         tarval_enable_fp_ops(old_fp_mode);
5819                 }
5820         }
5821
5822         /* remove unnecessary nodes */
5823         if (get_opt_constant_folding() ||
5824             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5825             (iro == iro_Id)   ||
5826             (iro == iro_Proj) ||
5827             (iro == iro_Block)  )  /* Flags tested local. */
5828                 n = equivalent_node(n);
5829
5830         /* Common Subexpression Elimination.
5831          *
5832          * Checks whether n is already available.
5833          * The block input is used to distinguish different subexpressions. Right
5834          * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
5835          * subexpressions within a block.
5836          */
5837         if (get_opt_cse())
5838                 n = identify_cons(current_ir_graph->value_table, n);
5839
5840         if (n != oldn) {
5841                 edges_node_deleted(oldn, current_ir_graph);
5842
5843                 /* We found an existing, better node, so we can deallocate the old node. */
5844                 irg_kill_node(current_ir_graph, oldn);
5845                 return n;
5846         }
5847
5848         /* Some more constant expression evaluation that does not allow to
5849            free the node. */
5850         iro = get_irn_opcode(n);
5851         if (get_opt_constant_folding() ||
5852             (iro == iro_Cond) ||
5853             (iro == iro_Proj))     /* Flags tested local. */
5854                 n = transform_node(n);
5855
5856         /* Remove nodes with dead (Bad) input.
5857            Run always for transformation induced Bads. */
5858         n = gigo (n);
5859
5860         /* Now we have a legal, useful node. Enter it in hash table for CSE */
5861         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
5862                 n = identify_remember(current_ir_graph->value_table, n);
5863         }
5864
5865         return n;
5866 }  /* optimize_node */
5867
5868
5869 /**
5870  * These optimizations never deallocate nodes (in place).  This can cause dead
5871  * nodes lying on the obstack.  Remove these by a dead node elimination,
5872  * i.e., a copying garbage collection.
5873  */
5874 ir_node *optimize_in_place_2(ir_node *n) {
5875         tarval *tv;
5876         ir_node *oldn = n;
5877         ir_opcode iro = get_irn_opcode(n);
5878
5879         if (!get_opt_optimize() && !is_Phi(n)) return n;
5880
5881         /* constant expression evaluation / constant folding */
5882         if (get_opt_constant_folding()) {
5883                 /* neither constants nor Tuple values can be evaluated */
5884                 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5885                         unsigned fp_model = get_irg_fp_model(current_ir_graph);
5886                         int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5887                         /* try to evaluate */
5888                         tv = computed_value(n);
5889                         if (tv != tarval_bad) {
5890                                 /* evaluation was successful -- replace the node. */
5891                                 ir_type *old_tp = get_irn_type(n);
5892                                 int i, arity = get_irn_arity(n);
5893
5894                                 /*
5895                                  * Try to recover the type of the new expression.
5896                                  */
5897                                 for (i = 0; i < arity && !old_tp; ++i)
5898                                         old_tp = get_irn_type(get_irn_n(n, i));
5899
5900                                 n = new_Const(get_tarval_mode(tv), tv);
5901
5902                                 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5903                                         set_Const_type(n, old_tp);
5904
5905                                 DBG_OPT_CSTEVAL(oldn, n);
5906                                 tarval_enable_fp_ops(old_fp_mode);
5907                                 return n;
5908                         }
5909                         tarval_enable_fp_ops(old_fp_mode);
5910                 }
5911         }
5912
5913         /* remove unnecessary nodes */
5914         if (get_opt_constant_folding() ||
5915             (iro == iro_Phi)  ||   /* always optimize these nodes. */
5916             (iro == iro_Id)   ||   /* ... */
5917             (iro == iro_Proj) ||   /* ... */
5918             (iro == iro_Block)  )  /* Flags tested local. */
5919                 n = equivalent_node(n);
5920
5921         /** common subexpression elimination **/
5922         /* Checks whether n is already available. */
5923         /* The block input is used to distinguish different subexpressions.  Right
5924            now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5925            subexpressions within a block. */
5926         if (get_opt_cse()) {
5927                 n = identify(current_ir_graph->value_table, n);
5928         }
5929
5930         /* Some more constant expression evaluation. */
5931         iro = get_irn_opcode(n);
5932         if (get_opt_constant_folding() ||
5933                 (iro == iro_Cond) ||
5934                 (iro == iro_Proj))     /* Flags tested local. */
5935                 n = transform_node(n);
5936
5937         /* Remove nodes with dead (Bad) input.
5938            Run always for transformation induced Bads.  */
5939         n = gigo(n);
5940
5941         /* Now we can verify the node, as it has no dead inputs any more. */
5942         irn_vrfy(n);
5943
5944         /* Now we have a legal, useful node. Enter it in hash table for cse.
5945            Blocks should be unique anyways.  (Except the successor of start:
5946            is cse with the start block!) */
5947         if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5948                 n = identify_remember(current_ir_graph->value_table, n);
5949
5950         return n;
5951 }  /* optimize_in_place_2 */
5952
5953 /**
5954  * Wrapper for external use, set proper status bits after optimization.
5955  */
5956 ir_node *optimize_in_place(ir_node *n) {
5957         /* Handle graph state */
5958         assert(get_irg_phase_state(current_ir_graph) != phase_building);
5959
5960         if (get_opt_global_cse())
5961                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5962         if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5963                 set_irg_outs_inconsistent(current_ir_graph);
5964
5965         /* FIXME: Maybe we could also test whether optimizing the node can
5966            change the control graph. */
5967         set_irg_doms_inconsistent(current_ir_graph);
5968         return optimize_in_place_2(n);
5969 }  /* optimize_in_place */
5970
5971 /*
5972  * Sets the default operation for an ir_ops.
5973  */
5974 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5975         ops = firm_set_default_computed_value(code, ops);
5976         ops = firm_set_default_equivalent_node(code, ops);
5977         ops = firm_set_default_transform_node(code, ops);
5978         ops = firm_set_default_node_cmp_attr(code, ops);
5979         ops = firm_set_default_get_type(code, ops);
5980         ops = firm_set_default_get_type_attr(code, ops);
5981         ops = firm_set_default_get_entity_attr(code, ops);
5982
5983         return ops;
5984 }  /* firm_set_default_operations */