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