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